Ver Fonte

Removing the optimizations. Its not worth it

Panagiotis Christopoulos Charitos há 14 anos atrás
pai
commit
93503c8e38

+ 41 - 0
anki/collision/Aabb.cpp

@@ -43,6 +43,47 @@ Aabb Aabb::getTransformed(const Transform& transform) const
 }
 
 
+//==============================================================================
+float Aabb::testPlane(const Plane& p) const
+{
+	const Aabb& aabb = *this;
+	Vec3 diagMin, diagMax;
+	// set min/max values for x,y,z direction
+	for(int i = 0; i < 3; i++)
+	{
+		if(p.getNormal()[i] >= 0.0)
+		{
+			diagMin[i] = aabb.getMin()[i];
+			diagMax[i] = aabb.getMax()[i];
+		}
+		else
+		{
+			diagMin[i] = aabb.getMax()[i];
+			diagMax[i] = aabb.getMin()[i];
+		}
+	}
+
+	// minimum on positive side of plane, box on positive side
+	float test = p.test(diagMin);
+	if(test > 0.0)
+	{
+		return test;
+	}
+
+	test = p.test(diagMax);
+	// min on non-positive side, max on non-negative side, intersection
+	if(test >= 0.0)
+	{
+		return 0.0;
+	}
+	// max on negative side, box on negative side
+	else
+	{
+		return test;
+	}
+}
+
+
 //==============================================================================
 Aabb Aabb::getCompoundShape(const Aabb& b) const
 {

+ 3 - 6
anki/collision/Aabb.h

@@ -74,13 +74,10 @@ class Aabb: public CollisionShape
 			v.visit(*this);
 		}
 
-		/// Overrides CollisionShape::testPlane
-		float testPlane(const Plane& p) const
-		{
-			return PlaneTests::test(p, *this);
-		}
+		/// Implements CollisionShape::testPlane
+		float testPlane(const Plane& p) const;
 
-		/// Overrides CollisionShape::transform
+		/// Implements CollisionShape::transform
 		void transform(const Transform& trf)
 		{
 			*this = getTransformed(trf);

+ 7 - 11
anki/collision/CollisionShape.h

@@ -2,8 +2,7 @@
 #define ANKI_COLLISION_COLLISION_SHAPE
 
 #include "anki/collision/Forward.h"
-#include "anki/collision/PlaneTests.h"
-#include "anki/collision/CollisionShapeTransform.h"
+#include "anki/math/Forward.h"
 
 
 namespace anki {
@@ -68,17 +67,14 @@ class CollisionShape
 		/// Visitor accept
 		virtual void accept(ConstVisitor& v) = 0;
 
-		/// See declaration of PlaneTests class
-		float testPlane(const Plane& p) const
-		{
-			return PlaneTests::test(p, *this);
-		}
+		/// If the collision shape intersects with the plane then the method
+		/// returns 0.0, else it returns the distance. If the distance is < 0.0
+		/// then the collision shape lies behind the plane and if > 0.0 then
+		/// in front of it
+		virtual float testPlane(const Plane& p) const = 0;
 
 		/// Transform
-		void transform(const Transform& trf)
-		{
-			CollisionShapeTransform::transform(trf, *this);
-		}
+		virtual void transform(const Transform& trf) = 0;
 
 	private:
 		CollisionShapeType cid;

+ 0 - 42
anki/collision/CollisionShapeTransform.cpp

@@ -1,42 +0,0 @@
-#include "anki/collision/CollisionShapeTransform.h"
-#include "anki/collision/Collision.h"
-#include "anki/math/Transform.h"
-
-
-namespace anki {
-
-
-//==============================================================================
-void CollisionShapeTransform::transform(const Transform& trf,
-	CollisionShape& cs)
-{
-	switch(cs.getCollisionShapeType())
-	{
-		case CollisionShape::CST_LINE_SEG:
-			static_cast<LineSegment&>(cs).transform(trf);
-			break;
-		case CollisionShape::CST_RAY:
-			static_cast<Ray&>(cs).transform(trf);
-			break;
-		case CollisionShape::CST_PLANE:
-			static_cast<Plane&>(cs).transform(trf);
-			break;
-		case CollisionShape::CST_SPHERE:
-			static_cast<Sphere&>(cs).transform(trf);
-			break;
-		case CollisionShape::CST_AABB:
-			static_cast<Aabb&>(cs).transform(trf);
-			break;
-		case CollisionShape::CST_OBB:
-			static_cast<Obb&>(cs).transform(trf);
-			break;
-		case CollisionShape::CST_PERSPECTIVE_CAMERA_FRUSTRUM:
-			static_cast<PerspectiveCameraShape&>(cs).transform(trf);
-			break;
-		default:
-			ASSERT(0 && "Forgot something");
-	}
-}
-
-
-} // end namespace

+ 0 - 23
anki/collision/CollisionShapeTransform.h

@@ -1,23 +0,0 @@
-#ifndef ANKI_COLLISION_COLLISION_SHAPE_TRANSFORM_H
-#define ANKI_COLLISION_COLLISION_SHAPE_TRANSFORM_H
-
-#include "anki/collision/Forward.h"
-#include "anki/math/Forward.h"
-
-
-namespace anki {
-
-
-/// Transforms a shape
-class CollisionShapeTransform
-{
-	public:
-		/// Generic transform
-		static void transform(const Transform& trf, CollisionShape& cs);
-};
-
-
-} // end namespace
-
-
-#endif

+ 35 - 0
anki/collision/LineSegment.cpp

@@ -6,6 +6,41 @@
 namespace anki {
 
 
+//==============================================================================
+float LineSegment::testPlane(const Plane& p) const
+{
+	const LineSegment& ls = *this;
+	const Vec3& p0 = ls.getOrigin();
+	Vec3 p1 = ls.getOrigin() + ls.getDirection();
+
+	float dist0 = p.test(p0);
+	float dist1 = p.test(p1);
+
+	if(dist0 > 0.0)
+	{
+		if(dist1 > 0.0)
+		{
+			return std::min(dist0, dist1);
+		}
+		else
+		{
+			return 0.0;
+		}
+	}
+	else
+	{
+		if(dist1 < 0.0)
+		{
+			return std::max(dist0, dist1);
+		}
+		else
+		{
+			return 0.0;
+		}
+	}
+}
+
+
 //==============================================================================
 LineSegment LineSegment::getTransformed(const Transform& transform) const
 {

+ 3 - 6
anki/collision/LineSegment.h

@@ -75,13 +75,10 @@ class LineSegment: public CollisionShape
 			v.visit(*this);
 		}
 
-		/// Overrides CollisionShape::testPlane
-		float testPlane(const Plane& p) const
-		{
-			return PlaneTests::test(p, *this);
-		}
+		/// Implements CollisionShape::testPlane
+		float testPlane(const Plane& p) const;
 
-		/// Overrides CollisionShape::transform
+		/// Implements CollisionShape::transform
 		void transform(const Transform& trf)
 		{
 			*this = getTransformed(trf);

+ 29 - 0
anki/collision/Obb.cpp

@@ -24,6 +24,35 @@ Obb::Obb(const Vec3& center_, const Mat3& rotation_,
 {}
 
 
+//==============================================================================
+float Obb::testPlane(const Plane& p) const
+{
+	const Obb& obb = *this;
+	Vec3 xNormal = obb.getRotation().getTransposed() * p.getNormal();
+
+	// maximum extent in direction of plane normal
+	float r = fabs(obb.getExtend().x() * xNormal.x()) +
+		fabs(obb.getExtend().y() * xNormal.y()) +
+		fabs(obb.getExtend().z() * xNormal.z());
+	// signed distance between box center and plane
+	float d = p.test(obb.getCenter());
+
+	// return signed distance
+	if(fabs(d) < r)
+	{
+		return 0.0;
+	}
+	else if(d < 0.0)
+	{
+		return d + r;
+	}
+	else
+	{
+		return d - r;
+	}
+}
+
+
 //==============================================================================
 Obb Obb::getTransformed(const Transform& transform) const
 {

+ 3 - 6
anki/collision/Obb.h

@@ -80,13 +80,10 @@ class Obb: public CollisionShape
 			v.visit(*this);
 		}
 
-		/// Overrides CollisionShape::testPlane
-		float testPlane(const Plane& p) const
-		{
-			return PlaneTests::test(p, *this);
-		}
+		/// Implements CollisionShape::testPlane
+		float testPlane(const Plane& p) const;
 
-		/// Overrides CollisionShape::transform
+		/// Implements CollisionShape::transform
 		void transform(const Transform& trf)
 		{
 			*this = getTransformed(trf);

+ 29 - 0
anki/collision/PerspectiveCameraShape.cpp

@@ -5,6 +5,35 @@
 namespace anki {
 
 
+//==============================================================================
+float PerspectiveCameraShape::testPlane(const Plane& p) const
+{
+	const PerspectiveCameraShape& pcs = *this;
+	float o = 0.0;
+
+	for(uint i = 0; i < 4; i++)
+	{
+		LineSegment ls(pcs.getEye(), pcs.getDirections()[i]);
+		float t = ls.testPlane(p);
+
+		if(t == 0)
+		{
+			return 0.0;
+		}
+		else if(t < 0.0)
+		{
+			o = std::max(o, t);
+		}
+		else
+		{
+			o = std::min(o, t);
+		}
+	}
+
+	return o;
+}
+
+
 //==============================================================================
 void PerspectiveCameraShape::setAll(float fovX, float fovY,
 	float zNear, float zFar, const Transform& trf)

+ 3 - 6
anki/collision/PerspectiveCameraShape.h

@@ -51,13 +51,10 @@ class PerspectiveCameraShape: public CollisionShape
 			v.visit(*this);
 		}
 
-		/// Overrides CollisionShape::testPlane
-		float testPlane(const Plane& p) const
-		{
-			return PlaneTests::test(p, *this);
-		}
+		/// Implements CollisionShape::testPlane
+		float testPlane(const Plane& p) const;
 
-		/// Overrides CollisionShape::transform
+		/// Implements CollisionShape::transform
 		void transform(const Transform& trf)
 		{
 			*this = getTransformed(trf);

+ 8 - 0
anki/collision/Plane.cpp

@@ -21,6 +21,14 @@ Plane::Plane(const Vec3& normal_, float offset_)
 {}
 
 
+//==============================================================================
+float Plane::testPlane(const Plane& /*p*/) const
+{
+	ASSERT(0 && "Ambiguous call");
+	return 0.0;
+}
+
+
 //==============================================================================
 void Plane::setFrom3Points(const Vec3& p0, const Vec3& p1, const Vec3& p2)
 {

+ 4 - 7
anki/collision/Plane.h

@@ -84,13 +84,10 @@ class Plane: public CollisionShape
 			v.visit(*this);
 		}
 
-		/// Overrides CollisionShape::testPlane
-		float testPlane(const Plane& p) const
-		{
-			return PlaneTests::test(p, *this);
-		}
+		/// Implements CollisionShape::testPlane
+		float testPlane(const Plane& p) const;
 
-		/// Overrides CollisionShape::transform
+		/// Implements CollisionShape::transform
 		void transform(const Transform& trf)
 		{
 			*this = getTransformed(trf);
@@ -124,7 +121,7 @@ class Plane: public CollisionShape
 		template<typename T>
 		float testShape(const T& x) const
 		{
-			return PlaneTests::test(*this, x);
+			return x.testPlane(*this, x);
 		}
 
 	private:

+ 0 - 231
anki/collision/PlaneTests.cpp

@@ -1,231 +0,0 @@
-#include "anki/collision/PlaneTests.h"
-#include "anki/collision/Collision.h"
-
-
-namespace anki {
-
-
-//==============================================================================
-float PlaneTests::test(const Plane& p, const CollisionShape& cs)
-{
-	float t;
-	switch(cs.getCollisionShapeType())
-	{
-		case CollisionShape::CST_LINE_SEG:
-			t = test(p, static_cast<const LineSegment&>(cs));
-			break;
-		case CollisionShape::CST_RAY:
-			t = test(p, static_cast<const Ray&>(cs));
-			break;
-		case CollisionShape::CST_PLANE:
-			t = test(p, static_cast<const Plane&>(cs));
-			break;
-		case CollisionShape::CST_SPHERE:
-			t = test(p, static_cast<const Sphere&>(cs));
-			break;
-		case CollisionShape::CST_AABB:
-			t = test(p, static_cast<const Aabb&>(cs));
-			break;
-		case CollisionShape::CST_OBB:
-			t = test(p, static_cast<const Obb&>(cs));
-			break;
-		case CollisionShape::CST_PERSPECTIVE_CAMERA_FRUSTRUM:
-			t = test(p, static_cast<const PerspectiveCameraShape&>(cs));
-			break;
-		default:
-			ASSERT(0 && "Forgot something");
-	}
-	return t;
-}
-
-
-//==============================================================================
-float PlaneTests::test(const Plane& p, const LineSegment& ls)
-{
-	const Vec3& p0 = ls.getOrigin();
-	Vec3 p1 = ls.getOrigin() + ls.getDirection();
-
-	float dist0 = p.test(p0);
-	float dist1 = p.test(p1);
-
-	if(dist0 > 0.0)
-	{
-		if(dist1 > 0.0)
-		{
-			return std::min(dist0, dist1);
-		}
-		else
-		{
-			return 0.0;
-		}
-	}
-	else
-	{
-		if(dist1 < 0.0)
-		{
-			return std::max(dist0, dist1);
-		}
-		else
-		{
-			return 0.0;
-		}
-	}
-}
-
-
-//==============================================================================
-float PlaneTests::test(const Plane& p, const Obb& obb)
-{
-	Vec3 xNormal = obb.getRotation().getTransposed() * p.getNormal();
-
-	// maximum extent in direction of plane normal
-	float r = fabs(obb.getExtend().x() * xNormal.x()) +
-		fabs(obb.getExtend().y() * xNormal.y()) +
-		fabs(obb.getExtend().z() * xNormal.z());
-	// signed distance between box center and plane
-	float d = p.test(obb.getCenter());
-
-	// return signed distance
-	if(fabs(d) < r)
-	{
-		return 0.0;
-	}
-	else if(d < 0.0)
-	{
-		return d + r;
-	}
-	else
-	{
-		return d - r;
-	}
-}
-
-
-//==============================================================================
-float PlaneTests::test(const Plane& p, const PerspectiveCameraShape& pcs)
-{
-	float o = 0.0;
-
-	for(uint i = 0; i < 4; i++)
-	{
-		LineSegment ls(pcs.getEye(), pcs.getDirections()[i]);
-		float t = test(p, ls);
-
-		if(t == 0)
-		{
-			return 0.0;
-		}
-		else if(t < 0.0)
-		{
-			o = std::max(o, t);
-		}
-		else
-		{
-			o = std::min(o, t);
-		}
-	}
-
-	return o;
-}
-
-
-//==============================================================================
-float PlaneTests::test(const Plane& /*p*/, const Plane& /*p1*/)
-{
-	ASSERT(0 && "Ambiguous call");
-	return 0.0;
-}
-
-
-//==============================================================================
-float PlaneTests::test(const Plane& p, const Ray& r)
-{
-	float dist = p.test(r.getOrigin());
-	float cos_ = p.getNormal().dot(r.getDirection());
-
-	if(cos_ > 0.0) // the ray points to the same half-space as the plane
-	{
-		if(dist < 0.0) // the ray's origin is behind the plane
-		{
-			return 0.0;
-		}
-		else
-		{
-			return dist;
-		}
-	}
-	else
-	{
-		if(dist > 0.0)
-		{
-			return 0.0;
-		}
-		else
-		{
-			return dist;
-		}
-	}
-}
-
-
-//==============================================================================
-float PlaneTests::test(const Plane& p, const Sphere& s)
-{
-	float dist = p.test(s.getCenter());
-
-	if(dist > s.getRadius())
-	{
-		return dist - s.getRadius();
-	}
-	else if(-dist > s.getRadius())
-	{
-		return dist + s.getRadius();
-	}
-	else
-	{
-		return 0.0;
-	}
-}
-
-
-//==============================================================================
-float PlaneTests::test(const Plane& p, const Aabb& aabb)
-{
-	Vec3 diagMin, diagMax;
-	// set min/max values for x,y,z direction
-	for(int i = 0; i < 3; i++)
-	{
-		if(p.getNormal()[i] >= 0.0)
-		{
-			diagMin[i] = aabb.getMin()[i];
-			diagMax[i] = aabb.getMax()[i];
-		}
-		else
-		{
-			diagMin[i] = aabb.getMax()[i];
-			diagMax[i] = aabb.getMin()[i];
-		}
-	}
-
-	// minimum on positive side of plane, box on positive side
-	float test = p.test(diagMin);
-	if(test > 0.0)
-	{
-		return test;
-	}
-
-	test = p.test(diagMax);
-	// min on non-positive side, max on non-negative side, intersection
-	if(test >= 0.0)
-	{
-		return 0.0;
-	}
-	// max on negative side, box on negative side
-	else
-	{
-		return test;
-	}
-}
-
-
-} // end namespace

+ 0 - 39
anki/collision/PlaneTests.h

@@ -1,39 +0,0 @@
-#ifndef ANKI_COLLISION_PLANE_TESTS_H
-#define ANKI_COLLISION_PLANE_TESTS_H
-
-#include "anki/collision/Forward.h"
-
-
-namespace anki {
-
-
-/// @addtogroup Collision
-/// @{
-///
-/// Contains methods for testing the position between a collision shape and a
-/// plane.
-///
-/// If the collision shape intersects with the plane then the method returns
-/// 0.0, else it returns the distance. If the distance is < 0.0 then the
-/// collision shape lies behind the plane and if > 0.0 then in front of it
-class PlaneTests
-{
-	public:
-		/// Generic method. It doesn't use visitor pattern for speed reasons
-		static float test(const Plane& p, const CollisionShape& cs);
-
-		static float test(const Plane& p, const LineSegment& ls);
-		static float test(const Plane& p, const Obb& obb);
-		static float test(const Plane& p, const PerspectiveCameraShape& pcs);
-		static float test(const Plane& p, const Plane& p1);
-		static float test(const Plane& p, const Ray& r);
-		static float test(const Plane& p, const Sphere& s);
-		static float test(const Plane& p, const Aabb& aabb);
-};
-/// @}
-
-
-} // end namespace
-
-
-#endif

+ 32 - 0
anki/collision/Ray.cpp

@@ -15,4 +15,36 @@ Ray Ray::getTransformed(const Transform& transform) const
 }
 
 
+//==============================================================================
+float Ray::testPlane(const Plane& p) const
+{
+	const Ray& r = *this;
+	float dist = p.test(r.getOrigin());
+	float cos_ = p.getNormal().dot(r.getDirection());
+
+	if(cos_ > 0.0) // the ray points to the same half-space as the plane
+	{
+		if(dist < 0.0) // the ray's origin is behind the plane
+		{
+			return 0.0;
+		}
+		else
+		{
+			return dist;
+		}
+	}
+	else
+	{
+		if(dist > 0.0)
+		{
+			return 0.0;
+		}
+		else
+		{
+			return dist;
+		}
+	}
+}
+
+
 } // end namespace

+ 3 - 6
anki/collision/Ray.h

@@ -70,13 +70,10 @@ class Ray: public CollisionShape
 			v.visit(*this);
 		}
 
-		/// Overrides CollisionShape::testPlane
-		float testPlane(const Plane& p) const
-		{
-			return PlaneTests::test(p, *this);
-		}
+		/// Implements CollisionShape::testPlane
+		float testPlane(const Plane& p) const;
 
-		/// Overrides CollisionShape::transform
+		/// Implements CollisionShape::transform
 		void transform(const Transform& trf)
 		{
 			*this = getTransformed(trf);

+ 21 - 0
anki/collision/Sphere.cpp

@@ -5,6 +5,27 @@
 namespace anki {
 
 
+//==============================================================================
+float Sphere::testPlane(const Plane& p) const
+{
+	const Sphere& s = *this;
+	float dist = p.test(s.getCenter());
+
+	if(dist > s.getRadius())
+	{
+		return dist - s.getRadius();
+	}
+	else if(-dist > s.getRadius())
+	{
+		return dist + s.getRadius();
+	}
+	else
+	{
+		return 0.0;
+	}
+}
+
+
 //==============================================================================
 Sphere Sphere::getTransformed(const Transform& transform) const
 {

+ 3 - 6
anki/collision/Sphere.h

@@ -70,13 +70,10 @@ class Sphere: public CollisionShape
 			v.visit(*this);
 		}
 
-		/// Overrides CollisionShape::testPlane
-		float testPlane(const Plane& p) const
-		{
-			return PlaneTests::test(p, *this);
-		}
+		/// Implements CollisionShape::testPlane
+		float testPlane(const Plane& p) const;
 
-		/// Overrides CollisionShape::transform
+		/// Implements CollisionShape::transform
 		void transform(const Transform& trf)
 		{
 			*this = getTransformed(trf);