diff options
author | Campbell Barton <ideasman42@gmail.com> | 2011-05-19 07:49:57 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2011-05-19 07:49:57 +0400 |
commit | 391dbde03da940ec8722f144ec2234b9743ffefc (patch) | |
tree | e576a13e5743f21b54c844d07c165e93023ebc72 /source/blender/blenlib | |
parent | c04f9b779c4384f3cc6961b2ed54456bdab14f43 (diff) |
added math function isect_line_plane_v3(), use for window_to_3d rather then having it inline.
Diffstat (limited to 'source/blender/blenlib')
-rw-r--r-- | source/blender/blenlib/BLI_math_geom.h | 12 | ||||
-rw-r--r-- | source/blender/blenlib/intern/math_geom.c | 48 |
2 files changed, 56 insertions, 4 deletions
diff --git a/source/blender/blenlib/BLI_math_geom.h b/source/blender/blenlib/BLI_math_geom.h index 4a7d749842d..634634f02e5 100644 --- a/source/blender/blenlib/BLI_math_geom.h +++ b/source/blender/blenlib/BLI_math_geom.h @@ -96,6 +96,18 @@ int isect_line_line_strict_v3(const float v1[3], const float v2[3], int isect_ray_plane_v3(float p1[3], float d[3], float v0[3], float v1[3], float v2[3], float *lambda, int clip); +/** + * Intersect line/plane, optionally treat line as directional (like a ray) with the no_flip argument. + * @param out The intersection point. + * @param l1 The first point of the line. + * @param l2 The second point of the line. + * @param plane_co A point on the plane to intersect with. + * @param plane_no The direction of the plane (does not need to be normalized). + * @param no_flip When true, the intersection point will always be from l1 to l2, even if this is not on the plane. + */ +int isect_line_plane_v3(float out[3], const float l1[3], const float l2[3], + const float plane_co[3], const float plane_no[3], const short no_flip); + /* line/ray triangle */ int isect_line_tri_v3(const float p1[3], const float p2[3], const float v0[3], const float v1[3], const float v2[3], float *lambda, float uv[2]); diff --git a/source/blender/blenlib/intern/math_geom.c b/source/blender/blenlib/intern/math_geom.c index 8785415c6a1..5979a24c807 100644 --- a/source/blender/blenlib/intern/math_geom.c +++ b/source/blender/blenlib/intern/math_geom.c @@ -37,7 +37,7 @@ #include "BLI_memarena.h" #include "BLI_utildefines.h" - +static float lambda_cp_line(const float p[3], const float l1[3], const float l2[3]); /********************************** Polygons *********************************/ @@ -640,6 +640,48 @@ int isect_ray_tri_threshold_v3(const float p1[3], const float d[3], const float return 1; } +int isect_line_plane_v3(float out[3], const float l1[3], const float l2[3], const float plane_co[3], const float plane_no[3], const short no_flip) +{ + float l_vec[3]; /* l1 -> l2 normalized vector */ + float p_no[3]; /* 'plane_no' normalized */ + float dot; + + sub_v3_v3v3(l_vec, l2, l1); + + normalize_v3(l_vec); + normalize_v3_v3(p_no, plane_no); + + dot= dot_v3v3(l_vec, p_no); + if(dot == 0.0f) { + return 0; + } + else { + float l1_plane[3]; /* line point aligned with the plane */ + float dist; /* 'plane_no' aligned distance to the 'plane_co' */ + + /* for pradictable flipping since the plane is only used to + * define a direction, ignore its flipping and aligned with 'l_vec' */ + if(dot < 0.0f) { + dot= -dot; + negate_v3(p_no); + } + + add_v3_v3v3(l1_plane, l1, p_no); + + dist = lambda_cp_line(plane_co, l1, l1_plane); + + /* treat line like a ray, when 'no_flip' is set */ + if(no_flip && dist < 0.0f) { + dist= -dist; + } + + mul_v3_fl(l_vec, dist / dot); + + add_v3_v3v3(out, l1, l_vec); + + return 1; + } +} /* Adapted from the paper by Kasper Fauerby */ /* "Improved Collision detection and Response" */ @@ -1075,16 +1117,14 @@ float closest_to_line_v2(float cp[2],const float p[2], const float l1[2], const return lambda; } -#if 0 /* little sister we only need to know lambda */ -static float lambda_cp_line(float p[3], float l1[3], float l2[3]) +static float lambda_cp_line(const float p[3], const float l1[3], const float l2[3]) { float h[3],u[3]; sub_v3_v3v3(u, l2, l1); sub_v3_v3v3(h, p, l1); return(dot_v3v3(u,h)/dot_v3v3(u,u)); } -#endif /* Similar to LineIntersectsTriangleUV, except it operates on a quad and in 2d, assumes point is in quad */ void isect_point_quad_uv_v2(const float v0[2], const float v1[2], const float v2[2], const float v3[2], const float pt[2], float *uv) |