0024393: Visualization - objects position with enchanced precision
[occt.git] / src / OpenGl / OpenGl_BVHTreeSelector.cxx
index 66492f3..21cfccb 100644 (file)
@@ -41,53 +41,53 @@ void OpenGl_BVHTreeSelector::SetViewVolume (const Handle(Graphic3d_Camera)& theC
   myIsProjectionParallel = theCamera->IsOrthographic();
 
   myCamera             = theCamera;
-  myProjectionMat      = theCamera->ProjectionMatrixF();
-  myWorldViewMat       = theCamera->OrientationMatrixF();
+  myProjectionMat      = theCamera->ProjectionMatrix();
+  myWorldViewMat       = theCamera->OrientationMatrix();
   myWorldViewProjState = theCamera->WorldViewProjState();
 
-  Standard_ShortReal nLeft = 0.0f, nRight = 0.0f, nTop = 0.0f, nBottom = 0.0f;
-  Standard_ShortReal fLeft = 0.0f, fRight = 0.0f, fTop = 0.0f, fBottom = 0.0f;
-  Standard_ShortReal aNear = 0.0f, aFar   = 0.0f;
+  Standard_Real nLeft = 0.0, nRight = 0.0, nTop = 0.0, nBottom = 0.0;
+  Standard_Real fLeft = 0.0, fRight = 0.0, fTop = 0.0, fBottom = 0.0;
+  Standard_Real aNear = 0.0, aFar   = 0.0;
   if (!myIsProjectionParallel)
   {
     // handle perspective projection
-    aNear   = myProjectionMat.GetValue (2, 3) / (- 1.0f + myProjectionMat.GetValue (2, 2));
-    aFar    = myProjectionMat.GetValue (2, 3) / (  1.0f + myProjectionMat.GetValue (2, 2));
+    aNear   = myProjectionMat.GetValue (2, 3) / (- 1.0 + myProjectionMat.GetValue (2, 2));
+    aFar    = myProjectionMat.GetValue (2, 3) / (  1.0 + myProjectionMat.GetValue (2, 2));
     // Near plane
-    nLeft   = aNear * (myProjectionMat.GetValue (0, 2) - 1.0f) / myProjectionMat.GetValue (0, 0);
-    nRight  = aNear * (myProjectionMat.GetValue (0, 2) + 1.0f) / myProjectionMat.GetValue (0, 0);
-    nTop    = aNear * (myProjectionMat.GetValue (1, 2) + 1.0f) / myProjectionMat.GetValue (1, 1);
-    nBottom = aNear * (myProjectionMat.GetValue (1, 2) - 1.0f) / myProjectionMat.GetValue (1, 1);
+    nLeft   = aNear * (myProjectionMat.GetValue (0, 2) - 1.0) / myProjectionMat.GetValue (0, 0);
+    nRight  = aNear * (myProjectionMat.GetValue (0, 2) + 1.0) / myProjectionMat.GetValue (0, 0);
+    nTop    = aNear * (myProjectionMat.GetValue (1, 2) + 1.0) / myProjectionMat.GetValue (1, 1);
+    nBottom = aNear * (myProjectionMat.GetValue (1, 2) - 1.0) / myProjectionMat.GetValue (1, 1);
     // Far plane
-    fLeft   = aFar  * (myProjectionMat.GetValue (0, 2) - 1.0f) / myProjectionMat.GetValue (0, 0);
-    fRight  = aFar  * (myProjectionMat.GetValue (0, 2) + 1.0f) / myProjectionMat.GetValue (0, 0);
-    fTop    = aFar  * (myProjectionMat.GetValue (1, 2) + 1.0f) / myProjectionMat.GetValue (1, 1);
-    fBottom = aFar  * (myProjectionMat.GetValue (1, 2) - 1.0f) / myProjectionMat.GetValue (1, 1);
+    fLeft   = aFar  * (myProjectionMat.GetValue (0, 2) - 1.0) / myProjectionMat.GetValue (0, 0);
+    fRight  = aFar  * (myProjectionMat.GetValue (0, 2) + 1.0) / myProjectionMat.GetValue (0, 0);
+    fTop    = aFar  * (myProjectionMat.GetValue (1, 2) + 1.0) / myProjectionMat.GetValue (1, 1);
+    fBottom = aFar  * (myProjectionMat.GetValue (1, 2) - 1.0) / myProjectionMat.GetValue (1, 1);
   }
   else
   {
     // handle orthographic projection
-    aNear   = (1.0f / myProjectionMat.GetValue (2, 2)) * (myProjectionMat.GetValue (2, 3) + 1.0f);
-    aFar    = (1.0f / myProjectionMat.GetValue (2, 2)) * (myProjectionMat.GetValue (2, 3) - 1.0f);
+    aNear   = (1.0 / myProjectionMat.GetValue (2, 2)) * (myProjectionMat.GetValue (2, 3) + 1.0);
+    aFar    = (1.0 / myProjectionMat.GetValue (2, 2)) * (myProjectionMat.GetValue (2, 3) - 1.0);
     // Near plane
-    nLeft   = ( 1.0f + myProjectionMat.GetValue (0, 3)) / (-myProjectionMat.GetValue (0, 0));
+    nLeft   = ( 1.0 + myProjectionMat.GetValue (0, 3)) / (-myProjectionMat.GetValue (0, 0));
     fLeft   = nLeft;
-    nRight  = ( 1.0f - myProjectionMat.GetValue (0, 3)) /   myProjectionMat.GetValue (0, 0);
+    nRight  = ( 1.0 - myProjectionMat.GetValue (0, 3)) /   myProjectionMat.GetValue (0, 0);
     fRight  = nRight;
-    nTop    = ( 1.0f - myProjectionMat.GetValue (1, 3)) /   myProjectionMat.GetValue (1, 1);
+    nTop    = ( 1.0 - myProjectionMat.GetValue (1, 3)) /   myProjectionMat.GetValue (1, 1);
     fTop    = nTop;
-    nBottom = (-1.0f - myProjectionMat.GetValue (1, 3)) /   myProjectionMat.GetValue (1, 1);
+    nBottom = (-1.0 - myProjectionMat.GetValue (1, 3)) /   myProjectionMat.GetValue (1, 1);
     fBottom = nBottom;
   }
 
-  OpenGl_Vec4 aLeftTopNear     (nLeft,  nTop,    -aNear, 1.0f), aRightBottomFar (fRight, fBottom, -aFar, 1.0f);
-  OpenGl_Vec4 aLeftBottomNear  (nLeft,  nBottom, -aNear, 1.0f), aRightTopFar    (fRight, fTop,    -aFar, 1.0f);
-  OpenGl_Vec4 aRightBottomNear (nRight, nBottom, -aNear, 1.0f), aLeftTopFar     (fLeft,  fTop,    -aFar, 1.0f);
-  OpenGl_Vec4 aRightTopNear    (nRight, nTop,    -aNear, 1.0f), aLeftBottomFar  (fLeft,  fBottom, -aFar, 1.0f);
+  OpenGl_Vec4d aLeftTopNear     (nLeft,  nTop,    -aNear, 1.0), aRightBottomFar (fRight, fBottom, -aFar, 1.0);
+  OpenGl_Vec4d aLeftBottomNear  (nLeft,  nBottom, -aNear, 1.0), aRightTopFar    (fRight, fTop,    -aFar, 1.0);
+  OpenGl_Vec4d aRightBottomNear (nRight, nBottom, -aNear, 1.0), aLeftTopFar     (fLeft,  fTop,    -aFar, 1.0);
+  OpenGl_Vec4d aRightTopNear    (nRight, nTop,    -aNear, 1.0), aLeftBottomFar  (fLeft,  fBottom, -aFar, 1.0);
 
-  const OpenGl_Mat4 aViewProj = myWorldViewMat * myProjectionMat;
-  OpenGl_Mat4 anInvWorldView;
-  myWorldViewMat.Inverted(anInvWorldView);
+  const OpenGl_Mat4d aViewProj = myWorldViewMat * myProjectionMat;
+  OpenGl_Mat4d anInvWorldView;
+  myWorldViewMat.Inverted (anInvWorldView);
 
   myClipVerts[ClipVert_LeftTopNear]     = anInvWorldView * aLeftTopNear;
   myClipVerts[ClipVert_RightBottomFar]  = anInvWorldView * aRightBottomFar;
@@ -107,17 +107,14 @@ void OpenGl_BVHTreeSelector::SetViewVolume (const Handle(Graphic3d_Camera)& theC
   myClipPlanes[Plane_Far]    = aViewProj.GetRow (3) - aViewProj.GetRow (2);
 
   gp_Pnt aPtCenter = theCamera->Center();
-  OpenGl_Vec4 aCenter (static_cast<Standard_ShortReal> (aPtCenter.X()),
-                       static_cast<Standard_ShortReal> (aPtCenter.Y()),
-                       static_cast<Standard_ShortReal> (aPtCenter.Z()),
-                       1.0f);
+  OpenGl_Vec4d aCenter (aPtCenter.X(), aPtCenter.Y(), aPtCenter.Z(), 1.0);
 
   for (Standard_Integer aPlaneIter = 0; aPlaneIter < PlanesNB; ++aPlaneIter)
   {
-    OpenGl_Vec4 anEq = myClipPlanes[aPlaneIter];
+    OpenGl_Vec4d anEq = myClipPlanes[aPlaneIter];
     if (SignedPlanePointDistance (anEq, aCenter) > 0)
     {
-      anEq *= -1.0f;
+      anEq *= -1.0;
       myClipPlanes[aPlaneIter] = anEq;
     }
   }
@@ -138,21 +135,21 @@ void OpenGl_BVHTreeSelector::SetViewportSize (const Standard_Integer theViewport
 // function : SignedPlanePointDistance
 // purpose  :
 // =======================================================================
-Standard_ShortReal OpenGl_BVHTreeSelector::SignedPlanePointDistance (const OpenGl_Vec4& theNormal,
-                                                                     const OpenGl_Vec4& thePnt)
+Standard_Real OpenGl_BVHTreeSelector::SignedPlanePointDistance (const OpenGl_Vec4d& theNormal,
+                                                                const OpenGl_Vec4d& thePnt)
 {
-  const Standard_ShortReal aNormLength = std::sqrt (theNormal.x() * theNormal.x()
-                                                  + theNormal.y() * theNormal.y()
-                                                  + theNormal.z() * theNormal.z());
+  const Standard_Real aNormLength = std::sqrt (theNormal.x() * theNormal.x()
+                                             + theNormal.y() * theNormal.y()
+                                             + theNormal.z() * theNormal.z());
 
-  if (aNormLength < FLT_EPSILON)
-    return 0.0f;
+  if (aNormLength < gp::Resolution())
+    return 0.0;
 
-  const Standard_ShortReal anInvNormLength = 1.0f / aNormLength;
-  const Standard_ShortReal aD  = theNormal.w() * anInvNormLength;
-  const Standard_ShortReal anA = theNormal.x() * anInvNormLength;
-  const Standard_ShortReal aB  = theNormal.y() * anInvNormLength;
-  const Standard_ShortReal aC  = theNormal.z() * anInvNormLength;
+  const Standard_Real anInvNormLength = 1.0 / aNormLength;
+  const Standard_Real aD  = theNormal.w() * anInvNormLength;
+  const Standard_Real anA = theNormal.x() * anInvNormLength;
+  const Standard_Real aB  = theNormal.y() * anInvNormLength;
+  const Standard_Real aC  = theNormal.z() * anInvNormLength;
   return aD + (anA * thePnt.x() + aB * thePnt.y() + aC * thePnt.z());
 }
 
@@ -166,14 +163,14 @@ void OpenGl_BVHTreeSelector::CacheClipPtsProjections()
   const Standard_Integer anIncFactor = myIsProjectionParallel ? 2 : 1;
   for (Standard_Integer aPlaneIter = 0; aPlaneIter < 5; aPlaneIter += anIncFactor)
   {
-    const OpenGl_Vec4 aPlane = myClipPlanes[aPlaneIter];
-    Standard_ShortReal aMaxProj = -std::numeric_limits<Standard_ShortReal>::max();
-    Standard_ShortReal aMinProj =  std::numeric_limits<Standard_ShortReal>::max();
+    const OpenGl_Vec4d aPlane = myClipPlanes[aPlaneIter];
+    Standard_Real aMaxProj = -std::numeric_limits<Standard_Real>::max();
+    Standard_Real aMinProj =  std::numeric_limits<Standard_Real>::max();
     for (Standard_Integer aCornerIter = 0; aCornerIter < ClipVerticesNB; ++aCornerIter)
     {
-      Standard_ShortReal aProjection = aPlane.x() * myClipVerts[aCornerIter].x() +
-                                       aPlane.y() * myClipVerts[aCornerIter].y() +
-                                       aPlane.z() * myClipVerts[aCornerIter].z();
+      Standard_Real aProjection = aPlane.x() * myClipVerts[aCornerIter].x()
+                                + aPlane.y() * myClipVerts[aCornerIter].y()
+                                + aPlane.z() * myClipVerts[aCornerIter].z();
       aMaxProj = Max (aProjection, aMaxProj);
       aMinProj = Min (aProjection, aMinProj);
     }
@@ -183,12 +180,15 @@ void OpenGl_BVHTreeSelector::CacheClipPtsProjections()
 
   for (Standard_Integer aDim = 0; aDim < 3; ++aDim)
   {
-    Standard_ShortReal aMaxProj = -std::numeric_limits<Standard_ShortReal>::max();
-    Standard_ShortReal aMinProj =  std::numeric_limits<Standard_ShortReal>::max();
+    Standard_Real aMaxProj = -std::numeric_limits<Standard_Real>::max();
+    Standard_Real aMinProj =  std::numeric_limits<Standard_Real>::max();
     for (Standard_Integer aCornerIter = 0; aCornerIter < ClipVerticesNB; ++aCornerIter)
     {
-      Standard_ShortReal aProjection = aDim == 0 ? myClipVerts[aCornerIter].x()
-        : (aDim == 1 ? myClipVerts[aCornerIter].y() : myClipVerts[aCornerIter].z());
+      Standard_Real aProjection = aDim == 0
+                                ? myClipVerts[aCornerIter].x()
+                                : (aDim == 1
+                                 ? myClipVerts[aCornerIter].y()
+                                 : myClipVerts[aCornerIter].z());
       aMaxProj = Max (aProjection, aMaxProj);
       aMinProj = Min (aProjection, aMinProj);
     }
@@ -201,8 +201,8 @@ void OpenGl_BVHTreeSelector::CacheClipPtsProjections()
 // function : Intersect
 // purpose  : Detects if AABB overlaps view volume using separating axis theorem (SAT)
 // =======================================================================
-Standard_Boolean OpenGl_BVHTreeSelector::Intersect (const OpenGl_Vec4& theMinPt,
-                                                    const OpenGl_Vec4& theMaxPt) const
+Standard_Boolean OpenGl_BVHTreeSelector::Intersect (const OpenGl_Vec3d& theMinPt,
+                                                    const OpenGl_Vec3d& theMaxPt) const
 {
   //     E1
   //    |_ E0
@@ -230,23 +230,23 @@ Standard_Boolean OpenGl_BVHTreeSelector::Intersect (const OpenGl_Vec4& theMinPt,
     return Standard_False;
   }
 
-  Standard_ShortReal aBoxProjMax = 0.0f, aBoxProjMin = 0.0f;
+  Standard_Real aBoxProjMax = 0.0, aBoxProjMin = 0.0;
   const Standard_Integer anIncFactor = myIsProjectionParallel ? 2 : 1;
   for (Standard_Integer aPlaneIter = 0; aPlaneIter < 5; aPlaneIter += anIncFactor)
   {
-    OpenGl_Vec4 aPlane = myClipPlanes[aPlaneIter];
-    aBoxProjMax = (aPlane.x() > 0.f ? (aPlane.x() * theMaxPt.x()) : aPlane.x() * theMinPt.x()) +
-                  (aPlane.y() > 0.f ? (aPlane.y() * theMaxPt.y()) : aPlane.y() * theMinPt.y()) +
-                  (aPlane.z() > 0.f ? (aPlane.z() * theMaxPt.z()) : aPlane.z() * theMinPt.z());
+    OpenGl_Vec4d aPlane = myClipPlanes[aPlaneIter];
+    aBoxProjMax = (aPlane.x() > 0.0 ? (aPlane.x() * theMaxPt.x()) : aPlane.x() * theMinPt.x())
+                + (aPlane.y() > 0.0 ? (aPlane.y() * theMaxPt.y()) : aPlane.y() * theMinPt.y())
+                + (aPlane.z() > 0.0 ? (aPlane.z() * theMaxPt.z()) : aPlane.z() * theMinPt.z());
     if (aBoxProjMax > myMinClipProjectionPts[aPlaneIter]
      && aBoxProjMax < myMaxClipProjectionPts[aPlaneIter])
     {
       continue;
     }
 
-    aBoxProjMin = (aPlane.x() < 0.f ? aPlane.x() * theMaxPt.x() : aPlane.x() * theMinPt.x()) +
-                  (aPlane.y() < 0.f ? aPlane.y() * theMaxPt.y() : aPlane.y() * theMinPt.y()) +
-                  (aPlane.z() < 0.f ? aPlane.z() * theMaxPt.z() : aPlane.z() * theMinPt.z());
+    aBoxProjMin = (aPlane.x() < 0.0 ? aPlane.x() * theMaxPt.x() : aPlane.x() * theMinPt.x())
+                + (aPlane.y() < 0.0 ? aPlane.y() * theMaxPt.y() : aPlane.y() * theMinPt.y())
+                + (aPlane.z() < 0.0 ? aPlane.z() * theMaxPt.z() : aPlane.z() * theMinPt.z());
     if (aBoxProjMin > myMaxClipProjectionPts[aPlaneIter]
      || aBoxProjMax < myMinClipProjectionPts[aPlaneIter])
     {