0024413: Visualization - get rid of projection shift from orthographic camera definition
[occt.git] / src / Graphic3d / Graphic3d_Camera.hxx
CommitLineData
b5ac8292 1// Created on: 2013-05-29
2// Created by: Anton POLETAEV
3// Copyright (c) 1999-2014 OPEN CASCADE SAS
4//
5// This file is part of Open CASCADE Technology software library.
6//
d5f74e42 7// This library is free software; you can redistribute it and/or modify it under
8// the terms of the GNU Lesser General Public License version 2.1 as published
b5ac8292 9// by the Free Software Foundation, with special exception defined in the file
10// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11// distribution for complete text of the license and disclaimer of any warranty.
12//
13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
15
16#ifndef _Graphic3d_Camera_HeaderFile
17#define _Graphic3d_Camera_HeaderFile
18
197ac94e 19#include <Graphic3d_Mat4d.hxx>
b5ac8292 20#include <Graphic3d_Mat4.hxx>
21#include <Graphic3d_Vec3.hxx>
22
197ac94e 23#include <NCollection_Handle.hxx>
24
b5ac8292 25#include <gp_Dir.hxx>
26#include <gp_Pnt.hxx>
27
28#include <Standard_Macro.hxx>
29#include <Standard_TypeDef.hxx>
30
31DEFINE_STANDARD_HANDLE (Graphic3d_Camera, Standard_Transient)
32
33//! Camera class provides object-oriented approach to setting up projection
34//! and orientation properties of 3D view.
35class Graphic3d_Camera : public Standard_Transient
36{
197ac94e 37private:
38
39 //! Template container for cached matrices or Real/ShortReal types.
40 template<typename Elem_t>
41 struct TransformMatrices
42 {
43 void InitOrientation()
44 {
45 Orientation = new NCollection_Mat4<Elem_t>();
46 }
47
48 void InitProjection()
49 {
50 MProjection = new NCollection_Mat4<Elem_t>();
51 LProjection = new NCollection_Mat4<Elem_t>();
52 RProjection = new NCollection_Mat4<Elem_t>();
53 }
54
55 void ResetOrientation()
56 {
57 Orientation.Nullify();
58 }
59
60 void ResetProjection()
61 {
62 MProjection.Nullify();
63 LProjection.Nullify();
64 RProjection.Nullify();
65 }
66
67 Standard_Boolean IsOrientationValid()
68 {
69 return !Orientation.IsNull();
70 }
71
72 Standard_Boolean IsProjectionValid()
73 {
74 return !MProjection.IsNull() &&
75 !LProjection.IsNull() &&
76 !RProjection.IsNull();
77 }
78
79 NCollection_Handle< NCollection_Mat4<Elem_t> > Orientation;
80 NCollection_Handle< NCollection_Mat4<Elem_t> > MProjection;
81 NCollection_Handle< NCollection_Mat4<Elem_t> > LProjection;
82 NCollection_Handle< NCollection_Mat4<Elem_t> > RProjection;
83 };
b5ac8292 84
85public:
86
87 //! Enumerates supported monographic projections.
88 //! - Projection_Orthographic : orthographic projection.
89 //! - Projection_Perspective : perspective projection.
90 //! - Projection_Stere : stereographic projection.
91 //! - Projection_MonoLeftEye : mono projection for stereo left eye.
92 //! - Projection_MonoRightEye : mono projection for stereo right eye.
93 enum Projection
94 {
95 Projection_Orthographic,
96 Projection_Perspective,
97 Projection_Stereo,
98 Projection_MonoLeftEye,
99 Projection_MonoRightEye
100 };
101
102 //! Enumerates approaches to define stereographic focus.
103 //! - FocusType_Absolute : focus is specified as absolute value.
104 //! - FocusType_Relative : focus is specified relative to
105 //! (as coefficient of) camera focal length.
106 enum FocusType
107 {
108 FocusType_Absolute,
109 FocusType_Relative
110 };
111
112 //! Enumerates approaches to define Intraocular distance.
113 //! - IODType_Absolute : Intraocular distance is defined as absolute value.
114 //! - IODType_Relative : Intraocular distance is defined relative to
115 //! (as coefficient of) camera focal length.
116 enum IODType
117 {
118 IODType_Absolute,
119 IODType_Relative
120 };
121
122public:
123
124 //! Default constructor.
125 //! Initializes camera with the following properties:
126 //! Eye (0, 0, -2); Center (0, 0, 0); Up (0, 1, 0);
127 //! Type (Orthographic); FOVy (45); Scale (1000); IsStereo(false);
197ac94e 128 //! ZNear (0.001); ZFar (3000.0); Aspect(1);
b5ac8292 129 //! ZFocus(1.0); ZFocusType(Relative); IOD(0.05); IODType(Relative)
130 Standard_EXPORT Graphic3d_Camera();
131
132 //! Copy constructor.
133 //! @param theOther [in] the camera to copy from.
134 Standard_EXPORT Graphic3d_Camera (const Handle(Graphic3d_Camera)& theOther);
135
136 //! Initialize mapping related parameters from other camera handle.
137 Standard_EXPORT void CopyMappingData (const Handle(Graphic3d_Camera)& theOtherCamera);
138
139 //! Initialize orientation related parameters from other camera handle.
140 Standard_EXPORT void CopyOrientationData (const Handle(Graphic3d_Camera)& theOtherCamera);
141
142 //! Copy properties of another camera.
143 //! @param theOther [in] the camera to copy from.
144 Standard_EXPORT void Copy (const Handle(Graphic3d_Camera)& theOther);
145
197ac94e 146//! @name Public camera properties
147public:
b5ac8292 148
149 //! Sets camera Eye position.
150 //! @param theEye [in] the location of camera's Eye.
151 Standard_EXPORT void SetEye (const gp_Pnt& theEye);
152
153 //! Get camera Eye position.
154 //! @return camera eye location.
155 const gp_Pnt& Eye() const
156 {
157 return myEye;
158 }
159
160 //! Sets Center of the camera.
161 //! @param theCenter [in] the point where the camera looks at.
162 Standard_EXPORT void SetCenter (const gp_Pnt& theCenter);
163
164 //! Get Center of the camera.
165 //! @return the point where the camera looks at.
166 const gp_Pnt& Center() const
167 {
168 return myCenter;
169 }
170
197ac94e 171 //! Sets camera Up direction vector, orthogonal to camera direction.
b5ac8292 172 //! @param theUp [in] the Up direction vector.
173 Standard_EXPORT void SetUp (const gp_Dir& theUp);
174
197ac94e 175 //! Orthogonalize up direction vector.
176 Standard_EXPORT void OrthogonalizeUp();
177
178 //! Return a copy of orthogonalized up direction vector.
179 Standard_EXPORT gp_Dir OrthogonalizedUp() const;
180
b5ac8292 181 //! Get camera Up direction vector.
182 //! @return Camera's Up direction vector.
183 const gp_Dir& Up() const
184 {
185 return myUp;
186 }
187
b5ac8292 188 //! Set camera axial scale.<br>
189 //! @param theAxialScale [in] the axial scale vector.
197ac94e 190 Standard_EXPORT void SetAxialScale (const gp_XYZ& theAxialScale);
b5ac8292 191
192 //! Get camera axial scale.
193 //! @return Camera's axial scale.
197ac94e 194 const gp_XYZ& AxialScale() const
b5ac8292 195 {
196 return myAxialScale;
197 }
198
199 //! Set distance of Eye from camera Center.
200 //! @param theDistance [in] the distance.
201 Standard_EXPORT void SetDistance (const Standard_Real theDistance);
202
203 //! Get distance of Eye from camera Center.
204 //! @return the distance.
205 Standard_EXPORT Standard_Real Distance() const;
206
207 //! Sets camera look direction.
208 //! @param theDir [in] the direction.
209 Standard_EXPORT void SetDirection (const gp_Dir& theDir);
210
211 //! Get camera look direction.
212 //! @return camera look direction.
213 Standard_EXPORT gp_Dir Direction() const;
214
215 //! Sets camera scale. For orthographic projection the scale factor
216 //! corresponds to parallel scale of view mapping (i.e. size
217 //! of viewport). For perspective camera scale is converted to
218 //! distance.
219 //! @param theScale [in] the scale factor.
220 Standard_EXPORT void SetScale (const Standard_Real theScale);
221
222 //! Get camera scale.
223 //! @return camera scale factor.
224 Standard_EXPORT Standard_Real Scale() const;
225
226 //! Change camera projection type.
197ac94e 227 //! When switching to perspective projection from orthographic one,
228 //! the ZNear and ZFar are reset to default values (0.001, 3000.0)
229 //! if less than 0.0.
b5ac8292 230 //! @param theProjectionType [in] the camera projection type.
231 Standard_EXPORT void SetProjectionType (const Projection theProjection);
232
233 //! @return camera projection type.
234 Projection ProjectionType() const
235 {
236 return myProjType;
237 }
238
239 //! Check that the camera projection is orthographic.
240 //! @return boolean flag that indicates whether the camera's projection is
241 //! orthographic or not.
242 Standard_Boolean IsOrthographic() const
243 {
244 return (myProjType == Projection_Orthographic);
245 }
246
247 //! Check whether the camera projection is stereo.
248 //! Please note that stereo rendering is now implemented with support of
249 //! Quad buffering.
250 //! @return boolean flag indicating whether the stereographic L/R projection
251 //! is chosen.
252 Standard_Boolean IsStereo() const
253 {
254 return (myProjType == Projection_Stereo);
255 }
256
257 //! Set Field Of View (FOV) in y axis for perspective projection.
258 //! @param theFOVy [in] the FOV in degrees.
259 Standard_EXPORT void SetFOVy (const Standard_Real theFOVy);
260
261 //! Get Field Of View (FOV) in y axis.
262 //! @return the FOV value in degrees.
263 Standard_Real FOVy() const
264 {
265 return myFOVy;
266 }
267
197ac94e 268 //! Change the Near and Far Z-clipping plane positions.
269 //! For orthographic projection, theZNear, theZFar can be negative or positive.
270 //! For perspective projection, only positive values are allowed.
271 //! Program error exception is raised if non-positive values are
272 //! specified for perspective projection or theZNear >= theZFar.
b5ac8292 273 //! @param theZNear [in] the distance of the plane from the Eye.
197ac94e 274 //! @param theZFar [in] the distance of the plane from the Eye.
275 Standard_EXPORT void SetZRange (const Standard_Real theZNear, const Standard_Real theZFar);
b5ac8292 276
277 //! Get the Near Z-clipping plane position.
278 //! @return the distance of the plane from the Eye.
279 Standard_Real ZNear() const
280 {
281 return myZNear;
282 }
283
b5ac8292 284 //! Get the Far Z-clipping plane position.
285 //! @return the distance of the plane from the Eye.
286 Standard_Real ZFar() const
287 {
288 return myZFar;
289 }
290
291 //! Change display ratio.
292 //! @param theAspect [in] the display ratio.
293 Standard_EXPORT void SetAspect (const Standard_Real theAspect);
294
295 //! Get camera display ratio.
296 //! @return display ratio.
297 Standard_Real Aspect() const
298 {
299 return myAspect;
300 }
301
302 //! Sets stereographic focus distance.
303 //! @param theType [in] the focus definition type. Focus can be defined
304 //! as absolute value or relatively to (as coefficient of) coefficient of
305 //! camera focal length.
306 //! @param theZFocus [in] the focus absolute value or coefficient depending
307 //! on the passed definition type.
308 Standard_EXPORT void SetZFocus (const FocusType theType, const Standard_Real theZFocus);
309
310 //! Get stereographic focus value.
311 //! @return absolute or relative stereographic focus value
312 //! depending on its definition type.
313 Standard_Real ZFocus() const
314 {
315 return myZFocus;
316 }
317
318 //! Get stereographic focus definition type.
319 //! @return definition type used for stereographic focus.
320 FocusType ZFocusType() const
321 {
322 return myZFocusType;
323 }
324
325 //! Sets Intraocular distance.
326 //! @param theType [in] the IOD definition type. IOD can be defined as
327 //! absolute value or relatively to (as coefficient of) camera focal length.
328 //! @param theIOD [in] the Intraocular distance.
329 Standard_EXPORT void SetIOD (const IODType theType, const Standard_Real theIOD);
330
331 //! Get Intraocular distance value.
332 //! @return absolute or relative IOD value depending on its definition type.
333 Standard_Real IOD() const
334 {
335 return myIOD;
336 }
337
338 //! Get Intraocular distance definition type.
339 //! @return definition type used for Intraocular distance.
340 IODType GetIODType() const
341 {
342 return myIODType;
343 }
344
197ac94e 345//! @name Basic camera operations
b5ac8292 346public:
347
348 //! Transform orientation components of the camera:
349 //! Eye, Up and Center points.
350 //! @param theTrsf [in] the transformation to apply.
351 Standard_EXPORT void Transform (const gp_Trsf& theTrsf);
352
353 //! Calculate view plane size at center (target) point
354 //! and distance between ZFar and ZNear planes.
355 //! @return values in form of gp_Pnt (Width, Height, Depth).
197ac94e 356 Standard_EXPORT gp_XYZ ViewDimensions() const;
357
358 //! Calculate WCS frustum planes for the camera projection volume.
359 //! Frustum is a convex volume determined by six planes directing
360 //! inwards.
361 //! The frustum planes are usually used as inputs for camera algorithms.
362 //! Thus, if any changes to projection matrix calculation are necessary,
363 //! the frustum planes calculation should be also touched.
364 //! @param theLeft [out] the frustum plane for left side of view.
365 //! @param theRight [out] the frustum plane for right side of view.
366 //! @param theBottom [out] the frustum plane for bottom side of view.
367 //! @param theTop [out] the frustum plane for top side of view.
368 //! @param theNear [out] the frustum plane for near side of view.
369 //! @param theFar [out] the frustum plane for far side of view.
370 Standard_EXPORT void Frustum (gp_Pln& theLeft,
371 gp_Pln& theRight,
372 gp_Pln& theBottom,
373 gp_Pln& theTop,
374 gp_Pln& theNear,
375 gp_Pln& theFar) const;
376
377//! @name Projection methods
b5ac8292 378public:
379
380 //! Project point from world coordinate space to
381 //! normalized device coordinates (mapping).
382 //! @param thePnt [in] the 3D point in WCS.
383 //! @return mapped point in NDC.
384 Standard_EXPORT gp_Pnt Project (const gp_Pnt& thePnt) const;
385
386 //! Unproject point from normalized device coordinates
387 //! to world coordinate space.
388 //! @param thePnt [in] the NDC point.
389 //! @return 3D point in WCS.
390 Standard_EXPORT gp_Pnt UnProject (const gp_Pnt& thePnt) const;
391
392 //! Convert point from view coordinate space to
393 //! projection coordinate space.
394 //! @param thePnt [in] the point in VCS.
395 //! @return point in NDC.
396 Standard_EXPORT gp_Pnt ConvertView2Proj (const gp_Pnt& thePnt) const;
397
398 //! Convert point from projection coordinate space
399 //! to view coordinate space.
400 //! @param thePnt [in] the point in NDC.
401 //! @return point in VCS.
402 Standard_EXPORT gp_Pnt ConvertProj2View (const gp_Pnt& thePnt) const;
403
404 //! Convert point from world coordinate space to
405 //! view coordinate space.
406 //! @param thePnt [in] the 3D point in WCS.
407 //! @return point in VCS.
408 Standard_EXPORT gp_Pnt ConvertWorld2View (const gp_Pnt& thePnt) const;
409
410 //! Convert point from view coordinate space to
411 //! world coordinates.
412 //! @param thePnt [in] the 3D point in VCS.
413 //! @return point in WCS.
414 Standard_EXPORT gp_Pnt ConvertView2World (const gp_Pnt& thePnt) const;
415
197ac94e 416//! @name Camera modification state
b5ac8292 417public:
418
197ac94e 419 //! Returns modification state of camera projection matrix
420 Standard_Size ProjectionState() const
421 {
422 return myProjectionState;
423 }
424
425 //! Returns modification state of camera model-view matrix
426 Standard_Size ModelViewState() const
427 {
428 return myOrientationState;
429 }
430
431//! @name Lazily-computed orientation and projection matrices derived from camera parameters
432public:
b5ac8292 433
197ac94e 434 //! Get orientation matrix.
435 //! @return camera orientation matrix.
436 Standard_EXPORT const Graphic3d_Mat4d& OrientationMatrix() const;
437
438 //! Get orientation matrix of Standard_ShortReal precision.
439 //! @return camera orientation matrix.
440 Standard_EXPORT const Graphic3d_Mat4& OrientationMatrixF() const;
441
442 //! Get monographic or middle point projection matrix used for monographic
443 //! rendering and for point projection / unprojection.
444 //! @return monographic projection matrix.
445 Standard_EXPORT const Graphic3d_Mat4d& ProjectionMatrix() const;
446
447 //! Get monographic or middle point projection matrix of Standard_ShortReal precision used for monographic
448 //! rendering and for point projection / unprojection.
449 //! @return monographic projection matrix.
450 Standard_EXPORT const Graphic3d_Mat4& ProjectionMatrixF() const;
451
452 //! @return stereographic matrix computed for left eye. Please note
453 //! that this method is used for rendering for <i>Projection_Stereo</i>.
454 Standard_EXPORT const Graphic3d_Mat4d& ProjectionStereoLeft() const;
455
456 //! @return stereographic matrix of Standard_ShortReal precision computed for left eye.
457 //! Please note that this method is used for rendering for <i>Projection_Stereo</i>.
458 Standard_EXPORT const Graphic3d_Mat4& ProjectionStereoLeftF() const;
459
460 //! @return stereographic matrix computed for right eye. Please note
461 //! that this method is used for rendering for <i>Projection_Stereo</i>.
462 Standard_EXPORT const Graphic3d_Mat4d& ProjectionStereoRight() const;
463
464 //! @return stereographic matrix of Standard_ShortReal precision computed for right eye.
465 //! Please note that this method is used for rendering for <i>Projection_Stereo</i>.
466 Standard_EXPORT const Graphic3d_Mat4& ProjectionStereoRightF() const;
467
468//! @name Managing projection and orientation cache
469private:
470
471 //! Compute projection matrices.
472 //! @param theMatrices [in] the matrices data container.
473 template <typename Elem_t>
474 Standard_EXPORT
475 TransformMatrices<Elem_t>& UpdateProjection (TransformMatrices<Elem_t>& theMatrices) const;
476
477 //! Compute orientation matrix.
478 //! @param theMatrices [in] the matrices data container.
479 template <typename Elem_t>
480 Standard_EXPORT
481 TransformMatrices<Elem_t>& UpdateOrientation (TransformMatrices<Elem_t>& theMatrices) const;
482
483 //! Invalidate state of projection matrix.
484 //! The matrix will be updated on request.
485 void InvalidateProjection();
486
487 //! Invalidate orientation matrix.
488 //! The matrix will be updated on request.
489 void InvalidateOrientation();
b5ac8292 490
491private:
492
493 //! Compose orthographic projection matrix for
494 //! the passed camera volume mapping.
495 //! @param theLeft [in] the left mapping (clipping) coordinate.
496 //! @param theRight [in] the right mapping (clipping) coordinate.
497 //! @param theBottom [in] the bottom mapping (clipping) coordinate.
498 //! @param theTop [in] the top mapping (clipping) coordinate.
499 //! @param theNear [in] the near mapping (clipping) coordinate.
500 //! @param theFar [in] the far mapping (clipping) coordinate.
b5ac8292 501 //! @param theOutMx [out] the projection matrix.
197ac94e 502 template <typename Elem_t>
b5ac8292 503 static void
197ac94e 504 OrthoProj (const Elem_t theLeft,
505 const Elem_t theRight,
506 const Elem_t theBottom,
507 const Elem_t theTop,
508 const Elem_t theNear,
509 const Elem_t theFar,
510 NCollection_Mat4<Elem_t>& theOutMx);
b5ac8292 511
512 //! Compose perspective projection matrix for
513 //! the passed camera volume mapping.
514 //! @param theLeft [in] the left mapping (clipping) coordinate.
515 //! @param theRight [in] the right mapping (clipping) coordinate.
516 //! @param theBottom [in] the bottom mapping (clipping) coordinate.
517 //! @param theTop [in] the top mapping (clipping) coordinate.
518 //! @param theNear [in] the near mapping (clipping) coordinate.
519 //! @param theFar [in] the far mapping (clipping) coordinate.
b5ac8292 520 //! @param theOutMx [out] the projection matrix.
197ac94e 521 template <typename Elem_t>
b5ac8292 522 static void
197ac94e 523 PerspectiveProj (const Elem_t theLeft,
524 const Elem_t theRight,
525 const Elem_t theBottom,
526 const Elem_t theTop,
527 const Elem_t theNear,
528 const Elem_t theFar,
529 NCollection_Mat4<Elem_t>& theOutMx);
b5ac8292 530
531 //! Compose projection matrix for L/R stereo eyes.
532 //! @param theLeft [in] the left mapping (clipping) coordinate.
533 //! @param theRight [in] the right mapping (clipping) coordinate.
534 //! @param theBottom [in] the bottom mapping (clipping) coordinate.
535 //! @param theTop [in] the top mapping (clipping) coordinate.
536 //! @param theNear [in] the near mapping (clipping) coordinate.
537 //! @param theFar [in] the far mapping (clipping) coordinate.
538 //! @param theIOD [in] the Intraocular distance.
539 //! @param theZFocus [in] the z coordinate of off-axis
540 //! projection plane with zero parallax.
b5ac8292 541 //! @param theIsLeft [in] boolean flag to choose between L/R eyes.
542 //! @param theOutMx [out] the projection matrix.
197ac94e 543 template <typename Elem_t>
b5ac8292 544 static void
197ac94e 545 StereoEyeProj (const Elem_t theLeft,
546 const Elem_t theRight,
547 const Elem_t theBottom,
548 const Elem_t theTop,
549 const Elem_t theNear,
550 const Elem_t theFar,
551 const Elem_t theIOD,
552 const Elem_t theZFocus,
553 const Standard_Boolean theIsLeft,
554 NCollection_Mat4<Elem_t>& theOutMx);
b5ac8292 555
556 //! Construct "look at" orientation transformation.
557 //! Reference point differs for perspective and ortho modes
558 //! (made for compatibility, to be improved..).
559 //! @param theEye [in] the eye coordinates in 3D space.
560 //! @param theLookAt [in] the point the camera looks at.
561 //! @param theUpDir [in] the up direction vector.
562 //! @param theAxialScale [in] the axial scale vector.
563 //! @param theOutMx [in/out] the orientation matrix.
197ac94e 564 template <typename Elem_t>
b5ac8292 565 static void
197ac94e 566 LookOrientation (const NCollection_Vec3<Elem_t>& theEye,
567 const NCollection_Vec3<Elem_t>& theLookAt,
568 const NCollection_Vec3<Elem_t>& theUpDir,
569 const NCollection_Vec3<Elem_t>& theAxialScale,
570 NCollection_Mat4<Elem_t>& theOutMx);
b5ac8292 571
572private:
573
574 gp_Dir myUp; //!< Camera up direction vector.
575 gp_Pnt myEye; //!< Camera eye position.
576 gp_Pnt myCenter; //!< Camera center.
577
197ac94e 578 gp_XYZ myAxialScale; //!< World axial scale.
b5ac8292 579
197ac94e 580 Projection myProjType; //!< Projection type used for rendering.
581 Standard_Real myFOVy; //!< Field Of View in y axis.
582 Standard_Real myZNear; //!< Distance to near clipping plane.
583 Standard_Real myZFar; //!< Distance to far clipping plane.
584 Standard_Real myAspect; //!< Width to height display ratio.
b5ac8292 585
586 Standard_Real myScale; //!< Specifies parallel scale for orthographic projection.
587 Standard_Real myZFocus; //!< Stereographic focus value.
588 FocusType myZFocusType; //!< Stereographic focus definition type.
589
590 Standard_Real myIOD; //!< Intraocular distance value.
591 IODType myIODType; //!< Intraocular distance definition type.
592
197ac94e 593 mutable TransformMatrices<Standard_Real> myMatricesD;
594 mutable TransformMatrices<Standard_ShortReal> myMatricesF;
b5ac8292 595
197ac94e 596 mutable Standard_Size myProjectionState;
597 mutable Standard_Size myOrientationState;
b5ac8292 598
599public:
600
601 DEFINE_STANDARD_RTTI(Graphic3d_Camera);
602
603};
604
605#endif