diff options
author | Campbell Barton <ideasman42@gmail.com> | 2014-07-21 10:58:17 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2014-07-21 10:58:17 +0400 |
commit | ce47231cdb961fbd15cb0b403db9577c0e1c0010 (patch) | |
tree | aaf3d452eb89717894dedc6175a121b796a5b21d /source/blender/blenlib | |
parent | cbde56572f9848b0bca34ae5ce3743e446d750d5 (diff) |
Math Lib: Add isect_point_tri_v3
Add to Python via mathutils.geometry
Diffstat (limited to 'source/blender/blenlib')
-rw-r--r-- | source/blender/blenlib/BLI_math_geom.h | 2 | ||||
-rw-r--r-- | source/blender/blenlib/intern/math_geom.c | 33 |
2 files changed, 35 insertions, 0 deletions
diff --git a/source/blender/blenlib/BLI_math_geom.h b/source/blender/blenlib/BLI_math_geom.h index ed0777aceea..57cde5637d3 100644 --- a/source/blender/blenlib/BLI_math_geom.h +++ b/source/blender/blenlib/BLI_math_geom.h @@ -169,6 +169,8 @@ int isect_point_tri_v2(const float pt[2], const float v1[2], const float v2[2], bool isect_point_tri_v2_cw(const float pt[2], const float v1[2], const float v2[2], const float v3[2]); int isect_point_tri_v2_int(const int x1, const int y1, const int x2, const int y2, const int a, const int b); bool isect_point_tri_prism_v3(const float p[3], const float v1[3], const float v2[3], const float v3[3]); +bool isect_point_tri_v3(const float p[3], const float v1[3], const float v2[3], const float v3[3], + float r_vi[3]); /* axis-aligned bounding box */ bool isect_aabb_aabb_v3(const float min1[3], const float max1[3], const float min2[3], const float max2[3]); diff --git a/source/blender/blenlib/intern/math_geom.c b/source/blender/blenlib/intern/math_geom.c index efb881cde1b..2cd32b3dca9 100644 --- a/source/blender/blenlib/intern/math_geom.c +++ b/source/blender/blenlib/intern/math_geom.c @@ -1910,6 +1910,39 @@ bool isect_point_tri_prism_v3(const float p[3], const float v1[3], const float v return 1; } +/** + * \param r_vi The point \a p projected onto the triangle. + * \return True when \a p is inside the triangle. + * \note Its up to the caller to check the distance between \a p and \a r_vi against an error margin. + */ +bool isect_point_tri_v3(const float p[3], const float v1[3], const float v2[3], const float v3[3], + float r_vi[3]) +{ + if (isect_point_tri_prism_v3(p, v1, v2, v3)) { + float no[3], n1[3], n2[3]; + + /* Could use normal_tri_v3, but doesn't have to be unit-length */ + sub_v3_v3v3(n1, v1, v2); + sub_v3_v3v3(n2, v2, v3); + cross_v3_v3v3(no, n1, n2); + + if (LIKELY(len_squared_v3(no) != 0.0f)) { + float plane[4]; + plane_from_point_normal_v3(plane, v1, no); + closest_to_plane_v3(r_vi, plane, p); + } + else { + /* degenerate */ + copy_v3_v3(r_vi, p); + } + + return true; + } + else { + return false; + } +} + bool clip_segment_v3_plane(float p1[3], float p2[3], const float plane[4]) { float dp[3], div, t, pc[3]; |