diff options
author | Campbell Barton <ideasman42@gmail.com> | 2013-11-30 08:57:16 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2013-11-30 08:57:16 +0400 |
commit | 5910531318c02f0f223ce608dcbe0a6e3232828a (patch) | |
tree | 93e619abd11f4849fa78de37a1c7093f16b91755 /source/blender | |
parent | 8aff45d8f671e7eb2f404c8f194e06d88c5147c9 (diff) |
Math Library: add functions cross_poly_v2, cross_tri_v2
also added utility macro for removing elements in the middle of an array
Diffstat (limited to 'source/blender')
-rw-r--r-- | source/blender/blenlib/BLI_math_geom.h | 7 | ||||
-rw-r--r-- | source/blender/blenlib/BLI_utildefines.h | 6 | ||||
-rw-r--r-- | source/blender/blenlib/intern/math_geom.c | 35 | ||||
-rw-r--r-- | source/blender/blenlib/intern/math_geom_inline.c | 17 |
4 files changed, 43 insertions, 22 deletions
diff --git a/source/blender/blenlib/BLI_math_geom.h b/source/blender/blenlib/BLI_math_geom.h index f815716148b..9073fbb8b80 100644 --- a/source/blender/blenlib/BLI_math_geom.h +++ b/source/blender/blenlib/BLI_math_geom.h @@ -54,14 +54,17 @@ void cent_quad_v3(float r[3], const float a[3], const float b[3], const float c[ float normal_tri_v3(float r[3], const float a[3], const float b[3], const float c[3]); float normal_quad_v3(float r[3], const float a[3], const float b[3], const float c[3], const float d[3]); -float area_tri_v2(const float a[2], const float b[2], const float c[2]); -float area_tri_signed_v2(const float v1[2], const float v2[2], const float v3[2]); +MINLINE float area_tri_v2(const float a[2], const float b[2], const float c[2]); +MINLINE float area_tri_signed_v2(const float v1[2], const float v2[2], const float v3[2]); float area_tri_v3(const float a[3], const float b[3], const float c[3]); float area_tri_signed_v3(const float v1[3], const float v2[3], const float v3[3], const float normal[3]); float area_quad_v3(const float a[3], const float b[3], const float c[3], const float d[3]); float area_poly_v3(int nr, float verts[][3], const float normal[3]); float area_poly_v2(int nr, float verts[][2]); +MINLINE float cross_tri_v2(const float v1[2], const float v2[2], const float v3[2]); +float cross_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]); diff --git a/source/blender/blenlib/BLI_utildefines.h b/source/blender/blenlib/BLI_utildefines.h index 0f7c0ac0fd8..db607d6d0c7 100644 --- a/source/blender/blenlib/BLI_utildefines.h +++ b/source/blender/blenlib/BLI_utildefines.h @@ -300,6 +300,12 @@ #define ARRAY_HAS_ITEM(arr_item, arr_start, tot) \ ((unsigned int)((arr_item) - (arr_start)) < (unsigned int)(tot)) +#define ARRAY_DELETE(arr, index, tot_delete, tot) { \ + BLI_assert(index + tot_delete <= tot); \ + memmove(&(arr)[(index)], \ + &(arr)[(index) + (tot_delete)], \ + (((tot) - (index)) - (tot_delete)) * sizeof(*(arr))); \ + } (void)0 /* Warning-free macros for storing ints in pointers. Use these _only_ * for storing an int in a pointer, not a pointer in an int (64bit)! */ diff --git a/source/blender/blenlib/intern/math_geom.c b/source/blender/blenlib/intern/math_geom.c index 0d15eede9ea..45612335323 100644 --- a/source/blender/blenlib/intern/math_geom.c +++ b/source/blender/blenlib/intern/math_geom.c @@ -88,16 +88,6 @@ float normal_quad_v3(float n[3], const float v1[3], const float v2[3], const flo return normalize_v3(n); } -float area_tri_v2(const float v1[2], const float v2[2], const float v3[2]) -{ - return 0.5f * fabsf((v1[0] - v2[0]) * (v2[1] - v3[1]) + (v1[1] - v2[1]) * (v3[0] - v2[0])); -} - -float area_tri_signed_v2(const float v1[2], const float v2[2], const float v3[2]) -{ - return 0.5f * ((v1[0] - v2[0]) * (v2[1] - v3[1]) + (v1[1] - v2[1]) * (v3[0] - v2[0])); -} - /* only convex Quadrilaterals */ float area_quad_v3(const float v1[3], const float v2[3], const float v3[3], const float v4[3]) { @@ -157,30 +147,35 @@ float area_poly_v3(int nr, float verts[][3], const float normal[3]) area = 0.0f; for (a = 0; a < nr; a++) { area += (co_curr[px] - co_prev[px]) * (co_curr[py] + co_prev[py]); - co_prev = verts[a]; - co_curr = verts[a + 1]; + co_prev = co_curr; + co_curr += 3; } return fabsf(0.5f * area / max); } -float area_poly_v2(int nr, float verts[][2]) +float cross_poly_v2(int nr, float verts[][2]) { int a; - float area; - float *co_curr, *co_prev; + float cross; + const float *co_curr, *co_prev; /* The Trapezium Area Rule */ co_prev = verts[nr - 1]; co_curr = verts[0]; - area = 0.0f; + cross = 0.0f; for (a = 0; a < nr; a++) { - area += (co_curr[0] - co_prev[0]) * (co_curr[1] + co_prev[1]); - co_prev = verts[a]; - co_curr = verts[a + 1]; + cross += (co_curr[0] - co_prev[0]) * (co_curr[1] + co_prev[1]); + co_prev = co_curr; + co_curr += 2; } - return fabsf(0.5f * area); + return cross; +} + +float area_poly_v2(int nr, float verts[][2]) +{ + return fabsf(0.5f * cross_poly_v2(nr, verts)); } /********************************* Planes **********************************/ diff --git a/source/blender/blenlib/intern/math_geom_inline.c b/source/blender/blenlib/intern/math_geom_inline.c index ac5c0033067..1910b964d78 100644 --- a/source/blender/blenlib/intern/math_geom_inline.c +++ b/source/blender/blenlib/intern/math_geom_inline.c @@ -34,6 +34,23 @@ #include <string.h> +/********************************** Polygons *********************************/ + +MINLINE float cross_tri_v2(const float v1[2], const float v2[2], const float v3[2]) +{ + return (v1[0] - v2[0]) * (v2[1] - v3[1]) + (v1[1] - v2[1]) * (v3[0] - v2[0]); +} + +MINLINE float area_tri_signed_v2(const float v1[2], const float v2[2], const float v3[2]) +{ + return 0.5f * ((v1[0] - v2[0]) * (v2[1] - v3[1]) + (v1[1] - v2[1]) * (v3[0] - v2[0])); +} + +MINLINE float area_tri_v2(const float v1[2], const float v2[2], const float v3[2]) +{ + return fabsf(area_tri_signed_v2(v1, v2, v3)); +} + /****************************** Spherical Harmonics **************************/ MINLINE void zero_sh(float r[9]) |