Extracting a sphere with vtkSphereWidget

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Extracting a sphere with vtkSphereWidget

TomasKiniulis
This post was updated on .
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

Below 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.

Any suggestions would be highly appreciated.

//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;
}

Reply | Threaded
Open this post in threaded view
|

Re: Extracting a sphere with vtkSphereWidget

T.J. Corona
Hello Tomas,

I'm quite surprised by the lack of information to learn this tool.

There are quite a few sources information out there on how to use VTK: a wiki, a user’s guide, a textbookgenerated documentation, and archives of this mailing list to name a few. 

 My task is to extract a sphere from a model using vtkSphereWidget and visualize its scalar atrribute distribution using vtkGlyphs.

So, you have a dataset and you wish to clip a sphere from it and draw glyphs on this sphere’s surface? There are several ways you could try this, but here’s one off the top of my head: import your dataset, construct a sphere source, use vtkProbeFilter to sample your first dataset onto your sphere, and then glyph the resulting fields on your sphere polydata. You probably could use vtkCutter to accomplish this as well. Good luck, and welcome to VTK!

Sincerely,
T.J.

On Jun 5, 2018, at 2:59 AM, TomasKiniulis <[hidden email]> wrote:

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 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 the VTK - Users mailing list archive at Nabble.com.
_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ

Search the list archives at: http://markmail.org/search/?q=vtkusers

Follow this link to subscribe/unsubscribe:
https://public.kitware.com/mailman/listinfo/vtkusers


_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ

Search the list archives at: http://markmail.org/search/?q=vtkusers

Follow this link to subscribe/unsubscribe:
https://public.kitware.com/mailman/listinfo/vtkusers
Reply | Threaded
Open this post in threaded view
|

Re: Extracting a sphere with vtkSphereWidget

TomasKiniulis
Hi T.J.

Thanks a lot for the response!

I did some progress on the task, using vtkCutter a sphere is extracted
correctly now. Although, right now, my problem is that all of the glyphs are
pointing to one direction. How can I make the glyphs point according to
scalar attribute distribution?

This is the code so far:
https://pastebin.com/yNYAEUgc
Model file used:
https://drive.google.com/file/d/1kZAFNFdw65PdiW1m1Ksstt_CKZLcFKPo/view?usp=sharing

Regards,
Tomas Kiniulis



--
Sent from: http://vtk.1045678.n5.nabble.com/VTK-Users-f1224199.html
_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ

Search the list archives at: http://markmail.org/search/?q=vtkusers

Follow this link to subscribe/unsubscribe:
https://public.kitware.com/mailman/listinfo/vtkusers
Reply | Threaded
Open this post in threaded view
|

Re: Extracting a sphere with vtkSphereWidget

T.J. Corona
Hi Tomas,

> My task is to extract a sphere from a model using vtkSphereWidget and visualize its scalar atrribute distribution using vtkGlyphs.

Your problem may be that glyphs are more appropriate for vector datasets. In which direction would you like the glyphs to point? If you which for the glyphs to point radially outward and have a magnitude dictated by some scalar attribute, I believe you will have to construct that dataset yourself (cell normals * scalar value) and set it as your dataset’s vector field. Hope this helps!

Sincerely,
T.J.

> On Jun 5, 2018, at 7:29 PM, TomasKiniulis <[hidden email]> wrote:
>
> Hi T.J.
>
> Thanks a lot for the response!
>
> I did some progress on the task, using vtkCutter a sphere is extracted
> correctly now. Although, right now, my problem is that all of the glyphs are
> pointing to one direction. How can I make the glyphs point according to
> scalar attribute distribution?
>
> This is the code so far:
> https://pastebin.com/yNYAEUgc
> Model file used:
> https://drive.google.com/file/d/1kZAFNFdw65PdiW1m1Ksstt_CKZLcFKPo/view?usp=sharing
>
> Regards,
> Tomas Kiniulis
>
>
>
> --
> Sent from: http://vtk.1045678.n5.nabble.com/VTK-Users-f1224199.html
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
>
> Search the list archives at: http://markmail.org/search/?q=vtkusers
>
> Follow this link to subscribe/unsubscribe:
> https://public.kitware.com/mailman/listinfo/vtkusers

_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ

Search the list archives at: http://markmail.org/search/?q=vtkusers

Follow this link to subscribe/unsubscribe:
https://public.kitware.com/mailman/listinfo/vtkusers