#include <vtkPolyDataMapper.h>
#include <vtkPolyData.h>
#include <vtkActor.h>
-
+#include <vtkPlaneSource.h>
+#include <vtkInteractorObserver.h>
vtkSmartPointer<vtkContourWidget> contourWidget;
vtkSmartPointer<vtkPolyData> pd;
vtkSmartPointer<vtkRenderWindowInteractor> interactor;
vtkSmartPointer<vtkOrientedGlyphContourRepresentation> contourRep;
+// class for handling interaction on/off
class KeyPressInteractorStyle : public vtkInteractorStyleImage//vtkInteractorStyleTrackballCamera
{
public:
};
vtkStandardNewMacro(KeyPressInteractorStyle);
-int main( int argc, char *argv[] )
+
+// class for handling click over the contour
+class ContourCallBack : public vtkCommand
+{
+public:
+ static ContourCallBack *New()
+ {
+ return new ContourCallBack;
+ }
+ ContourCallBack(){}
+
+ virtual void Execute(vtkObject *caller, unsigned long eid, void* clientData)
+ {
+
+ vtkContourWidget *contourWidget =
+ reinterpret_cast<vtkContourWidget*>(caller);
+
+ // Retrieve the windows event x, y, z
+ std::cout << "window click " << contourWidget->GetInteractor()->GetEventPosition()[0] << " "
+ << contourWidget->GetInteractor()->GetEventPosition()[1] << " "
+ << contourWidget->GetInteractor()->GetEventPosition()[2]
+ << std::endl;
+
+ // get the node postiion (the world position)
+ vtkContourRepresentation * rep=contourWidget->GetContourRepresentation();
+ double* nodepos = new double[2]();
+ rep->GetActiveNodeWorldPosition(nodepos);
+
+ std::cout << "widget click " << nodepos[0] << " "<< nodepos[1] << " " << nodepos[2] << std::endl;
+
+ // change position
+ double* restringedPosition = new double[2]();
+ restringedPosition[0] = nodepos[0];
+ restringedPosition[1] = nodepos[1];
+ restringedPosition[2] = 0; // ToDo: change is hard coded but must work for every plane/image
+
+ rep->SetActiveNodeToWorldPosition(restringedPosition);
+ }
+
+};
+
+int main(int argc, char *argv[])
{
- // Create the RenderWindow, Renderer and both Actors
- //
- vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
- vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
- renderWindow->AddRenderer(renderer);
-
- interactor =
- vtkSmartPointer<vtkRenderWindowInteractor>::New();
- interactor->SetRenderWindow(renderWindow);
-
- vtkSmartPointer<KeyPressInteractorStyle> style =
- vtkSmartPointer<KeyPressInteractorStyle>::New();
- interactor->SetInteractorStyle(style);
- style->EndRotate();
- style->SetCurrentRenderer(renderer);
-
- renderer->SetBackground(0.1, 0.2, 0.4);
- renderWindow->SetSize(600, 600);
-
- // image 2d reader
- vtkSmartPointer<vtkPNGReader> reader =
- vtkSmartPointer<vtkPNGReader>::New();
- reader->SetFileName("C:\\Users\\JoseLuis\\Downloads\\pulmon.png");
-
- // Visualize
- vtkSmartPointer<vtkImageViewer2> imageViewer =
- vtkSmartPointer<vtkImageViewer2>::New();
- imageViewer->SetInputConnection(reader->GetOutputPort());
- vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
- vtkSmartPointer<vtkRenderWindowInteractor>::New();
- imageViewer->SetupInteractor(renderWindowInteractor);
-
-
- renderWindowInteractor->Start();
-
- // contour representation
- contourRep =
- vtkSmartPointer<vtkOrientedGlyphContourRepresentation>::New();
- contourRep->GetLinesProperty()->SetColor(1, 0, 0); //set color to red
-
- contourWidget =
- vtkSmartPointer<vtkContourWidget>::New();
- contourWidget->SetInteractor(interactor);
- contourWidget->SetRepresentation(contourRep);
- contourWidget->On();
-
- pd = vtkSmartPointer<vtkPolyData>::New();
-
- vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
- vtkSmartPointer<vtkCellArray> lines = vtkSmartPointer<vtkCellArray>::New();
- vtkIdType* lineIndices = new vtkIdType[21];
- for (int i = 0; i< 20; i++)
- {
- const double angle = 2.0*vtkMath::Pi()*i/20.0;
- points->InsertPoint(static_cast<vtkIdType>(i), 0.1*cos(angle),
- 0.1*sin(angle), 0.0 );
- lineIndices[i] = static_cast<vtkIdType>(i);
- }
-
- lineIndices[20] = 0;
- lines->InsertNextCell(21,lineIndices);
- delete [] lineIndices;
- pd->SetPoints(points);
- pd->SetLines(lines);
-
- imageViewer->Render();
- imageViewer->GetRenderer()->ResetCamera();
-
- vtkSmartPointer<vtkPolyData> poly = contourRep->GetContourRepresentationAsPolyData();
-
- vtkSmartPointer<vtkPolyDataMapper> map = vtkSmartPointer<vtkPolyDataMapper>::New();
- map->SetInputData(poly);
-
- vtkSmartPointer<vtkActor> act = vtkSmartPointer<vtkActor>::New();
- act->SetMapper(map);
-
-
- imageViewer->GetRenderer()->AddActor(act);
- //imageViewer->Render();
-
- contourWidget->Initialize(pd);
- contourWidget->Render();
- renderer->ResetCamera();
- renderWindow->Render();
-
-
-
- interactor->Initialize();
- interactor->Start();
-
- //contourWidget->Off();
-
- return EXIT_SUCCESS;
+ // Create the RenderWindow, Renderer and both Actors
+ //
+ vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
+ vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
+ renderWindow->AddRenderer(renderer);
+
+ vtkSmartPointer<vtkRenderWindowInteractor> interactor =
+ vtkSmartPointer<vtkRenderWindowInteractor>::New();
+ interactor->SetRenderWindow(renderWindow);
+
+ renderer->SetBackground(0.1, 0.2, 0.4);
+ renderWindow->SetSize(600, 600);
+
+ /*vtkSmartPointer<KeyPressInteractorStyle> style =
+ vtkSmartPointer<KeyPressInteractorStyle>::New();
+ interactor->SetInteractorStyle(style);*/
+
+ vtkSmartPointer<vtkOrientedGlyphContourRepresentation> contourRep =
+ vtkSmartPointer<vtkOrientedGlyphContourRepresentation>::New();
+ contourRep->GetLinesProperty()->SetColor(0, 0, 1); //set color to red
+
+ vtkSmartPointer<vtkContourWidget> contourWidget =
+ vtkSmartPointer<vtkContourWidget>::New();
+ contourWidget->SetInteractor(interactor);
+ contourWidget->SetRepresentation(contourRep);
+
+ contourWidget->On();
+
+ for (int i = 0; i < argc; i++)
+ {
+ if (strcmp("-Shift", argv[i]) == 0)
+ {
+ contourWidget->GetEventTranslator()->RemoveTranslation(
+ vtkCommand::LeftButtonPressEvent);
+ contourWidget->GetEventTranslator()->SetTranslation(
+ vtkCommand::LeftButtonPressEvent,
+ vtkWidgetEvent::Translate);
+ }
+ else if (strcmp("-Scale", argv[i]) == 0)
+ {
+ contourWidget->GetEventTranslator()->RemoveTranslation(
+ vtkCommand::LeftButtonPressEvent);
+ contourWidget->GetEventTranslator()->SetTranslation(
+ vtkCommand::LeftButtonPressEvent,
+ vtkWidgetEvent::Scale);
+ }
+ }
+
+ //Add an observer to contour widget
+ vtkSmartPointer<ContourCallBack> contourCallBack =
+ vtkSmartPointer<ContourCallBack>::New();
+ contourWidget->AddObserver(vtkCommand::InteractionEvent, contourCallBack);
+
+ // quick points for representation
+ vtkSmartPointer<vtkPolyData> pd = vtkSmartPointer<vtkPolyData>::New();
+
+ vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
+ vtkSmartPointer<vtkCellArray> lines = vtkSmartPointer<vtkCellArray>::New();
+ vtkIdType* lineIndices = new vtkIdType[21];
+ for (int i = 0; i< 20; i++)
+ {
+ const double angle = 2.0*vtkMath::Pi()*i / 20.0;
+ points->InsertPoint(static_cast<vtkIdType>(i), 0.2*cos(angle),
+ 0.2*sin(angle), 0.0);
+ lineIndices[i] = static_cast<vtkIdType>(i);
+ }
+
+ lineIndices[20] = 0;
+ lines->InsertNextCell(21, lineIndices);
+ delete[] lineIndices;
+ pd->SetPoints(points);
+ pd->SetLines(lines);
+
+
+ contourWidget->Initialize(pd);
+
+ // visual reference
+ // Create a plane
+ vtkSmartPointer<vtkPlaneSource> planeSource =
+ vtkSmartPointer<vtkPlaneSource>::New();
+ planeSource->SetCenter(0.0, 0.0, 0.0);
+ planeSource->SetNormal(0.0, 0.0, 1.0);
+ planeSource->Update();
+
+ vtkPolyData* plane = planeSource->GetOutput();
+
+ // Create a mapper and actor
+ vtkSmartPointer<vtkPolyDataMapper> mapper =
+ vtkSmartPointer<vtkPolyDataMapper>::New();
+ mapper->SetInputData(plane);
+
+ vtkSmartPointer<vtkActor> planeActor =
+ vtkSmartPointer<vtkActor>::New();
+ planeActor->SetMapper(mapper);
+
+
+ contourWidget->Render();
+ renderer->GetActiveCamera()->SetPosition(0, 0, 2.1);
+ renderer->AddActor(planeActor);
+ renderWindow->Render();
+
+ interactor->Initialize();
+ interactor->Start();
+
+ contourWidget->Off();
+
+ return EXIT_SUCCESS;
}