Prechádzať zdrojové kódy

Move Plane and Frustum to core/math. Also port Plane to NMF

Daniele Bartolini 12 rokov pred
rodič
commit
963396d435

+ 0 - 1
engine/CMakeLists.txt

@@ -135,7 +135,6 @@ set (MATH_SRC
 	core/math/Frustum.cpp
 	core/math/Matrix3x3.cpp
 	core/math/Matrix4x4.cpp
-	core/math/Plane.cpp
 )
 
 set (MATH_HEADERS

+ 22 - 31
engine/core/math/Frustum.cpp

@@ -29,6 +29,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Intersection.h"
 #include "Matrix4x4.h"
 #include "AABB.h"
+#include "Plane.h"
 
 namespace crown
 {
@@ -52,12 +53,12 @@ Frustum::Frustum(const Frustum& frustum)
 //-----------------------------------------------------------------------------
 bool Frustum::contains_point(const Vector3& point) const
 {
-	if (m_planes[FrustumPlane::LEFT].distance_to_point(point) < 0.0) return false;
-	if (m_planes[FrustumPlane::RIGHT].distance_to_point(point) < 0.0) return false;
-	if (m_planes[FrustumPlane::BOTTOM].distance_to_point(point) < 0.0) return false;
-	if (m_planes[FrustumPlane::TOP].distance_to_point(point) < 0.0) return false;
-	if (m_planes[FrustumPlane::NEAR].distance_to_point(point) < 0.0) return false;
-	if (m_planes[FrustumPlane::FAR].distance_to_point(point) < 0.0) return false;
+	if (plane::distance_to_point(m_planes[FrustumPlane::LEFT], point) < 0.0) return false;
+	if (plane::distance_to_point(m_planes[FrustumPlane::RIGHT], point) < 0.0) return false;
+	if (plane::distance_to_point(m_planes[FrustumPlane::BOTTOM], point) < 0.0) return false;
+	if (plane::distance_to_point(m_planes[FrustumPlane::TOP], point) < 0.0) return false;
+	if (plane::distance_to_point(m_planes[FrustumPlane::NEAR], point) < 0.0) return false;
+	if (plane::distance_to_point(m_planes[FrustumPlane::FAR], point) < 0.0) return false;
 
 	return true;
 }
@@ -80,24 +81,15 @@ Vector3 Frustum::vertex(uint32_t index) const
 
 	switch (index)
 	{
-		case 0:
-			return Intersection::test_plane_3(m_planes[4], m_planes[0], m_planes[2], ip);
-		case 1:
-			return Intersection::test_plane_3(m_planes[4], m_planes[1], m_planes[2], ip);
-		case 2:
-			return Intersection::test_plane_3(m_planes[4], m_planes[1], m_planes[3], ip);
-		case 3:
-			return Intersection::test_plane_3(m_planes[4], m_planes[0], m_planes[3], ip);
-		case 4:
-			return Intersection::test_plane_3(m_planes[5], m_planes[0], m_planes[2], ip);
-		case 5:
-			return Intersection::test_plane_3(m_planes[5], m_planes[1], m_planes[2], ip);
-		case 6:
-			return Intersection::test_plane_3(m_planes[5], m_planes[1], m_planes[3], ip);
-		case 7:
-			return Intersection::test_plane_3(m_planes[5], m_planes[0], m_planes[3], ip);
-		default:
-			break;
+		case 0: return Intersection::test_plane_3(m_planes[4], m_planes[0], m_planes[2], ip);
+		case 1: return Intersection::test_plane_3(m_planes[4], m_planes[1], m_planes[2], ip);
+		case 2: return Intersection::test_plane_3(m_planes[4], m_planes[1], m_planes[3], ip);
+		case 3: return Intersection::test_plane_3(m_planes[4], m_planes[0], m_planes[3], ip);
+		case 4: return Intersection::test_plane_3(m_planes[5], m_planes[0], m_planes[2], ip);
+		case 5: return Intersection::test_plane_3(m_planes[5], m_planes[1], m_planes[2], ip);
+		case 6: return Intersection::test_plane_3(m_planes[5], m_planes[1], m_planes[3], ip);
+		case 7: return Intersection::test_plane_3(m_planes[5], m_planes[0], m_planes[3], ip);
+		default: break;
 	}
 
 	return ip;
@@ -142,12 +134,12 @@ void Frustum::from_matrix(const Matrix4x4& m)
 	m_planes[FrustumPlane::FAR].n.z		= m.m[11] - m.m[10];
 	m_planes[FrustumPlane::FAR].d		= m.m[15] - m.m[14];
 
-	m_planes[FrustumPlane::LEFT].normalize();
-	m_planes[FrustumPlane::RIGHT].normalize();
-	m_planes[FrustumPlane::BOTTOM].normalize();
-	m_planes[FrustumPlane::TOP].normalize();
-	m_planes[FrustumPlane::NEAR].normalize();
-	m_planes[FrustumPlane::FAR].normalize();
+	plane::normalize(m_planes[FrustumPlane::LEFT]);
+	plane::normalize(m_planes[FrustumPlane::RIGHT]);
+	plane::normalize(m_planes[FrustumPlane::BOTTOM]);
+	plane::normalize(m_planes[FrustumPlane::TOP]);
+	plane::normalize(m_planes[FrustumPlane::NEAR]);
+	plane::normalize(m_planes[FrustumPlane::FAR]);
 }
 
 //-----------------------------------------------------------------------------
@@ -172,4 +164,3 @@ AABB Frustum::to_aabb() const
 }
 
 } // namespace crown
-

+ 9 - 6
engine/core/math/Intersection.h

@@ -181,7 +181,7 @@ inline bool Intersection::test_plane_3(const Plane& p1, const Plane& p2, const P
 //-----------------------------------------------------------------------------
 inline bool Intersection::test_static_sphere_plane(const Sphere& s, const Plane& p)
 {
-	if (math::abs(p.distance_to_point(s.center())) < s.radius())
+	if (math::abs(plane::distance_to_point(p, s.center())) < s.radius())
 	{
 		return true;
 	}
@@ -205,7 +205,7 @@ inline bool Intersection::test_dynamic_sphere_plane(const Sphere& s, const Vecto
 	float t0;	// Time at which the sphere int32_tersects the plane remaining at the front side of the plane
 	float t1;	// Time at which the sphere int32_tersects the plane remaining at the back side of the plane
 
-	float sphereToPlaneDistance = p.distance_to_point(sphereCenter);
+	float sphereToPlaneDistance = plane::distance_to_point(p, sphereCenter);
 	float planeNormalDotVelocity = vector3::dot(p.n, d);
 
 	if (planeNormalDotVelocity > 0.0)
@@ -393,17 +393,20 @@ inline bool Intersection::test_dynamic_box_box(const AABB& b1, const Vector3& v1
 //-----------------------------------------------------------------------------
 inline bool Intersection::test_frustum_sphere(const Frustum& f, const Sphere& s)
 {
-	if (f.m_planes[0].distance_to_point(s.center()) < -s.radius() || f.m_planes[1].distance_to_point(s.center()) < -s.radius())
+	if (plane::distance_to_point(f.m_planes[0], s.center()) < -s.radius() ||
+		plane::distance_to_point(f.m_planes[1], s.center()) < -s.radius())
 	{
 		return false;
 	}
 
-	if (f.m_planes[2].distance_to_point(s.center()) < -s.radius() || f.m_planes[3].distance_to_point(s.center()) < -s.radius())
+	if (plane::distance_to_point(f.m_planes[2], s.center()) < -s.radius() ||
+		plane::distance_to_point(f.m_planes[3], s.center()) < -s.radius())
 	{
 		return false;
 	}
 
-	if (f.m_planes[4].distance_to_point(s.center()) < -s.radius() || f.m_planes[5].distance_to_point(s.center()) < -s.radius())
+	if (plane::distance_to_point(f.m_planes[4], s.center()) < -s.radius() ||
+		plane::distance_to_point(f.m_planes[5], s.center()) < -s.radius())
 	{
 		return false;
 	}
@@ -422,7 +425,7 @@ inline bool Intersection::test_frustum_box(const Frustum& f, const AABB& b)
 
 		for (uint32_t j = 0; j < 8; j++)
 		{
-			if (f.m_planes[i].distance_to_point(aabb::vertex(b, j)) < 0.0)
+			if (plane::distance_to_point(f.m_planes[i], aabb::vertex(b, j)) < 0.0)
 			{
 				vertexOutCount++;
 			}

+ 15 - 1
engine/core/math/MathTypes.h

@@ -102,10 +102,24 @@ struct AABB
 	AABB();
 
 	/// Constructs from @a min and @a max.
-	AABB(const Vector3& min, const Vector3& max);			
+	AABB(const Vector3& min, const Vector3& max);
 
 	Vector3 min;
 	Vector3 max;
 };
 
+/// 3D Plane.
+/// The form is ax + by + cz + d = 0
+/// where: d = -vector3::dot(n, p)
+struct Plane
+{
+	/// Does nothing for efficiency.
+	Plane();						
+	Plane(const Vector3& n, float d);
+	Plane(const Plane& p);
+
+	Vector3 n;
+	float d;
+};
+
 } // namespace crown

+ 0 - 85
engine/core/math/Plane.cpp

@@ -1,85 +0,0 @@
-/*
-Copyright (c) 2013 Daniele Bartolini, Michele Rossi
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-#include "MathUtils.h"
-#include "Plane.h"
-#include "Types.h"
-
-namespace crown
-{
-
-const Plane Plane::ZERO = Plane(vector3::ZERO, 0.0);
-const Plane	Plane::XAXIS = Plane(vector3::XAXIS, 0.0);
-const Plane	Plane::YAXIS = Plane(vector3::YAXIS, 0.0);
-const Plane	Plane::ZAXIS = Plane(vector3::ZAXIS, 0.0);
-
-//-----------------------------------------------------------------------------
-Plane::Plane()
-{
-}
-
-//-----------------------------------------------------------------------------
-Plane::Plane(const Plane& p) : n(p.n), d(p.d)
-{
-}
-
-//-----------------------------------------------------------------------------
-Plane::Plane(const Vector3& normal, float dist) : n(normal), d(dist)
-{
-}
-
-//-----------------------------------------------------------------------------
-Plane& Plane::normalize()
-{
-	float len = vector3::length(n);
-
-	if (math::equals(len, (float)0.0))
-	{
-		return *this;
-	}
-
-	len = (float)1.0 / len;
-
-	n *= len;
-	d *= len;
-
-	return *this;
-}
-
-//-----------------------------------------------------------------------------
-float Plane::distance_to_point(const Vector3& p) const
-{
-	return vector3::dot(n, p) + d;
-}
-
-//-----------------------------------------------------------------------------
-bool Plane::contains_point(const Vector3& p) const
-{
-	return math::equals(vector3::dot(n, p) + d, (float)0.0);
-}
-
-} // namespace crown
-

+ 54 - 29
engine/core/math/Plane.h

@@ -29,44 +29,69 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Types.h"
 #include "Sphere.h"
 #include "Vector3.h"
+#include "MathTypes.h"
 
 namespace crown
 {
 
-/// 3D Plane.
-/// 
-/// The form is ax + by + cz + d = 0
-/// where: d = -n.Dot(p)
-struct Plane
+namespace plane
 {
-public:
+	/// Normalizes the plane @æ p and returns its result.
+	Plane& normalize(Plane& p);
 
-	Vector3				n;
-	float				d;
+	/// Returns the signed distance between plane @a p and point @a point.
+	float distance_to_point(const Plane& p, const Vector3& point);
 
-public:
+} // namespace plane
 
-	/// Does nothing for efficiency.
-						Plane();						
-						Plane(const Plane& p);
-
-	/// Constructs from a normal and distance factor						
-						Plane(const Vector3& normal, float dist);		
-
-	/// Normalizes the plane
-	Plane&				normalize();							
-
-	/// Returns the signed distance between point @a p and the plane
-	float				distance_to_point(const Vector3& p) const;	
+namespace plane
+{
+	const Plane ZERO = Plane(vector3::ZERO, 0.0);
+	const Plane	XAXIS = Plane(vector3::XAXIS, 0.0);
+	const Plane	YAXIS = Plane(vector3::YAXIS, 0.0);
+	const Plane	ZAXIS = Plane(vector3::ZAXIS, 0.0);
+
+	//-----------------------------------------------------------------------------
+	inline Plane& normalize(Plane& p)
+	{
+		float len = vector3::length(p.n);
+
+		if (math::equals(len, (float) 0.0))
+		{
+			return p;
+		}
+
+		const float inv_len = (float) 1.0 / len;
+
+		p.n *= inv_len;
+		p.d *= inv_len;
+
+		return p;
+	}
+
+	//-----------------------------------------------------------------------------
+	inline float distance_to_point(const Plane& p, const Vector3& point)
+	{
+		return vector3::dot(p.n, point) + p.d;
+	}
+} // namespace plane
+
+//-----------------------------------------------------------------------------
+inline Plane::Plane()
+{
+	// Do not initialize
+}
 
-	/// Returns whether the plane contains the point @a p	
-	bool				contains_point(const Vector3& p) const;		
+//-----------------------------------------------------------------------------
+inline Plane::Plane(const Plane& p)
+	: n(p.n), d(p.d)
+{
+}
 
-	static const Plane	ZERO;
-	static const Plane	XAXIS;
-	static const Plane	YAXIS;
-	static const Plane	ZAXIS;
-};
+//-----------------------------------------------------------------------------
+inline Plane::Plane(const Vector3& normal, float dist)
+	: n(normal), d(dist)
+{
+}
 
 } // namespace crown
-