]> git.mxchange.org Git - simgear.git/commitdiff
Modified Files:
authorfrohlich <frohlich>
Tue, 30 Jan 2007 20:12:15 +0000 (20:12 +0000)
committerfrohlich <frohlich>
Tue, 30 Jan 2007 20:12:15 +0000 (20:12 +0000)
SGIntersect.hxx SGVec3.hxx SGVec4.hxx: Add convinience methods

simgear/math/SGIntersect.hxx
simgear/math/SGVec3.hxx
simgear/math/SGVec4.hxx

index 4d87d68844c19af03a4a1dcd707f8740a314653c..5951e158fafcfea6f81268a3affe04ca4666b2fa 100644 (file)
@@ -287,6 +287,38 @@ intersects(SGVec3<T>& dst, const SGPlane<T>& plane, const SGLineSegment<T>& line
 { return intersects(dst, lineSegment, plane); }
 
 
+// Distance of a line segment to a point
+template<typename T>
+inline T
+distSqr(const SGLineSegment<T>& lineSeg, const SGVec3<T>& p)
+{
+  SGVec3<T> ps = p - lineSeg.getStart();
+
+  T psdotdir = dot(ps, lineSeg.getDirection());
+  if (psdotdir <= 0)
+    return dot(ps, ps);
+
+  SGVec3<T> pe = p - lineSeg.getEnd();
+  if (0 <= dot(pe, lineSeg.getDirection()))
+    return dot(pe, pe);
+  return dot(ps, ps) - psdotdir*psdotdir/dot(lineSeg.getDirection(), lineSeg.getDirection());
+}
+// make it symmetric
+template<typename T>
+inline T
+distSqr(const SGVec3<T>& p, const SGLineSegment<T>& lineSeg)
+{ return distSqr(lineSeg, p); }
+// with sqrt
+template<typename T>
+inline T
+dist(const SGVec3<T>& p, const SGLineSegment<T>& lineSeg)
+{ return sqrt(distSqr(lineSeg, p)); }
+template<typename T>
+inline T
+dist(const SGLineSegment<T>& lineSeg, const SGVec3<T>& p)
+{ return sqrt(distSqr(lineSeg, p)); }
+
 template<typename T>
 inline bool
 intersects(const SGRay<T>& ray, const SGSphere<T>& sphere)
index e33e8c05350bb1f96f39dfe95fd3b6c13619b678..9e07584e1755411837ea83b8effadb930fd8282f 100644 (file)
@@ -96,6 +96,8 @@ public:
   { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; }
   explicit SGVec3(const osg::Vec3d& d)
   { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; }
+  explicit SGVec3(const SGVec2<T>& v2, const T& v3 = 0)
+  { data()[0] = v2[0]; data()[1] = v2[1]; data()[2] = v3; }
 
   /// Access by index, the index is unchecked
   const T& operator()(unsigned i) const
index d610a6ca8c11851285edfaa9a3f33f901cbf7a84..7c714323f102ec8a0ee42c874c53a2a683461579 100644 (file)
@@ -96,6 +96,8 @@ public:
   { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; data()[3] = d[3]; }
   explicit SGVec4(const osg::Vec4d& d)
   { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; data()[3] = d[3]; }
+  explicit SGVec4(const SGVec3<T>& v3, const T& v4 = 0)
+  { data()[0] = v3[0]; data()[1] = v3[1]; data()[2] = v3[2]; data()[3] = v4; }
 
 
   /// Access by index, the index is unchecked