diff --git a/source/blender/editors/gizmo_library/gizmo_types/snap3d_gizmo.c b/source/blender/editors/gizmo_library/gizmo_types/snap3d_gizmo.c index e259ce39d09..b2a196888c9 100644 --- a/source/blender/editors/gizmo_library/gizmo_types/snap3d_gizmo.c +++ b/source/blender/editors/gizmo_library/gizmo_types/snap3d_gizmo.c @@ -214,7 +214,7 @@ static void snap_cursor_init(SnapGizmo3D *snap_gizmo) snap_gizmo->snap_state->draw_point = true; snap_gizmo->snap_state->draw_plane = false; - rgba_float_to_uchar(snap_gizmo->snap_state->color_point, snap_gizmo->gizmo.color); + rgba_float_to_uchar(snap_gizmo->snap_state->target_color, snap_gizmo->gizmo.color); snap_gizmo->snap_state->poll = snap_cursor_poll; snap_gizmo->snap_state->poll_data = snap_gizmo; diff --git a/source/blender/editors/include/ED_view3d.h b/source/blender/editors/include/ED_view3d.h index ca23f3ac664..4adafb40c34 100644 --- a/source/blender/editors/include/ED_view3d.h +++ b/source/blender/editors/include/ED_view3d.h @@ -322,8 +322,8 @@ typedef struct V3DSnapCursorData { typedef struct V3DSnapCursorState { /* Setup. */ eV3DSnapCursor flag; - uchar color_line[4]; - uchar color_point[4]; + uchar source_color[4]; + uchar target_color[4]; uchar color_box[4]; float *prevpoint; float box_dimensions[3]; @@ -348,13 +348,13 @@ void ED_view3d_cursor_snap_data_update(V3DSnapCursorState *state, int y); V3DSnapCursorData *ED_view3d_cursor_snap_data_get(void); struct SnapObjectContext *ED_view3d_cursor_snap_context_ensure(struct Scene *scene); -void ED_view3d_cursor_snap_draw_util(struct RegionView3D *rv3d, - const float loc_prev[3], - const float loc_curr[3], - const float normal[3], - const uchar color_line[4], - const uchar color_point[4], - eSnapMode snap_elem_type); +void ED_view3d_cursor_snap_draw_util(RegionView3D *rv3d, + const float source_loc[3], + const float target_loc[3], + const float target_normal[3], + const uchar source_color[4], + const uchar target_color[4], + const eSnapMode target_type); /* view3d_iterators.cc */ diff --git a/source/blender/editors/space_view3d/view3d_cursor_snap.c b/source/blender/editors/space_view3d/view3d_cursor_snap.c index 40dcf9a9cad..4bdfb689b41 100644 --- a/source/blender/editors/space_view3d/view3d_cursor_snap.c +++ b/source/blender/editors/space_view3d/view3d_cursor_snap.c @@ -79,8 +79,8 @@ typedef struct SnapCursorDataIntern { static SnapCursorDataIntern g_data_intern = { .state_default = {.flag = V3D_SNAPCURSOR_SNAP_EDIT_GEOM_FINAL, - .color_point = {255, 255, 255, 255}, - .color_line = {255, 255, 255, 128}, + .target_color = {255, 255, 255, 255}, + .source_color = {255, 255, 255, 128}, .color_box = {255, 255, 255, 128}, .box_dimensions = {1.0f, 1.0f, 1.0f}, .draw_point = true}}; @@ -367,14 +367,14 @@ static void cursor_box_draw(const float dimensions[3], uchar color[4]) } void ED_view3d_cursor_snap_draw_util(RegionView3D *rv3d, - const float loc_prev[3], - const float loc_curr[3], - const float normal[3], - const uchar color_line[4], - const uchar color_point[4], - const eSnapMode snap_elem_type) + const float source_loc[3], + const float target_loc[3], + const float target_normal[3], + const uchar source_color[4], + const uchar target_color[4], + const eSnapMode target_type) { - if (!loc_prev && !loc_curr) { + if (!source_loc && !target_loc) { return; } @@ -388,20 +388,23 @@ void ED_view3d_cursor_snap_draw_util(RegionView3D *rv3d, immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR); - if (loc_curr) { - immUniformColor4ubv(color_point); - imm_drawcircball(loc_curr, ED_view3d_pixel_size(rv3d, loc_curr) * radius, view_inv, pos); + if (target_loc) { + immUniformColor4ubv(target_color); + imm_drawcircball(target_loc, ED_view3d_pixel_size(rv3d, target_loc) * radius, view_inv, pos); /* draw normal if needed */ - if (normal) { + if (target_normal) { immBegin(GPU_PRIM_LINES, 2); - immVertex3fv(pos, loc_curr); - immVertex3f(pos, loc_curr[0] + normal[0], loc_curr[1] + normal[1], loc_curr[2] + normal[2]); + immVertex3fv(pos, target_loc); + immVertex3f(pos, + target_loc[0] + target_normal[0], + target_loc[1] + target_normal[1], + target_loc[2] + target_normal[2]); immEnd(); } } - if (loc_prev) { + if (source_loc) { /* Draw an "X" indicating where the previous snap point is. * This is useful for indicating perpendicular snap. */ @@ -411,7 +414,7 @@ void ED_view3d_cursor_snap_draw_util(RegionView3D *rv3d, /* Multiply by 0.75f so that the final size of the "X" is close to that of * the circle. * (A closer value is 0.7071f, but we don't need to be exact here). */ - float x_size = 0.75f * radius * ED_view3d_pixel_size(rv3d, loc_prev); + float x_size = 0.75f * radius * ED_view3d_pixel_size(rv3d, source_loc); mul_v3_v3fl(vx, view_inv[0], x_size); mul_v3_v3fl(vy, view_inv[1], x_size); @@ -421,12 +424,12 @@ void ED_view3d_cursor_snap_draw_util(RegionView3D *rv3d, negate_v3_v3(v3, v1); negate_v3_v3(v4, v2); - add_v3_v3(v1, loc_prev); - add_v3_v3(v2, loc_prev); - add_v3_v3(v3, loc_prev); - add_v3_v3(v4, loc_prev); + add_v3_v3(v1, source_loc); + add_v3_v3(v2, source_loc); + add_v3_v3(v3, source_loc); + add_v3_v3(v4, source_loc); - immUniformColor4ubv(color_line); + immUniformColor4ubv(source_color); immBegin(GPU_PRIM_LINES, 4); immVertex3fv(pos, v3); immVertex3fv(pos, v1); @@ -434,7 +437,7 @@ void ED_view3d_cursor_snap_draw_util(RegionView3D *rv3d, immVertex3fv(pos, v2); immEnd(); - if (loc_curr && (snap_elem_type & SCE_SNAP_TO_EDGE_PERPENDICULAR)) { + if (target_loc && (target_type & SCE_SNAP_TO_EDGE_PERPENDICULAR)) { /* Dashed line. */ immUnbindProgram(); @@ -444,11 +447,11 @@ void ED_view3d_cursor_snap_draw_util(RegionView3D *rv3d, immUniform2f("viewport_size", viewport_size[2], viewport_size[3]); immUniform1f("dash_width", 6.0f * U.pixelsize); immUniform1f("udash_factor", 1.0f / 4.0f); - immUniformColor4ubv(color_line); + immUniformColor4ubv(source_color); immBegin(GPU_PRIM_LINES, 2); - immVertex3fv(pos, loc_prev); - immVertex3fv(pos, loc_curr); + immVertex3fv(pos, source_loc); + immVertex3fv(pos, target_loc); immEnd(); } } @@ -883,8 +886,8 @@ static void v3d_cursor_snap_draw_fn(bContext *C, int x, int y, void *UNUSED(cust prev_point, snap_data->loc, NULL, - state->color_line, - state->color_point, + state->source_color, + state->target_color, snap_data->snap_elem); } diff --git a/source/blender/editors/transform/transform.h b/source/blender/editors/transform/transform.h index 508c85b04f3..e0988d88ca8 100644 --- a/source/blender/editors/transform/transform.h +++ b/source/blender/editors/transform/transform.h @@ -302,7 +302,7 @@ typedef struct TransSnap { short face_nearest_steps; eTSnap status; /* Snapped Element Type (currently for objects only). */ - eSnapMode snapElem; + eSnapMode target_type; /** snapping from this point (in global-space). */ float snap_source[3]; /** to this point (in global-space). */ diff --git a/source/blender/editors/transform/transform_constraints.c b/source/blender/editors/transform/transform_constraints.c index 1e565078617..dd15c5f555a 100644 --- a/source/blender/editors/transform/transform_constraints.c +++ b/source/blender/editors/transform/transform_constraints.c @@ -399,11 +399,11 @@ static void applyAxisConstraintVec(const TransInfo *t, if (transform_snap_is_active(t)) { if (validSnap(t)) { - is_snap_to_edge = (t->tsnap.snapElem & SCE_SNAP_TO_EDGE) != 0; - is_snap_to_face = (t->tsnap.snapElem & SCE_SNAP_TO_FACE) != 0; + is_snap_to_edge = (t->tsnap.target_type & SCE_SNAP_TO_EDGE) != 0; + is_snap_to_face = (t->tsnap.target_type & SCE_SNAP_TO_FACE) != 0; is_snap_to_point = !is_snap_to_edge && !is_snap_to_face; } - else if (t->tsnap.snapElem & SCE_SNAP_TO_GRID) { + else if (t->tsnap.target_type & SCE_SNAP_TO_GRID) { is_snap_to_point = true; } } diff --git a/source/blender/editors/transform/transform_mode_edge_slide.c b/source/blender/editors/transform/transform_mode_edge_slide.c index bc3d6fb7718..165c3983a2b 100644 --- a/source/blender/editors/transform/transform_mode_edge_slide.c +++ b/source/blender/editors/transform/transform_mode_edge_slide.c @@ -1294,11 +1294,11 @@ static void edge_slide_snap_apply(TransInfo *t, float *value) side_index = t_snap >= t_mid; } - if (t->tsnap.snapElem & (SCE_SNAP_TO_EDGE | SCE_SNAP_TO_FACE)) { + if (t->tsnap.target_type & (SCE_SNAP_TO_EDGE | SCE_SNAP_TO_FACE)) { float co_dir[3]; sub_v3_v3v3(co_dir, co_dest[side_index], co_orig); normalize_v3(co_dir); - if (t->tsnap.snapElem & SCE_SNAP_TO_EDGE) { + if (t->tsnap.target_type & SCE_SNAP_TO_EDGE) { transform_constraint_snap_axis_to_edge(t, co_dir, dvec); } else { diff --git a/source/blender/editors/transform/transform_mode_translate.c b/source/blender/editors/transform/transform_mode_translate.c index de94ac01560..3ee12e2671d 100644 --- a/source/blender/editors/transform/transform_mode_translate.c +++ b/source/blender/editors/transform/transform_mode_translate.c @@ -384,7 +384,7 @@ static void translate_snap_grid_apply(TransInfo *t, float in[3]; if (t->con.mode & CON_APPLY) { - BLI_assert(t->tsnap.snapElem == SCE_SNAP_TO_NONE); + BLI_assert(t->tsnap.target_type == SCE_SNAP_TO_NONE); t->con.applyVec(t, NULL, NULL, loc, in); } else { @@ -429,7 +429,7 @@ static bool translate_snap_grid(TransInfo *t, float *val) } translate_snap_grid_apply(t, t->idx_max, grid_dist, val, val); - t->tsnap.snapElem = SCE_SNAP_TO_GRID; + t->tsnap.target_type = SCE_SNAP_TO_GRID; return true; } @@ -627,7 +627,7 @@ static void applyTranslation(TransInfo *t, const int UNUSED(mval[2])) /* Test for mixed snap with grid. */ float snap_dist_sq = FLT_MAX; - if (t->tsnap.snapElem != SCE_SNAP_TO_NONE) { + if (t->tsnap.target_type != SCE_SNAP_TO_NONE) { snap_dist_sq = len_squared_v3v3(t->values, global_dir); } if ((snap_dist_sq == FLT_MAX) || (len_squared_v3v3(global_dir, incr_dir) < snap_dist_sq)) { diff --git a/source/blender/editors/transform/transform_mode_vert_slide.c b/source/blender/editors/transform/transform_mode_vert_slide.c index 922d3dd5816..e6458cbe5af 100644 --- a/source/blender/editors/transform/transform_mode_vert_slide.c +++ b/source/blender/editors/transform/transform_mode_vert_slide.c @@ -539,11 +539,11 @@ static void vert_slide_snap_apply(TransInfo *t, float *value) getSnapPoint(t, dvec); sub_v3_v3(dvec, t->tsnap.snap_source); - if (t->tsnap.snapElem & (SCE_SNAP_TO_EDGE | SCE_SNAP_TO_FACE)) { + if (t->tsnap.target_type & (SCE_SNAP_TO_EDGE | SCE_SNAP_TO_FACE)) { float co_dir[3]; sub_v3_v3v3(co_dir, co_curr_3d, co_orig_3d); normalize_v3(co_dir); - if (t->tsnap.snapElem & SCE_SNAP_TO_EDGE) { + if (t->tsnap.target_type & SCE_SNAP_TO_EDGE) { transform_constraint_snap_axis_to_edge(t, co_dir, dvec); } else { diff --git a/source/blender/editors/transform/transform_snap.cc b/source/blender/editors/transform/transform_snap.cc index 1140a0163fa..d8c5fff1f62 100644 --- a/source/blender/editors/transform/transform_snap.cc +++ b/source/blender/editors/transform/transform_snap.cc @@ -174,7 +174,8 @@ void drawSnapping(const bContext *C, TransInfo *t) return; } - const bool draw_source = (t->flag & T_DRAW_SNAP_SOURCE) && (t->tsnap.status & (SNAP_SOURCE_FOUND | SNAP_MULTI_POINTS)); + const bool draw_source = (t->flag & T_DRAW_SNAP_SOURCE) && + (t->tsnap.status & (SNAP_SOURCE_FOUND | SNAP_MULTI_POINTS)); const bool draw_target = (t->tsnap.status & (SNAP_TARGET_FOUND | SNAP_MULTI_POINTS)); if (!(draw_source || draw_target)) { @@ -197,9 +198,9 @@ void drawSnapping(const bContext *C, TransInfo *t) } if (t->spacetype == SPACE_VIEW3D) { - const float *loc_cur = nullptr; - const float *loc_prev = nullptr; - const float *normal = nullptr; + const float *source_loc = nullptr; + const float *target_loc = nullptr; + const float *target_normal = nullptr; GPU_depth_test(GPU_DEPTH_NONE); @@ -233,19 +234,19 @@ void drawSnapping(const bContext *C, TransInfo *t) /* draw normal if needed */ if (usingSnappingNormal(t) && validSnappingNormal(t)) { - normal = t->tsnap.snapNormal; + target_normal = t->tsnap.snapNormal; } if (draw_source) { - loc_prev = t->tsnap.snap_source; + source_loc = t->tsnap.snap_source; } if (t->tsnap.status & SNAP_TARGET_FOUND) { - loc_cur = t->tsnap.snap_target; + target_loc = t->tsnap.snap_target; } ED_view3d_cursor_snap_draw_util( - rv3d, loc_prev, loc_cur, normal, col, activeCol, t->tsnap.snapElem); + rv3d, source_loc, target_loc, target_normal, col, activeCol, t->tsnap.target_type); GPU_depth_test(GPU_DEPTH_LESS_EQUAL); } @@ -543,7 +544,7 @@ void transform_snap_mixed_apply(TransInfo *t, float *vec) void resetSnapping(TransInfo *t) { t->tsnap.status = SNAP_RESETTED; - t->tsnap.snapElem = SCE_SNAP_TO_NONE; + t->tsnap.target_type = SCE_SNAP_TO_NONE; t->tsnap.mode = SCE_SNAP_TO_NONE; t->tsnap.target_operation = SCE_SNAP_TARGET_ALL; t->tsnap.source_operation = SCE_SNAP_SOURCE_CLOSEST; @@ -1123,7 +1124,7 @@ static void snap_target_view3d_fn(TransInfo *t, float * /*vec*/) t->tsnap.status &= ~SNAP_TARGET_FOUND; } - t->tsnap.snapElem = snap_elem; + t->tsnap.target_type = snap_elem; } static void snap_target_uv_fn(TransInfo *t, float * /*vec*/) diff --git a/source/blender/editors/transform/transform_snap_object.cc b/source/blender/editors/transform/transform_snap_object.cc index f8cf2cf9836..6268eb523af 100644 --- a/source/blender/editors/transform/transform_snap_object.cc +++ b/source/blender/editors/transform/transform_snap_object.cc @@ -96,7 +96,7 @@ static bool test_projected_edge_dist(const DistProjectedAABBPrecalc *precalc, return test_projected_vert_dist(precalc, clip_plane, clip_plane_len, is_persp, near_co, nearest); } -Nearest2dUserData::Nearest2dUserData(SnapObjectContext *sctx, const float4x4 &obmat) +SnapData::SnapData(SnapObjectContext *sctx, const float4x4 &obmat) : nearest_precalc(), obmat_(obmat), is_persp(sctx->runtime.rv3d ? sctx->runtime.rv3d->is_persp : false), @@ -117,7 +117,7 @@ Nearest2dUserData::Nearest2dUserData(SnapObjectContext *sctx, const float4x4 &ob copy_v3_fl3(this->nearest_point.no, 0.0f, 0.0f, 1.0f); } -void Nearest2dUserData::clip_planes_enable(SnapObjectContext *sctx, bool skip_occlusion_plane) +void SnapData::clip_planes_enable(SnapObjectContext *sctx, bool skip_occlusion_plane) { float(*clip_planes)[4] = sctx->runtime.clip_plane; int clip_plane_len = sctx->runtime.clip_plane_len; @@ -136,7 +136,7 @@ void Nearest2dUserData::clip_planes_enable(SnapObjectContext *sctx, bool skip_oc BLI_assert(this->clip_planes.size() == clip_plane_len); } -bool Nearest2dUserData::snap_boundbox(const float3 &min, const float3 &max) +bool SnapData::snap_boundbox(const float3 &min, const float3 &max) { /* In vertex and edges you need to get the pixel distance from ray to BoundBox, * see: #46099, #46816 */ @@ -162,7 +162,7 @@ bool Nearest2dUserData::snap_boundbox(const float3 &min, const float3 &max) return true; } -bool Nearest2dUserData::snap_point(const float3 &co, int index) +bool SnapData::snap_point(const float3 &co, int index) { if (test_projected_vert_dist(&this->nearest_precalc, reinterpret_cast(this->clip_planes.data()), @@ -177,7 +177,7 @@ bool Nearest2dUserData::snap_point(const float3 &co, int index) return false; } -bool Nearest2dUserData::snap_edge(const float3 &va, const float3 &vb, int edge_index) +bool SnapData::snap_edge(const float3 &va, const float3 &vb, int edge_index) { if (test_projected_edge_dist(&this->nearest_precalc, reinterpret_cast(this->clip_planes.data()), @@ -194,9 +194,9 @@ bool Nearest2dUserData::snap_edge(const float3 &va, const float3 &vb, int edge_i return false; } -eSnapMode Nearest2dUserData::snap_edge_points_impl(SnapObjectContext *sctx, - int edge_index, - float dist_px_sq_orig) +eSnapMode SnapData::snap_edge_points_impl(SnapObjectContext *sctx, + int edge_index, + float dist_px_sq_orig) { eSnapMode elem = SCE_SNAP_TO_EDGE; @@ -220,7 +220,8 @@ eSnapMode Nearest2dUserData::snap_edge_points_impl(SnapObjectContext *sctx, this->nearest_point.dist_sq = dist_px_sq_orig; eSnapMode snap_to = sctx->runtime.snap_to_flag; - int e_mode_len = ((snap_to & SCE_SNAP_TO_EDGE) != 0) + ((snap_to & SCE_SNAP_TO_VERTEX) != 0) + + int e_mode_len = ((snap_to & SCE_SNAP_TO_EDGE) != 0) + + ((snap_to & SCE_SNAP_TO_EDGE_ENDPOINT) != 0) + ((snap_to & SCE_SNAP_TO_EDGE_MIDPOINT) != 0); float range = 1.0f / (2 * e_mode_len - 1); @@ -256,12 +257,12 @@ eSnapMode Nearest2dUserData::snap_edge_points_impl(SnapObjectContext *sctx, } /* Leave this one for last so it doesn't change the normal. */ - if (snap_to & SCE_SNAP_TO_VERTEX) { + if (snap_to & SCE_SNAP_TO_EDGE_ENDPOINT) { if (lambda < (range) || (1.0f - range) < lambda) { int v_id = lambda < 0.5f ? 0 : 1; if (this->snap_point(v_pair[v_id], v_id)) { - elem = SCE_SNAP_TO_VERTEX; + elem = SCE_SNAP_TO_EDGE_ENDPOINT; this->copy_vert_no(vindex[v_id], this->nearest_point.no); } } @@ -271,9 +272,7 @@ eSnapMode Nearest2dUserData::snap_edge_points_impl(SnapObjectContext *sctx, return elem; } -void Nearest2dUserData::register_result(SnapObjectContext *sctx, - Object *ob_eval, - const ID *id_eval) +void SnapData::register_result(SnapObjectContext *sctx, Object *ob_eval, const ID *id_eval) { BLI_assert(this->nearest_point.index != -2); @@ -442,8 +441,7 @@ static eSnapMode iter_snap_objects(SnapObjectContext *sctx, IterSnapObjsCallback const bool is_object_active = (base == base_act); Object *obj_eval = DEG_get_evaluated_object(sctx->runtime.depsgraph, base->object); if (obj_eval->transflag & OB_DUPLI || - blender::bke::object_has_geometry_set_instances(*obj_eval)) - { + blender::bke::object_has_geometry_set_instances(*obj_eval)) { ListBase *lb = object_duplilist(sctx->runtime.depsgraph, sctx->scene, obj_eval); LISTBASE_FOREACH (DupliObject *, dupli_ob, lb) { BLI_assert(DEG_is_evaluated_object(dupli_ob->ob)); @@ -604,11 +602,6 @@ static bool raycastObjects(SnapObjectContext *sctx) /** \name Surface Snap Functions * \{ */ -struct NearestWorldObjUserData { - const float *init_co; - const float *curr_co; -}; - static void nearest_world_tree_co(BVHTree *tree, BVHTree_NearestPointCallback nearest_cb, void *treedata, @@ -710,8 +703,7 @@ static eSnapMode nearest_world_object_fn(SnapObjectContext *sctx, if (ob_data == nullptr) { if (ob_eval->type == OB_MESH) { if (snap_object_editmesh( - sctx, ob_eval, nullptr, obmat, SCE_SNAP_INDIVIDUAL_NEAREST, use_hide)) - { + sctx, ob_eval, nullptr, obmat, SCE_SNAP_INDIVIDUAL_NEAREST, use_hide)) { retval = true; } } @@ -768,7 +760,7 @@ void cb_snap_vert(void *userdata, const int clip_plane_len, BVHTreeNearest *nearest) { - Nearest2dUserData *data = static_cast(userdata); + SnapData *data = static_cast(userdata); const float *co; data->get_vert_co(index, &co); @@ -786,7 +778,7 @@ void cb_snap_edge(void *userdata, const int clip_plane_len, BVHTreeNearest *nearest) { - Nearest2dUserData *data = static_cast(userdata); + SnapData *data = static_cast(userdata); int vindex[2]; data->get_edge_verts_index(index, vindex); @@ -862,11 +854,11 @@ eSnapMode snap_object_center(SnapObjectContext *sctx, } /* For now only vertex supported. */ - if ((snap_to_flag & SCE_SNAP_TO_VERTEX) == 0) { + if ((snap_to_flag & SCE_SNAP_TO_POINT) == 0) { return SCE_SNAP_TO_NONE; } - Nearest2dUserData nearest2d(sctx, float4x4(obmat)); + SnapData nearest2d(sctx, float4x4(obmat)); nearest2d.clip_planes_enable(sctx); @@ -1349,16 +1341,11 @@ eSnapMode ED_transform_snap_object_project_view3d_ex(SnapObjectContext *sctx, } } - if (snap_to_flag & (SCE_SNAP_TO_VERTEX | SCE_SNAP_TO_EDGE | SCE_SNAP_TO_EDGE_MIDPOINT | - SCE_SNAP_TO_EDGE_PERPENDICULAR)) - { + if (snap_to_flag & (SCE_SNAP_TO_POINT | SNAP_TO_EDGE_ELEMENTS)) { eSnapMode elem_test, elem = SCE_SNAP_TO_NONE; - /* First snap to edge instead of middle or perpendicular. */ - sctx->runtime.snap_to_flag &= (SCE_SNAP_TO_VERTEX | SCE_SNAP_TO_EDGE); - if (snap_to_flag & (SCE_SNAP_TO_EDGE_MIDPOINT | SCE_SNAP_TO_EDGE_PERPENDICULAR)) { - sctx->runtime.snap_to_flag |= SCE_SNAP_TO_EDGE; - } + /* Remove what has already been computed. */ + sctx->runtime.snap_to_flag &= ~(SCE_SNAP_TO_FACE | SCE_SNAP_INDIVIDUAL_NEAREST); /* By convention we only snap to the original elements of a curve. */ if (has_hit && sctx->ret.ob->type != OB_CURVES_LEGACY) { @@ -1394,11 +1381,7 @@ eSnapMode ED_transform_snap_object_project_view3d_ex(SnapObjectContext *sctx, elem = elem_test; } - if ((elem == SCE_SNAP_TO_EDGE) && - (snap_to_flag & - (SCE_SNAP_TO_VERTEX | SCE_SNAP_TO_EDGE_MIDPOINT | SCE_SNAP_TO_EDGE_PERPENDICULAR))) - { - sctx->runtime.snap_to_flag = snap_to_flag; + if ((elem == SCE_SNAP_TO_EDGE) && (snap_to_flag & SNAP_TO_EDGE_ELEMENTS)) { elem = snap_edge_points(sctx, square_f(*dist_px)); } diff --git a/source/blender/editors/transform/transform_snap_object.hh b/source/blender/editors/transform/transform_snap_object.hh index 1a2e51b355b..bf1feb169a5 100644 --- a/source/blender/editors/transform/transform_snap_object.hh +++ b/source/blender/editors/transform/transform_snap_object.hh @@ -10,12 +10,16 @@ #define MAX_CLIPPLANE_LEN 3 -struct SnapData_EditMesh; +#define SNAP_TO_EDGE_ELEMENTS \ + (SCE_SNAP_TO_EDGE | SCE_SNAP_TO_EDGE_ENDPOINT | SCE_SNAP_TO_EDGE_MIDPOINT | \ + SCE_SNAP_TO_EDGE_PERPENDICULAR) + +struct SnapCache_EditMesh; struct SnapObjectContext { struct Scene *scene; - blender::Map> editmesh_caches; + blender::Map> editmesh_caches; /* Filter data, returns true to check this value */ struct { @@ -92,7 +96,7 @@ struct RayCastAll_Data { ListBase *hit_list; }; -class Nearest2dUserData { +class SnapData { public: /* Read-only. */ DistProjectedAABBPrecalc nearest_precalc; @@ -107,8 +111,8 @@ class Nearest2dUserData { public: /* Constructor. */ - Nearest2dUserData(SnapObjectContext *sctx, - const blender::float4x4 &obmat = blender::float4x4::identity()); + SnapData(SnapObjectContext *sctx, + const blender::float4x4 &obmat = blender::float4x4::identity()); void clip_planes_enable(SnapObjectContext *sctx, bool skip_occlusion_plane = false); bool snap_boundbox(const blender::float3 &min, const blender::float3 &max); @@ -176,7 +180,7 @@ eSnapMode snapCurve(SnapObjectContext *sctx, Object *ob_eval, const float obmat[ /* transform_snap_object_editmesh.cc */ -struct SnapData_EditMesh { +struct SnapCache_EditMesh { /* Verts, Edges. */ BVHTree *bvhtree[2]; bool cached[2]; @@ -189,7 +193,7 @@ struct SnapData_EditMesh { void clear(); - ~SnapData_EditMesh() + ~SnapCache_EditMesh() { this->clear(); } diff --git a/source/blender/editors/transform/transform_snap_object_armature.cc b/source/blender/editors/transform/transform_snap_object_armature.cc index 570fa0d201d..c4f037c7b42 100644 --- a/source/blender/editors/transform/transform_snap_object_armature.cc +++ b/source/blender/editors/transform/transform_snap_object_armature.cc @@ -34,7 +34,7 @@ eSnapMode snapArmature(SnapObjectContext *sctx, bArmature *arm = static_cast(ob_eval->data); - Nearest2dUserData nearest2d(sctx, float4x4(obmat)); + SnapData nearest2d(sctx, float4x4(obmat)); const bool is_editmode = arm->edbo != nullptr; @@ -104,7 +104,7 @@ eSnapMode snapArmature(SnapObjectContext *sctx, float dist_px_sq_edge = nearest2d.nearest_point.dist_sq; nearest2d.nearest_point.dist_sq = sctx->ret.dist_px_sq; if (nearest2d.snap_point(head_vec) || nearest2d.snap_point(tail_vec)) { - retval = SCE_SNAP_TO_VERTEX; + retval = SCE_SNAP_TO_POINT; } else if (retval) { nearest2d.nearest_point.dist_sq = dist_px_sq_edge; diff --git a/source/blender/editors/transform/transform_snap_object_camera.cc b/source/blender/editors/transform/transform_snap_object_camera.cc index 842eda2aa94..3cb9d636c59 100644 --- a/source/blender/editors/transform/transform_snap_object_camera.cc +++ b/source/blender/editors/transform/transform_snap_object_camera.cc @@ -25,7 +25,7 @@ eSnapMode snapCamera(SnapObjectContext *sctx, { eSnapMode retval = SCE_SNAP_TO_NONE; - if (!(sctx->runtime.snap_to_flag & SCE_SNAP_TO_VERTEX)) { + if (!(sctx->runtime.snap_to_flag & SCE_SNAP_TO_POINT)) { return retval; } @@ -44,7 +44,7 @@ eSnapMode snapCamera(SnapObjectContext *sctx, BKE_tracking_get_camera_object_matrix(object, orig_camera_mat); invert_m4_m4(orig_camera_imat, orig_camera_mat); - Nearest2dUserData nearest2d(sctx); + SnapData nearest2d(sctx); nearest2d.clip_planes_enable(sctx); MovieTracking *tracking = &clip->tracking; @@ -77,7 +77,7 @@ eSnapMode snapCamera(SnapObjectContext *sctx, mul_m4_v3(vertex_obmat, bundle_pos); if (nearest2d.snap_point(bundle_pos)) { - retval = SCE_SNAP_TO_VERTEX; + retval = SCE_SNAP_TO_POINT; } } } diff --git a/source/blender/editors/transform/transform_snap_object_curve.cc b/source/blender/editors/transform/transform_snap_object_curve.cc index 2e785aa6817..2db417d3eec 100644 --- a/source/blender/editors/transform/transform_snap_object_curve.cc +++ b/source/blender/editors/transform/transform_snap_object_curve.cc @@ -27,13 +27,13 @@ eSnapMode snapCurve(SnapObjectContext *sctx, Object *ob_eval, const float obmat[ bool has_snap = false; /* Only vertex snapping mode (eg control points and handles) supported for now). */ - if ((sctx->runtime.snap_to_flag & SCE_SNAP_TO_VERTEX) == 0) { + if ((sctx->runtime.snap_to_flag & SCE_SNAP_TO_POINT) == 0) { return SCE_SNAP_TO_NONE; } Curve *cu = static_cast(ob_eval->data); - Nearest2dUserData nearest2d(sctx, float4x4(obmat)); + SnapData nearest2d(sctx, float4x4(obmat)); const bool use_obedit = BKE_object_is_in_editmode(ob_eval); @@ -52,57 +52,55 @@ eSnapMode snapCurve(SnapObjectContext *sctx, Object *ob_eval, const float obmat[ LISTBASE_FOREACH (Nurb *, nu, (use_obedit ? &cu->editnurb->nurbs : &cu->nurb)) { for (int u = 0; u < nu->pntsu; u++) { - if (sctx->runtime.snap_to_flag & SCE_SNAP_TO_VERTEX) { - if (use_obedit) { - if (nu->bezt) { - if (nu->bezt[u].hide) { - /* Skip hidden. */ - continue; - } - - bool is_selected = (nu->bezt[u].f2 & SELECT) != 0; - if (is_selected && skip_selected) { - continue; - } - has_snap |= nearest2d.snap_point(nu->bezt[u].vec[1]); - - /* Don't snap if handle is selected (moving), - * or if it is aligning to a moving handle. */ - bool is_selected_h1 = (nu->bezt[u].f1 & SELECT) != 0; - bool is_selected_h2 = (nu->bezt[u].f3 & SELECT) != 0; - bool is_autoalign_h1 = (nu->bezt[u].h1 & HD_ALIGN) != 0; - bool is_autoalign_h2 = (nu->bezt[u].h2 & HD_ALIGN) != 0; - if (!skip_selected || !(is_selected_h1 || (is_autoalign_h1 && is_selected_h2))) { - has_snap |= nearest2d.snap_point(nu->bezt[u].vec[0]); - } - - if (!skip_selected || !(is_selected_h2 || (is_autoalign_h2 && is_selected_h1))) { - has_snap |= nearest2d.snap_point(nu->bezt[u].vec[2]); - } + if (use_obedit) { + if (nu->bezt) { + if (nu->bezt[u].hide) { + /* Skip hidden. */ + continue; } - else { - if (nu->bp[u].hide) { - /* Skip hidden. */ - continue; - } - bool is_selected = (nu->bp[u].f1 & SELECT) != 0; - if (is_selected && skip_selected) { - continue; - } + bool is_selected = (nu->bezt[u].f2 & SELECT) != 0; + if (is_selected && skip_selected) { + continue; + } + has_snap |= nearest2d.snap_point(nu->bezt[u].vec[1]); - has_snap |= nearest2d.snap_point(nu->bp[u].vec); + /* Don't snap if handle is selected (moving), + * or if it is aligning to a moving handle. */ + bool is_selected_h1 = (nu->bezt[u].f1 & SELECT) != 0; + bool is_selected_h2 = (nu->bezt[u].f3 & SELECT) != 0; + bool is_autoalign_h1 = (nu->bezt[u].h1 & HD_ALIGN) != 0; + bool is_autoalign_h2 = (nu->bezt[u].h2 & HD_ALIGN) != 0; + if (!skip_selected || !(is_selected_h1 || (is_autoalign_h1 && is_selected_h2))) { + has_snap |= nearest2d.snap_point(nu->bezt[u].vec[0]); + } + + if (!skip_selected || !(is_selected_h2 || (is_autoalign_h2 && is_selected_h1))) { + has_snap |= nearest2d.snap_point(nu->bezt[u].vec[2]); } } else { - /* Curve is not visible outside editmode if nurb length less than two. */ - if (nu->pntsu > 1) { - if (nu->bezt) { - has_snap |= nearest2d.snap_point(nu->bezt[u].vec[1]); - } - else { - has_snap |= nearest2d.snap_point(nu->bp[u].vec); - } + if (nu->bp[u].hide) { + /* Skip hidden. */ + continue; + } + + bool is_selected = (nu->bp[u].f1 & SELECT) != 0; + if (is_selected && skip_selected) { + continue; + } + + has_snap |= nearest2d.snap_point(nu->bp[u].vec); + } + } + else { + /* Curve is not visible outside editmode if nurb length less than two. */ + if (nu->pntsu > 1) { + if (nu->bezt) { + has_snap |= nearest2d.snap_point(nu->bezt[u].vec[1]); + } + else { + has_snap |= nearest2d.snap_point(nu->bp[u].vec); } } } diff --git a/source/blender/editors/transform/transform_snap_object_editmesh.cc b/source/blender/editors/transform/transform_snap_object_editmesh.cc index 5144ff559a2..cd6f48dfd79 100644 --- a/source/blender/editors/transform/transform_snap_object_editmesh.cc +++ b/source/blender/editors/transform/transform_snap_object_editmesh.cc @@ -27,7 +27,7 @@ using namespace blender; /** \name Snap Object Data * \{ */ -void SnapData_EditMesh::clear() +void SnapCache_EditMesh::clear() { for (int i = 0; i < ARRAY_SIZE(this->bvhtree); i++) { if (!this->cached[i]) { @@ -77,46 +77,48 @@ static blender::bke::MeshRuntime *snap_object_data_editmesh_runtime_get(Object * return ((Mesh *)ob_eval->data)->runtime; } -static SnapData_EditMesh *snap_object_data_editmesh_get(SnapObjectContext *sctx, - Object *ob_eval, - BMEditMesh *em, - const bool create) +static SnapCache_EditMesh *snap_object_data_editmesh_get(SnapObjectContext *sctx, + Object *ob_eval, + BMEditMesh *em, + const bool create) { - SnapData_EditMesh *sod = nullptr; + SnapCache_EditMesh *em_cache = nullptr; bool init = false; - if (std::unique_ptr *sod_p = sctx->editmesh_caches.lookup_ptr(em)) { - sod = sod_p->get(); + if (std::unique_ptr *em_cache_p = sctx->editmesh_caches.lookup_ptr(em)) { + em_cache = em_cache_p->get(); bool is_dirty = false; /* Check if the geometry has changed. */ - if (sod->treedata_editmesh.em != em) { + if (em_cache->treedata_editmesh.em != em) { is_dirty = true; } - else if (sod->mesh_runtime) { - if (sod->mesh_runtime != snap_object_data_editmesh_runtime_get(ob_eval)) { + else if (em_cache->mesh_runtime) { + if (em_cache->mesh_runtime != snap_object_data_editmesh_runtime_get(ob_eval)) { if (G.moving) { /* WORKAROUND: 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); + BLI_assert(!em_cache->treedata_editmesh.cached && !em_cache->cached[0] && + !em_cache->cached[1]); + em_cache->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)) + else if (em_cache->treedata_editmesh.tree && em_cache->treedata_editmesh.cached && + !bvhcache_has_tree(em_cache->mesh_runtime->bvh_cache, + em_cache->treedata_editmesh.tree)) { /* The tree is owned by the EditMesh and may have been freed since we last used! */ is_dirty = true; } - else if (sod->bvhtree[0] && sod->cached[0] && - !bvhcache_has_tree(sod->mesh_runtime->bvh_cache, sod->bvhtree[0])) + else if (em_cache->bvhtree[0] && em_cache->cached[0] && + !bvhcache_has_tree(em_cache->mesh_runtime->bvh_cache, em_cache->bvhtree[0])) { /* The tree is owned by the EditMesh and may have been freed since we last used! */ is_dirty = true; } - else if (sod->bvhtree[1] && sod->cached[1] && - !bvhcache_has_tree(sod->mesh_runtime->bvh_cache, sod->bvhtree[1])) + else if (em_cache->bvhtree[1] && em_cache->cached[1] && + !bvhcache_has_tree(em_cache->mesh_runtime->bvh_cache, em_cache->bvhtree[1])) { /* The tree is owned by the EditMesh and may have been freed since we last used! */ is_dirty = true; @@ -124,14 +126,14 @@ static SnapData_EditMesh *snap_object_data_editmesh_get(SnapObjectContext *sctx, } if (is_dirty) { - sod->clear(); + em_cache->clear(); init = true; } } else if (create) { - std::unique_ptr sod_ptr = std::make_unique(); - sod = sod_ptr.get(); - sctx->editmesh_caches.add_new(em, std::move(sod_ptr)); + std::unique_ptr em_cache_ptr = std::make_unique(); + em_cache = em_cache_ptr.get(); + sctx->editmesh_caches.add_new(em, std::move(em_cache_ptr)); init = true; } @@ -139,22 +141,22 @@ static SnapData_EditMesh *snap_object_data_editmesh_get(SnapObjectContext *sctx, /* Operators only update the editmesh looptris of the original mesh. */ BLI_assert(em == BKE_editmesh_from_object(DEG_get_original_object(ob_eval))); - sod->treedata_editmesh.em = em; - sod->mesh_runtime = snap_object_data_editmesh_runtime_get(ob_eval); - snap_editmesh_minmax(sctx, em->bm, sod->min, sod->max); + em_cache->treedata_editmesh.em = em; + em_cache->mesh_runtime = snap_object_data_editmesh_runtime_get(ob_eval); + snap_editmesh_minmax(sctx, em->bm, em_cache->min, em_cache->max); } - return sod; + return em_cache; } -static BVHTreeFromEditMesh *snap_object_data_editmesh_treedata_get(SnapData_EditMesh *sod, +static BVHTreeFromEditMesh *snap_object_data_editmesh_treedata_get(SnapCache_EditMesh *em_cache, SnapObjectContext *sctx, BMEditMesh *em) { - BVHTreeFromEditMesh *treedata = &sod->treedata_editmesh; + BVHTreeFromEditMesh *treedata = &em_cache->treedata_editmesh; if (treedata->tree == nullptr) { - em = sod->treedata_editmesh.em; + em = em_cache->treedata_editmesh.em; if (sctx->callbacks.edit_mesh.test_face_fn) { BMesh *bm = em->bm; @@ -177,8 +179,8 @@ static BVHTreeFromEditMesh *snap_object_data_editmesh_treedata_get(SnapData_Edit 4, BVHTREE_FROM_EM_LOOPTRI, /* WORKAROUND: avoid updating while transforming. */ - G.moving ? nullptr : &sod->mesh_runtime->bvh_cache, - &sod->mesh_runtime->eval_mutex); + G.moving ? nullptr : &em_cache->mesh_runtime->bvh_cache, + &em_cache->mesh_runtime->eval_mutex); } } if (treedata->tree == nullptr) { @@ -210,18 +212,18 @@ static eSnapMode editmesh_snap_mode_supported(BMEditMesh *em) return snap_mode_supported; } -static SnapData_EditMesh *editmesh_snapdata_init(SnapObjectContext *sctx, - Object *ob_eval, - eSnapMode snap_to_flag) +static SnapCache_EditMesh *editmesh_snapdata_init(SnapObjectContext *sctx, + Object *ob_eval, + eSnapMode snap_to_flag) { BMEditMesh *em = BKE_editmesh_from_object(ob_eval); if (em == nullptr) { return nullptr; } - SnapData_EditMesh *sod = snap_object_data_editmesh_get(sctx, ob_eval, em, false); - if (sod != nullptr) { - return sod; + SnapCache_EditMesh *em_cache = snap_object_data_editmesh_get(sctx, ob_eval, em, false); + if (em_cache != nullptr) { + return em_cache; } eSnapMode snap_mode_used = snap_to_flag & editmesh_snap_mode_supported(em); @@ -268,7 +270,7 @@ static void editmesh_looptri_raycast_backface_culling_cb(void *userdata, } } -static bool raycastEditMesh(SnapData_EditMesh *sod, +static bool raycastEditMesh(SnapCache_EditMesh *em_cache, SnapObjectContext *sctx, BMEditMesh *em, const float obmat[4][4], @@ -299,7 +301,7 @@ static bool raycastEditMesh(SnapData_EditMesh *sod, /* was BKE_boundbox_ray_hit_check, see: cf6ca226fa58 */ if (!isect_ray_aabb_v3_simple( - ray_start_local, ray_normal_local, sod->min, sod->max, &len_diff, nullptr)) + ray_start_local, ray_normal_local, em_cache->min, em_cache->max, &len_diff, nullptr)) { return retval; } @@ -315,7 +317,7 @@ static bool raycastEditMesh(SnapData_EditMesh *sod, len_diff = 0.0f; } - BVHTreeFromEditMesh *treedata = snap_object_data_editmesh_treedata_get(sod, sctx, em); + BVHTreeFromEditMesh *treedata = snap_object_data_editmesh_treedata_get(em_cache, sctx, em); if (treedata == nullptr) { return retval; } @@ -370,7 +372,7 @@ static bool raycastEditMesh(SnapData_EditMesh *sod, sctx->ret.ray_depth_max = hit.dist; - em = sod->treedata_editmesh.em; + em = em_cache->treedata_editmesh.em; sctx->ret.index = BM_elem_index_get(em->looptris[hit.index][0]->f); retval = true; @@ -385,12 +387,12 @@ static bool raycastEditMesh(SnapData_EditMesh *sod, /** \name Surface Snap Functions * \{ */ -static bool nearest_world_editmesh(SnapData_EditMesh *sod, +static bool nearest_world_editmesh(SnapCache_EditMesh *em_cache, SnapObjectContext *sctx, BMEditMesh *em, const float (*obmat)[4]) { - BVHTreeFromEditMesh *treedata = snap_object_data_editmesh_treedata_get(sod, sctx, em); + BVHTreeFromEditMesh *treedata = snap_object_data_editmesh_treedata_get(em_cache, sctx, em); if (treedata == nullptr) { return false; } @@ -404,14 +406,12 @@ static bool nearest_world_editmesh(SnapData_EditMesh *sod, /** \name Subclass for Snapping to Edges or Points of an EditMesh * \{ */ -class Nearest2dUserData_EditMesh : public Nearest2dUserData { +class SnapData_EditMesh : public SnapData { public: BMesh *bm; - Nearest2dUserData_EditMesh(SnapObjectContext *sctx, - BMesh *bm, - const float4x4 &obmat) - : Nearest2dUserData(sctx, obmat), bm(bm){}; + SnapData_EditMesh(SnapObjectContext *sctx, BMesh *bm, const float4x4 &obmat) + : SnapData(sctx, obmat), bm(bm){}; void get_vert_co(const int index, const float **r_co) { @@ -449,7 +449,7 @@ eSnapMode snap_polygon_editmesh(SnapObjectContext *sctx, eSnapMode elem = SCE_SNAP_TO_NONE; BMEditMesh *em = BKE_editmesh_from_object(ob_eval); - Nearest2dUserData_EditMesh nearest2d(sctx, em->bm, float4x4(obmat)); + SnapData_EditMesh nearest2d(sctx, em->bm, float4x4(obmat)); nearest2d.clip_planes_enable(sctx); BVHTreeNearest nearest{}; @@ -504,7 +504,7 @@ eSnapMode snap_edge_points_editmesh(SnapObjectContext *sctx, int edge) { BMEditMesh *em = BKE_editmesh_from_object(ob_eval); - Nearest2dUserData_EditMesh nearest2d(sctx, em->bm, float4x4(obmat)); + SnapData_EditMesh nearest2d(sctx, em->bm, float4x4(obmat)); eSnapMode elem = nearest2d.snap_edge_points_impl(sctx, edge, dist_pex_sq_orig); if (nearest2d.nearest_point.index != -2) { nearest2d.register_result(sctx, ob_eval, nullptr); @@ -512,7 +512,7 @@ eSnapMode snap_edge_points_editmesh(SnapObjectContext *sctx, return elem; } -static eSnapMode snapEditMesh(SnapData_EditMesh *sod, +static eSnapMode snapEditMesh(SnapCache_EditMesh *em_cache, SnapObjectContext *sctx, Object *ob_eval, BMEditMesh *em, @@ -521,16 +521,16 @@ static eSnapMode snapEditMesh(SnapData_EditMesh *sod, { BLI_assert(snap_to_flag != SCE_SNAP_TO_FACE); - Nearest2dUserData_EditMesh nearest2d(sctx, em->bm, float4x4(obmat)); + SnapData_EditMesh nearest2d(sctx, em->bm, float4x4(obmat)); /* Was BKE_boundbox_ray_hit_check, see: cf6ca226fa58. */ - if (!nearest2d.snap_boundbox(sod->min, sod->max)) { + if (!nearest2d.snap_boundbox(em_cache->min, em_cache->max)) { return SCE_SNAP_TO_NONE; } - if (snap_to_flag & SCE_SNAP_TO_VERTEX) { + if (snap_to_flag & SCE_SNAP_TO_POINT) { BVHTreeFromEditMesh treedata{}; - treedata.tree = sod->bvhtree[0]; + treedata.tree = em_cache->bvhtree[0]; if (treedata.tree == nullptr) { if (sctx->callbacks.edit_mesh.test_vert_fn) { @@ -550,17 +550,17 @@ static eSnapMode snapEditMesh(SnapData_EditMesh *sod, 2, BVHTREE_FROM_EM_VERTS, /* WORKAROUND: avoid updating while transforming. */ - G.moving ? nullptr : &sod->mesh_runtime->bvh_cache, - &sod->mesh_runtime->eval_mutex); + G.moving ? nullptr : &em_cache->mesh_runtime->bvh_cache, + &em_cache->mesh_runtime->eval_mutex); } - sod->bvhtree[0] = treedata.tree; - sod->cached[0] = treedata.cached; + em_cache->bvhtree[0] = treedata.tree; + em_cache->cached[0] = treedata.cached; } } if (snap_to_flag & SCE_SNAP_TO_EDGE) { BVHTreeFromEditMesh treedata{}; - treedata.tree = sod->bvhtree[1]; + treedata.tree = em_cache->bvhtree[1]; if (treedata.tree == nullptr) { if (sctx->callbacks.edit_mesh.test_edge_fn) { @@ -580,11 +580,11 @@ static eSnapMode snapEditMesh(SnapData_EditMesh *sod, 2, BVHTREE_FROM_EM_EDGES, /* WORKAROUND: avoid updating while transforming. */ - G.moving ? nullptr : &sod->mesh_runtime->bvh_cache, - &sod->mesh_runtime->eval_mutex); + G.moving ? nullptr : &em_cache->mesh_runtime->bvh_cache, + &em_cache->mesh_runtime->eval_mutex); } - sod->bvhtree[1] = treedata.tree; - sod->cached[1] = treedata.cached; + em_cache->bvhtree[1] = treedata.tree; + em_cache->cached[1] = treedata.cached; } } @@ -594,12 +594,12 @@ static eSnapMode snapEditMesh(SnapData_EditMesh *sod, nearest.index = -1; nearest.dist_sq = sctx->ret.dist_px_sq; - eSnapMode elem = SCE_SNAP_TO_VERTEX; + eSnapMode elem = SCE_SNAP_TO_POINT; - if (sod->bvhtree[0] && (snap_to_flag & SCE_SNAP_TO_VERTEX)) { + if (em_cache->bvhtree[0] && (snap_to_flag & SCE_SNAP_TO_POINT)) { BM_mesh_elem_table_ensure(em->bm, BM_VERT); BM_mesh_elem_index_ensure(em->bm, BM_VERT); - BLI_bvhtree_find_nearest_projected(sod->bvhtree[0], + BLI_bvhtree_find_nearest_projected(em_cache->bvhtree[0], nearest2d.pmat_local.ptr(), sctx->runtime.win_size, sctx->runtime.mval, @@ -610,12 +610,12 @@ static eSnapMode snapEditMesh(SnapData_EditMesh *sod, &nearest2d); } - if (sod->bvhtree[1] && (snap_to_flag & SCE_SNAP_TO_EDGE)) { + if (em_cache->bvhtree[1] && (snap_to_flag & SNAP_TO_EDGE_ELEMENTS)) { int last_index = nearest.index; nearest.index = -1; BM_mesh_elem_table_ensure(em->bm, BM_EDGE | BM_VERT); BM_mesh_elem_index_ensure(em->bm, BM_EDGE | BM_VERT); - BLI_bvhtree_find_nearest_projected(sod->bvhtree[1], + BLI_bvhtree_find_nearest_projected(em_cache->bvhtree[1], nearest2d.pmat_local.ptr(), sctx->runtime.win_size, sctx->runtime.mval, @@ -653,31 +653,29 @@ eSnapMode snap_object_editmesh(SnapObjectContext *sctx, { eSnapMode elem = SCE_SNAP_TO_NONE; - SnapData_EditMesh *sod = editmesh_snapdata_init(sctx, ob_eval, snap_to_flag); - if (sod == nullptr) { + SnapCache_EditMesh *em_cache = editmesh_snapdata_init(sctx, ob_eval, snap_to_flag); + if (em_cache == nullptr) { return elem; } - BMEditMesh *em = sod->treedata_editmesh.em; + BMEditMesh *em = em_cache->treedata_editmesh.em; eSnapMode snap_mode_used = snap_to_flag & editmesh_snap_mode_supported(em); - if (snap_mode_used & (SCE_SNAP_TO_EDGE | SCE_SNAP_TO_EDGE_MIDPOINT | - SCE_SNAP_TO_EDGE_PERPENDICULAR | SCE_SNAP_TO_VERTEX)) - { - elem = snapEditMesh(sod, sctx, ob_eval, em, obmat, snap_to_flag); + if (snap_mode_used & (SNAP_TO_EDGE_ELEMENTS | SCE_SNAP_TO_POINT)) { + elem = snapEditMesh(em_cache, sctx, ob_eval, em, obmat, snap_to_flag); if (elem) { return elem; } } if (snap_mode_used & SCE_SNAP_TO_FACE) { - if (raycastEditMesh(sod, sctx, em, obmat, sctx->runtime.object_index++)) { + if (raycastEditMesh(em_cache, sctx, em, obmat, sctx->runtime.object_index++)) { return SCE_SNAP_TO_FACE; } } if (snap_mode_used & SCE_SNAP_INDIVIDUAL_NEAREST) { - if (nearest_world_editmesh(sod, sctx, em, obmat)) { + if (nearest_world_editmesh(em_cache, sctx, em, obmat)) { return SCE_SNAP_INDIVIDUAL_NEAREST; } } diff --git a/source/blender/editors/transform/transform_snap_object_mesh.cc b/source/blender/editors/transform/transform_snap_object_mesh.cc index 6f7d9a85726..20dc28c6be1 100644 --- a/source/blender/editors/transform/transform_snap_object_mesh.cc +++ b/source/blender/editors/transform/transform_snap_object_mesh.cc @@ -239,7 +239,7 @@ static bool nearest_world_mesh(SnapObjectContext *sctx, /** \name Subclass for Snapping to Edges or Points of a Mesh * \{ */ -class Nearest2dUserData_Mesh : public Nearest2dUserData { +class SnapData_Mesh : public SnapData { public: const float3 *vert_positions; const float3 *vert_normals; @@ -248,10 +248,8 @@ class Nearest2dUserData_Mesh : public Nearest2dUserData { const int *corner_edges; const MLoopTri *looptris; - Nearest2dUserData_Mesh(SnapObjectContext *sctx, - const Mesh *mesh_eval, - const float4x4 &obmat) - : Nearest2dUserData(sctx, obmat) + SnapData_Mesh(SnapObjectContext *sctx, const Mesh *mesh_eval, const float4x4 &obmat) + : SnapData(sctx, obmat) { this->vert_positions = mesh_eval->vert_positions().data(); this->vert_normals = mesh_eval->vert_normals().data(); @@ -314,7 +312,7 @@ static void cb_snap_edge_verts(void *userdata, const int clip_plane_len, BVHTreeNearest *nearest) { - Nearest2dUserData_Mesh *data = static_cast(userdata); + SnapData_Mesh *data = static_cast(userdata); int vindex[2]; data->get_edge_verts_index(index, vindex); @@ -334,7 +332,7 @@ static void cb_snap_tri_verts(void *userdata, const int clip_plane_len, BVHTreeNearest *nearest) { - Nearest2dUserData_Mesh *data = static_cast(userdata); + SnapData_Mesh *data = static_cast(userdata); int vindex[3]; data->get_tri_verts_index(index, vindex); @@ -365,7 +363,7 @@ static void cb_snap_tri_edges(void *userdata, const int clip_plane_len, BVHTreeNearest *nearest) { - Nearest2dUserData_Mesh *data = static_cast(userdata); + SnapData_Mesh *data = static_cast(userdata); if (data->use_backface_culling) { int vindex[3]; @@ -410,7 +408,7 @@ eSnapMode snap_polygon_mesh(SnapObjectContext *sctx, const Mesh *mesh_eval = reinterpret_cast(id); - Nearest2dUserData_Mesh nearest2d(sctx, mesh_eval, float4x4(obmat)); + SnapData_Mesh nearest2d(sctx, mesh_eval, float4x4(obmat)); nearest2d.clip_planes_enable(sctx); BVHTreeNearest nearest{}; @@ -461,7 +459,7 @@ eSnapMode snap_edge_points_mesh(SnapObjectContext *sctx, float dist_pex_sq_orig, int edge) { - Nearest2dUserData_Mesh nearest2d(sctx, reinterpret_cast(id), float4x4(obmat)); + SnapData_Mesh nearest2d(sctx, reinterpret_cast(id), float4x4(obmat)); eSnapMode elem = nearest2d.snap_edge_points_impl(sctx, edge, dist_pex_sq_orig); if (nearest2d.nearest_point.index != -2) { nearest2d.register_result(sctx, ob_eval, id); @@ -479,11 +477,11 @@ static eSnapMode snapMesh(SnapObjectContext *sctx, if (me_eval->totvert == 0) { return SCE_SNAP_TO_NONE; } - if (me_eval->totedge == 0 && !(sctx->runtime.snap_to_flag & SCE_SNAP_TO_VERTEX)) { + if (me_eval->totedge == 0 && !(sctx->runtime.snap_to_flag & SCE_SNAP_TO_POINT)) { return SCE_SNAP_TO_NONE; } - Nearest2dUserData_Mesh nearest2d(sctx, me_eval, float4x4(obmat)); + SnapData_Mesh nearest2d(sctx, me_eval, float4x4(obmat)); if (ob_eval->data == me_eval) { const BoundBox *bb = BKE_mesh_boundbox_get(ob_eval); @@ -498,7 +496,7 @@ static eSnapMode snapMesh(SnapObjectContext *sctx, BVHTree *bvhtree[2] = {nullptr}; bvhtree[0] = BKE_bvhtree_from_mesh_get(&treedata_dummy, me_eval, BVHTREE_FROM_LOOSEEDGES, 2); BLI_assert(treedata_dummy.cached); - if (sctx->runtime.snap_to_flag & SCE_SNAP_TO_VERTEX) { + if (sctx->runtime.snap_to_flag & SCE_SNAP_TO_POINT) { bvhtree[1] = BKE_bvhtree_from_mesh_get(&treedata_dummy, me_eval, BVHTREE_FROM_LOOSEVERTS, 2); BLI_assert(treedata_dummy.cached); } @@ -510,10 +508,10 @@ static eSnapMode snapMesh(SnapObjectContext *sctx, nearest.dist_sq = sctx->ret.dist_px_sq; int last_index = nearest.index; - eSnapMode elem = SCE_SNAP_TO_VERTEX; + eSnapMode elem = SCE_SNAP_TO_POINT; if (bvhtree[1]) { - BLI_assert(sctx->runtime.snap_to_flag & SCE_SNAP_TO_VERTEX); + BLI_assert(sctx->runtime.snap_to_flag & SCE_SNAP_TO_POINT); /* snap to loose verts */ BLI_bvhtree_find_nearest_projected(bvhtree[1], nearest2d.pmat_local.ptr(), @@ -528,7 +526,7 @@ static eSnapMode snapMesh(SnapObjectContext *sctx, last_index = nearest.index; } - if (sctx->runtime.snap_to_flag & SCE_SNAP_TO_EDGE) { + if (sctx->runtime.snap_to_flag & (SNAP_TO_EDGE_ELEMENTS & ~SCE_SNAP_TO_EDGE_ENDPOINT)) { if (bvhtree[0]) { /* Snap to loose edges. */ BLI_bvhtree_find_nearest_projected( @@ -562,7 +560,7 @@ static eSnapMode snapMesh(SnapObjectContext *sctx, } } else { - BLI_assert(sctx->runtime.snap_to_flag & SCE_SNAP_TO_VERTEX); + BLI_assert(sctx->runtime.snap_to_flag & SCE_SNAP_TO_EDGE_ENDPOINT); if (bvhtree[0]) { /* Snap to loose edge verts. */ BLI_bvhtree_find_nearest_projected( @@ -631,9 +629,7 @@ eSnapMode snap_object_mesh(SnapObjectContext *sctx, const Mesh *mesh_eval = reinterpret_cast(id); eSnapMode snap_mode_used = snap_to_flag & mesh_snap_mode_supported(mesh_eval); - if (snap_mode_used & (SCE_SNAP_TO_EDGE | SCE_SNAP_TO_EDGE_MIDPOINT | - SCE_SNAP_TO_EDGE_PERPENDICULAR | SCE_SNAP_TO_VERTEX)) - { + if (snap_mode_used & (SNAP_TO_EDGE_ELEMENTS | SCE_SNAP_TO_POINT)) { elem = snapMesh(sctx, ob_eval, mesh_eval, obmat, use_hide); if (elem) { return elem; diff --git a/source/blender/makesdna/DNA_scene_types.h b/source/blender/makesdna/DNA_scene_types.h index 2fe18ec3565..2b666f251c1 100644 --- a/source/blender/makesdna/DNA_scene_types.h +++ b/source/blender/makesdna/DNA_scene_types.h @@ -2305,7 +2305,10 @@ typedef enum eSnapMode { SCE_SNAP_TO_NODE_Y = (1 << 1), /** #ToolSettings.snap_mode and #ToolSettings.snap_node_mode and #ToolSettings.snap_uv_mode */ - SCE_SNAP_TO_VERTEX = (1 << 0), + SCE_SNAP_TO_POINT = (1 << 0), + /* Even with the same value, there is a distinction between point and endpoint in the snap code. + * Therefore, use different enums for better code readability. */ + SCE_SNAP_TO_EDGE_ENDPOINT = (1 << 0), SCE_SNAP_TO_EDGE = (1 << 1), SCE_SNAP_TO_FACE = (1 << 2), SCE_SNAP_TO_VOLUME = (1 << 3), @@ -2324,6 +2327,8 @@ typedef enum eSnapMode { ENUM_OPERATORS(eSnapMode, SCE_SNAP_INDIVIDUAL_PROJECT) #endif +#define SCE_SNAP_TO_VERTEX (SCE_SNAP_TO_POINT | SCE_SNAP_TO_EDGE_ENDPOINT) + #define SCE_SNAP_TO_GEOM \ (SCE_SNAP_TO_VERTEX | SCE_SNAP_TO_EDGE | SCE_SNAP_TO_FACE | SCE_SNAP_TO_EDGE_PERPENDICULAR | \ SCE_SNAP_TO_EDGE_MIDPOINT)