KiCad PCB EDA Suite
track_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 <[email protected]>
5 * Copyright (C) 2015-2020 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 "track_ball.h"
32#include "trackball.h"
33#include "../3d_math.h"
34#include <wx/log.h>
35
36#include <glm/gtc/quaternion.hpp>
37
38// stdlib
39#include <algorithm>
40
41TRACK_BALL::TRACK_BALL( float aInitialDistance ) :
42 CAMERA( aInitialDistance )
43{
44 wxLogTrace( m_logTrace, wxT( "TRACK_BALL::TRACK_BALL" ) );
45
46 memset( m_quat_t0, 0, sizeof( m_quat_t0 ) );
47 memset( m_quat_t1, 0, sizeof( m_quat_t1 ) );
48
49 trackball( m_quat_t0, 0.0, 0.0, 0.0, 0.0 );
50 trackball( m_quat_t1, 0.0, 0.0, 0.0, 0.0 );
51}
52
53void TRACK_BALL::Drag( const wxPoint& aNewMousePosition )
54{
56
57 double spin_quat[4];
58
59 // "Pass the x and y coordinates of the last and current positions of
60 // the mouse, scaled so they are from (-1.0 ... 1.0)."
61 const float zoom = 1.0f;
62
63 trackball( spin_quat, zoom * ( 2.0 * m_lastPosition.x - m_windowSize.x ) / m_windowSize.x,
64 zoom * ( m_windowSize.y - 2.0 * m_lastPosition.y ) / m_windowSize.y,
65 zoom * ( 2.0 * aNewMousePosition.x - m_windowSize.x ) / m_windowSize.x,
66 zoom * ( m_windowSize.y - 2.0 * aNewMousePosition.y ) / m_windowSize.y );
67
68 float spin_matrix[4][4];
69 build_rotmatrix( spin_matrix, spin_quat );
70 m_rotationMatrix = glm::make_mat4( &spin_matrix[0][0] ) * m_rotationMatrix;
71
73
75}
76
77void TRACK_BALL::Pan( const wxPoint& aNewMousePosition )
78{
80
82 {
84 ( m_lastPosition.x - aNewMousePosition.x ) / m_windowSize.x;
86 ( aNewMousePosition.y - m_lastPosition.y ) / m_windowSize.y;
87 }
88 else // PROJECTION_TYPE::PERSPECTIVE
89 {
90 // Unproject the coordinates using the precomputed frustum tangent (zoom level dependent)
91 const float panFactor = -m_camera_pos.z * m_frustum.tang * 2;
92 m_camera_pos.x -= panFactor * m_frustum.ratio *
93 ( m_lastPosition.x - aNewMousePosition.x ) / m_windowSize.x;
94 m_camera_pos.y -= panFactor * ( aNewMousePosition.y - m_lastPosition.y ) / m_windowSize.y;
95 }
96
99}
100
101void TRACK_BALL::Pan( const SFVEC3F& aDeltaOffsetInc )
102{
103 m_parametersChanged = true;
104
105 m_camera_pos += aDeltaOffsetInc;
106
109}
110
111void TRACK_BALL::Pan_T1( const SFVEC3F& aDeltaOffsetInc )
112{
113 m_camera_pos_t1 = m_camera_pos + aDeltaOffsetInc;
114}
115
117{
119
120 memset( m_quat_t1, 0, sizeof( m_quat_t1 ) );
121 trackball( m_quat_t1, 0.0, 0.0, 0.0, 0.0 );
122}
123
125{
127
128 double quat[4];
129
130 // Charge the quaternions with the current rotation matrix to allow dual input.
131 std::copy_n( glm::value_ptr( glm::conjugate( glm::quat_cast( m_rotationMatrix ) ) ),
132 sizeof( quat ) / sizeof( quat[0] ), quat );
133
134 memcpy( m_quat_t0, quat, sizeof( quat ) );
135 memcpy( m_quat_t1, quat, sizeof( quat ) );
136}
137
139{
140 wxASSERT( t >= 0.0f );
141
142 // Limit t o 1.0
143 t = ( t > 1.0f ) ? 1.0f : t;
144
145 switch( m_interpolation_mode )
146 {
148 t = BezierBlend( t );
149 break;
150
152 t = QuadricEasingInOut( t );
153 break;
154
156 default:
157 break;
158 }
159
160 const float t0 = 1.0f - t;
161 double quat[4];
162 quat[0] = m_quat_t0[0] * t0 + m_quat_t1[0] * t;
163 quat[1] = m_quat_t0[1] * t0 + m_quat_t1[1] * t;
164 quat[2] = m_quat_t0[2] * t0 + m_quat_t1[2] * t;
165 quat[3] = m_quat_t0[3] * t0 + m_quat_t1[3] * t;
166
167 float rotationMatrix[4][4];
168
169 build_rotmatrix( rotationMatrix, quat );
170
171 m_rotationMatrix = glm::make_mat4( &rotationMatrix[0][0] );
172
174}
float BezierBlend(float t)
Definition: 3d_math.h:179
float QuadricEasingInOut(float t)
Definition: 3d_math.h:163
A class used to derive camera objects from.
Definition: camera.h:78
bool m_parametersChanged
Set to true if any of the parameters in the camera was changed.
Definition: camera.h:355
CAMERA_INTERPOLATION m_interpolation_mode
Definition: camera.h:337
wxPoint m_lastPosition
The last mouse position in the screen.
Definition: camera.h:304
virtual void Reset_T1()
Definition: camera.cpp:103
virtual void Interpolate(float t)
It will update the matrix to interpolate between T0 and T1 values.
Definition: camera.cpp:645
CAMERA_FRUSTUM m_frustum
Definition: camera.h:314
void updateFrustum()
Definition: camera.cpp:290
virtual void SetT0_and_T1_current_T()
This will set T0 and T1 with the current values.
Definition: camera.cpp:631
SFVEC3F m_camera_pos
Definition: camera.h:324
PROJECTION_TYPE m_projectionType
Definition: camera.h:312
SFVEC2I m_windowSize
The window size that this camera is working.
Definition: camera.h:299
void updateViewMatrix()
Definition: camera.cpp:154
glm::mat4 m_rotationMatrix
Definition: camera.h:306
SFVEC3F m_camera_pos_t1
Definition: camera.h:326
void Pan_T1(const SFVEC3F &aDeltaOffsetInc) override
Definition: track_ball.cpp:111
void SetT0_and_T1_current_T() override
This will set T0 and T1 with the current values.
Definition: track_ball.cpp:124
void Interpolate(float t) override
It will update the matrix to interpolate between T0 and T1 values.
Definition: track_ball.cpp:138
TRACK_BALL(float aInitialDistance)
Definition: track_ball.cpp:41
void Pan(const wxPoint &aNewMousePosition) override
Definition: track_ball.cpp:77
void Reset_T1() override
Definition: track_ball.cpp:116
double m_quat_t1[4]
Definition: track_ball.h:64
double m_quat_t0[4]
interpolate quaternions of the trackball
Definition: track_ball.h:63
void Drag(const wxPoint &aNewMousePosition) override
Calculate a new mouse drag position.
Definition: track_ball.cpp:53
static const wxChar * m_logTrace
Trace mask used to enable or disable the trace output of this class.
Definition: camera.h:364
float ratio
Definition: camera.h:59
float nh
Definition: camera.h:60
float nw
Definition: camera.h:60
float tang
Definition: camera.h:59
Declaration for a track ball camera.
void build_rotmatrix(float m[4][4], double q[4])
Definition: trackball.cpp:306
void trackball(double q[4], double p1x, double p1y, double p2x, double p2y)
Definition: trackball.cpp:155
glm::vec3 SFVEC3F
Definition: xv3d_types.h:44