1 /*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: TestMeanValueCoordinatesInterpolation.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 #include "vtkActor.h"
16 #include "vtkCamera.h"
17 #include "vtkCellArray.h"
18 #include "vtkDebugLeaks.h"
19 #include "vtkDoubleArray.h"
20 #include "vtkLight.h"
21 #include "vtkLightCollection.h"
22 #include "vtkMath.h"
23 #include "vtkPlane.h"
24 #include "vtkPlaneSource.h"
25 #include "vtkPointData.h"
26 #include "vtkPolyData.h"
27 #include "vtkPolyDataMapper.h"
28 #include "vtkPolygon.h"
29 #include "vtkProbeFilter.h"
30 #include "vtkProperty.h"
31 #include "vtkRegressionTestImage.h"
32 #include "vtkRenderWindow.h"
33 #include "vtkRenderWindowInteractor.h"
34 #include "vtkRenderer.h"
35 #include "vtkSmartPointer.h"
36 #include "vtkUnstructuredGrid.h"
37
38 // Test MVC interpolation of polygon cell
TestMeanValueCoordinatesInterpolation2(int argc,char * argv[])39 int TestMeanValueCoordinatesInterpolation2(int argc, char* argv[])
40 {
41 vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
42 vtkSmartPointer<vtkRenderer> renderer1 = vtkSmartPointer<vtkRenderer>::New();
43 renderer->SetViewport(0, 0, 0.5, 1);
44
45 vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();
46 renWin->SetMultiSamples(0);
47 renWin->AddRenderer(renderer);
48 renWin->AddRenderer(renderer1);
49 renderer1->SetViewport(0.5, 0, 1, 1);
50
51 vtkSmartPointer<vtkRenderWindowInteractor> iren =
52 vtkSmartPointer<vtkRenderWindowInteractor>::New();
53 iren->SetRenderWindow(renWin);
54
55 //
56 // Case 0: convex pentagon
57 //
58 // create a regular pentagon
59 double pentagon[5][3];
60 for (int i = 0; i < 5; i++)
61 {
62 pentagon[i][0] = sin(vtkMath::RadiansFromDegrees(72.0 * i));
63 pentagon[i][1] = cos(vtkMath::RadiansFromDegrees(72.0 * i));
64 pentagon[i][2] = 0.0;
65 }
66
67 vtkSmartPointer<vtkCellArray> pentagonCell = vtkSmartPointer<vtkCellArray>::New();
68 pentagonCell->InsertNextCell(5);
69 for (vtkIdType i = 0; i < 5; i++)
70 {
71 pentagonCell->InsertCellPoint(i);
72 }
73
74 vtkSmartPointer<vtkPoints> pentagonPoints = vtkSmartPointer<vtkPoints>::New();
75 pentagonPoints->Initialize();
76 for (int i = 0; i < 5; i++)
77 {
78 pentagonPoints->InsertNextPoint(pentagon[i]);
79 }
80
81 vtkSmartPointer<vtkDoubleArray> pointDataArray = vtkSmartPointer<vtkDoubleArray>::New();
82 pointDataArray->Initialize();
83 for (int i = 0; i < 5; i++)
84 {
85 pointDataArray->InsertNextValue((pentagon[i][0] + 1.0) / 2.0);
86 }
87
88 vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
89 polydata->SetPoints(pentagonPoints);
90 polydata->SetPolys(pentagonCell);
91 polydata->GetPointData()->SetScalars(pointDataArray);
92
93 vtkPolygon* polygon = static_cast<vtkPolygon*>(polydata->GetCell(0));
94 polygon->SetUseMVCInterpolation(true);
95
96 // Okay now sample on a plane and see how it interpolates
97 vtkSmartPointer<vtkPlaneSource> pSource = vtkSmartPointer<vtkPlaneSource>::New();
98 pSource->SetOrigin(-1.0, -1.0, 0);
99 pSource->SetPoint1(1.0, -1.0, 0);
100 pSource->SetPoint2(-1.0, 1.0, 0);
101 pSource->SetXResolution(100);
102 pSource->SetYResolution(100);
103
104 // mvc interpolation
105 vtkSmartPointer<vtkProbeFilter> interp = vtkSmartPointer<vtkProbeFilter>::New();
106 interp->SetInputConnection(pSource->GetOutputPort());
107 interp->SetSourceData(polydata);
108
109 vtkSmartPointer<vtkPolyDataMapper> interpMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
110 interpMapper->SetInputConnection(interp->GetOutputPort());
111
112 vtkSmartPointer<vtkActor> interpActor = vtkSmartPointer<vtkActor>::New();
113 interpActor->SetMapper(interpMapper);
114
115 //
116 // Case 1: convex polygon meshes
117 //
118 pentagon[0][0] = 0.0;
119 pentagon[0][1] = 0.0;
120 pentagon[0][2] = 0.0;
121
122 vtkSmartPointer<vtkPoints> pentagonPoints1 = vtkSmartPointer<vtkPoints>::New();
123 pentagonPoints1->Initialize();
124 for (int i = 0; i < 5; i++)
125 {
126 pentagonPoints1->InsertNextPoint(pentagon[i]);
127 }
128
129 vtkSmartPointer<vtkCellArray> pentagonCell1 = vtkSmartPointer<vtkCellArray>::New();
130 pentagonCell1->InsertNextCell(5);
131 for (vtkIdType i = 0; i < 5; i++)
132 {
133 pentagonCell1->InsertCellPoint(i);
134 }
135
136 vtkSmartPointer<vtkDoubleArray> pointDataArray1 = vtkSmartPointer<vtkDoubleArray>::New();
137 pointDataArray1->Initialize();
138 for (int i = 0; i < 5; i++)
139 {
140 pointDataArray1->InsertNextValue((pentagon[i][0] + 1.0) / 2.0);
141 }
142
143 vtkSmartPointer<vtkPolyData> polydata1 = vtkSmartPointer<vtkPolyData>::New();
144 polydata1->SetPoints(pentagonPoints1);
145 polydata1->SetPolys(pentagonCell1);
146 polydata1->GetPointData()->SetScalars(pointDataArray1);
147
148 vtkPolygon* polygon1 = static_cast<vtkPolygon*>(polydata1->GetCell(0));
149 polygon1->SetUseMVCInterpolation(true);
150
151 // Okay now sample on a plane and see how it interpolates
152 vtkSmartPointer<vtkPlaneSource> pSource1 = vtkSmartPointer<vtkPlaneSource>::New();
153 pSource1->SetOrigin(-1.0, -1.0, 0);
154 pSource1->SetPoint1(1.0, -1.0, 0);
155 pSource1->SetPoint2(-1.0, 1.0, 0);
156 pSource1->SetXResolution(100);
157 pSource1->SetYResolution(100);
158
159 // interpolation 1: use the more general but slower MVC algorithm.
160 vtkSmartPointer<vtkProbeFilter> interp1 = vtkSmartPointer<vtkProbeFilter>::New();
161 interp1->SetInputConnection(pSource1->GetOutputPort());
162 interp1->SetSourceData(polydata1);
163
164 vtkSmartPointer<vtkPolyDataMapper> interpMapper1 = vtkSmartPointer<vtkPolyDataMapper>::New();
165 interpMapper1->SetInputConnection(interp1->GetOutputPort());
166
167 vtkSmartPointer<vtkActor> interpActor1 = vtkSmartPointer<vtkActor>::New();
168 interpActor1->SetMapper(interpMapper1);
169
170 //
171 // add actors to renderer
172 //
173 vtkSmartPointer<vtkProperty> lightProperty = vtkSmartPointer<vtkProperty>::New();
174 lightProperty->LightingOff();
175 interpActor->SetProperty(lightProperty);
176 interpActor1->SetProperty(lightProperty);
177
178 renderer->AddActor(interpActor);
179 renderer->ResetCamera();
180 renderer->SetBackground(1, 1, 1);
181
182 renderer1->AddActor(interpActor1);
183 renderer1->ResetCamera();
184 renderer1->SetBackground(1, 1, 1);
185
186 renWin->SetSize(600, 300);
187
188 // interact with data
189 renWin->Render();
190
191 int retVal = vtkRegressionTestImage(renWin);
192
193 if (retVal == vtkRegressionTester::DO_INTERACTOR)
194 {
195 iren->Start();
196 }
197
198 return !retVal;
199 }
200