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  Version 2.0.0, packaged on July 2010.
00006 
00007  http://glc-lib.sourceforge.net
00008 
00009  GLC-lib is free software; you can redistribute it and/or modify
00010  it under the terms of the GNU Lesser General Public License as published by
00011  the Free Software Foundation; either version 3 of the License, or
00012  (at your option) any later version.
00013 
00014  GLC-lib is distributed in the hope that it will be useful,
00015  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  GNU Lesser General Public License for more details.
00018 
00019  You should have received a copy of the GNU Lesser General Public License
00020  along with GLC-lib; if not, write to the Free Software
00021  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00022 
00023  *****************************************************************************/
00025 
00026 #include "glc_trackballmover.h"
00027 #include "glc_viewport.h"
00028 
00029 // Default constructor
00030 GLC_TrackBallMover::GLC_TrackBallMover(GLC_Viewport* pViewport, const QList<GLC_RepMover*>& repsList)
00031 : GLC_Mover(pViewport, repsList)
00032 , m_Ratio(0.95)
00033 {
00034         GLC_Mover::m_MoverInfo.m_MatrixInfo.append(GLC_Matrix4x4());
00035         GLC_Mover::m_MoverInfo.m_VectorInfo.append(GLC_Vector3d());
00036 }
00037 
00038 // Copy constructor
00039 GLC_TrackBallMover::GLC_TrackBallMover(const GLC_TrackBallMover& mover)
00040 : GLC_Mover(mover)
00041 , m_Ratio(mover.m_Ratio)
00042 {
00043 
00044 }
00045 
00046 GLC_TrackBallMover::~GLC_TrackBallMover()
00047 {
00048 
00049 }
00050 
00052 // Get Functions
00054 
00055 // Return a clone of the mover
00056 GLC_Mover* GLC_TrackBallMover::clone() const
00057 {
00058         return new GLC_TrackBallMover(*this);
00059 }
00060 
00062 // Set Functions
00064 
00065 // Initialized the mover
00066 void GLC_TrackBallMover::init(QMouseEvent * e)
00067 {
00068         GLC_Mover::m_PreviousVector.setVect(mapForTracking(static_cast<double>(e->x()), static_cast<double>(e->y())));
00069 
00070         const double Angle= acos(glc::Z_AXIS * GLC_Mover::m_PreviousVector);
00071         const GLC_Vector3d AxeRot(glc::Z_AXIS ^ GLC_Mover::m_PreviousVector);
00072 
00073         GLC_Matrix4x4 Matrice(AxeRot, Angle);
00074 
00075         GLC_Mover::m_MoverInfo.m_MatrixInfo.first()= Matrice;
00076         GLC_Mover::m_MoverInfo.m_VectorInfo.first()= GLC_Mover::m_PreviousVector;
00077         // Update trackball representations
00078         initRepresentation();
00079 }
00080 
00081 // Move the camera
00082 bool GLC_TrackBallMover::move(QMouseEvent * e)
00083 {
00084         const GLC_Vector3d VectCurOrbit(mapForTracking(static_cast<double>(e->x()), static_cast<double>(e->y())));
00085 
00086         // Update camera position (orbit)
00087         GLC_Mover::m_pViewport->cameraHandle()->orbit(GLC_Mover::m_PreviousVector, VectCurOrbit);
00088 
00089         // Update arcs of circle's positionning matrix
00090         const GLC_Matrix4x4 MatRot(GLC_Mover::m_PreviousVector, VectCurOrbit);
00091 
00092         GLC_Mover::m_MoverInfo.m_MatrixInfo.first()= MatRot;
00093         updateRepresentation();
00094 
00095         // Previous vector become current vector
00096         GLC_Mover::m_PreviousVector = VectCurOrbit;
00097 
00098         return true;
00099 }
00100 
00102 // Private services Functions
00104 
00105 // Convert mouse View coordinate to tracking coordinate (Centred and betwen (-1,-1) and (1,1))
00106 GLC_Vector3d GLC_TrackBallMover::mapForTracking( double x, double y) const
00107 {
00108         double AspectRatio;
00109         const double winHSize= static_cast<double>(GLC_Mover::m_pViewport->viewHSize());
00110         const double winVSize= static_cast<double>(GLC_Mover::m_pViewport->viewVSize());
00111 
00112         // Change origine and cover
00113         if (winHSize < winVSize)
00114         {
00115                 AspectRatio= winVSize / winHSize;
00116                 x= ( (x - winHSize  / 2.0 ) / ( winHSize / 2.0) ) / m_Ratio;
00117                 y= AspectRatio * ( ( winVSize / 2.0 - y) / ( winVSize / 2.0 ) ) / m_Ratio;
00118         }
00119         else
00120         {
00121                 AspectRatio= winHSize / winVSize;
00122                 x= AspectRatio * ( (x - winHSize  / 2.0 ) / ( winHSize / 2.0) ) / m_Ratio;
00123                 y= ( (winVSize / 2.0 - y) / (winVSize / 2.0 ) ) / m_Ratio;
00124         }
00125 
00126         // Distance between pick point and origine can't be over then 1 (1 is radius of orbit circle)
00127         GLC_Vector3d mousePos(x, y, 0.0);
00128         if (mousePos.length() > 1.0)
00129         {
00130                 mousePos.setLength(1.0);
00131         }
00132         else
00133         {
00134                 mousePos.setZ(sqrt(1.0 - mousePos.x() *  mousePos.x() - mousePos.y() * mousePos.y()));
00135         }
00136 
00137         return mousePos;
00138 
00139 }

SourceForge.net Logo

©2005-2010 Laurent Ribon