GLC_lib  2.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
glc_trackballmover.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 
3  This file is part of the GLC-lib library.
4  Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
5  http://glc-lib.sourceforge.net
6 
7  GLC-lib is free software; you can redistribute it and/or modify
8  it under the terms of the GNU Lesser General Public License as published by
9  the Free Software Foundation; either version 3 of the License, or
10  (at your option) any later version.
11 
12  GLC-lib is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with GLC-lib; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21  *****************************************************************************/
23 
24 #include "glc_trackballmover.h"
25 #include "glc_viewport.h"
26 #include "glc_reptrackballmover.h"
27 
28 // Default constructor
29 GLC_TrackBallMover::GLC_TrackBallMover(GLC_Viewport* pViewport, const QList<GLC_RepMover*>& repsList)
30 : GLC_Mover(pViewport, repsList)
31 , m_Ratio(0.95)
32 {
35 }
36 
37 // Copy constructor
39 : GLC_Mover(mover)
40 , m_Ratio(mover.m_Ratio)
41 {
42 
43 }
44 
46 {
47 
48 }
49 
51 // Get Functions
53 
54 // Return a clone of the mover
56 {
57  return new GLC_TrackBallMover(*this);
58 }
59 
61 // Set Functions
63 
64 // Initialized the mover
66 {
67  GLC_Mover::m_PreviousVector.setVect(mapForTracking(static_cast<double>(userInput.x()), static_cast<double>(userInput.y())));
68 
69  const double Angle= acos(glc::Z_AXIS * GLC_Mover::m_PreviousVector);
70  const GLC_Vector3d AxeRot(glc::Z_AXIS ^ GLC_Mover::m_PreviousVector);
71 
72  GLC_Matrix4x4 Matrice(AxeRot, Angle);
73 
74  GLC_Mover::m_MoverInfo.m_MatrixInfo.first()= Matrice;
76  // Update trackball representations
78 }
79 
80 // Move the camera
82 {
83  const GLC_Vector3d VectCurOrbit(mapForTracking(static_cast<double>(userInput.x()), static_cast<double>(userInput.y())));
84 
85  // Update camera position (orbit)
87 
88  // Update arcs of circle's positionning matrix
89  const GLC_Matrix4x4 MatRot(GLC_Mover::m_PreviousVector, VectCurOrbit);
90 
91  GLC_Mover::m_MoverInfo.m_MatrixInfo.first()= MatRot;
93 
94  // Previous vector become current vector
95  GLC_Mover::m_PreviousVector = VectCurOrbit;
96 
97  return true;
98 }
99 
101 {
102  m_Ratio= ratio;
103  const int repCount= m_RepMoverList.count();
104  for (int i= 0; i < repCount; ++i)
105  {
106  GLC_RepTrackBallMover* pRep= dynamic_cast<GLC_RepTrackBallMover*>(m_RepMoverList.at(i));
107  if (NULL != pRep)
108  {
109  pRep->setRatio(ratio);
110  }
111  }
112 }
114 // Private services Functions
116 
117 // Convert mouse View coordinate to tracking coordinate (Centred and betwen (-1,-1) and (1,1))
119 {
120  double AspectRatio;
121  const double winHSize= static_cast<double>(GLC_Mover::m_pViewport->viewHSize());
122  const double winVSize= static_cast<double>(GLC_Mover::m_pViewport->viewVSize());
123 
124  // Change origine and cover
125  if (winHSize < winVSize)
126  {
127  AspectRatio= winVSize / winHSize;
128  x= ( (x - winHSize / 2.0 ) / ( winHSize / 2.0) ) / m_Ratio;
129  y= AspectRatio * ( ( winVSize / 2.0 - y) / ( winVSize / 2.0 ) ) / m_Ratio;
130  }
131  else
132  {
133  AspectRatio= winHSize / winVSize;
134  x= AspectRatio * ( (x - winHSize / 2.0 ) / ( winHSize / 2.0) ) / m_Ratio;
135  y= ( (winVSize / 2.0 - y) / (winVSize / 2.0 ) ) / m_Ratio;
136  }
137 
138  // Distance between pick point and origine can't be over then 1 (1 is radius of orbit circle)
139  GLC_Vector3d mousePos(x, y, 0.0);
140  if (mousePos.length() > 1.0)
141  {
142  mousePos.setLength(1.0);
143  }
144  else
145  {
146  mousePos.setZ(sqrt(1.0 - mousePos.x() * mousePos.x() - mousePos.y() * mousePos.y()));
147  }
148 
149  return mousePos;
150 
151 }

©2005-2013 Laurent Ribon