1 /*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: TestPolygon.cxx
5
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 All rights reserved.
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
13
14 =========================================================================*/
15
16 // .NAME
17 // .SECTION Description
18 // this program tests the BoundedTriangulate method in Polygon
19
20 #include "vtkIdList.h"
21 #include "vtkNew.h"
22 #include "vtkPolygon.h"
23 #include "vtkPoints.h"
24
25 // #define VISUAL_DEBUG 1
26
27 #ifdef VISUAL_DEBUG
28 #include <vtkCellArray.h>
29 #include <vtkPolyData.h>
30 #include <vtkPolyDataMapper.h>
31 #include <vtkProperty.h>
32 #include <vtkActor.h>
33 #include <vtkRenderWindow.h>
34 #include <vtkRenderer.h>
35 #include <vtkRenderWindowInteractor.h>
36 #endif
37
38 #include <vector>
39
ValidTessellation(vtkPolygon * polygon,vtkIdList * outTris)40 bool ValidTessellation(vtkPolygon* polygon, vtkIdList* outTris)
41 {
42 // Check that there are enough triangles
43 if (outTris->GetNumberOfIds() / 3 != polygon->GetNumberOfPoints() - 2)
44 {
45 return false;
46 }
47
48 // Check that all of the edges of the polygon are represented
49 std::vector<bool> edges(polygon->GetNumberOfPoints(), false);
50
51 for (int i=0; i<polygon->GetNumberOfPoints(); i++)
52 {
53 vtkIdType edge[2] = { polygon->GetPointId(i),
54 polygon->GetPointId((i+1)%polygon->GetNumberOfPoints()) };
55 for (int j=0; j<outTris->GetNumberOfIds(); j+=3)
56 {
57 for (int k=0;k<3;k++)
58 {
59 vtkIdType triedge[2] =
60 { polygon->PointIds->GetId(outTris->GetId(j+k)),
61 polygon->PointIds->GetId(outTris->GetId(j+((k+1)%3))) };
62
63 if ((triedge[0] == edge[0] && triedge[1] == edge[1]) ||
64 (triedge[0] == edge[1] && triedge[1] == edge[0]))
65 {
66 edges[i] = true;
67 break;
68 }
69 }
70 if (edges[i] == true)
71 break;
72 }
73 if (edges[i] == false)
74 break;
75 }
76
77 for (std::size_t i=0; i<edges.size(); i++)
78 {
79 if (!edges[i])
80 {
81 return false;
82 }
83 }
84
85 return true;
86 }
87
TestPolygonBoundedTriangulate(int,char * [])88 int TestPolygonBoundedTriangulate(int,char *[])
89 {
90 vtkNew<vtkPolygon> polygon;
91
92 polygon->GetPoints()->InsertNextPoint(125.703, 149.84, 45.852);
93 polygon->GetPoints()->InsertNextPoint(126.438, 147.984, 44.3112);
94 polygon->GetPoints()->InsertNextPoint(126.219, 148.174, 44.4463);
95 polygon->GetPoints()->InsertNextPoint(126.196, 148.202, 44.4683);
96 polygon->GetPoints()->InsertNextPoint(126.042, 148.398, 44.6184);
97 polygon->GetPoints()->InsertNextPoint(125.854, 148.635, 44.8);
98 polygon->GetPoints()->InsertNextPoint(125.598, 148.958, 45.0485);
99 polygon->GetPoints()->InsertNextPoint(125.346, 149.24, 45.26);
100 polygon->GetPoints()->InsertNextPoint(125.124, 149.441, 45.4041);
101
102 polygon->GetPointIds()->SetNumberOfIds(polygon->GetPoints()->GetNumberOfPoints());
103 for (vtkIdType i = 0; i < polygon->GetPoints()->GetNumberOfPoints(); i++)
104 {
105 polygon->GetPointIds()->SetId(i,i);
106 }
107
108 vtkNew<vtkIdList> outTris;
109
110 int success = polygon->BoundedTriangulate(outTris, 1.e-2);
111
112 if (!success || !ValidTessellation(polygon, outTris))
113 {
114 cerr << "ERROR: vtkPolygon::BoundedTriangulate should triangulate this polygon" << endl;
115 return EXIT_FAILURE;
116 }
117
118 #ifdef VISUAL_DEBUG
119 vtkNew<vtkCellArray> triangles;
120 for (vtkIdType i=0; i < outTris->GetNumberOfIds(); i+=3)
121 {
122 vtkIdType t[3] = { outTris->GetId(i),
123 outTris->GetId(i+1),
124 outTris->GetId(i+2) };
125 triangles->InsertNextCell(3, t);
126 }
127
128 vtkNew<vtkPolyData> polydata;
129 polydata->SetPoints(polygon->GetPoints());
130 polydata->SetPolys(triangles);
131
132 vtkNew<vtkPolyDataMapper> mapper;
133 mapper->SetInputData(polydata);
134
135 vtkNew<vtkActor> actor;
136 actor->SetMapper(mapper);
137 actor->GetProperty()->SetRepresentationToWireframe();
138
139 // Create a renderer, render window, and an interactor
140 vtkNew<vtkRenderer> renderer;
141 vtkNew<vtkRenderWindow> renderWindow;
142 renderWindow->AddRenderer(renderer);
143 vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
144 renderWindowInteractor->SetRenderWindow(renderWindow);
145
146 // Add the actors to the scene
147 renderer->AddActor(actor);
148 renderer->SetBackground(.1, .2, .4);
149
150 // Render and interact
151 renderWindow->Render();
152 renderWindowInteractor->Start();
153 #endif
154
155 return EXIT_SUCCESS;
156 }
157