1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkTDxInteractorStyleCamera.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkTDxInteractorStyleCamera.h"
#include "vtkTransform.h"
#include <cassert>
#include "vtkCamera.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkTDxMotionEventInfo.h"
#include "vtkObjectFactory.h"
#include "vtkTDxInteractorStyleSettings.h"
vtkStandardNewMacro(vtkTDxInteractorStyleCamera);
// ----------------------------------------------------------------------------
vtkTDxInteractorStyleCamera::vtkTDxInteractorStyleCamera()
{
this->Transform=vtkTransform::New();
// this->DebugOn();
}
// ----------------------------------------------------------------------------
vtkTDxInteractorStyleCamera::~vtkTDxInteractorStyleCamera()
{
this->Transform->Delete();
}
// ----------------------------------------------------------------------------
void vtkTDxInteractorStyleCamera::OnMotionEvent(
vtkTDxMotionEventInfo *motionInfo)
{
assert("pre: motionInfo_exist" && motionInfo!=0);
vtkDebugMacro(<<"vtkTDxInteractorStyleCamera::OnMotionEvent()");
if(this->Renderer==0 || this->Settings==0)
{
vtkDebugMacro(<<"vtkTDxInteractorStyleCamera::OnMotionEvent() no renderer or no settings");
return;
}
vtkCamera *c=this->Renderer->GetActiveCamera();
vtkRenderWindow *w=this->Renderer->GetRenderWindow();
vtkRenderWindowInteractor *i=w->GetInteractor();
vtkDebugMacro(<< "x=" << motionInfo->X << " y=" << motionInfo->Y << " z="
<< motionInfo->Z
<< " angle=" << motionInfo->Angle << " rx="
<< motionInfo->AxisX << " ry="
<< motionInfo->AxisY << " rz="<< motionInfo->AxisZ);
vtkTransform *eyeToWorld=c->GetViewTransformObject();
double axisEye[3];
double axisWorld[3];
// As we want to rotate the camera, the incoming data are expressed in
// eye coordinates.
if(this->Settings->GetUseRotationX())
{
axisEye[0]=motionInfo->AxisX;
}
else
{
axisEye[0]=0.0;
}
if(this->Settings->GetUseRotationY())
{
axisEye[1]=motionInfo->AxisY;
}
else
{
axisEye[1]=0.0;
}
if(this->Settings->GetUseRotationZ())
{
axisEye[2]=motionInfo->AxisZ;
}
else
{
axisEye[2]=0.0;
}
// Get the rotation axis in world coordinates.
this->Transform->Identity();
this->Transform->Concatenate(eyeToWorld);
this->Transform->Inverse();
this->Transform->TransformVector(axisEye,axisWorld);
// Get the translation vector in world coordinates. Used at the end.
double translationEye[3];
double translationWorld[3];
translationEye[0]=motionInfo->X*this->Settings->GetTranslationXSensitivity();
translationEye[1]=motionInfo->Y*this->Settings->GetTranslationYSensitivity();
translationEye[2]=motionInfo->Z*this->Settings->GetTranslationZSensitivity();
this->Transform->TransformVector(translationEye,translationWorld);
this->Transform->Identity();
// default multiplication is "pre" which means applied to the "right" of
// the current matrix, which follows the OpenGL multiplication convention.
// 2. translate (affect position and focalPoint)
this->Transform->Translate(translationWorld);
// 1. build the displacement (aka affine rotation) with the axis
// passing through the focal point.
double *p=c->GetFocalPoint();
this->Transform->Translate(p[0],p[1],p[2]);
this->Transform->RotateWXYZ(motionInfo->Angle*
this->Settings->GetAngleSensitivity(),
axisWorld[0],axisWorld[1],axisWorld[2]);
this->Transform->Translate(-p[0],-p[1],-p[2]);
// Apply the transform to the camera position
double *pos=c->GetPosition();
double newPosition[3];
this->Transform->TransformPoint(pos,newPosition);
// Apply the vector part of the transform to the camera view up vector
double *up=c->GetViewUp();
double newUp[3];
this->Transform->TransformVector(up,newUp);
// Apply the transform to the camera position
double newFocalPoint[3];
this->Transform->TransformPoint(p,newFocalPoint);
// Set the new view up vector and position of the camera.
c->SetViewUp(newUp);
c->SetPosition(newPosition);
c->SetFocalPoint(newFocalPoint);
this->Renderer->ResetCameraClippingRange();
// Display the result.
i->Render();
}
//----------------------------------------------------------------------------
void vtkTDxInteractorStyleCamera::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
}
|