From 03854e8e3c70da3d78f626160ea68adef93caaca Mon Sep 17 00:00:00 2001 From: Falk David Date: Mon, 20 Feb 2023 12:33:58 +0100 Subject: [PATCH 1/2] Curves: Add cursor snapping support --- .../editors/curves/intern/curves_data.cc | 34 +++++++++++++++++++ source/blender/editors/include/ED_curves.h | 13 +++++++ source/blender/editors/util/ed_transverts.c | 16 +++++++-- 3 files changed, 61 insertions(+), 2 deletions(-) diff --git a/source/blender/editors/curves/intern/curves_data.cc b/source/blender/editors/curves/intern/curves_data.cc index c11879a18f9..7c5a516ca1b 100644 --- a/source/blender/editors/curves/intern/curves_data.cc +++ b/source/blender/editors/curves/intern/curves_data.cc @@ -3,7 +3,34 @@ #include "BKE_curves.hh" #include "BKE_geometry_fields.hh" +#include "BLI_task.hh" + #include "ED_curves.h" +#include "ED_transverts.h" + +namespace blender::ed::curves { + +void transverts_from_curves_positions_create(bke::CurvesGeometry &curves, TransVertStore *tvs) +{ + Vector selected_indices; + IndexMask selection = retrieve_selected_points(curves, selected_indices); + MutableSpan positions = curves.positions_for_write(); + + tvs->transverts = static_cast( + MEM_calloc_arrayN(selection.size(), sizeof(TransVert), __func__)); + tvs->transverts_tot = selection.size(); + + threading::parallel_for(selection.index_range(), 1024, [&](const IndexRange selection_range) { + for (const int point_i : selection_range) { + TransVert &tv = tvs->transverts[point_i]; + tv.loc = reinterpret_cast(&positions[selection[point_i]]); + tv.flag = true; + copy_v3_v3(tv.oldloc, tv.loc); + } + }); +} + +} // namespace blender::ed::curves float (*ED_curves_point_normals_array_create(const Curves *curves_id))[3] { @@ -21,3 +48,10 @@ float (*ED_curves_point_normals_array_create(const Curves *curves_id))[3] return reinterpret_cast(data); } + +void ED_curves_transverts_create(Curves *curves_id, TransVertStore *tvs) +{ + using namespace blender; + bke::CurvesGeometry &curves = curves_id->geometry.wrap(); + ed::curves::transverts_from_curves_positions_create(curves, tvs); +} diff --git a/source/blender/editors/include/ED_curves.h b/source/blender/editors/include/ED_curves.h index 178ec7a5b19..1adcade614f 100644 --- a/source/blender/editors/include/ED_curves.h +++ b/source/blender/editors/include/ED_curves.h @@ -12,6 +12,7 @@ struct UndoType; struct SelectPick_Params; struct ViewContext; struct rcti; +struct TransVertStore; #ifdef __cplusplus extern "C" { @@ -32,6 +33,11 @@ void ED_keymap_curves(struct wmKeyConfig *keyconf); */ float (*ED_curves_point_normals_array_create(const struct Curves *curves_id))[3]; +/** + * Wrapper for `transverts_from_curves_positions_create`. + */ +void ED_curves_transverts_create(struct Curves *curves_id, struct TransVertStore *tvs); + /** \} */ #ifdef __cplusplus @@ -56,6 +62,13 @@ bke::CurvesGeometry primitive_random_sphere(int curves_size, int points_per_curv VectorSet get_unique_editable_curves(const bContext &C); void ensure_surface_deformation_node_exists(bContext &C, Object &curves_ob); +/** + * Allocate an array of `TransVert` for cursor/selection snapping (See + * `ED_transverts_create_from_obedit` in `view3d_snap.c`). + * Note: the `TransVert` elements in \a tvs are expected to write to the positions of \a curves. + */ +void transverts_from_curves_positions_create(bke::CurvesGeometry &curves, TransVertStore *tvs); + /* -------------------------------------------------------------------- */ /** \name Poll Functions * \{ */ diff --git a/source/blender/editors/util/ed_transverts.c b/source/blender/editors/util/ed_transverts.c index e91242cb9e6..b85f9ff9c12 100644 --- a/source/blender/editors/util/ed_transverts.c +++ b/source/blender/editors/util/ed_transverts.c @@ -9,6 +9,7 @@ #include "DNA_armature_types.h" #include "DNA_curve_types.h" +#include "DNA_curves_types.h" #include "DNA_lattice_types.h" #include "DNA_meta_types.h" #include "DNA_object_types.h" @@ -30,6 +31,7 @@ #include "DEG_depsgraph.h" #include "ED_armature.h" +#include "ED_curves.h" #include "ED_transverts.h" /* own include */ @@ -181,8 +183,14 @@ static void set_mapped_co(void *vuserdata, int index, const float co[3], const f bool ED_transverts_check_obedit(const Object *obedit) { - return ( - ELEM(obedit->type, OB_ARMATURE, OB_LATTICE, OB_MESH, OB_SURF, OB_CURVES_LEGACY, OB_MBALL)); + return (ELEM(obedit->type, + OB_ARMATURE, + OB_LATTICE, + OB_MESH, + OB_SURF, + OB_CURVES_LEGACY, + OB_MBALL, + OB_CURVES)); } void ED_transverts_create_from_obedit(TransVertStore *tvs, const Object *obedit, const int mode) @@ -481,6 +489,10 @@ void ED_transverts_create_from_obedit(TransVertStore *tvs, const Object *obedit, bp++; } } + else if (obedit->type == OB_CURVES) { + Curves *curves_id = obedit->data; + ED_curves_transverts_create(curves_id, tvs); + } if (!tvs->transverts_tot && tvs->transverts) { /* Prevent memory leak. happens for curves/lattices due to -- 2.30.2 From bd91650e21a22001d65289697c11f4633a53e466 Mon Sep 17 00:00:00 2001 From: Falk David Date: Tue, 21 Feb 2023 10:52:04 +0100 Subject: [PATCH 2/2] Cleanup --- source/blender/editors/curves/intern/curves_data.cc | 6 ++++-- source/blender/editors/include/ED_curves.h | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/source/blender/editors/curves/intern/curves_data.cc b/source/blender/editors/curves/intern/curves_data.cc index 7c5a516ca1b..d178ee60890 100644 --- a/source/blender/editors/curves/intern/curves_data.cc +++ b/source/blender/editors/curves/intern/curves_data.cc @@ -5,6 +5,8 @@ #include "BLI_task.hh" +#include "DNA_object_types.h" + #include "ED_curves.h" #include "ED_transverts.h" @@ -23,8 +25,8 @@ void transverts_from_curves_positions_create(bke::CurvesGeometry &curves, TransV threading::parallel_for(selection.index_range(), 1024, [&](const IndexRange selection_range) { for (const int point_i : selection_range) { TransVert &tv = tvs->transverts[point_i]; - tv.loc = reinterpret_cast(&positions[selection[point_i]]); - tv.flag = true; + tv.loc = positions[selection[point_i]]; + tv.flag = SELECT; copy_v3_v3(tv.oldloc, tv.loc); } }); diff --git a/source/blender/editors/include/ED_curves.h b/source/blender/editors/include/ED_curves.h index 1adcade614f..c86508548fd 100644 --- a/source/blender/editors/include/ED_curves.h +++ b/source/blender/editors/include/ED_curves.h @@ -65,7 +65,7 @@ void ensure_surface_deformation_node_exists(bContext &C, Object &curves_ob); /** * Allocate an array of `TransVert` for cursor/selection snapping (See * `ED_transverts_create_from_obedit` in `view3d_snap.c`). - * Note: the `TransVert` elements in \a tvs are expected to write to the positions of \a curves. + * \note: the `TransVert` elements in \a tvs are expected to write to the positions of \a curves. */ void transverts_from_curves_positions_create(bke::CurvesGeometry &curves, TransVertStore *tvs); -- 2.30.2