diff options
Diffstat (limited to 'source/blender/editors/transform/transform_snap_object.c')
-rw-r--r-- | source/blender/editors/transform/transform_snap_object.c | 667 |
1 files changed, 336 insertions, 331 deletions
diff --git a/source/blender/editors/transform/transform_snap_object.c b/source/blender/editors/transform/transform_snap_object.c index e5f6f207e3c..512f912a532 100644 --- a/source/blender/editors/transform/transform_snap_object.c +++ b/source/blender/editors/transform/transform_snap_object.c @@ -44,6 +44,7 @@ #include "BKE_duplilist.h" #include "BKE_editmesh.h" #include "BKE_geometry_set.h" +#include "BKE_global.h" #include "BKE_layer.h" #include "BKE_mesh.h" #include "BKE_mesh_runtime.h" @@ -145,9 +146,37 @@ struct SnapObjectContext { /** \name Utilities * \{ */ -static bool editmesh_eval_final_is_bmesh(const BMEditMesh *em) +/* Mesh used for snapping. + * If NULL the BMesh should be used. */ +static Mesh *mesh_for_snap(Object *ob_eval, eSnapEditType edit_mode_type, bool *r_use_hide) { - return (em->mesh_eval_final->runtime.wrapper_type == ME_WRAPPER_TYPE_BMESH); + Mesh *me_eval = ob_eval->data; + bool use_hide = false; + if (BKE_object_is_in_editmode(ob_eval)) { + if (edit_mode_type == SNAP_GEOM_EDIT) { + return NULL; + } + + BMEditMesh *em_eval = BKE_editmesh_from_object(ob_eval); + if ((edit_mode_type == SNAP_GEOM_FINAL) && em_eval->mesh_eval_final) { + if (em_eval->mesh_eval_final->runtime.wrapper_type == ME_WRAPPER_TYPE_BMESH) { + return NULL; + } + me_eval = em_eval->mesh_eval_final; + use_hide = true; + } + else if ((edit_mode_type == SNAP_GEOM_CAGE) && em_eval->mesh_eval_cage) { + if (em_eval->mesh_eval_cage->runtime.wrapper_type == ME_WRAPPER_TYPE_BMESH) { + return NULL; + } + me_eval = em_eval->mesh_eval_cage; + use_hide = true; + } + } + if (r_use_hide) { + *r_use_hide = use_hide; + } + return me_eval; } /** \} */ @@ -209,30 +238,69 @@ static void snap_object_data_clear(SnapObjectData *sod) memset(&sod->type, 0x0, sizeof(*sod) - offsetof(SnapObjectData, type)); } -static SnapObjectData *snap_object_data_lookup(SnapObjectContext *sctx, Object *ob) +static SnapObjectData *snap_object_data_lookup(SnapObjectContext *sctx, Object *ob_eval) { - SnapObjectData *sod = BLI_ghash_lookup(sctx->cache.object_map, ob); + SnapObjectData *sod = BLI_ghash_lookup(sctx->cache.object_map, ob_eval); if (sod == NULL) { if (sctx->cache.data_to_object_map != NULL) { - ob = BLI_ghash_lookup(sctx->cache.data_to_object_map, ob->data); + ob_eval = BLI_ghash_lookup(sctx->cache.data_to_object_map, ob_eval->data); /* Could be NULl when mixing edit-mode and non edit-mode objects. */ - if (ob != NULL) { - sod = BLI_ghash_lookup(sctx->cache.object_map, ob); + if (ob_eval != NULL) { + sod = BLI_ghash_lookup(sctx->cache.object_map, ob_eval); } } } return sod; } -static SnapObjectData *snap_object_data_mesh_get(SnapObjectContext *sctx, Object *ob) +static SnapObjectData *snap_object_data_mesh_get(SnapObjectContext *sctx, + Object *ob_eval, + Mesh *me_eval, + bool use_hide) { SnapObjectData *sod; void **sod_p; bool init = false; - if (BLI_ghash_ensure_p(sctx->cache.object_map, ob, &sod_p)) { + if (BLI_ghash_ensure_p(sctx->cache.object_map, ob_eval, &sod_p)) { sod = *sod_p; + bool is_dirty = false; if (sod->type != SNAP_MESH) { + is_dirty = true; + } + else if (sod->treedata_mesh.tree && sod->treedata_mesh.cached && + !bvhcache_has_tree(me_eval->runtime.bvh_cache, sod->treedata_mesh.tree)) { + /* The tree is owned by the Mesh and may have been freed since we last used. */ + is_dirty = true; + } + else if (sod->bvhtree[0] && sod->cached[0] && + !bvhcache_has_tree(me_eval->runtime.bvh_cache, sod->bvhtree[0])) { + /* The tree is owned by the Mesh and may have been freed since we last used. */ + is_dirty = true; + } + else if (sod->bvhtree[1] && sod->cached[1] && + !bvhcache_has_tree(me_eval->runtime.bvh_cache, sod->bvhtree[1])) { + /* The tree is owned by the Mesh and may have been freed since we last used. */ + is_dirty = true; + } + else if (!sod->treedata_mesh.looptri_allocated && + sod->treedata_mesh.looptri != me_eval->runtime.looptris.array) { + is_dirty = true; + } + else if (!sod->treedata_mesh.vert_allocated && sod->treedata_mesh.vert != me_eval->mvert) { + is_dirty = true; + } + else if (!sod->treedata_mesh.loop_allocated && sod->treedata_mesh.loop != me_eval->mloop) { + is_dirty = true; + } + else if (!sod->treedata_mesh.edge_allocated && sod->treedata_mesh.edge != me_eval->medge) { + is_dirty = true; + } + else if (sod->poly != me_eval->mpoly) { + is_dirty = true; + } + + if (is_dirty) { snap_object_data_clear(sod); init = true; } @@ -244,8 +312,32 @@ static SnapObjectData *snap_object_data_mesh_get(SnapObjectContext *sctx, Object if (init) { sod->type = SNAP_MESH; - /* start assuming that it has each of these element types */ - sod->has_looptris = true; + + /* The BVHTree from looptris is always required. */ + BLI_assert(sod->treedata_mesh.tree == NULL); + BKE_bvhtree_from_mesh_get(&sod->treedata_mesh, + me_eval, + use_hide ? BVHTREE_FROM_LOOPTRI_NO_HIDDEN : BVHTREE_FROM_LOOPTRI, + 4); + + if (sod->treedata_mesh.tree == NULL) { + sod->treedata_mesh.vert = me_eval->mvert; + sod->treedata_mesh.loop = me_eval->mloop; + sod->treedata_mesh.looptri = BKE_mesh_runtime_looptri_ensure(me_eval); + BLI_assert(sod->has_looptris == false); + } + else { + BLI_assert(sod->treedata_mesh.vert != NULL); + BLI_assert(sod->treedata_mesh.loop != NULL); + BLI_assert(sod->treedata_mesh.looptri != NULL); + sod->has_looptris = true; + } + + /* Required for snapping with occlusion. */ + sod->treedata_mesh.edge = me_eval->medge; + sod->poly = me_eval->mpoly; + + /* Start assuming that it has each of these element types. */ sod->has_loose_edge = true; sod->has_loose_vert = true; } @@ -253,26 +345,26 @@ static SnapObjectData *snap_object_data_mesh_get(SnapObjectContext *sctx, Object return sod; } -static struct Mesh_Runtime *snap_object_data_editmesh_runtime_get(Object *ob) +static struct Mesh_Runtime *snap_object_data_editmesh_runtime_get(Object *ob_eval) { - BMEditMesh *em = BKE_editmesh_from_object(ob); - if (em->mesh_eval_final) { - return &em->mesh_eval_final->runtime; + BMEditMesh *em_eval = BKE_editmesh_from_object(ob_eval); + if (em_eval->mesh_eval_final) { + return &em_eval->mesh_eval_final->runtime; } - if (em->mesh_eval_cage) { - return &em->mesh_eval_cage->runtime; + if (em_eval->mesh_eval_cage) { + return &em_eval->mesh_eval_cage->runtime; } - return &((Mesh *)ob->data)->runtime; + return &((Mesh *)ob_eval->data)->runtime; } static SnapObjectData *snap_object_data_editmesh_get(SnapObjectContext *sctx, - Object *ob, + Object *ob_eval, BMEditMesh *em) { SnapObjectData *sod; void **sod_p; - bool init = false, init_min_max = true, clear_cache = false; + bool init = false; { /* Use object-data as the key in ghash since the editmesh @@ -281,48 +373,53 @@ static SnapObjectData *snap_object_data_editmesh_get(SnapObjectContext *sctx, sctx->cache.data_to_object_map = BLI_ghash_ptr_new(__func__); } void **ob_p; - if (BLI_ghash_ensure_p(sctx->cache.data_to_object_map, ob->data, &ob_p)) { - ob = *ob_p; + if (BLI_ghash_ensure_p(sctx->cache.data_to_object_map, ob_eval->data, &ob_p)) { + ob_eval = *ob_p; } else { - *ob_p = ob; + *ob_p = ob_eval; } } - if (BLI_ghash_ensure_p(sctx->cache.object_map, ob, &sod_p)) { + if (BLI_ghash_ensure_p(sctx->cache.object_map, ob_eval, &sod_p)) { sod = *sod_p; - bool clear = false; + bool is_dirty = false; /* Check if the geometry has changed. */ if (sod->type != SNAP_EDIT_MESH) { - clear = true; + is_dirty = true; } else if (sod->treedata_editmesh.em != em) { - clear_cache = true; - init = true; + is_dirty = true; } else if (sod->mesh_runtime) { - if (sod->mesh_runtime != snap_object_data_editmesh_runtime_get(ob)) { - clear_cache = true; - init = true; + if (sod->mesh_runtime != snap_object_data_editmesh_runtime_get(ob_eval)) { + if (G.moving) { + /* Hack to avoid updating while transforming. */ + BLI_assert(!sod->treedata_editmesh.cached && !sod->cached[0] && !sod->cached[1]); + sod->mesh_runtime = snap_object_data_editmesh_runtime_get(ob_eval); + } + else { + is_dirty = true; + } } else if (sod->treedata_editmesh.tree && sod->treedata_editmesh.cached && !bvhcache_has_tree(sod->mesh_runtime->bvh_cache, sod->treedata_editmesh.tree)) { /* The tree is owned by the EditMesh and may have been freed since we last used! */ - clear = true; + is_dirty = true; } else if (sod->bvhtree[0] && sod->cached[0] && !bvhcache_has_tree(sod->mesh_runtime->bvh_cache, sod->bvhtree[0])) { /* The tree is owned by the EditMesh and may have been freed since we last used! */ - clear = true; + is_dirty = true; } else if (sod->bvhtree[1] && sod->cached[1] && !bvhcache_has_tree(sod->mesh_runtime->bvh_cache, sod->bvhtree[1])) { /* The tree is owned by the EditMesh and may have been freed since we last used! */ - clear = true; + is_dirty = true; } } - if (clear) { + if (is_dirty) { snap_object_data_clear(sod); init = true; } @@ -335,27 +432,8 @@ static SnapObjectData *snap_object_data_editmesh_get(SnapObjectContext *sctx, if (init) { sod->type = SNAP_EDIT_MESH; sod->treedata_editmesh.em = em; - - if (clear_cache) { - /* Only init min and max when you have a non-custom bvhtree pending. */ - init_min_max = false; - if (sod->treedata_editmesh.cached) { - sod->treedata_editmesh.tree = NULL; - init_min_max = true; - } - for (int i = 0; i < ARRAY_SIZE(sod->bvhtree); i++) { - if (sod->cached[i]) { - sod->bvhtree[i] = NULL; - init_min_max = true; - } - } - } - - if (init_min_max) { - bm_mesh_minmax(em->bm, sod->min, sod->max); - } - - sod->mesh_runtime = snap_object_data_editmesh_runtime_get(ob); + sod->mesh_runtime = snap_object_data_editmesh_runtime_get(ob_eval); + bm_mesh_minmax(em->bm, sod->min, sod->max); } return sod; @@ -368,9 +446,9 @@ static SnapObjectData *snap_object_data_editmesh_get(SnapObjectContext *sctx, * \{ */ typedef void (*IterSnapObjsCallback)(SnapObjectContext *sctx, - Object *ob, + Object *ob_eval, float obmat[4][4], - bool use_obedit, + eSnapEditType edit_mode_type, bool use_backface_culling, bool is_object_active, void *data); @@ -387,10 +465,17 @@ static void iter_snap_objects(SnapObjectContext *sctx, ViewLayer *view_layer = DEG_get_input_view_layer(depsgraph); const View3D *v3d = sctx->v3d_data.v3d; const eSnapSelect snap_select = params->snap_select; - const bool use_object_edit_cage = params->use_object_edit_cage; + const eSnapEditType edit_mode_type = params->edit_mode_type; const bool use_backface_culling = params->use_backface_culling; Base *base_act = view_layer->basact; + if (snap_select == SNAP_ONLY_ACTIVE) { + Object *obj_eval = DEG_get_evaluated_object(depsgraph, base_act->object); + sob_callback( + sctx, obj_eval, obj_eval->obmat, edit_mode_type, use_backface_culling, true, data); + return; + } + for (Base *base = view_layer->object_bases.first; base != NULL; base = base->next) { if (!BASE_VISIBLE(v3d, base)) { continue; @@ -425,7 +510,7 @@ static void iter_snap_objects(SnapObjectContext *sctx, sob_callback(sctx, dupli_ob->ob, dupli_ob->mat, - use_object_edit_cage, + edit_mode_type, use_backface_culling, is_object_active, data); @@ -436,7 +521,7 @@ static void iter_snap_objects(SnapObjectContext *sctx, sob_callback(sctx, obj_eval, obj_eval->obmat, - use_object_edit_cage, + edit_mode_type, use_backface_culling, is_object_active, data); @@ -464,7 +549,7 @@ struct RayCastAll_Data { float len_diff; float local_scale; - Object *ob; + Object *ob_eval; uint ob_uuid; /* output data */ @@ -476,7 +561,7 @@ static struct SnapObjectHitDepth *hit_depth_create(const float depth, const float co[3], const float no[3], int index, - Object *ob, + Object *ob_eval, const float obmat[4][4], uint ob_uuid) { @@ -487,7 +572,7 @@ static struct SnapObjectHitDepth *hit_depth_create(const float depth, copy_v3_v3(hit->no, no); hit->index = index; - hit->ob = ob; + hit->ob_eval = ob_eval; copy_m4_m4(hit->obmat, (float(*)[4])obmat); hit->ob_uuid = ob_uuid; @@ -529,7 +614,7 @@ static void raycast_all_cb(void *userdata, int index, const BVHTreeRay *ray, BVH normalize_v3(normal); struct SnapObjectHitDepth *hit_item = hit_depth_create( - depth, location, normal, hit->index, data->ob, data->obmat, data->ob_uuid); + depth, location, normal, hit->index, data->ob_eval, data->obmat, data->ob_uuid); BLI_addtail(data->hit_list, hit_item); } } @@ -601,8 +686,8 @@ static void editmesh_looptri_raycast_backface_culling_cb(void *userdata, static bool raycastMesh(SnapObjectContext *sctx, const float ray_start[3], const float ray_dir[3], - Object *ob, - Mesh *me, + Object *ob_eval, + Mesh *me_eval, const float obmat[4][4], const uint ob_index, bool use_hide, @@ -617,7 +702,7 @@ static bool raycastMesh(SnapObjectContext *sctx, { bool retval = false; - if (me->totpoly == 0) { + if (me_eval->totpoly == 0) { return retval; } @@ -641,7 +726,7 @@ static bool raycastMesh(SnapObjectContext *sctx, } /* Test BoundBox */ - BoundBox *bb = BKE_mesh_boundbox_get(ob); + BoundBox *bb = BKE_mesh_boundbox_get(ob_eval); if (bb) { /* was BKE_boundbox_ray_hit_check, see: cf6ca226fa58 */ if (!isect_ray_aabb_v3_simple( @@ -661,53 +746,18 @@ static bool raycastMesh(SnapObjectContext *sctx, len_diff = 0.0f; } - SnapObjectData *sod = snap_object_data_mesh_get(sctx, ob); + SnapObjectData *sod = snap_object_data_mesh_get(sctx, ob_eval, me_eval, use_hide); BVHTreeFromMesh *treedata = &sod->treedata_mesh; - /* The tree is owned by the Mesh and may have been freed since we last used. */ - if (treedata->tree) { - BLI_assert(treedata->cached); - if (!bvhcache_has_tree(me->runtime.bvh_cache, treedata->tree)) { - free_bvhtree_from_mesh(treedata); - } - else { - /* Update Pointers. */ - if (treedata->vert && treedata->vert_allocated == false) { - treedata->vert = me->mvert; - } - if (treedata->loop && treedata->loop_allocated == false) { - treedata->loop = me->mloop; - } - if (treedata->looptri && treedata->looptri_allocated == false) { - treedata->looptri = BKE_mesh_runtime_looptri_ensure(me); - } - /* required for snapping with occlusion. */ - treedata->edge = me->medge; - sod->poly = me->mpoly; - } - } - if (treedata->tree == NULL) { - if (use_hide) { - BKE_bvhtree_from_mesh_get(treedata, me, BVHTREE_FROM_LOOPTRI_NO_HIDDEN, 4); - } - else { - BKE_bvhtree_from_mesh_get(treedata, me, BVHTREE_FROM_LOOPTRI, 4); - } - - /* required for snapping with occlusion. */ - treedata->edge = me->medge; - sod->poly = me->mpoly; - - if (treedata->tree == NULL) { - return retval; - } + return retval; } float timat[3][3]; /* transpose inverse matrix for normals */ transpose_m3_m4(timat, imat); + BLI_assert(treedata->raycast_callback != NULL); if (r_hit_list) { struct RayCastAll_Data data; @@ -717,7 +767,7 @@ static bool raycastMesh(SnapObjectContext *sctx, data.timat = timat; data.len_diff = len_diff; data.local_scale = local_scale; - data.ob = ob; + data.ob_eval = ob_eval; data.ob_uuid = ob_index; data.hit_list = r_hit_list; data.retval = retval; @@ -776,7 +826,7 @@ static bool raycastMesh(SnapObjectContext *sctx, static bool raycastEditMesh(SnapObjectContext *sctx, const float ray_start[3], const float ray_dir[3], - Object *ob, + Object *ob_eval, BMEditMesh *em, const float obmat[4][4], const uint ob_index, @@ -813,7 +863,7 @@ static bool raycastEditMesh(SnapObjectContext *sctx, local_depth *= local_scale; } - SnapObjectData *sod = snap_object_data_editmesh_get(sctx, ob, em); + SnapObjectData *sod = snap_object_data_editmesh_get(sctx, ob_eval, em); /* Test BoundBox */ @@ -839,7 +889,8 @@ static bool raycastEditMesh(SnapObjectContext *sctx, if (treedata->tree == NULL) { /* Operators only update the editmesh looptris of the original mesh. */ - BLI_assert(sod->treedata_editmesh.em == BKE_editmesh_from_object(DEG_get_original_object(ob))); + BLI_assert(sod->treedata_editmesh.em == + BKE_editmesh_from_object(DEG_get_original_object(ob_eval))); em = sod->treedata_editmesh.em; if (sctx->callbacks.edit_mesh.test_face_fn) { @@ -886,7 +937,7 @@ static bool raycastEditMesh(SnapObjectContext *sctx, data.timat = timat; data.len_diff = len_diff; data.local_scale = local_scale; - data.ob = ob; + data.ob_eval = ob_eval; data.ob_uuid = ob_index; data.hit_list = r_hit_list; data.retval = retval; @@ -967,9 +1018,9 @@ struct RaycastObjUserData { * \note Duplicate args here are documented at #snapObjectsRay */ static void raycast_obj_fn(SnapObjectContext *sctx, - Object *ob, + Object *ob_eval, float obmat[4][4], - bool use_obedit, + eSnapEditType edit_mode_type, bool use_backface_culling, bool is_object_active, void *data) @@ -982,53 +1033,46 @@ static void raycast_obj_fn(SnapObjectContext *sctx, bool retval = false; if (use_occlusion_test) { - if (use_obedit && sctx->use_v3d && XRAY_FLAG_ENABLED(sctx->v3d_data.v3d)) { + if ((edit_mode_type == SNAP_GEOM_EDIT) && sctx->use_v3d && + XRAY_FLAG_ENABLED(sctx->v3d_data.v3d)) { /* Use of occlude geometry in editing mode disabled. */ return; } - if (ELEM(ob->dt, OB_BOUNDBOX, OB_WIRE)) { + if (ELEM(ob_eval->dt, OB_BOUNDBOX, OB_WIRE)) { /* Do not hit objects that are in wire or bounding box * display mode. */ return; } } - switch (ob->type) { + switch (ob_eval->type) { case OB_MESH: { - Mesh *me = ob->data; bool use_hide = false; - if (BKE_object_is_in_editmode(ob)) { - if (use_obedit || editmesh_eval_final_is_bmesh(me->edit_mesh)) { - /* Operators only update the editmesh looptris of the original mesh. */ - BMEditMesh *em_orig = BKE_editmesh_from_object(DEG_get_original_object(ob)); - retval = raycastEditMesh(sctx, - dt->ray_start, - dt->ray_dir, - ob, - em_orig, - obmat, - ob_index, - use_backface_culling, - ray_depth, - dt->r_loc, - dt->r_no, - dt->r_index, - dt->r_hit_list); - break; - } - - BMEditMesh *em = BKE_editmesh_from_object(ob); - if (em->mesh_eval_final) { - me = em->mesh_eval_final; - use_hide = true; - } + Mesh *me_eval = mesh_for_snap(ob_eval, edit_mode_type, &use_hide); + if (me_eval == NULL) { + /* Operators only update the editmesh looptris of the original mesh. */ + BMEditMesh *em_orig = BKE_editmesh_from_object(DEG_get_original_object(ob_eval)); + retval = raycastEditMesh(sctx, + dt->ray_start, + dt->ray_dir, + ob_eval, + em_orig, + obmat, + ob_index, + use_backface_culling, + ray_depth, + dt->r_loc, + dt->r_no, + dt->r_index, + dt->r_hit_list); + break; } retval = raycastMesh(sctx, dt->ray_start, dt->ray_dir, - ob, - me, + ob_eval, + me_eval, obmat, ob_index, use_hide, @@ -1044,12 +1088,12 @@ static void raycast_obj_fn(SnapObjectContext *sctx, case OB_SURF: case OB_FONT: { if (!is_object_active) { - Mesh *mesh_eval = BKE_object_get_evaluated_mesh(ob); + Mesh *mesh_eval = BKE_object_get_evaluated_mesh(ob_eval); if (mesh_eval) { retval = raycastMesh(sctx, dt->ray_start, dt->ray_dir, - ob, + ob_eval, mesh_eval, obmat, ob_index, @@ -1068,7 +1112,7 @@ static void raycast_obj_fn(SnapObjectContext *sctx, if (retval) { if (dt->r_ob) { - *dt->r_ob = ob; + *dt->r_ob = ob_eval; } if (dt->r_obmat) { copy_m4_m4(dt->r_obmat, obmat); @@ -1323,6 +1367,33 @@ typedef struct Nearest2dUserData { bool use_backface_culling; } Nearest2dUserData; +static void nearest2d_data_init(SnapObjectData *sod, + bool is_persp, + bool use_backface_culling, + Nearest2dUserData *r_nearest2d) +{ + if (sod->type == SNAP_MESH) { + r_nearest2d->userdata = &sod->treedata_mesh; + r_nearest2d->get_vert_co = (Nearest2DGetVertCoCallback)cb_mvert_co_get; + r_nearest2d->get_edge_verts_index = (Nearest2DGetEdgeVertsCallback)cb_medge_verts_get; + r_nearest2d->copy_vert_no = (Nearest2DCopyVertNoCallback)cb_mvert_no_copy; + r_nearest2d->get_tri_verts_index = (Nearest2DGetTriVertsCallback)cb_mlooptri_verts_get; + r_nearest2d->get_tri_edges_index = (Nearest2DGetTriEdgesCallback)cb_mlooptri_edges_get; + } + else { + BLI_assert(sod->type == SNAP_EDIT_MESH); + r_nearest2d->userdata = sod->treedata_editmesh.em; + r_nearest2d->get_vert_co = (Nearest2DGetVertCoCallback)cb_bvert_co_get; + r_nearest2d->get_edge_verts_index = (Nearest2DGetEdgeVertsCallback)cb_bedge_verts_get; + r_nearest2d->copy_vert_no = (Nearest2DCopyVertNoCallback)cb_bvert_no_copy; + r_nearest2d->get_tri_verts_index = NULL; + r_nearest2d->get_tri_edges_index = NULL; + } + + r_nearest2d->is_persp = is_persp; + r_nearest2d->use_backface_culling = use_backface_culling; +} + static void cb_snap_vert(void *userdata, int index, const struct DistProjectedAABBPrecalc *precalc, @@ -1470,7 +1541,7 @@ static void cb_snap_tri_verts(void *userdata, static short snap_mesh_polygon(SnapObjectContext *sctx, SnapData *snapdata, - Object *ob, + Object *ob_eval, const float obmat[4][4], bool use_backface_culling, /* read/write args */ @@ -1495,28 +1566,21 @@ static short snap_mesh_polygon(SnapObjectContext *sctx, mul_v4_m4v4(clip_planes_local[i], tobmat, snapdata->clip_plane[i]); } - Nearest2dUserData nearest2d = { - .is_persp = snapdata->view_proj == VIEW_PROJ_PERSP, - .use_backface_culling = use_backface_culling, - }; - BVHTreeNearest nearest = { .index = -1, .dist_sq = square_f(*dist_px), }; - SnapObjectData *sod = snap_object_data_lookup(sctx, ob); - + SnapObjectData *sod = snap_object_data_lookup(sctx, ob_eval); BLI_assert(sod != NULL); + Nearest2dUserData nearest2d; + nearest2d_data_init( + sod, snapdata->view_proj == VIEW_PROJ_PERSP, use_backface_culling, &nearest2d); + if (sod->type == SNAP_MESH) { BVHTreeFromMesh *treedata = &sod->treedata_mesh; - nearest2d.userdata = treedata; - nearest2d.get_vert_co = (Nearest2DGetVertCoCallback)cb_mvert_co_get; - nearest2d.get_edge_verts_index = (Nearest2DGetEdgeVertsCallback)cb_medge_verts_get; - nearest2d.copy_vert_no = (Nearest2DCopyVertNoCallback)cb_mvert_no_copy; - const MPoly *mp = &sod->poly[*r_index]; const MLoop *ml = &treedata->loop[mp->loopstart]; if (snapdata->snap_to_flag & SCE_SNAP_MODE_EDGE) { @@ -1547,11 +1611,6 @@ static short snap_mesh_polygon(SnapObjectContext *sctx, BLI_assert(sod->type == SNAP_EDIT_MESH); BMEditMesh *em = sod->treedata_editmesh.em; - nearest2d.userdata = em; - nearest2d.get_vert_co = (Nearest2DGetVertCoCallback)cb_bvert_co_get; - nearest2d.get_edge_verts_index = (Nearest2DGetEdgeVertsCallback)cb_bedge_verts_get; - nearest2d.copy_vert_no = (Nearest2DCopyVertNoCallback)cb_bvert_no_copy; - BM_mesh_elem_table_ensure(em->bm, BM_FACE); BMFace *f = BM_face_at_index(em->bm, *r_index); BMLoop *l_iter, *l_first; @@ -1608,7 +1667,7 @@ static short snap_mesh_polygon(SnapObjectContext *sctx, static short snap_mesh_edge_verts_mixed(SnapObjectContext *sctx, SnapData *snapdata, - Object *ob, + Object *ob_eval, const float obmat[4][4], float original_dist_px, const float prev_co[3], @@ -1622,32 +1681,16 @@ static short snap_mesh_edge_verts_mixed(SnapObjectContext *sctx, { short elem = SCE_SNAP_MODE_EDGE; - if (ob->type != OB_MESH) { + if (ob_eval->type != OB_MESH) { return elem; } - SnapObjectData *sod = snap_object_data_lookup(sctx, ob); - + SnapObjectData *sod = snap_object_data_lookup(sctx, ob_eval); BLI_assert(sod != NULL); Nearest2dUserData nearest2d; - { - nearest2d.is_persp = snapdata->view_proj == VIEW_PROJ_PERSP; - nearest2d.use_backface_culling = use_backface_culling; - if (sod->type == SNAP_MESH) { - nearest2d.userdata = &sod->treedata_mesh; - nearest2d.get_vert_co = (Nearest2DGetVertCoCallback)cb_mvert_co_get; - nearest2d.get_edge_verts_index = (Nearest2DGetEdgeVertsCallback)cb_medge_verts_get; - nearest2d.copy_vert_no = (Nearest2DCopyVertNoCallback)cb_mvert_no_copy; - } - else { - BLI_assert(sod->type == SNAP_EDIT_MESH); - nearest2d.userdata = sod->treedata_editmesh.em; - nearest2d.get_vert_co = (Nearest2DGetVertCoCallback)cb_bvert_co_get; - nearest2d.get_edge_verts_index = (Nearest2DGetEdgeVertsCallback)cb_bedge_verts_get; - nearest2d.copy_vert_no = (Nearest2DCopyVertNoCallback)cb_bvert_no_copy; - } - } + nearest2d_data_init( + sod, snapdata->view_proj == VIEW_PROJ_PERSP, use_backface_culling, &nearest2d); int vindex[2]; nearest2d.get_edge_verts_index(*r_index, vindex, nearest2d.userdata); @@ -1772,9 +1815,8 @@ static short snap_mesh_edge_verts_mixed(SnapObjectContext *sctx, } static short snapArmature(SnapData *snapdata, - Object *ob, + Object *ob_eval, const float obmat[4][4], - bool use_obedit, /* read/write args */ float *dist_px, /* return args */ @@ -1795,11 +1837,11 @@ static short snapArmature(SnapData *snapdata, dist_squared_to_projected_aabb_precalc( &neasrest_precalc, lpmat, snapdata->win_size, snapdata->mval); - use_obedit = use_obedit && BKE_object_is_in_editmode(ob); + bool use_obedit = ((bArmature *)ob_eval->data)->edbo != NULL; if (use_obedit == false) { /* Test BoundBox */ - BoundBox *bb = BKE_armature_boundbox_get(ob); + BoundBox *bb = BKE_armature_boundbox_get(ob_eval); if (bb && !snap_bound_box_check_dist( bb->vec[0], bb->vec[6], lpmat, snapdata->win_size, snapdata->mval, dist_px_sq)) { return retval; @@ -1814,7 +1856,7 @@ static short snapArmature(SnapData *snapdata, bool is_persp = snapdata->view_proj == VIEW_PROJ_PERSP; - bArmature *arm = ob->data; + bArmature *arm = ob_eval->data; if (arm->edbo) { LISTBASE_FOREACH (EditBone *, eBone, arm->edbo) { if (eBone->layer & arm->layer) { @@ -1858,8 +1900,8 @@ static short snapArmature(SnapData *snapdata, } } } - else if (ob->pose && ob->pose->chanbase.first) { - LISTBASE_FOREACH (bPoseChannel *, pchan, &ob->pose->chanbase) { + else if (ob_eval->pose && ob_eval->pose->chanbase.first) { + LISTBASE_FOREACH (bPoseChannel *, pchan, &ob_eval->pose->chanbase) { Bone *bone = pchan->bone; /* skip hidden bones */ if (bone && !(bone->flag & (BONE_HIDDEN_P | BONE_HIDDEN_PG))) { @@ -1917,7 +1959,7 @@ static short snapArmature(SnapData *snapdata, } static short snapCurve(SnapData *snapdata, - Object *ob, + Object *ob_eval, const float obmat[4][4], bool use_obedit, /* read/write args */ @@ -1934,7 +1976,7 @@ static short snapCurve(SnapData *snapdata, return 0; } - Curve *cu = ob->data; + Curve *cu = ob_eval->data; float dist_px_sq = square_f(*dist_px); float lpmat[4][4]; @@ -1944,11 +1986,11 @@ static short snapCurve(SnapData *snapdata, dist_squared_to_projected_aabb_precalc( &neasrest_precalc, lpmat, snapdata->win_size, snapdata->mval); - use_obedit = use_obedit && BKE_object_is_in_editmode(ob); + use_obedit = use_obedit && BKE_object_is_in_editmode(ob_eval); if (use_obedit == false) { /* Test BoundBox */ - BoundBox *bb = BKE_curve_boundbox_get(ob); + BoundBox *bb = BKE_curve_boundbox_get(ob_eval); if (bb && !snap_bound_box_check_dist( bb->vec[0], bb->vec[6], lpmat, snapdata->win_size, snapdata->mval, dist_px_sq)) { return 0; @@ -2068,7 +2110,7 @@ static short snapCurve(SnapData *snapdata, /* may extend later (for now just snaps to empty center) */ static short snap_object_center(SnapData *snapdata, - Object *ob, + Object *ob_eval, const float obmat[4][4], /* read/write args */ float *dist_px, @@ -2079,7 +2121,7 @@ static short snap_object_center(SnapData *snapdata, { short retval = 0; - if (ob->transflag & OB_DUPLI) { + if (ob_eval->transflag & OB_DUPLI) { return retval; } @@ -2222,9 +2264,10 @@ static short snapCamera(const SnapObjectContext *sctx, static short snapMesh(SnapObjectContext *sctx, SnapData *snapdata, - Object *ob, - Mesh *me, + Object *ob_eval, + Mesh *me_eval, const float obmat[4][4], + bool use_hide, bool use_backface_culling, /* read/write args */ float *dist_px, @@ -2234,10 +2277,10 @@ static short snapMesh(SnapObjectContext *sctx, int *r_index) { BLI_assert(snapdata->snap_to_flag != SCE_SNAP_MODE_FACE); - if (me->totvert == 0) { + if (me_eval->totvert == 0) { return 0; } - if (me->totedge == 0 && !(snapdata->snap_to_flag & SCE_SNAP_MODE_VERTEX)) { + if (me_eval->totedge == 0 && !(snapdata->snap_to_flag & SCE_SNAP_MODE_VERTEX)) { return 0; } @@ -2247,65 +2290,46 @@ static short snapMesh(SnapObjectContext *sctx, float dist_px_sq = square_f(*dist_px); /* Test BoundBox */ - BoundBox *bb = BKE_mesh_boundbox_get(ob); + BoundBox *bb = BKE_mesh_boundbox_get(ob_eval); if (bb && !snap_bound_box_check_dist( bb->vec[0], bb->vec[6], lpmat, snapdata->win_size, snapdata->mval, dist_px_sq)) { return 0; } - SnapObjectData *sod = snap_object_data_mesh_get(sctx, ob); + SnapObjectData *sod = snap_object_data_mesh_get(sctx, ob_eval, me_eval, use_hide); - BVHTreeFromMesh *treedata, dummy_treedata; + BVHTreeFromMesh *treedata, treedata_tmp; treedata = &sod->treedata_mesh; - /* The tree is owned by the Mesh and may have been freed since we last used! */ - if (treedata->cached && treedata->tree && - !bvhcache_has_tree(me->runtime.bvh_cache, treedata->tree)) { - free_bvhtree_from_mesh(treedata); - } - if (sod->cached[0] && sod->bvhtree[0] && - !bvhcache_has_tree(me->runtime.bvh_cache, sod->bvhtree[0])) { - sod->bvhtree[0] = NULL; - } - if (sod->cached[1] && sod->bvhtree[1] && - !bvhcache_has_tree(me->runtime.bvh_cache, sod->bvhtree[1])) { - sod->bvhtree[1] = NULL; - } - - if (sod->has_looptris && treedata->tree == NULL) { - BKE_bvhtree_from_mesh_get(treedata, me, BVHTREE_FROM_LOOPTRI, 4); - sod->has_looptris = (treedata->tree != NULL); - if (sod->has_looptris) { - /* Make sure that the array of edges is referenced in the callbacks. */ - treedata->edge = me->medge; /* CustomData_get_layer(&me->edata, CD_MEDGE);? */ - } - } if (sod->has_loose_edge && sod->bvhtree[0] == NULL) { - sod->bvhtree[0] = BKE_bvhtree_from_mesh_get(&dummy_treedata, me, BVHTREE_FROM_LOOSEEDGES, 2); - sod->has_loose_edge = sod->bvhtree[0] != NULL; - sod->cached[0] = dummy_treedata.cached; - - if (sod->has_loose_edge) { - BLI_assert(treedata->vert_allocated == false); - treedata->vert = dummy_treedata.vert; - treedata->vert_allocated = dummy_treedata.vert_allocated; - - BLI_assert(treedata->edge_allocated == false); - treedata->edge = dummy_treedata.edge; - treedata->edge_allocated = dummy_treedata.edge_allocated; + sod->bvhtree[0] = BKE_bvhtree_from_mesh_get( + &treedata_tmp, me_eval, BVHTREE_FROM_LOOSEEDGES, 2); + if (sod->bvhtree[0] == NULL) { + sod->has_loose_edge = false; } + sod->cached[0] = treedata_tmp.cached; + BLI_assert(!ELEM(true, + treedata_tmp.vert_allocated, + treedata_tmp.edge_allocated, + treedata_tmp.face_allocated, + treedata_tmp.loop_allocated, + treedata_tmp.looptri_allocated)); } + if (snapdata->snap_to_flag & SCE_SNAP_MODE_VERTEX) { if (sod->has_loose_vert && sod->bvhtree[1] == NULL) { - sod->bvhtree[1] = BKE_bvhtree_from_mesh_get(&dummy_treedata, me, BVHTREE_FROM_LOOSEVERTS, 2); - sod->has_loose_vert = sod->bvhtree[1] != NULL; - sod->cached[1] = dummy_treedata.cached; - - if (sod->has_loose_vert) { - BLI_assert(treedata->vert_allocated == false); - treedata->vert = dummy_treedata.vert; - treedata->vert_allocated = dummy_treedata.vert_allocated; + sod->bvhtree[1] = BKE_bvhtree_from_mesh_get( + &treedata_tmp, me_eval, BVHTREE_FROM_LOOSEVERTS, 2); + if (sod->bvhtree[1] == NULL) { + sod->has_loose_vert = false; } + sod->cached[1] = treedata_tmp.cached; + BLI_assert(!ELEM(true, + treedata_tmp.vert_allocated, + treedata_tmp.edge_allocated, + treedata_tmp.face_allocated, + treedata_tmp.loop_allocated, + treedata_tmp.looptri_allocated)); } } else { @@ -2313,33 +2337,9 @@ static short snapMesh(SnapObjectContext *sctx, sod->has_loose_vert = false; } - /* Update pointers. */ - if (treedata->vert_allocated == false) { - treedata->vert = me->mvert; /* CustomData_get_layer(&me->vdata, CD_MVERT);? */ - } - if (treedata->tree || sod->bvhtree[0]) { - if (treedata->edge_allocated == false) { - /* If raycast has been executed before, `treedata->edge` can be NULL. */ - treedata->edge = me->medge; /* CustomData_get_layer(&me->edata, CD_MEDGE);? */ - } - if (treedata->loop && treedata->loop_allocated == false) { - treedata->loop = me->mloop; /* CustomData_get_layer(&me->edata, CD_MLOOP);? */ - } - if (treedata->looptri && treedata->looptri_allocated == false) { - treedata->looptri = BKE_mesh_runtime_looptri_ensure(me); - } - } - - Nearest2dUserData nearest2d = { - .userdata = treedata, - .get_vert_co = (Nearest2DGetVertCoCallback)cb_mvert_co_get, - .get_edge_verts_index = (Nearest2DGetEdgeVertsCallback)cb_medge_verts_get, - .get_tri_verts_index = (Nearest2DGetTriVertsCallback)cb_mlooptri_verts_get, - .get_tri_edges_index = (Nearest2DGetTriEdgesCallback)cb_mlooptri_edges_get, - .copy_vert_no = (Nearest2DCopyVertNoCallback)cb_mvert_no_copy, - .is_persp = snapdata->view_proj == VIEW_PROJ_PERSP, - .use_backface_culling = use_backface_culling, - }; + Nearest2dUserData nearest2d; + nearest2d_data_init( + sod, snapdata->view_proj == VIEW_PROJ_PERSP, use_backface_culling, &nearest2d); BVHTreeNearest nearest = { .index = -1, @@ -2455,7 +2455,7 @@ static short snapMesh(SnapObjectContext *sctx, static short snapEditMesh(SnapObjectContext *sctx, SnapData *snapdata, - Object *ob, + Object *ob_eval, BMEditMesh *em, const float obmat[4][4], bool use_backface_culling, @@ -2484,7 +2484,7 @@ static short snapEditMesh(SnapObjectContext *sctx, float dist_px_sq = square_f(*dist_px); - SnapObjectData *sod = snap_object_data_editmesh_get(sctx, ob, em); + SnapObjectData *sod = snap_object_data_editmesh_get(sctx, ob_eval, em); /* Test BoundBox */ @@ -2558,14 +2558,9 @@ static short snapEditMesh(SnapObjectContext *sctx, } } - Nearest2dUserData nearest2d = { - .userdata = em, - .get_vert_co = (Nearest2DGetVertCoCallback)cb_bvert_co_get, - .get_edge_verts_index = (Nearest2DGetEdgeVertsCallback)cb_bedge_verts_get, - .copy_vert_no = (Nearest2DCopyVertNoCallback)cb_bvert_no_copy, - .is_persp = snapdata->view_proj == VIEW_PROJ_PERSP, - .use_backface_culling = use_backface_culling, - }; + Nearest2dUserData nearest2d; + nearest2d_data_init( + sod, snapdata->view_proj == VIEW_PROJ_PERSP, use_backface_culling, &nearest2d); BVHTreeNearest nearest = { .index = -1, @@ -2659,9 +2654,9 @@ struct SnapObjUserData { * \note Duplicate args here are documented at #snapObjectsRay */ static void snap_obj_fn(SnapObjectContext *sctx, - Object *ob, + Object *ob_eval, float obmat[4][4], - bool use_obedit, + eSnapEditType edit_mode_type, bool use_backface_culling, bool UNUSED(is_object_active), void *data) @@ -2669,41 +2664,36 @@ static void snap_obj_fn(SnapObjectContext *sctx, struct SnapObjUserData *dt = data; short retval = 0; - switch (ob->type) { + switch (ob_eval->type) { case OB_MESH: { - Mesh *me = ob->data; - if (BKE_object_is_in_editmode(ob)) { - if (use_obedit || editmesh_eval_final_is_bmesh(me->edit_mesh)) { - /* Operators only update the editmesh looptris of the original mesh. */ - BMEditMesh *em_orig = BKE_editmesh_from_object(DEG_get_original_object(ob)); - retval = snapEditMesh(sctx, - dt->snapdata, - ob, - em_orig, - obmat, - use_backface_culling, - dt->dist_px, - dt->r_loc, - dt->r_no, - dt->r_index); - break; - } - - BMEditMesh *em = BKE_editmesh_from_object(ob); - if (em->mesh_eval_final) { - me = em->mesh_eval_final; - } + bool use_hide; + Mesh *me_eval = mesh_for_snap(ob_eval, edit_mode_type, &use_hide); + if (me_eval == NULL) { + /* Operators only update the editmesh looptris of the original mesh. */ + BMEditMesh *em_orig = BKE_editmesh_from_object(DEG_get_original_object(ob_eval)); + retval = snapEditMesh(sctx, + dt->snapdata, + ob_eval, + em_orig, + obmat, + use_backface_culling, + dt->dist_px, + dt->r_loc, + dt->r_no, + dt->r_index); + break; } - else if (ob->dt == OB_BOUNDBOX) { + if (ob_eval->dt == OB_BOUNDBOX) { /* Do not snap to objects that are in bounding box display mode */ return; } retval = snapMesh(sctx, dt->snapdata, - ob, - me, + ob_eval, + me_eval, obmat, + use_hide, use_backface_culling, dt->dist_px, dt->r_loc, @@ -2713,21 +2703,28 @@ static void snap_obj_fn(SnapObjectContext *sctx, } case OB_ARMATURE: retval = snapArmature( - dt->snapdata, ob, obmat, use_obedit, dt->dist_px, dt->r_loc, dt->r_no, dt->r_index); + dt->snapdata, ob_eval, obmat, dt->dist_px, dt->r_loc, dt->r_no, dt->r_index); break; case OB_CURVE: - retval = snapCurve( - dt->snapdata, ob, obmat, use_obedit, dt->dist_px, dt->r_loc, dt->r_no, dt->r_index); + retval = snapCurve(dt->snapdata, + ob_eval, + obmat, + edit_mode_type == SNAP_GEOM_EDIT, + dt->dist_px, + dt->r_loc, + dt->r_no, + dt->r_index); break; /* Use ATTR_FALLTHROUGH if we want to snap to the generated mesh. */ case OB_SURF: case OB_FONT: { - Mesh *mesh_eval = BKE_object_get_evaluated_mesh(ob); + Mesh *mesh_eval = BKE_object_get_evaluated_mesh(ob_eval); if (mesh_eval) { retval |= snapMesh(sctx, dt->snapdata, - ob, + ob_eval, mesh_eval, obmat, + false, use_backface_culling, dt->dist_px, dt->r_loc, @@ -2740,17 +2737,17 @@ static void snap_obj_fn(SnapObjectContext *sctx, case OB_GPENCIL: case OB_LAMP: retval = snap_object_center( - dt->snapdata, ob, obmat, dt->dist_px, dt->r_loc, dt->r_no, dt->r_index); + dt->snapdata, ob_eval, obmat, dt->dist_px, dt->r_loc, dt->r_no, dt->r_index); break; case OB_CAMERA: retval = snapCamera( - sctx, dt->snapdata, ob, obmat, dt->dist_px, dt->r_loc, dt->r_no, dt->r_index); + sctx, dt->snapdata, ob_eval, obmat, dt->dist_px, dt->r_loc, dt->r_no, dt->r_index); break; } if (retval) { if (dt->r_ob) { - *dt->r_ob = ob; + *dt->r_ob = ob_eval; } if (dt->r_obmat) { copy_m4_m4(dt->r_obmat, obmat); @@ -3023,7 +3020,7 @@ static short transform_snap_context_project_view3d_mixed_impl( short retval = 0; bool has_hit = false; - Object *ob = NULL; + Object *ob_eval = NULL; float loc[3]; /* Not all snapping callbacks set the normal, * initialize this since any hit copies both the `loc` and `no`. */ @@ -3060,7 +3057,7 @@ static short transform_snap_context_project_view3d_mixed_impl( loc, no, &index, - &ob, + &ob_eval, obmat, NULL); @@ -3072,7 +3069,7 @@ static short transform_snap_context_project_view3d_mixed_impl( copy_v3_v3(r_no, no); } if (r_ob) { - *r_ob = ob; + *r_ob = ob_eval; } if (r_obmat) { copy_m4_m4(r_obmat, obmat); @@ -3108,9 +3105,10 @@ static short transform_snap_context_project_view3d_mixed_impl( snapdata.has_occlusion_plane = false; /* By convention we only snap to the original elements of a curve. */ - if (has_hit && ob->type != OB_CURVE) { + if (has_hit && ob_eval->type != OB_CURVE) { /* Compute the new clip_pane but do not add it yet. */ float new_clipplane[4]; + BLI_ASSERT_UNIT_V3(no); plane_from_point_normal_v3(new_clipplane, loc, no); if (dot_v3v3(snapdata.clip_plane[0], new_clipplane) > 0.0f) { /* The plane is facing the wrong direction. */ @@ -3121,8 +3119,15 @@ static short transform_snap_context_project_view3d_mixed_impl( new_clipplane[3] += 0.01f; /* Try to snap only to the polygon. */ - elem_test = snap_mesh_polygon( - sctx, &snapdata, ob, obmat, params->use_backface_culling, &dist_px_tmp, loc, no, &index); + elem_test = snap_mesh_polygon(sctx, + &snapdata, + ob_eval, + obmat, + params->use_backface_culling, + &dist_px_tmp, + loc, + no, + &index); if (elem_test) { elem = elem_test; } @@ -3137,7 +3142,7 @@ static short transform_snap_context_project_view3d_mixed_impl( } elem_test = snapObjectsRay( - sctx, depsgraph, &snapdata, params, &dist_px_tmp, loc, no, &index, &ob, obmat); + sctx, depsgraph, &snapdata, params, &dist_px_tmp, loc, no, &index, &ob_eval, obmat); if (elem_test) { elem = elem_test; } @@ -3148,7 +3153,7 @@ static short transform_snap_context_project_view3d_mixed_impl( snapdata.snap_to_flag = snap_to_flag; elem = snap_mesh_edge_verts_mixed(sctx, &snapdata, - ob, + ob_eval, obmat, *dist_px, prev_co, @@ -3167,7 +3172,7 @@ static short transform_snap_context_project_view3d_mixed_impl( copy_v3_v3(r_no, no); } if (r_ob) { - *r_ob = ob; + *r_ob = ob_eval; } if (r_obmat) { copy_m4_m4(r_obmat, obmat); |