KiCad PCB EDA Suite
camera.h
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) 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 
30 #ifndef CAMERA_H
31 #define CAMERA_H
32 
33 #include "../3d_rendering/3d_render_raytracing/ray.h"
34 #include <wx/gdicmn.h> // for wxSize
35 #include <vector>
36 
37 enum class PROJECTION_TYPE
38 {
39  ORTHO,
41 };
42 
48 {
59  float nearD, farD, ratio, angle, tang;
60  float nw, nh, fw, fh;
61 };
62 
63 
65 {
66  LINEAR,
67  EASING_IN_OUT, // Quadratic
68  BEZIER,
69 };
70 
71 
77 class CAMERA
78 {
79 public:
87  explicit CAMERA( float aRangeScale );
88 
89  virtual ~CAMERA()
90  {
91  }
92 
98  const glm::mat4 GetRotationMatrix() const;
99 
100  const glm::mat4& GetViewMatrix() const;
101  const glm::mat4& GetViewMatrix_Inv() const;
102 
103  const glm::mat4& GetProjectionMatrix() const;
104  const glm::mat4& GetProjectionMatrixInv() const;
105 
106  const SFVEC3F& GetRight() const { return m_right; }
107  const SFVEC3F& GetUp() const { return m_up; }
108  const SFVEC3F& GetDir() const { return m_dir; }
109  const SFVEC3F& GetPos() const { return m_pos; }
110  const SFVEC2F& GetFocalLen() const { return m_focalLen; }
111  float GetNear() const { return m_frustum.nearD; }
112  float GetFar() const { return m_frustum.farD; }
113 
114  void SetBoardLookAtPos( const SFVEC3F& aBoardPos )
115  {
116  if( m_board_lookat_pos_init != aBoardPos )
117  {
118  m_board_lookat_pos_init = aBoardPos;
119  SetLookAtPos( aBoardPos );
120  }
121  }
122 
123  virtual void SetLookAtPos( const SFVEC3F& aLookAtPos ) = 0;
124 
125  void SetLookAtPos_T1( const SFVEC3F& aLookAtPos )
126  {
127  m_lookat_pos_t1 = aLookAtPos;
128  }
129 
130  const SFVEC3F& GetLookAtPos_T1() const { return m_lookat_pos_t1; }
131 
132  const SFVEC3F& GetCameraPos() const { return m_camera_pos; }
133 
137  virtual void Drag( const wxPoint& aNewMousePosition ) = 0;
138 
139  virtual void Pan( const wxPoint& aNewMousePosition ) = 0;
140 
141  virtual void Pan( const SFVEC3F& aDeltaOffsetInc ) = 0;
142 
143  virtual void Pan_T1( const SFVEC3F& aDeltaOffsetInc ) = 0;
144 
148  virtual void Reset();
149  virtual void Reset_T1();
150 
151  void ResetXYpos();
152  void ResetXYpos_T1();
153 
157  void SetCurMousePosition( const wxPoint& aPosition );
158 
159  void ToggleProjection();
161 
167  bool SetCurWindowSize( const wxSize& aSize );
168 
169  void ZoomReset();
170 
171  bool Zoom( float aFactor );
172 
173  bool Zoom_T1( float aFactor );
174 
175  float ZoomGet() const ;
176 
177  void RotateX( float aAngleInRadians );
178  void RotateY( float aAngleInRadians );
179  void RotateZ( float aAngleInRadians );
180 
181  void RotateX_T1( float aAngleInRadians );
182  void RotateY_T1( float aAngleInRadians );
183  void RotateZ_T1( float aAngleInRadians );
184 
188  virtual void SetT0_and_T1_current_T();
189 
195  virtual void Interpolate( float t );
196 
197  void SetInterpolateMode( CAMERA_INTERPOLATION aInterpolateMode )
198  {
199  m_interpolation_mode = aInterpolateMode;
200  }
201 
205  bool ParametersChanged();
206 
211 
219  void MakeRay( const SFVEC2I& aWindowPos, SFVEC3F& aOutOrigin, SFVEC3F& aOutDirection ) const;
220 
229  void MakeRay( const SFVEC2F& aWindowPos, SFVEC3F& aOutOrigin, SFVEC3F& aOutDirection ) const;
230 
237  void MakeRayAtCurrrentMousePosition( SFVEC3F& aOutOrigin, SFVEC3F& aOutDirection ) const;
238 
239 protected:
240  void rebuildProjection();
241  void updateFrustum();
242  void updateViewMatrix();
243 
244  void updateRotationMatrix();
245 
252 
256  float m_zoom;
257  float m_zoom_t0;
258  float m_zoom_t1;
259 
264 
268  wxPoint m_lastPosition;
269 
270  glm::mat4 m_rotationMatrix;
272  glm::mat4 m_viewMatrix;
277 
279 
284 
286 
291 
296 
300 
302 
306  std::vector< float > m_scr_nX;
307  std::vector< float > m_scr_nY;
308 
313  std::vector< SFVEC3F > m_right_nX;
314  std::vector< SFVEC3F > m_up_nY;
315 
320 
328  static const wxChar* m_logTrace;
329 };
330 
331 #endif // CAMERA_H
SFVEC3F fbl
Far Bottom Left.
Definition: camera.h:57
void RotateX(float aAngleInRadians)
Definition: camera.cpp:510
SFVEC3F fc
Definition: camera.h:50
void SetLookAtPos_T1(const SFVEC3F &aLookAtPos)
Definition: camera.h:125
virtual void Pan(const wxPoint &aNewMousePosition)=0
glm::mat4 m_viewMatrixInverse
Definition: camera.h:273
SFVEC3F m_rotate_aux
Stores the rotation angle auxiliary.
Definition: camera.h:297
void SetBoardLookAtPos(const SFVEC3F &aBoardPos)
Definition: camera.h:114
void RotateZ_T1(float aAngleInRadians)
Definition: camera.cpp:543
float m_zoom
3D zoom value (Min 0.0 ...
Definition: camera.h:256
SFVEC3F ftl
Far Top Left.
Definition: camera.h:55
bool Zoom_T1(float aFactor)
Definition: camera.cpp:484
PROJECTION_TYPE GetProjection()
Definition: camera.h:160
virtual void Pan_T1(const SFVEC3F &aDeltaOffsetInc)=0
const glm::mat4 & GetViewMatrix_Inv() const
Definition: camera.cpp:412
bool m_parametersChanged
Set to true if any of the parameters in the camera was changed.
Definition: camera.h:319
const glm::mat4 & GetProjectionMatrix() const
Definition: camera.cpp:376
const SFVEC3F & GetLookAtPos_T1() const
Definition: camera.h:130
virtual void SetLookAtPos(const SFVEC3F &aLookAtPos)=0
SFVEC3F m_lookat_pos_t0
Definition: camera.h:293
bool ParametersChangedQuery() const
Definition: camera.h:210
SFVEC3F m_dir
Definition: camera.h:282
SFVEC3F m_lookat_pos_t1
Definition: camera.h:294
void RotateY_T1(float aAngleInRadians)
Definition: camera.cpp:537
const SFVEC3F & GetPos() const
Definition: camera.h:109
const SFVEC3F & GetRight() const
Definition: camera.h:106
PROJECTION_TYPE
Definition: camera.h:37
virtual void Reset()
Reset the camera to initial state.
Definition: camera.cpp:69
virtual ~CAMERA()
Definition: camera.h:89
float m_zoom_t1
Definition: camera.h:258
SFVEC3F nbr
Near Bottom Right.
Definition: camera.h:54
SFVEC3F m_rotate_aux_t1
Definition: camera.h:299
float ratio
Definition: camera.h:59
const glm::mat4 GetRotationMatrix() const
Get the rotation matrix to be applied in a transformation camera.
Definition: camera.cpp:151
glm::ivec2 SFVEC2I
Definition: xv3d_types.h:39
SFVEC2F m_focalLen
Definition: camera.h:285
std::vector< SFVEC3F > m_right_nX
Precalc values array used to calc ray for each pixel, for X and Y axis of each new camera position.
Definition: camera.h:313
glm::mat4 m_viewMatrix
Definition: camera.h:272
float GetFar() const
Definition: camera.h:112
virtual void Drag(const wxPoint &aNewMousePosition)=0
Calculate a new mouse drag position.
float nw
Definition: camera.h:60
SFVEC3F m_up
Definition: camera.h:281
float fw
Definition: camera.h:60
float tang
Definition: camera.h:59
SFVEC3F fbr
Far Bottom Right.
Definition: camera.h:58
float fh
Definition: camera.h:60
SFVEC3F m_camera_pos_init
Definition: camera.h:287
SFVEC3F m_pos
Definition: camera.h:283
A class used to derive camera objects from.
Definition: camera.h:77
void rebuildProjection()
Definition: camera.cpp:157
Frustum is a implementation based on a tutorial by http://www.lighthouse3d.com/tutorials/view-frustum...
Definition: camera.h:47
PROJECTION_TYPE m_projectionType
Definition: camera.h:276
SFVEC3F nbl
Near Bottom Left.
Definition: camera.h:53
wxPoint m_lastPosition
The last mouse position in the screen.
Definition: camera.h:268
glm::vec2 SFVEC2F
Definition: xv3d_types.h:42
SFVEC3F m_camera_pos_t0
Definition: camera.h:289
void updateRotationMatrix()
Definition: camera.cpp:130
CAMERA(float aRangeScale)
Initialize a camera.
Definition: camera.cpp:54
const glm::mat4 & GetViewMatrix() const
Definition: camera.cpp:406
void RotateY(float aAngleInRadians)
Definition: camera.cpp:517
void MakeRay(const SFVEC2I &aWindowPos, SFVEC3F &aOutOrigin, SFVEC3F &aOutDirection) const
Make a ray based on a windows screen position.
Definition: camera.cpp:308
void SetCurMousePosition(const wxPoint &aPosition)
Update the current mouse position without make any new calculations on camera.
Definition: camera.cpp:418
float farD
Definition: camera.h:59
void updateViewMatrix()
Definition: camera.cpp:122
SFVEC3F m_right
Definition: camera.h:280
virtual void Reset_T1()
Definition: camera.cpp:101
const glm::mat4 & GetProjectionMatrixInv() const
Definition: camera.cpp:382
float m_zoom_t0
Definition: camera.h:257
glm::mat4 m_rotationMatrix
Definition: camera.h:270
void MakeRayAtCurrrentMousePosition(SFVEC3F &aOutOrigin, SFVEC3F &aOutDirection) const
Make a ray based on the latest mouse position.
Definition: camera.cpp:364
SFVEC2I m_windowSize
The window size that this camera is working.
Definition: camera.h:263
SFVEC3F m_camera_pos_t1
Definition: camera.h:290
float ZoomGet() const
Definition: camera.cpp:504
bool Zoom(float aFactor)
Definition: camera.cpp:461
SFVEC3F m_lookat_pos
Definition: camera.h:292
glm::mat4 m_projectionMatrix
Definition: camera.h:274
CAMERA_INTERPOLATION m_interpolation_mode
Definition: camera.h:301
void SetInterpolateMode(CAMERA_INTERPOLATION aInterpolateMode)
Definition: camera.h:197
SFVEC3F m_camera_pos
Definition: camera.h:288
SFVEC3F m_board_lookat_pos_init
Default boardlookat position (the board center).
Definition: camera.h:295
float nearD
Definition: camera.h:59
void updateFrustum()
Definition: camera.cpp:250
bool SetCurWindowSize(const wxSize &aSize)
Update the windows size of the camera.
Definition: camera.cpp:435
std::vector< SFVEC3F > m_up_nY
Definition: camera.h:314
const SFVEC3F & GetDir() const
Definition: camera.h:108
const SFVEC3F & GetUp() const
Definition: camera.h:107
virtual void SetT0_and_T1_current_T()
This will set T0 and T1 with the current values.
Definition: camera.cpp:549
static const wxChar * m_logTrace
Trace mask used to enable or disable the trace output of this class.
Definition: camera.h:328
CAMERA_INTERPOLATION
Definition: camera.h:64
void ResetXYpos()
Definition: camera.cpp:388
CAMERA_FRUSTUM m_frustum
Definition: camera.h:278
virtual void Interpolate(float t)
It will update the matrix to interpolate between T0 and T1 values.
Definition: camera.cpp:563
glm::vec3 SFVEC3F
Definition: xv3d_types.h:44
SFVEC3F ntl
Near Top Left.
Definition: camera.h:51
float m_range_scale
The nominal range expected to be used in the camera.
Definition: camera.h:251
void RotateZ(float aAngleInRadians)
Definition: camera.cpp:524
SFVEC3F ntr
Near Top Right.
Definition: camera.h:52
const SFVEC3F & GetCameraPos() const
Definition: camera.h:132
void ToggleProjection()
Definition: camera.cpp:424
glm::mat4 m_rotationMatrixAux
Definition: camera.h:271
bool ParametersChanged()
Definition: camera.cpp:581
std::vector< float > m_scr_nY
Definition: camera.h:307
void ResetXYpos_T1()
Definition: camera.cpp:399
void ZoomReset()
Definition: camera.cpp:451
std::vector< float > m_scr_nX
Precalc values array used to calc ray for each pixel (constant for the same window size).
Definition: camera.h:306
const SFVEC2F & GetFocalLen() const
Definition: camera.h:110
SFVEC3F nc
Definition: camera.h:49
glm::mat4 m_projectionMatrixInv
Definition: camera.h:275
float nh
Definition: camera.h:60
SFVEC3F m_rotate_aux_t0
Definition: camera.h:298
float angle
Definition: camera.h:59
SFVEC3F ftr
Far Top Right.
Definition: camera.h:56
void RotateX_T1(float aAngleInRadians)
Definition: camera.cpp:531
float GetNear() const
Definition: camera.h:111