]> git.mxchange.org Git - simgear.git/blobdiff - simgear/math/SGIntersect.hxx
fix SGPagedLOD change for 2.8.3
[simgear.git] / simgear / math / SGIntersect.hxx
index b2438dc6e3dd05a379f9cadcff765390b017d872..24741f6409bd78dcfeb603525aab6cbd0a474592 100644 (file)
@@ -38,25 +38,11 @@ intersects(const SGBox<T1>& box, const SGSphere<T2>& sphere)
 {
   if (sphere.empty())
     return false;
-  // Is more or less trivially included in the next tests
-  // if (box.empty())
-  //   return false;
-
-  if (sphere.getCenter().x() < box.getMin().x() - sphere.getRadius())
-    return false;
-  if (sphere.getCenter().y() < box.getMin().y() - sphere.getRadius())
-    return false;
-  if (sphere.getCenter().z() < box.getMin().z() - sphere.getRadius())
-    return false;
-
-  if (box.getMax().x() + sphere.getRadius() < sphere.getCenter().x())
-    return false;
-  if (box.getMax().y() + sphere.getRadius() < sphere.getCenter().y())
-    return false;
-  if (box.getMax().z() + sphere.getRadius() < sphere.getCenter().z())
+  if (box.empty())
     return false;
 
-  return true;
+  SGVec3<T1> closest = box.getClosestPoint(sphere.getCenter());
+  return distSqr(closest, SGVec3<T1>(sphere.getCenter())) <= sphere.getRadius2();
 }
 // make it symmetric
 template<typename T1, typename T2>
@@ -572,13 +558,15 @@ closestPoint(const SGTriangle<T>& tri, const SGVec3<T>& p)
   T u = b*e - c*d;
   T v = b*d - a*e;
 
+/*
   // Regions
   // \2|
   //  \|
-  //   |\
+  //   |\ 
   // 3 |0\ 1
   //----------
   // 4 | 5 \ 6
+*/
 
   if (u + v <= det) {
     if (u < 0) {
@@ -743,9 +731,9 @@ inline SGVec3<T>
 closestPoint(const SGVec3<T>& p, const SGTriangle<T>& tri)
 { return closestPoint(tri, p); }
 
-template<typename T>
+template<typename T, typename T2>
 inline bool
-intersects(const SGTriangle<T>& tri, const SGSphere<T>& sphere)
+intersects(const SGTriangle<T>& tri, const SGSphere<T2>& sphere)
 {
   // This method minimizes the distance function Q(u, v) = || p - x ||
   // where x is a point in the trialgle given by the vertices v_i
@@ -753,7 +741,7 @@ intersects(const SGTriangle<T>& tri, const SGSphere<T>& sphere)
   // The theoretical analysis is somehow too long for a comment.
   // May be it is sufficient to see that this code passes all the tests.
 
-  SGVec3<T> off = tri.getBaseVertex() - sphere.getCenter();
+  SGVec3<T> off = tri.getBaseVertex() - SGVec3<T>(sphere.getCenter());
   T baseDist2 = dot(off, off);
   T a = dot(tri.getEdge(0), tri.getEdge(0));
   T b = dot(tri.getEdge(0), tri.getEdge(1));
@@ -766,13 +754,15 @@ intersects(const SGTriangle<T>& tri, const SGSphere<T>& sphere)
   T u = b*e - c*d;
   T v = b*d - a*e;
 
+/*
   // Regions
   // \2|
   //  \|
-  //   |\
+  //   |\ 
   // 3 |0\ 1
   //----------
   // 4 | 5 \ 6
+*/
 
   if (u + v <= det) {
     if (u < 0) {
@@ -954,9 +944,9 @@ intersects(const SGTriangle<T>& tri, const SGSphere<T>& sphere)
     }
   }
 }
-template<typename T>
+template<typename T1, typename T2>
 inline bool
-intersects(const SGSphere<T>& sphere, const SGTriangle<T>& tri)
+intersects(const SGSphere<T1>& sphere, const SGTriangle<T2>& tri)
 { return intersects(tri, sphere); }