Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "mitkTDMouseVtkCameraController.h"
00019 #include "mitkVtkPropRenderer.h"
00020 #include "vtkCamera.h"
00021 #include "vtkRenderer.h"
00022 #include "vtkTransform.h"
00023 #include "mitkVector.h"
00024 #include "mitkInteractionConst.h"
00025 #include "mitkStateEvent.h"
00026 #include "mitkTDMouseEventThrower.h"
00027 #include "mitkTDMouseEvent.h"
00028 #include "mitkGlobalInteraction.h"
00029 #include "vtkMath.h"
00030
00031 const double TRANSLATIONSENSITIVITY = 2.0;
00032 const double ROTATIONSENSIVITY = 3.5;
00033 const double ANGLESENSITIVITY = 3.5;
00034
00035
00036 mitk::TDMouseVtkCameraController::TDMouseVtkCameraController()
00037 : CameraController("TDMouseInteraction")
00038 {
00039
00040 mitk::TDMouseEventThrower* thrower = mitk::TDMouseEventThrower::GetInstance();
00041
00042 mitk::GlobalInteraction::GetInstance()->AddListener(this);
00043
00044 CONNECT_ACTION(AcONTDMOUSEINPUT, OnTDMouseEvent);
00045 CONNECT_ACTION(AcONTDMOUSEKEYDOWN, OnTDMouseKeyDown);
00046 m_ClippingRangeIsSet = false;
00047 }
00048
00049 mitk::TDMouseVtkCameraController::~TDMouseVtkCameraController()
00050 {
00051 }
00052
00053 bool mitk::TDMouseVtkCameraController::OnTDMouseEvent(mitk::Action* a, const mitk::StateEvent* e)
00054 {
00055
00056 mitk::BaseRenderer const* br = this->GetRenderer();
00057 mitk::BaseRenderer::MapperSlotId id = ((mitk::BaseRenderer*)(br))->GetMapperID();
00058 if (id != mitk::BaseRenderer::Standard3D)
00059 return true;
00060
00061
00062 if (this->GetRenderer() != mitk::GlobalInteraction::GetInstance()->GetFocus())
00063 return true;
00064
00065
00066 vtkRenderer* vtkRenderer = ((mitk::VtkPropRenderer*)this->GetRenderer())->GetVtkRenderer();
00067 if (vtkRenderer == NULL)
00068 return false;
00069
00070
00071 vtkCamera* vtkCam = (vtkCamera*)vtkRenderer->GetActiveCamera();
00072
00073 if(!m_ClippingRangeIsSet)
00074 vtkCam->SetClippingRange(0.1, 1000000);
00075
00076 const mitk::TDMouseEvent* tdevent = dynamic_cast<const mitk::TDMouseEvent*>(e->GetEvent());
00077 if (tdevent == NULL)
00078 {
00079 std::cout<<"Wrong event for TDMouseCameraController!"<<std::endl;
00080 return false;
00081 }
00082
00083
00084 mitk::Vector3D translation = tdevent->GetTranslation();
00085 mitk::Vector3D rotation = tdevent->GetRotation();
00086 mitk::ScalarType angle = tdevent->GetAngle();
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105 double sceneSensivity = 1.0;
00106
00107 mitk::DataStorage* ds = m_Renderer->GetDataStorage();
00108 mitk::BoundingBox::Pointer bb = ds->ComputeBoundingBox();
00109 mitk::BoundingBox::AccumulateType length = bb->GetDiagonalLength2();
00110 if (length > 0.00001)
00111 sceneSensivity *= 100.0 / (sqrt(length)) ;
00112
00113
00114 translation *= sceneSensivity * TRANSLATIONSENSITIVITY;
00115 rotation *= sceneSensivity * ROTATIONSENSIVITY;
00116 angle *= sceneSensivity * ANGLESENSITIVITY;
00117
00118
00119
00120 mitk::Vector3D camPosition;
00121 double camPositionTemp[3];
00122 vtkCam->GetPosition(camPositionTemp);
00123 camPosition[0] = camPositionTemp[0]; camPosition[1] = camPositionTemp[1]; camPosition[2] = camPositionTemp[2];
00124
00125
00126 mitk::Vector3D upCamVector;
00127 double upCamTemp[3];
00128 vtkCam->GetViewUp(upCamTemp);
00129 upCamVector[0] = upCamTemp[0]; upCamVector[1] = upCamTemp[1]; upCamVector[2] = upCamTemp[2];
00130 upCamVector.Normalize();
00131
00132
00133 mitk::Vector3D focalPoint;
00134 double focalPointTemp[3];
00135 vtkCam->GetFocalPoint(focalPointTemp);
00136 focalPoint[0] = focalPointTemp[0]; focalPoint[1] = focalPointTemp[1]; focalPoint[2] = focalPointTemp[2];
00137 mitk::Vector3D focalVector;
00138 focalVector = focalPoint - camPosition;
00139 focalVector.Normalize();
00140
00141
00142 mitk::Vector3D crossVector;
00143 crossVector = CrossProduct(upCamVector, focalVector);
00144 crossVector.Normalize();
00145
00146
00147
00148
00149
00150 mitk::Vector3D vectorX = crossVector * -translation[0];
00151 double nextCamPosition[3];
00152 nextCamPosition[0] = camPosition[0] + vectorX[0];
00153 nextCamPosition[1] = camPosition[1] + vectorX[1];
00154 nextCamPosition[2] = camPosition[2] + vectorX[2];
00155
00156
00157 mitk::Vector3D vectorY = upCamVector * translation[1];
00158 nextCamPosition[0] += vectorY[0];
00159 nextCamPosition[1] += vectorY[1];
00160 nextCamPosition[2] += vectorY[2];
00161
00162
00163 mitk::Vector3D vectorZ = focalVector * -translation[2];
00164 nextCamPosition[0] += vectorZ[0];
00165 nextCamPosition[1] += vectorZ[1];
00166 nextCamPosition[2] += vectorZ[2];
00167
00168
00169 double nextPosition[3];
00170 nextPosition[0] = nextCamPosition[0]; nextPosition[1] = nextCamPosition[1]; nextPosition[2] = nextCamPosition[2];
00171 vtkCam->SetPosition(nextPosition);
00172
00173
00174 double currentFocalPoint[3], nextFocalPoint[3];
00175 vtkCam->GetFocalPoint(currentFocalPoint);
00176 nextFocalPoint[0] = currentFocalPoint[0] + vectorX[0] + vectorY[0] + vectorZ[0];
00177 nextFocalPoint[1] = currentFocalPoint[1] + vectorX[1] + vectorY[1] + vectorZ[1]; ;
00178 nextFocalPoint[2] = currentFocalPoint[2] + vectorX[2] + vectorY[2] + vectorZ[2];
00179 vtkCam->SetFocalPoint(nextFocalPoint);
00180
00181
00182
00183
00184
00185
00186
00187 vtkCam->Pitch(rotation[0]*angle);
00188
00189
00190
00191
00192 vtkCam->Yaw(rotation[1]*angle);
00193
00194
00195
00196 vtkCam->Roll(-rotation[2]*angle * 1.5);
00197
00198
00199
00200 vtkCam->OrthogonalizeViewUp();
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213 vtkRenderer->ResetCameraClippingRange();
00214
00215 mitk::RenderingManager::GetInstance()->ForceImmediateUpdate(mitk::GlobalInteraction::GetInstance()->GetFocus()->GetRenderWindow());
00216
00217 return true;
00218 }
00219
00220
00221 bool mitk::TDMouseVtkCameraController::OnTDMouseKeyDown(mitk::Action* a, const mitk::StateEvent* e)
00222 {
00223
00224 const mitk::VtkPropRenderer* glRenderer = dynamic_cast<const mitk::VtkPropRenderer*>(m_Renderer);
00225 if (glRenderer)
00226 {
00227 vtkRenderer* vtkRenderer = glRenderer->GetVtkRenderer();
00228 mitk::DataStorage* ds = m_Renderer->GetDataStorage();
00229 if (ds == NULL)
00230 return false;
00231
00232 mitk::BoundingBox::Pointer bb = ds->ComputeBoundingBox();
00233
00234 mitk::Point3D middle =bb->GetCenter();
00235 vtkRenderer->GetActiveCamera()->SetFocalPoint(middle[0],middle[1],middle[2]);
00236
00237 vtkRenderer->ResetCamera();
00238 mitk::RenderingManager::GetInstance()->RequestUpdateAll();
00239 return true;
00240 }
00241 return false;
00242 }