1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
/*=========================================================================
Program: Visualization Toolkit
Module: TestPolygon.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
// .NAME
// .SECTION Description
// this program tests the Polygon
#include "vtkPolygon.h"
#include "vtkPoints.h"
#include <limits>
#include "vtkSmartPointer.h"
#include "vtkIdTypeArray.h"
template<class A>
bool fuzzyCompare(A a, A b) {
return fabs(a - b) < std::numeric_limits<A>::epsilon();
}
int TestPolygon(int,char *[])
{
vtkSmartPointer<vtkPolygon> polygon = vtkSmartPointer<vtkPolygon>::New();
polygon->GetPointIds()->SetNumberOfIds(4);
polygon->GetPointIds()->SetId(0,0);
polygon->GetPointIds()->SetId(1,1);
polygon->GetPointIds()->SetId(2,2);
polygon->GetPointIds()->SetId(3,3);
polygon->GetPoints()->SetNumberOfPoints(4);
polygon->GetPoints()->SetPoint(0, 0.0, 0.0, 0.0);
polygon->GetPoints()->SetPoint(1, 2.0, 0.0, 0.0);
polygon->GetPoints()->SetPoint(2, 2.0, 2.0, 0.0);
polygon->GetPoints()->SetPoint(3, 0.0, 2.0, 0.0);
double area = polygon->ComputeArea();
if(!fuzzyCompare(area, 4.0))
{
cerr << "ERROR: polygon area is " << area << ", should be 4.0" << endl;
return EXIT_FAILURE;
}
//////// Test Normal : void vtkPolygon::ComputeNormal (int numPts, double *pts, double n[3]) ///////////
double normal[3];
double points[12];
for(int i = 0; i < polygon->GetNumberOfPoints(); i++)
{
double p[3];
polygon->GetPoints()->GetPoint(i,p);
points[0 + i*3] = p[0];
points[1 + i*3] = p[1];
points[2 + i*3] = p[2];
}
vtkPolygon::ComputeNormal(polygon->GetNumberOfPoints(), points, normal);
if(!fuzzyCompare(normal[0], 0.0) || !fuzzyCompare(normal[1], 0.0) || !fuzzyCompare(normal[2], 1.0))
{
cerr << "ERROR: The normal (" << normal[0] << ", " << normal[1] << ", " << normal[2] << " is incorrect (should be (0,0,1))" << endl;
return EXIT_FAILURE;
}
///////// Test Normal : void vtkPolygon::ComputeNormal(vtkIdTypeArray *ids, vtkPoints *p, double n[3]) /////////
vtkSmartPointer<vtkIdTypeArray> idArray = vtkSmartPointer<vtkIdTypeArray>::New();
for(int i = 0; i < polygon->GetNumberOfPoints(); i++)
{
idArray->InsertNextValue(i);
}
vtkPolygon::ComputeNormal(idArray, polygon->GetPoints(), normal);
if(!fuzzyCompare(normal[0], 0.0) || !fuzzyCompare(normal[1], 0.0) || !fuzzyCompare(normal[2], 1.0))
{
cerr << "ERROR: The normal (" << normal[0] << ", " << normal[1] << ", " << normal[2] << " is incorrect (should be (0,0,1))" << endl;
return EXIT_FAILURE;
}
//////////////////// Polygon intersection test //////////////////
{
vtkSmartPointer<vtkPolygon> polygon1 = vtkSmartPointer<vtkPolygon>::New();
polygon1->GetPointIds()->SetNumberOfIds(4);
polygon1->GetPointIds()->SetId(0,0);
polygon1->GetPointIds()->SetId(1,1);
polygon1->GetPointIds()->SetId(2,2);
polygon1->GetPointIds()->SetId(3,3);
polygon1->GetPoints()->SetNumberOfPoints(4);
polygon1->GetPoints()->SetPoint(0, 0.0, -1.0, -1.0);
polygon1->GetPoints()->SetPoint(1, 0.0, 1.0, -1.0);
polygon1->GetPoints()->SetPoint(2, 0.0, 1.0, 1.0);
polygon1->GetPoints()->SetPoint(3, 0.0, -1.0, 1.0);
double points1[12];
for(int i = 0; i < polygon1->GetNumberOfPoints(); i++)
{
double p[3];
polygon1->GetPoints()->GetPoint(i,p);
points1[0 + i*3] = p[0];
points1[1 + i*3] = p[1];
points1[2 + i*3] = p[2];
}
double bounds1[6];
polygon1->GetBounds(bounds1);
vtkSmartPointer<vtkPolygon> polygon2 = vtkSmartPointer<vtkPolygon>::New();
polygon2->GetPointIds()->SetNumberOfIds(4);
polygon2->GetPointIds()->SetId(0,0);
polygon2->GetPointIds()->SetId(1,1);
polygon2->GetPointIds()->SetId(2,2);
polygon2->GetPointIds()->SetId(3,3);
polygon2->GetPoints()->SetNumberOfPoints(4);
polygon2->GetPoints()->SetPoint(0, 1.0, -1.0, 0.0);
polygon2->GetPoints()->SetPoint(1, 1.0, 1.0, 0.0);
polygon2->GetPoints()->SetPoint(2, -1.0, 1.0, 0.0);
polygon2->GetPoints()->SetPoint(3, -1.0, -1.0, 0.0);
double points2[12];
for(int i = 0; i < polygon2->GetNumberOfPoints(); i++)
{
double p[3];
polygon2->GetPoints()->GetPoint(i,p);
points2[0 + i*3] = p[0];
points2[1 + i*3] = p[1];
points2[2 + i*3] = p[2];
}
double bounds2[6];
polygon2->GetBounds(bounds2);
/*
int vtkPolygon::IntersectPolygonWithPolygon(int npts, double *pts,double bounds[6],
int npts2, double *pts2,
double bounds2[6], double tol2,
double x[3])
*/
double intersection[3];
int intersect = vtkPolygon::IntersectPolygonWithPolygon(static_cast<int>(polygon1->GetNumberOfPoints()), points1, bounds1,
static_cast<int>(polygon2->GetNumberOfPoints()), points2, bounds2,
1e-6, intersection);
if(!intersect)
{
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
|