Blender  V3.3
editmesh_knife.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2007 Blender Foundation. All rights reserved. */
3 
10 #ifdef _MSC_VER
11 # define _USE_MATH_DEFINES
12 #endif
13 
14 #include "MEM_guardedalloc.h"
15 
16 #include "BLF_api.h"
17 
18 #include "BLI_alloca.h"
19 #include "BLI_array.h"
20 #include "BLI_linklist.h"
21 #include "BLI_listbase.h"
22 #include "BLI_math.h"
23 #include "BLI_memarena.h"
24 #include "BLI_smallhash.h"
25 #include "BLI_stack.h"
26 #include "BLI_string.h"
27 
28 #include "BLT_translation.h"
29 
30 #include "BKE_bvhutils.h"
31 #include "BKE_context.h"
32 #include "BKE_editmesh.h"
33 #include "BKE_editmesh_bvh.h"
34 #include "BKE_layer.h"
35 #include "BKE_report.h"
36 #include "BKE_scene.h"
37 #include "BKE_unit.h"
38 
39 #include "GPU_immediate.h"
40 #include "GPU_matrix.h"
41 #include "GPU_state.h"
42 
43 #include "ED_mesh.h"
44 #include "ED_numinput.h"
45 #include "ED_screen.h"
46 #include "ED_space_api.h"
47 #include "ED_transform.h"
48 #include "ED_view3d.h"
49 
50 #include "WM_api.h"
51 #include "WM_types.h"
52 
53 #include "DNA_object_types.h"
54 
55 #include "UI_interface.h"
56 #include "UI_resources.h"
57 
58 #include "RNA_access.h"
59 #include "RNA_define.h"
60 
61 #include "DEG_depsgraph.h"
62 #include "DEG_depsgraph_query.h"
63 
64 #include "mesh_intern.h" /* Own include. */
65 
66 /* Detect isolated holes and fill them. */
67 #define USE_NET_ISLAND_CONNECT
68 
69 #define KMAXDIST (10 * U.dpi_fac) /* Max mouse distance from edge before not detecting it. */
70 
71 /* WARNING: Knife float precision is fragile:
72  * Be careful before making changes here see: (T43229, T42864, T42459, T41164).
73  */
74 #define KNIFE_FLT_EPS 0.00001f
75 #define KNIFE_FLT_EPS_SQUARED (KNIFE_FLT_EPS * KNIFE_FLT_EPS)
76 #define KNIFE_FLT_EPSBIG 0.0005f
77 
78 #define KNIFE_FLT_EPS_PX_VERT 0.5f
79 #define KNIFE_FLT_EPS_PX_EDGE 0.05f
80 #define KNIFE_FLT_EPS_PX_FACE 0.05f
81 
82 #define KNIFE_DEFAULT_ANGLE_SNAPPING_INCREMENT 30.0f
83 #define KNIFE_MIN_ANGLE_SNAPPING_INCREMENT 0.0f
84 #define KNIFE_MAX_ANGLE_SNAPPING_INCREMENT 180.0f
85 
86 typedef struct KnifeColors {
87  uchar line[3];
88  uchar edge[3];
99 
100 /* Knife-tool Operator. */
101 typedef struct KnifeVert {
104  BMVert *v; /* Non-NULL if this is an original vert. */
107 
108  float co[3], cageco[3];
110  bool is_cut; /* Along a cut created by user input (will draw too). */
112  bool is_splitting; /* Created when an edge was split. */
114 
115 typedef struct Ref {
116  struct Ref *next, *prev;
117  void *ref;
118 } Ref;
119 
120 typedef struct KnifeEdge {
122  BMFace *basef; /* Face to restrict face fill to. */
124 
125  BMEdge *e; /* Non-NULL if this is an original edge. */
126  bool is_cut; /* Along a cut created by user input (will draw too). */
128  int splits; /* Number of times this edge has been split. */
130 
131 typedef struct KnifeLineHit {
132  float hit[3], cagehit[3];
133  float schit[2]; /* Screen coordinates for cagehit. */
134  float l; /* Lambda along cut line. */
135  float perc; /* Lambda along hit line. */
136  float m; /* Depth front-to-back. */
137 
138  /* Exactly one of kfe, v, or f should be non-NULL,
139  * saying whether cut line crosses and edge,
140  * is snapped to a vert, or is in the middle of some face. */
147 
148 typedef struct KnifePosData {
149  float co[3];
150  float cage[3];
151 
152  /* At most one of vert, edge, or bmface should be non-NULL,
153  * saying whether the point is snapped to a vertex, edge, or in a face.
154  * If none are set, this point is in space and is_space should be true. */
158  Object *ob; /* Object of the vert, edge or bmface. */
160 
161  /* When true, the cursor isn't over a face. */
162  bool is_space;
163 
164  float mval[2]; /* Mouse screen position (may be non-integral if snapped to something). */
166 
167 typedef struct KnifeMeasureData {
168  float cage[3];
169  float mval[2];
170  bool is_stored;
172 
173 typedef struct KnifeUndoFrame {
174  int cuts; /* Line hits cause multiple edges/cuts to be created at once. */
175  int splits; /* Number of edges split. */
176  KnifePosData pos; /* Store previous KnifePosData. */
178 
180 
181 typedef struct KnifeBVH {
182  BVHTree *tree; /* Knife Custom BVH Tree. */
183  BMLoop *(*looptris)[3]; /* Used by #knife_bvh_raycast_cb to store the intersecting looptri. */
184  float uv[2]; /* Used by #knife_bvh_raycast_cb to store the intersecting uv. */
186 
187  /* Use #bm_ray_cast_cb_elem_not_in_face_check. */
188  bool (*filter_cb)(BMFace *f, void *userdata);
189  void *filter_data;
190 
192 
194 typedef struct KnifeObjectInfo {
195  const float (*cagecos)[3];
196 
203  const int (*tri_indices)[3];
204 
208 
209 /* struct for properties used while drawing */
210 typedef struct KnifeTool_OpData {
211  ARegion *region; /* Region that knifetool was activated in. */
212  void *draw_handle; /* For drawing preview loop. */
213  ViewContext vc; /* NOTE: _don't_ use 'mval', instead use the one we define below. */
214  float mval[2]; /* Mouse value with snapping applied. */
215 
217 
218  /* Used for swapping current object when in multi-object edit mode. */
222 
225 
227 
228  /* Reused for edge-net filling. */
229  struct {
230  /* Cleared each use. */
232 #ifdef USE_NET_ISLAND_CONNECT
233  MemArena *arena;
234 #endif
236 
241 
243 
246  bool no_cuts; /* A cut has not been made yet. */
247 
249  BLI_Stack *splitstack; /* Store edge splits by #knife_split_edge. */
250 
251  float vthresh;
252  float ethresh;
253 
254  /* Used for drag-cutting. */
257 
258  /* Data for mouse-position-derived data. */
259  KnifePosData curr; /* Current point under the cursor. */
260  KnifePosData prev; /* Last added cut (a line draws from the cursor to this). */
261  KnifePosData init; /* The first point in the cut-list, used for closing the loop. */
262 
263  /* Number of knife edges `kedges`. */
264  int totkedge;
265  /* Number of knife vertices, `kverts`. */
266  int totkvert;
267 
269 
271 
272  /* Run by the UI or not. */
274 
275  /* Operator options. */
276  bool cut_through; /* Preference, can be modified at runtime (that feature may go). */
277  bool only_select; /* Set on initialization. */
278  bool select_result; /* Set on initialization. */
279 
280  bool is_ortho;
283 
284  float clipsta, clipend;
285 
288 
289  int prevmode;
293 
295  float angle_snapping_increment; /* Degrees */
296 
297  /* Use to check if we're currently dragging an angle snapped line. */
301  float angle;
302  /* Relative angle snapping reference edge. */
305  int snap_edge; /* Used by #KNF_MODAL_CYCLE_ANGLE_SNAP_EDGE to choose an edge for snapping. */
306 
310  char axis_string[2];
311 
314  KnifeMeasureData mdata; /* Data for distance and angle drawing calculations. */
315 
316  KnifeUndoFrame *undo; /* Current undo frame. */
318 
321 
322 enum {
342 };
343 
344 enum {
348 };
349 
350 enum {
355 };
356 
357 enum {
361 };
362 
363 enum {
368 };
369 
370 /* -------------------------------------------------------------------- */
374 static void knifetool_raycast_planes(const KnifeTool_OpData *kcd, float r_v1[3], float r_v2[3])
375 {
376  float planes[4][4];
378  kcd->vc.rv3d->persmat, planes[2], planes[0], planes[1], planes[3], NULL, NULL);
379 
380  /* Ray-cast all planes. */
381  float ray_dir[3];
382  float ray_hit_best[2][3] = {{UNPACK3(kcd->prev.cage)}, {UNPACK3(kcd->curr.cage)}};
383  float lambda_best[2] = {-FLT_MAX, FLT_MAX};
384  int i;
385 
386  {
387  float curr_cage_adjust[3];
388  float co_depth[3];
389 
390  copy_v3_v3(co_depth, kcd->prev.cage);
391  ED_view3d_win_to_3d(kcd->vc.v3d, kcd->region, co_depth, kcd->curr.mval, curr_cage_adjust);
392 
393  sub_v3_v3v3(ray_dir, curr_cage_adjust, kcd->prev.cage);
394  }
395 
396  for (i = 0; i < 4; i++) {
397  float ray_hit[3];
398  float lambda_test;
399  if (!isect_ray_plane_v3(kcd->prev.cage, ray_dir, planes[i], &lambda_test, false)) {
400  continue;
401  }
402 
403  madd_v3_v3v3fl(ray_hit, kcd->prev.cage, ray_dir, lambda_test);
404  if (lambda_test < 0.0f) {
405  if (lambda_test > lambda_best[0]) {
406  copy_v3_v3(ray_hit_best[0], ray_hit);
407  lambda_best[0] = lambda_test;
408  }
409  }
410  else {
411  if (lambda_test < lambda_best[1]) {
412  copy_v3_v3(ray_hit_best[1], ray_hit);
413  lambda_best[1] = lambda_test;
414  }
415  }
416  }
417 
418  copy_v3_v3(r_v1, ray_hit_best[0]);
419  copy_v3_v3(r_v2, ray_hit_best[1]);
420 }
421 
423 {
424  float v1[3], v2[3];
425 
427 
429 
432  GPU_line_width(2.0);
433 
435  immVertex3fv(pos, v1);
436  immVertex3fv(pos, v2);
437  immEnd();
438 
440 }
441 
443 {
444  if (compare_v3v3(kcd->prev.cage, kcd->curr.cage, KNIFE_FLT_EPSBIG)) {
445  return;
446  }
447 
448  float v1[3], v2[3];
449 
450  /* This is causing buggy behavior when `prev.cage` and `curr.cage` are too close together. */
452 
455 
456  switch (kcd->constrain_axis) {
457  case KNF_CONSTRAIN_AXIS_X: {
459  break;
460  }
461  case KNF_CONSTRAIN_AXIS_Y: {
463  break;
464  }
465  case KNF_CONSTRAIN_AXIS_Z: {
467  break;
468  }
469  default: {
471  break;
472  }
473  }
474 
475  GPU_line_width(2.0);
476 
478  immVertex3fv(pos, v1);
479  immVertex3fv(pos, v2);
480  immEnd();
481 
483 }
484 
486 {
488  GPU_matrix_push();
491 
494 
495  char numstr[256];
496  float numstr_size[2];
497  float posit[2];
498  const float bg_margin = 4.0f * U.dpi_fac;
499  const float font_size = 14.0f * U.pixelsize;
500  const int distance_precision = 4;
501 
502  /* Calculate distance and convert to string. */
503  const float cut_len = len_v3v3(kcd->prev.cage, kcd->curr.cage);
504 
505  UnitSettings *unit = &kcd->scene->unit;
506  if (unit->system == USER_UNIT_NONE) {
507  BLI_snprintf(numstr, sizeof(numstr), "%.*f", distance_precision, cut_len);
508  }
509  else {
511  sizeof(numstr),
512  (double)(cut_len * unit->scale_length),
513  distance_precision,
515  unit,
516  false);
517  }
518 
520  BLF_size(blf_mono_font, font_size, U.dpi);
522  BLF_width_and_height(blf_mono_font, numstr, sizeof(numstr), &numstr_size[0], &numstr_size[1]);
523 
524  /* Center text. */
525  mid_v2_v2v2(posit, kcd->prev.mval, kcd->curr.mval);
526  posit[0] -= numstr_size[0] / 2.0f;
527  posit[1] -= numstr_size[1] / 2.0f;
528 
529  /* Draw text background. */
530  float color_back[4] = {0.0f, 0.0f, 0.0f, 0.5f}; /* TODO: Replace with theme color. */
531  immUniformColor4fv(color_back);
532 
534  immRectf(pos,
535  posit[0] - bg_margin,
536  posit[1] - bg_margin,
537  posit[0] + bg_margin + numstr_size[0],
538  posit[1] + bg_margin + numstr_size[1]);
541 
542  /* Draw text. */
543  uchar color_text[3];
544  UI_GetThemeColor3ubv(TH_TEXT, color_text);
545 
546  BLF_color3ubv(blf_mono_font, color_text);
547  BLF_position(blf_mono_font, posit[0], posit[1], 0.0f);
548  BLF_draw(blf_mono_font, numstr, sizeof(numstr));
550 
551  GPU_matrix_pop();
553 }
554 
555 static void knifetool_draw_angle(const KnifeTool_OpData *kcd,
556  const float start[3],
557  const float mid[3],
558  const float end[3],
559  const float start_ss[2],
560  const float mid_ss[2],
561  const float end_ss[2],
562  const float angle)
563 {
564  const RegionView3D *rv3d = kcd->region->regiondata;
565  const int arc_steps = 24;
566  const float arc_size = 64.0f * U.dpi_fac;
567  const float bg_margin = 4.0f * U.dpi_fac;
568  const float cap_size = 4.0f * U.dpi_fac;
569  const float font_size = 14.0f * U.pixelsize;
570  const int angle_precision = 3;
571 
572  /* Angle arc in 3d space. */
574 
575  const uint pos_3d = GPU_vertformat_attr_add(
578 
579  {
580  float dir_tmp[3];
581  float ar_coord[3];
582 
583  float dir_a[3];
584  float dir_b[3];
585  float quat[4];
586  float axis[3];
587  float arc_angle;
588 
589  const float inverse_average_scale = 1 /
590  (kcd->curr.ob->obmat[0][0] + kcd->curr.ob->obmat[1][1] +
591  kcd->curr.ob->obmat[2][2]);
592 
593  const float px_scale =
594  3.0f * inverse_average_scale *
596  min_fff(arc_size, len_v2v2(start_ss, mid_ss) / 2.0f, len_v2v2(end_ss, mid_ss) / 2.0f));
597 
598  sub_v3_v3v3(dir_a, start, mid);
599  sub_v3_v3v3(dir_b, end, mid);
600  normalize_v3(dir_a);
601  normalize_v3(dir_b);
602 
603  cross_v3_v3v3(axis, dir_a, dir_b);
604  arc_angle = angle_normalized_v3v3(dir_a, dir_b);
605 
606  axis_angle_to_quat(quat, axis, arc_angle / arc_steps);
607 
608  copy_v3_v3(dir_tmp, dir_a);
609 
611  GPU_line_width(1.0);
612 
613  immBegin(GPU_PRIM_LINE_STRIP, arc_steps + 1);
614  for (int j = 0; j <= arc_steps; j++) {
615  madd_v3_v3v3fl(ar_coord, mid, dir_tmp, px_scale);
616  mul_qt_v3(quat, dir_tmp);
617 
618  immVertex3fv(pos_3d, ar_coord);
619  }
620  immEnd();
621  }
622 
624 
625  /* Angle text and background in 2d space. */
627  GPU_matrix_push();
630 
631  uint pos_2d = GPU_vertformat_attr_add(
634 
635  /* Angle as string. */
636  char numstr[256];
637  float numstr_size[2];
638  float posit[2];
639 
640  UnitSettings *unit = &kcd->scene->unit;
641  if (unit->system == USER_UNIT_NONE) {
642  BLI_snprintf(numstr, sizeof(numstr), "%.*f°", angle_precision, RAD2DEGF(angle));
643  }
644  else {
646  numstr, sizeof(numstr), (double)angle, angle_precision, B_UNIT_ROTATION, unit, false);
647  }
648 
650  BLF_size(blf_mono_font, font_size, U.dpi);
652  BLF_width_and_height(blf_mono_font, numstr, sizeof(numstr), &numstr_size[0], &numstr_size[1]);
653 
654  posit[0] = mid_ss[0] + (cap_size * 2.0f);
655  posit[1] = mid_ss[1] - (numstr_size[1] / 2.0f);
656 
657  /* Draw text background. */
658  float color_back[4] = {0.0f, 0.0f, 0.0f, 0.5f}; /* TODO: Replace with theme color. */
659  immUniformColor4fv(color_back);
660 
662  immRectf(pos_2d,
663  posit[0] - bg_margin,
664  posit[1] - bg_margin,
665  posit[0] + bg_margin + numstr_size[0],
666  posit[1] + bg_margin + numstr_size[1]);
669 
670  /* Draw text. */
671  uchar color_text[3];
672  UI_GetThemeColor3ubv(TH_TEXT, color_text);
673 
674  BLF_color3ubv(blf_mono_font, color_text);
675  BLF_position(blf_mono_font, posit[0], posit[1], 0.0f);
677  BLF_draw(blf_mono_font, numstr, sizeof(numstr));
679 
680  GPU_matrix_pop();
682 
684 }
685 
687 {
688  Ref *ref;
689  KnifeVert *kfv;
690  KnifeVert *tempkfv;
691  KnifeEdge *kfe;
692  KnifeEdge *tempkfe;
693 
694  if (kcd->curr.vert) {
695  kfv = kcd->curr.vert;
696 
697  float min_angle = FLT_MAX;
698  float angle = 0.0f;
699  float *end;
700 
701  kfe = ((Ref *)kfv->edges.first)->ref;
702  for (ref = kfv->edges.first; ref; ref = ref->next) {
703  tempkfe = ref->ref;
704  if (tempkfe->v1 != kfv) {
705  tempkfv = tempkfe->v1;
706  }
707  else {
708  tempkfv = tempkfe->v2;
709  }
710  angle = angle_v3v3v3(kcd->prev.cage, kcd->curr.cage, tempkfv->cageco);
711  if (angle < min_angle) {
712  min_angle = angle;
713  kfe = tempkfe;
714  end = tempkfv->cageco;
715  }
716  }
717 
718  if (min_angle > KNIFE_FLT_EPSBIG) {
719  /* Last vertex in screen space. */
720  float end_ss[2];
722 
724  kcd->prev.cage,
725  kcd->curr.cage,
726  end,
727  kcd->prev.mval,
728  kcd->curr.mval,
729  end_ss,
730  min_angle);
731  }
732  }
733  else if (kcd->curr.edge) {
734  kfe = kcd->curr.edge;
735 
736  /* Check for most recent cut (if cage is part of previous cut). */
737  if (!compare_v3v3(kfe->v1->cageco, kcd->prev.cage, KNIFE_FLT_EPSBIG) &&
738  !compare_v3v3(kfe->v2->cageco, kcd->prev.cage, KNIFE_FLT_EPSBIG)) {
739  /* Determine acute angle. */
740  float angle1 = angle_v3v3v3(kcd->prev.cage, kcd->curr.cage, kfe->v1->cageco);
741  float angle2 = angle_v3v3v3(kcd->prev.cage, kcd->curr.cage, kfe->v2->cageco);
742 
743  float angle;
744  float *end;
745  if (angle1 < angle2) {
746  angle = angle1;
747  end = kfe->v1->cageco;
748  }
749  else {
750  angle = angle2;
751  end = kfe->v2->cageco;
752  }
753 
754  /* Last vertex in screen space. */
755  float end_ss[2];
757 
759  kcd, kcd->prev.cage, kcd->curr.cage, end, kcd->prev.mval, kcd->curr.mval, end_ss, angle);
760  }
761  }
762 
763  if (kcd->prev.vert) {
764  kfv = kcd->prev.vert;
765  float min_angle = FLT_MAX;
766  float angle = 0.0f;
767  float *end;
768 
769  /* If using relative angle snapping, always draw angle to reference edge. */
771  kfe = kcd->snap_ref_edge;
772  if (kfe->v1 != kfv) {
773  tempkfv = kfe->v1;
774  }
775  else {
776  tempkfv = kfe->v2;
777  }
778  min_angle = angle_v3v3v3(kcd->curr.cage, kcd->prev.cage, tempkfv->cageco);
779  end = tempkfv->cageco;
780  }
781  else {
782  /* Choose minimum angle edge. */
783  kfe = ((Ref *)kfv->edges.first)->ref;
784  for (ref = kfv->edges.first; ref; ref = ref->next) {
785  tempkfe = ref->ref;
786  if (tempkfe->v1 != kfv) {
787  tempkfv = tempkfe->v1;
788  }
789  else {
790  tempkfv = tempkfe->v2;
791  }
792  angle = angle_v3v3v3(kcd->curr.cage, kcd->prev.cage, tempkfv->cageco);
793  if (angle < min_angle) {
794  min_angle = angle;
795  kfe = tempkfe;
796  end = tempkfv->cageco;
797  }
798  }
799  }
800 
801  if (min_angle > KNIFE_FLT_EPSBIG) {
802  /* Last vertex in screen space. */
803  float end_ss[2];
805 
807  kcd->curr.cage,
808  kcd->prev.cage,
809  end,
810  kcd->curr.mval,
811  kcd->prev.mval,
812  end_ss,
813  min_angle);
814  }
815  }
816  else if (kcd->prev.edge) {
817  /* Determine acute angle. */
818  kfe = kcd->prev.edge;
819  float angle1 = angle_v3v3v3(kcd->curr.cage, kcd->prev.cage, kfe->v1->cageco);
820  float angle2 = angle_v3v3v3(kcd->curr.cage, kcd->prev.cage, kfe->v2->cageco);
821 
822  float angle;
823  float *end;
824  /* kcd->prev.edge can have one vertex part of cut and one part of mesh? */
825  /* This never seems to happen for kcd->curr.edge. */
826  if ((!kcd->prev.vert || kcd->prev.vert->v == kfe->v1->v) || kfe->v1->is_cut) {
827  angle = angle2;
828  end = kfe->v2->cageco;
829  }
830  else if ((!kcd->prev.vert || kcd->prev.vert->v == kfe->v2->v) || kfe->v2->is_cut) {
831  angle = angle1;
832  end = kfe->v1->cageco;
833  }
834  else {
835  if (angle1 < angle2) {
836  angle = angle1;
837  end = kfe->v1->cageco;
838  }
839  else {
840  angle = angle2;
841  end = kfe->v2->cageco;
842  }
843  }
844 
845  /* Last vertex in screen space. */
846  float end_ss[2];
848 
850  kcd, kcd->curr.cage, kcd->prev.cage, end, kcd->curr.mval, kcd->prev.mval, end_ss, angle);
851  }
852  else if (kcd->mdata.is_stored && !kcd->prev.is_space) {
853  float angle = angle_v3v3v3(kcd->curr.cage, kcd->prev.cage, kcd->mdata.cage);
855  kcd->curr.cage,
856  kcd->prev.cage,
857  kcd->mdata.cage,
858  kcd->curr.mval,
859  kcd->prev.mval,
860  kcd->mdata.mval,
861  angle);
862  }
863 }
864 
866 {
867  switch (kcd->dist_angle_mode) {
868  case KNF_MEASUREMENT_BOTH: {
871  break;
872  }
875  break;
876  }
877  case KNF_MEASUREMENT_ANGLE: {
879  break;
880  }
881  }
882 }
883 
884 /* Modal loop selection drawing callback. */
885 static void knifetool_draw(const bContext *UNUSED(C), ARegion *UNUSED(region), void *arg)
886 {
887  const KnifeTool_OpData *kcd = arg;
889 
891  GPU_polygon_offset(1.0f, 1.0f);
892 
895 
897 
898  if (kcd->mode == MODE_DRAGGING) {
900  GPU_line_width(2.0);
901 
903  immVertex3fv(pos, kcd->prev.cage);
904  immVertex3fv(pos, kcd->curr.cage);
905  immEnd();
906  }
907 
908  if (kcd->prev.vert) {
911 
913  immVertex3fv(pos, kcd->prev.cage);
914  immEnd();
915  }
916 
917  if (kcd->prev.bmface || kcd->prev.edge) {
920 
922  immVertex3fv(pos, kcd->prev.cage);
923  immEnd();
924  }
925 
926  if (kcd->curr.vert) {
929 
931  immVertex3fv(pos, kcd->curr.cage);
932  immEnd();
933  }
934  else if (kcd->curr.edge) {
936  GPU_line_width(2.0);
937 
939  immVertex3fv(pos, kcd->curr.edge->v1->cageco);
940  immVertex3fv(pos, kcd->curr.edge->v2->cageco);
941  immEnd();
942  }
943 
944  if (kcd->curr.bmface || kcd->curr.edge) {
947 
949  immVertex3fv(pos, kcd->curr.cage);
950  immEnd();
951  }
952 
953  if (kcd->depth_test) {
955  }
956 
957  if (kcd->totkedge > 0) {
958  BLI_mempool_iter iter;
959  KnifeEdge *kfe;
960 
962  GPU_line_width(1.0);
963 
965 
966  BLI_mempool_iternew(kcd->kedges, &iter);
967  for (kfe = BLI_mempool_iterstep(&iter); kfe; kfe = BLI_mempool_iterstep(&iter)) {
968  if (!kfe->is_cut || kfe->is_invalid) {
969  continue;
970  }
971 
972  immVertex3fv(pos, kfe->v1->cageco);
973  immVertex3fv(pos, kfe->v2->cageco);
974  }
975 
976  immEnd();
977 
980  }
981 
982  if (kcd->totkvert > 0) {
983  BLI_mempool_iter iter;
984  KnifeVert *kfv;
985 
987  GPU_point_size(5.0 * UI_DPI_FAC);
988 
990 
991  BLI_mempool_iternew(kcd->kverts, &iter);
992  for (kfv = BLI_mempool_iterstep(&iter); kfv; kfv = BLI_mempool_iterstep(&iter)) {
993  if (!kfv->is_cut || kfv->is_invalid) {
994  continue;
995  }
996 
997  immVertex3fv(pos, kfv->cageco);
998  }
999 
1000  immEnd();
1001 
1004  }
1005 
1006  /* Draw relative angle snapping reference edge. */
1009  GPU_line_width(2.0);
1010 
1014  immEnd();
1015  }
1016 
1017  if (kcd->totlinehit > 0) {
1018  KnifeLineHit *lh;
1019  int i, snapped_verts_count, other_verts_count;
1020  float fcol[4];
1021 
1023 
1025  GPU_vertbuf_data_alloc(vert, kcd->totlinehit);
1026 
1027  lh = kcd->linehits;
1028  for (i = 0, snapped_verts_count = 0, other_verts_count = 0; i < kcd->totlinehit; i++, lh++) {
1029  if (lh->v) {
1030  GPU_vertbuf_attr_set(vert, pos, snapped_verts_count++, lh->cagehit);
1031  }
1032  else {
1033  GPU_vertbuf_attr_set(vert, pos, kcd->totlinehit - 1 - other_verts_count++, lh->cagehit);
1034  }
1035  }
1036 
1039 
1040  /* Draw any snapped verts first. */
1041  rgba_uchar_to_float(fcol, kcd->colors.point_a);
1042  GPU_batch_uniform_4fv(batch, "color", fcol);
1043  GPU_point_size(11 * UI_DPI_FAC);
1044  if (snapped_verts_count > 0) {
1045  GPU_batch_draw_range(batch, 0, snapped_verts_count);
1046  }
1047 
1048  /* Now draw the rest. */
1050  GPU_batch_uniform_4fv(batch, "color", fcol);
1052  if (other_verts_count > 0) {
1053  GPU_batch_draw_range(batch, snapped_verts_count, other_verts_count);
1054  }
1055 
1057 
1059  }
1060 
1061  immUnbindProgram();
1062 
1064 
1065  if (kcd->mode == MODE_DRAGGING) {
1066  if (kcd->is_angle_snapping) {
1068  }
1069  else if (kcd->axis_constrained) {
1071  }
1072 
1073  if (kcd->show_dist_angle) {
1075  }
1076  }
1077 
1079 
1080  /* Reset default. */
1082 }
1083 
1086 /* -------------------------------------------------------------------- */
1091 {
1092  char header[UI_MAX_DRAW_STR];
1093  char buf[UI_MAX_DRAW_STR];
1094 
1095  char *p = buf;
1096  int available_len = sizeof(buf);
1097 
1098 #define WM_MODALKEY(_id) \
1099  WM_modalkeymap_operator_items_to_string_buf( \
1100  op->type, (_id), true, UI_MAX_SHORTCUT_STR, &available_len, &p)
1101 
1102  BLI_snprintf(
1103  header,
1104  sizeof(header),
1105  TIP_("%s: confirm, %s: cancel, %s: undo, "
1106  "%s: start/define cut, %s: close cut, %s: new cut, "
1107  "%s: midpoint snap (%s), %s: ignore snap (%s), "
1108  "%s: angle constraint %.2f(%.2f) (%s%s%s%s), %s: cut through (%s), "
1109  "%s: panning, %s%s%s: orientation lock (%s), "
1110  "%s: distance/angle measurements (%s), "
1111  "%s: x-ray (%s)"),
1123  (kcd->angle >= 0.0f) ? RAD2DEGF(kcd->angle) : 360.0f + RAD2DEGF(kcd->angle),
1128  kcd->angle_snapping ?
1129  ((kcd->angle_snapping_mode == KNF_CONSTRAIN_ANGLE_MODE_SCREEN) ? "Screen" : "Relative") :
1130  "OFF",
1131  /* TODO: Can this be simplified? */
1135  "",
1136  (kcd->angle_snapping_mode == KNF_CONSTRAIN_ANGLE_MODE_RELATIVE) ? ": cycle edge" : "",
1137 
1148  WM_bool_as_string(!kcd->depth_test));
1149 
1150 #undef WM_MODALKEY
1151 
1152  ED_workspace_status_text(C, header);
1153 }
1154 
1157 /* -------------------------------------------------------------------- */
1161 static const int *knife_bm_tri_index_get(const KnifeTool_OpData *kcd,
1162  int base_index,
1163  int tri_index,
1164  int tri_index_buf[3])
1165 {
1166  const KnifeObjectInfo *obinfo = &kcd->objects_info[base_index];
1167  if (obinfo->tri_indices) {
1168  return obinfo->tri_indices[tri_index];
1169  }
1170  for (int i = 0; i < 3; i++) {
1171  tri_index_buf[i] = BM_elem_index_get(obinfo->em->looptris[tri_index][i]->v);
1172  }
1173  return tri_index_buf;
1174 }
1175 
1177  int base_index,
1178  int tri_index,
1179  float cos[3][3])
1180 {
1181  const KnifeObjectInfo *obinfo = &kcd->objects_info[base_index];
1182  int tri_ind_buf[3];
1183  const int *tri_ind = knife_bm_tri_index_get(kcd, base_index, tri_index, tri_ind_buf);
1184  for (int i = 0; i < 3; i++) {
1185  copy_v3_v3(cos[i], obinfo->cagecos[tri_ind[i]]);
1186  }
1187 }
1188 
1190  int base_index,
1191  int tri_index,
1192  float cos[3][3])
1193 {
1194  knife_bm_tri_cagecos_get(kcd, base_index, tri_index, cos);
1195  const Object *ob = kcd->objects[base_index];
1196  for (int i = 0; i < 3; i++) {
1197  mul_m4_v3(ob->obmat, cos[i]);
1198  }
1199 }
1200 
1203 /* -------------------------------------------------------------------- */
1208 {
1209  return (BM_elem_flag_test(f, BM_ELEM_SELECT) != 0);
1210 }
1211 
1213 {
1214  return (BM_elem_flag_test(f, BM_ELEM_HIDDEN) == 0);
1215 }
1216 
1218 {
1219  Object *ob;
1220  BMEditMesh *em;
1221 
1222  /* Test Function. */
1223  bool (*test_fn)(BMFace *);
1224  if ((kcd->only_select && kcd->cut_through)) {
1225  test_fn = knife_bm_face_is_select;
1226  }
1227  else {
1228  test_fn = knife_bm_face_is_not_hidden;
1229  }
1230 
1231  /* Construct BVH Tree. */
1232  const float epsilon = FLT_EPSILON * 2.0f;
1233  int tottri = 0;
1234  int ob_tottri = 0;
1235  BMLoop *(*looptris)[3];
1236  BMFace *f_test = NULL, *f_test_prev = NULL;
1237  bool test_fn_ret = false;
1238 
1239  /* Calculate tottri. */
1240  for (uint b = 0; b < kcd->objects_len; b++) {
1241  ob_tottri = 0;
1242  ob = kcd->objects[b];
1243  em = BKE_editmesh_from_object(ob);
1244 
1245  for (int i = 0; i < em->tottri; i++) {
1246  f_test = em->looptris[i][0]->f;
1247  if (f_test != f_test_prev) {
1248  test_fn_ret = test_fn(f_test);
1249  f_test_prev = f_test;
1250  }
1251 
1252  if (test_fn_ret) {
1253  ob_tottri++;
1254  }
1255  }
1256 
1257  tottri += ob_tottri;
1258  }
1259 
1260  kcd->bvh.tree = BLI_bvhtree_new(tottri, epsilon, 8, 8);
1261 
1262  f_test_prev = NULL;
1263  test_fn_ret = false;
1264 
1265  /* Add tri's for each object.
1266  * TODO:
1267  * test_fn can leave large gaps between bvh tree indices.
1268  * Compacting bvh tree indices may be possible.
1269  * Don't forget to update #knife_bvh_intersect_plane!
1270  */
1271  tottri = 0;
1272  for (uint b = 0; b < kcd->objects_len; b++) {
1273  ob = kcd->objects[b];
1274  em = BKE_editmesh_from_object(ob);
1275  looptris = em->looptris;
1276 
1277  for (int i = 0; i < em->tottri; i++) {
1278 
1279  f_test = looptris[i][0]->f;
1280  if (f_test != f_test_prev) {
1281  test_fn_ret = test_fn(f_test);
1282  f_test_prev = f_test;
1283  }
1284 
1285  if (!test_fn_ret) {
1286  continue;
1287  }
1288 
1289  float tri_cos[3][3];
1290  knife_bm_tri_cagecos_get_worldspace(kcd, b, i, tri_cos);
1291  BLI_bvhtree_insert(kcd->bvh.tree, i + tottri, &tri_cos[0][0], 3);
1292  }
1293 
1294  tottri += em->tottri;
1295  }
1296 
1298 }
1299 
1300 /* Wrapper for #BLI_bvhtree_free. */
1302 {
1303  if (kcd->bvh.tree) {
1304  BLI_bvhtree_free(kcd->bvh.tree);
1305  kcd->bvh.tree = NULL;
1306  }
1307 }
1308 
1309 static void knife_bvh_raycast_cb(void *userdata,
1310  int index,
1311  const BVHTreeRay *ray,
1312  BVHTreeRayHit *hit)
1313 {
1314  if (index == -1) {
1315  return;
1316  }
1317 
1318  KnifeTool_OpData *kcd = userdata;
1319  BMLoop **ltri;
1320  Object *ob;
1321  BMEditMesh *em;
1322 
1323  float dist, uv[2];
1324  bool isect;
1325  int tottri;
1326 
1327  tottri = 0;
1328  uint b = 0;
1329  for (; b < kcd->objects_len; b++) {
1330  index -= tottri;
1331  ob = kcd->objects[b];
1332  em = BKE_editmesh_from_object(ob);
1333  tottri = em->tottri;
1334  if (index < tottri) {
1335  ltri = em->looptris[index];
1336  break;
1337  }
1338  }
1339 
1340  if (kcd->bvh.filter_cb) {
1341  if (!kcd->bvh.filter_cb(ltri[0]->f, kcd->bvh.filter_data)) {
1342  return;
1343  }
1344  }
1345 
1346  float tri_cos[3][3];
1347  knife_bm_tri_cagecos_get_worldspace(kcd, b, index, tri_cos);
1348  isect = (ray->radius > 0.0f ?
1350  ray->origin, ray->direction, UNPACK3(tri_cos), &dist, uv, ray->radius) :
1351 #ifdef USE_KDOPBVH_WATERTIGHT
1353  ray->origin, ray->isect_precalc, UNPACK3(tri_cos), &dist, uv));
1354 #else
1355  isect_ray_tri_v3(ray->origin, ray->direction, UNPACK3(tri_cos), &dist, uv);
1356 #endif
1357 
1358  if (isect && dist < hit->dist) {
1359  madd_v3_v3v3fl(hit->co, ray->origin, ray->direction, dist);
1360 
1361  /* Discard clipped points. */
1362  if (RV3D_CLIPPING_ENABLED(kcd->vc.v3d, kcd->vc.rv3d) &&
1363  ED_view3d_clipping_test(kcd->vc.rv3d, hit->co, false)) {
1364  return;
1365  }
1366 
1367  hit->dist = dist;
1368  hit->index = index;
1369 
1370  copy_v3_v3(hit->no, ltri[0]->f->no);
1371 
1372  kcd->bvh.looptris = em->looptris;
1373  copy_v2_v2(kcd->bvh.uv, uv);
1374  kcd->bvh.base_index = b;
1375  }
1376 }
1377 
1378 /* `co` is expected to be in world space. */
1380  const float co[3],
1381  const float dir[3],
1382  const float radius,
1383  float *r_dist,
1384  float r_hitout[3],
1385  float r_cagehit[3],
1386  uint *r_base_index)
1387 {
1388  BMFace *face;
1389  BVHTreeRayHit hit;
1390  const float dist = r_dist ? *r_dist : FLT_MAX;
1391  hit.dist = dist;
1392  hit.index = -1;
1393 
1394  BLI_bvhtree_ray_cast(kcd->bvh.tree, co, dir, radius, &hit, knife_bvh_raycast_cb, kcd);
1395 
1396  /* Handle Hit */
1397  if (hit.index != -1 && hit.dist != dist) {
1398  face = kcd->bvh.looptris[hit.index][0]->f;
1399 
1400  /* Hits returned in world space. */
1401  if (r_hitout) {
1402  float tri_cos[3][3];
1403  knife_bm_tri_cagecos_get_worldspace(kcd, kcd->bvh.base_index, hit.index, tri_cos);
1404  interp_v3_v3v3v3_uv(r_hitout, UNPACK3(tri_cos), kcd->bvh.uv);
1405 
1406  if (r_cagehit) {
1407  copy_v3_v3(r_cagehit, hit.co);
1408  }
1409  }
1410 
1411  if (r_dist) {
1412  *r_dist = hit.dist;
1413  }
1414 
1415  if (r_base_index) {
1416  *r_base_index = kcd->bvh.base_index;
1417  }
1418 
1419  return face;
1420  }
1421  return NULL;
1422 }
1423 
1424 /* `co` is expected to be in world space. */
1426  const float co[3],
1427  const float dir[3],
1428  const float radius,
1429  float *r_dist,
1430  float r_hitout[3],
1431  float r_cagehit[3],
1432  uint *r_base_index,
1433  bool (*filter_cb)(BMFace *f, void *userdata),
1434  void *filter_userdata)
1435 {
1436  kcd->bvh.filter_cb = filter_cb;
1437  kcd->bvh.filter_data = filter_userdata;
1438 
1439  BMFace *face;
1440  BVHTreeRayHit hit;
1441  const float dist = r_dist ? *r_dist : FLT_MAX;
1442  hit.dist = dist;
1443  hit.index = -1;
1444 
1445  BLI_bvhtree_ray_cast(kcd->bvh.tree, co, dir, radius, &hit, knife_bvh_raycast_cb, kcd);
1446 
1447  kcd->bvh.filter_cb = NULL;
1448  kcd->bvh.filter_data = NULL;
1449 
1450  /* Handle Hit */
1451  if (hit.index != -1 && hit.dist != dist) {
1452  face = kcd->bvh.looptris[hit.index][0]->f;
1453 
1454  /* Hits returned in world space. */
1455  if (r_hitout) {
1456  float tri_cos[3][3];
1457  knife_bm_tri_cagecos_get_worldspace(kcd, kcd->bvh.base_index, hit.index, tri_cos);
1458  interp_v3_v3v3v3_uv(r_hitout, UNPACK3(tri_cos), kcd->bvh.uv);
1459 
1460  if (r_cagehit) {
1461  copy_v3_v3(r_cagehit, hit.co);
1462  }
1463  }
1464 
1465  if (r_dist) {
1466  *r_dist = hit.dist;
1467  }
1468 
1469  if (r_base_index) {
1470  *r_base_index = kcd->bvh.base_index;
1471  }
1472 
1473  return face;
1474  }
1475  return NULL;
1476 }
1477 
1480 /* -------------------------------------------------------------------- */
1484 static void knife_project_v2(const KnifeTool_OpData *kcd, const float co[3], float sco[2])
1485 {
1487 }
1488 
1489 /* Ray is returned in world space. */
1491  const float mval[2],
1492  const float ofs,
1493  float r_origin[3],
1494  float r_origin_ofs[3])
1495 {
1496  /* Unproject to find view ray. */
1497  ED_view3d_unproject_v3(kcd->vc.region, mval[0], mval[1], 0.0f, r_origin);
1498  ED_view3d_unproject_v3(kcd->vc.region, mval[0], mval[1], ofs, r_origin_ofs);
1499 }
1500 
1501 /* No longer used, but may be useful in the future. */
1503  float mval[3],
1504  float r_cage[3])
1505 {
1506  float origin[3];
1507  float origin_ofs[3];
1508  float ray[3], ray_normal[3];
1509  float co[3]; /* Unused. */
1510 
1511  knife_input_ray_segment(kcd, mval, 1.0f, origin, origin_ofs);
1512 
1513  sub_v3_v3v3(ray, origin_ofs, origin);
1514  normalize_v3_v3(ray_normal, ray);
1515 
1516  knife_bvh_raycast(kcd, origin, ray_normal, 0.0f, NULL, co, r_cage, NULL);
1517 }
1518 
1520 {
1521  bool v1_inside, v2_inside;
1522  bool v1_inface, v2_inface;
1523  BMLoop *l1, *l2;
1524 
1525  if (!f || !v1 || !v2) {
1526  return false;
1527  }
1528 
1529  l1 = v1->v ? BM_face_vert_share_loop(f, v1->v) : NULL;
1530  l2 = v2->v ? BM_face_vert_share_loop(f, v2->v) : NULL;
1531 
1532  if ((l1 && l2) && BM_loop_is_adjacent(l1, l2)) {
1533  /* Boundary-case, always false to avoid edge-in-face checks below. */
1534  return false;
1535  }
1536 
1537  /* Find out if v1 and v2, if set, are part of the face. */
1538  v1_inface = (l1 != NULL);
1539  v2_inface = (l2 != NULL);
1540 
1541  /* BM_face_point_inside_test uses best-axis projection so this isn't most accurate test... */
1542  v1_inside = v1_inface ? false : BM_face_point_inside_test(f, v1->co);
1543  v2_inside = v2_inface ? false : BM_face_point_inside_test(f, v2->co);
1544  if ((v1_inface && v2_inside) || (v2_inface && v1_inside) || (v1_inside && v2_inside)) {
1545  return true;
1546  }
1547 
1548  if (v1_inface && v2_inface) {
1549  float mid[3];
1550  /* Can have case where v1 and v2 are on shared chain between two faces.
1551  * BM_face_splits_check_legal does visibility and self-intersection tests,
1552  * but it is expensive and maybe a bit buggy, so use a simple
1553  * "is the midpoint in the face" test. */
1554  mid_v3_v3v3(mid, v1->co, v2->co);
1555  return BM_face_point_inside_test(f, mid);
1556  }
1557  return false;
1558 }
1559 
1561 {
1563  kcd->vc.depsgraph, kcd->vc.v3d, kcd->vc.rv3d, &kcd->clipsta, &kcd->clipend, true);
1564 }
1565 
1568 /* -------------------------------------------------------------------- */
1575 {
1576  BMElem *ele_test;
1577  KnifeEdge *kfe = NULL;
1578 
1579  /* vert? */
1580  ele_test = (BMElem *)kfv->v;
1581 
1582  if (r_kfe || ele_test == NULL) {
1583  if (kfv->v == NULL) {
1584  Ref *ref;
1585  for (ref = kfv->edges.first; ref; ref = ref->next) {
1586  kfe = ref->ref;
1587  if (kfe->e) {
1588  if (r_kfe) {
1589  *r_kfe = kfe;
1590  }
1591  break;
1592  }
1593  }
1594  }
1595  }
1596 
1597  /* edge? */
1598  if (ele_test == NULL) {
1599  if (kfe) {
1600  ele_test = (BMElem *)kfe->e;
1601  }
1602  }
1603 
1604  /* face? */
1605  if (ele_test == NULL) {
1606  if (BLI_listbase_is_single(&kfe->faces)) {
1607  ele_test = ((Ref *)kfe->faces.first)->ref;
1608  }
1609  }
1610 
1611  return ele_test;
1612 }
1613 
1615 {
1616  BMElem *ele_test;
1617 
1618  ele_test = (BMElem *)kfe->e;
1619 
1620  if (ele_test == NULL) {
1621  ele_test = (BMElem *)kfe->basef;
1622  }
1623 
1624  return ele_test;
1625 }
1626 
1629 /* -------------------------------------------------------------------- */
1634 {
1635  ListBase *list;
1636 
1637  list = BLI_memarena_alloc(kcd->arena, sizeof(ListBase));
1638  BLI_listbase_clear(list);
1639  return list;
1640 }
1641 
1642 static void knife_append_list(KnifeTool_OpData *kcd, ListBase *lst, void *elem)
1643 {
1644  Ref *ref;
1645 
1646  ref = BLI_mempool_calloc(kcd->refs);
1647  ref->ref = elem;
1648  BLI_addtail(lst, ref);
1649 }
1650 
1651 static Ref *find_ref(ListBase *lb, void *ref)
1652 {
1653  Ref *ref1;
1654 
1655  for (ref1 = lb->first; ref1; ref1 = ref1->next) {
1656  if (ref1->ref == ref) {
1657  return ref1;
1658  }
1659  }
1660 
1661  return NULL;
1662 }
1663 
1664 static void knife_append_list_no_dup(KnifeTool_OpData *kcd, ListBase *lst, void *elem)
1665 {
1666  if (!find_ref(lst, elem)) {
1667  knife_append_list(kcd, lst, elem);
1668  }
1669 }
1670 
1672 {
1673  knife_append_list(kcd, &kfe->v1->edges, kfe);
1674  knife_append_list(kcd, &kfe->v2->edges, kfe);
1675 }
1676 
1677 /* Add faces of an edge to a KnifeVert's faces list. No checks for dups. */
1679 {
1680  BMIter bmiter;
1681  BMFace *f;
1682 
1683  BM_ITER_ELEM (f, &bmiter, e, BM_FACES_OF_EDGE) {
1684  knife_append_list(kcd, &kfv->faces, f);
1685  }
1686 }
1687 
1688 /* Find a face in common in the two faces lists.
1689  * If more than one, return the first; if none, return NULL. */
1691 {
1692  Ref *ref1, *ref2;
1693 
1694  for (ref1 = faces1->first; ref1; ref1 = ref1->next) {
1695  for (ref2 = faces2->first; ref2; ref2 = ref2->next) {
1696  if (ref1->ref == ref2->ref) {
1697  return (BMFace *)(ref1->ref);
1698  }
1699  }
1700  }
1701  return NULL;
1702 }
1703 
1706 /* -------------------------------------------------------------------- */
1710 static KnifeVert *new_knife_vert(KnifeTool_OpData *kcd, const float co[3], const float cageco[3])
1711 {
1712  KnifeVert *kfv = BLI_mempool_calloc(kcd->kverts);
1713 
1714  kcd->totkvert++;
1715 
1716  copy_v3_v3(kfv->co, co);
1717  copy_v3_v3(kfv->cageco, cageco);
1718 
1719  return kfv;
1720 }
1721 
1723 {
1724  KnifeEdge *kfe = BLI_mempool_calloc(kcd->kedges);
1725  kcd->totkedge++;
1726  return kfe;
1727 }
1728 
1729 /* Get a KnifeVert wrapper for an existing BMVert. */
1731 {
1732  KnifeVert *kfv = BLI_ghash_lookup(kcd->origvertmap, v);
1733  const float *cageco;
1734 
1735  if (!kfv) {
1736  BMIter bmiter;
1737  BMFace *f;
1738 
1739  if (BM_elem_index_get(v) >= 0) {
1740  cageco = kcd->objects_info[base_index].cagecos[BM_elem_index_get(v)];
1741  }
1742  else {
1743  cageco = v->co;
1744  }
1745 
1746  float cageco_ws[3];
1747  mul_v3_m4v3(cageco_ws, ob->obmat, cageco);
1748 
1749  kfv = new_knife_vert(kcd, v->co, cageco_ws);
1750  kfv->v = v;
1751  kfv->ob = ob;
1752  kfv->base_index = base_index;
1753 
1754  BLI_ghash_insert(kcd->origvertmap, v, kfv);
1755  BM_ITER_ELEM (f, &bmiter, v, BM_FACES_OF_VERT) {
1756  knife_append_list(kcd, &kfv->faces, f);
1757  }
1758  }
1759 
1760  return kfv;
1761 }
1762 
1763 /* Get a KnifeEdge wrapper for an existing BMEdge. */
1765 {
1766  KnifeEdge *kfe = BLI_ghash_lookup(kcd->origedgemap, e);
1767  if (!kfe) {
1768  BMIter bmiter;
1769  BMFace *f;
1770 
1771  kfe = new_knife_edge(kcd);
1772  kfe->e = e;
1773  kfe->v1 = get_bm_knife_vert(kcd, e->v1, ob, base_index);
1774  kfe->v2 = get_bm_knife_vert(kcd, e->v2, ob, base_index);
1775 
1776  knife_add_to_vert_edges(kcd, kfe);
1777 
1778  BLI_ghash_insert(kcd->origedgemap, e, kfe);
1779 
1780  BM_ITER_ELEM (f, &bmiter, e, BM_FACES_OF_EDGE) {
1781  knife_append_list(kcd, &kfe->faces, f);
1782  }
1783  }
1784 
1785  return kfe;
1786 }
1787 
1789  Object *ob,
1790  uint base_index,
1791  BMFace *f)
1792 {
1793  ListBase *list = BLI_ghash_lookup(kcd->kedgefacemap, f);
1794 
1795  if (!list) {
1796  BMIter bmiter;
1797  BMEdge *e;
1798 
1799  list = knife_empty_list(kcd);
1800 
1801  BM_ITER_ELEM (e, &bmiter, f, BM_EDGES_OF_FACE) {
1802  knife_append_list(kcd, list, get_bm_knife_edge(kcd, e, ob, base_index));
1803  }
1804 
1805  BLI_ghash_insert(kcd->kedgefacemap, f, list);
1806  }
1807 
1808  return list;
1809 }
1810 
1812 {
1813  knife_append_list(kcd, knife_get_face_kedges(kcd, kfe->v1->ob, kfe->v1->base_index, f), kfe);
1814  knife_append_list(kcd, &kfe->faces, f);
1815 }
1816 
1818  KnifeEdge *kfe,
1819  const float co[3],
1820  const float cageco[3],
1821  KnifeEdge **r_kfe)
1822 {
1823  KnifeEdge *newkfe = new_knife_edge(kcd);
1824  Ref *ref;
1825  BMFace *f;
1826 
1827  newkfe->v1 = kfe->v1;
1828  newkfe->v2 = new_knife_vert(kcd, co, cageco);
1829  newkfe->v2->ob = kfe->v1->ob;
1830  newkfe->v2->base_index = kfe->v1->base_index;
1831  newkfe->v2->is_cut = true;
1832  if (kfe->e) {
1833  knife_add_edge_faces_to_vert(kcd, newkfe->v2, kfe->e);
1834  }
1835  else {
1836  /* kfe cuts across an existing face.
1837  * If v1 and v2 are in multiple faces together (e.g., if they
1838  * are in doubled polys) then this arbitrarily chooses one of them. */
1839  f = knife_find_common_face(&kfe->v1->faces, &kfe->v2->faces);
1840  if (f) {
1841  knife_append_list(kcd, &newkfe->v2->faces, f);
1842  }
1843  }
1844  newkfe->basef = kfe->basef;
1845 
1846  ref = find_ref(&kfe->v1->edges, kfe);
1847  BLI_remlink(&kfe->v1->edges, ref);
1848 
1849  kfe->v1 = newkfe->v2;
1850  kfe->v1->is_splitting = true;
1851  BLI_addtail(&kfe->v1->edges, ref);
1852 
1853  for (ref = kfe->faces.first; ref; ref = ref->next) {
1854  knife_edge_append_face(kcd, newkfe, ref->ref);
1855  }
1856 
1857  knife_add_to_vert_edges(kcd, newkfe);
1858 
1859  newkfe->is_cut = kfe->is_cut;
1860  newkfe->e = kfe->e;
1861 
1862  newkfe->splits++;
1863  kfe->splits++;
1864 
1865  kcd->undo->splits++;
1866 
1867  BLI_stack_push(kcd->splitstack, (void *)&kfe);
1868  BLI_stack_push(kcd->splitstack, (void *)&newkfe);
1869 
1870  *r_kfe = newkfe;
1871 
1872  return newkfe->v2;
1873 }
1874 
1875 /* Rejoin two edges split by #knife_split_edge. */
1876 static void knife_join_edge(KnifeEdge *newkfe, KnifeEdge *kfe)
1877 {
1878  newkfe->is_invalid = true;
1879  newkfe->v2->is_invalid = true;
1880 
1881  kfe->v1 = newkfe->v1;
1882 
1883  kfe->splits--;
1884  kfe->v1->is_splitting = false;
1885  kfe->v2->is_splitting = false;
1886 }
1887 
1890 /* -------------------------------------------------------------------- */
1894 /* User has just clicked for first time or first time after a restart (E key).
1895  * Copy the current position data into prev. */
1897 {
1898  kcd->prev = kcd->curr;
1899  kcd->curr.is_space = 0; /* TODO: Why do we do this? */
1900  kcd->mdata.is_stored = false;
1901 
1902  if (kcd->prev.vert == NULL && kcd->prev.edge == NULL) {
1903  float origin[3], origin_ofs[3];
1904  float ofs_local[3];
1905 
1906  negate_v3_v3(ofs_local, kcd->vc.rv3d->ofs);
1907 
1908  knife_input_ray_segment(kcd, kcd->curr.mval, 1.0f, origin, origin_ofs);
1909 
1910  if (!isect_line_plane_v3(
1911  kcd->prev.cage, origin, origin_ofs, ofs_local, kcd->vc.rv3d->viewinv[2])) {
1912  zero_v3(kcd->prev.cage);
1913  }
1914 
1915  copy_v3_v3(kcd->prev.co, kcd->prev.cage); /* TODO: do we need this? */
1916  copy_v3_v3(kcd->curr.cage, kcd->prev.cage);
1917  copy_v3_v3(kcd->curr.co, kcd->prev.co);
1918  }
1919 }
1920 
1922 {
1923  kpos->bmface = lh->f;
1924  kpos->vert = lh->v;
1925  kpos->edge = lh->kfe;
1926  copy_v3_v3(kpos->cage, lh->cagehit);
1927  copy_v3_v3(kpos->co, lh->hit);
1928  copy_v2_v2(kpos->mval, lh->schit);
1929 }
1930 
1931 /* Primary key: lambda along cut
1932  * Secondary key: lambda along depth
1933  * Tertiary key: pointer comparisons of verts if both snapped to verts
1934  */
1935 static int linehit_compare(const void *vlh1, const void *vlh2)
1936 {
1937  const KnifeLineHit *lh1 = vlh1;
1938  const KnifeLineHit *lh2 = vlh2;
1939 
1940  if (lh1->l < lh2->l) {
1941  return -1;
1942  }
1943  if (lh1->l > lh2->l) {
1944  return 1;
1945  }
1946  if (lh1->m < lh2->m) {
1947  return -1;
1948  }
1949  if (lh1->m > lh2->m) {
1950  return 1;
1951  }
1952  if (lh1->v < lh2->v) {
1953  return -1;
1954  }
1955  if (lh1->v > lh2->v) {
1956  return 1;
1957  }
1958  return 0;
1959 }
1960 
1961 /*
1962  * Sort linehits by distance along cut line, and secondarily from
1963  * front to back (from eye), and tertiarily by snap vertex,
1964  * and remove any duplicates.
1965  */
1967 {
1968  bool is_double = false;
1969 
1970  int n = kcd->totlinehit;
1971  KnifeLineHit *linehits = kcd->linehits;
1972  if (n == 0) {
1973  return;
1974  }
1975 
1976  qsort(linehits, n, sizeof(KnifeLineHit), linehit_compare);
1977 
1978  /* Remove any edge hits that are preceded or followed
1979  * by a vertex hit that is very near. Mark such edge hits using
1980  * l == -1 and then do another pass to actually remove.
1981  * Also remove all but one of a series of vertex hits for the same vertex. */
1982  for (int i = 0; i < n; i++) {
1983  KnifeLineHit *lhi = &linehits[i];
1984  if (lhi->v == NULL) {
1985  continue;
1986  }
1987 
1988  for (int j = i - 1; j >= 0; j--) {
1989  KnifeLineHit *lhj = &linehits[j];
1990  if (!lhj->kfe || fabsf(lhi->l - lhj->l) > KNIFE_FLT_EPSBIG ||
1991  fabsf(lhi->m - lhj->m) > KNIFE_FLT_EPSBIG) {
1992  break;
1993  }
1994 
1995  if (lhi->kfe == lhj->kfe) {
1996  lhj->l = -1.0f;
1997  is_double = true;
1998  }
1999  }
2000  for (int j = i + 1; j < n; j++) {
2001  KnifeLineHit *lhj = &linehits[j];
2002  if (fabsf(lhi->l - lhj->l) > KNIFE_FLT_EPSBIG || fabsf(lhi->m - lhj->m) > KNIFE_FLT_EPSBIG) {
2003  break;
2004  }
2005  if ((lhj->kfe && (lhi->kfe == lhj->kfe)) || (lhi->v == lhj->v)) {
2006  lhj->l = -1.0f;
2007  is_double = true;
2008  }
2009  }
2010  }
2011 
2012  if (is_double) {
2013  /* Delete-in-place loop: copying from pos j to pos i+1. */
2014  int i = 0;
2015  int j = 1;
2016  while (j < n) {
2017  KnifeLineHit *lhi = &linehits[i];
2018  KnifeLineHit *lhj = &linehits[j];
2019  if (lhj->l == -1.0f) {
2020  j++; /* Skip copying this one. */
2021  }
2022  else {
2023  /* Copy unless a no-op. */
2024  if (lhi->l == -1.0f) {
2025  /* Could happen if linehits[0] is being deleted. */
2026  memcpy(&linehits[i], &linehits[j], sizeof(KnifeLineHit));
2027  }
2028  else {
2029  if (i + 1 != j) {
2030  memcpy(&linehits[i + 1], &linehits[j], sizeof(KnifeLineHit));
2031  }
2032  i++;
2033  }
2034  j++;
2035  }
2036  }
2037  kcd->totlinehit = i + 1;
2038  }
2039 }
2040 
2041 /* Add hit to list of hits in facehits[f], where facehits is a map, if not already there. */
2043  GHash *facehits,
2044  BMFace *f,
2045  KnifeLineHit *hit)
2046 {
2047  ListBase *list = BLI_ghash_lookup(facehits, f);
2048 
2049  if (!list) {
2050  list = knife_empty_list(kcd);
2051  BLI_ghash_insert(facehits, f, list);
2052  }
2053  knife_append_list_no_dup(kcd, list, hit);
2054 }
2055 
2061  const KnifeLineHit *lh,
2062  const float co[3])
2063 {
2064 
2065  if (lh->v && lh->v->v) {
2066  BMLoop *l; /* side-of-loop */
2067  if ((l = BM_face_vert_share_loop(f, lh->v->v)) &&
2068  (BM_loop_point_side_of_loop_test(l, co) < 0.0f)) {
2069  return true;
2070  }
2071  }
2072  else if ((lh->kfe && lh->kfe->e)) {
2073  BMLoop *l; /* side-of-edge */
2074  if ((l = BM_face_edge_share_loop(f, lh->kfe->e)) &&
2075  (BM_loop_point_side_of_edge_test(l, co) < 0.0f)) {
2076  return true;
2077  }
2078  }
2079 
2080  return false;
2081 }
2082 
2084  KnifeLineHit *lh1,
2085  KnifeLineHit *lh2,
2086  BMFace *f)
2087 {
2088  KnifeEdge *kfe, *kfe2;
2089  BMEdge *e_base;
2090 
2091  if ((lh1->v && lh1->v == lh2->v) || (lh1->kfe && lh1->kfe == lh2->kfe)) {
2092  return;
2093  }
2094 
2095  /* If the cut is on an edge. */
2096  if ((lh1->v && lh2->v) && (lh1->v->v && lh2->v && lh2->v->v) &&
2097  (e_base = BM_edge_exists(lh1->v->v, lh2->v->v))) {
2098  return;
2099  }
2102  return;
2103  }
2104 
2105  /* Check if edge actually lies within face (might not, if this face is concave). */
2106  if ((lh1->v && !lh1->kfe) && (lh2->v && !lh2->kfe)) {
2107  if (!knife_verts_edge_in_face(lh1->v, lh2->v, f)) {
2108  return;
2109  }
2110  }
2111 
2112  kfe = new_knife_edge(kcd);
2113  kfe->is_cut = true;
2114  kfe->basef = f;
2115 
2116  if (lh1->v) {
2117  kfe->v1 = lh1->v;
2118  }
2119  else if (lh1->kfe) {
2120  kfe->v1 = knife_split_edge(kcd, lh1->kfe, lh1->hit, lh1->cagehit, &kfe2);
2121  lh1->v = kfe->v1; /* Record the #KnifeVert for this hit. */
2122  }
2123  else {
2124  BLI_assert(lh1->f);
2125  kfe->v1 = new_knife_vert(kcd, lh1->hit, lh1->cagehit);
2126  kfe->v1->ob = lh1->ob;
2127  kfe->v1->base_index = lh1->base_index;
2128  kfe->v1->is_cut = true;
2129  kfe->v1->is_face = true;
2130  knife_append_list(kcd, &kfe->v1->faces, lh1->f);
2131  lh1->v = kfe->v1; /* Record the #KnifeVert for this hit. */
2132  }
2133 
2134  if (lh2->v) {
2135  kfe->v2 = lh2->v;
2136  }
2137  else if (lh2->kfe) {
2138  kfe->v2 = knife_split_edge(kcd, lh2->kfe, lh2->hit, lh2->cagehit, &kfe2);
2139  lh2->v = kfe->v2; /* Future uses of lh2 won't split again. */
2140  }
2141  else {
2142  BLI_assert(lh2->f);
2143  kfe->v2 = new_knife_vert(kcd, lh2->hit, lh2->cagehit);
2144  kfe->v2->ob = lh2->ob;
2145  kfe->v2->base_index = lh2->base_index;
2146  kfe->v2->is_cut = true;
2147  kfe->v2->is_face = true;
2148  knife_append_list(kcd, &kfe->v2->faces, lh2->f);
2149  lh2->v = kfe->v2; /* Record the KnifeVert for this hit. */
2150  }
2151 
2152  knife_add_to_vert_edges(kcd, kfe);
2153 
2154  if (kfe->basef && !find_ref(&kfe->faces, kfe->basef)) {
2155  knife_edge_append_face(kcd, kfe, kfe->basef);
2156  }
2157 
2158  /* Update current undo frame cut count. */
2159  kcd->undo->cuts++;
2160 }
2161 
2162 /* Given a list of KnifeLineHits for one face, sorted by l
2163  * and then by m, make the required KnifeVerts and
2164  * KnifeEdges.
2165  */
2166 static void knife_cut_face(KnifeTool_OpData *kcd, BMFace *f, ListBase *hits)
2167 {
2168  Ref *r;
2169 
2170  if (BLI_listbase_count_at_most(hits, 2) != 2) {
2171  return;
2172  }
2173 
2174  for (r = hits->first; r->next; r = r->next) {
2175  knife_add_single_cut(kcd, r->ref, r->next->ref, f);
2176  }
2177 }
2178 
2180 {
2181  KnifeEdge *kfe;
2182  Ref *ref;
2183  int edge_array_len = BLI_listbase_count(kfedges);
2184  int i;
2185 
2186  BMEdge **edge_array = BLI_array_alloca(edge_array, edge_array_len);
2187 
2188  /* Point to knife edges we've created edges in, edge_array aligned. */
2189  KnifeEdge **kfe_array = BLI_array_alloca(kfe_array, edge_array_len);
2190 
2192 
2193  i = 0;
2194  for (ref = kfedges->first; ref; ref = ref->next) {
2195  bool is_new_edge = false;
2196  kfe = ref->ref;
2197 
2198  if (kfe->is_invalid) {
2199  continue;
2200  }
2201 
2202  if (kfe->e == NULL) {
2203  if (kfe->v1->v && kfe->v2->v) {
2204  kfe->e = BM_edge_exists(kfe->v1->v, kfe->v2->v);
2205  }
2206  }
2207 
2208  if (kfe->e) {
2209  if (BM_edge_in_face(kfe->e, f)) {
2210  /* Shouldn't happen, but in this case just ignore. */
2211  continue;
2212  }
2213  }
2214  else {
2215  if (kfe->v1->v == NULL) {
2216  kfe->v1->v = BM_vert_create(bm, kfe->v1->co, NULL, 0);
2217  }
2218  if (kfe->v2->v == NULL) {
2219  kfe->v2->v = BM_vert_create(bm, kfe->v2->co, NULL, 0);
2220  }
2221  BLI_assert(kfe->e == NULL);
2222  kfe->e = BM_edge_create(bm, kfe->v1->v, kfe->v2->v, NULL, 0);
2223  if (kfe->e) {
2225  BM_edge_select_set(bm, kfe->e, true);
2226  }
2227  is_new_edge = true;
2228  }
2229  }
2230 
2231  BLI_assert(kfe->e);
2232 
2233  if (BLI_gset_add(kcd->edgenet.edge_visit, kfe->e)) {
2234  kfe_array[i] = is_new_edge ? kfe : 0;
2235  edge_array[i] = kfe->e;
2236  i += 1;
2237  }
2238  }
2239 
2240  if (i) {
2241  const int edge_array_len_orig = i;
2242  edge_array_len = i;
2243 
2244 #ifdef USE_NET_ISLAND_CONNECT
2245  uint edge_array_holes_len;
2246  BMEdge **edge_array_holes;
2248  f,
2249  edge_array,
2250  edge_array_len,
2251  true,
2252  kcd->edgenet.arena,
2253  &edge_array_holes,
2254  &edge_array_holes_len)) {
2256  for (i = edge_array_len; i < edge_array_holes_len; i++) {
2257  BM_edge_select_set(bm, edge_array_holes[i], true);
2258  }
2259  }
2260 
2261  edge_array_len = edge_array_holes_len;
2262  edge_array = edge_array_holes; /* Owned by the arena. */
2263  }
2264 #endif
2265 
2266  {
2267  BMFace **face_arr = NULL;
2268  int face_arr_len;
2269 
2270  BM_face_split_edgenet(bm, f, edge_array, edge_array_len, &face_arr, &face_arr_len);
2271 
2272  if (face_arr) {
2273  MEM_freeN(face_arr);
2274  }
2275  }
2276 
2277  /* Remove dangling edges, not essential - but nice for users. */
2278  for (i = 0; i < edge_array_len_orig; i++) {
2279  if (kfe_array[i] == NULL) {
2280  continue;
2281  }
2282  if (BM_edge_is_wire(kfe_array[i]->e)) {
2283  BM_edge_kill(bm, kfe_array[i]->e);
2284  kfe_array[i]->e = NULL;
2285  }
2286  }
2287 
2288 #ifdef USE_NET_ISLAND_CONNECT
2290 #endif
2291  }
2292 
2294 }
2295 
2296 static int sort_verts_by_dist_cb(void *co_p, const void *cur_a_p, const void *cur_b_p)
2297 {
2298  const KnifeVert *cur_a = ((const Ref *)cur_a_p)->ref;
2299  const KnifeVert *cur_b = ((const Ref *)cur_b_p)->ref;
2300  const float *co = co_p;
2301  const float a_sq = len_squared_v3v3(co, cur_a->co);
2302  const float b_sq = len_squared_v3v3(co, cur_b->co);
2303 
2304  if (a_sq < b_sq) {
2305  return -1;
2306  }
2307  if (a_sq > b_sq) {
2308  return 1;
2309  }
2310  return 0;
2311 }
2312 
2313 /* Use the network of KnifeEdges and KnifeVerts accumulated to make real BMVerts and BMEdedges. */
2315 {
2317  BMesh *bm = em->bm;
2318  KnifeEdge *kfe;
2319  KnifeVert *kfv;
2320  BMFace *f;
2321  BMEdge *e, *enew;
2322  ListBase *list;
2323  Ref *ref;
2324  float pct;
2325  SmallHashIter hiter;
2326  BLI_mempool_iter iter;
2327  SmallHash fhash_, *fhash = &fhash_;
2328  SmallHash ehash_, *ehash = &ehash_;
2329 
2330  BLI_smallhash_init(fhash);
2331  BLI_smallhash_init(ehash);
2332 
2333  /* Put list of cutting edges for a face into fhash, keyed by face. */
2334  BLI_mempool_iternew(kcd->kedges, &iter);
2335  for (kfe = BLI_mempool_iterstep(&iter); kfe; kfe = BLI_mempool_iterstep(&iter)) {
2336  if (kfe->is_invalid || kfe->v1->ob != ob) {
2337  continue;
2338  }
2339 
2340  /* Select edges that lie directly on the cut. */
2341  if (kcd->select_result) {
2342  if (kfe->e && kfe->is_cut) {
2343  BM_edge_select_set(bm, kfe->e, true);
2344  }
2345  }
2346 
2347  f = kfe->basef;
2348  if (!f || kfe->e) {
2349  continue;
2350  }
2351  list = BLI_smallhash_lookup(fhash, (uintptr_t)f);
2352  if (!list) {
2353  list = knife_empty_list(kcd);
2354  BLI_smallhash_insert(fhash, (uintptr_t)f, list);
2355  }
2356  knife_append_list(kcd, list, kfe);
2357  }
2358 
2359  /* Put list of splitting vertices for an edge into ehash, keyed by edge. */
2360  BLI_mempool_iternew(kcd->kverts, &iter);
2361  for (kfv = BLI_mempool_iterstep(&iter); kfv; kfv = BLI_mempool_iterstep(&iter)) {
2362  if (kfv->v || kfv->is_invalid || kfv->ob != ob) {
2363  continue; /* Already have a BMVert. */
2364  }
2365  for (ref = kfv->edges.first; ref; ref = ref->next) {
2366  kfe = ref->ref;
2367  e = kfe->e;
2368  if (!e) {
2369  continue;
2370  }
2371  list = BLI_smallhash_lookup(ehash, (uintptr_t)e);
2372  if (!list) {
2373  list = knife_empty_list(kcd);
2374  BLI_smallhash_insert(ehash, (uintptr_t)e, list);
2375  }
2376  /* There can be more than one kfe in kfv's list with same e. */
2377  if (!find_ref(list, kfv)) {
2378  knife_append_list(kcd, list, kfv);
2379  }
2380  }
2381  }
2382 
2383  /* Split bmesh edges where needed. */
2384  for (list = BLI_smallhash_iternew(ehash, &hiter, (uintptr_t *)&e); list;
2385  list = BLI_smallhash_iternext(&hiter, (uintptr_t *)&e)) {
2387 
2388  for (ref = list->first; ref; ref = ref->next) {
2389  kfv = ref->ref;
2390  pct = line_point_factor_v3(kfv->co, e->v1->co, e->v2->co);
2391  kfv->v = BM_edge_split(bm, e, e->v1, &enew, pct);
2392  }
2393  }
2394 
2395  if (kcd->only_select) {
2397  }
2398 
2399  /* Do cuts for each face. */
2400  for (list = BLI_smallhash_iternew(fhash, &hiter, (uintptr_t *)&f); list;
2401  list = BLI_smallhash_iternext(&hiter, (uintptr_t *)&f)) {
2402  knife_make_face_cuts(kcd, bm, f, list);
2403  }
2404 
2405  BLI_smallhash_release(fhash);
2406  BLI_smallhash_release(ehash);
2407 }
2408 
2409 /* User has just left-clicked after the first time.
2410  * Add all knife cuts implied by line from prev to curr.
2411  * If that line crossed edges then kcd->linehits will be non-NULL.
2412  * Make all of the KnifeVerts and KnifeEdges implied by this cut.
2413  */
2415 {
2416  int i;
2417  GHash *facehits;
2418  BMFace *f;
2419  Ref *r;
2420  GHashIterator giter;
2421  ListBase *list;
2422 
2423  /* Allocate new undo frame on stack, unless cut is being dragged. */
2424  if (!kcd->is_drag_undo) {
2425  kcd->undo = BLI_stack_push_r(kcd->undostack);
2426  kcd->undo->pos = kcd->prev;
2427  kcd->undo->cuts = 0;
2428  kcd->undo->splits = 0;
2429  kcd->undo->mdata = kcd->mdata;
2430  kcd->is_drag_undo = true;
2431  }
2432 
2433  /* Save values for angle drawing calculations. */
2434  copy_v3_v3(kcd->mdata.cage, kcd->prev.cage);
2435  copy_v2_v2(kcd->mdata.mval, kcd->prev.mval);
2436  kcd->mdata.is_stored = true;
2437 
2439  if (kcd->totlinehit == 0) {
2440  if (kcd->is_drag_hold == false) {
2441  kcd->prev = kcd->curr;
2442  }
2443  return;
2444  }
2445 
2446  /* Consider most recent linehit in angle drawing calculations. */
2447  if (kcd->totlinehit >= 2) {
2448  copy_v3_v3(kcd->mdata.cage, kcd->linehits[kcd->totlinehit - 2].cagehit);
2449  }
2450 
2451  /* Make facehits: map face -> list of linehits touching it. */
2452  facehits = BLI_ghash_ptr_new("knife facehits");
2453  for (i = 0; i < kcd->totlinehit; i++) {
2454  KnifeLineHit *lh = &kcd->linehits[i];
2455  if (lh->f) {
2456  add_hit_to_facehits(kcd, facehits, lh->f, lh);
2457  }
2458  if (lh->v) {
2459  for (r = lh->v->faces.first; r; r = r->next) {
2460  add_hit_to_facehits(kcd, facehits, r->ref, lh);
2461  }
2462  }
2463  if (lh->kfe) {
2464  for (r = lh->kfe->faces.first; r; r = r->next) {
2465  add_hit_to_facehits(kcd, facehits, r->ref, lh);
2466  }
2467  }
2468  }
2469 
2470  /* NOTE: as following loop progresses, the 'v' fields of
2471  * the linehits will be filled in (as edges are split or
2472  * in-face verts are made), so it may be true that both
2473  * the v and the kfe or f fields will be non-NULL. */
2474  GHASH_ITER (giter, facehits) {
2475  f = (BMFace *)BLI_ghashIterator_getKey(&giter);
2476  list = (ListBase *)BLI_ghashIterator_getValue(&giter);
2477  knife_cut_face(kcd, f, list);
2478  }
2479 
2480  /* Set up for next cut. */
2481  kcd->prev = kcd->curr;
2482 
2483  if (kcd->prev.bmface) {
2484  /* Was "in face" but now we have a KnifeVert it is snapped to. */
2485  KnifeLineHit *lh = &kcd->linehits[kcd->totlinehit - 1];
2486  kcd->prev.vert = lh->v;
2487  kcd->prev.bmface = NULL;
2488  }
2489 
2490  if (kcd->is_drag_hold) {
2491  KnifeLineHit *lh = &kcd->linehits[kcd->totlinehit - 1];
2492  linehit_to_knifepos(&kcd->prev, lh);
2493  }
2494 
2495  BLI_ghash_free(facehits, NULL, NULL);
2496  MEM_freeN(kcd->linehits);
2497  kcd->linehits = NULL;
2498  kcd->totlinehit = 0;
2499 }
2500 
2502 {
2503  if (kcd->linehits) {
2504  MEM_freeN(kcd->linehits);
2505  kcd->linehits = NULL;
2506  kcd->totlinehit = 0;
2507  }
2508 }
2509 
2512 /* -------------------------------------------------------------------- */
2516 /* Record the index in kcd->em->looptris of first looptri triple for a given face,
2517  * given an index for some triple in that array.
2518  * This assumes that all of the triangles for a given face are contiguous
2519  * in that array (as they are by the current tessellation routines).
2520  * Actually store index + 1 in the hash, because 0 looks like "no entry"
2521  * to hash lookup routine; will reverse this in the get routine.
2522  * Doing this lazily rather than all at once for all faces.
2523  */
2524 static void set_lowest_face_tri(KnifeTool_OpData *kcd, BMEditMesh *em, BMFace *f, int index)
2525 {
2526  int i;
2527 
2528  if (BLI_ghash_lookup(kcd->facetrimap, f)) {
2529  return;
2530  }
2531 
2532  BLI_assert(index >= 0 && index < em->tottri);
2533  BLI_assert(em->looptris[index][0]->f == f);
2534  for (i = index - 1; i >= 0; i--) {
2535  if (em->looptris[i][0]->f != f) {
2536  i++;
2537  break;
2538  }
2539  }
2540  if (i == -1) {
2541  i++;
2542  }
2543 
2544  BLI_ghash_insert(kcd->facetrimap, f, POINTER_FROM_INT(i + 1));
2545 }
2546 
2547 /* This should only be called for faces that have had a lowest face tri set by previous function.
2548  */
2550 {
2551  int ans;
2552 
2553  ans = POINTER_AS_INT(BLI_ghash_lookup(kcd->facetrimap, f));
2554  BLI_assert(ans != 0);
2555  return ans - 1;
2556 }
2557 
2567  const float s[2],
2568  const float v1[3],
2569  const float v2[3],
2570  Object *ob,
2571  uint base_index,
2572  BMFace *f,
2573  const float face_tol_sq,
2574  float hit_co[3],
2575  float hit_cageco[3])
2576 {
2578 
2579  int tottri, tri_i;
2580  float raydir[3];
2581  float tri_norm[3], tri_plane[4];
2582  float se1[2], se2[2];
2583  float d, lambda;
2584  BMLoop **tri;
2585  ListBase *list;
2586  Ref *ref;
2587  KnifeEdge *kfe;
2588 
2589  sub_v3_v3v3(raydir, v2, v1);
2590  normalize_v3(raydir);
2591  tri_i = get_lowest_face_tri(kcd, f);
2592  tottri = em->tottri;
2593  BLI_assert(tri_i >= 0 && tri_i < tottri);
2594 
2595  for (; tri_i < tottri; tri_i++) {
2596  float tri_cos[3][3];
2597  float ray_tri_uv[2];
2598 
2599  tri = em->looptris[tri_i];
2600  if (tri[0]->f != f) {
2601  break;
2602  }
2603 
2604  knife_bm_tri_cagecos_get_worldspace(kcd, base_index, tri_i, tri_cos);
2605 
2606  /* Using epsilon test in case ray is directly through an internal
2607  * tessellation edge and might not hit either tessellation tri with
2608  * an exact test;
2609  * We will exclude hits near real edges by a later test. */
2611  v1, raydir, UNPACK3(tri_cos), &lambda, ray_tri_uv, KNIFE_FLT_EPS)) {
2612  /* Check if line coplanar with tri. */
2613  normal_tri_v3(tri_norm, UNPACK3(tri_cos));
2614  plane_from_point_normal_v3(tri_plane, tri_cos[0], tri_norm);
2615  if ((dist_squared_to_plane_v3(v1, tri_plane) < KNIFE_FLT_EPS) &&
2616  (dist_squared_to_plane_v3(v2, tri_plane) < KNIFE_FLT_EPS)) {
2617  return false;
2618  }
2619  interp_v3_v3v3v3_uv(hit_cageco, UNPACK3(tri_cos), ray_tri_uv);
2620  /* Now check that far enough away from verts and edges. */
2621  list = knife_get_face_kedges(kcd, ob, base_index, f);
2622  for (ref = list->first; ref; ref = ref->next) {
2623  kfe = ref->ref;
2624  if (kfe->is_invalid) {
2625  continue;
2626  }
2627  knife_project_v2(kcd, kfe->v1->cageco, se1);
2628  knife_project_v2(kcd, kfe->v2->cageco, se2);
2629  d = dist_squared_to_line_segment_v2(s, se1, se2);
2630  if (d < face_tol_sq) {
2631  return false;
2632  }
2633  }
2634  interp_v3_v3v3v3_uv(hit_co, tri[0]->v->co, tri[1]->v->co, tri[2]->v->co, ray_tri_uv);
2635  return true;
2636  }
2637  }
2638  return false;
2639 }
2640 
2646 {
2647  Object *ob;
2648  BMEditMesh *em;
2649  BMIter iter;
2650  BMVert *v;
2651  float min[3], max[3];
2652  float ws[3];
2653  INIT_MINMAX(min, max);
2654 
2655  for (uint b = 0; b < kcd->objects_len; b++) {
2656  ob = kcd->objects[b];
2657  em = BKE_editmesh_from_object(ob);
2658 
2659  const float(*cagecos)[3] = kcd->objects_info[b].cagecos;
2660  if (cagecos) {
2661  for (int i = 0; i < em->bm->totvert; i++) {
2662  copy_v3_v3(ws, cagecos[i]);
2663  mul_m4_v3(ob->obmat, ws);
2664  minmax_v3v3_v3(min, max, ws);
2665  }
2666  }
2667  else {
2668  BM_ITER_MESH (v, &iter, em->bm, BM_VERTS_OF_MESH) {
2669  copy_v3_v3(ws, v->co);
2670  mul_m4_v3(ob->obmat, ws);
2671  minmax_v3v3_v3(min, max, ws);
2672  }
2673  }
2674  }
2675 
2676  kcd->ortho_extent = len_v3v3(min, max) / 2;
2678 }
2679 
2680 /* Do edges e1 and e2 go between exactly the same coordinates? */
2681 static bool coinciding_edges(BMEdge *e1, BMEdge *e2)
2682 {
2683  const float *co11, *co12, *co21, *co22;
2684 
2685  co11 = e1->v1->co;
2686  co12 = e1->v2->co;
2687  co21 = e2->v1->co;
2688  co22 = e2->v2->co;
2689  if ((equals_v3v3(co11, co21) && equals_v3v3(co12, co22)) ||
2690  (equals_v3v3(co11, co22) && equals_v3v3(co12, co21))) {
2691  return true;
2692  }
2693  return false;
2694 }
2695 
2696 /* Callback used in point_is_visible to exclude hits on the faces that are the same
2697  * as or contain the hitting element (which is in user_data).
2698  * Also (see T44492) want to exclude hits on faces that butt up to the hitting element
2699  * (e.g., when you double an edge by an edge split).
2700  */
2702 {
2703  bool ans;
2704  BMEdge *e, *e2;
2705  BMIter iter;
2706 
2707  switch (((BMElem *)user_data)->head.htype) {
2708  case BM_FACE:
2709  ans = (BMFace *)user_data != f;
2710  break;
2711  case BM_EDGE:
2712  e = (BMEdge *)user_data;
2713  ans = !BM_edge_in_face(e, f);
2714  if (ans) {
2715  /* Is it a boundary edge, coincident with a split edge? */
2716  if (BM_edge_is_boundary(e)) {
2717  BM_ITER_ELEM (e2, &iter, f, BM_EDGES_OF_FACE) {
2718  if (coinciding_edges(e, e2)) {
2719  ans = false;
2720  break;
2721  }
2722  }
2723  }
2724  }
2725  break;
2726  case BM_VERT:
2727  ans = !BM_vert_in_face((BMVert *)user_data, f);
2728  break;
2729  default:
2730  ans = true;
2731  break;
2732  }
2733  return ans;
2734 }
2735 
2744  const float p[3],
2745  const float s[2],
2746  BMElem *ele_test)
2747 {
2748  BMFace *f_hit;
2749 
2750  /* If box clipping on, make sure p is not clipped. */
2751  if (RV3D_CLIPPING_ENABLED(kcd->vc.v3d, kcd->vc.rv3d) &&
2752  ED_view3d_clipping_test(kcd->vc.rv3d, p, false)) {
2753  return false;
2754  }
2755 
2756  /* If not cutting through, make sure no face is in front of p. */
2757  if (!kcd->cut_through) {
2758  float dist;
2759  float view[3], p_ofs[3];
2760 
2761  /* TODO: I think there's a simpler way to get the required raycast ray. */
2762  ED_view3d_unproject_v3(kcd->vc.region, s[0], s[1], 0.0f, view);
2763 
2764  /* Make p_ofs a little towards view, so ray doesn't hit p's face. */
2765  sub_v3_v3(view, p);
2766  dist = normalize_v3(view);
2767  copy_v3_v3(p_ofs, p);
2768 
2769  /* Avoid projecting behind the viewpoint. */
2770  if (kcd->is_ortho && (kcd->vc.rv3d->persp != RV3D_CAMOB)) {
2771  dist = kcd->vc.v3d->clip_end * 2.0f;
2772  }
2773 
2774  if (RV3D_CLIPPING_ENABLED(kcd->vc.v3d, kcd->vc.rv3d)) {
2775  float view_clip[2][3];
2776  /* NOTE: view_clip[0] should never get clipped. */
2777  copy_v3_v3(view_clip[0], p_ofs);
2778  madd_v3_v3v3fl(view_clip[1], p_ofs, view, dist);
2779 
2780  if (clip_segment_v3_plane_n(view_clip[0],
2781  view_clip[1],
2782  kcd->vc.rv3d->clip_local,
2783  6,
2784  view_clip[0],
2785  view_clip[1])) {
2786  dist = len_v3v3(p_ofs, view_clip[1]);
2787  }
2788  }
2789 
2790  /* See if there's a face hit between p1 and the view. */
2791  if (ele_test) {
2792  f_hit = knife_bvh_raycast_filter(kcd,
2793  p_ofs,
2794  view,
2795  KNIFE_FLT_EPS,
2796  &dist,
2797  NULL,
2798  NULL,
2799  NULL,
2801  ele_test);
2802  }
2803  else {
2804  f_hit = knife_bvh_raycast(kcd, p_ofs, view, KNIFE_FLT_EPS, &dist, NULL, NULL, NULL);
2805  }
2806 
2807  if (f_hit) {
2808  return false;
2809  }
2810  }
2811 
2812  return true;
2813 }
2814 
2815 /* Clip the line (v1, v2) to planes perpendicular to it and distances d from
2816  * the closest point on the line to the origin. */
2817 static void clip_to_ortho_planes(float v1[3], float v2[3], const float center[3], const float d)
2818 {
2819  float closest[3], dir[3];
2820 
2821  sub_v3_v3v3(dir, v1, v2);
2822  normalize_v3(dir);
2823 
2824  /* could be v1 or v2 */
2825  sub_v3_v3(v1, center);
2828 
2829  madd_v3_v3v3fl(v1, closest, dir, d);
2830  madd_v3_v3v3fl(v2, closest, dir, -d);
2831 }
2832 
2834 {
2835  lh->m = dot_m4_v3_row_z(kcd->vc.rv3d->persmatob, lh->cagehit);
2836 }
2837 
2838 /* Finds visible (or all, if cutting through) edges that intersects the current screen drag line.
2839  */
2841 {
2842  SmallHash faces, kfes, kfvs, fobs;
2843  float v1[3], v2[3], v3[3], v4[3], s1[2], s2[2];
2844  int *results, *result;
2845  BMLoop **ls;
2846  BMFace *f;
2847  KnifeEdge *kfe;
2848  KnifeVert *v;
2849  ListBase *list;
2850  Ref *ref;
2851  KnifeLineHit *linehits = NULL;
2852  BLI_array_declare(linehits);
2853  SmallHashIter hiter;
2854  KnifeLineHit hit;
2855  void *val;
2856  void **val_p;
2857  float s[2], se1[2], se2[2], sint[2];
2858  float r1[3], r2[3];
2859  float d1, d2, lambda;
2860  float vert_tol, vert_tol_sq;
2861  float line_tol, line_tol_sq;
2862  float face_tol, face_tol_sq;
2863  uint tot;
2864  int i;
2865 
2866  if (kcd->linehits) {
2867  MEM_freeN(kcd->linehits);
2868  kcd->linehits = NULL;
2869  kcd->totlinehit = 0;
2870  }
2871 
2872  copy_v3_v3(v1, kcd->prev.cage);
2873  copy_v3_v3(v2, kcd->curr.cage);
2874 
2875  /* Project screen line's 3d coordinates back into 2d. */
2876  knife_project_v2(kcd, v1, s1);
2877  knife_project_v2(kcd, v2, s2);
2878 
2879  if (kcd->is_interactive) {
2880  if (len_squared_v2v2(s1, s2) < 1.0f) {
2881  return;
2882  }
2883  }
2884  else {
2885  if (len_squared_v2v2(s1, s2) < KNIFE_FLT_EPS_SQUARED) {
2886  return;
2887  }
2888  }
2889 
2890  /* Unproject screen line. */
2891  ED_view3d_win_to_segment_clipped(kcd->vc.depsgraph, kcd->region, kcd->vc.v3d, s1, v1, v3, true);
2892  ED_view3d_win_to_segment_clipped(kcd->vc.depsgraph, kcd->region, kcd->vc.v3d, s2, v2, v4, true);
2893 
2894  /* Numeric error, 'v1' -> 'v2', 'v2' -> 'v4'
2895  * can end up being ~2000 units apart with an orthogonal perspective.
2896  *
2897  * (from ED_view3d_win_to_segment_clipped() above)
2898  * This gives precision error; rather than solving properly
2899  * (which may involve using doubles everywhere!),
2900  * limit the distance between these points. */
2901  if (kcd->is_ortho && (kcd->vc.rv3d->persp != RV3D_CAMOB)) {
2902  if (kcd->ortho_extent == 0.0f) {
2903  calc_ortho_extent(kcd);
2904  }
2905  clip_to_ortho_planes(v1, v3, kcd->ortho_extent_center, kcd->ortho_extent + 10.0f);
2906  clip_to_ortho_planes(v2, v4, kcd->ortho_extent_center, kcd->ortho_extent + 10.0f);
2907  }
2908 
2909  float plane[4];
2910  {
2911  float v1_v2[3], v1_v3[3];
2912  sub_v3_v3v3(v1_v2, v2, v1);
2913  sub_v3_v3v3(v1_v3, v3, v1);
2914  cross_v3_v3v3(plane, v1_v2, v1_v3);
2915  plane_from_point_normal_v3(plane, v1, plane);
2916  }
2917 
2918  /* First use BVH tree to find faces, knife edges, and knife verts that might
2919  * intersect the cut plane with rays v1-v3 and v2-v4.
2920  * This de-duplicates the candidates before doing more expensive intersection tests. */
2921 
2922  results = BLI_bvhtree_intersect_plane(kcd->bvh.tree, plane, &tot);
2923  if (!results) {
2924  return;
2925  }
2926 
2928  BLI_smallhash_init(&kfes);
2929  BLI_smallhash_init(&kfvs);
2930  BLI_smallhash_init(&fobs);
2931 
2932  Object *ob;
2933  BMEditMesh *em;
2934  uint b = 0;
2935 
2936  for (i = 0, result = results; i < tot; i++, result++) {
2937  for (b = 0; b < kcd->objects_len; b++) {
2938  ob = kcd->objects[b];
2939  em = BKE_editmesh_from_object(ob);
2940  if (*result >= 0 && *result < em->tottri) {
2941  ls = (BMLoop **)em->looptris[*result];
2942  break;
2943  }
2944  *result -= em->tottri;
2945  }
2946 
2947  f = ls[0]->f;
2948  set_lowest_face_tri(kcd, em, f, *result);
2949 
2950  /* Occlude but never cut unselected faces (when only_select is used). */
2951  if (kcd->only_select && !BM_elem_flag_test(f, BM_ELEM_SELECT)) {
2952  continue;
2953  }
2954  /* For faces, store index of lowest hit looptri in hash. */
2955  if (BLI_smallhash_haskey(&faces, (uintptr_t)f)) {
2956  continue;
2957  }
2958  /* Don't care what the value is except that it is non-NULL, for iterator. */
2960  BLI_smallhash_insert(&fobs, (uintptr_t)f, (void *)(uintptr_t)b);
2961 
2962  list = knife_get_face_kedges(kcd, ob, b, f);
2963  for (ref = list->first; ref; ref = ref->next) {
2964  kfe = ref->ref;
2965  if (kfe->is_invalid) {
2966  continue;
2967  }
2968  if (BLI_smallhash_haskey(&kfes, (uintptr_t)kfe)) {
2969  continue;
2970  }
2971  BLI_smallhash_insert(&kfes, (uintptr_t)kfe, kfe);
2972  v = kfe->v1;
2973  BLI_smallhash_reinsert(&kfvs, (uintptr_t)v, v);
2974  v = kfe->v2;
2975  BLI_smallhash_reinsert(&kfvs, (uintptr_t)v, v);
2976  }
2977  }
2978 
2979  /* Now go through the candidates and find intersections. */
2980  /* These tolerances, in screen space, are for intermediate hits,
2981  * as ends are already snapped to screen. */
2982 
2983  if (kcd->is_interactive) {
2984  vert_tol = KNIFE_FLT_EPS_PX_VERT;
2985  line_tol = KNIFE_FLT_EPS_PX_EDGE;
2986  face_tol = KNIFE_FLT_EPS_PX_FACE;
2987  }
2988  else {
2989  /* Use 1/100th of a pixel, see T43896 (too big), T47910 (too small).
2990  *
2991  * Update, leave this as is until we investigate not using pixel coords
2992  * for geometry calculations: T48023. */
2993  vert_tol = line_tol = face_tol = 0.5f;
2994  }
2995 
2996  vert_tol_sq = vert_tol * vert_tol;
2997  line_tol_sq = line_tol * line_tol;
2998  face_tol_sq = face_tol * face_tol;
2999 
3000  /* Assume these tolerances swamp floating point rounding errors in calculations below. */
3001 
3002  /* First look for vertex hits. */
3003  for (val_p = BLI_smallhash_iternew_p(&kfvs, &hiter, (uintptr_t *)&v); val_p;
3004  val_p = BLI_smallhash_iternext_p(&hiter, (uintptr_t *)&v)) {
3005  KnifeEdge *kfe_hit = NULL;
3006 
3007  bool kfv_is_in_cut = false;
3008  if (ELEM(v, kcd->prev.vert, kcd->curr.vert)) {
3009  /* This KnifeVert was captured by the snap system.
3010  * Since the tolerance distance can be different, add this vertex directly.
3011  * Otherwise, the cut may fail or a close cut on a connected edge can be performed. */
3012  bm_elem_from_knife_vert(v, &kfe_hit);
3013  copy_v2_v2(s, (v == kcd->prev.vert) ? kcd->prev.mval : kcd->curr.mval);
3014  kfv_is_in_cut = true;
3015  }
3016  else {
3017  knife_project_v2(kcd, v->cageco, s);
3018  float d = dist_squared_to_line_segment_v2(s, s1, s2);
3019  if ((d <= vert_tol_sq) &&
3020  (point_is_visible(kcd, v->cageco, s, bm_elem_from_knife_vert(v, &kfe_hit)))) {
3021  kfv_is_in_cut = true;
3022  }
3023  }
3024 
3025  if (kfv_is_in_cut) {
3026  memset(&hit, 0, sizeof(hit));
3027  hit.v = v;
3028 
3029  /* If this isn't from an existing BMVert, it may have been added to a BMEdge originally.
3030  * Knowing if the hit comes from an edge is important for edge-in-face checks later on.
3031  * See: #knife_add_single_cut -> #knife_verts_edge_in_face, T42611. */
3032  if (kfe_hit) {
3033  hit.kfe = kfe_hit;
3034  }
3035 
3036  hit.ob = v->ob;
3037  hit.base_index = v->base_index;
3038  copy_v3_v3(hit.hit, v->co);
3039  copy_v3_v3(hit.cagehit, v->cageco);
3040  copy_v2_v2(hit.schit, s);
3041  set_linehit_depth(kcd, &hit);
3042  BLI_array_append(linehits, hit);
3043  }
3044  else {
3045  /* This vertex isn't used so remove from `kfvs`.
3046  * This is useful to detect KnifeEdges that can be skipped.
3047  * And it optimizes smallhash_iternext a little bit. */
3048  *val_p = NULL;
3049  }
3050  }
3051 
3052  /* Now edge hits; don't add if a vertex at end of edge should have hit. */
3053  for (val = BLI_smallhash_iternew(&kfes, &hiter, (uintptr_t *)&kfe); val;
3054  val = BLI_smallhash_iternext(&hiter, (uintptr_t *)&kfe)) {
3055 
3056  /* If we intersect any of the vertices, don't attempt to intersect the edge. */
3057  if (BLI_smallhash_lookup(&kfvs, (intptr_t)kfe->v1) ||
3058  BLI_smallhash_lookup(&kfvs, (intptr_t)kfe->v2)) {
3059  continue;
3060  }
3061 
3062  knife_project_v2(kcd, kfe->v1->cageco, se1);
3063  knife_project_v2(kcd, kfe->v2->cageco, se2);
3064  int isect_kind = 1;
3065  if (kfe == kcd->prev.edge) {
3066  /* This KnifeEdge was captured by the snap system. */
3067  copy_v2_v2(sint, kcd->prev.mval);
3068  }
3069  else if (kfe == kcd->curr.edge) {
3070  /* This KnifeEdge was captured by the snap system. */
3071  copy_v2_v2(sint, kcd->curr.mval);
3072  }
3073  else {
3074  isect_kind = isect_seg_seg_v2_point_ex(s1, s2, se1, se2, 0.0f, sint);
3075  if (isect_kind == -1) {
3076  /* isect_seg_seg_v2_point doesn't do tolerance test around ends of s1-s2. */
3077  closest_to_line_segment_v2(sint, s1, se1, se2);
3078  if (len_squared_v2v2(sint, s1) <= line_tol_sq) {
3079  isect_kind = 1;
3080  }
3081  else {
3082  closest_to_line_segment_v2(sint, s2, se1, se2);
3083  if (len_squared_v2v2(sint, s2) <= line_tol_sq) {
3084  isect_kind = 1;
3085  }
3086  }
3087  }
3088  }
3089  if (isect_kind == 1) {
3090  d1 = len_v2v2(sint, se1);
3091  d2 = len_v2v2(se2, se1);
3092  if (!(d1 <= line_tol || d2 <= line_tol || fabsf(d1 - d2) <= line_tol)) {
3093  float p_cage[3], p_cage_tmp[3];
3094  lambda = d1 / d2;
3095  /* Can't just interpolate between ends of kfe because
3096  * that doesn't work with perspective transformation.
3097  * Need to find 3d intersection of ray through sint. */
3098  knife_input_ray_segment(kcd, sint, 1.0f, r1, r2);
3099  isect_kind = isect_line_line_v3(
3100  kfe->v1->cageco, kfe->v2->cageco, r1, r2, p_cage, p_cage_tmp);
3101  if (isect_kind >= 1 && point_is_visible(kcd, p_cage, sint, bm_elem_from_knife_edge(kfe))) {
3102  memset(&hit, 0, sizeof(hit));
3103  if (kcd->snap_midpoints) {
3104  /* Choose intermediate point snap too. */
3105  mid_v3_v3v3(p_cage, kfe->v1->cageco, kfe->v2->cageco);
3106  mid_v2_v2v2(sint, se1, se2);
3107  lambda = 0.5f;
3108  }
3109  hit.kfe = kfe;
3111  hit.hit, p_cage, kfe->v1->co, kfe->v2->co, kfe->v1->cageco, kfe->v2->cageco);
3112  hit.ob = kfe->v1->ob;
3113  hit.base_index = kfe->v1->base_index;
3114  copy_v3_v3(hit.cagehit, p_cage);
3115  copy_v2_v2(hit.schit, sint);
3116  hit.perc = lambda;
3117  set_linehit_depth(kcd, &hit);
3118  BLI_array_append(linehits, hit);
3119  }
3120  }
3121  }
3122  }
3123 
3124  /* Now face hits; don't add if a vertex or edge in face should have hit. */
3125  const bool use_hit_prev = (kcd->prev.vert == NULL) && (kcd->prev.edge == NULL);
3126  const bool use_hit_curr = (kcd->curr.vert == NULL) && (kcd->curr.edge == NULL) &&
3127  !kcd->is_drag_hold;
3128  if (use_hit_prev || use_hit_curr) {
3129  for (val = BLI_smallhash_iternew(&faces, &hiter, (uintptr_t *)&f); val;
3130  val = BLI_smallhash_iternext(&hiter, (uintptr_t *)&f)) {
3131  float p[3], p_cage[3];
3132 
3133  uint base_index = (uint)(uintptr_t)BLI_smallhash_lookup(&fobs, (uintptr_t)f);
3134  ob = kcd->objects[base_index];
3135 
3136  if (use_hit_prev &&
3137  knife_ray_intersect_face(kcd, s1, v1, v3, ob, base_index, f, face_tol_sq, p, p_cage)) {
3138  if (point_is_visible(kcd, p_cage, s1, (BMElem *)f)) {
3139  memset(&hit, 0, sizeof(hit));
3140  hit.f = f;
3141  hit.ob = ob;
3142  hit.base_index = base_index;
3143  copy_v3_v3(hit.hit, p);
3144  copy_v3_v3(hit.cagehit, p_cage);
3145  copy_v2_v2(hit.schit, s1);
3146  set_linehit_depth(kcd, &hit);
3147  BLI_array_append(linehits, hit);
3148  }
3149  }
3150 
3151  if (use_hit_curr &&
3152  knife_ray_intersect_face(kcd, s2, v2, v4, ob, base_index, f, face_tol_sq, p, p_cage)) {
3153  if (point_is_visible(kcd, p_cage, s2, (BMElem *)f)) {
3154  memset(&hit, 0, sizeof(hit));
3155  hit.f = f;
3156  hit.ob = ob;
3157  hit.base_index = base_index;
3158  copy_v3_v3(hit.hit, p);
3159  copy_v3_v3(hit.cagehit, p_cage);
3160  copy_v2_v2(hit.schit, s2);
3161  set_linehit_depth(kcd, &hit);
3162  BLI_array_append(linehits, hit);
3163  }
3164  }
3165  }
3166  }
3167 
3168  kcd->linehits = linehits;
3169  kcd->totlinehit = BLI_array_len(linehits);
3170 
3171  /* Find position along screen line, used for sorting. */
3172  for (i = 0; i < kcd->totlinehit; i++) {
3173  KnifeLineHit *lh = kcd->linehits + i;
3174 
3175  lh->l = len_v2v2(lh->schit, s1) / len_v2v2(s2, s1);
3176  }
3177 
3179  BLI_smallhash_release(&kfes);
3180  BLI_smallhash_release(&kfvs);
3181  BLI_smallhash_release(&fobs);
3182  MEM_freeN(results);
3183 }
3184 
3187 /* -------------------------------------------------------------------- */
3192 {
3193  zero_v3(kpd->co);
3194  zero_v3(kpd->cage);
3195  kpd->vert = NULL;
3196  kpd->edge = NULL;
3197  kpd->bmface = NULL;
3198  zero_v2(kpd->mval);
3199 }
3200 
3203 /* -------------------------------------------------------------------- */
3208  Object **r_ob,
3209  uint *r_base_index,
3210  bool *is_space,
3211  float r_co[3],
3212  float r_cageco[3])
3213 {
3214  BMFace *f;
3215  float dist = KMAXDIST;
3216  float origin[3];
3217  float origin_ofs[3];
3218  float ray[3], ray_normal[3];
3219 
3220  /* Unproject to find view ray. */
3221  knife_input_ray_segment(kcd, kcd->curr.mval, 1.0f, origin, origin_ofs);
3222  sub_v3_v3v3(ray, origin_ofs, origin);
3223  normalize_v3_v3(ray_normal, ray);
3224 
3225  f = knife_bvh_raycast(kcd, origin, ray_normal, 0.0f, NULL, r_co, r_cageco, r_base_index);
3226 
3227  if (f && kcd->only_select && BM_elem_flag_test(f, BM_ELEM_SELECT) == 0) {
3228  f = NULL;
3229  }
3230 
3231  if (is_space) {
3232  *is_space = !f;
3233  }
3234 
3235  if (f) {
3236  *r_ob = kcd->objects[*r_base_index];
3237  }
3238  else {
3239  if (kcd->is_interactive) {
3240  /* Try to use back-buffer selection method if ray casting failed.
3241  *
3242  * Apply the mouse coordinates to a copy of the view-context
3243  * since we don't want to rely on this being set elsewhere. */
3244  ViewContext vc = kcd->vc;
3245  vc.mval[0] = (int)kcd->curr.mval[0];
3246  vc.mval[1] = (int)kcd->curr.mval[1];
3247 
3248  f = EDBM_face_find_nearest(&vc, &dist);
3249 
3250  /* Cheat for now; just put in the origin instead
3251  * of a true coordinate on the face.
3252  * This just puts a point 1.0f in front of the view. */
3253  add_v3_v3v3(r_co, origin, ray);
3254  /* Use this value for the cage location too as it's used to find near edges/vertices. */
3255  copy_v3_v3(r_cageco, r_co);
3256  }
3257  }
3258 
3259  return f;
3260 }
3261 
3269  const float radius,
3270  Object *ob,
3271  uint base_index,
3272  BMFace *f,
3273  const float cageco[3])
3274 {
3275  const float radius_sq = radius * radius;
3276  ListBase *list;
3277  Ref *ref;
3278  float sco[2];
3279  float dis_sq;
3280  int c = 0;
3281 
3282  knife_project_v2(kcd, cageco, sco);
3283 
3284  list = knife_get_face_kedges(kcd, ob, base_index, f);
3285  for (ref = list->first; ref; ref = ref->next) {
3286  KnifeEdge *kfe = ref->ref;
3287  int i;
3288 
3289  if (kfe->is_invalid) {
3290  continue;
3291  }
3292 
3293  for (i = 0; i < 2; i++) {
3294  KnifeVert *kfv = i ? kfe->v2 : kfe->v1;
3295  float kfv_sco[2];
3296 
3297  if (kfv->is_invalid) {
3298  continue;
3299  }
3300 
3301  knife_project_v2(kcd, kfv->cageco, kfv_sco);
3302 
3303  dis_sq = len_squared_v2v2(kfv_sco, sco);
3304  if (dis_sq < radius_sq) {
3305  if (RV3D_CLIPPING_ENABLED(kcd->vc.v3d, kcd->vc.rv3d)) {
3306  if (ED_view3d_clipping_test(kcd->vc.rv3d, kfv->cageco, false) == 0) {
3307  c++;
3308  }
3309  }
3310  else {
3311  c++;
3312  }
3313  }
3314  }
3315  }
3316 
3317  return c;
3318 }
3319 
3326 static float knife_snap_size(KnifeTool_OpData *kcd, float maxsize)
3327 {
3328  BLI_assert(kcd->is_interactive == true);
3329  int density = 0;
3330 
3331  if (!kcd->curr.is_space) {
3333  kcd, maxsize * 2.0f, kcd->curr.ob, kcd->curr.base_index, kcd->curr.bmface, kcd->curr.cage);
3334  }
3335 
3336  return density ? min_ff(maxsize / ((float)density * 0.5f), maxsize) : maxsize;
3337 }
3338 
3339 /* Snap to edge when in a constrained mode.
3340  * Returns 'lambda' calculated (in screen-space). */
3342  const float sco[3],
3343  const float kfv1_sco[2],
3344  const float kfv2_sco[2],
3345  float *r_dist_sq,
3346  float *r_lambda)
3347 {
3348  /* If snapping, check we're in bounds. */
3349  float sco_snap[2];
3350  isect_line_line_v2_point(kfv1_sco, kfv2_sco, kcd->prev.mval, kcd->curr.mval, sco_snap);
3351  float lambda = line_point_factor_v2(sco_snap, kfv1_sco, kfv2_sco);
3352 
3353  /* Be strict when constrained within edge. */
3354  if ((lambda < 0.0f - KNIFE_FLT_EPSBIG) || (lambda > 1.0f + KNIFE_FLT_EPSBIG)) {
3355  return false;
3356  }
3357 
3358  float dis_sq = len_squared_v2v2(sco, sco_snap);
3359  if (dis_sq < *r_dist_sq) {
3360  *r_dist_sq = dis_sq;
3361  *r_lambda = lambda;
3362  return true;
3363  }
3364  return false;
3365 }
3366 
3367 /* Use when lambda is in screen-space. */
3369  float r_co[3],
3370  const float v1[3],
3371  const float v2[3],
3372  float lambda_ss)
3373 {
3374  if (kcd->is_ortho) {
3375  interp_v3_v3v3(r_co, v1, v2, lambda_ss);
3376  }
3377  else {
3378  /* Transform into screen-space, interp, then transform back. */
3379  float v1_ss[3], v2_ss[3];
3380 
3381  mul_v3_project_m4_v3(v1_ss, (float(*)[4])kcd->vc.rv3d->persmat, v1);
3382  mul_v3_project_m4_v3(v2_ss, (float(*)[4])kcd->vc.rv3d->persmat, v2);
3383 
3384  interp_v3_v3v3(r_co, v1_ss, v2_ss, lambda_ss);
3385 
3386  mul_project_m4_v3((float(*)[4])kcd->vc.rv3d->persinv, r_co);
3387  }
3388 }
3389 
3390 /* p is closest point on edge to the mouse cursor. */
3392  KnifeTool_OpData *kcd, Object *ob, uint base_index, BMFace *f, float p[3], float cagep[3])
3393 {
3394  float sco[2];
3395  float maxdist;
3396 
3397  if (kcd->is_interactive) {
3398  maxdist = knife_snap_size(kcd, kcd->ethresh);
3399 
3400  if (kcd->ignore_vert_snapping) {
3401  maxdist *= 0.5f;
3402  }
3403  }
3404  else {
3405  maxdist = KNIFE_FLT_EPS;
3406  }
3407 
3408  const float maxdist_sq = maxdist * maxdist;
3409  KnifeEdge *cure = NULL;
3410  float cur_cagep[3];
3411  ListBase *list;
3412  Ref *ref;
3413  float dis_sq, curdis_sq = maxdist_sq;
3414 
3415  knife_project_v2(kcd, cagep, sco);
3416 
3417  /* Look through all edges associated with this face. */
3418  list = knife_get_face_kedges(kcd, ob, base_index, f);
3419  for (ref = list->first; ref; ref = ref->next) {
3420  KnifeEdge *kfe = ref->ref;
3421  float kfv1_sco[2], kfv2_sco[2], test_cagep[3];
3422  float lambda;
3423 
3424  if (kfe->is_invalid) {
3425  continue;
3426  }
3427 
3428  /* Project edge vertices into screen space. */
3429  knife_project_v2(kcd, kfe->v1->cageco, kfv1_sco);
3430  knife_project_v2(kcd, kfe->v2->cageco, kfv2_sco);
3431 
3432  /* Check if we're close enough and calculate 'lambda'. */
3433  /* In constrained mode calculate lambda differently, unless constrained along kcd->prev.edge */
3434  if ((kcd->is_angle_snapping || kcd->axis_constrained) && (kfe != kcd->prev.edge) &&
3435  (kcd->mode == MODE_DRAGGING)) {
3436  dis_sq = curdis_sq;
3437  if (!knife_snap_edge_constrained(kcd, sco, kfv1_sco, kfv2_sco, &dis_sq, &lambda)) {
3438  continue;
3439  }
3440  }
3441  else {
3442  dis_sq = dist_squared_to_line_segment_v2(sco, kfv1_sco, kfv2_sco);
3443  if (dis_sq < curdis_sq) {
3444  lambda = line_point_factor_v2(sco, kfv1_sco, kfv2_sco);
3445  }
3446  else {
3447  continue;
3448  }
3449  }
3450 
3451  /* Now we have 'lambda' calculated (in screen-space). */
3452  knife_interp_v3_v3v3(kcd, test_cagep, kfe->v1->cageco, kfe->v2->cageco, lambda);
3453 
3454  if (RV3D_CLIPPING_ENABLED(kcd->vc.v3d, kcd->vc.rv3d)) {
3455  /* Check we're in the view */
3456  if (ED_view3d_clipping_test(kcd->vc.rv3d, test_cagep, false)) {
3457  continue;
3458  }
3459  }
3460 
3461  cure = kfe;
3462  curdis_sq = dis_sq;
3463  copy_v3_v3(cur_cagep, test_cagep);
3464  }
3465 
3466  if (cure && !kcd->ignore_edge_snapping) {
3467  KnifeVert *edgesnap = NULL;
3468 
3469  if (kcd->snap_midpoints) {
3470  mid_v3_v3v3(p, cure->v1->co, cure->v2->co);
3471  mid_v3_v3v3(cagep, cure->v1->cageco, cure->v2->cageco);
3472  }
3473  else {
3474  float lambda = line_point_factor_v3(cur_cagep, cure->v1->cageco, cure->v2->cageco);
3475  copy_v3_v3(cagep, cur_cagep);
3476  interp_v3_v3v3(p, cure->v1->co, cure->v2->co, lambda);
3477  }
3478 
3479  /* Update mouse coordinates to the snapped-to edge's screen coordinates
3480  * this is important for angle snap, which uses the previous mouse position. */
3481  edgesnap = new_knife_vert(kcd, p, cagep);
3482  edgesnap->ob = ob;
3483  edgesnap->base_index = base_index;
3484  knife_project_v2(kcd, edgesnap->cageco, kcd->curr.mval);
3485  }
3486 
3487  return cure;
3488 }
3489 
3490 /* Find a vertex near the mouse cursor, if it exists. */
3492  KnifeEdge *kfe,
3493  float p[3],
3494  float cagep[3])
3495 {
3496  float sco[2];
3497  float maxdist;
3498 
3499  if (kcd->is_interactive) {
3500  maxdist = knife_snap_size(kcd, kcd->vthresh);
3501  if (kcd->ignore_vert_snapping) {
3502  maxdist *= 0.5f;
3503  }
3504  }
3505  else {
3506  maxdist = KNIFE_FLT_EPS;
3507  }
3508 
3509  const float maxdist_sq = maxdist * maxdist;
3510  KnifeVert *curv = NULL;
3511  float cur_kfv_sco[2];
3512  float dis_sq, curdis_sq = FLT_MAX;
3513 
3514  knife_project_v2(kcd, cagep, sco);
3515 
3516  for (int i = 0; i < 2; i++) {
3517  KnifeVert *kfv = i ? kfe->v2 : kfe->v1;
3518  float kfv_sco[2];
3519 
3520  knife_project_v2(kcd, kfv->cageco, kfv_sco);
3521 
3522  /* Be strict when in a constrained mode, the vertex needs to be very close to the cut line,
3523  * or we ignore. */
3524  if ((kcd->is_angle_snapping || kcd->axis_constrained) && (kcd->mode == MODE_DRAGGING)) {
3525  if (dist_squared_to_line_segment_v2(kfv_sco, kcd->prev.mval, kcd->curr.mval) >
3526  KNIFE_FLT_EPSBIG) {
3527  continue;
3528  }
3529  }
3530 
3531  dis_sq = len_squared_v2v2(kfv_sco, sco);
3532  if (dis_sq < curdis_sq && dis_sq < maxdist_sq) {
3533  if (!RV3D_CLIPPING_ENABLED(kcd->vc.v3d, kcd->vc.rv3d) ||
3534  !ED_view3d_clipping_test(kcd->vc.rv3d, kfv->cageco, false)) {
3535  curv = kfv;
3536  curdis_sq = dis_sq;
3537  copy_v2_v2(cur_kfv_sco, kfv_sco);
3538  }
3539  }
3540  }
3541 
3542  if (curv && !kcd->ignore_vert_snapping) {
3543  copy_v3_v3(p, curv->co);
3544  copy_v3_v3(cagep, curv->cageco);
3545 
3546  /* Update mouse coordinates to the snapped-to vertex's screen coordinates
3547  * this is important for angle snap, which uses the previous mouse position. */
3548  copy_v2_v2(kcd->curr.mval, cur_kfv_sco);
3549  }
3550 
3551  return curv;
3552 }
3553 
3557 static float snap_v2_angle(float r[2], const float v[2], const float v_ref[2], float angle_snap)
3558 {
3559  float m2[2][2];
3560  float v_unit[2];
3561  float angle, angle_delta;
3562 
3563  BLI_ASSERT_UNIT_V2(v_ref);
3564 
3565  normalize_v2_v2(v_unit, v);
3566  angle = angle_signed_v2v2(v_unit, v_ref);
3567  angle_delta = (roundf(angle / angle_snap) * angle_snap) - angle;
3568  angle_to_mat2(m2, angle_delta);
3569 
3570  mul_v2_m2v2(r, m2, v);
3571  return angle + angle_delta;
3572 }
3573 
3574 /* Update both kcd->curr.mval and kcd->mval to snap to required angle. */
3576 {
3577  const float dvec_ref[2] = {1.0f, 0.0f};
3578  float dvec[2], dvec_snap[2];
3579 
3580  float snap_step;
3581  /* Currently user can input any float between 0 and 180. */
3584  snap_step = DEG2RADF(kcd->angle_snapping_increment);
3585  }
3586  else {
3588  }
3589 
3590  sub_v2_v2v2(dvec, kcd->curr.mval, kcd->prev.mval);
3591  if (is_zero_v2(dvec)) {
3592  return false;
3593  }
3594 
3595  kcd->angle = snap_v2_angle(dvec_snap, dvec, dvec_ref, snap_step);
3596 
3597  add_v2_v2v2(kcd->curr.mval, kcd->prev.mval, dvec_snap);
3598 
3599  copy_v2_v2(kcd->mval, kcd->curr.mval);
3600 
3601  return true;
3602 }
3603 
3607 static float snap_v3_angle_plane(
3608  float r[3], const float v[3], const float v_ref[3], const float plane_no[3], float snap_step)
3609 {
3610  /* Calculate angle between current cut vector and reference vector. */
3611  float angle, angle_delta;
3612  angle = angle_signed_on_axis_v3v3_v3(v, v_ref, plane_no);
3613  /* Use this to calculate the angle to rotate by based on snap_step. */
3614  angle_delta = (roundf(angle / snap_step) * snap_step) - angle;
3615 
3616  /* Snap to angle. */
3617  rotate_v3_v3v3fl(r, v, plane_no, angle_delta);
3618  return angle + angle_delta;
3619 }
3620 
3621 /* Snap to required angle along the plane of the face nearest to kcd->prev. */
3623 {
3624  Ref *ref;
3625  KnifeEdge *kfe;
3626  KnifeVert *kfv;
3627  BMFace *f;
3628  float refv[3];
3629 
3630  /* Ray for kcd->curr. */
3631  float curr_origin[3];
3632  float curr_origin_ofs[3];
3633  float curr_ray[3], curr_ray_normal[3];
3634  float curr_co[3], curr_cage[3]; /* Unused. */
3635 
3636  float plane[4];
3637  float ray_hit[3];
3638  float lambda;
3639 
3640  knife_input_ray_segment(kcd, kcd->curr.mval, 1.0f, curr_origin, curr_origin_ofs);
3641  sub_v3_v3v3(curr_ray, curr_origin_ofs, curr_origin);
3642  normalize_v3_v3(curr_ray_normal, curr_ray);
3643 
3644  BMFace *fcurr = knife_bvh_raycast(
3645  kcd, curr_origin, curr_ray_normal, 0.0f, NULL, curr_co, curr_cage, NULL);
3646 
3647  if (!fcurr) {
3648  return false;
3649  }
3650 
3651  /* Calculate a reference vector using previous cut segment, edge or vertex.
3652  * If none exists then exit. */
3653  if (kcd->prev.vert) {
3654  int count = 0;
3655  for (ref = kcd->prev.vert->edges.first; ref; ref = ref->next) {
3656  kfe = ((KnifeEdge *)(ref->ref));
3657  if (kfe->is_invalid) {
3658  continue;
3659  }
3660  if (kfe->e) {
3661  if (!BM_edge_in_face(kfe->e, fcurr)) {
3662  continue;
3663  }
3664  }
3665  if (count == kcd->snap_edge) {
3666  kfv = compare_v3v3(kfe->v1->cageco, kcd->prev.cage, KNIFE_FLT_EPSBIG) ? kfe->v2 : kfe->v1;
3667  sub_v3_v3v3(refv, kfv->cageco, kcd->prev.cage);
3668  kcd->snap_ref_edge = kfe;
3669  break;
3670  }
3671  count++;
3672  }
3673  }
3674  else if (kcd->prev.edge) {
3675  kfv = compare_v3v3(kcd->prev.edge->v1->cageco, kcd->prev.cage, KNIFE_FLT_EPSBIG) ?
3676  kcd->prev.edge->v2 :
3677  kcd->prev.edge->v1;
3678  sub_v3_v3v3(refv, kfv->cageco, kcd->prev.cage);
3679  kcd->snap_ref_edge = kcd->prev.edge;
3680  }
3681  else {
3682  return false;
3683  }
3684 
3685  /* Choose best face for plane. */
3686  BMFace *fprev = NULL;
3687  if (kcd->prev.vert && kcd->prev.vert->v) {
3688  for (ref = kcd->prev.vert->faces.first; ref; ref = ref->next) {
3689  f = ((BMFace *)(ref->ref));
3690  if (f == fcurr) {
3691  fprev = f;
3692  }
3693  }
3694  }
3695  else if (kcd->prev.edge) {
3696  for (ref = kcd->prev.edge->faces.first; ref; ref = ref->next) {
3697  f = ((BMFace *)(ref->ref));
3698  if (f == fcurr) {
3699  fprev = f;
3700  }
3701  }
3702  }
3703  else {
3704  /* Cut segment was started in a face. */
3705  float prev_origin[3];
3706  float prev_origin_ofs[3];
3707  float prev_ray[3], prev_ray_normal[3];
3708  float prev_co[3], prev_cage[3]; /* Unused. */
3709 
3710  knife_input_ray_segment(kcd, kcd->prev.mval, 1.0f, prev_origin, prev_origin_ofs);
3711 
3712  sub_v3_v3v3(prev_ray, prev_origin_ofs, prev_origin);
3713  normalize_v3_v3(prev_ray_normal, prev_ray);
3714 
3715  /* kcd->prev.face is usually not set. */
3716  fprev = knife_bvh_raycast(
3717  kcd, prev_origin, prev_ray_normal, 0.0f, NULL, prev_co, prev_cage, NULL);
3718  }
3719 
3720  if (!fprev || fprev != fcurr) {
3721  return false;
3722  }
3723 
3724  /* Use normal global direction. */
3725  float no_global[3];
3726  copy_v3_v3(no_global, fprev->no);
3727  mul_transposed_mat3_m4_v3(kcd->curr.ob->imat, no_global);
3728  normalize_v3(no_global);
3729 
3730  plane_from_point_normal_v3(plane, kcd->prev.cage, no_global);
3731 
3732  if (isect_ray_plane_v3(curr_origin, curr_ray_normal, plane, &lambda, false)) {
3733  madd_v3_v3v3fl(ray_hit, curr_origin, curr_ray_normal, lambda);
3734 
3735  /* Calculate snap step. */
3736  float snap_step;
3739  snap_step = DEG2RADF(kcd->angle_snapping_increment);
3740  }
3741  else {
3743  }
3744 
3745  float v1[3];
3746  float v2[3];
3747  float rotated_vec[3];
3748  /* Maybe check for vectors being zero here? */
3749  sub_v3_v3v3(v1, ray_hit, kcd->prev.cage);
3750  copy_v3_v3(v2, refv);
3751  kcd->angle = snap_v3_angle_plane(rotated_vec, v1, v2, no_global, snap_step);
3752  add_v3_v3(rotated_vec, kcd->prev.cage);
3753 
3754  knife_project_v2(kcd, rotated_vec, kcd->curr.mval);
3755  copy_v2_v2(kcd->mval, kcd->curr.mval);
3756  return true;
3757  }
3758  return false;
3759 }
3760 
3762 {
3763  Ref *ref;
3764  KnifeEdge *kfe;
3765 
3766  /* Ray for kcd->curr. */
3767  float curr_origin[3];
3768  float curr_origin_ofs[3];
3769  float curr_ray[3], curr_ray_normal[3];
3770  float curr_co[3], curr_cage[3]; /* Unused. */
3771 
3772  knife_input_ray_segment(kcd, kcd->curr.mval, 1.0f, curr_origin, curr_origin_ofs);
3773  sub_v3_v3v3(curr_ray, curr_origin_ofs, curr_origin);
3774  normalize_v3_v3(curr_ray_normal, curr_ray);
3775 
3776  BMFace *fcurr = knife_bvh_raycast(
3777  kcd, curr_origin, curr_ray_normal, 0.0f, NULL, curr_co, curr_cage, NULL);
3778 
3779  int count = 0;
3780 
3781  if (!fcurr) {
3782  return count;
3783  }
3784 
3785  if (kcd->prev.vert) {
3786  for (ref = kcd->prev.vert->edges.first; ref; ref = ref->next) {
3787  kfe = ((KnifeEdge *)(ref->ref));
3788  if (kfe->is_invalid) {
3789  continue;
3790  }
3791  if (kfe->e) {
3792  if (!BM_edge_in_face(kfe->e, fcurr)) {
3793  continue;
3794  }
3795  }
3796  count++;
3797  }
3798  }
3799  else if (kcd->prev.edge) {
3800  return 1;
3801  }
3802  return count;
3803 }
3804 
3805 /* Reset the snapping angle num input. */
3807 {
3808  kcd->num.val[0] = 0;
3809  while (kcd->num.str_cur > 0) {
3810  kcd->num.str[kcd->num.str_cur - 1] = '\0';
3811  kcd->num.str_cur--;
3812  }
3813 }
3820 {
3821  /* Obtain current mouse position in world space. */
3822  float curr_cage_adjusted[3];
3824  kcd->vc.v3d, kcd->region, kcd->prev.cage, kcd->curr.mval, curr_cage_adjusted);
3825 
3826  /* Constrain axes. */
3827  Scene *scene = kcd->scene;
3828  ViewLayer *view_layer = kcd->vc.view_layer;
3829  Object *obedit = (kcd->prev.ob) ? kcd->prev.ob : kcd->vc.obedit;
3830  RegionView3D *rv3d = kcd->region->regiondata;
3831  const short scene_orientation = BKE_scene_orientation_get_index(scene, SCE_ORIENT_DEFAULT);
3832  /* Scene orientation takes priority. */
3833  const short orientation_type = scene_orientation ? scene_orientation :
3834  kcd->constrain_axis_mode - 1;
3835  const int pivot_point = scene->toolsettings->transform_pivot_point;
3836  float mat[3][3];
3838  scene, view_layer, kcd->vc.v3d, rv3d, obedit, obedit, orientation_type, pivot_point, mat);
3839 
3840  /* Apply orientation matrix (can be simplified?). */
3841  float co[3][3];
3842  copy_v3_v3(co[0], kcd->prev.cage);
3843  copy_v3_v3(co[2], curr_cage_adjusted);
3844  invert_m3(mat);
3845  mul_m3_m3_pre(co, mat);
3846  for (int i = 0; i <= 2; i++) {
3847  if ((kcd->constrain_axis - 1) != i) {
3848  /* kcd->curr_cage_adjusted[i] = prev_cage_adjusted[i]; */
3849  co[2][i] = co[0][i];
3850  }
3851  }
3852  invert_m3(mat);
3853  mul_m3_m3_pre(co, mat);
3854  copy_v3_v3(kcd->prev.cage, co[0]);
3855  copy_v3_v3(curr_cage_adjusted, co[2]);
3856 
3857  /* Set mval to closest point on constrained line in screen space. */
3858  float curr_screenspace[2];
3859  float prev_screenspace[2];
3860  knife_project_v2(kcd, curr_cage_adjusted, curr_screenspace);
3861  knife_project_v2(kcd, kcd->prev.cage, prev_screenspace);
3862  float intersection[2];
3863  if (closest_to_line_v2(intersection, kcd->curr.mval, prev_screenspace, curr_screenspace)) {
3865  }
3866  else {
3867  copy_v2_v2(kcd->curr.mval, curr_screenspace);
3868  }
3869  copy_v2_v2(kcd->mval, kcd->curr.mval);
3870 }
3871 
3880 static bool knife_snap_update_from_mval(KnifeTool_OpData *kcd, const float mval[2])
3881 {
3882  knife_pos_data_clear(&kcd->curr);
3883  copy_v2_v2(kcd->curr.mval, mval);
3884 
3885  /* view matrix may have changed, reproject */
3886  knife_project_v2(kcd, kcd->prev.cage, kcd->prev.mval);
3887 
3888  kcd->is_angle_snapping = false;
3889  if (kcd->mode == MODE_DRAGGING) {
3890  if (kcd->angle_snapping) {
3893  }
3896  if (kcd->is_angle_snapping) {
3898  }
3899  }
3900  }
3901 
3902  if (kcd->axis_constrained) {
3903  knife_constrain_axis(kcd);
3904  }
3905  }
3906 
3907  {
3908  kcd->curr.ob = kcd->vc.obedit;
3909  kcd->curr.bmface = knife_find_closest_face(kcd,
3910  &kcd->curr.ob,
3911  &kcd->curr.base_index,
3912  &kcd->curr.is_space,
3913  kcd->curr.co,
3914  kcd->curr.cage);
3915 
3916  if (kcd->curr.bmface) {
3918  kcd, kcd->curr.ob, kcd->curr.base_index, kcd->curr.bmface, kcd->curr.co, kcd->curr.cage);
3919  }
3920 
3921  if (kcd->curr.edge) {
3923  kcd, kcd->curr.edge, kcd->curr.co, kcd->curr.cage);
3924 
3925  if (kcd->ignore_edge_snapping) {
3926  kcd->curr.edge = NULL;
3927  }
3928  }
3929  }
3930 
3931  return kcd->curr.vert || kcd->curr.edge || (kcd->curr.bmface && !kcd->curr.is_space);
3932 }
3933 
3941 {
3942  Ref *ref;
3943  KnifeEdge *kfe, *newkfe;
3944  KnifeEdge *lastkfe = NULL;
3945  KnifeVert *v1, *v2;
3946  KnifeUndoFrame *undo;
3947  BLI_mempool_iter iterkfe;
3948 
3949  undo = BLI_stack_peek(kcd->undostack);
3950 
3951  /* Undo edge splitting. */
3952  for (int i = 0; i < undo->splits; i++) {
3953  BLI_stack_pop(kcd->splitstack, &newkfe);
3954  BLI_stack_pop(kcd->splitstack, &kfe);
3955  knife_join_edge(newkfe, kfe);
3956  }
3957 
3958  for (int i = 0; i < undo->cuts; i++) {
3959 
3960  BLI_mempool_iternew(kcd->kedges, &iterkfe);
3961  for (kfe = BLI_mempool_iterstep(&iterkfe); kfe; kfe = BLI_mempool_iterstep(&iterkfe)) {
3962  if (!kfe->is_cut || kfe->is_invalid || kfe->splits) {
3963  continue;
3964  }
3965  lastkfe = kfe;
3966  }
3967 
3968  if (lastkfe) {
3969  lastkfe->is_invalid = true;
3970 
3971  /* TODO: Are they always guaranteed to be in this order? */
3972  v1 = lastkfe->v1;
3973  v2 = lastkfe->v2;
3974 
3975  /* Only remove first vertex if it is the start segment of the cut. */
3976  if (!v1->is_invalid && !v1->is_splitting) {
3977  v1->is_invalid = true;
3978  /* If the first vertex is touching any other cut edges don't remove it. */
3979  for (ref = v1->edges.first; ref; ref = ref->next) {
3980  kfe = ref->ref;
3981  if (kfe->is_cut && !kfe->is_invalid) {
3982  v1->is_invalid = false;
3983  break;
3984  }
3985  }
3986  }
3987 
3988  /* Only remove second vertex if it is the end segment of the cut. */
3989  if (!v2->is_invalid && !v2->is_splitting) {
3990  v2->is_invalid = true;
3991  /* If the second vertex is touching any other cut edges don't remove it. */
3992  for (ref = v2->edges.first; ref; ref = ref->next) {
3993  kfe = ref->ref;
3994  if (kfe->is_cut && !kfe->is_invalid) {
3995  v2->is_invalid = false;
3996  break;
3997  }
3998  }
3999  }
4000  }
4001  }
4002 
4003  if (kcd->mode == MODE_DRAGGING) {
4004  /* Restore kcd->prev. */
4005  kcd->prev = undo->pos;
4006  }
4007 
4008  /* Restore data for distance and angle measurements. */
4009  kcd->mdata = undo->mdata;
4010 
4012 }
4013 
4016 /* -------------------------------------------------------------------- */
4021  Object *ob,
4022  uint base_index,
4023  bool use_tri_indices)
4024 {
4025 
4026  Scene *scene_eval = (Scene *)DEG_get_evaluated_id(kcd->vc.depsgraph, &kcd->scene->id);
4027  Object *obedit_eval = (Object *)DEG_get_evaluated_id(kcd->vc.depsgraph, &ob->id);
4028  BMEditMesh *em_eval = BKE_editmesh_from_object(obedit_eval);
4029 
4031 
4032  KnifeObjectInfo *obinfo = &kcd->objects_info[base_index];
4033  obinfo->em = em_eval;
4034  obinfo->cagecos = (const float(*)[3])BKE_editmesh_vert_coords_alloc(
4035  kcd->vc.depsgraph, em_eval, scene_eval, obedit_eval, NULL);
4036 
4037  if (use_tri_indices) {
4038  BMLoop *(*looptris)[3] = em_eval->looptris;
4039  int(*tri_indices)[3] = MEM_mallocN(sizeof(int[3]) * em_eval->tottri, __func__);
4040  for (int i = 0; i < em_eval->tottri; i++) {
4041  BMLoop **tri = looptris[i];
4042  tri_indices[i][0] = BM_elem_index_get(tri[0]->v);
4043  tri_indices[i][1] = BM_elem_index_get(tri[1]->v);
4044  tri_indices[i][2] = BM_elem_index_get(tri[2]->v);
4045  }
4046  obinfo->tri_indices = tri_indices;
4047  }
4048 }
4049 
4050 static void knifetool_free_obinfo(KnifeTool_OpData *kcd, uint base_index)
4051 {
4052  MEM_SAFE_FREE(kcd->objects_info[base_index].cagecos);
4053  MEM_SAFE_FREE(kcd->objects_info[base_index].tri_indices);
4054 }
4055 
4058 /* -------------------------------------------------------------------- */
4062 static void knife_init_colors(KnifeColors *colors)
4063 {
4064  /* Possible BMESH_TODO: add explicit themes or calculate these by
4065  * figuring out contrasting colors with grid / edges / verts
4066  * a la UI_make_axis_color. */
4072  colors->curpoint_a[3] = 102;
4075  colors->point_a[3] = 102;
4076 
4081 }
4082 
4083 /* called when modal loop selection gets set up... */
4084 static void knifetool_init(ViewContext *vc,
4085  KnifeTool_OpData *kcd,
4086  Object **objects,
4087  const int objects_len,
4088  const bool only_select,
4089  const bool cut_through,
4090  const bool xray,
4091  const int visible_measurements,
4092  const int angle_snapping,
4093  const float angle_snapping_increment,
4094  const bool is_interactive)
4095 {
4096  /* Needed so multiple non-interactive cuts (also called knife-project)
4097  * doesn't access indices of loops that were created by cutting, see: T97153. */
4098  bool use_tri_indices = !is_interactive;
4099 
4100  kcd->vc = *vc;
4101 
4102  Scene *scene = vc->scene;
4103 
4104  /* Assign the drawing handle for drawing preview line... */
4105  kcd->scene = scene;
4106  kcd->region = vc->region;
4107 
4108  if (objects) {
4109  kcd->objects = objects;
4110  kcd->objects_len = objects_len;
4111  kcd->objects_free = false;
4112  }
4113  else {
4115  vc->view_layer, vc->v3d, &kcd->objects_len);
4116  kcd->objects_free = true;
4117  }
4118 
4119  Object *ob;
4120  BMEditMesh *em;
4121  kcd->objects_info = MEM_callocN(sizeof(*kcd->objects_info) * kcd->objects_len, "knife cagecos");
4122  for (uint b = 0; b < kcd->objects_len; b++) {
4123  ob = kcd->objects[b];
4124  em = BKE_editmesh_from_object(ob);
4125  knifetool_init_obinfo(kcd, ob, b, use_tri_indices);
4126 
4127  /* Can't usefully select resulting edges in face mode. */
4128  kcd->select_result = (em->selectmode != SCE_SELECT_FACE);
4129  }
4130  knife_bvh_init(kcd);
4131 
4132  /* Cut all the way through the mesh if use_occlude_geometry button not pushed. */
4133  kcd->is_interactive = is_interactive;
4134  kcd->cut_through = cut_through;
4135  kcd->only_select = only_select;
4136  kcd->depth_test = xray;
4137  kcd->dist_angle_mode = visible_measurements;
4139  kcd->angle_snapping_mode = angle_snapping;
4141  kcd->angle_snapping_increment = angle_snapping_increment;
4142 
4143  kcd->arena = BLI_memarena_new(MEM_SIZE_OPTIMAL(1 << 15), "knife");
4144 #ifdef USE_NET_ISLAND_CONNECT
4145  kcd->edgenet.arena = BLI_memarena_new(MEM_SIZE_OPTIMAL(1 << 15), __func__);
4146 #endif
4147  kcd->edgenet.edge_visit = BLI_gset_ptr_new(__func__);
4148 
4149  kcd->vthresh = KMAXDIST - 1;
4150  kcd->ethresh = KMAXDIST;
4151 
4152  knife_recalc_ortho(kcd);
4153 
4155 
4156  kcd->refs = BLI_mempool_create(sizeof(Ref), 0, 2048, 0);
4157  kcd->kverts = BLI_mempool_create(sizeof(KnifeVert), 0, 512, BLI_MEMPOOL_ALLOW_ITER);
4158  kcd->kedges = BLI_mempool_create(sizeof(KnifeEdge), 0, 512, BLI_MEMPOOL_ALLOW_ITER);
4159 
4160  kcd->undostack = BLI_stack_new(sizeof(KnifeUndoFrame), "knife undostack");
4161  kcd->splitstack = BLI_stack_new(sizeof(KnifeEdge *), "knife splitstack");
4162 
4163  kcd->origedgemap = BLI_ghash_ptr_new("knife origedgemap");
4164  kcd->origvertmap = BLI_ghash_ptr_new("knife origvertmap");
4165  kcd->kedgefacemap = BLI_ghash_ptr_new("knife kedgefacemap");
4166  kcd->facetrimap = BLI_ghash_ptr_new("knife facetrimap");
4167 
4168  knife_pos_data_clear(&kcd->curr);
4169  knife_pos_data_clear(&kcd->prev);
4170 
4171  if (is_interactive) {
4174 
4175  knife_init_colors(&kcd->colors);
4176  }
4177 
4178  kcd->no_cuts = true;
4179 
4180  kcd->axis_string[0] = ' ';
4181  kcd->axis_string[1] = '\0';
4182 
4183  /* Initialize number input handling for angle snapping. */
4184  initNumInput(&kcd->num);
4185  kcd->num.idx_max = 0;
4186  kcd->num.val_flag[0] |= NUM_NO_NEGATIVE;
4187  kcd->num.unit_sys = scene->unit.system;
4188  kcd->num.unit_type[0] = B_UNIT_NONE;
4189 }
4190 
4191 /* called when modal loop selection is done... */
4193 {
4194  if (!kcd) {
4195  return;
4196  }
4197 
4198  if (kcd->is_interactive) {
4200 
4201  /* Deactivate the extra drawing stuff in 3D-View. */
4203  }
4204 
4205  /* Free the custom data. */
4206  BLI_mempool_destroy(kcd->refs);
4209 
4210  BLI_stack_free(kcd->undostack);
4211  BLI_stack_free(kcd->splitstack);
4212 
4217 
4218  BLI_memarena_free(kcd->arena);
4219 #ifdef USE_NET_ISLAND_CONNECT
4221 #endif
4223 
4224  /* Tag for redraw. */
4226 
4227  /* Knife BVH cleanup. */
4228  for (int i = 0; i < kcd->objects_len; i++) {
4229  knifetool_free_obinfo(kcd, i);
4230  }
4231  MEM_freeN((void *)kcd->objects_info);
4232  knife_bvh_free(kcd);
4233 
4234  /* Line-hits cleanup. */
4235  if (kcd->linehits) {
4236  MEM_freeN(kcd->linehits);
4237  }
4238 
4239  /* Free object bases. */
4240  if (kcd->objects_free) {
4241  MEM_freeN(kcd->objects);
4242  }
4243 
4244  /* Destroy kcd itself. */
4245  MEM_freeN(kcd);
4246 }
4247 
4248 static void knifetool_exit(wmOperator *op)
4249 {
4250  KnifeTool_OpData *kcd = op->customdata;
4251  knifetool_exit_ex(kcd);
4252  op->customdata = NULL;
4253 }
4254 
4257 /* -------------------------------------------------------------------- */
4263 {
4264  /* If no hits are found this would normally default to (0, 0, 0) so instead
4265  * get a point at the mouse ray closest to the previous point.
4266  * Note that drawing lines in `free-space` isn't properly supported
4267  * but there's no guarantee (0, 0, 0) has any geometry either - campbell */
4268  if (!knife_snap_update_from_mval(kcd, kcd->mval)) {
4269  float origin[3];
4270  float origin_ofs[3];
4271 
4272  knife_input_ray_segment(kcd, kcd->curr.mval, 1.0f, origin, origin_ofs);
4273 
4274  if (!isect_line_plane_v3(
4275  kcd->curr.cage, origin, origin_ofs, kcd->prev.cage, kcd->vc.rv3d->viewinv[2])) {
4276  copy_v3_v3(kcd->curr.cage, kcd->prev.cage);
4277 
4278  /* Should never fail! */
4279  BLI_assert(0);
4280  }
4281  }
4282 
4283  if (kcd->mode == MODE_DRAGGING) {
4284  knife_find_line_hits(kcd);
4285  }
4286  return 1;
4287 }
4288 
4289 static void knifetool_update_mval(KnifeTool_OpData *kcd, const float mval[2])
4290 {
4291  knife_recalc_ortho(kcd);
4292  copy_v2_v2(kcd->mval, mval);
4293 
4294  if (knife_update_active(kcd)) {
4296  }
4297 }
4298 
4299 static void knifetool_update_mval_i(KnifeTool_OpData *kcd, const int mval_i[2])
4300 {
4301  const float mval[2] = {UNPACK2(mval_i)};
4302  knifetool_update_mval(kcd, mval);
4303 }
4304 
4307 /* -------------------------------------------------------------------- */
4312 {
4313  knife_make_cuts(kcd, ob);
4314 }
4315 
4321 {
4324  EDBM_update(ob->data,
4325  &(const struct EDBMUpdate_Params){
4326  .calc_looptri = true,
4327  .calc_normals = true,
4328  .is_destructive = true,
4329  });
4330 }
4331 
4332 /* Called on tool confirmation. */
4334 {
4335  /* Separate pre/post passes are needed because `em->looptris` recalculation from the 'post' pass
4336  * causes causes triangle indices in #KnifeTool_OpData.bvh to get out of sync.
4337  * So perform all the cuts before doing any mesh recalculation, see: T101721. */
4338  for (uint b = 0; b < kcd->objects_len; b++) {
4339  Object *ob = kcd->objects[b];
4340  knifetool_finish_single_pre(kcd, ob);
4341  }
4342  for (uint b = 0; b < kcd->objects_len; b++) {
4343  Object *ob = kcd->objects[b];
4345  }
4346 }
4347 
4349 {
4350  KnifeTool_OpData *kcd = op->customdata;
4351  knifetool_finish_ex(kcd);
4352 }
4353 
4356 /* -------------------------------------------------------------------- */
4361 {
4362  /* this is just a wrapper around exit() */
4363  knifetool_exit(op);
4364 }
4365 
4367 {
4368  static const EnumPropertyItem modal_items[] = {
4369  {KNF_MODAL_CANCEL, "CANCEL", 0, "Cancel", ""},
4370  {KNF_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
4371  {KNF_MODAL_UNDO, "UNDO", 0, "Undo", ""},
4372  {KNF_MODAL_MIDPOINT_ON, "SNAP_MIDPOINTS_ON", 0, "Snap to Midpoints On", ""},
4373  {KNF_MODAL_MIDPOINT_OFF, "SNAP_MIDPOINTS_OFF", 0, "Snap to Midpoints Off", ""},
4374  {KNF_MODAL_IGNORE_SNAP_ON, "IGNORE_SNAP_ON", 0, "Ignore Snapping On", ""},
4375  {KNF_MODAL_IGNORE_SNAP_OFF, "IGNORE_SNAP_OFF", 0, "Ignore Snapping Off", ""},
4376  {KNF_MODAL_ANGLE_SNAP_TOGGLE, "ANGLE_SNAP_TOGGLE", 0, "Toggle Angle Snapping", ""},
4378  "CYCLE_ANGLE_SNAP_EDGE",
4379  0,
4380  "Cycle Angle Snapping Relative Edge",
4381  ""},
4382  {KNF_MODAL_CUT_THROUGH_TOGGLE, "CUT_THROUGH_TOGGLE", 0, "Toggle Cut Through", ""},
4384  "SHOW_DISTANCE_ANGLE_TOGGLE",
4385  0,
4386  "Toggle Distance and Angle Measurements",
4387  ""},
4388  {KNF_MODAL_DEPTH_TEST_TOGGLE, "DEPTH_TEST_TOGGLE", 0, "Toggle Depth Testing", ""},
4389  {KNF_MODAL_NEW_CUT, "NEW_CUT", 0, "End Current Cut", ""},
4390  {KNF_MODAL_ADD_CUT, "ADD_CUT", 0, "Add Cut", ""},
4391  {KNF_MODAL_ADD_CUT_CLOSED, "ADD_CUT_CLOSED", 0, "Add Cut Closed", ""},
4392  {KNF_MODAL_PANNING, "PANNING", 0, "Panning", ""},
4393  {KNF_MODAL_X_AXIS, "X_AXIS", 0, "X Axis Locking", ""},
4394  {KNF_MODAL_Y_AXIS, "Y_AXIS", 0, "Y Axis Locking", ""},
4395  {KNF_MODAL_Z_AXIS, "Z_AXIS", 0, "Z Axis Locking", ""},
4396  {0, NULL, 0, NULL, NULL},
4397  };
4398 
4399  wmKeyMap *keymap = WM_modalkeymap_find(keyconf, "Knife Tool Modal Map");
4400 
4401  /* This function is called for each spacetype, only needs to add map once. */
4402  if (keymap && keymap->modal_items) {
4403  return NULL;
4404  }
4405 
4406  keymap = WM_modalkeymap_ensure(keyconf, "Knife Tool Modal Map", modal_items);
4407 
4408  WM_modalkeymap_assign(keymap, "MESH_OT_knife_tool");
4409 
4410  return keymap;
4411 }
4412 
4413 /* Turn off angle snapping. */
4415 {
4417  kcd->angle_snapping = false;
4418  kcd->is_angle_snapping = false;
4419 }
4420 
4421 /* Turn off orientation locking. */
4423 {
4426  kcd->axis_constrained = false;
4427 }
4428 
4429 static int knifetool_modal(bContext *C, wmOperator *op, const wmEvent *event)
4430 {
4431  KnifeTool_OpData *kcd = op->customdata;
4432  bool do_refresh = false;
4433 
4434  if (!kcd->curr.ob || kcd->curr.ob->type != OB_MESH) {
4435  knifetool_exit(op);
4437  return OPERATOR_FINISHED;
4438  }
4439 
4440  kcd->region = kcd->vc.region;
4441 
4442  ED_view3d_init_mats_rv3d(kcd->curr.ob, kcd->vc.rv3d); /* Needed to initialize clipping. */
4443 
4444  if (kcd->mode == MODE_PANNING) {
4445  kcd->mode = kcd->prevmode;
4446  }
4447 
4448  bool handled = false;
4449  float snapping_increment_temp;
4450 
4451  if (kcd->angle_snapping) {
4452  if (kcd->num.str_cur >= 3 ||
4455  }
4456  knife_update_header(C, op, kcd); /* Update the angle multiple. */
4457  /* Modal numinput active, try to handle numeric inputs first... */
4458  if (event->val == KM_PRESS && hasNumInput(&kcd->num) && handleNumInput(C, &kcd->num, event)) {
4459  handled = true;
4460  applyNumInput(&kcd->num, &snapping_increment_temp);
4461  /* Restrict number key input to 0 - 180 degree range. */
4462  if (snapping_increment_temp > KNIFE_MIN_ANGLE_SNAPPING_INCREMENT &&
4463  snapping_increment_temp <= KNIFE_MAX_ANGLE_SNAPPING_INCREMENT) {
4464  kcd->angle_snapping_increment = snapping_increment_temp;
4465  }
4466  knife_update_active(kcd);
4467  knife_update_header(C, op, kcd);
4469  return OPERATOR_RUNNING_MODAL;
4470  }
4471  }
4472 
4473  /* Handle modal keymap. */
4474  if (event->type == EVT_MODAL_MAP) {
4475  switch (event->val) {
4476  case KNF_MODAL_CANCEL:
4477  /* finish */
4479 
4480  knifetool_exit(op);
4482 
4483  return OPERATOR_CANCELLED;
4484  case KNF_MODAL_CONFIRM: {
4485  const bool changed = (kcd->totkvert != 0);
4486  /* finish */
4488 
4489  knifetool_finish(op);
4490  knifetool_exit(op);
4492 
4493  /* Cancel to prevent undo push for empty cuts. */
4494  if (!changed) {
4495  return OPERATOR_CANCELLED;
4496  }
4497  return OPERATOR_FINISHED;
4498  }
4499  case KNF_MODAL_UNDO:
4500  if (BLI_stack_is_empty(kcd->undostack)) {
4502  knifetool_exit(op);
4504  return OPERATOR_CANCELLED;
4505  }
4506  knifetool_undo(kcd);
4507  knife_update_active(kcd);
4509  handled = true;
4510  break;
4511  case KNF_MODAL_MIDPOINT_ON:
4512  kcd->snap_midpoints = true;
4513 
4514  knife_recalc_ortho(kcd);
4515  knife_update_active(kcd);
4516  knife_update_header(C, op, kcd);
4518  do_refresh = true;
4519  handled = true;
4520  break;
4522  kcd->snap_midpoints = false;
4523 
4524  knife_recalc_ortho(kcd);
4525  knife_update_active(kcd);
4526  knife_update_header(C, op, kcd);
4528  do_refresh = true;
4529  handled = true;
4530  break;
4533  kcd->ignore_vert_snapping = kcd->ignore_edge_snapping = true;
4534  knife_update_header(C, op, kcd);
4535  do_refresh = true;
4536  handled = true;
4537  break;
4540  kcd->ignore_vert_snapping = kcd->ignore_edge_snapping = false;
4541  knife_update_header(C, op, kcd);
4542  do_refresh = true;
4543  handled = true;
4544  break;
4547  kcd->angle_snapping_mode++;
4548  kcd->snap_ref_edges_count = 0;
4549  kcd->snap_edge = 0;
4550  }
4551  else {
4553  }
4556  RNA_float_get(op->ptr, "angle_snapping_increment"));
4559  knife_update_active(kcd);
4560  knife_update_header(C, op, kcd);
4562  do_refresh = true;
4563  handled = true;
4564  break;
4567  if (kcd->snap_ref_edges_count) {
4568  kcd->snap_edge++;
4569  kcd->snap_edge %= kcd->snap_ref_edges_count;
4570  }
4571  }
4572  do_refresh = true;
4573  handled = true;
4574  break;
4576  kcd->cut_through = !kcd->cut_through;
4577  knife_update_header(C, op, kcd);
4578  do_refresh = true;
4579  handled = true;
4580  break;
4582  if (kcd->dist_angle_mode != KNF_MEASUREMENT_ANGLE) {
4583  kcd->dist_angle_mode++;
4584  }
4585  else {
4587  }
4589  knife_update_header(C, op, kcd);
4590  do_refresh = true;
4591  handled = true;
4592  break;
4594  kcd->depth_test = !kcd->depth_test;
4596  knife_update_header(C, op, kcd);
4597  do_refresh = true;
4598  handled = true;
4599  break;
4600  case KNF_MODAL_NEW_CUT:
4601  /* If no cuts have been made, exit.
4602  * Preserves right click cancel workflow which most tools use,
4603  * but stops accidentally deleting entire cuts with right click.
4604  */
4605  if (kcd->no_cuts) {
4607  knifetool_exit(op);
4609  return OPERATOR_CANCELLED;
4610  }
4612  knife_finish_cut(kcd);
4613  kcd->mode = MODE_IDLE;
4614  handled = true;
4615  break;
4616  case KNF_MODAL_ADD_CUT:
4617  kcd->no_cuts = false;
4618  knife_recalc_ortho(kcd);
4619 
4620  /* Get the value of the event which triggered this one. */
4621  if (event->prev_val != KM_RELEASE) {
4622  if (kcd->mode == MODE_DRAGGING) {
4623  knife_add_cut(kcd);
4624  }
4625  else if (kcd->mode != MODE_PANNING) {
4626  knife_start_cut(kcd);
4627  kcd->mode = MODE_DRAGGING;
4628  kcd->init = kcd->curr;
4629  }
4630 
4631  /* Freehand drawing is incompatible with cut-through. */
4632  if (kcd->cut_through == false) {
4633  kcd->is_drag_hold = true;
4634  /* No edge snapping while dragging (edges are too sticky when cuts are immediate). */
4635  kcd->ignore_edge_snapping = true;
4636  }
4637  }
4638  else {
4639  kcd->is_drag_hold = false;
4640  kcd->ignore_edge_snapping = false;
4641  kcd->is_drag_undo = false;
4642 
4643  /* Needed because the last face 'hit' is ignored when dragging. */
4644  knifetool_update_mval(kcd, kcd->curr.mval);
4645  }
4646 
4648  handled = true;
4649  break;
4651  if (kcd->mode == MODE_DRAGGING) {
4652 
4653  /* Shouldn't be possible with default key-layout, just in case. */
4654  if (kcd->is_drag_hold) {
4655  kcd->is_drag_hold = false;
4656  kcd->is_drag_undo = false;
4657  knifetool_update_mval(kcd, kcd->curr.mval);
4658  }
4659 
4660  kcd->prev = kcd->curr;
4661  kcd->curr = kcd->init;
4662 
4663  knife_project_v2(kcd, kcd->curr.cage, kcd->curr.mval);
4664  knifetool_update_mval(kcd, kcd->curr.mval);
4665 
4666  knife_add_cut(kcd);
4667 
4668  /* KNF_MODAL_NEW_CUT */
4669  knife_finish_cut(kcd);
4670  kcd->mode = MODE_IDLE;
4671  }
4672  handled = true;
4673  break;
4674  case KNF_MODAL_PANNING:
4675  if (event->val != KM_RELEASE) {
4676  if (kcd->mode != MODE_PANNING) {
4677  kcd->prevmode = kcd->mode;
4678  kcd->mode = MODE_PANNING;
4679  }
4680  }
4681  else {
4682  kcd->mode = kcd->prevmode;
4683  }
4684 
4686  return OPERATOR_PASS_THROUGH;
4687  }
4688  }
4689  else { /* non-modal-mapped events */
4690  switch (event->type) {
4691  case MOUSEPAN:
4692  case MOUSEZOOM:
4693  case MOUSEROTATE:
4694  case WHEELUPMOUSE:
4695  case WHEELDOWNMOUSE:
4696  case NDOF_MOTION:
4697  return OPERATOR_PASS_THROUGH;
4698  case MOUSEMOVE: /* Mouse moved somewhere to select another loop. */
4699  if (kcd->mode != MODE_PANNING) {
4700  knifetool_update_mval_i(kcd, event->mval);
4701  knife_update_header(C, op, kcd);
4702 
4703  if (kcd->is_drag_hold) {
4704  if (kcd->totlinehit >= 2) {
4705  knife_add_cut(kcd);
4706  }
4707  }
4708  }
4709 
4710  break;
4711  }
4712  }
4713 
4714  if (kcd->angle_snapping) {
4715  if (kcd->num.str_cur >= 3 ||
4718  }
4719  if (event->type != EVT_MODAL_MAP) {
4720  /* Modal number-input inactive, try to handle numeric inputs last. */
4721  if (!handled && event->val == KM_PRESS && handleNumInput(C, &kcd->num, event)) {
4722  applyNumInput(&kcd->num, &snapping_increment_temp);
4723  /* Restrict number key input to 0 - 180 degree range. */
4724  if (snapping_increment_temp > KNIFE_MIN_ANGLE_SNAPPING_INCREMENT &&
4725  snapping_increment_temp <= KNIFE_MAX_ANGLE_SNAPPING_INCREMENT) {
4726  kcd->angle_snapping_increment = snapping_increment_temp;
4727  }
4728  knife_update_active(kcd);
4729  knife_update_header(C, op, kcd);
4731  return OPERATOR_RUNNING_MODAL;
4732  }
4733  }
4734  }
4735 
4736  /* Constrain axes with X,Y,Z keys. */
4737  if (event->type == EVT_MODAL_MAP) {
4739  if (event->val == KNF_MODAL_X_AXIS && kcd->constrain_axis != KNF_CONSTRAIN_AXIS_X) {
4742  kcd->axis_string[0] = 'X';
4743  }
4744  else if (event->val == KNF_MODAL_Y_AXIS && kcd->constrain_axis != KNF_CONSTRAIN_AXIS_Y) {
4747  kcd->axis_string[0] = 'Y';
4748  }
4749  else if (event->val == KNF_MODAL_Z_AXIS && kcd->constrain_axis != KNF_CONSTRAIN_AXIS_Z) {
4752  kcd->axis_string[0] = 'Z';
4753  }
4754  else {
4755  /* Cycle through modes with repeated key presses. */
4757  kcd->constrain_axis_mode++;
4758  kcd->axis_string[0] += 32; /* Lower case. */
4759  }
4760  else {
4763  }
4764  }
4767  knife_update_header(C, op, kcd);
4769  do_refresh = true;
4770  }
4771  }
4772 
4773  if (kcd->mode == MODE_DRAGGING) {
4775  }
4776  else {
4778  }
4779 
4780  if (do_refresh) {
4781  /* We don't really need to update mval,
4782  * but this happens to be the best way to refresh at the moment. */
4783  knifetool_update_mval_i(kcd, event->mval);
4784  knife_update_header(C, op, kcd);
4785  }
4786 
4787  /* Keep going until the user confirms. */
4788  return OPERATOR_RUNNING_MODAL;
4789 }
4790 
4791 static int knifetool_invoke(bContext *C, wmOperator *op, const wmEvent *event)
4792 {
4793  const bool only_select = RNA_boolean_get(op->ptr, "only_selected");
4794  const bool cut_through = !RNA_boolean_get(op->ptr, "use_occlude_geometry");
4795  const bool xray = !RNA_boolean_get(op->ptr, "xray");
4796  const int visible_measurements = RNA_enum_get(op->ptr, "visible_measurements");
4797  const int angle_snapping = RNA_enum_get(op->ptr, "angle_snapping");
4798  const bool wait_for_input = RNA_boolean_get(op->ptr, "wait_for_input");
4799  const float angle_snapping_increment = RAD2DEGF(
4800  RNA_float_get(op->ptr, "angle_snapping_increment"));
4801 
4802  ViewContext vc;
4803  KnifeTool_OpData *kcd;
4804 
4805  em_setup_viewcontext(C, &vc);
4806 
4807  /* alloc new customdata */
4808  kcd = op->customdata = MEM_callocN(sizeof(KnifeTool_OpData), __func__);
4809 
4810  knifetool_init(&vc,
4811  kcd,
4812  NULL,
4813  0,
4814  only_select,
4815  cut_through,
4816  xray,
4817  visible_measurements,
4818  angle_snapping,
4819  angle_snapping_increment,
4820  true);
4821 
4822  if (only_select) {
4823  Object *obedit;
4824  BMEditMesh *em;
4825  bool faces_selected = false;
4826 
4827  for (uint b = 0; b < kcd->objects_len; b++) {
4828  obedit = kcd->objects[b];
4829  em = BKE_editmesh_from_object(obedit);
4830  if (em->bm->totfacesel != 0) {
4831  faces_selected = true;
4832  }
4833  }
4834 
4835  if (!faces_selected) {
4836  BKE_report(op->reports, RPT_ERROR, "Selected faces required");
4837  knifetool_cancel(C, op);
4838  return OPERATOR_CANCELLED;
4839  }
4840  }
4841 
4843 
4844  /* Add a modal handler for this operator - handles loop selection. */
4847 
4848  knifetool_update_mval_i(kcd, event->mval);
4849 
4850  if (wait_for_input == false) {
4851  /* Avoid copy-paste logic. */
4852  wmEvent event_modal = {
4853  .prev_val = KM_NOTHING,
4854  .type = EVT_MODAL_MAP,
4855  .val = KNF_MODAL_ADD_CUT,
4856  };
4857  int ret = knifetool_modal(C, op, &event_modal);
4860  }
4861 
4862  knife_update_header(C, op, kcd);
4863 
4864  return OPERATOR_RUNNING_MODAL;
4865 }
4866 
4868 {
4869  /* Description. */
4870  ot->name = "Knife Topology Tool";
4871  ot->idname = "MESH_OT_knife_tool";
4872  ot->description = "Cut new topology";
4873 
4874  /* Callbacks. */
4879 
4880  /* Flags. */
4882 
4883  /* Properties. */
4884  PropertyRNA *prop;
4885  static const EnumPropertyItem visible_measurements_items[] = {
4886  {KNF_MEASUREMENT_NONE, "NONE", 0, "None", "Show no measurements"},
4887  {KNF_MEASUREMENT_BOTH, "BOTH", 0, "Both", "Show both distances and angles"},
4888  {KNF_MEASUREMENT_DISTANCE, "DISTANCE", 0, "Distance", "Show just distance measurements"},
4889  {KNF_MEASUREMENT_ANGLE, "ANGLE", 0, "Angle", "Show just angle measurements"},
4890  {0, NULL, 0, NULL, NULL},
4891  };
4892 
4893  static const EnumPropertyItem angle_snapping_items[] = {
4894  {KNF_CONSTRAIN_ANGLE_MODE_NONE, "NONE", 0, "None", "No angle snapping"},
4895  {KNF_CONSTRAIN_ANGLE_MODE_SCREEN, "SCREEN", 0, "Screen", "Screen space angle snapping"},
4897  "RELATIVE",
4898  0,
4899  "Relative",
4900  "Angle snapping relative to the previous cut edge"},
4901  {0, NULL, 0, NULL, NULL},
4902  };
4903 
4905  "use_occlude_geometry",
4906  true,
4907  "Occlude Geometry",
4908  "Only cut the front most geometry");
4909  RNA_def_boolean(ot->srna, "only_selected", false, "Only Selected", "Only cut selected geometry");
4910  RNA_def_boolean(ot->srna, "xray", true, "X-Ray", "Show cuts hidden by geometry");
4911 
4912  RNA_def_enum(ot->srna,
4913  "visible_measurements",
4914  visible_measurements_items,
4916  "Measurements",
4917  "Visible distance and angle measurements");
4918  RNA_def_enum(ot->srna,
4919  "angle_snapping",
4920  angle_snapping_items,
4922  "Angle Snapping",
4923  "Angle snapping mode");
4924 
4925  prop = RNA_def_float(ot->srna,
4926  "angle_snapping_increment",
4930  "Angle Snap Increment",
4931  "The angle snap increment used when in constrained angle mode",
4935 
4936  prop = RNA_def_boolean(ot->srna, "wait_for_input", true, "Wait for Input", "");
4938 }
4939 
4942 /* -------------------------------------------------------------------- */
4948 static bool edbm_mesh_knife_point_isect(LinkNode *polys, const float cent_ss[2])
4949 {
4950  LinkNode *p = polys;
4951  int isect = 0;
4952 
4953  while (p) {
4954  const float(*mval_fl)[2] = p->link;
4955  const int mval_tot = MEM_allocN_len(mval_fl) / sizeof(*mval_fl);
4956  isect += (int)isect_point_poly_v2(cent_ss, mval_fl, mval_tot - 1, false);
4957  p = p->next;
4958  }
4959 
4960  if (isect % 2) {
4961  return true;
4962  }
4963  return false;
4964 }
4965 
4967  Object **objects,
4968  const int objects_len,
4969  LinkNode *polys,
4970  bool use_tag,
4971  bool cut_through)
4972 {
4973  KnifeTool_OpData *kcd;
4974 
4975  /* Init. */
4976  {
4977  const bool only_select = false;
4978  const bool is_interactive = false; /* Can enable for testing. */
4979  const bool xray = false;
4980  const int visible_measurements = KNF_MEASUREMENT_NONE;
4981  const int angle_snapping = KNF_CONSTRAIN_ANGLE_MODE_NONE;
4982  const float angle_snapping_increment = KNIFE_DEFAULT_ANGLE_SNAPPING_INCREMENT;
4983 
4984  kcd = MEM_callocN(sizeof(KnifeTool_OpData), __func__);
4985 
4986  knifetool_init(vc,
4987  kcd,
4988  objects,
4989  objects_len,
4990  only_select,
4991  cut_through,
4992  xray,
4993  visible_measurements,
4994  angle_snapping,
4995  angle_snapping_increment,
4996  is_interactive);
4997 
4998  kcd->ignore_edge_snapping = true;
4999  kcd->ignore_vert_snapping = true;
5000  }
5001 
5002  /* Execute. */
5003  {
5004  LinkNode *p = polys;
5005 
5006  knife_recalc_ortho(kcd);
5007 
5008  while (p) {
5009  const float(*mval_fl)[2] = p->link;
5010  const int mval_tot = MEM_allocN_len(mval_fl) / sizeof(*mval_fl);
5011  int i;
5012 
5013  for (i = 0; i < mval_tot; i++) {
5014  knifetool_update_mval(kcd, mval_fl[i]);
5015  if (i == 0) {
5016  knife_start_cut(kcd);
5017  kcd->mode = MODE_DRAGGING;
5018  }
5019  else {
5020  knife_add_cut(kcd);
5021  }
5022  }
5023  knife_finish_cut(kcd);
5024  kcd->mode = MODE_IDLE;
5025  p = p->next;
5026  }
5027  }
5028 
5029  /* Finish. */
5030  {
5031  /* See #knifetool_finish_ex for why multiple passes are needed. */
5032  for (uint b = 0; b < kcd->objects_len; b++) {
5033  Object *ob = kcd->objects[b];
5035 
5036  if (use_tag) {
5038  }
5039 
5040  knifetool_finish_single_pre(kcd, ob);
5041  }
5042 
5043  for (uint b = 0; b < kcd->objects_len; b++) {
5044  Object *ob = kcd->objects[b];
5046 
5047  /* Tag faces inside! */
5048  if (use_tag) {
5049  BMesh *bm = em->bm;
5050  BMEdge *e;
5051  BMIter iter;
5052  bool keep_search;
5053 
5054  /* Use face-loop tag to store if we have intersected. */
5055 #define F_ISECT_IS_UNKNOWN(f) BM_elem_flag_test(BM_FACE_FIRST_LOOP(f), BM_ELEM_TAG)
5056 #define F_ISECT_SET_UNKNOWN(f) BM_elem_flag_enable(BM_FACE_FIRST_LOOP(f), BM_ELEM_TAG)
5057 #define F_ISECT_SET_OUTSIDE(f) BM_elem_flag_disable(BM_FACE_FIRST_LOOP(f), BM_ELEM_TAG)
5058  {
5059  BMFace *f;
5060  BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
5063  }
5064  }
5065 
5066  /* Tag all faces linked to cut edges. */
5067  BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
5068  /* Check are we tagged?, then we are an original face. */
5070  continue;
5071  }
5072 
5073  BMFace *f;
5074  BMIter fiter;
5075  BM_ITER_ELEM (f, &fiter, e, BM_FACES_OF_EDGE) {
5076  float cent[3], cent_ss[2];
5077  BM_face_calc_point_in_face(f, cent);
5078  mul_m4_v3(ob->obmat, cent);
5079  knife_project_v2(kcd, cent, cent_ss);
5080  if (edbm_mesh_knife_point_isect(polys, cent_ss)) {
5082  }
5083  }
5084  }
5085 
5086  /* Expand tags for faces which are not cut, but are inside the polys. */
5087  do {
5088  BMFace *f;
5089  keep_search = false;
5090  BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
5092  continue;
5093  }
5094 
5095  /* Am I connected to a tagged face via an un-tagged edge
5096  * (ie, not across a cut)? */
5097  BMLoop *l_first = BM_FACE_FIRST_LOOP(f);
5098  BMLoop *l_iter = l_first;
5099  bool found = false;
5100 
5101  do {
5102  if (BM_elem_flag_test(l_iter->e, BM_ELEM_TAG) != false) {
5103  /* Now check if the adjacent faces is tagged. */
5104  BMLoop *l_radial_iter = l_iter->radial_next;
5105  if (l_radial_iter != l_iter) {
5106  do {
5107  if (BM_elem_flag_test(l_radial_iter->f, BM_ELEM_TAG)) {
5108  found = true;
5109  }
5110  } while ((l_radial_iter = l_radial_iter->radial_next) != l_iter &&
5111  (found == false));
5112  }
5113  }
5114  } while ((l_iter = l_iter->next) != l_first && (found == false));
5115 
5116  if (found) {
5117  float cent[3], cent_ss[2];
5118  BM_face_calc_point_in_face(f, cent);
5119  mul_m4_v3(ob->obmat, cent);
5120  knife_project_v2(kcd, cent, cent_ss);
5121  if ((kcd->cut_through || point_is_visible(kcd, cent, cent_ss, (BMElem *)f)) &&
5122  edbm_mesh_knife_point_isect(polys, cent_ss)) {
5124  keep_search = true;
5125  }
5126  else {
5127  /* Don't lose time on this face again, set it as outside. */
5129  }
5130  }
5131  }
5132  } while (keep_search);
5133 
5134 #undef F_ISECT_IS_UNKNOWN
5135 #undef F_ISECT_SET_UNKNOWN
5136 #undef F_ISECT_SET_OUTSIDE
5137  }
5138  }
5139 
5140  for (uint b = 0; b < kcd->objects_len; b++) {
5141  /* Defer freeing data until the BVH tree is finished with, see: #point_is_visible and
5142  * the doc-string for #knifetool_finish_single_post. */
5143  Object *ob = kcd->objects[b];
5145  }
5146 
5147  knifetool_exit_ex(kcd);
5148  kcd = NULL;
5149  }
5150 }
5151 
typedef float(TangentPoint)[2]
struct wmWindow * CTX_wm_window(const bContext *C)
Definition: context.c:723
float(* BKE_editmesh_vert_coords_alloc(struct Depsgraph *depsgraph, struct BMEditMesh *em, struct Scene *scene, struct Object *ob, int *r_vert_len))[3]
Definition: editmesh.c:212
BMEditMesh * BKE_editmesh_from_object(struct Object *ob)
Return the BMEditMesh for a given object.
Definition: editmesh.c:58
#define BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, v3d, r_len)
Definition: BKE_layer.h:542
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition: report.c:83
int BKE_scene_orientation_get_index(struct Scene *scene, int slot_index)
Definition: scene.cc:2470
@ B_UNIT_LENGTH
Definition: BKE_unit.h:101
@ B_UNIT_ROTATION
Definition: BKE_unit.h:105
@ B_UNIT_NONE
Definition: BKE_unit.h:100
size_t BKE_unit_value_as_string(char *str, int len_max, double value, int prec, int type, const struct UnitSettings *settings, bool pad)
@ BLF_ROTATION
Definition: BLF_api.h:334
void BLF_color3ubv(int fontid, const unsigned char rgb[3])
Definition: blf.c:407
void BLF_width_and_height(int fontid, const char *str, size_t str_len, float *r_width, float *r_height) ATTR_NONNULL()
Definition: blf.c:662
void BLF_disable(int fontid, int option)
Definition: blf.c:279
void BLF_rotation(int fontid, float angle)
Definition: blf.c:766
void BLF_draw(int fontid, const char *str, size_t str_len) ATTR_NONNULL(2)
Definition: blf.c:538
int blf_mono_font
Definition: blf.c:48
void BLF_enable(int fontid, int option)
Definition: blf.c:270
void BLF_size(int fontid, float size, int dpi)
Definition: blf.c:363
void BLF_position(int fontid, float x, float y, float z)
Definition: blf.c:308
#define BLI_array_alloca(arr, realsize)
Definition: BLI_alloca.h:22
A (mainly) macro array library.
#define BLI_array_append(arr, item)
Definition: BLI_array.h:98
#define BLI_array_declare(arr)
Definition: BLI_array.h:50
#define BLI_array_len(arr)
Definition: BLI_array.h:63
#define BLI_assert(a)
Definition: BLI_assert.h:46
struct GSet GSet
Definition: BLI_ghash.h:340
BLI_INLINE void * BLI_ghashIterator_getKey(GHashIterator *ghi) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.h:298
BLI_INLINE void * BLI_ghashIterator_getValue(GHashIterator *ghi) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.h:302
#define GHASH_ITER(gh_iter_, ghash_)
Definition: BLI_ghash.h:321
GSet * BLI_gset_ptr_new(const char *info)
void BLI_gset_clear(GSet *gs, GSetKeyFreeFP keyfreefp)
Definition: BLI_ghash.c:1032
unsigned int BLI_gset_len(const GSet *gs) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.c:957
void * BLI_ghash_lookup(const GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.c:734
void BLI_ghash_insert(GHash *gh, void *key, void *val)
Definition: BLI_ghash.c:710
void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition: BLI_ghash.c:863
void BLI_gset_free(GSet *gs, GSetKeyFreeFP keyfreefp)
Definition: BLI_ghash.c:1037
GHash * BLI_ghash_ptr_new(const char *info) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
bool BLI_gset_add(GSet *gs, void *key)
Definition: BLI_ghash.c:969
void BLI_bvhtree_balance(BVHTree *tree)
Definition: BLI_kdopbvh.c:937
BVHTree * BLI_bvhtree_new(int maxsize, float epsilon, char tree_type, char axis)
Definition: BLI_kdopbvh.c:854
void BLI_bvhtree_free(BVHTree *tree)
Definition: BLI_kdopbvh.c:926
void BLI_bvhtree_insert(BVHTree *tree, int index, const float co[3], int numpoints)
Definition: BLI_kdopbvh.c:979
int * BLI_bvhtree_intersect_plane(BVHTree *tree, float plane[4], uint *r_intersect_num)
Definition: BLI_kdopbvh.c:1420
int BLI_bvhtree_ray_cast(BVHTree *tree, const float co[3], const float dir[3], float radius, BVHTreeRayHit *hit, BVHTree_RayCastCallback callback, void *userdata)
Definition: BLI_kdopbvh.c:1942
BLI_INLINE void BLI_listbase_clear(struct ListBase *lb)
Definition: BLI_listbase.h:273
int BLI_listbase_count_at_most(const struct ListBase *listbase, int count_max) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void void BLI_INLINE bool BLI_listbase_is_single(const struct ListBase *lb)
Definition: BLI_listbase.h:265
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:80
void BLI_remlink(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:100
void void void BLI_listbase_sort_r(ListBase *listbase, int(*cmp)(void *, const void *, const void *), void *thunk) ATTR_NONNULL(1
int BLI_listbase_count(const struct ListBase *listbase) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
#define BLI_ASSERT_UNIT_V2(v)
MINLINE float min_ff(float a, float b)
MINLINE float min_fff(float a, float b, float c)
void rgba_uchar_to_float(float r_col[4], const unsigned char col_ub[4])
Definition: math_color.c:383
void plane_from_point_normal_v3(float r_plane[4], const float plane_co[3], const float plane_no[3])
Definition: math_geom.c:209
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])
Definition: math_geom.c:3464
bool isect_ray_plane_v3(const float ray_origin[3], const float ray_direction[3], const float plane[4], float *r_lambda, bool clip)
Definition: math_geom.c:1713
int isect_line_line_v3(const float v1[3], const float v2[3], const float v3[3], const float v4[3], float r_i1[3], float r_i2[3])
Definition: math_geom.c:2935
float dist_squared_to_plane_v3(const float p[3], const float plane[4])
Definition: math_geom.c:436
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])
Definition: math_geom.c:3906
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])
Definition: math_geom.c:1662
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_dist, float r_uv[2])
Definition: math_geom.c:1811
float line_point_factor_v2(const float p[2], const float l1[2], const float l2[2])
Definition: math_geom.c:3274
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.c:357
float line_point_factor_v3(const float p[3], const float l1[3], const float l2[3])
Definition: math_geom.c:3254
int isect_line_line_v2_point(const float v0[2], const float v1[2], const float v2[2], const float v3[2], float r_vi[2])
Definition: math_geom.c:1085
float closest_to_line_v2(float r_close[2], const float p[2], const float l1[2], const float l2[2])
Definition: math_geom.c:3183
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
Definition: math_geom.c:2078
float normal_tri_v3(float n[3], const float v1[3], const float v2[3], const float v3[3])
Definition: math_geom.c:33
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)
Definition: math_geom.c:1735
bool isect_point_poly_v2(const float pt[2], const float verts[][2], unsigned int nr, bool use_holes)
void planes_from_projmat(const float mat[4][4], float left[4], float right[4], float bottom[4], float top[4], float near[4], float far[4])
Definition: math_geom.c:4615
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 vi[2])
Definition: math_geom.c:1195
float dist_squared_to_line_segment_v2(const float p[2], const float l1[2], const float l2[2])
Definition: math_geom.c:283
void mul_project_m4_v3(const float M[4][4], float vec[3])
Definition: math_matrix.c:820
void mul_v3_project_m4_v3(float r[3], const float mat[4][4], const float vec[3])
Definition: math_matrix.c:831
void mul_m3_m3_pre(float R[3][3], const float A[3][3])
Definition: math_matrix.c:401
void mul_v2_m2v2(float r[2], const float M[2][2], const float v[2])
Definition: math_matrix.c:777
void mul_m4_v3(const float M[4][4], float r[3])
Definition: math_matrix.c:729
void mul_transposed_mat3_m4_v3(const float M[4][4], float r[3])
Definition: math_matrix.c:946
void mul_v3_m4v3(float r[3], const float M[4][4], const float v[3])
Definition: math_matrix.c:739
bool invert_m3(float R[3][3])
Definition: math_matrix.c:1171
void axis_angle_to_quat(float r[4], const float axis[3], float angle)
#define DEG2RADF(_deg)
void mul_qt_v3(const float q[4], float r[3])
Definition: math_rotation.c:59
void angle_to_mat2(float R[2][2], float angle)
#define RAD2DEGF(_rad)
MINLINE bool compare_v3v3(const float a[3], const float b[3], float limit) ATTR_WARN_UNUSED_RESULT
void rotate_v3_v3v3fl(float r[3], const float p[3], const float axis[3], float angle)
Definition: math_vector.c:800
MINLINE float len_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
void minmax_v3v3_v3(float min[3], float max[3], const float vec[3])
Definition: math_vector.c:867
MINLINE float len_squared_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
MINLINE float normalize_v3(float r[3])
MINLINE void sub_v3_v3(float r[3], const float a[3])
MINLINE float len_squared_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void copy_v2_v2(float r[2], const float a[2])
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE void negate_v3_v3(float r[3], const float a[3])
void project_plane_normalized_v3_v3v3(float out[3], const float p[3], const float v_plane[3])
Definition: math_vector.c:652
void interp_v3_v3v3(float r[3], const float a[3], const float b[3], float t)
Definition: math_vector.c:29
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void cross_v3_v3v3(float r[3], const float a[3], const float b[3])
float angle_signed_on_axis_v3v3_v3(const float v1[3], const float v2[3], const float axis[3]) ATTR_WARN_UNUSED_RESULT
Definition: math_vector.c:488
MINLINE float dot_m4_v3_row_z(const float M[4][4], const float a[3]) ATTR_WARN_UNUSED_RESULT
float angle_signed_v2v2(const float v1[2], const float v2[2]) ATTR_WARN_UNUSED_RESULT
Definition: math_vector.c:439
void mid_v2_v2v2(float r[2], const float a[2], const float b[2])
Definition: math_vector.c:244
MINLINE float normalize_v3_v3(float r[3], const float a[3])
MINLINE void add_v2_v2v2(float r[2], const float a[2], const float b[2])
MINLINE void sub_v2_v2v2(float r[2], const float a[2], const float b[2])
MINLINE void zero_v2(float r[2])
MINLINE bool equals_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
void mid_v3_v3v3(float r[3], const float a[3], const float b[3])
Definition: math_vector.c:237
MINLINE bool is_zero_v2(const float a[2]) ATTR_WARN_UNUSED_RESULT
MINLINE void madd_v3_v3v3fl(float r[3], const float a[3], const float b[3], float f)
MINLINE float len_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
MINLINE void zero_v3(float r[3])
float angle_v3v3v3(const float a[3], const float b[3], const float c[3]) ATTR_WARN_UNUSED_RESULT
Definition: math_vector.c:361
float angle_normalized_v3v3(const float v1[3], const float v2[3]) ATTR_WARN_UNUSED_RESULT
Definition: math_vector.c:445
MINLINE void add_v3_v3(float r[3], const float a[3])
MINLINE float normalize_v2_v2(float r[2], const float a[2])
void interp_v3_v3v3v3_uv(float p[3], const float v1[3], const float v2[3], const float v3[3], const float uv[2])
Definition: math_vector.c:202
void BLI_memarena_free(struct MemArena *ma) ATTR_NONNULL(1)
Definition: BLI_memarena.c:94
struct MemArena * BLI_memarena_new(size_t bufsize, const char *name) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL ATTR_NONNULL(2) ATTR_MALLOC
Definition: BLI_memarena.c:64
void * BLI_memarena_alloc(struct MemArena *ma, size_t size) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_ALLOC_SIZE(2)
Definition: BLI_memarena.c:116
void void BLI_memarena_clear(MemArena *ma) ATTR_NONNULL(1)
Definition: BLI_memarena.c:208
void BLI_mempool_iternew(BLI_mempool *pool, BLI_mempool_iter *iter) ATTR_NONNULL()
Definition: BLI_mempool.c:498
@ BLI_MEMPOOL_ALLOW_ITER
Definition: BLI_mempool.h:107
void * BLI_mempool_iterstep(BLI_mempool_iter *iter) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: BLI_mempool.c:577
int BLI_mempool_len(const BLI_mempool *pool) ATTR_NONNULL(1)
Definition: BLI_mempool.c:434
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
Definition: BLI_mempool.c:253
void BLI_mempool_destroy(BLI_mempool *pool) ATTR_NONNULL(1)
Definition: BLI_mempool.c:707
void * BLI_mempool_calloc(BLI_mempool *pool) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL ATTR_NONNULL(1)
Definition: BLI_mempool.c:347
void ** BLI_smallhash_iternew_p(const SmallHash *sh, SmallHashIter *iter, uintptr_t *key) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
Definition: smallhash.c:320
void * BLI_smallhash_iternext(SmallHashIter *iter, uintptr_t *key) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
Definition: smallhash.c:298
bool BLI_smallhash_haskey(const SmallHash *sh, uintptr_t key) ATTR_NONNULL(1)
Definition: smallhash.c:269
void BLI_smallhash_init(SmallHash *sh) ATTR_NONNULL(1)
Definition: smallhash.c:196
void ** BLI_smallhash_iternext_p(SmallHashIter *iter, uintptr_t *key) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
Definition: smallhash.c:305
void BLI_smallhash_insert(SmallHash *sh, uintptr_t key, void *item) ATTR_NONNULL(1)
Definition: smallhash.c:208
void BLI_smallhash_release(SmallHash *sh) ATTR_NONNULL(1)
Definition: smallhash.c:201
void * BLI_smallhash_lookup(const SmallHash *sh, uintptr_t key) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
Definition: smallhash.c:255
bool BLI_smallhash_reinsert(SmallHash *sh, uintptr_t key, void *item) ATTR_NONNULL(1)
Definition: smallhash.c:225
void * BLI_smallhash_iternew(const SmallHash *sh, SmallHashIter *iter, uintptr_t *key) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
Definition: smallhash.c:312
void BLI_stack_pop(BLI_Stack *stack, void *dst) ATTR_NONNULL()
Definition: stack.c:135
void * BLI_stack_push_r(BLI_Stack *stack) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: stack.c:101
void * BLI_stack_peek(BLI_Stack *stack) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: stack.c:166
void BLI_stack_push(BLI_Stack *stack, const void *src) ATTR_NONNULL()
Definition: stack.c:129
bool BLI_stack_is_empty(const BLI_Stack *stack) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: stack.c:247
void BLI_stack_free(BLI_Stack *stack) ATTR_NONNULL()
Definition: stack.c:94
void BLI_stack_discard(BLI_Stack *stack) ATTR_NONNULL()
Definition: stack.c:173
#define BLI_stack_new(esize, descr)
size_t BLI_snprintf(char *__restrict dst, size_t maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
unsigned char uchar
Definition: BLI_sys_types.h:70
unsigned int uint
Definition: BLI_sys_types.h:67
#define UNPACK2(a)
#define UNUSED_FUNCTION(x)
#define INIT_MINMAX(min, max)
#define POINTER_FROM_INT(i)
#define UNUSED_VARS_NDEBUG(...)
#define UNUSED(x)
#define POINTER_AS_INT(i)
#define UNPACK3(a)
#define ELEM(...)
#define TIP_(msgid)
struct ID * DEG_get_evaluated_id(const struct Depsgraph *depsgraph, struct ID *id)
Object is a sort of wrapper for general info.
@ OB_MESH
#define USER_UNIT_NONE
#define SCE_SELECT_FACE
@ SCE_ORIENT_DEFAULT
@ SPACE_VIEW3D
#define RV3D_CAMOB
#define RV3D_CLIPPING_ENABLED(v3d, rv3d)
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
@ OPERATOR_RUNNING_MODAL
@ OPERATOR_PASS_THROUGH
@ OP_IS_MODAL_CURSOR_REGION
void EDBM_update(struct Mesh *me, const struct EDBMUpdate_Params *params)
void em_setup_viewcontext(struct bContext *C, struct ViewContext *vc)
struct BMFace * EDBM_face_find_nearest(struct ViewContext *vc, float *dist_px_manhattan_p)
void EDBM_flag_disable_all(struct BMEditMesh *em, char hflag)
void EDBM_selectmode_flush(struct BMEditMesh *em)
void initNumInput(NumInput *n)
Definition: numinput.c:69
@ NUM_NO_NEGATIVE
Definition: ED_numinput.h:56
bool applyNumInput(NumInput *n, float *vec)
Definition: numinput.c:189
bool hasNumInput(const NumInput *n)
Definition: numinput.c:170
bool handleNumInput(struct bContext *C, NumInput *n, const struct wmEvent *event)
bool ED_operator_editmesh_view3d(struct bContext *C)
Definition: screen_ops.c:442
void ED_region_tag_redraw(struct ARegion *region)
Definition: area.c:655
void ED_workspace_status_text(struct bContext *C, const char *str)
Definition: area.c:816
#define REGION_DRAW_POST_VIEW
Definition: ED_space_api.h:62
void * ED_region_draw_cb_activate(struct ARegionType *art, void(*draw)(const struct bContext *, struct ARegion *, void *), void *customdata, int type)
Definition: spacetypes.c:226
bool ED_region_draw_cb_exit(struct ARegionType *art, void *handle)
Definition: spacetypes.c:241
short ED_transform_calc_orientation_from_type_ex(const struct Scene *scene, struct ViewLayer *view_layer, const struct View3D *v3d, const struct RegionView3D *rv3d, struct Object *ob, struct Object *obedit, short orientation_index, int pivot_point, float r_mat[3][3])
bool ED_view3d_win_to_segment_clipped(const struct Depsgraph *depsgraph, const struct ARegion *region, const struct View3D *v3d, const float mval[2], float r_ray_start[3], float r_ray_end[3], bool do_clip_planes)
bool ED_view3d_clip_range_get(const struct Depsgraph *depsgraph, const struct View3D *v3d, const struct RegionView3D *rv3d, float *r_clipsta, float *r_clipend, bool use_ortho_factor)
@ V3D_PROJ_TEST_NOP
Definition: ED_view3d.h:234
eV3DProjStatus ED_view3d_project_float_global(const struct ARegion *region, const float co[3], float r_co[2], eV3DProjTest flag)
float ED_view3d_pixel_size_no_ui_scale(const struct RegionView3D *rv3d, const float co[3])
bool ED_view3d_unproject_v3(const struct ARegion *region, float regionx, float regiony, float regionz, float world[3])
bool ED_view3d_clipping_test(const struct RegionView3D *rv3d, const float co[3], bool is_local)
void ED_view3d_win_to_3d(const struct View3D *v3d, const struct ARegion *region, const float depth_pt[3], const float mval[2], float r_out[3])
void ED_view3d_init_mats_rv3d(const struct Object *ob, struct RegionView3D *rv3d)
Definition: space_view3d.c:166
static AppView * view
NSNotificationCenter * center
GPUBatch
Definition: GPU_batch.h:78
void GPU_batch_draw_range(GPUBatch *batch, int v_first, int v_count)
Definition: gpu_batch.cc:229
void GPU_batch_discard(GPUBatch *)
Definition: gpu_batch.cc:109
void GPU_batch_program_set_builtin(GPUBatch *batch, eGPUBuiltinShader shader_id)
Definition: gpu_batch.cc:287
GPUBatch * GPU_batch_create_ex(GPUPrimType prim, GPUVertBuf *vert, GPUIndexBuf *elem, eGPUBatchFlag owns_flag)
Definition: gpu_batch.cc:43
void GPU_batch_draw(GPUBatch *batch)
Definition: gpu_batch.cc:223
#define GPU_batch_uniform_4fv(batch, name, val)
Definition: GPU_batch.h:152
@ GPU_BATCH_OWNS_VBO
Definition: GPU_batch.h:30
void immUnbindProgram(void)
void immBindBuiltinProgram(eGPUBuiltinShader shader_id)
void immUniformColor3ubv(const unsigned char rgb[3])
GPUBatch * immBeginBatchAtMost(GPUPrimType, uint vertex_len)
void immUniformThemeColor3(int color_id)
void immUniformColor4fv(const float rgba[4])
GPUVertFormat * immVertexFormat(void)
void immVertex3fv(uint attr_id, const float data[3])
void immBegin(GPUPrimType, uint vertex_len)
void immEnd(void)
void immRectf(uint pos, float x1, float y1, float x2, float y2)
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble GLdouble r _GL_VOID_RET _GL_VOID GLfloat GLfloat r _GL_VOID_RET _GL_VOID GLint GLint r _GL_VOID_RET _GL_VOID GLshort GLshort r _GL_VOID_RET _GL_VOID GLdouble GLdouble r
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble v1
void GPU_matrix_pop(void)
Definition: gpu_matrix.cc:126
void GPU_matrix_pop_projection(void)
Definition: gpu_matrix.cc:140
void GPU_matrix_push(void)
Definition: gpu_matrix.cc:119
void GPU_polygon_offset(float viewdist, float dist)
Definition: gpu_matrix.cc:721
void GPU_matrix_identity_set(void)
Definition: gpu_matrix.cc:168
void GPU_matrix_push_projection(void)
Definition: gpu_matrix.cc:133
@ GPU_PRIM_LINES
Definition: GPU_primitive.h:20
@ GPU_PRIM_POINTS
Definition: GPU_primitive.h:19
@ GPU_PRIM_LINE_STRIP
Definition: GPU_primitive.h:22
@ GPU_SHADER_2D_UNIFORM_COLOR
Definition: GPU_shader.h:201
@ GPU_SHADER_3D_UNIFORM_COLOR
Definition: GPU_shader.h:230
@ GPU_BLEND_NONE
Definition: GPU_state.h:60
@ GPU_BLEND_ALPHA
Definition: GPU_state.h:62
void GPU_blend(eGPUBlend blend)
Definition: gpu_state.cc:39
void GPU_line_width(float width)
Definition: gpu_state.cc:158
void GPU_point_size(float size)
Definition: gpu_state.cc:164
@ GPU_DEPTH_LESS_EQUAL
Definition: GPU_state.h:86
@ GPU_DEPTH_NONE
Definition: GPU_state.h:83
void GPU_depth_test(eGPUDepthTest test)
Definition: gpu_state.cc:65
#define GPU_vertbuf_create_with_format(format)
struct GPUVertBuf GPUVertBuf
void GPU_vertbuf_data_alloc(GPUVertBuf *, uint v_len)
void GPU_vertbuf_attr_set(GPUVertBuf *, uint a_idx, uint v_idx, const void *data)
@ GPU_FETCH_FLOAT
uint GPU_vertformat_attr_add(GPUVertFormat *, const char *name, GPUVertCompType, uint comp_len, GPUVertFetchMode)
@ GPU_COMP_F32
Read Guarded memory(de)allocation.
#define MEM_SAFE_FREE(v)
#define MEM_SIZE_OPTIMAL(size)
@ PROP_SKIP_SAVE
Definition: RNA_types.h:218
@ PROP_HIDDEN
Definition: RNA_types.h:216
@ PROP_ANGLE
Definition: RNA_types.h:145
#define C
Definition: RandGen.cpp:25
#define UI_MAX_DRAW_STR
Definition: UI_interface.h:91
#define UI_DPI_FAC
Definition: UI_interface.h:305
void UI_GetThemeColorType3ubv(int colorid, int spacetype, unsigned char col[3])
Definition: resources.c:1369
@ TH_WIRE
Definition: UI_resources.h:69
@ TH_ACTIVE_SPLINE
Definition: UI_resources.h:120
@ TH_TRANSFORM
Definition: UI_resources.h:76
@ TH_NURB_VLINE
Definition: UI_resources.h:104
@ TH_NURB_ULINE
Definition: UI_resources.h:103
@ TH_AXIS_Y
Definition: UI_resources.h:301
@ TH_AXIS_X
Definition: UI_resources.h:300
@ TH_HANDLE_SEL_VECT
Definition: UI_resources.h:116
@ TH_AXIS_Z
Definition: UI_resources.h:302
@ TH_TEXT
Definition: UI_resources.h:42
@ TH_NURB_SEL_ULINE
Definition: UI_resources.h:105
void UI_GetThemeColor3ubv(int colorid, unsigned char col[3])
Definition: resources.c:1323
@ KM_NOTHING
Definition: WM_types.h:266
@ KM_PRESS
Definition: WM_types.h:267
@ KM_RELEASE
Definition: WM_types.h:268
@ OPTYPE_BLOCKING
Definition: WM_types.h:150
@ OPTYPE_UNDO
Definition: WM_types.h:148
@ OPTYPE_REGISTER
Definition: WM_types.h:146
#define BM_FACE_FIRST_LOOP(p)
Definition: bmesh_class.h:622
@ BM_FACE
Definition: bmesh_class.h:386
@ BM_VERT
Definition: bmesh_class.h:383
@ BM_EDGE
Definition: bmesh_class.h:384
@ BM_ELEM_HIDDEN
Definition: bmesh_class.h:472
@ BM_ELEM_SELECT
Definition: bmesh_class.h:471
@ BM_ELEM_TAG
Definition: bmesh_class.h:484
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.
Definition: bmesh_core.c:41
void BM_edge_kill(BMesh *bm, BMEdge *e)
Definition: bmesh_core.c:927
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.
Definition: bmesh_core.c:123
#define BM_elem_index_get(ele)
Definition: bmesh_inline.h:110
#define BM_elem_flag_disable(ele, hflag)
Definition: bmesh_inline.h:15
#define BM_elem_flag_test(ele, hflag)
Definition: bmesh_inline.h:12
#define BM_elem_flag_enable(ele, hflag)
Definition: bmesh_inline.h:14
#define BM_ITER_ELEM(ele, iter, data, itype)
#define BM_ITER_MESH(ele, iter, bm, itype)
@ BM_FACES_OF_EDGE
@ BM_FACES_OF_VERT
@ BM_EDGES_OF_MESH
@ BM_VERTS_OF_MESH
@ BM_FACES_OF_MESH
@ BM_EDGES_OF_FACE
ATTR_WARN_UNUSED_RESULT BMesh * bm
void BM_mesh_elem_hflag_enable_all(BMesh *bm, const char htype, const char hflag, const bool respecthide)
void BM_edge_select_set(BMesh *bm, BMEdge *e, const bool select)
Select Edge.
void BM_mesh_elem_index_ensure(BMesh *bm, const char htype)
Definition: bmesh_mesh.cc:446
BMVert * BM_edge_split(BMesh *bm, BMEdge *e, BMVert *v, BMEdge **r_e, float fac)
Edge Split.
Definition: bmesh_mods.c:448
void BM_face_calc_point_in_face(const BMFace *f, float r_co[3])
bool BM_face_point_inside_test(const BMFace *f, const float co[3])
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)
bool BM_face_split_edgenet(BMesh *bm, BMFace *f, BMEdge **edge_net, const int edge_net_len, BMFace ***r_face_arr, int *r_face_arr_len)
BMEdge * BM_edge_exists(BMVert *v_a, BMVert *v_b)
Definition: bmesh_query.c:1553
float BM_loop_point_side_of_loop_test(const BMLoop *l, const float co[3])
Definition: bmesh_query.c:232
bool BM_edge_in_face(const BMEdge *e, const BMFace *f)
Definition: bmesh_query.c:420
BMLoop * BM_face_edge_share_loop(BMFace *f, BMEdge *e)
Return the Loop Shared by Face and Edge.
Definition: bmesh_query.c:1115
bool BM_vert_in_face(BMVert *v, BMFace *f)
Definition: bmesh_query.c:306
BMLoop * BM_face_vert_share_loop(BMFace *f, BMVert *v)
Return the Loop Shared by Face and Vertex.
Definition: bmesh_query.c:1100
float BM_loop_point_side_of_edge_test(const BMLoop *l, const float co[3])
Definition: bmesh_query.c:238
BLI_INLINE bool BM_edge_is_boundary(const BMEdge *e) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
BLI_INLINE bool BM_loop_is_adjacent(const BMLoop *l_a, const BMLoop *l_b) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
BLI_INLINE bool BM_edge_is_wire(const BMEdge *e) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
ATTR_WARN_UNUSED_RESULT const BMVert * v2
ATTR_WARN_UNUSED_RESULT const BMLoop * l
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
ATTR_WARN_UNUSED_RESULT const BMVert * v
unsigned int U
Definition: btGjkEpa3.h:78
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)
Scene scene
void * user_data
static void knife_bvh_init(KnifeTool_OpData *kcd)
static void knifetool_disable_angle_snapping(KnifeTool_OpData *kcd)
static void knife_edge_append_face(KnifeTool_OpData *kcd, KnifeEdge *kfe, BMFace *f)
static void knife_find_line_hits(KnifeTool_OpData *kcd)
static KnifeEdge * get_bm_knife_edge(KnifeTool_OpData *kcd, BMEdge *e, Object *ob, uint base_index)
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 int knife_calculate_snap_ref_edges(KnifeTool_OpData *kcd)
static void knife_input_ray_segment(KnifeTool_OpData *kcd, const float mval[2], const float ofs, float r_origin[3], float r_origin_ofs[3])
struct Ref Ref
static bool knife_snap_angle_screen(KnifeTool_OpData *kcd)
#define KNIFE_FLT_EPS_PX_VERT
static void knife_constrain_axis(KnifeTool_OpData *kcd)
static void UNUSED_FUNCTION() knifetool_recast_cageco(KnifeTool_OpData *kcd, float mval[3], float r_cage[3])
static void knife_make_cuts(KnifeTool_OpData *kcd, Object *ob)
static void knifetool_init(ViewContext *vc, KnifeTool_OpData *kcd, Object **objects, const int objects_len, 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 void knife_bvh_free(KnifeTool_OpData *kcd)
static void calc_ortho_extent(KnifeTool_OpData *kcd)
static bool knife_snap_angle_relative(KnifeTool_OpData *kcd)
static void knifetool_init_obinfo(KnifeTool_OpData *kcd, Object *ob, uint base_index, bool use_tri_indices)
static void knife_bm_tri_cagecos_get(const KnifeTool_OpData *kcd, int base_index, int tri_index, float cos[3][3])
static float snap_v3_angle_plane(float r[3], const float v[3], const float v_ref[3], const float plane_no[3], float snap_step)
struct KnifePosData KnifePosData
static void knifetool_draw_visible_distances(const KnifeTool_OpData *kcd)
static ListBase * knife_empty_list(KnifeTool_OpData *kcd)
static void knifetool_draw_orientation_locking(const KnifeTool_OpData *kcd)
static void knife_project_v2(const KnifeTool_OpData *kcd, const float co[3], float sco[2])
static void knifetool_draw_dist_angle(const KnifeTool_OpData *kcd)
@ KNF_MODAL_CANCEL
@ KNF_MODAL_DEPTH_TEST_TOGGLE
@ KNF_MODAL_CYCLE_ANGLE_SNAP_EDGE
@ KNF_MODAL_SHOW_DISTANCE_ANGLE_TOGGLE
@ KNF_MODAL_X_AXIS
@ KNF_MODAL_MIDPOINT_OFF
@ KNF_MODAL_PANNING
@ KNF_MODAL_Y_AXIS
@ KNF_MODAL_NEW_CUT
@ KNF_MODAL_ANGLE_SNAP_TOGGLE
@ KNF_MODAL_IGNORE_SNAP_OFF
@ KNF_MODAL_UNDO
@ KNF_MODAL_ADD_CUT_CLOSED
@ KNF_MODAL_ADD_CUT
@ KNF_MODAL_IGNORE_SNAP_ON
@ KNF_MODAL_CONFIRM
@ KNF_MODAL_MIDPOINT_ON
@ KNF_MODAL_CUT_THROUGH_TOGGLE
@ KNF_MODAL_Z_AXIS
static void linehit_to_knifepos(KnifePosData *kpos, KnifeLineHit *lh)
void EDBM_mesh_knife(ViewContext *vc, Object **objects, const int objects_len, LinkNode *polys, bool use_tag, bool cut_through)
static int knife_sample_screen_density_from_closest_face(KnifeTool_OpData *kcd, const float radius, Object *ob, uint base_index, BMFace *f, const float cageco[3])
static bool knife_verts_edge_in_face(KnifeVert *v1, KnifeVert *v2, BMFace *f)
static void knifetool_undo(KnifeTool_OpData *kcd)
static void knifetool_update_mval_i(KnifeTool_OpData *kcd, const int mval_i[2])
static void knifetool_draw(const bContext *UNUSED(C), ARegion *UNUSED(region), void *arg)
#define KNIFE_FLT_EPS_PX_EDGE
wmKeyMap * knifetool_modal_keymap(wmKeyConfig *keyconf)
static void knifetool_finish_ex(KnifeTool_OpData *kcd)
static int linehit_compare(const void *vlh1, const void *vlh2)
static void knife_append_list(KnifeTool_OpData *kcd, ListBase *lst, void *elem)
static KnifeVert * knife_split_edge(KnifeTool_OpData *kcd, KnifeEdge *kfe, const float co[3], const float cageco[3], KnifeEdge **r_kfe)
static KnifeEdge * knife_find_closest_edge_of_face(KnifeTool_OpData *kcd, Object *ob, uint base_index, BMFace *f, float p[3], float cagep[3])
static void prepare_linehits_for_cut(KnifeTool_OpData *kcd)
static void knifetool_cancel(bContext *UNUSED(C), wmOperator *op)
#define F_ISECT_SET_UNKNOWN(f)
static void knife_make_face_cuts(KnifeTool_OpData *kcd, BMesh *bm, BMFace *f, ListBase *kfedges)
static int knifetool_invoke(bContext *C, wmOperator *op, const wmEvent *event)
static KnifeVert * get_bm_knife_vert(KnifeTool_OpData *kcd, BMVert *v, Object *ob, uint base_index)
static bool knife_snap_edge_constrained(KnifeTool_OpData *kcd, const float sco[3], const float kfv1_sco[2], const float kfv2_sco[2], float *r_dist_sq, float *r_lambda)
#define WM_MODALKEY(_id)
static void knifetool_update_mval(KnifeTool_OpData *kcd, const float mval[2])
static BMFace * knife_bvh_raycast(KnifeTool_OpData *kcd, const float co[3], const float dir[3], const float radius, float *r_dist, float r_hitout[3], float r_cagehit[3], uint *r_base_index)
static bool knife_bm_face_is_not_hidden(BMFace *f)
static void knifetool_exit(wmOperator *op)
static ListBase * knife_get_face_kedges(KnifeTool_OpData *kcd, Object *ob, uint base_index, BMFace *f)
static BMFace * knife_find_closest_face(KnifeTool_OpData *kcd, Object **r_ob, uint *r_base_index, bool *is_space, float r_co[3], float r_cageco[3])
static void knifetool_finish_single_post(KnifeTool_OpData *UNUSED(kcd), Object *ob)
static void knife_finish_cut(KnifeTool_OpData *kcd)
static BMElem * bm_elem_from_knife_vert(KnifeVert *kfv, KnifeEdge **r_kfe)
#define F_ISECT_SET_OUTSIDE(f)
static void knife_add_to_vert_edges(KnifeTool_OpData *kcd, KnifeEdge *kfe)
void MESH_OT_knife_tool(wmOperatorType *ot)
static void clip_to_ortho_planes(float v1[3], float v2[3], const float center[3], const float d)
#define KNIFE_FLT_EPSBIG
static void knife_bvh_raycast_cb(void *userdata, int index, const BVHTreeRay *ray, BVHTreeRayHit *hit)
static void knifetool_finish_single_pre(KnifeTool_OpData *kcd, Object *ob)
struct KnifeColors KnifeColors
static void knife_recalc_ortho(KnifeTool_OpData *kcd)
struct KnifeVert KnifeVert
static KnifeEdge * new_knife_edge(KnifeTool_OpData *kcd)
struct KnifeUndoFrame KnifeUndoFrame
#define KNIFE_MAX_ANGLE_SNAPPING_INCREMENT
static void knife_join_edge(KnifeEdge *newkfe, KnifeEdge *kfe)
static KnifeVert * knife_find_closest_vert_of_edge(KnifeTool_OpData *kcd, KnifeEdge *kfe, float p[3], float cagep[3])
static void add_hit_to_facehits(KnifeTool_OpData *kcd, GHash *facehits, BMFace *f, KnifeLineHit *hit)
static int get_lowest_face_tri(KnifeTool_OpData *kcd, BMFace *f)
#define KNIFE_DEFAULT_ANGLE_SNAPPING_INCREMENT
static void knifetool_raycast_planes(const KnifeTool_OpData *kcd, float r_v1[3], float r_v2[3])
static void knife_pos_data_clear(KnifePosData *kpd)
static Ref * find_ref(ListBase *lb, void *ref)
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_hitout[3], float r_cagehit[3], uint *r_base_index, bool(*filter_cb)(BMFace *f, void *userdata), void *filter_userdata)
static void knifetool_draw_visible_angles(const KnifeTool_OpData *kcd)
struct KnifeObjectInfo KnifeObjectInfo
static KnifeVert * new_knife_vert(KnifeTool_OpData *kcd, const float co[3], const float cageco[3])
static bool knife_add_single_cut__is_linehit_outside_face(BMFace *f, const KnifeLineHit *lh, const float co[3])
static void knifetool_draw_angle_snapping(const KnifeTool_OpData *kcd)
@ KNF_CONSTRAIN_AXIS_MODE_LOCAL
@ KNF_CONSTRAIN_AXIS_MODE_GLOBAL
@ KNF_CONSTRAIN_AXIS_MODE_NONE
static float snap_v2_angle(float r[2], const float v[2], const float v_ref[2], float angle_snap)
static void knife_reset_snap_angle_input(KnifeTool_OpData *kcd)
static void knife_cut_face(KnifeTool_OpData *kcd, BMFace *f, ListBase *hits)
static bool bm_ray_cast_cb_elem_not_in_face_check(BMFace *f, void *user_data)
@ KNF_CONSTRAIN_ANGLE_MODE_NONE
@ KNF_CONSTRAIN_ANGLE_MODE_SCREEN
@ KNF_CONSTRAIN_ANGLE_MODE_RELATIVE
static void knife_start_cut(KnifeTool_OpData *kcd)
static void knife_add_single_cut(KnifeTool_OpData *kcd, KnifeLineHit *lh1, KnifeLineHit *lh2, BMFace *f)
static int knife_update_active(KnifeTool_OpData *kcd)
static const int * knife_bm_tri_index_get(const KnifeTool_OpData *kcd, int base_index, int tri_index, int tri_index_buf[3])
#define KNIFE_FLT_EPS_PX_FACE
static float knife_snap_size(KnifeTool_OpData *kcd, float maxsize)
#define KNIFE_MIN_ANGLE_SNAPPING_INCREMENT
#define KMAXDIST
static BMElem * bm_elem_from_knife_edge(KnifeEdge *kfe)
static bool point_is_visible(KnifeTool_OpData *kcd, const float p[3], const float s[2], BMElem *ele_test)
static void knife_add_cut(KnifeTool_OpData *kcd)
static bool knife_snap_update_from_mval(KnifeTool_OpData *kcd, const float mval[2])
static void knifetool_finish(wmOperator *op)
static void knife_update_header(bContext *C, wmOperator *op, KnifeTool_OpData *kcd)
static void knife_append_list_no_dup(KnifeTool_OpData *kcd, ListBase *lst, void *elem)
static BMFace * knife_find_common_face(ListBase *faces1, ListBase *faces2)
struct KnifeEdge KnifeEdge
struct KnifeBVH KnifeBVH
#define KNIFE_FLT_EPS_SQUARED
@ KNF_MEASUREMENT_NONE
@ KNF_MEASUREMENT_BOTH
@ KNF_MEASUREMENT_DISTANCE
@ KNF_MEASUREMENT_ANGLE
static bool knife_ray_intersect_face(KnifeTool_OpData *kcd, const float s[2], const float v1[3], const float v2[3], Object *ob, uint base_index, BMFace *f, const float face_tol_sq, float hit_co[3], float hit_cageco[3])
static int knifetool_modal(bContext *C, wmOperator *op, const wmEvent *event)
#define KNIFE_FLT_EPS
struct KnifeTool_OpData KnifeTool_OpData
static void knifetool_free_obinfo(KnifeTool_OpData *kcd, uint base_index)
static void knife_interp_v3_v3v3(const KnifeTool_OpData *kcd, float r_co[3], const float v1[3], const float v2[3], float lambda_ss)
struct KnifeMeasureData KnifeMeasureData
static int sort_verts_by_dist_cb(void *co_p, const void *cur_a_p, const void *cur_b_p)
@ KNF_CONSTRAIN_AXIS_Y
@ KNF_CONSTRAIN_AXIS_Z
@ KNF_CONSTRAIN_AXIS_X
@ KNF_CONSTRAIN_AXIS_NONE
static void knifetool_disable_orientation_locking(KnifeTool_OpData *kcd)
static void knife_add_edge_faces_to_vert(KnifeTool_OpData *kcd, KnifeVert *kfv, BMEdge *e)
static bool coinciding_edges(BMEdge *e1, BMEdge *e2)
static bool knife_bm_face_is_select(BMFace *f)
static bool edbm_mesh_knife_point_isect(LinkNode *polys, const float cent_ss[2])
static void knife_init_colors(KnifeColors *colors)
static void set_linehit_depth(KnifeTool_OpData *kcd, KnifeLineHit *lh)
struct KnifeLineHit KnifeLineHit
static void knifetool_exit_ex(KnifeTool_OpData *kcd)
static void set_lowest_face_tri(KnifeTool_OpData *kcd, BMEditMesh *em, BMFace *f, int index)
static void knife_bm_tri_cagecos_get_worldspace(const KnifeTool_OpData *kcd, int base_index, int tri_index, float cos[3][3])
#define F_ISECT_IS_UNKNOWN(f)
uint pos
struct @653::@655 batch
int count
format
Definition: logImageCore.h:38
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
size_t(* MEM_allocN_len)(const void *vmemh)
Definition: mallocn.c:26
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:31
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:33
static char faces[256]
#define fabsf(x)
Definition: metal/compat.h:219
static unsigned c
Definition: RandGen.cpp:83
Intersection< segment > intersection
INLINE Rall1d< T, V, S > cos(const Rall1d< T, V, S > &arg)
Definition: rall1d.h:319
static double epsilon
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
static const pxr::TfToken density("density", pxr::TfToken::Immortal)
return ret
float RNA_float_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:4957
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:4863
int RNA_enum_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:5004
PropertyRNA * RNA_def_float(StructOrFunctionRNA *cont_, const char *identifier, float default_value, float hardmin, float hardmax, const char *ui_name, const char *ui_description, float softmin, float softmax)
Definition: rna_define.c:3836
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, bool default_value, const char *ui_name, const char *ui_description)
Definition: rna_define.c:3493
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
Definition: rna_define.c:1490
void RNA_def_property_subtype(PropertyRNA *prop, PropertySubType subtype)
Definition: rna_define.c:1534
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, int default_value, const char *ui_name, const char *ui_description)
Definition: rna_define.c:3783
#define min(a, b)
Definition: sort.c:35
_W64 unsigned int uintptr_t
Definition: stdint.h:119
_W64 int intptr_t
Definition: stdint.h:118
void * regiondata
struct ARegionType * type
BMVert * v1
Definition: bmesh_class.h:122
BMVert * v2
Definition: bmesh_class.h:122
short selectmode
Definition: BKE_editmesh.h:52
struct BMLoop *(* looptris)[3]
Definition: BKE_editmesh.h:48
struct BMesh * bm
Definition: BKE_editmesh.h:40
float no[3]
Definition: bmesh_class.h:271
struct BMVert * v
Definition: bmesh_class.h:153
struct BMEdge * e
Definition: bmesh_class.h:164
struct BMLoop * radial_next
Definition: bmesh_class.h:204
struct BMFace * f
Definition: bmesh_class.h:171
struct BMLoop * next
Definition: bmesh_class.h:233
float co[3]
Definition: bmesh_class.h:87
int totvert
Definition: bmesh_class.h:297
int totfacesel
Definition: bmesh_class.h:298
float co[3]
Definition: BLI_kdopbvh.h:68
float no[3]
Definition: BLI_kdopbvh.h:70
float origin[3]
Definition: BLI_kdopbvh.h:54
struct IsectRayPrecalc * isect_precalc
Definition: BLI_kdopbvh.h:60
float direction[3]
Definition: BLI_kdopbvh.h:56
float radius
Definition: BLI_kdopbvh.h:58
BMLoop *(* looptris)[3]
float uv[2]
void * filter_data
bool(* filter_cb)(BMFace *f, void *userdata)
uint base_index
BVHTree * tree
uchar edge_extra[3]
uchar yaxis[3]
uchar axis_extra[3]
uchar curpoint[3]
uchar xaxis[3]
uchar curpoint_a[4]
uchar zaxis[3]
uchar point_a[4]
uchar point[3]
uchar line[3]
uchar edge[3]
BMFace * basef
BMEdge * e
KnifeVert * v1
ListBase faces
KnifeVert * v2
float cagehit[3]
float schit[2]
KnifeEdge * kfe
KnifeVert * v
const float(* cagecos)[3]
const int(* tri_indices)[3]
BMEditMesh * em
KnifeVert * vert
BMFace * bmface
KnifeEdge * edge
KnifeEdge * snap_ref_edge
KnifePosData curr
KnifeMeasureData mdata
float angle_snapping_increment
BLI_mempool * refs
KnifePosData prev
enum KnifeTool_OpData::@467 mode
KnifeUndoFrame * undo
BLI_mempool * kverts
BLI_Stack * splitstack
BLI_Stack * undostack
struct KnifeTool_OpData::@466 edgenet
KnifeObjectInfo * objects_info
BLI_mempool * kedges
KnifeColors colors
KnifeLineHit * linehits
KnifePosData init
float ortho_extent_center[3]
KnifePosData pos
KnifeMeasureData mdata
float co[3]
float cageco[3]
BMVert * v
Object * ob
ListBase edges
bool is_splitting
ListBase faces
void * link
Definition: BLI_linklist.h:24
struct LinkNode * next
Definition: BLI_linklist.h:23
void * first
Definition: DNA_listBase.h:31
short idx_max
Definition: ED_numinput.h:20
float val[NUM_MAX_ELEMENTS]
Definition: ED_numinput.h:31
short val_flag[NUM_MAX_ELEMENTS]
Definition: ED_numinput.h:29
int unit_sys
Definition: ED_numinput.h:21
char str[NUM_STR_REP_LEN]
Definition: ED_numinput.h:40
int unit_type[NUM_MAX_ELEMENTS]
Definition: ED_numinput.h:23
int str_cur
Definition: ED_numinput.h:43
float imat[4][4]
float obmat[4][4]
void * data
void * ref
struct Ref * next
struct Ref * prev
float persmat[4][4]
float persinv[4][4]
float persmatob[4][4]
float clip_local[6][4]
float viewinv[4][4]
struct ToolSettings * toolsettings
struct UnitSettings unit
char transform_pivot_point
float clip_end
struct Depsgraph * depsgraph
Definition: ED_view3d.h:64
int mval[2]
Definition: ED_view3d.h:74
struct Scene * scene
Definition: ED_view3d.h:65
struct ARegion * region
Definition: ED_view3d.h:69
struct ViewLayer * view_layer
Definition: ED_view3d.h:66
struct Object * obedit
Definition: ED_view3d.h:68
struct wmWindow * win
Definition: ED_view3d.h:71
struct View3D * v3d
Definition: ED_view3d.h:70
struct RegionView3D * rv3d
Definition: ED_view3d.h:72
short val
Definition: WM_types.h:680
int mval[2]
Definition: WM_types.h:684
short prev_val
Definition: WM_types.h:722
short type
Definition: WM_types.h:678
const void * modal_items
int(* invoke)(struct bContext *, struct wmOperator *, const struct wmEvent *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:919
const char * name
Definition: WM_types.h:888
int(* modal)(struct bContext *, struct wmOperator *, const struct wmEvent *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:935
const char * idname
Definition: WM_types.h:890
bool(* poll)(struct bContext *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:943
void(* cancel)(struct bContext *, struct wmOperator *)
Definition: WM_types.h:927
struct StructRNA * srna
Definition: WM_types.h:969
const char * description
Definition: WM_types.h:893
struct ReportList * reports
struct PointerRNA * ptr
float max
void WM_cursor_modal_set(wmWindow *win, int val)
Definition: wm_cursors.c:191
void WM_cursor_modal_restore(wmWindow *win)
Definition: wm_cursors.c:200
@ WM_CURSOR_KNIFE
Definition: wm_cursors.h:31
wmEventHandler_Op * WM_event_add_modal_handler(bContext *C, wmOperator *op)
@ MOUSEPAN
@ EVT_MODAL_MAP
@ MOUSEZOOM
@ MOUSEROTATE
@ WHEELUPMOUSE
@ WHEELDOWNMOUSE
@ MOUSEMOVE
@ NDOF_MOTION
wmOperatorType * ot
Definition: wm_files.c:3479
wmKeyMap * WM_modalkeymap_find(wmKeyConfig *keyconf, const char *idname)
Definition: wm_keymap.c:914
void WM_modalkeymap_assign(wmKeyMap *km, const char *opname)
Definition: wm_keymap.c:985
const char * WM_bool_as_string(bool test)
Definition: wm_keymap.c:2052
wmKeyMap * WM_modalkeymap_ensure(wmKeyConfig *keyconf, const char *idname, const EnumPropertyItem *items)
Definition: wm_keymap.c:888
void wmOrtho2_region_pixelspace(const ARegion *region)
Definition: wm_subwindow.c:103