Blender V5.3
transform_convert_curves.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <optional>
10
11#include "BLI_array.hh"
12#include "BLI_array_utils.hh"
15#include "BLI_math_matrix_c.hh"
16#include "BLI_span.hh"
17
18#include "BKE_attribute.hh"
19#include "BKE_context.hh"
20#include "BKE_crazyspace.hh"
21#include "BKE_curves.hh"
22#include "BKE_curves_utils.hh"
23#include "BKE_geometry_set.hh"
24
25#include "ED_curves.hh"
26
27#include "MEM_guardedalloc.h"
28
29#include "transform.hh"
30#include "transform_convert.hh"
31#include "transform_snap.hh"
32
33/* -------------------------------------------------------------------- */
36
38
40 const Span<IndexMask> points_to_transform_per_attr,
41 const int curve_index,
42 TransCustomData &custom_data)
43{
44 if (points_to_transform_per_attr.size() == 1) {
45 return;
46 }
47 const VArraySpan<int8_t> handle_types_left = curves.handle_types_left();
48 const VArraySpan<int8_t> handle_types_right = curves.handle_types_right();
49 CurvesTransformData &transform_data = *static_cast<CurvesTransformData *>(custom_data.data);
50
51 IndexMaskMemory memory;
52 /* When control point is selected both handles are threaded as selected and transformed together.
53 * So these will be excluded from alignment. */
54 const IndexMask &selected_points = points_to_transform_per_attr[0];
55 const IndexMask selected_left_handles = IndexMask::from_difference(
56 points_to_transform_per_attr[1], selected_points, memory);
58 /* Left are excluded here to align only one handle when both are selected. */
59 const IndexMask selected_right_handles = evaluate_expression(
60 builder.subtract({&points_to_transform_per_attr[2]},
61 {&selected_left_handles, &selected_points}),
62 memory);
63
64 const IndexMask &affected_handles = IndexMask::from_union(
65 selected_left_handles, selected_right_handles, memory);
66
67 auto aligned_handles_to_selection = [&](const VArraySpan<int8_t> &handle_types) {
68 return IndexMask::from_predicate(affected_handles, memory, [&](const int64_t i) {
69 return handle_types[i] == BEZIER_HANDLE_ALIGN;
70 });
71 };
72
73 const IndexMask both_aligned = IndexMask::from_intersection(
74 aligned_handles_to_selection(handle_types_left),
75 aligned_handles_to_selection(handle_types_right),
76 memory);
77
78 transform_data.aligned_with_left[curve_index] = IndexMask::from_intersection(
79 selected_left_handles, both_aligned, transform_data.memory);
80 transform_data.aligned_with_right[curve_index] = IndexMask::from_intersection(
81 selected_right_handles, both_aligned, transform_data.memory);
82}
83
85 MutableSpan<float> r_distances)
86{
87 BLI_assert(positions.size() == r_distances.size());
88 Array<bool, 32> visited(positions.size(), false);
89
91 while (!queue.is_empty()) {
92 int64_t index = queue.pop_index();
93 if (visited[index]) {
94 continue;
95 }
96 visited[index] = true;
97
98 const int left_i = index - 1;
99 if (left_i >= 0 && !visited[left_i]) {
100 const float left_dist = r_distances[index] +
101 math::distance(positions[index], positions[left_i]);
102 if (left_dist < r_distances[left_i]) {
103 r_distances[left_i] = left_dist;
104 queue.priority_increased(left_i);
105 }
106 }
107
108 const int right_i = index + 1;
109 if (right_i < positions.size() && !visited[right_i]) {
110 const float right_dist = r_distances[index] +
111 math::distance(positions[index], positions[right_i]);
112 if (right_dist < r_distances[right_i]) {
113 r_distances[right_i] = right_dist;
114 queue.priority_increased(right_i);
115 }
116 }
117 }
118}
119
121 MutableSpan<float> r_distances)
122{
123 BLI_assert(positions.size() == r_distances.size());
124 Array<bool, 32> visited(positions.size(), false);
125
126 InplacePriorityQueue<float, std::less<>> queue(r_distances);
127 while (!queue.is_empty()) {
128 int64_t index = queue.pop_index();
129 if (visited[index]) {
130 continue;
131 }
132 visited[index] = true;
133
134 const int left_i = math::mod_periodic<int>(index - 1, positions.size());
135 const float left_dist = r_distances[index] +
136 math::distance(positions[index], positions[left_i]);
137 if (left_dist < r_distances[left_i] && !visited[left_i]) {
138 r_distances[left_i] = left_dist;
139 queue.priority_increased(left_i);
140 }
141
142 const int right_i = math::mod_periodic<int>(index + 1, positions.size());
143 const float right_dist = r_distances[index] +
144 math::distance(positions[index], positions[right_i]);
145 if (right_dist < r_distances[right_i] && !visited[right_i]) {
146 r_distances[right_i] = right_dist;
147 queue.priority_increased(right_i);
148 }
149 }
150}
151
152static IndexMask handles_by_type(const IndexMask &handles,
153 const VArray<int8_t> &types,
154 const HandleType type,
155 IndexMaskMemory &memory)
156{
157 if (const std::optional<int8_t> single_type = types.get_if_single()) {
158 return HandleType(*single_type) == type ? handles : IndexMask();
159 }
160 const VArraySpan types_span = types;
162 handles, memory, [&](const int64_t i) { return types_span[i] == type; });
163}
164
166 const IndexMask &auto_handles,
167 const IndexMask &auto_handles_opposite,
168 const IndexMask &selected_handles,
169 const IndexMask &selected_handles_opposite,
170 const StringRef handle_type_name,
171 IndexMaskMemory &memory)
172{
174 const IndexMask convert_to_align = evaluate_expression(
175 builder.merge({
176 /* Selected BEZIER_HANDLE_AUTO handles from one side. */
177 &builder.intersect({&selected_handles, &auto_handles}),
178 /* Both sides are BEZIER_HANDLE_AUTO and opposite side is selected.
179 * It ensures to convert both handles, when only one is transformed. */
180 &builder.intersect({&selected_handles_opposite, &auto_handles_opposite, &auto_handles}),
181 }),
182 memory);
183 if (convert_to_align.is_empty()) {
184 return false;
185 }
186 bke::MutableAttributeAccessor attributes = curves.attributes_for_write();
188 handle_type_name, bke::AttrDomain::Point);
189 index_mask::masked_fill(handle_types.span, int8_t(BEZIER_HANDLE_ALIGN), convert_to_align);
190 handle_types.finish();
191 return true;
192}
193
195 const IndexMask &selected_handles,
196 const StringRef handle_type_name)
197{
198 bke::MutableAttributeAccessor attributes = curves.attributes_for_write();
199
200 IndexMaskMemory memory;
201 const IndexMask selected_vector = handles_by_type(
202 selected_handles,
203 *attributes.lookup_or_default<int8_t>(handle_type_name, bke::AttrDomain::Point, 0),
205 memory);
206 if (selected_vector.is_empty()) {
207 return false;
208 }
209
211 handle_type_name, bke::AttrDomain::Point);
212 index_mask::masked_fill(handle_types.span, int8_t(BEZIER_HANDLE_FREE), selected_vector);
213 handle_types.finish();
214 return true;
215}
216
218 const std::array<IndexMask, 3> &selection_per_attribute,
219 const IndexMask &bezier_points,
221{
222 IndexMaskMemory memory;
223
224 const IndexMask selected_left = IndexMask::from_difference(
225 selection_per_attribute[1], selection_per_attribute[0], memory);
226 const IndexMask selected_right = IndexMask::from_difference(
227 selection_per_attribute[2], selection_per_attribute[0], memory);
228
229 const IndexMask auto_left = handles_by_type(
230 bezier_points, curves.handle_types_left(), BEZIER_HANDLE_AUTO, memory);
231 const IndexMask auto_right = handles_by_type(
232 bezier_points, curves.handle_types_right(), BEZIER_HANDLE_AUTO, memory);
233
234 bool changed = false;
235
236 if (ELEM(mode, TFM_ROTATION, TFM_RESIZE) && selection_per_attribute[0].size() == 1 &&
237 selected_left.is_empty() && selected_right.is_empty())
238 {
239 const int64_t selected_point = selection_per_attribute[0].first();
240 if (auto_left.contains(selected_point)) {
241 curves.handle_types_left_for_write()[selected_point] = BEZIER_HANDLE_ALIGN;
242 changed = true;
243 }
244 if (auto_right.contains(selected_point)) {
245 curves.handle_types_right_for_write()[selected_point] = BEZIER_HANDLE_ALIGN;
246 changed = true;
247 }
248 }
249 else {
250 changed |= update_auto_handle_types(
251 curves, auto_left, auto_right, selected_left, selected_right, "handle_type_left", memory);
252 changed |= update_auto_handle_types(
253 curves, auto_right, auto_left, selected_right, selected_left, "handle_type_right", memory);
254
255 changed |= update_vector_handle_types(curves, selected_left, "handle_type_left");
256 changed |= update_vector_handle_types(curves, selected_right, "handle_type_right");
257 }
258
259 if (changed) {
260 curves.tag_topology_changed();
261 }
262
263 return changed;
264}
265
267 std::optional<Span<float3>> positions,
268 TransCustomData &custom_data)
269{
270 CurvesTransformData &transform_data = *static_cast<CurvesTransformData *>(custom_data.data);
271 transform_data.selection_by_layer.append(selection);
272 const int data_offset = transform_data.layer_offsets.last();
273 transform_data.layer_offsets.append(data_offset + selection.size());
274 if (positions) {
276 *positions,
277 selection,
278 transform_data.positions.as_mutable_span().slice(data_offset, selection.size()));
279 }
280 else {
281 transform_data.positions.as_mutable_span()
282 .slice(data_offset, selection.size())
283 .fill(float3(0.0f));
284 }
285 return transform_data.positions.as_mutable_span().slice(transform_data.layer_offsets.last(1),
286 selection.size());
287}
288
290{
292 Array<Vector<IndexMask>> points_to_transform_per_attribute(t->data_container_len);
293 Array<IndexMask> bezier_curves(t->data_container_len);
294 const bool use_proportional_edit = (t->flag & T_PROP_EDIT_ALL) != 0;
295 const bool use_connected_only = (t->flag & T_PROP_CONNECTED) != 0;
296
297 /* Evaluated depsgraph is necessary for taking into account deformation from modifiers. */
298 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
299
300 /* Count selected elements per object and create TransData structs. */
301 for (const int i : trans_data_contrainers.index_range()) {
302 TransDataContainer &tc = trans_data_contrainers[i];
303 Curves *curves_id = id_cast<Curves *>(tc.obedit->data);
304 bke::CurvesGeometry &curves = curves_id->geometry.wrap();
306 tc.custom.type);
308 curves);
309 std::array<IndexMask, 3> selection_per_attribute;
310
311 bezier_curves[i] = bke::curves::indices_for_type(curves.curve_types(),
312 curves.curve_type_counts(),
314 curves.curves_range(),
315 curves_transform_data->memory);
316
318 curves.points_by_curve(), bezier_curves[i], curves_transform_data->memory);
319
320 for (const int attribute_i : selection_attribute_names.index_range()) {
321 const StringRef &selection_name = selection_attribute_names[attribute_i];
322 selection_per_attribute[attribute_i] = ed::curves::retrieve_selected_points(
323 curves, selection_name, bezier_points, curves_transform_data->memory);
324 }
325
326 /* Alter selection as in legacy curves bezt_select_to_transform_triple_flag(). */
327 if (!bezier_points.is_empty()) {
328 update_handle_types_for_transform(t->mode, selection_per_attribute, bezier_points, curves);
329
331 const index_mask::Expr &selected_bezier_points = builder.intersect(
332 {&bezier_points, selection_per_attribute.data()});
333
334 /* Select bezier handles that must be transformed because the control point is
335 * selected. */
336 selection_per_attribute[1] = evaluate_expression(
337 builder.merge({&selection_per_attribute[1], &selected_bezier_points}),
338 curves_transform_data->memory);
339 selection_per_attribute[2] = evaluate_expression(
340 builder.merge({&selection_per_attribute[2], &selected_bezier_points}),
341 curves_transform_data->memory);
342 }
343
344 if (use_proportional_edit) {
345 tc.data_len = curves.points_num() + 2 * bezier_points.size();
346 points_to_transform_per_attribute[i].append(curves.points_range());
347
348 if (selection_attribute_names.size() > 1) {
349 points_to_transform_per_attribute[i].append(bezier_points);
350 points_to_transform_per_attribute[i].append(bezier_points);
351 }
352 }
353 else {
354 tc.data_len = 0;
355 for (const int selection_i : selection_attribute_names.index_range()) {
356 points_to_transform_per_attribute[i].append(selection_per_attribute[selection_i]);
357 tc.data_len += points_to_transform_per_attribute[i][selection_i].size();
358 }
359 }
360
361 if (tc.data_len > 0) {
363 curves_transform_data->positions.reinitialize(tc.data_len);
364 }
365 else {
366 tc.custom.type.free_cb(t, &tc, &tc.custom.type);
367 }
368 }
369
370 /* Populate TransData structs. */
371 for (const int i : trans_data_contrainers.index_range()) {
372 TransDataContainer &tc = trans_data_contrainers[i];
373 if (tc.data_len == 0) {
374 continue;
375 }
376 Object *object = tc.obedit;
377 Curves *curves_id = id_cast<Curves *>(object->data);
378 bke::CurvesGeometry &curves = curves_id->geometry.wrap();
379 const bke::crazyspace::GeometryDeformation deformation =
381
382 std::optional<MutableSpan<float>> value_attribute;
383 bke::SpanAttributeWriter<float> attribute_writer;
384 if (t->mode == TFM_CURVE_SHRINKFATTEN) {
385 bke::MutableAttributeAccessor attributes = curves.attributes_for_write();
386 attribute_writer = attributes.lookup_or_add_for_write_span<float>(
387 "radius",
390 value_attribute = attribute_writer.span;
391 }
392 else if (t->mode == TFM_TILT) {
393 bke::MutableAttributeAccessor attributes = curves.attributes_for_write();
394 attribute_writer = attributes.lookup_or_add_for_write_span<float>("tilt",
396 value_attribute = attribute_writer.span;
397 }
398
399 CurvesTransformData &transform_data = *static_cast<CurvesTransformData *>(tc.custom.type.data);
400 transform_data.aligned_with_left.reinitialize(1);
401 transform_data.aligned_with_right.reinitialize(1);
402
404 tc,
405 curves,
406 object->object_to_world(),
407 deformation,
408 value_attribute,
409 points_to_transform_per_attribute[i],
410 curves.curves_range(),
411 use_connected_only,
412 bezier_curves[i]);
413 create_aligned_handles_masks(curves, points_to_transform_per_attribute[i], 0, tc.custom.type);
414
415 /* TODO: This is wrong. The attribute writer should live at least as long as the span. */
416 attribute_writer.finish();
417 }
418}
419
422 const int curve_index)
423{
425 return;
426 }
427 const CurvesTransformData &transform_data = *static_cast<const CurvesTransformData *>(
428 custom_data.data);
429
430 const Span<float3> positions = curves.positions();
431 MutableSpan<float3> handle_positions_left = curves.handle_positions_left_for_write();
432 MutableSpan<float3> handle_positions_right = curves.handle_positions_right_for_write();
433
435 transform_data.aligned_with_left[curve_index],
436 positions,
437 handle_positions_left,
438 handle_positions_right);
440 transform_data.aligned_with_right[curve_index],
441 positions,
442 handle_positions_right,
443 handle_positions_left);
444}
445
447{
448 if (t->state != TRANS_CANCEL) {
450 }
451
452 const Span<TransDataContainer> trans_data_contrainers(t->data_container, t->data_container_len);
453 for (const TransDataContainer &tc : trans_data_contrainers) {
454 Curves *curves_id = id_cast<Curves *>(tc.obedit->data);
455 bke::CurvesGeometry &curves = curves_id->geometry.wrap();
456 if (t->mode == TFM_CURVE_SHRINKFATTEN) {
457 curves.tag_radii_changed();
458 }
459 else if (t->mode == TFM_TILT) {
460 curves.tag_normals_changed();
461 }
462 else {
463 const Vector<MutableSpan<float3>> positions_per_selection_attr =
465 for (const int i : positions_per_selection_attr.index_range()) {
467 tc.custom.type, i, positions_per_selection_attr[i]);
468 }
469 curves.tag_positions_changed();
470 curves.calculate_bezier_auto_handles();
471 calculate_single_aligned_handles(tc.custom.type, curves, 0);
472 }
473 DEG_id_tag_update(&curves_id->id, ID_RECALC_GEOMETRY);
474 }
475}
476
478{
479 const CurvesTransformData &transform_data = *static_cast<CurvesTransformData *>(
480 custom_data.data);
481 return OffsetIndices(transform_data.layer_offsets.as_span().slice(
482 transform_data.layer_offsets.size() - num - 1, num + 1));
483}
484
492static void fill_map(const CurveType curve_type,
493 const IndexRange curve_points,
494 const OffsetIndices<int> position_offsets_in_td,
495 const int handles_offset,
497{
498 const int position_index = curve_points.start() + position_offsets_in_td[0].start();
499 if (curve_type == CURVE_TYPE_BEZIER) {
500 const int left_handle_index = handles_offset + position_offsets_in_td[1].start();
501 const int right_handle_index = handles_offset + position_offsets_in_td[2].start();
502 std::array<int, 3> first_per_attr = {left_handle_index, position_index, right_handle_index};
503 threading::parallel_for(curve_points.index_range(), 4096, [&](const IndexRange range) {
504 for (const int i : range) {
505 for (const int attr : IndexRange(3)) {
506 map[i * 3 + attr] = first_per_attr[attr] + i;
507 }
508 }
509 });
510 }
511 else {
512 array_utils::fill_index_range(map, position_index);
513 }
514}
515
517{
518 CurvesTransformData *transform_data = MEM_new<CurvesTransformData>(__func__);
519 transform_data->layer_offsets.append(0);
520 custom_data.data = transform_data;
521 custom_data.free_cb = [](TransInfo *, TransDataContainer *, TransCustomData *custom_data) {
522 CurvesTransformData *data = static_cast<CurvesTransformData *>(custom_data->data);
523 MEM_delete(data);
524 custom_data->data = nullptr;
525 };
526 return transform_data;
527}
528
530 const int layer,
531 MutableSpan<float3> positions_dst)
532{
533 const CurvesTransformData &transform_data = *static_cast<CurvesTransformData *>(
534 custom_data.data);
535 const IndexMask &selection = transform_data.selection_by_layer[layer];
536 OffsetIndices<int> offsets{transform_data.layer_offsets};
537 Span<float3> positions = transform_data.positions.as_span().slice(offsets[layer]);
538
539 array_utils::scatter(positions, selection, positions_dst);
540}
541
545 const float4x4 &transform,
546 const bke::crazyspace::GeometryDeformation &deformation,
547 std::optional<MutableSpan<float>> value_attribute,
548 const Span<IndexMask> points_to_transform_per_attr,
549 const IndexMask &affected_curves,
550 bool use_connected_only,
551 const IndexMask &bezier_curves,
552 void *extra)
553{
554 const std::array<std::optional<Span<float3>>, 3> src_positions_per_selection_attr = {
555 curves.positions(), curves.handle_positions_left(), curves.handle_positions_right()};
556 const View3D *v3d = (t.spacetype == SPACE_VIEW3D) ? static_cast<const View3D *>(t.view) :
557 nullptr;
558 const bool hide_handles = (v3d != nullptr) ? (v3d->overlay.handle_display == CURVE_HANDLE_NONE) :
559 false;
560 const bool use_individual_origin = (t.around == V3D_AROUND_LOCAL_ORIGINS);
561 const Span<float3> point_positions = curves.positions();
562 const VArray<bool> cyclic = curves.cyclic();
563 const VArray<bool> point_selection = *curves.attributes().lookup_or_default<bool>(
564 ".selection", bke::AttrDomain::Point, true);
565 const VArray<int8_t> curve_types = curves.curve_types();
566
567 std::array<MutableSpan<float3>, 3> positions_per_selection_attr;
568 for (const int selection_i : points_to_transform_per_attr.index_range()) {
569 positions_per_selection_attr[selection_i] = append_positions_to_custom_data(
570 points_to_transform_per_attr[selection_i],
571 src_positions_per_selection_attr[selection_i],
572 tc.custom.type);
573 }
574
575 MutableSpan<TransData> all_tc_data = MutableSpan(tc.data, tc.data_len);
576 OffsetIndices<int> position_offsets_in_td = recent_position_offsets(
577 tc.custom.type, points_to_transform_per_attr.size());
578
579 Vector<VArray<bool>> selection_attrs;
581 curves);
582 for (const StringRef selection_name : selection_attribute_names) {
583 const VArray<bool> selection_attr = *curves.attributes().lookup_or_default<bool>(
584 selection_name, bke::AttrDomain::Point, true);
585 selection_attrs.append(selection_attr);
586 }
587
588 const float3x3 mtx_base = transform.view<3, 3>();
589 const float3x3 smtx_base = math::pseudo_invert(mtx_base);
590
591 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
592 Array<float3> mean_center_point_per_curve(curves.curves_num(), float3(0));
593 if (use_individual_origin) {
594 affected_curves.foreach_index(
595 [&](const int64_t curve_i) {
596 const IndexRange points = points_by_curve[curve_i];
597 IndexMaskMemory memory;
598 const IndexMask selection =
599 IndexMask::from_bools(point_selection, memory).slice_content(points);
600 if (selection.is_empty()) {
601 /* For proportional editing around individual origins, unselected points will not use
602 * the TransData center (instead the closest point found is used, see logic in
603 * #set_prop_dist / #prop_dist_loc_get). */
604 return;
605 }
606 float3 center(0.0f);
608 [&](const int64_t point_i) { center += point_positions[point_i]; });
609 center /= selection.size();
610 mean_center_point_per_curve[curve_i] = center;
611 },
613 }
614
615 const Array<int> point_to_curve_map = curves.point_to_curve_map();
616 for (const int selection_i : position_offsets_in_td.index_range()) {
617 if (position_offsets_in_td[selection_i].is_empty()) {
618 continue;
619 }
620 MutableSpan<TransData> tc_data = all_tc_data.slice(position_offsets_in_td[selection_i]);
621 MutableSpan<float3> positions = positions_per_selection_attr[selection_i];
622 const IndexMask points_to_transform = points_to_transform_per_attr[selection_i];
623 const VArray<bool> selection = selection_attrs[selection_i];
624
625 points_to_transform.foreach_index(
626 [&](const int64_t domain_i, const int64_t transform_i) {
627 const int curve_i = point_to_curve_map[domain_i];
628
629 TransData &td = tc_data[transform_i];
630 float3 *elem = &positions[transform_i];
631
632 float3 center;
633 const bool use_local_center = hide_handles || use_individual_origin ||
634 point_selection[domain_i];
635 const bool use_mean_center = use_individual_origin &&
636 !(curve_types[curve_i] == CURVE_TYPE_BEZIER);
637 if (use_mean_center) {
638 center = mean_center_point_per_curve[curve_i];
639 }
640 else if (use_local_center) {
641 center = point_positions[domain_i];
642 }
643 else {
644 center = *elem;
645 }
646
647 copy_v3_v3(td.iloc, *elem);
648 copy_v3_v3(td.center, center);
649 td.loc = *elem;
650
651 td.flag = 0;
652 if (selection[domain_i]) {
653 td.flag = TD_SELECTED;
654 }
655
656 td.extra = extra;
657
658 if (value_attribute) {
659 float *value = &((*value_attribute)[domain_i]);
660 td.val = value;
661 td.ival = *value;
662 }
663 else {
664 td.val = nullptr;
665 }
666
667 if (deformation.deform_mats.is_empty()) {
668 copy_m3_m3(td.smtx, smtx_base.ptr());
669 copy_m3_m3(td.mtx, mtx_base.ptr());
670 }
671 else {
672 const float3x3 mtx = deformation.deform_mats[domain_i] * mtx_base;
673 const float3x3 smtx = math::pseudo_invert(mtx);
674 copy_m3_m3(td.smtx, smtx.ptr());
675 copy_m3_m3(td.mtx, mtx.ptr());
676 }
677 },
679 }
680 if (points_to_transform_per_attr.size() > 1 && points_to_transform_per_attr.first().is_empty()) {
681 auto update_handle_center = [&](const int handle_selection_attr,
682 const int opposite_handle_selection_attr) {
683 const IndexMask &handles_to_transform = points_to_transform_per_attr[handle_selection_attr];
684 const IndexMask &opposite_handles_to_transform =
685 points_to_transform_per_attr[opposite_handle_selection_attr];
686
687 if (handles_to_transform.size() == 1 && opposite_handles_to_transform.size() <= 1) {
688 MutableSpan<TransData> tc_data = all_tc_data.slice(
689 position_offsets_in_td[handle_selection_attr]);
690 copy_v3_v3(tc_data[0].center, point_positions[handles_to_transform.first()]);
691 }
692 };
693 update_handle_center(1, 2);
694 update_handle_center(2, 1);
695 }
696
697 if (use_connected_only) {
698 Array<int> curves_offsets_in_td_buffer(curves.curves_num() + 1, 0);
699 affected_curves.foreach_index(
700 [&](const int64_t curve) {
701 curves_offsets_in_td_buffer[curve] =
702 points_to_transform_per_attr[0].slice_content(points_by_curve[curve]).size();
703 },
705 offset_indices::accumulate_counts_to_offsets(curves_offsets_in_td_buffer);
706 const OffsetIndices<int> curves_offsets_in_td(curves_offsets_in_td_buffer);
707
708 Array<int> bezier_offsets_in_td(curves.curves_num() + 1, 0);
709 offset_indices::copy_group_sizes(points_by_curve, bezier_curves, bezier_offsets_in_td);
711
712 affected_curves.foreach_segment(
713 [&](const IndexMaskSegment segment) {
714 Array<int> map;
715 Array<float> closest_distances;
716 Array<float3> mapped_curve_positions;
717
718 for (const int curve_i : segment) {
719 const int selection_attrs_num = curve_types[curve_i] == CURVE_TYPE_BEZIER ? 3 : 1;
720 const IndexRange curve_points = points_by_curve[curve_i];
721 const IndexRange editable_curve_points = curves_offsets_in_td[curve_i];
722 const int total_curve_points = selection_attrs_num * editable_curve_points.size();
723 map.reinitialize(total_curve_points);
724 closest_distances.reinitialize(total_curve_points);
725 closest_distances.fill(std::numeric_limits<float>::max());
726 mapped_curve_positions.reinitialize(total_curve_points);
727
728 fill_map(CurveType(curve_types[curve_i]),
729 editable_curve_points,
730 position_offsets_in_td,
731 bezier_offsets_in_td[curve_i],
732 map);
733
734 bool has_any_selected = false;
735 for (const int selection_attr_i : IndexRange(selection_attrs_num)) {
736 has_any_selected = has_any_selected ||
738 selection_attrs[selection_attr_i], curve_points);
739 }
740 if (!has_any_selected) {
741 for (const int i : map) {
742 TransData &td = all_tc_data[i];
743 td.flag |= TD_SKIP;
744 }
745 continue;
746 }
747
748 for (const int i : closest_distances.index_range()) {
749 TransData &td = all_tc_data[map[i]];
750 mapped_curve_positions[i] = td.loc;
751 if (td.flag & TD_SELECTED) {
752 closest_distances[i] = 0.0f;
753 }
754 }
755
756 if (cyclic[curve_i]) {
757 cyclic_curve_connected_point_distances(mapped_curve_positions.as_span(),
758 closest_distances.as_mutable_span());
759 }
760 else {
761 curve_connected_point_distances(mapped_curve_positions.as_span(),
762 closest_distances.as_mutable_span());
763 }
764
765 for (const int i : closest_distances.index_range()) {
766 TransData &td = all_tc_data[map[i]];
767 td.dist = closest_distances[i];
768 }
769 }
770 },
772 }
773}
774
776
778 /*flags*/ (T_EDIT | T_POINTS),
779 /*create_trans_data*/ createTransCurvesVerts,
780 /*recalc_data*/ recalcData_curves,
781 /*special_aftertrans_update*/ nullptr,
782};
783
784} // namespace blender::ed::transform::curves
Low-level operations for curves.
Low-level operations for curves.
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define ELEM(...)
int64_t size() const
Definition BLI_array.hh:267
Span< T > as_span() const
Definition BLI_array.hh:254
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:259
IndexRange index_range() const
Definition BLI_array.hh:371
void fill(const T &value) const
Definition BLI_array.hh:283
void reinitialize(const int64_t new_size)
Definition BLI_array.hh:430
static IndexMask from_predicate(const IndexMask &universe, LinearAllocator<> &memory, Fn &&predicate, exec_mode::Mode mode=exec_mode::parallel)
static IndexMask from_bools(Span< bool > bools, LinearAllocator<> &memory)
static IndexMask from_difference(const IndexMask &mask_a, const IndexMask &mask_b, LinearAllocator<> &memory)
static IndexMask from_union(const IndexMask &mask_a, const IndexMask &mask_b, LinearAllocator<> &memory)
static IndexMask from_intersection(const IndexMask &mask_a, const IndexMask &mask_b, LinearAllocator<> &memory)
constexpr int64_t size() const
constexpr int64_t start() const
constexpr IndexRange index_range() const
void priority_increased(const int64_t index)
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr MutableSpan slice(const int64_t start, const int64_t size) const
Definition BLI_span.hh:579
constexpr IndexRange index_range() const
Definition BLI_span.hh:676
constexpr const T & first() const
Definition BLI_span.hh:319
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
std::optional< T > get_if_single() const
static VArray from_single(T value, const int64_t size)
int64_t size() const
void append(const T &value)
const T & last(const int64_t n=0) const
IndexRange index_range() const
Span< T > as_span() const
GAttributeReader lookup_or_default(StringRef name, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
GSpanAttributeWriter lookup_or_add_for_write_span(StringRef name, AttrDomain domain, AttrType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
const IntersectionExpr & intersect(const Span< Term > terms)
const UnionExpr & merge(const Span< Term > terms)
const DifferenceExpr & subtract(const Term &main_term, const Span< Term > subtract_terms)
void foreach_index_optimized(Fn &&fn, Mode mode=exec_mode::serial) const
IndexMask slice_content(IndexRange range) const
void foreach_segment(Fn &&fn, Mode mode=exec_mode::serial) const
bool contains(int64_t query_index) const
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
#define layer
void gather(const GVArray &src, const IndexMask &indices, GMutableSpan dst, exec_mode::Mode mode=exec_mode::parallel)
void scatter(const Span< T > src, const Span< IndexT > indices, MutableSpan< T > dst, const Mode mode={})
void fill_index_range(MutableSpan< T > span, const T start=0)
GeometryDeformation get_evaluated_curves_deformation(const Object *ob_eval, const Object &ob_orig)
void calculate_single_aligned_handles(const IndexMask &selection, Span< float3 > positions, Span< float3 > align_with, MutableSpan< float3 > align_handles)
IndexMask indices_for_type(const VArray< int8_t > &types, const std::array< int, CURVE_TYPES_NUM > &type_counts, const CurveType type, const IndexMask &selection, LinearAllocator<> &memory)
IndexMask curve_to_point_selection(OffsetIndices< int > points_by_curve, const IndexMask &curve_selection, LinearAllocator<> &memory)
bool has_anything_selected(const VArray< bool > &varray, const IndexRange range_to_check)
IndexMask retrieve_selected_points(const bke::CurvesGeometry &curves, LinearAllocator<> &memory)
Vector< MutableSpan< float3 > > get_curves_positions_for_write(bke::CurvesGeometry &curves)
Span< StringRef > get_curves_selection_attribute_names(const bke::CurvesGeometry &curves)
static bool update_auto_handle_types(bke::CurvesGeometry &curves, const IndexMask &auto_handles, const IndexMask &auto_handles_opposite, const IndexMask &selected_handles, const IndexMask &selected_handles_opposite, const StringRef handle_type_name, IndexMaskMemory &memory)
void create_aligned_handles_masks(const bke::CurvesGeometry &curves, Span< IndexMask > points_to_transform_per_attr, int curve_index, TransCustomData &custom_data)
static void recalcData_curves(TransInfo *t)
static MutableSpan< float3 > append_positions_to_custom_data(const IndexMask selection, std::optional< Span< float3 > > positions, TransCustomData &custom_data)
static OffsetIndices< int > recent_position_offsets(TransCustomData &custom_data, int num)
CurvesTransformData * create_curves_transform_custom_data(TransCustomData &custom_data)
void calculate_single_aligned_handles(const TransCustomData &custom_data, bke::CurvesGeometry &curves, int curve_index)
bool update_handle_types_for_transform(eTfmMode mode, const std::array< IndexMask, 3 > &selection_per_attribute, const IndexMask &bezier_points, bke::CurvesGeometry &curves)
static bool update_vector_handle_types(bke::CurvesGeometry &curves, const IndexMask &selected_handles, const StringRef handle_type_name)
static void cyclic_curve_connected_point_distances(const Span< float3 > positions, MutableSpan< float > r_distances)
static void fill_map(const CurveType curve_type, const IndexRange curve_points, const OffsetIndices< int > position_offsets_in_td, const int handles_offset, MutableSpan< int > map)
void curve_populate_trans_data_structs(const TransInfo &t, TransDataContainer &tc, bke::CurvesGeometry &curves, const float4x4 &transform, const bke::crazyspace::GeometryDeformation &deformation, std::optional< MutableSpan< float > > value_attribute, Span< IndexMask > points_to_transform_per_attr, const IndexMask &affected_curves, bool use_connected_only, const IndexMask &bezier_curves, void *extra=nullptr)
static void curve_connected_point_distances(const Span< float3 > positions, MutableSpan< float > r_distances)
static IndexMask handles_by_type(const IndexMask &handles, const VArray< int8_t > &types, const HandleType type, IndexMaskMemory &memory)
void copy_positions_from_curves_transform_custom_data(const TransCustomData &custom_data, int layer, MutableSpan< float3 > positions_dst)
static void createTransCurvesVerts(bContext *C, TransInfo *t)
void transform_snap_project_individual_apply(TransInfo *t)
constexpr ParallelGrainSize grain_size(int grain_size)
void masked_fill(MutableSpan< T > data, const T &value, const IndexMask &mask)
T distance(const T &a, const T &b)
MatBase< T, Size, Size > pseudo_invert(const MatBase< T, Size, Size > &mat, T epsilon=1e-8)
T mod_periodic(const T &a, const T &b)
void copy_group_sizes(OffsetIndices< int > offsets, const IndexMask &mask, MutableSpan< int > sizes)
OffsetIndices< int > accumulate_counts_to_offsets(MutableSpan< int > counts_to_offsets, int start_offset=0)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
MINLINE void copy_v3_v3(float r[3], const float a[3])
void copy_m3_m3(float m1[3][3], const float m2[3][3])
@ V3D_AROUND_LOCAL_ORIGINS
MatBase< float, 4, 4 > float4x4
void DEG_id_tag_update(ID *id, unsigned int flags)
BMesh const char void * data
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
ATTR_WARN_UNUSED_RESULT const size_t num
MatBase< float, 3, 3 > float3x3
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
VecBase< float, 3 > float3
signed char int8_t
Definition BKE_text.h:76
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
const c_style_mat & ptr() const
eHandleDisplay handle_display
View3DOverlay overlay
void(*) free_cb(TransInfo *, TransDataContainer *tc, TransCustomData *custom_data)
Definition transform.hh:655
TransDataContainer * data_container
Definition transform.hh:828
#define T_PROP_EDIT_ALL
Definition transform.hh:34
conversion and adaptation of different datablocks to a common struct.