Motion Callbacs in VTK (SOLUTION)

Sean Spicer spicer at bme.stanford.edu
Fri Nov 12 11:04:04 EST 1999


Okay, I figured out how to do what I want, so I'll share the code, but
There's still a question...see the end of the code fragment.

___________________________________________________________________________

#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkInteractorStyleTrackball.h"
#include "vtkInteractorStyleUser.h"

struct argsToPass
{
  vtkRenderWindow             *renWin;
  vtkRenderer                 *ren1;
  vtkRenderWindowInteractor   *iren;
  vtkInteractorStyleTrackball *defaultStyle;
  vtkInteractorStyleUser      *userStyle;

};

argsToPass *myArgs;

void my_function(void *arg);

void changeBehavior(void *arg);

void left_mouse_down(void *arg);
void left_mouse_up(void *arg);

int MODIFY = 1;
int LEFT_MOUSE_DOWN = -1;

void main( int argc, char *argv[] )
{

  if(argc != 2){
    cerr << "Usage: TestCode <file>" << endl;
    exit(0);
  }

  // Create the renderer, render window, and interactor
  vtkRenderer *ren1 = vtkRenderer::New();
  vtkRenderWindow *renWin = vtkRenderWindow::New();
    renWin->AddRenderer(ren1);
  vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
    iren->SetRenderWindow(renWin);


  ... Whole bunch of initialization ...

 
  renWin->SetSize(300,300);

  renWin->Render();

  SAVEIMAGE( renWin );
  
  myArgs = new argsToPass;
  myArgs -> renWin      = renWin;
  myArgs -> ren1        = ren1;
  myArgs -> iren        = iren;

  vtkInteractorStyleTrackball *defaultStyle = vtkInteractorStyleTrackball::New();
  defaultStyle = (vtkInteractorStyleTrackball *)iren->GetInteractorStyle();
  defaultStyle -> SetTrackballModeToTrackball();
  
  vtkInteractorStyleUser *userStyle = vtkInteractorStyleUser::New();
  userStyle -> SetUserInteractionMethod(my_function, (void *)myArgs);
  userStyle -> SetLeftButtonPressMethod(left_mouse_down, NULL);
  userStyle -> SetLeftButtonReleaseMethod(left_mouse_up, NULL);
 
  myArgs -> defaultStyle = defaultStyle;
  myArgs -> userStyle = userStyle;

  // Interact with the data at 3 frames per second
  iren->SetDesiredUpdateRate(3);
  iren->SetStillUpdateRate(0.01);
  iren->SetUserMethod(changeBehavior, (void *)myArgs);
  iren->Initialize();
  iren->Start();

 
  ... Clean Up ...
}

void changeBehavior(void *arg)
{
  
  argsToPass *myArgs = (argsToPass *) arg;

  if(MODIFY){
    cerr << "Changing to user Interaction Mode" << endl;

    /* QUESTION -- Why do I have to delete this??? */
    vtkInteractorStyleTrackball *defaultStyle = vtkInteractorStyleTrackball::New();
    defaultStyle = (vtkInteractorStyleTrackball *)myArgs -> iren->GetInteractorStyle();

    defaultStyle -> Delete();

    myArgs -> iren -> SetInteractorStyle(myArgs -> userStyle);
    myArgs -> userStyle -> StartUserInteraction();

    MODIFY = !MODIFY;
    LEFT_MOUSE_DOWN = -1;
  }
  
  else{
    cerr << "Changing to default Interaction Mode" << endl;

    vtkInteractorStyleTrackball *defaultStyle = vtkInteractorStyleTrackball::New();
    myArgs -> defaultStyle = defaultStyle;
    defaultStyle -> SetTrackballModeToTrackball();
    
    myArgs -> userStyle -> EndUserInteraction();
    myArgs -> iren -> SetInteractorStyle(myArgs -> defaultStyle);
    MODIFY = !MODIFY;
    LEFT_MOUSE_DOWN = -1;
  }
}

void left_mouse_down(void *)
{
  LEFT_MOUSE_DOWN = 1;
}

void left_mouse_up(void *)
{
  LEFT_MOUSE_DOWN = 0;
}

void my_function(void *arg)
{

  int lastpos[2];
  int oldpos[2];
  
  int *winsize = new int[2];

  int window, level;
  
  argsToPass *myArgs = (argsToPass *) arg;
  vtkInteractorStyleUser * userStyle = (vtkInteractorStyleUser *) myArgs -> userStyle;
  winsize = userStyle -> GetInteractor() -> GetRenderWindow() -> GetSize();

  userStyle->GetLastPos(lastpos);
  userStyle->GetOldPos(oldpos);

  if(lastpos[0] > winsize[0] || lastpos[0] < 0) return;
  if(lastpos[1] > winsize[1] || lastpos[1] < 0) return;

  if(LEFT_MOUSE_DOWN == 1)
    if((lastpos[0]-oldpos[0] != 0) || (lastpos[1]-oldpos[1] != 0)){
      
      val1  = floor(1078.0*(float)lastpos[0]/(float)winsize[0]);
      val2  = floor(3201.0*(float)lastpos[1]/(float)winsize[1]);

      OperateOnData(val1, val2)

      myArgs -> renWin -> Render();
      
    }

}


___________________________________________________________________________

My question is why do I have to delete the default Style?  Why can I not
just set the Interaction Style to be the new userStyle when I want to use
the my_funtion routine, and set it back to the default style when I want
that?  If I do this the program core dumps when I switch back to the
default style.  

Any Comments/Ideas?

sean

___________________________________________________________________________
Sean Spicer 	     	          Stanford University Medical Center  
Biomechanical Engineering         Division of Vascular Surgery, Suite H3642
Cardiovascular Biomechanics Lab   Stanford CA, 94305 
                                  Telephone...650.723.1695
  			          Fax.........650.723.8762

            http://solvedeath.stanford.edu/~spicer
 



-----------------------------------------------------------------------------
This is the private VTK discussion list.  Please keep messages on-topic.
Check the FAQ at: <http://www.automatrix.com/cgi-bin/vtkfaq>
To UNSUBSCRIBE, send message body containing "unsubscribe vtkusers" to
<majordomo at gsao.med.ge.com>.  For help, send message body containing
"info vtkusers" to the same address.     Live long and prosper.
-----------------------------------------------------------------------------




More information about the vtkusers mailing list