commit b23d7ee059c67c42e4f9ce269dfbcc1481ecaaf4
parent cb9250662cfae39b3dd67eb96442f3aa6e26e688
Author: Christophe Coustet <christophe.coustet@meso-star.com>
Date: Thu, 21 Jan 2021 10:30:02 +0100
Fix hit normal on closest_point computation
Diffstat:
3 files changed, 9 insertions(+), 7 deletions(-)
diff --git a/src/s2d.h b/src/s2d.h
@@ -123,7 +123,7 @@ static const struct s2d_vertex_data S2D_VERTEX_DATA_NULL = S2D_VERTEX_DATA_NULL_
/* Intersection point */
struct s2d_hit {
struct s2d_primitive prim; /* Intersected primitive */
- float normal[2]; /* Unormalized geometry normal */
+ float normal[2]; /* Unormalized geometry normal (left hand convention) */
float u; /* Barycentric coordinates onto `prim'. pos = (1 - u)*v0 + u*v1 */
float distance; /* Hit distance from the ray origin */
};
diff --git a/src/s2d_scene_view_closest_point.c b/src/s2d_scene_view_closest_point.c
@@ -121,9 +121,9 @@ closest_point_line_segments
dst = f2_len(vec);
if(dst >= args->query->radius) return 0;
- /* Compute the segment normal */
+ /* Compute the segment normal (left hand convention) */
N[0] = E[1];
- N[1] = E[0];
+ N[1] = -E[0];
/* Flip the geometry normal wrt the flip line_segments flag */
if(geom->flip_contour) f2_minus(N, N);
diff --git a/src/test_s2d_closest_point.c b/src/test_s2d_closest_point.c
@@ -121,8 +121,8 @@ closest_point_line_segments
f2_set(pt->pos, closest_pt);
pt->dst = dst;
pt->iprim = iseg;
- f2_sub(pt->normal, v1, v0);
- SWAP(float, pt->normal[0], pt->normal[1]);
+ pt->normal[0] = v1[1] - v0[1];
+ pt->normal[1] = -v1[0] + v0[0];
f2_normalize(pt->normal, pt->normal);
}
}
@@ -409,7 +409,7 @@ test_single_segment(struct s2d_device* dev)
const float amplitude = exp2f((float)a);
const float eps = 1e-6f * amplitude;
FOR_EACH(i, 0, 1000) {
- float A[2], B[2], AB[2], N[2];
+ float A[2], B[2], AB[2], N[2], hit_N[2];
int n;
/* Randomly generate a segment AB */
@@ -420,7 +420,7 @@ test_single_segment(struct s2d_device* dev)
} while (f2_eq_eps(A, B, eps));
f2_sub(AB, B, A);
- f2(N, -AB[1], AB[0]);
+ f2(N, AB[1], -AB[0]); /* Left hand convention */
f2_normalize(N, N);
f2_set(vertices + 0, A);
@@ -472,6 +472,8 @@ test_single_segment(struct s2d_device* dev)
CHK(f2_eq_eps(attr.value, proj, eps));
CHK(eq_epsf(hit.distance, fabsf(h), eps));
}
+ f2_normalize(hit_N, hit.normal);
+ CHK(f2_eq_eps(N, hit_N, FLT_EPSILON));
}
CHK(s2d_scene_view_ref_put(view) == RES_OK);