Blender V5.3
curves_pen.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9
10#include "BKE_attribute.hh"
11#include "BKE_context.hh"
12#include "BKE_curves.hh"
13#include "BKE_curves_utils.hh"
14#include "BKE_deform.hh"
15#include "BKE_grease_pencil.hh"
16#include "BKE_material.hh"
17#include "BKE_report.hh"
18
19#include "BLI_array_utils.hh"
20
21#include "BLT_translation.hh"
22
23#include "WM_api.hh"
24#include "WM_types.hh"
25
26#include "RNA_access.hh"
27#include "RNA_define.hh"
28#include "RNA_enum_types.hh"
29
30#include "DEG_depsgraph.hh"
31
32#include "DNA_material_types.h"
33
34#include "ED_curves.hh"
35#include "ED_screen.hh"
36#include "ED_view3d.hh"
37
38#include "UI_resources.hh"
39
40namespace blender::ed::curves {
41
42namespace pen_tool {
43
44enum class PenModal : int8_t {
45 /* Move the handles of the adjacent control point. */
47 /* Move the entire point even if only the handles are selected. */
49 /* Snap the handles to multiples of 45 degrees. */
51};
52
54 {BEZIER_HANDLE_AUTO, "AUTO", 0, "Auto", ""},
55 {BEZIER_HANDLE_VECTOR, "VECTOR", 0, "Vector", ""},
56 {0, nullptr, 0, nullptr, nullptr},
57};
58
59/* Used to scale the default select distance. */
60constexpr float selection_distance_factor = 0.3f;
61constexpr float selection_distance_factor_edge = 0.2f;
62
63/* Used when creating a single curve from nothing. */
64constexpr float default_handle_px_distance = 16.0f;
65
66/* Total number of curve handle types. */
67constexpr int CURVE_HANDLE_TYPES_NUM = 4;
68
69/* Edges are prioritized less than all other types. */
70constexpr float selection_edge_priority_factor = 0.1f;
71/* Points will overwrite edges to allow control point to be selected easier. */
73
77
78bool ClosestElement::is_closer(const float new_distance_squared,
79 const ElementMode new_element_mode,
80 const float threshold_distance) const
81{
82 const float threshold_distance_sq = threshold_distance * threshold_distance;
83
84 if (new_distance_squared > threshold_distance_sq) {
85 return false;
86 }
87
88 float old_priority = 1.0f;
89 float new_priority = 1.0f;
90
91 if (this->element_mode == ElementMode::Edge) {
92 if (new_element_mode != ElementMode::Edge) {
93 old_priority = selection_edge_priority_factor;
94
95 /* Overwrite edges with points if the point is within the overwrite distance. */
96 if (new_distance_squared <
98 {
99 return true;
100 }
101 }
102 }
103 else {
104 if (new_element_mode == ElementMode::Edge) {
105 new_priority = selection_edge_priority_factor;
106
107 /* Overwrite edges with points if the point is within the overwrite distance. */
108 if (this->distance_squared <
110 {
111 return false;
112 }
113 }
114 }
115
116 if (new_distance_squared * old_priority < this->distance_squared * new_priority) {
117 return true;
118 }
119
120 return false;
121}
122
123/* Will check if the point is closer than the existing element. */
126 const IndexMask &editable_curves,
127 const float4x4 &layer_to_object,
128 const int drawing_index,
129 const float2 &mouse_co,
130 ClosestElement &r_closest_element)
131{
132 const Span<float3> positions = curves.positions();
133 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
134
135 editable_curves.foreach_index([&](const int curve_i) {
136 const IndexRange points = points_by_curve[curve_i];
137 for (const int point_i : points) {
138 const float2 pos_proj = ptd.layer_to_screen(layer_to_object, positions[point_i]);
139 const float distance_squared = math::distance_squared(pos_proj, mouse_co);
140
141 /* Save the closest point. */
142 if (r_closest_element.is_closer(
144 {
145 r_closest_element.curve_index = curve_i;
146 r_closest_element.point_index = point_i;
147 r_closest_element.element_mode = ElementMode::Point;
148 r_closest_element.distance_squared = distance_squared;
149 r_closest_element.drawing_index = drawing_index;
150 }
151 }
152 });
153}
154
155/* Will check if the handle is closer than the existing element. */
158 const IndexMask &bezier_points,
159 const float4x4 &layer_to_object,
160 const int drawing_index,
161 const float2 &mouse_co,
162 ClosestElement &r_closest_element)
163{
164 const Array<int> point_to_curve_map = curves.point_to_curve_map();
165
166 if (const std::optional<Span<float3>> handle_left = curves.handle_positions_left()) {
167 const Span<float3> handle_left_span = *handle_left;
168 bezier_points.foreach_index([&](const int point_i) {
169 const float2 pos_proj = ptd.layer_to_screen(layer_to_object, handle_left_span[point_i]);
170 const float distance_squared = math::distance_squared(pos_proj, mouse_co);
171
172 /* Save the closest point. */
173 if (r_closest_element.is_closer(
175 {
176 r_closest_element.curve_index = point_to_curve_map[point_i];
177 r_closest_element.point_index = point_i;
178 r_closest_element.element_mode = ElementMode::HandleLeft;
179 r_closest_element.distance_squared = distance_squared;
180 r_closest_element.drawing_index = drawing_index;
181 }
182 });
183 }
184
185 if (const std::optional<Span<float3>> handle_right = curves.handle_positions_right()) {
186 const Span<float3> handle_right_span = *handle_right;
187 bezier_points.foreach_index([&](const int point_i) {
188 const float2 pos_proj = ptd.layer_to_screen(layer_to_object, handle_right_span[point_i]);
189 const float distance_squared = math::distance_squared(pos_proj, mouse_co);
190
191 /* Save the closest point. */
192 if (r_closest_element.is_closer(
194 {
195 r_closest_element.curve_index = point_to_curve_map[point_i];
196 r_closest_element.point_index = point_i;
197 r_closest_element.element_mode = ElementMode::HandleRight;
198 r_closest_element.distance_squared = distance_squared;
199 r_closest_element.drawing_index = drawing_index;
200 }
201 });
202 }
203}
204
206 const float2 &pos_2,
207 const float2 &pos,
208 float &r_local_t)
209{
210 const float2 dif_m = pos - pos_1;
211 const float2 dif_l = pos_2 - pos_1;
212 const float d = math::dot(dif_m, dif_l);
213 const float l2 = math::dot(dif_l, dif_l);
214 const float t = math::clamp(d / l2, 0.0f, 1.0f);
215 r_local_t = t;
216 return dif_l * t + pos_1;
217}
218
219/* Will check if the edge point is closer than the existing element. */
222 const IndexMask &editable_curves,
223 const float4x4 &layer_to_object,
224 const int drawing_index,
225 const float2 &mouse_co,
226 ClosestElement &r_closest_element)
227{
228 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
229 const OffsetIndices<int> evaluated_points_by_curve = curves.evaluated_points_by_curve();
230 const Span<float3> positions = curves.positions();
231 const Span<float3> evaluated_positions = curves.evaluated_positions();
232 const VArray<bool> cyclic = curves.cyclic();
233 const VArray<int8_t> types = curves.curve_types();
234
235 editable_curves.foreach_index([&](const int curve_i) {
236 const IndexRange src_points = points_by_curve[curve_i];
237 const IndexRange eval_points = evaluated_points_by_curve[curve_i];
238
239 for (const int src_i : src_points.index_range().drop_back(cyclic[curve_i] ? 0 : 1)) {
240 if (types[curve_i] != CURVE_TYPE_BEZIER) {
241 const int src_i_1 = src_i + src_points.first();
242 const int src_i_2 = (src_i + 1) % src_points.size() + src_points.first();
243 const float2 pos_1_proj = ptd.layer_to_screen(layer_to_object, positions[src_i_1]);
244 const float2 pos_2_proj = ptd.layer_to_screen(layer_to_object, positions[src_i_2]);
245 float local_t;
246 const float2 closest_pos = line_segment_closest_point(
247 pos_1_proj, pos_2_proj, mouse_co, local_t);
248
249 const float distance_squared = math::distance_squared(closest_pos, mouse_co);
250 const float t = local_t;
251
252 /* Save the closest point. */
253 if (r_closest_element.is_closer(
255 {
256 r_closest_element.point_index = src_points.first() + src_i;
257 r_closest_element.edge_t = t;
258 r_closest_element.element_mode = ElementMode::Edge;
259 r_closest_element.curve_index = curve_i;
260 r_closest_element.distance_squared = distance_squared;
261 r_closest_element.drawing_index = drawing_index;
262 }
263 }
264 else {
265 const Span<int> offsets = curves.bezier_evaluated_offsets_for_curve(curve_i);
266 const IndexRange eval_range = IndexRange::from_begin_end_inclusive(offsets[src_i],
267 offsets[src_i + 1])
268 .shift(eval_points.first());
269 const int point_num = eval_range.size() - 1;
270
271 for (const int eval_i : IndexRange(point_num)) {
272 const int eval_point_i_1 = eval_range.first() + eval_i;
273 const int eval_point_i_2 = (eval_range.first() + eval_i + 1 - eval_points.first()) %
274 eval_points.size() +
275 eval_points.first();
276 const float2 pos_1_proj = ptd.layer_to_screen(layer_to_object,
277 evaluated_positions[eval_point_i_1]);
278 const float2 pos_2_proj = ptd.layer_to_screen(layer_to_object,
279 evaluated_positions[eval_point_i_2]);
280 float local_t;
281 const float2 closest_pos = line_segment_closest_point(
282 pos_1_proj, pos_2_proj, mouse_co, local_t);
283
284 const float distance_squared = math::distance_squared(closest_pos, mouse_co);
285 const float t = (eval_i + local_t) / float(point_num);
286
287 /* Save the closest point. */
288 if (r_closest_element.is_closer(
290 {
291 r_closest_element.point_index = src_points.first() + src_i;
292 r_closest_element.element_mode = ElementMode::Edge;
293 r_closest_element.edge_t = t;
294 r_closest_element.curve_index = curve_i;
295 r_closest_element.distance_squared = distance_squared;
296 r_closest_element.drawing_index = drawing_index;
297 }
298 }
299 }
300 }
301 });
302}
303
305{
306 ClosestElement closest_element;
307 closest_element.element_mode = ElementMode::None;
308
309 for (const int curves_index : ptd.curves_range()) {
310 const bke::CurvesGeometry &curves = ptd.get_curves(curves_index);
311 const float4x4 layer_to_object = ptd.layer_to_object_per_curves[curves_index];
312
313 if (curves.is_empty()) {
314 continue;
315 }
316
317 IndexMaskMemory memory;
318 const IndexMask bezier_points = ptd.visible_bezier_handle_points(curves_index, memory);
319 const IndexMask editable_curves = ptd.editable_curves(curves_index, memory);
320
322 ptd, curves, editable_curves, layer_to_object, curves_index, mouse_co, closest_element);
324 ptd, curves, bezier_points, layer_to_object, curves_index, mouse_co, closest_element);
326 ptd, curves, editable_curves, layer_to_object, curves_index, mouse_co, closest_element);
327 }
328 return closest_element;
329}
330
332{
333 WorkspaceStatus status(C);
334 status.opmodal(IFACE_("Snap Angle"), op->type, int(PenModal::SnapAngle));
335 status.opmodal(IFACE_("Move Current Handle"), op->type, int(PenModal::MoveHandle));
336 status.opmodal(IFACE_("Move Entire Point"), op->type, int(PenModal::MoveEntire));
337}
338
339/* Snaps to the closest diagonal, horizontal or vertical. */
341{
342 using namespace math;
343 const float sin225 = sin(AngleRadian::from_degree(22.5f));
344 return sign(p) * length(p) * normalize(sign(normalize(abs(p)) - sin225) + 1.0f);
345}
346
347static void move_segment(const PenToolOperation &ptd,
349 const float4x4 &layer_to_world)
350{
351 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
352 MutableSpan<float3> positions = curves.positions_for_write();
353 MutableSpan<int8_t> handle_types_left = curves.handle_types_left_for_write();
354 MutableSpan<int8_t> handle_types_right = curves.handle_types_right_for_write();
355 MutableSpan<float3> handles_left = curves.handle_positions_left_for_write();
356 MutableSpan<float3> handles_right = curves.handle_positions_right_for_write();
357
358 const int curve_i = ptd.closest_element.curve_index;
359 const IndexRange points = points_by_curve[curve_i];
360 const int point_i1 = ptd.closest_element.point_index;
361 const int point_i2 = (ptd.closest_element.point_index + 1 - points.first()) % points.size() +
362 points.first();
363
364 const float3 depth_point = positions[point_i1];
365 const float3 Pm = ptd.screen_to_layer(layer_to_world, ptd.mouse_co, depth_point);
366 const float3 P0 = positions[point_i1];
367 const float3 P3 = positions[point_i2];
368 const float3 p1 = handles_right[point_i1];
369 const float3 p2 = handles_left[point_i2];
370 const float3 k2 = p1 - p2;
371
372 const float t = ptd.closest_element.edge_t;
373 const float t_sq = t * t;
374 const float t_cu = t_sq * t;
375 const float one_minus_t = 1.0f - t;
376 const float one_minus_t_sq = one_minus_t * one_minus_t;
377 const float one_minus_t_cu = one_minus_t_sq * one_minus_t;
378
398
399 const float denom = 3.0f * one_minus_t * t;
400 if (denom == 0.0f) {
401 return;
402 }
403
404 const float3 P1 = (Pm - one_minus_t_cu * P0 - t_cu * P3) / denom + k2 * t;
405 const float3 P2 = P1 - k2;
406
407 handles_right[point_i1] = P1;
408 handles_left[point_i2] = P2;
409 handle_types_right[point_i1] = BEZIER_HANDLE_FREE;
410 handle_types_left[point_i2] = BEZIER_HANDLE_FREE;
411
412 /* Only change `Align`, Keep `Vector` and `Auto` the same. */
413 if (handle_types_left[point_i1] == BEZIER_HANDLE_ALIGN) {
414 handle_types_left[point_i1] = BEZIER_HANDLE_FREE;
415 }
416 if (handle_types_right[point_i2] == BEZIER_HANDLE_ALIGN) {
417 handle_types_right[point_i2] = BEZIER_HANDLE_FREE;
418 }
419
420 curves.calculate_bezier_auto_handles();
421}
422
425 const IndexMask &selection,
426 const float4x4 &layer_to_world,
427 const float4x4 &layer_to_object)
428{
429 if (selection.is_empty()) {
430 return false;
431 }
432
433 MutableSpan<float3> positions = curves.positions_for_write();
434 const bke::AttributeAccessor attributes = curves.attributes();
435
436 MutableSpan<int8_t> handle_types_left = curves.handle_types_left_for_write();
437 MutableSpan<int8_t> handle_types_right = curves.handle_types_right_for_write();
438 MutableSpan<float3> handles_left = curves.handle_positions_left_for_write();
439 MutableSpan<float3> handles_right = curves.handle_positions_right_for_write();
440
441 const VArray<bool> left_selected = *attributes.lookup_or_default<bool>(
442 ".selection_handle_left", bke::AttrDomain::Point, true);
443 const VArray<bool> right_selected = *attributes.lookup_or_default<bool>(
444 ".selection_handle_right", bke::AttrDomain::Point, true);
445
446 selection.foreach_index(
447 [&](const int64_t point_i) {
448 const float3 depth_point = positions[point_i];
449 float2 offset = ptd.xy - ptd.prev_xy;
450
451 if ((ptd.move_point && !ptd.point_added &&
452 !(left_selected[point_i] || right_selected[point_i])) ||
453 ptd.move_entire)
454 {
455 const float2 pos = ptd.layer_to_screen(layer_to_object, positions[point_i]);
456 const float2 pos_left = ptd.layer_to_screen(layer_to_object, handles_left[point_i]);
457 const float2 pos_right = ptd.layer_to_screen(layer_to_object, handles_right[point_i]);
458 positions[point_i] = ptd.screen_to_layer(layer_to_world, pos + offset, depth_point);
459 handles_left[point_i] = ptd.screen_to_layer(
460 layer_to_world, pos_left + offset, depth_point);
461 handles_right[point_i] = ptd.screen_to_layer(
462 layer_to_world, pos_right + offset, depth_point);
463 return;
464 }
465
466 const bool is_left = !right_selected[point_i];
467 if (ptd.move_handle) {
468 if (is_left) {
469 const float2 pos_left = ptd.layer_to_screen(layer_to_object, handles_left[point_i]);
470 handles_left[point_i] = ptd.screen_to_layer(
471 layer_to_world, pos_left + offset, depth_point);
472 }
473 else {
474 const float2 pos_right = ptd.layer_to_screen(layer_to_object, handles_right[point_i]);
475 handles_right[point_i] = ptd.screen_to_layer(
476 layer_to_world, pos_right + offset, depth_point);
477 }
478 handle_types_left[point_i] = BEZIER_HANDLE_FREE;
479 handle_types_right[point_i] = BEZIER_HANDLE_FREE;
480 return;
481 }
482
483 const float2 center_point = ptd.layer_to_screen(layer_to_object, depth_point);
484 offset = ptd.mouse_co - ptd.center_of_mass_co;
485
486 if (ptd.snap_angle) {
487 offset = snap_8_angles(offset);
488 }
489
490 /* Set both handles to be `Aligned` if this point is newly added or is
491 * no longer control freely. */
492 if (ptd.point_added || ptd.handle_moved) {
493 handle_types_left[point_i] = BEZIER_HANDLE_ALIGN;
494 handle_types_right[point_i] = BEZIER_HANDLE_ALIGN;
495 }
496
497 if (is_left) {
498 if (handle_types_right[point_i] == BEZIER_HANDLE_AUTO) {
499 handle_types_right[point_i] = BEZIER_HANDLE_ALIGN;
500 }
501 handle_types_left[point_i] = handle_types_right[point_i];
502 if (handle_types_right[point_i] == BEZIER_HANDLE_VECTOR) {
503 handle_types_left[point_i] = BEZIER_HANDLE_FREE;
504 }
505
506 if (ptd.point_added) {
507 handles_left[point_i] = ptd.project(center_point + offset);
508 }
509 else {
510 handles_left[point_i] = ptd.screen_to_layer(
511 layer_to_world, center_point + offset, depth_point);
512 }
513
514 if (handle_types_right[point_i] == BEZIER_HANDLE_ALIGN) {
515 handles_right[point_i] = 2.0f * depth_point - handles_left[point_i];
516 }
517 }
518 else {
519 if (handle_types_left[point_i] == BEZIER_HANDLE_AUTO) {
520 handle_types_left[point_i] = BEZIER_HANDLE_ALIGN;
521 }
522 handle_types_right[point_i] = handle_types_left[point_i];
523 if (handle_types_left[point_i] == BEZIER_HANDLE_VECTOR) {
524 handle_types_right[point_i] = BEZIER_HANDLE_FREE;
525 }
526
527 if (ptd.point_added) {
528 handles_right[point_i] = ptd.project(center_point + offset);
529 }
530 else {
531 handles_right[point_i] = ptd.screen_to_layer(
532 layer_to_world, center_point + offset, depth_point);
533 }
534
535 if (handle_types_left[point_i] == BEZIER_HANDLE_ALIGN) {
536 handles_left[point_i] = 2.0f * depth_point - handles_right[point_i];
537 }
538 }
539 },
541
542 curves.calculate_bezier_auto_handles();
543
544 return true;
545}
546
547static std::optional<bke::CurvesGeometry> extrude_curves(const PenToolOperation &ptd,
548 const bke::CurvesGeometry &src,
549 const float4x4 &layer_to_object,
550 const IndexMask editable_curves)
551{
552 const bke::AttributeAccessor src_attributes = src.attributes();
553 const OffsetIndices<int> points_by_curve = src.points_by_curve();
554 const VArray<bool> &src_cyclic = src.cyclic();
555 const VArray<int8_t> types = src.curve_types();
556 const int old_points_num = src.points_num();
557
558 const VArray<bool> point_selection = *src_attributes.lookup_or_default<bool>(
559 ".selection", bke::AttrDomain::Point, true);
560 const VArray<bool> left_selected = *src_attributes.lookup_or_default<bool>(
561 ".selection_handle_left", bke::AttrDomain::Point, true);
562 const VArray<bool> right_selected = *src_attributes.lookup_or_default<bool>(
563 ".selection_handle_right", bke::AttrDomain::Point, true);
564
565 Vector<int> dst_to_src_points(old_points_num);
566 array_utils::fill_index_range(dst_to_src_points.as_mutable_span());
567
568 Vector<bool> dst_selected_start(old_points_num, false);
569 Vector<bool> dst_selected_center(old_points_num, false);
570 Vector<bool> dst_selected_end(old_points_num, false);
571
572 Array<int> dst_curve_counts(src.curves_num());
574 points_by_curve, src.curves_range(), dst_curve_counts.as_mutable_span());
575
576 /* Point offset keeps track of the points inserted. */
577 int point_offset = 0;
578 editable_curves.foreach_index([&](const int curve_index) {
579 const IndexRange curve_points = points_by_curve[curve_index];
580 /* Skip cyclic curves unless they only have one point. */
581 if (src_cyclic[curve_index] && curve_points.size() != 1) {
582 return;
583 }
584 const bool is_bezier = types[curve_index] == CURVE_TYPE_BEZIER;
585
586 bool first_selected = point_selection[curve_points.first()];
587 if (is_bezier) {
588 first_selected |= left_selected[curve_points.first()];
589 first_selected |= right_selected[curve_points.first()];
590 }
591
592 bool last_selected = point_selection[curve_points.last()];
593 if (is_bezier) {
594 last_selected |= left_selected[curve_points.last()];
595 last_selected |= right_selected[curve_points.last()];
596 }
597
598 if (first_selected) {
599 if (curve_points.size() != 1) {
600 /* Start-point extruded, we insert a new point at the beginning of the curve. */
601 dst_to_src_points.insert(curve_points.first() + point_offset, curve_points.first());
602 dst_selected_start.insert(curve_points.first() + point_offset, true);
603 dst_selected_center.insert(curve_points.first() + point_offset, !is_bezier);
604 dst_selected_end.insert(curve_points.first() + point_offset, false);
605 dst_curve_counts[curve_index]++;
606 point_offset++;
607 }
608 }
609
610 if (last_selected) {
611 /* End-point extruded, we insert a new point at the end of the curve. */
612 dst_to_src_points.insert(curve_points.last() + point_offset + 1, curve_points.last());
613 dst_selected_end.insert(curve_points.last() + point_offset + 1, true);
614 dst_selected_center.insert(curve_points.last() + point_offset + 1, !is_bezier);
615 dst_selected_start.insert(curve_points.last() + point_offset + 1, false);
616 dst_curve_counts[curve_index]++;
617 point_offset++;
618 }
619 });
620
621 if (point_offset == 0) {
622 return std::nullopt;
623 }
624
625 bke::CurvesGeometry dst(dst_to_src_points.size(), src.curves_num());
627
628 /* Setup curve offsets, based on the number of points in each curve. */
629 MutableSpan<int> new_curve_offsets = dst.offsets_for_write();
630 array_utils::copy(dst_curve_counts.as_span(), new_curve_offsets.drop_back(1));
632
634
635 /* Selection attribute. */
639 dst, bke::AttrDomain::Point, bke::AttrType::Bool, ".selection_handle_left");
641 dst, bke::AttrDomain::Point, bke::AttrType::Bool, ".selection_handle_right");
642 selection_left.span.copy_from(dst_selected_start.as_span());
643 selection.span.copy_from(dst_selected_center.as_span());
644 selection_right.span.copy_from(dst_selected_end.as_span());
645 selection_left.finish();
646 selection.finish();
647 selection_right.finish();
648
650 src_attributes, bke::AttrDomain::Curve, bke::AttrDomain::Curve, {}, dst_attributes);
651
652 bke::gather_attributes(src_attributes,
656 {".selection", ".selection_handle_left", ".selection_handle_right"}),
657 dst_to_src_points,
658 dst_attributes);
659
660 Span<float3> src_positions = src.positions();
661 MutableSpan<float3> dst_positions = dst.positions_for_write();
662 MutableSpan<bool> dst_cyclic = dst.cyclic_for_write();
663 const Array<int> dst_point_to_curve_map = dst.point_to_curve_map();
664 MutableSpan<int8_t> handle_types_left = dst.handle_types_left_for_write();
665 MutableSpan<int8_t> handle_types_right = dst.handle_types_right_for_write();
667 for (const int i : dst_to_src_points.index_range()) {
668 if (!(dst_selected_end[i] || dst_selected_start[i])) {
669 continue;
670 }
671 const float3 depth_point = src_positions[dst_to_src_points[i]];
672 const float2 pos = ptd.layer_to_screen(layer_to_object, depth_point) - ptd.center_of_mass_co +
673 ptd.mouse_co;
674 dst_positions[i] = ptd.project(pos);
675 handle_types_left[i] = ptd.extrude_handle;
676 handle_types_right[i] = ptd.extrude_handle;
677 radius[i] = ptd.radius;
678 dst_cyclic[dst_point_to_curve_map[i]] = false;
679 }
680
681 dst.update_curve_types();
683 if (src.nurbs_has_custom_knots()) {
684 IndexMaskMemory memory;
685 const VArray<int8_t> curve_types = src.curve_types();
686 const VArray<int8_t> knot_modes = dst.nurbs_knots_modes();
687 const OffsetIndices<int> dst_points_by_curve = dst.points_by_curve();
688 const IndexMask include_curves = IndexMask::from_predicate(
689 src.curves_range(), memory, [&](const int64_t curve_index) {
690 return curve_types[curve_index] == CURVE_TYPE_NURBS &&
691 knot_modes[curve_index] == NURBS_KNOT_MODE_CUSTOM &&
692 points_by_curve[curve_index].size() == dst_points_by_curve[curve_index].size();
693 });
695 include_curves.complement(dst.curves_range(), memory),
698 dst);
699 bke::curves::nurbs::gather_custom_knots(src, include_curves, 0, dst);
700 }
701 return dst;
702}
703
705{
706 const bke::AttributeAccessor src_attributes = src.attributes();
707 const OffsetIndices<int> points_by_curve = src.points_by_curve();
708 const VArray<int8_t> src_curve_types = src.curve_types();
709 const int old_points_num = src.points_num();
710 const int src_point_index = ptd.closest_element.point_index;
711 const int dst_point_index = src_point_index + 1;
712 const int curve_index = ptd.closest_element.curve_index;
713 const IndexRange points = points_by_curve[curve_index];
714 const int src_point_index_2 = (src_point_index + 1 - points.first()) % points.size() +
715 points.first();
716 const int dst_point_index_2 = (dst_point_index - points.first() + 1) % (points.size() + 1) +
717 points.first();
718
719 Vector<int> dst_to_src_points(old_points_num);
721
722 Array<int> dst_curve_counts(src.curves_num());
724 points_by_curve, src.curves_range(), dst_curve_counts.as_mutable_span());
725
726 dst_to_src_points.insert(src_point_index + 1, src_point_index);
727 dst_curve_counts[curve_index]++;
728
729 bke::CurvesGeometry dst(dst_to_src_points.size(), src.curves_num());
731
732 /* Setup curve offsets, based on the number of points in each curve. */
733 MutableSpan<int> new_curve_offsets = dst.offsets_for_write();
734 array_utils::copy(dst_curve_counts.as_span(), new_curve_offsets.drop_back(1));
736
738
739 /* Selection attribute. */
740 for (const StringRef selection_attribute_name :
742 {
744 dst, bke::AttrDomain::Point, bke::AttrType::Bool, selection_attribute_name);
745 ed::curves::fill_selection_false(selection_writer.span);
746 ed::curves::fill_selection_true(selection_writer.span,
747 IndexRange::from_single(dst_point_index));
748 selection_writer.finish();
749 }
750
752 src_attributes, bke::AttrDomain::Curve, bke::AttrDomain::Curve, {}, dst_attributes);
753 bke::gather_attributes(src_attributes,
757 {".selection", ".selection_handle_left", ".selection_handle_right"}),
758 dst_to_src_points,
759 dst_attributes);
760
761 const Span<float3> src_positions = src.positions();
762 MutableSpan<float3> dst_positions = dst.positions_for_write();
763
764 if (src_curve_types[curve_index] == CURVE_TYPE_BEZIER) {
765 MutableSpan<int8_t> handle_types_left = dst.handle_types_left_for_write();
766 MutableSpan<int8_t> handle_types_right = dst.handle_types_right_for_write();
767 const Span<float3> src_handles_left = *src.handle_positions_left();
768 const Span<float3> src_handles_right = *src.handle_positions_right();
771 handle_types_left[dst_point_index] = BEZIER_HANDLE_ALIGN;
772 handle_types_right[dst_point_index] = BEZIER_HANDLE_ALIGN;
773
775 src_positions[src_point_index],
776 src_handles_right[src_point_index],
777 src_handles_left[src_point_index_2],
778 src_positions[src_point_index_2],
780
781 dst_positions[dst_point_index] = inserted_point.position;
782 dst_handles_left[dst_point_index] = inserted_point.left_handle;
783 dst_handles_right[dst_point_index] = inserted_point.right_handle;
784 dst_handles_right[dst_point_index - 1] = inserted_point.handle_prev;
785 dst_handles_left[dst_point_index_2] = inserted_point.handle_next;
786 handle_types_right[dst_point_index - 1] = BEZIER_HANDLE_FREE;
787 handle_types_left[dst_point_index_2] = BEZIER_HANDLE_FREE;
788 }
789 else {
790 dst_positions[dst_point_index] = math::interpolate(src_positions[src_point_index],
791 src_positions[src_point_index_2],
793 }
794
795 dst.update_curve_types();
797 if (src.nurbs_has_custom_knots()) {
798 IndexMaskMemory memory;
799 const VArray<int8_t> curve_types = src.curve_types();
800 const VArray<int8_t> knot_modes = dst.nurbs_knots_modes();
801 const OffsetIndices<int> dst_points_by_curve = dst.points_by_curve();
802 const IndexMask include_curves = IndexMask::from_predicate(
803 src.curves_range(), memory, [&](const int64_t curve_index) {
804 return curve_types[curve_index] == CURVE_TYPE_NURBS &&
805 knot_modes[curve_index] == NURBS_KNOT_MODE_CUSTOM &&
806 points_by_curve[curve_index].size() == dst_points_by_curve[curve_index].size();
807 });
809 include_curves.complement(dst.curves_range(), memory),
812 dst);
813 bke::curves::nurbs::gather_custom_knots(src, include_curves, 0, dst);
814 }
815
816 src = std::move(dst);
817}
818
821 const float4x4 &layer_to_world)
822{
823 const float3 depth_point = ptd.project(ptd.mouse_co);
824
825 /* Add single curve to the end. */
826 const int num_old_points = curves.points_num();
827 curves.resize(curves.points_num() + 1, curves.curves_num() + 1);
828 curves.offsets_for_write().last(1) = num_old_points;
829
830 bke::MutableAttributeAccessor attributes = curves.attributes_for_write();
831
832 Set<std::string> curve_attributes_to_skip;
833
834 curves.positions_for_write().last() = depth_point;
835 curves.curve_types_for_write().last() = CURVE_TYPE_BEZIER;
836 curve_attributes_to_skip.add("curve_type");
837 curves.handle_types_left_for_write().last() = ptd.extrude_handle;
838 curves.handle_types_right_for_write().last() = ptd.extrude_handle;
839 curves.update_curve_types();
840 curves.resolution_for_write().last() = 12;
841 curve_attributes_to_skip.add("resolution");
842
843 const int material_index = ptd.vc.obact->actcol - 1;
844 if (material_index != -1) {
845 bke::SpanAttributeWriter<int> material_indexes = attributes.lookup_or_add_for_write_span<int>(
846 "material_index", bke::AttrDomain::Curve, bke::AttributeInitValue(0));
847 material_indexes.span.last() = material_index;
848 material_indexes.finish();
849 curve_attributes_to_skip.add("material_index");
850 }
851
852 MutableSpan<float3> handles_left = curves.handle_positions_left_for_write();
853 MutableSpan<float3> handles_right = curves.handle_positions_right_for_write();
854 handles_left.last() = ptd.screen_to_layer(
855 layer_to_world, ptd.mouse_co - float2(default_handle_px_distance / 2.0f, 0.0f), depth_point);
856 handles_right.last() = ptd.screen_to_layer(
857 layer_to_world, ptd.mouse_co + float2(default_handle_px_distance / 2.0f, 0.0f), depth_point);
858 curves.radius_for_write().last() = ptd.radius;
859
860 for (const StringRef selection_attribute_name :
862 {
864 curves, bke::AttrDomain::Point, bke::AttrType::Bool, selection_attribute_name);
865
867 IndexRange::from_single(curves.points_range().last()));
868 selection.finish();
869 }
870
871 /* Initialize the rest of the attributes with default values. */
873 attributes,
876 "radius",
877 "handle_left",
878 "handle_right",
879 "handle_type_left",
880 "handle_type_right",
881 ".selection",
882 ".selection_handle_left",
883 ".selection_handle_right"}),
884 curves.points_range().take_back(1));
887 bke::attribute_filter_from_skip_ref(curve_attributes_to_skip),
888 curves.curves_range().take_back(1));
889}
890
893 const IndexRange points,
894 const bool clear_selection)
895{
896 bool changed = false;
897
898 for (const StringRef selection_attribute_name :
900 {
902 curves, bke::AttrDomain::Point, bke::AttrType::Bool, selection_attribute_name);
903
904 const bool last_selected = ed::curves::has_anything_selected(
905 selection_writer.span.slice(IndexRange::from_single(points.last())));
906 const bool first_selected = ed::curves::has_anything_selected(
907 selection_writer.span.slice(IndexRange::from_single(points.first())));
908
909 /* Close the curve by selecting the other end point. */
910 if ((ptd.closest_element.point_index == points.first() && last_selected) ||
911 (ptd.closest_element.point_index == points.last() && first_selected))
912 {
913 curves.cyclic_for_write()[ptd.closest_element.curve_index] = true;
914 curves.calculate_bezier_auto_handles();
915 changed = true;
916 }
917
918 if (clear_selection) {
919 ed::curves::fill_selection_false(selection_writer.span);
920 }
921
922 if (ptd.select_point) {
923 if ((selection_attribute_name == ".selection" &&
925 (selection_attribute_name == ".selection_handle_left" &&
927 (selection_attribute_name == ".selection_handle_right" &&
929 {
930
931 ed::curves::fill_selection_true(selection_writer.span,
933 changed = true;
934 }
935 }
936
937 selection_writer.finish();
938 }
939
940 return changed;
941}
942
943static float2 calculate_center_of_mass(const PenToolOperation &ptd, const bool ends_only)
944{
945 float2 pos = float2(0.0f, 0.0f);
946 int num = 0;
947
948 for (const int curves_index : ptd.curves_range()) {
949 const bke::CurvesGeometry &curves = ptd.get_curves(curves_index);
950 const float4x4 &layer_to_object = ptd.layer_to_object_per_curves[curves_index];
951 const Span<float3> positions = curves.positions();
952 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
953 const Array<int> point_to_curve_map = curves.point_to_curve_map();
954 const VArray<bool> &cyclic = curves.cyclic();
955
956 IndexMaskMemory memory;
957 const IndexMask selection = ptd.all_selected_points(curves_index, memory);
958
959 selection.foreach_index([&](const int64_t point_i) {
960 if (ends_only) {
961 const int curve_i = point_to_curve_map[point_i];
962 const IndexRange points = points_by_curve[curve_i];
963
964 /* Skip cyclic curves unless they only have one point. */
965 if (cyclic[curve_i] && points.size() != 1) {
966 return;
967 }
968
969 if (point_i != points.first() && point_i != points.last()) {
970 return;
971 }
972 }
973 pos += ptd.layer_to_screen(layer_to_object, positions[point_i]);
974 num++;
975 });
976 }
977
978 if (num == 0) {
979 return pos;
980 }
981 return pos / num;
982}
983
984static void invoke_curves(PenToolOperation &ptd, bContext *C, wmOperator *op, const wmEvent *event)
985{
988
989 std::atomic<bool> add_single = ptd.extrude_point;
990 std::atomic<bool> changed = false;
991 std::atomic<bool> point_added = false;
992 std::atomic<bool> point_removed = false;
993
994 const bool extrude_only = !ptd.select_point && !ptd.move_seg && !ptd.move_point &&
995 ptd.extrude_point;
996
997 threading::parallel_for(ptd.curves_range(), 1, [&](const IndexRange curves_range) {
998 for (const int curves_index : curves_range) {
999 bke::CurvesGeometry &curves = ptd.get_curves(curves_index);
1000
1001 if (curves.is_empty()) {
1002 continue;
1003 }
1004
1005 if (ptd.closest_element.element_mode == ElementMode::Edge) {
1006 if (ptd.move_seg || ptd.insert_point) {
1007 add_single.store(false, std::memory_order_relaxed);
1008 if (ptd.insert_point && ptd.closest_element.drawing_index == curves_index) {
1009 insert_point_to_curve(ptd, curves);
1010 ptd.tag_curve_changed(curves_index);
1011 changed.store(true, std::memory_order_relaxed);
1012 }
1013 continue;
1014 }
1015 }
1016
1017 if (ptd.closest_element.element_mode == ElementMode::None || extrude_only) {
1018 if (ptd.extrude_point) {
1019 IndexMaskMemory memory;
1020 const IndexMask editable_curves = ptd.editable_curves(curves_index, memory);
1021 const float4x4 &layer_to_object = ptd.layer_to_object_per_curves[curves_index];
1022
1023 if (std::optional<bke::CurvesGeometry> result = extrude_curves(
1024 ptd, curves, layer_to_object, editable_curves))
1025 {
1026 curves = std::move(*result);
1027 }
1028 else {
1029 for (const StringRef selection_attribute_name :
1030 ed::curves::get_curves_selection_attribute_names(curves))
1031 {
1032 bke::GSpanAttributeWriter selection_writer = ed::curves::ensure_selection_attribute(
1033 curves, bke::AttrDomain::Point, bke::AttrType::Bool, selection_attribute_name);
1034 ed::curves::fill_selection_false(selection_writer.span);
1035 selection_writer.finish();
1036 }
1037 continue;
1038 }
1039
1040 add_single.store(false, std::memory_order_relaxed);
1041 point_added.store(true, std::memory_order_relaxed);
1042 ptd.tag_curve_changed(curves_index);
1043
1044 changed.store(true, std::memory_order_relaxed);
1045 continue;
1046 }
1047
1048 continue;
1049 }
1050
1051 if (curves_index != ptd.closest_element.drawing_index) {
1052 if (event->val != KM_DBL_CLICK && !ptd.delete_point) {
1053 for (const StringRef selection_attribute_name :
1054 ed::curves::get_curves_selection_attribute_names(curves))
1055 {
1056 bke::GSpanAttributeWriter selection_writer = ed::curves::ensure_selection_attribute(
1057 curves, bke::AttrDomain::Point, bke::AttrType::Bool, selection_attribute_name);
1058 ed::curves::fill_selection_false(selection_writer.span);
1059 selection_writer.finish();
1060 }
1061 }
1062
1063 continue;
1064 }
1065
1066 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
1067 const IndexRange points = points_by_curve[ptd.closest_element.curve_index];
1068
1069 if (event->val == KM_DBL_CLICK && ptd.cycle_handle_type) {
1070 const int8_t handle_type = curves.handle_types_right()[ptd.closest_element.point_index];
1071 /* Cycle to the next type. */
1072 const int8_t new_handle_type = (handle_type + 1) % CURVE_HANDLE_TYPES_NUM;
1073
1074 curves.handle_types_left_for_write()[ptd.closest_element.point_index] = new_handle_type;
1075 curves.handle_types_right_for_write()[ptd.closest_element.point_index] = new_handle_type;
1076 curves.calculate_bezier_auto_handles();
1077 ptd.tag_curve_changed(curves_index);
1078 add_single.store(false, std::memory_order_relaxed);
1079 }
1080
1081 if (ptd.delete_point) {
1082 curves.remove_points(IndexRange::from_single(ptd.closest_element.point_index), {});
1083 add_single.store(false, std::memory_order_relaxed);
1084 point_removed.store(true, std::memory_order_relaxed);
1085 ptd.tag_curve_changed(curves_index);
1086 continue;
1087 }
1088
1089 const bool clear_selection = event->val != KM_DBL_CLICK && !ptd.delete_point;
1090 if (close_curve_and_select(ptd, curves, points, clear_selection)) {
1091 ptd.tag_curve_changed(curves_index);
1092 add_single.store(false, std::memory_order_relaxed);
1093 }
1094
1095 changed.store(true, std::memory_order_relaxed);
1096 }
1097 });
1098
1099 if (add_single) {
1100 if (ptd.can_create_new_curve(op)) {
1101 const int curves_index = *ptd.active_drawing_index;
1102
1103 const float4x4 &layer_to_world = ptd.layer_to_world_per_curves[curves_index];
1104 bke::CurvesGeometry &curves = ptd.get_curves(curves_index);
1105
1106 add_single_point_and_curve(ptd, curves, layer_to_world);
1107 ptd.single_point_attributes(curves, curves_index);
1108 ptd.tag_curve_changed(curves_index);
1109
1110 changed.store(true, std::memory_order_relaxed);
1111 point_added = true;
1112 }
1113 }
1114
1115 ptd.point_added = point_added;
1116 ptd.point_removed = point_removed;
1117
1119 if (changed) {
1120 ptd.update_view(C);
1121 }
1122}
1123
1125 const int handle_display,
1126 IndexMaskMemory &memory)
1127{
1128 if (handle_display == CURVE_HANDLE_NONE) {
1129 return IndexMask(0);
1130 }
1131 if (handle_display == CURVE_HANDLE_ALL) {
1132 return curves.points_range();
1133 }
1134 /* else handle_display == CURVE_HANDLE_SELECTED */
1135
1136 if (!curves.has_curve_with_type(CURVE_TYPE_BEZIER)) {
1137 return IndexMask(0);
1138 }
1139
1140 const Array<int> point_to_curve_map = curves.point_to_curve_map();
1141 const VArray<int8_t> types = curves.curve_types();
1142
1143 const VArray<bool> selected_point = *curves.attributes().lookup_or_default<bool>(
1144 ".selection", bke::AttrDomain::Point, true);
1145 const VArray<bool> selected_left = *curves.attributes().lookup_or_default<bool>(
1146 ".selection_handle_left", bke::AttrDomain::Point, true);
1147 const VArray<bool> selected_right = *curves.attributes().lookup_or_default<bool>(
1148 ".selection_handle_right", bke::AttrDomain::Point, true);
1149
1150 const IndexMask selected_points = IndexMask::from_predicate(
1151 curves.points_range(), memory, [&](const int64_t point_i) {
1152 const bool is_selected = selected_point[point_i] || selected_left[point_i] ||
1153 selected_right[point_i];
1154 const bool is_bezier = types[point_to_curve_map[point_i]] == CURVE_TYPE_BEZIER;
1155 return is_selected && is_bezier;
1156 });
1157
1158 return selected_points;
1159}
1160
1162 const float3 &point) const
1163{
1165 vc.region, math::transform_point(layer_to_object, point), projection);
1166}
1167
1169 const float2 &screen_co,
1170 const float3 &depth_point_layer) const
1171{
1172 const float3 depth_point = math::transform_point(layer_to_world, depth_point_layer);
1173 float3 proj_point;
1174 ED_view3d_win_to_3d(vc.v3d, vc.region, depth_point, screen_co, proj_point);
1175 return math::transform_point(math::invert(layer_to_world), proj_point);
1176}
1177
1179{
1180 /* If in tools region, wait till we get to the main (3D-space)
1181 * region before allowing drawing to take place. */
1183
1184 wmWindow *win = CTX_wm_window(C);
1185 /* Set cursor to indicate modal. */
1187
1189
1190 this->vc = vc;
1191 this->projection = ED_view3d_ob_project_mat_get(this->vc.rv3d, this->vc.obact);
1192
1193 /* Distance threshold for mouse clicks to affect the spline or its points */
1194 this->mouse_co = float2(event->mval);
1197
1198 this->extrude_point = RNA_boolean_get(op->ptr, "extrude_point");
1199 this->delete_point = RNA_boolean_get(op->ptr, "delete_point");
1200 this->insert_point = RNA_boolean_get(op->ptr, "insert_point");
1201 this->move_seg = RNA_boolean_get(op->ptr, "move_segment");
1202 this->select_point = RNA_boolean_get(op->ptr, "select_point");
1203 this->move_point = RNA_boolean_get(op->ptr, "move_point");
1204 this->cycle_handle_type = RNA_boolean_get(op->ptr, "cycle_handle_type");
1205 this->extrude_handle = RNA_enum_get(op->ptr, "extrude_handle");
1206 /* Size is stored as the diameter. */
1207 this->radius = RNA_float_get(op->ptr, "size") / 2.0f;
1208
1209 this->move_entire = false;
1210 this->snap_angle = false;
1211
1212 this->handle_moved = false;
1213
1214 if (!(ELEM(event->type, LEFTMOUSE) && ELEM(event->val, KM_PRESS, KM_DBL_CLICK))) {
1216 }
1217
1218 if (std::optional<wmOperatorStatus> result = this->initialize(C, op, event)) {
1219 return *result;
1220 }
1221
1222 /* Add a modal handler for this operator. */
1224
1225 invoke_curves(*this, C, op, event);
1226
1228}
1229
1231{
1232 this->mouse_co = float2(event->mval);
1233 this->xy = float2(event->xy);
1234 this->prev_xy = float2(event->prev_xy);
1235
1236 if (event->type == LEFTMOUSE && event->val == KM_RELEASE) {
1237 return OPERATOR_FINISHED;
1238 }
1239 if (this->point_removed) {
1240 return OPERATOR_FINISHED;
1241 }
1242
1243 if (event->type == EVT_MODAL_MAP) {
1244 if (event->val == int(PenModal::MoveEntire)) {
1245 this->move_entire = !this->move_entire;
1246 }
1247 else if (event->val == int(PenModal::SnapAngle)) {
1248 this->snap_angle = !this->snap_angle;
1249 }
1250 else if (event->val == int(PenModal::MoveHandle)) {
1251 this->move_handle = !this->move_handle;
1252
1253 /* Record if handle has every been moved. */
1254 if (this->move_handle) {
1255 this->handle_moved = true;
1256 }
1257 }
1258 }
1259
1260 std::atomic<bool> changed = false;
1261 this->center_of_mass_co = calculate_center_of_mass(*this, false);
1262 if (ELEM(event->type, MOUSEMOVE, INBETWEEN_MOUSEMOVE)) {
1263 if (this->move_seg && this->closest_element.element_mode == ElementMode::Edge) {
1264 const int curves_index = this->closest_element.drawing_index;
1265 const float4x4 &layer_to_world = this->layer_to_world_per_curves[curves_index];
1266 bke::CurvesGeometry &curves = this->get_curves(curves_index);
1267
1268 move_segment(*this, curves, layer_to_world);
1269 this->tag_curve_changed(curves_index);
1270 changed.store(true, std::memory_order_relaxed);
1271 }
1272 else {
1274 for (const int curves_index : curves_range) {
1275 bke::CurvesGeometry &curves = this->get_curves(curves_index);
1276 const float4x4 &layer_to_object = this->layer_to_object_per_curves[curves_index];
1277 const float4x4 &layer_to_world = this->layer_to_world_per_curves[curves_index];
1278
1279 IndexMaskMemory memory;
1280 const IndexMask selection = this->all_selected_points(curves_index, memory);
1281
1282 if (move_handles_in_curve(*this, curves, selection, layer_to_world, layer_to_object)) {
1283 changed.store(true, std::memory_order_relaxed);
1284 this->tag_curve_changed(curves_index);
1285 }
1286 }
1287 });
1288 }
1289 }
1290
1292 if (changed) {
1293 this->update_view(C);
1294 }
1295
1296 /* Still running... */
1298}
1299
1301 public:
1303
1304 float3 project(const float2 &screen_co) const override
1305 {
1306 const float4x4 &layer_to_world = this->layer_to_world_per_curves[*this->active_drawing_index];
1307 return this->screen_to_layer(layer_to_world, screen_co, float3(0.0f));
1308 }
1309
1310 IndexMask all_selected_points(const int curves_index, IndexMaskMemory &memory) const override
1311 {
1312 const Curves *curves_id = this->all_curves[curves_index];
1313 const bke::CurvesGeometry &curves = curves_id->geometry.wrap();
1314 return retrieve_all_selected_points(curves, this->vc.v3d->overlay.handle_display, memory);
1315 }
1316
1318 IndexMaskMemory &memory) const override
1319 {
1320 const Curves *curves_id = this->all_curves[curves_index];
1321 const bke::CurvesGeometry &curves = curves_id->geometry.wrap();
1323 curves, this->vc.v3d->overlay.handle_display, memory);
1324 }
1325
1326 IndexMask editable_curves(const int curves_index, IndexMaskMemory & /*memory*/) const override
1327 {
1328 const Curves *curves_id = this->all_curves[curves_index];
1329 const bke::CurvesGeometry &curves = curves_id->geometry.wrap();
1330 return curves.curves_range();
1331 }
1332
1333 void tag_curve_changed(const int curves_index) const override
1334 {
1335 Curves *curves_id = this->all_curves[curves_index];
1336 bke::CurvesGeometry &curves = curves_id->geometry.wrap();
1337 curves.tag_topology_changed();
1338 }
1339
1340 bke::CurvesGeometry &get_curves(const int curves_index) const override
1341 {
1342 Curves *curves_id = this->all_curves[curves_index];
1343 return curves_id->geometry.wrap();
1344 }
1345
1346 IndexRange curves_range() const override
1347 {
1348 return this->all_curves.index_range();
1349 }
1350
1352 const int /*curves_index*/) const override
1353 {
1354 }
1355
1356 bool can_create_new_curve(wmOperator *op) const override
1357 {
1358 if (this->active_drawing_index == std::nullopt) {
1359 BKE_report(op->reports, RPT_ERROR, "No active Curves Object");
1360 return false;
1361 }
1362
1363 return true;
1364 }
1365
1366 void update_view(bContext *C) const override
1367 {
1368 for (Curves *curves_id : this->all_curves) {
1369 DEG_id_tag_update(&curves_id->id, ID_RECALC_GEOMETRY);
1370 WM_event_add_notifier(C, NC_GEOM | ND_DATA, curves_id);
1371 }
1372 ED_region_tag_redraw(this->vc.region);
1373 }
1374
1375 std::optional<wmOperatorStatus> initialize(bContext *C,
1376 wmOperator * /*op*/,
1377 const wmEvent * /*event*/) override
1378 {
1379 this->active_drawing_index = std::nullopt;
1380 VectorSet<Curves *> unique_curves;
1381
1382 const Main &bmain = *CTX_data_main(C);
1383
1384 Object *object = CTX_data_active_object(C);
1385 if (object && object_has_editable_curves(bmain, *object)) {
1386 unique_curves.add_new(id_cast<Curves *>(object->data));
1387 this->layer_to_world_per_curves.append(object->object_to_world());
1388 this->active_drawing_index = 0;
1389 }
1390
1391 CTX_DATA_BEGIN (C, Object *, object, selected_objects) {
1392 if (object_has_editable_curves(bmain, *object)) {
1393 if (unique_curves.add(id_cast<Curves *>(object->data))) {
1394 this->layer_to_world_per_curves.append(object->object_to_world());
1395 }
1396 }
1397 }
1399
1400 for (Curves *curves_id : unique_curves) {
1401 this->all_curves.append(curves_id);
1402 }
1403
1404 this->layer_to_object_per_curves.append_n_times(float4x4::identity(), this->all_curves.size());
1405
1406 return std::nullopt;
1407 }
1408};
1409
1410/* Exit and free memory. */
1412{
1413 CurvesPenToolOperation *ptd = static_cast<CurvesPenToolOperation *>(op->customdata);
1414
1415 /* Clear status message area. */
1416 ED_workspace_status_text(C, nullptr);
1417
1419
1420 ptd->update_view(C);
1421
1422 MEM_delete(ptd);
1423 /* Clear pointer. */
1424 op->customdata = nullptr;
1425}
1426
1427/* Invoke handler: Initialize the operator. */
1429{
1430 /* Allocate new data. */
1431 CurvesPenToolOperation *ptd_pointer = MEM_new<CurvesPenToolOperation>(__func__);
1432 op->customdata = ptd_pointer;
1433 CurvesPenToolOperation &ptd = *ptd_pointer;
1434
1435 const wmOperatorStatus result = ptd.invoke(C, op, event);
1437 curves_pen_exit(C, op);
1438 }
1439 return result;
1440}
1441
1442/* Modal handler: Events handling during interactive part. */
1444{
1445 CurvesPenToolOperation &ptd = *reinterpret_cast<CurvesPenToolOperation *>(op->customdata);
1446
1447 const wmOperatorStatus result = ptd.modal(C, op, event);
1449 curves_pen_exit(C, op);
1450 }
1451 return result;
1452}
1453
1455{
1457
1459 "extrude_point",
1460 false,
1461 "Extrude Point",
1462 "Add a point connected to the last selected point");
1463 RNA_def_enum(ot->srna,
1464 "extrude_handle",
1467 "Extrude Handle Type",
1468 "Type of the extruded handle");
1469 RNA_def_boolean(ot->srna, "delete_point", false, "Delete Point", "Delete an existing point");
1471 ot->srna, "insert_point", false, "Insert Point", "Insert Point into a curve segment");
1473 ot->srna, "move_segment", false, "Move Segment", "Move an existing curve segment");
1475 ot->srna, "select_point", false, "Select Point", "Select a point or its handles");
1476 RNA_def_boolean(ot->srna, "move_point", false, "Move Point", "Move a point or its handles");
1478 "cycle_handle_type",
1479 false,
1480 "Cycle Handle Type",
1481 "Cycle between all four handle types");
1483 ot->srna, "size", 0.01f, 0.0f, FLT_MAX, "Size", "Diameter of new points", 0.0f, 10.0f);
1484}
1485
1487{
1488 using namespace blender::ed::curves::pen_tool;
1489 static const EnumPropertyItem modal_items[] = {
1491 "MOVE_HANDLE",
1492 0,
1493 "Move Current Handle",
1494 "Move the current handle of the control point freely"},
1496 "MOVE_ENTIRE",
1497 0,
1498 "Move Entire Point",
1499 "Move the entire point using its handles"},
1500 {int(PenModal::SnapAngle),
1501 "SNAP_ANGLE",
1502 0,
1503 "Snap Angle",
1504 "Snap the handle angle to 45 degrees"},
1505 {0, nullptr, 0, nullptr, nullptr},
1506 };
1507
1508 wmKeyMap *keymap = WM_modalkeymap_find(keyconf, "Pen Tool Modal Map");
1509
1510 /* This function is called for each space-type and both Grease Pencil and Curves, only needs to
1511 * add map once. */
1512 if (keymap && keymap->modal_items) {
1513 return keymap;
1514 }
1515
1516 keymap = WM_modalkeymap_ensure(keyconf, "Pen Tool Modal Map", modal_items);
1517
1518 return keymap;
1519}
1520
1521} // namespace pen_tool
1522
1524{
1525 /* Identifiers. */
1526 ot->name = "Curves Pen";
1527 ot->idname = "CURVES_OT_pen";
1528 ot->description = "Construct and edit Bézier curves";
1529
1530 /* Callbacks. */
1533
1534 /* Flags. */
1535 ot->flag = OPTYPE_UNDO;
1536
1537 /* Properties. */
1539}
1540
1545
1547{
1548 wmKeyMap *keymap = pen_tool::ensure_keymap(keyconf);
1549 WM_modalkeymap_assign(keymap, "CURVES_OT_pen");
1550}
1551
1552} // namespace blender::ed::curves
#define CTX_DATA_BEGIN(C, Type, instance, member)
#define CTX_DATA_END
Low-level operations for curves.
Low-level operations for curves.
support for deformation groups and hooks.
Low-level operations for grease pencil.
General operations, lookup, etc. for materials.
#define IFACE_(msgid)
#define C
Definition RandGen.cpp:29
#define NC_GEOM
Definition WM_types.hh:406
#define ND_DATA
Definition WM_types.hh:552
#define ELEM(...)
Span< T > as_span() const
Definition BLI_array.hh:254
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:259
void copy_from(GSpan values)
GMutableSpan slice(const int64_t start, int64_t size) const
static IndexMask from_predicate(const IndexMask &universe, LinearAllocator<> &memory, Fn &&predicate, exec_mode::Mode mode=exec_mode::parallel)
constexpr int64_t first() const
constexpr IndexRange shift(int64_t n) const
constexpr IndexRange drop_back(int64_t n) const
constexpr int64_t last(const int64_t n=0) const
constexpr int64_t size() const
constexpr IndexRange()=default
static constexpr IndexRange from_single(const int64_t index)
constexpr IndexRange index_range() const
static constexpr IndexRange from_begin_end_inclusive(const int64_t begin, const int64_t last)
constexpr MutableSpan drop_back(const int64_t n) const
Definition BLI_span.hh:624
constexpr T & last(const int64_t n=0) const
Definition BLI_span.hh:695
bool add(const Key &key)
Definition BLI_set.hh:248
bool add(const Key &key)
void add_new(const Key &key)
int64_t size() const
void append(const T &value)
void insert(const int64_t insert_index, const T &value)
IndexRange index_range() const
MutableSpan< T > as_mutable_span()
Span< T > as_span() const
void opmodal(std::string text, const wmOperatorType *ot, int propvalue, bool inverted=false)
Definition area.cc:1107
GAttributeReader lookup_or_default(StringRef name, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
MutableSpan< int8_t > handle_types_left_for_write()
VArray< int8_t > curve_types() const
MutableSpan< float3 > positions_for_write()
MutableSpan< int8_t > handle_types_right_for_write()
std::optional< Span< float3 > > handle_positions_right() const
VArray< bool > cyclic() const
AttributeAccessor attributes() const
Span< float3 > positions() const
MutableSpan< float3 > handle_positions_right_for_write()
std::optional< Span< float3 > > handle_positions_left() const
IndexRange curves_range() const
VArray< int8_t > nurbs_knots_modes() const
MutableSpan< float3 > handle_positions_left_for_write()
MutableSpan< int > offsets_for_write()
MutableSpan< float > radius_for_write()
MutableSpan< bool > cyclic_for_write()
OffsetIndices< int > points_by_curve() const
Array< int > point_to_curve_map() const
MutableAttributeAccessor attributes_for_write()
GSpanAttributeWriter lookup_or_add_for_write_span(StringRef name, AttrDomain domain, AttrType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
void single_point_attributes(bke::CurvesGeometry &, const int) const override
float3 project(const float2 &screen_co) const override
bool can_create_new_curve(wmOperator *op) const override
IndexMask visible_bezier_handle_points(const int curves_index, IndexMaskMemory &memory) const override
bke::CurvesGeometry & get_curves(const int curves_index) const override
IndexMask editable_curves(const int curves_index, IndexMaskMemory &) const override
std::optional< wmOperatorStatus > initialize(bContext *C, wmOperator *, const wmEvent *) override
void tag_curve_changed(const int curves_index) const override
IndexMask all_selected_points(const int curves_index, IndexMaskMemory &memory) const override
virtual void tag_curve_changed(int curves_index) const =0
virtual IndexRange curves_range() const =0
virtual IndexMask editable_curves(int curves_index, IndexMaskMemory &memory) const =0
wmOperatorStatus invoke(bContext *C, wmOperator *op, const wmEvent *event)
virtual IndexMask visible_bezier_handle_points(int curves_index, IndexMaskMemory &memory) const =0
virtual IndexMask all_selected_points(int curves_index, IndexMaskMemory &memory) const =0
virtual bke::CurvesGeometry & get_curves(int curves_index) const =0
virtual float3 project(const float2 &screen_co) const =0
virtual void update_view(bContext *C) const =0
float2 layer_to_screen(const float4x4 &layer_to_object, const float3 &point) const
float3 screen_to_layer(const float4x4 &layer_to_world, const float2 &screen_co, const float3 &depth_point_layer) const
wmOperatorStatus modal(bContext *C, wmOperator *op, const wmEvent *event)
virtual std::optional< wmOperatorStatus > initialize(bContext *C, wmOperator *op, const wmEvent *event)=0
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
IndexMask complement(const IndexMask &universe, LinearAllocator<> &memory) const
#define FLT_MAX
#define sin
VecBase< float, D > normalize(VecOp< float, D >) RET
constexpr T sign(T) RET
#define abs
float length(VecOp< float, D >) RET
float distance_squared(VecT a, VecT b)
void copy(const GVArray &src, GMutableSpan dst, exec_mode::Mode mode=exec_mode::parallel)
void fill_index_range(MutableSpan< T > span, const T start=0)
Insertion insert(const float3 &point_prev, const float3 &handle_prev, const float3 &handle_next, const float3 &point_next, float parameter)
void gather_custom_knots(const bke::CurvesGeometry &src, const IndexMask &src_curves, int dst_curve_offset, bke::CurvesGeometry &dst)
void update_custom_knot_modes(const IndexMask &mask, const KnotsMode mode_for_regular, const KnotsMode mode_for_cyclic, bke::CurvesGeometry &curves)
auto attribute_filter_from_skip_ref(const Span< StringRef > skip)
void fill_attribute_range_default(MutableAttributeAccessor dst_attributes, AttrDomain domain, const AttributeFilter &attribute_filter, IndexRange range)
void copy_attributes(const AttributeAccessor src_attributes, AttrDomain src_domain, AttrDomain dst_domain, const AttributeFilter &attribute_filter, MutableAttributeAccessor dst_attributes)
void gather_attributes(AttributeAccessor src_attributes, AttrDomain src_domain, AttrDomain dst_domain, const AttributeFilter &attribute_filter, const IndexMask &selection, MutableAttributeAccessor dst_attributes)
constexpr float default_handle_px_distance
Definition curves_pen.cc:64
static void move_segment(const PenToolOperation &ptd, bke::CurvesGeometry &curves, const float4x4 &layer_to_world)
static void add_single_point_and_curve(const PenToolOperation &ptd, bke::CurvesGeometry &curves, const float4x4 &layer_to_world)
void pen_tool_common_props(wmOperatorType *ot)
static void pen_status_indicators(bContext *C, wmOperator *op)
constexpr float selection_point_overwrite_edge_distance_factor
Definition curves_pen.cc:72
static float2 snap_8_angles(const float2 &p)
static bool close_curve_and_select(const PenToolOperation &ptd, bke::CurvesGeometry &curves, const IndexRange points, const bool clear_selection)
constexpr int CURVE_HANDLE_TYPES_NUM
Definition curves_pen.cc:67
static void curves_pen_exit(bContext *C, wmOperator *op)
static bool move_handles_in_curve(const PenToolOperation &ptd, bke::CurvesGeometry &curves, const IndexMask &selection, const float4x4 &layer_to_world, const float4x4 &layer_to_object)
static const EnumPropertyItem prop_handle_types[]
Definition curves_pen.cc:53
static std::optional< bke::CurvesGeometry > extrude_curves(const PenToolOperation &ptd, const bke::CurvesGeometry &src, const float4x4 &layer_to_object, const IndexMask editable_curves)
static ClosestElement find_closest_element(const PenToolOperation &ptd, const float2 &mouse_co)
static float2 line_segment_closest_point(const float2 &pos_1, const float2 &pos_2, const float2 &pos, float &r_local_t)
static void pen_find_closest_handle(const PenToolOperation &ptd, const bke::CurvesGeometry &curves, const IndexMask &bezier_points, const float4x4 &layer_to_object, const int drawing_index, const float2 &mouse_co, ClosestElement &r_closest_element)
static void pen_find_closest_point(const PenToolOperation &ptd, const bke::CurvesGeometry &curves, const IndexMask &editable_curves, const float4x4 &layer_to_object, const int drawing_index, const float2 &mouse_co, ClosestElement &r_closest_element)
static wmOperatorStatus curves_pen_invoke(bContext *C, wmOperator *op, const wmEvent *event)
constexpr float selection_point_overwrite_edge_distance_factor_sq
Definition curves_pen.cc:74
static IndexMask retrieve_visible_bezier_handle_points(const bke::CurvesGeometry &curves, const int handle_display, IndexMaskMemory &memory)
static void invoke_curves(PenToolOperation &ptd, bContext *C, wmOperator *op, const wmEvent *event)
static float2 calculate_center_of_mass(const PenToolOperation &ptd, const bool ends_only)
constexpr float selection_distance_factor
Definition curves_pen.cc:60
static void insert_point_to_curve(const PenToolOperation &ptd, bke::CurvesGeometry &src)
constexpr float selection_distance_factor_edge
Definition curves_pen.cc:61
static wmOperatorStatus curves_pen_modal(bContext *C, wmOperator *op, const wmEvent *event)
constexpr float selection_edge_priority_factor
Definition curves_pen.cc:70
wmKeyMap * ensure_keymap(wmKeyConfig *keyconf)
static void pen_find_closest_edge_point(const PenToolOperation &ptd, const bke::CurvesGeometry &curves, const IndexMask &editable_curves, const float4x4 &layer_to_object, const int drawing_index, const float2 &mouse_co, ClosestElement &r_closest_element)
bool has_anything_selected(const VArray< bool > &varray, const IndexRange range_to_check)
void fill_selection_false(GMutableSpan selection)
bool object_has_editable_curves(const Main &bmain, const Object &object)
Definition curves_ops.cc:87
void ED_curves_pentool_modal_keymap(wmKeyConfig *keyconf)
void fill_selection_true(GMutableSpan selection)
Span< StringRef > get_curves_selection_attribute_names(const bke::CurvesGeometry &curves)
void ED_operatortypes_curves_pen()
static void CURVES_OT_pen(wmOperatorType *ot)
IndexMask retrieve_all_selected_points(const bke::CurvesGeometry &curves, const int handle_display, LinearAllocator<> &memory)
bke::GSpanAttributeWriter ensure_selection_attribute(bke::CurvesGeometry &curves, bke::AttrDomain selection_domain, bke::AttrType create_type, StringRef attribute_name)
constexpr ParallelGrainSize grain_size(int grain_size)
T clamp(const T &a, const T &min, const T &max)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
CartesianBasis invert(const CartesianBasis &basis)
T interpolate(const T &a, const T &b, const FactorT &t)
T distance_squared(const VecBase< T, Size > &a, const VecBase< T, Size > &b)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
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
void WM_operatortype_append(void(*opfunc)(wmOperatorType *))
__int64 int64_t
Definition BKE_text.h:90
int RNA_enum_get(PointerRNA *ptr, const char *name)
wmKeyMap * WM_modalkeymap_find(wmKeyConfig *keyconf, const char *idname)
Definition wm_keymap.cc:999
ViewContext ED_view3d_viewcontext_init(bContext *C, Depsgraph *depsgraph)
float4x4 ED_view3d_ob_project_mat_get(const RegionView3D *rv3d, const Object *ob)
Depsgraph * CTX_data_depsgraph_pointer(const bContext *C)
void WM_cursor_modal_set(wmWindow *win, int val)
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
float2 ED_view3d_project_float_v2_m4(const ARegion *region, const float co[3], const float4x4 &mat)
wmWindow * CTX_wm_window(const bContext *C)
MatBase< float, 4, 4 > float4x4
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:681
@ NURBS_KNOT_MODE_ENDPOINT
@ NURBS_KNOT_MODE_NORMAL
@ INBETWEEN_MOUSEMOVE
VecBase< float, 2 > float2
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
void WM_cursor_modal_restore(wmWindow *win)
void WM_operator_properties_mouse_select(wmOperatorType *ot)
ATTR_WARN_UNUSED_RESULT const size_t num
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
wmEventHandler_Op * WM_event_add_modal_handler(bContext *C, wmOperator *op)
@ WM_CURSOR_CROSS
Definition wm_cursors.hh:28
Main * CTX_data_main(const bContext *C)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
void BKE_defgroup_copy_list(ListBaseT< bDeformGroup > *outbase, const ListBaseT< bDeformGroup > *inbase)
Definition deform.cc:75
PropertyRNA * RNA_def_float_distance(StructOrFunctionRNA *cont_, const char *identifier, const float default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
@ KM_DBL_CLICK
Definition WM_types.hh:327
@ KM_RELEASE
Definition WM_types.hh:325
void WM_modalkeymap_assign(wmKeyMap *km, const char *opname)
wmKeyMap * WM_modalkeymap_ensure(wmKeyConfig *keyconf, const char *idname, const EnumPropertyItem *items)
Definition wm_keymap.cc:972
@ OPTYPE_UNDO
Definition WM_types.hh:190
VecBase< float, 3 > float3
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
float ED_view3d_select_dist_px()
void ED_view3d_win_to_3d(const View3D *v3d, const ARegion *region, const float depth_pt[3], const float mval[2], float r_out[3])
Object * CTX_data_active_object(const bContext *C)
void ED_workspace_status_text(bContext *C, const char *str)
Definition area.cc:1153
float RNA_float_get(PointerRNA *ptr, const char *name)
signed char int8_t
Definition BKE_text.h:76
static AngleRadian from_degree(float angle_degree)
ListBaseT< struct bDeformGroup > vertex_group_names
RegionView3D * rv3d
Definition ED_view3d.hh:86
bool is_closer(const float new_distance_squared, const ElementMode new_element_mode, const float threshold_distance) const
Definition curves_pen.cc:78
wmEventType type
Definition WM_types.hh:817
const char * description
Definition WM_types.hh:1115
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
wmOperatorStatus(*) modal(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1157
struct ReportList * reports
struct wmOperatorType * type