Blender V5.3
editmesh_knife.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2007 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include <fmt/format.h>
12
13#include "MEM_guardedalloc.h"
14
15#include "BLF_api.hh"
16
17#include "BLI_array.hh"
18#include "BLI_listbase.hh"
19#include "BLI_map.hh"
20#include "BLI_math_color_c.hh"
21#include "BLI_math_geom.hh"
22#include "BLI_math_geom_c.hh"
23#include "BLI_math_matrix_c.hh"
25#include "BLI_math_vector.hh"
27
28#include "BLI_memarena.hh"
29#include "BLI_set.hh"
30#include "BLI_stack_c.hh"
31#include "BLI_string_utf8.hh"
32#include "BLI_vector.hh"
33
34#include "BLT_translation.hh"
35
36#include "BKE_bvhutils.hh"
37#include "BKE_context.hh"
38#include "BKE_editmesh.hh"
39#include "BKE_layer.hh"
40#include "BKE_mesh_types.hh"
41#include "BKE_report.hh"
42#include "BKE_scene.hh"
43#include "BKE_screen.hh"
44#include "BKE_unit.hh"
45
46#include "GPU_immediate.hh"
47#include "GPU_matrix.hh"
48#include "GPU_state.hh"
49
50#include "ED_mesh.hh"
51#include "ED_numinput.hh"
52#include "ED_screen.hh"
53#include "ED_space_api.hh"
54#include "ED_transform.hh"
55#include "ED_view3d.hh"
56
57#include "WM_api.hh"
58#include "WM_types.hh"
59
60#include "DNA_mesh_types.h"
61#include "DNA_object_types.h"
62
63#include "UI_interface.hh"
64#include "UI_resources.hh"
65
66#include "RNA_access.hh"
67#include "RNA_define.hh"
68
70
71#include "mesh_intern.hh" /* Own include. */
72
73namespace blender {
74
75/* Detect isolated holes and fill them. */
76#define USE_NET_ISLAND_CONNECT
77
78#define KMAXDIST (10 * UI_SCALE_FAC) /* Max mouse distance from edge before not detecting it. */
79
80/* WARNING: Knife float precision is fragile:
81 * Be careful before making changes here see: (#43229, #42864, #42459, #41164).
82 */
83#define KNIFE_FLT_EPS 0.00001f
84#define KNIFE_FLT_EPS_SQUARED (KNIFE_FLT_EPS * KNIFE_FLT_EPS)
85#define KNIFE_FLT_EPSBIG 0.0005f
86
87#define KNIFE_FLT_EPS_PX_VERT 0.5f
88#define KNIFE_FLT_EPS_PX_EDGE 0.05f
89#define KNIFE_FLT_EPS_PX_FACE 0.05f
90
91#define KNIFE_DEFAULT_ANGLE_SNAPPING_INCREMENT 30.0f
92#define KNIFE_MIN_ANGLE_SNAPPING_INCREMENT 0.0f
93#define KNIFE_MAX_ANGLE_SNAPPING_INCREMENT 180.0f
94
108
109/* Knife-tool Operator. */
110struct KnifeVert {
111 BMVert *v; /* Non-null if this is an original vert. */
112 ListBaseT<LinkData> edges;
113 ListBaseT<LinkData> faces;
114
115 /* Index of the associated object.
116 * -1 represents the absence of an object. */
118
119 float co[3]; /* Vertex position in the original mesh. Equivalent to #BMVert::co[3]. */
120 float3 cageco; /* Vertex position in the Cage mesh and in World Space. */
121 bool is_cut; /* Along a cut created by user input (will draw too). */
123 bool is_splitting; /* Created when an edge was split. */
124};
125
126struct KnifeEdge {
128 BMFace *basef; /* Face to restrict face fill to. */
129 ListBaseT<LinkData> faces;
130
131 BMEdge *e; /* Non-null if this is an original edge. */
132 bool is_cut; /* Along a cut created by user input (will draw too). */
134 int splits; /* Number of times this edge has been split. */
135};
136
138 float hit[3], cagehit[3];
139 float schit[2]; /* Screen coordinates for cagehit. */
140 float l; /* Lambda along cut line. */
141 float m; /* Depth front-to-back. */
142
143 /* Exactly one of kfe, v, or f should be non-null,
144 * saying whether cut line crosses and edge,
145 * is snapped to a vert, or is in the middle of some face. */
149
150 /* Index of the associated object.
151 * -1 represents the absence of an object. */
153};
154
157
158 /* At most one of vert, edge, or bmface should be non-null,
159 * saying whether the point is snapped to a vertex, edge, or in a face.
160 * If none are set, this point is in space and is_space should be true. */
164
165 /* Index of the associated object.
166 * -1 represents the absence of an object. */
168
169 float2 mval; /* Mouse screen position (may be non-integral if snapped to something). */
170
171 bool is_space() const
172 {
173 return this->ob_index == -1;
174 }
175};
176
178 float cage[3];
179 float mval[2];
181};
182
184 int cuts; /* Line hits cause multiple edges/cuts to be created at once. */
185 int splits; /* Number of edges split. */
186 KnifePosData pos; /* Store previous KnifePosData. */
188};
189
190struct KnifeBVH {
191 BVHTree *tree; /* Knife Custom BVH Tree. */
192
193 /* Used by #knife_bvh_raycast_cb to store the intersecting triangles. */
196
197 /* Use #bm_ray_cast_cb_elem_not_in_face_check. */
198 bool (*filter_cb)(BMFace *f, void *userdata);
200};
201
218
220
221/* struct for properties used while drawing */
223 ARegion *region; /* Region that knifetool was activated in. */
224 void *draw_handle; /* For drawing preview loop. */
225 ViewContext vc; /* NOTE: _don't_ use 'mval', instead use the one we define below. */
226
228
229 /* Used for swapping current object when in multi-object edit mode. */
231
234
236
237 /* Reused for edge-net filling. */
238 struct {
239 /* Cleared each use. */
241#ifdef USE_NET_ISLAND_CONNECT
243#endif
245
250
252
255 bool no_cuts; /* A cut has not been made yet. */
256
258 BLI_Stack *splitstack; /* Store edge splits by #knife_split_edge. */
259
260 float vthresh;
261 float ethresh;
262
263 /* Used for drag-cutting. */
265
266 /* Data for mouse-position-derived data. */
267 KnifePosData curr; /* Current point under the cursor. */
268 KnifePosData prev; /* Last added cut (a line draws from the cursor to this). */
269 KnifePosData init; /* The first point in the cut-list, used for closing the loop. */
270
271 /* Number of knife edges `kedges`. */
273 /* Number of knife vertices, `kverts`. */
275
277
279
280 /* Run by the UI or not. */
282
283 /* Operator options. */
284 bool cut_through; /* Preference, can be modified at runtime (that feature may go). */
285 bool only_select; /* Set on initialization. */
286 bool select_result; /* Set on initialization. */
287
291
293
296
301
303 float angle_snapping_increment; /* Degrees */
304
305 /* Use to check if we're currently dragging an angle snapped line. */
309 float angle;
310 /* Relative angle snapping reference edge. */
313 int snap_edge; /* Used by #KNF_MODAL_CYCLE_ANGLE_SNAP_EDGE to choose an edge for snapping. */
314
318 char axis_string[2];
319
322 KnifeMeasureData mdata; /* Data for distance and angle drawing calculations. */
323
324 KnifeUndoFrame *undo; /* Current undo frame. */
326
328};
329
330enum {
350};
351
352enum {
356};
357
358enum {
363};
364
365enum {
369};
370
371enum {
376};
377
378/* -------------------------------------------------------------------- */
381
382static void knife_draw_line(const KnifeTool_OpData *kcd, const uchar color[3])
383{
384 if (compare_v3v3(kcd->prev.cage, kcd->curr.cage, KNIFE_FLT_EPSBIG)) {
385 return;
386 }
387 const float3 dir = math::normalize(kcd->curr.cage - kcd->prev.cage) * kcd->vc.v3d->clip_end;
388 const float3 v1 = kcd->prev.cage + dir;
389 const float3 v2 = kcd->prev.cage - dir;
390
391 uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", gpu::VertAttrType::SFLOAT_32_32_32);
394 GPU_line_width(2.0);
396 immVertex3fv(pos, v1);
397 immVertex3fv(pos, v2);
398 immEnd();
400}
401
408
410{
411 const uchar *color;
412 switch (kcd->constrain_axis) {
414 color = kcd->colors.xaxis;
415 break;
416 }
418 color = kcd->colors.yaxis;
419 break;
420 }
422 color = kcd->colors.zaxis;
423 break;
424 }
425 default: {
426 color = kcd->colors.axis_extra;
427 break;
428 }
429 }
431}
432
434{
439
440 uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", gpu::VertAttrType::SFLOAT_32_32);
442
443 char numstr[256];
444 float numstr_size[2];
445 float posit[2];
446 const float bg_margin = 4.0f * UI_SCALE_FAC;
447 const float font_size = 14.0f;
448 const int distance_precision = 4;
449
450 /* Calculate distance and convert to string. */
451 const float cut_len = len_v3v3(kcd->prev.cage, kcd->curr.cage);
452
453 const UnitSettings &unit = kcd->scene->unit;
454 if (unit.system == USER_UNIT_NONE) {
455 SNPRINTF_UTF8(numstr, "%.*f", distance_precision, cut_len);
456 }
457 else {
459 numstr, sizeof(numstr), cut_len, distance_precision, B_UNIT_LENGTH, unit, false, true);
460 }
461
463 BLF_size(blf_mono_font, font_size * UI_SCALE_FAC);
465 BLF_width_and_height(blf_mono_font, numstr, sizeof(numstr), &numstr_size[0], &numstr_size[1]);
466
467 /* Center text. */
468 mid_v2_v2v2(posit, kcd->prev.mval, kcd->curr.mval);
469 posit[0] -= numstr_size[0] / 2.0f;
470 posit[1] -= numstr_size[1] / 2.0f;
471
472 /* Draw text background. */
473 float color_back[4] = {0.0f, 0.0f, 0.0f, 0.5f}; /* TODO: Replace with theme color. */
474 immUniformColor4fv(color_back);
475
477 immRectf(pos,
478 posit[0] - bg_margin,
479 posit[1] - bg_margin,
480 posit[0] + bg_margin + numstr_size[0],
481 posit[1] + bg_margin + numstr_size[1]);
484
485 /* Draw text. */
486 uchar color_text[3];
488
489 BLF_color3ubv(blf_mono_font, color_text);
490 BLF_position(blf_mono_font, posit[0], posit[1], 0.0f);
491 BLF_draw(blf_mono_font, numstr, sizeof(numstr));
493
496}
497
499 const float start[3],
500 const float mid[3],
501 const float end[3],
502 const float start_ss[2],
503 const float mid_ss[2],
504 const float end_ss[2],
505 const float angle)
506{
507 const RegionView3D *rv3d = static_cast<const RegionView3D *>(kcd->region->regiondata);
508 const int arc_steps = 24;
509 const float arc_size = 64.0f * UI_SCALE_FAC;
510 const float bg_margin = 4.0f * UI_SCALE_FAC;
511 const float cap_size = 4.0f * UI_SCALE_FAC;
512 const float font_size = 14.0f;
513 const int angle_precision = 3;
514
515 /* Angle arc in 3d space. */
517
518 const uint pos_3d = GPU_vertformat_attr_add(
519 immVertexFormat(), "pos", gpu::VertAttrType::SFLOAT_32_32_32);
521
522 {
523 float dir_tmp[3];
524 float ar_coord[3];
525
526 float dir_a[3];
527 float dir_b[3];
528 float quat[4];
529 float axis[3];
530 float arc_angle;
531
532 Object *ob = kcd->objects[kcd->bvh.ob_index];
533 const float inverse_average_scale = 1 / (ob->object_to_world().ptr()[0][0] +
534 ob->object_to_world().ptr()[1][1] +
535 ob->object_to_world().ptr()[2][2]);
536
537 const float px_scale =
538 3.0f * inverse_average_scale *
540 std::min({arc_size, len_v2v2(start_ss, mid_ss) / 2.0f, len_v2v2(end_ss, mid_ss) / 2.0f}));
541
542 sub_v3_v3v3(dir_a, start, mid);
543 sub_v3_v3v3(dir_b, end, mid);
544 normalize_v3(dir_a);
545 normalize_v3(dir_b);
546
547 cross_v3_v3v3(axis, dir_a, dir_b);
548 arc_angle = angle_normalized_v3v3(dir_a, dir_b);
549
550 axis_angle_to_quat(quat, axis, arc_angle / arc_steps);
551
552 copy_v3_v3(dir_tmp, dir_a);
553
555 GPU_line_width(1.0);
556
557 immBegin(GPU_PRIM_LINE_STRIP, arc_steps + 1);
558 for (int j = 0; j <= arc_steps; j++) {
559 madd_v3_v3v3fl(ar_coord, mid, dir_tmp, px_scale);
560 mul_qt_v3(quat, dir_tmp);
561
562 immVertex3fv(pos_3d, ar_coord);
563 }
564 immEnd();
565 }
566
568
569 /* Angle text and background in 2d space. */
574
575 uint pos_2d = GPU_vertformat_attr_add(immVertexFormat(), "pos", gpu::VertAttrType::SFLOAT_32_32);
577
578 /* Angle as string. */
579 char numstr[256];
580 float numstr_size[2];
581 float posit[2];
582
583 const UnitSettings &unit = kcd->scene->unit;
584 if (unit.system == USER_UNIT_NONE) {
585 SNPRINTF_UTF8(numstr, "%.*f" BLI_STR_UTF8_DEGREE_SIGN, angle_precision, RAD2DEGF(angle));
586 }
587 else {
589 sizeof(numstr),
590 double(angle),
591 angle_precision,
593 unit,
594 false,
595 true);
596 }
597
599 BLF_size(blf_mono_font, font_size * UI_SCALE_FAC);
601 BLF_width_and_height(blf_mono_font, numstr, sizeof(numstr), &numstr_size[0], &numstr_size[1]);
602
603 posit[0] = mid_ss[0] + (cap_size * 2.0f);
604 posit[1] = mid_ss[1] - (numstr_size[1] / 2.0f);
605
606 /* Draw text background. */
607 float color_back[4] = {0.0f, 0.0f, 0.0f, 0.5f}; /* TODO: Replace with theme color. */
608 immUniformColor4fv(color_back);
609
611 immRectf(pos_2d,
612 posit[0] - bg_margin,
613 posit[1] - bg_margin,
614 posit[0] + bg_margin + numstr_size[0],
615 posit[1] + bg_margin + numstr_size[1]);
618
619 /* Draw text. */
620 uchar color_text[3];
622
623 BLF_color3ubv(blf_mono_font, color_text);
624 BLF_position(blf_mono_font, posit[0], posit[1], 0.0f);
626 BLF_draw(blf_mono_font, numstr, sizeof(numstr));
628
631
633}
634
636{
637 KnifeVert *kfv;
638 KnifeVert *tempkfv;
639 KnifeEdge *kfe;
640 KnifeEdge *tempkfe;
641
642 if (kcd->curr.vert) {
643 kfv = kcd->curr.vert;
644
645 float min_angle = FLT_MAX;
646 float angle = 0.0f;
647 float *end;
648
649 kfe = static_cast<KnifeEdge *>(kfv->edges.first()->data);
650 for (LinkData &ref : kfv->edges) {
651 tempkfe = static_cast<KnifeEdge *>(ref.data);
652 if (tempkfe->v1 != kfv) {
653 tempkfv = tempkfe->v1;
654 }
655 else {
656 tempkfv = tempkfe->v2;
657 }
658 angle = angle_v3v3v3(kcd->prev.cage, kcd->curr.cage, tempkfv->cageco);
659 if (angle < min_angle) {
660 min_angle = angle;
661 kfe = tempkfe;
662 end = tempkfv->cageco;
663 }
664 }
665
666 if (min_angle > KNIFE_FLT_EPSBIG) {
667 /* Last vertex in screen space. */
668 float end_ss[2];
670
672 kcd->prev.cage,
673 kcd->curr.cage,
674 end,
675 kcd->prev.mval,
676 kcd->curr.mval,
677 end_ss,
678 min_angle);
679 }
680 }
681 else if (kcd->curr.edge) {
682 kfe = kcd->curr.edge;
683
684 /* Check for most recent cut (if cage is part of previous cut). */
685 if (!compare_v3v3(kfe->v1->cageco, kcd->prev.cage, KNIFE_FLT_EPSBIG) &&
687 {
688 /* Determine acute angle. */
689 float angle1 = angle_v3v3v3(kcd->prev.cage, kcd->curr.cage, kfe->v1->cageco);
690 float angle2 = angle_v3v3v3(kcd->prev.cage, kcd->curr.cage, kfe->v2->cageco);
691
692 float angle;
693 float *end;
694 if (angle1 < angle2) {
695 angle = angle1;
696 end = kfe->v1->cageco;
697 }
698 else {
699 angle = angle2;
700 end = kfe->v2->cageco;
701 }
702
703 /* Last vertex in screen space. */
704 float end_ss[2];
706
708 kcd, kcd->prev.cage, kcd->curr.cage, end, kcd->prev.mval, kcd->curr.mval, end_ss, angle);
709 }
710 }
711
712 if (kcd->prev.vert) {
713 kfv = kcd->prev.vert;
714 float min_angle = FLT_MAX;
715 float angle = 0.0f;
716 float *end;
717
718 /* If using relative angle snapping, always draw angle to reference edge. */
720 kfe = kcd->snap_ref_edge;
721 if (kfe->v1 != kfv) {
722 tempkfv = kfe->v1;
723 }
724 else {
725 tempkfv = kfe->v2;
726 }
727 min_angle = angle_v3v3v3(kcd->curr.cage, kcd->prev.cage, tempkfv->cageco);
728 end = tempkfv->cageco;
729 }
730 else {
731 /* Choose minimum angle edge. */
732 kfe = static_cast<KnifeEdge *>(kfv->edges.first()->data);
733 for (LinkData &ref : kfv->edges) {
734 tempkfe = static_cast<KnifeEdge *>(ref.data);
735 if (tempkfe->v1 != kfv) {
736 tempkfv = tempkfe->v1;
737 }
738 else {
739 tempkfv = tempkfe->v2;
740 }
741 angle = angle_v3v3v3(kcd->curr.cage, kcd->prev.cage, tempkfv->cageco);
742 if (angle < min_angle) {
743 min_angle = angle;
744 kfe = tempkfe;
745 end = tempkfv->cageco;
746 }
747 }
748 }
749
750 if (min_angle > KNIFE_FLT_EPSBIG) {
751 /* Last vertex in screen space. */
752 float end_ss[2];
754
756 kcd->curr.cage,
757 kcd->prev.cage,
758 end,
759 kcd->curr.mval,
760 kcd->prev.mval,
761 end_ss,
762 min_angle);
763 }
764 }
765 else if (kcd->prev.edge) {
766 /* Determine acute angle. */
767 kfe = kcd->prev.edge;
768 float angle1 = angle_v3v3v3(kcd->curr.cage, kcd->prev.cage, kfe->v1->cageco);
769 float angle2 = angle_v3v3v3(kcd->curr.cage, kcd->prev.cage, kfe->v2->cageco);
770
771 float angle;
772 float *end;
773 /* kcd->prev.edge can have one vertex part of cut and one part of mesh? */
774 /* This never seems to happen for kcd->curr.edge. */
775 if ((!kcd->prev.vert || kcd->prev.vert->v == kfe->v1->v) || kfe->v1->is_cut) {
776 angle = angle2;
777 end = kfe->v2->cageco;
778 }
779 else if ((!kcd->prev.vert || kcd->prev.vert->v == kfe->v2->v) || kfe->v2->is_cut) {
780 angle = angle1;
781 end = kfe->v1->cageco;
782 }
783 else {
784 if (angle1 < angle2) {
785 angle = angle1;
786 end = kfe->v1->cageco;
787 }
788 else {
789 angle = angle2;
790 end = kfe->v2->cageco;
791 }
792 }
793
794 /* Last vertex in screen space. */
795 float end_ss[2];
797
799 kcd, kcd->curr.cage, kcd->prev.cage, end, kcd->curr.mval, kcd->prev.mval, end_ss, angle);
800 }
801 else if (kcd->mdata.is_stored && !kcd->prev.is_space()) {
802 float angle = angle_v3v3v3(kcd->curr.cage, kcd->prev.cage, kcd->mdata.cage);
804 kcd->curr.cage,
805 kcd->prev.cage,
806 kcd->mdata.cage,
807 kcd->curr.mval,
808 kcd->prev.mval,
809 kcd->mdata.mval,
810 angle);
811 }
812}
813
815{
816 switch (kcd->dist_angle_mode) {
820 break;
821 }
824 break;
825 }
828 break;
829 }
830 }
831}
832
833/* Modal loop selection drawing callback. */
834static void knifetool_draw(const bContext * /*C*/, ARegion * /*region*/, void *arg)
835{
836 const KnifeTool_OpData *kcd = static_cast<const KnifeTool_OpData *>(arg);
838
840 GPU_polygon_offset(1.0f, 1.0f);
841
842 GPUVertFormat *format = immVertexFormat();
843 uint pos = GPU_vertformat_attr_add(format, "pos", gpu::VertAttrType::SFLOAT_32_32_32);
844
845 /* Draw points. */
848
849 /* Needed for AA points. */
851
852 if (kcd->prev.vert) {
854 immUniform1f("size", 11 * UI_SCALE_FAC);
855
857 immVertex3fv(pos, kcd->prev.cage);
858 immEnd();
859 }
860
861 if (kcd->prev.bmface || kcd->prev.edge) {
863 immUniform1f("size", 9 * UI_SCALE_FAC);
864
866 immVertex3fv(pos, kcd->prev.cage);
867 immEnd();
868 }
869
870 if (kcd->curr.vert) {
872 immUniform1f("size", 11 * UI_SCALE_FAC);
873
875 immVertex3fv(pos, kcd->curr.cage);
876 immEnd();
877 }
878 else if (kcd->curr.edge) {
879 /* Lines (handled below.) */
880 }
881
882 if (kcd->curr.bmface || kcd->curr.edge) {
884 immUniform1f("size", 9 * UI_SCALE_FAC);
885
887 immVertex3fv(pos, kcd->curr.cage);
888 immEnd();
889 }
890
891 if (kcd->depth_test) {
893 }
894
895 if (kcd->totkvert > 0) {
896 BLI_mempool_iter iter;
897 KnifeVert *kfv;
898
900 immUniform1f("size", 5.0 * UI_SCALE_FAC);
901
902 gpu::Batch *batch = immBeginBatchAtMost(GPU_PRIM_POINTS, BLI_mempool_len(kcd->kverts));
903
904 BLI_mempool_iternew(kcd->kverts, &iter);
905 for (kfv = static_cast<KnifeVert *>(BLI_mempool_iterstep(&iter)); kfv;
906 kfv = static_cast<KnifeVert *>(BLI_mempool_iterstep(&iter)))
907 {
908 if (!kfv->is_cut || kfv->is_invalid) {
909 continue;
910 }
911
912 immVertex3fv(pos, kfv->cageco);
913 }
914
915 immEnd();
916
917 GPU_batch_draw(batch);
918 GPU_batch_discard(batch);
919 }
920
922
924
925 /* Draw lines. */
927
928 if (kcd->mode == MODE_DRAGGING) {
930 GPU_line_width(2.0);
931
933 immVertex3fv(pos, kcd->prev.cage);
934 immVertex3fv(pos, kcd->curr.cage);
935 immEnd();
936 }
937
938 if (kcd->curr.vert) {
939 /* Points (handled above). */
940 }
941 else if (kcd->curr.edge) {
943 GPU_line_width(2.0);
944
946 immVertex3fv(pos, kcd->curr.edge->v1->cageco);
947 immVertex3fv(pos, kcd->curr.edge->v2->cageco);
948 immEnd();
949 }
950
951 if (kcd->totkedge > 0) {
952 BLI_mempool_iter iter;
953 KnifeEdge *kfe;
954
956 GPU_line_width(1.0);
957
958 gpu::Batch *batch = immBeginBatchAtMost(GPU_PRIM_LINES, BLI_mempool_len(kcd->kedges) * 2);
959
960 BLI_mempool_iternew(kcd->kedges, &iter);
961 for (kfe = static_cast<KnifeEdge *>(BLI_mempool_iterstep(&iter)); kfe;
962 kfe = static_cast<KnifeEdge *>(BLI_mempool_iterstep(&iter)))
963 {
964 if (!kfe->is_cut || kfe->is_invalid) {
965 continue;
966 }
967
968 immVertex3fv(pos, kfe->v1->cageco);
969 immVertex3fv(pos, kfe->v2->cageco);
970 }
971
972 immEnd();
973
974 GPU_batch_draw(batch);
975 GPU_batch_discard(batch);
976 }
977
978 /* Draw relative angle snapping reference edge. */
981 GPU_line_width(2.0);
982
984 immVertex3fv(pos, kcd->snap_ref_edge->v1->cageco);
985 immVertex3fv(pos, kcd->snap_ref_edge->v2->cageco);
986 immEnd();
987 }
988
989 const int64_t total_hits = kcd->linehits.size();
990 if (total_hits > 0) {
992
993 gpu::VertBuf *vert = GPU_vertbuf_create_with_format(*format);
994 GPU_vertbuf_data_alloc(*vert, total_hits);
995
996 int other_verts_count = 0;
997 int snapped_verts_count = 0;
998 for (const KnifeLineHit &hit : kcd->linehits) {
999 if (hit.v) {
1000 GPU_vertbuf_attr_set(vert, pos, snapped_verts_count++, hit.cagehit);
1001 }
1002 else {
1003 GPU_vertbuf_attr_set(vert, pos, total_hits - 1 - other_verts_count++, hit.cagehit);
1004 }
1005 }
1006
1007 gpu::Batch *batch = GPU_batch_create_ex(GPU_PRIM_POINTS, vert, nullptr, GPU_BATCH_OWNS_VBO);
1009
1010 /* Draw any snapped verts first. */
1011 float fcol[4];
1013 GPU_batch_uniform_4fv(batch, "color", fcol);
1014 GPU_batch_uniform_1f(batch, "size", 11 * UI_SCALE_FAC);
1015
1016 if (snapped_verts_count > 0) {
1017 GPU_batch_draw_range(batch, 0, snapped_verts_count);
1018 }
1019
1020 /* Now draw the rest. */
1022 GPU_batch_uniform_4fv(batch, "color", fcol);
1023 GPU_batch_uniform_1f(batch, "size", 7 * UI_SCALE_FAC);
1024
1025 if (other_verts_count > 0) {
1026 GPU_batch_draw_range(batch, snapped_verts_count, other_verts_count);
1027 }
1028
1029 GPU_batch_discard(batch);
1030
1032 }
1033
1035
1037
1038 if (kcd->mode == MODE_DRAGGING) {
1039 if (kcd->is_angle_snapping) {
1041 }
1042 else if (kcd->axis_constrained) {
1044 }
1045
1046 if (kcd->show_dist_angle) {
1048 }
1049 }
1050
1052
1053 /* Reset default. */
1055}
1056
1058
1059/* -------------------------------------------------------------------- */
1062
1064{
1065 auto get_modal_key_str = [&](int id) {
1066 return WM_modalkeymap_operator_items_to_string(op->type, id, true).value_or("");
1067 };
1068
1069 WorkspaceStatus status(C);
1070 status.opmodal(IFACE_("Cut"), op->type, KNF_MODAL_ADD_CUT);
1071 status.opmodal(IFACE_("Close"), op->type, KNF_MODAL_ADD_CUT_CLOSED);
1072 status.opmodal(IFACE_("Stop"), op->type, KNF_MODAL_NEW_CUT);
1073 status.opmodal(IFACE_("Confirm"), op->type, KNF_MODAL_CONFIRM);
1074 status.opmodal(IFACE_("Cancel"), op->type, KNF_MODAL_CANCEL);
1075 status.opmodal(IFACE_("Undo"), op->type, KNF_MODAL_UNDO);
1076 status.opmodal(IFACE_("Pan View"), op->type, KNF_MODAL_PANNING);
1077 status.opmodal(IFACE_("Midpoint Snap"), op->type, KNF_MODAL_MIDPOINT_ON, kcd->snap_midpoints);
1078 status.opmodal(
1079 IFACE_("Ignore Snap"), op->type, KNF_MODAL_IGNORE_SNAP_ON, kcd->ignore_edge_snapping);
1080 status.opmodal(IFACE_("Cut Through"), op->type, KNF_MODAL_CUT_THROUGH_TOGGLE, kcd->cut_through);
1081 status.opmodal({}, op->type, KNF_MODAL_X_AXIS, kcd->constrain_axis == 1);
1082 status.opmodal({}, op->type, KNF_MODAL_Y_AXIS, kcd->constrain_axis == 2);
1083 status.opmodal({}, op->type, KNF_MODAL_Z_AXIS, kcd->constrain_axis == 3);
1084 status.item(IFACE_("Axis"), ICON_NONE);
1085 status.opmodal(
1087 status.opmodal(IFACE_("X-Ray"), op->type, KNF_MODAL_DEPTH_TEST_TOGGLE, !kcd->depth_test);
1088
1089 const std::string angle = fmt::format(
1090 "{}: {:.2f}({:.2f}) ({}{}{}{})",
1091 IFACE_("Angle Constraint"),
1092 (kcd->angle >= 0.0f) ? RAD2DEGF(kcd->angle) : 360.0f + RAD2DEGF(kcd->angle),
1097 kcd->angle_snapping ?
1099 IFACE_("Relative")) :
1100 IFACE_("Off"), /* TODO: Can this be simplified? */
1103 get_modal_key_str(KNF_MODAL_CYCLE_ANGLE_SNAP_EDGE) :
1104 "",
1106 "");
1107
1109}
1110
1112
1113/* -------------------------------------------------------------------- */
1116
1117static const int *knife_bm_tri_index_get(const KnifeTool_OpData *kcd,
1118 int ob_index,
1119 int tri_index,
1120 int tri_index_buf[3])
1121{
1122 const KnifeObjectInfo *obinfo = &kcd->objects_info[ob_index];
1123 if (!obinfo->tri_indices.is_empty()) {
1124 return obinfo->tri_indices[tri_index];
1125 }
1126 const std::array<BMLoop *, 3> &ltri = obinfo->em->looptris[tri_index];
1127 for (int i = 0; i < 3; i++) {
1128 tri_index_buf[i] = BM_elem_index_get(ltri[i]->v);
1129 }
1130 return tri_index_buf;
1131}
1132
1134 int ob_index,
1135 int tri_index,
1136 float cos[3][3])
1137{
1138 const KnifeObjectInfo *obinfo = &kcd->objects_info[ob_index];
1139 int tri_ind_buf[3];
1140 const int *tri_ind = knife_bm_tri_index_get(kcd, ob_index, tri_index, tri_ind_buf);
1141 for (int i = 0; i < 3; i++) {
1142 copy_v3_v3(cos[i], obinfo->positions_cage[tri_ind[i]]);
1143 }
1144}
1145
1147 int ob_index,
1148 int tri_index,
1149 float cos[3][3])
1150{
1151 knife_bm_tri_cagecos_get(kcd, ob_index, tri_index, cos);
1152 const Object *ob = kcd->objects[ob_index];
1153 for (int i = 0; i < 3; i++) {
1154 mul_m4_v3(ob->object_to_world().ptr(), cos[i]);
1155 }
1156}
1157
1159
1160/* -------------------------------------------------------------------- */
1163
1165{
1166 return (BM_elem_flag_test(f, BM_ELEM_SELECT) != 0);
1167}
1168
1170{
1171 return (BM_elem_flag_test(f, BM_ELEM_HIDDEN) == 0);
1172}
1173
1175{
1176 Object *ob;
1177 BMEditMesh *em;
1178
1179 /* Test Function. */
1180 bool (*test_fn)(BMFace *);
1181 if (kcd->only_select && kcd->cut_through) {
1182 test_fn = knife_bm_face_is_select;
1183 }
1184 else {
1186 }
1187
1188 /* Construct BVH Tree. */
1189 const float epsilon = FLT_EPSILON * 2.0f;
1190 int tottri = 0;
1191 int ob_tottri = 0;
1193 BMFace *f_test = nullptr, *f_test_prev = nullptr;
1194 bool test_fn_ret = false;
1195
1196 /* Calculate tottri. */
1197 for (Object *ob : kcd->objects) {
1198 ob_tottri = 0;
1199 em = BKE_editmesh_from_object(ob);
1200
1201 for (int i = 0; i < em->looptris.size(); i++) {
1202 f_test = em->looptris[i][0]->f;
1203 if (f_test != f_test_prev) {
1204 test_fn_ret = test_fn(f_test);
1205 f_test_prev = f_test;
1206 }
1207
1208 if (test_fn_ret) {
1209 ob_tottri++;
1210 }
1211 }
1212
1213 tottri += ob_tottri;
1214 }
1215
1216 kcd->bvh.tree = BLI_bvhtree_new(tottri, epsilon, 8, 8);
1217
1218 f_test_prev = nullptr;
1219 test_fn_ret = false;
1220
1221 /* Add triangles for each object.
1222 * TODO:
1223 * test_fn can leave large gaps between bvh tree indices.
1224 * Compacting bvh tree indices may be possible.
1225 * Don't forget to update #knife_bvh_intersect_plane! */
1226 tottri = 0;
1227 for (const int ob_index : kcd->objects.index_range()) {
1228 ob = kcd->objects[ob_index];
1229 em = BKE_editmesh_from_object(ob);
1230 looptris = em->looptris;
1231
1232 for (int i = 0; i < em->looptris.size(); i++) {
1233
1234 f_test = looptris[i][0]->f;
1235 if (f_test != f_test_prev) {
1236 test_fn_ret = test_fn(f_test);
1237 f_test_prev = f_test;
1238 }
1239
1240 if (!test_fn_ret) {
1241 continue;
1242 }
1243
1244 float tri_cos[3][3];
1245 knife_bm_tri_cagecos_get_worldspace(kcd, ob_index, i, tri_cos);
1246 BLI_bvhtree_insert(kcd->bvh.tree, i + tottri, &tri_cos[0][0], 3);
1247 }
1248
1249 tottri += em->looptris.size();
1250 }
1251
1253}
1254
1255/* Wrapper for #BLI_bvhtree_free. */
1257{
1258 if (kcd->bvh.tree) {
1260 kcd->bvh.tree = nullptr;
1261 }
1262}
1263
1264static void knife_bvh_raycast_cb(void *userdata,
1265 int index,
1266 const BVHTreeRay *ray,
1267 BVHTreeRayHit *hit)
1268{
1269 if (index == -1) {
1270 return;
1271 }
1272
1273 KnifeTool_OpData *kcd = static_cast<KnifeTool_OpData *>(userdata);
1274 BMLoop *const *ltri = nullptr;
1275 Object *ob;
1276 BMEditMesh *em;
1277
1278 float dist;
1279 bool isect;
1280 int tottri;
1281
1282 tottri = 0;
1283 int ob_index = 0;
1284 for (; ob_index < kcd->objects.size(); ob_index++) {
1285 index -= tottri;
1286 ob = kcd->objects[ob_index];
1287 em = BKE_editmesh_from_object(ob);
1288 tottri = em->looptris.size();
1289 if (index < tottri) {
1290 ltri = em->looptris[index].data();
1291 break;
1292 }
1293 }
1294 BLI_assert(ltri != nullptr);
1295
1296 if (kcd->bvh.filter_cb) {
1297 if (!kcd->bvh.filter_cb(ltri[0]->f, kcd->bvh.filter_data)) {
1298 return;
1299 }
1300 }
1301
1302 float tri_cos[3][3];
1303 knife_bm_tri_cagecos_get_worldspace(kcd, ob_index, index, tri_cos);
1304 isect = (ray->radius > 0.0f ?
1306 ray->origin, ray->direction, UNPACK3(tri_cos), &dist, nullptr, ray->radius) :
1307#ifdef USE_KDOPBVH_WATERTIGHT
1309 ray->origin, ray->isect_precalc, UNPACK3(tri_cos), &dist, nullptr));
1310#else
1311isect_ray_tri_v3(ray->origin, ray->direction, UNPACK3(tri_cos), &dist, nullptr);
1312#endif
1313
1314 if (isect && dist < hit->dist) {
1315 madd_v3_v3v3fl(hit->co, ray->origin, ray->direction, dist);
1316
1317 /* Discard clipped points. */
1318 if (RV3D_CLIPPING_ENABLED(kcd->vc.v3d, kcd->vc.rv3d) &&
1319 ED_view3d_clipping_test(kcd->vc.rv3d, hit->co, false))
1320 {
1321 return;
1322 }
1323
1324 hit->dist = dist;
1325 hit->index = index;
1326
1327 copy_v3_v3(hit->no, ltri[0]->f->no);
1328
1329 kcd->bvh.looptris = em->looptris;
1330 kcd->bvh.ob_index = ob_index;
1331 }
1332}
1333
1334/* `co` is expected to be in world space. */
1336 const float co[3],
1337 const float dir[3],
1338 const float radius,
1339 float *r_dist,
1340 float r_cagehit[3],
1341 int *r_ob_index)
1342{
1343 BMFace *face;
1344 BVHTreeRayHit hit;
1345 const float dist = r_dist ? *r_dist : FLT_MAX;
1346 hit.dist = dist;
1347 hit.index = -1;
1348
1349 BLI_bvhtree_ray_cast(kcd->bvh.tree, co, dir, radius, &hit, knife_bvh_raycast_cb, kcd);
1350
1351 /* Handle Hit */
1352 if (hit.index != -1 && hit.dist != dist) {
1353 face = kcd->bvh.looptris[hit.index][0]->f;
1354
1355 if (r_cagehit) {
1356 copy_v3_v3(r_cagehit, hit.co);
1357 }
1358
1359 if (r_dist) {
1360 *r_dist = hit.dist;
1361 }
1362
1363 if (r_ob_index) {
1364 *r_ob_index = kcd->bvh.ob_index;
1365 }
1366
1367 return face;
1368 }
1369 return nullptr;
1370}
1371
1372/* `co` is expected to be in world space. */
1374 const float co[3],
1375 const float dir[3],
1376 const float radius,
1377 float *r_dist,
1378 float r_cagehit[3],
1379 int *r_ob_index,
1380 bool (*filter_cb)(BMFace *f, void *userdata),
1381 void *filter_userdata)
1382{
1383 kcd->bvh.filter_cb = filter_cb;
1384 kcd->bvh.filter_data = filter_userdata;
1385
1386 BMFace *face = knife_bvh_raycast(kcd, co, dir, radius, r_dist, r_cagehit, r_ob_index);
1387
1388 kcd->bvh.filter_cb = nullptr;
1389 kcd->bvh.filter_data = nullptr;
1390
1391 return face;
1392}
1393
1395
1396/* -------------------------------------------------------------------- */
1399
1400static void knife_project_v2(const KnifeTool_OpData *kcd, const float co[3], float sco[2])
1401{
1403}
1404
1405/* Ray is returned in world space. */
1407 const float mval[2],
1408 float r_origin[3],
1409 float r_end[3])
1410{
1411 /* Unproject to find view ray. */
1413 kcd->vc.depsgraph, kcd->region, kcd->vc.v3d, mval, r_origin, r_end, false);
1414}
1415
1416/* No longer used, but may be useful in the future. */
1418 float mval[3],
1419 float r_cage[3])
1420{
1421 float origin[3];
1422 float origin_ofs[3];
1423 float ray[3], ray_normal[3];
1424
1425 knife_input_ray_segment(kcd, mval, origin, origin_ofs);
1426
1427 sub_v3_v3v3(ray, origin_ofs, origin);
1428 normalize_v3_v3(ray_normal, ray);
1429
1430 knife_bvh_raycast(kcd, origin, ray_normal, 0.0f, nullptr, r_cage, nullptr);
1431}
1432
1434{
1435 bool v1_inside, v2_inside;
1436 bool v1_inface, v2_inface;
1437 BMLoop *l1, *l2;
1438
1439 if (!f || !v1 || !v2) {
1440 return false;
1441 }
1442
1443 l1 = v1->v ? BM_face_vert_share_loop(f, v1->v) : nullptr;
1444 l2 = v2->v ? BM_face_vert_share_loop(f, v2->v) : nullptr;
1445
1446 if ((l1 && l2) && BM_loop_is_adjacent(l1, l2)) {
1447 /* Boundary-case, always false to avoid edge-in-face checks below. */
1448 return false;
1449 }
1450
1451 /* Find out if v1 and v2, if set, are part of the face. */
1452 v1_inface = (l1 != nullptr);
1453 v2_inface = (l2 != nullptr);
1454
1455 /* BM_face_point_inside_test uses best-axis projection so this isn't most accurate test... */
1456 v1_inside = v1_inface ? false : BM_face_point_inside_test(f, v1->co);
1457 v2_inside = v2_inface ? false : BM_face_point_inside_test(f, v2->co);
1458 if ((v1_inface && v2_inside) || (v2_inface && v1_inside) || (v1_inside && v2_inside)) {
1459 return true;
1460 }
1461
1462 if (v1_inface && v2_inface) {
1463 float mid[3];
1464 /* Can have case where v1 and v2 are on shared chain between two faces.
1465 * BM_face_splits_check_legal does visibility and self-intersection tests,
1466 * but it is expensive and maybe a bit buggy, so use a simple
1467 * "is the midpoint in the face" test. */
1468 mid_v3_v3v3(mid, v1->co, v2->co);
1469 return BM_face_point_inside_test(f, mid);
1470 }
1471 return false;
1472}
1473
1475{
1477 kcd->vc.depsgraph, kcd->vc.v3d, kcd->vc.rv3d, true, &kcd->clipsta, &kcd->clipend);
1478}
1479
1481
1482/* -------------------------------------------------------------------- */
1487
1489{
1490 BMElem *ele_test;
1491 KnifeEdge *kfe = nullptr;
1492
1493 /* vert? */
1494 ele_test = reinterpret_cast<BMElem *>(kfv->v);
1495
1496 if (r_kfe || ele_test == nullptr) {
1497 if (kfv->v == nullptr) {
1498 for (LinkData &ref : kfv->edges) {
1499 kfe = static_cast<KnifeEdge *>(ref.data);
1500 if (kfe->e) {
1501 if (r_kfe) {
1502 *r_kfe = kfe;
1503 }
1504 break;
1505 }
1506 }
1507 }
1508 }
1509
1510 /* edge? */
1511 if (ele_test == nullptr) {
1512 if (kfe) {
1513 ele_test = reinterpret_cast<BMElem *>(kfe->e);
1514 }
1515 }
1516
1517 /* face? */
1518 if (ele_test == nullptr) {
1519 if (kfe->faces.is_single()) {
1520 ele_test = static_cast<BMElem *>(kfe->faces.first()->data);
1521 }
1522 }
1523
1524 return ele_test;
1525}
1526
1528{
1529 BMElem *ele_test;
1530
1531 ele_test = reinterpret_cast<BMElem *>(kfe->e);
1532
1533 if (ele_test == nullptr) {
1534 ele_test = reinterpret_cast<BMElem *>(kfe->basef);
1535 }
1536
1537 return ele_test;
1538}
1539
1541
1542/* -------------------------------------------------------------------- */
1545
1546static ListBaseT<LinkData> *knife_empty_list(KnifeTool_OpData *kcd)
1547{
1548 ListBaseT<LinkData> *list;
1549
1550 list = static_cast<ListBaseT<LinkData> *>(
1551 BLI_memarena_alloc(kcd->arena, sizeof(ListBaseT<LinkData>)));
1552 list->clear_no_delete();
1553 return list;
1554}
1555
1556static void knife_append_list(KnifeTool_OpData *kcd, ListBaseT<LinkData> *lst, void *elem)
1557{
1558 LinkData *ref;
1559
1560 ref = static_cast<LinkData *>(BLI_mempool_calloc(kcd->refs));
1561 ref->data = elem;
1562 BLI_addtail(lst, ref);
1563}
1564
1565static LinkData *find_ref(ListBaseT<LinkData> *lb, void *ref)
1566{
1567 for (LinkData &ref1 : *lb) {
1568 if (ref1.data == ref) {
1569 return &ref1;
1570 }
1571 }
1572
1573 return nullptr;
1574}
1575
1576static void knife_append_list_no_dup(KnifeTool_OpData *kcd, ListBaseT<LinkData> *lst, void *elem)
1577{
1578 if (!find_ref(lst, elem)) {
1579 knife_append_list(kcd, lst, elem);
1580 }
1581}
1582
1584{
1585 knife_append_list(kcd, &kfe->v1->edges, kfe);
1586 knife_append_list(kcd, &kfe->v2->edges, kfe);
1587}
1588
1589/* Add faces of an edge to a KnifeVert's faces list. No checks for duplicates. */
1591{
1592 BMIter bmiter;
1593 BMFace *f;
1594
1595 BM_ITER_ELEM (f, &bmiter, e, BM_FACES_OF_EDGE) {
1596 knife_append_list(kcd, &kfv->faces, f);
1597 }
1598}
1599
1600/* Find a face in common in the two faces lists.
1601 * If more than one, return the first; if none, return nullptr. */
1602static BMFace *knife_find_common_face(ListBaseT<LinkData> *faces1, ListBaseT<LinkData> *faces2)
1603{
1604 for (LinkData &ref1 : *faces1) {
1605 for (LinkData &ref2 : *faces2) {
1606 if (ref1.data == ref2.data) {
1607 return static_cast<BMFace *>(ref1.data);
1608 }
1609 }
1610 }
1611 return nullptr;
1612}
1613
1615
1616/* -------------------------------------------------------------------- */
1619
1620static KnifeVert *new_knife_vert(KnifeTool_OpData *kcd, const float co[3], const float cageco[3])
1621{
1622 KnifeVert *kfv = static_cast<KnifeVert *>(BLI_mempool_calloc(kcd->kverts));
1623
1624 kcd->totkvert++;
1625
1626 copy_v3_v3(kfv->co, co);
1627 copy_v3_v3(kfv->cageco, cageco);
1628
1629 return kfv;
1630}
1631
1633{
1634 KnifeEdge *kfe = static_cast<KnifeEdge *>(BLI_mempool_calloc(kcd->kedges));
1635 kcd->totkedge++;
1636 return kfe;
1637}
1638
1639/* Get a KnifeVert wrapper for an existing BMVert. */
1641{
1642 KnifeVert *kfv = kcd->origvertmap->lookup_default(v, nullptr);
1643 const float *cageco;
1644
1645 if (!kfv) {
1646 BMIter bmiter;
1647 BMFace *f;
1648
1649 if (BM_elem_index_get(v) >= 0) {
1650 cageco = kcd->objects_info[ob_index].positions_cage[BM_elem_index_get(v)];
1651 }
1652 else {
1653 cageco = v->co;
1654 }
1655
1656 float cageco_ws[3];
1657 Object *ob = kcd->objects[ob_index];
1658 mul_v3_m4v3(cageco_ws, ob->object_to_world().ptr(), cageco);
1659
1660 kfv = new_knife_vert(kcd, v->co, cageco_ws);
1661 kfv->v = v;
1662 kfv->ob_index = ob_index;
1663
1664 kcd->origvertmap->add(v, kfv);
1665 BM_ITER_ELEM (f, &bmiter, v, BM_FACES_OF_VERT) {
1666 knife_append_list(kcd, &kfv->faces, f);
1667 }
1668 }
1669
1670 return kfv;
1671}
1672
1673/* Get a KnifeEdge wrapper for an existing BMEdge. */
1675{
1676 KnifeEdge *kfe = kcd->origedgemap->lookup_default(e, nullptr);
1677 if (!kfe) {
1678 BMIter bmiter;
1679 BMFace *f;
1680
1681 kfe = new_knife_edge(kcd);
1682 kfe->e = e;
1683 kfe->v1 = get_bm_knife_vert(kcd, e->v1, ob_index);
1684 kfe->v2 = get_bm_knife_vert(kcd, e->v2, ob_index);
1685
1686 knife_add_to_vert_edges(kcd, kfe);
1687
1688 kcd->origedgemap->add(e, kfe);
1689
1690 BM_ITER_ELEM (f, &bmiter, e, BM_FACES_OF_EDGE) {
1691 knife_append_list(kcd, &kfe->faces, f);
1692 }
1693 }
1694
1695 return kfe;
1696}
1697
1698static ListBaseT<LinkData> *knife_get_face_kedges(KnifeTool_OpData *kcd, int ob_index, BMFace *f)
1699{
1700 ListBaseT<LinkData> *list = kcd->kedgefacemap->lookup_default(f, nullptr);
1701
1702 if (!list) {
1703 BMIter bmiter;
1704 BMEdge *e;
1705
1706 list = knife_empty_list(kcd);
1707
1708 BM_ITER_ELEM (e, &bmiter, f, BM_EDGES_OF_FACE) {
1709 knife_append_list(kcd, list, get_bm_knife_edge(kcd, e, ob_index));
1710 }
1711
1712 kcd->kedgefacemap->add(f, list);
1713 }
1714
1715 return list;
1716}
1717
1719{
1720 knife_append_list(kcd, knife_get_face_kedges(kcd, kfe->v1->ob_index, f), kfe);
1721 knife_append_list(kcd, &kfe->faces, f);
1722}
1723
1725 KnifeEdge *kfe,
1726 const float co[3],
1727 const float cageco[3],
1728 KnifeEdge **r_kfe)
1729{
1730 KnifeEdge *newkfe = new_knife_edge(kcd);
1731 LinkData *ref;
1732 BMFace *f;
1733
1734 newkfe->v1 = kfe->v1;
1735 newkfe->v2 = new_knife_vert(kcd, co, cageco);
1736 newkfe->v2->ob_index = kfe->v1->ob_index;
1737 newkfe->v2->is_cut = true;
1738 if (kfe->e) {
1739 knife_add_edge_faces_to_vert(kcd, newkfe->v2, kfe->e);
1740 }
1741 else {
1742 /* kfe cuts across an existing face.
1743 * If v1 and v2 are in multiple faces together (e.g., if they
1744 * are in doubled polys) then this arbitrarily chooses one of them. */
1745 f = knife_find_common_face(&kfe->v1->faces, &kfe->v2->faces);
1746 if (f) {
1747 knife_append_list(kcd, &newkfe->v2->faces, f);
1748 }
1749 }
1750 newkfe->basef = kfe->basef;
1751
1752 ref = find_ref(&kfe->v1->edges, kfe);
1753 BLI_remlink(&kfe->v1->edges, ref);
1754
1755 kfe->v1 = newkfe->v2;
1756 kfe->v1->is_splitting = true;
1757 BLI_addtail(&kfe->v1->edges, ref);
1758
1759 for (LinkData &ref : kfe->faces) {
1760 knife_edge_append_face(kcd, newkfe, static_cast<BMFace *>(ref.data));
1761 }
1762
1763 knife_add_to_vert_edges(kcd, newkfe);
1764
1765 newkfe->is_cut = kfe->is_cut;
1766 newkfe->e = kfe->e;
1767
1768 newkfe->splits++;
1769 kfe->splits++;
1770
1771 kcd->undo->splits++;
1772
1773 BLI_stack_push(kcd->splitstack, static_cast<void *>(&kfe));
1774 BLI_stack_push(kcd->splitstack, static_cast<void *>(&newkfe));
1775
1776 *r_kfe = newkfe;
1777
1778 return newkfe->v2;
1779}
1780
1781/* Rejoin two edges split by #knife_split_edge. */
1782static void knife_join_edge(KnifeEdge *newkfe, KnifeEdge *kfe)
1783{
1784 newkfe->is_invalid = true;
1785 newkfe->v2->is_invalid = true;
1786
1787 kfe->v1 = newkfe->v1;
1788
1789 kfe->splits--;
1790 kfe->v1->is_splitting = false;
1791 kfe->v2->is_splitting = false;
1792}
1793
1795
1796/* -------------------------------------------------------------------- */
1799
1800static void knife_snap_curr(KnifeTool_OpData *kcd,
1801 const float2 &mval,
1802 const float3 &ray_orig,
1803 const float3 &ray_dir,
1804 const float3 *curr_cage_constrain,
1805 const float3 *fallback);
1806
1807/* User has just clicked for first time or first time after a restart (E key).
1808 * Copy the current position data into prev. */
1809static void knife_start_cut(KnifeTool_OpData *kcd, const float2 &mval)
1810{
1811 float3 ray_orig;
1812 float3 ray_dir;
1814 kcd->vc.depsgraph, kcd->region, kcd->vc.v3d, mval, ray_orig, ray_dir, false);
1815
1816 /* Make sure we have a reasonable `prev.cage` for #knife_snap_curr() to work with in case it
1817 * finds no hits. */
1818 if (kcd->prev.vert == nullptr && kcd->prev.edge == nullptr) {
1819 float ofs_local[3];
1820 negate_v3_v3(ofs_local, kcd->vc.rv3d->ofs);
1821 ED_view3d_win_to_3d(kcd->vc.v3d, kcd->region, ofs_local, mval, kcd->prev.cage);
1822 }
1823
1824 knife_snap_curr(kcd, mval, ray_orig, ray_dir, nullptr, nullptr);
1825 kcd->prev = kcd->curr;
1826 kcd->mdata.is_stored = false;
1827}
1828
1830{
1831 kpos->bmface = lh->f;
1832 kpos->vert = lh->v;
1833 kpos->edge = lh->kfe;
1834 copy_v3_v3(kpos->cage, lh->cagehit);
1835 copy_v2_v2(kpos->mval, lh->schit);
1836}
1837
1838/* Primary key: lambda along cut
1839 * Secondary key: lambda along depth
1840 * Tertiary key: pointer comparisons of verts if both snapped to verts
1841 */
1842static int linehit_compare(const KnifeLineHit &lh1, const KnifeLineHit &lh2)
1843{
1844 if (lh1.l < lh2.l) {
1845 return true;
1846 }
1847 if (lh1.l > lh2.l) {
1848 return false;
1849 }
1850 if (lh1.m < lh2.m) {
1851 return true;
1852 }
1853 if (lh1.m > lh2.m) {
1854 return false;
1855 }
1856 if (lh1.v < lh2.v) {
1857 return true;
1858 }
1859 if (lh1.v > lh2.v) {
1860 return false;
1861 }
1862 return false;
1863}
1864
1865/*
1866 * Sort linehits by distance along cut line, and secondarily from
1867 * front to back (from eye), and tertiarily by snap vertex,
1868 * and remove any duplicates.
1869 */
1871{
1872 bool is_double = false;
1873
1874 if (kcd->linehits.is_empty()) {
1875 return;
1876 }
1877
1878 std::ranges::sort(kcd->linehits, linehit_compare);
1879
1880 /* Remove any edge hits that are preceded or followed
1881 * by a vertex hit that is very near. Mark such edge hits using
1882 * l == -1 and then do another pass to actually remove.
1883 * Also remove all but one of a series of vertex hits for the same vertex. */
1884 const int64_t total_hits = kcd->linehits.size();
1885 for (int i = 0; i < total_hits; i++) {
1886 KnifeLineHit *lhi = &kcd->linehits[i];
1887 if (lhi->v == nullptr) {
1888 continue;
1889 }
1890
1891 for (int j = i - 1; j >= 0; j--) {
1892 KnifeLineHit *lhj = &kcd->linehits[j];
1893 if (!lhj->kfe || fabsf(lhi->l - lhj->l) > KNIFE_FLT_EPSBIG ||
1894 fabsf(lhi->m - lhj->m) > KNIFE_FLT_EPSBIG)
1895 {
1896 break;
1897 }
1898
1899 if (lhi->kfe == lhj->kfe) {
1900 lhj->l = -1.0f;
1901 is_double = true;
1902 }
1903 }
1904 for (int j = i + 1; j < total_hits; j++) {
1905 KnifeLineHit *lhj = &kcd->linehits[j];
1906 if (fabsf(lhi->l - lhj->l) > KNIFE_FLT_EPSBIG || fabsf(lhi->m - lhj->m) > KNIFE_FLT_EPSBIG) {
1907 break;
1908 }
1909 if ((lhj->kfe && (lhi->kfe == lhj->kfe)) || (lhi->v == lhj->v)) {
1910 lhj->l = -1.0f;
1911 is_double = true;
1912 }
1913 }
1914 }
1915
1916 if (is_double) {
1917 /* Delete-in-place loop: copying from pos j to pos i+1. */
1918 int i = 0;
1919 int j = 1;
1920 while (j < total_hits) {
1921 KnifeLineHit *lhi = &kcd->linehits[i];
1922 KnifeLineHit *lhj = &kcd->linehits[j];
1923 if (lhj->l == -1.0f) {
1924 j++; /* Skip copying this one. */
1925 }
1926 else {
1927 /* Copy unless a no-op. */
1928 if (lhi->l == -1.0f) {
1929 /* Could happen if linehits[0] is being deleted. */
1930 memcpy(&kcd->linehits[i], &kcd->linehits[j], sizeof(KnifeLineHit));
1931 }
1932 else {
1933 if (i + 1 != j) {
1934 memcpy(&kcd->linehits[i + 1], &kcd->linehits[j], sizeof(KnifeLineHit));
1935 }
1936 i++;
1937 }
1938 j++;
1939 }
1940 }
1941 kcd->linehits.resize(i + 1);
1942 }
1943}
1944
1945/* Add hit to list of hits in facehits[f], where facehits is a map, if not already there. */
1947 Map<BMFace *, ListBaseT<LinkData> *> &facehits,
1948 BMFace *f,
1949 KnifeLineHit *hit)
1950{
1951 ListBaseT<LinkData> *list = facehits.lookup_or_add_cb(f,
1952 [&]() { return knife_empty_list(kcd); });
1953 knife_append_list_no_dup(kcd, list, hit);
1954}
1955
1961 const KnifeLineHit *lh,
1962 const float co[3])
1963{
1964
1965 if (lh->v && lh->v->v) {
1966 BMLoop *l; /* side-of-loop */
1967 if ((l = BM_face_vert_share_loop(f, lh->v->v)) &&
1968 (BM_loop_point_side_of_loop_test(l, co) < 0.0f))
1969 {
1970 return true;
1971 }
1972 }
1973 else if (lh->kfe && lh->kfe->e) {
1974 BMLoop *l; /* side-of-edge */
1975 if ((l = BM_face_edge_share_loop(f, lh->kfe->e)) &&
1976 (BM_loop_point_side_of_edge_test(l, co) < 0.0f))
1977 {
1978 return true;
1979 }
1980 }
1981
1982 return false;
1983}
1984
1986 KnifeLineHit *lh1,
1987 KnifeLineHit *lh2,
1988 BMFace *f)
1989{
1990 KnifeEdge *kfe, *kfe2;
1991 BMEdge *e_base;
1992
1993 if ((lh1->v && lh1->v == lh2->v) || (lh1->kfe && lh1->kfe == lh2->kfe)) {
1994 return;
1995 }
1996
1997 /* If the cut is on an edge. */
1998 if ((lh1->v && lh2->v) && (lh1->v->v && lh2->v && lh2->v->v) &&
1999 (e_base = BM_edge_exists(lh1->v->v, lh2->v->v)))
2000 {
2001 return;
2002 }
2005 {
2006 return;
2007 }
2008
2009 /* Check if edge actually lies within face (might not, if this face is concave). */
2010 if ((lh1->v && !lh1->kfe) && (lh2->v && !lh2->kfe)) {
2011 if (!knife_verts_edge_in_face(lh1->v, lh2->v, f)) {
2012 return;
2013 }
2014 }
2015
2016 kfe = new_knife_edge(kcd);
2017 kfe->is_cut = true;
2018 kfe->basef = f;
2019
2020 if (lh1->v) {
2021 kfe->v1 = lh1->v;
2022 }
2023 else if (lh1->kfe) {
2024 kfe->v1 = knife_split_edge(kcd, lh1->kfe, lh1->hit, lh1->cagehit, &kfe2);
2025 lh1->v = kfe->v1; /* Record the #KnifeVert for this hit. */
2026 }
2027 else {
2028 BLI_assert(lh1->f);
2029 kfe->v1 = new_knife_vert(kcd, lh1->hit, lh1->cagehit);
2030 kfe->v1->ob_index = lh1->ob_index;
2031 kfe->v1->is_cut = true;
2032 knife_append_list(kcd, &kfe->v1->faces, lh1->f);
2033 lh1->v = kfe->v1; /* Record the #KnifeVert for this hit. */
2034 }
2035
2036 if (lh2->v) {
2037 kfe->v2 = lh2->v;
2038 }
2039 else if (lh2->kfe) {
2040 kfe->v2 = knife_split_edge(kcd, lh2->kfe, lh2->hit, lh2->cagehit, &kfe2);
2041 lh2->v = kfe->v2; /* Future uses of lh2 won't split again. */
2042 }
2043 else {
2044 BLI_assert(lh2->f);
2045 kfe->v2 = new_knife_vert(kcd, lh2->hit, lh2->cagehit);
2046 kfe->v2->ob_index = lh2->ob_index;
2047 kfe->v2->is_cut = true;
2048 knife_append_list(kcd, &kfe->v2->faces, lh2->f);
2049 lh2->v = kfe->v2; /* Record the KnifeVert for this hit. */
2050 }
2051
2052 knife_add_to_vert_edges(kcd, kfe);
2053
2054 if (kfe->basef && !find_ref(&kfe->faces, kfe->basef)) {
2055 knife_edge_append_face(kcd, kfe, kfe->basef);
2056 }
2057
2058 /* Update current undo frame cut count. */
2059 kcd->undo->cuts++;
2060}
2061
2062/* Given a list of KnifeLineHits for one face, sorted by l
2063 * and then by m, make the required KnifeVerts and
2064 * KnifeEdges.
2065 */
2066static void knife_cut_face(KnifeTool_OpData *kcd, BMFace *f, ListBaseT<LinkData> *hits)
2067{
2068 LinkData *r;
2069
2070 if (BLI_listbase_count_at_most(hits, 2) != 2) {
2071 return;
2072 }
2073
2074 for (r = hits->first(); r->next; r = r->next) {
2076 kcd, static_cast<KnifeLineHit *>(r->data), static_cast<KnifeLineHit *>(r->next->data), f);
2077 }
2078}
2079
2081 BMesh *bm,
2082 BMFace *f,
2083 ListBaseT<LinkData> *kfedges)
2084{
2085 KnifeEdge *kfe;
2086 int edge_array_len = kfedges->count();
2087 int i;
2088
2089 blender::Array<BMEdge *, BM_DEFAULT_TOPOLOGY_STACK_SIZE> edge_array_buf(edge_array_len);
2090 BMEdge **edge_array = edge_array_buf.data();
2091
2092 /* Point to knife edges we've created edges in, edge_array aligned. */
2094 KnifeEdge **kfe_array = kfe_array_buf.data();
2095
2096 BLI_assert(kcd->edgenet.edge_visit->is_empty());
2097
2098 i = 0;
2099 for (LinkData &ref : *kfedges) {
2100 bool is_new_edge = false;
2101 kfe = static_cast<KnifeEdge *>(ref.data);
2102
2103 if (kfe->is_invalid) {
2104 continue;
2105 }
2106
2107 if (kfe->e == nullptr) {
2108 if (kfe->v1->v && kfe->v2->v) {
2109 kfe->e = BM_edge_exists(kfe->v1->v, kfe->v2->v);
2110 }
2111 }
2112
2113 if (kfe->e) {
2114 if (BM_edge_in_face(kfe->e, f)) {
2115 /* Shouldn't happen, but in this case just ignore. */
2116 continue;
2117 }
2118 }
2119 else {
2120 if (kfe->v1->v == nullptr) {
2121 kfe->v1->v = BM_vert_create(bm, kfe->v1->co, nullptr, eBMCreateFlag(0));
2122 }
2123 if (kfe->v2->v == nullptr) {
2124 kfe->v2->v = BM_vert_create(bm, kfe->v2->co, nullptr, eBMCreateFlag(0));
2125 }
2126 BLI_assert(kfe->e == nullptr);
2127 kfe->e = BM_edge_create(bm, kfe->v1->v, kfe->v2->v, nullptr, eBMCreateFlag(0));
2128 if (kfe->e) {
2130 BM_edge_select_set(bm, kfe->e, true);
2131 }
2132 is_new_edge = true;
2133 }
2134 }
2135
2136 BLI_assert(kfe->e);
2137
2138 if (kcd->edgenet.edge_visit->add(kfe->e)) {
2139 kfe_array[i] = is_new_edge ? kfe : nullptr;
2140 edge_array[i] = kfe->e;
2141 i += 1;
2142 }
2143 }
2144
2145 if (i) {
2146 const int edge_array_len_orig = i;
2147 edge_array_len = i;
2148
2149#ifdef USE_NET_ISLAND_CONNECT
2150 uint edge_array_holes_len;
2151 BMEdge **edge_array_holes;
2153 f,
2154 edge_array,
2155 edge_array_len,
2156 true,
2157 kcd->edgenet.arena,
2158 &edge_array_holes,
2159 &edge_array_holes_len))
2160 {
2162 for (i = edge_array_len; i < edge_array_holes_len; i++) {
2163 BM_edge_select_set(bm, edge_array_holes[i], true);
2164 }
2165 }
2166
2167 edge_array_len = edge_array_holes_len;
2168 edge_array = edge_array_holes; /* Owned by the arena. */
2169 }
2170#endif
2171
2172 {
2173 BM_face_split_edgenet(bm, f, edge_array, edge_array_len, nullptr);
2174 }
2175
2176 /* Remove dangling edges, not essential - but nice for users. */
2177 for (i = 0; i < edge_array_len_orig; i++) {
2178 if (kfe_array[i] == nullptr) {
2179 continue;
2180 }
2181 if (BM_edge_is_wire(kfe_array[i]->e)) {
2182 BM_edge_kill(bm, kfe_array[i]->e);
2183 kfe_array[i]->e = nullptr;
2184 }
2185 }
2186
2187#ifdef USE_NET_ISLAND_CONNECT
2189#endif
2190 }
2191
2192 kcd->edgenet.edge_visit->clear();
2193}
2194
2195static int sort_verts_by_dist_cb(void *co_p, const void *cur_a_p, const void *cur_b_p)
2196{
2197 const KnifeVert *cur_a = static_cast<const KnifeVert *>(
2198 (static_cast<const LinkData *>(cur_a_p))->data);
2199 const KnifeVert *cur_b = static_cast<const KnifeVert *>(
2200 (static_cast<const LinkData *>(cur_b_p))->data);
2201 const float *co = static_cast<const float *>(co_p);
2202 const float a_sq = len_squared_v3v3(co, cur_a->co);
2203 const float b_sq = len_squared_v3v3(co, cur_b->co);
2204
2205 if (a_sq < b_sq) {
2206 return -1;
2207 }
2208 if (a_sq > b_sq) {
2209 return 1;
2210 }
2211 return 0;
2212}
2213
2214/* Use the network of KnifeEdges and KnifeVerts accumulated to make real BMVerts and BMEdedges. */
2215static void knife_make_cuts(KnifeTool_OpData *kcd, int ob_index)
2216{
2217 Object *ob = kcd->objects[ob_index];
2219 KnifeEdge *kfe;
2220 KnifeVert *kfv;
2221 BMEdge *enew;
2222 ListBaseT<LinkData> *list;
2223 float pct;
2224 BLI_mempool_iter iter;
2227
2228 /* Put list of cutting edges for a face into fhash, keyed by face. */
2229 BLI_mempool_iternew(kcd->kedges, &iter);
2230 for (kfe = static_cast<KnifeEdge *>(BLI_mempool_iterstep(&iter)); kfe;
2231 kfe = static_cast<KnifeEdge *>(BLI_mempool_iterstep(&iter)))
2232 {
2233 if (kfe->is_invalid || kfe->v1->ob_index != ob_index) {
2234 continue;
2235 }
2236
2237 /* Select edges that lie directly on the cut. */
2238 if (kcd->select_result) {
2239 if (kfe->e && kfe->is_cut) {
2240 BM_edge_select_set(bm, kfe->e, true);
2241 }
2242 }
2243
2244 BMFace *f = kfe->basef;
2245 if (!f || kfe->e) {
2246 continue;
2247 }
2248 list = fhash.lookup_default(f, nullptr);
2249 if (!list) {
2250 list = knife_empty_list(kcd);
2251 fhash.add(f, list);
2252 }
2253 knife_append_list(kcd, list, kfe);
2254 }
2255
2256 /* Put list of splitting vertices for an edge into ehash, keyed by edge. */
2257 BLI_mempool_iternew(kcd->kverts, &iter);
2258 for (kfv = static_cast<KnifeVert *>(BLI_mempool_iterstep(&iter)); kfv;
2259 kfv = static_cast<KnifeVert *>(BLI_mempool_iterstep(&iter)))
2260 {
2261 if (kfv->v || kfv->is_invalid || kfv->ob_index != ob_index) {
2262 continue; /* Already have a BMVert. */
2263 }
2264 for (LinkData &ref : kfv->edges) {
2265 kfe = static_cast<KnifeEdge *>(ref.data);
2266 BMEdge *e = kfe->e;
2267 if (!e) {
2268 continue;
2269 }
2270 list = ehash.lookup_default(e, nullptr);
2271 if (!list) {
2272 list = knife_empty_list(kcd);
2273 ehash.add(e, list);
2274 }
2275 /* There can be more than one kfe in kfv's list with same e. */
2276 if (!find_ref(list, kfv)) {
2277 knife_append_list(kcd, list, kfv);
2278 }
2279 }
2280 }
2281
2282 /* Split bmesh edges where needed. */
2283 for (auto [e, list] : ehash.items()) {
2285
2286 for (LinkData &ref : *list) {
2287 kfv = static_cast<KnifeVert *>(ref.data);
2288 pct = line_point_factor_v3(kfv->co, e->v1->co, e->v2->co);
2289 kfv->v = BM_edge_split(bm, e, e->v1, &enew, pct);
2290 }
2291 }
2292
2293 if (kcd->only_select) {
2295 }
2296
2297 /* Do cuts for each face. */
2298 for (auto [f, list] : fhash.items()) {
2299 knife_make_face_cuts(kcd, bm, f, list);
2300 }
2301}
2302
2303/* User has just left-clicked after the first time.
2304 * Add all knife cuts implied by line from prev to curr.
2305 * If that line crossed edges then kcd->linehits will be non-null.
2306 * Make all of the KnifeVerts and KnifeEdges implied by this cut.
2307 */
2309{
2310
2311 /* Allocate new undo frame on stack, unless cut is being dragged. */
2312 if (!kcd->is_drag_undo) {
2313 kcd->undo = static_cast<KnifeUndoFrame *>(BLI_stack_push_r(kcd->undostack));
2314 kcd->undo->pos = kcd->prev;
2315 kcd->undo->cuts = 0;
2316 kcd->undo->splits = 0;
2317 kcd->undo->mdata = kcd->mdata;
2318 kcd->is_drag_undo = true;
2319 }
2320
2321 /* Save values for angle drawing calculations. */
2322 copy_v3_v3(kcd->mdata.cage, kcd->prev.cage);
2323 copy_v2_v2(kcd->mdata.mval, kcd->prev.mval);
2324 kcd->mdata.is_stored = true;
2325
2327 if (kcd->linehits.is_empty()) {
2328 if (kcd->is_drag_hold == false) {
2329 kcd->prev = kcd->curr;
2330 }
2331 return;
2332 }
2333
2334 /* Consider most recent linehit in angle drawing calculations. */
2335 if (kcd->linehits.size() >= 2) {
2336 copy_v3_v3(kcd->mdata.cage, kcd->linehits[kcd->linehits.size() - 2].cagehit);
2337 }
2338
2339 /* Make facehits: map face -> list of linehits touching it. */
2341 for (KnifeLineHit &hit : kcd->linehits) {
2342 KnifeLineHit *lh = &hit;
2343 if (lh->f) {
2344 add_hit_to_facehits(kcd, facehits, lh->f, lh);
2345 }
2346 if (lh->v) {
2347 for (LinkData &r : lh->v->faces) {
2348 add_hit_to_facehits(kcd, facehits, static_cast<BMFace *>(r.data), lh);
2349 }
2350 }
2351 if (lh->kfe) {
2352 for (LinkData &r : lh->kfe->faces) {
2353 add_hit_to_facehits(kcd, facehits, static_cast<BMFace *>(r.data), lh);
2354 }
2355 }
2356 }
2357
2358 /* NOTE: as following loop progresses, the 'v' fields of
2359 * the linehits will be filled in (as edges are split or
2360 * in-face verts are made), so it may be true that both
2361 * the v and the kfe or f fields will be non-null. */
2362 for (const auto &item : facehits.items()) {
2363 BMFace *f = item.key;
2364 ListBaseT<LinkData> *list = item.value;
2365 knife_cut_face(kcd, f, list);
2366 }
2367
2368 /* Set up for next cut. */
2369 kcd->prev = kcd->curr;
2370
2371 if (kcd->prev.bmface) {
2372 /* Was "in face" but now we have a KnifeVert it is snapped to. */
2373 KnifeLineHit *lh = &kcd->linehits.last();
2374 kcd->prev.vert = lh->v;
2375 kcd->prev.bmface = nullptr;
2376 }
2377
2378 if (kcd->is_drag_hold) {
2379 KnifeLineHit *lh = &kcd->linehits.last();
2380 linehit_to_knifepos(&kcd->prev, lh);
2381 }
2382
2383 kcd->linehits.clear_and_shrink();
2384}
2385
2387{
2388 kcd->linehits.clear_and_shrink();
2389}
2390
2392
2393/* -------------------------------------------------------------------- */
2396
2397/* Record the index in kcd->em->looptris of first looptri triple for a given face,
2398 * given an index for some triple in that array.
2399 * This assumes that all of the triangles for a given face are contiguous
2400 * in that array (as they are by the current tessellation routines).
2401 * Actually store index + 1 in the hash, because 0 looks like "no entry"
2402 * to hash lookup routine; will reverse this in the get routine.
2403 * Doing this lazily rather than all at once for all faces.
2404 */
2406{
2407 int i;
2408
2409 if (kcd->facetrimap->contains(f)) {
2410 return;
2411 }
2412
2413 BLI_assert(index >= 0 && index < em->looptris.size());
2414 BLI_assert(em->looptris[index][0]->f == f);
2415 for (i = index - 1; i >= 0; i--) {
2416 if (em->looptris[i][0]->f != f) {
2417 i++;
2418 break;
2419 }
2420 }
2421 if (i == -1) {
2422 i++;
2423 }
2424
2425 kcd->facetrimap->add(f, i + 1);
2426}
2427
2428/* This should only be called for faces that have had a lowest face tri set by previous function.
2429 */
2431{
2432 int ans;
2433
2434 ans = kcd->facetrimap->lookup(f);
2435 BLI_assert(ans != 0);
2436 return ans - 1;
2437}
2438
2448 const float s[2],
2449 const float v1[3],
2450 const float v2[3],
2451 int ob_index,
2452 BMFace *f,
2453 const float face_tol_sq,
2454 float hit_co[3],
2455 float hit_cageco[3])
2456{
2457 Object *ob = kcd->objects[ob_index];
2459
2460 int tottri, tri_i;
2461 float raydir[3];
2462 float tri_norm[3], tri_plane[4];
2463 float se1[2], se2[2];
2464 float d, lambda;
2465 ListBaseT<LinkData> *list;
2466 KnifeEdge *kfe;
2467
2468 sub_v3_v3v3(raydir, v2, v1);
2469 normalize_v3(raydir);
2470 tri_i = get_lowest_face_tri(kcd, f);
2471 tottri = em->looptris.size();
2472 BLI_assert(tri_i >= 0 && tri_i < tottri);
2473
2474 for (; tri_i < tottri; tri_i++) {
2475 float tri_cos[3][3];
2476 float ray_tri_uv[2];
2477
2478 const std::array<BMLoop *, 3> &ltri = em->looptris[tri_i];
2479 if (ltri[0]->f != f) {
2480 break;
2481 }
2482
2483 knife_bm_tri_cagecos_get_worldspace(kcd, ob_index, tri_i, tri_cos);
2484
2485 /* Using epsilon test in case ray is directly through an internal
2486 * tessellation edge and might not hit either tessellation tri with
2487 * an exact test;
2488 * We will exclude hits near real edges by a later test. */
2489 if (isect_ray_tri_epsilon_v3(v1, raydir, UNPACK3(tri_cos), &lambda, ray_tri_uv, KNIFE_FLT_EPS))
2490 {
2491 /* Check if line coplanar with tri. */
2492 normal_tri_v3(tri_norm, UNPACK3(tri_cos));
2493 plane_from_point_normal_v3(tri_plane, tri_cos[0], tri_norm);
2494 if ((dist_squared_to_plane_v3(v1, tri_plane) < KNIFE_FLT_EPS) &&
2496 {
2497 return false;
2498 }
2499 interp_v3_v3v3v3_uv(hit_cageco, UNPACK3(tri_cos), ray_tri_uv);
2500 /* Now check that far enough away from verts and edges. */
2501 list = knife_get_face_kedges(kcd, ob_index, f);
2502 for (LinkData &ref : *list) {
2503 kfe = static_cast<KnifeEdge *>(ref.data);
2504 if (kfe->is_invalid) {
2505 continue;
2506 }
2507 knife_project_v2(kcd, kfe->v1->cageco, se1);
2508 knife_project_v2(kcd, kfe->v2->cageco, se2);
2509 d = dist_squared_to_line_segment_v2(s, se1, se2);
2510 if (d < face_tol_sq) {
2511 return false;
2512 }
2513 }
2514 interp_v3_v3v3v3_uv(hit_co, ltri[0]->v->co, ltri[1]->v->co, ltri[2]->v->co, ray_tri_uv);
2515 return true;
2516 }
2517 }
2518 return false;
2519}
2520
2526{
2527 Object *ob;
2528 BMIter iter;
2529 BMVert *v;
2530 float min[3], max[3];
2531 float ws[3];
2533
2534 for (int ob_index = 0; ob_index < kcd->objects.size(); ob_index++) {
2535 ob = kcd->objects[ob_index];
2537
2538 const Span<float3> positions_cage = kcd->objects_info[ob_index].positions_cage;
2539 if (!positions_cage.is_empty()) {
2540 for (int i = 0; i < bm->totvert; i++) {
2541 copy_v3_v3(ws, positions_cage[i]);
2542 mul_m4_v3(ob->object_to_world().ptr(), ws);
2543 minmax_v3v3_v3(min, max, ws);
2544 }
2545 }
2546 else {
2547 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
2548 copy_v3_v3(ws, v->co);
2549 mul_m4_v3(ob->object_to_world().ptr(), ws);
2550 minmax_v3v3_v3(min, max, ws);
2551 }
2552 }
2553 }
2554
2555 kcd->ortho_extent = len_v3v3(min, max) / 2;
2557}
2558
2559/* Do edges e1 and e2 go between exactly the same coordinates? */
2560static bool coinciding_edges(BMEdge *e1, BMEdge *e2)
2561{
2562 const float *co11, *co12, *co21, *co22;
2563
2564 co11 = e1->v1->co;
2565 co12 = e1->v2->co;
2566 co21 = e2->v1->co;
2567 co22 = e2->v2->co;
2568 if ((equals_v3v3(co11, co21) && equals_v3v3(co12, co22)) ||
2569 (equals_v3v3(co11, co22) && equals_v3v3(co12, co21)))
2570 {
2571 return true;
2572 }
2573 return false;
2574}
2575
2576/* Callback used in point_is_visible to exclude hits on the faces that are the same
2577 * as or contain the hitting element (which is in user_data).
2578 * Also (see #44492) want to exclude hits on faces that butt up to the hitting element
2579 * (e.g., when you double an edge by an edge split).
2580 */
2581static bool bm_ray_cast_cb_elem_not_in_face_check(BMFace *f, void *user_data)
2582{
2583 bool ans;
2584 BMEdge *e, *e2;
2585 BMIter iter;
2586
2587 switch ((static_cast<BMElem *>(user_data))->head.htype) {
2588 case BM_FACE:
2589 ans = static_cast<BMFace *>(user_data) != f;
2590 break;
2591 case BM_EDGE:
2592 e = static_cast<BMEdge *>(user_data);
2593 ans = !BM_edge_in_face(e, f);
2594 if (ans) {
2595 /* Is it a boundary edge, coincident with a split edge? */
2596 if (BM_edge_is_boundary(e)) {
2597 BM_ITER_ELEM (e2, &iter, f, BM_EDGES_OF_FACE) {
2598 if (coinciding_edges(e, e2)) {
2599 ans = false;
2600 break;
2601 }
2602 }
2603 }
2604 }
2605 break;
2606 case BM_VERT:
2607 ans = !BM_vert_in_face(static_cast<BMVert *>(user_data), f);
2608 break;
2609 default:
2610 ans = true;
2611 break;
2612 }
2613 return ans;
2614}
2615
2624 const float p[3],
2625 const float s[2],
2626 BMElem *ele_test)
2627{
2628 BMFace *f_hit;
2629
2630 /* Reject points that lie behind the viewpoint (perspective views only). */
2631 if (!kcd->is_ortho && mul_project_m4_v3_zfac(kcd->vc.rv3d->persmat, p) <= 0.0f) {
2632 return false;
2633 }
2634
2635 /* If box clipping on, make sure p is not clipped. */
2636 if (RV3D_CLIPPING_ENABLED(kcd->vc.v3d, kcd->vc.rv3d) &&
2637 ED_view3d_clipping_test(kcd->vc.rv3d, p, false))
2638 {
2639 return false;
2640 }
2641
2642 /* If not cutting through, make sure no face is in front of p. */
2643 if (!kcd->cut_through) {
2644 float dist;
2645 float view[3], p_ofs[3];
2646
2647 /* TODO: I think there's a simpler way to get the required raycast ray. */
2648 ED_view3d_unproject_v3(kcd->vc.region, s[0], s[1], 0.0f, view);
2649
2650 /* Make p_ofs a little towards view, so ray doesn't hit p's face. */
2651 sub_v3_v3(view, p);
2652 dist = normalize_v3(view);
2653 copy_v3_v3(p_ofs, p);
2654
2655 /* Avoid projecting behind the viewpoint. */
2656 if (kcd->is_ortho && (kcd->vc.rv3d->persp != RV3D_CAMOB)) {
2657 dist = kcd->vc.v3d->clip_end * 2.0f;
2658 }
2659
2660 if (RV3D_CLIPPING_ENABLED(kcd->vc.v3d, kcd->vc.rv3d)) {
2661 float view_clip[2][3];
2662 /* NOTE: view_clip[0] should never get clipped. */
2663 copy_v3_v3(view_clip[0], p_ofs);
2664 madd_v3_v3v3fl(view_clip[1], p_ofs, view, dist);
2665
2667 view_clip[0], view_clip[1], kcd->vc.rv3d->clip_local, 6, view_clip[0], view_clip[1]))
2668 {
2669 dist = len_v3v3(p_ofs, view_clip[1]);
2670 }
2671 }
2672
2673 /* See if there's a face hit between p1 and the view. */
2674 if (ele_test) {
2675 f_hit = knife_bvh_raycast_filter(kcd,
2676 p_ofs,
2677 view,
2679 &dist,
2680 nullptr,
2681 nullptr,
2683 ele_test);
2684 }
2685 else {
2686 f_hit = knife_bvh_raycast(kcd, p_ofs, view, KNIFE_FLT_EPS, &dist, nullptr, nullptr);
2687 }
2688
2689 if (f_hit) {
2690 return false;
2691 }
2692 }
2693
2694 return true;
2695}
2696
2697/* Clip the line (v1, v2) to planes perpendicular to it and distances d from
2698 * the closest point on the line to the origin. */
2699static void clip_to_ortho_planes(float v1[3], float v2[3], const float center[3], const float d)
2700{
2701 float closest[3], dir[3];
2702
2703 sub_v3_v3v3(dir, v1, v2);
2704 normalize_v3(dir);
2705
2706 /* could be v1 or v2 */
2707 sub_v3_v3(v1, center);
2709 add_v3_v3(closest, center);
2710
2711 madd_v3_v3v3fl(v1, closest, dir, d);
2712 madd_v3_v3v3fl(v2, closest, dir, -d);
2713}
2714
2716 float s1[2],
2717 float s2[2],
2718 float sco[2],
2719 float cage[3],
2720 int ob_index,
2721 KnifeVert *v,
2722 KnifeEdge *kfe,
2723 KnifeLineHit *r_hit)
2724{
2725 memset(r_hit, 0, sizeof(*r_hit));
2726 copy_v3_v3(r_hit->cagehit, cage);
2727 copy_v2_v2(r_hit->schit, sco);
2728 r_hit->ob_index = ob_index;
2729
2730 /* Find position along screen line, used for sorting. */
2731 r_hit->l = len_v2v2(sco, s1) / len_v2v2(s2, s1);
2732
2733 r_hit->m = dot_m4_v3_row_z(kcd->vc.rv3d->persmatob, cage);
2734
2735 r_hit->v = v;
2736
2737 /* If this isn't from an existing BMVert, it may have been added to a BMEdge originally.
2738 * Knowing if the hit comes from an edge is important for edge-in-face checks later on.
2739 * See: #knife_add_single_cut -> #knife_verts_edge_in_face, #42611. */
2740 r_hit->kfe = kfe;
2741
2742 if (v) {
2743 copy_v3_v3(r_hit->hit, v->co);
2744 }
2745 else if (kfe) {
2747 r_hit->hit, cage, kfe->v1->co, kfe->v2->co, kfe->v1->cageco, kfe->v2->cageco);
2748 }
2749}
2750
2752 float s1[2],
2753 float s2[2],
2754 float sco[2],
2755 float ray_start[3],
2756 float ray_end[3],
2757 int ob_index,
2758 BMFace *f,
2759 float face_tol_sq,
2760 KnifeLineHit *r_hit)
2761{
2762 float3 p, cage;
2763 if (!knife_ray_intersect_face(kcd, sco, ray_start, ray_end, ob_index, f, face_tol_sq, p, cage)) {
2764 return false;
2765 }
2766 if (!point_is_visible(kcd, cage, sco, reinterpret_cast<BMElem *>(f))) {
2767 return false;
2768 }
2769 knife_linehit_set(kcd, s1, s2, sco, cage, ob_index, nullptr, nullptr, r_hit);
2770 copy_v3_v3(r_hit->hit, p);
2771 r_hit->f = f;
2772 return true;
2773}
2774
2775/* Finds visible (or all, if cutting through) edges that intersects the current screen drag line.
2776 */
2778{
2779 float3 v1, v2;
2780 float2 s1, s2;
2781 int *results, *result;
2782 ListBaseT<LinkData> *list;
2783 KnifeLineHit hit;
2784 float s[2], se1[2], se2[2];
2785 float d1, d2;
2786 float vert_tol, vert_tol_sq;
2787 float line_tol, line_tol_sq;
2788 float face_tol, face_tol_sq;
2789 uint tot;
2790 int i;
2791
2792 kcd->linehits.clear_and_shrink();
2793
2794 copy_v3_v3(v1, kcd->prev.cage);
2795 copy_v3_v3(v2, kcd->curr.cage);
2796
2797 /* Project screen line's 3d coordinates back into 2d. */
2798 knife_project_v2(kcd, v1, s1);
2799 knife_project_v2(kcd, v2, s2);
2800
2801 if (kcd->is_interactive) {
2802 if (len_squared_v2v2(s1, s2) < 1.0f) {
2803 return;
2804 }
2805 }
2806 else {
2808 return;
2809 }
2810 }
2811
2812 float4 plane;
2813 {
2814 if (kcd->is_ortho) {
2815 cross_v3_v3v3(plane, v2 - v1, kcd->vc.rv3d->viewinv[2]);
2816 }
2817 else {
2818 float3 orig = kcd->vc.rv3d->viewinv[3];
2819 float3 o_v1 = v1 - orig;
2820 float3 o_v2 = v2 - orig;
2821 cross_v3_v3v3(plane, o_v1, o_v2);
2822 }
2823 plane_from_point_normal_v3(plane, v1, plane);
2824 }
2825
2826 /* First use BVH tree to find faces, knife edges, and knife verts that might
2827 * intersect the cut plane with rays v1-v3 and v2-v4.
2828 * This de-duplicates the candidates before doing more expensive intersection tests. */
2829
2830 results = BLI_bvhtree_intersect_plane(kcd->bvh.tree, plane, &tot);
2831 if (!results) {
2832 return;
2833 }
2834
2835 Set<BMFace *> faces;
2837 Set<KnifeEdge *> kfes;
2838 Set<KnifeVert *> kfvs;
2839
2840 Object *ob;
2841 BMEditMesh *em;
2842
2843 for (i = 0, result = results; i < tot; i++, result++) {
2844 uint ob_index = 0;
2845 BMLoop *const *ltri = nullptr;
2846 for (ob_index = 0; ob_index < kcd->objects.size(); ob_index++) {
2847 ob = kcd->objects[ob_index];
2848 em = BKE_editmesh_from_object(ob);
2849 if (*result >= 0 && *result < em->looptris.size()) {
2850 ltri = em->looptris[*result].data();
2851 break;
2852 }
2853 *result -= em->looptris.size();
2854 }
2855 BLI_assert(ltri != nullptr);
2856 BMFace *f = ltri[0]->f;
2857 set_lowest_face_tri(kcd, em, f, *result);
2858
2859 /* Occlude but never cut unselected faces (when only_select is used). */
2860 if (kcd->only_select && !BM_elem_flag_test(f, BM_ELEM_SELECT)) {
2861 continue;
2862 }
2863 /* For faces, store index of lowest hit looptri in hash. */
2864 if (faces.contains(f)) {
2865 continue;
2866 }
2867 /* Don't care what the value is except that it is non-null, for iterator. */
2868 faces.add(f);
2869 fobs.add(f, ob_index);
2870
2871 list = knife_get_face_kedges(kcd, ob_index, f);
2872 for (LinkData &ref : *list) {
2873 KnifeEdge *kfe = static_cast<KnifeEdge *>(ref.data);
2874 if (kfe->is_invalid) {
2875 continue;
2876 }
2877 if (kfes.contains(kfe)) {
2878 continue;
2879 }
2880 kfes.add(kfe);
2881 kfvs.add(kfe->v1);
2882 kfvs.add(kfe->v2);
2883 }
2884 }
2885
2886 /* Now go through the candidates and find intersections. */
2887 /* These tolerances, in screen space, are for intermediate hits,
2888 * as ends are already snapped to screen. */
2889
2890 if (kcd->is_interactive) {
2891 vert_tol = KNIFE_FLT_EPS_PX_VERT;
2892 line_tol = KNIFE_FLT_EPS_PX_EDGE;
2893 face_tol = KNIFE_FLT_EPS_PX_FACE;
2894 }
2895 else {
2896 /* Use 1/100th of a pixel, see #43896 (too big), #47910 (too small).
2897 *
2898 * Update, leave this as is until we investigate not using pixel coords
2899 * for geometry calculations: #48023. */
2900 vert_tol = line_tol = face_tol = 0.5f;
2901 }
2902
2903 vert_tol_sq = vert_tol * vert_tol;
2904 line_tol_sq = line_tol * line_tol;
2905 face_tol_sq = face_tol * face_tol;
2906
2907 /* Assume these tolerances swamp floating point rounding errors in calculations below. */
2908
2909 /* First look for vertex hits. */
2910 Vector<KnifeLineHit> linehits;
2911 for (KnifeVert *v : kfvs) {
2912 KnifeEdge *kfe_hit = nullptr;
2913
2914 bool kfv_is_in_cut = false;
2915 if (ELEM(v, kcd->prev.vert, kcd->curr.vert)) {
2916 /* This KnifeVert was captured by the snap system.
2917 * Since the tolerance distance can be different, add this vertex directly.
2918 * Otherwise, the cut may fail or a close cut on a connected edge can be performed. */
2919 bm_elem_from_knife_vert(v, &kfe_hit);
2920 copy_v2_v2(s, (v == kcd->prev.vert) ? kcd->prev.mval : kcd->curr.mval);
2921 kfv_is_in_cut = true;
2922 }
2923 else {
2924 knife_project_v2(kcd, v->cageco, s);
2925 float d = dist_squared_to_line_segment_v2(s, s1, s2);
2926 if ((d <= vert_tol_sq) &&
2927 point_is_visible(kcd, v->cageco, s, bm_elem_from_knife_vert(v, &kfe_hit)))
2928 {
2929 kfv_is_in_cut = true;
2930 }
2931 }
2932
2933 if (kfv_is_in_cut) {
2934 knife_linehit_set(kcd, s1, s2, s, v->cageco, v->ob_index, v, kfe_hit, &hit);
2935 linehits.append(hit);
2936 }
2937 else {
2938 /* This vertex isn't used so remove from `kfvs`.
2939 * This is useful to detect KnifeEdges that can be skipped.
2940 * And it optimizes iteration a little bit. */
2941 kfvs.remove(v);
2942 }
2943 }
2944
2945 /* Now edge hits; don't add if a vertex at end of edge should have hit. */
2946 for (KnifeEdge *kfe : kfes) {
2947 /* If we intersect any of the vertices, don't attempt to intersect the edge. */
2948 if (kfvs.contains(kfe->v1) || kfvs.contains(kfe->v2)) {
2949 continue;
2950 }
2951
2952 knife_project_v2(kcd, kfe->v1->cageco, se1);
2953 knife_project_v2(kcd, kfe->v2->cageco, se2);
2954 float3 p_cage;
2955 float2 p_cage_ss;
2956 bool kfe_is_in_cut = false;
2957 if (kfe == kcd->prev.edge) {
2958 /* This KnifeEdge was captured by the snap system. */
2959 p_cage = kcd->prev.cage;
2960 p_cage_ss = kcd->prev.mval;
2961 kfe_is_in_cut = true;
2962 }
2963 else if (kfe == kcd->curr.edge) {
2964 /* This KnifeEdge was captured by the snap system. */
2965 p_cage = kcd->curr.cage;
2966 p_cage_ss = kcd->curr.mval;
2967 kfe_is_in_cut = true;
2968 }
2969 else {
2970 int isect_kind = isect_seg_seg_v2_point_ex(s1, s2, se1, se2, 0.0f, p_cage_ss);
2971 if (isect_kind == -1) {
2972 /* isect_seg_seg_v2_point doesn't do tolerance test around ends of s1-s2. */
2973 closest_to_line_segment_v2(p_cage_ss, s1, se1, se2);
2974 if (len_squared_v2v2(p_cage_ss, s1) <= line_tol_sq) {
2975 isect_kind = 1;
2976 }
2977 else {
2978 closest_to_line_segment_v2(p_cage_ss, s2, se1, se2);
2979 if (len_squared_v2v2(p_cage_ss, s2) <= line_tol_sq) {
2980 isect_kind = 1;
2981 }
2982 }
2983 }
2984 if (isect_kind == 1) {
2985 d1 = len_v2v2(p_cage_ss, se1);
2986 d2 = len_v2v2(se2, se1);
2987 if (!(d1 <= line_tol || d2 <= line_tol || fabsf(d1 - d2) <= line_tol)) {
2988 /* Can't just interpolate between ends of `kfe` because
2989 * that doesn't work with perspective transformation. */
2990 float lambda;
2991 float3 kfe_dir = kfe->v2->cageco - kfe->v1->cageco;
2992 if (isect_ray_plane_v3(kfe->v1->cageco, kfe_dir, plane, &lambda, false)) {
2993 p_cage = kfe->v1->cageco + kfe_dir * lambda;
2994 if (point_is_visible(kcd, p_cage, p_cage_ss, bm_elem_from_knife_edge(kfe))) {
2995 if (kcd->snap_midpoints) {
2996 /* Choose intermediate point snap too. */
2997 mid_v3_v3v3(p_cage, kfe->v1->cageco, kfe->v2->cageco);
2998 mid_v2_v2v2(p_cage_ss, se1, se2);
2999 }
3000 kfe_is_in_cut = true;
3001 }
3002 }
3003 }
3004 }
3005 }
3006 if (kfe_is_in_cut) {
3007 knife_linehit_set(kcd, s1, s2, p_cage_ss, p_cage, kfe->v1->ob_index, nullptr, kfe, &hit);
3008 linehits.append(hit);
3009 }
3010 }
3011
3012 /* Now face hits; don't add if a vertex or edge in face should have hit, except
3013 * in the case where cut through is enabled since skipping face hits at vertex or edge
3014 * cuts would result in incomplete cuts. See #158104. */
3015 const bool use_hit_prev = (kcd->prev.vert == nullptr && kcd->prev.edge == nullptr) ||
3016 kcd->cut_through;
3017 const bool use_hit_curr = ((kcd->curr.vert == nullptr && kcd->curr.edge == nullptr) ||
3018 kcd->cut_through) &&
3019 !kcd->is_drag_hold;
3020 if (use_hit_prev || use_hit_curr) {
3021 float3 v3, v4;
3022
3023 /* Unproject screen line. */
3025 kcd->vc.depsgraph, kcd->region, kcd->vc.v3d, s1, v1, v3, true);
3027 kcd->vc.depsgraph, kcd->region, kcd->vc.v3d, s2, v2, v4, true);
3028
3029 /* Numeric error, 'v1' -> 'v2', 'v2' -> 'v4'
3030 * can end up being ~2000 units apart with an orthogonal perspective.
3031 *
3032 * (from ED_view3d_win_to_segment_clipped() above)
3033 * This gives precision error; rather than solving properly
3034 * (which may involve using doubles everywhere!),
3035 * limit the distance between these points. */
3036 if (kcd->is_ortho && (kcd->vc.rv3d->persp != RV3D_CAMOB)) {
3037 if (kcd->ortho_extent == 0.0f) {
3038 calc_ortho_extent(kcd);
3039 }
3040 clip_to_ortho_planes(v1, v3, kcd->ortho_extent_center, kcd->ortho_extent + 10.0f);
3042 }
3043
3044 for (BMFace *f : faces) {
3045 int ob_index = fobs.lookup(f);
3046 if (use_hit_prev &&
3047 knife_linehit_face_test(kcd, s1, s2, s1, v1, v3, ob_index, f, face_tol_sq, &hit))
3048 {
3049 linehits.append(hit);
3050 }
3051
3052 if (use_hit_curr &&
3053 knife_linehit_face_test(kcd, s1, s2, s2, v2, v4, ob_index, f, face_tol_sq, &hit))
3054 {
3055 linehits.append(hit);
3056 }
3057 }
3058 }
3059
3060 kcd->linehits = std::move(linehits);
3061
3062 MEM_delete(results);
3063}
3064
3066
3067/* -------------------------------------------------------------------- */
3070
3072{
3073 zero_v3(kpd->cage);
3074 kpd->vert = nullptr;
3075 kpd->edge = nullptr;
3076 kpd->bmface = nullptr;
3077 kpd->ob_index = -1;
3078 zero_v2(kpd->mval);
3079}
3080
3082
3083/* -------------------------------------------------------------------- */
3086
3088 const float2 &mval,
3089 const float3 &ray_orig,
3090 const float3 &ray_dir,
3091 KnifePosData *r_kpd)
3092{
3093 float3 cage;
3094 int ob_index;
3095 BMFace *f;
3096 float dist = KMAXDIST;
3097
3098 f = knife_bvh_raycast(kcd, ray_orig, ray_dir, 0.0f, nullptr, cage, &ob_index);
3099
3100 if (f && kcd->only_select && BM_elem_flag_test(f, BM_ELEM_SELECT) == 0) {
3101 f = nullptr;
3102 ob_index = -1;
3103 }
3104
3105 if (f == nullptr) {
3106 if (kcd->is_interactive) {
3107 /* Try to use back-buffer selection method if ray casting failed.
3108 *
3109 * Apply the mouse coordinates to a copy of the view-context
3110 * since we don't want to rely on this being set elsewhere. */
3111 ViewContext vc = kcd->vc;
3112 vc.mval[0] = int(mval[0]);
3113 vc.mval[1] = int(mval[1]);
3114
3116 f = EDBM_face_find_nearest(&vc, &dist);
3117 }
3118
3119 if (f) {
3120 /* Cheat for now; just put in the origin instead
3121 * of a true coordinate on the face.
3122 * This just puts a point 1.0f in front of the view. */
3123 cage = ray_orig + ray_dir;
3124
3125 ob_index = 0;
3126 BLI_assert(ob_index == kcd->objects.first_index_of_try(vc.obact));
3127 }
3128 }
3129 }
3130
3131 if (f) {
3132 r_kpd->cage = cage;
3133 r_kpd->bmface = f;
3134 r_kpd->ob_index = ob_index;
3135 r_kpd->mval = mval;
3136
3137 return true;
3138 }
3139 return false;
3140}
3141
3149 KnifeTool_OpData *kcd, const float radius, int ob_index, BMFace *f, const float cageco[3])
3150{
3151 const float radius_sq = radius * radius;
3152 ListBaseT<LinkData> *list;
3153 float sco[2];
3154 float dis_sq;
3155 int c = 0;
3156
3157 knife_project_v2(kcd, cageco, sco);
3158
3159 list = knife_get_face_kedges(kcd, ob_index, f);
3160 for (LinkData &ref : *list) {
3161 KnifeEdge *kfe = static_cast<KnifeEdge *>(ref.data);
3162 int i;
3163
3164 if (kfe->is_invalid) {
3165 continue;
3166 }
3167
3168 for (i = 0; i < 2; i++) {
3169 KnifeVert *kfv = i ? kfe->v2 : kfe->v1;
3170 float kfv_sco[2];
3171
3172 if (kfv->is_invalid) {
3173 continue;
3174 }
3175
3176 knife_project_v2(kcd, kfv->cageco, kfv_sco);
3177
3178 dis_sq = len_squared_v2v2(kfv_sco, sco);
3179 if (dis_sq < radius_sq) {
3180 if (RV3D_CLIPPING_ENABLED(kcd->vc.v3d, kcd->vc.rv3d)) {
3181 if (ED_view3d_clipping_test(kcd->vc.rv3d, kfv->cageco, false) == 0) {
3182 c++;
3183 }
3184 }
3185 else {
3186 c++;
3187 }
3188 }
3189 }
3190 }
3191
3192 return c;
3193}
3194
3201static float knife_snap_size(KnifeTool_OpData *kcd, float maxsize)
3202{
3203 BLI_assert(kcd->is_interactive == true);
3204 int density = 0;
3205
3206 if (!kcd->curr.is_space()) {
3208 kcd, maxsize * 2.0f, kcd->curr.ob_index, kcd->curr.bmface, kcd->curr.cage));
3209 }
3210
3211 return density ? min_ff(maxsize / (float(density) * 0.5f), maxsize) : maxsize;
3212}
3213
3219static bool knife_closest_constrain_to_edge(const float3 &cut_origin,
3220 const float3 &cut_dir,
3221 const float3 &kfv1_cageco,
3222 const float3 &kfv2_cageco,
3223 float r_close[3])
3224{
3225 /* If snapping, check we're in bounds. */
3226 float lambda;
3227 if (!isect_ray_line_v3(cut_origin, cut_dir, kfv1_cageco, kfv2_cageco, &lambda)) {
3228 return false;
3229 }
3230
3231 /* Be strict when constrained within edge. */
3232 if ((lambda < 0.0f - KNIFE_FLT_EPSBIG) || (lambda > 1.0f + KNIFE_FLT_EPSBIG)) {
3233 return false;
3234 }
3235
3236 interp_v3_v3v3(r_close, kfv1_cageco, kfv2_cageco, lambda);
3237 return true;
3238}
3239
3240/* `r_kpd->cage` is closest point on edge to the knife point. */
3242 int ob_index,
3243 BMFace *f,
3244 const float2 &curr_cage_ss,
3245 const float3 *curr_cage_constrain,
3246 const float3 &ray_orig,
3247 const float3 &ray_dir,
3248 KnifePosData *r_kpd)
3249{
3250 float maxdist;
3251
3252 if (kcd->is_interactive) {
3253 maxdist = knife_snap_size(kcd, kcd->ethresh);
3254
3255 if (kcd->ignore_vert_snapping) {
3256 maxdist *= 0.5f;
3257 }
3258 }
3259 else {
3260 maxdist = KNIFE_FLT_EPS;
3261 }
3262
3263 const float maxdist_sq = maxdist * maxdist;
3264 float cur_dist_sq = maxdist_sq;
3265 bool has_hit = false;
3266
3267 const float3 &cut_origin = kcd->prev.cage;
3268 const float3 cut_dir = math::normalize(
3269 (curr_cage_constrain ? *curr_cage_constrain : kcd->curr.cage) - kcd->prev.cage);
3270
3271 /* Look through all edges associated with this face. */
3272 ListBaseT<LinkData> *list = knife_get_face_kedges(kcd, ob_index, f);
3273 for (LinkData &ref : *list) {
3274 KnifeEdge *kfe = static_cast<KnifeEdge *>(ref.data);
3275 float test_cagep[3];
3276
3277 if (kfe->is_invalid) {
3278 continue;
3279 }
3280
3281 /* Get the closest point on the edge. */
3282 if ((kcd->is_angle_snapping || kcd->axis_constrained) && (kfe != kcd->prev.edge) &&
3283 (kcd->mode == MODE_DRAGGING))
3284 {
3285 /* Check if it is within the edges' bounds. */
3287 cut_origin, cut_dir, kfe->v1->cageco, kfe->v2->cageco, test_cagep))
3288 {
3289 continue;
3290 }
3291 }
3292 else {
3293 closest_ray_to_segment_v3(ray_orig, ray_dir, kfe->v1->cageco, kfe->v2->cageco, test_cagep);
3294 }
3295
3296 /* Check if we're close enough. */
3297 float2 closest_ss;
3298 knife_project_v2(kcd, test_cagep, closest_ss);
3299 float dis_sq = len_squared_v2v2(closest_ss, curr_cage_ss);
3300 if (dis_sq >= cur_dist_sq) {
3301 continue;
3302 }
3303
3304 if (RV3D_CLIPPING_ENABLED(kcd->vc.v3d, kcd->vc.rv3d)) {
3305 /* Check we're in the view */
3306 if (ED_view3d_clipping_test(kcd->vc.rv3d, test_cagep, false)) {
3307 continue;
3308 }
3309 }
3310
3311 cur_dist_sq = dis_sq;
3312
3313 r_kpd->edge = kfe;
3314 if (kcd->snap_midpoints) {
3315 mid_v3_v3v3(r_kpd->cage, kfe->v1->cageco, kfe->v2->cageco);
3316 knife_project_v2(kcd, r_kpd->cage, r_kpd->mval);
3317 }
3318 else {
3319 copy_v3_v3(r_kpd->cage, test_cagep);
3320 r_kpd->mval = closest_ss;
3321 }
3322
3323 has_hit = true;
3324 }
3325
3326 return has_hit;
3327}
3328
3329/* Find a vertex near the mouse cursor, if it exists. */
3331 const KnifeEdge *kfe,
3332 const float2 &cage_ss,
3333 KnifePosData *r_kpd)
3334{
3335 float maxdist;
3336
3337 if (kcd->is_interactive) {
3338 maxdist = knife_snap_size(kcd, kcd->vthresh);
3339 if (kcd->ignore_vert_snapping) {
3340 maxdist *= 0.5f;
3341 }
3342 }
3343 else {
3344 maxdist = KNIFE_FLT_EPS;
3345 }
3346
3347 const float maxdist_sq = maxdist * maxdist;
3348 KnifeVert *curv = nullptr;
3349 float cur_kfv_sco[2];
3350 float dis_sq, curdis_sq = FLT_MAX;
3351
3352 for (int i = 0; i < 2; i++) {
3353 KnifeVert *kfv = i ? kfe->v2 : kfe->v1;
3354 float kfv_sco[2];
3355
3356 knife_project_v2(kcd, kfv->cageco, kfv_sco);
3357
3358 /* Be strict when in a constrained mode, the vertex needs to be very close to the cut line,
3359 * or we ignore. */
3360 if ((kcd->is_angle_snapping || kcd->axis_constrained) && (kcd->mode == MODE_DRAGGING)) {
3361 if (dist_squared_to_line_segment_v2(kfv_sco, kcd->prev.mval, kcd->curr.mval) >
3363 {
3364 continue;
3365 }
3366 }
3367
3368 dis_sq = len_squared_v2v2(kfv_sco, cage_ss);
3369 if (dis_sq < curdis_sq && dis_sq < maxdist_sq) {
3370 if (!RV3D_CLIPPING_ENABLED(kcd->vc.v3d, kcd->vc.rv3d) ||
3371 !ED_view3d_clipping_test(kcd->vc.rv3d, kfv->cageco, false))
3372 {
3373 curv = kfv;
3374 curdis_sq = dis_sq;
3375 copy_v2_v2(cur_kfv_sco, kfv_sco);
3376 }
3377 }
3378 }
3379
3380 if (curv) {
3381 r_kpd->cage = curv->cageco;
3382 r_kpd->vert = curv;
3383
3384 /* Update mouse coordinates to the snapped-to vertex's screen coordinates
3385 * this is important for angle snap, which uses the previous mouse position. */
3386 r_kpd->mval = cur_kfv_sco;
3387
3388 return true;
3389 }
3390
3391 return false;
3392}
3393
3398 float3 &r, const float3 &dvec, const float3 &vecx, const float3 &axis, float angle_snap)
3399{
3400 const float angle = angle_signed_on_axis_v3v3_v3(dvec, vecx, axis);
3401 const float angle_delta = (roundf(angle / angle_snap) * angle_snap) - angle;
3402 rotate_normalized_v3_v3v3fl(r, dvec, axis, angle_delta);
3403 return angle + angle_delta;
3404}
3405
3407 const float3 &vec_x,
3408 const float3 &axis,
3409 const float3 &ray_orig,
3410 const float3 &ray_dir,
3411 float3 &r_cage,
3412 float &r_angle)
3413{
3414 float3 curr_cage_projected;
3416 curr_cage_projected, ray_orig, ray_orig + ray_dir, kcd->prev.cage, axis))
3417 {
3418 return false;
3419 }
3420 const float3 dvec = curr_cage_projected - kcd->prev.cage;
3421 float snap_step;
3422 /* Currently user can input any float between 0 and 180. */
3425 {
3426 snap_step = DEG2RADF(kcd->angle_snapping_increment);
3427 }
3428 else {
3430 }
3431
3432 if (is_zero_v2(dvec)) {
3433 return false;
3434 }
3435
3436 float3 dvec_snap;
3437 r_angle = knife_snap_v3_angle(dvec_snap, dvec, vec_x, axis, snap_step);
3438 r_cage = kcd->prev.cage + dvec_snap;
3439 return true;
3440}
3441
3442/* Update both kcd->curr.mval and kcd->mval to snap to required angle. */
3444 const float3 &ray_orig,
3445 const float3 &ray_dir,
3446 float3 &r_cage,
3447 float &r_angle)
3448{
3449 const float3 vec_x = kcd->vc.rv3d->viewinv[0];
3450 const float3 vec_z = kcd->vc.rv3d->viewinv[2];
3451 return knife_snap_angle_impl(kcd, vec_x, vec_z, ray_orig, ray_dir, r_cage, r_angle);
3452}
3453
3454/* Snap to required angle along the plane of the face nearest to kcd->prev. */
3456 const float3 &ray_orig,
3457 const float3 &ray_dir,
3458 float3 &r_cage,
3459 float &r_angle)
3460{
3461 BMFace *fcurr = knife_bvh_raycast(kcd, ray_orig, ray_dir, 0.0f, nullptr, nullptr, nullptr);
3462
3463 if (!fcurr) {
3464 return false;
3465 }
3466
3467 /* Calculate a reference vector using previous cut segment, edge or vertex.
3468 * If none exists then exit. */
3469 float3 refv;
3470 if (kcd->prev.vert) {
3471 int count = 0;
3472 for (LinkData &ref : kcd->prev.vert->edges) {
3473 KnifeEdge *kfe = (static_cast<KnifeEdge *>(ref.data));
3474 if (kfe->is_invalid) {
3475 continue;
3476 }
3477 if (kfe->e) {
3478 if (!BM_edge_in_face(kfe->e, fcurr)) {
3479 continue;
3480 }
3481 }
3482 if (count == kcd->snap_edge) {
3484 kfe->v2 :
3485 kfe->v1;
3486 refv = kfv->cageco - kcd->prev.cage;
3487 kcd->snap_ref_edge = kfe;
3488 break;
3489 }
3490 count++;
3491 }
3492 }
3493 else if (kcd->prev.edge) {
3495 kcd->prev.edge->v2 :
3496 kcd->prev.edge->v1;
3497 refv = kfv->cageco - kcd->prev.cage;
3498 kcd->snap_ref_edge = kcd->prev.edge;
3499 }
3500 else {
3501 return false;
3502 }
3503
3504 /* Choose best face for plane. */
3505 BMFace *fprev = nullptr;
3506 int fprev_ob_index = kcd->prev.ob_index;
3507 if (kcd->prev.vert && kcd->prev.vert->v) {
3508 for (LinkData &ref : kcd->prev.vert->faces) {
3509 BMFace *f = (static_cast<BMFace *>(ref.data));
3510 if (f == fcurr) {
3511 fprev = f;
3512 }
3513 }
3514 }
3515 else if (kcd->prev.edge) {
3516 for (LinkData &ref : kcd->prev.edge->faces) {
3517 BMFace *f = (static_cast<BMFace *>(ref.data));
3518 if (f == fcurr) {
3519 fprev = f;
3520 }
3521 }
3522 }
3523 else {
3524 /* Cut segment was started in a face. */
3525 float3 prev_ray_orig, prev_ray_dir;
3527 kcd->region,
3528 kcd->vc.v3d,
3529 kcd->prev.mval,
3530 prev_ray_orig,
3531 prev_ray_dir,
3532 false);
3533
3534 /* kcd->prev.face is usually not set. */
3535 fprev = knife_bvh_raycast(
3536 kcd, prev_ray_orig, prev_ray_dir, 0.0f, nullptr, nullptr, &fprev_ob_index);
3537 }
3538
3539 if (!fprev || fprev != fcurr) {
3540 return false;
3541 }
3542
3543 /* Use normal global direction. */
3544 Object *ob = kcd->objects[fprev_ob_index];
3545 float3 no_global = fprev->no;
3546 mul_transposed_mat3_m4_v3(ob->world_to_object().ptr(), no_global);
3547 normalize_v3(no_global);
3548
3549 return knife_snap_angle_impl(kcd, refv, no_global, ray_orig, ray_dir, r_cage, r_angle);
3550}
3551
3553 const float3 &ray_orig,
3554 const float3 &ray_dir)
3555{
3556 BMFace *fcurr = knife_bvh_raycast(kcd, ray_orig, ray_dir, 0.0f, nullptr, nullptr, nullptr);
3557
3558 int count = 0;
3559
3560 if (!fcurr) {
3561 return count;
3562 }
3563
3564 if (kcd->prev.vert) {
3565 for (LinkData &ref : kcd->prev.vert->edges) {
3566 KnifeEdge *kfe = (static_cast<KnifeEdge *>(ref.data));
3567 if (kfe->is_invalid) {
3568 continue;
3569 }
3570 if (kfe->e) {
3571 if (!BM_edge_in_face(kfe->e, fcurr)) {
3572 continue;
3573 }
3574 }
3575 count++;
3576 }
3577 }
3578 else if (kcd->prev.edge) {
3579 return 1;
3580 }
3581 return count;
3582}
3583
3584/* Reset the snapping angle num input. */
3586{
3587 kcd->num.val[0] = 0;
3588 while (kcd->num.str_cur > 0) {
3589 kcd->num.str[kcd->num.str_cur - 1] = '\0';
3590 kcd->num.str_cur--;
3591 }
3592}
3593
3599 const float3 &ray_orig,
3600 const float3 &ray_dir,
3601 float3 &r_cage)
3602{
3603 float3 constrain_dir;
3604 {
3605 /* Constrain axes. */
3606 Scene *scene = kcd->scene;
3607 ViewLayer *view_layer = kcd->vc.view_layer;
3608 Object *obedit = (kcd->prev.ob_index != -1) ? kcd->objects[kcd->prev.ob_index] :
3609 kcd->vc.obedit;
3610 RegionView3D *rv3d = static_cast<RegionView3D *>(kcd->region->regiondata);
3611 const short scene_orientation = BKE_scene_orientation_get_index(scene, SCE_ORIENT_DEFAULT);
3612 /* Scene orientation takes priority. */
3613 const short orientation_type = scene_orientation ? scene_orientation :
3614 kcd->constrain_axis_mode - 1;
3615 const int pivot_point = scene->toolsettings->transform_pivot_point;
3616 float mat[3][3];
3618 scene,
3619 view_layer,
3620 kcd->vc.v3d,
3621 rv3d,
3622 obedit,
3623 obedit,
3624 orientation_type,
3625 pivot_point,
3626 mat);
3627
3628 constrain_dir = mat[kcd->constrain_axis - 1];
3629 }
3630
3631 float lambda;
3632 if (!isect_ray_ray_v3(kcd->prev.cage, constrain_dir, ray_orig, ray_dir, &lambda, nullptr)) {
3633 return;
3634 }
3635
3636 float3 cage_dir = constrain_dir * lambda;
3637 if (math::is_zero(cage_dir)) {
3638 return;
3639 }
3640
3641 r_cage = kcd->prev.cage + cage_dir;
3642}
3643
3650 const float2 &mval,
3651 const float3 &ray_orig,
3652 const float3 &ray_dir,
3653 const float3 *curr_cage_constrain,
3654 const float3 *fallback)
3655{
3657
3658 if (knife_find_closest_face(kcd, mval, ray_orig, ray_dir, &kcd->curr)) {
3659 if (!kcd->ignore_edge_snapping || !kcd->ignore_vert_snapping) {
3660 KnifePosData kpos_tmp = kcd->curr;
3662 kcd->curr.ob_index,
3663 kcd->curr.bmface,
3664 kcd->curr.mval,
3665 curr_cage_constrain,
3666 ray_orig,
3667 ray_dir,
3668 &kpos_tmp))
3669 {
3670 if (!kcd->ignore_edge_snapping) {
3671 kcd->curr = kpos_tmp;
3672 }
3673 if (!kcd->ignore_vert_snapping) {
3674 knife_find_closest_vert_of_edge(kcd, kpos_tmp.edge, kpos_tmp.mval, &kcd->curr);
3675 }
3676 }
3677 }
3678 }
3679
3680 if (kcd->curr.vert || kcd->curr.edge || kcd->curr.bmface) {
3681 return;
3682 }
3683
3684 kcd->curr.mval = mval;
3685 if (fallback) {
3686 /* If no geometry was found, use the fallback point. */
3687 kcd->curr.cage = *fallback;
3688 return;
3689 }
3690
3691 /* If no hits are found this would normally default to (0, 0, 0) so instead
3692 * get a point at the mouse ray closest to the previous point.
3693 * Note that drawing lines in `free-space` isn't properly supported
3694 * but there's no guarantee (0, 0, 0) has any geometry either - campbell */
3695
3697 kcd->curr.cage, ray_orig, ray_orig + ray_dir, kcd->prev.cage, kcd->vc.rv3d->viewinv[2]))
3698 {
3699 /* Should never fail! */
3700 kcd->curr.cage = kcd->prev.cage;
3701 BLI_assert(0);
3702 }
3703}
3704
3714{
3715 /* Mouse and ray with snapping applied. */
3716 float3 ray_orig;
3717 float3 ray_dir;
3718 float2 mval_constrain = mval;
3720 kcd->vc.depsgraph, kcd->region, kcd->vc.v3d, mval, ray_orig, ray_dir, false);
3721
3723
3724 /* view matrix may have changed, reproject */
3725 knife_project_v2(kcd, kcd->prev.cage, kcd->prev.mval);
3726
3727 bool is_constrained = false;
3728 kcd->is_angle_snapping = false;
3729 if (kcd->mode == MODE_DRAGGING) {
3730 if (kcd->angle_snapping) {
3733 kcd, ray_orig, ray_dir, kcd->curr.cage, kcd->angle);
3734 }
3737 kcd, ray_orig, ray_dir, kcd->curr.cage, kcd->angle);
3738 if (kcd->is_angle_snapping) {
3739 kcd->snap_ref_edges_count = knife_calculate_snap_ref_edges(kcd, ray_orig, ray_dir);
3740 }
3741 }
3742 }
3743
3744 if (kcd->is_angle_snapping) {
3745 is_constrained = true;
3746 }
3747 else if (kcd->axis_constrained) {
3748 knife_constrain_axis(kcd, ray_orig, ray_dir, kcd->curr.cage);
3749 is_constrained = true;
3750 }
3751 }
3752
3753 float3 fallback;
3754 float3 curr_cage_constrain;
3755 if (is_constrained) {
3756 /* Update ray and `mval_constrain`. */
3757 if (kcd->is_ortho) {
3758 float3 l1 = kcd->curr.cage - ray_dir;
3759 if (!isect_line_plane_v3(ray_orig, l1, kcd->curr.cage, ray_orig, ray_dir)) {
3760 /* Should never fail! */
3761 ray_orig = l1;
3763 }
3764 }
3765 else {
3766 ray_dir = math::normalize(kcd->curr.cage - ray_orig);
3767 }
3768 knife_project_v2(kcd, kcd->curr.cage, mval_constrain);
3769 curr_cage_constrain = kcd->curr.cage;
3770 fallback = kcd->curr.cage;
3771 }
3772
3773 knife_snap_curr(kcd,
3774 mval_constrain,
3775 ray_orig,
3776 ray_dir,
3777 is_constrained ? &curr_cage_constrain : nullptr,
3778 is_constrained ? &fallback : nullptr);
3779}
3780
3788{
3789 KnifeEdge *kfe, *newkfe;
3790 KnifeEdge *lastkfe = nullptr;
3791 KnifeVert *v1, *v2;
3793 BLI_mempool_iter iterkfe;
3794
3795 undo = static_cast<KnifeUndoFrame *>(BLI_stack_peek(kcd->undostack));
3796
3797 /* Undo edge splitting. */
3798 for (int i = 0; i < undo->splits; i++) {
3799 BLI_stack_pop(kcd->splitstack, &newkfe);
3800 BLI_stack_pop(kcd->splitstack, &kfe);
3801 knife_join_edge(newkfe, kfe);
3802 }
3803
3804 for (int i = 0; i < undo->cuts; i++) {
3805
3806 BLI_mempool_iternew(kcd->kedges, &iterkfe);
3807 for (kfe = static_cast<KnifeEdge *>(BLI_mempool_iterstep(&iterkfe)); kfe;
3808 kfe = static_cast<KnifeEdge *>(BLI_mempool_iterstep(&iterkfe)))
3809 {
3810 if (!kfe->is_cut || kfe->is_invalid || kfe->splits) {
3811 continue;
3812 }
3813 lastkfe = kfe;
3814 }
3815
3816 if (lastkfe) {
3817 lastkfe->is_invalid = true;
3818
3819 /* TODO: Are they always guaranteed to be in this order? */
3820 v1 = lastkfe->v1;
3821 v2 = lastkfe->v2;
3822
3823 /* Only remove first vertex if it is the start segment of the cut. */
3824 if (!v1->is_invalid && !v1->is_splitting) {
3825 v1->is_invalid = true;
3826 /* If the first vertex is touching any other cut edges don't remove it. */
3827 for (LinkData &ref : v1->edges) {
3828 kfe = static_cast<KnifeEdge *>(ref.data);
3829 if (kfe->is_cut && !kfe->is_invalid) {
3830 v1->is_invalid = false;
3831 break;
3832 }
3833 }
3834 }
3835
3836 /* Only remove second vertex if it is the end segment of the cut. */
3837 if (!v2->is_invalid && !v2->is_splitting) {
3838 v2->is_invalid = true;
3839 /* If the second vertex is touching any other cut edges don't remove it. */
3840 for (LinkData &ref : v2->edges) {
3841 kfe = static_cast<KnifeEdge *>(ref.data);
3842 if (kfe->is_cut && !kfe->is_invalid) {
3843 v2->is_invalid = false;
3844 break;
3845 }
3846 }
3847 }
3848 }
3849 }
3850
3851 if (ELEM(kcd->mode, MODE_DRAGGING, MODE_IDLE)) {
3852 /* Restore kcd->prev. */
3853 kcd->prev = undo->pos;
3854 }
3855
3856 /* Restore data for distance and angle measurements. */
3857 kcd->mdata = undo->mdata;
3858
3860}
3861
3863
3864/* -------------------------------------------------------------------- */
3867
3869 Object *ob,
3870 int ob_index,
3871 bool use_tri_indices)
3872{
3873 Scene *scene_eval = DEG_get_evaluated(kcd->vc.depsgraph, kcd->scene);
3874 Object *obedit_eval = DEG_get_evaluated(kcd->vc.depsgraph, ob);
3875 const Mesh &mesh_orig = *id_cast<const Mesh *>(ob->data);
3876 const Mesh &mesh_eval = *id_cast<const Mesh *>(obedit_eval->data);
3877
3878 KnifeObjectInfo *obinfo = &kcd->objects_info[ob_index];
3879
3880 if (BKE_editmesh_eval_orig_map_available(mesh_eval, &mesh_orig)) {
3882 BMEditMesh &em_eval = *mesh_eval.runtime->edit_mesh;
3883 obinfo->em = &em_eval;
3884 obinfo->bm = bm;
3886 kcd->vc.depsgraph, bm, scene_eval, obedit_eval);
3887 }
3888 else {
3889 obinfo->em = mesh_orig.runtime->edit_mesh.get();
3891 obinfo->positions_cage = BM_mesh_vert_coords_alloc(obinfo->bm);
3892 }
3893
3895
3896 if (use_tri_indices) {
3897 const Span<std::array<BMLoop *, 3>> looptris = obinfo->em->looptris;
3898 obinfo->tri_indices.reinitialize(obinfo->em->looptris.size());
3899 for (int i = 0; i < looptris.size(); i++) {
3900 const std::array<BMLoop *, 3> &ltri = looptris[i];
3901 obinfo->tri_indices[i][0] = BM_elem_index_get(ltri[0]->v);
3902 obinfo->tri_indices[i][1] = BM_elem_index_get(ltri[1]->v);
3903 obinfo->tri_indices[i][2] = BM_elem_index_get(ltri[2]->v);
3904 }
3905 }
3906}
3907
3909
3910/* -------------------------------------------------------------------- */
3913
3914static void knife_init_colors(KnifeColors *colors)
3915{
3916 /* Possible BMESH_TODO: add explicit themes or calculate these by
3917 * figuring out contrasting colors with grid / edges / verts
3918 * a la theme::make_axis_color. */
3924 colors->curpoint_a[3] = 102;
3927 colors->point_a[3] = 102;
3928
3933}
3934
3935/* called when modal loop selection gets set up... */
3937 KnifeTool_OpData *kcd,
3938 Vector<Object *> objects,
3939 const bool only_select,
3940 const bool cut_through,
3941 const bool xray,
3942 const int visible_measurements,
3943 const int angle_snapping,
3944 const float angle_snapping_increment,
3945 const bool is_interactive)
3946{
3947 /* Needed so multiple non-interactive cuts (also called knife-project)
3948 * doesn't access indices of loops that were created by cutting, see: #97153. */
3949 bool use_tri_indices = !is_interactive;
3950
3951 kcd->vc = *vc;
3952
3953 Scene *scene = vc->scene;
3954
3955 /* Assign the drawing handle for drawing preview line... */
3956 kcd->scene = scene;
3957 kcd->region = vc->region;
3958
3959 kcd->objects = std::move(objects);
3960
3961 Object *ob;
3962 BMEditMesh *em;
3963 kcd->objects_info.reinitialize(kcd->objects.size());
3964 for (int ob_index = 0; ob_index < kcd->objects.size(); ob_index++) {
3965 ob = kcd->objects[ob_index];
3966 em = BKE_editmesh_from_object(ob);
3967 knifetool_init_obinfo(kcd, ob, ob_index, use_tri_indices);
3968
3969 /* Can't usefully select resulting edges in face mode. */
3971 }
3972 knife_bvh_init(kcd);
3973
3974 /* Cut all the way through the mesh if use_occlude_geometry button not pushed. */
3975 kcd->is_interactive = is_interactive;
3976 kcd->cut_through = cut_through;
3977 kcd->only_select = only_select;
3978 kcd->depth_test = xray;
3979 kcd->dist_angle_mode = visible_measurements;
3981 kcd->angle_snapping_mode = angle_snapping;
3983 kcd->angle_snapping_increment = angle_snapping_increment;
3984
3985 kcd->arena = BLI_memarena_new(MEM_SIZE_OPTIMAL(1 << 15), "knife");
3986#ifdef USE_NET_ISLAND_CONNECT
3987 kcd->edgenet.arena = BLI_memarena_new(MEM_SIZE_OPTIMAL(1 << 15), __func__);
3988#endif
3989 kcd->edgenet.edge_visit = MEM_new<Set<BMEdge *>>(__func__);
3990
3991 kcd->vthresh = KMAXDIST - 1;
3992 kcd->ethresh = KMAXDIST;
3993
3994 knife_recalc_ortho(kcd);
3995
3997
3998 kcd->refs = BLI_mempool_create(sizeof(LinkData), 0, 2048, 0);
4001
4002 kcd->undostack = BLI_stack_new(sizeof(KnifeUndoFrame), "knife undostack");
4003 kcd->splitstack = BLI_stack_new(sizeof(KnifeEdge *), "knife splitstack");
4004
4005 kcd->origedgemap = MEM_new<Map<BMEdge *, KnifeEdge *>>("knife origedgemap");
4006 kcd->origvertmap = MEM_new<Map<BMVert *, KnifeVert *>>("knife origvertmap");
4007 kcd->kedgefacemap = MEM_new<Map<BMFace *, ListBaseT<LinkData> *>>("knife kedgefacemap");
4008 kcd->facetrimap = MEM_new<Map<BMFace *, int>>("knife facetrimap");
4009
4012
4013 if (is_interactive) {
4016
4018 }
4019
4020 kcd->no_cuts = true;
4021
4022 kcd->axis_string[0] = ' ';
4023 kcd->axis_string[1] = '\0';
4024
4025 /* Initialize number input handling for angle snapping. */
4026 initNumInput(&kcd->num);
4027 kcd->num.idx_max = 0;
4028 kcd->num.val_flag[0] |= NUM_NO_NEGATIVE;
4029 kcd->num.unit_sys = scene->unit.system;
4030 kcd->num.unit_type[0] = B_UNIT_NONE;
4031}
4032
4033/* called when modal loop selection is done... */
4035{
4036 if (!kcd) {
4037 return;
4038 }
4039
4040 if (kcd->is_interactive) {
4042
4043 /* Deactivate the extra drawing stuff in 3D-View. */
4045 }
4046
4047 /* Free the custom data. */
4051
4054
4055 MEM_delete(kcd->origedgemap);
4056 MEM_delete(kcd->origvertmap);
4057 MEM_delete(kcd->kedgefacemap);
4058 MEM_delete(kcd->facetrimap);
4059
4061#ifdef USE_NET_ISLAND_CONNECT
4063#endif
4064 MEM_delete(kcd->edgenet.edge_visit);
4065
4066 /* Tag for redraw. */
4068
4069 /* Knife BVH cleanup. */
4070 knife_bvh_free(kcd);
4071
4072 /* Destroy kcd itself. */
4073 MEM_delete(kcd);
4074}
4075
4077{
4078 KnifeTool_OpData *kcd = static_cast<KnifeTool_OpData *>(op->customdata);
4079 knifetool_exit_ex(kcd);
4080 op->customdata = nullptr;
4081}
4082
4084
4085/* -------------------------------------------------------------------- */
4088
4090static int knife_update_active(KnifeTool_OpData *kcd, const float2 &mval)
4091{
4092 knife_snap_update_from_mval(kcd, mval);
4093
4094 if (kcd->mode == MODE_DRAGGING) {
4096 }
4097 return 1;
4098}
4099
4100static void knifetool_update_mval(KnifeTool_OpData *kcd, const float2 &mval)
4101{
4102 knife_recalc_ortho(kcd);
4103
4104 if (knife_update_active(kcd, mval)) {
4106 }
4107}
4108
4110
4111/* -------------------------------------------------------------------- */
4114
4115static void knifetool_finish_single_pre(KnifeTool_OpData *kcd, int ob_index)
4116{
4117 knife_make_cuts(kcd, ob_index);
4118}
4119
4125{
4130
4131 EDBMUpdate_Params params{};
4132 params.calc_looptris = true;
4133 params.calc_normals = true;
4134 params.is_destructive = true;
4135 EDBM_update(id_cast<Mesh *>(ob->data), &params);
4136}
4137
4138/* Called on tool confirmation. */
4140{
4141 /* Separate pre/post passes are needed because `em->looptris` recalculation from the 'post' pass
4142 * causes triangle indices in #KnifeTool_OpData.bvh to get out of sync.
4143 * So perform all the cuts before doing any mesh recalculation, see: #101721. */
4144 for (int ob_index : kcd->objects.index_range()) {
4145 knifetool_finish_single_pre(kcd, ob_index);
4146 }
4147 for (Object *ob : kcd->objects) {
4149 }
4150}
4151
4153{
4154 KnifeTool_OpData *kcd = static_cast<KnifeTool_OpData *>(op->customdata);
4156}
4157
4159
4160/* -------------------------------------------------------------------- */
4163
4164static void knifetool_cancel(bContext * /*C*/, wmOperator *op)
4165{
4166 /* this is just a wrapper around exit() */
4167 knifetool_exit(op);
4168}
4169
4171{
4172 static const EnumPropertyItem modal_items[] = {
4173 {KNF_MODAL_CANCEL, "CANCEL", 0, "Cancel", ""},
4174 {KNF_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
4175 {KNF_MODAL_UNDO, "UNDO", 0, "Undo", ""},
4176 {KNF_MODAL_MIDPOINT_ON, "SNAP_MIDPOINTS_ON", 0, "Snap to Midpoints On", ""},
4177 {KNF_MODAL_MIDPOINT_OFF, "SNAP_MIDPOINTS_OFF", 0, "Snap to Midpoints Off", ""},
4178 {KNF_MODAL_IGNORE_SNAP_ON, "IGNORE_SNAP_ON", 0, "Ignore Snapping On", ""},
4179 {KNF_MODAL_IGNORE_SNAP_OFF, "IGNORE_SNAP_OFF", 0, "Ignore Snapping Off", ""},
4180 {KNF_MODAL_ANGLE_SNAP_TOGGLE, "ANGLE_SNAP_TOGGLE", 0, "Toggle Angle Snapping", ""},
4182 "CYCLE_ANGLE_SNAP_EDGE",
4183 0,
4184 "Cycle Angle Snapping Relative Edge",
4185 ""},
4186 {KNF_MODAL_CUT_THROUGH_TOGGLE, "CUT_THROUGH_TOGGLE", 0, "Toggle Cut Through", ""},
4188 "SHOW_DISTANCE_ANGLE_TOGGLE",
4189 0,
4190 "Toggle Distance and Angle Measurements",
4191 ""},
4192 {KNF_MODAL_DEPTH_TEST_TOGGLE, "DEPTH_TEST_TOGGLE", 0, "Toggle Depth Testing", ""},
4193 {KNF_MODAL_NEW_CUT, "NEW_CUT", 0, "End Current Cut", ""},
4194 {KNF_MODAL_ADD_CUT, "ADD_CUT", 0, "Add Cut", ""},
4195 {KNF_MODAL_ADD_CUT_CLOSED, "ADD_CUT_CLOSED", 0, "Add Cut Closed", ""},
4196 {KNF_MODAL_PANNING, "PANNING", 0, "Panning", ""},
4197 {KNF_MODAL_X_AXIS, "X_AXIS", 0, "X Axis Locking", ""},
4198 {KNF_MODAL_Y_AXIS, "Y_AXIS", 0, "Y Axis Locking", ""},
4199 {KNF_MODAL_Z_AXIS, "Z_AXIS", 0, "Z Axis Locking", ""},
4200 {0, nullptr, 0, nullptr, nullptr},
4201 };
4202
4203 wmKeyMap *keymap = WM_modalkeymap_find(keyconf, "Knife Tool Modal Map");
4204
4205 /* This function is called for each space-type, only needs to add map once. */
4206 if (keymap && keymap->modal_items) {
4207 return nullptr;
4208 }
4209
4210 keymap = WM_modalkeymap_ensure(keyconf, "Knife Tool Modal Map", modal_items);
4211
4212 WM_modalkeymap_assign(keymap, "MESH_OT_knife_tool");
4213
4214 return keymap;
4215}
4216
4217/* Turn off angle snapping. */
4224
4225/* Turn off orientation locking. */
4232
4234{
4235 KnifeTool_OpData *kcd = static_cast<KnifeTool_OpData *>(op->customdata);
4236 bool do_refresh = false;
4237
4238 Object *ob = (kcd->curr.ob_index != -1) ? kcd->objects[kcd->curr.ob_index] : kcd->vc.obedit;
4239 if (!ob || ob->type != OB_MESH) {
4240 knifetool_exit(op);
4241 ED_workspace_status_text(C, nullptr);
4242 return OPERATOR_FINISHED;
4243 }
4244
4245 kcd->region = kcd->vc.region;
4246
4247 ED_view3d_init_mats_rv3d(ob, kcd->vc.rv3d); /* Needed to initialize clipping. */
4248
4249 if (kcd->mode == MODE_PANNING) {
4250 kcd->mode = KnifeMode(kcd->prevmode);
4251 }
4252
4253 bool handled = false;
4254 float snapping_increment_temp;
4255 const float2 mval = {float(event->mval[0]), float(event->mval[1])};
4256
4257 if (kcd->angle_snapping) {
4258 if (kcd->num.str_cur >= 3 ||
4260 {
4262 }
4263 knife_update_header(C, op, kcd); /* Update the angle multiple. */
4264 /* Modal numinput active, try to handle numeric inputs first... */
4265 if (event->val == KM_PRESS && hasNumInput(&kcd->num) && handleNumInput(C, &kcd->num, event)) {
4266 handled = true;
4267 applyNumInput(&kcd->num, &snapping_increment_temp);
4268 /* Restrict number key input to 0 - 180 degree range. */
4269 if (snapping_increment_temp > KNIFE_MIN_ANGLE_SNAPPING_INCREMENT &&
4270 snapping_increment_temp <= KNIFE_MAX_ANGLE_SNAPPING_INCREMENT)
4271 {
4272 kcd->angle_snapping_increment = snapping_increment_temp;
4273 }
4274 knife_update_active(kcd, mval);
4275 knife_update_header(C, op, kcd);
4278 }
4279 }
4280
4281 /* Handle modal keymap. */
4282 if (event->type == EVT_MODAL_MAP) {
4283 switch (event->val) {
4284 case KNF_MODAL_CANCEL:
4285 /* finish */
4287
4288 knifetool_exit(op);
4289 ED_workspace_status_text(C, nullptr);
4290
4291 return OPERATOR_CANCELLED;
4292 case KNF_MODAL_CONFIRM: {
4293 const bool changed = (kcd->totkvert != 0);
4294 /* finish */
4296
4297 knifetool_finish(op);
4298 knifetool_exit(op);
4299 ED_workspace_status_text(C, nullptr);
4300
4301 /* Cancel to prevent undo push for empty cuts. */
4302 if (!changed) {
4303 return OPERATOR_CANCELLED;
4304 }
4305 return OPERATOR_FINISHED;
4306 }
4307 case KNF_MODAL_UNDO:
4308 if (BLI_stack_is_empty(kcd->undostack)) {
4310 knifetool_exit(op);
4311 ED_workspace_status_text(C, nullptr);
4312 return OPERATOR_CANCELLED;
4313 }
4314 knifetool_undo(kcd);
4315 knife_update_active(kcd, mval);
4317 handled = true;
4318 break;
4320 kcd->snap_midpoints = true;
4321
4322 knife_recalc_ortho(kcd);
4323 knife_update_active(kcd, mval);
4324 do_refresh = true;
4325 handled = true;
4326 break;
4328 kcd->snap_midpoints = false;
4329
4330 knife_recalc_ortho(kcd);
4331 knife_update_active(kcd, mval);
4332 do_refresh = true;
4333 handled = true;
4334 break;
4336 kcd->ignore_vert_snapping = kcd->ignore_edge_snapping = true;
4337 do_refresh = true;
4338 handled = true;
4339 break;
4341 kcd->ignore_vert_snapping = kcd->ignore_edge_snapping = false;
4342 do_refresh = true;
4343 handled = true;
4344 break;
4347 kcd->angle_snapping_mode++;
4348 kcd->snap_ref_edges_count = 0;
4349 kcd->snap_edge = 0;
4350 }
4351 else {
4353 }
4356 RNA_float_get(op->ptr, "angle_snapping_increment"));
4359 knife_update_active(kcd, mval);
4360 do_refresh = true;
4361 handled = true;
4362 break;
4365 if (kcd->snap_ref_edges_count) {
4366 kcd->snap_edge++;
4367 kcd->snap_edge %= kcd->snap_ref_edges_count;
4369 do_refresh = true;
4370 handled = true;
4371 }
4372 }
4373 break;
4375 kcd->cut_through = !kcd->cut_through;
4376 knife_update_active(kcd, mval);
4377 do_refresh = true;
4378 handled = true;
4379 break;
4382 kcd->dist_angle_mode++;
4383 }
4384 else {
4386 }
4388 do_refresh = true;
4389 handled = true;
4390 break;
4392 kcd->depth_test = !kcd->depth_test;
4393 do_refresh = true;
4394 handled = true;
4395 break;
4396 case KNF_MODAL_NEW_CUT:
4397 /* If no cuts have been made, exit.
4398 * Preserves right click cancel workflow which most tools use,
4399 * but stops accidentally deleting entire cuts with right click.
4400 */
4401 if (kcd->no_cuts) {
4403 knifetool_exit(op);
4404 ED_workspace_status_text(C, nullptr);
4405 return OPERATOR_CANCELLED;
4406 }
4408 knife_finish_cut(kcd);
4409 kcd->mode = MODE_IDLE;
4410 handled = true;
4411 break;
4412 case KNF_MODAL_ADD_CUT:
4413 kcd->no_cuts = false;
4414 knife_recalc_ortho(kcd);
4415
4416 /* Get the value of the event which triggered this one. */
4417 if (event->prev_val != KM_RELEASE) {
4418 if (kcd->mode == MODE_DRAGGING) {
4419 knife_add_cut(kcd);
4420 }
4421 else if (kcd->mode != MODE_PANNING) {
4422 knife_start_cut(kcd, mval);
4423 kcd->mode = MODE_DRAGGING;
4424 kcd->init = kcd->curr;
4425 }
4426
4427 /* Freehand drawing is incompatible with cut-through. */
4428 if (kcd->cut_through == false) {
4429 kcd->is_drag_hold = true;
4430 /* No edge snapping while dragging (edges are too sticky when cuts are immediate). */
4431 kcd->ignore_edge_snapping = true;
4432 }
4433 }
4434 else {
4435 kcd->is_drag_hold = false;
4436 kcd->ignore_edge_snapping = false;
4437 kcd->is_drag_undo = false;
4438
4439 /* Needed because the last face 'hit' is ignored when dragging. */
4440 knifetool_update_mval(kcd, kcd->curr.mval);
4441 }
4442
4444 handled = true;
4445 break;
4447 if (kcd->mode == MODE_DRAGGING) {
4448
4449 /* Shouldn't be possible with default key-layout, just in case. */
4450 if (kcd->is_drag_hold) {
4451 kcd->is_drag_hold = false;
4452 kcd->is_drag_undo = false;
4453 knifetool_update_mval(kcd, kcd->curr.mval);
4454 }
4455
4456 kcd->prev = kcd->curr;
4457 kcd->curr = kcd->init;
4458
4459 knife_project_v2(kcd, kcd->curr.cage, kcd->curr.mval);
4460 knifetool_update_mval(kcd, kcd->curr.mval);
4461
4462 knife_add_cut(kcd);
4463
4464 /* KNF_MODAL_NEW_CUT */
4465 knife_finish_cut(kcd);
4466 kcd->mode = MODE_IDLE;
4467 }
4468 handled = true;
4469 break;
4470 case KNF_MODAL_PANNING:
4471 if (event->val != KM_RELEASE) {
4472 if (kcd->mode != MODE_PANNING) {
4473 kcd->prevmode = kcd->mode;
4474 kcd->mode = MODE_PANNING;
4475 }
4476 }
4477 else {
4478 kcd->mode = KnifeMode(kcd->prevmode);
4479 }
4480
4482 return OPERATOR_PASS_THROUGH;
4483 }
4484 }
4485 else { /* non-modal-mapped events */
4486 switch (event->type) {
4487 case MOUSEPAN:
4488 case MOUSEZOOM:
4489 case MOUSEROTATE:
4490 case WHEELUPMOUSE:
4491 case WHEELDOWNMOUSE:
4492 case NDOF_MOTION:
4493 return OPERATOR_PASS_THROUGH;
4494 case MOUSEMOVE: /* Mouse moved somewhere to select another loop. */
4495 if (kcd->mode != MODE_PANNING) {
4496 knifetool_update_mval(kcd, mval);
4497 do_refresh = true;
4498
4499 if (kcd->is_drag_hold) {
4500 if (kcd->linehits.size() >= 2) {
4501 knife_add_cut(kcd);
4502 }
4503 }
4504 }
4505
4506 break;
4507 default: {
4508 break;
4509 }
4510 }
4511 }
4512
4513 if (kcd->angle_snapping) {
4514 if (kcd->num.str_cur >= 3 ||
4516 {
4518 }
4519 if (event->type != EVT_MODAL_MAP) {
4520 /* Modal number-input inactive, try to handle numeric inputs last. */
4521 if (!handled && event->val == KM_PRESS && handleNumInput(C, &kcd->num, event)) {
4522 applyNumInput(&kcd->num, &snapping_increment_temp);
4523 /* Restrict number key input to 0 - 180 degree range. */
4524 if (snapping_increment_temp > KNIFE_MIN_ANGLE_SNAPPING_INCREMENT &&
4525 snapping_increment_temp <= KNIFE_MAX_ANGLE_SNAPPING_INCREMENT)
4526 {
4527 kcd->angle_snapping_increment = snapping_increment_temp;
4528 }
4529 knife_update_active(kcd, mval);
4530 knife_update_header(C, op, kcd);
4533 }
4534 }
4535 }
4536
4537 /* Constrain axes with X,Y,Z keys. */
4538 if (event->type == EVT_MODAL_MAP) {
4540 if (event->val == KNF_MODAL_X_AXIS && kcd->constrain_axis != KNF_CONSTRAIN_AXIS_X) {
4543 kcd->axis_string[0] = 'X';
4544 }
4545 else if (event->val == KNF_MODAL_Y_AXIS && kcd->constrain_axis != KNF_CONSTRAIN_AXIS_Y) {
4548 kcd->axis_string[0] = 'Y';
4549 }
4550 else if (event->val == KNF_MODAL_Z_AXIS && kcd->constrain_axis != KNF_CONSTRAIN_AXIS_Z) {
4553 kcd->axis_string[0] = 'Z';
4554 }
4555 else {
4556 /* Cycle through modes with repeated key presses. */
4558 kcd->constrain_axis_mode++;
4559 kcd->axis_string[0] += 32; /* Lower case. */
4560 }
4561 else {
4564 }
4565 }
4568
4569 /* Needed so changes to constraints are re-evaluated without any cursor motion. */
4570 knifetool_update_mval(kcd, mval);
4571
4572 do_refresh = true;
4573 }
4574 }
4575
4576 if (kcd->mode == MODE_DRAGGING) {
4578 }
4579 else {
4581 }
4582
4583 if (do_refresh) {
4585 knife_update_header(C, op, kcd);
4586 }
4587
4588 /* Keep going until the user confirms. */
4590}
4591
4593{
4594 const bool only_select = RNA_boolean_get(op->ptr, "only_selected");
4595 const bool cut_through = !RNA_boolean_get(op->ptr, "use_occlude_geometry");
4596 const bool xray = !RNA_boolean_get(op->ptr, "xray");
4597 const int visible_measurements = RNA_enum_get(op->ptr, "visible_measurements");
4598 const int angle_snapping = RNA_enum_get(op->ptr, "angle_snapping");
4599 const bool wait_for_input = RNA_boolean_get(op->ptr, "wait_for_input");
4600 const float angle_snapping_increment = RAD2DEGF(
4601 RNA_float_get(op->ptr, "angle_snapping_increment"));
4602
4604
4605 /* alloc new customdata */
4606 KnifeTool_OpData *kcd = MEM_new<KnifeTool_OpData>(__func__);
4607 op->customdata = kcd;
4608 knifetool_init(&vc,
4609 kcd,
4611 *vc.bmain, vc.scene, vc.view_layer, vc.v3d),
4612 only_select,
4613 cut_through,
4614 xray,
4615 visible_measurements,
4616 angle_snapping,
4617 angle_snapping_increment,
4618 true);
4619
4620 if (only_select) {
4621 bool faces_selected = false;
4622 for (Object *obedit : kcd->objects) {
4623 const BMesh *bm = BKE_editmesh_bmesh_get(obedit);
4624 if (bm->totfacesel != 0) {
4625 faces_selected = true;
4626 }
4627 }
4628
4629 if (!faces_selected) {
4630 BKE_report(op->reports, RPT_ERROR, "Selected faces required");
4631 knifetool_cancel(C, op);
4632 return OPERATOR_CANCELLED;
4633 }
4634 }
4635
4637
4638 /* Add a modal handler for this operator - handles loop selection. */
4641
4642 if (wait_for_input == false) {
4643 /* Avoid copy-paste logic. */
4644 wmEvent event_modal{};
4645 event_modal.prev_val = KM_NOTHING;
4646 event_modal.type = EVT_MODAL_MAP;
4647 event_modal.val = KNF_MODAL_ADD_CUT;
4648
4649 copy_v2_v2_int(event_modal.mval, event->mval);
4650
4651 wmOperatorStatus retval = knifetool_modal(C, op, &event_modal);
4652 OPERATOR_RETVAL_CHECK(retval);
4654 UNUSED_VARS_NDEBUG(retval);
4655 }
4656
4657 knife_update_header(C, op, kcd);
4658
4660}
4661
4663{
4664 /* Description. */
4665 ot->name = "Knife Topology Tool";
4666 ot->idname = "MESH_OT_knife_tool";
4667 ot->description = "Cut new topology";
4668
4669 /* Callbacks. */
4671 ot->modal = knifetool_modal;
4674
4675 /* Flags. */
4677
4678 /* Properties. */
4679 PropertyRNA *prop;
4680 static const EnumPropertyItem visible_measurements_items[] = {
4681 {KNF_MEASUREMENT_NONE, "NONE", 0, "None", "Show no measurements"},
4682 {KNF_MEASUREMENT_BOTH, "BOTH", 0, "Both", "Show both distances and angles"},
4683 {KNF_MEASUREMENT_DISTANCE, "DISTANCE", 0, "Distance", "Show just distance measurements"},
4684 {KNF_MEASUREMENT_ANGLE, "ANGLE", 0, "Angle", "Show just angle measurements"},
4685 {0, nullptr, 0, nullptr, nullptr},
4686 };
4687
4688 static const EnumPropertyItem angle_snapping_items[] = {
4689 {KNF_CONSTRAIN_ANGLE_MODE_NONE, "NONE", 0, "None", "No angle snapping"},
4690 {KNF_CONSTRAIN_ANGLE_MODE_SCREEN, "SCREEN", 0, "Screen", "Screen space angle snapping"},
4692 "RELATIVE",
4693 0,
4694 "Relative",
4695 "Angle snapping relative to the previous cut edge"},
4696 {0, nullptr, 0, nullptr, nullptr},
4697 };
4698
4700 "use_occlude_geometry",
4701 true,
4702 "Occlude Geometry",
4703 "Only cut the front most geometry");
4704 RNA_def_boolean(ot->srna, "only_selected", false, "Only Selected", "Only cut selected geometry");
4705 RNA_def_boolean(ot->srna, "xray", true, "X-Ray", "Show cuts hidden by geometry");
4706
4707 prop = RNA_def_enum(ot->srna,
4708 "visible_measurements",
4709 visible_measurements_items,
4711 "Measurements",
4712 "Visible distance and angle measurements");
4714
4715 prop = RNA_def_enum(ot->srna,
4716 "angle_snapping",
4717 angle_snapping_items,
4719 "Angle Snapping",
4720 "Angle snapping mode");
4722
4723 prop = RNA_def_float(ot->srna,
4724 "angle_snapping_increment",
4728 "Angle Snap Increment",
4729 "The angle snap increment used when in constrained angle mode",
4733
4734 prop = RNA_def_boolean(ot->srna, "wait_for_input", true, "Wait for Input", "");
4736}
4737
4739
4740/* -------------------------------------------------------------------- */
4745
4746static bool edbm_mesh_knife_point_isect(const Span<Array<float2>> polys, const float2 cent_ss)
4747{
4748 int isect = 0;
4749
4750 for (const Array<float2> &poly : polys) {
4751 isect += int(math::isect_point_poly(cent_ss, poly.as_span().drop_back(1)));
4752 }
4753
4754 if (isect % 2) {
4755 return true;
4756 }
4757 return false;
4758}
4759
4761 const Span<Object *> objects,
4762 const Span<Array<float2>> polys,
4763 bool use_tag,
4764 bool cut_through)
4765{
4766 KnifeTool_OpData *kcd;
4767
4768 /* Init. */
4769 {
4770 const bool only_select = false;
4771 const bool is_interactive = false; /* Can enable for testing. */
4772 const bool xray = false;
4773 const int visible_measurements = KNF_MEASUREMENT_NONE;
4774 const int angle_snapping = KNF_CONSTRAIN_ANGLE_MODE_NONE;
4775 const float angle_snapping_increment = KNIFE_DEFAULT_ANGLE_SNAPPING_INCREMENT;
4776
4777 kcd = MEM_new<KnifeTool_OpData>(__func__);
4778
4779 knifetool_init(vc,
4780 kcd,
4781 {objects},
4782 only_select,
4783 cut_through,
4784 xray,
4785 visible_measurements,
4786 angle_snapping,
4787 angle_snapping_increment,
4788 is_interactive);
4789
4790 kcd->ignore_edge_snapping = true;
4791 kcd->ignore_vert_snapping = true;
4792 }
4793
4794 /* Execute. */
4795 {
4796 knife_recalc_ortho(kcd);
4797
4798 for (const Array<float2> &poly : polys) {
4799 knife_start_cut(kcd, poly[0]);
4800 kcd->mode = MODE_DRAGGING;
4801
4802 for (const float2 &mval : poly.as_span().drop_front(1)) {
4803 knifetool_update_mval(kcd, mval);
4804 knife_add_cut(kcd);
4805 }
4806
4807 knife_finish_cut(kcd);
4808 kcd->mode = MODE_IDLE;
4809 }
4810 }
4811
4812 /* Finish. */
4813 {
4814 /* See #knifetool_finish_ex for why multiple passes are needed. */
4815 for (int ob_index : kcd->objects.index_range()) {
4816 Object *ob = kcd->objects[ob_index];
4818
4819 if (use_tag) {
4821 }
4822
4823 knifetool_finish_single_pre(kcd, ob_index);
4824 }
4825
4826 for (Object *ob : kcd->objects) {
4828
4829 /* Tag faces inside! */
4830 if (use_tag) {
4831 BMEdge *e;
4832 BMIter iter;
4833 bool keep_search;
4834
4835/* Use face-loop tag to store if we have intersected. */
4836#define F_ISECT_IS_UNKNOWN(f) BM_elem_flag_test(BM_FACE_FIRST_LOOP(f), BM_ELEM_TAG)
4837#define F_ISECT_SET_UNKNOWN(f) BM_elem_flag_enable(BM_FACE_FIRST_LOOP(f), BM_ELEM_TAG)
4838#define F_ISECT_SET_OUTSIDE(f) BM_elem_flag_disable(BM_FACE_FIRST_LOOP(f), BM_ELEM_TAG)
4839 {
4840 BMFace *f;
4841 BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
4844 }
4845 }
4846
4847 /* Tag all faces linked to cut edges. */
4848 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
4849 /* Check are we tagged?, then we are an original face. */
4851 continue;
4852 }
4853
4854 BMFace *f;
4855 BMIter fiter;
4856 BM_ITER_ELEM (f, &fiter, e, BM_FACES_OF_EDGE) {
4857 float cent[3], cent_ss[2];
4859 mul_m4_v3(ob->object_to_world().ptr(), cent);
4860 knife_project_v2(kcd, cent, cent_ss);
4861 if (edbm_mesh_knife_point_isect(polys, cent_ss)) {
4863 }
4864 }
4865 }
4866
4867 /* Expand tags for faces which are not cut, but are inside the polys. */
4868 do {
4869 BMFace *f;
4870 keep_search = false;
4871 BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
4873 continue;
4874 }
4875
4876 /* Am I connected to a tagged face via an un-tagged edge
4877 * (ie, not across a cut)? */
4878 BMLoop *l_first = BM_FACE_FIRST_LOOP(f);
4879 BMLoop *l_iter = l_first;
4880 bool found = false;
4881
4882 do {
4883 if (BM_elem_flag_test(l_iter->e, BM_ELEM_TAG) != false) {
4884 /* Now check if the adjacent faces is tagged. */
4885 BMLoop *l_radial_iter = l_iter->radial_next;
4886 if (l_radial_iter != l_iter) {
4887 do {
4888 if (BM_elem_flag_test(l_radial_iter->f, BM_ELEM_TAG)) {
4889 found = true;
4890 }
4891 } while ((l_radial_iter = l_radial_iter->radial_next) != l_iter &&
4892 (found == false));
4893 }
4894 }
4895 } while ((l_iter = l_iter->next) != l_first && (found == false));
4896
4897 if (found) {
4898 float cent[3], cent_ss[2];
4900 mul_m4_v3(ob->object_to_world().ptr(), cent);
4901 knife_project_v2(kcd, cent, cent_ss);
4902 if ((kcd->cut_through ||
4903 point_is_visible(kcd, cent, cent_ss, reinterpret_cast<BMElem *>(f))) &&
4904 edbm_mesh_knife_point_isect(polys, cent_ss))
4905 {
4907 keep_search = true;
4908 }
4909 else {
4910 /* Don't lose time on this face again, set it as outside. */
4912 }
4913 }
4914 }
4915 } while (keep_search);
4916
4917#undef F_ISECT_IS_UNKNOWN
4918#undef F_ISECT_SET_UNKNOWN
4919#undef F_ISECT_SET_OUTSIDE
4920 }
4921 }
4922
4923 for (Object *ob : kcd->objects) {
4924 /* Defer freeing data until the BVH tree is finished with, see: #point_is_visible and
4925 * the docstring for #knifetool_finish_single_post. */
4927 }
4928
4929 knifetool_exit_ex(kcd);
4930 kcd = nullptr;
4931 }
4932}
4933
4935
4936} // namespace blender
#define DEG2RADF(_deg)
#define RAD2DEGF(_rad)
#define SNPRINTF_UTF8(dst, format,...)
#define BLI_STR_UTF8_DEGREE_SIGN
#define IFACE_(msgid)
#define BLT_I18NCONTEXT_ID_MESH
Object is a sort of wrapper for general info.
#define UI_SCALE_FAC
#define RV3D_CLIPPING_ENABLED(v3d, rv3d)
#define OPERATOR_RETVAL_CHECK(ret)
#define REGION_DRAW_POST_VIEW
#define GPU_batch_uniform_1f(batch, name, x)
Definition GPU_batch.hh:267
#define GPU_batch_uniform_4fv(batch, name, val)
Definition GPU_batch.hh:275
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define UNUSED_FUNCTION(x)
#define INIT_MINMAX(min, max)
#define BLI_assert_unreachable()
#define UNUSED_VARS_NDEBUG(...)
#define UNPACK3(a)
#define ELEM(...)
#define BM_FACE_FIRST_LOOP(p)
#define BM_elem_index_get(ele)
#define BM_elem_flag_disable(ele, hflag)
#define BM_elem_flag_test(ele, hflag)
#define BM_elem_flag_enable(ele, hflag)
#define BM_ITER_ELEM(ele, iter, data, itype)
#define BM_ITER_MESH(ele, iter, bm, itype)
#define MEM_SIZE_OPTIMAL(size)
btScalar max
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
bool closest(btVector3 &v)
const T * data() const
Definition BLI_array.hh:323
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:295
const Value & lookup(const Key &key) const
Definition BLI_map.hh:545
Value lookup_default(const Key &key, const Value &default_value) const
Definition BLI_map.hh:570
ItemIterator items() const &
Definition BLI_map.hh:902
bool contains(const Key &key) const
Definition BLI_set.hh:310
bool add(const Key &key)
Definition BLI_set.hh:248
bool remove(const Key &key)
Definition BLI_set.hh:404
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr bool is_empty() const
Definition BLI_span.hh:264
void append(const T &value)
void item(std::string text, int icon1, int icon2=0)
Definition area.cc:1076
void opmodal(std::string text, const wmOperatorType *ot, int propvalue, bool inverted=false)
Definition area.cc:1107
#define roundf(x)
#define KNIFE_FLT_EPS_PX_VERT
#define KNIFE_FLT_EPS_PX_EDGE
#define F_ISECT_SET_UNKNOWN(f)
#define F_ISECT_SET_OUTSIDE(f)
#define KNIFE_FLT_EPSBIG
#define KNIFE_MAX_ANGLE_SNAPPING_INCREMENT
#define KNIFE_DEFAULT_ANGLE_SNAPPING_INCREMENT
#define KNIFE_FLT_EPS_PX_FACE
#define KNIFE_MIN_ANGLE_SNAPPING_INCREMENT
#define KMAXDIST
#define KNIFE_FLT_EPS_SQUARED
#define KNIFE_FLT_EPS
#define F_ISECT_IS_UNKNOWN(f)
#define FLT_MAX
#define FLT_EPSILON
#define cos
short calc_orientation_from_type_ex(const Main &bmain, const Scene *scene, ViewLayer *view_layer, const View3D *v3d, const RegionView3D *rv3d, Object *ob, Object *obedit, short orientation_index, int pivot_point, float r_mat[3][3])
bool is_zero(const T &a)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
bool isect_point_poly(const float2 &pt, Span< float2 > verts)
void get_color_type_3ubv(int colorid, int spacetype, unsigned char col[3])
void get_color_3ubv(int colorid, unsigned char col[3])
BMesh Iterators.
MINLINE float min_ff(float a, float b)
void EDBM_flag_disable_all(BMesh *bm, char hflag)
MINLINE void madd_v3_v3v3fl(float r[3], const float a[3], const float b[3], float f)
static bool coinciding_edges(BMEdge *e1, BMEdge *e2)
MINLINE float dot_m4_v3_row_z(const float M[4][4], const float a[3]) ATTR_WARN_UNUSED_RESULT
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
MINLINE float len_squared_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void negate_v3_v3(float r[3], const float a[3])
static KnifeEdge * get_bm_knife_edge(KnifeTool_OpData *kcd, BMEdge *e, int ob_index)
float angle_normalized_v3v3(const float v1[3], const float v2[3]) ATTR_WARN_UNUSED_RESULT
void BM_mesh_elem_index_ensure(BMesh *bm, const char htype)
float line_point_factor_v3(const float p[3], const float l1[3], const float l2[3])
void BM_mesh_elem_hflag_enable_all(BMesh *bm, const char htype, const char hflag, const bool respecthide)
__int64 int64_t
Definition BKE_text.h:90
void wmOrtho2_region_pixelspace(const ARegion *region)
MINLINE bool is_zero_v2(const float v[2]) ATTR_WARN_UNUSED_RESULT
BLI_INLINE bool BM_edge_is_wire(const BMEdge *e) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
MINLINE void copy_v2_v2_int(int r[2], const int a[2])
MINLINE void rgba_uchar_to_float(float r_col[4], const unsigned char col_ub[4])
static int get_lowest_face_tri(KnifeTool_OpData *kcd, BMFace *f)
int RNA_enum_get(PointerRNA *ptr, const char *name)
static KnifeVert * knife_split_edge(KnifeTool_OpData *kcd, KnifeEdge *kfe, const float co[3], const float cageco[3], KnifeEdge **r_kfe)
gpu::Batch * GPU_batch_create_ex(GPUPrimType primitive_type, gpu::VertBuf *vertex_buf, gpu::IndexBuf *index_buf, GPUBatchFlag owns_flag)
Definition gpu_batch.cc:53
static void prepare_linehits_for_cut(KnifeTool_OpData *kcd)
bool BM_edge_in_face(const BMEdge *e, const BMFace *f)
void rotate_normalized_v3_v3v3fl(float out[3], const float p[3], const float axis[3], float angle)
static wmOperatorStatus knifetool_invoke(bContext *C, wmOperator *op, const wmEvent *event)
void BLF_position(int fontid, float x, float y, float z)
Definition blf.cc:390
static void knife_constrain_axis(const KnifeTool_OpData *kcd, const float3 &ray_orig, const float3 &ray_dir, float3 &r_cage)
void immEnd()
void EDBM_selectmode_flush(BMesh *bm, short selectmode)
MemArena * BLI_memarena_new(size_t bufsize, const char *name) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL ATTR_NONNULL(2) ATTR_MALLOC
@ KNF_MODAL_IGNORE_SNAP_OFF
@ KNF_MODAL_MIDPOINT_ON
@ KNF_MODAL_ADD_CUT_CLOSED
@ KNF_MODAL_CUT_THROUGH_TOGGLE
@ KNF_MODAL_IGNORE_SNAP_ON
@ KNF_MODAL_SHOW_DISTANCE_ANGLE_TOGGLE
@ KNF_MODAL_MIDPOINT_OFF
@ KNF_MODAL_DEPTH_TEST_TOGGLE
@ KNF_MODAL_CYCLE_ANGLE_SNAP_EDGE
@ KNF_MODAL_ANGLE_SNAP_TOGGLE
static void knife_start_cut(KnifeTool_OpData *kcd, const float2 &mval)
static bool knife_snap_angle_impl(const KnifeTool_OpData *kcd, const float3 &vec_x, const float3 &axis, const float3 &ray_orig, const float3 &ray_dir, float3 &r_cage, float &r_angle)
wmKeyMap * WM_modalkeymap_find(wmKeyConfig *keyconf, const char *idname)
Definition wm_keymap.cc:999
BMesh * BKE_editmesh_bmesh_get_for_write(Mesh *mesh)
Definition editmesh.cc:79
int * BLI_bvhtree_intersect_plane(const BVHTree *tree, float plane[4], uint *r_intersect_num)
BLI_INLINE bool BM_loop_is_adjacent(const BMLoop *l_a, const BMLoop *l_b) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
bool BLI_stack_is_empty(const BLI_Stack *stack) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition stack.cc:253
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
static bool knife_find_closest_edge_of_face(KnifeTool_OpData *kcd, int ob_index, BMFace *f, const float2 &curr_cage_ss, const float3 *curr_cage_constrain, const float3 &ray_orig, const float3 &ray_dir, KnifePosData *r_kpd)
bool handleNumInput(bContext *C, NumInput *n, const wmEvent *event)
Definition numinput.cc:344
static void knife_finish_cut(KnifeTool_OpData *kcd)
static bool knife_linehit_face_test(KnifeTool_OpData *kcd, float s1[2], float s2[2], float sco[2], float ray_start[3], float ray_end[3], int ob_index, BMFace *f, float face_tol_sq, KnifeLineHit *r_hit)
void minmax_v3v3_v3(float min[3], float max[3], const float vec[3])
void BLI_stack_free(BLI_Stack *stack) ATTR_NONNULL()
Definition stack.cc:99
ATTR_WARN_UNUSED_RESULT const BMLoop * l
bool isect_ray_tri_watertight_v3(const float ray_origin[3], const struct IsectRayPrecalc *isect_precalc, const float v0[3], const float v1[3], const float v2[3], float *r_lambda, float r_uv[2])
static bool knife_add_single_cut__is_linehit_outside_face(BMFace *f, const KnifeLineHit *lh, const float co[3])
bool isect_ray_plane_v3(const float ray_origin[3], const float ray_direction[3], const float plane[4], float *r_lambda, bool clip)
static void knifetool_draw_orientation_locking(const KnifeTool_OpData *kcd)
static BMElem * bm_elem_from_knife_edge(KnifeEdge *kfe)
static void knife_make_cuts(KnifeTool_OpData *kcd, int ob_index)
static ListBaseT< LinkData > * knife_get_face_kedges(KnifeTool_OpData *kcd, int ob_index, BMFace *f)
static void knifetool_disable_angle_snapping(KnifeTool_OpData *kcd)
static void knife_recalc_ortho(KnifeTool_OpData *kcd)
void GPU_matrix_identity_set()
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])
bool isect_ray_line_v3(const float ray_origin[3], const float ray_direction[3], const float v0[3], const float v1[3], float *r_lambda)
GPUVertFormat * immVertexFormat()
void BLI_bvhtree_free(BVHTree *tree)
float angle_v3v3v3(const float a[3], const float b[3], const float c[3]) ATTR_WARN_UNUSED_RESULT
MINLINE float mul_project_m4_v3_zfac(const float mat[4][4], const float co[3]) ATTR_WARN_UNUSED_RESULT
void EDBM_update(Mesh *mesh, const EDBMUpdate_Params *params)
void BLI_stack_pop(BLI_Stack *stack, void *dst) ATTR_NONNULL()
Definition stack.cc:141
static void knifetool_init(ViewContext *vc, KnifeTool_OpData *kcd, Vector< Object * > objects, const bool only_select, const bool cut_through, const bool xray, const int visible_measurements, const int angle_snapping, const float angle_snapping_increment, const bool is_interactive)
static int knife_update_active(KnifeTool_OpData *kcd, const float2 &mval)
void WM_cursor_modal_set(wmWindow *win, int val)
static bool point_is_visible(KnifeTool_OpData *kcd, const float p[3], const float s[2], BMElem *ele_test)
static void knifetool_finish(wmOperator *op)
void immBegin(GPUPrimType, uint vertex_len)
static void knife_find_line_hits(KnifeTool_OpData *kcd)
int isect_seg_seg_v2_point_ex(const float v0[2], const float v1[2], const float v2[2], const float v3[2], float endpoint_bias, float r_vi[2])
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
void BLI_bvhtree_balance(BVHTree *tree)
static LinkData * find_ref(ListBaseT< LinkData > *lb, void *ref)
BMLoop * BM_face_edge_share_loop(BMFace *f, BMEdge *e)
Return the Loop Shared by Face and Edge.
void axis_angle_to_quat(float r[4], const float axis[3], float angle)
int BKE_scene_orientation_get_index(Scene *scene, int slot_index)
Definition scene.cc:2678
static bool knife_find_closest_face(KnifeTool_OpData *kcd, const float2 &mval, const float3 &ray_orig, const float3 &ray_dir, KnifePosData *r_kpd)
@ GPU_PRIM_LINE_STRIP
bool hasNumInput(const NumInput *n)
Definition numinput.cc:188
static void knifetool_finish_single_pre(KnifeTool_OpData *kcd, int ob_index)
void GPU_batch_discard(gpu::Batch *batch)
@ KNF_CONSTRAIN_AXIS_NONE
wmWindow * CTX_wm_window(const bContext *C)
void MESH_OT_knife_tool(wmOperatorType *ot)
static void knife_add_cut(KnifeTool_OpData *kcd)
@ TH_GIZMO_SECONDARY
@ KNF_CONSTRAIN_AXIS_MODE_GLOBAL
@ KNF_CONSTRAIN_AXIS_MODE_NONE
@ KNF_CONSTRAIN_AXIS_MODE_LOCAL
void project_plane_normalized_v3_v3v3(float out[3], const float p[3], const float v_plane[3])
ATTR_WARN_UNUSED_RESULT const BMVert * v
static bool knife_snap_angle_relative(KnifeTool_OpData *kcd, const float3 &ray_orig, const float3 &ray_dir, float3 &r_cage, float &r_angle)
void immUniformColor4fv(const float rgba[4])
BMVert * BM_vert_create(BMesh *bm, const float co[3], const BMVert *v_example, const eBMCreateFlag create_flag)
Main function for creating a new vertex.
gpu::Batch * immBeginBatchAtMost(GPUPrimType, uint vertex_len)
static void knifetool_undo(KnifeTool_OpData *kcd)
float normal_tri_v3(float n[3], const float v1[3], const float v2[3], const float v3[3])
Definition math_geom.cc:45
static bool knife_bm_face_is_select(BMFace *f)
void BLI_stack_discard(BLI_Stack *stack) ATTR_NONNULL()
Definition stack.cc:179
int BLI_bvhtree_ray_cast(const BVHTree *tree, const float co[3], const float dir[3], float radius, BVHTreeRayHit *hit, BVHTree_RayCastCallback callback, void *userdata)
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:681
void * BLI_stack_push_r(BLI_Stack *stack) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition stack.cc:106
void BLF_disable(int fontid, FontFlags flag)
Definition blf.cc:331
void plane_from_point_normal_v3(float r_plane[4], const float plane_co[3], const float plane_no[3])
Definition math_geom.cc:225
void immUnbindProgram()
static void knifetool_draw(const bContext *, ARegion *, void *arg)
static void knife_init_colors(KnifeColors *colors)
float angle_signed_on_axis_v3v3_v3(const float v1[3], const float v2[3], const float axis[3]) ATTR_WARN_UNUSED_RESULT
static BMFace * knife_find_common_face(ListBaseT< LinkData > *faces1, ListBaseT< LinkData > *faces2)
void GPU_batch_draw_range(gpu::Batch *batch, int vertex_first, int vertex_count)
void immUniformColor3ubv(const unsigned char rgb[3])
void GPU_line_width(float width)
Definition gpu_state.cc:151
static void knife_snap_curr(KnifeTool_OpData *kcd, const float2 &mval, const float3 &ray_orig, const float3 &ray_dir, const float3 *curr_cage_constrain, const float3 *fallback)
static bool knife_bm_face_is_not_hidden(BMFace *f)
@ GPU_BLEND_NONE
Definition GPU_state.hh:88
@ GPU_BLEND_ALPHA
Definition GPU_state.hh:90
bool ED_view3d_unproject_v3(const ARegion *region, float regionx, float regiony, float regionz, float world[3])
static void knife_input_ray_segment(const KnifeTool_OpData *kcd, const float mval[2], float r_origin[3], float r_end[3])
static void calc_ortho_extent(KnifeTool_OpData *kcd)
static KnifeEdge * new_knife_edge(KnifeTool_OpData *kcd)
@ KNF_MEASUREMENT_ANGLE
@ KNF_MEASUREMENT_DISTANCE
bool BM_face_point_inside_test(const BMFace *f, const float co[3])
float dist_squared_to_line_segment_v2(const float p[2], const float l1[2], const float l2[2])
Definition math_geom.cc:307
static void knifetool_draw_visible_angles(const KnifeTool_OpData *kcd)
MINLINE void sub_v3_v3(float r[3], const float a[3])
VecBase< float, 4 > float4
void mul_qt_v3(const float q[4], float r[3])
void immBindBuiltinProgram(GPUBuiltinShader shader_id)
static ListBaseT< LinkData > * knife_empty_list(KnifeTool_OpData *kcd)
wmKeyMap * knifetool_modal_keymap(wmKeyConfig *keyconf)
void mul_m4_v3(const float M[4][4], float r[3])
MINLINE bool equals_v3v3(const float v1[3], const float v2[3]) ATTR_WARN_UNUSED_RESULT
static void knife_project_v2(const KnifeTool_OpData *kcd, const float co[3], float sco[2])
static void knife_append_list_no_dup(KnifeTool_OpData *kcd, ListBaseT< LinkData > *lst, void *elem)
static void knifetool_draw_dist_angle(const KnifeTool_OpData *kcd)
void BLI_mempool_destroy(BLI_mempool *pool) ATTR_NONNULL(1)
static void knifetool_draw_visible_distances(const KnifeTool_OpData *kcd)
static float knife_snap_size(KnifeTool_OpData *kcd, float maxsize)
void BLF_size(int fontid, float size)
Definition blf.cc:445
void * ED_region_draw_cb_activate(ARegionType *art, void(*draw)(const bContext *, ARegion *, void *), void *customdata, int type)
void EDBM_mesh_knife(ViewContext *vc, const Span< Object * > objects, const Span< Array< float2 > > polys, bool use_tag, bool cut_through)
VecBase< float, 2 > float2
void immUniform1f(const char *name, float x)
@ GPU_DEPTH_NONE
Definition GPU_state.hh:132
@ GPU_DEPTH_LESS_EQUAL
Definition GPU_state.hh:135
void transform_point_by_seg_v3(float p_dst[3], const float p_src[3], const float l_dst_p1[3], const float l_dst_p2[3], const float l_src_p1[3], const float l_src_p2[3])
void interp_v3_v3v3v3_uv(float p[3], const float v1[3], const float v2[3], const float v3[3], const float uv[2])
void void void BLI_listbase_sort_r(ListBase *listbase, int(*cmp)(void *, const void *, const void *), void *thunk) ATTR_NONNULL(1
Definition listbase.cc:328
static void linehit_to_knifepos(KnifePosData *kpos, KnifeLineHit *lh)
unsigned char uchar
Definition BKE_text.h:58
static KnifeVert * get_bm_knife_vert(KnifeTool_OpData *kcd, BMVert *v, int ob_index)
MINLINE void cross_v3_v3v3(float r[3], const float a[3], const float b[3])
bool ED_region_draw_cb_exit(ARegionType *art, void *handle)
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
void BM_edge_kill(BMesh *bm, BMEdge *e)
static void knifetool_disable_orientation_locking(KnifeTool_OpData *kcd)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
BMesh const char void * data
void BLF_color3ubv(int fontid, const unsigned char rgb[3])
Definition blf.cc:478
void GPU_vertbuf_attr_set(gpu::VertBuf *, uint a_idx, uint v_idx, const void *data)
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
static void knife_reset_snap_angle_input(KnifeTool_OpData *kcd)
@ GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA
void initNumInput(NumInput *n)
Definition numinput.cc:85
BLI_mempool * BLI_mempool_create(unsigned int esize, unsigned int elem_num, unsigned int pchunk, unsigned int flag) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL
void BLI_memarena_clear(MemArena *ma) ATTR_NONNULL(1)
static bool knife_closest_constrain_to_edge(const float3 &cut_origin, const float3 &cut_dir, const float3 &kfv1_cageco, const float3 &kfv2_cageco, float r_close[3])
void immUniformThemeColor3(int color_id)
eV3DProjStatus ED_view3d_project_float_global(const ARegion *region, const float co[3], float r_co[2], eV3DProjTest flag)
static void knife_bvh_init(KnifeTool_OpData *kcd)
BVHTree * BLI_bvhtree_new(int maxsize, float epsilon, char tree_type, char axis)
static bool edbm_mesh_knife_point_isect(const Span< Array< float2 > > polys, const float2 cent_ss)
static void clip_to_ortho_planes(float v1[3], float v2[3], const float center[3], const float d)
void RNA_def_property_subtype(PropertyRNA *prop, PropertySubType subtype)
void WM_cursor_modal_restore(wmWindow *win)
bool BM_vert_in_face(BMVert *v, BMFace *f)
static bool knife_ray_intersect_face(KnifeTool_OpData *kcd, const float s[2], const float v1[3], const float v2[3], int ob_index, BMFace *f, const float face_tol_sq, float hit_co[3], float hit_cageco[3])
static void knife_bm_tri_cagecos_get(const KnifeTool_OpData *kcd, int ob_index, int tri_index, float cos[3][3])
void GPU_batch_draw(gpu::Batch *batch)
static BMFace * knife_bvh_raycast_filter(KnifeTool_OpData *kcd, const float co[3], const float dir[3], const float radius, float *r_dist, float r_cagehit[3], int *r_ob_index, bool(*filter_cb)(BMFace *f, void *userdata), void *filter_userdata)
void BLI_mempool_iternew(BLI_mempool *pool, BLI_mempool_iter *iter) ATTR_NONNULL()
@ GPU_BATCH_OWNS_VBO
Definition GPU_batch.hh:44
void GPU_program_point_size(bool enable)
Definition gpu_state.cc:165
BLI_INLINE bool BM_edge_is_boundary(const BMEdge *e) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void interp_v3_v3v3(float r[3], const float a[3], const float b[3], float t)
void * BLI_mempool_iterstep(BLI_mempool_iter *iter) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Array< float3 > BM_mesh_vert_coords_alloc(const BMesh *bm)
static void knifetool_cancel(bContext *, wmOperator *op)
size_t BKE_unit_value_as_string(char *str, int str_maxncpy, double value, int prec, int type, const UnitSettings &settings, bool pad, bool do_unit_suffix)
Definition unit.cc:1929
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
static void knife_add_single_cut(KnifeTool_OpData *kcd, KnifeLineHit *lh1, KnifeLineHit *lh2, BMFace *f)
void * BLI_stack_peek(BLI_Stack *stack) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition stack.cc:172
static void knifetool_draw_angle(const KnifeTool_OpData *kcd, const float start[3], const float mid[3], const float end[3], const float start_ss[2], const float mid_ss[2], const float end_ss[2], const float angle)
static void knife_pos_data_clear(KnifePosData *kpd)
bool BKE_editmesh_eval_orig_map_available(const Mesh &mesh_eval, const Mesh *mesh_orig)
Definition editmesh.cc:93
void ED_view3d_init_mats_rv3d(const Object *ob, RegionView3D *rv3d)
bool isect_ray_tri_v3(const float ray_origin[3], const float ray_direction[3], const float v0[3], const float v1[3], const float v2[3], float *r_lambda, float r_uv[2])
size_t BKE_unit_value_as_string_scaled(char *str, int str_maxncpy, double value, int prec, int type, const UnitSettings &settings, bool pad, bool do_unit_suffix)
Definition unit.cc:1944
MINLINE void zero_v3(float r[3])
void BLF_draw(int fontid, const char *str, size_t str_len, ResultBLF *r_info=nullptr) ATTR_NONNULL(2)
Definition blf.cc:587
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
int BLI_listbase_count_at_most(const ListBase *listbase, int count_max) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:518
bool isect_ray_ray_v3(const float ray_origin_a[3], const float ray_direction_a[3], const float ray_origin_b[3], const float ray_direction_b[3], float *r_lambda_a, float *r_lambda_b)
void GPU_batch_program_set_builtin(gpu::Batch *batch, GPUBuiltinShader shader_id)
void BM_face_calc_point_in_face(const BMFace *f, float r_co[3])
bool isect_line_plane_v3(float r_isect_co[3], const float l1[3], const float l2[3], const float plane_co[3], const float plane_no[3]) ATTR_WARN_UNUSED_RESULT
BMFace * EDBM_face_find_nearest(ViewContext *vc, float *dist_px_manhattan_p)
bool applyNumInput(NumInput *n, float *vec)
Definition numinput.cc:207
bool BKE_object_is_visible_in_viewport(const View3D *v3d, const Object *ob)
static void knife_make_face_cuts(KnifeTool_OpData *kcd, BMesh *bm, BMFace *f, ListBaseT< LinkData > *kfedges)
bool ED_view3d_clip_range_get(const Depsgraph *depsgraph, const View3D *v3d, const RegionView3D *rv3d, bool use_ortho_factor, float *r_clip_start, float *r_clip_end)
static bool knife_find_closest_vert_of_edge(KnifeTool_OpData *kcd, const KnifeEdge *kfe, const float2 &cage_ss, KnifePosData *r_kpd)
MINLINE float normalize_v3_v3(float r[3], const float a[3])
static void knifetool_finish_ex(KnifeTool_OpData *kcd)
static void knifetool_update_mval(KnifeTool_OpData *kcd, const float2 &mval)
void RNA_def_property_translation_context(PropertyRNA *prop, const char *context)
BMEditMesh * BKE_editmesh_from_object(Object *ob)
Return the BMEditMesh for a given object.
Definition editmesh.cc:59
void * BLI_memarena_alloc(MemArena *ma, size_t size) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_ALLOC_SIZE(2)
BMEdge * BM_edge_exists(BMVert *v_a, BMVert *v_b)
MINLINE float normalize_v3(float n[3])
static bool knife_snap_angle_screen(const KnifeTool_OpData *kcd, const float3 &ray_orig, const float3 &ray_dir, float3 &r_cage, float &r_angle)
MINLINE void add_v3_v3(float r[3], const float a[3])
wmEventHandler_Op * WM_event_add_modal_handler(bContext *C, wmOperator *op)
void immVertex3fv(uint attr_id, const float data[3])
ViewContext em_setup_viewcontext(bContext *C)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
static void knife_edge_append_face(KnifeTool_OpData *kcd, KnifeEdge *kfe, BMFace *f)
void mid_v3_v3v3(float r[3], const float a[3], const float b[3])
static bool bm_ray_cast_cb_elem_not_in_face_check(BMFace *f, void *user_data)
MINLINE bool compare_v3v3(const float v1[3], const float v2[3], float limit) ATTR_WARN_UNUSED_RESULT
BMVert * BM_edge_split(BMesh *bm, BMEdge *e, BMVert *v, BMEdge **r_e, float fac)
Edge Split.
bool ED_view3d_clipping_test(const RegionView3D *rv3d, const float co[3], bool is_local)
@ WM_CURSOR_KNIFE
Definition wm_cursors.hh:33
float closest_ray_to_segment_v3(const float ray_origin[3], const float ray_direction[3], const float v0[3], const float v1[3], float r_close[3])
Definition math_geom.cc:425
float dist_squared_to_plane_v3(const float p[3], const float plane[4])
Definition math_geom.cc:486
static void knife_add_edge_faces_to_vert(KnifeTool_OpData *kcd, KnifeVert *kfv, BMEdge *e)
static void UNUSED_FUNCTION knifetool_recast_cageco(KnifeTool_OpData *kcd, float mval[3], float r_cage[3])
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
static int linehit_compare(const KnifeLineHit &lh1, const KnifeLineHit &lh2)
ATTR_WARN_UNUSED_RESULT const BMVert * v2
void GPU_matrix_pop_projection()
static void knife_join_edge(KnifeEdge *newkfe, KnifeEdge *kfe)
float ED_view3d_pixel_size_no_ui_scale(const RegionView3D *rv3d, const float co[3])
bool ED_view3d_win_to_segment_clipped(const Depsgraph *depsgraph, const ARegion *region, const View3D *v3d, const float mval[2], float r_ray_start[3], float r_ray_end[3], bool do_clip_planes)
bool ED_view3d_win_to_ray_clipped(Depsgraph *depsgraph, const ARegion *region, const View3D *v3d, const float mval[2], float r_ray_start[3], float r_ray_normal[3], bool do_clip_planes)
@ BLI_MEMPOOL_ALLOW_ITER
static void knife_draw_line(const KnifeTool_OpData *kcd, const uchar color[3])
static void knife_bvh_raycast_cb(void *userdata, int index, const BVHTreeRay *ray, BVHTreeRayHit *hit)
MINLINE float len_v2v2(const float v1[2], const float v2[2]) ATTR_WARN_UNUSED_RESULT
static BMFace * knife_bvh_raycast(KnifeTool_OpData *kcd, const float co[3], const float dir[3], const float radius, float *r_dist, float r_cagehit[3], int *r_ob_index)
Array< float3 > BKE_editmesh_vert_coords_alloc(Depsgraph *depsgraph, const BMesh *bm, Scene *scene, Object *ob)
Definition editmesh.cc:190
void GPU_vertbuf_data_alloc(gpu::VertBuf &verts, uint v_len)
static void add_hit_to_facehits(KnifeTool_OpData *kcd, Map< BMFace *, ListBaseT< LinkData > * > &facehits, BMFace *f, KnifeLineHit *hit)
static wmOperatorStatus knifetool_modal(bContext *C, wmOperator *op, const wmEvent *event)
void mid_v2_v2v2(float r[2], const float a[2], const float b[2])
void * BLI_mempool_calloc(BLI_mempool *pool) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL ATTR_NONNULL(1)
@ PROP_SKIP_SAVE
Definition RNA_types.hh:414
@ PROP_HIDDEN
Definition RNA_types.hh:408
@ V3D_PROJ_TEST_NOP
Definition ED_view3d.hh:295
bool BM_face_split_edgenet_connect_islands(BMesh *bm, BMFace *f, BMEdge **edge_net_init, const uint edge_net_init_len, bool use_partial_connect, MemArena *mem_arena, BMEdge ***r_edge_net_new, uint *r_edge_net_new_len)
Vector< Object * > BKE_view_layer_array_from_objects_in_edit_mode_unique_data(const Main &bmain, const Scene *scene, ViewLayer *view_layer, const View3D *v3d)
void BLI_bvhtree_insert(BVHTree *tree, int index, const float co[3], int numpoints)
static int sort_verts_by_dist_cb(void *co_p, const void *cur_a_p, const void *cur_b_p)
bool BM_face_split_edgenet(BMesh *bm, BMFace *f, BMEdge **edge_net, const int edge_net_len, Vector< BMFace * > *r_face_arr)
static KnifeVert * new_knife_vert(KnifeTool_OpData *kcd, const float co[3], const float cageco[3])
const BMesh * BKE_editmesh_bmesh_get(const Object *ob)
Definition editmesh.cc:65
@ KM_NOTHING
Definition WM_types.hh:323
@ KM_RELEASE
Definition WM_types.hh:325
void WM_modalkeymap_assign(wmKeyMap *km, const char *opname)
void BLI_stack_push(BLI_Stack *stack, const void *src) ATTR_NONNULL()
Definition stack.cc:135
bool EDBM_uvselect_clear(BMesh *bm)
int blf_mono_font
Definition blf.cc:50
static void knife_linehit_set(KnifeTool_OpData *kcd, float s1[2], float s2[2], float sco[2], float cage[3], int ob_index, KnifeVert *v, KnifeEdge *kfe, KnifeLineHit *r_hit)
BMLoop * BM_face_vert_share_loop(BMFace *f, BMVert *v)
Return the Loop Shared by Face and Vertex.
static float knife_snap_v3_angle(float3 &r, const float3 &dvec, const float3 &vecx, const float3 &axis, float angle_snap)
bool ED_operator_editmesh_view3d(bContext *C)
bool isect_ray_tri_epsilon_v3(const float ray_origin[3], const float ray_direction[3], const float v0[3], const float v1[3], const float v2[3], float *r_lambda, float r_uv[2], float epsilon)
static int knife_sample_screen_density_from_closest_face(KnifeTool_OpData *kcd, const float radius, int ob_index, BMFace *f, const float cageco[3])
wmKeyMap * WM_modalkeymap_ensure(wmKeyConfig *keyconf, const char *idname, const EnumPropertyItem *items)
Definition wm_keymap.cc:972
static void knife_cut_face(KnifeTool_OpData *kcd, BMFace *f, ListBaseT< LinkData > *hits)
void GPU_blend(GPUBlend blend)
Definition gpu_state.cc:44
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
@ OPTYPE_BLOCKING
Definition WM_types.hh:192
void GPU_polygon_offset(float viewdist, float dist)
static int knife_calculate_snap_ref_edges(KnifeTool_OpData *kcd, const float3 &ray_orig, const float3 &ray_dir)
float BM_loop_point_side_of_edge_test(const BMLoop *l, const float co[3])
int BLI_mempool_len(const BLI_mempool *pool) ATTR_NONNULL(1)
VecBase< float, 3 > float3
static void knife_append_list(KnifeTool_OpData *kcd, ListBaseT< LinkData > *lst, void *elem)
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
std::optional< std::string > WM_modalkeymap_operator_items_to_string(wmOperatorType *ot, const int propvalue, const bool compact)
MINLINE float len_squared_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
bool clip_segment_v3_plane_n(const float p1[3], const float p2[3], const float plane_array[][4], int plane_num, float r_p1[3], float r_p2[3])
void BLF_width_and_height(int fontid, const char *str, size_t str_len, float *r_width, float *r_height) ATTR_NONNULL()
Definition blf.cc:822
@ B_UNIT_LENGTH
Definition BKE_unit.hh:166
@ B_UNIT_NONE
Definition BKE_unit.hh:165
@ B_UNIT_ROTATION
Definition BKE_unit.hh:170
void GPU_matrix_pop()
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 BLF_rotation(int fontid, float angle)
Definition blf.cc:936
void GPU_matrix_push_projection()
BLI_Stack * BLI_stack_new(size_t elem_size, const char *description) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition stack.cc:85
static void knifetool_finish_single_post(KnifeTool_OpData *, Object *ob)
float closest_to_line_segment_v2(float r_close[2], const float p[2], const float l1[2], const float l2[2])
Definition math_geom.cc:381
static void knifetool_exit_ex(KnifeTool_OpData *kcd)
void GPU_depth_test(GPUDepthTest test)
Definition gpu_state.cc:70
static void knifetool_init_obinfo(KnifeTool_OpData *kcd, Object *ob, int ob_index, bool use_tri_indices)
PropertyRNA * RNA_def_float(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)
void immRectf(uint pos, float x1, float y1, float x2, float y2)
MINLINE void zero_v2(float r[2])
void ED_workspace_status_text(bContext *C, const char *str)
Definition area.cc:1153
static void knife_update_header(bContext *C, wmOperator *op, KnifeTool_OpData *kcd)
static void knife_add_to_vert_edges(KnifeTool_OpData *kcd, KnifeEdge *kfe)
BMEdge * BM_edge_create(BMesh *bm, BMVert *v1, BMVert *v2, const BMEdge *e_example, const eBMCreateFlag create_flag)
Main function for creating a new edge.
float RNA_float_get(PointerRNA *ptr, const char *name)
static void knife_bm_tri_cagecos_get_worldspace(const KnifeTool_OpData *kcd, int ob_index, int tri_index, float cos[3][3])
static void knifetool_draw_angle_snapping(const KnifeTool_OpData *kcd)
void BLF_enable(int fontid, FontFlags flag)
Definition blf.cc:322
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
@ BLF_ROTATION
Definition BLF_enums.hh:36
static void knife_bvh_free(KnifeTool_OpData *kcd)
void GPU_matrix_push()
static BMElem * bm_elem_from_knife_vert(KnifeVert *kfv, KnifeEdge **r_kfe)
void BLI_memarena_free(MemArena *ma) ATTR_NONNULL(1)
static bool knife_verts_edge_in_face(KnifeVert *v1, KnifeVert *v2, BMFace *f)
static const int * knife_bm_tri_index_get(const KnifeTool_OpData *kcd, int ob_index, int tri_index, int tri_index_buf[3])
static void knife_snap_update_from_mval(KnifeTool_OpData *kcd, const float2 &mval)
static void knifetool_exit(wmOperator *op)
@ KNF_CONSTRAIN_ANGLE_MODE_NONE
@ KNF_CONSTRAIN_ANGLE_MODE_RELATIVE
@ KNF_CONSTRAIN_ANGLE_MODE_SCREEN
void mul_transposed_mat3_m4_v3(const float M[4][4], float r[3])
float BM_loop_point_side_of_loop_test(const BMLoop *l, const float co[3])
static void set_lowest_face_tri(KnifeTool_OpData *kcd, BMEditMesh *em, BMFace *f, int index)
@ NUM_NO_NEGATIVE
void BM_edge_select_set(BMesh *bm, BMEdge *e, const bool select)
Select Edge.
#define fabsf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
bke::ARegionRuntime * runtime
Array< std::array< BMLoop *, 3 > > looptris
struct BMLoop * next
struct BMEdge * e
struct BMLoop * radial_next
struct BMFace * f
struct IsectRayPrecalc * isect_precalc
bool(*) filter_cb(BMFace *f, void *userdata)
Span< std::array< BMLoop *, 3 > > looptris
ListBaseT< LinkData > faces
Array< float3 > positions_cage
Map< BMFace *, ListBaseT< LinkData > * > * kedgefacemap
Array< KnifeObjectInfo > objects_info
Map< BMEdge *, KnifeEdge * > * origedgemap
Map< BMVert *, KnifeVert * > * origvertmap
Map< BMFace *, int > * facetrimap
struct blender::KnifeTool_OpData::@114110275332016177143025026167226207221303063253 edgenet
Vector< Object * > objects
Vector< KnifeLineHit > linehits
ListBaseT< LinkData > edges
ListBaseT< LinkData > faces
struct LinkData * next
bke::MeshRuntime * runtime
float val[NUM_MAX_ELEMENTS]
int unit_type[NUM_MAX_ELEMENTS]
char str[NUM_STR_REP_LEN]
short val_flag[NUM_MAX_ELEMENTS]
eRegionView3D_Persp persp
struct UnitSettings unit
struct ToolSettings * toolsettings
ViewLayer * view_layer
Definition ED_view3d.hh:80
RegionView3D * rv3d
Definition ED_view3d.hh:86
Depsgraph * depsgraph
Definition ED_view3d.hh:78
struct ARegionType * type
std::shared_ptr< BMEditMesh > edit_mesh
wmEventType type
Definition WM_types.hh:817
const char * description
Definition WM_types.hh:1115
void(*) cancel(bContext *C, wmOperator *op)
Definition WM_types.hh:1149
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
struct ReportList * reports
struct wmOperatorType * type