Skip to content

Commit 9f7c448

Browse files
committed
- added tests for cone_vs_plane
1 parent 96bde09 commit 9f7c448

File tree

2 files changed

+127
-5
lines changed

2 files changed

+127
-5
lines changed

src/lib.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -749,14 +749,14 @@ pub fn capsule_vs_plane<T: Float + FloatOps<T> + SignedNumber + SignedNumberOps<
749749
}
750750

751751
/// return the classification of cone defined by position cp, direction cv with height h and radius at the base of r. vs the plane defined by point x and normal n
752-
pub fn cone_vs_plane<T: Float, V: VecN<T> + Cross<T> + SignedVecN<T> + VecFloatOps<T>>(cp: V, cv: V, h: T, r: T, x: V, n: V) -> Classification {
752+
pub fn cone_vs_plane<T: Float + SignedNumberOps<T>, V: VecN<T> + Cross<T> + Dot<T> + SignedVecN<T> + VecFloatOps<T>>(cp: V, cv: V, h: T, r: T, x: V, n: V) -> Classification {
753753
let tip = cp + cv * h;
754754
let pd = plane_distance(x, n);
755755
// check if the tip and cones extent are on different sides of the plane
756756
let d1 = dot(n, tip) + pd;
757757
// extent from the tip is at the base centre point perp of cv at the radius edge... we need to choose the side toward the plane
758-
let perp = normalize(cross(cross(n, cv), cv));
759-
let extent = cp + perp * r;
758+
let perp = normalize(cross(cross(n, -cv), -cv));
759+
let extent = cp + perp * r * signum(dot(cv, n));
760760
let d2 = dot(n, extent);
761761
if d1 < T::zero() && d2 < T::zero() {
762762
Classification::Behind
@@ -1350,10 +1350,9 @@ pub fn map_to_range<T: Float, X: Base<T>>(v: X, in_start: X, in_end: X, out_star
13501350
// capsule_vs_sphere
13511351
// obb_vs_aabb
13521352
// obb_vs_sphere
1353+
// obb_vs_obb
13531354
// line_vs_sphere
13541355
// line_vs_aabb
1355-
// obb_vs_aabb
1356-
// obb_vs_sphere
13571356

13581357
// TODO c++
13591358
// point inside cone test has no passes

tests/tests.rs

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2766,4 +2766,127 @@ fn swizzle() {
27662766
let mut v4x = vec4f(2.0, 2.0, 2.0, 2.0);
27672767
v4x.set_yxzw(v4);
27682768
assert_eq!(v4x, vec4f(7.0, 8.0, 5.0, 3.0));
2769+
}
2770+
2771+
#[test]
2772+
fn cone_vs_plane_test() {
2773+
// use a simple plane at 0,0,0 +z
2774+
let x = Vec3f::zero();
2775+
let n = Vec3f::unit_z();
2776+
2777+
// behind
2778+
/*
2779+
+z
2780+
^
2781+
|
2782+
--------------------- +x (0, 0, 0)
2783+
cp2 (5,-1)
2784+
cp1 (0,-5)
2785+
*/
2786+
2787+
let cp1 = Vec3f::new(0.0, 0.0, -5.0);
2788+
let cp2 = Vec3f::new(5.0, 0.0, -1.0);
2789+
let r = 4.0;
2790+
let cv = normalize(cp2-cp1);
2791+
let h = dist(cp1, cp2);
2792+
assert_eq!(cone_vs_plane(cp1, cv, h, r, x, n), Classification::Behind);
2793+
2794+
// infront
2795+
/*
2796+
+z
2797+
^ cp2 (2,2)
2798+
| cp1 (1,1)
2799+
--------------------- +x (0, 0, 0)
2800+
*/
2801+
2802+
let cp1 = Vec3f::new(1.0, 0.0, 1.0);
2803+
let cp2 = Vec3f::new(2.0, 0.0, 2.0);
2804+
let r = 0.5;
2805+
let cv = normalize(cp2-cp1);
2806+
let h = dist(cp1, cp2);
2807+
assert_eq!(cone_vs_plane(cp1, cv, h, r, x, n), Classification::Infront);
2808+
2809+
2810+
// intersect fully through the line seg
2811+
/*
2812+
+z
2813+
^ cp2 (5,5)
2814+
|
2815+
--------------------- +x (0, 0, 0)
2816+
2817+
cp1 (0,-1)
2818+
*/
2819+
2820+
let cp1 = Vec3f::new(0.0, 0.0, -1.0);
2821+
let cp2 = Vec3f::new(5.0, 0.0, 5.0);
2822+
let r = 4.0;
2823+
let cv = normalize(cp2-cp1);
2824+
let h = dist(cp1, cp2);
2825+
assert_eq!(cone_vs_plane(cp1, cv, h, r, x, n), Classification::Intersects);
2826+
2827+
// intersect the base on the right side
2828+
/*
2829+
+z
2830+
^
2831+
|
2832+
--------------------- +x (0, 0, 0)
2833+
cp1 (5, -1)
2834+
2835+
cp2 (0,-5)
2836+
*/
2837+
2838+
let cp1 = Vec3f::new(5.0, 0.0, -1.0);
2839+
let cp2 = Vec3f::new(0.0, 0.0, -5.0);
2840+
let r = 10.0;
2841+
let cv = normalize(cp2-cp1);
2842+
let h = dist(cp1, cp2);
2843+
assert_eq!(cone_vs_plane(cp1, cv, h, r, x, n), Classification::Intersects);
2844+
2845+
// intersect the base on the left side
2846+
/*
2847+
+z
2848+
^
2849+
|
2850+
--------------------- +x (0, 0, 0)
2851+
cp1 (5, -1)
2852+
2853+
cp1 (10,-5)
2854+
*/
2855+
2856+
let cp1 = Vec3f::new(5.0, 0.0, -1.0);
2857+
let cp2 = Vec3f::new(10.0, 0.0, -5.0);
2858+
let r = 100.0;
2859+
let cv = normalize(cp2-cp1);
2860+
let h = dist(cp1, cp2);
2861+
assert_eq!(cone_vs_plane(cp1, cv, h, r, x, n), Classification::Intersects);
2862+
2863+
// intersect the base on the right side
2864+
/*
2865+
cp2 (0,5)
2866+
2867+
cp1 (5, 1)
2868+
--------------------- +x (0, 0, 0)
2869+
*/
2870+
2871+
let cp1 = Vec3f::new(5.0, 0.0, 1.0);
2872+
let cp2 = Vec3f::new(0.0, 0.0, 5.0);
2873+
let r = 10.0;
2874+
let cv = normalize(cp2-cp1);
2875+
let h = dist(cp1, cp2);
2876+
assert_eq!(cone_vs_plane(cp1, cv, h, r, x, n), Classification::Intersects);
2877+
2878+
// intersect the base on the left side
2879+
/*
2880+
cp1 (10,5)
2881+
2882+
cp1 (5, 1)
2883+
--------------------- +x (0, 0, 0)
2884+
*/
2885+
2886+
let cp1 = Vec3f::new(5.0, 0.0, 1.0);
2887+
let cp2 = Vec3f::new(10.0, 0.0, 5.0);
2888+
let r = 100.0;
2889+
let cv = normalize(cp2-cp1);
2890+
let h = dist(cp1, cp2);
2891+
assert_eq!(cone_vs_plane(cp1, cv, h, r, x, n), Classification::Intersects);
27692892
}

0 commit comments

Comments
 (0)