diff options
author | Campbell Barton <ideasman42@gmail.com> | 2009-12-27 04:32:58 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2009-12-27 04:32:58 +0300 |
commit | fde4686d77dd72ccfa88febdea360136bfd17a36 (patch) | |
tree | 52a715e72488c70457f5c29a74e24c0e3977e470 /source/blender/blenlib | |
parent | 9c82e1efc32bdff04c3136ef5a2959ed73711ca7 (diff) |
barycentric transform utility geometry function.
From 2 triangles and 1 point, the relative position between the point and the first triangle is applied to the second triangle to find the target point.
the barycentric weights are calculated in 2D space with a signed area so values outside the triangle bounds are supported.
wrapped by python:
pt_to = Geometry.BarycentricTransform(pt_from, t1a, t1b, t1c, t2a, t1b, t1c)
NOTE:
- moved some barycentric weight functions out of projection painting into the math lib.
- ended up making some of the math functions use const args.
TODO:
- support exceptional cases. zero area tries and similar.
Diffstat (limited to 'source/blender/blenlib')
-rw-r--r-- | source/blender/blenlib/BLI_math_geom.h | 18 | ||||
-rw-r--r-- | source/blender/blenlib/BLI_math_rotation.h | 3 | ||||
-rw-r--r-- | source/blender/blenlib/BLI_math_vector.h | 20 | ||||
-rw-r--r-- | source/blender/blenlib/intern/math_geom.c | 88 | ||||
-rw-r--r-- | source/blender/blenlib/intern/math_rotation.c | 2 | ||||
-rw-r--r-- | source/blender/blenlib/intern/math_vector.c | 12 | ||||
-rw-r--r-- | source/blender/blenlib/intern/math_vector_inline.c | 8 |
7 files changed, 118 insertions, 33 deletions
diff --git a/source/blender/blenlib/BLI_math_geom.h b/source/blender/blenlib/BLI_math_geom.h index 49d335f6c5c..e676001fba5 100644 --- a/source/blender/blenlib/BLI_math_geom.h +++ b/source/blender/blenlib/BLI_math_geom.h @@ -37,12 +37,13 @@ extern "C" { void cent_tri_v3(float r[3], float a[3], float b[3], float c[3]); void cent_quad_v3(float r[3], float a[3], float b[3], float c[3], float d[3]); -float normal_tri_v3(float r[3], float a[3], float b[3], float c[3]); -float normal_quad_v3(float r[3], float a[3], float b[3], float c[3], float d[3]); +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(float a[2], float b[2], float c[2]); -float area_tri_v3(float a[3], float b[3], float c[3]); -float area_quad_v3(float a[3], float b[3], float c[3], 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]); +float area_tri_v3(const float a[3], const float b[3], const float c[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], float normal[3]); /********************************* Distance **********************************/ @@ -120,6 +121,13 @@ void interp_weights_poly_v3(float w[], float v[][3], int n, float p[3]); void interp_cubic_v3(float x[3], float v[3], float x1[3], float v1[3], float x2[3], float v2[3], float t); +void barycentric_transform(float pt_tar[3], float const pt_src[3], + const float tri_tar_p1[3], const float tri_tar_p2[3], const float tri_tar_p3[3], + const float tri_src_p1[3], const float tri_src_p2[3], const float tri_src_p3[3]); + +void barycentric_weights_v2(const float v1[2], const float v2[2], const float v3[2], + const float co[2], float w[3]); + /***************************** View & Projection *****************************/ void lookat_m4(float mat[4][4], float vx, float vy, diff --git a/source/blender/blenlib/BLI_math_rotation.h b/source/blender/blenlib/BLI_math_rotation.h index b221d89487f..b640cf6dc3c 100644 --- a/source/blender/blenlib/BLI_math_rotation.h +++ b/source/blender/blenlib/BLI_math_rotation.h @@ -70,7 +70,8 @@ void mat3_to_quat(float q[4], float mat[3][3]); void mat4_to_quat(float q[4], float mat[4][4]); void tri_to_quat(float q[4], float a[3], float b[3], float c[3]); void vec_to_quat(float q[4], float vec[3], short axis, short upflag); -void rotation_between_vecs_to_quat(float q[4], float v1[3], float v2[3]); +/* note: v1 and v2 must be normalized */ +void rotation_between_vecs_to_quat(float q[4], const float v1[3], const float v2[3]); /* TODO: don't what this is, but it's not the same as mat3_to_quat */ void mat3_to_quat_is_ok(float q[4], float mat[3][3]); diff --git a/source/blender/blenlib/BLI_math_vector.h b/source/blender/blenlib/BLI_math_vector.h index e915a9a85f3..26e7ff5abe9 100644 --- a/source/blender/blenlib/BLI_math_vector.h +++ b/source/blender/blenlib/BLI_math_vector.h @@ -51,8 +51,8 @@ extern "C" { MINLINE void zero_v2(float r[2]); MINLINE void zero_v3(float r[3]); -MINLINE void copy_v2_v2(float r[2], float a[2]); -MINLINE void copy_v3_v3(float r[3], float a[3]); +MINLINE void copy_v2_v2(float r[2], const float a[2]); +MINLINE void copy_v3_v3(float r[3], const float a[3]); MINLINE void swap_v2_v2(float a[2], float b[2]); MINLINE void swap_v3_v3(float a[3], float b[3]); @@ -67,7 +67,7 @@ MINLINE void add_v3_v3v3(float r[3], float a[3], float b[3]); MINLINE void sub_v2_v2(float r[2], float a[2]); MINLINE void sub_v2_v2v2(float r[2], float a[2], float b[2]); MINLINE void sub_v3_v3(float r[3], float a[3]); -MINLINE void sub_v3_v3v3(float r[3], float a[3], float b[3]); +MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3]); MINLINE void mul_v2_fl(float r[2], float f); MINLINE void mul_v3_fl(float r[3], float f); @@ -87,7 +87,7 @@ MINLINE float dot_v2v2(float a[2], float b[2]); MINLINE float dot_v3v3(float a[3], float b[3]); MINLINE float cross_v2v2(float a[2], float b[2]); -MINLINE void cross_v3_v3v3(float r[3], float a[3], float b[3]); +MINLINE void cross_v3_v3v3(float r[3], const float a[3], const float b[3]); MINLINE void star_m3_v3(float R[3][3],float a[3]); @@ -103,11 +103,11 @@ MINLINE float normalize_v3(float r[3]); /******************************* Interpolation *******************************/ -void interp_v2_v2v2(float r[2], float a[2], float b[2], float t); -void interp_v2_v2v2v2(float r[2], float a[2], float b[2], float c[3], float t[3]); -void interp_v3_v3v3(float r[3], float a[3], float b[3], float t); -void interp_v3_v3v3v3(float p[3], float v1[3], float v2[3], float v3[3], float w[3]); -void interp_v3_v3v3v3v3(float p[3], float v1[3], float v2[3], float v3[3], float v4[3], float w[4]); +void interp_v2_v2v2(float r[2], const float a[2], const float b[2], const float t); +void interp_v2_v2v2v2(float r[2], const float a[2], const float b[2], const float c[3], const float t[3]); +void interp_v3_v3v3(float r[3], const float a[3], const float b[3], const float t); +void interp_v3_v3v3v3(float p[3], const float v1[3], const float v2[3], const float v3[3], const float w[3]); +void interp_v3_v3v3v3v3(float p[3], const float v1[3], const float v2[3], const float v3[3], const float v4[3], const float w[4]); void mid_v3_v3v3(float r[3], float a[3], float b[3]); @@ -130,7 +130,7 @@ float angle_v2v2v2(float a[2], float b[2], float c[2]); float angle_normalized_v2v2(float a[2], float b[2]); float angle_v3v3(float a[2], float b[2]); float angle_v3v3v3(float a[2], float b[2], float c[2]); -float angle_normalized_v3v3(float a[3], float b[3]); +float angle_normalized_v3v3(const float v1[3], const float v2[3]); void angle_tri_v3(float angles[3], const float v1[3], const float v2[3], const float v3[3]); void angle_quad_v3(float angles[4], const float v1[3], const float v2[3], const float v3[3], const float v4[3]); diff --git a/source/blender/blenlib/intern/math_geom.c b/source/blender/blenlib/intern/math_geom.c index 3e714b384a7..dd773cb00c1 100644 --- a/source/blender/blenlib/intern/math_geom.c +++ b/source/blender/blenlib/intern/math_geom.c @@ -49,7 +49,7 @@ void cent_quad_v3(float *cent, float *v1, float *v2, float *v3, float *v4) cent[2]= 0.25f*(v1[2]+v2[2]+v3[2]+v4[2]); } -float normal_tri_v3(float *n, float *v1, float *v2, float *v3) +float normal_tri_v3(float n[3], const float v1[3], const float v2[3], const float v3[3]) { float n1[3],n2[3]; @@ -65,7 +65,7 @@ float normal_tri_v3(float *n, float *v1, float *v2, float *v3) return normalize_v3(n); } -float normal_quad_v3(float *n, float *v1, float *v2, float *v3, float *v4) +float normal_quad_v3(float n[3], const float v1[3], const float v2[3], const float v3[3], const float v4[3]) { /* real cross! */ float n1[3],n2[3]; @@ -85,13 +85,17 @@ float normal_quad_v3(float *n, float *v1, float *v2, float *v3, float *v4) return normalize_v3(n); } -float area_tri_v2(float *v1, float *v2, float *v3) +float area_tri_v2(const float v1[2], const float v2[2], const float v3[2]) { - return (float)(0.5*fabs((v1[0]-v2[0])*(v2[1]-v3[1]) + (v1[1]-v2[1])*(v3[0]-v2[0]))); + return (float)(0.5f*fabs((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 (float)(0.5f*((v1[0]-v2[0])*(v2[1]-v3[1]) + (v1[1]-v2[1])*(v3[0]-v2[0]))); +} -float area_quad_v3(float *v1, float *v2, float *v3, float *v4) /* only convex Quadrilaterals */ +float area_quad_v3(const float v1[3], const float v2[3], const float v3[3], const float v4[3]) /* only convex Quadrilaterals */ { float len, vec1[3], vec2[3], n[3]; @@ -108,7 +112,7 @@ float area_quad_v3(float *v1, float *v2, float *v3, float *v4) /* only convex return (len/2.0f); } -float area_tri_v3(float *v1, float *v2, float *v3) /* Triangles */ +float area_tri_v3(const float v1[3], const float v2[3], const float v3[3]) /* Triangles */ { float len, vec1[3], vec2[3], n[3]; @@ -1334,6 +1338,77 @@ void interp_weights_face_v3(float *w,float *v1, float *v2, float *v3, float *v4, } } +/* used by projection painting + * note: using area_tri_signed_v2 means locations outside the triangle are correctly weighted */ +void barycentric_weights_v2(const float v1[2], const float v2[2], const float v3[2], const float co[2], float w[3]) +{ + float wtot_inv, wtot; + + w[0] = area_tri_signed_v2(v2, v3, co); + w[1] = area_tri_signed_v2(v3, v1, co); + w[2] = area_tri_signed_v2(v1, v2, co); + wtot = w[0]+w[1]+w[2]; + + if (wtot != 0.0f) { + wtot_inv = 1.0f/wtot; + + w[0] = w[0]*wtot_inv; + w[1] = w[1]*wtot_inv; + w[2] = w[2]*wtot_inv; + } + else /* dummy values for zero area face */ + w[0] = w[1] = w[2] = 1.0f/3.0f; +} + +/* given 2 triangles in 3D space, and a point in relation to the first triangle. + * calculate the location of a point in relation to the second triangle. + * Useful for finding relative positions with geometry */ +void barycentric_transform(float pt_tar[3], float const pt_src[3], + const float tri_tar_p1[3], const float tri_tar_p2[3], const float tri_tar_p3[3], + const float tri_src_p1[3], const float tri_src_p2[3], const float tri_src_p3[3]) +{ + /* this works by moving the source triangle so its normal is pointing on the Z + * axis where its barycentric wights can be calculated in 2D and its Z offset can + * be re-applied. The weights are applied directly to the targets 3D points and the + * z-depth is used to scale the targets normal as an offset. + * This saves transforming the target into its Z-Up orientation and back (which could also work) */ + const float z_up[3] = {0, 0, 1}; + float no_tar[3], no_src[3]; + float quat_src[4]; + float pt_src_xy[3]; + float tri_xy_src[3][3]; + float w_src[3]; + float area_tar, area_src; + float z_ofs_src; + + normal_tri_v3(no_tar, tri_tar_p1, tri_tar_p2, tri_tar_p3); + normal_tri_v3(no_src, tri_src_p1, tri_src_p2, tri_src_p3); + + rotation_between_vecs_to_quat(quat_src, no_src, z_up); + normalize_qt(quat_src); + + copy_v3_v3(pt_src_xy, pt_src); + copy_v3_v3(tri_xy_src[0], tri_src_p1); + copy_v3_v3(tri_xy_src[1], tri_src_p2); + copy_v3_v3(tri_xy_src[2], tri_src_p3); + + /* make the source tri xy space */ + mul_qt_v3(quat_src, pt_src_xy); + mul_qt_v3(quat_src, tri_xy_src[0]); + mul_qt_v3(quat_src, tri_xy_src[1]); + mul_qt_v3(quat_src, tri_xy_src[2]); + + barycentric_weights_v2(tri_xy_src[0], tri_xy_src[1], tri_xy_src[2], pt_src_xy, w_src); + interp_v3_v3v3v3(pt_tar, tri_tar_p1, tri_tar_p2, tri_tar_p3, w_src); + + area_tar= sqrtf(area_tri_v3(tri_tar_p1, tri_tar_p2, tri_tar_p3)); + area_src= sqrtf(area_tri_v2(tri_xy_src[0], tri_xy_src[1], tri_xy_src[2])); + + z_ofs_src= tri_xy_src[0][2] - pt_src_xy[2]; + madd_v3_v3v3fl(pt_tar, pt_tar, no_tar, (z_ofs_src / area_src) * area_tar); +} + + /* Mean value weights - smooth interpolation weights for polygons with * more than 3 vertices */ static float mean_value_half_tan(float *v1, float *v2, float *v3) @@ -1789,3 +1864,4 @@ void vcloud_estimate_transform(int list_size, float (*pos)[3], float *weight,flo } } } + diff --git a/source/blender/blenlib/intern/math_rotation.c b/source/blender/blenlib/intern/math_rotation.c index 084db725409..61d5eeaaa2c 100644 --- a/source/blender/blenlib/intern/math_rotation.c +++ b/source/blender/blenlib/intern/math_rotation.c @@ -327,7 +327,7 @@ void normalize_qt(float *q) } } -void rotation_between_vecs_to_quat(float *q, float v1[3], float v2[3]) +void rotation_between_vecs_to_quat(float *q, const float v1[3], const float v2[3]) { float axis[3]; float angle; diff --git a/source/blender/blenlib/intern/math_vector.c b/source/blender/blenlib/intern/math_vector.c index 3188587a96c..d99d96d28ff 100644 --- a/source/blender/blenlib/intern/math_vector.c +++ b/source/blender/blenlib/intern/math_vector.c @@ -34,7 +34,7 @@ //******************************* Interpolation *******************************/ -void interp_v2_v2v2(float *target, float *a, float *b, float t) +void interp_v2_v2v2(float *target, const float *a, const float *b, const float t) { float s = 1.0f-t; @@ -44,13 +44,13 @@ void interp_v2_v2v2(float *target, float *a, float *b, float t) /* weight 3 2D vectors, * 'w' must be unit length but is not a vector, just 3 weights */ -void interp_v2_v2v2v2(float p[2], float v1[2], float v2[2], float v3[2], float w[3]) +void interp_v2_v2v2v2(float p[2], const float v1[2], const float v2[2], const float v3[2], const float w[3]) { p[0] = v1[0]*w[0] + v2[0]*w[1] + v3[0]*w[2]; p[1] = v1[1]*w[0] + v2[1]*w[1] + v3[1]*w[2]; } -void interp_v3_v3v3(float *target, float *a, float *b, float t) +void interp_v3_v3v3(float target[3], const float a[3], const float b[3], const float t) { float s = 1.0f-t; @@ -61,7 +61,7 @@ void interp_v3_v3v3(float *target, float *a, float *b, float t) /* weight 3 vectors, * 'w' must be unit length but is not a vector, just 3 weights */ -void interp_v3_v3v3v3(float p[3], float v1[3], float v2[3], float v3[3], float w[3]) +void interp_v3_v3v3v3(float p[3], const float v1[3], const float v2[3], const float v3[3], const float w[3]) { p[0] = v1[0]*w[0] + v2[0]*w[1] + v3[0]*w[2]; p[1] = v1[1]*w[0] + v2[1]*w[1] + v3[1]*w[2]; @@ -70,7 +70,7 @@ void interp_v3_v3v3v3(float p[3], float v1[3], float v2[3], float v3[3], float w /* weight 3 vectors, * 'w' must be unit length but is not a vector, just 3 weights */ -void interp_v3_v3v3v3v3(float p[3], float v1[3], float v2[3], float v3[3], float v4[3], float w[4]) +void interp_v3_v3v3v3v3(float p[3], const float v1[3], const float v2[3], const float v3[3], const float v4[3], const float w[4]) { p[0] = v1[0]*w[0] + v2[0]*w[1] + v3[0]*w[2] + v4[0]*w[3]; p[1] = v1[1]*w[0] + v2[1]*w[1] + v3[1]*w[2] + v4[1]*w[3]; @@ -191,7 +191,7 @@ float angle_v2v2(float *v1, float *v2) return angle_normalized_v2v2(vec1, vec2); } -float angle_normalized_v3v3(float *v1, float *v2) +float angle_normalized_v3v3(const float v1[3], const float v2[3]) { /* this is the same as acos(dot_v3v3(v1, v2)), but more accurate */ if (dot_v3v3(v1, v2) < 0.0f) { diff --git a/source/blender/blenlib/intern/math_vector_inline.c b/source/blender/blenlib/intern/math_vector_inline.c index cb629312712..16f35dbc5fa 100644 --- a/source/blender/blenlib/intern/math_vector_inline.c +++ b/source/blender/blenlib/intern/math_vector_inline.c @@ -45,13 +45,13 @@ MINLINE void zero_v3(float r[3]) r[2]= 0.0f; } -MINLINE void copy_v2_v2(float r[2], float a[2]) +MINLINE void copy_v2_v2(float r[2], const float a[2]) { r[0]= a[0]; r[1]= a[1]; } -MINLINE void copy_v3_v3(float r[3], float a[3]) +MINLINE void copy_v3_v3(float r[3], const float a[3]) { r[0]= a[0]; r[1]= a[1]; @@ -118,7 +118,7 @@ MINLINE void sub_v3_v3(float *r, float *a) r[2] -= a[2]; } -MINLINE void sub_v3_v3v3(float *r, float *a, float *b) +MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3]) { r[0]= a[0] - b[0]; r[1]= a[1] - b[1]; @@ -216,7 +216,7 @@ MINLINE float cross_v2v2(float a[2], float b[2]) return a[0]*b[1] - a[1]*b[0]; } -MINLINE void cross_v3_v3v3(float r[3], float a[3], float b[3]) +MINLINE void cross_v3_v3v3(float r[3], const float a[3], const float b[3]) { r[0]= a[1]*b[2] - a[2]*b[1]; r[1]= a[2]*b[0] - a[0]*b[2]; |