[solved] Converting a function to renderSTL files
-
Hi folks!
I'm trying to convert this function http://www.vtk.org/Wiki/VTK/Examples/Cxx/IO/ReadSTL to render the STL file inside a QVTKWidget inside my main window.
But i'm stocked, cause i dont know almost nothing about VTK. I did this function:void vtkWidget2::renderSTL(QString pathStl) { vtkSmartPointer<vtkSTLReader> reader = vtkSmartPointer<vtkSTLReader>::New(); reader->SetFileName(pathStl.toStdString().c_str()); reader->Update(); // Visualize 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(); vtkSmartPointer<vtkRenderWindow> renderWindow = this->GetRenderWindow(); renderWindow->AddRenderer(renderer); vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New(); renderWindowInteractor->SetRenderWindow(renderWindow); renderer->AddActor(actor); renderer->SetBackground(.3, .6, .3); // Background color green renderWindow->Render(); renderWindowInteractor->Start(); }
And i dont change almost nothing, when i run my app, a popup window is made. So my doubt is: How to return the render to my vtkWidget on main window?
Thanks! -
@Lays147 said:
vtkSmartPointer<vtkRenderWindow> renderWindow = this->GetRenderWindow();
VTK is based on reference counting and even though it does provide smart pointers they should be used with care.
My expectation is that this line will transfer ownership of object you return with "this->GetRendererWindow()" to renderWindow.
and this object is mostly likely will be deleted as soon renderWindow go out of scope.This is definitely not what you want.
-
@alex_malyu
The original code of this line is:
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
That line that you mark, is just a test that im trying. -
QVTKWidget creates its own render window which you should use.
It can be returned with GetRenderWindow();if you do not like to write GetRenderWindow()->...;
you can use:
vtkRenderWindow* renderWindow = this->GetRenderWindow();It is simply wrong to use smart pointer in such case.
Also I highly recommend to subscribe to vtk mailing list and asks questions there.
There many more people there who use both Qt and VTK than here. -
@Lays147 said:
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
And yes above line is correct, cause new instance of vtkRenderWindow is created and is going to be deleted when you go out of scope,
Difference is that everything in this case is done within the scope, and window can be deleted,
when in your case it should persist after you exit the function. -
One more thing which can make a difference
AFAIK you should not or at least do not have to create your own interactor when you work withy QVtkWidget.this->GetRenderWindow()->GetInteractor() will give you a pointer if you would need it ( for example to get or set vtkInteractorStyle.
-
@alex_malyu its so messed up all this stuff to me... I'm all day trying to discover how do this code works inside my widget.
I already subscribe the mail list, and there i receive the same tip that you gave.
Is the first day that i'm work with VTK, and try to understand is dificult. But i'm not giving up. I cant.
So on my event i call this function vtkWidget->renderSTL(pathtoSTLFile)
How to make the change of context? i will substitute that line to those that you said? What is the moment that i use GetRenderWindow()? I will need to return something to my main?
GetRenderWindow returns a window of vtkGenericOpenGLRenderWindow* its different that vtkRenderWindow. I will need convert?
Thanks. -
@Lays147 said:
vtkGenericOpenGLRenderWindow
//! QVTKWidget displays a VTK window in a Qt window.
class VTKGUISUPPORTQT_EXPORT QVTKWidget : public QWidget
....// Description:
// Get the vtk render window.
virtual vtkRenderWindow* GetRenderWindow();
...As you see above vtkRenderWindow* GetRenderWindow(); returns
pointer to vtkRenderWindowNormally you should not care which subclass was really allocated unless you really need to write platform/implementation specific code. Which in your case is not applicable.
Just get back to your code,
void vtkWidget2::renderSTL(QString pathStl)
{
vtkSmartPointer<vtkSTLReader> reader = vtkSmartPointer<vtkSTLReader>::New();
reader->SetFileName(pathStl.toStdString().c_str());
reader->Update();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();
vtkRenderWindow* renderWindow = this->GetRenderWindow();renderWindow->AddRenderer(renderer);
renderer->AddActor(actor);
renderer->SetBackground(.3, .6, .3); // Background color greenrenderer->ResetCamera();
renderWindow->Render();
}Try above version which should work.
-
-
@alex_malyu I found the problem. Im use the class QVTKWidget2 where the return of the GetRenderView is different. Now i change to QVTKWidget and what you said make coherence to me.
vtkWidget is just the name of the class that i make. dont cate.