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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkTDxInteractorStyleGeo.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 "vtkTDxInteractorStyleGeo.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(vtkTDxInteractorStyleGeo);
// ----------------------------------------------------------------------------
vtkTDxInteractorStyleGeo::vtkTDxInteractorStyleGeo()
{
this->Transform=vtkTransform::New();
// this->DebugOn();
}
// ----------------------------------------------------------------------------
vtkTDxInteractorStyleGeo::~vtkTDxInteractorStyleGeo()
{
this->Transform->Delete();
}
// ----------------------------------------------------------------------------
void vtkTDxInteractorStyleGeo::OnMotionEvent(
vtkTDxMotionEventInfo *motionInfo)
{
assert("pre: motionInfo_exist" && motionInfo!=0);
const double TyCalibration=0.1; // the value works well.
const double RxCalibration=0.1; // this value works well.
vtkDebugMacro(<<"vtkTDxInteractorStyleGeo::OnMotionEvent()");
if(this->Renderer==0 || this->Settings==0)
{
vtkDebugMacro(<<"vtkTDxInteractorStyleGeo::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();
// Get the rotation axis in world coordinates.
this->Transform->Identity();
this->Transform->Concatenate(eyeToWorld);
this->Transform->Inverse();
double xAxisEye[3];
double xAxisWorld[3];
xAxisEye[0]=1.0;
xAxisEye[1]=0.0;
xAxisEye[2]=0.0;
this->Transform->TransformVector(xAxisEye,xAxisWorld);
double yAxisEye[3];
double yAxisWorld[3];
yAxisEye[0]=0.0;
yAxisEye[1]=1.0;
yAxisEye[2]=0.0;
this->Transform->TransformVector(yAxisEye,yAxisWorld);
double zAxisEye[3];
double zAxisWorld[3];
zAxisEye[0]=0.0;
zAxisEye[1]=0.0;
zAxisEye[2]=1.0;
this->Transform->TransformVector(zAxisEye,zAxisWorld);
// Get the translation vector in world coordinates. Used at the end.
double translationEye[3];
double translationWorld[3];
translationEye[0]=0.0;
translationEye[1]=0.0;
translationEye[2]=TyCalibration*motionInfo->Y*
this->Settings->GetTranslationYSensitivity();
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.
double *p=c->GetFocalPoint();
// 1. build the displacement (aka affine rotation) with the axes
// passing through the focal point.
this->Transform->Translate(p[0],p[1],p[2]);
// Device X translation maps to camera Y rotation (west to east)
this->Transform->RotateWXYZ(
motionInfo->X*this->Settings->GetAngleSensitivity(),
yAxisWorld[0],yAxisWorld[1],yAxisWorld[2]);
// Device Z translation maps to camera X rotation (south to north)
this->Transform->RotateWXYZ(
motionInfo->Z*this->Settings->GetAngleSensitivity(),
xAxisWorld[0],xAxisWorld[1],xAxisWorld[2]);
// Device Y rotation maps to camera Z rotation (tilt)
this->Transform->RotateWXYZ(motionInfo->Angle*motionInfo->AxisY*
this->Settings->GetAngleSensitivity(),
zAxisWorld[0],zAxisWorld[1],zAxisWorld[2]);
this->Transform->Translate(-p[0],-p[1],-p[2]);
//2. build the displacement (aka affine rotation) with the axes
// passing through the camera position.
double *pos=c->GetPosition();
this->Transform->Translate(pos[0],pos[1],pos[2]);
// Device X rotation maps to camera X rotation
this->Transform->RotateWXYZ(
RxCalibration*motionInfo->Angle*motionInfo->AxisX*
this->Settings->GetAngleSensitivity(),
xAxisWorld[0],xAxisWorld[1],xAxisWorld[2]);
this->Transform->Translate(-pos[0],-pos[1],-pos[2]);
// Apply the transform to the camera position
double newPosition[3];
this->Transform->TransformPoint(pos,newPosition);
// 3. In addition position is translated (not the focal point)
newPosition[0]=newPosition[0]+translationWorld[0];
newPosition[1]=newPosition[1]+translationWorld[1];
newPosition[2]=newPosition[2]+translationWorld[2];
// 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 focal point
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 vtkTDxInteractorStyleGeo::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
}
|