]> Creatis software - cpPlugins.git/commitdiff
contour widget file added, still in development
authorjose guzman <jose@gmail.com>
Sun, 27 Sep 2015 11:22:52 +0000 (13:22 +0200)
committerjose guzman <jose@gmail.com>
Sun, 27 Sep 2015 11:22:52 +0000 (13:22 +0200)
appli/examples/CMakeLists.txt
appli/examples/example_ContourWidget.cxx [new file with mode: 0644]

index 586eddf18764c678ea82b15248fef7bb6e52442a..a4fb5d7995ea8a760cdb9c184002cbb17627edfe 100644 (file)
@@ -18,6 +18,7 @@ SET(
   example_MPR
   )
 
+
 FOREACH(prog ${EXAMPLES_PROGRAMS}) 
   ADD_EXECUTABLE(
     ${prog}
@@ -32,6 +33,7 @@ ENDFOREACH(prog)
 
 SET(
   NOPLUGINS_EXAMPLES_PROGRAMS
+  example_ContourWidget
   ## example_ExtractDICOMSeries
   ## example_ImageGaussianModelEstimator
   ## example_ReadQuadEdgeMeshWithoutPlugins
diff --git a/appli/examples/example_ContourWidget.cxx b/appli/examples/example_ContourWidget.cxx
new file mode 100644 (file)
index 0000000..d9fad29
--- /dev/null
@@ -0,0 +1,189 @@
+#include <vtkSmartPointer.h>
+#include <vtkProperty.h>
+#include <vtkContourWidget.h>
+#include <vtkOrientedGlyphContourRepresentation.h>
+#include <vtkRenderer.h>
+#include <vtkRenderWindow.h>
+#include <vtkRenderWindowInteractor.h>
+#include <vtkCommand.h>
+#include <vtkDebugLeaks.h>
+#include <vtkCamera.h>
+#include <vtkPlane.h>
+#include <vtkPolyData.h>
+#include <vtkCellArray.h>
+#include <vtkPoints.h>
+#include <vtkMath.h>
+#include <vtkWidgetEvent.h>
+#include <vtkWidgetEventTranslator.h>
+#include <vtkInteractorStyleTrackballCamera.h>
+#include <vtkInteractorStyleImage.h>
+
+#include <vtkPNGReader.h>
+#include <vtkImageViewer2.h> 
+#include <vtkObjectFactory.h>
+#include <vtkPolyDataMapper.h>
+#include <vtkPolyData.h>
+#include <vtkActor.h>
+
+
+vtkSmartPointer<vtkContourWidget> contourWidget;
+vtkSmartPointer<vtkPolyData> pd;
+vtkSmartPointer<vtkRenderWindowInteractor> interactor;
+vtkSmartPointer<vtkOrientedGlyphContourRepresentation> contourRep;
+
+class KeyPressInteractorStyle : public vtkInteractorStyleImage//vtkInteractorStyleTrackballCamera
+{
+public:
+       static KeyPressInteractorStyle* New();
+       vtkTypeMacro(KeyPressInteractorStyle, vtkInteractorStyleTrackballCamera);
+
+       
+
+       virtual void OnKeyPress()
+       {
+               // Get the keypress
+               vtkRenderWindowInteractor *rwi = this->Interactor;
+               std::string key = rwi->GetKeySym();
+
+               // Output the key that was pressed
+               std::cout << "Pressed " << key << std::endl;
+
+               // Handle an arrow key
+               if (key == "Up")
+               {
+                       contourWidget =
+                               vtkSmartPointer<vtkContourWidget>::New();
+                       contourWidget->SetInteractor(interactor);
+                       contourWidget->SetRepresentation(contourRep);
+                       contourWidget->On();
+
+                       std::cout << "add and move points" << std::endl;
+                       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")
+               {
+                       std::cout << "Remove points" << std::endl;
+                       contourWidget->GetEventTranslator()->RemoveTranslation(
+                               vtkCommand::LeftButtonPressEvent);
+                       contourWidget->GetEventTranslator()->SetTranslation(
+                               vtkCommand::LeftButtonPressEvent,
+                               vtkWidgetEvent::Delete);
+                       
+               }
+
+               // Forward events
+               vtkInteractorStyleTrackballCamera::OnKeyPress();
+       }
+
+};
+vtkStandardNewMacro(KeyPressInteractorStyle);
+
+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;
+}