glc_trackballmover.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 
00003  This file is part of the GLC-lib library.
00004  Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
00005  http://glc-lib.sourceforge.net
00006 
00007  GLC-lib is free software; you can redistribute it and/or modify
00008  it under the terms of the GNU Lesser General Public License as published by
00009  the Free Software Foundation; either version 3 of the License, or
00010  (at your option) any later version.
00011 
00012  GLC-lib is distributed in the hope that it will be useful,
00013  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  GNU Lesser General Public License for more details.
00016 
00017  You should have received a copy of the GNU Lesser General Public License
00018  along with GLC-lib; if not, write to the Free Software
00019  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021  *****************************************************************************/
00023 
00024 #include "glc_trackballmover.h"
00025 #include "glc_viewport.h"
00026 #include "glc_reptrackballmover.h"
00027 
00028 // Default constructor
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 // Copy constructor
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 // Get Functions
00053 
00054 // Return a clone of the mover
00055 GLC_Mover* GLC_TrackBallMover::clone() const
00056 {
00057         return new GLC_TrackBallMover(*this);
00058 }
00059 
00061 // Set Functions
00063 
00064 // Initialized the mover
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         // Update trackball representations
00077         initRepresentation();
00078 }
00079 
00080 // Move the camera
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         // Update camera position (orbit)
00086         GLC_Mover::m_pViewport->cameraHandle()->orbit(GLC_Mover::m_PreviousVector, VectCurOrbit);
00087 
00088         // Update arcs of circle's positionning matrix
00089         const GLC_Matrix4x4 MatRot(GLC_Mover::m_PreviousVector, VectCurOrbit);
00090 
00091         GLC_Mover::m_MoverInfo.m_MatrixInfo.first()= MatRot;
00092         updateRepresentation();
00093 
00094         // Previous vector become current vector
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 // Private services Functions
00116 
00117 // Convert mouse View coordinate to tracking coordinate (Centred and betwen (-1,-1) and (1,1))
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         // Change origine and cover
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         // Distance between pick point and origine can't be over then 1 (1 is radius of orbit circle)
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 }

SourceForge.net Logo

©2005-2011 Laurent Ribon