commit cb9250662cfae39b3dd67eb96442f3aa6e26e688
parent a3a432d64ee51109e30f871a2c6b9ba38572dbee
Author: Christophe Coustet <christophe.coustet@meso-star.com>
Date: Thu, 14 Jan 2021 17:26:57 +0100
Performance improvement
Diffstat:
1 file changed, 8 insertions(+), 7 deletions(-)
diff --git a/src/s2d_scene_view_closest_point.c b/src/s2d_scene_view_closest_point.c
@@ -53,29 +53,30 @@ closest_point_segment
float* s) /* Parametric coordinate of the closest point onto [v0, v1] */
{
float v[2]; /* Vector from v0 to p */
- float segment_len; /* Length of the [v0, v1] */
- float dst; /* Dst from v0 to the orthogonal projection of p onto [v0, v1] */
+ float segment_len2; /* Square length of the [v0, v1] */
+ float dst_x_seglen; /* |p' v0| x |v0 v1|
+ * p' is the orthogonal projection of p onto [v0, v1] */
ASSERT(p && v0 && v1);
ASSERT(f2_eq(f2_sub(v, v1, v0), E));
/* Orthogonally project the point onto the segment */
f2_sub(v, p, v0);
- segment_len = f2_len(E);
- dst = f2_dot(v, E) / segment_len;
+ dst_x_seglen = f2_dot(v, E);
/* Check if the closest point is the segment vertex 'v0' */
- if(dst <= 0) {
+ if(dst_x_seglen <= 0) {
*s = 0;
return f2_set(closest_pt, v0);
}
/* Check if the closest point is the segment vertex 'v1' */
- if(dst >= segment_len) {
+ segment_len2 = f2_dot(E,E);
+ if(dst_x_seglen >= segment_len2) {
*s = 1;
return f2_set(closest_pt, v1);
}
/* The closest point is on the segment */
- *s = dst / segment_len;
+ *s = dst_x_seglen / segment_len2;
ASSERT(*s == CLAMP(*s, 0, 1));
return f2_add(closest_pt, f2_mulf(closest_pt, E, *s), v0);
}