]> Creatis software - cpPlugins.git/blob - appli/examples/example_ContourWidget.cxx
contour widget example update
[cpPlugins.git] / appli / examples / example_ContourWidget.cxx
1 #include <vtkSmartPointer.h>
2 #include <vtkProperty.h>
3 #include <vtkContourWidget.h>
4 #include <vtkOrientedGlyphContourRepresentation.h>
5 #include <vtkRenderer.h>
6 #include <vtkRenderWindow.h>
7 #include <vtkRenderWindowInteractor.h>
8 #include <vtkCommand.h>
9 #include <vtkDebugLeaks.h>
10 #include <vtkCamera.h>
11 #include <vtkPlane.h>
12 #include <vtkPolyData.h>
13 #include <vtkCellArray.h>
14 #include <vtkPoints.h>
15 #include <vtkMath.h>
16 #include <vtkWidgetEvent.h>
17 #include <vtkWidgetEventTranslator.h>
18 #include <vtkInteractorStyleTrackballCamera.h>
19 #include <vtkInteractorStyleImage.h>
20
21 #include <vtkPNGReader.h>
22 #include <vtkImageViewer2.h> 
23 #include <vtkObjectFactory.h>
24 #include <vtkPolyDataMapper.h>
25 #include <vtkPolyData.h>
26 #include <vtkActor.h>
27 #include <vtkPlaneSource.h>
28 #include <vtkInteractorObserver.h>
29
30 vtkSmartPointer<vtkContourWidget> contourWidget;
31 vtkSmartPointer<vtkPolyData> pd;
32 vtkSmartPointer<vtkRenderWindowInteractor> interactor;
33 vtkSmartPointer<vtkOrientedGlyphContourRepresentation> contourRep;
34
35 // class for handling interaction on/off
36 class KeyPressInteractorStyle : public vtkInteractorStyleImage//vtkInteractorStyleTrackballCamera
37 {
38 public:
39         static KeyPressInteractorStyle* New();
40         vtkTypeMacro(KeyPressInteractorStyle, vtkInteractorStyleTrackballCamera);
41
42         
43
44         virtual void OnKeyPress()
45         {
46                 // Get the keypress
47                 vtkRenderWindowInteractor *rwi = this->Interactor;
48                 std::string key = rwi->GetKeySym();
49
50                 // Output the key that was pressed
51                 std::cout << "Pressed " << key << std::endl;
52
53                 // Handle an arrow key
54                 if (key == "Up")
55                 {
56                         contourWidget =
57                                 vtkSmartPointer<vtkContourWidget>::New();
58                         contourWidget->SetInteractor(interactor);
59                         contourWidget->SetRepresentation(contourRep);
60                         contourWidget->On();
61
62                         std::cout << "add and move points" << std::endl;
63                         contourWidget->GetEventTranslator()->RemoveTranslation(
64                                 vtkCommand::LeftButtonPressEvent);
65                         contourWidget->GetEventTranslator()->SetTranslation(
66                                 vtkCommand::LeftButtonPressEvent,
67                                 vtkWidgetEvent::Translate);
68
69                         //contourWidget->Initialize(pd);
70                         //contourWidget->Render();
71
72                         //interactor->Initialize();
73                         //interactor->Start();
74                         //Interactor->Disable();
75                 }
76
77                 // Handle a "normal" key
78                 if (key == "Down")
79                 {
80                         std::cout << "Remove points" << std::endl;
81                         contourWidget->GetEventTranslator()->RemoveTranslation(
82                                 vtkCommand::LeftButtonPressEvent);
83                         contourWidget->GetEventTranslator()->SetTranslation(
84                                 vtkCommand::LeftButtonPressEvent,
85                                 vtkWidgetEvent::Delete);
86                         
87                 }
88
89                 // Forward events
90                 vtkInteractorStyleTrackballCamera::OnKeyPress();
91         }
92
93 };
94 vtkStandardNewMacro(KeyPressInteractorStyle);
95
96
97 // class for handling click over the contour
98 class ContourCallBack : public vtkCommand
99 {
100 public:
101         static ContourCallBack *New()
102         {
103                 return new ContourCallBack;
104         }
105         ContourCallBack(){}
106
107         virtual void Execute(vtkObject *caller, unsigned long eid, void* clientData)
108         {
109
110                 vtkContourWidget *contourWidget =
111                         reinterpret_cast<vtkContourWidget*>(caller);
112
113                 // Retrieve the windows event x, y, z
114                 std::cout << "window click " << contourWidget->GetInteractor()->GetEventPosition()[0] << " "
115                         << contourWidget->GetInteractor()->GetEventPosition()[1] << " "
116                         << contourWidget->GetInteractor()->GetEventPosition()[2] 
117                         << std::endl;
118
119                 // get the node postiion (the world position)
120                 vtkContourRepresentation * rep=contourWidget->GetContourRepresentation();
121                 double* nodepos = new double[2]();
122                 rep->GetActiveNodeWorldPosition(nodepos);
123                 
124                 std::cout << "widget click " << nodepos[0] << " "<< nodepos[1] << " " << nodepos[2] << std::endl;       
125
126                 // change position
127                 double* restringedPosition = new double[2]();
128                 restringedPosition[0] = nodepos[0];
129                 restringedPosition[1] = nodepos[1];
130                 restringedPosition[2] = 0; // ToDo: change is hard coded but must work for every plane/image
131
132                 rep->SetActiveNodeToWorldPosition(restringedPosition);
133         }
134
135 };
136
137 int main(int argc, char *argv[])
138 {
139         // Create the RenderWindow, Renderer and both Actors
140         //
141         vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
142         vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
143         renderWindow->AddRenderer(renderer);
144
145         vtkSmartPointer<vtkRenderWindowInteractor> interactor =
146                 vtkSmartPointer<vtkRenderWindowInteractor>::New();
147         interactor->SetRenderWindow(renderWindow);
148
149         renderer->SetBackground(0.1, 0.2, 0.4);
150         renderWindow->SetSize(600, 600);
151
152         /*vtkSmartPointer<KeyPressInteractorStyle> style =
153                 vtkSmartPointer<KeyPressInteractorStyle>::New();
154         interactor->SetInteractorStyle(style);*/
155
156         vtkSmartPointer<vtkOrientedGlyphContourRepresentation> contourRep =
157                 vtkSmartPointer<vtkOrientedGlyphContourRepresentation>::New();
158         contourRep->GetLinesProperty()->SetColor(0, 0, 1); //set color to red
159
160         vtkSmartPointer<vtkContourWidget> contourWidget =
161                 vtkSmartPointer<vtkContourWidget>::New();
162         contourWidget->SetInteractor(interactor);
163         contourWidget->SetRepresentation(contourRep);
164         
165         contourWidget->On();
166
167         for (int i = 0; i < argc; i++)
168         {
169                 if (strcmp("-Shift", argv[i]) == 0)
170                 {
171                         contourWidget->GetEventTranslator()->RemoveTranslation(
172                                 vtkCommand::LeftButtonPressEvent);
173                         contourWidget->GetEventTranslator()->SetTranslation(
174                                 vtkCommand::LeftButtonPressEvent,
175                                 vtkWidgetEvent::Translate);
176                 }
177                 else if (strcmp("-Scale", argv[i]) == 0)
178                 {
179                         contourWidget->GetEventTranslator()->RemoveTranslation(
180                                 vtkCommand::LeftButtonPressEvent);
181                         contourWidget->GetEventTranslator()->SetTranslation(
182                                 vtkCommand::LeftButtonPressEvent,
183                                 vtkWidgetEvent::Scale);
184                 }
185         }
186
187         //Add an observer to contour widget
188         vtkSmartPointer<ContourCallBack> contourCallBack =
189                 vtkSmartPointer<ContourCallBack>::New();
190         contourWidget->AddObserver(vtkCommand::InteractionEvent, contourCallBack);
191
192         // quick points for representation
193         vtkSmartPointer<vtkPolyData> pd = vtkSmartPointer<vtkPolyData>::New();
194
195         vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
196         vtkSmartPointer<vtkCellArray> lines = vtkSmartPointer<vtkCellArray>::New();
197         vtkIdType* lineIndices = new vtkIdType[21];
198         for (int i = 0; i< 20; i++)
199         {
200                 const double angle = 2.0*vtkMath::Pi()*i / 20.0;
201                 points->InsertPoint(static_cast<vtkIdType>(i), 0.2*cos(angle),
202                         0.2*sin(angle), 0.0);
203                 lineIndices[i] = static_cast<vtkIdType>(i);
204         }
205
206         lineIndices[20] = 0;
207         lines->InsertNextCell(21, lineIndices);
208         delete[] lineIndices;
209         pd->SetPoints(points);
210         pd->SetLines(lines);
211
212
213         contourWidget->Initialize(pd);
214         
215         // visual reference
216         // Create a plane
217         vtkSmartPointer<vtkPlaneSource> planeSource =
218                 vtkSmartPointer<vtkPlaneSource>::New();
219         planeSource->SetCenter(0.0, 0.0, 0.0);
220         planeSource->SetNormal(0.0, 0.0, 1.0);  
221         planeSource->Update();
222
223         vtkPolyData* plane = planeSource->GetOutput();
224
225         // Create a mapper and actor
226         vtkSmartPointer<vtkPolyDataMapper> mapper =
227                 vtkSmartPointer<vtkPolyDataMapper>::New();
228         mapper->SetInputData(plane);
229
230         vtkSmartPointer<vtkActor> planeActor =
231                 vtkSmartPointer<vtkActor>::New();
232         planeActor->SetMapper(mapper);
233         
234         
235         contourWidget->Render();
236         renderer->GetActiveCamera()->SetPosition(0, 0, 2.1);
237         renderer->AddActor(planeActor);
238         renderWindow->Render();
239
240         interactor->Initialize();
241         interactor->Start();
242
243         contourWidget->Off();
244
245         return EXIT_SUCCESS;
246 }