Math Lib: add shell_v3v3_normalized_to_dist and v2 version
authorCampbell Barton <ideasman42@gmail.com>
Sat, 19 Apr 2014 12:12:50 +0000 (22:12 +1000)
committerCampbell Barton <ideasman42@gmail.com>
Sat, 19 Apr 2014 12:17:10 +0000 (22:17 +1000)
bypass angle calculation to avoids (asin, sqrt, cos).

source/blender/blenkernel/intern/fcurve.c
source/blender/blenlib/BLI_math_base.h
source/blender/blenlib/BLI_math_geom.h
source/blender/blenlib/intern/math_base_inline.c
source/blender/blenlib/intern/math_geom_inline.c
source/blender/bmesh/intern/bmesh_queries.c
source/blender/bmesh/operators/bmo_extrude.c
source/blender/bmesh/operators/bmo_inset.c
source/blender/editors/sculpt_paint/paint_image_proj.c
source/blender/modifiers/intern/MOD_solidify.c

index fc74ae5fe60000eecd0e3c79073c22eef9a0940b..d4fa9de104368b9c77dce5b674995beefed88ebe 100644 (file)
@@ -1928,6 +1928,7 @@ static void berekenx(float *f, float *o, int b)
 /* Calculate F-Curve value for 'evaltime' using BezTriple keyframes */
 static float fcurve_eval_keyframes(FCurve *fcu, BezTriple *bezts, float evaltime)
 {
+       const float eps = 1.e-8f;
        BezTriple *bezt, *prevbezt, *lastbezt;
        float v1[2], v2[2], v3[2], v4[2], opl[32], dx, fac;
        unsigned int a;
@@ -2073,7 +2074,7 @@ static float fcurve_eval_keyframes(FCurve *fcu, BezTriple *bezts, float evaltime
                if (exact) {
                        cvalue = prevbezt->vec[1][1];
                }
-               else if (fabsf(bezt->vec[1][0] - evaltime) < SMALL_NUMBER) {
+               else if (fabsf(bezt->vec[1][0] - evaltime) < eps) {
                        cvalue = bezt->vec[1][1];
                }
                /* evaltime occurs within the interval defined by these two keyframes */
index 68ffb4b48939831055780dade673524ed166762e..5bd8490db6e90dead1dd9e4349c2893edc7ce2f0 100644 (file)
@@ -241,8 +241,6 @@ MINLINE int mod_i(int i, int n);
 MINLINE unsigned int highest_order_bit_i(unsigned int n);
 MINLINE unsigned short highest_order_bit_s(unsigned short n);
 
-MINLINE float shell_angle_to_dist(const float angle);
-
 #if defined(_MSC_VER) && (_MSC_VER < 1800)
 extern double copysign(double x, double y);
 extern double round(double x);
index 6ba8910db147ce7d35c6c88a0c0f4fe2bf8f0f36..5067cee73523ba0d78a09935195077927b12b560 100644 (file)
@@ -314,6 +314,10 @@ MINLINE int min_axis_v3(const float vec[3]);
 
 MINLINE int poly_to_tri_count(const int poly_count, const int corner_count);
 
+MINLINE float shell_angle_to_dist(const float angle);
+MINLINE float shell_v3v3_normalized_to_dist(const float a[3], const float b[3]);
+MINLINE float shell_v2v2_normalized_to_dist(const float a[2], const float b[2]);
+
 /**************************** Inline Definitions ******************************/
 
 #if BLI_MATH_DO_INLINE
index 82c6e68ccc249ee765c17f49814b46c31a4fa56b..e6217329145b9cc4812b1435dc40e94acb0599e8 100644 (file)
@@ -44,9 +44,6 @@
 #  define UNLIKELY(x)     (x)
 #endif
 
-/* A few small defines. Keep'em local! */
-#define SMALL_NUMBER  1.e-8f
-
 MINLINE float sqrt3f(float f)
 {
        if      (UNLIKELY(f == 0.0f)) return 0.0f;
@@ -111,15 +108,6 @@ MINLINE float interpf(float target, float origin, float fac)
        return (fac * target) + (1.0f - fac) * origin;
 }
 
-/* useful to calculate an even width shell, by taking the angle between 2 planes.
- * The return value is a scale on the offset.
- * no angle between planes is 1.0, as the angle between the 2 planes approaches 180d
- * the distance gets very high, 180d would be inf, but this case isn't valid */
-MINLINE float shell_angle_to_dist(const float angle)
-{
-       return (UNLIKELY(angle < SMALL_NUMBER)) ? 1.0f : fabsf(1.0f / cosf(angle));
-}
-
 /* used for zoom values*/
 MINLINE float power_of_2(float val)
 {
index 0e243c5e198e57c53f149d8d2477a07096fb261e..1e54dd11a763cd33a5b812eb3aa0c974d066b58b 100644 (file)
@@ -34,6 +34,9 @@
 
 #include <string.h>
 
+/* A few small defines. Keep'em local! */
+#define SMALL_NUMBER  1.e-8f
+
 /********************************** Polygons *********************************/
 
 MINLINE float cross_tri_v2(const float v1[2], const float v2[2], const float v3[2])
@@ -227,4 +230,35 @@ MINLINE float plane_point_side_v3(const float plane[4], const float co[3])
        return dot_v3v3(co, plane) + plane[3];
 }
 
+/* useful to calculate an even width shell, by taking the angle between 2 planes.
+ * The return value is a scale on the offset.
+ * no angle between planes is 1.0, as the angle between the 2 planes approaches 180d
+ * the distance gets very high, 180d would be inf, but this case isn't valid */
+MINLINE float shell_angle_to_dist(const float angle)
+{
+       return (UNLIKELY(angle < SMALL_NUMBER)) ? 1.0f : fabsf(1.0f / cosf(angle));
+}
+/**
+ * equivalent to ``shell_angle_to_dist(angle_normalized_v3v3(a, b))``
+ */
+MINLINE float shell_v3v3_normalized_to_dist(const float a[3], const float b[3])
+{
+       const float angle_cos = fabsf(dot_v3v3(a, b));
+       BLI_ASSERT_UNIT_V3(a);
+       BLI_ASSERT_UNIT_V3(b);
+       return (UNLIKELY(angle_cos < SMALL_NUMBER)) ? 1.0f : (1.0f / angle_cos);
+}
+/**
+ * equivalent to ``shell_angle_to_dist(angle_normalized_v2v2(a, b))``
+ */
+MINLINE float shell_v2v2_normalized_to_dist(const float a[2], const float b[2])
+{
+       const float angle_cos = fabsf(dot_v2v2(a, b));
+       BLI_ASSERT_UNIT_V2(a);
+       BLI_ASSERT_UNIT_V2(b);
+       return (UNLIKELY(angle_cos < SMALL_NUMBER)) ? 1.0f : (1.0f / angle_cos);
+}
+
+#undef SMALL_NUMBER
+
 #endif /* __MATH_GEOM_INLINE_C__ */
index f240a10efc88897b9ca8fc9d830f985713341745..67f333215d9a0696a23f26baa69cbd3be87dab62 100644 (file)
@@ -1236,7 +1236,7 @@ float BM_vert_calc_shell_factor(BMVert *v)
 
        BM_ITER_ELEM (l, &iter, v, BM_LOOPS_OF_VERT) {
                const float face_angle = BM_loop_calc_face_angle(l);
-               accum_shell += shell_angle_to_dist(angle_normalized_v3v3(v->no, l->f->no)) * face_angle;
+               accum_shell += shell_v3v3_normalized_to_dist(v->no, l->f->no) * face_angle;
                accum_angle += face_angle;
        }
 
@@ -1260,7 +1260,7 @@ float BM_vert_calc_shell_factor_ex(BMVert *v, const char hflag)
        BM_ITER_ELEM (l, &iter, v, BM_LOOPS_OF_VERT) {
                if (BM_elem_flag_test(l->f, hflag)) {  /* <-- main difference to BM_vert_calc_shell_factor! */
                        const float face_angle = BM_loop_calc_face_angle(l);
-                       accum_shell += shell_angle_to_dist(angle_normalized_v3v3(v->no, l->f->no)) * face_angle;
+                       accum_shell += shell_v3v3_normalized_to_dist(v->no, l->f->no) * face_angle;
                        accum_angle += face_angle;
                        tot_sel++;
                }
index 90c514bfd02dd1bb24663c330b4bfc27d011d7b3..f924d478f1a42771940b00c7c22bef2b9751f968 100644 (file)
@@ -602,7 +602,7 @@ static void solidify_add_thickness(BMesh *bm, const float dist)
                                v = l->v;
                                index = BM_elem_index_get(v);
                                vert_accum[index] += face_angles[i];
-                               vert_angles[index] += shell_angle_to_dist(angle_normalized_v3v3(v->no, f->no)) * face_angles[i];
+                               vert_angles[index] += shell_v3v3_normalized_to_dist(v->no, f->no) * face_angles[i];
                                i++;
                        }
                }
index 29522b627075e355fdb959b1dcc5b532f3e30e71..e0285dc960a35ddc9a7ef0dbc3908f505e63dcd7 100644 (file)
@@ -758,7 +758,7 @@ void bmo_inset_region_exec(BMesh *bm, BMOperator *op)
                                                                normalize_v3(tvec);
 
                                                                if (use_even_offset) {
-                                                                       mul_v3_fl(tvec, shell_angle_to_dist(angle_normalized_v3v3(e_no_a, tvec)));
+                                                                       mul_v3_fl(tvec, shell_v3v3_normalized_to_dist(e_no_a, tvec));
                                                                }
                                                        }
                                                        else {
index c6de57492743a4bde243252903606189df91008c..ac48bee404ed64704c006e0fe298cd95c0e12a48 100644 (file)
@@ -983,19 +983,16 @@ static void uv_image_outset(float (*orig_uv)[2], float (*outset_uv)[2], const fl
                normalize_v2(dir3);
        }
 
-       /* TODO - angle_normalized_v2v2(...) * (M_PI/180.0f)
-        * This is incorrect. Its already given radians but without it wont work.
-        * need to look into a fix - campbell */
        if (is_quad) {
-               a1 = shell_angle_to_dist(angle_normalized_v2v2(dir4, dir1) * ((float)M_PI / 180.0f));
-               a2 = shell_angle_to_dist(angle_normalized_v2v2(dir1, dir2) * ((float)M_PI / 180.0f));
-               a3 = shell_angle_to_dist(angle_normalized_v2v2(dir2, dir3) * ((float)M_PI / 180.0f));
-               a4 = shell_angle_to_dist(angle_normalized_v2v2(dir3, dir4) * ((float)M_PI / 180.0f));
+               a1 = shell_v2v2_normalized_to_dist(dir4, dir1);
+               a2 = shell_v2v2_normalized_to_dist(dir1, dir2);
+               a3 = shell_v2v2_normalized_to_dist(dir2, dir3);
+               a4 = shell_v2v2_normalized_to_dist(dir3, dir4);
        }
        else {
-               a1 = shell_angle_to_dist(angle_normalized_v2v2(dir3, dir1) * ((float)M_PI / 180.0f));
-               a2 = shell_angle_to_dist(angle_normalized_v2v2(dir1, dir2) * ((float)M_PI / 180.0f));
-               a3 = shell_angle_to_dist(angle_normalized_v2v2(dir2, dir3) * ((float)M_PI / 180.0f));
+               a1 = shell_v2v2_normalized_to_dist(dir3, dir1);
+               a2 = shell_v2v2_normalized_to_dist(dir1, dir2);
+               a3 = shell_v2v2_normalized_to_dist(dir2, dir3);
        }
 
        if (is_quad) {
index 721a77df29c152332b8f58ce053fb8fa5ad53d13..90a5d56f451f7e078df5511a555616abf44077af 100644 (file)
@@ -538,13 +538,13 @@ static DerivedMesh *applyModifier(
                                    LIKELY(((orig_medge[ml[i_curr].e].flag & ME_EDGE_TMP_TAG) == 0) &&
                                           ((orig_medge[ml[i_next].e].flag & ME_EDGE_TMP_TAG) == 0)))
                                {
-                                       vert_angles[vidx] += shell_angle_to_dist(angle_normalized_v3v3(vert_nors[vidx], face_nors[i])) * angle;
+                                       vert_angles[vidx] += shell_v3v3_normalized_to_dist(vert_nors[vidx], face_nors[i]) * angle;
                                }
                                else {
                                        vert_angles[vidx] += angle;
                                }
 #else
-                               vert_angles[vidx] += shell_angle_to_dist(angle_normalized_v3v3(vert_nors[vidx], face_nors[i])) * angle;
+                               vert_angles[vidx] += shell_v3v3_normalized_to_dist(vert_nors[vidx], face_nors[i]) * angle;
 #endif
                                /* --- end non-angle-calc section --- */