Code cleanup: use length squared where possible

This commit is contained in:
2014-02-02 01:36:40 +11:00
parent 798e684c7c
commit a1a0ebbf49
10 changed files with 40 additions and 34 deletions

View File

@@ -920,7 +920,7 @@ void BKE_brush_jitter_pos(const Scene *scene, Brush *brush, const float pos[2],
do {
rand_pos[0] = BLI_rng_get_float(brush_rng) - 0.5f;
rand_pos[1] = BLI_rng_get_float(brush_rng) - 0.5f;
} while (len_v2(rand_pos) > 0.5f);
} while (len_squared_v2(rand_pos) > (0.5f * 0.5f));
if (brush->flag & BRUSH_ABSOLUTE_JITTER) {

View File

@@ -1178,7 +1178,7 @@ static void mask_calc_point_handle(MaskSplinePoint *point, MaskSplinePoint *poin
sub_v3_v3v3(v2, bezt->vec[2], bezt->vec[1]);
add_v3_v3v3(vec, v1, v2);
if (len_v3(vec) > 1e-3) {
if (len_squared_v3(vec) > (1e-3f * 1e-3f)) {
h[0] = vec[1];
h[1] = -vec[0];
h[2] = 0.0f;

View File

@@ -1553,7 +1553,7 @@ static void find_first_points(PROCESS *process, MetaBall *mb, int a)
/* find "first points" on Implicit Surface of MetaElemnt ml */
copy_v3_v3(workp, in);
workp_v = in_v;
max_len = len_v3v3(out, in);
max_len = len_squared_v3v3(out, in);
nx = fabsf((out[0] - in[0]) / process->size);
ny = fabsf((out[1] - in[1]) / process->size);
@@ -1589,7 +1589,7 @@ static void find_first_points(PROCESS *process, MetaBall *mb, int a)
add_cube(process, c_i, c_j, c_k, 2);
}
}
len = len_v3v3(workp, in);
len = len_squared_v3v3(workp, in);
workp_v = tmp_v;
}

View File

@@ -154,7 +154,7 @@ static int customdata_compare(CustomData *c1, CustomData *c2, Mesh *m1, Mesh *m2
int vtot = m1->totvert;
for (j = 0; j < vtot; j++, v1++, v2++) {
if (len_v3v3(v1->co, v2->co) > thresh)
if (len_squared_v3v3(v1->co, v2->co) > thresh_sq)
return MESHCMP_VERTCOMISMATCH;
/* I don't care about normals, let's just do coodinates */
}

View File

@@ -235,11 +235,12 @@ void BLI_replaceNode(BGraph *graph, BNode *node_src, BNode *node_replaced)
void BLI_removeDoubleNodes(BGraph *graph, float limit)
{
const float limit_sq = limit * limit;
BNode *node_src, *node_replaced;
for (node_src = graph->nodes.first; node_src; node_src = node_src->next) {
for (node_replaced = graph->nodes.first; node_replaced; node_replaced = node_replaced->next) {
if (node_replaced != node_src && len_v3v3(node_replaced->p, node_src->p) <= limit) {
if (node_replaced != node_src && len_squared_v3v3(node_replaced->p, node_src->p) <= limit_sq) {
BLI_replaceNode(graph, node_src, node_replaced);
}
}
@@ -249,12 +250,13 @@ void BLI_removeDoubleNodes(BGraph *graph, float limit)
BNode *BLI_FindNodeByPosition(BGraph *graph, const float p[3], const float limit)
{
const float limit_sq = limit * limit;
BNode *closest_node = NULL, *node;
float min_distance = 0.0f;
for (node = graph->nodes.first; node; node = node->next) {
float distance = len_v3v3(p, node->p);
if (distance <= limit && (closest_node == NULL || distance < min_distance)) {
float distance = len_squared_v3v3(p, node->p);
if (distance <= limit_sq && (closest_node == NULL || distance < min_distance)) {
closest_node = node;
min_distance = distance;
}
@@ -476,6 +478,7 @@ void BLI_mirrorAlongAxis(float v[3], float center[3], float axis[3])
static void testRadialSymmetry(BGraph *graph, BNode *root_node, RadialArc *ring, int total, float axis[3], float limit, int group)
{
const float limit_sq = limit * limit;
int symmetric = 1;
int i;
@@ -525,7 +528,7 @@ static void testRadialSymmetry(BGraph *graph, BNode *root_node, RadialArc *ring,
BLI_mirrorAlongAxis(p, root_node->p, normal);
/* check if it's within limit before continuing */
if (len_v3v3(node1->p, p) > limit) {
if (len_squared_v3v3(node1->p, p) > limit_sq) {
symmetric = 0;
}
@@ -707,6 +710,7 @@ static void flagAxialSymmetry(BNode *root_node, BNode *end_node, BArc *arc, int
static void testAxialSymmetry(BGraph *graph, BNode *root_node, BNode *node1, BNode *node2, BArc *arc1, BArc *arc2, float axis[3], float limit, int group)
{
const float limit_sq = limit * limit;
float nor[3], vec[3], p[3];
sub_v3_v3v3(p, node1->p, root_node->p);
@@ -733,7 +737,7 @@ static void testAxialSymmetry(BGraph *graph, BNode *root_node, BNode *node1, BNo
BLI_mirrorAlongAxis(p, root_node->p, nor);
/* check if it's within limit before continuing */
if (len_v3v3(node1->p, p) <= limit) {
if (len_squared_v3v3(node1->p, p) <= limit_sq) {
/* mark node as symmetric physically */
copy_v3_v3(root_node->symmetry_axis, nor);
root_node->symmetry_flag |= SYM_PHYSICAL;

View File

@@ -4586,7 +4586,7 @@ static bool ui_numedit_but_CURVE(uiBlock *block, uiBut *but, uiHandleButtonData
d[0] = mx - data->dragstartx;
d[1] = my - data->dragstarty;
if (len_v2(d) < 3.0f)
if (len_squared_v2(d) < (3.0f * 3.0f))
snap = false;
}

View File

@@ -66,13 +66,13 @@ void MASK_OT_normals_make_consistent(struct wmOperatorType *ot);
void MASK_OT_handle_type_set(struct wmOperatorType *ot);
int ED_mask_feather_find_nearest(
const struct bContext *C, struct Mask *mask, float normal_co[2], int threshold,
bool ED_mask_feather_find_nearest(
const struct bContext *C, struct Mask *mask, const float normal_co[2], const float threshold,
struct MaskLayer **masklay_r, struct MaskSpline **spline_r, struct MaskSplinePoint **point_r,
struct MaskSplinePointUW **uw_r, float *score);
struct MaskSplinePoint *ED_mask_point_find_nearest(
const struct bContext *C, struct Mask *mask, float normal_co[2], int threshold,
const struct bContext *C, struct Mask *mask, const float normal_co[2], const float threshold,
struct MaskLayer **masklay_r, struct MaskSpline **spline_r, int *is_handle_r,
float *score);

View File

@@ -59,7 +59,7 @@
/******************** utility functions *********************/
MaskSplinePoint *ED_mask_point_find_nearest(const bContext *C, Mask *mask, float normal_co[2], int threshold,
MaskSplinePoint *ED_mask_point_find_nearest(const bContext *C, Mask *mask, const float normal_co[2], const float threshold,
MaskLayer **masklay_r, MaskSpline **spline_r, int *is_handle_r,
float *score)
{
@@ -71,6 +71,7 @@ MaskSplinePoint *ED_mask_point_find_nearest(const bContext *C, Mask *mask, float
MaskSpline *point_spline = NULL;
MaskSplinePoint *point = NULL;
float co[2];
const float threshold_sq = threshold * threshold;
float len = FLT_MAX, scalex, scaley;
int is_handle = FALSE, width, height;
@@ -105,7 +106,7 @@ MaskSplinePoint *ED_mask_point_find_nearest(const bContext *C, Mask *mask, float
handle[0] *= scalex;
handle[1] *= scaley;
cur_len = len_v2v2(co, handle);
cur_len = len_squared_v2v2(co, handle);
if (cur_len < len) {
point_masklay = masklay;
@@ -116,7 +117,7 @@ MaskSplinePoint *ED_mask_point_find_nearest(const bContext *C, Mask *mask, float
}
}
cur_len = len_v2v2(co, vec);
cur_len = len_squared_v2v2(co, vec);
if (cur_len < len) {
point_spline = spline;
@@ -129,7 +130,7 @@ MaskSplinePoint *ED_mask_point_find_nearest(const bContext *C, Mask *mask, float
}
}
if (len < threshold) {
if (len < threshold_sq) {
if (masklay_r)
*masklay_r = point_masklay;
@@ -140,7 +141,7 @@ MaskSplinePoint *ED_mask_point_find_nearest(const bContext *C, Mask *mask, float
*is_handle_r = is_handle;
if (score)
*score = len;
*score = sqrtf(len);
return point;
}
@@ -157,9 +158,9 @@ MaskSplinePoint *ED_mask_point_find_nearest(const bContext *C, Mask *mask, float
return NULL;
}
int ED_mask_feather_find_nearest(const bContext *C, Mask *mask, float normal_co[2], int threshold,
MaskLayer **masklay_r, MaskSpline **spline_r, MaskSplinePoint **point_r,
MaskSplinePointUW **uw_r, float *score)
bool ED_mask_feather_find_nearest(const bContext *C, Mask *mask, const float normal_co[2], const float threshold,
MaskLayer **masklay_r, MaskSpline **spline_r, MaskSplinePoint **point_r,
MaskSplinePointUW **uw_r, float *score)
{
ScrArea *sa = CTX_wm_area(C);
ARegion *ar = CTX_wm_region(C);
@@ -168,6 +169,7 @@ int ED_mask_feather_find_nearest(const bContext *C, Mask *mask, float normal_co[
MaskSpline *point_spline = NULL;
MaskSplinePoint *point = NULL;
MaskSplinePointUW *uw = NULL;
const float threshold_sq = threshold * threshold;
float len = FLT_MAX, co[2];
float scalex, scaley;
int width, height;
@@ -203,7 +205,7 @@ int ED_mask_feather_find_nearest(const bContext *C, Mask *mask, float normal_co[
vec[0] = (*fp)[0] * scalex;
vec[1] = (*fp)[1] * scaley;
cur_len = len_v2v2(vec, co);
cur_len = len_squared_v2v2(vec, co);
if (point == NULL || cur_len < len) {
if (j == 0)
@@ -225,7 +227,7 @@ int ED_mask_feather_find_nearest(const bContext *C, Mask *mask, float normal_co[
}
}
if (len < threshold) {
if (len < threshold_sq) {
if (masklay_r)
*masklay_r = point_masklay;
@@ -239,7 +241,7 @@ int ED_mask_feather_find_nearest(const bContext *C, Mask *mask, float normal_co[
*uw_r = uw;
if (score)
*score = len;
*score = sqrtf(len);
return TRUE;
}

View File

@@ -769,12 +769,12 @@ static float get_shortest_pattern_side(MovieTrackingMarker *marker)
next = (i + 1) % 4;
cur_len = len_v2v2(marker->pattern_corners[i], marker->pattern_corners[next]);
cur_len = len_squared_v2v2(marker->pattern_corners[i], marker->pattern_corners[next]);
len = min_ff(cur_len, len);
}
return len;
return sqrtf(len);
}
static void draw_marker_slide_square(float x, float y, float dx, float dy, int outline, float px[2])
@@ -1031,8 +1031,7 @@ static void getArrowEndPoint(const int width, const int height, const float zoom
direction[0] *= width;
direction[1] *= height;
max_length = len_v2(direction);
normalize_v2(direction);
max_length = normalize_v2(direction);
mul_v2_fl(direction, min_ff(32.0f / zoom, max_length));
direction[0] /= width;
direction[1] /= height;
@@ -1461,7 +1460,7 @@ static void draw_tracking_tracks(SpaceClip *sc, Scene *scene, ARegion *ar, Movie
sub_v2_v2(vec, npos);
if (len_v2(vec) < 3.0f)
if (len_squared_v2(vec) < (3.0f * 3.0f))
glColor3f(0.0f, 1.0f, 0.0f);
else
glColor3f(1.0f, 0.0f, 0.0f);

View File

@@ -546,10 +546,11 @@ static int get_mouse_pattern_corner(SpaceClip *sc, MovieTrackingMarker *marker,
next = (i + 1) % 4;
cur_len = len_v2v2(marker->pattern_corners[i], marker->pattern_corners[next]);
cur_len = len_squared_v2v2(marker->pattern_corners[i], marker->pattern_corners[next]);
len = min_ff(cur_len, len);
}
len = sqrtf(len);
dx = 12.0f / width / sc->zoom;
dy = 12.0f / height / sc->zoom;
@@ -913,7 +914,7 @@ static int slide_marker_modal(bContext *C, wmOperator *op, const wmEvent *event)
sub_v2_v2(start, data->old_pos);
if (len_v2(start) > 0.0f) {
if (len_squared_v2(start) != 0.0f) {
float mval[2];
if (data->accurate) {
@@ -986,7 +987,7 @@ static int slide_marker_modal(bContext *C, wmOperator *op, const wmEvent *event)
ED_clip_point_stable_pos(sc, ar, mval[0], mval[1], &end[0], &end[1]);
sub_v2_v2(end, data->old_pos);
if (len_v2(start) > 0.0f) {
if (len_squared_v2(start) != 0.0f) {
scale = len_v2(end) / len_v2(start);
if (scale < 0.0f) {
@@ -2207,7 +2208,7 @@ static void set_axis(Scene *scene, Object *ob, MovieClip *clip, MovieTrackingOb
sub_v3_v3(vec, obmat[3]);
}
if (len_v2(vec) < 1e-3f)
if (len_squared_v2(vec) < (1e-3f * 1e-3f))
return;
unit_m4(mat);