glc_trackballmover.cpp
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
00019
00020
00021
00023
00024 #include "glc_trackballmover.h"
00025 #include "glc_viewport.h"
00026 #include "glc_reptrackballmover.h"
00027
00028
00029 GLC_TrackBallMover::GLC_TrackBallMover(GLC_Viewport* pViewport, const QList<GLC_RepMover*>& repsList)
00030 : GLC_Mover(pViewport, repsList)
00031 , m_Ratio(0.95)
00032 {
00033 GLC_Mover::m_MoverInfo.m_MatrixInfo.append(GLC_Matrix4x4());
00034 GLC_Mover::m_MoverInfo.m_VectorInfo.append(GLC_Vector3d());
00035 }
00036
00037
00038 GLC_TrackBallMover::GLC_TrackBallMover(const GLC_TrackBallMover& mover)
00039 : GLC_Mover(mover)
00040 , m_Ratio(mover.m_Ratio)
00041 {
00042
00043 }
00044
00045 GLC_TrackBallMover::~GLC_TrackBallMover()
00046 {
00047
00048 }
00049
00051
00053
00054
00055 GLC_Mover* GLC_TrackBallMover::clone() const
00056 {
00057 return new GLC_TrackBallMover(*this);
00058 }
00059
00061
00063
00064
00065 void GLC_TrackBallMover::init(const GLC_UserInput& userInput)
00066 {
00067 GLC_Mover::m_PreviousVector.setVect(mapForTracking(static_cast<double>(userInput.x()), static_cast<double>(userInput.y())));
00068
00069 const double Angle= acos(glc::Z_AXIS * GLC_Mover::m_PreviousVector);
00070 const GLC_Vector3d AxeRot(glc::Z_AXIS ^ GLC_Mover::m_PreviousVector);
00071
00072 GLC_Matrix4x4 Matrice(AxeRot, Angle);
00073
00074 GLC_Mover::m_MoverInfo.m_MatrixInfo.first()= Matrice;
00075 GLC_Mover::m_MoverInfo.m_VectorInfo.first()= GLC_Mover::m_PreviousVector;
00076
00077 initRepresentation();
00078 }
00079
00080
00081 bool GLC_TrackBallMover::move(const GLC_UserInput& userInput)
00082 {
00083 const GLC_Vector3d VectCurOrbit(mapForTracking(static_cast<double>(userInput.x()), static_cast<double>(userInput.y())));
00084
00085
00086 GLC_Mover::m_pViewport->cameraHandle()->orbit(GLC_Mover::m_PreviousVector, VectCurOrbit);
00087
00088
00089 const GLC_Matrix4x4 MatRot(GLC_Mover::m_PreviousVector, VectCurOrbit);
00090
00091 GLC_Mover::m_MoverInfo.m_MatrixInfo.first()= MatRot;
00092 updateRepresentation();
00093
00094
00095 GLC_Mover::m_PreviousVector = VectCurOrbit;
00096
00097 return true;
00098 }
00099
00100 void GLC_TrackBallMover::setRatio(double ratio)
00101 {
00102 m_Ratio= ratio;
00103 const int repCount= m_RepMoverList.count();
00104 for (int i= 0; i < repCount; ++i)
00105 {
00106 GLC_RepTrackBallMover* pRep= dynamic_cast<GLC_RepTrackBallMover*>(m_RepMoverList.at(i));
00107 if (NULL != pRep)
00108 {
00109 pRep->setRatio(ratio);
00110 }
00111 }
00112 }
00114
00116
00117
00118 GLC_Vector3d GLC_TrackBallMover::mapForTracking( double x, double y) const
00119 {
00120 double AspectRatio;
00121 const double winHSize= static_cast<double>(GLC_Mover::m_pViewport->viewHSize());
00122 const double winVSize= static_cast<double>(GLC_Mover::m_pViewport->viewVSize());
00123
00124
00125 if (winHSize < winVSize)
00126 {
00127 AspectRatio= winVSize / winHSize;
00128 x= ( (x - winHSize / 2.0 ) / ( winHSize / 2.0) ) / m_Ratio;
00129 y= AspectRatio * ( ( winVSize / 2.0 - y) / ( winVSize / 2.0 ) ) / m_Ratio;
00130 }
00131 else
00132 {
00133 AspectRatio= winHSize / winVSize;
00134 x= AspectRatio * ( (x - winHSize / 2.0 ) / ( winHSize / 2.0) ) / m_Ratio;
00135 y= ( (winVSize / 2.0 - y) / (winVSize / 2.0 ) ) / m_Ratio;
00136 }
00137
00138
00139 GLC_Vector3d mousePos(x, y, 0.0);
00140 if (mousePos.length() > 1.0)
00141 {
00142 mousePos.setLength(1.0);
00143 }
00144 else
00145 {
00146 mousePos.setZ(sqrt(1.0 - mousePos.x() * mousePos.x() - mousePos.y() * mousePos.y()));
00147 }
00148
00149 return mousePos;
00150
00151 }