#include <vtkActor.h>
#include <vtkPlaneSource.h>
#include <vtkInteractorObserver.h>
+#include <cmath>
vtkSmartPointer<vtkContourWidget> contourWidget;
vtkSmartPointer<vtkPolyData> pd;
};
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
virtual void Execute(vtkObject *caller, unsigned long eid, void* clientData)
{
+
vtkContourWidget *contourWidget =
reinterpret_cast<vtkContourWidget*>(caller);
// 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();
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
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);
}
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);