1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    BoxClipTriangulateAndInterpolate.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 /*----------------------------------------------------------------------------
17  Copyright (c) Sandia Corporation
18  See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
19 ----------------------------------------------------------------------------*/
20 
21 // This code tests for the case when vtkBoxClipDataSet is given a collection of
22 // cells that it must triangulate and interpolate.  At one time there was a bug
23 // that sent the wrong indices for interpolating in this case.
24 
25 #include "vtkActor.h"
26 #include "vtkBoxClipDataSet.h"
27 #include "vtkCellArray.h"
28 #include "vtkDataSetSurfaceFilter.h"
29 #include "vtkDoubleArray.h"
30 #include "vtkPointData.h"
31 #include "vtkPolyDataMapper.h"
32 #include "vtkProperty.h"
33 #include "vtkRenderer.h"
34 #include "vtkRenderWindow.h"
35 #include "vtkRenderWindowInteractor.h"
36 #include "vtkUnstructuredGrid.h"
37 
38 #include "vtkSmartPointer.h"
39 #define VTK_CREATE(type, var)                                   \
40   vtkSmartPointer<type> var = vtkSmartPointer<type>::New()
41 
42 const int NumImagesX = 6;
43 const int NumImagesY = 2;
44 
CreateHex(vtkUnstructuredGrid * hex)45 static void CreateHex(vtkUnstructuredGrid *hex)
46 {
47   VTK_CREATE(vtkPoints, points);
48   points->Allocate(24);
49   points->InsertNextPoint(-0.5, -0.5, -0.5);
50   points->InsertNextPoint(0.5, -0.5, -0.5);
51   points->InsertNextPoint(0.5, 0.5, -0.5);
52   points->InsertNextPoint(-0.5, 0.5, -0.5);
53   points->InsertNextPoint(-0.5, -0.5, 0.5);
54   points->InsertNextPoint(0.5, -0.5, 0.5);
55   points->InsertNextPoint(0.5, 0.5, 0.5);
56   points->InsertNextPoint(-0.5, 0.5, 0.5);
57   hex->SetPoints(points);
58 
59   VTK_CREATE(vtkCellArray, cells);
60   cells->Allocate(8);
61   cells->InsertNextCell(8);
62   cells->InsertCellPoint(0);
63   cells->InsertCellPoint(1);
64   cells->InsertCellPoint(2);
65   cells->InsertCellPoint(3);
66   cells->InsertCellPoint(4);
67   cells->InsertCellPoint(5);
68   cells->InsertCellPoint(6);
69   cells->InsertCellPoint(7);
70   hex->SetCells(VTK_HEXAHEDRON, cells);
71 
72   VTK_CREATE(vtkDoubleArray, data);
73   data->SetName("data");
74   data->SetNumberOfComponents(1);
75   data->SetNumberOfTuples(8);
76   data->SetValue(0, 0.0);
77   data->SetValue(1, 0.0);
78   data->SetValue(2, 1.0);
79   data->SetValue(3, 1.0);
80   data->SetValue(4, 0.0);
81   data->SetValue(5, 0.0);
82   data->SetValue(6, 1.0);
83   data->SetValue(7, 1.0);
84   hex->GetPointData()->SetScalars(data);
85 }
86 
CreateQuad(vtkPolyData * quad)87 static void CreateQuad(vtkPolyData *quad)
88 {
89   VTK_CREATE(vtkPoints, points);
90   points->Allocate(12);
91   points->InsertNextPoint(-0.5, -0.5, 0.0);
92   points->InsertNextPoint(0.5, -0.5, 0.0);
93   points->InsertNextPoint(0.5, 0.5, 0.0);
94   points->InsertNextPoint(-0.5, 0.5, 0.0);
95   quad->SetPoints(points);
96 
97   VTK_CREATE(vtkCellArray, cells);
98   cells->Allocate(4);
99   cells->InsertNextCell(4);
100   cells->InsertCellPoint(0);
101   cells->InsertCellPoint(1);
102   cells->InsertCellPoint(2);
103   cells->InsertCellPoint(3);
104   quad->SetPolys(cells);
105 
106   VTK_CREATE(vtkDoubleArray, data);
107   data->SetName("data");
108   data->SetNumberOfComponents(1);
109   data->SetNumberOfTuples(4);
110   data->SetValue(0, 0.0);
111   data->SetValue(1, 0.0);
112   data->SetValue(2, 1.0);
113   data->SetValue(3, 1.0);
114   quad->GetPointData()->SetScalars(data);
115 }
116 
CreateLine(vtkPolyData * line)117 static void CreateLine(vtkPolyData *line)
118 {
119   VTK_CREATE(vtkPoints, points);
120   points->Allocate(12);
121   points->InsertNextPoint(0.0, -0.5, 0.0);
122   points->InsertNextPoint(0.0, -0.25, 0.0);
123   points->InsertNextPoint(0.0, 0.25, 0.0);
124   points->InsertNextPoint(0.0, 0.25, 0.0);
125   line->SetPoints(points);
126 
127   VTK_CREATE(vtkCellArray, cells);
128   cells->Allocate(4);
129   cells->InsertNextCell(4);
130   cells->InsertCellPoint(0);
131   cells->InsertCellPoint(1);
132   cells->InsertCellPoint(2);
133   cells->InsertCellPoint(3);
134   line->SetLines(cells);
135 
136   VTK_CREATE(vtkDoubleArray, data);
137   data->SetName("data");
138   data->SetNumberOfComponents(1);
139   data->SetNumberOfTuples(4);
140   data->SetValue(0, 0.0);
141   data->SetValue(1, 1.0);
142   data->SetValue(2, 1.0);
143   data->SetValue(3, 1.0);
144   line->GetPointData()->SetScalars(data);
145 }
146 
SetClipAsHexahedron(vtkBoxClipDataSet * clip,double xmin,double xmax,double ymin,double ymax,double zmin,double zmax)147 static void SetClipAsHexahedron(vtkBoxClipDataSet *clip,
148                                 double xmin, double xmax,
149                                 double ymin, double ymax,
150                                 double zmin, double zmax)
151 {
152   double lowPoint[3] = {xmin, ymin, zmin};
153   double highPoint[3] = {xmax, ymax, zmax};
154   const double negXVec[3] = {-1.0, 0.0, 0.0};
155   const double negYVec[3] = {0.0, -1.0, 0.0};
156   const double negZVec[3] = {0.0, 0.0, -1.0};
157   const double posXVec[3] = {1.0, 0.0, 0.0};
158   const double posYVec[3] = {0.0, 1.0, 0.0};
159   const double posZVec[3] = {0.0, 0.0, 1.0};
160 
161   clip->SetBoxClip(negXVec, lowPoint,
162                    negYVec, lowPoint,
163                    negZVec, lowPoint,
164                    posXVec, highPoint,
165                    posYVec, highPoint,
166                    posZVec, highPoint);
167 }
168 
AddToRenderWindow(vtkRenderWindow * renwin,vtkBoxClipDataSet * boxclip,int x=0,int y=0)169 static void AddToRenderWindow(vtkRenderWindow *renwin,
170                               vtkBoxClipDataSet *boxclip,
171                               int x = 0, int y = 0)
172 {
173   VTK_CREATE(vtkRenderer, renderer);
174   renderer->SetViewport(static_cast<double>(x)/NumImagesX,
175                         static_cast<double>(y)/NumImagesY,
176                         static_cast<double>(x+1)/NumImagesX,
177                         static_cast<double>(y+1)/NumImagesY);
178 
179   VTK_CREATE(vtkDataSetSurfaceFilter, surface1);
180   surface1->SetInputConnection(boxclip->GetOutputPort(0));
181 
182   VTK_CREATE(vtkPolyDataMapper, mapper1);
183   mapper1->SetInputConnection(surface1->GetOutputPort());
184   mapper1->InterpolateScalarsBeforeMappingOn();
185 
186   VTK_CREATE(vtkActor, actor1);
187   actor1->SetMapper(mapper1);
188   actor1->GetProperty()->SetLineWidth(10.0);
189   renderer->AddActor(actor1);
190 
191   VTK_CREATE(vtkDataSetSurfaceFilter, surface2);
192   surface2->SetInputConnection(boxclip->GetOutputPort(1));
193 
194   VTK_CREATE(vtkPolyDataMapper, mapper2);
195   mapper2->SetInputConnection(surface2->GetOutputPort());
196 
197   VTK_CREATE(vtkActor, actor2);
198   actor2->SetMapper(mapper2);
199   actor2->GetProperty()->SetLineWidth(10.0);
200   renderer->AddActor(actor2);
201 
202   renwin->AddRenderer(renderer);
203 }
204 
BoxClipTriangulateAndInterpolate(int,char * [])205 int BoxClipTriangulateAndInterpolate(int, char *[])
206 {
207   VTK_CREATE(vtkRenderWindow, renwin);
208   renwin->SetSize(600, 400);
209 
210   VTK_CREATE(vtkUnstructuredGrid, hex);
211   CreateHex(hex);
212 
213   VTK_CREATE(vtkPolyData, quad);
214   CreateQuad(quad);
215 
216   VTK_CREATE(vtkPolyData, line);
217   CreateLine(line);
218 
219   VTK_CREATE(vtkBoxClipDataSet, clip00);
220   clip00->SetInputData(hex);
221   clip00->SetBoxClip(0.0, 1.0, -1.0, 1.0, -1.0, 1.0);
222   AddToRenderWindow(renwin, clip00, 0, 0);
223 
224   VTK_CREATE(vtkBoxClipDataSet, clip01);
225   clip01->SetInputData(hex);
226   clip01->GenerateClippedOutputOn();
227   clip01->SetBoxClip(0.0, 1.0, -1.0, 1.0, -1.0, 1.0);
228   AddToRenderWindow(renwin, clip01, 0, 1);
229 
230   VTK_CREATE(vtkBoxClipDataSet, clip10);
231   clip10->SetInputData(hex);
232   SetClipAsHexahedron(clip10, 0.0, 1.0, -1.0, 1.0, -1.0, 1.0);
233   AddToRenderWindow(renwin, clip10, 1, 0);
234 
235   VTK_CREATE(vtkBoxClipDataSet, clip11);
236   clip11->SetInputData(hex);
237   clip11->GenerateClippedOutputOn();
238   SetClipAsHexahedron(clip11, 0.0, 1.0, -1.0, 1.0, -1.0, 1.0);
239   AddToRenderWindow(renwin, clip11, 1, 1);
240 
241   VTK_CREATE(vtkBoxClipDataSet, clip20);
242   clip20->SetInputData(quad);
243   clip20->SetBoxClip(0.0, 1.0, -1.0, 1.0, -1.0, 1.0);
244   AddToRenderWindow(renwin, clip20, 2, 0);
245 
246   VTK_CREATE(vtkBoxClipDataSet, clip21);
247   clip21->SetInputData(quad);
248   clip21->GenerateClippedOutputOn();
249   clip21->SetBoxClip(0.0, 1.0, -1.0, 1.0, -1.0, 1.0);
250   AddToRenderWindow(renwin, clip21, 2, 1);
251 
252   VTK_CREATE(vtkBoxClipDataSet, clip30);
253   clip30->SetInputData(quad);
254   SetClipAsHexahedron(clip30, 0.0, 1.0, -1.0, 1.0, -1.0, 1.0);
255   AddToRenderWindow(renwin, clip30, 3, 0);
256 
257   VTK_CREATE(vtkBoxClipDataSet, clip31);
258   clip31->SetInputData(quad);
259   clip31->GenerateClippedOutputOn();
260   SetClipAsHexahedron(clip31, 0.0, 1.0, -1.0, 1.0, -1.0, 1.0);
261   AddToRenderWindow(renwin, clip31, 3, 1);
262 
263   VTK_CREATE(vtkBoxClipDataSet, clip40);
264   clip40->SetInputData(line);
265   clip40->SetBoxClip(-1.0, 1.0, 0.0, 1.0, -1.0, 1.0);
266   AddToRenderWindow(renwin, clip40, 4, 0);
267 
268   VTK_CREATE(vtkBoxClipDataSet, clip41);
269   clip41->SetInputData(line);
270   clip41->GenerateClippedOutputOn();
271   clip41->SetBoxClip(-1.0, 1.0, 0.0, 1.0, -1.0, 1.0);
272   AddToRenderWindow(renwin, clip41, 4, 1);
273 
274   VTK_CREATE(vtkBoxClipDataSet, clip50);
275   clip50->SetInputData(line);
276   SetClipAsHexahedron(clip50, -1.0, 1.0, 0.0, 1.0, -1.0, 1.0);
277   AddToRenderWindow(renwin, clip50, 5, 0);
278 
279   VTK_CREATE(vtkBoxClipDataSet, clip51);
280   clip51->SetInputData(line);
281   clip51->GenerateClippedOutputOn();
282   SetClipAsHexahedron(clip51, -1.0, 1.0, 0.0, 1.0, -1.0, 1.0);
283   AddToRenderWindow(renwin, clip51, 5, 1);
284 
285   VTK_CREATE(vtkRenderWindowInteractor, iren);
286   iren->SetRenderWindow(renwin);
287   renwin->Render();
288   iren->Start();
289 
290   return EXIT_SUCCESS;
291 }
292