Daniele Bartolini hace 11 años
padre
commit
79d6569675
Se han modificado 3 ficheros con 59 adiciones y 98 borrados
  1. 9 9
      engine/core/math/intersection.cpp
  2. 10 0
      engine/core/math/math_types.h
  3. 40 89
      engine/core/math/sphere.h

+ 9 - 9
engine/core/math/intersection.cpp

@@ -52,11 +52,11 @@ namespace math
 	/// -1.0f if no collision.
 	float ray_sphere_intersection(const Vector3& from, const Vector3& dir, const Sphere& s)
 	{
-		Vector3 v = s.center() - from;
+		Vector3 v = s.c - from;
 		float b = vector3::dot(v, dir);
-		float det = (s.radius() * s.radius()) - vector3::dot(v, v) + (b * b);
+		float det = (s.r * s.r) - vector3::dot(v, v) + (b * b);
 
-		if (det < 0.0 || b < s.radius())
+		if (det < 0.0 || b < s.r)
 		{
 			return -1.0f;
 		}
@@ -184,20 +184,20 @@ namespace math
 
 	bool frustum_sphere_intersection(const Frustum& f, const Sphere& s)
 	{
-		if (plane::distance_to_point(f.left, s.center()) < -s.radius() ||
-			plane::distance_to_point(f.right, s.center()) < -s.radius())
+		if (plane::distance_to_point(f.left, s.c) < -s.r ||
+			plane::distance_to_point(f.right, s.c) < -s.r)
 		{
 			return false;
 		}
 
-		if (plane::distance_to_point(f.bottom, s.center()) < -s.radius() ||
-			plane::distance_to_point(f.top, s.center()) < -s.radius())
+		if (plane::distance_to_point(f.bottom, s.c) < -s.r ||
+			plane::distance_to_point(f.top, s.c) < -s.r)
 		{
 			return false;
 		}
 
-		if (plane::distance_to_point(f.near, s.center()) < -s.radius() ||
-			plane::distance_to_point(f.far, s.center()) < -s.radius())
+		if (plane::distance_to_point(f.near, s.c) < -s.r ||
+			plane::distance_to_point(f.far, s.c) < -s.r)
 		{
 			return false;
 		}

+ 10 - 0
engine/core/math/math_types.h

@@ -204,4 +204,14 @@ struct Frustum
 	Plane far;
 };
 
+/// @ingroup Math
+struct Sphere
+{
+	Sphere();
+	Sphere(const Vector3& nc, float nr);
+
+	Vector3 c;
+	float r;
+};
+
 } // namespace crown

+ 40 - 89
engine/core/math/sphere.h

@@ -28,121 +28,72 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #include "types.h"
 #include "math_utils.h"
+#include "math_types.h"
 #include "vector3.h"
 
 namespace crown
 {
-
-/// Sphere.
-///
-/// Used mainly for collision detection and intersection tests.
-class Sphere
+namespace sphere
 {
-public:
-
-	/// Does nothing for efficiency.
-	Sphere();
-
-	/// Constructs from @a center and @a radius.
-	Sphere(const Vector3& center, float radius);
-	Sphere(const Sphere& a);
+	float volume(const Sphere& s);
 
-	const Vector3& center() const;
-	float radius() const;
-	float volume() const;
+	/// Adds @a num @a points to the sphere expanding if necessary.
+	void add_points(Sphere& s, uint32_t num, const Vector3* points);
 
-	void set_c(const Vector3& center);
-	void set_radius(float radius);
-
-	/// Adds @a count @a points to the sphere expanding if necessary.
-	void add_points(const Vector3* points, uint32_t count);
-
-	/// Adds @a count @a spheres expanding if necessary.
-	void add_spheres(const Sphere* spheres, uint32_t count);
+	/// Adds @a num @a spheres expanding if necessary.
+	void add_spheres(Sphere& s, uint32_t num, const Sphere* spheres);
 
 	/// Returns whether point @a p is contained into the sphere.
-	bool contains_point(const Vector3& p) const;
-
-private:
-
-	Vector3 _c;
-	float _r;
-};
-
-inline Sphere::Sphere()
-{
-}
-
-inline Sphere::Sphere(const Vector3& center, float radius) : _c(center), _r(radius)
-{
-}
-
-inline Sphere::Sphere(const Sphere& a) : _c(a._c), _r(a._r)
-{
-}
-
-inline const Vector3& Sphere::center() const
-{
-	return _c;
-}
-
-inline float Sphere::radius() const
-{
-	return _r;
-}
+	bool contains_point(const Sphere& s, const Vector3& p);
+} // namespace sphere
 
-inline float Sphere::volume() const
+namespace sphere
 {
-	return float(4.0 / 3.0 * math::PI) * _r * _r * _r;
-}
-
-inline void Sphere::set_c(const Vector3& center)
-{
-	_c = center;
-}
-
-inline void Sphere::set_radius(float radius)
-{
-	_r = radius;
-}
-
-inline void Sphere::add_points(const Vector3* points, uint32_t count)
-{
-	for (uint32_t i = 0; i < count; i++)
+	inline float volume(const Sphere& s)
 	{
-		const Vector3& p = points[i];
-
-		float dist = vector3::squared_length(p - _c);
+		return float(4.0 / 3.0 * math::PI) * s.r*s.r*s.r;
+	}
 
-		if (dist >= _r * _r)
+	inline void add_points(Sphere& s, uint32_t num, const Vector3* points)
+	{
+		for (uint32_t i = 0; i < num; ++i)
 		{
-			_r = math::sqrt(dist);
+			const float dist = vector3::squared_length(points[i] - s.c);
+			if (dist >= s.r*s.r)
+				s.r = math::sqrt(dist);
 		}
 	}
-}
 
-inline void Sphere::add_spheres(const Sphere* spheres, uint32_t count)
-{
-	for (uint32_t i = 0; i < count; i++)
+	inline void add_spheres(Sphere& s, uint32_t num, const Sphere* spheres)
 	{
-		const Sphere& s = spheres[i];
-
-		float dist = vector3::squared_length(s._c - _c);
-
-		if (dist < (s._r + _r) * (s._r + _r))
+		for (uint32_t i = 0; i < num; ++i)
 		{
-			if (s._r * s._r > _r * _r)
+			const float dist = vector3::squared_length(spheres[i].c - s.c);
+
+			if (dist < (spheres[i].r + s.r) * (spheres[i].r + s.r))
 			{
-				_r = math::sqrt(dist + s._r * s._r);
+				if (spheres[i].r * spheres[i].r > s.r * s.r)
+					s.r = math::sqrt(dist + spheres[i].r * spheres[i].r);
 			}
 		}
 	}
+
+	inline bool contains_point(const Sphere& s, const Vector3& p)
+	{
+		float dist = vector3::squared_length(p - s.c);
+		return dist < s.r*s.r;
+	}
+} // namespace sphere
+
+inline Sphere::Sphere()
+{
+	// Do nothing
 }
 
-inline bool Sphere::contains_point(const Vector3& p) const
+inline Sphere::Sphere(const Vector3& nc, float nr)
+	: c(nc)
+	, r(nr)
 {
-	float dist = vector3::squared_length(p - _c);
-	return (dist < _r * _r);
 }
 
 } // namespace crown