[vtkusers] Extracting a sphere with vtkSphereWidget
TomasKiniulis
tkiniulis at gmail.com
Tue Jun 5 02:59:26 EDT 2018
Hey folks, have a course in university to study The Visualization Toolkit.
I'm a complete newbie and I'm quite surprised by the lack of information to
learn this tool. So I hope you can help me here.My task is to extract a
sphere from a model using vtkSphereWidget and visualize its scalar atrribute
distribution using vtkGlyphs. How can I use a vtkSphereWidget to extract a
sphere? Do I need to create a sphere that follows around the widget? Any
examples you could suggest of such task?Found this useful example, but it's
using a plane to cut. How would I use this with a
sphere?https://www.vtk.org/Wiki/VTK/Examples/Cxx/VisualizationAlgorithms/CutterThis
is my messy code, my attempt here was to make box cut instead of a plane
one. But instead it throws me an error that "there is no suitable conversion
function from "vtkSmartPointer to "vtkImplicitFunction *" exists.//Cutter
code#include #include #include #include #include #include #include #include
#include #include #include #include #include #include #include #include
#include #include #include #include class SphereCallback : public
vtkCommand{public: static SphereCallback *New() { return new
SphereCallback; } SphereCallback() {} virtual void Execute(vtkObject
*caller, unsigned long, void*) { vtkSphereWidget *sphereWidget =
reinterpret_cast<vtkSphereWidget*>(caller); double center[3];
sphereWidget->GetCenter(center); std::cout << "Center: " << center[0] << "
" << center[1] << " " << center[2] << std::endl; }};int main(int, char *[]){
//nuskaitymas vtkSmartPointer reader = vtkSmartPointer::New();
reader->SetFileName("Models\\carotid.vtk"); reader->Update();
vtkSmartPointer carotidMapper = vtkSmartPointer::New();
carotidMapper->SetInputConnection(reader->GetOutputPort()); //Create carotid
actor vtkSmartPointer carotidActor = vtkSmartPointer::New();
carotidActor->GetProperty()->SetOpacity(0.5);
carotidActor->SetMapper(carotidMapper); //Creating plane to cut this
majestic peace of model vtkSmartPointer sphereC = vtkSmartPointer::New();
sphereC->SetXLength(40); sphereC->SetYLength(30); sphereC->SetZLength(20);
sphereC->SetOrigin(20, 0, 0); // Create carotid cutter vtkSmartPointer
cutterC = vtkSmartPointer::New(); cutterC->SetCutFunction(sphereC);
cutterC->SetInputConnection(reader->GetOutputPort()); cutterC->Update();
vtkSmartPointer cutterMapperC = vtkSmartPointer::New();
cutterMapperC->SetInputConnection(cutterC->GetOutputPort()); //
vtkSmartPointer cube = vtkSmartPointer::New(); cube->SetXLength(40);
cube->SetYLength(30); cube->SetZLength(20); vtkSmartPointer cubeMapper =
vtkSmartPointer::New();
cubeMapper->SetInputConnection(cube->GetOutputPort()); // Create a plane to
cut,here it cuts in the XZ direction (xz normal=(1,0,0);XY =(0,0,1),YZ
=(0,1,0) vtkSmartPointer plane = vtkSmartPointer::New();
plane->SetOrigin(10, 0, 0); plane->SetNormal(1, 0, 0); // Create cutter
vtkSmartPointer cutter = vtkSmartPointer::New();
cutter->SetCutFunction(plane);
cutter->SetInputConnection(cube->GetOutputPort()); cutter->Update();
vtkSmartPointer cutterMapper = vtkSmartPointer::New();
cutterMapper->SetInputConnection(cutter->GetOutputPort()); // Create plane
actor vtkSmartPointer planeActor = vtkSmartPointer::New();
planeActor->GetProperty()->SetColor(1.0, 1, 0);
planeActor->GetProperty()->SetLineWidth(2);
planeActor->SetMapper(cutterMapper); //Create second plane actor
vtkSmartPointer planeActor2 = vtkSmartPointer::New();
planeActor2->GetProperty()->SetColor(1.0, 1, 0);
planeActor2->GetProperty()->SetLineWidth(2);
planeActor2->SetMapper(cutterMapper2); //Create carotid actor
vtkSmartPointer carotidPlaneActor = vtkSmartPointer::New();
carotidPlaneActor ->GetProperty()->SetColor(1.0, 1, 0);
carotidPlaneActor->GetProperty()->SetLineWidth(2);
carotidPlaneActor->SetMapper(cutterMapperC); // Create cube actor
vtkSmartPointer cubeActor = vtkSmartPointer::New();
cubeActor->GetProperty()->SetColor(0.5, 1, 0.5);
cubeActor->GetProperty()->SetOpacity(0.5); cubeActor->SetMapper(cubeMapper);
// Create renderers and add actors of plane and cube vtkSmartPointer
renderer = vtkSmartPointer::New(); renderer->AddActor(planeActor);
//display the rectangle resulting from the cut
renderer->AddActor(cubeActor); //display the cube
renderer->AddActor(carotidActor); //display carotid
renderer->AddActor(planeActor2); //display rectangle of second cut
renderer->AddActor(carotidPlaneActor); //display rectangle of carotid cut //
Add renderer to renderwindow and render vtkSmartPointer renderWindow =
vtkSmartPointer::New(); renderWindow->AddRenderer(renderer); //Sphere
widget interactor // An interactor vtkSmartPointer renderWindowInteractor
= vtkSmartPointer::New();
renderWindowInteractor->SetRenderWindow(renderWindow); vtkSmartPointer
sphereWidget = vtkSmartPointer::New();
sphereWidget->SetInteractor(renderWindowInteractor);
sphereWidget->SetRepresentationToWireframe();
sphereWidget->SetProp3D(carotidActor); sphereWidget->SetPlaceFactor(0.5);
sphereWidget->PlaceWidget(); vtkSmartPointer cutter2 =
vtkSmartPointer::New(); cutter2->SetCutFunction(sphereWidget);
cutter2->SetInputConnection(cube->GetOutputPort()); cutter2->Update();
vtkSmartPointer cutterMapper2 = vtkSmartPointer::New();
cutterMapper2->SetInputConnection(cutter2->GetOutputPort());
sphereWidget->SetInteractor(renderWindowInteractor);
sphereWidget->SetRepresentationToSurface(); vtkSmartPointer sphereCallback
= vtkSmartPointer::New();
sphereWidget->AddObserver(vtkCommand::InteractionEvent, sphereCallback);
renderWindow->SetSize(600, 600);
renderWindowInteractor->SetRenderWindow(renderWindow);
renderer->SetBackground(0, 0, 0); renderWindow->Render(); //sphere widget
interactor renderWindowInteractor->Initialize(); renderWindow->Render();
sphereWidget->On(); renderWindowInteractor->Start(); return
EXIT_SUCCESS;}Any suggestions would be highly appreciated.
--
Sent from: http://vtk.1045678.n5.nabble.com/VTK-Users-f1224199.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://public.kitware.com/pipermail/vtkusers/attachments/20180604/a547773f/attachment.html>
More information about the vtkusers
mailing list