KiCad PCB EDA Suite
ctrack_ball.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2015-2016 Mario Luzeiro <mrluzeiro@ua.pt>
5  * Copyright (C) 1992-2016 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program 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 General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
31 #include "ctrack_ball.h"
32 #include "trackball.h"
33 #include "../3d_math.h"
34 #include <wx/log.h>
35 
36 
37 CTRACK_BALL::CTRACK_BALL( float aRangeScale ) : CCAMERA( aRangeScale )
38 {
39  wxLogTrace( m_logTrace, wxT( "CTRACK_BALL::CTRACK_BALL" ) );
40 
41  memset( m_quat, 0, sizeof( m_quat ) );
42  memset( m_quat_t0, 0, sizeof( m_quat_t0 ) );
43  memset( m_quat_t1, 0, sizeof( m_quat_t1 ) );
44 
45  trackball( m_quat, 0.0, 0.0, 0.0, 0.0 );
46  trackball( m_quat_t0, 0.0, 0.0, 0.0, 0.0 );
47  trackball( m_quat_t1, 0.0, 0.0, 0.0, 0.0 );
48 }
49 
50 
51 void CTRACK_BALL::Drag( const wxPoint &aNewMousePosition )
52 {
53  m_parametersChanged = true;
54 
55  double spin_quat[4];
56 
57  // "Pass the x and y coordinates of the last and current positions of
58  // the mouse, scaled so they are from (-1.0 ... 1.0)."
59  const float zoom = 1.0f;
60 
61  trackball( spin_quat,
62  zoom * (2.0 * m_lastPosition.x - m_windowSize.x) / m_windowSize.x,
63  zoom * (m_windowSize.y - 2.0 * m_lastPosition.y) / m_windowSize.y,
64  zoom * (2.0 * aNewMousePosition.x - m_windowSize.x) / m_windowSize.x,
65  zoom * ( m_windowSize.y - 2.0 * aNewMousePosition.y ) / m_windowSize.y);
66 
67  add_quats( spin_quat, m_quat, m_quat );
68 
69  float rotationMatrix[4][4];
70 
71  build_rotmatrix( rotationMatrix, m_quat );
72 
73  m_rotationMatrix = glm::make_mat4( &rotationMatrix[0][0] );
74 
76 
77  updateFrustum();
78 }
79 
80 
81 void CTRACK_BALL::SetLookAtPos( const SFVEC3F &aLookAtPos )
82 {
83  if( m_lookat_pos != aLookAtPos )
84  {
85  m_lookat_pos = aLookAtPos;
86 
88  updateFrustum();
89 
90  m_parametersChanged = true;
91  }
92 }
93 
94 
95 void CTRACK_BALL::Pan( const wxPoint &aNewMousePosition )
96 {
97  m_parametersChanged = true;
98 
100  {
101  // With the ortographic projection, there is just a zoom factor
102  const float panFactor = m_zoom / 37.5f; // Magic number from CCAMERA::rebuildProjection
103  m_camera_pos.x -= panFactor * ( m_lastPosition.x - aNewMousePosition.x );
104  m_camera_pos.y -= panFactor * ( aNewMousePosition.y - m_lastPosition.y );
105  }
106  else // PROJECTION_PERSPECTIVE
107  {
108  // Unproject the coordinates using the precomputed frustum tangent (zoom level dependent)
109  const float panFactor = -m_camera_pos.z * m_frustum.tang * 2;
110  m_camera_pos.x -= panFactor * m_frustum.ratio * ( m_lastPosition.x - aNewMousePosition.x ) / m_windowSize.x;
111  m_camera_pos.y -= panFactor * ( aNewMousePosition.y - m_lastPosition.y ) / m_windowSize.y;
112  }
113 
115  updateFrustum();
116 }
117 
118 
119 void CTRACK_BALL::Pan( const SFVEC3F &aDeltaOffsetInc )
120 {
121  m_parametersChanged = true;
122 
123  m_camera_pos += aDeltaOffsetInc;
124 
126  updateFrustum();
127 }
128 
129 
130 void CTRACK_BALL::Pan_T1( const SFVEC3F &aDeltaOffsetInc )
131 {
132  m_camera_pos_t1 = m_camera_pos + aDeltaOffsetInc;
133 }
134 
135 
137 {
138  CCAMERA::Reset();
139 
140  memset( m_quat, 0, sizeof( m_quat ) );
141  trackball( m_quat, 0.0, 0.0, 0.0, 0.0 );
142 }
143 
144 
146 {
148 
149  memset( m_quat_t1, 0, sizeof( m_quat_t1 ) );
150  trackball( m_quat_t1, 0.0, 0.0, 0.0, 0.0 );
151 }
152 
153 
155 {
157 
158  memcpy( m_quat_t0, m_quat, sizeof( m_quat ) );
159  memcpy( m_quat_t1, m_quat, sizeof( m_quat ) );
160 }
161 
162 
164 {
165  wxASSERT( t >= 0.0f );
166 
167  // Limit t o 1.0
168  t = (t > 1.0f)?1.0f:t;
169 
170  switch( m_interpolation_mode )
171  {
173  t = BezierBlend( t );
174  break;
175 
177  t = QuadricEasingInOut( t );
178  break;
179 
181  default:
182  break;
183  }
184 
185  const float t0 = 1.0f - t;
186 
187  m_quat[0] = m_quat_t0[0] * t0 + m_quat_t1[0] * t;
188  m_quat[1] = m_quat_t0[1] * t0 + m_quat_t1[1] * t;
189  m_quat[2] = m_quat_t0[2] * t0 + m_quat_t1[2] * t;
190  m_quat[3] = m_quat_t0[3] * t0 + m_quat_t1[3] * t;
191 
192  float rotationMatrix[4][4];
193 
194  build_rotmatrix( rotationMatrix, m_quat );
195 
196  m_rotationMatrix = glm::make_mat4( &rotationMatrix[0][0] );
197 
199 }
Declaration for a track ball camera.
bool m_parametersChanged
Set to true if any of the parameters in the camera was changed.
Definition: ccamera.h:322
void build_rotmatrix(float m[4][4], double q[4])
Definition: trackball.cpp:306
virtual void SetT0_and_T1_current_T()
SetT0_and_T1_current_T - This will set T0 and T1 with the current values.
Definition: ccamera.cpp:535
virtual void Reset()
Reset the camera to initial state.
Definition: ccamera.cpp:60
void Pan_T1(const SFVEC3F &aDeltaOffsetInc) override
static const wxChar * m_logTrace
Trace mask used to enable or disable the trace output of this class.
Definition: ccamera.h:330
void SetLookAtPos(const SFVEC3F &aLookAtPos) override
Definition: ctrack_ball.cpp:81
float BezierBlend(float t)
Definition: 3d_math.h:188
void Reset() override
Reset the camera to initial state.
Class CCAMERA is a virtual class used to derive CCAMERA objects from.
Definition: ccamera.h:80
double m_quat[4]
quarternion of the trackball
Definition: ctrack_ball.h:66
void updateFrustum()
Definition: ccamera.cpp:233
float QuadricEasingInOut(float t)
Definition: 3d_math.h:172
float tang
Definition: ccamera.h:60
void updateViewMatrix()
Definition: ccamera.cpp:101
wxPoint m_lastPosition
The last mouse position in the screen.
Definition: ccamera.h:269
void trackball(double q[4], double p1x, double p1y, double p2x, double p2y)
Definition: trackball.cpp:155
SFVEC3F m_camera_pos_t1
Definition: ccamera.h:291
virtual void Interpolate(float t)
Interpolate - It will update the matrix to interpolate between T0 and T1 values.
Definition: ccamera.cpp:549
glm::mat4 m_rotationMatrix
Definition: ccamera.h:271
CTRACK_BALL(float aRangeScale)
Definition: ctrack_ball.cpp:37
FRUSTUM m_frustum
Definition: ccamera.h:279
void SetT0_and_T1_current_T() override
SetT0_and_T1_current_T - This will set T0 and T1 with the current values.
void Drag(const wxPoint &aNewMousePosition) override
Calculate a new mouse drag position.
Definition: ctrack_ball.cpp:51
virtual void Reset_T1()
Definition: ccamera.cpp:92
float m_zoom
3D zoom value (Min 0.0 ...
Definition: ccamera.h:257
SFVEC3F m_camera_pos
Definition: ccamera.h:289
double m_quat_t0[4]
Definition: ctrack_ball.h:67
void add_quats(double q1[4], double q2[4], double dest[4])
Definition: trackball.cpp:249
void Reset_T1() override
double m_quat_t1[4]
Definition: ctrack_ball.h:68
glm::vec3 SFVEC3F
Definition: xv3d_types.h:47
SFVEC2I m_windowSize
The window size that this camera is working.
Definition: ccamera.h:264
CAMERA_INTERPOLATION m_interpolation_mode
Definition: ccamera.h:302
void Pan(const wxPoint &aNewMousePosition) override
Definition: ctrack_ball.cpp:95
float ratio
Definition: ccamera.h:60
void Interpolate(float t) override
Interpolate - It will update the matrix to interpolate between T0 and T1 values.
SFVEC3F m_lookat_pos
Definition: ccamera.h:293
PROJECTION_TYPE m_projectionType
Definition: ccamera.h:277