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/Cutter
This 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<vtkCubeSource> to "vtkImplicitFunction *" exists.
<code>//Cutter code
#include <vtkSmartPointer.h>
#include <vtkCubeSource.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkPlane.h>
#include <vtkCutter.h>
#include <vtkProperty.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkStructuredPoints.h>
#include <vtkStructuredPointsReader.h>
#include <vtkDataSetAttributes.h>
#include <vtkDataSetMapper.h>
#include <vtkGlyph3D.h>
#include <vtkSphereSource.h>
#include <vtkSphereWidget.h>
#include <vtkSphereRepresentation.h>
#include <vtkCommand.h>
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<vtkStructuredPointsReader> reader = vtkSmartPointer<vtkStructuredPointsReader>::New();
reader->SetFileName("Models\\carotid.vtk");
reader->Update();
vtkSmartPointer<vtkDataSetMapper> carotidMapper = vtkSmartPointer<vtkDataSetMapper>::New();
carotidMapper->SetInputConnection(reader->GetOutputPort());
//Create carotid actor
vtkSmartPointer<vtkActor> carotidActor =
vtkSmartPointer<vtkActor>::New();
carotidActor->GetProperty()->SetOpacity(0.5);
carotidActor->SetMapper(carotidMapper);
//Creating plane to cut this majestic peace of model
vtkSmartPointer<vtkCubeSource> sphereC =
vtkSmartPointer<vtkCubeSource>::New();
sphereC->SetXLength(40);
sphereC->SetYLength(30);
sphereC->SetZLength(20);
sphereC->SetOrigin(20, 0, 0);
// Create carotid cutter
vtkSmartPointer<vtkCutter> cutterC =
vtkSmartPointer<vtkCutter>::New();
cutterC->SetCutFunction(sphereC);
cutterC->SetInputConnection(reader->GetOutputPort());
cutterC->Update();
vtkSmartPointer<vtkDataSetMapper> cutterMapperC =
vtkSmartPointer<vtkDataSetMapper>::New();
cutterMapperC->SetInputConnection(cutterC->GetOutputPort());
//
vtkSmartPointer<vtkCubeSource> cube =
vtkSmartPointer<vtkCubeSource>::New();
cube->SetXLength(40);
cube->SetYLength(30);
cube->SetZLength(20);
vtkSmartPointer<vtkPolyDataMapper> cubeMapper =
vtkSmartPointer<vtkPolyDataMapper>::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<vtkPlane> plane =
vtkSmartPointer<vtkPlane>::New();
plane->SetOrigin(10, 0, 0);
plane->SetNormal(1, 0, 0);
// Create cutter
vtkSmartPointer<vtkCutter> cutter =
vtkSmartPointer<vtkCutter>::New();
cutter->SetCutFunction(plane);
cutter->SetInputConnection(cube->GetOutputPort());
cutter->Update();
vtkSmartPointer<vtkPolyDataMapper> cutterMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
cutterMapper->SetInputConnection(cutter->GetOutputPort());
// Create plane actor
vtkSmartPointer<vtkActor> planeActor =
vtkSmartPointer<vtkActor>::New();
planeActor->GetProperty()->SetColor(1.0, 1, 0);
planeActor->GetProperty()->SetLineWidth(2);
planeActor->SetMapper(cutterMapper);
//Create second plane actor
vtkSmartPointer<vtkActor> planeActor2 =
vtkSmartPointer<vtkActor>::New();
planeActor2->GetProperty()->SetColor(1.0, 1, 0);
planeActor2->GetProperty()->SetLineWidth(2);
planeActor2->SetMapper(cutterMapper2);
//Create carotid actor
vtkSmartPointer<vtkActor> carotidPlaneActor =
vtkSmartPointer<vtkActor>::New();
carotidPlaneActor ->GetProperty()->SetColor(1.0, 1, 0);
carotidPlaneActor->GetProperty()->SetLineWidth(2);
carotidPlaneActor->SetMapper(cutterMapperC);
// Create cube actor
vtkSmartPointer<vtkActor> cubeActor =
vtkSmartPointer<vtkActor>::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<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::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<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
//Sphere widget interactor
// An interactor
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
vtkSmartPointer<vtkSphereWidget> sphereWidget = vtkSmartPointer<vtkSphereWidget>::New();
sphereWidget->SetInteractor(renderWindowInteractor);
sphereWidget->SetRepresentationToWireframe();
sphereWidget->SetProp3D(carotidActor);
sphereWidget->SetPlaceFactor(0.5);
sphereWidget->PlaceWidget();
vtkSmartPointer<vtkCutter> cutter2 =
vtkSmartPointer<vtkCutter>::New();
cutter2->SetCutFunction(sphereWidget);
cutter2->SetInputConnection(cube->GetOutputPort());
cutter2->Update();
vtkSmartPointer<vtkDataSetMapper> cutterMapper2 =
vtkSmartPointer<vtkDataSetMapper>::New();
cutterMapper2->SetInputConnection(cutter2->GetOutputPort());
sphereWidget->SetInteractor(renderWindowInteractor);
sphereWidget->SetRepresentationToSurface();
vtkSmartPointer<SphereCallback> sphereCallback =
vtkSmartPointer<SphereCallback>::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;
}</code>
Any suggestions would be highly appreciated.
<br/><hr align="left" width="300" />
Sent from the <a href="http://vtk.1045678.n5.nabble.com/VTK-Users-f1224199.html">VTK - Users mailing list archive</a> at Nabble.com.<br/>