]> Creatis software - cpPlugins.git/blobdiff - appli/examples/example_ContourWidget.cxx
Parameters are now part of the pipeline update process
[cpPlugins.git] / appli / examples / example_ContourWidget.cxx
index d9fad29133ff4381afc54f187c61c3f8f9689479..c3b405cde3c3d59440d9bd94831d66771f1a5746 100644 (file)
 #include <vtkPolyDataMapper.h>
 #include <vtkPolyData.h>
 #include <vtkActor.h>
-
+#include <vtkPlaneSource.h>
+#include <vtkInteractorObserver.h>
+#include <cmath>
 
 vtkSmartPointer<vtkContourWidget> contourWidget;
 vtkSmartPointer<vtkPolyData> pd;
 vtkSmartPointer<vtkRenderWindowInteractor> interactor;
 vtkSmartPointer<vtkOrientedGlyphContourRepresentation> contourRep;
 
+// class for handling interaction on/off
 class KeyPressInteractorStyle : public vtkInteractorStyleImage//vtkInteractorStyleTrackballCamera
 {
 public:
@@ -45,145 +48,232 @@ public:
                vtkRenderWindowInteractor *rwi = this->Interactor;
                std::string key = rwi->GetKeySym();
 
+               int ctrlKeyInd = rwi->GetControlKey();
+
+               int altKeyInd = rwi->GetAltKey();
+
+               int shiftKeyInd = rwi->GetShiftKey();
+
                // Output the key that was pressed
                std::cout << "Pressed " << key << std::endl;
 
                // Handle an arrow key
                if (key == "Up")
+               {       
+
+                       std::cout << "UP" << std::endl;
+                       
+               }
+
+               // Handle a "normal" key
+               if (key == "Down")
+               {
+                       std::cout << "DOWN" << std::endl;               
+                       
+               }
+
+               if (ctrlKeyInd != 0)
                {
-                       contourWidget =
-                               vtkSmartPointer<vtkContourWidget>::New();
-                       contourWidget->SetInteractor(interactor);
-                       contourWidget->SetRepresentation(contourRep);
-                       contourWidget->On();
+                       if (key == "z" || key == "Z")
+                       {
+                               std::cout << "undo" << std::endl;
+
+                       }
+
+                       if (key == "y" || key == "Y")
+                       {
+                               std::cout << "redo" << std::endl;
+
+                       }
+               }
+
+               // Forward events
+               vtkInteractorStyleTrackballCamera::OnKeyPress();
+       }
+
+};
+vtkStandardNewMacro(KeyPressInteractorStyle);
+
+double* GetClosesstPointToPlane(double * n, double * p)
+{
+       double* closest = new double[2]();
+               
+       double c = -(n[0]*p[0] + n[1]*p[1] + n[2]*p[2]) / (n[0]*n[0] + n[1]*n[1] + n[2]*n[2]);
+       closest[0] = p[0] + c*n[0];
+       closest[1] = p[1] + c*n[1];
+       closest[2] = p[2] + c*n[2];
+
+       return closest;
+}
+
+double* normal;
+double* center;
+
+// 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)
+       {
+               
 
-                       std::cout << "add and move points" << std::endl;
+               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* worldclick = new double[2]();
+               rep->GetActiveNodeWorldPosition(worldclick);
+               
+               std::cout << "worldclick click " << worldclick[0] << " " << worldclick[1] << " " << worldclick[2] << std::endl;
+
+               // change position
+               double* projection = 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
+
+               projection = GetClosesstPointToPlane(normal, worldclick);
+
+               std::cout << "projection click " << projection[0] << " " << projection[1] << " " << projection[2] << std::endl;
+
+               rep->SetActiveNodeToWorldPosition(projection);
+               //delete(nodepos);
+       }
+
+};
+
+int main(int argc, char *argv[])
+{
+       normal = new double[2]();
+       normal[0] = 1;
+       normal[1] = 1;
+       normal[2] = 1;
+       center = new double[2]();
+
+       // 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);
+
+       // visual reference
+       // Create a plane
+       vtkSmartPointer<vtkPlaneSource> planeSource =
+               vtkSmartPointer<vtkPlaneSource>::New();
+       planeSource->SetCenter(center); // 0.0, 0.0, 0.0);
+       planeSource->SetNormal(normal); //1.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);
+
+       vtkSmartPointer<KeyPressInteractorStyle> style =
+               vtkSmartPointer<KeyPressInteractorStyle>::New();
+       interactor->SetInteractorStyle(style);
+
+       // create the contour stuff
+       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);
-
-                       //contourWidget->Initialize(pd);
-                       //contourWidget->Render();
-
-                       //interactor->Initialize();
-                       //interactor->Start();
-                       //Interactor->Disable();
                }
-
-               // Handle a "normal" key
-               if (key == "Down")
+               else if (strcmp("-Scale", argv[i]) == 0)
                {
-                       std::cout << "Remove points" << std::endl;
                        contourWidget->GetEventTranslator()->RemoveTranslation(
                                vtkCommand::LeftButtonPressEvent);
                        contourWidget->GetEventTranslator()->SetTranslation(
                                vtkCommand::LeftButtonPressEvent,
-                               vtkWidgetEvent::Delete);
-                       
+                               vtkWidgetEvent::Scale);
                }
+       }
 
-               // Forward events
-               vtkInteractorStyleTrackballCamera::OnKeyPress();
+       //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;
+               double* closest = new double[2]();
+               double* p = new double[2]();
+
+               p[0] = 0.2*cos(angle);
+               p[1] = 0.2*sin(angle);
+               p[2] = 0.0;
+
+               closest = GetClosesstPointToPlane(normal, p);
+               points->InsertPoint(static_cast<vtkIdType>(i), closest);
+               lineIndices[i] = static_cast<vtkIdType>(i);
        }
 
-};
-vtkStandardNewMacro(KeyPressInteractorStyle);
+       lineIndices[20] = 0;
+       lines->InsertNextCell(21, lineIndices);
+       delete[] lineIndices;
+       pd->SetPoints(points);
+       pd->SetLines(lines);
 
-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;
+       contourWidget->Initialize(pd);  
+       
+       contourWidget->Render();
+       renderer->GetActiveCamera()->SetPosition(0, 0, 2.1);
+       renderer->AddActor(planeActor);
+       renderWindow->Render();
+
+       interactor->Initialize();
+       interactor->Start();
+
+       contourWidget->Off();
+
+       return EXIT_SUCCESS;
 }