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 = 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:
87  explicit CAMERA( float aRangeScale, float aDefaultZoom );
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  void SetProjection( PROJECTION_TYPE aProjection ) { m_projectionType = aProjection; }
162 
168  bool SetCurWindowSize( const wxSize& aSize );
169 
170  void ZoomReset();
171 
172  bool Zoom( float aFactor );
173 
174  bool Zoom_T1( float aFactor );
175 
176  float ZoomGet() const ;
177 
178  void RotateX( float aAngleInRadians );
179  void RotateY( float aAngleInRadians );
180  void RotateZ( float aAngleInRadians );
181 
182  void RotateX_T1( float aAngleInRadians );
183  void RotateY_T1( float aAngleInRadians );
184  void RotateZ_T1( float aAngleInRadians );
185 
189  virtual void SetT0_and_T1_current_T();
190 
196  virtual void Interpolate( float t );
197 
198  void SetInterpolateMode( CAMERA_INTERPOLATION aInterpolateMode )
199  {
200  m_interpolation_mode = aInterpolateMode;
201  }
202 
206  bool ParametersChanged();
207 
212 
220  void MakeRay( const SFVEC2I& aWindowPos, SFVEC3F& aOutOrigin, SFVEC3F& aOutDirection ) const;
221 
230  void MakeRay( const SFVEC2F& aWindowPos, SFVEC3F& aOutOrigin, SFVEC3F& aOutDirection ) const;
231 
238  void MakeRayAtCurrrentMousePosition( SFVEC3F& aOutOrigin, SFVEC3F& aOutDirection ) const;
239 
240 protected:
241  void rebuildProjection();
242  void updateFrustum();
243  void updateViewMatrix();
244 
245  void updateRotationMatrix();
246 
253 
254 
259  float m_zoom;
260  float m_zoom_t0;
261  float m_zoom_t1;
262 
267 
271  wxPoint m_lastPosition;
272 
273  glm::mat4 m_rotationMatrix;
275  glm::mat4 m_viewMatrix;
280 
282 
287 
289 
294 
299 
303 
305 
309  std::vector< float > m_scr_nX;
310  std::vector< float > m_scr_nY;
311 
316  std::vector< SFVEC3F > m_right_nX;
317  std::vector< SFVEC3F > m_up_nY;
318 
323 
331  static const wxChar* m_logTrace;
332 };
333 
334 #endif // CAMERA_H
SFVEC3F fbl
Far Bottom Left.
Definition: camera.h:57
void RotateX(float aAngleInRadians)
Definition: camera.cpp:511
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:276
SFVEC3F m_rotate_aux
Stores the rotation angle auxiliary.
Definition: camera.h:300
void SetBoardLookAtPos(const SFVEC3F &aBoardPos)
Definition: camera.h:114
void RotateZ_T1(float aAngleInRadians)
Definition: camera.cpp:544
float m_zoom
Definition: camera.h:259
SFVEC3F ftl
Far Top Left.
Definition: camera.h:55
bool Zoom_T1(float aFactor)
Definition: camera.cpp:485
PROJECTION_TYPE GetProjection()
Definition: camera.h:160
virtual void Pan_T1(const SFVEC3F &aDeltaOffsetInc)=0
const glm::mat4 & GetViewMatrix_Inv() const
Definition: camera.cpp:413
bool m_parametersChanged
Set to true if any of the parameters in the camera was changed.
Definition: camera.h:322
const glm::mat4 & GetProjectionMatrix() const
Definition: camera.cpp:377
const SFVEC3F & GetLookAtPos_T1() const
Definition: camera.h:130
virtual void SetLookAtPos(const SFVEC3F &aLookAtPos)=0
SFVEC3F m_lookat_pos_t0
Definition: camera.h:296
bool ParametersChangedQuery() const
Definition: camera.h:211
SFVEC3F m_dir
Definition: camera.h:285
SFVEC3F m_lookat_pos_t1
Definition: camera.h:297
void RotateY_T1(float aAngleInRadians)
Definition: camera.cpp:538
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:70
virtual ~CAMERA()
Definition: camera.h:89
float m_zoom_t1
Definition: camera.h:261
SFVEC3F nbr
Near Bottom Right.
Definition: camera.h:54
SFVEC3F m_rotate_aux_t1
Definition: camera.h:302
float ratio
Definition: camera.h:59
float m_default_zoom
3D zoom value (Min 0.0 ...
Definition: camera.h:258
const glm::mat4 GetRotationMatrix() const
Get the rotation matrix to be applied in a transformation camera.
Definition: camera.cpp:152
glm::ivec2 SFVEC2I
Definition: xv3d_types.h:39
SFVEC2F m_focalLen
Definition: camera.h:288
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:316
glm::mat4 m_viewMatrix
Definition: camera.h:275
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:284
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:290
SFVEC3F m_pos
Definition: camera.h:286
A class used to derive camera objects from.
Definition: camera.h:77
void rebuildProjection()
Definition: camera.cpp:158
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:279
SFVEC3F nbl
Near Bottom Left.
Definition: camera.h:53
wxPoint m_lastPosition
The last mouse position in the screen.
Definition: camera.h:271
glm::vec2 SFVEC2F
Definition: xv3d_types.h:42
SFVEC3F m_camera_pos_t0
Definition: camera.h:292
void updateRotationMatrix()
Definition: camera.cpp:131
const glm::mat4 & GetViewMatrix() const
Definition: camera.cpp:407
void RotateY(float aAngleInRadians)
Definition: camera.cpp:518
void MakeRay(const SFVEC2I &aWindowPos, SFVEC3F &aOutOrigin, SFVEC3F &aOutDirection) const
Make a ray based on a windows screen position.
Definition: camera.cpp:309
void SetCurMousePosition(const wxPoint &aPosition)
Update the current mouse position without make any new calculations on camera.
Definition: camera.cpp:419
float farD
Definition: camera.h:59
void updateViewMatrix()
Definition: camera.cpp:123
SFVEC3F m_right
Definition: camera.h:283
virtual void Reset_T1()
Definition: camera.cpp:102
const glm::mat4 & GetProjectionMatrixInv() const
Definition: camera.cpp:383
float m_zoom_t0
Definition: camera.h:260
glm::mat4 m_rotationMatrix
Definition: camera.h:273
void MakeRayAtCurrrentMousePosition(SFVEC3F &aOutOrigin, SFVEC3F &aOutDirection) const
Make a ray based on the latest mouse position.
Definition: camera.cpp:365
SFVEC2I m_windowSize
The window size that this camera is working.
Definition: camera.h:266
SFVEC3F m_camera_pos_t1
Definition: camera.h:293
float ZoomGet() const
Definition: camera.cpp:505
bool Zoom(float aFactor)
Definition: camera.cpp:462
SFVEC3F m_lookat_pos
Definition: camera.h:295
glm::mat4 m_projectionMatrix
Definition: camera.h:277
CAMERA_INTERPOLATION m_interpolation_mode
Definition: camera.h:304
void SetInterpolateMode(CAMERA_INTERPOLATION aInterpolateMode)
Definition: camera.h:198
SFVEC3F m_camera_pos
Definition: camera.h:291
SFVEC3F m_board_lookat_pos_init
Default boardlookat position (the board center).
Definition: camera.h:298
float nearD
Definition: camera.h:59
void updateFrustum()
Definition: camera.cpp:251
bool SetCurWindowSize(const wxSize &aSize)
Update the windows size of the camera.
Definition: camera.cpp:436
std::vector< SFVEC3F > m_up_nY
Definition: camera.h:317
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:550
static const wxChar * m_logTrace
Trace mask used to enable or disable the trace output of this class.
Definition: camera.h:331
CAMERA_INTERPOLATION
Definition: camera.h:64
void ResetXYpos()
Definition: camera.cpp:389
CAMERA(float aRangeScale, float aDefaultZoom)
Initialize a camera.
Definition: camera.cpp:54
CAMERA_FRUSTUM m_frustum
Definition: camera.h:281
virtual void Interpolate(float t)
It will update the matrix to interpolate between T0 and T1 values.
Definition: camera.cpp:564
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:252
void RotateZ(float aAngleInRadians)
Definition: camera.cpp:525
SFVEC3F ntr
Near Top Right.
Definition: camera.h:52
const SFVEC3F & GetCameraPos() const
Definition: camera.h:132
void ToggleProjection()
Definition: camera.cpp:425
glm::mat4 m_rotationMatrixAux
Definition: camera.h:274
bool ParametersChanged()
Definition: camera.cpp:582
std::vector< float > m_scr_nY
Definition: camera.h:310
void ResetXYpos_T1()
Definition: camera.cpp:400
void ZoomReset()
Definition: camera.cpp:452
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:309
const SFVEC2F & GetFocalLen() const
Definition: camera.h:110
SFVEC3F nc
Definition: camera.h:49
glm::mat4 m_projectionMatrixInv
Definition: camera.h:278
float nh
Definition: camera.h:60
SFVEC3F m_rotate_aux_t0
Definition: camera.h:301
float angle
Definition: camera.h:59
SFVEC3F ftr
Far Top Right.
Definition: camera.h:56
void SetProjection(PROJECTION_TYPE aProjection)
Definition: camera.h:161
void RotateX_T1(float aAngleInRadians)
Definition: camera.cpp:532
float GetNear() const
Definition: camera.h:111