]> Creatis software - cpPlugins.git/commitdiff
contour widget example done
authorjose guzman <jose@gmail.com>
Tue, 29 Sep 2015 09:01:26 +0000 (11:01 +0200)
committerjose guzman <jose@gmail.com>
Tue, 29 Sep 2015 09:01:26 +0000 (11:01 +0200)
appli/examples/example_ContourWidget.cxx

index 5e5352a416c4aa6e88be8fd84716ed43706569bb..a523ce6c1c654839e0ef38e183884995e23c179b 100644 (file)
@@ -26,6 +26,7 @@
 #include <vtkActor.h>
 #include <vtkPlaneSource.h>
 #include <vtkInteractorObserver.h>
+#include <cmath>
 
 vtkSmartPointer<vtkContourWidget> contourWidget;
 vtkSmartPointer<vtkPolyData> pd;
@@ -93,6 +94,20 @@ public:
 };
 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
@@ -106,6 +121,7 @@ public:
 
        virtual void Execute(vtkObject *caller, unsigned long eid, void* clientData)
        {
+               
 
                vtkContourWidget *contourWidget =
                        reinterpret_cast<vtkContourWidget*>(caller);
@@ -118,24 +134,35 @@ public:
 
                // get the node postiion (the world position)
                vtkContourRepresentation * rep=contourWidget->GetContourRepresentation();
-               double* nodepos = new double[2]();
-               rep->GetActiveNodeWorldPosition(nodepos);
+               double* worldclick = new double[2]();
+               rep->GetActiveNodeWorldPosition(worldclick);
                
-               std::cout << "widget click " << nodepos[0] << " "<< nodepos[1] << " " << nodepos[2] << std::endl;       
+               std::cout << "worldclick click " << worldclick[0] << " " << worldclick[1] << " " << worldclick[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
+               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
 
-               rep->SetActiveNodeToWorldPosition(restringedPosition);
+               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();
@@ -149,10 +176,30 @@ int main(int argc, char *argv[])
        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
@@ -198,8 +245,15 @@ int main(int argc, char *argv[])
        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);
+               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);
        }
 
@@ -209,28 +263,7 @@ int main(int argc, char *argv[])
        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->Initialize(pd);  
        
        contourWidget->Render();
        renderer->GetActiveCamera()->SetPosition(0, 0, 2.1);