743b841f2aba3060cc53b88ca855e428c33d66a3
[occt.git] / src / Graphic3d / Graphic3d_TransformPers.hxx
1 // Created on: 2015-06-18
2 // Created by: Anton POLETAEV
3 // Copyright (c) 2015 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_TransformPers_HeaderFile
17 #define _Graphic3d_TransformPers_HeaderFile
18
19 #include <Aspect_TypeOfTriedronPosition.hxx>
20 #include <Bnd_Box.hxx>
21 #include <BVH_Box.hxx>
22 #include <Graphic3d_Camera.hxx>
23 #include <Graphic3d_TransformUtils.hxx>
24 #include <Graphic3d_TransModeFlags.hxx>
25 #include <Graphic3d_Vec.hxx>
26 #include <NCollection_Mat4.hxx>
27
28 DEFINE_STANDARD_HANDLE(Graphic3d_TransformPers, Standard_Transient)
29
30 //! Transformation Persistence definition.
31 //!
32 //! Transformation Persistence defines a mutable Local Coordinate system which depends on camera position,
33 //! so that visual appearance of the object becomes partially immutable while camera moves.
34 //! Object visually preserves particular property such as size, placement, rotation or their combination.
35 //!
36 //! Graphic3d_TMF_ZoomPers, Graphic3d_TMF_RotatePers and Graphic3d_TMF_ZoomRotatePers define Local Coordinate system
37 //! having origin in specified anchor point defined in World Coordinate system,
38 //! while Graphic3d_TMF_TriedronPers and Graphic3d_TMF_2d define origin as 2D offset from screen corner in pixels.
39 //!
40 //! Graphic3d_TMF_2d, Graphic3d_TMF_TriedronPers and Graphic3d_TMF_ZoomPers defines Local Coordinate system where length units are pixels.
41 //! Beware that Graphic3d_RenderingParams::ResolutionRatio() will be ignored!
42 //! For other Persistence flags, normal (world) length units will apply.
43 //!
44 //! WARNING: Graphic3d_TMF_None is not permitted for defining instance of this class - NULL handle should be used for this purpose!
45 class Graphic3d_TransformPers : public Standard_Transient
46 {
47   DEFINE_STANDARD_RTTIEXT(Graphic3d_TransformPers, Standard_Transient)
48 public:
49
50   //! Return true if specified mode is zoom/rotate transformation persistence.
51   static Standard_Boolean IsZoomOrRotate (Graphic3d_TransModeFlags theMode)
52   {
53     return (theMode & (Graphic3d_TMF_ZoomPers | Graphic3d_TMF_RotatePers)) != 0;
54   }
55
56   //! Return true if specified mode is 2d/trihedron transformation persistence.
57   static Standard_Boolean IsTrihedronOr2d (Graphic3d_TransModeFlags theMode)
58   {
59     return (theMode & (Graphic3d_TMF_TriedronPers | Graphic3d_TMF_2d)) != 0;
60   }
61
62 public:
63
64   //! Set transformation persistence.
65   Graphic3d_TransformPers (const Graphic3d_TransModeFlags theMode)
66   : myMode (theMode)
67   {
68     if (IsZoomOrRotate (theMode))
69     {
70       SetPersistence (theMode, gp_Pnt(0.0, 0.0, 0.0));
71     }
72     else if (IsTrihedronOr2d (theMode))
73     {
74       SetPersistence (theMode, Aspect_TOTP_LEFT_LOWER, Graphic3d_Vec2i (0, 0));
75     }
76     else if (theMode == Graphic3d_TMF_CameraPers)
77     {
78       myMode = theMode;
79     }
80     else
81     {
82       throw Standard_ProgramError("Graphic3d_TransformPers::SetPersistence(), wrong persistence mode.");
83     }
84   }
85
86   //! Set Zoom/Rotate transformation persistence with an anchor 3D point.
87   //! Anchor point defines the origin of Local Coordinate system within World Coordinate system.
88   //! Throws an exception if persistence mode is not Graphic3d_TMF_ZoomPers, Graphic3d_TMF_ZoomRotatePers or Graphic3d_TMF_RotatePers.
89   Graphic3d_TransformPers (const Graphic3d_TransModeFlags theMode,
90                            const gp_Pnt& thePnt)
91   : myMode (Graphic3d_TMF_None)
92   {
93     SetPersistence (theMode, thePnt);
94   }
95
96   //! Set 2d/trihedron transformation persistence with a corner and 2D offset.
97   //! 2D offset defines the origin of Local Coordinate system as projection of 2D point on screen plane into World Coordinate system.
98   //! Throws an exception if persistence mode is not Graphic3d_TMF_TriedronPers or Graphic3d_TMF_2d.
99   //! The offset is a positive displacement from the view corner in pixels.
100   Graphic3d_TransformPers (const Graphic3d_TransModeFlags theMode,
101                            const Aspect_TypeOfTriedronPosition theCorner,
102                            const Graphic3d_Vec2i& theOffset = Graphic3d_Vec2i (0, 0))
103   : myMode (Graphic3d_TMF_None)
104   {
105     SetPersistence (theMode, theCorner, theOffset);
106   }
107
108   //! Return true for Graphic3d_TMF_ZoomPers, Graphic3d_TMF_ZoomRotatePers or Graphic3d_TMF_RotatePers modes.
109   Standard_Boolean IsZoomOrRotate() const { return IsZoomOrRotate (myMode); }
110
111   //! Return true for Graphic3d_TMF_TriedronPers and Graphic3d_TMF_2d modes.
112   Standard_Boolean IsTrihedronOr2d() const { return IsTrihedronOr2d (myMode); }
113
114   //! Transformation persistence mode flags.
115   Graphic3d_TransModeFlags Mode() const { return myMode; }
116
117   //! Transformation persistence mode flags.
118   Graphic3d_TransModeFlags Flags() const { return myMode; }
119
120   //! Set Zoom/Rotate transformation persistence with an anchor 3D point.
121   //! Throws an exception if persistence mode is not Graphic3d_TMF_ZoomPers, Graphic3d_TMF_ZoomRotatePers or Graphic3d_TMF_RotatePers.
122   void SetPersistence (const Graphic3d_TransModeFlags theMode,
123                        const gp_Pnt& thePnt)
124   {
125     if (!IsZoomOrRotate (theMode))
126     {
127       throw Standard_ProgramError("Graphic3d_TransformPers::SetPersistence(), wrong persistence mode.");
128     }
129
130     myMode = theMode;
131     myParams.Params3d.PntX = thePnt.X();
132     myParams.Params3d.PntY = thePnt.Y();
133     myParams.Params3d.PntZ = thePnt.Z();
134   }
135
136   //! Set 2d/trihedron transformation persistence with a corner and 2D offset.
137   //! Throws an exception if persistence mode is not Graphic3d_TMF_TriedronPers or Graphic3d_TMF_2d.
138   void SetPersistence (const Graphic3d_TransModeFlags theMode,
139                        const Aspect_TypeOfTriedronPosition theCorner,
140                        const Graphic3d_Vec2i& theOffset)
141   {
142     if (!IsTrihedronOr2d (theMode))
143     {
144       throw Standard_ProgramError("Graphic3d_TransformPers::SetPersistence(), wrong persistence mode.");
145     }
146
147     myMode = theMode;
148     myParams.Params2d.Corner  = theCorner;
149     myParams.Params2d.OffsetX = theOffset.x();
150     myParams.Params2d.OffsetY = theOffset.y();
151   }
152
153 public:
154
155   //! Return the anchor point for zoom/rotate transformation persistence.
156   gp_Pnt AnchorPoint() const
157   {
158     if (!IsZoomOrRotate())
159     {
160       throw Standard_ProgramError("Graphic3d_TransformPers::AnchorPoint(), wrong persistence mode.");
161     }
162
163     return gp_Pnt (myParams.Params3d.PntX, myParams.Params3d.PntY, myParams.Params3d.PntZ);
164   }
165
166   //! Set the anchor point for zoom/rotate transformation persistence.
167   void SetAnchorPoint (const gp_Pnt& thePnt)
168   {
169     if (!IsZoomOrRotate())
170     {
171       throw Standard_ProgramError("Graphic3d_TransformPers::SetAnchorPoint(), wrong persistence mode.");
172     }
173
174     myParams.Params3d.PntX = thePnt.X();
175     myParams.Params3d.PntY = thePnt.Y();
176     myParams.Params3d.PntZ = thePnt.Z();
177   }
178
179   //! Return the corner for 2d/trihedron transformation persistence.
180   Aspect_TypeOfTriedronPosition Corner2d() const
181   {
182     if (!IsTrihedronOr2d())
183     {
184       throw Standard_ProgramError("Graphic3d_TransformPers::Corner2d(), wrong persistence mode.");
185     }
186
187     return myParams.Params2d.Corner;
188   }
189
190   //! Set the corner for 2d/trihedron transformation persistence.
191   void SetCorner2d (const Aspect_TypeOfTriedronPosition thePos)
192   {
193     if (!IsTrihedronOr2d())
194     {
195       throw Standard_ProgramError("Graphic3d_TransformPers::SetCorner2d(), wrong persistence mode.");
196     }
197
198     myParams.Params2d.Corner = thePos;
199   }
200
201   //! Return the offset from the corner for 2d/trihedron transformation persistence.
202   Graphic3d_Vec2i Offset2d() const
203   {
204     if (!IsTrihedronOr2d())
205     {
206       throw Standard_ProgramError("Graphic3d_TransformPers::Offset2d(), wrong persistence mode.");
207     }
208
209     return Graphic3d_Vec2i (myParams.Params2d.OffsetX, myParams.Params2d.OffsetY);
210   }
211
212   //! Set the offset from the corner for 2d/trihedron transformation persistence.
213   void SetOffset2d (const Graphic3d_Vec2i& theOffset)
214   {
215     if (!IsTrihedronOr2d())
216     {
217       throw Standard_ProgramError("Graphic3d_TransformPers::SetOffset2d(), wrong persistence mode.");
218     }
219
220     myParams.Params2d.OffsetX = theOffset.x();
221     myParams.Params2d.OffsetY = theOffset.y();
222   }
223
224 public:
225
226   //! Apply transformation to bounding box of presentation.
227   //! @param theCamera [in] camera definition
228   //! @param theProjection [in] the projection transformation matrix.
229   //! @param theWorldView [in] the world view transformation matrix.
230   //! @param theViewportWidth [in] the width of viewport (for 2d persistence).
231   //! @param theViewportHeight [in] the height of viewport (for 2d persistence).
232   //! @param theBoundingBox [in/out] the bounding box to transform.
233   template<class T>
234   void Apply (const Handle(Graphic3d_Camera)& theCamera,
235               const NCollection_Mat4<T>& theProjection,
236               const NCollection_Mat4<T>& theWorldView,
237               const Standard_Integer theViewportWidth,
238               const Standard_Integer theViewportHeight,
239               Bnd_Box& theBoundingBox) const;
240
241   //! Apply transformation to bounding box of presentation
242   //! @param theCamera [in] camera definition
243   //! @param theProjection [in] the projection transformation matrix.
244   //! @param theWorldView [in] the world view transformation matrix.
245   //! @param theViewportWidth [in] the width of viewport (for 2d persistence).
246   //! @param theViewportHeight [in] the height of viewport (for 2d persistence).
247   //! @param theBoundingBox [in/out] the bounding box to transform.
248   template<class T>
249   void Apply (const Handle(Graphic3d_Camera)& theCamera,
250               const NCollection_Mat4<T>& theProjection,
251               const NCollection_Mat4<T>& theWorldView,
252               const Standard_Integer theViewportWidth,
253               const Standard_Integer theViewportHeight,
254               BVH_Box<T, 3>& theBoundingBox) const;
255
256   //! Compute transformation.
257   //! Computed matrix can be applied to model world transformation
258   //! of an object to implement effect of transformation persistence.
259   //! @param theCamera [in] camera definition
260   //! @param theProjection [in] the projection transformation matrix.
261   //! @param theWorldView [in] the world view transformation matrix.
262   //! @param theViewportWidth [in] the width of viewport (for 2d persistence).
263   //! @param theViewportHeight [in] the height of viewport (for 2d persistence).
264   //! @return transformation matrix to be applied to model world transformation of an object.
265   template<class T>
266   NCollection_Mat4<T> Compute (const Handle(Graphic3d_Camera)& theCamera,
267                                const NCollection_Mat4<T>& theProjection,
268                                const NCollection_Mat4<T>& theWorldView,
269                                const Standard_Integer theViewportWidth,
270                                const Standard_Integer theViewportHeight) const;
271
272   //! Apply transformation persistence on specified matrices.
273   //! @param theCamera camera definition
274   //! @param theProjection projection matrix to modify
275   //! @param theWorldView  world-view matrix to modify
276   //! @param theViewportWidth  viewport width
277   //! @param theViewportHeight viewport height
278   template<class T>
279   void Apply (const Handle(Graphic3d_Camera)& theCamera,
280               const NCollection_Mat4<T>& theProjection,
281               NCollection_Mat4<T>& theWorldView,
282               const Standard_Integer theViewportWidth,
283               const Standard_Integer theViewportHeight) const;
284
285   //! Dumps the content of me into the stream
286   Standard_EXPORT virtual void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const;
287
288 private:
289
290   //! 3D anchor point for zoom/rotate transformation persistence.
291   struct PersParams3d
292   {
293     Standard_Real PntX;
294     Standard_Real PntY;
295     Standard_Real PntZ;
296
297     //! Dumps the content of me into the stream
298     Standard_EXPORT void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const;
299   };
300
301   //! 2d/trihedron transformation persistence parameters.
302   struct PersParams2d
303   {
304     Standard_Integer OffsetX;
305     Standard_Integer OffsetY;
306     Aspect_TypeOfTriedronPosition Corner;
307
308     //! Dumps the content of me into the stream
309     Standard_EXPORT void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const;
310   };
311
312 private:
313
314   Graphic3d_TransModeFlags myMode;  //!< Transformation persistence mode flags
315   union
316   {
317     PersParams3d Params3d;
318     PersParams2d Params2d;
319   } myParams;
320
321 };
322
323 // =======================================================================
324 // function : Apply
325 // purpose  : Apply transformation to world view and projection matrices.
326 // =======================================================================
327 template<class T>
328 void Graphic3d_TransformPers::Apply (const Handle(Graphic3d_Camera)& theCamera,
329                                      const NCollection_Mat4<T>& theProjection,
330                                      NCollection_Mat4<T>& theWorldView,
331                                      const Standard_Integer theViewportWidth,
332                                      const Standard_Integer theViewportHeight) const
333 {
334   (void )theViewportWidth;
335   (void )theProjection;
336   if (myMode == Graphic3d_TMF_None
337    || theViewportHeight == 0)
338   {
339     return;
340   }
341
342   // use total size when tiling is active
343   const Standard_Integer aVPSizeY = theCamera->Tile().IsValid() ? theCamera->Tile().TotalSize.y() : theViewportHeight;
344
345   // a small enough jitter compensation offset
346   // to avoid image dragging within single pixel in corner cases
347   const Standard_Real aJitterComp = 0.001;
348   if (myMode == Graphic3d_TMF_TriedronPers)
349   {
350     // reset Z focus for trihedron persistence
351     const Standard_Real aFocus = theCamera->IsOrthographic()
352                                ? theCamera->Distance()
353                                : (theCamera->ZFocusType() == Graphic3d_Camera::FocusType_Relative
354                                 ? Standard_Real(theCamera->ZFocus() * theCamera->Distance())
355                                 : Standard_Real(theCamera->ZFocus()));
356
357     // scale factor to pixels
358     const gp_XYZ aViewDim = theCamera->ViewDimensions (aFocus);
359     const Standard_Real aScale = Abs(aViewDim.Y()) / Standard_Real(aVPSizeY);
360     const gp_Dir aForward = theCamera->Direction();
361     gp_XYZ aCenter = theCamera->Center().XYZ() + aForward.XYZ() * (aFocus - theCamera->Distance());
362     if ((myParams.Params2d.Corner & (Aspect_TOTP_LEFT | Aspect_TOTP_RIGHT)) != 0)
363     {
364       const Standard_Real anOffsetX = (Standard_Real(myParams.Params2d.OffsetX) + aJitterComp) * aScale;
365       const gp_Dir aSide   = aForward.Crossed (theCamera->Up());
366       const gp_XYZ aDeltaX = aSide.XYZ() * (Abs(aViewDim.X()) * theCamera->NDC2dOffsetX() - anOffsetX);
367       if ((myParams.Params2d.Corner & Aspect_TOTP_RIGHT) != 0)
368       {
369         aCenter += aDeltaX;
370       }
371       else
372       {
373         aCenter -= aDeltaX;
374       }
375     }
376     if ((myParams.Params2d.Corner & (Aspect_TOTP_TOP | Aspect_TOTP_BOTTOM)) != 0)
377     {
378       const Standard_Real anOffsetY = (Standard_Real(myParams.Params2d.OffsetY) + aJitterComp) * aScale;
379       const gp_XYZ aDeltaY = theCamera->Up().XYZ() * (Abs(aViewDim.Y()) * theCamera->NDC2dOffsetY() - anOffsetY);
380       if ((myParams.Params2d.Corner & Aspect_TOTP_TOP) != 0)
381       {
382         aCenter += aDeltaY;
383       }
384       else
385       {
386         aCenter -= aDeltaY;
387       }
388     }
389
390     NCollection_Mat4<Standard_Real> aWorldView = theCamera->OrientationMatrix();
391     Graphic3d_TransformUtils::Translate (aWorldView, aCenter.X(), aCenter.Y(), aCenter.Z());
392     Graphic3d_TransformUtils::Scale     (aWorldView, aScale,      aScale,      aScale);
393     theWorldView.ConvertFrom (aWorldView);
394     return;
395   }
396   else if (myMode == Graphic3d_TMF_2d)
397   {
398     const Standard_Real aFocus = theCamera->IsOrthographic()
399                                ? theCamera->Distance()
400                                : (theCamera->ZFocusType() == Graphic3d_Camera::FocusType_Relative
401                                 ? Standard_Real(theCamera->ZFocus() * theCamera->Distance())
402                                 : Standard_Real(theCamera->ZFocus()));
403
404     // scale factor to pixels
405     const gp_XYZ        aViewDim = theCamera->ViewDimensions (aFocus);
406     const Standard_Real aScale   = Abs(aViewDim.Y()) / Standard_Real(aVPSizeY);
407     gp_XYZ aCenter (0.0, 0.0, -aFocus);
408     if ((myParams.Params2d.Corner & (Aspect_TOTP_LEFT | Aspect_TOTP_RIGHT)) != 0)
409     {
410       aCenter.SetX (-aViewDim.X() * theCamera->NDC2dOffsetX() + (Standard_Real(myParams.Params2d.OffsetX) + aJitterComp) * aScale);
411       if ((myParams.Params2d.Corner & Aspect_TOTP_RIGHT) != 0)
412       {
413         aCenter.SetX (-aCenter.X());
414       }
415     }
416     if ((myParams.Params2d.Corner & (Aspect_TOTP_TOP | Aspect_TOTP_BOTTOM)) != 0)
417     {
418       aCenter.SetY (-aViewDim.Y() * theCamera->NDC2dOffsetY() + (Standard_Real(myParams.Params2d.OffsetY) + aJitterComp) * aScale);
419       if ((myParams.Params2d.Corner & Aspect_TOTP_TOP) != 0)
420       {
421         aCenter.SetY (-aCenter.Y());
422       }
423     }
424
425     theWorldView.InitIdentity();
426     Graphic3d_TransformUtils::Translate (theWorldView, T(aCenter.X()), T(aCenter.Y()), T(aCenter.Z()));
427     Graphic3d_TransformUtils::Scale     (theWorldView, T(aScale),      T(aScale),      T(aScale));
428     return;
429   }
430   else if ((myMode & Graphic3d_TMF_CameraPers) != 0)
431   {
432     theWorldView.InitIdentity();
433   }
434   else
435   {
436     // Compute reference point for transformation in untransformed projection space.
437     NCollection_Mat4<Standard_Real> aWorldView = theCamera->OrientationMatrix();
438     Graphic3d_TransformUtils::Translate (aWorldView, myParams.Params3d.PntX, myParams.Params3d.PntY, myParams.Params3d.PntZ);
439     if ((myMode & Graphic3d_TMF_RotatePers) != 0)
440     {
441       // lock rotation by nullifying rotation component
442       aWorldView.SetValue (0, 0, 1.0);
443       aWorldView.SetValue (1, 0, 0.0);
444       aWorldView.SetValue (2, 0, 0.0);
445
446       aWorldView.SetValue (0, 1, 0.0);
447       aWorldView.SetValue (1, 1, 1.0);
448       aWorldView.SetValue (2, 1, 0.0);
449
450       aWorldView.SetValue (0, 2, 0.0);
451       aWorldView.SetValue (1, 2, 0.0);
452       aWorldView.SetValue (2, 2, 1.0);
453     }
454
455     if ((myMode & Graphic3d_TMF_ZoomPers) != 0)
456     {
457       // lock zooming
458       gp_Vec aVecToEye (theCamera->Direction());
459       gp_Vec aVecToObj (theCamera->Eye(), gp_Pnt (myParams.Params3d.PntX, myParams.Params3d.PntY, myParams.Params3d.PntZ));
460       const Standard_Real aFocus = aVecToObj.Dot (aVecToEye);
461       const gp_XYZ aViewDim = theCamera->ViewDimensions (aFocus);
462       const Standard_Real aScale = Abs(aViewDim.Y()) / Standard_Real(aVPSizeY);
463       Graphic3d_TransformUtils::Scale (aWorldView, aScale, aScale, aScale);
464     }
465     theWorldView.ConvertFrom (aWorldView);
466     return;
467   }
468 }
469
470 // =======================================================================
471 // function : Apply
472 // purpose  : Apply transformation to bounding box of presentation.
473 // =======================================================================
474 template<class T>
475 void Graphic3d_TransformPers::Apply (const Handle(Graphic3d_Camera)& theCamera,
476                                      const NCollection_Mat4<T>& theProjection,
477                                      const NCollection_Mat4<T>& theWorldView,
478                                      const Standard_Integer theViewportWidth,
479                                      const Standard_Integer theViewportHeight,
480                                      Bnd_Box& theBoundingBox) const
481 {
482   if (theBoundingBox.IsVoid())
483   {
484     return;
485   }
486
487   T aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
488
489   theBoundingBox.Get (aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
490
491   typename BVH_Box<T, 3>::BVH_VecNt aMin (aXmin, aYmin, aZmin);
492   typename BVH_Box<T, 3>::BVH_VecNt aMax (aXmax, aYmax, aZmax);
493   BVH_Box<T, 3> aBBox (aMin, aMax);
494
495   Apply (theCamera, theProjection, theWorldView, theViewportWidth, theViewportHeight, aBBox);
496
497   theBoundingBox = Bnd_Box();
498   theBoundingBox.Update (aBBox.CornerMin().x(), aBBox.CornerMin().y(), aBBox.CornerMin().z(),
499                          aBBox.CornerMax().x(), aBBox.CornerMax().y(), aBBox.CornerMax().z());
500 }
501
502 // =======================================================================
503 // function : Apply
504 // purpose  : Apply transformation to bounding box of presentation.
505 // =======================================================================
506 template<class T>
507 void Graphic3d_TransformPers::Apply (const Handle(Graphic3d_Camera)& theCamera,
508                                      const NCollection_Mat4<T>& theProjection,
509                                      const NCollection_Mat4<T>& theWorldView,
510                                      const Standard_Integer theViewportWidth,
511                                      const Standard_Integer theViewportHeight,
512                                      BVH_Box<T, 3>& theBoundingBox) const
513 {
514   NCollection_Mat4<T> aTPers = Compute (theCamera, theProjection, theWorldView, theViewportWidth, theViewportHeight);
515   if (aTPers.IsIdentity()
516   || !theBoundingBox.IsValid())
517   {
518     return;
519   }
520
521   const typename BVH_Box<T, 3>::BVH_VecNt& aMin = theBoundingBox.CornerMin();
522   const typename BVH_Box<T, 3>::BVH_VecNt& aMax = theBoundingBox.CornerMax();
523
524   typename BVH_Box<T, 4>::BVH_VecNt anArrayOfCorners[8];
525   anArrayOfCorners[0] = typename BVH_Box<T, 4>::BVH_VecNt (aMin.x(), aMin.y(), aMin.z(), static_cast<T> (1.0));
526   anArrayOfCorners[1] = typename BVH_Box<T, 4>::BVH_VecNt (aMin.x(), aMin.y(), aMax.z(), static_cast<T> (1.0));
527   anArrayOfCorners[2] = typename BVH_Box<T, 4>::BVH_VecNt (aMin.x(), aMax.y(), aMin.z(), static_cast<T> (1.0));
528   anArrayOfCorners[3] = typename BVH_Box<T, 4>::BVH_VecNt (aMin.x(), aMax.y(), aMax.z(), static_cast<T> (1.0));
529   anArrayOfCorners[4] = typename BVH_Box<T, 4>::BVH_VecNt (aMax.x(), aMin.y(), aMin.z(), static_cast<T> (1.0));
530   anArrayOfCorners[5] = typename BVH_Box<T, 4>::BVH_VecNt (aMax.x(), aMin.y(), aMax.z(), static_cast<T> (1.0));
531   anArrayOfCorners[6] = typename BVH_Box<T, 4>::BVH_VecNt (aMax.x(), aMax.y(), aMin.z(), static_cast<T> (1.0));
532   anArrayOfCorners[7] = typename BVH_Box<T, 4>::BVH_VecNt (aMax.x(), aMax.y(), aMax.z(), static_cast<T> (1.0));
533
534   theBoundingBox.Clear();
535   for (Standard_Integer anIt = 0; anIt < 8; ++anIt)
536   {
537     typename BVH_Box<T, 4>::BVH_VecNt& aCorner = anArrayOfCorners[anIt];
538     aCorner  = aTPers * aCorner;
539     aCorner = aCorner / aCorner.w();
540     theBoundingBox.Add (typename BVH_Box<T, 3>::BVH_VecNt (aCorner.x(), aCorner.y(), aCorner.z()));
541   }
542 }
543
544 // =======================================================================
545 // function : Compute
546 // purpose  : Compute transformation.
547 // =======================================================================
548 template<class T>
549 NCollection_Mat4<T> Graphic3d_TransformPers::Compute (const Handle(Graphic3d_Camera)& theCamera,
550                                                       const NCollection_Mat4<T>& theProjection,
551                                                       const NCollection_Mat4<T>& theWorldView,
552                                                       const Standard_Integer theViewportWidth,
553                                                       const Standard_Integer theViewportHeight) const
554 {
555   if (myMode == Graphic3d_TMF_None)
556   {
557     return NCollection_Mat4<T>();
558   }
559
560   NCollection_Mat4<T> aWorldView (theWorldView);
561   NCollection_Mat4<T> anUnviewMat;
562   if (!theWorldView.Inverted (anUnviewMat))
563   {
564     return NCollection_Mat4<T>();
565   }
566
567   // compute only world-view matrix difference to avoid floating point instability
568   // caused by projection matrix modifications outside of this algorithm (e.g. by Z-fit)
569   Apply (theCamera, theProjection, aWorldView, theViewportWidth, theViewportHeight);
570   return anUnviewMat * aWorldView;
571 }
572
573 #endif // _Graphic3d_TransformPers_HeaderFile