Бранимир Караџић 4 lat temu
rodzic
commit
5e8c84618f
3 zmienionych plików z 138 dodań i 76 usunięć
  1. 80 14
      include/bx/bounds.h
  2. 55 0
      include/bx/inline/bounds.inl
  3. 3 62
      src/bounds.cpp

+ 80 - 14
include/bx/bounds.h

@@ -10,6 +10,20 @@
 
 namespace bx
 {
+	///
+	struct Line
+	{
+		Vec3 pos = init::None;
+		Vec3 dir = init::None;
+	};
+
+	///
+	struct LineSegment
+	{
+		Vec3 pos = init::None;
+		Vec3 end = init::None;
+	};
+
 	///
 	struct Aabb
 	{
@@ -20,33 +34,33 @@ namespace bx
 	///
 	struct Capsule
 	{
-		Vec3 pos = init::None;
-		Vec3 end = init::None;
-		float    radius;
+		Vec3  pos = init::None;
+		Vec3  end = init::None;
+		float radius;
 	};
 
 	///
 	struct Cone
 	{
-		Vec3 pos = init::None;
-		Vec3 end = init::None;
-		float    radius;
+		Vec3  pos = init::None;
+		Vec3  end = init::None;
+		float radius;
 	};
 
 	///
 	struct Cylinder
 	{
-		Vec3 pos = init::None;
-		Vec3 end = init::None;
-		float    radius;
+		Vec3  pos = init::None;
+		Vec3  end = init::None;
+		float radius;
 	};
 
 	///
 	struct Disk
 	{
-		Vec3 center = init::None;
-		Vec3 normal = init::None;
-		float    radius;
+		Vec3  center = init::None;
+		Vec3  normal = init::None;
+		float radius;
 	};
 
 	///
@@ -58,8 +72,8 @@ namespace bx
 	///
 	struct Sphere
 	{
-		Vec3 center = init::None;
-		float    radius;
+		Vec3  center = init::None;
+		float radius;
 	};
 
 	///
@@ -84,9 +98,37 @@ namespace bx
 		Plane plane = init::None;
 	};
 
+	///
+	struct Interval
+	{
+		///
+		Interval(float _val);
+
+		///
+		Interval(float _min, float _max);
+
+		///
+		void set(float _val);
+
+		///
+		void setCenter(float _val);
+
+		///
+		void expand(float _val);
+
+		float min;
+		float max;
+	};
+
 	///
 	Vec3 getPointAt(const Ray& _ray, float _t);
 
+	///
+	Vec3 getPointAt(const Line& _line, float _t);
+
+	///
+	Vec3 getPointAt(const LineSegment& _line, float _t);
+
 	///
 	Vec3 getCenter(const Aabb& _aabb);
 
@@ -183,6 +225,30 @@ namespace bx
 	/// Intersect ray / triangle.
 	bool intersect(const Ray& _ray, const Triangle& _triangle, Hit* _hit = NULL);
 
+	///
+	Vec3 closestPoint(const Line& _line, const Vec3& _point);
+
+	///
+	Vec3 closestPoint(const LineSegment& _line, const Vec3& _point);
+
+	///
+	Vec3 closestPoint(const Plane& _plane, const Vec3& _point);
+
+	///
+	Vec3 closestPoint(const Aabb& _aabb, const Vec3& _point);
+
+	///
+	Vec3 closestPoint(const Obb& _obb, const Vec3& _point);
+
+	///
+	Vec3 closestPoint(const Triangle& _triangle, const Vec3& _point);
+
+	///
+	bool overlap(const Interval& _interval, float _pos);
+
+	///
+	bool overlap(const Interval& _intervalA, const Interval& _intervalB);
+
 	///
 	bool overlap(const Aabb& _aabb, const Vec3& _pos);
 

+ 55 - 0
include/bx/inline/bounds.inl

@@ -9,16 +9,71 @@
 
 namespace bx
 {
+	inline Interval::Interval(float _val)
+		: min(_val)
+		, max(_val)
+	{
+	}
+
+	inline Interval::Interval(float _min, float _max)
+		: min(_min)
+		, max(_max)
+	{
+	}
+
+	inline void Interval::set(float _val)
+	{
+		min = _val;
+		max = _val;
+	}
+
+	inline void Interval::setCenter(float _val)
+	{
+		const float extents = (max - min) * 0.5f;
+		min = _val - extents;
+		max = _val + extents;
+	}
+
+	inline void Interval::expand(float _val)
+	{
+		min = bx::min(min, _val);
+		max = bx::max(max, _val);
+	}
+
 	inline Vec3 getPointAt(const Ray& _ray, float _t)
 	{
 		return mad(_ray.dir, _t, _ray.pos);
 	}
 
+	inline Vec3 getPointAt(const Line& _line, float _t)
+	{
+		return mad(_line.dir, _t, _line.pos);
+	}
+
+	inline Vec3 getPointAt(const LineSegment& _line, float _t)
+	{
+		return lerp(_line.pos, _line.end, _t);
+	}
+
 	inline bool intersect(const Ray& _ray, const Plane& _plane, Hit* _hit)
 	{
 		return intersect(_ray, _plane, false, _hit);
 	}
 
+	inline bool overlap(const Interval& _interval, float _t)
+	{
+		return _t > _interval.min
+			&& _t < _interval.max
+			;
+	}
+
+	inline bool overlap(const Interval& _intervalA, const Interval& _intervalB)
+	{
+		return _intervalA.max > _intervalB.min
+			&& _intervalB.max > _intervalA.min
+			;
+	}
+
 	inline bool overlap(const Aabb& _aabb, const Sphere& _sphere)
 	{
 		return overlap(_sphere, _aabb);

+ 3 - 62
src/bounds.cpp

@@ -856,43 +856,6 @@ namespace bx
 		calcPlane(_outPlane, _triangle.v0, _triangle.v1, _triangle.v2);
 	}
 
-	struct Interval
-	{
-		Interval(float _val)
-			: start(_val)
-			, end(_val)
-		{
-		}
-
-		Interval(float _start, float _end)
-			: start(_start)
-			, end(_end)
-		{
-		}
-
-		void set(float _val)
-		{
-			start = _val;
-			end   = _val;
-		}
-
-		void expand(float _val)
-		{
-			start = min(_val, start);
-			end   = max(_val, end);
-		}
-
-		float start;
-		float end;
-	};
-
-	bool overlap(const Interval& _a, const Interval& _b)
-	{
-		return _a.end > _b.start
-			&& _b.end > _a.start
-			;
-	}
-
 	float projectToAxis(const Vec3& _axis, const Vec3& _point)
 	{
 		return dot(_axis, _point);
@@ -913,7 +876,7 @@ namespace bx
 	Interval projectToAxis(const Vec3& _axis, const Aabb& _aabb)
 	{
 		const float extent = abs(projectToAxis(abs(_axis), getExtents(_aabb) ) );
-		const float center =         projectToAxis(    _axis , getCenter (_aabb) );
+		const float center =     projectToAxis(    _axis , getCenter (_aabb) );
 		return
 		{
 			center - extent,
@@ -1057,17 +1020,6 @@ namespace bx
 		return isNearZero(dot(_v, _v) );
 	}
 
-	struct Line
-	{
-		Vec3 pos = init::None;
-		Vec3 dir = init::None;
-	};
-
-	inline Vec3 getPointAt(const Line& _line, float _t)
-	{
-		return mad(_line.dir, _t, _line.pos);
-	}
-
 	bool intersect(Line& _outLine, const Plane& _planeA, const Plane& _planeB)
 	{
 		const Vec3  axb   = cross(_planeA.normal, _planeB.normal);
@@ -1107,17 +1059,6 @@ namespace bx
 		return result;
 	}
 
-	struct LineSegment
-	{
-		Vec3 pos;
-		Vec3 end;
-	};
-
-	inline Vec3 getPointAt(const LineSegment& _line, float _t)
-	{
-		return lerp(_line.pos, _line.end, _t);
-	}
-
 	bool intersect(float& _outTa, float& _outTb, const LineSegment& _a, const LineSegment& _b)
 	{
 		// Reference(s):
@@ -1380,7 +1321,7 @@ namespace bx
 	}
 
 	static void calcObbVertices(
-		Vec3* _outVertices
+		  Vec3* _outVertices
 		, const Vec3& _axisX
 		, const Vec3& _axisY
 		, const Vec3& _axisZ
@@ -1485,7 +1426,7 @@ namespace bx
 			const Vec3 bd = sub(_capsuleB.end, _capsuleB.pos);
 
 			return overlap(
-				Sphere{mad(ad, ta, _capsuleA.pos), _capsuleA.radius}
+				  Sphere{mad(ad, ta, _capsuleA.pos), _capsuleA.radius}
 				, Sphere{mad(bd, tb, _capsuleB.pos), _capsuleB.radius}
 				);
 		}