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 "vtkClipPolyData.h"
19 #include "vtkDebugLeaks.h"
20 #include "vtkElevationFilter.h"
21 #include "vtkIdTypeArray.h"
22 #include "vtkLight.h"
23 #include "vtkLightCollection.h"
24 #include "vtkNew.h"
25 #include "vtkPlane.h"
26 #include "vtkPlaneSource.h"
27 #include "vtkPolyData.h"
28 #include "vtkPolyDataMapper.h"
29 #include "vtkProbePolyhedron.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 "vtkSphereSource.h"
37 
TestMeanValueCoordinatesInterpolation1(int argc,char * argv[])38 int TestMeanValueCoordinatesInterpolation1(int argc, char* argv[])
39 {
40   vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
41   vtkSmartPointer<vtkRenderer> renderer1 = vtkSmartPointer<vtkRenderer>::New();
42   renderer->SetViewport(0, 0, 0.5, 1);
43 
44   vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();
45   renWin->SetMultiSamples(0);
46   renWin->AddRenderer(renderer);
47   renWin->AddRenderer(renderer1);
48   renderer1->SetViewport(0.5, 0, 1, 1);
49 
50   vtkSmartPointer<vtkRenderWindowInteractor> iren =
51     vtkSmartPointer<vtkRenderWindowInteractor>::New();
52   iren->SetRenderWindow(renWin);
53 
54   //
55   // Case 0: triangle meshes
56   //
57   // Create a sphere
58   vtkSmartPointer<vtkSphereSource> sphere = vtkSmartPointer<vtkSphereSource>::New();
59   sphere->SetThetaResolution(51);
60   sphere->SetPhiResolution(17);
61 
62   // Generate some scalars on the sphere
63   vtkSmartPointer<vtkElevationFilter> ele = vtkSmartPointer<vtkElevationFilter>::New();
64   ele->SetInputConnection(sphere->GetOutputPort());
65   ele->SetLowPoint(-0.5, 0, 0);
66   ele->SetHighPoint(0.5, 0, 0);
67   ele->Update();
68 
69   // Now clip the sphere in half and display it
70   vtkSmartPointer<vtkPlane> plane = vtkSmartPointer<vtkPlane>::New();
71   plane->SetOrigin(0, 0, 0);
72   plane->SetNormal(0, 0, 1);
73 
74   vtkSmartPointer<vtkClipPolyData> clip = vtkSmartPointer<vtkClipPolyData>::New();
75   clip->SetInputConnection(ele->GetOutputPort());
76   clip->SetClipFunction(plane);
77 
78   vtkSmartPointer<vtkPolyDataMapper> sphereMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
79   sphereMapper->SetInputConnection(clip->GetOutputPort());
80 
81   vtkSmartPointer<vtkActor> sphereActor = vtkSmartPointer<vtkActor>::New();
82   sphereActor->SetMapper(sphereMapper);
83 
84   // Okay now sample the sphere mesh with a plane and see how it interpolates
85   vtkSmartPointer<vtkPlaneSource> pSource = vtkSmartPointer<vtkPlaneSource>::New();
86   pSource->SetOrigin(-1.0, -1.0, 0);
87   pSource->SetPoint1(1.0, -1.0, 0);
88   pSource->SetPoint2(-1.0, 1.0, 0);
89   pSource->SetXResolution(50);
90   pSource->SetYResolution(50);
91 
92   // interpolation 0: use the faster MVC algorithm specialized for triangle meshes.
93   vtkSmartPointer<vtkProbePolyhedron> interp = vtkSmartPointer<vtkProbePolyhedron>::New();
94   interp->SetInputConnection(pSource->GetOutputPort());
95   interp->SetSourceConnection(ele->GetOutputPort());
96 
97   vtkSmartPointer<vtkPolyDataMapper> interpMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
98   interpMapper->SetInputConnection(interp->GetOutputPort());
99 
100   vtkSmartPointer<vtkActor> interpActor = vtkSmartPointer<vtkActor>::New();
101   interpActor->SetMapper(interpMapper);
102 
103   //
104   // Case 1: general meshes
105   //
106   // Create a sphere
107   vtkSmartPointer<vtkSphereSource> sphere1 = vtkSmartPointer<vtkSphereSource>::New();
108   sphere1->SetThetaResolution(51);
109   sphere1->SetPhiResolution(17);
110 
111   // Generate some scalars on the sphere
112   vtkSmartPointer<vtkElevationFilter> ele1 = vtkSmartPointer<vtkElevationFilter>::New();
113   ele1->SetInputConnection(sphere1->GetOutputPort());
114   ele1->SetLowPoint(-0.5, 0, 0);
115   ele1->SetHighPoint(0.5, 0, 0);
116   ele1->Update();
117 
118   // create a cell with 4 points
119   vtkPolyData* spherePoly = vtkPolyData::SafeDownCast(ele1->GetOutput());
120   vtkCellArray* polys = spherePoly->GetPolys();
121 
122   // merge the first two cell, this will make vtkProbePolyhedron select the
123   // more general MVC algorithm.
124   vtkNew<vtkIdTypeArray> legacyArray;
125   polys->ExportLegacyFormat(legacyArray);
126   vtkIdType* p = legacyArray->GetPointer(0);
127   vtkIdType pids[4] = { p[1], p[2], p[6], p[3] };
128 
129   vtkSmartPointer<vtkCellArray> newPolys = vtkSmartPointer<vtkCellArray>::New();
130   newPolys->Initialize();
131   for (int i = 2; i < polys->GetNumberOfCells(); i++)
132   {
133     pids[0] = p[4 * i + 1];
134     pids[1] = p[4 * i + 2];
135     pids[2] = p[4 * i + 3];
136     newPolys->InsertNextCell(3, pids);
137   }
138   spherePoly->SetPolys(newPolys);
139 
140   // Now clip the sphere in half and display it
141   vtkSmartPointer<vtkPlane> plane1 = vtkSmartPointer<vtkPlane>::New();
142   plane1->SetOrigin(0, 0, 0);
143   plane1->SetNormal(0, 0, 1);
144 
145   vtkSmartPointer<vtkClipPolyData> clip1 = vtkSmartPointer<vtkClipPolyData>::New();
146   clip1->SetInputData(spherePoly);
147   clip1->SetClipFunction(plane1);
148 
149   vtkSmartPointer<vtkPolyDataMapper> sphereMapper1 = vtkSmartPointer<vtkPolyDataMapper>::New();
150   sphereMapper1->SetInputConnection(clip1->GetOutputPort());
151 
152   vtkSmartPointer<vtkActor> sphereActor1 = vtkSmartPointer<vtkActor>::New();
153   sphereActor1->SetMapper(sphereMapper1);
154 
155   // Okay now sample the sphere mesh with a plane and see how it interpolates
156   vtkSmartPointer<vtkPlaneSource> pSource1 = vtkSmartPointer<vtkPlaneSource>::New();
157   pSource1->SetOrigin(-1.0, -1.0, 0);
158   pSource1->SetPoint1(1.0, -1.0, 0);
159   pSource1->SetPoint2(-1.0, 1.0, 0);
160   pSource1->SetXResolution(50);
161   pSource1->SetYResolution(50);
162 
163   // interpolation 1: use the more general but slower MVC algorithm.
164   vtkSmartPointer<vtkProbePolyhedron> interp1 = vtkSmartPointer<vtkProbePolyhedron>::New();
165   interp1->SetInputConnection(pSource1->GetOutputPort());
166   interp1->SetSourceConnection(ele1->GetOutputPort());
167 
168   vtkSmartPointer<vtkPolyDataMapper> interpMapper1 = vtkSmartPointer<vtkPolyDataMapper>::New();
169   interpMapper1->SetInputConnection(interp1->GetOutputPort());
170 
171   vtkSmartPointer<vtkActor> interpActor1 = vtkSmartPointer<vtkActor>::New();
172   interpActor1->SetMapper(interpMapper1);
173 
174   //
175   // add actors to renderer
176   //
177   vtkSmartPointer<vtkProperty> lightProperty = vtkSmartPointer<vtkProperty>::New();
178   lightProperty->LightingOff();
179   sphereActor->SetProperty(lightProperty);
180   interpActor->SetProperty(lightProperty);
181   interpActor1->SetProperty(lightProperty);
182 
183   renderer->AddActor(sphereActor);
184   renderer->AddActor(interpActor);
185   renderer->ResetCamera();
186   renderer->SetBackground(1, 1, 1);
187 
188   renderer1->AddActor(sphereActor);
189   renderer1->AddActor(interpActor1);
190   renderer1->ResetCamera();
191   renderer1->SetBackground(1, 1, 1);
192 
193   renWin->SetSize(600, 300);
194 
195   // interact with data
196   renWin->Render();
197 
198   int retVal = vtkRegressionTestImage(renWin);
199 
200   if (retVal == vtkRegressionTester::DO_INTERACTOR)
201   {
202     iren->Start();
203   }
204 
205   return !retVal;
206 }
207