From: jose guzman Date: Mon, 28 Sep 2015 13:41:44 +0000 (+0200) Subject: contour widget example update X-Git-Tag: v0.1~359 X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=4fa665c644376beb7fb88980742222a2462bced8;p=cpPlugins.git contour widget example update --- diff --git a/appli/examples/example_ContourWidget.cxx b/appli/examples/example_ContourWidget.cxx index d9fad29..5e5352a 100644 --- a/appli/examples/example_ContourWidget.cxx +++ b/appli/examples/example_ContourWidget.cxx @@ -24,13 +24,15 @@ #include #include #include - +#include +#include vtkSmartPointer contourWidget; vtkSmartPointer pd; vtkSmartPointer interactor; vtkSmartPointer contourRep; +// class for handling interaction on/off class KeyPressInteractorStyle : public vtkInteractorStyleImage//vtkInteractorStyleTrackballCamera { public: @@ -91,99 +93,154 @@ 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(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 renderer = vtkSmartPointer::New(); - vtkSmartPointer renderWindow = vtkSmartPointer::New(); - renderWindow->AddRenderer(renderer); - - interactor = - vtkSmartPointer::New(); - interactor->SetRenderWindow(renderWindow); - - vtkSmartPointer style = - vtkSmartPointer::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 reader = - vtkSmartPointer::New(); - reader->SetFileName("C:\\Users\\JoseLuis\\Downloads\\pulmon.png"); - - // Visualize - vtkSmartPointer imageViewer = - vtkSmartPointer::New(); - imageViewer->SetInputConnection(reader->GetOutputPort()); - vtkSmartPointer renderWindowInteractor = - vtkSmartPointer::New(); - imageViewer->SetupInteractor(renderWindowInteractor); - - - renderWindowInteractor->Start(); - - // contour representation - contourRep = - vtkSmartPointer::New(); - contourRep->GetLinesProperty()->SetColor(1, 0, 0); //set color to red - - contourWidget = - vtkSmartPointer::New(); - contourWidget->SetInteractor(interactor); - contourWidget->SetRepresentation(contourRep); - contourWidget->On(); - - pd = vtkSmartPointer::New(); - - vtkSmartPointer points = vtkSmartPointer::New(); - vtkSmartPointer lines = vtkSmartPointer::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(i), 0.1*cos(angle), - 0.1*sin(angle), 0.0 ); - lineIndices[i] = static_cast(i); - } - - lineIndices[20] = 0; - lines->InsertNextCell(21,lineIndices); - delete [] lineIndices; - pd->SetPoints(points); - pd->SetLines(lines); - - imageViewer->Render(); - imageViewer->GetRenderer()->ResetCamera(); - - vtkSmartPointer poly = contourRep->GetContourRepresentationAsPolyData(); - - vtkSmartPointer map = vtkSmartPointer::New(); - map->SetInputData(poly); - - vtkSmartPointer act = vtkSmartPointer::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 renderer = vtkSmartPointer::New(); + vtkSmartPointer renderWindow = vtkSmartPointer::New(); + renderWindow->AddRenderer(renderer); + + vtkSmartPointer interactor = + vtkSmartPointer::New(); + interactor->SetRenderWindow(renderWindow); + + renderer->SetBackground(0.1, 0.2, 0.4); + renderWindow->SetSize(600, 600); + + /*vtkSmartPointer style = + vtkSmartPointer::New(); + interactor->SetInteractorStyle(style);*/ + + vtkSmartPointer contourRep = + vtkSmartPointer::New(); + contourRep->GetLinesProperty()->SetColor(0, 0, 1); //set color to red + + vtkSmartPointer contourWidget = + vtkSmartPointer::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 = + vtkSmartPointer::New(); + contourWidget->AddObserver(vtkCommand::InteractionEvent, contourCallBack); + + // quick points for representation + vtkSmartPointer pd = vtkSmartPointer::New(); + + vtkSmartPointer points = vtkSmartPointer::New(); + vtkSmartPointer lines = vtkSmartPointer::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(i), 0.2*cos(angle), + 0.2*sin(angle), 0.0); + lineIndices[i] = static_cast(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 planeSource = + vtkSmartPointer::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 mapper = + vtkSmartPointer::New(); + mapper->SetInputData(plane); + + vtkSmartPointer planeActor = + vtkSmartPointer::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; }