1 // Created on: 2013-05-29
2 // Created by: Anton POLETAEV
3 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #ifndef _Graphic3d_Camera_HeaderFile
17 #define _Graphic3d_Camera_HeaderFile
19 #include <Graphic3d_CameraTile.hxx>
20 #include <Graphic3d_Mat4d.hxx>
21 #include <Graphic3d_Mat4.hxx>
22 #include <Graphic3d_Vec3.hxx>
23 #include <Graphic3d_WorldViewProjState.hxx>
25 #include <NCollection_Handle.hxx>
30 #include <Standard_Macro.hxx>
31 #include <Standard_TypeDef.hxx>
33 #include <Bnd_Box.hxx>
35 //! Forward declaration
36 class Graphic3d_WorldViewProjState;
38 //! Camera class provides object-oriented approach to setting up projection
39 //! and orientation properties of 3D view.
40 class Graphic3d_Camera : public Standard_Transient
44 //! Template container for cached matrices or Real/ShortReal types.
45 template<typename Elem_t>
46 struct TransformMatrices
48 void InitOrientation()
50 Orientation = new NCollection_Mat4<Elem_t>();
55 MProjection = new NCollection_Mat4<Elem_t>();
56 LProjection = new NCollection_Mat4<Elem_t>();
57 RProjection = new NCollection_Mat4<Elem_t>();
60 void ResetOrientation()
62 Orientation.Nullify();
65 void ResetProjection()
67 MProjection.Nullify();
68 LProjection.Nullify();
69 RProjection.Nullify();
72 Standard_Boolean IsOrientationValid()
74 return !Orientation.IsNull();
77 Standard_Boolean IsProjectionValid()
79 return !MProjection.IsNull() &&
80 !LProjection.IsNull() &&
81 !RProjection.IsNull();
84 NCollection_Handle< NCollection_Mat4<Elem_t> > Orientation;
85 NCollection_Handle< NCollection_Mat4<Elem_t> > MProjection;
86 NCollection_Handle< NCollection_Mat4<Elem_t> > LProjection;
87 NCollection_Handle< NCollection_Mat4<Elem_t> > RProjection;
92 //! Enumerates supported monographic projections.
93 //! - Projection_Orthographic : orthographic projection.
94 //! - Projection_Perspective : perspective projection.
95 //! - Projection_Stereo : stereographic projection.
96 //! - Projection_MonoLeftEye : mono projection for stereo left eye.
97 //! - Projection_MonoRightEye : mono projection for stereo right eye.
100 Projection_Orthographic,
101 Projection_Perspective,
103 Projection_MonoLeftEye,
104 Projection_MonoRightEye
107 //! Enumerates approaches to define stereographic focus.
108 //! - FocusType_Absolute : focus is specified as absolute value.
109 //! - FocusType_Relative : focus is specified relative to
110 //! (as coefficient of) camera focal length.
117 //! Enumerates approaches to define Intraocular distance.
118 //! - IODType_Absolute : Intraocular distance is defined as absolute value.
119 //! - IODType_Relative : Intraocular distance is defined relative to
120 //! (as coefficient of) camera focal length.
129 //! Default constructor.
130 //! Initializes camera with the following properties:
131 //! Eye (0, 0, -2); Center (0, 0, 0); Up (0, 1, 0);
132 //! Type (Orthographic); FOVy (45); Scale (1000); IsStereo(false);
133 //! ZNear (0.001); ZFar (3000.0); Aspect(1);
134 //! ZFocus(1.0); ZFocusType(Relative); IOD(0.05); IODType(Relative)
135 Standard_EXPORT Graphic3d_Camera();
137 //! Copy constructor.
138 //! @param theOther [in] the camera to copy from.
139 Standard_EXPORT Graphic3d_Camera (const Handle(Graphic3d_Camera)& theOther);
141 //! Initialize mapping related parameters from other camera handle.
142 Standard_EXPORT void CopyMappingData (const Handle(Graphic3d_Camera)& theOtherCamera);
144 //! Initialize orientation related parameters from other camera handle.
145 Standard_EXPORT void CopyOrientationData (const Handle(Graphic3d_Camera)& theOtherCamera);
147 //! Copy properties of another camera.
148 //! @param theOther [in] the camera to copy from.
149 Standard_EXPORT void Copy (const Handle(Graphic3d_Camera)& theOther);
151 //! @name Public camera properties
154 //! Sets camera Eye position.
155 //! @param theEye [in] the location of camera's Eye.
156 Standard_EXPORT void SetEye (const gp_Pnt& theEye);
158 //! Get camera Eye position.
159 //! @return camera eye location.
160 const gp_Pnt& Eye() const
165 //! Sets Center of the camera.
166 //! @param theCenter [in] the point where the camera looks at.
167 Standard_EXPORT void SetCenter (const gp_Pnt& theCenter);
169 //! Get Center of the camera.
170 //! @return the point where the camera looks at.
171 const gp_Pnt& Center() const
176 //! Sets camera Up direction vector, orthogonal to camera direction.
177 //! @param theUp [in] the Up direction vector.
178 Standard_EXPORT void SetUp (const gp_Dir& theUp);
180 //! Orthogonalize up direction vector.
181 Standard_EXPORT void OrthogonalizeUp();
183 //! Return a copy of orthogonalized up direction vector.
184 Standard_EXPORT gp_Dir OrthogonalizedUp() const;
186 //! Get camera Up direction vector.
187 //! @return Camera's Up direction vector.
188 const gp_Dir& Up() const
193 //! Set camera axial scale.
194 //! @param theAxialScale [in] the axial scale vector.
195 Standard_EXPORT void SetAxialScale (const gp_XYZ& theAxialScale);
197 //! Get camera axial scale.
198 //! @return Camera's axial scale.
199 const gp_XYZ& AxialScale() const
204 //! Set distance of Eye from camera Center.
205 //! @param theDistance [in] the distance.
206 Standard_EXPORT void SetDistance (const Standard_Real theDistance);
208 //! Get distance of Eye from camera Center.
209 //! @return the distance.
210 Standard_EXPORT Standard_Real Distance() const;
212 //! Sets camera look direction.
213 //! @param theDir [in] the direction.
214 Standard_EXPORT void SetDirection (const gp_Dir& theDir);
216 //! Get camera look direction.
217 //! @return camera look direction.
218 Standard_EXPORT gp_Dir Direction() const;
220 //! Sets camera scale. For orthographic projection the scale factor
221 //! corresponds to parallel scale of view mapping (i.e. size
222 //! of viewport). For perspective camera scale is converted to
223 //! distance. The scale specifies equal size of the view projection in
224 //! both dimensions assuming that the aspect is 1.0. The projection height
225 //! and width are specified with the scale and correspondingly multiplied
227 //! @param theScale [in] the scale factor.
228 Standard_EXPORT void SetScale (const Standard_Real theScale);
230 //! Get camera scale.
231 //! @return camera scale factor.
232 Standard_EXPORT Standard_Real Scale() const;
234 //! Change camera projection type.
235 //! When switching to perspective projection from orthographic one,
236 //! the ZNear and ZFar are reset to default values (0.001, 3000.0)
237 //! if less than 0.0.
238 //! @param theProjectionType [in] the camera projection type.
239 Standard_EXPORT void SetProjectionType (const Projection theProjection);
241 //! @return camera projection type.
242 Projection ProjectionType() const
247 //! Check that the camera projection is orthographic.
248 //! @return boolean flag that indicates whether the camera's projection is
249 //! orthographic or not.
250 Standard_Boolean IsOrthographic() const
252 return (myProjType == Projection_Orthographic);
255 //! Check whether the camera projection is stereo.
256 //! Please note that stereo rendering is now implemented with support of
258 //! @return boolean flag indicating whether the stereographic L/R projection
260 Standard_Boolean IsStereo() const
262 return (myProjType == Projection_Stereo);
265 //! Set Field Of View (FOV) in y axis for perspective projection.
266 //! @param theFOVy [in] the FOV in degrees.
267 Standard_EXPORT void SetFOVy (const Standard_Real theFOVy);
269 //! Get Field Of View (FOV) in y axis.
270 //! @return the FOV value in degrees.
271 Standard_Real FOVy() const
276 //! Estimate Z-min and Z-max planes of projection volume to match the
277 //! displayed objects. The methods ensures that view volume will
278 //! be close by depth range to the displayed objects. Fitting assumes that
279 //! for orthogonal projection the view volume contains the displayed objects
280 //! completely. For zoomed perspective view, the view volume is adjusted such
281 //! that it contains the objects or their parts, located in front of the camera.
282 //! @param theScaleFactor [in] the scale factor for Z-range.
283 //! The range between Z-min, Z-max projection volume planes
284 //! evaluated by z fitting method will be scaled using this coefficient.
285 //! Program error exception is thrown if negative or zero value is passed.
286 //! @param theMinMax [in] applicative min max boundaries.
287 //! @param theScaleFactor [in] real graphical boundaries (not accounting infinite flag).
288 Standard_EXPORT bool ZFitAll (const Standard_Real theScaleFactor,
289 const Bnd_Box& theMinMax,
290 const Bnd_Box& theGraphicBB,
291 Standard_Real& theZNear,
292 Standard_Real& theZFar) const;
294 //! Change Z-min and Z-max planes of projection volume to match the displayed objects.
295 void ZFitAll (const Standard_Real theScaleFactor, const Bnd_Box& theMinMax, const Bnd_Box& theGraphicBB)
297 Standard_Real aZNear = 0.0, aZFar = 1.0;
298 ZFitAll (theScaleFactor, theMinMax, theGraphicBB, aZNear, aZFar);
299 SetZRange (aZNear, aZFar);
302 //! Change the Near and Far Z-clipping plane positions.
303 //! For orthographic projection, theZNear, theZFar can be negative or positive.
304 //! For perspective projection, only positive values are allowed.
305 //! Program error exception is raised if non-positive values are
306 //! specified for perspective projection or theZNear >= theZFar.
307 //! @param theZNear [in] the distance of the plane from the Eye.
308 //! @param theZFar [in] the distance of the plane from the Eye.
309 Standard_EXPORT void SetZRange (const Standard_Real theZNear, const Standard_Real theZFar);
311 //! Get the Near Z-clipping plane position.
312 //! @return the distance of the plane from the Eye.
313 Standard_Real ZNear() const
318 //! Get the Far Z-clipping plane position.
319 //! @return the distance of the plane from the Eye.
320 Standard_Real ZFar() const
325 //! Changes width / height display ratio.
326 //! @param theAspect [in] the display ratio.
327 Standard_EXPORT void SetAspect (const Standard_Real theAspect);
329 //! Get camera display ratio.
330 //! @return display ratio.
331 Standard_Real Aspect() const
336 //! Sets stereographic focus distance.
337 //! @param theType [in] the focus definition type. Focus can be defined
338 //! as absolute value or relatively to (as coefficient of) coefficient of
339 //! camera focal length.
340 //! @param theZFocus [in] the focus absolute value or coefficient depending
341 //! on the passed definition type.
342 Standard_EXPORT void SetZFocus (const FocusType theType, const Standard_Real theZFocus);
344 //! Get stereographic focus value.
345 //! @return absolute or relative stereographic focus value
346 //! depending on its definition type.
347 Standard_Real ZFocus() const
352 //! Get stereographic focus definition type.
353 //! @return definition type used for stereographic focus.
354 FocusType ZFocusType() const
359 //! Sets Intraocular distance.
360 //! @param theType [in] the IOD definition type. IOD can be defined as
361 //! absolute value or relatively to (as coefficient of) camera focal length.
362 //! @param theIOD [in] the Intraocular distance.
363 Standard_EXPORT void SetIOD (const IODType theType, const Standard_Real theIOD);
365 //! Get Intraocular distance value.
366 //! @return absolute or relative IOD value depending on its definition type.
367 Standard_Real IOD() const
372 //! Get Intraocular distance definition type.
373 //! @return definition type used for Intraocular distance.
374 IODType GetIODType() const
379 //! Get current tile.
380 const Graphic3d_CameraTile& Tile() const { return myTile; }
382 //! Sets the Tile defining the drawing sub-area within View.
383 //! Note that tile defining a region outside the view boundaries is also valid - use method Graphic3d_CameraTile::Cropped() to assign a cropped copy.
384 //! @param theTile tile definition
385 Standard_EXPORT void SetTile (const Graphic3d_CameraTile& theTile);
387 //! @name Basic camera operations
390 //! Transform orientation components of the camera:
391 //! Eye, Up and Center points.
392 //! @param theTrsf [in] the transformation to apply.
393 Standard_EXPORT void Transform (const gp_Trsf& theTrsf);
395 //! Calculate view plane size at center (target) point
396 //! and distance between ZFar and ZNear planes.
397 //! @return values in form of gp_Pnt (Width, Height, Depth).
398 gp_XYZ ViewDimensions() const
400 return ViewDimensions (Distance());
403 //! Calculate view plane size at center point with specified Z offset
404 //! and distance between ZFar and ZNear planes.
405 //! @param theZValue [in] the distance from the eye in eye-to-center direction
406 //! @return values in form of gp_Pnt (Width, Height, Depth).
407 Standard_EXPORT gp_XYZ ViewDimensions (const Standard_Real theZValue) const;
409 //! Calculate WCS frustum planes for the camera projection volume.
410 //! Frustum is a convex volume determined by six planes directing
412 //! The frustum planes are usually used as inputs for camera algorithms.
413 //! Thus, if any changes to projection matrix calculation are necessary,
414 //! the frustum planes calculation should be also touched.
415 //! @param theLeft [out] the frustum plane for left side of view.
416 //! @param theRight [out] the frustum plane for right side of view.
417 //! @param theBottom [out] the frustum plane for bottom side of view.
418 //! @param theTop [out] the frustum plane for top side of view.
419 //! @param theNear [out] the frustum plane for near side of view.
420 //! @param theFar [out] the frustum plane for far side of view.
421 Standard_EXPORT void Frustum (gp_Pln& theLeft,
426 gp_Pln& theFar) const;
428 //! @name Projection methods
431 //! Project point from world coordinate space to
432 //! normalized device coordinates (mapping).
433 //! @param thePnt [in] the 3D point in WCS.
434 //! @return mapped point in NDC.
435 Standard_EXPORT gp_Pnt Project (const gp_Pnt& thePnt) const;
437 //! Unproject point from normalized device coordinates
438 //! to world coordinate space.
439 //! @param thePnt [in] the NDC point.
440 //! @return 3D point in WCS.
441 Standard_EXPORT gp_Pnt UnProject (const gp_Pnt& thePnt) const;
443 //! Convert point from view coordinate space to
444 //! projection coordinate space.
445 //! @param thePnt [in] the point in VCS.
446 //! @return point in NDC.
447 Standard_EXPORT gp_Pnt ConvertView2Proj (const gp_Pnt& thePnt) const;
449 //! Convert point from projection coordinate space
450 //! to view coordinate space.
451 //! @param thePnt [in] the point in NDC.
452 //! @return point in VCS.
453 Standard_EXPORT gp_Pnt ConvertProj2View (const gp_Pnt& thePnt) const;
455 //! Convert point from world coordinate space to
456 //! view coordinate space.
457 //! @param thePnt [in] the 3D point in WCS.
458 //! @return point in VCS.
459 Standard_EXPORT gp_Pnt ConvertWorld2View (const gp_Pnt& thePnt) const;
461 //! Convert point from view coordinate space to
462 //! world coordinates.
463 //! @param thePnt [in] the 3D point in VCS.
464 //! @return point in WCS.
465 Standard_EXPORT gp_Pnt ConvertView2World (const gp_Pnt& thePnt) const;
467 //! @name Camera modification state
470 //! @return projection modification state of the camera.
471 const Graphic3d_WorldViewProjState& WorldViewProjState() const
473 return myWorldViewProjState;
477 //! Returns modification state of camera projection matrix
478 Standard_Size ProjectionState() const
480 return myWorldViewProjState.ProjectionState();
483 //! Returns modification state of camera world view transformation matrix.
484 Standard_Size WorldViewState() const
486 return myWorldViewProjState.WorldViewState();
489 //! @name Lazily-computed orientation and projection matrices derived from camera parameters
492 //! Get orientation matrix.
493 //! @return camera orientation matrix.
494 Standard_EXPORT const Graphic3d_Mat4d& OrientationMatrix() const;
496 //! Get orientation matrix of Standard_ShortReal precision.
497 //! @return camera orientation matrix.
498 Standard_EXPORT const Graphic3d_Mat4& OrientationMatrixF() const;
500 //! Get monographic or middle point projection matrix used for monographic
501 //! rendering and for point projection / unprojection.
502 //! @return monographic projection matrix.
503 Standard_EXPORT const Graphic3d_Mat4d& ProjectionMatrix() const;
505 //! Get monographic or middle point projection matrix of Standard_ShortReal precision used for monographic
506 //! rendering and for point projection / unprojection.
507 //! @return monographic projection matrix.
508 Standard_EXPORT const Graphic3d_Mat4& ProjectionMatrixF() const;
510 //! @return stereographic matrix computed for left eye. Please note
511 //! that this method is used for rendering for <i>Projection_Stereo</i>.
512 Standard_EXPORT const Graphic3d_Mat4d& ProjectionStereoLeft() const;
514 //! @return stereographic matrix of Standard_ShortReal precision computed for left eye.
515 //! Please note that this method is used for rendering for <i>Projection_Stereo</i>.
516 Standard_EXPORT const Graphic3d_Mat4& ProjectionStereoLeftF() const;
518 //! @return stereographic matrix computed for right eye. Please note
519 //! that this method is used for rendering for <i>Projection_Stereo</i>.
520 Standard_EXPORT const Graphic3d_Mat4d& ProjectionStereoRight() const;
522 //! @return stereographic matrix of Standard_ShortReal precision computed for right eye.
523 //! Please note that this method is used for rendering for <i>Projection_Stereo</i>.
524 Standard_EXPORT const Graphic3d_Mat4& ProjectionStereoRightF() const;
526 //! Invalidate state of projection matrix.
527 //! The matrix will be updated on request.
528 Standard_EXPORT void InvalidateProjection();
530 //! Invalidate orientation matrix.
531 //! The matrix will be updated on request.
532 Standard_EXPORT void InvalidateOrientation();
534 //! @name Managing projection and orientation cache
537 //! Compute projection matrices.
538 //! @param theMatrices [in] the matrices data container.
539 template <typename Elem_t>
541 TransformMatrices<Elem_t>& UpdateProjection (TransformMatrices<Elem_t>& theMatrices) const;
543 //! Compute orientation matrix.
544 //! @param theMatrices [in] the matrices data container.
545 template <typename Elem_t>
547 TransformMatrices<Elem_t>& UpdateOrientation (TransformMatrices<Elem_t>& theMatrices) const;
551 //! Compose orthographic projection matrix for
552 //! the passed camera volume mapping.
553 //! @param theLeft [in] the left mapping (clipping) coordinate.
554 //! @param theRight [in] the right mapping (clipping) coordinate.
555 //! @param theBottom [in] the bottom mapping (clipping) coordinate.
556 //! @param theTop [in] the top mapping (clipping) coordinate.
557 //! @param theNear [in] the near mapping (clipping) coordinate.
558 //! @param theFar [in] the far mapping (clipping) coordinate.
559 //! @param theOutMx [out] the projection matrix.
560 template <typename Elem_t>
562 OrthoProj (const Elem_t theLeft,
563 const Elem_t theRight,
564 const Elem_t theBottom,
566 const Elem_t theNear,
568 NCollection_Mat4<Elem_t>& theOutMx);
570 //! Compose perspective projection matrix for
571 //! the passed camera volume mapping.
572 //! @param theLeft [in] the left mapping (clipping) coordinate.
573 //! @param theRight [in] the right mapping (clipping) coordinate.
574 //! @param theBottom [in] the bottom mapping (clipping) coordinate.
575 //! @param theTop [in] the top mapping (clipping) coordinate.
576 //! @param theNear [in] the near mapping (clipping) coordinate.
577 //! @param theFar [in] the far mapping (clipping) coordinate.
578 //! @param theOutMx [out] the projection matrix.
579 template <typename Elem_t>
581 PerspectiveProj (const Elem_t theLeft,
582 const Elem_t theRight,
583 const Elem_t theBottom,
585 const Elem_t theNear,
587 NCollection_Mat4<Elem_t>& theOutMx);
589 //! Compose projection matrix for L/R stereo eyes.
590 //! @param theLeft [in] the left mapping (clipping) coordinate.
591 //! @param theRight [in] the right mapping (clipping) coordinate.
592 //! @param theBottom [in] the bottom mapping (clipping) coordinate.
593 //! @param theTop [in] the top mapping (clipping) coordinate.
594 //! @param theNear [in] the near mapping (clipping) coordinate.
595 //! @param theFar [in] the far mapping (clipping) coordinate.
596 //! @param theIOD [in] the Intraocular distance.
597 //! @param theZFocus [in] the z coordinate of off-axis
598 //! projection plane with zero parallax.
599 //! @param theIsLeft [in] boolean flag to choose between L/R eyes.
600 //! @param theOutMx [out] the projection matrix.
601 template <typename Elem_t>
603 StereoEyeProj (const Elem_t theLeft,
604 const Elem_t theRight,
605 const Elem_t theBottom,
607 const Elem_t theNear,
610 const Elem_t theZFocus,
611 const Standard_Boolean theIsLeft,
612 NCollection_Mat4<Elem_t>& theOutMx);
614 //! Construct "look at" orientation transformation.
615 //! Reference point differs for perspective and ortho modes
616 //! (made for compatibility, to be improved..).
617 //! @param theEye [in] the eye coordinates in 3D space.
618 //! @param theLookAt [in] the point the camera looks at.
619 //! @param theUpDir [in] the up direction vector.
620 //! @param theAxialScale [in] the axial scale vector.
621 //! @param theOutMx [in/out] the orientation matrix.
622 template <typename Elem_t>
624 LookOrientation (const NCollection_Vec3<Elem_t>& theEye,
625 const NCollection_Vec3<Elem_t>& theLookAt,
626 const NCollection_Vec3<Elem_t>& theUpDir,
627 const NCollection_Vec3<Elem_t>& theAxialScale,
628 NCollection_Mat4<Elem_t>& theOutMx);
632 gp_Dir myUp; //!< Camera up direction vector.
633 gp_Pnt myEye; //!< Camera eye position.
634 gp_Pnt myCenter; //!< Camera center.
636 gp_XYZ myAxialScale; //!< World axial scale.
638 Projection myProjType; //!< Projection type used for rendering.
639 Standard_Real myFOVy; //!< Field Of View in y axis.
640 Standard_Real myZNear; //!< Distance to near clipping plane.
641 Standard_Real myZFar; //!< Distance to far clipping plane.
642 Standard_Real myAspect; //!< Width to height display ratio.
644 Standard_Real myScale; //!< Specifies parallel scale for orthographic projection.
645 Standard_Real myZFocus; //!< Stereographic focus value.
646 FocusType myZFocusType; //!< Stereographic focus definition type.
648 Standard_Real myIOD; //!< Intraocular distance value.
649 IODType myIODType; //!< Intraocular distance definition type.
651 Graphic3d_CameraTile myTile;//!< Tile defining sub-area for drawing
653 mutable TransformMatrices<Standard_Real> myMatricesD;
654 mutable TransformMatrices<Standard_ShortReal> myMatricesF;
656 mutable Graphic3d_WorldViewProjState myWorldViewProjState;
660 DEFINE_STANDARD_RTTIEXT(Graphic3d_Camera,Standard_Transient)
663 DEFINE_STANDARD_HANDLE (Graphic3d_Camera, Standard_Transient)