1 /*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: $RCSfile$
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 "vtkPolyData.h"
17 #include "vtkPolyDataMapper.h"
18 #include "vtkRenderWindow.h"
19 #include "vtkRenderWindowInteractor.h"
20 #include "vtkRenderer.h"
21 #include "vtkRenderer.h"
22 #include "vtkSphereSource.h"
23 #include "vtkElevationFilter.h"
24 #include "vtkPlane.h"
25 #include "vtkPlaneSource.h"
26 #include "vtkProperty.h"
27 #include "vtkSmartPointer.h"
28 #include "vtkCellArray.h"
29 #include "vtkPoints.h"
30 #include "vtkDeformPointSet.h"
31 #include "vtkCamera.h"
32 #include "vtkNew.h"
33 #include "vtkRegressionTestImage.h"
34 #include "vtkTestUtilities.h"
35
TestDeformPointSet(int argc,char * argv[])36 int TestDeformPointSet( int argc, char *argv[] )
37 {
38 vtkNew<vtkRenderer> renderer;
39 vtkNew<vtkRenderWindow> renWin;
40 renWin->AddRenderer(renderer.GetPointer());
41 vtkNew<vtkRenderWindowInteractor> iren;
42 iren->SetRenderWindow(renWin.GetPointer());
43
44 // Create a sphere to warp
45 vtkNew<vtkSphereSource> sphere;
46 sphere->SetThetaResolution(51);
47 sphere->SetPhiResolution(17);
48
49 // Generate some scalars on the sphere
50 vtkNew<vtkElevationFilter> ele;
51 ele->SetInputConnection(sphere->GetOutputPort());
52 ele->SetLowPoint(0,0,-0.5);
53 ele->SetHighPoint(0,0,0.5);
54
55 // Now create a control mesh, in this case a octagon
56 vtkNew<vtkPoints> pts;
57 pts->SetNumberOfPoints(6);
58 pts->SetPoint(0, -1, 0, 0);
59 pts->SetPoint(1, 1, 0, 0);
60 pts->SetPoint(2, 0,-1, 0);
61 pts->SetPoint(3, 0, 1, 0);
62 pts->SetPoint(4, 0, 0,-1);
63 pts->SetPoint(5, 0, 0, 1);
64
65 vtkNew<vtkCellArray> tris;
66 tris->InsertNextCell(3);
67 tris->InsertCellPoint(2);
68 tris->InsertCellPoint(0);
69 tris->InsertCellPoint(4);
70 tris->InsertNextCell(3);
71 tris->InsertCellPoint(1);
72 tris->InsertCellPoint(2);
73 tris->InsertCellPoint(4);
74 tris->InsertNextCell(3);
75 tris->InsertCellPoint(3);
76 tris->InsertCellPoint(1);
77 tris->InsertCellPoint(4);
78 tris->InsertNextCell(3);
79 tris->InsertCellPoint(0);
80 tris->InsertCellPoint(3);
81 tris->InsertCellPoint(4);
82 tris->InsertNextCell(3);
83 tris->InsertCellPoint(0);
84 tris->InsertCellPoint(2);
85 tris->InsertCellPoint(5);
86 tris->InsertNextCell(3);
87 tris->InsertCellPoint(2);
88 tris->InsertCellPoint(1);
89 tris->InsertCellPoint(5);
90 tris->InsertNextCell(3);
91 tris->InsertCellPoint(1);
92 tris->InsertCellPoint(3);
93 tris->InsertCellPoint(5);
94 tris->InsertNextCell(3);
95 tris->InsertCellPoint(3);
96 tris->InsertCellPoint(0);
97 tris->InsertCellPoint(5);
98
99 vtkNew<vtkPolyData> pd;
100 pd->SetPoints(pts.GetPointer());
101 pd->SetPolys(tris.GetPointer());
102
103 // Display the control mesh
104 vtkNew<vtkPolyDataMapper> meshMapper;
105 meshMapper->SetInputData(pd.GetPointer());
106 vtkNew<vtkActor> meshActor;
107 meshActor->SetMapper(meshMapper.GetPointer());
108 meshActor->GetProperty()->SetRepresentationToWireframe();
109 meshActor->GetProperty()->SetColor(0,0,0);
110
111 // Okay now let's do the intitial weight generation
112 vtkNew<vtkDeformPointSet> deform;
113 deform->SetInputConnection(ele->GetOutputPort());
114 deform->SetControlMeshData(pd.GetPointer());
115 deform->Update(); //this creates the initial weights
116
117 // Now move one point and deform
118 pts->SetPoint(5, 0,0,3);
119 pts->Modified();
120 deform->Update();
121
122 // Display the warped sphere
123 vtkNew<vtkPolyDataMapper> sphereMapper;
124 sphereMapper->SetInputConnection(deform->GetOutputPort());
125 vtkNew<vtkActor> sphereActor;
126 sphereActor->SetMapper(sphereMapper.GetPointer());
127
128 renderer->AddActor(sphereActor.GetPointer());
129 renderer->AddActor(meshActor.GetPointer());
130 renderer->GetActiveCamera()->SetPosition(1,1,1);
131 renderer->ResetCamera();
132
133 renderer->SetBackground(1,1,1);
134 renWin->SetSize(300,300);
135 renWin->Render();
136
137 int retVal = vtkRegressionTestImage( renWin.GetPointer() );
138 if ( retVal == vtkRegressionTester::DO_INTERACTOR)
139 {
140 iren->Start();
141 }
142
143 return !retVal;
144 }
145