0031354: Visualization - Dump improvement for V3d, Graphic3d, Aspect
[occt.git] / src / Graphic3d / Graphic3d_Camera.hxx
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 //
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.
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
19 #include <Aspect_Eye.hxx>
20 #include <Aspect_FrustumLRBT.hxx>
21 #include <Graphic3d_CameraTile.hxx>
22 #include <Graphic3d_Mat4d.hxx>
23 #include <Graphic3d_Mat4.hxx>
24 #include <Graphic3d_Vec3.hxx>
25 #include <Graphic3d_WorldViewProjState.hxx>
26 #include <NCollection_Lerp.hxx>
27 #include <NCollection_Array1.hxx>
28
29 #include <gp_Dir.hxx>
30 #include <gp_Pnt.hxx>
31
32 #include <Standard_Macro.hxx>
33 #include <Standard_TypeDef.hxx>
34
35 #include <Bnd_Box.hxx>
36
37 //! Forward declaration
38 class Graphic3d_WorldViewProjState;
39
40 //! Camera class provides object-oriented approach to setting up projection
41 //! and orientation properties of 3D view.
42 class Graphic3d_Camera : public Standard_Transient
43 {
44 private:
45
46   //! Template container for cached matrices or Real/ShortReal types.
47   template<typename Elem_t>
48   struct TransformMatrices
49   {
50
51     //! Default constructor.
52     TransformMatrices() : myIsOrientationValid (Standard_False), myIsProjectionValid (Standard_False) {}
53
54     //! Initialize orientation.
55     void InitOrientation()
56     {
57       myIsOrientationValid = Standard_True;
58       Orientation.InitIdentity();
59     }
60
61     //! Initialize projection.
62     void InitProjection()
63     {
64       myIsProjectionValid = Standard_True;
65       MProjection.InitIdentity();
66       LProjection.InitIdentity();
67       RProjection.InitIdentity();
68     }
69
70     //! Invalidate orientation.
71     void ResetOrientation() { myIsOrientationValid = Standard_False; }
72
73     //! Invalidate projection.
74     void ResetProjection()  { myIsProjectionValid  = Standard_False; }
75
76     //! Return true if Orientation was not invalidated.
77     Standard_Boolean IsOrientationValid() const { return myIsOrientationValid; }
78
79     //! Return true if Projection was not invalidated.
80     Standard_Boolean IsProjectionValid()  const { return myIsProjectionValid;  }
81     
82     //! Dumps the content of me into the stream
83     void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const
84     {
85       if (IsOrientationValid())
86       {
87         OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &Orientation)
88       }
89       if (IsProjectionValid())
90       {
91         OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &MProjection)
92         OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &LProjection)
93         OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &RProjection)
94       }
95       OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myIsOrientationValid)
96       OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myIsProjectionValid)
97     }
98
99   public:
100
101     NCollection_Mat4<Elem_t> Orientation;
102     NCollection_Mat4<Elem_t> MProjection;
103     NCollection_Mat4<Elem_t> LProjection;
104     NCollection_Mat4<Elem_t> RProjection;
105
106   private:
107
108     Standard_Boolean myIsOrientationValid;
109     Standard_Boolean myIsProjectionValid;
110
111   };
112
113 public:
114
115   //! Enumerates supported monographic projections.
116   //! - Projection_Orthographic : orthographic projection.
117   //! - Projection_Perspective  : perspective projection.
118   //! - Projection_Stereo       : stereographic projection.
119   //! - Projection_MonoLeftEye  : mono projection for stereo left eye.
120   //! - Projection_MonoRightEye : mono projection for stereo right eye.
121   enum Projection
122   {
123     Projection_Orthographic,
124     Projection_Perspective,
125     Projection_Stereo,
126     Projection_MonoLeftEye,
127     Projection_MonoRightEye
128   };
129
130   //! Enumerates approaches to define stereographic focus.
131   //! - FocusType_Absolute : focus is specified as absolute value.
132   //! - FocusType_Relative : focus is specified relative to
133   //! (as coefficient of) camera focal length.
134   enum FocusType
135   {
136     FocusType_Absolute,
137     FocusType_Relative
138   };
139
140   //! Enumerates approaches to define Intraocular distance.
141   //! - IODType_Absolute : Intraocular distance is defined as absolute value.
142   //! - IODType_Relative : Intraocular distance is defined relative to
143   //! (as coefficient of) camera focal length.
144   enum IODType
145   {
146     IODType_Absolute,
147     IODType_Relative
148   };
149
150 public:
151
152   //! Default constructor.
153   //! Initializes camera with the following properties:
154   //! Eye (0, 0, -2); Center (0, 0, 0); Up (0, 1, 0);
155   //! Type (Orthographic); FOVy (45); Scale (1000); IsStereo(false);
156   //! ZNear (0.001); ZFar (3000.0); Aspect(1);
157   //! ZFocus(1.0); ZFocusType(Relative); IOD(0.05); IODType(Relative)
158   Standard_EXPORT Graphic3d_Camera();
159
160   //! Copy constructor.
161   //! @param theOther [in] the camera to copy from.
162   Standard_EXPORT Graphic3d_Camera (const Handle(Graphic3d_Camera)& theOther);
163
164   //! Initialize mapping related parameters from other camera handle.
165   Standard_EXPORT void CopyMappingData (const Handle(Graphic3d_Camera)& theOtherCamera);
166
167   //! Initialize orientation related parameters from other camera handle.
168   Standard_EXPORT void CopyOrientationData (const Handle(Graphic3d_Camera)& theOtherCamera);
169
170   //! Copy properties of another camera.
171   //! @param theOther [in] the camera to copy from.
172   Standard_EXPORT void Copy (const Handle(Graphic3d_Camera)& theOther);
173
174 //! @name Public camera properties
175 public:
176
177   //! Get camera look direction.
178   //! @return camera look direction.
179   const gp_Dir& Direction() const { return myDirection; }
180
181   //! Sets camera look direction preserving the current Eye() position.
182   //! WARNING! This method does NOT verify that the current Up() vector is orthogonal to the new Direction.
183   //! @param theDir [in] the direction.
184   Standard_EXPORT void SetDirectionFromEye (const gp_Dir& theDir);
185
186   //! Sets camera look direction and computes the new Eye position relative to current Center.
187   //! WARNING! This method does NOT verify that the current Up() vector is orthogonal to the new Direction.
188   //! @param theDir [in] the direction.
189   Standard_EXPORT void SetDirection (const gp_Dir& theDir);
190
191   //! Get camera Up direction vector.
192   //! @return Camera's Up direction vector.
193   const gp_Dir& Up() const { return myUp; }
194
195   //! Sets camera Up direction vector, orthogonal to camera direction.
196   //! WARNING! This method does NOT verify that the new Up vector is orthogonal to the current Direction().
197   //! @param theUp [in] the Up direction vector.
198   //! @sa OrthogonalizeUp().
199   Standard_EXPORT void SetUp (const gp_Dir& theUp);
200
201   //! Orthogonalize up direction vector.
202   Standard_EXPORT void OrthogonalizeUp();
203
204   //! Return a copy of orthogonalized up direction vector.
205   Standard_EXPORT gp_Dir OrthogonalizedUp() const;
206
207   //! Right side direction.
208   gp_Dir SideRight() const
209   {
210     return -(gp_Vec (Direction()) ^ gp_Vec (OrthogonalizedUp()));
211   }
212
213   //! Get camera Eye position.
214   //! @return camera eye location.
215   const gp_Pnt& Eye() const { return myEye; }
216
217   //! Sets camera Eye position.
218   //! Unlike SetEye(), this method only changes Eye point and preserves camera direction.
219   //! @param theEye [in] the location of camera's Eye.
220   //! @sa SetEye()
221   Standard_EXPORT void MoveEyeTo (const gp_Pnt& theEye);
222
223   //! Sets camera Eye and Center positions.
224   //! @param theEye    [in] the location of camera's Eye
225   //! @param theCenter [in] the location of camera's Center
226   Standard_EXPORT void SetEyeAndCenter (const gp_Pnt& theEye,
227                                         const gp_Pnt& theCenter);
228
229   //! Sets camera Eye position.
230   //! WARNING! For backward compatibility reasons, this method also changes view direction,
231   //! so that the new direction is computed from new Eye position to old Center position.
232   //! @param theEye [in] the location of camera's Eye.
233   //! @sa MoveEyeTo(), SetEyeAndCenter()
234   Standard_EXPORT void SetEye (const gp_Pnt& theEye);
235
236   //! Get Center of the camera, e.g. the point where camera looks at.
237   //! This point is computed as Eye() translated along Direction() at Distance().
238   //! @return the point where the camera looks at.
239   gp_Pnt Center() const
240   {
241     return myEye.XYZ() + myDirection.XYZ() * myDistance;
242   }
243
244   //! Sets Center of the camera, e.g. the point where camera looks at.
245   //! This methods changes camera direction, so that the new direction is computed
246   //! from current Eye position to specified Center position.
247   //! @param theCenter [in] the point where the camera looks at.
248   Standard_EXPORT void SetCenter (const gp_Pnt& theCenter);
249
250   //! Get distance of Eye from camera Center.
251   //! @return the distance.
252   Standard_Real Distance() const { return myDistance; }
253
254   //! Set distance of Eye from camera Center.
255   //! @param theDistance [in] the distance.
256   Standard_EXPORT void SetDistance (const Standard_Real theDistance);
257
258   //! Get camera scale.
259   //! @return camera scale factor.
260   Standard_EXPORT Standard_Real Scale() const;
261
262   //! Sets camera scale. For orthographic projection the scale factor
263   //! corresponds to parallel scale of view mapping  (i.e. size
264   //! of viewport). For perspective camera scale is converted to
265   //! distance. The scale specifies equal size of the view projection in
266   //! both dimensions assuming that the aspect is 1.0. The projection height
267   //! and width are specified with the scale and correspondingly multiplied
268   //! by the aspect.
269   //! @param theScale [in] the scale factor.
270   Standard_EXPORT void SetScale (const Standard_Real theScale);
271
272   //! Get camera axial scale.
273   //! @return Camera's axial scale.
274   const gp_XYZ& AxialScale() const { return myAxialScale; }
275
276   //! Set camera axial scale.
277   //! @param theAxialScale [in] the axial scale vector.
278   Standard_EXPORT void SetAxialScale (const gp_XYZ& theAxialScale);
279
280   //! Change camera projection type.
281   //! When switching to perspective projection from orthographic one,
282   //! the ZNear and ZFar are reset to default values (0.001, 3000.0)
283   //! if less than 0.0.
284   //! @param theProjectionType [in] the camera projection type.
285   Standard_EXPORT void SetProjectionType (const Projection theProjection);
286
287   //! @return camera projection type.
288   Projection ProjectionType() const
289   {
290     return myProjType;
291   }
292
293   //! Check that the camera projection is orthographic.
294   //! @return boolean flag that indicates whether the camera's projection is
295   //! orthographic or not.
296   Standard_Boolean IsOrthographic() const
297   {
298     return (myProjType == Projection_Orthographic);
299   }
300
301   //! Check whether the camera projection is stereo.
302   //! Please note that stereo rendering is now implemented with support of
303   //! Quad buffering.
304   //! @return boolean flag indicating whether the stereographic L/R projection
305   //! is chosen.
306   Standard_Boolean IsStereo() const
307   {
308     return (myProjType == Projection_Stereo);
309   }
310
311   //! Set Field Of View (FOV) in y axis for perspective projection.
312   //! Field of View in x axis is automatically scaled from view aspect ratio.
313   //! @param theFOVy [in] the FOV in degrees.
314   Standard_EXPORT void SetFOVy (const Standard_Real theFOVy);
315
316   //! Get Field Of View (FOV) in y axis.
317   //! @return the FOV value in degrees.
318   Standard_Real FOVy() const { return myFOVy; }
319
320   //! Get Field Of View (FOV) in x axis.
321   //! @return the FOV value in degrees.
322   Standard_Real FOVx() const { return myFOVx; }
323
324   //! Get Field Of View (FOV) restriction for 2D on-screen elements; 180 degrees by default.
325   //! When 2D FOV is smaller than FOVy or FOVx, 2D elements defined within offset from view corner
326   //! will be extended to fit into specified 2D FOV.
327   //! This can be useful to make 2D elements sharply visible, like in case of HMD normally having extra large FOVy.
328   Standard_Real FOV2d() const { return myFOV2d; }
329
330   //! Set Field Of View (FOV) restriction for 2D on-screen elements.
331   Standard_EXPORT void SetFOV2d (Standard_Real theFOV);
332
333   //! Estimate Z-min and Z-max planes of projection volume to match the
334   //! displayed objects. The methods ensures that view volume will
335   //! be close by depth range to the displayed objects. Fitting assumes that
336   //! for orthogonal projection the view volume contains the displayed objects
337   //! completely. For zoomed perspective view, the view volume is adjusted such
338   //! that it contains the objects or their parts, located in front of the camera.
339   //! @param theScaleFactor [in] the scale factor for Z-range.
340   //!   The range between Z-min, Z-max projection volume planes
341   //!   evaluated by z fitting method will be scaled using this coefficient.
342   //!   Program error exception is thrown if negative or zero value is passed.
343   //! @param theMinMax [in] applicative min max boundaries.
344   //! @param theScaleFactor [in] real graphical boundaries (not accounting infinite flag).
345   Standard_EXPORT bool ZFitAll (const Standard_Real theScaleFactor,
346                                 const Bnd_Box&      theMinMax,
347                                 const Bnd_Box&      theGraphicBB,
348                                 Standard_Real&      theZNear,
349                                 Standard_Real&      theZFar) const;
350
351   //! Change Z-min and Z-max planes of projection volume to match the displayed objects.
352   void ZFitAll (const Standard_Real theScaleFactor, const Bnd_Box& theMinMax, const Bnd_Box& theGraphicBB)
353   {
354     Standard_Real aZNear = 0.0, aZFar = 1.0;
355     ZFitAll (theScaleFactor, theMinMax, theGraphicBB, aZNear, aZFar);
356     SetZRange (aZNear, aZFar);
357   }
358
359   //! Change the Near and Far Z-clipping plane positions.
360   //! For orthographic projection, theZNear, theZFar can be negative or positive.
361   //! For perspective projection, only positive values are allowed.
362   //! Program error exception is raised if non-positive values are
363   //! specified for perspective projection or theZNear >= theZFar.
364   //! @param theZNear [in] the distance of the plane from the Eye.
365   //! @param theZFar [in] the distance of the plane from the Eye.
366   Standard_EXPORT void SetZRange (const Standard_Real theZNear, const Standard_Real theZFar);
367
368   //! Get the Near Z-clipping plane position.
369   //! @return the distance of the plane from the Eye.
370   Standard_Real ZNear() const
371   {
372     return myZNear;
373   }
374
375   //! Get the Far Z-clipping plane position.
376   //! @return the distance of the plane from the Eye.
377   Standard_Real ZFar() const
378   {
379     return myZFar;
380   }
381
382   //! Changes width / height display ratio.
383   //! @param theAspect [in] the display ratio.
384   Standard_EXPORT void SetAspect (const Standard_Real theAspect);
385
386   //! Get camera display ratio.
387   //! @return display ratio.
388   Standard_Real Aspect() const
389   {
390     return myAspect;
391   }
392
393   //! Sets stereographic focus distance.
394   //! @param theType [in] the focus definition type. Focus can be defined
395   //! as absolute value or relatively to (as coefficient of) coefficient of
396   //! camera focal length.
397   //! @param theZFocus [in] the focus absolute value or coefficient depending
398   //! on the passed definition type.
399   Standard_EXPORT void SetZFocus (const FocusType theType, const Standard_Real theZFocus);
400
401   //! Get stereographic focus value.
402   //! @return absolute or relative stereographic focus value
403   //! depending on its definition type.
404   Standard_Real ZFocus() const
405   {
406     return myZFocus;
407   }
408
409   //! Get stereographic focus definition type.
410   //! @return definition type used for stereographic focus.
411   FocusType ZFocusType() const
412   {
413     return myZFocusType;
414   }
415
416   //! Sets Intraocular distance.
417   //! @param theType [in] the IOD definition type. IOD can be defined as
418   //! absolute value or relatively to (as coefficient of) camera focal length.
419   //! @param theIOD [in] the Intraocular distance.
420   Standard_EXPORT void SetIOD (const IODType theType, const Standard_Real theIOD);
421
422   //! Get Intraocular distance value.
423   //! @return absolute or relative IOD value depending on its definition type.
424   Standard_Real IOD() const
425   {
426     return myIOD;
427   }
428
429   //! Get Intraocular distance definition type.
430   //! @return definition type used for Intraocular distance.
431   IODType GetIODType() const
432   {
433     return myIODType;
434   }
435
436   //! Get current tile.
437   const Graphic3d_CameraTile& Tile() const { return myTile; }
438
439   //! Sets the Tile defining the drawing sub-area within View.
440   //! Note that tile defining a region outside the view boundaries is also valid - use method Graphic3d_CameraTile::Cropped() to assign a cropped copy.
441   //! @param theTile tile definition
442   Standard_EXPORT void SetTile (const Graphic3d_CameraTile& theTile);
443
444 //! @name Basic camera operations
445 public:
446
447   //! Transform orientation components of the camera:
448   //! Eye, Up and Center points.
449   //! @param theTrsf [in] the transformation to apply.
450   Standard_EXPORT void Transform (const gp_Trsf& theTrsf);
451
452   //! Calculate view plane size at center (target) point
453   //! and distance between ZFar and ZNear planes.
454   //! @return values in form of gp_Pnt (Width, Height, Depth).
455   gp_XYZ ViewDimensions() const
456   {
457     return ViewDimensions (Distance());
458   }
459
460   //! Calculate view plane size at center point with specified Z offset
461   //! and distance between ZFar and ZNear planes.
462   //! @param theZValue [in] the distance from the eye in eye-to-center direction
463   //! @return values in form of gp_Pnt (Width, Height, Depth).
464   Standard_EXPORT gp_XYZ ViewDimensions (const Standard_Real theZValue) const;
465
466   //! Return offset to the view corner in NDC space within dimension X for 2d on-screen elements, which is normally 0.5.
467   //! Can be clamped when FOVx exceeds FOV2d.
468   Standard_Real NDC2dOffsetX() const
469   {
470     return myFOV2d >= myFOVx
471          ? 0.5
472          : 0.5 * myFOV2d / myFOVx;
473   }
474
475   //! Return offset to the view corner in NDC space within dimension X for 2d on-screen elements, which is normally 0.5.
476   //! Can be clamped when FOVy exceeds FOV2d.
477   Standard_Real NDC2dOffsetY() const
478   {
479     return myFOV2d >= myFOVy
480          ? 0.5
481          : 0.5 * myFOV2d / myFOVy;
482   }
483
484   //! Calculate WCS frustum planes for the camera projection volume.
485   //! Frustum is a convex volume determined by six planes directing
486   //! inwards.
487   //! The frustum planes are usually used as inputs for camera algorithms.
488   //! Thus, if any changes to projection matrix calculation are necessary,
489   //! the frustum planes calculation should be also touched.
490   //! @param theLeft [out] the frustum plane for left side of view.
491   //! @param theRight [out] the frustum plane for right side of view.
492   //! @param theBottom [out] the frustum plane for bottom side of view.
493   //! @param theTop [out] the frustum plane for top side of view.
494   //! @param theNear [out] the frustum plane for near side of view.
495   //! @param theFar [out] the frustum plane for far side of view.
496   Standard_EXPORT void Frustum (gp_Pln& theLeft,
497                                 gp_Pln& theRight,
498                                 gp_Pln& theBottom,
499                                 gp_Pln& theTop,
500                                 gp_Pln& theNear,
501                                 gp_Pln& theFar) const;
502
503 //! @name Projection methods
504 public:
505
506   //! Project point from world coordinate space to
507   //! normalized device coordinates (mapping).
508   //! @param thePnt [in] the 3D point in WCS.
509   //! @return mapped point in NDC.
510   Standard_EXPORT gp_Pnt Project (const gp_Pnt& thePnt) const;
511
512   //! Unproject point from normalized device coordinates
513   //! to world coordinate space.
514   //! @param thePnt [in] the NDC point.
515   //! @return 3D point in WCS.
516   Standard_EXPORT gp_Pnt UnProject (const gp_Pnt& thePnt) const;
517
518   //! Convert point from view coordinate space to
519   //! projection coordinate space.
520   //! @param thePnt [in] the point in VCS.
521   //! @return point in NDC.
522   Standard_EXPORT gp_Pnt ConvertView2Proj (const gp_Pnt& thePnt) const;
523
524   //! Convert point from projection coordinate space
525   //! to view coordinate space.
526   //! @param thePnt [in] the point in NDC.
527   //! @return point in VCS.
528   Standard_EXPORT gp_Pnt ConvertProj2View (const gp_Pnt& thePnt) const;
529
530   //! Convert point from world coordinate space to
531   //! view coordinate space.
532   //! @param thePnt [in] the 3D point in WCS.
533   //! @return point in VCS.
534   Standard_EXPORT gp_Pnt ConvertWorld2View (const gp_Pnt& thePnt) const;
535
536   //! Convert point from view coordinate space to
537   //! world coordinates.
538   //! @param thePnt [in] the 3D point in VCS.
539   //! @return point in WCS.
540   Standard_EXPORT gp_Pnt ConvertView2World (const gp_Pnt& thePnt) const;
541
542 //! @name Camera modification state
543 public:
544
545   //! @return projection modification state of the camera.
546   const Graphic3d_WorldViewProjState& WorldViewProjState() const
547   {
548     return myWorldViewProjState;
549   }
550
551
552   //! Returns modification state of camera projection matrix
553   Standard_Size ProjectionState() const
554   {
555     return myWorldViewProjState.ProjectionState();
556   }
557
558   //! Returns modification state of camera world view transformation matrix.
559   Standard_Size WorldViewState() const
560   {
561     return myWorldViewProjState.WorldViewState();
562   }
563
564 //! @name Lazily-computed orientation and projection matrices derived from camera parameters
565 public:
566
567   //! Get orientation matrix.
568   //! @return camera orientation matrix.
569   Standard_EXPORT const Graphic3d_Mat4d& OrientationMatrix() const;
570
571   //! Get orientation matrix of Standard_ShortReal precision.
572   //! @return camera orientation matrix.
573   Standard_EXPORT const Graphic3d_Mat4& OrientationMatrixF() const;
574
575   //! Get monographic or middle point projection matrix used for monographic
576   //! rendering and for point projection / unprojection.
577   //! @return monographic projection matrix.
578   Standard_EXPORT const Graphic3d_Mat4d& ProjectionMatrix() const;
579
580   //! Get monographic or middle point projection matrix of Standard_ShortReal precision used for monographic
581   //! rendering and for point projection / unprojection.
582   //! @return monographic projection matrix.
583   Standard_EXPORT const Graphic3d_Mat4& ProjectionMatrixF() const;
584
585   //! @return stereographic matrix computed for left eye. Please note
586   //! that this method is used for rendering for <i>Projection_Stereo</i>.
587   Standard_EXPORT const Graphic3d_Mat4d& ProjectionStereoLeft() const;
588
589   //! @return stereographic matrix of Standard_ShortReal precision computed for left eye.
590   //! Please note that this method is used for rendering for <i>Projection_Stereo</i>.
591   Standard_EXPORT const Graphic3d_Mat4& ProjectionStereoLeftF() const;
592
593   //! @return stereographic matrix computed for right eye. Please note
594   //! that this method is used for rendering for <i>Projection_Stereo</i>.
595   Standard_EXPORT const Graphic3d_Mat4d& ProjectionStereoRight() const;
596
597   //! @return stereographic matrix of Standard_ShortReal precision computed for right eye.
598   //! Please note that this method is used for rendering for <i>Projection_Stereo</i>.
599   Standard_EXPORT const Graphic3d_Mat4& ProjectionStereoRightF() const;
600
601   //! Invalidate state of projection matrix.
602   //! The matrix will be updated on request.
603   Standard_EXPORT void InvalidateProjection();
604
605   //! Invalidate orientation matrix.
606   //! The matrix will be updated on request.
607   Standard_EXPORT void InvalidateOrientation();
608
609 public:
610
611   //! Get stereo projection matrices.
612   //! @param theProjL      [out] left  eye projection matrix
613   //! @param theHeadToEyeL [out] left  head to eye translation matrix
614   //! @param theProjR      [out] right eye projection matrix
615   //! @param theHeadToEyeR [out] right head to eye translation matrix
616   Standard_EXPORT void StereoProjection (Graphic3d_Mat4d& theProjL,
617                                          Graphic3d_Mat4d& theHeadToEyeL,
618                                          Graphic3d_Mat4d& theProjR,
619                                          Graphic3d_Mat4d& theHeadToEyeR) const;
620
621   //! Get stereo projection matrices.
622   //! @param theProjL      [out] left  eye projection matrix
623   //! @param theHeadToEyeL [out] left  head to eye translation matrix
624   //! @param theProjR      [out] right eye projection matrix
625   //! @param theHeadToEyeR [out] right head to eye translation matrix
626   Standard_EXPORT void StereoProjectionF (Graphic3d_Mat4& theProjL,
627                                           Graphic3d_Mat4& theHeadToEyeL,
628                                           Graphic3d_Mat4& theProjR,
629                                           Graphic3d_Mat4& theHeadToEyeR) const;
630
631   //! Unset all custom frustums and projection matrices.
632   Standard_EXPORT void ResetCustomProjection();
633
634   //! Return TRUE if custom stereo frustums are set.
635   bool IsCustomStereoFrustum() const { return myIsCustomFrustomLR; }
636
637   //! Set custom stereo frustums.
638   //! These can be retrieved from APIs like OpenVR.
639   Standard_EXPORT void SetCustomStereoFrustums (const Aspect_FrustumLRBT<Standard_Real>& theFrustumL,
640                                                 const Aspect_FrustumLRBT<Standard_Real>& theFrustumR);
641
642   //! Return TRUE if custom stereo projection matrices are set.
643   bool IsCustomStereoProjection() const { return myIsCustomProjMatLR; }
644
645   //! Set custom stereo projection matrices.
646   //! @param theProjL      [in] left  eye projection matrix
647   //! @param theHeadToEyeL [in] left  head to eye translation matrix
648   //! @param theProjR      [in] right eye projection matrix
649   //! @param theHeadToEyeR [in] right head to eye translation matrix
650   Standard_EXPORT void SetCustomStereoProjection (const Graphic3d_Mat4d& theProjL,
651                                                   const Graphic3d_Mat4d& theHeadToEyeL,
652                                                   const Graphic3d_Mat4d& theProjR,
653                                                   const Graphic3d_Mat4d& theHeadToEyeR);
654
655   //! Return TRUE if custom projection matrix is set.
656   bool IsCustomMonoProjection() const { return myIsCustomProjMatM; }
657
658   //! Set custom projection matrix.
659   Standard_EXPORT void SetCustomMonoProjection (const Graphic3d_Mat4d& theProj);
660
661   //! Dumps the content of me into the stream
662   Standard_EXPORT void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const;
663
664 //! @name Managing projection and orientation cache
665 private:
666
667   //! Get stereo projection matrices.
668   //! @param theProjL      [out] left  eye projection matrix
669   //! @param theHeadToEyeL [out] left  head to eye translation matrix
670   //! @param theProjR      [out] right eye projection matrix
671   //! @param theHeadToEyeR [out] right head to eye translation matrix
672   template <typename Elem_t>
673   Standard_EXPORT void stereoProjection (NCollection_Mat4<Elem_t>& theProjL,
674                                          NCollection_Mat4<Elem_t>& theHeadToEyeL,
675                                          NCollection_Mat4<Elem_t>& theProjR,
676                                          NCollection_Mat4<Elem_t>& theHeadToEyeR) const;
677
678   //! Compute projection matrices.
679   //! @param theProjM [out] mono projection matrix
680   //! @param theProjL [out] left  eye projection matrix
681   //! @param theProjR [out] right eye projection matrix
682   //! @param theToAddHeadToEye [in] flag to pre-multiply head-to-eye translation
683   template <typename Elem_t>
684   Standard_EXPORT void computeProjection (NCollection_Mat4<Elem_t>& theProjM,
685                                           NCollection_Mat4<Elem_t>& theProjL,
686                                           NCollection_Mat4<Elem_t>& theProjR,
687                                           bool theToAddHeadToEye) const;
688
689   //! Compute projection matrices.
690   //! @param theMatrices [in] the matrices data container.
691   template <typename Elem_t>
692   TransformMatrices<Elem_t>& UpdateProjection (TransformMatrices<Elem_t>& theMatrices) const
693   {
694     if (!theMatrices.IsProjectionValid())
695     {
696       theMatrices.InitProjection();
697       computeProjection (theMatrices.MProjection, theMatrices.LProjection, theMatrices.RProjection, true);
698     }
699     return theMatrices;
700   }
701
702   //! Compute orientation matrix.
703   //! @param theMatrices [in] the matrices data container.
704   template <typename Elem_t>
705   Standard_EXPORT
706     TransformMatrices<Elem_t>& UpdateOrientation (TransformMatrices<Elem_t>& theMatrices) const;
707
708 private:
709
710   //! Compose orthographic projection matrix for the passed camera volume mapping.
711   //! @param theOutMx [out] the projection matrix
712   //! @param theLRBT [in] the left/right/bottom/top mapping (clipping) coordinates
713   //! @param theNear [in] the near mapping (clipping) coordinate
714   //! @param theFar [in] the far mapping (clipping) coordinate
715   template <typename Elem_t>
716   static void orthoProj (NCollection_Mat4<Elem_t>& theOutMx,
717                          const Aspect_FrustumLRBT<Elem_t>& theLRBT,
718                          const Elem_t theNear,
719                          const Elem_t theFar);
720
721   //! Compose perspective projection matrix for the passed camera volume mapping.
722   //! @param theOutMx [out] the projection matrix
723   //! @param theLRBT [in] the left/right/bottom/top mapping (clipping) coordinates
724   //! @param theNear [in] the near mapping (clipping) coordinate
725   //! @param theFar [in] the far mapping (clipping) coordinate
726   template <typename Elem_t>
727   static void perspectiveProj (NCollection_Mat4<Elem_t>& theOutMx,
728                                const Aspect_FrustumLRBT<Elem_t>& theLRBT,
729                                const Elem_t theNear,
730                                const Elem_t theFar);
731
732   //! Compose projection matrix for L/R stereo eyes.
733   //! @param theOutMx [out] the projection matrix
734   //! @param theLRBT [in] the left/right/bottom/top mapping (clipping) coordinates
735   //! @param theNear [in] the near mapping (clipping) coordinate
736   //! @param theFar [in] the far mapping (clipping) coordinate
737   //! @param theIOD [in] the Intraocular distance
738   //! @param theZFocus [in] the z coordinate of off-axis projection plane with zero parallax
739   //! @param theEyeIndex [in] choose between L/R eyes
740   template <typename Elem_t>
741   static void stereoEyeProj (NCollection_Mat4<Elem_t>& theOutMx,
742                              const Aspect_FrustumLRBT<Elem_t>& theLRBT,
743                              const Elem_t theNear,
744                              const Elem_t theFar,
745                              const Elem_t theIOD,
746                              const Elem_t theZFocus,
747                              const Aspect_Eye theEyeIndex);
748
749   //! Construct "look at" orientation transformation.
750   //! Reference point differs for perspective and ortho modes 
751   //! (made for compatibility, to be improved..).
752   //! @param theEye [in] the eye coordinates in 3D space.
753   //! @param theFwdDir [in] view direction
754   //! @param theUpDir [in] the up direction vector.
755   //! @param theAxialScale [in] the axial scale vector.
756   //! @param theOutMx [in/out] the orientation matrix.
757   template <typename Elem_t>
758   static void
759     LookOrientation (const NCollection_Vec3<Elem_t>& theEye,
760                      const NCollection_Vec3<Elem_t>& theFwdDir,
761                      const NCollection_Vec3<Elem_t>& theUpDir,
762                      const NCollection_Vec3<Elem_t>& theAxialScale,
763                      NCollection_Mat4<Elem_t>&       theOutMx);
764
765 public:
766
767   //! Enumerates vertices of view volume.
768   enum
769   {
770     FrustumVert_LeftBottomNear,
771     FrustumVert_LeftBottomFar,
772     FrustumVert_LeftTopNear,
773     FrustumVert_LeftTopFar,
774     FrustumVert_RightBottomNear,
775     FrustumVert_RightBottomFar,
776     FrustumVert_RightTopNear,
777     FrustumVert_RightTopFar,
778     FrustumVerticesNB
779   };
780
781   //! Fill array of current view frustum corners.
782   //! The size of this array is equal to FrustumVerticesNB.
783   //! The order of vertices is as defined in FrustumVert_* enumeration.
784   Standard_EXPORT void FrustumPoints (NCollection_Array1<Graphic3d_Vec3d>& thePoints,
785                                       const Graphic3d_Mat4d& theModelWorld = Graphic3d_Mat4d()) const;
786
787 private:
788
789   gp_Dir        myUp;       //!< Camera up direction vector
790   gp_Dir        myDirection;//!< Camera view direction (from eye)
791   gp_Pnt        myEye;      //!< Camera eye position
792   Standard_Real myDistance; //!< distance from Eye to Center
793
794   gp_XYZ myAxialScale; //!< World axial scale.
795
796   Projection    myProjType; //!< Projection type used for rendering.
797   Standard_Real myFOVy;     //!< Field Of View in y axis.
798   Standard_Real myFOVx;     //!< Field Of View in x axis.
799   Standard_Real myFOV2d;    //!< Field Of View limit for 2d on-screen elements
800   Standard_Real myFOVyTan;  //!< Field Of View as Tan(DTR_HALF * myFOVy)
801   Standard_Real myZNear;    //!< Distance to near clipping plane.
802   Standard_Real myZFar;     //!< Distance to far clipping plane.
803   Standard_Real myAspect;   //!< Width to height display ratio.
804
805   Standard_Real myScale;      //!< Specifies parallel scale for orthographic projection.
806   Standard_Real myZFocus;     //!< Stereographic focus value.
807   FocusType     myZFocusType; //!< Stereographic focus definition type.
808
809   Standard_Real myIOD;     //!< Intraocular distance value.
810   IODType       myIODType; //!< Intraocular distance definition type.
811
812   Graphic3d_CameraTile myTile;//!< Tile defining sub-area for drawing
813
814   Graphic3d_Mat4d  myCustomProjMatM;
815   Graphic3d_Mat4d  myCustomProjMatL;
816   Graphic3d_Mat4d  myCustomProjMatR;
817   Graphic3d_Mat4d  myCustomHeadToEyeMatL;
818   Graphic3d_Mat4d  myCustomHeadToEyeMatR;
819   Aspect_FrustumLRBT<Standard_Real> myCustomFrustumL; //!< left  custom frustum
820   Aspect_FrustumLRBT<Standard_Real> myCustomFrustumR; //!< right custom frustum
821   Standard_Boolean myIsCustomProjMatM;  //!< flag indicating usage of custom projection matrix
822   Standard_Boolean myIsCustomProjMatLR; //!< flag indicating usage of custom stereo projection matrices
823   Standard_Boolean myIsCustomFrustomLR; //!< flag indicating usage of custom stereo frustums
824
825   mutable TransformMatrices<Standard_Real>      myMatricesD;
826   mutable TransformMatrices<Standard_ShortReal> myMatricesF;
827
828   mutable Graphic3d_WorldViewProjState myWorldViewProjState;
829
830 public:
831
832   DEFINE_STANDARD_RTTIEXT(Graphic3d_Camera,Standard_Transient)
833 };
834
835 DEFINE_STANDARD_HANDLE (Graphic3d_Camera, Standard_Transient)
836
837 //! Linear interpolation tool for camera orientation and position.
838 //! This tool interpolates camera parameters scale, eye, center, rotation (up and direction vectors) independently.
839 //!
840 //! Eye/Center interpolation is performed through defining an anchor point in-between Center and Eye.
841 //! The anchor position is defined as point near to the camera point which has smaller translation part.
842 //! The main idea is to keep the distance between Center and Eye
843 //! (which will change if Center and Eye translation will be interpolated independently).
844 //! E.g.:
845 //!  - When both Center and Eye are moved at the same vector -> both will be just translated by straight line
846 //!  - When Center is not moved -> camera Eye    will move around Center through arc
847 //!  - When Eye    is not moved -> camera Center will move around Eye    through arc
848 //!  - When both Center and Eye are move by different vectors -> transformation will be something in between,
849 //!    and will try interpolate linearly the distance between Center and Eye.
850 //!
851 //! This transformation might be not in line with user expectations.
852 //! In this case, application might define intermediate camera positions for interpolation
853 //! or implement own interpolation logic.
854 template<>
855 Standard_EXPORT void NCollection_Lerp<Handle(Graphic3d_Camera)>::Interpolate (const double theT,
856                                                                               Handle(Graphic3d_Camera)& theResult) const;
857 typedef NCollection_Lerp<Handle(Graphic3d_Camera)> Graphic3d_CameraLerp;
858
859 #endif