[vtkusers] vtkAxis

James C. Robinson j.robinson at kepler-systems.com
Fri Mar 19 07:10:36 EST 2004


David,

Below is code taken directly from my application. I assume that you know
already that the best way to achieve the effect of having an axis in the
window is to create a separate renderer in an embedded viewport for those
axis (pAxisRenderer in my code). "Renderer" is the renderer for the main
part of the view. You then create a callback that tracks what the camera for
the main renderer is doing and in that callback you set the position
parameters etc. of the axis renderer camera to mimic it. Finally, the trick
to achieve the same scaling as you zoom in and out is to control the size of
the viewport relative to the window size. (NB: My code gives the choice for
the axis viewport to scale up and down with the window size, or remain
constant no matter what the window size).

Also note that the bulk of this code is not attributable to me but to some
good soul (don't have the time to respond) who answered a related question
for me about 4 weeks ago.

Regards,

Jim

PS: No doubt more adept developers mind see my code as heavy, but it works.
I use separate actors to have separate colours (red, green, blue) for each
axis. There may be a more elegant way of achieving this.



////////////////////////////////////////////////////////////////////////////
/
// CMeshviewDoc commands
////////////////////////////////////////////////////////////////////////////
/
void CMeshviewDoc::NewAxis()
{
  // make the 3D axis
  pXAxis       = vtkArrowSource::New();
  pYAxis       = vtkArrowSource::New();
  pZAxis       = vtkArrowSource::New();
  pXAxisMapper = vtkPolyDataMapper::New();
  pYAxisMapper = vtkPolyDataMapper::New();
  pZAxisMapper = vtkPolyDataMapper::New();
  pXAxisActor  = vtkActor::New();
  pYAxisActor  = vtkActor::New();
  pZAxisActor  = vtkActor::New();

  pXLabel       = vtkVectorText::New() ;
  pYLabel       = vtkVectorText::New() ;
  pZLabel       = vtkVectorText::New() ;
  pXLabelMapper = vtkPolyDataMapper::New();
  pYLabelMapper = vtkPolyDataMapper::New();
  pZLabelMapper = vtkPolyDataMapper::New();
  pXLabelActor  = vtkFollower::New();
  pYLabelActor  = vtkFollower::New();
  pZLabelActor  = vtkFollower::New();

  pCBCommand   = vtkCallbackCommand::New();

}///////////////////////////////////////////////////////////////////////////
//
// CMeshviewDoc class
////////////////////////////////////////////////////////////////////////////
/
void CMeshviewDoc::CreateAxesActor()
{
// make the 3D axis

    int ArrowResolution = 8 ;
// X Axis
    pXAxis->SetShaftResolution(ArrowResolution) ;
    pXAxisMapper->SetInput(pXAxis->GetOutput()) ;
    pXAxisMapper->ScalarVisibilityOff() ;
    pXAxisActor->SetMapper(pXAxisMapper) ;
    pXAxisActor->GetProperty()->SetColor(0,1,0) ;
// Label
    pXLabel->SetText("x") ;
    pXLabelMapper->SetInput(pXLabel->GetOutput()) ;
    pXLabelActor->SetMapper(pXLabelMapper) ;
    pXLabelActor->GetProperty()->SetColor(0,1,0) ;
        pXLabelActor->SetScale(0.2,0.2,0.2) ;
        pXLabelActor->AddPosition(1.1,0.0,0.0) ;
        pXLabelActor->SetCamera(GetView()->Renderer->GetActiveCamera()) ;

// Y Axis
    pYAxis->SetShaftResolution(ArrowResolution) ;
    pYAxisMapper->SetInput(pYAxis->GetOutput()) ;
    pYAxisMapper->ScalarVisibilityOff() ;
    pYAxisActor->SetMapper(pYAxisMapper) ;
    pYAxisActor->GetProperty()->SetColor(0,0,1) ;
    pYAxisActor->RotateZ(90) ;
// Label
    pYLabel->SetText("y") ;
    pYLabelMapper->SetInput(pYLabel->GetOutput()) ;
    pYLabelActor->SetMapper(pYLabelMapper) ;
    pYLabelActor->GetProperty()->SetColor(0,0,1) ;
        pYLabelActor->SetScale(0.2,0.2,0.2) ;
        pYLabelActor->AddPosition(0.0,1.1,0.0) ;
        pYLabelActor->SetCamera(GetView()->Renderer->GetActiveCamera()) ;

// Z Axis
    pZAxis->SetShaftResolution(ArrowResolution) ;
        pZAxisMapper->SetInput(pZAxis->GetOutput()) ;
    pZAxisMapper->ScalarVisibilityOff() ;
    pZAxisActor->SetMapper(pZAxisMapper) ;
    pZAxisActor->GetProperty()->SetColor(1,0,0) ;
    pZAxisActor->RotateY(90) ;
// Label
    pZLabel->SetText("z") ;
    pZLabelMapper->SetInput(pZLabel->GetOutput()) ;
    pZLabelActor->SetMapper(pZLabelMapper) ;
    pZLabelActor->GetProperty()->SetColor(1,0,0) ;
        pZLabelActor->SetScale(0.2,0.2,0.2) ;
        pZLabelActor->AddPosition(0.0,0.0,-1.1) ;
        pZLabelActor->SetCamera(GetView()->Renderer->GetActiveCamera()) ;

  // callback command is set to watch the main renderer
    pCBCommand->SetCallback(updateAxes);

pCBCommand->SetClientData((void*)GetView()->Renderer->GetActiveCamera());
    GetView()->Renderer->AddObserver(vtkCommand::AnyEvent,pCBCommand);

}
////////////////////////////////////////////////////////////////////////////
/
// CMeshviewDoc class
////////////////////////////////////////////////////////////////////////////
/
void updateAxes(vtkObject* caller,
                                  unsigned long vtkNotUsed(event),
                                  void* arg,
                                  void* vtkNotUsed(whatIsThis))
{
  double    cPos[3], cFoc[3], aFoc[3];
  int       *size;

  if (pVTKView->pAxisRenderer == NULL) return ;
  // set the axis camera according to the main renderer.
  vtkCamera *cam = (vtkCamera *)arg;
  cam->GetPosition(cPos);
  cam->GetFocalPoint(cFoc);
  pVTKView->pAxisRenderer->GetActiveCamera()->GetFocalPoint(aFoc);
  pVTKView->pAxisRenderer->GetActiveCamera()->SetViewUp(cam->GetViewUp());
  pVTKView->pAxisRenderer->GetActiveCamera()->SetPosition(

cPos[0] - cFoc[0] + aFoc[0],\

cPos[1] - cFoc[1] + aFoc[1],\

cPos[2] - cFoc[2] + aFoc[2]);
  pVTKView->pAxisRenderer->ResetCamera();

  size = pVTKView->RenderWindow->GetSize();
  vtkFloatingPointType ViewPortLeft, ViewPortBottom, ViewPortRight,
ViewPortTop ;

  if (pVTKView->AxesViewportFixed)
  { // keep the axis window size a constant "AxesViewportSize" pixels
squared (ugly).
    ViewPortLeft   =
((vtkFloatingPointType)size[0]-(vtkFloatingPointType)pVTKView->AxesViewportS
ize)/(vtkFloatingPointType)size[0] ;
    ViewPortRight  = 1.0 ;
    ViewPortBottom = 0.0 ;
    ViewPortTop    =
(vtkFloatingPointType)pVTKView->AxesViewportSize/(vtkFloatingPointType)size[
1] ;
  } else
  { // The Viewport changes to keep the same scale related to the window
    ViewPortLeft   = 1.0 - pVTKView->AxesViewportScaledSize ;
    ViewPortRight  = 1.0 ;
    ViewPortBottom = 0.0 ;
    ViewPortTop    = pVTKView->AxesViewportScaledSize ;
  }

pVTKView->pAxisRenderer->SetViewport(ViewPortLeft,ViewPortBottom,ViewPortRig
ht,ViewPortTop);
}

______________________

James C. Robinson, PhD,
Chartered Engineer,
Kepler Simulation Systems Ltd.,
42 Rivergrove,
Glanmire, Co. Cork,
Eire

Tel:         +353-21-4822028
Tel:         +353-87-2393010
Fax:        +353-21-4822028
E-mail:     j.robinson at kepler-systems.com
______________________





More information about the vtkusers mailing list