From: jose guzman Date: Sun, 27 Sep 2015 11:22:52 +0000 (+0200) Subject: contour widget file added, still in development X-Git-Tag: v0.1~361 X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=e9fba2cf4f290e7dbdd195086025737863c538cd;p=cpPlugins.git contour widget file added, still in development --- diff --git a/appli/examples/CMakeLists.txt b/appli/examples/CMakeLists.txt index 586eddf..a4fb5d7 100644 --- a/appli/examples/CMakeLists.txt +++ b/appli/examples/CMakeLists.txt @@ -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 index 0000000..d9fad29 --- /dev/null +++ b/appli/examples/example_ContourWidget.cxx @@ -0,0 +1,189 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + + +vtkSmartPointer contourWidget; +vtkSmartPointer pd; +vtkSmartPointer interactor; +vtkSmartPointer 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::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 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; +}