Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved] Converting a function to renderSTL files
QtWS25 Last Chance

[solved] Converting a function to renderSTL files

Scheduled Pinned Locked Moved General and Desktop
qt5.5.1vtk
10 Posts 2 Posters 3.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Lays147L Offline
    Lays147L Offline
    Lays147
    wrote on last edited by SGaist
    #1

    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!

    Lays Rodrigues
    Newby on Qt - Learning always!
    Using QT 5.7
    ArchLinux

    1 Reply Last reply
    0
    • A Offline
      A Offline
      alex_malyu
      wrote on last edited by
      #2

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

      Lays147L 1 Reply Last reply
      1
      • A alex_malyu

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

        Lays147L Offline
        Lays147L Offline
        Lays147
        wrote on last edited by
        #3

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

        Lays Rodrigues
        Newby on Qt - Learning always!
        Using QT 5.7
        ArchLinux

        A 1 Reply Last reply
        0
        • Lays147L Lays147

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

          A Offline
          A Offline
          alex_malyu
          wrote on last edited by
          #4

          @Lays147

          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.

          Lays147L 1 Reply Last reply
          1
          • A Offline
            A Offline
            alex_malyu
            wrote on last edited by alex_malyu
            #5

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

            1 Reply Last reply
            0
            • A Offline
              A Offline
              alex_malyu
              wrote on last edited by alex_malyu
              #6

              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.

              1 Reply Last reply
              0
              • A alex_malyu

                @Lays147

                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.

                Lays147L Offline
                Lays147L Offline
                Lays147
                wrote on last edited by
                #7

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

                Lays Rodrigues
                Newby on Qt - Learning always!
                Using QT 5.7
                ArchLinux

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  alex_malyu
                  wrote on last edited by
                  #8

                  @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 vtkRenderWindow

                  Normally 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 green

                  renderer->ResetCamera();
                  renderWindow->Render();
                  }

                  Try above version which should work.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    alex_malyu
                    wrote on last edited by
                    #9

                    @alex_malyu said:

                    vtkWidget2

                    just making sure vtkWidget2 is a subclass of QVtkWidget?

                    Lays147L 1 Reply Last reply
                    0
                    • A alex_malyu

                      @alex_malyu said:

                      vtkWidget2

                      just making sure vtkWidget2 is a subclass of QVtkWidget?

                      Lays147L Offline
                      Lays147L Offline
                      Lays147
                      wrote on last edited by
                      #10

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

                      Lays Rodrigues
                      Newby on Qt - Learning always!
                      Using QT 5.7
                      ArchLinux

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved