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_Mat4d.hxx>
20 #include <Graphic3d_Mat4.hxx>
21 #include <Graphic3d_Vec3.hxx>
23 #include <NCollection_Handle.hxx>
28 #include <Standard_Macro.hxx>
29 #include <Standard_TypeDef.hxx>
31 #include <Bnd_Box.hxx>
33 DEFINE_STANDARD_HANDLE (Graphic3d_Camera, Standard_Transient)
35 //! Camera class provides object-oriented approach to setting up projection
36 //! and orientation properties of 3D view.
37 class Graphic3d_Camera : public Standard_Transient
41 //! Template container for cached matrices or Real/ShortReal types.
42 template<typename Elem_t>
43 struct TransformMatrices
45 void InitOrientation()
47 Orientation = new NCollection_Mat4<Elem_t>();
52 MProjection = new NCollection_Mat4<Elem_t>();
53 LProjection = new NCollection_Mat4<Elem_t>();
54 RProjection = new NCollection_Mat4<Elem_t>();
57 void ResetOrientation()
59 Orientation.Nullify();
62 void ResetProjection()
64 MProjection.Nullify();
65 LProjection.Nullify();
66 RProjection.Nullify();
69 Standard_Boolean IsOrientationValid()
71 return !Orientation.IsNull();
74 Standard_Boolean IsProjectionValid()
76 return !MProjection.IsNull() &&
77 !LProjection.IsNull() &&
78 !RProjection.IsNull();
81 NCollection_Handle< NCollection_Mat4<Elem_t> > Orientation;
82 NCollection_Handle< NCollection_Mat4<Elem_t> > MProjection;
83 NCollection_Handle< NCollection_Mat4<Elem_t> > LProjection;
84 NCollection_Handle< NCollection_Mat4<Elem_t> > RProjection;
89 //! Enumerates supported monographic projections.
90 //! - Projection_Orthographic : orthographic projection.
91 //! - Projection_Perspective : perspective projection.
92 //! - Projection_Stere : stereographic projection.
93 //! - Projection_MonoLeftEye : mono projection for stereo left eye.
94 //! - Projection_MonoRightEye : mono projection for stereo right eye.
97 Projection_Orthographic,
98 Projection_Perspective,
100 Projection_MonoLeftEye,
101 Projection_MonoRightEye
104 //! Enumerates approaches to define stereographic focus.
105 //! - FocusType_Absolute : focus is specified as absolute value.
106 //! - FocusType_Relative : focus is specified relative to
107 //! (as coefficient of) camera focal length.
114 //! Enumerates approaches to define Intraocular distance.
115 //! - IODType_Absolute : Intraocular distance is defined as absolute value.
116 //! - IODType_Relative : Intraocular distance is defined relative to
117 //! (as coefficient of) camera focal length.
126 //! Default constructor.
127 //! Initializes camera with the following properties:
128 //! Eye (0, 0, -2); Center (0, 0, 0); Up (0, 1, 0);
129 //! Type (Orthographic); FOVy (45); Scale (1000); IsStereo(false);
130 //! ZNear (0.001); ZFar (3000.0); Aspect(1);
131 //! ZFocus(1.0); ZFocusType(Relative); IOD(0.05); IODType(Relative)
132 Standard_EXPORT Graphic3d_Camera();
134 //! Copy constructor.
135 //! @param theOther [in] the camera to copy from.
136 Standard_EXPORT Graphic3d_Camera (const Handle(Graphic3d_Camera)& theOther);
138 //! Initialize mapping related parameters from other camera handle.
139 Standard_EXPORT void CopyMappingData (const Handle(Graphic3d_Camera)& theOtherCamera);
141 //! Initialize orientation related parameters from other camera handle.
142 Standard_EXPORT void CopyOrientationData (const Handle(Graphic3d_Camera)& theOtherCamera);
144 //! Copy properties of another camera.
145 //! @param theOther [in] the camera to copy from.
146 Standard_EXPORT void Copy (const Handle(Graphic3d_Camera)& theOther);
148 //! @name Public camera properties
151 //! Sets camera Eye position.
152 //! @param theEye [in] the location of camera's Eye.
153 Standard_EXPORT void SetEye (const gp_Pnt& theEye);
155 //! Get camera Eye position.
156 //! @return camera eye location.
157 const gp_Pnt& Eye() const
162 //! Sets Center of the camera.
163 //! @param theCenter [in] the point where the camera looks at.
164 Standard_EXPORT void SetCenter (const gp_Pnt& theCenter);
166 //! Get Center of the camera.
167 //! @return the point where the camera looks at.
168 const gp_Pnt& Center() const
173 //! Sets camera Up direction vector, orthogonal to camera direction.
174 //! @param theUp [in] the Up direction vector.
175 Standard_EXPORT void SetUp (const gp_Dir& theUp);
177 //! Orthogonalize up direction vector.
178 Standard_EXPORT void OrthogonalizeUp();
180 //! Return a copy of orthogonalized up direction vector.
181 Standard_EXPORT gp_Dir OrthogonalizedUp() const;
183 //! Get camera Up direction vector.
184 //! @return Camera's Up direction vector.
185 const gp_Dir& Up() const
190 //! Set camera axial scale.
191 //! @param theAxialScale [in] the axial scale vector.
192 Standard_EXPORT void SetAxialScale (const gp_XYZ& theAxialScale);
194 //! Get camera axial scale.
195 //! @return Camera's axial scale.
196 const gp_XYZ& AxialScale() const
201 //! Set distance of Eye from camera Center.
202 //! @param theDistance [in] the distance.
203 Standard_EXPORT void SetDistance (const Standard_Real theDistance);
205 //! Get distance of Eye from camera Center.
206 //! @return the distance.
207 Standard_EXPORT Standard_Real Distance() const;
209 //! Sets camera look direction.
210 //! @param theDir [in] the direction.
211 Standard_EXPORT void SetDirection (const gp_Dir& theDir);
213 //! Get camera look direction.
214 //! @return camera look direction.
215 Standard_EXPORT gp_Dir Direction() const;
217 //! Sets camera scale. For orthographic projection the scale factor
218 //! corresponds to parallel scale of view mapping (i.e. size
219 //! of viewport). For perspective camera scale is converted to
220 //! distance. The scale specifies equal size of the view projection in
221 //! both dimensions assuming that the aspect is 1.0. The projection height
222 //! and width are specified with the scale and correspondingly multiplied
224 //! @param theScale [in] the scale factor.
225 Standard_EXPORT void SetScale (const Standard_Real theScale);
227 //! Get camera scale.
228 //! @return camera scale factor.
229 Standard_EXPORT Standard_Real Scale() const;
231 //! Change camera projection type.
232 //! When switching to perspective projection from orthographic one,
233 //! the ZNear and ZFar are reset to default values (0.001, 3000.0)
234 //! if less than 0.0.
235 //! @param theProjectionType [in] the camera projection type.
236 Standard_EXPORT void SetProjectionType (const Projection theProjection);
238 //! @return camera projection type.
239 Projection ProjectionType() const
244 //! Check that the camera projection is orthographic.
245 //! @return boolean flag that indicates whether the camera's projection is
246 //! orthographic or not.
247 Standard_Boolean IsOrthographic() const
249 return (myProjType == Projection_Orthographic);
252 //! Check whether the camera projection is stereo.
253 //! Please note that stereo rendering is now implemented with support of
255 //! @return boolean flag indicating whether the stereographic L/R projection
257 Standard_Boolean IsStereo() const
259 return (myProjType == Projection_Stereo);
262 //! Set Field Of View (FOV) in y axis for perspective projection.
263 //! @param theFOVy [in] the FOV in degrees.
264 Standard_EXPORT void SetFOVy (const Standard_Real theFOVy);
266 //! Get Field Of View (FOV) in y axis.
267 //! @return the FOV value in degrees.
268 Standard_Real FOVy() const
273 //! Change Z-min and Z-max planes of projection volume to match the
274 //! displayed objects. The methods ensures that view volume will
275 //! be close by depth range to the displayed objects. Fitting assumes that
276 //! for orthogonal projection the view volume contains the displayed objects
277 //! completely. For zoomed perspective view, the view volume is adjusted such
278 //! that it contains the objects or their parts, located in front of the camera.
279 //! @param theScaleFactor [in] the scale factor for Z-range.
280 //! The range between Z-min, Z-max projection volume planes
281 //! evaluated by z fitting method will be scaled using this coefficient.
282 //! Program error exception is thrown if negative or zero value is passed.
283 //! @param theMinMax [in] applicative min max boundaries.
284 //! @param theScaleFactor [in] real graphical boundaries (not accounting infinite flag).
286 Standard_EXPORT void ZFitAll (const Standard_Real theScaleFactor, const Bnd_Box& theMinMax, const Bnd_Box& theGraphicBB);
288 //! Change the Near and Far Z-clipping plane positions.
289 //! For orthographic projection, theZNear, theZFar can be negative or positive.
290 //! For perspective projection, only positive values are allowed.
291 //! Program error exception is raised if non-positive values are
292 //! specified for perspective projection or theZNear >= theZFar.
293 //! @param theZNear [in] the distance of the plane from the Eye.
294 //! @param theZFar [in] the distance of the plane from the Eye.
295 Standard_EXPORT void SetZRange (const Standard_Real theZNear, const Standard_Real theZFar);
297 //! Get the Near Z-clipping plane position.
298 //! @return the distance of the plane from the Eye.
299 Standard_Real ZNear() const
304 //! Get the Far Z-clipping plane position.
305 //! @return the distance of the plane from the Eye.
306 Standard_Real ZFar() const
311 //! Changes width / height display ratio.
312 //! @param theAspect [in] the display ratio.
313 Standard_EXPORT void SetAspect (const Standard_Real theAspect);
315 //! Get camera display ratio.
316 //! @return display ratio.
317 Standard_Real Aspect() const
322 //! Sets stereographic focus distance.
323 //! @param theType [in] the focus definition type. Focus can be defined
324 //! as absolute value or relatively to (as coefficient of) coefficient of
325 //! camera focal length.
326 //! @param theZFocus [in] the focus absolute value or coefficient depending
327 //! on the passed definition type.
328 Standard_EXPORT void SetZFocus (const FocusType theType, const Standard_Real theZFocus);
330 //! Get stereographic focus value.
331 //! @return absolute or relative stereographic focus value
332 //! depending on its definition type.
333 Standard_Real ZFocus() const
338 //! Get stereographic focus definition type.
339 //! @return definition type used for stereographic focus.
340 FocusType ZFocusType() const
345 //! Sets Intraocular distance.
346 //! @param theType [in] the IOD definition type. IOD can be defined as
347 //! absolute value or relatively to (as coefficient of) camera focal length.
348 //! @param theIOD [in] the Intraocular distance.
349 Standard_EXPORT void SetIOD (const IODType theType, const Standard_Real theIOD);
351 //! Get Intraocular distance value.
352 //! @return absolute or relative IOD value depending on its definition type.
353 Standard_Real IOD() const
358 //! Get Intraocular distance definition type.
359 //! @return definition type used for Intraocular distance.
360 IODType GetIODType() const
365 //! @name Basic camera operations
368 //! Transform orientation components of the camera:
369 //! Eye, Up and Center points.
370 //! @param theTrsf [in] the transformation to apply.
371 Standard_EXPORT void Transform (const gp_Trsf& theTrsf);
373 //! Calculate view plane size at center (target) point
374 //! and distance between ZFar and ZNear planes.
375 //! @return values in form of gp_Pnt (Width, Height, Depth).
376 Standard_EXPORT gp_XYZ ViewDimensions() const;
378 //! Calculate WCS frustum planes for the camera projection volume.
379 //! Frustum is a convex volume determined by six planes directing
381 //! The frustum planes are usually used as inputs for camera algorithms.
382 //! Thus, if any changes to projection matrix calculation are necessary,
383 //! the frustum planes calculation should be also touched.
384 //! @param theLeft [out] the frustum plane for left side of view.
385 //! @param theRight [out] the frustum plane for right side of view.
386 //! @param theBottom [out] the frustum plane for bottom side of view.
387 //! @param theTop [out] the frustum plane for top side of view.
388 //! @param theNear [out] the frustum plane for near side of view.
389 //! @param theFar [out] the frustum plane for far side of view.
390 Standard_EXPORT void Frustum (gp_Pln& theLeft,
395 gp_Pln& theFar) const;
397 //! @name Projection methods
400 //! Project point from world coordinate space to
401 //! normalized device coordinates (mapping).
402 //! @param thePnt [in] the 3D point in WCS.
403 //! @return mapped point in NDC.
404 Standard_EXPORT gp_Pnt Project (const gp_Pnt& thePnt) const;
406 //! Unproject point from normalized device coordinates
407 //! to world coordinate space.
408 //! @param thePnt [in] the NDC point.
409 //! @return 3D point in WCS.
410 Standard_EXPORT gp_Pnt UnProject (const gp_Pnt& thePnt) const;
412 //! Convert point from view coordinate space to
413 //! projection coordinate space.
414 //! @param thePnt [in] the point in VCS.
415 //! @return point in NDC.
416 Standard_EXPORT gp_Pnt ConvertView2Proj (const gp_Pnt& thePnt) const;
418 //! Convert point from projection coordinate space
419 //! to view coordinate space.
420 //! @param thePnt [in] the point in NDC.
421 //! @return point in VCS.
422 Standard_EXPORT gp_Pnt ConvertProj2View (const gp_Pnt& thePnt) const;
424 //! Convert point from world coordinate space to
425 //! view coordinate space.
426 //! @param thePnt [in] the 3D point in WCS.
427 //! @return point in VCS.
428 Standard_EXPORT gp_Pnt ConvertWorld2View (const gp_Pnt& thePnt) const;
430 //! Convert point from view coordinate space to
431 //! world coordinates.
432 //! @param thePnt [in] the 3D point in VCS.
433 //! @return point in WCS.
434 Standard_EXPORT gp_Pnt ConvertView2World (const gp_Pnt& thePnt) const;
436 //! @name Camera modification state
439 //! Returns modification state of camera projection matrix
440 Standard_Size ProjectionState() const
442 return myProjectionState;
445 //! Returns modification state of camera model-view matrix
446 Standard_Size ModelViewState() const
448 return myOrientationState;
451 //! @name Lazily-computed orientation and projection matrices derived from camera parameters
454 //! Get orientation matrix.
455 //! @return camera orientation matrix.
456 Standard_EXPORT const Graphic3d_Mat4d& OrientationMatrix() const;
458 //! Get orientation matrix of Standard_ShortReal precision.
459 //! @return camera orientation matrix.
460 Standard_EXPORT const Graphic3d_Mat4& OrientationMatrixF() const;
462 //! Get monographic or middle point projection matrix used for monographic
463 //! rendering and for point projection / unprojection.
464 //! @return monographic projection matrix.
465 Standard_EXPORT const Graphic3d_Mat4d& ProjectionMatrix() const;
467 //! Get monographic or middle point projection matrix of Standard_ShortReal precision used for monographic
468 //! rendering and for point projection / unprojection.
469 //! @return monographic projection matrix.
470 Standard_EXPORT const Graphic3d_Mat4& ProjectionMatrixF() const;
472 //! @return stereographic matrix computed for left eye. Please note
473 //! that this method is used for rendering for <i>Projection_Stereo</i>.
474 Standard_EXPORT const Graphic3d_Mat4d& ProjectionStereoLeft() const;
476 //! @return stereographic matrix of Standard_ShortReal precision computed for left eye.
477 //! Please note that this method is used for rendering for <i>Projection_Stereo</i>.
478 Standard_EXPORT const Graphic3d_Mat4& ProjectionStereoLeftF() const;
480 //! @return stereographic matrix computed for right eye. Please note
481 //! that this method is used for rendering for <i>Projection_Stereo</i>.
482 Standard_EXPORT const Graphic3d_Mat4d& ProjectionStereoRight() const;
484 //! @return stereographic matrix of Standard_ShortReal precision computed for right eye.
485 //! Please note that this method is used for rendering for <i>Projection_Stereo</i>.
486 Standard_EXPORT const Graphic3d_Mat4& ProjectionStereoRightF() const;
488 //! @name Managing projection and orientation cache
491 //! Compute projection matrices.
492 //! @param theMatrices [in] the matrices data container.
493 template <typename Elem_t>
495 TransformMatrices<Elem_t>& UpdateProjection (TransformMatrices<Elem_t>& theMatrices) const;
497 //! Compute orientation matrix.
498 //! @param theMatrices [in] the matrices data container.
499 template <typename Elem_t>
501 TransformMatrices<Elem_t>& UpdateOrientation (TransformMatrices<Elem_t>& theMatrices) const;
503 //! Invalidate state of projection matrix.
504 //! The matrix will be updated on request.
505 void InvalidateProjection();
507 //! Invalidate orientation matrix.
508 //! The matrix will be updated on request.
509 void InvalidateOrientation();
513 //! Compose orthographic projection matrix for
514 //! the passed camera volume mapping.
515 //! @param theLeft [in] the left mapping (clipping) coordinate.
516 //! @param theRight [in] the right mapping (clipping) coordinate.
517 //! @param theBottom [in] the bottom mapping (clipping) coordinate.
518 //! @param theTop [in] the top mapping (clipping) coordinate.
519 //! @param theNear [in] the near mapping (clipping) coordinate.
520 //! @param theFar [in] the far mapping (clipping) coordinate.
521 //! @param theOutMx [out] the projection matrix.
522 template <typename Elem_t>
524 OrthoProj (const Elem_t theLeft,
525 const Elem_t theRight,
526 const Elem_t theBottom,
528 const Elem_t theNear,
530 NCollection_Mat4<Elem_t>& theOutMx);
532 //! Compose perspective projection matrix for
533 //! the passed camera volume mapping.
534 //! @param theLeft [in] the left mapping (clipping) coordinate.
535 //! @param theRight [in] the right mapping (clipping) coordinate.
536 //! @param theBottom [in] the bottom mapping (clipping) coordinate.
537 //! @param theTop [in] the top mapping (clipping) coordinate.
538 //! @param theNear [in] the near mapping (clipping) coordinate.
539 //! @param theFar [in] the far mapping (clipping) coordinate.
540 //! @param theOutMx [out] the projection matrix.
541 template <typename Elem_t>
543 PerspectiveProj (const Elem_t theLeft,
544 const Elem_t theRight,
545 const Elem_t theBottom,
547 const Elem_t theNear,
549 NCollection_Mat4<Elem_t>& theOutMx);
551 //! Compose projection matrix for L/R stereo eyes.
552 //! @param theLeft [in] the left mapping (clipping) coordinate.
553 //! @param theRight [in] the right mapping (clipping) coordinate.
554 //! @param theBottom [in] the bottom mapping (clipping) coordinate.
555 //! @param theTop [in] the top mapping (clipping) coordinate.
556 //! @param theNear [in] the near mapping (clipping) coordinate.
557 //! @param theFar [in] the far mapping (clipping) coordinate.
558 //! @param theIOD [in] the Intraocular distance.
559 //! @param theZFocus [in] the z coordinate of off-axis
560 //! projection plane with zero parallax.
561 //! @param theIsLeft [in] boolean flag to choose between L/R eyes.
562 //! @param theOutMx [out] the projection matrix.
563 template <typename Elem_t>
565 StereoEyeProj (const Elem_t theLeft,
566 const Elem_t theRight,
567 const Elem_t theBottom,
569 const Elem_t theNear,
572 const Elem_t theZFocus,
573 const Standard_Boolean theIsLeft,
574 NCollection_Mat4<Elem_t>& theOutMx);
576 //! Construct "look at" orientation transformation.
577 //! Reference point differs for perspective and ortho modes
578 //! (made for compatibility, to be improved..).
579 //! @param theEye [in] the eye coordinates in 3D space.
580 //! @param theLookAt [in] the point the camera looks at.
581 //! @param theUpDir [in] the up direction vector.
582 //! @param theAxialScale [in] the axial scale vector.
583 //! @param theOutMx [in/out] the orientation matrix.
584 template <typename Elem_t>
586 LookOrientation (const NCollection_Vec3<Elem_t>& theEye,
587 const NCollection_Vec3<Elem_t>& theLookAt,
588 const NCollection_Vec3<Elem_t>& theUpDir,
589 const NCollection_Vec3<Elem_t>& theAxialScale,
590 NCollection_Mat4<Elem_t>& theOutMx);
594 gp_Dir myUp; //!< Camera up direction vector.
595 gp_Pnt myEye; //!< Camera eye position.
596 gp_Pnt myCenter; //!< Camera center.
598 gp_XYZ myAxialScale; //!< World axial scale.
600 Projection myProjType; //!< Projection type used for rendering.
601 Standard_Real myFOVy; //!< Field Of View in y axis.
602 Standard_Real myZNear; //!< Distance to near clipping plane.
603 Standard_Real myZFar; //!< Distance to far clipping plane.
604 Standard_Real myAspect; //!< Width to height display ratio.
606 Standard_Real myScale; //!< Specifies parallel scale for orthographic projection.
607 Standard_Real myZFocus; //!< Stereographic focus value.
608 FocusType myZFocusType; //!< Stereographic focus definition type.
610 Standard_Real myIOD; //!< Intraocular distance value.
611 IODType myIODType; //!< Intraocular distance definition type.
613 mutable TransformMatrices<Standard_Real> myMatricesD;
614 mutable TransformMatrices<Standard_ShortReal> myMatricesF;
616 mutable Standard_Size myProjectionState;
617 mutable Standard_Size myOrientationState;
621 DEFINE_STANDARD_RTTI(Graphic3d_Camera);