Math Lib: add shell_v3v3_normalized_to_dist and v2 version

bypass angle calculation to avoids (asin, sqrt, cos).
This commit is contained in:
2014-04-19 22:12:50 +10:00
parent 9c93c0bf08
commit a51a0ca772
10 changed files with 53 additions and 31 deletions

View 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 */

View 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);

View 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

View 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)
{

View 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__ */

View 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++;
}

View 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++;
}
}

View 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 {

View 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) {

View 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 --- */