BLI_kdtree: use 1d tree for select similar

This commit is contained in:
Campbell Barton 2019-03-20 01:56:53 +11:00
parent 7a937436ab
commit 899fc0331c
5 changed files with 115 additions and 65 deletions

View File

@ -1362,7 +1362,8 @@ static void nurb_bpoint_direction_worldspace_get(Object *ob, Nurb *nu, BPoint *b
normalize_v3(r_dir);
}
static void curve_nurb_selected_type_get(Object *ob, Nurb *nu, const int type, KDTree_3d *r_tree)
static void curve_nurb_selected_type_get(
Object *ob, Nurb *nu, const int type, KDTree_1d *tree_1d, KDTree_3d *tree_3d)
{
float tree_entry[3] = {0.0f, 0.0f, 0.0f};
@ -1393,7 +1394,12 @@ static void curve_nurb_selected_type_get(Object *ob, Nurb *nu, const int type, K
break;
}
}
BLI_kdtree_3d_insert(r_tree, tree_index++, tree_entry);
if (tree_1d) {
BLI_kdtree_1d_insert(tree_1d, tree_index++, tree_entry);
}
else {
BLI_kdtree_3d_insert(tree_3d, tree_index++, tree_entry);
}
}
}
}
@ -1423,7 +1429,12 @@ static void curve_nurb_selected_type_get(Object *ob, Nurb *nu, const int type, K
break;
}
}
BLI_kdtree_3d_insert(r_tree, tree_index++, tree_entry);
if (tree_1d) {
BLI_kdtree_1d_insert(tree_1d, tree_index++, tree_entry);
}
else {
BLI_kdtree_3d_insert(tree_3d, tree_index++, tree_entry);
}
}
}
}
@ -1431,7 +1442,8 @@ static void curve_nurb_selected_type_get(Object *ob, Nurb *nu, const int type, K
static bool curve_nurb_select_similar_type(
Object *ob, Nurb *nu, const int type,
const KDTree_3d *tree, const float thresh, const int compare)
const KDTree_1d *tree_1d, const KDTree_3d *tree_3d,
const float thresh, const int compare)
{
const float thresh_cos = cosf(thresh * (float)M_PI_2);
bool changed = false;
@ -1448,7 +1460,7 @@ static bool curve_nurb_select_similar_type(
case SIMCURHAND_RADIUS:
{
float radius_ref = bezt->radius;
if (ED_select_similar_compare_float_tree(tree, radius_ref, thresh, compare)) {
if (ED_select_similar_compare_float_tree(tree_1d, radius_ref, thresh, compare)) {
select = true;
}
break;
@ -1456,7 +1468,7 @@ static bool curve_nurb_select_similar_type(
case SIMCURHAND_WEIGHT:
{
float weight_ref = bezt->weight;
if (ED_select_similar_compare_float_tree(tree, weight_ref, thresh, compare)) {
if (ED_select_similar_compare_float_tree(tree_1d, weight_ref, thresh, compare)) {
select = true;
}
break;
@ -1466,7 +1478,7 @@ static bool curve_nurb_select_similar_type(
float dir[3];
nurb_bezt_direction_worldspace_get(ob, nu, bezt, dir);
KDTreeNearest_3d nearest;
if (BLI_kdtree_3d_find_nearest(tree, dir, &nearest) != -1) {
if (BLI_kdtree_3d_find_nearest(tree_3d, dir, &nearest) != -1) {
float orient = angle_normalized_v3v3(dir, nearest.co);
float delta = thresh_cos - fabsf(cosf(orient));
if (ED_select_similar_compare_float(delta, thresh, compare)) {
@ -1496,7 +1508,7 @@ static bool curve_nurb_select_similar_type(
case SIMCURHAND_RADIUS:
{
float radius_ref = bp->radius;
if (ED_select_similar_compare_float_tree(tree, radius_ref, thresh, compare)) {
if (ED_select_similar_compare_float_tree(tree_1d, radius_ref, thresh, compare)) {
select = true;
}
break;
@ -1504,7 +1516,7 @@ static bool curve_nurb_select_similar_type(
case SIMCURHAND_WEIGHT:
{
float weight_ref = bp->weight;
if (ED_select_similar_compare_float_tree(tree, weight_ref, thresh, compare)) {
if (ED_select_similar_compare_float_tree(tree_1d, weight_ref, thresh, compare)) {
select = true;
}
break;
@ -1514,7 +1526,7 @@ static bool curve_nurb_select_similar_type(
float dir[3];
nurb_bpoint_direction_worldspace_get(ob, nu, bp, dir);
KDTreeNearest_3d nearest;
if (BLI_kdtree_3d_find_nearest(tree, dir, &nearest) != -1) {
if (BLI_kdtree_3d_find_nearest(tree_3d, dir, &nearest) != -1) {
float orient = angle_normalized_v3v3(dir, nearest.co);
float delta = fabsf(cosf(orient)) - thresh_cos;
if (ED_select_similar_compare_float(delta, thresh, compare)) {
@ -1560,14 +1572,17 @@ static int curve_select_similar_exec(bContext *C, wmOperator *op)
return OPERATOR_CANCELLED;
}
KDTree_3d *tree = NULL;
KDTree_1d *tree_1d = NULL;
KDTree_3d *tree_3d = NULL;
short type_ref = 0;
switch (optype) {
case SIMCURHAND_RADIUS:
case SIMCURHAND_WEIGHT:
tree_1d = BLI_kdtree_1d_new(tot_nurbs_selected_all);
break;
case SIMCURHAND_DIRECTION:
tree = BLI_kdtree_3d_new(tot_nurbs_selected_all);
tree_3d = BLI_kdtree_3d_new(tot_nurbs_selected_all);
break;
}
@ -1591,14 +1606,17 @@ static int curve_select_similar_exec(bContext *C, wmOperator *op)
case SIMCURHAND_RADIUS:
case SIMCURHAND_WEIGHT:
case SIMCURHAND_DIRECTION:
curve_nurb_selected_type_get(obedit, nu, optype, tree);
curve_nurb_selected_type_get(obedit, nu, optype, tree_1d, tree_3d);
break;
}
}
}
if (tree != NULL) {
BLI_kdtree_3d_balance(tree);
if (tree_1d != NULL) {
BLI_kdtree_1d_balance(tree_1d);
}
if (tree_3d != NULL) {
BLI_kdtree_3d_balance(tree_3d);
}
/* Select control points with desired type. */
@ -1622,7 +1640,8 @@ static int curve_select_similar_exec(bContext *C, wmOperator *op)
case SIMCURHAND_RADIUS:
case SIMCURHAND_WEIGHT:
case SIMCURHAND_DIRECTION:
changed = curve_nurb_select_similar_type(obedit, nu, optype, tree, thresh, compare);
changed = curve_nurb_select_similar_type(
obedit, nu, optype, tree_1d, tree_3d, thresh, compare);
break;
}
}
@ -1634,8 +1653,12 @@ static int curve_select_similar_exec(bContext *C, wmOperator *op)
}
MEM_freeN(objects);
if (tree != NULL) {
BLI_kdtree_3d_free(tree);
if (tree_1d != NULL) {
BLI_kdtree_1d_free(tree_1d);
}
if (tree_3d != NULL) {
BLI_kdtree_3d_free(tree_3d);
}
return OPERATOR_FINISHED;

View File

@ -21,7 +21,7 @@
#ifndef __ED_SELECT_UTILS_H__
#define __ED_SELECT_UTILS_H__
struct KDTree_3d;
struct KDTree_1d;
enum {
SEL_TOGGLE = 0,
@ -55,7 +55,7 @@ int ED_select_op_action(const eSelectOp sel_op, const bool is_select, const bool
int ED_select_op_action_deselected(const eSelectOp sel_op, const bool is_select, const bool is_inside);
int ED_select_similar_compare_float(const float delta, const float thresh, const int compare);
bool ED_select_similar_compare_float_tree(const struct KDTree_3d *tree, const float length, const float thresh, const int compare);
bool ED_select_similar_compare_float_tree(const struct KDTree_1d *tree, const float length, const float thresh, const int compare);
eSelectOp ED_select_op_modal(const eSelectOp sel_op, const bool is_first);

View File

@ -183,8 +183,9 @@ static int similar_face_select_exec(bContext *C, wmOperator *op)
return OPERATOR_CANCELLED;
}
KDTree_1d *tree_1d = NULL;
KDTree_3d *tree_3d = NULL;
KDTree_4d *tree_plane = NULL;
KDTree_4d *tree_4d = NULL;
GSet *gset = NULL;
GSet **gset_array = NULL;
int face_data_value = SIMFACE_DATA_NONE;
@ -192,11 +193,13 @@ static int similar_face_select_exec(bContext *C, wmOperator *op)
switch (type) {
case SIMFACE_AREA:
case SIMFACE_PERIMETER:
tree_1d = BLI_kdtree_1d_new(tot_faces_selected_all);
break;
case SIMFACE_NORMAL:
tree_3d = BLI_kdtree_3d_new(tot_faces_selected_all);
break;
case SIMFACE_COPLANAR:
tree_plane = BLI_kdtree_4d_new(tot_faces_selected_all);
tree_4d = BLI_kdtree_4d_new(tot_faces_selected_all);
break;
case SIMFACE_SIDES:
case SIMFACE_MATERIAL:
@ -272,15 +275,13 @@ static int similar_face_select_exec(bContext *C, wmOperator *op)
case SIMFACE_AREA:
{
float area = BM_face_calc_area_with_mat3(face, ob_m3);
float dummy[3] = {area, 0.0f, 0.0f};
BLI_kdtree_3d_insert(tree_3d, tree_index++, dummy);
BLI_kdtree_1d_insert(tree_1d, tree_index++, &area);
break;
}
case SIMFACE_PERIMETER:
{
float perimeter = BM_face_calc_perimeter_with_mat3(face, ob_m3);
float dummy[3] = {perimeter, 0.0f, 0.0f};
BLI_kdtree_3d_insert(tree_3d, tree_index++, dummy);
BLI_kdtree_1d_insert(tree_1d, tree_index++, &perimeter);
break;
}
case SIMFACE_NORMAL:
@ -289,7 +290,6 @@ static int similar_face_select_exec(bContext *C, wmOperator *op)
copy_v3_v3(normal, face->no);
mul_transposed_mat3_m4_v3(ob->imat, normal);
normalize_v3(normal);
BLI_kdtree_3d_insert(tree_3d, tree_index++, normal);
break;
}
@ -297,7 +297,7 @@ static int similar_face_select_exec(bContext *C, wmOperator *op)
{
float plane[4];
face_to_plane(ob, face, plane);
BLI_kdtree_4d_insert(tree_plane, tree_index++, plane);
BLI_kdtree_4d_insert(tree_4d, tree_index++, plane);
break;
}
case SIMFACE_SMOOTH:
@ -336,11 +336,14 @@ static int similar_face_select_exec(bContext *C, wmOperator *op)
BLI_assert((type != SIMFACE_FREESTYLE) || (face_data_value != SIMFACE_DATA_NONE));
if (tree_1d != NULL) {
BLI_kdtree_1d_balance(tree_1d);
}
if (tree_3d != NULL) {
BLI_kdtree_3d_balance(tree_3d);
}
if (tree_plane != NULL) {
BLI_kdtree_4d_balance(tree_plane);
if (tree_4d != NULL) {
BLI_kdtree_4d_balance(tree_4d);
}
for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
@ -424,7 +427,7 @@ static int similar_face_select_exec(bContext *C, wmOperator *op)
case SIMFACE_AREA:
{
float area = BM_face_calc_area_with_mat3(face, ob_m3);
if (ED_select_similar_compare_float_tree(tree_3d, area, thresh, compare)) {
if (ED_select_similar_compare_float_tree(tree_1d, area, thresh, compare)) {
select = true;
}
break;
@ -432,7 +435,7 @@ static int similar_face_select_exec(bContext *C, wmOperator *op)
case SIMFACE_PERIMETER:
{
float perimeter = BM_face_calc_perimeter_with_mat3(face, ob_m3);
if (ED_select_similar_compare_float_tree(tree_3d, perimeter, thresh, compare)) {
if (ED_select_similar_compare_float_tree(tree_1d, perimeter, thresh, compare)) {
select = true;
}
break;
@ -460,7 +463,7 @@ static int similar_face_select_exec(bContext *C, wmOperator *op)
face_to_plane(ob, face, plane);
KDTreeNearest_4d nearest;
if (BLI_kdtree_4d_find_nearest(tree_plane, plane, &nearest) != -1) {
if (BLI_kdtree_4d_find_nearest(tree_4d, plane, &nearest) != -1) {
if (nearest.dist <= thresh) {
if ((fabsf(plane[3] - nearest.co[3]) <= thresh) &&
(angle_v3v3(plane, nearest.co) <= thresh_radians))
@ -551,7 +554,7 @@ face_select_all:
MEM_freeN(objects);
BLI_kdtree_3d_free(tree_3d);
BLI_kdtree_4d_free(tree_plane);
BLI_kdtree_4d_free(tree_4d);
if (gset != NULL) {
BLI_gset_free(gset, NULL);
}
@ -671,6 +674,7 @@ static int similar_edge_select_exec(bContext *C, wmOperator *op)
return OPERATOR_CANCELLED;
}
KDTree_1d *tree_1d = NULL;
KDTree_3d *tree_3d = NULL;
GSet *gset = NULL;
int edge_data_value = SIMEDGE_DATA_NONE;
@ -680,6 +684,8 @@ static int similar_edge_select_exec(bContext *C, wmOperator *op)
case SIMEDGE_BEVEL:
case SIMEDGE_FACE_ANGLE:
case SIMEDGE_LENGTH:
tree_1d = BLI_kdtree_1d_new(tot_edges_selected_all);
break;
case SIMEDGE_DIR:
tree_3d = BLI_kdtree_3d_new(tot_edges_selected_all);
break;
@ -720,8 +726,7 @@ static int similar_edge_select_exec(bContext *C, wmOperator *op)
case SIMEDGE_BEVEL:
{
if (!CustomData_has_layer(&bm->edata, custom_data_type)) {
float dummy[3] = {0.0f, 0.0f, 0.0f};
BLI_kdtree_3d_insert(tree_3d, tree_index++, dummy);
BLI_kdtree_1d_insert(tree_1d, tree_index++, (float[1]){0.0f});
continue;
}
break;
@ -751,16 +756,14 @@ static int similar_edge_select_exec(bContext *C, wmOperator *op)
case SIMEDGE_LENGTH:
{
float length = edge_length_squared_worldspace_get(ob, edge);
float dummy[3] = {length, 0.0f, 0.0f};
BLI_kdtree_3d_insert(tree_3d, tree_index++, dummy);
BLI_kdtree_1d_insert(tree_1d, tree_index++, &length);
break;
}
case SIMEDGE_FACE_ANGLE:
{
if (BM_edge_face_count_at_most(edge, 2) == 2) {
float angle = BM_edge_calc_face_angle_with_imat3(edge, ob_m3_inv);
float dummy[3] = {angle, 0.0f, 0.0f};
BLI_kdtree_3d_insert(tree_3d, tree_index++, dummy);
BLI_kdtree_1d_insert(tree_1d, tree_index++, &angle);
}
break;
}
@ -793,8 +796,7 @@ static int similar_edge_select_exec(bContext *C, wmOperator *op)
case SIMEDGE_BEVEL:
{
const float *value = CustomData_bmesh_get(&bm->edata, edge->head.data, custom_data_type);
float dummy[3] = {*value, 0.0f, 0.0f};
BLI_kdtree_3d_insert(tree_3d, tree_index++, dummy);
BLI_kdtree_1d_insert(tree_1d, tree_index++, value);
break;
}
}
@ -804,6 +806,9 @@ static int similar_edge_select_exec(bContext *C, wmOperator *op)
BLI_assert((type != SIMEDGE_FREESTYLE) || (edge_data_value != SIMEDGE_DATA_NONE));
if (tree_1d != NULL) {
BLI_kdtree_1d_balance(tree_1d);
}
if (tree_3d != NULL) {
BLI_kdtree_3d_balance(tree_3d);
}
@ -832,7 +837,7 @@ static int similar_edge_select_exec(bContext *C, wmOperator *op)
/* Proceed only if we have to select all the edges that have custom data value of 0.0f.
* In this case we will just select all the edges.
* Otherwise continue the for loop. */
if (!ED_select_similar_compare_float_tree(tree_3d, 0.0f, thresh, compare)) {
if (!ED_select_similar_compare_float_tree(tree_1d, 0.0f, thresh, compare)) {
continue;
}
}
@ -884,7 +889,7 @@ static int similar_edge_select_exec(bContext *C, wmOperator *op)
case SIMEDGE_LENGTH:
{
float length = edge_length_squared_worldspace_get(ob, edge);
if (ED_select_similar_compare_float_tree(tree_3d, length, thresh, compare)) {
if (ED_select_similar_compare_float_tree(tree_1d, length, thresh, compare)) {
select = true;
}
break;
@ -893,7 +898,7 @@ static int similar_edge_select_exec(bContext *C, wmOperator *op)
{
if (BM_edge_face_count_at_most(edge, 2) == 2) {
float angle = BM_edge_calc_face_angle_with_imat3(edge, ob_m3_inv);
if (ED_select_similar_compare_float_tree(tree_3d, angle, thresh, SIM_CMP_EQ)) {
if (ED_select_similar_compare_float_tree(tree_1d, angle, thresh, SIM_CMP_EQ)) {
select = true;
}
}
@ -940,7 +945,7 @@ static int similar_edge_select_exec(bContext *C, wmOperator *op)
}
const float *value = CustomData_bmesh_get(&bm->edata, edge->head.data, custom_data_type);
if (ED_select_similar_compare_float_tree(tree_3d, *value, thresh, compare)) {
if (ED_select_similar_compare_float_tree(tree_1d, *value, thresh, compare)) {
select = true;
}
break;

View File

@ -194,7 +194,9 @@ static const EnumPropertyItem prop_similar_types[] = {
{0, NULL, 0, NULL, NULL},
};
static void mball_select_similar_type_get(Object *obedit, MetaBall *mb, int type, KDTree_3d *r_tree)
static void mball_select_similar_type_get(
Object *obedit, MetaBall *mb, int type,
KDTree_1d *tree_1d, KDTree_3d *tree_3d)
{
float tree_entry[3] = {0.0f, 0.0f, 0.0f};
MetaElem *ml;
@ -231,12 +233,20 @@ static void mball_select_similar_type_get(Object *obedit, MetaBall *mb, int typ
break;
}
}
BLI_kdtree_3d_insert(r_tree, tree_index++, tree_entry);
if (tree_1d) {
BLI_kdtree_1d_insert(tree_1d, tree_index++, tree_entry);
}
else {
BLI_kdtree_3d_insert(tree_3d, tree_index++, tree_entry);
}
}
}
}
static bool mball_select_similar_type(Object *obedit, MetaBall *mb, int type, const KDTree_3d *tree, const float thresh)
static bool mball_select_similar_type(
Object *obedit, MetaBall *mb, int type,
const KDTree_1d *tree_1d, const KDTree_3d *tree_3d,
const float thresh)
{
MetaElem *ml;
bool changed = false;
@ -254,7 +264,7 @@ static bool mball_select_similar_type(Object *obedit, MetaBall *mb, int type, co
mul_m3_v3(smat, radius_vec);
radius = (radius_vec[0] + radius_vec[1] + radius_vec[2]) / 3;
if (ED_select_similar_compare_float_tree(tree, radius, thresh, SIM_CMP_EQ)) {
if (ED_select_similar_compare_float_tree(tree_1d, radius, thresh, SIM_CMP_EQ)) {
select = true;
}
break;
@ -262,7 +272,7 @@ static bool mball_select_similar_type(Object *obedit, MetaBall *mb, int type, co
case SIMMBALL_STIFFNESS:
{
float s = ml->s;
if (ED_select_similar_compare_float_tree(tree, s, thresh, SIM_CMP_EQ)) {
if (ED_select_similar_compare_float_tree(tree_1d, s, thresh, SIM_CMP_EQ)) {
select = true;
}
break;
@ -278,7 +288,7 @@ static bool mball_select_similar_type(Object *obedit, MetaBall *mb, int type, co
float thresh_cos = cosf(thresh * (float)M_PI_2);
KDTreeNearest_3d nearest;
if (BLI_kdtree_3d_find_nearest(tree, dir, &nearest) != -1) {
if (BLI_kdtree_3d_find_nearest(tree_3d, dir, &nearest) != -1) {
float orient = angle_normalized_v3v3(dir, nearest.co);
/* Map to 0-1 to compare orientation. */
float delta = thresh_cos - fabsf(cosf(orient));
@ -311,10 +321,17 @@ static int mball_select_similar_exec(bContext *C, wmOperator *op)
tot_mball_selected_all = BKE_mball_select_count_multi(objects, objects_len);
short type_ref = 0;
KDTree_3d *tree = NULL;
KDTree_1d *tree_1d = NULL;
KDTree_3d *tree_3d = NULL;
if (type != SIMMBALL_TYPE) {
tree = BLI_kdtree_3d_new(tot_mball_selected_all);
switch (type) {
case SIMMBALL_RADIUS:
case SIMMBALL_STIFFNESS:
tree_1d = BLI_kdtree_1d_new(tot_mball_selected_all);
break;
case SIMMBALL_ROTATION:
tree_3d = BLI_kdtree_3d_new(tot_mball_selected_all);
break;
}
/* Get type of selected MetaBall */
@ -337,7 +354,7 @@ static int mball_select_similar_exec(bContext *C, wmOperator *op)
case SIMMBALL_RADIUS:
case SIMMBALL_STIFFNESS:
case SIMMBALL_ROTATION:
mball_select_similar_type_get(obedit, mb, type, tree);
mball_select_similar_type_get(obedit, mb, type, tree_1d, tree_3d);
break;
default:
BLI_assert(0);
@ -345,8 +362,11 @@ static int mball_select_similar_exec(bContext *C, wmOperator *op)
}
}
if (tree != NULL) {
BLI_kdtree_3d_balance(tree);
if (tree_1d != NULL) {
BLI_kdtree_1d_balance(tree_1d);
}
if (tree_3d != NULL) {
BLI_kdtree_3d_balance(tree_3d);
}
/* Select MetaBalls with desired type. */
for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
@ -370,7 +390,7 @@ static int mball_select_similar_exec(bContext *C, wmOperator *op)
case SIMMBALL_RADIUS:
case SIMMBALL_STIFFNESS:
case SIMMBALL_ROTATION:
changed = mball_select_similar_type(obedit, mb, type, tree, thresh);
changed = mball_select_similar_type(obedit, mb, type, tree_1d, tree_3d, thresh);
break;
default:
BLI_assert(0);
@ -384,8 +404,11 @@ static int mball_select_similar_exec(bContext *C, wmOperator *op)
}
MEM_freeN(objects);
if (tree != NULL) {
BLI_kdtree_3d_free(tree);
if (tree_1d != NULL) {
BLI_kdtree_1d_free(tree_1d);
}
if (tree_3d != NULL) {
BLI_kdtree_3d_free(tree_3d);
}
return OPERATOR_FINISHED;
}

View File

@ -97,7 +97,7 @@ int ED_select_similar_compare_float(const float delta, const float thresh, const
}
}
bool ED_select_similar_compare_float_tree(const KDTree_3d *tree, const float length, const float thresh, const int compare)
bool ED_select_similar_compare_float_tree(const KDTree_1d *tree, const float length, const float thresh, const int compare)
{
/* Length of the edge we want to compare against. */
float nearest_edge_length;
@ -123,9 +123,8 @@ bool ED_select_similar_compare_float_tree(const KDTree_3d *tree, const float len
return false;
}
KDTreeNearest_3d nearest;
float dummy[3] = {nearest_edge_length, 0.0f, 0.0f};
if (BLI_kdtree_3d_find_nearest(tree, dummy, &nearest) != -1) {
KDTreeNearest_1d nearest;
if (BLI_kdtree_1d_find_nearest(tree, &nearest_edge_length, &nearest) != -1) {
float delta = length - nearest.co[0];
return ED_select_similar_compare_float(delta, thresh, compare);
}