[vtkusers] Rendering during ITK Registration

Pat LeSmithe qed777 at gmail.com
Wed Sep 17 04:40:11 EDT 2008


Hello, Jaety,

Did you happen to get a response about your question?  I'm curious,
especially about the best practice, because I have a structurally
similar problem.  What I've done on Linux in C++ is define a callback
function

class vtkMyCallback : public vtkCommand
{
public:
  static vtkMyCallback *New()
    { return new vtkMyCallback; }
  virtual void Execute(vtkObject *caller, unsigned long, void*)
    {
      vtkRenderWindowInteractor *iren =
        reinterpret_cast<vtkRenderWindowInteractor*>(caller);

      iren->Render();
      iren->CreateTimer(VTKI_TIMER_UPDATE);
    }
};

which I add in the "interactor" thread:

#define VTK_CREATE(type, name) \
  vtkSmartPointer<type> name = vtkSmartPointer<type>::New()
[...]
iren->Initialize();
VTK_CREATE(vtkMyCallback, mo1);
iren->AddObserver(vtkCommand::TimerEvent, mo1);
iren->CreateTimer(VTKI_TIMER_FIRST);
iren->Start();
[...]

I believe VTK's X timer auto-expires after 10 ms.  With the
TrackballCamera style, this is pretty close to what I need, though there
is a bit of "drift".  That is, it's mostly TrackballCamera with a small
admixture of JoystickCamera.

I recommend adding synchronization directives, e.g., mutex locks, around
the iren->Render() call in the callback and the data->Modified() call
--- or equivalent --- in the "worker" thread.  Calling the latter
ensures the former retrieves the new data on the next render.  (Thanks
for Burlen Loring for this and other tips.)

Please let me know if I'm misinterpreting the problem --- I'm not
familiar with ITK --- or if there is a better way short of writing an
interactor style from scratch.  Is Qt or KW the way to go?  Is this a FAQ?

Thanks.

Sincerely,
Pat LeSmithe

Jaety Edwards wrote:
> Hello all,
> I have a question about threading and VTK. Specifically, about how I can
> have a renderWindow that can be updated either from a renderWindowInteractor
> running on one thread, or from a separate worker thread.
> 
> In more detail, I'm doing some image registration work (using ITK), and I
> would like to use VTK to visualize the progress of the registration at every
> n'th iteration. So far, no problem. But I would also like to be able to
> interact with the visualization window at the same time, for example to
> rotate the viewpoint as it progresses. To make this work, what I've done is
> to have my registration code running in its own thread and then launch an
> interactor on the main one. At each iteration, the registration code calls
> SetPosition on the appropriate vtkActor. So far, all of this works fine, and
> if I interact with the renderWindow (for example click and drag the mouse),
> the renderWindow redraws, picking up the changes that my registration code
> has made to the actor's position.
> 
> What I can't figure out how to do is to have the registration code safely
> tell the renderWindow to re-render itself without me interacting with the
> window. I can't for example call window->Render() directly from the
> registration thread without causing errors. My best guess as to why comes
> from this note:
> http://public.kitware.com/pipermail/vtkusers/2003-December/070728.html
> which explains that trying to use the same OpenGL context across two threads
> is a big no-no, and that seems to be exactly what I'm doing when I go that
> route. So I need to make sure that the Render call is always made from the
> main thread, but I don't know how to make that happen, and any tips from the
> community will be greatly appreciated.
> 
> Thanks so much,
> Jaety
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> This is the private VTK discussion list.
> Please keep messages on-topic. Check the FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers



More information about the vtkusers mailing list