1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 // This software is distributed WITHOUT ANY WARRANTY; without even
6 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
7 // PURPOSE. See the above copyright notice for more information.
8 //
9 // Copyright 2014 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
10 // Copyright 2014 UT-Battelle, LLC.
11 // Copyright 2014 Los Alamos National Security.
12 //
13 // Under the terms of Contract DE-NA0003525 with NTESS,
14 // the U.S. Government retains certain rights in this software.
15 //
16 // Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
17 // Laboratory (LANL), the U.S. Government retains certain rights in
18 // this software.
19 //============================================================================
20
21 #ifndef VTKM_DEVICE_ADAPTER
22 #define VTKM_DEVICE_ADAPTER VTKM_DEVICE_ADAPTER_SERIAL
23 #endif
24
25 #include <vtkm/cont/CellSetExplicit.h>
26 #include <vtkm/cont/DataSet.h>
27 #include <vtkm/cont/DataSetBuilderExplicit.h>
28 #include <vtkm/filter/Triangulate.h>
29
30 #include <vtkm/cont/testing/Testing.h>
31
32 //Suppress warnings about glut being deprecated on OSX
33 #if (defined(VTKM_GCC) || defined(VTKM_CLANG))
34 #pragma GCC diagnostic push
35 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
36 #endif
37
38 #if defined(__APPLE__)
39 #include <GLUT/glut.h>
40 #else
41 #include <GL/glut.h>
42 #endif
43
44 using DeviceAdapter = VTKM_DEFAULT_DEVICE_ADAPTER_TAG;
45
46 namespace
47 {
48
49 // Takes input uniform grid and outputs unstructured grid of triangles
50 static vtkm::cont::DataSet outDataSet;
51 static vtkm::Id numberOfInPoints;
52
53 } // anonymous namespace
54
55 //
56 // Construct an input data set with uniform grid of indicated dimensions, origin and spacing
57 //
MakeTriangulateExplicitDataSet()58 vtkm::cont::DataSet MakeTriangulateExplicitDataSet()
59 {
60 vtkm::cont::DataSetBuilderExplicitIterative builder;
61 builder.Begin();
62
63 builder.AddPoint(0, 0, 0); // 0
64 builder.AddPoint(1, 0, 0); // 1
65 builder.AddPoint(2, 0, 0); // 2
66 builder.AddPoint(3, 0, 0); // 3
67 builder.AddPoint(0, 1, 0); // 4
68 builder.AddPoint(1, 1, 0); // 5
69 builder.AddPoint(2, 1, 0); // 6
70 builder.AddPoint(3, 1, 0); // 7
71 builder.AddPoint(0, 2, 0); // 8
72 builder.AddPoint(1, 2, 0); // 9
73 builder.AddPoint(2, 2, 0); // 10
74 builder.AddPoint(3, 2, 0); // 11
75 builder.AddPoint(0, 3, 0); // 12
76 builder.AddPoint(3, 3, 0); // 13
77 builder.AddPoint(1, 4, 0); // 14
78 builder.AddPoint(2, 4, 0); // 15
79
80 builder.AddCell(vtkm::CELL_SHAPE_TRIANGLE);
81 builder.AddCellPoint(0);
82 builder.AddCellPoint(1);
83 builder.AddCellPoint(5);
84
85 builder.AddCell(vtkm::CELL_SHAPE_QUAD);
86 builder.AddCellPoint(1);
87 builder.AddCellPoint(2);
88 builder.AddCellPoint(6);
89 builder.AddCellPoint(5);
90
91 builder.AddCell(vtkm::CELL_SHAPE_QUAD);
92 builder.AddCellPoint(5);
93 builder.AddCellPoint(6);
94 builder.AddCellPoint(10);
95 builder.AddCellPoint(9);
96
97 builder.AddCell(vtkm::CELL_SHAPE_QUAD);
98 builder.AddCellPoint(4);
99 builder.AddCellPoint(5);
100 builder.AddCellPoint(9);
101 builder.AddCellPoint(8);
102
103 builder.AddCell(vtkm::CELL_SHAPE_TRIANGLE);
104 builder.AddCellPoint(2);
105 builder.AddCellPoint(3);
106 builder.AddCellPoint(7);
107
108 builder.AddCell(vtkm::CELL_SHAPE_QUAD);
109 builder.AddCellPoint(6);
110 builder.AddCellPoint(7);
111 builder.AddCellPoint(11);
112 builder.AddCellPoint(10);
113
114 builder.AddCell(vtkm::CELL_SHAPE_POLYGON);
115 builder.AddCellPoint(9);
116 builder.AddCellPoint(10);
117 builder.AddCellPoint(13);
118 builder.AddCellPoint(15);
119 builder.AddCellPoint(14);
120 builder.AddCellPoint(12);
121
122 return builder.Create();
123 }
124
125 //
126 // Initialize the OpenGL state
127 //
initializeGL()128 void initializeGL()
129 {
130 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
131 glMatrixMode(GL_PROJECTION);
132 glLoadIdentity();
133 glOrtho(-0.5f, 3.5f, -0.5f, 4.5f, -1.0f, 1.0f);
134 }
135
136 //
137 // Render the output using simple OpenGL
138 //
displayCall()139 void displayCall()
140 {
141 glClear(GL_COLOR_BUFFER_BIT);
142 glLineWidth(3.0f);
143
144 // Get cell set and the number of cells and vertices
145 vtkm::cont::CellSetSingleType<> cellSet;
146 outDataSet.GetCellSet(0).CopyTo(cellSet);
147 vtkm::Id numberOfCells = cellSet.GetNumberOfCells();
148
149 auto vertexArray = outDataSet.GetCoordinateSystem().GetData();
150
151 // Draw the two triangles belonging to each quad
152 vtkm::Float32 color[4][3] = {
153 { 1.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, { 0.0f, 0.0f, 1.0f }, { 1.0f, 1.0f, 0.0f }
154 };
155
156 for (vtkm::Id triangle = 0; triangle < numberOfCells; triangle++)
157 {
158 vtkm::Id indx = triangle % 4;
159 glColor3f(color[indx][0], color[indx][1], color[indx][2]);
160
161 // Get the indices of the vertices that make up this triangle
162 vtkm::Vec<vtkm::Id, 3> triIndices;
163 cellSet.GetIndices(triangle, triIndices);
164
165 // Get the vertex points for this triangle
166 vtkm::Vec<vtkm::Float64, 3> pt0 = vertexArray.GetPortalConstControl().Get(triIndices[0]);
167 vtkm::Vec<vtkm::Float64, 3> pt1 = vertexArray.GetPortalConstControl().Get(triIndices[1]);
168 vtkm::Vec<vtkm::Float64, 3> pt2 = vertexArray.GetPortalConstControl().Get(triIndices[2]);
169
170 // Draw the triangle filled with alternating colors
171 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
172 glBegin(GL_TRIANGLES);
173 glVertex3d(pt0[0], pt0[1], pt0[2]);
174 glVertex3d(pt1[0], pt1[1], pt1[2]);
175 glVertex3d(pt2[0], pt2[1], pt2[2]);
176 glEnd();
177 }
178 glFlush();
179 }
180
181 // Triangulate and render explicit grid example
main(int argc,char * argv[])182 int main(int argc, char* argv[])
183 {
184 std::cout << "TrianguleExplicitGrid Example" << std::endl;
185
186 // Create the input uniform cell set
187 vtkm::cont::DataSet inDataSet = MakeTriangulateExplicitDataSet();
188 vtkm::cont::CellSetExplicit<> inCellSet;
189 inDataSet.GetCellSet(0).CopyTo(inCellSet);
190
191 numberOfInPoints = inCellSet.GetNumberOfPoints();
192
193 // Convert 2D explicit cells to triangles
194 vtkm::filter::Triangulate triangulate;
195 outDataSet = triangulate.Execute(inDataSet);
196
197 // Render the output dataset of tets
198 glutInit(&argc, argv);
199 glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
200 glutInitWindowSize(1000, 1000);
201 glutInitWindowPosition(100, 100);
202
203 glutCreateWindow("VTK-m Explicit Triangulate");
204
205 initializeGL();
206
207 glutDisplayFunc(displayCall);
208 glutMainLoop();
209
210 outDataSet.Clear();
211 return 0;
212 }
213
214 #if (defined(VTKM_GCC) || defined(VTKM_CLANG))
215 #pragma GCC diagnostic pop
216 #endif
217