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>
12 #include <vtkPolyData.h>
13 #include <vtkCellArray.h>
14 #include <vtkPoints.h>
16 #include <vtkWidgetEvent.h>
17 #include <vtkWidgetEventTranslator.h>
18 #include <vtkInteractorStyleTrackballCamera.h>
19 #include <vtkInteractorStyleImage.h>
21 #include <vtkPNGReader.h>
22 #include <vtkImageViewer2.h>
23 #include <vtkObjectFactory.h>
24 #include <vtkPolyDataMapper.h>
25 #include <vtkPolyData.h>
27 #include <vtkPlaneSource.h>
28 #include <vtkInteractorObserver.h>
30 vtkSmartPointer<vtkContourWidget> contourWidget;
31 vtkSmartPointer<vtkPolyData> pd;
32 vtkSmartPointer<vtkRenderWindowInteractor> interactor;
33 vtkSmartPointer<vtkOrientedGlyphContourRepresentation> contourRep;
35 // class for handling interaction on/off
36 class KeyPressInteractorStyle : public vtkInteractorStyleImage//vtkInteractorStyleTrackballCamera
39 static KeyPressInteractorStyle* New();
40 vtkTypeMacro(KeyPressInteractorStyle, vtkInteractorStyleTrackballCamera);
44 virtual void OnKeyPress()
47 vtkRenderWindowInteractor *rwi = this->Interactor;
48 std::string key = rwi->GetKeySym();
50 // Output the key that was pressed
51 std::cout << "Pressed " << key << std::endl;
53 // Handle an arrow key
57 vtkSmartPointer<vtkContourWidget>::New();
58 contourWidget->SetInteractor(interactor);
59 contourWidget->SetRepresentation(contourRep);
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);
69 //contourWidget->Initialize(pd);
70 //contourWidget->Render();
72 //interactor->Initialize();
73 //interactor->Start();
74 //Interactor->Disable();
77 // Handle a "normal" key
80 std::cout << "Remove points" << std::endl;
81 contourWidget->GetEventTranslator()->RemoveTranslation(
82 vtkCommand::LeftButtonPressEvent);
83 contourWidget->GetEventTranslator()->SetTranslation(
84 vtkCommand::LeftButtonPressEvent,
85 vtkWidgetEvent::Delete);
90 vtkInteractorStyleTrackballCamera::OnKeyPress();
94 vtkStandardNewMacro(KeyPressInteractorStyle);
97 // class for handling click over the contour
98 class ContourCallBack : public vtkCommand
101 static ContourCallBack *New()
103 return new ContourCallBack;
107 virtual void Execute(vtkObject *caller, unsigned long eid, void* clientData)
110 vtkContourWidget *contourWidget =
111 reinterpret_cast<vtkContourWidget*>(caller);
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]
119 // get the node postiion (the world position)
120 vtkContourRepresentation * rep=contourWidget->GetContourRepresentation();
121 double* nodepos = new double[2]();
122 rep->GetActiveNodeWorldPosition(nodepos);
124 std::cout << "widget click " << nodepos[0] << " "<< nodepos[1] << " " << nodepos[2] << std::endl;
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
132 rep->SetActiveNodeToWorldPosition(restringedPosition);
137 int main(int argc, char *argv[])
139 // Create the RenderWindow, Renderer and both Actors
141 vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
142 vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
143 renderWindow->AddRenderer(renderer);
145 vtkSmartPointer<vtkRenderWindowInteractor> interactor =
146 vtkSmartPointer<vtkRenderWindowInteractor>::New();
147 interactor->SetRenderWindow(renderWindow);
149 renderer->SetBackground(0.1, 0.2, 0.4);
150 renderWindow->SetSize(600, 600);
152 /*vtkSmartPointer<KeyPressInteractorStyle> style =
153 vtkSmartPointer<KeyPressInteractorStyle>::New();
154 interactor->SetInteractorStyle(style);*/
156 vtkSmartPointer<vtkOrientedGlyphContourRepresentation> contourRep =
157 vtkSmartPointer<vtkOrientedGlyphContourRepresentation>::New();
158 contourRep->GetLinesProperty()->SetColor(0, 0, 1); //set color to red
160 vtkSmartPointer<vtkContourWidget> contourWidget =
161 vtkSmartPointer<vtkContourWidget>::New();
162 contourWidget->SetInteractor(interactor);
163 contourWidget->SetRepresentation(contourRep);
167 for (int i = 0; i < argc; i++)
169 if (strcmp("-Shift", argv[i]) == 0)
171 contourWidget->GetEventTranslator()->RemoveTranslation(
172 vtkCommand::LeftButtonPressEvent);
173 contourWidget->GetEventTranslator()->SetTranslation(
174 vtkCommand::LeftButtonPressEvent,
175 vtkWidgetEvent::Translate);
177 else if (strcmp("-Scale", argv[i]) == 0)
179 contourWidget->GetEventTranslator()->RemoveTranslation(
180 vtkCommand::LeftButtonPressEvent);
181 contourWidget->GetEventTranslator()->SetTranslation(
182 vtkCommand::LeftButtonPressEvent,
183 vtkWidgetEvent::Scale);
187 //Add an observer to contour widget
188 vtkSmartPointer<ContourCallBack> contourCallBack =
189 vtkSmartPointer<ContourCallBack>::New();
190 contourWidget->AddObserver(vtkCommand::InteractionEvent, contourCallBack);
192 // quick points for representation
193 vtkSmartPointer<vtkPolyData> pd = vtkSmartPointer<vtkPolyData>::New();
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++)
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);
207 lines->InsertNextCell(21, lineIndices);
208 delete[] lineIndices;
209 pd->SetPoints(points);
213 contourWidget->Initialize(pd);
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();
223 vtkPolyData* plane = planeSource->GetOutput();
225 // Create a mapper and actor
226 vtkSmartPointer<vtkPolyDataMapper> mapper =
227 vtkSmartPointer<vtkPolyDataMapper>::New();
228 mapper->SetInputData(plane);
230 vtkSmartPointer<vtkActor> planeActor =
231 vtkSmartPointer<vtkActor>::New();
232 planeActor->SetMapper(mapper);
235 contourWidget->Render();
236 renderer->GetActiveCamera()->SetPosition(0, 0, 2.1);
237 renderer->AddActor(planeActor);
238 renderWindow->Render();
240 interactor->Initialize();
243 contourWidget->Off();