Blender V5.3
editcurve_pen.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "DNA_curve_types.h"
11
12#include "MEM_guardedalloc.h"
13
14#include "BLI_listbase.hh"
15#include "BLI_math_geom_c.hh"
16#include "BLI_math_matrix_c.hh"
17#include "BLI_math_vector_c.hh"
18
19#include "BKE_context.hh"
20#include "BKE_curve.hh"
21
22#include "DEG_depsgraph.hh"
23
24#include "WM_api.hh"
25
26#include "ED_curve.hh"
27#include "ED_screen.hh"
28#include "ED_select_utils.hh"
29#include "ED_view3d.hh"
30
31#include "curve_intern.hh"
32
33#include "RNA_access.hh"
34#include "RNA_define.hh"
35
36#include <cfloat>
37
38namespace blender {
39
40#define FOREACH_SELECTED_BEZT_BEGIN(bezt, nurbs) \
41 for (Nurb &nu : nurbs) { \
42 if (nu.type == CU_BEZIER) { \
43 for (int i = 0; i < nu.pntsu; i++) { \
44 BezTriple *bezt = nu.bezt + i; \
45 if (BEZT_ISSEL_ANY(bezt) && !bezt->hide) {
46
47#define FOREACH_SELECTED_BEZT_END \
48 } \
49 } \
50 } \
51 BKE_nurb_handles_calc(&nu); \
52 } \
53 ((void)0)
54
55/* Used to scale the default select distance. */
56#define SEL_DIST_FACTOR 0.2f
57
61struct CutData {
62 /* Index of the last #BezTriple or BPoint before the cut. */
64 /* Nurb to which the cut belongs to. */
66 /* Minimum distance to curve from mouse location. */
67 float min_dist;
68 /* Fraction of segments after which the new point divides the curve segment. */
69 float parameter;
70 /* Whether the currently identified closest point has any vertices before/after it. */
72 /* Locations of adjacent vertices and cut location. */
73 float prev_loc[3], cut_loc[3], next_loc[3];
74 /* Mouse location in floats. */
75 float mval[2];
76};
77
82 /* Nurb being altered. */
84 /* Index of the #BezTriple before the segment. */
86 /* Fraction along the segment at which mouse was pressed. */
87 float t;
88};
89
92 /* Whether the mouse is clicking and dragging. */
94 /* Whether a new point was added at the beginning of tool execution. */
96 /* Whether a segment is being altered by click and drag. */
98 /* Whether some action was done. Used for select. */
99 bool changed;
100 /* Whether a point was found underneath the mouse. */
102 /* Whether multiple selected points should be moved. */
104 /* Whether a point has already been selected. */
106 /* Whether a shift-click occurred. */
108
109 /* Whether the current handle type of the moved handle is free. */
111 /* Whether the shortcut for moving the adjacent handle is pressed. */
113 /* Whether the current state of the moved handle is linked. */
115 /* Whether the current state of the handle angle is locked. */
117 /* Whether the shortcut for moving the entire point is pressed. */
119
120 /* Data about found point. Used for closing splines. */
124};
125
127 {HD_AUTO, "AUTO", 0, "Auto", ""},
128 {HD_VECT, "VECTOR", 0, "Vector", ""},
129 {0, nullptr, 0, nullptr, nullptr},
130};
131
133 OFF = 0,
136};
137
139 {OFF, "OFF", 0, "None", ""},
140 {ON_PRESS, "ON_PRESS", 0, "On Press", "Move handles after closing the spline"},
141 {ON_CLICK, "ON_CLICK", 0, "On Click", "Spline closes on release if not dragged"},
142 {0, nullptr, 0, nullptr, nullptr},
143};
144
145static void update_location_for_2d_curve(const ViewContext *vc, float location[3])
146{
147 Curve *cu = id_cast<Curve *>(vc->obedit->data);
148 if (CU_IS_2D(cu)) {
149 const float eps = 1e-6f;
150
151 /* Get the view vector to `location`. */
152 float view_dir[3];
153 ED_view3d_global_to_vector(vc->rv3d, location, view_dir);
154
155 /* Get the plane. */
156 const float *plane_co = vc->obedit->object_to_world().location();
157 float plane_no[3];
158 /* Only normalize to avoid precision errors. */
159 normalize_v3_v3(plane_no, vc->obedit->object_to_world()[2]);
160
161 if (fabsf(dot_v3v3(view_dir, plane_no)) < eps) {
162 /* Can't project on an aligned plane. */
163 }
164 else {
165 float lambda;
166 if (isect_ray_plane_v3_factor(location, view_dir, plane_co, plane_no, &lambda)) {
167 /* Check if we're behind the viewport */
168 float location_test[3];
169 madd_v3_v3v3fl(location_test, location, view_dir, lambda);
170 if ((vc->rv3d->is_persp == false) ||
171 (mul_project_m4_v3_zfac(vc->rv3d->persmat, location_test) > 0.0f))
172 {
173 copy_v3_v3(location, location_test);
174 }
175 }
176 }
177 }
178
179 float imat[4][4];
180 invert_m4_m4(imat, vc->obedit->object_to_world().ptr());
181 mul_m4_v3(imat, location);
182
183 if (CU_IS_2D(cu)) {
184 location[2] = 0.0f;
185 }
186}
187
189 const float pos_2d[2],
190 const float depth[3],
191 float r_pos_3d[3])
192{
193 mul_v3_m4v3(r_pos_3d, vc->obedit->object_to_world().ptr(), depth);
194 ED_view3d_win_to_3d(vc->v3d, vc->region, r_pos_3d, pos_2d, r_pos_3d);
195 update_location_for_2d_curve(vc, r_pos_3d);
196}
197
199 const int pos_2d[2],
200 const float depth[3],
201 float r_pos_3d[3])
202{
203 const float pos_2d_fl[2] = {float(pos_2d[0]), float(pos_2d[1])};
204 screenspace_to_worldspace(vc, pos_2d_fl, depth, r_pos_3d);
205}
206
208 const float pos_3d[3],
209 float r_pos_2d[2])
210{
212 vc->region, pos_3d, r_pos_2d, V3D_PROJ_TEST_CLIP_BB | V3D_PROJ_TEST_CLIP_WIN) ==
214}
215
216static void move_bezt_by_displacement(BezTriple *bezt, const float disp_3d[3])
217{
218 add_v3_v3(bezt->vec[0], disp_3d);
219 add_v3_v3(bezt->vec[1], disp_3d);
220 add_v3_v3(bezt->vec[2], disp_3d);
221}
222
226static void move_bezt_to_location(BezTriple *bezt, const float location[3])
227{
228 float disp_3d[3];
229 sub_v3_v3v3(disp_3d, location, bezt->vec[1]);
230 move_bezt_by_displacement(bezt, disp_3d);
231}
232
236static void remove_handle_movement_constraints(BezTriple *bezt, const bool f1, const bool f3)
237{
238 if (f1) {
239 if (bezt->h1 == HD_VECT) {
240 bezt->h1 = HD_FREE;
241 }
242 if (bezt->h1 == HD_AUTO) {
243 bezt->h1 = HD_ALIGN;
244 bezt->h2 = HD_ALIGN;
245 }
246 }
247 if (f3) {
248 if (bezt->h2 == HD_VECT) {
249 bezt->h2 = HD_FREE;
250 }
251 if (bezt->h2 == HD_AUTO) {
252 bezt->h1 = HD_ALIGN;
253 bezt->h2 = HD_ALIGN;
254 }
255 }
256}
257
259 BezTriple *bezt,
260 const int bezt_idx,
261 const float disp_2d[2],
262 const float distance,
263 const bool link_handles,
264 const bool lock_angle)
265{
266 if (lock_angle) {
267 float disp_3d[3];
268 sub_v3_v3v3(disp_3d, bezt->vec[bezt_idx], bezt->vec[1]);
270 add_v3_v3v3(bezt->vec[bezt_idx], bezt->vec[1], disp_3d);
271 }
272 else {
273 float pos[2], dst[2];
274 worldspace_to_screenspace(vc, bezt->vec[bezt_idx], pos);
275 add_v2_v2v2(dst, pos, disp_2d);
276
277 float location[3];
278 screenspace_to_worldspace(vc, dst, bezt->vec[bezt_idx], location);
279 if (bezt_idx == 1) {
280 move_bezt_to_location(bezt, location);
281 }
282 else {
283 copy_v3_v3(bezt->vec[bezt_idx], location);
284 if (bezt->h1 == HD_ALIGN && bezt->h2 == HD_ALIGN) {
285 /* Move the handle on the opposite side. */
286 float handle_vec[3];
287 sub_v3_v3v3(handle_vec, bezt->vec[1], location);
288 const int other_handle = bezt_idx == 2 ? 0 : 2;
289 normalize_v3_length(handle_vec, len_v3v3(bezt->vec[1], bezt->vec[other_handle]));
290 add_v3_v3v3(bezt->vec[other_handle], bezt->vec[1], handle_vec);
291 }
292 }
293
294 if (link_handles) {
295 float handle[3];
296 sub_v3_v3v3(handle, bezt->vec[1], bezt->vec[bezt_idx]);
297 add_v3_v3v3(bezt->vec[(bezt_idx + 2) % 4], bezt->vec[1], handle);
298 }
299 }
300}
301
302static void move_bp_to_location(const ViewContext *vc, BPoint *bp, const float mval[2])
303{
304 float location[3];
305 screenspace_to_worldspace(vc, mval, bp->vec, location);
306
307 copy_v3_v3(bp->vec, location);
308}
309
315static bool get_selected_center(const ListBaseT<Nurb> *nurbs,
316 const bool mid_only,
317 const bool bezt_only,
318 float r_center[3])
319{
320 int end_count = 0;
321 zero_v3(r_center);
322 for (Nurb &nu : *nurbs) {
323 if (nu.type == CU_BEZIER) {
324 for (int i = 0; i < nu.pntsu; i++) {
325 BezTriple *bezt = nu.bezt + i;
326 if (bezt->hide) {
327 continue;
328 }
329 if (mid_only) {
330 if (BEZT_ISSEL_ANY(bezt)) {
331 add_v3_v3(r_center, bezt->vec[1]);
332 end_count++;
333 }
334 }
335 else {
336 if (BEZT_ISSEL_IDX(bezt, 1)) {
337 add_v3_v3(r_center, bezt->vec[1]);
338 end_count++;
339 }
340 else if (BEZT_ISSEL_IDX(bezt, 0)) {
341 add_v3_v3(r_center, bezt->vec[0]);
342 end_count++;
343 }
344 else if (BEZT_ISSEL_IDX(bezt, 2)) {
345 add_v3_v3(r_center, bezt->vec[2]);
346 end_count++;
347 }
348 }
349 }
350 }
351 else if (!bezt_only) {
352 for (int i = 0; i < nu.pntsu; i++) {
353 if (!nu.bp->hide && (nu.bp + i)->f1 & SELECT) {
354 add_v3_v3(r_center, (nu.bp + i)->vec);
355 end_count++;
356 }
357 }
358 }
359 }
360 if (end_count) {
361 mul_v3_fl(r_center, 1.0f / end_count);
362 return true;
363 }
364 return false;
365}
366
371 const wmEvent *event,
372 CurvePenData *cpd,
373 ListBaseT<Nurb> *nurbs,
374 const bool bezt_only)
375{
376 const float mval[2] = {float(event->xy[0]), float(event->xy[1])};
377 const float prev_mval[2] = {float(event->prev_xy[0]), float(event->prev_xy[1])};
378 float disp_2d[2];
379 sub_v2_v2v2(disp_2d, mval, prev_mval);
380
381 const bool link_handles = cpd->link_handles && !cpd->free_toggle;
382 const bool lock_angle = cpd->lock_angle;
383 const bool move_entire = cpd->move_entire;
384
385 float distance = 0.0f;
386 if (lock_angle) {
387 float mval_3d[3], center_mid[3];
388 get_selected_center(nurbs, true, true, center_mid);
389 screenspace_to_worldspace_int(vc, event->mval, center_mid, mval_3d);
390 distance = len_v3v3(center_mid, mval_3d);
391 }
392
393 for (Nurb &nu : *nurbs) {
394 if (nu.type == CU_BEZIER) {
395 for (int i = 0; i < nu.pntsu; i++) {
396 BezTriple *bezt = nu.bezt + i;
397 if (bezt->hide) {
398 continue;
399 }
400 if (BEZT_ISSEL_IDX(bezt, 1) || (move_entire && BEZT_ISSEL_ANY(bezt))) {
401 move_bezt_handle_or_vertex_by_displacement(vc, bezt, 1, disp_2d, 0.0f, false, false);
402 }
403 else {
405 bezt, BEZT_ISSEL_IDX(bezt, 0), BEZT_ISSEL_IDX(bezt, 2));
406 if (BEZT_ISSEL_IDX(bezt, 0)) {
408 vc, bezt, 0, disp_2d, distance, link_handles, lock_angle);
409 }
410 else if (BEZT_ISSEL_IDX(bezt, 2)) {
412 vc, bezt, 2, disp_2d, distance, link_handles, lock_angle);
413 }
414 }
415 }
417 }
418 else if (!bezt_only) {
419 for (int i = 0; i < nu.pntsu; i++) {
420 BPoint *bp = nu.bp + i;
421 if (!bp->hide && (bp->f1 & SELECT)) {
422 float pos[2], dst[2];
423 worldspace_to_screenspace(vc, bp->vec, pos);
424 add_v2_v2v2(dst, pos, disp_2d);
425 move_bp_to_location(vc, bp, dst);
426 }
427 }
428 }
429 }
430}
431
432static int get_nurb_index(const ListBaseT<Nurb> *nurbs, const Nurb *nurb)
433{
434 return BLI_findindex(nurbs, nurb);
435}
436
437static void delete_nurb(Curve *cu, Nurb *nu)
438{
439 EditNurb *editnurb = cu->editnurb;
440 ListBaseT<Nurb> *nurbs = &editnurb->nurbs;
441 const int nu_index = get_nurb_index(nurbs, nu);
442 if (cu->actnu == nu_index) {
443 BKE_curve_nurb_vert_active_set(cu, nullptr, nullptr);
444 }
445
446 BLI_remlink(nurbs, nu);
447 BKE_nurb_free(nu);
448}
449
450static void delete_bezt_from_nurb(const BezTriple *bezt, Nurb *nu, EditNurb *editnurb)
451{
452 BLI_assert(nu->type == CU_BEZIER);
453 const int index = BKE_curve_nurb_vert_index_get(nu, bezt);
454 nu->pntsu -= 1;
455 memmove(nu->bezt + index, nu->bezt + index + 1, (nu->pntsu - index) * sizeof(BezTriple));
457}
458
459static void delete_bp_from_nurb(const BPoint *bp, Nurb *nu, EditNurb *editnurb)
460{
461 BLI_assert(nu->type == CU_NURBS || nu->type == CU_POLY);
462 const int index = BKE_curve_nurb_vert_index_get(nu, bp);
463 nu->pntsu -= 1;
464 memmove(nu->bp + index, nu->bp + index + 1, (nu->pntsu - index) * sizeof(BPoint));
466}
467
473 const ListBaseT<Nurb> *nurbs,
474 const float point[2],
475 Nurb **r_nu,
476 BezTriple **r_bezt,
477 BPoint **r_bp,
478 int *r_bezt_idx)
479{
480 *r_nu = nullptr;
481 *r_bezt = nullptr;
482 *r_bp = nullptr;
483
484 float min_dist_bezt = FLT_MAX;
485 int closest_handle = 0;
486 BezTriple *closest_bezt = nullptr;
487 Nurb *closest_bezt_nu = nullptr;
488
489 float min_dist_bp = FLT_MAX;
490 BPoint *closest_bp = nullptr;
491 Nurb *closest_bp_nu = nullptr;
492
493 for (Nurb &nu : *nurbs) {
494 if (nu.type == CU_BEZIER) {
495 for (int i = 0; i < nu.pntsu; i++) {
496 BezTriple *bezt = &nu.bezt[i];
497 float bezt_vec[2];
498 int start = 0, end = 3;
499
500 /* Consider handles only if visible. Else only consider the middle point of the triple. */
501 int handle_display = vc->v3d->overlay.handle_display;
502 if (handle_display == CURVE_HANDLE_NONE ||
503 (handle_display == CURVE_HANDLE_SELECTED && !BEZT_ISSEL_ANY(bezt)))
504 {
505 start = 1;
506 end = 2;
507 }
508
509 /* Loop over each of the 3 points of the #BezTriple and update data of closest bezt. */
510 for (int j = start; j < end; j++) {
511 if (worldspace_to_screenspace(vc, bezt->vec[j], bezt_vec)) {
512 const float dist = len_manhattan_v2v2(bezt_vec, point);
513 if (dist < min_dist_bezt) {
514 min_dist_bezt = dist;
515 closest_bezt = bezt;
516 closest_bezt_nu = &nu;
517 closest_handle = j;
518 }
519 }
520 }
521 }
522 }
523 else {
524 for (int i = 0; i < nu.pntsu; i++) {
525 BPoint *bp = &nu.bp[i];
526 float bp_vec[2];
527
528 /* Update data of closest #BPoint. */
529 if (worldspace_to_screenspace(vc, bp->vec, bp_vec)) {
530 const float dist = len_manhattan_v2v2(bp_vec, point);
531 if (dist < min_dist_bp) {
532 min_dist_bp = dist;
533 closest_bp = bp;
534 closest_bp_nu = &nu;
535 }
536 }
537 }
538 }
539 }
540
541 /* Assign closest data to the returned variables. */
542 const float threshold_dist = ED_view3d_select_dist_px() * SEL_DIST_FACTOR;
543 if (min_dist_bezt < threshold_dist || min_dist_bp < threshold_dist) {
544 if (min_dist_bp < min_dist_bezt) {
545 *r_bp = closest_bp;
546 *r_nu = closest_bp_nu;
547 }
548 else {
549 *r_bezt = closest_bezt;
550 *r_bezt_idx = closest_handle;
551 *r_nu = closest_bezt_nu;
552 }
553 return true;
554 }
555 return false;
556}
557
562 const BezTriple *bezt2,
563 const float parameter,
564 float r_point[3])
565{
566 float tmp1[3], tmp2[3], tmp3[3];
567 interp_v3_v3v3(tmp1, bezt1->vec[1], bezt1->vec[2], parameter);
568 interp_v3_v3v3(tmp2, bezt1->vec[2], bezt2->vec[0], parameter);
569 interp_v3_v3v3(tmp3, bezt2->vec[0], bezt2->vec[1], parameter);
570 interp_v3_v3v3(tmp1, tmp1, tmp2, parameter);
571 interp_v3_v3v3(tmp2, tmp2, tmp3, parameter);
572 interp_v3_v3v3(r_point, tmp1, tmp2, parameter);
573}
574
578static void calculate_new_bezier_point(const float point_prev[3],
579 float handle_prev[3],
580 float new_left_handle[3],
581 float new_right_handle[3],
582 float handle_next[3],
583 const float point_next[3],
584 const float parameter)
585{
586 float center_point[3];
587 interp_v3_v3v3(center_point, handle_prev, handle_next, parameter);
588 interp_v3_v3v3(handle_prev, point_prev, handle_prev, parameter);
589 interp_v3_v3v3(handle_next, handle_next, point_next, parameter);
590 interp_v3_v3v3(new_left_handle, handle_prev, center_point, parameter);
591 interp_v3_v3v3(new_right_handle, center_point, handle_next, parameter);
592}
593
594static bool is_cyclic(const Nurb *nu)
595{
596 return nu->flagu & CU_NURB_CYCLIC;
597}
598
602static void insert_bezt_to_nurb(Nurb *nu, const CutData *data, Curve *cu)
603{
604 EditNurb *editnurb = cu->editnurb;
605
606 BezTriple *new_bezt_array = MEM_new_array_uninitialized<BezTriple>((nu->pntsu + 1), __func__);
607 const int index = data->bezt_index + 1;
608 /* Copy all control points before the cut to the new memory. */
609 ED_curve_beztcpy(editnurb, new_bezt_array, nu->bezt, index);
610 BezTriple *new_bezt = new_bezt_array + index;
611
612 /* Duplicate control point after the cut. */
613 ED_curve_beztcpy(editnurb, new_bezt, new_bezt - 1, 1);
614 copy_v3_v3(new_bezt->vec[1], data->cut_loc);
615
616 if (index < nu->pntsu) {
617 /* Copy all control points after the cut to the new memory. */
618 ED_curve_beztcpy(editnurb, new_bezt_array + index + 1, nu->bezt + index, nu->pntsu - index);
619 }
620
621 nu->pntsu += 1;
623
624 BezTriple *next_bezt;
625 if (is_cyclic(nu) && (index == nu->pntsu - 1)) {
626 next_bezt = new_bezt_array;
627 }
628 else {
629 next_bezt = new_bezt + 1;
630 }
631
632 /* Interpolate radius, tilt, weight */
633 new_bezt->tilt = interpf(next_bezt->tilt, (new_bezt - 1)->tilt, data->parameter);
634 new_bezt->radius = interpf(next_bezt->radius, (new_bezt - 1)->radius, data->parameter);
635 new_bezt->weight = interpf(next_bezt->weight, (new_bezt - 1)->weight, data->parameter);
636
637 new_bezt->h1 = new_bezt->h2 = HD_ALIGN;
638
639 calculate_new_bezier_point((new_bezt - 1)->vec[1],
640 (new_bezt - 1)->vec[2],
641 new_bezt->vec[0],
642 new_bezt->vec[2],
643 next_bezt->vec[0],
644 next_bezt->vec[1],
645 data->parameter);
646
647 MEM_delete(nu->bezt);
648 nu->bezt = new_bezt_array;
649 ED_curve_deselect_all(editnurb);
651 BEZT_SEL_IDX(new_bezt, 1);
652}
653
657static void insert_bp_to_nurb(Nurb *nu, const CutData *data, Curve *cu)
658{
659 EditNurb *editnurb = cu->editnurb;
660
661 BPoint *new_bp_array = MEM_new_array_uninitialized<BPoint>((nu->pntsu + 1), __func__);
662 const int index = data->bp_index + 1;
663 /* Copy all control points before the cut to the new memory. */
664 ED_curve_bpcpy(editnurb, new_bp_array, nu->bp, index);
665 BPoint *new_bp = new_bp_array + index;
666
667 /* Duplicate control point after the cut. */
668 ED_curve_bpcpy(editnurb, new_bp, new_bp - 1, 1);
669 copy_v3_v3(new_bp->vec, data->cut_loc);
670
671 if (index < nu->pntsu) {
672 /* Copy all control points after the cut to the new memory. */
673 ED_curve_bpcpy(editnurb, new_bp_array + index + 1, nu->bp + index, (nu->pntsu - index));
674 }
675
676 nu->pntsu += 1;
678
679 BPoint *next_bp;
680 if (is_cyclic(nu) && (index == nu->pntsu - 1)) {
681 next_bp = new_bp_array;
682 }
683 else {
684 next_bp = new_bp + 1;
685 }
686
687 /* Interpolate radius, tilt, weight */
688 new_bp->tilt = interpf(next_bp->tilt, (new_bp - 1)->tilt, data->parameter);
689 new_bp->radius = interpf(next_bp->radius, (new_bp - 1)->radius, data->parameter);
690 new_bp->weight = interpf(next_bp->weight, (new_bp - 1)->weight, data->parameter);
691
692 MEM_delete(nu->bp);
693 nu->bp = new_bp_array;
694 ED_curve_deselect_all(editnurb);
696 new_bp->f1 |= SELECT;
697}
698
710static void get_updated_data_for_edge(const float point[2],
711 const float point1[2],
712 const float point2[2],
713 const int point_idx,
714 const int resolu_idx,
715 float *r_min_dist,
716 int *r_min_i,
717 float *r_param)
718{
719 float edge[2], vec1[2], vec2[2];
720 sub_v2_v2v2(edge, point1, point2);
721 sub_v2_v2v2(vec1, point1, point);
722 sub_v2_v2v2(vec2, point, point2);
723 const float len_vec1 = len_v2(vec1);
724 const float len_vec2 = len_v2(vec2);
725 const float dot1 = dot_v2v2(edge, vec1);
726 const float dot2 = dot_v2v2(edge, vec2);
727
728 /* Signs of dot products being equal implies that the angles formed with the external point are
729 * either both acute or both obtuse, meaning the external point is closer to a point on the edge
730 * rather than an endpoint. */
731 if ((dot1 > 0) == (dot2 > 0)) {
732 const float perp_dist = len_vec1 * sinf(angle_v2v2(vec1, edge));
733 if (*r_min_dist > perp_dist) {
734 *r_min_dist = perp_dist;
735 *r_min_i = point_idx;
736 *r_param = resolu_idx + len_vec1 * cos_v2v2v2(point, point1, point2) / len_v2(edge);
737 }
738 }
739 else {
740 if (*r_min_dist > len_vec2) {
741 *r_min_dist = len_vec2;
742 *r_min_i = point_idx;
743 *r_param = resolu_idx;
744 }
745 }
746}
747
752 const ViewContext *vc, CutData *cd, Nurb *nu, const int resolu, const float point[2])
753{
754 float min_dist = cd->min_dist, param = 0.0f;
755 int min_i = 0;
756 const int end = is_cyclic(nu) ? nu->pntsu : nu->pntsu - 1;
757
758 if (nu->type == CU_BEZIER) {
759 for (int i = 0; i < end; i++) {
760 float *points = MEM_new_array_uninitialized<float>(3 * (resolu + 1), __func__);
761
762 const BezTriple *bezt1 = nu->bezt + i;
763 const BezTriple *bezt2 = nu->bezt + (i + 1) % nu->pntsu;
764
765 /* Calculate all points on curve. */
766 for (int j = 0; j < 3; j++) {
768 bezt1->vec[2][j],
769 bezt2->vec[0][j],
770 bezt2->vec[1][j],
771 points + j,
772 resolu,
773 sizeof(float[3]));
774 }
775
776 float point1[2], point2[2];
777 worldspace_to_screenspace(vc, points, point1);
778 const float len_vec1 = len_v2v2(point, point1);
779
780 if (min_dist > len_vec1) {
781 min_dist = len_vec1;
782 min_i = i;
783 param = 0;
784 }
785
786 for (int j = 0; j < resolu; j++) {
787 worldspace_to_screenspace(vc, points + 3 * (j + 1), point2);
788 get_updated_data_for_edge(point, point1, point2, i, j, &min_dist, &min_i, &param);
789 copy_v2_v2(point1, point2);
790 }
791
792 MEM_delete(points);
793 }
794 if (cd->min_dist > min_dist) {
795 cd->min_dist = min_dist;
796 cd->nurb = nu;
797 cd->bezt_index = min_i;
798 cd->parameter = param / resolu;
799 }
800 }
801 else {
802 float point1[2], point2[2];
803 worldspace_to_screenspace(vc, nu->bp->vec, point1);
804 for (int i = 0; i < end; i++) {
805 worldspace_to_screenspace(vc, (nu->bp + (i + 1) % nu->pntsu)->vec, point2);
806 get_updated_data_for_edge(point, point1, point2, i, 0, &min_dist, &min_i, &param);
807 copy_v2_v2(point1, point2);
808 }
809
810 if (cd->min_dist > min_dist) {
811 cd->min_dist = min_dist;
812 cd->nurb = nu;
813 cd->bp_index = min_i;
814 cd->parameter = param;
815 }
816 }
817}
818
819/* Update #CutData for all the Nurbs in the curve. */
821 const ListBaseT<Nurb> *nurbs,
822 const float point[2],
823 const float sel_dist,
824 CutData *cd)
825{
826 cd->min_dist = FLT_MAX;
827 for (Nurb &nu : *nurbs) {
828 update_cut_data_for_nurb(vc, cd, &nu, nu.resolu, point);
829 }
830
831 return cd->min_dist < sel_dist;
832}
833
834static CutData init_cut_data(const wmEvent *event)
835{
836 CutData cd{};
837 cd.bezt_index = 0;
838 cd.bp_index = 0;
839 cd.min_dist = FLT_MAX;
840 cd.parameter = 0.5f;
841 cd.has_prev = false;
842 cd.has_next = false;
843 cd.mval[0] = event->mval[0];
844 cd.mval[1] = event->mval[1];
845 return cd;
846}
847
848static bool insert_point_to_segment(const ViewContext *vc, const wmEvent *event)
849{
850 Curve *cu = id_cast<Curve *>(vc->obedit->data);
851 CutData cd = init_cut_data(event);
852 const float mval[2] = {float(event->mval[0]), float(event->mval[1])};
853 const float threshold_dist_px = ED_view3d_select_dist_px() * SEL_DIST_FACTOR;
854 const bool near_spline = update_cut_data_for_all_nurbs(
855 vc, BKE_curve_editNurbs_get(cu), mval, threshold_dist_px, &cd);
856
857 if (near_spline && !cd.nurb->hide) {
858 Nurb *nu = cd.nurb;
859 if (nu->type == CU_BEZIER) {
860 cd.min_dist = FLT_MAX;
861 /* Update cut data at a higher resolution for better accuracy. */
862 update_cut_data_for_nurb(vc, &cd, cd.nurb, 25, mval);
863
865 &nu->bezt[(cd.bezt_index + 1) % (nu->pntsu)],
866 cd.parameter,
867 cd.cut_loc);
868
869 insert_bezt_to_nurb(nu, &cd, cu);
870 }
871 else {
873 (nu->bp + cd.bp_index)->vec,
874 (nu->bp + (cd.bp_index + 1) % nu->pntsu)->vec,
875 cd.parameter);
876 insert_bp_to_nurb(nu, &cd, cu);
877 }
878 return true;
879 }
880
881 return false;
882}
883
889 Curve *cu, View3D *v3d, Nurb **r_nu, BezTriple **r_bezt, BPoint **r_bp)
890{
891 ListBaseT<Nurb> *nurbs = &cu->editnurb->nurbs;
892 BezTriple *bezt;
893 BPoint *bp;
894 int a;
895
896 *r_nu = nullptr;
897 *r_bezt = nullptr;
898 *r_bp = nullptr;
899
900 for (Nurb &nu : *nurbs) {
901 if (nu.type == CU_BEZIER) {
902 bezt = nu.bezt;
903 a = nu.pntsu;
904 while (a--) {
905 if (BEZT_ISSEL_ANY_HIDDENHANDLES(v3d, bezt)) {
906 if (*r_bezt || *r_bp) {
907 *r_bp = nullptr;
908 *r_bezt = nullptr;
909 return;
910 }
911 *r_bezt = bezt;
912 *r_nu = &nu;
913 }
914 bezt++;
915 }
916 }
917 else {
918 bp = nu.bp;
919 a = nu.pntsu * nu.pntsv;
920 while (a--) {
921 if (bp->f1 & SELECT) {
922 if (*r_bezt || *r_bp) {
923 *r_bp = nullptr;
924 *r_bezt = nullptr;
925 return;
926 }
927 *r_bp = bp;
928 *r_nu = &nu;
929 }
930 bp++;
931 }
932 }
933 }
934}
935
937 ListBaseT<Nurb> *nurbs,
938 Curve *cu,
939 const float disp_3d[3])
940{
941 int nu_index = 0;
942 for (Nurb &nu1 : *nurbs) {
943 if (nu1.type == CU_BEZIER) {
944 BezTriple *last_bezt = nu1.bezt + nu1.pntsu - 1;
945 const bool first_sel = BEZT_ISSEL_ANY(nu1.bezt);
946 const bool last_sel = BEZT_ISSEL_ANY(last_bezt) && nu1.pntsu > 1;
947 if (first_sel) {
948 if (last_sel) {
949 BezTriple *new_bezt = MEM_new_array_uninitialized<BezTriple>((nu1.pntsu + 2), __func__);
950 ED_curve_beztcpy(editnurb, new_bezt, nu1.bezt, 1);
951 ED_curve_beztcpy(editnurb, new_bezt + nu1.pntsu + 1, last_bezt, 1);
952 BEZT_DESEL_ALL(nu1.bezt);
953 BEZT_DESEL_ALL(last_bezt);
954 ED_curve_beztcpy(editnurb, new_bezt + 1, nu1.bezt, nu1.pntsu);
955
956 move_bezt_by_displacement(new_bezt, disp_3d);
957 move_bezt_by_displacement(new_bezt + nu1.pntsu + 1, disp_3d);
958 MEM_delete(nu1.bezt);
959 nu1.bezt = new_bezt;
960 nu1.pntsu += 2;
961
962 /* Set the new points selection. */
963 BEZT_DESEL_ALL(new_bezt);
964 BEZT_SEL_IDX(new_bezt, 0);
965
966 BEZT_DESEL_ALL(new_bezt + (nu1.pntsu - 1));
967 BEZT_SEL_IDX(new_bezt + (nu1.pntsu - 1), 2);
968 }
969 else {
970 BezTriple *new_bezt = MEM_new_array_uninitialized<BezTriple>((nu1.pntsu + 1), __func__);
971 ED_curve_beztcpy(editnurb, new_bezt, nu1.bezt, 1);
972 BEZT_DESEL_ALL(nu1.bezt);
973 ED_curve_beztcpy(editnurb, new_bezt + 1, nu1.bezt, nu1.pntsu);
974 move_bezt_by_displacement(new_bezt, disp_3d);
975 MEM_delete(nu1.bezt);
976 nu1.bezt = new_bezt;
977 nu1.pntsu++;
978
979 /* Set the new points selection. */
980 BEZT_DESEL_ALL(new_bezt);
981 BEZT_SEL_IDX(new_bezt, 0);
982 }
983 cu->actnu = nu_index;
984 cu->actvert = 0;
985 }
986 else if (last_sel) {
987 BezTriple *new_bezt = MEM_new_array_uninitialized<BezTriple>((nu1.pntsu + 1), __func__);
988 ED_curve_beztcpy(editnurb, new_bezt + nu1.pntsu, last_bezt, 1);
989 BEZT_DESEL_ALL(last_bezt);
990 ED_curve_beztcpy(editnurb, new_bezt, nu1.bezt, nu1.pntsu);
991 move_bezt_by_displacement(new_bezt + nu1.pntsu, disp_3d);
992 MEM_delete(nu1.bezt);
993 nu1.bezt = new_bezt;
994 nu1.pntsu++;
995 cu->actnu = nu_index;
996 cu->actvert = nu1.pntsu - 1;
997
998 /* Set the new points selection. */
999 BEZT_DESEL_ALL(new_bezt + (nu1.pntsu - 1));
1000 BEZT_SEL_IDX(new_bezt + (nu1.pntsu - 1), 2);
1001 }
1002 }
1003 else {
1004 BPoint *last_bp = nu1.bp + nu1.pntsu - 1;
1005 const bool first_sel = nu1.bp->f1 & SELECT;
1006 const bool last_sel = last_bp->f1 & SELECT && nu1.pntsu > 1;
1007 if (first_sel) {
1008 if (last_sel) {
1009 BPoint *new_bp = MEM_new_array_uninitialized<BPoint>((nu1.pntsu + 2), __func__);
1010 ED_curve_bpcpy(editnurb, new_bp, nu1.bp, 1);
1011 ED_curve_bpcpy(editnurb, new_bp + nu1.pntsu + 1, last_bp, 1);
1012 nu1.bp->f1 &= ~SELECT;
1013 last_bp->f1 &= ~SELECT;
1014 ED_curve_bpcpy(editnurb, new_bp + 1, nu1.bp, nu1.pntsu);
1015 add_v3_v3(new_bp->vec, disp_3d);
1016 add_v3_v3((new_bp + nu1.pntsu + 1)->vec, disp_3d);
1017 MEM_delete(nu1.bp);
1018 nu1.bp = new_bp;
1019 nu1.pntsu += 2;
1020 }
1021 else {
1022 BPoint *new_bp = MEM_new_array_uninitialized<BPoint>((nu1.pntsu + 1), __func__);
1023 ED_curve_bpcpy(editnurb, new_bp, nu1.bp, 1);
1024 nu1.bp->f1 &= ~SELECT;
1025 ED_curve_bpcpy(editnurb, new_bp + 1, nu1.bp, nu1.pntsu);
1026 add_v3_v3(new_bp->vec, disp_3d);
1027 MEM_delete(nu1.bp);
1028 nu1.bp = new_bp;
1029 nu1.pntsu++;
1030 }
1032 cu->actnu = nu_index;
1033 cu->actvert = 0;
1034 }
1035 else if (last_sel) {
1036 BPoint *new_bp = MEM_new_array_uninitialized<BPoint>((nu1.pntsu + 1), __func__);
1037 ED_curve_bpcpy(editnurb, new_bp, nu1.bp, nu1.pntsu);
1038 ED_curve_bpcpy(editnurb, new_bp + nu1.pntsu, last_bp, 1);
1039 last_bp->f1 &= ~SELECT;
1040 ED_curve_bpcpy(editnurb, new_bp, nu1.bp, nu1.pntsu);
1041 add_v3_v3((new_bp + nu1.pntsu)->vec, disp_3d);
1042 MEM_delete(nu1.bp);
1043 nu1.bp = new_bp;
1044 nu1.pntsu++;
1046 cu->actnu = nu_index;
1047 cu->actvert = nu1.pntsu - 1;
1048 }
1050 }
1051 nu_index++;
1052 }
1053}
1054
1058static void deselect_all_center_vertices(ListBaseT<Nurb> *nurbs)
1059{
1060 for (Nurb &nu1 : *nurbs) {
1061 if (nu1.pntsu > 1) {
1062 int start, end;
1063 if (is_cyclic(&nu1)) {
1064 start = 0;
1065 end = nu1.pntsu;
1066 }
1067 else {
1068 start = 1;
1069 end = nu1.pntsu - 1;
1070 }
1071 for (int i = start; i < end; i++) {
1072 if (nu1.type == CU_BEZIER) {
1073 BEZT_DESEL_ALL(nu1.bezt + i);
1074 }
1075 else {
1076 (nu1.bp + i)->f1 &= ~SELECT;
1077 }
1078 }
1079 }
1080 }
1081}
1082
1083static bool is_last_bezt(const Nurb *nu, const BezTriple *bezt)
1084{
1085 return nu->pntsu > 1 && nu->bezt + nu->pntsu - 1 == bezt && !is_cyclic(nu);
1086}
1087
1092 const wmEvent *event,
1093 const eBezTriple_Handle extrude_handle)
1094{
1095 Curve *cu = id_cast<Curve *>(vc->obedit->data);
1096 ListBaseT<Nurb> *nurbs = BKE_curve_editNurbs_get(cu);
1097 float center[3] = {0.0f, 0.0f, 0.0f};
1099 bool sel_exists = get_selected_center(nurbs, true, false, center);
1100
1101 float location[3];
1102 if (sel_exists) {
1103 mul_v3_m4v3(location, vc->obedit->object_to_world().ptr(), center);
1104 }
1105 else {
1106 copy_v3_v3(location, vc->scene->cursor.location);
1107 }
1108
1109 ED_view3d_win_to_3d_int(vc->v3d, vc->region, location, event->mval, location);
1110
1111 update_location_for_2d_curve(vc, location);
1112 EditNurb *editnurb = cu->editnurb;
1113
1114 if (sel_exists) {
1115 float disp_3d[3];
1116 sub_v3_v3v3(disp_3d, location, center);
1117 /* Reimplemented due to unexpected behavior for extrusion of 2-point spline. */
1118 extrude_vertices_from_selected_endpoints(editnurb, nurbs, cu, disp_3d);
1119 }
1120 else {
1121 Nurb *old_last_nu = editnurb->nurbs.last();
1122 ed_editcurve_addvert(cu, editnurb, vc->v3d, location);
1123 Nurb *new_last_nu = editnurb->nurbs.last();
1124
1125 if (old_last_nu != new_last_nu) {
1127 new_last_nu,
1128 new_last_nu->bezt ?
1129 static_cast<const void *>(new_last_nu->bezt) :
1130 static_cast<const void *>(new_last_nu->bp));
1131 new_last_nu->flagu &= ~CU_NURB_CYCLIC;
1132 }
1133 }
1134
1136 if (bezt) {
1137 bezt->h1 = extrude_handle;
1138 bezt->h2 = extrude_handle;
1139 }
1140 }
1142}
1143
1148 wmOperator *op,
1149 const wmEvent *event,
1150 const float sel_dist)
1151{
1152 Curve *cu = id_cast<Curve *>(vc->obedit->data);
1153 ListBaseT<Nurb> *nurbs = BKE_curve_editNurbs_get(cu);
1154 CutData cd = init_cut_data(event);
1155
1156 const float mval[2] = {float(event->mval[0]), float(event->mval[1])};
1157 const bool nearby = update_cut_data_for_all_nurbs(vc, nurbs, mval, sel_dist, &cd);
1158
1159 if (nearby) {
1160 if (cd.nurb && (cd.nurb->type == CU_BEZIER) && RNA_boolean_get(op->ptr, "move_segment")) {
1161 MoveSegmentData *seg_data;
1162 CurvePenData *cpd = static_cast<CurvePenData *>(op->customdata);
1163 cpd->msd = seg_data = MEM_new_zeroed<MoveSegmentData>(__func__);
1164 seg_data->bezt_index = cd.bezt_index;
1165 seg_data->nu = cd.nurb;
1166 seg_data->t = cd.parameter;
1167 }
1168 return true;
1169 }
1170 return false;
1171}
1172
1173static void move_segment(const ViewContext *vc, MoveSegmentData *seg_data, const wmEvent *event)
1174{
1175 Nurb *nu = seg_data->nu;
1176 BezTriple *bezt1 = nu->bezt + seg_data->bezt_index;
1177 BezTriple *bezt2 = BKE_nurb_bezt_get_next(nu, bezt1);
1178
1179 int h1 = 2, h2 = 0;
1180 if (bezt1->hide) {
1181 if (bezt2->hide) {
1182 return;
1183 }
1184 /*
1185 * Swap bezt1 and bezt2 in all calculations if only bezt2 is visible.
1186 * (The first point needs to be visible for the calculations of the second point to be valid)
1187 */
1188 BezTriple *temp_bezt = bezt2;
1189 bezt2 = bezt1;
1190 bezt1 = temp_bezt;
1191 h1 = 0;
1192 h2 = 2;
1193 }
1194
1195 const float t = max_ff(min_ff(seg_data->t, 0.9f), 0.1f);
1196 const float t_sq = t * t;
1197 const float t_cu = t_sq * t;
1198 const float one_minus_t = 1 - t;
1199 const float one_minus_t_sq = one_minus_t * one_minus_t;
1200 const float one_minus_t_cu = one_minus_t_sq * one_minus_t;
1201
1202 float mouse_3d[3];
1203 float depth[3];
1204 /* Use the center of the spline segment as depth. */
1205 get_bezier_interpolated_point(bezt1, bezt2, t, depth);
1206 screenspace_to_worldspace_int(vc, event->mval, depth, mouse_3d);
1207
1208 /*
1209 * Equation of Bezier Curve
1210 * => B(t) = (1-t)^3 * P0 + 3(1-t)^2 * t * P1 + 3(1-t) * t^2 * P2 + t^3 * P3
1211 *
1212 * Mouse location (Say Pm) should satisfy this equation.
1213 * Therefore => (1/t - 1) * P1 + P2 = (Pm - (1 - t)^3 * P0 - t^3 * P3) / [3 * (1 - t) * t^2] = k1
1214 * (in code)
1215 *
1216 * Another constraint is required to identify P1 and P2.
1217 * The constraint used is that the vector between P1 and P2 doesn't change.
1218 * Therefore => P1 - P2 = k2
1219 *
1220 * From the two equations => P1 = t(k1 + k2) and P2 = P1 - K2
1221 */
1222
1223 float k1[3];
1224 const float denom = 3.0f * one_minus_t * t_sq;
1225 k1[0] = (mouse_3d[0] - one_minus_t_cu * bezt1->vec[1][0] - t_cu * bezt2->vec[1][0]) / denom;
1226 k1[1] = (mouse_3d[1] - one_minus_t_cu * bezt1->vec[1][1] - t_cu * bezt2->vec[1][1]) / denom;
1227 k1[2] = (mouse_3d[2] - one_minus_t_cu * bezt1->vec[1][2] - t_cu * bezt2->vec[1][2]) / denom;
1228
1229 float k2[3];
1230 sub_v3_v3v3(k2, bezt1->vec[h1], bezt2->vec[h2]);
1231
1232 if (!bezt1->hide) {
1233 /* P1 = t(k1 + k2) */
1234 add_v3_v3v3(bezt1->vec[h1], k1, k2);
1235 mul_v3_fl(bezt1->vec[h1], t);
1236
1237 remove_handle_movement_constraints(bezt1, true, true);
1238
1239 /* Move opposite handle as well if type is align. */
1240 if (bezt1->h1 == HD_ALIGN) {
1241 float handle_vec[3];
1242 sub_v3_v3v3(handle_vec, bezt1->vec[1], bezt1->vec[h1]);
1243 normalize_v3_length(handle_vec, len_v3v3(bezt1->vec[1], bezt1->vec[h2]));
1244 add_v3_v3v3(bezt1->vec[h2], bezt1->vec[1], handle_vec);
1245 }
1246 }
1247
1248 if (!bezt2->hide) {
1249 /* P2 = P1 - K2 */
1250 sub_v3_v3v3(bezt2->vec[h2], bezt1->vec[h1], k2);
1251
1252 remove_handle_movement_constraints(bezt2, true, true);
1253
1254 /* Move opposite handle as well if type is align. */
1255 if (bezt2->h2 == HD_ALIGN) {
1256 float handle_vec[3];
1257 sub_v3_v3v3(handle_vec, bezt2->vec[1], bezt2->vec[h2]);
1258 normalize_v3_length(handle_vec, len_v3v3(bezt2->vec[1], bezt2->vec[h1]));
1259 add_v3_v3v3(bezt2->vec[h1], bezt2->vec[1], handle_vec);
1260 }
1261 }
1262}
1263
1268{
1269 if (bezt->h1 != HD_FREE || bezt->h2 != HD_FREE) {
1270 bezt->h1 = bezt->h2 = HD_FREE;
1271 }
1272 else {
1273 bezt->h1 = bezt->h2 = HD_ALIGN;
1274 }
1275}
1276
1280static void toggle_sel_bezt_free_align_handles(ListBaseT<Nurb> *nurbs)
1281{
1282 FOREACH_SELECTED_BEZT_BEGIN (bezt, *nurbs) {
1284 }
1286}
1287
1291static bool delete_point_under_mouse(const ViewContext *vc, const wmEvent *event)
1292{
1293 BezTriple *bezt = nullptr;
1294 BPoint *bp = nullptr;
1295 Nurb *nu = nullptr;
1296 int temp = 0;
1297 Curve *cu = id_cast<Curve *>(vc->obedit->data);
1298 EditNurb *editnurb = cu->editnurb;
1299 ListBaseT<Nurb> *nurbs = BKE_curve_editNurbs_get(cu);
1300 const float mouse_point[2] = {float(event->mval[0]), float(event->mval[1])};
1301
1302 get_closest_vertex_to_point_in_nurbs(vc, nurbs, mouse_point, &nu, &bezt, &bp, &temp);
1303 const bool found_point = nu != nullptr;
1304
1305 bool deleted = false;
1306 if (found_point) {
1308 if (nu) {
1309 if (nu->type == CU_BEZIER) {
1310 BezTriple *next_bezt = BKE_nurb_bezt_get_next(nu, bezt);
1311 BezTriple *prev_bezt = BKE_nurb_bezt_get_prev(nu, bezt);
1312 if (next_bezt && prev_bezt) {
1313 const int bez_index = BKE_curve_nurb_vert_index_get(nu, bezt);
1314 const uint span_step[2] = {uint(bez_index), uint(bez_index)};
1315 ed_dissolve_bez_segment(prev_bezt, next_bezt, nu, cu, 1, span_step);
1316 }
1317 delete_bezt_from_nurb(bezt, nu, editnurb);
1318 }
1319 else {
1320 delete_bp_from_nurb(bp, nu, editnurb);
1321 }
1322
1323 if (nu->pntsu == 0) {
1324 delete_nurb(cu, nu);
1325 nu = nullptr;
1326 }
1327 deleted = true;
1328 cu->actvert = CU_ACT_NONE;
1329 }
1330 }
1331
1332 if (nu && nu->type == CU_BEZIER) {
1334 }
1335
1336 return deleted;
1337}
1338
1339static void move_adjacent_handle(const ViewContext *vc,
1340 const wmEvent *event,
1341 ListBaseT<Nurb> *nurbs)
1342{
1343 FOREACH_SELECTED_BEZT_BEGIN (bezt, *nurbs) {
1344 BezTriple *adj_bezt;
1345 int bezt_idx;
1346 if (nu.pntsu == 1) {
1347 continue;
1348 }
1349 if (nu.bezt == bezt) {
1350 adj_bezt = BKE_nurb_bezt_get_next(&nu, bezt);
1351 bezt_idx = 0;
1352 }
1353 else if (nu.bezt + nu.pntsu - 1 == bezt) {
1354 adj_bezt = BKE_nurb_bezt_get_prev(&nu, bezt);
1355 bezt_idx = 2;
1356 }
1357 else {
1358 if (BEZT_ISSEL_IDX(bezt, 0)) {
1359 adj_bezt = BKE_nurb_bezt_get_prev(&nu, bezt);
1360 bezt_idx = 2;
1361 }
1362 else if (BEZT_ISSEL_IDX(bezt, 2)) {
1363 adj_bezt = BKE_nurb_bezt_get_next(&nu, bezt);
1364 bezt_idx = 0;
1365 }
1366 else {
1367 continue;
1368 }
1369 }
1370 adj_bezt->h1 = adj_bezt->h2 = HD_FREE;
1371
1372 int2 displacement = int2(event->xy) - int2(event->prev_xy);
1373 const float disp_fl[2] = {float(displacement[0]), float(displacement[1])};
1375 vc, adj_bezt, bezt_idx, disp_fl, 0.0f, false, false);
1377 }
1379}
1380
1385 Nurb *sel_nu,
1386 BezTriple *sel_bezt,
1387 BPoint *sel_bp)
1388{
1389 if (sel_bezt || (sel_bp && sel_nu->pntsu > 2)) {
1390 const bool is_bezt_endpoint = ((sel_nu->type == CU_BEZIER) &&
1391 ELEM(sel_bezt, sel_nu->bezt, sel_nu->bezt + sel_nu->pntsu - 1));
1392 const bool is_bp_endpoint = ((sel_nu->type != CU_BEZIER) &&
1393 ELEM(sel_bp, sel_nu->bp, sel_nu->bp + sel_nu->pntsu - 1));
1394 if (!(is_bezt_endpoint || is_bp_endpoint)) {
1395 return false;
1396 }
1397
1398 Nurb *nu = nullptr;
1399 BezTriple *bezt = nullptr;
1400 BPoint *bp = nullptr;
1401 Curve *cu = id_cast<Curve *>(vc->obedit->data);
1402 int bezt_idx;
1403 const float mval_fl[2] = {float(vc->mval[0]), float(vc->mval[1])};
1404
1406 vc, &(cu->editnurb->nurbs), mval_fl, &nu, &bezt, &bp, &bezt_idx);
1407
1408 if (nu == sel_nu &&
1409 ((nu->type == CU_BEZIER && bezt != sel_bezt &&
1410 ELEM(bezt, nu->bezt, nu->bezt + nu->pntsu - 1) && bezt_idx == 1) ||
1411 (nu->type != CU_BEZIER && bp != sel_bp && ELEM(bp, nu->bp, nu->bp + nu->pntsu - 1))))
1412 {
1413 View3D *v3d = vc->v3d;
1414 ListBaseT<Nurb> *nurbs = object_editcurve_get(vc->obedit);
1415 curve_toggle_cyclic(v3d, nurbs, 0);
1416 return true;
1417 }
1418 }
1419 return false;
1420}
1421
1422static void init_selected_bezt_handles(ListBaseT<Nurb> *nurbs)
1423{
1424 FOREACH_SELECTED_BEZT_BEGIN (bezt, *nurbs) {
1425 bezt->h1 = bezt->h2 = HD_ALIGN;
1426 copy_v3_v3(bezt->vec[0], bezt->vec[1]);
1427 copy_v3_v3(bezt->vec[2], bezt->vec[1]);
1428 BEZT_DESEL_ALL(bezt);
1429 BEZT_SEL_IDX(bezt, is_last_bezt(&nu, bezt) ? 2 : 0);
1430 }
1432}
1433
1434static void toggle_select_bezt(BezTriple *bezt, const int bezt_idx, Curve *cu, Nurb *nu)
1435{
1436 if (BEZT_ISSEL_IDX(bezt, bezt_idx)) {
1437 BEZT_DESEL_IDX(bezt, bezt_idx);
1438 }
1439 else {
1440 BEZT_SEL_IDX(bezt, bezt_idx);
1441 }
1442
1443 if (BEZT_ISSEL_ANY(bezt)) {
1444 BKE_curve_nurb_vert_active_set(cu, nu, bezt);
1445 }
1446}
1447
1448static void toggle_select_bp(BPoint *bp, Curve *cu, Nurb *nu)
1449{
1450 if (bp->f1 & SELECT) {
1451 bp->f1 &= ~SELECT;
1452 }
1453 else {
1454 bp->f1 |= SELECT;
1456 }
1457}
1458
1459static void toggle_handle_types(BezTriple *bezt, int bezt_idx, CurvePenData *cpd)
1460{
1461 if (bezt_idx == 0) {
1462 if (bezt->h1 == HD_VECT) {
1463 bezt->h1 = bezt->h2 = HD_AUTO;
1464 }
1465 else {
1466 bezt->h1 = HD_VECT;
1467 if (bezt->h2 != HD_VECT) {
1468 bezt->h2 = HD_FREE;
1469 }
1470 }
1471 cpd->changed = true;
1472 }
1473 else if (bezt_idx == 2) {
1474 if (bezt->h2 == HD_VECT) {
1475 bezt->h1 = bezt->h2 = HD_AUTO;
1476 }
1477 else {
1478 bezt->h2 = HD_VECT;
1479 if (bezt->h1 != HD_VECT) {
1480 bezt->h1 = HD_FREE;
1481 }
1482 }
1483 cpd->changed = true;
1484 }
1485}
1486
1487static void cycle_handles(BezTriple *bezt)
1488{
1489 if (bezt->h1 == HD_AUTO) {
1490 bezt->h1 = bezt->h2 = HD_VECT;
1491 }
1492 else if (bezt->h1 == HD_VECT) {
1493 bezt->h1 = bezt->h2 = HD_ALIGN;
1494 }
1495 else if (bezt->h1 == HD_ALIGN) {
1496 bezt->h1 = bezt->h2 = HD_FREE;
1497 }
1498 else {
1499 bezt->h1 = bezt->h2 = HD_AUTO;
1500 }
1501}
1502
1503enum {
1509};
1510
1512{
1513 static const EnumPropertyItem modal_items[] = {
1515 "FREE_ALIGN_TOGGLE",
1516 0,
1517 "Free-Align Toggle",
1518 "Move handle of newly added point freely"},
1520 "MOVE_ADJACENT",
1521 0,
1522 "Move Adjacent Handle",
1523 "Move the closer handle of the adjacent vertex"},
1525 "MOVE_ENTIRE",
1526 0,
1527 "Move Entire Point",
1528 "Move the entire point using its handles"},
1530 "LINK_HANDLES",
1531 0,
1532 "Link Handles",
1533 "Mirror the movement of one handle onto the other"},
1535 "LOCK_ANGLE",
1536 0,
1537 "Lock Angle",
1538 "Move the handle along its current angle"},
1539 {0, nullptr, 0, nullptr, nullptr},
1540 };
1541
1542 wmKeyMap *keymap = WM_modalkeymap_find(keyconf, "Curve Pen Modal Map");
1543
1544 /* This function is called for each space-type, only needs to add map once. */
1545 if (keymap && keymap->modal_items) {
1546 return nullptr;
1547 }
1548
1549 keymap = WM_modalkeymap_ensure(keyconf, "Curve Pen Modal Map", modal_items);
1550
1551 WM_modalkeymap_assign(keymap, "CURVE_OT_pen");
1552
1553 return keymap;
1554}
1555
1557{
1558 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
1559 Object *obedit = CTX_data_edit_object(C);
1560
1561 ViewContext vc = ED_view3d_viewcontext_init(C, depsgraph);
1562 Curve *cu = id_cast<Curve *>(vc.obedit->data);
1563 ListBaseT<Nurb> *nurbs = &cu->editnurb->nurbs;
1564 const float threshold_dist_px = ED_view3d_select_dist_px() * SEL_DIST_FACTOR;
1565
1566 BezTriple *bezt = nullptr;
1567 BPoint *bp = nullptr;
1568 Nurb *nu = nullptr;
1569
1570 const SelectPick_Params params = {
1571 /*sel_op*/ SEL_OP_SET,
1572 /*deselect_all*/ false,
1573 };
1574
1576
1577 /* Distance threshold for mouse clicks to affect the spline or its points */
1578 const float mval_fl[2] = {float(event->mval[0]), float(event->mval[1])};
1579
1580 const bool extrude_point = RNA_boolean_get(op->ptr, "extrude_point");
1581 const bool delete_point = RNA_boolean_get(op->ptr, "delete_point");
1582 const bool insert_point = RNA_boolean_get(op->ptr, "insert_point");
1583 const bool move_seg = RNA_boolean_get(op->ptr, "move_segment");
1584 const bool select_point = RNA_boolean_get(op->ptr, "select_point");
1585 const bool move_point = RNA_boolean_get(op->ptr, "move_point");
1586 const bool close_spline = RNA_boolean_get(op->ptr, "close_spline");
1587 const bool toggle_vector = RNA_boolean_get(op->ptr, "toggle_vector");
1588 const bool cycle_handle_type = RNA_boolean_get(op->ptr, "cycle_handle_type");
1589 const int close_spline_method = RNA_enum_get(op->ptr, "close_spline_method");
1590 const eBezTriple_Handle extrude_handle = eBezTriple_Handle(
1591 RNA_enum_get(op->ptr, "extrude_handle"));
1592
1593 CurvePenData *cpd;
1594 if (op->customdata == nullptr) {
1595 op->customdata = cpd = MEM_new_zeroed<CurvePenData>(__func__);
1596 }
1597 else {
1598 cpd = static_cast<CurvePenData *>(op->customdata);
1599 cpd->select_multi = event->modifier == KM_SHIFT;
1600 }
1601
1602 if (event->type == EVT_MODAL_MAP) {
1603 if (cpd->msd == nullptr) {
1604 if (event->val == PEN_MODAL_FREE_ALIGN_TOGGLE) {
1606 cpd->link_handles = false;
1607 }
1608 else if (event->val == PEN_MODAL_LINK_HANDLES) {
1609 cpd->link_handles = !cpd->link_handles;
1610 if (cpd->link_handles) {
1611 move_all_selected_points(&vc, event, cpd, nurbs, false);
1612 }
1613 }
1614 else if (event->val == PEN_MODAL_MOVE_ENTIRE) {
1615 cpd->move_entire = !cpd->move_entire;
1616 }
1617 else if (event->val == PEN_MODAL_MOVE_ADJACENT) {
1618 cpd->move_adjacent = !cpd->move_adjacent;
1619 }
1620 else if (event->val == PEN_MODAL_LOCK_ANGLE) {
1621 cpd->lock_angle = !cpd->lock_angle;
1622 }
1623 }
1624 else {
1625 if (event->val == PEN_MODAL_FREE_ALIGN_TOGGLE) {
1626 BezTriple *bezt1 = cpd->msd->nu->bezt + cpd->msd->bezt_index;
1627 BezTriple *bezt2 = BKE_nurb_bezt_get_next(cpd->msd->nu, bezt1);
1630 }
1631 }
1632 }
1633
1634 if (ISMOUSE_MOTION(event->type)) {
1635 /* Check if dragging */
1636 if (!cpd->dragging && WM_event_drag_test(event, event->prev_press_xy)) {
1637 cpd->dragging = true;
1638
1639 if (cpd->new_point) {
1641 }
1642 }
1643
1644 if (cpd->dragging) {
1645 if (cpd->spline_nearby && move_seg && cpd->msd != nullptr) {
1646 MoveSegmentData *seg_data = cpd->msd;
1647 move_segment(&vc, seg_data, event);
1648 cpd->changed = true;
1649 if (seg_data->nu && seg_data->nu->type == CU_BEZIER) {
1650 BKE_nurb_handles_calc(seg_data->nu);
1651 }
1652 }
1653 else if (cpd->move_adjacent) {
1654 move_adjacent_handle(&vc, event, nurbs);
1655 cpd->changed = true;
1656 }
1657 else if (cpd->new_point || (move_point && !cpd->spline_nearby && cpd->found_point)) {
1658 /* Move only the bezt handles if it's a new point. */
1659 move_all_selected_points(&vc, event, cpd, nurbs, cpd->new_point);
1660 cpd->changed = true;
1661 }
1662 }
1663 }
1664 else if (ELEM(event->type, LEFTMOUSE)) {
1665 if (ELEM(event->val, KM_RELEASE, KM_DBL_CLICK)) {
1666 if (delete_point && !cpd->new_point && !cpd->dragging) {
1667 if (ED_curve_editnurb_select_pick(C, event->mval, threshold_dist_px, params)) {
1668 cpd->changed = delete_point_under_mouse(&vc, event);
1669 }
1670 }
1671
1672 /* Close spline on Click, if enabled. */
1673 if (!cpd->changed && close_spline && close_spline_method == ON_CLICK && cpd->found_point &&
1674 !cpd->dragging)
1675 {
1676 if (cpd->nu && !is_cyclic(cpd->nu)) {
1677 copy_v2_v2_int(vc.mval, event->mval);
1678 cpd->changed = make_cyclic_if_endpoints(&vc, cpd->nu, cpd->bezt, cpd->bp);
1679 }
1680 }
1681
1682 if (!cpd->changed && (insert_point || extrude_point) && cpd->spline_nearby && !cpd->dragging)
1683 {
1684 if (insert_point) {
1685 insert_point_to_segment(&vc, event);
1686 cpd->new_point = true;
1687 cpd->changed = true;
1688 }
1689 else if (extrude_point) {
1690 extrude_points_from_selected_vertices(&vc, event, extrude_handle);
1691 cpd->changed = true;
1692 }
1693 }
1694
1695 if (!cpd->changed && toggle_vector) {
1696 int bezt_idx;
1697 get_closest_vertex_to_point_in_nurbs(&vc, nurbs, mval_fl, &nu, &bezt, &bp, &bezt_idx);
1698 if (bezt) {
1699 if (bezt_idx == 1 && cycle_handle_type) {
1700 cycle_handles(bezt);
1701 cpd->changed = true;
1702 }
1703 else {
1704 toggle_handle_types(bezt, bezt_idx, cpd);
1705 }
1706
1707 if (nu && nu->type == CU_BEZIER) {
1709 }
1710 }
1711 }
1712
1713 if (!cpd->selection_made && !cpd->changed) {
1714 if (cpd->select_multi) {
1715 int bezt_idx;
1716 get_closest_vertex_to_point_in_nurbs(&vc, nurbs, mval_fl, &nu, &bezt, &bp, &bezt_idx);
1717 if (bezt) {
1718 toggle_select_bezt(bezt, bezt_idx, cu, nu);
1719 }
1720 else if (bp) {
1721 toggle_select_bp(bp, cu, nu);
1722 }
1723 else {
1725 }
1726 }
1727 else if (select_point) {
1728 ED_curve_editnurb_select_pick(C, event->mval, threshold_dist_px, params);
1729 }
1730 }
1731
1732 if (cpd->msd != nullptr) {
1733 MEM_delete(cpd->msd);
1734 }
1735 MEM_delete(cpd);
1737 }
1738 }
1739
1742 DEG_id_tag_update(obedit->data, 0);
1743
1744 return ret;
1745}
1746
1748{
1749 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
1750 ViewContext vc = ED_view3d_viewcontext_init(C, depsgraph);
1751 Curve *cu = id_cast<Curve *>(vc.obedit->data);
1752 ListBaseT<Nurb> *nurbs = &cu->editnurb->nurbs;
1753
1754 BezTriple *bezt = nullptr;
1755 BPoint *bp = nullptr;
1756 Nurb *nu = nullptr;
1757
1758 CurvePenData *cpd;
1759 op->customdata = cpd = MEM_new_zeroed<CurvePenData>(__func__);
1760
1761 /* Distance threshold for mouse clicks to affect the spline or its points */
1762 const float mval_fl[2] = {float(event->mval[0]), float(event->mval[1])};
1763 const float threshold_dist_px = ED_view3d_select_dist_px() * SEL_DIST_FACTOR;
1764
1765 const bool extrude_point = RNA_boolean_get(op->ptr, "extrude_point");
1766 const bool insert_point = RNA_boolean_get(op->ptr, "insert_point");
1767 const bool move_seg = RNA_boolean_get(op->ptr, "move_segment");
1768 const bool move_point = RNA_boolean_get(op->ptr, "move_point");
1769 const bool close_spline = RNA_boolean_get(op->ptr, "close_spline");
1770 const int close_spline_method = RNA_enum_get(op->ptr, "close_spline_method");
1771 const eBezTriple_Handle extrude_handle = eBezTriple_Handle(
1772 RNA_enum_get(op->ptr, "extrude_handle"));
1773
1774 if (ELEM(event->type, LEFTMOUSE) && ELEM(event->val, KM_PRESS, KM_DBL_CLICK)) {
1775 /* Get the details of points selected at the start of the operation.
1776 * Used for closing the spline when endpoints are clicked consecutively and for selecting a
1777 * single point. */
1778 get_first_selected_point(cu, vc.v3d, &nu, &bezt, &bp);
1779 cpd->nu = nu;
1780 cpd->bezt = bezt;
1781 cpd->bp = bp;
1782
1783 /* Get the details of the vertex closest to the mouse at the start of the operation. */
1784 Nurb *nu1;
1785 BezTriple *bezt1;
1786 BPoint *bp1;
1787 int bezt_idx = 0;
1789 &vc, nurbs, mval_fl, &nu1, &bezt1, &bp1, &bezt_idx);
1790
1791 if (move_point && nu1 && !nu1->hide &&
1792 (bezt || (bezt1 && !BEZT_ISSEL_IDX(bezt1, bezt_idx)) || (bp1 && !(bp1->f1 & SELECT))))
1793 {
1794 /* Select the closest bezt or bp. */
1796 if (bezt1) {
1797 BEZT_SEL_IDX(bezt1, bezt_idx);
1798 BKE_curve_nurb_vert_active_set(cu, nu1, bezt1);
1799 }
1800 else if (bp1) {
1801 bp1->f1 |= SELECT;
1802 BKE_curve_nurb_vert_active_set(cu, nu1, bp1);
1803 }
1804
1805 cpd->selection_made = true;
1806 }
1807 if (cpd->found_point) {
1808 /* Close the spline on press. */
1809 if (close_spline && close_spline_method == ON_PRESS && cpd->nu && !is_cyclic(cpd->nu)) {
1810 copy_v2_v2_int(vc.mval, event->mval);
1812 &vc, cpd->nu, cpd->bezt, cpd->bp);
1813 }
1814 }
1815 else if (!cpd->changed) {
1816 if (is_spline_nearby(&vc, op, event, threshold_dist_px)) {
1817 cpd->spline_nearby = true;
1818
1819 /* If move segment is disabled, then insert point on key press and set
1820 * "new_point" to true so that the new point's handles can be controlled. */
1821 if (insert_point && !move_seg) {
1822 insert_point_to_segment(&vc, event);
1823 cpd->new_point = cpd->changed = cpd->link_handles = true;
1824 }
1825 }
1826 else if (extrude_point) {
1827 extrude_points_from_selected_vertices(&vc, event, extrude_handle);
1828 cpd->new_point = cpd->changed = cpd->link_handles = true;
1829 }
1830 }
1831 }
1833
1835}
1836
1838{
1839 /* identifiers */
1840 ot->name = "Curve Pen";
1841 ot->idname = "CURVE_OT_pen";
1842 ot->description = "Construct and edit splines";
1843
1844 /* API callbacks. */
1846 ot->modal = curve_pen_modal;
1848
1849 /* flags */
1850 ot->flag = OPTYPE_UNDO;
1851
1852 /* properties */
1854
1856 "extrude_point",
1857 false,
1858 "Extrude Point",
1859 "Add a point connected to the last selected point");
1860 RNA_def_enum(ot->srna,
1861 "extrude_handle",
1863 HD_VECT,
1864 "Extrude Handle Type",
1865 "Type of the extruded handle");
1866 RNA_def_boolean(ot->srna, "delete_point", false, "Delete Point", "Delete an existing point");
1868 ot->srna, "insert_point", false, "Insert Point", "Insert Point into a curve segment");
1870 ot->srna, "move_segment", false, "Move Segment", "Move an existing curve segment");
1872 ot->srna, "select_point", false, "Select Point", "Select a point or its handles");
1873 RNA_def_boolean(ot->srna, "move_point", false, "Move Point", "Move a point or its handles");
1875 "close_spline",
1876 true,
1877 "Close Spline",
1878 "Make a spline cyclic by clicking endpoints");
1879 RNA_def_enum(ot->srna,
1880 "close_spline_method",
1882 OFF,
1883 "Close Spline Method",
1884 "The condition for close spline to activate");
1886 ot->srna, "toggle_vector", false, "Toggle Vector", "Toggle between Vector and Auto handles");
1888 "cycle_handle_type",
1889 false,
1890 "Cycle Handle Type",
1891 "Cycle between all four handle types");
1892}
1893
1894} // namespace blender
#define CU_IS_2D(cu)
Definition BKE_curve.hh:93
#define SELECT
Definition BKE_mask.hh:19
#define CU_ACT_NONE
#define BEZT_DESEL_IDX(bezt, i)
#define BEZT_ISSEL_IDX(bezt, i)
#define BEZT_ISSEL_ANY_HIDDENHANDLES(v3d, bezt)
#define BEZT_ISSEL_ANY(bezt)
#define BEZT_DESEL_ALL(bezt)
#define BEZT_SEL_IDX(bezt, i)
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define NC_GEOM
Definition WM_types.hh:406
#define ND_DATA
Definition WM_types.hh:552
#define ND_SELECT
Definition WM_types.hh:551
#define ELEM(...)
#define FOREACH_SELECTED_BEZT_END
#define SEL_DIST_FACTOR
#define FOREACH_SELECTED_BEZT_BEGIN(bezt, nurbs)
#define FLT_MAX
float distance(float, float) RET
BMesh Iterators.
bool isect_ray_plane_v3_factor(const float ray_origin[3], const float ray_direction[3], const float plane_co[3], const float plane_no[3], float *r_lambda)
MINLINE float min_ff(float a, float b)
MINLINE void madd_v3_v3v3fl(float r[3], const float a[3], const float b[3], float f)
static void calculate_new_bezier_point(const float point_prev[3], float handle_prev[3], float new_left_handle[3], float new_right_handle[3], float handle_next[3], const float point_next[3], const float parameter)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BKE_curve_nurb_vert_active_set(Curve *cu, const Nurb *nu, const void *vert)
Definition curve.cc:5048
static bool delete_point_under_mouse(const ViewContext *vc, const wmEvent *event)
@ V3D_PROJ_RET_OK
Definition ED_view3d.hh:273
static void get_first_selected_point(Curve *cu, View3D *v3d, Nurb **r_nu, BezTriple **r_bezt, BPoint **r_bp)
void ED_curve_bpcpy(EditNurb *editnurb, BPoint *dst, BPoint *src, int count)
int BKE_curve_nurb_vert_index_get(const Nurb *nu, const void *vert)
Definition curve.cc:5037
@ CURVE_HANDLE_SELECTED
MINLINE void copy_v2_v2_int(int r[2], const int a[2])
int RNA_enum_get(PointerRNA *ptr, const char *name)
static void toggle_sel_bezt_free_align_handles(ListBaseT< Nurb > *nurbs)
wmKeyMap * WM_modalkeymap_find(wmKeyConfig *keyconf, const char *idname)
Definition wm_keymap.cc:999
ViewContext ED_view3d_viewcontext_init(bContext *C, Depsgraph *depsgraph)
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
static void move_bezt_by_displacement(BezTriple *bezt, const float disp_3d[3])
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
static bool get_closest_vertex_to_point_in_nurbs(const ViewContext *vc, const ListBaseT< Nurb > *nurbs, const float point[2], Nurb **r_nu, BezTriple **r_bezt, BPoint **r_bp, int *r_bezt_idx)
MINLINE void add_v2_v2v2(float r[2], const float a[2], const float b[2])
static void toggle_bezt_free_align_handles(BezTriple *bezt)
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
eV3DProjStatus ED_view3d_project_float_object(const ARegion *region, const float co[3], float r_co[2], eV3DProjTest flag)
void mul_v3_m4v3(float r[3], const float mat[4][4], const float vec[3])
MINLINE void copy_v2_v2(float r[2], const float a[2])
static void insert_bp_to_nurb(Nurb *nu, const CutData *data, Curve *cu)
MINLINE float mul_project_m4_v3_zfac(const float mat[4][4], const float co[3]) ATTR_WARN_UNUSED_RESULT
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
bool ED_operator_editcurve(bContext *C)
static bool insert_point_to_segment(const ViewContext *vc, const wmEvent *event)
static bool update_cut_data_for_all_nurbs(const ViewContext *vc, const ListBaseT< Nurb > *nurbs, const float point[2], const float sel_dist, CutData *cd)
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
BezTriple * BKE_nurb_bezt_get_prev(Nurb *nu, BezTriple *bezt)
Definition curve.cc:988
MINLINE void sub_v2_v2v2(float r[2], const float a[2], const float b[2])
void ED_curve_beztcpy(EditNurb *editnurb, BezTriple *dst, BezTriple *src, int count)
wmKeyMap * curve_pen_modal_keymap(wmKeyConfig *keyconf)
@ PEN_MODAL_LINK_HANDLES
@ PEN_MODAL_MOVE_ENTIRE
@ PEN_MODAL_FREE_ALIGN_TOGGLE
@ PEN_MODAL_MOVE_ADJACENT
static wmOperatorStatus curve_pen_invoke(bContext *C, wmOperator *op, const wmEvent *event)
static CutData init_cut_data(const wmEvent *event)
static void extrude_vertices_from_selected_endpoints(EditNurb *editnurb, ListBaseT< Nurb > *nurbs, Curve *cu, const float disp_3d[3])
static bool get_selected_center(const ListBaseT< Nurb > *nurbs, const bool mid_only, const bool bezt_only, float r_center[3])
static void move_all_selected_points(const ViewContext *vc, const wmEvent *event, CurvePenData *cpd, ListBaseT< Nurb > *nurbs, const bool bezt_only)
void ED_view3d_global_to_vector(const RegionView3D *rv3d, const float coord[3], float r_out[3])
static void move_bezt_to_location(BezTriple *bezt, const float location[3])
void BKE_curve_nurb_vert_active_validate(Curve *cu)
Definition curve.cc:5092
MINLINE void mul_v3_fl(float r[3], float f)
static void delete_bp_from_nurb(const BPoint *bp, Nurb *nu, EditNurb *editnurb)
static bool make_cyclic_if_endpoints(const ViewContext *vc, Nurb *sel_nu, BezTriple *sel_bezt, BPoint *sel_bp)
static void delete_bezt_from_nurb(const BezTriple *bezt, Nurb *nu, EditNurb *editnurb)
void mul_m4_v3(const float M[4][4], float r[3])
static void update_cut_data_for_nurb(const ViewContext *vc, CutData *cd, Nurb *nu, const int resolu, const float point[2])
static void toggle_select_bezt(BezTriple *bezt, const int bezt_idx, Curve *cu, Nurb *nu)
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:593
VecBase< int32_t, 2 > int2
static void move_bezt_handle_or_vertex_by_displacement(const ViewContext *vc, BezTriple *bezt, const int bezt_idx, const float disp_2d[2], const float distance, const bool link_handles, const bool lock_angle)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
bool WM_event_drag_test(const wmEvent *event, const int prev_xy[2])
static void get_updated_data_for_edge(const float point[2], const float point1[2], const float point2[2], const int point_idx, const int resolu_idx, float *r_min_dist, int *r_min_i, float *r_param)
void BKE_nurb_handles_calc(Nurb *nu)
Definition curve.cc:3992
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
static void screenspace_to_worldspace(const ViewContext *vc, const float pos_2d[2], const float depth[3], float r_pos_3d[3])
void DEG_id_tag_update(ID *id, unsigned int flags)
BMesh const char void * data
void BKE_curve_editNurb_keyIndex_delCV(CVKeyIndexMap *keyindex, const void *cv)
Definition curve.cc:358
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
Object * CTX_data_edit_object(const bContext *C)
void WM_operator_properties_mouse_select(wmOperatorType *ot)
void CURVE_OT_pen(wmOperatorType *ot)
void interp_v3_v3v3(float r[3], const float a[3], const float b[3], float t)
BezTriple * BKE_nurb_bezt_get_next(Nurb *nu, BezTriple *bezt)
Definition curve.cc:946
float cos_v2v2v2(const float p1[2], const float p2[2], const float p3[2]) ATTR_WARN_UNUSED_RESULT
static void delete_nurb(Curve *cu, Nurb *nu)
static const EnumPropertyItem prop_close_spline_method[]
bool ED_curve_editnurb_select_pick(bContext *C, const int mval[2], const int dist_px, const SelectPick_Params &params)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
ListBaseT< Nurb > * object_editcurve_get(Object *ob)
Definition editcurve.cc:95
MINLINE void zero_v3(float r[3])
MINLINE float dot_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
static void get_bezier_interpolated_point(const BezTriple *bezt1, const BezTriple *bezt2, const float parameter, float r_point[3])
MINLINE float normalize_v3_v3(float r[3], const float a[3])
static int get_nurb_index(const ListBaseT< Nurb > *nurbs, const Nurb *nurb)
static void toggle_handle_types(BezTriple *bezt, int bezt_idx, CurvePenData *cpd)
static void move_bp_to_location(const ViewContext *vc, BPoint *bp, const float mval[2])
static void init_selected_bezt_handles(ListBaseT< Nurb > *nurbs)
MINLINE void add_v3_v3(float r[3], const float a[3])
wmEventHandler_Op * WM_event_add_modal_handler(bContext *C, wmOperator *op)
MINLINE float len_manhattan_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
static void insert_bezt_to_nurb(Nurb *nu, const CutData *data, Curve *cu)
static void screenspace_to_worldspace_int(const ViewContext *vc, const int pos_2d[2], const float depth[3], float r_pos_3d[3])
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
static bool is_last_bezt(const Nurb *nu, const BezTriple *bezt)
int ed_editcurve_addvert(Curve *cu, EditNurb *editnurb, View3D *v3d, const float location_init[3])
static void move_segment(const ViewContext *vc, MoveSegmentData *seg_data, const wmEvent *event)
static void extrude_points_from_selected_vertices(const ViewContext *vc, const wmEvent *event, const eBezTriple_Handle extrude_handle)
void interp_v2_v2v2(float r[2], const float a[2], const float b[2], float t)
MINLINE float max_ff(float a, float b)
MINLINE float len_v2v2(const float v1[2], const float v2[2]) ATTR_WARN_UNUSED_RESULT
static void update_location_for_2d_curve(const ViewContext *vc, float location[3])
MINLINE float normalize_v3_length(float n[3], float unit_length)
static void toggle_select_bp(BPoint *bp, Curve *cu, Nurb *nu)
@ V3D_PROJ_TEST_CLIP_WIN
Definition ED_view3d.hh:297
@ V3D_PROJ_TEST_CLIP_BB
Definition ED_view3d.hh:296
static bool is_cyclic(const Nurb *nu)
float angle_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
@ KM_DBL_CLICK
Definition WM_types.hh:327
@ KM_RELEASE
Definition WM_types.hh:325
void WM_modalkeymap_assign(wmKeyMap *km, const char *opname)
MINLINE float interpf(float target, float origin, float t)
void ED_view3d_win_to_3d_int(const View3D *v3d, const ARegion *region, const float depth_pt[3], const int mval[2], float r_out[3])
ListBaseT< Nurb > * BKE_curve_editNurbs_get(Curve *cu)
Definition curve.cc:441
static void move_adjacent_handle(const ViewContext *vc, const wmEvent *event, ListBaseT< Nurb > *nurbs)
static void deselect_all_center_vertices(ListBaseT< Nurb > *nurbs)
wmKeyMap * WM_modalkeymap_ensure(wmKeyConfig *keyconf, const char *idname, const EnumPropertyItem *items)
Definition wm_keymap.cc:972
static wmOperatorStatus curve_pen_modal(bContext *C, wmOperator *op, const wmEvent *event)
@ OPTYPE_UNDO
Definition WM_types.hh:190
MINLINE float len_v2(const float v[2]) ATTR_WARN_UNUSED_RESULT
bool ED_curve_deselect_all(EditNurb *editnurb)
static bool is_spline_nearby(ViewContext *vc, wmOperator *op, const wmEvent *event, const float sel_dist)
static void cycle_handles(BezTriple *bezt)
void BKE_nurb_knot_calc_u(Nurb *nu)
Definition curve.cc:1214
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
MINLINE float len_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
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])
void BKE_curve_forward_diff_bezier(float q0, float q1, float q2, float q3, float *p, int it, int stride)
Definition curve.cc:1696
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
static bool worldspace_to_screenspace(const ViewContext *vc, const float pos_3d[3], float r_pos_2d[2])
static void remove_handle_movement_constraints(BezTriple *bezt, const bool f1, const bool f3)
static const EnumPropertyItem prop_handle_types[]
void ed_dissolve_bez_segment(BezTriple *bezt_prev, BezTriple *bezt_next, const Nurb *nu, const Curve *cu, const uint span_len, const uint span_step[2])
bool curve_toggle_cyclic(View3D *v3d, ListBaseT< Nurb > *editnurb, int direction)
void BKE_nurb_free(Nurb *nu)
Definition curve.cc:593
const btScalar eps
Definition poly34.cpp:11
#define fabsf
#define sinf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
eBezTriple_Handle h2
eBezTriple_Handle h1
MoveSegmentData * msd
EditNurb * editnurb
CVKeyIndexMap * keyindex
ListBaseT< Nurb > nurbs
BezTriple * bezt
eNurbKnotFlag flagu
View3DCursor cursor
eHandleDisplay handle_display
View3DOverlay overlay
RegionView3D * rv3d
Definition ED_view3d.hh:86
int prev_press_xy[2]
Definition WM_types.hh:890
wmEventType type
Definition WM_types.hh:817
const char * description
Definition WM_types.hh:1115
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
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
#define ISMOUSE_MOTION(event_type)