vtkSelectVisiblePoints filter output has 0 points - c++

I’m trying to obtain a polyData with only the visible part of a 3D model.
To that extent, I’m passing the original data through a vtkSelectVisiblePoints filter.
I’m using a mock renderer, mapper and actor because I want to post-process the visible points before displaying them.
However, the output of the vtkSelectVisiblePoints filter contains “0” points for some reason…
Using the following example:
http://www.vtk.org/Wiki/VTK/Examples/Cxx/PolyData/SelectVisiblePoints
I came up with the following piece of code:
// Render window and interactor
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->SetSize(800, 800);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
// Mock renderer, mapper and actor
vtkSmartPointer<vtkRenderer> mockRenderer =
vtkSmartPointer<vtkRenderer>::New();
renderWindow->AddRenderer(mockRenderer);
vtkSmartPointer<vtkPolyDataMapper> mockMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mockMapper->SetInput(reader->GetOutput());
vtkSmartPointer<vtkActor> mockActor = vtkSmartPointer<vtkActor>::New();
mockActor->SetMapper(mockMapper);
mockRenderer->AddActor(mockActor);
// Set camera to the correct position
mockRenderer->GetActiveCamera()->SetPosition(0, -1, 0);
mockRenderer->GetActiveCamera()->SetFocalPoint(0, 0, 0);
mockRenderer->GetActiveCamera()->SetViewUp(0, 1, 0);
mockRenderer->ResetCamera();
vtkSmartPointer<vtkSelectVisiblePoints> selectVisiblePoints =
vtkSmartPointer<vtkSelectVisiblePoints>::New();
selectVisiblePoints->SetInput(reader->GetOutput());
selectVisiblePoints->SetRenderer(mockRenderer);
selectVisiblePoints->Update();
std::cout << "Visible nr of points = " << selectVisiblePoints->GetOutput()->GetNumberOfPoints() << std::endl;
renderWindow->RemoveRenderer(mockRenderer);
… prints 0.
However, if I call renderWindow->Render(), the visible part of the model is correctly displayed...
Am I missing anything...?

The answer was right there, in the documentation of the filter:
Warning You must carefully synchronize the execution of this filter.
The filter refers to a renderer, which is modified every time a render
occurs. Therefore, the filter is always out of date, and always
executes. You may have to perform two rendering passes
It worked as expected if I added the //new lines, like Arnas also suggested in his comment:
vtkSmartPointer<vtkSelectVisiblePoints> selectVisiblePoints =
vtkSmartPointer<vtkSelectVisiblePoints>::New();
selectVisiblePoints->SetInput(originalData);
selectVisiblePoints->SetRenderer(renderer);
selectVisiblePoints->Update();
renderWindow->Render(); // new
selectVisiblePoints->Update(); // new
std::cout << "Visible nr of points = " << selectVisiblePoints->GetOutput()->GetNumberOfPoints() << std::endl;

Related

How can I use the vtkStreamTracer class to generate a streamline with my unstructured grid data(vtu)

I'm using the VTK and C++ to visualize data coming from xflow in vtu. When I use the Paraview, it can generate the right streamline, but when I use the VTK to visualize my data, it only displays the outline and the airplane model. There is no streamline being generated.
vtkSmartPointer<vtkNamedColors> namedColors = vtkSmartPointer<vtkNamedColors>::New();
//The AirplaneModel file
std::string meshfilename = "K:/Project/VTKproject/Airplane_Process/data/Mesh.vtp";
vtkSmartPointer<vtkXMLPolyDataReader> meshreader = vtkSmartPointer<vtkXMLPolyDataReader>::New();
meshreader->SetFileName(meshfilename.c_str());
meshreader->Update();
//The data file
std::string datafilename ="K:/Project/VTKproject/Airplane_Process/data/data.vtu";
vtkSmartPointer<vtkXMLUnstructuredGridReader> datareader = vtkSmartPointer<vtkXMLUnstructuredGridReader>::New();
datareader->SetFileName(datafilename.c_str());
datareader->Update();
//The seed points
vtkSmartPointer<vtkPointSource> seeds = vtkSmartPointer<vtkPointSource>::New();
seeds->SetCenter(0,0,0);
seeds->SetRadius(0.05);
seeds->SetNumberOfPoints(100);
//streamline generate
vtkSmartPointer<vtkStreamTracer> streamline = vtkSmartPointer<vtkStreamTracer>::New();
datareader->Update();
streamline->SetInputConnection(datareader->GetOutputPort());
streamline->SetSourceConnection(seeds->GetOutputPort());
//streamline->SetIntegrationStepUnit(vtkStreamTracer::CELL_LENGTH_UNIT);
streamline->SetMaximumPropagation(500);
streamline->SetMaximumIntegrationStep(0.1);
streamline->SetMinimumIntegrationStep(0.01);
streamline->SetInitialIntegrationStep(0.05);
//streamline->SetIntegrationStepUnit(vtkStreamTracer::CELL_LENGTH_UNIT);
streamline->SetIntegrationDirectionToBoth();
streamline->SetIntegratorTypeToRungeKutta45();
streamline->Update();
vtkSmartPointer<vtkPolyDataMapper> streamLineMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
streamLineMapper->SetInputConnection(streamline->GetOutputPort());
vtkSmartPointer<vtkActor> streamLineActor = vtkSmartPointer<vtkActor>::New();
streamLineActor->SetMapper(streamLineMapper);
//outlinefilter
vtkSmartPointer<vtkOutlineFilter> outline =vtkSmartPointer<vtkOutlineFilter>::New();
outline->SetInputConnection(datareader->GetOutputPort());
vtkSmartPointer<vtkPolyDataMapper> outlineMapper =vtkSmartPointer<vtkPolyDataMapper>::New();
outlineMapper->SetInputConnection(outline->GetOutputPort());
vtkSmartPointer<vtkActor> outlineActor =vtkSmartPointer<vtkActor>::New();
outlineActor->SetMapper(outlineMapper);
outlineActor->GetProperty()->SetColor(0,0,0);
//
vtkSmartPointer<vtkPolyDataMapper> meshmapper = vtkSmartPointer<vtkPolyDataMapper>::New();
meshmapper->SetInputConnection(meshreader->GetOutputPort());
vtkSmartPointer<vtkActor> meshactor = vtkSmartPointer<vtkActor>::New();
meshactor->SetMapper(meshmapper);
//rendering
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(meshactor);
renderer->AddActor(outlineActor);
renderer->AddActor(streamLineActor);
//renderer->AddActor(actor);
renderer->SetBackground(namedColors->GetColor3d("Cadet").GetData());
//window
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
//interaction
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
vtkSmartPointer<vtkInteractorStyleTrackballCamera> style =
vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
renderWindowInteractor->SetInteractorStyle(style);
renderWindowInteractor->Initialize();
renderWindow->SetSize(300, 300);
renderWindow->Render();
renderWindowInteractor->Start();
This is my desired output generated in paraview
This is the result of my code:
Any ideas on how to solve this?
I've been searching more or less the same thing. I tried your code on my data. And I also didnt see the result. On the other hand I did a debug statement :
streamline->Print(std::cout);
streamline->GetOutput()->Print(std::cout);
and my result is :
vtkPStreamTracer (0x555c851095d0)
Debug: Off
Modified Time: 1444545
Reference Count: 2
Registered Events: (none)
Executive: 0x555c854fb270
ErrorCode: Success
Information: 0x555c8556c100
AbortExecute: Off
Progress: 1
Progress Text: (None)
Start position: 0 0 0
Terminal speed: 1e-12
Maximum propagation: 500 unit: length.
Integration step unit: cell length.
Initial integration step: 0.05
Minimum integration step: 0.01
Maximum integration step: 0.1
Integration direction: both directions.
Integrator: 0x555c85572320
Maximum error: 1e-06
Maximum number of steps: 2000
Vorticity computation: On
Rotation scale: 1
Controller: 0
So I think the problem might be more about something else ?
Have you already solved it ?
Sorry I'm new here also, couldnt find the "comment" button.

VTK Iso Slider not working or not showing slider widget

I have some crazy issues in my code, and these issue is:
1) As you see in code below, if I execute it I will got a 3D preview model without sliderWidget. (Fixed)
2) If I change this line from mapper->SetInputConnection(surface->GetOutputPort()); to mapper->SetInputConnection(reader->GetOutputPort()); I will got a sliderWidget but I will not see a 3D model. (Fixed)
3) If I try to set custom key event, its will be work and change value but every change I need to wait until its reload or build 3d dicom (its work like you run and create a 3D model every time not like a slider).
class vtkSliderCallback : public vtkCommand
{
public:
int counter = 1;
static vtkSliderCallback *New()
{
return new vtkSliderCallback;
}
virtual void Execute(vtkObject *caller, unsigned long, void*)
{
vtkSliderWidget *sliderWidget =
reinterpret_cast<vtkSliderWidget*>(caller);
cout << this->counter << endl;
this->SphereSource->SetValue(this->counter++, static_cast<vtkSliderRepresentation *>(sliderWidget->GetRepresentation())->GetValue());
}
vtkSliderCallback():SphereSource(0) {}
vtkMarchingCubes *SphereSource;
};
int main(int argc, char* argv[])
{
// Verify input arguments
if ( argc < 4 )
{
std::cout << "Usage: " << argv[0]
<< " DicomSiresFolder" << " isoValueStep" << " OutputDirectory" << std::endl;
return EXIT_FAILURE;
}
std::string folder = argv[1];
// A sphere
vtkSmartPointer<vtkImageData> volume =
vtkSmartPointer<vtkImageData>::New();
vtkSmartPointer<vtkDICOMImageReader> reader =
vtkSmartPointer<vtkDICOMImageReader>::New();
reader->SetDirectoryName(folder.c_str());
reader->Update();
volume->DeepCopy(reader->GetOutput());
vtkSmartPointer<vtkMarchingCubes> surface =
vtkSmartPointer<vtkMarchingCubes>::New();
surface->SetInputData(volume);
surface->ComputeNormalsOn();
surface->SetValue(0, 400);
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(surface->GetOutputPort());
mapper->ScalarVisibilityOff();
vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
actor->GetProperty()->SetInterpolationToFlat();
// A renderer and render window
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
// An interactor
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
// Add the actors to the scene
renderer->AddActor(actor);
// Render an image (lights and cameras are created automatically)
renderWindow->Render();
vtkSmartPointer<vtkSliderRepresentation3D> sliderRep =
vtkSmartPointer<vtkSliderRepresentation3D>::New();
sliderRep->SetMinimumValue(-800.0);
sliderRep->SetMaximumValue(800.0);
sliderRep->SetTitleText("Iso Resolution");
sliderRep->GetPoint1Coordinate()->SetCoordinateSystemToWorld();
sliderRep->GetPoint1Coordinate()->SetValue(-4,6,0);
sliderRep->GetPoint2Coordinate()->SetCoordinateSystemToWorld();
sliderRep->GetPoint2Coordinate()->SetValue(4,6,0);
sliderRep->SetSliderLength(0.075);
sliderRep->SetSliderWidth(0.05);
sliderRep->SetEndCapLength(0.05);
vtkSmartPointer<vtkSliderWidget> sliderWidget =
vtkSmartPointer<vtkSliderWidget>::New();
sliderWidget->SetInteractor(renderWindowInteractor);
sliderWidget->SetRepresentation(sliderRep);
sliderWidget->SetAnimationModeToAnimate();
sliderWidget->EnabledOn();
vtkSmartPointer<vtkSliderCallback> callback =
vtkSmartPointer<vtkSliderCallback>::New();
callback->SphereSource = surface;
sliderWidget->AddObserver(vtkCommand::InteractionEvent,callback);
renderWindowInteractor->Initialize();
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
UPDATE:
I Fixed issue on point 1 and 2, but still Issue 3 needed to fix.
Your callback is changing the value for vtkMarchingCube, so you're running a marching cubes every time, so yes, it's rebuilding a mesh every time. As far as I understand what you need to do, one solution can be to precompute vtkMarchingCubes output for all possible values: not elegant (but it could work if you have enough memory).
In any case, use vtkFlyingEdges3D instead of vtkMarchingCubes, it's much faster (it could be fast enough to solve your problem without any other modification).

vtk c++ update contour from contourfilter

I have a 3D .vtk model that I render and I extract the contour from the resulting image using a vtkContourFilter (using vtk version 7.0.0 on Ubuntu 16.04).
I would like to project it from different perspectives, but as I loop over the different camera positions (I checked that the camera positions are indeed changed) the interactive viewer that launches with each iteration always shows the contour from the first image.
When I output the first few coordinates of the found contour points (which I store as a vtkPolyData) I also noticed that the content in my set of contour points does not change.
I have tried some online suggestions that worked for others, such as adding:
ContFilter->Modified();
ContFilter->Update();
and
polyData->Modified(); // This is the 3D vtkPolyData that I project
and
ContFilter->SetValue(0, 10);
ContFilter->SetValue(0, 255);
As a wild guess I also tried adding:
polyData->Modified();
// Remove old links
renderWindow->RemoveRenderer(renderer);
mapper->RemoveAllInputs();
// Set new links
renderer->SetActiveCamera(camera);
renderWindow->AddRenderer(renderer);
renderer->Modified();
renderer->ResetCameraClippingRange();
renderWindow->Modified();
mapper->SetInputData(polyData);
renderWindow->Render();
within the for loop, before using the ContourFilter, but it still does not update. With that I tried everything I could think of and find online.
This is the relevant code:
// Prepare the rendering environment to project the 3D model to an image from different perspectives
vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New();
mapper->SetInputData(polyData);
mapper->ScalarVisibilityOff();
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
actor->GetProperty()->SetInterpolationToFlat();
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->SetBackground(1,1,1);
renderer->AddActor(actor);
vtkSmartPointer<vtkCamera> camera = vtkSmartPointer<vtkCamera>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->SetOffScreenRendering(1);
vtkSmartPointer<vtkWindowToImageFilter> windowToImageFilter = vtkSmartPointer<vtkWindowToImageFilter>::New();
vtkSmartPointer<vtkContourFilter> ContFilter = vtkSmartPointer<vtkContourFilter>::New();
vtkSmartPointer<vtkPolyData> contour = vtkSmartPointer<vtkPolyData>::New();
// Loop over the camera positions. At each iteration render/project,
// extract the contour and finally render the 3D model and the found
// contour
double * iPoint;
double * camPos;
double * contourStart;
int nContours;
for(int i=0; i<positions->GetNumberOfPoints(); i++){
// Print the camera position
iPoint = positions->GetPoint(i);
std::cout << iPoint[0] << " " << iPoint[1] << " " << iPoint[2] << std::endl;
//Move camera
camera->SetPosition(iPoint[0], iPoint[1], iPoint[2]);
camera->SetFocalPoint(focalPointOrig[0], focalPointOrig[1], focalPointOrig[2]);
camera->SetViewAngle(viewAngle);
camera->Modified();
camera->SetRoll(90);
// Does this help to update the view?
polyData->Modified();
// Remove old links and set them again
renderWindow->RemoveRenderer(renderer);
mapper->RemoveAllInputs();
renderer->SetActiveCamera(camera);
renderWindow->AddRenderer(renderer);
renderer->Modified();
renderer->ResetCameraClippingRange();
renderWindow->Modified();
// Render/project the data
mapper->SetInputData(polyData);
renderWindow->Render();
// Print camera position for debugging
camera->GetPosition(camPos);
std::cout << camPos[0] << " " << camPos[1] << " " << camPos[2] << std::endl;
// Get the image and apply a contourfilter
windowToImageFilter->SetInput(renderWindow);
windowToImageFilter->Update();
ContFilter->SetInputConnection(windowToImageFilter->GetOutputPort());
// Saw someone do this as a workaround for updating the view
ContFilter->SetValue(0, 10);
ContFilter->SetValue(0, 255);
// Does this help to update the view?
ContFilter->Modified();
//Get the contour from the contourfilter
ContFilter->Update();
contour = ContFilter->GetOutput();
// Print the first points coordinates to see if they changed
contourStart = contour->GetPoint(1);
std::cout << contourStart[0] << " " << contourStart[1] << " " << std::endl;
// Print the number of contours to see if it may be stored as an additional contour
nContours = ContFilter->GetNumberOfContours();
std::cout << nContours << std::endl;
// Render the 3D model and the found contour
actor->GetProperty()->SetColor(0.9,0.9,0.8);
// Create a mapper and actor of the silhouette
vtkSmartPointer<vtkPolyDataMapper> mapper_contour = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper_contour->SetInputData(contour);
// Try this again here
polyData->Modified();
vtkSmartPointer<vtkActor> actor_contour = vtkSmartPointer<vtkActor>::New();
actor_contour->SetMapper(mapper_contour);
actor_contour->GetProperty()->SetLineWidth(2.);
// 2 renderers and a render window
vtkSmartPointer<vtkRenderer> renderer1 = vtkSmartPointer<vtkRenderer>::New();
renderer1->AddActor(actor);
vtkSmartPointer<vtkRenderer> renderer2 = vtkSmartPointer<vtkRenderer>::New();
renderer2->AddActor(actor_contour);
// Set the 3D model renderer to the same perspective but don't change the camera perspective of the contour
renderer1->SetActiveCamera(camera);
// Setup the window
vtkSmartPointer<vtkRenderWindow> renderwindow = vtkSmartPointer<vtkRenderWindow>::New();
renderwindow->SetSize(1600, 800);
renderwindow->AddRenderer(renderer1);
renderer1->SetViewport(0., 0., 0.5, 1.);
renderwindow->AddRenderer(renderer2);
renderer2->SetViewport(0.5, 0., 1., 1.);
// Setup the interactor
vtkSmartPointer<vtkInteractorStyleTrackballCamera> style = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
iren->SetRenderWindow( renderwindow);
iren->SetInteractorStyle(style);
// Display the coordinate system axes
vtkSmartPointer<vtkAxesActor> axes = vtkSmartPointer<vtkAxesActor>::New();
vtkSmartPointer<vtkOrientationMarkerWidget> widget = vtkSmartPointer<vtkOrientationMarkerWidget>::New();
widget->SetOutlineColor( 0.9300, 0.5700, 0.1300 );
widget->SetOrientationMarker( axes );
widget->SetInteractor( iren );
widget->SetViewport( 0.0, 0.0, 0.4, 0.4 );
widget->SetEnabled( 1 );
widget->InteractiveOn();
// Render the 3D model and the found contour
renderwindow->Render();
iren->Start();
}
Just found the answer.
As mentioned in the warning in the detailed description of the vtkWindowToImageFilter class reference webpage (https://www.vtk.org/doc/nightly/html/classvtkWindowToImageFilter.html), vtkWindows generally do not rerender unless you call their Modified() function. Now my projected views are updated like I wanted.
So I changed
// Get the image and apply a contourfilter
windowToImageFilter->SetInput(renderWindow);
windowToImageFilter->Update();
to
// Get the image and apply a contourfilter
windowToImageFilter->Modified();
windowToImageFilter->SetInput(renderWindow);
windowToImageFilter->Update();
See here the warning text in case the link above ever stops working:
Warning:
A vtkWindow doesn't behave like other parts of the VTK pipeline: its modification time doesn't get updated when an image is rendered. As a result, naive use of vtkWindowToImageFilter will produce an image of the first image that the window rendered, but which is never updated on subsequent window updates. This behavior is unexpected and in general undesirable.
To force an update of the output image, call vtkWindowToImageFilter's Modified method after rendering to the window.
In VTK versions 4 and later, this filter is part of the canonical way to output an image of a window to a file (replacing the obsolete SaveImageAsPPM method for vtkRenderWindows that existed in 3.2 and earlier). Connect this filter to the output of the window, and filter's output to a writer such as vtkPNGWriter.
Reading back alpha planes is dependent on the correct operation of the render window's GetRGBACharPixelData method, which in turn is dependent on the configuration of the window's alpha planes. As of VTK 4.4+, machine-independent behavior is not automatically assured because of these dependencies.

Modifying the polyData points/vertices in VTK

I'm trying to use VTK to change points, i.e. I am changing coordinates (x,y,z) by a (+1/-1) value. I am reading an .OBJ file and then accessing the points of a model and when I am trying to visualise the changes in the model the RenderWindow doesnt show anything. Below is my code:-
vtkSmartPointer<vtkOBJReader> reader = vtkSmartPointer<vtkOBJReader>::New();
reader->SetFileName(filename.c_str());
reader->Update();
vtkSmartPointer<vtkPolyData> polyData = reader->GetOutput();
polyData->Update();
Point3d point;
std::vector<Point3d> vertices;
double p[3];
vtkPoints* points = vtkPoints::New();
vtkDoubleArray* pcoord = vtkDoubleArray::New();
pcoord->SetNumberOfComponents(3);
pcoord->SetNumberOfTuples(polyData->GetNumberOfPoints());
for(vtkIdType i = 0; i < polyData->GetNumberOfPoints(); i++)
{
polyData->GetPoint(i,p);
p[0] +=1;
p[1] +=1;
p[2] +=1;
pcoord->SetTuple(i, p);
}
points->SetData(pcoord);
polyData->SetPoints(points);
polyData->Modified();
//Visualize Code
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(reader->GetOutputPort());
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(actor);
vtkRenderWindow* renderWindow = vtkRenderWindow::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();
I am new to this 3D and VTK stuff, Please check where I am missing since I want to change the coordinates of each vertex (x,y,z). Do I need to further change something else as well?
Many Thanks.
First thing, you have alignment issues in the code that you included in your question. I believe you are missing a for loop too. (where is "i" defined?)
The major issue is that you are not rendering polyData at all. The only thing you are rendering is what is in the .obj file. Evident from this line:
mapper->SetInputConnection(reader->GetOutputPort());
In order to render polyData replace the above line with:
mapper->SetInputData(polyData);
This assumes polyData was created correctly. I can't easily tell this from the code you provided.
Here is an example that may help.

Program Stops automatically right after a short render

I'm having a small issue with VTK. When I launch my program, it starts to render and then automatically stops while it should actually keep on rendering.
I cannot see where that error may come from. I wrote an other piece of code for the exact same rendering which works just fine.
So here is the constructor of my class:
Drawing::Drawing(void)
{
translation = vtkSmartPointer<vtkTransform>::New();
ctxView = vtkContextView::New();
win = ctxView->GetRenderWindow();
ren = ctxView->GetRenderer();
cam = ren->GetActiveCamera();
ren->SetBackground(.0,.0,.0);
}
Here is the piece of code not working:
void Drawing::read(){
std::string filename = BUNNY;
// Read all the data from the file
vtkSmartPointer<vtkXMLPolyDataReader> reader =vtkSmartPointer<vtkXMLPolyDataReader>::New();
reader->SetFileName(filename.c_str());
reader->Update();
cout << "File Found and Loaded : " << filename << endl ;
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(reader->GetOutputPort());
mainActor = vtkSmartPointer<vtkActor>::New();
mainActor->SetMapper(mapper);
ren->AddActor(mainActor);
win->PolygonSmoothingOn();
win->Render();
win->Start();
}
And here is the piece of code working:
void otherRead(){
std::string filename = BUNNY;
// Read all the data from the file
vtkSmartPointer<vtkXMLPolyDataReader> reader =vtkSmartPointer<vtkXMLPolyDataReader>::New();
reader->SetFileName(filename.c_str());
reader->Update();
cout << "File Found and Loaded : " << filename << endl ;
// Visualize
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(reader->GetOutputPort());
mainActor = vtkSmartPointer<vtkActor>::New();
mainActor->SetMapper(mapper);
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renderWindow);
vtkInteractorStyleMultiTouchCamera *style =
vtkInteractorStyleMultiTouchCamera::New();
iren->SetInteractorStyle(style);
renderer->AddActor(mainActor);
renderer->SetBackground(0,0,0); // Background color green
renderWindow->PolygonSmoothingOn();
renderWindow->Render();
renderWindowInteractor->Start();
}
I don't know if it's a vtk or a c++ problem actually. The output of the not-working piece of code is just a few milliseconds window and then the program exists while the otherRead() gives a normal rendering without exiting the program.
Thanks in advance for the help
So it was indeed a vtk problem. One very basic some might say but still not that easy to figure out when you're starting with vtk.
I'm posting the answer, the documentation is not big on vtk so it might help other newbies. So in fact, the event loop for the window has to be started. It can be done thanks to the following lines:
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(win);
vtkInteractorStyleMultiTouchCamera *style =
vtkInteractorStyleMultiTouchCamera::New();
iren->SetInteractorStyle(style);
//Start the event loop
iren->Initialize();
iren->Start();
Hope this helps :)