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 <[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 
30 #ifndef CAMERA_H
31 #define CAMERA_H
32 
33 #include "../3d_rendering/raytracing/ray.h"
34 #include <wx/gdicmn.h> // for wxSize
35 #include <vector>
36 
37 enum class PROJECTION_TYPE
38 {
39  ORTHO = 0,
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:
85  explicit CAMERA( float aInitialDistance );
86 
87  virtual ~CAMERA()
88  {
89  }
90 
96  const glm::mat4 GetRotationMatrix() const;
97 
98  const glm::mat4& GetViewMatrix() const;
99  const glm::mat4& GetViewMatrix_Inv() const;
100 
101  const glm::mat4& GetProjectionMatrix() const;
102  const glm::mat4& GetProjectionMatrixInv() const;
103 
104  const SFVEC3F& GetRight() const { return m_right; }
105  const SFVEC3F& GetUp() const { return m_up; }
106  const SFVEC3F& GetDir() const { return m_dir; }
107  const SFVEC3F& GetPos() const { return m_pos; }
108  const SFVEC2F& GetFocalLen() const { return m_focalLen; }
109  float GetNear() const { return m_frustum.nearD; }
110  float GetFar() const { return m_frustum.farD; }
111 
112  void SetBoardLookAtPos( const SFVEC3F& aBoardPos )
113  {
114  if( m_board_lookat_pos_init != aBoardPos )
115  {
116  m_board_lookat_pos_init = aBoardPos;
117  SetLookAtPos( aBoardPos );
118  }
119  }
120 
121  virtual void SetLookAtPos( const SFVEC3F& aLookAtPos ) = 0;
122 
123  void SetLookAtPos_T1( const SFVEC3F& aLookAtPos )
124  {
125  m_lookat_pos_t1 = aLookAtPos;
126  }
127 
128  const SFVEC3F& GetLookAtPos_T1() const { return m_lookat_pos_t1; }
129 
130  const SFVEC3F& GetCameraPos() const { return m_camera_pos; }
131  const SFVEC3F& GetCameraInitPos() const { return m_camera_pos_init; }
132 
133  float GetCameraMinDimension() const;
134 
138  virtual void Drag( const wxPoint& aNewMousePosition ) = 0;
139 
140  virtual void Pan( const wxPoint& aNewMousePosition ) = 0;
141 
142  virtual void Pan( const SFVEC3F& aDeltaOffsetInc ) = 0;
143 
144  virtual void Pan_T1( const SFVEC3F& aDeltaOffsetInc ) = 0;
145 
149  virtual void Reset();
150  virtual void Reset_T1();
151 
152  void ResetXYpos();
153  void ResetXYpos_T1();
154 
158  void SetCurMousePosition( const wxPoint& aPosition );
159 
160  void ToggleProjection();
162  void SetProjection( PROJECTION_TYPE aProjection ) { m_projectionType = aProjection; }
163 
169  bool SetCurWindowSize( const wxSize& aSize );
170 
171  void ZoomReset();
172 
173  bool Zoom( float aFactor );
174 
175  bool Zoom_T1( float aFactor );
176 
177  float GetZoom() const { return m_zoom; }
178 
179  float GetMinZoom() { return m_minZoom; }
180  void SetMinZoom( float minZoom )
181  {
182  m_minZoom = minZoom;
183  zoomChanged();
184  }
185 
186  float GetMaxZoom() { return m_maxZoom; }
187  void SetMaxZoom( float maxZoom )
188  {
189  m_maxZoom = maxZoom;
190  zoomChanged();
191  }
192 
193  void RotateX( float aAngleInRadians );
194  void RotateY( float aAngleInRadians );
195  void RotateZ( float aAngleInRadians );
196 
197  void RotateX_T1( float aAngleInRadians );
198  void RotateY_T1( float aAngleInRadians );
199  void RotateZ_T1( float aAngleInRadians );
200 
204  virtual void SetT0_and_T1_current_T();
205 
211  virtual void Interpolate( float t );
212 
213  void SetInterpolateMode( CAMERA_INTERPOLATION aInterpolateMode )
214  {
215  m_interpolation_mode = aInterpolateMode;
216  }
217 
221  bool ParametersChanged();
222 
227 
235  void MakeRay( const SFVEC2I& aWindowPos, SFVEC3F& aOutOrigin, SFVEC3F& aOutDirection ) const;
236 
245  void MakeRay( const SFVEC2F& aWindowPos, SFVEC3F& aOutOrigin, SFVEC3F& aOutDirection ) const;
246 
253  void MakeRayAtCurrrentMousePosition( SFVEC3F& aOutOrigin, SFVEC3F& aOutDirection ) const;
254 
255 protected:
256  void zoomChanged();
257  void rebuildProjection();
258  void updateFrustum();
259  void updateViewMatrix();
260 
261  void updateRotationMatrix();
262 
266  float m_zoom;
267  float m_zoom_t0;
268  float m_zoom_t1;
269 
273  float m_minZoom;
274  float m_maxZoom;
275 
280 
284  wxPoint m_lastPosition;
285 
286  glm::mat4 m_rotationMatrix;
288  glm::mat4 m_viewMatrix;
293 
295 
300 
302 
307 
312 
316 
318 
322  std::vector< float > m_scr_nX;
323  std::vector< float > m_scr_nY;
324 
329  std::vector< SFVEC3F > m_right_nX;
330  std::vector< SFVEC3F > m_up_nY;
331 
336 
344  static const wxChar* m_logTrace;
345 };
346 
347 #endif // CAMERA_H
SFVEC3F fbl
Far Bottom Left.
Definition: camera.h:57
void SetMinZoom(float minZoom)
Definition: camera.h:180
void RotateX(float aAngleInRadians)
Definition: camera.cpp:519
SFVEC3F fc
Definition: camera.h:50
void SetLookAtPos_T1(const SFVEC3F &aLookAtPos)
Definition: camera.h:123
virtual void Pan(const wxPoint &aNewMousePosition)=0
glm::mat4 m_viewMatrixInverse
Definition: camera.h:289
SFVEC3F m_rotate_aux
Stores the rotation angle auxiliary.
Definition: camera.h:313
void SetBoardLookAtPos(const SFVEC3F &aBoardPos)
Definition: camera.h:112
void RotateZ_T1(float aAngleInRadians)
Definition: camera.cpp:552
float m_zoom
3D zoom value – Z-distance is scaled by it
Definition: camera.h:266
SFVEC3F ftl
Far Top Left.
Definition: camera.h:55
bool Zoom_T1(float aFactor)
Definition: camera.cpp:497
PROJECTION_TYPE GetProjection()
Definition: camera.h:161
virtual void Pan_T1(const SFVEC3F &aDeltaOffsetInc)=0
const glm::mat4 & GetViewMatrix_Inv() const
Definition: camera.cpp:433
bool m_parametersChanged
Set to true if any of the parameters in the camera was changed.
Definition: camera.h:335
CAMERA(float aInitialDistance)
Initialize a camera.
Definition: camera.cpp:54
const SFVEC3F & GetCameraInitPos() const
Definition: camera.h:131
const glm::mat4 & GetProjectionMatrix() const
Definition: camera.cpp:391
const SFVEC3F & GetLookAtPos_T1() const
Definition: camera.h:128
virtual void SetLookAtPos(const SFVEC3F &aLookAtPos)=0
SFVEC3F m_lookat_pos_t0
Definition: camera.h:309
bool ParametersChangedQuery() const
Definition: camera.h:226
SFVEC3F m_dir
Definition: camera.h:298
SFVEC3F m_lookat_pos_t1
Definition: camera.h:310
void RotateY_T1(float aAngleInRadians)
Definition: camera.cpp:546
const SFVEC3F & GetPos() const
Definition: camera.h:107
const SFVEC3F & GetRight() const
Definition: camera.h:104
PROJECTION_TYPE
Definition: camera.h:37
virtual void Reset()
Reset the camera to initial state.
Definition: camera.cpp:71
virtual ~CAMERA()
Definition: camera.h:87
float m_zoom_t1
Definition: camera.h:268
SFVEC3F nbr
Near Bottom Right.
Definition: camera.h:54
SFVEC3F m_rotate_aux_t1
Definition: camera.h:315
float GetMinZoom()
Definition: camera.h:179
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:168
glm::ivec2 SFVEC2I
Definition: xv3d_types.h:39
SFVEC2F m_focalLen
Definition: camera.h:301
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:329
glm::mat4 m_viewMatrix
Definition: camera.h:288
float GetFar() const
Definition: camera.h:110
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:297
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:303
SFVEC3F m_pos
Definition: camera.h:299
float GetCameraMinDimension() const
Definition: camera.cpp:403
A class used to derive camera objects from.
Definition: camera.h:77
float GetMaxZoom()
Definition: camera.h:186
void rebuildProjection()
Definition: camera.cpp:174
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:292
SFVEC3F nbl
Near Bottom Left.
Definition: camera.h:53
wxPoint m_lastPosition
The last mouse position in the screen.
Definition: camera.h:284
glm::vec2 SFVEC2F
Definition: xv3d_types.h:42
SFVEC3F m_camera_pos_t0
Definition: camera.h:305
void updateRotationMatrix()
Definition: camera.cpp:147
const glm::mat4 & GetViewMatrix() const
Definition: camera.cpp:427
void RotateY(float aAngleInRadians)
Definition: camera.cpp:526
void MakeRay(const SFVEC2I &aWindowPos, SFVEC3F &aOutOrigin, SFVEC3F &aOutDirection) const
Make a ray based on a windows screen position.
Definition: camera.cpp:323
void SetCurMousePosition(const wxPoint &aPosition)
Update the current mouse position without make any new calculations on camera.
Definition: camera.cpp:439
float farD
Definition: camera.h:59
float GetZoom() const
Definition: camera.h:177
void updateViewMatrix()
Definition: camera.cpp:139
SFVEC3F m_right
Definition: camera.h:296
virtual void Reset_T1()
Definition: camera.cpp:103
void zoomChanged()
Definition: camera.cpp:124
const glm::mat4 & GetProjectionMatrixInv() const
Definition: camera.cpp:397
float m_zoom_t0
Definition: camera.h:267
glm::mat4 m_rotationMatrix
Definition: camera.h:286
void MakeRayAtCurrrentMousePosition(SFVEC3F &aOutOrigin, SFVEC3F &aOutDirection) const
Make a ray based on the latest mouse position.
Definition: camera.cpp:379
float m_maxZoom
Definition: camera.h:274
SFVEC2I m_windowSize
The window size that this camera is working.
Definition: camera.h:279
SFVEC3F m_camera_pos_t1
Definition: camera.h:306
bool Zoom(float aFactor)
Definition: camera.cpp:482
SFVEC3F m_lookat_pos
Definition: camera.h:308
glm::mat4 m_projectionMatrix
Definition: camera.h:290
CAMERA_INTERPOLATION m_interpolation_mode
Definition: camera.h:317
void SetInterpolateMode(CAMERA_INTERPOLATION aInterpolateMode)
Definition: camera.h:213
SFVEC3F m_camera_pos
Definition: camera.h:304
SFVEC3F m_board_lookat_pos_init
Default boardlookat position (the board center).
Definition: camera.h:311
float nearD
Definition: camera.h:59
void updateFrustum()
Definition: camera.cpp:265
bool SetCurWindowSize(const wxSize &aSize)
Update the windows size of the camera.
Definition: camera.cpp:456
float m_minZoom
Possible 3D zoom range.
Definition: camera.h:273
std::vector< SFVEC3F > m_up_nY
Definition: camera.h:330
const SFVEC3F & GetDir() const
Definition: camera.h:106
const SFVEC3F & GetUp() const
Definition: camera.h:105
virtual void SetT0_and_T1_current_T()
This will set T0 and T1 with the current values.
Definition: camera.cpp:558
static const wxChar * m_logTrace
Trace mask used to enable or disable the trace output of this class.
Definition: camera.h:344
CAMERA_INTERPOLATION
Definition: camera.h:64
void ResetXYpos()
Definition: camera.cpp:409
CAMERA_FRUSTUM m_frustum
Definition: camera.h:294
virtual void Interpolate(float t)
It will update the matrix to interpolate between T0 and T1 values.
Definition: camera.cpp:572
glm::vec3 SFVEC3F
Definition: xv3d_types.h:44
SFVEC3F ntl
Near Top Left.
Definition: camera.h:51
void RotateZ(float aAngleInRadians)
Definition: camera.cpp:533
SFVEC3F ntr
Near Top Right.
Definition: camera.h:52
const SFVEC3F & GetCameraPos() const
Definition: camera.h:130
void SetMaxZoom(float maxZoom)
Definition: camera.h:187
void ToggleProjection()
Definition: camera.cpp:445
glm::mat4 m_rotationMatrixAux
Definition: camera.h:287
bool ParametersChanged()
Definition: camera.cpp:590
std::vector< float > m_scr_nY
Definition: camera.h:323
void ResetXYpos_T1()
Definition: camera.cpp:420
void ZoomReset()
Definition: camera.cpp:472
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:322
const SFVEC2F & GetFocalLen() const
Definition: camera.h:108
SFVEC3F nc
Definition: camera.h:49
glm::mat4 m_projectionMatrixInv
Definition: camera.h:291
float nh
Definition: camera.h:60
SFVEC3F m_rotate_aux_t0
Definition: camera.h:314
float angle
Definition: camera.h:59
SFVEC3F ftr
Far Top Right.
Definition: camera.h:56
void SetProjection(PROJECTION_TYPE aProjection)
Definition: camera.h:162
void RotateX_T1(float aAngleInRadians)
Definition: camera.cpp:540
float GetNear() const
Definition: camera.h:109