From: jose guzman Date: Tue, 29 Sep 2015 09:01:26 +0000 (+0200) Subject: contour widget example done X-Git-Tag: v0.1~358 X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=8919b8f97d766e10514bdd765d597e08693cc7a2;p=cpPlugins.git contour widget example done --- diff --git a/appli/examples/example_ContourWidget.cxx b/appli/examples/example_ContourWidget.cxx index 5e5352a..a523ce6 100644 --- a/appli/examples/example_ContourWidget.cxx +++ b/appli/examples/example_ContourWidget.cxx @@ -26,6 +26,7 @@ #include #include #include +#include vtkSmartPointer contourWidget; vtkSmartPointer 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(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 renderer = vtkSmartPointer::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 planeSource = + vtkSmartPointer::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 mapper = + vtkSmartPointer::New(); + mapper->SetInputData(plane); + + vtkSmartPointer planeActor = + vtkSmartPointer::New(); + planeActor->SetMapper(mapper); + /*vtkSmartPointer style = vtkSmartPointer::New(); interactor->SetInteractorStyle(style);*/ + // create the contour stuff vtkSmartPointer contourRep = vtkSmartPointer::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(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(i), closest); lineIndices[i] = static_cast(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 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->Initialize(pd); contourWidget->Render(); renderer->GetActiveCamera()->SetPosition(0, 0, 2.1);