[Yake-svn] SF.net SVN: yake:[1929] trunk/yake/yake/base/math
Status: Beta
Brought to you by:
psyclonist
From: <psy...@us...> - 2008-07-24 18:47:21
|
Revision: 1929 http://yake.svn.sourceforge.net/yake/?rev=1929&view=rev Author: psyclonist Date: 2008-07-24 18:47:30 +0000 (Thu, 24 Jul 2008) Log Message: ----------- * [base/math] added Plane::distanceTo() * [base/math] added Plane c'tor: from 3 points * [base/math] added intersect(Plane,Ray) Modified Paths: -------------- trunk/yake/yake/base/math/yakePlane.h trunk/yake/yake/base/math/yakeRay.h Modified: trunk/yake/yake/base/math/yakePlane.h =================================================================== --- trunk/yake/yake/base/math/yakePlane.h 2008-06-30 20:25:46 UTC (rev 1928) +++ trunk/yake/yake/base/math/yakePlane.h 2008-07-24 18:47:30 UTC (rev 1929) @@ -33,22 +33,58 @@ #endif #include "yakeMath.h" #include "yakeVector3.h" +#include "yakeRay.h" namespace yake { namespace math { + /** Represents a plane in space. */ class YAKE_BASE_API Plane { public: + /** @param kD distance (from the plane to the origin) in direction of rkNormal */ Plane( const Vector3 & rkNormal, const real kD ) : normal( rkNormal ), d( kD ) { } + Plane(const Point3& p0, const Point3& p1, const Point3& p2) : + normal((p1-p0).crossProduct(p2-p0).normalisedCopy()), d(-normal.dotProduct(p0.asVector())) + { + } + /** Positive if plane faces the point, + negative if it's on the other side ("behind" the plane). + @note d=(pt-p0).n + @note n.pt+D + */ + real distanceTo(const Point3& pt) const + { + return (normal.dotProduct(pt.asVector()) + d); + } Vector3 normal; real d; }; -} -} + /** Intersects a ray with a plane (if possible) and returns + the parameter on the ray which can be used to calculate + the intersection point. + @return result.first is true if the ray intersects. result.second is + the length along the ray which can be used to calculate the + intersection point like this: intPt= ray.origin + ray.direction * result.second + or intPt= ray.getPoint(result.second). + @note The returned length can be negative! + */ + inline std::pair<bool,real> intersect(const Plane& plane, const Ray& ray) + { + const real denom = plane.normal.dotProduct(ray.direction()); + if (fabs(denom)<0.0001) //@todo use a proper EPS + return std::make_pair(false,0); + const real nom = plane.normal.dotProduct(ray.origin().asVector()) + plane.d; + const real s = - (nom / denom); + return (s >= 0.) ? std::make_pair(true,s) : std::make_pair(false,0); + } +} // namespace math +using math::Plane; +} // namespace yake + #endif Modified: trunk/yake/yake/base/math/yakeRay.h =================================================================== --- trunk/yake/yake/base/math/yakeRay.h 2008-06-30 20:25:46 UTC (rev 1928) +++ trunk/yake/yake/base/math/yakeRay.h 2008-07-24 18:47:30 UTC (rev 1929) @@ -39,6 +39,7 @@ namespace yake { namespace math { + /** Represents a ray; from a point along a direction. */ class YAKE_BASE_API Ray { public: @@ -52,6 +53,14 @@ { return mOrigin; } + const Vector3& direction() const + { + return mDirection; + } + const Point3& origin() const + { + return mOrigin; + } const Vector3& getDirection() const { return mDirection; @@ -64,11 +73,14 @@ { mDirection = rDirection; } + /** Evaluate the parameter s (i.e. length here) + along the ray. + @note s may be negative. + */ Point3 getPoint( const real s ) const { return (mOrigin + (mDirection * s)); } - std::pair<bool, real> intersects(const Plane& plane) const; private: Point3 mOrigin; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |