diff options
author | Campbell Barton <ideasman42@gmail.com> | 2013-08-22 19:30:24 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2013-08-22 19:30:24 +0400 |
commit | 2ee6c0ac6b774817d760e1e756e97eea476d412e (patch) | |
tree | a7b0807f16ae5838c981d224d5774f81de8a8e54 | |
parent | 29c8d3e20de92577d9067b70c6745bf1f0654e9c (diff) |
add utility functions for dealing with planes
- plane_point_side_v3(), a bit like line_point_side_v2()
- isect_point_planes_v3(), moved from paint_hide.c
functions to convert between point/normal pairs.
- plane_from_point_normal_v3()
- plane_to_point_normal_v3()
-rw-r--r-- | source/blender/blenlib/BLI_math_geom.h | 9 | ||||
-rw-r--r-- | source/blender/blenlib/intern/math_geom.c | 43 | ||||
-rw-r--r-- | source/blender/blenlib/intern/math_geom_inline.c | 5 | ||||
-rw-r--r-- | source/blender/editors/sculpt_paint/paint_hide.c | 14 | ||||
-rw-r--r-- | source/blender/editors/space_view3d/view3d_draw.c | 8 |
5 files changed, 59 insertions, 20 deletions
diff --git a/source/blender/blenlib/BLI_math_geom.h b/source/blender/blenlib/BLI_math_geom.h index 4c98c4be897..51b89df0e4e 100644 --- a/source/blender/blenlib/BLI_math_geom.h +++ b/source/blender/blenlib/BLI_math_geom.h @@ -61,6 +61,14 @@ float area_quad_v3(const float a[3], const float b[3], const float c[3], const f float area_poly_v3(int nr, float verts[][3], const float normal[3]); float area_poly_v2(int nr, float verts[][2]); +/********************************* Planes **********************************/ + +void plane_from_point_normal_v3(float r_plane[4], const float plane_co[3], const float plane_no[3]); +void plane_to_point_normal_v3(const float plane[4], float r_plane_co[3], float r_plane_no[3]); +MINLINE float plane_point_side_v3(const float plane[4], const float co[3]); + +/********************************* Volume **********************************/ + float volume_tetrahedron_v3(const float v1[3], const float v2[3], const float v3[3], const float v4[3]); int is_quad_convex_v3(const float v1[3], const float v2[3], const float v3[3], const float v4[3]); @@ -120,6 +128,7 @@ bool isect_ray_plane_v3(const float p1[3], const float d[3], const float v0[3], const float v1[3], const float v2[3], float *r_lambda, const int clip); +bool isect_point_planes_v3(float (*planes)[4], int totplane, const float p[3]); bool 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 bool no_flip); diff --git a/source/blender/blenlib/intern/math_geom.c b/source/blender/blenlib/intern/math_geom.c index c0457d45f34..11b50802a22 100644 --- a/source/blender/blenlib/intern/math_geom.c +++ b/source/blender/blenlib/intern/math_geom.c @@ -183,6 +183,28 @@ float area_poly_v2(int nr, float verts[][2]) return fabsf(0.5f * area); } +/********************************* Planes **********************************/ + +/** + * Calculate a plane from a point and a direction, + * \note \a point_no isn't required to be normalized. + */ +void plane_from_point_normal_v3(float r_plane[4], const float plane_co[3], const float plane_no[3]) +{ + copy_v3_v3(r_plane, plane_no); + r_plane[3] = -dot_v3v3(r_plane, plane_co); +} + +/** + * Get a point and a normal from a plane. + */ +void plane_to_point_normal_v3(const float plane[4], float r_plane_co[3], float r_plane_no[3]) +{ + const float length = normalize_v3_v3(r_plane_no, plane); + madd_v3_v3v3fl(r_plane_co, r_plane_no, r_plane_no, (-plane[3] / length) - 1.0f); +} + + /********************************* Volume **********************************/ /** @@ -1043,6 +1065,22 @@ bool isect_ray_tri_threshold_v3(const float p1[3], const float d[3], } /** + * Check if a point is behind all planes. + */ +bool isect_point_planes_v3(float (*planes)[4], int totplane, const float p[3]) +{ + int i; + + for (i = 0; i < totplane; i++) { + if (plane_point_side_v3(planes[i], p) > 0.0f) { + return false; + } + } + + return true; +} + +/** * Intersect line/plane, optionally treat line as directional (like a ray) with the no_flip argument. * * \param out The intersection point. @@ -1757,7 +1795,7 @@ bool clip_segment_v3_plane(float p1[3], float p2[3], const float plane[4]) if (div == 0.0f) /* parallel */ return 1; - t = -(dot_v3v3(p1, plane) + plane[3]) / div; + t = -plane_point_side_v3(plane, p1) / div; if (div > 0.0f) { /* behind plane, completely clipped */ @@ -1811,7 +1849,7 @@ bool clip_segment_v3_plane_n(float r_p1[3], float r_p2[3], float plane_array[][4 const float div = dot_v3v3(dp, plane); if (div != 0.0f) { - const float t = -(dot_v3v3(p1, plane) + plane[3]) / div; + const float t = -plane_point_side_v3(plane, p1) / div; if (div > 0.0f) { /* clip a */ if (t >= 1.0f) { @@ -3616,4 +3654,3 @@ int is_quad_convex_v2(const float v1[2], const float v2[2], const float v3[2], c /* linetests, the 2 diagonals have to instersect to be convex */ return (isect_line_line_v2(v1, v3, v2, v4) > 0); } - diff --git a/source/blender/blenlib/intern/math_geom_inline.c b/source/blender/blenlib/intern/math_geom_inline.c index f32b477787b..ac5c0033067 100644 --- a/source/blender/blenlib/intern/math_geom_inline.c +++ b/source/blender/blenlib/intern/math_geom_inline.c @@ -185,4 +185,9 @@ MINLINE int poly_to_tri_count(const int poly_count, const int corner_count) } } +MINLINE float plane_point_side_v3(const float plane[4], const float co[3]) +{ + return dot_v3v3(co, plane) + plane[3]; +} + #endif /* __MATH_GEOM_INLINE_C__ */ diff --git a/source/blender/editors/sculpt_paint/paint_hide.c b/source/blender/editors/sculpt_paint/paint_hide.c index db6380e920f..fc31aacffcf 100644 --- a/source/blender/editors/sculpt_paint/paint_hide.c +++ b/source/blender/editors/sculpt_paint/paint_hide.c @@ -71,18 +71,6 @@ #include <assert.h> -static int planes_contain_v3(float (*planes)[4], int totplane, const float p[3]) -{ - int i; - - for (i = 0; i < totplane; i++) { - if (dot_v3v3(planes[i], p) + planes[i][3] > 0) - return 0; - } - - return 1; -} - /* return true if the element should be hidden/shown */ static int is_effected(PartialVisArea area, float planes[4][4], @@ -95,7 +83,7 @@ static int is_effected(PartialVisArea area, return mask > 0.5f; } else { - int inside = planes_contain_v3(planes, 4, co); + bool inside = isect_point_planes_v3(planes, 4, co); return ((inside && area == PARTIALVIS_INSIDE) || (!inside && area == PARTIALVIS_OUTSIDE)); } diff --git a/source/blender/editors/space_view3d/view3d_draw.c b/source/blender/editors/space_view3d/view3d_draw.c index 9f6ad4a2d39..ccb53468ca9 100644 --- a/source/blender/editors/space_view3d/view3d_draw.c +++ b/source/blender/editors/space_view3d/view3d_draw.c @@ -220,10 +220,10 @@ void ED_view3d_clipping_enable(void) static bool view3d_clipping_test(const float co[3], float clip[6][4]) { - if (0.0f < clip[0][3] + dot_v3v3(co, clip[0])) - if (0.0f < clip[1][3] + dot_v3v3(co, clip[1])) - if (0.0f < clip[2][3] + dot_v3v3(co, clip[2])) - if (0.0f < clip[3][3] + dot_v3v3(co, clip[3])) + if (plane_point_side_v3(clip[0], co) > 0.0f) + if (plane_point_side_v3(clip[1], co) > 0.0f) + if (plane_point_side_v3(clip[2], co) > 0.0f) + if (plane_point_side_v3(clip[3], co) > 0.0f) return false; return true; |