Blender V5.3
sculpt_trim.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10#include "GEO_mesh_boolean.hh"
11
12#include "BLI_math_geom_c.hh"
13#include "BLI_math_matrix.hh"
14#include "BLI_math_matrix_c.hh"
15#include "BLI_math_vector.hh"
16#include "BLI_math_vector_c.hh"
17#include "BLI_polyfill_2d.hh"
18#include "BLI_rect.hh"
19
20#include "BKE_brush.hh"
21#include "BKE_context.hh"
22#include "BKE_layer.hh"
23#include "BKE_lib_id.hh"
24#include "BKE_mesh.hh"
25#include "BKE_object.hh"
26#include "BKE_object_types.hh"
27#include "BKE_report.hh"
28
29#include "DEG_depsgraph.hh"
30
31#include "ED_sculpt.hh"
32
33#include "RNA_access.hh"
34#include "RNA_define.hh"
35
36#include "WM_api.hh"
37#include "WM_types.hh"
38
39#include "bmesh.hh"
40
41#include "../paint_intern.hh"
42#include "sculpt_face_set.hh"
43#include "sculpt_gesture.hh"
44#include "sculpt_intern.hh"
45#include "sculpt_islands.hh"
46
48
49enum class OperationType {
51 Difference = 1,
52 Union = 2,
53 Join = 3,
54};
55
56/* Intersect is not exposed in the UI because it does not work correctly with symmetry (it deletes
57 * the symmetrical part of the mesh in the first symmetry pass). */
60 "DIFFERENCE",
61 0,
62 "Difference",
63 "Use a difference boolean operation"},
64 {int(OperationType::Union), "UNION", 0, "Union", "Use a union boolean operation"},
66 "JOIN",
67 0,
68 "Join",
69 "Join the new mesh as separate geometry, without performing any boolean operation"},
70 {0, nullptr, 0, nullptr, nullptr},
71};
72
73enum class OrientationType {
74 View = 0,
75 Surface = 1,
76};
79 "VIEW",
80 0,
81 "View",
82 "Use the view to orientate the trimming shape"},
84 "SURFACE",
85 0,
86 "Surface",
87 "Use the surface normal to orientate the trimming shape"},
88 {0, nullptr, 0, nullptr, nullptr},
89};
90
91enum class ExtrudeMode {
92 Project = 0,
93 Fixed = 1,
94};
95
98 "PROJECT",
99 0,
100 "Project",
101 "Align trim geometry with the perspective of the current view for a tapered shape"},
102 {int(ExtrudeMode::Fixed),
103 "FIXED",
104 0,
105 "Fixed",
106 "Align trim geometry orthogonally for a shape with 90 degree angles"},
107 {0, nullptr, 0, nullptr, nullptr},
108};
109
112 "EXACT",
113 0,
114 "Exact",
115 "Slower solver with the best results for coplanar faces"},
117 "FLOAT",
118 0,
119 "Float",
120 "Simple solver with good performance, without support for overlapping geometry"},
122 "MANIFOLD",
123 0,
124 "Manifold",
125 "Fastest solver that works only on manifold meshes but gives better results"},
126 {0, nullptr, 0, nullptr, nullptr},
127};
128
149
150/* Recalculate the mesh normals for the generated trim mesh. */
151static void update_normals(gesture::GestureData &gesture_data)
152{
153 TrimOperation *trim_operation = reinterpret_cast<TrimOperation *>(gesture_data.operation);
154 Mesh *trim_mesh = trim_operation->mesh;
155
156 const BMAllocTemplate allocsize = BMALLOC_TEMPLATE_FROM_ME(trim_mesh);
157
158 BMeshCreateParams bm_create_params{};
159 bm_create_params.use_toolflags = true;
160 BMesh *bm = BM_mesh_create(&allocsize, &bm_create_params);
161
162 BMeshFromMeshParams bm_from_me_params{};
163 bm_from_me_params.calc_face_normal = true;
164 bm_from_me_params.calc_vert_normal = true;
165 BM_mesh_bm_from_me(bm, trim_mesh, &bm_from_me_params);
166
170 "recalc_face_normals faces=%hf",
173
174 BMeshToMeshParams convert_params{};
175 convert_params.calc_object_remap = false;
176 Mesh *result = BKE_mesh_from_bmesh_nomain(bm, &convert_params, trim_mesh);
177
179 BKE_id_free(nullptr, trim_mesh);
180 trim_operation->mesh = result;
181}
182
183/* Get the origin and normal that are going to be used for calculating the depth and position of
184 * the trimming geometry. */
186 float *r_origin,
187 float *r_normal)
188{
189 TrimOperation *trim_operation = reinterpret_cast<TrimOperation *>(gesture_data.operation);
190 /* Use the view origin and normal in world space. The trimming mesh coordinates are
191 * calculated in world space, aligned to the view, and then converted to object space to
192 * store them in the final trimming mesh which is going to be used in the boolean operation.
193 */
194 switch (trim_operation->orientation) {
196 mul_v3_m4v3(r_origin,
197 gesture_data.vc.obact->object_to_world().ptr(),
198 trim_operation->initial_location);
199 copy_v3_v3(r_normal, gesture_data.world_space_view_normal);
200 negate_v3(r_normal);
201 break;
203 mul_v3_m4v3(r_origin,
204 gesture_data.vc.obact->object_to_world().ptr(),
205 trim_operation->initial_location);
206 /* Transforming the normal does not take non uniform scaling into account. Sculpt mode is not
207 * expected to work on object with non uniform scaling. */
208 copy_v3_v3(r_normal, trim_operation->initial_normal);
209 mul_mat3_m4_v3(gesture_data.vc.obact->object_to_world().ptr(), r_normal);
210 break;
211 }
212}
213
214/* Calculates the depth of the drawn shape inside the scene. */
215static void calculate_depth(gesture::GestureData &gesture_data,
216 float &r_depth_front,
217 float &r_depth_back)
218{
219 TrimOperation *trim_operation = reinterpret_cast<TrimOperation *>(gesture_data.operation);
220
221 SculptSession &ss = *gesture_data.ss;
222 ViewContext &vc = gesture_data.vc;
223
224 float shape_plane[4];
225 float shape_origin[3];
226 float shape_normal[3];
227 get_origin_and_normal(gesture_data, shape_origin, shape_normal);
228 plane_from_point_normal_v3(shape_plane, shape_origin, shape_normal);
229
230 float depth_front = FLT_MAX;
231 float depth_back = -FLT_MAX;
232
234 const float4x4 &object_to_world = vc.obact->object_to_world();
235
236 for (const int i : positions.index_range()) {
237 /* Convert the coordinates to world space to calculate the depth. When generating the trimming
238 * mesh, coordinates are first calculated in world space, then converted to object space to
239 * store them. */
240 const float3 world_space_vco = math::transform_point(object_to_world, positions[i]);
241 const float dist = dist_signed_to_plane_v3(world_space_vco, shape_plane);
242 depth_front = std::min(dist, depth_front);
243 depth_back = std::max(dist, depth_back);
244 }
245
246 if (trim_operation->use_cursor_depth) {
247 float world_space_gesture_initial_location[3];
248 mul_v3_m4v3(world_space_gesture_initial_location,
249 object_to_world.ptr(),
250 trim_operation->initial_location);
251
252 float mid_point_depth;
253 if (trim_operation->orientation == OrientationType::View) {
254 mid_point_depth = trim_operation->initial_hit ?
255 dist_signed_to_plane_v3(world_space_gesture_initial_location,
256 shape_plane) :
257 (depth_back + depth_front) * 0.5f;
258 }
259 else {
260 /* When using normal orientation, if the stroke started over the mesh, position the mid point
261 * at 0 distance from the shape plane. This positions the trimming shape half inside of the
262 * surface. */
263 mid_point_depth = trim_operation->initial_hit ? 0.0f : (depth_back + depth_front) * 0.5f;
264 }
265
266 float depth_radius;
267
268 if (trim_operation->initial_hit) {
269 depth_radius = ss.cursor_radius;
270 }
271 else {
272 /* ss.cursor_radius is only valid if the stroke started
273 * over the sculpt mesh. If it's not we must
274 * compute the radius ourselves. See #81452.
275 */
276
277 depth_radius = object_space_radius_get(
278 vc, *gesture_data.paint, *gesture_data.brush, trim_operation->initial_location);
279 }
280
281 depth_front = mid_point_depth - depth_radius;
282 depth_back = mid_point_depth + depth_radius;
283 }
284
285 r_depth_front = depth_front;
286 r_depth_back = depth_back;
287}
288
295 const float bb_min[3],
296 const float bb_max[3],
297 const ARegion &region,
298 const RegionView3D &rv3d,
299 const Object &ob)
300{
301 int i, j, k;
302
304
305 /* return zero if the bounding box has non-positive volume */
306 if (bb_min[0] > bb_max[0] || bb_min[1] > bb_max[1] || bb_min[2] > bb_max[2]) {
307 return false;
308 }
309
310 const float4x4 projection = ED_view3d_ob_project_mat_get(&rv3d, &ob);
311
312 for (i = 0; i < 2; i++) {
313 for (j = 0; j < 2; j++) {
314 for (k = 0; k < 2; k++) {
315 float vec[3];
316 int proj_i[2];
317 vec[0] = i ? bb_min[0] : bb_max[0];
318 vec[1] = j ? bb_min[1] : bb_max[1];
319 vec[2] = k ? bb_min[2] : bb_max[2];
320 /* convert corner to screen space */
321 const float2 proj = ED_view3d_project_float_v2_m4(&region, vec, projection);
322 /* expand 2D rectangle */
323
324 /* we could project directly to int? */
325 proj_i[0] = proj[0];
326 proj_i[1] = proj[1];
327
328 BLI_rcti_do_minmax_v(rect, proj_i);
329 }
330 }
331 }
332
333 /* return false if the rectangle has non-positive area */
334 return rect->xmin < rect->xmax && rect->ymin < rect->ymax;
335}
336
337/* Calculates a scalar factor to use to ensure a drawn line gesture
338 * encompasses the entire object to be acted on. */
339static float calc_expand_factor(const gesture::GestureData &gesture_data)
340{
341 Object &object = *gesture_data.vc.obact;
342
343 rcti rect;
346 &rect, bounds.min, bounds.max, *gesture_data.vc.region, *gesture_data.vc.rv3d, object);
347
348 const float2 min_corner(rect.xmin, rect.ymin);
349 const float2 max_corner(rect.xmax, rect.ymax);
350
351 /* Multiply the screen space bounds by an arbitrary factor to ensure the created points are
352 * sufficiently far and enclose the mesh to be operated on. */
353 return math::distance(min_corner, max_corner) * 2.0f;
354}
355
356/* Converts a line gesture's points into usable screen points. */
358{
359 if (gesture_data.shape_type != gesture::ShapeType::Line) {
360 return gesture_data.gesture_points;
361 }
362
363 const float expand_factor = calc_expand_factor(gesture_data);
364
365 float2 start(gesture_data.gesture_points[0]);
366 float2 end(gesture_data.gesture_points[1]);
367
368 const float2 dir = math::normalize(end - start);
369
370 if (!gesture_data.line.use_side_planes) {
371 end = end + dir * expand_factor;
372 start = start - dir * expand_factor;
373 }
374
375 float2 perp(dir.y, -dir.x);
376
377 if (gesture_data.line.flip) {
378 perp *= -1;
379 }
380
381 const float2 parallel_start = start + perp * expand_factor;
382 const float2 parallel_end = end + perp * expand_factor;
383
384 return {start, end, parallel_end, parallel_start};
385}
386
387static void generate_geometry(gesture::GestureData &gesture_data)
388{
389 TrimOperation *trim_operation = reinterpret_cast<TrimOperation *>(gesture_data.operation);
390 ViewContext &vc = gesture_data.vc;
391 ARegion *region = vc.region;
392
393 const Array<float2> screen_points = gesture_to_screen_points(gesture_data);
394 BLI_assert(screen_points.size() > 1);
395
396 const int trim_totverts = screen_points.size() * 2;
397 const int trim_faces_nums = (2 * (screen_points.size() - 2)) + (2 * screen_points.size());
398 trim_operation->mesh = BKE_mesh_new_nomain(
399 trim_totverts, 0, trim_faces_nums, trim_faces_nums * 3);
400 trim_operation->true_mesh_co = MEM_new_array_uninitialized<float[3]>(trim_totverts, "mesh orco");
401
402 float shape_origin[3];
403 float shape_normal[3];
404 float shape_plane[4];
405 get_origin_and_normal(gesture_data, shape_origin, shape_normal);
406 plane_from_point_normal_v3(shape_plane, shape_origin, shape_normal);
407
408 const float (*ob_imat)[4] = vc.obact->world_to_object().ptr();
409
410 /* Write vertices coordinates OperationType::Difference for the front face. */
411 MutableSpan<float3> positions = trim_operation->mesh->vert_positions_for_write();
412
413 float depth_front;
414 float depth_back;
415 calculate_depth(gesture_data, depth_front, depth_back);
416
417 if (!trim_operation->use_cursor_depth) {
418 float pad_factor = (depth_back - depth_front) * 0.01f + 0.001f;
419
420 /* When using cursor depth, don't modify the depth set by the cursor radius. If full depth is
421 * used, adding a little padding to the trimming shape can help avoiding booleans with coplanar
422 * faces. */
423 depth_front -= pad_factor;
424 depth_back += pad_factor;
425 }
426
427 float depth_point[3];
428
429 /* Get origin point for OrientationType::View.
430 * NOTE: for projection extrusion we add depth_front here
431 * instead of in the loop.
432 */
433 if (trim_operation->extrude_mode == ExtrudeMode::Fixed) {
434 copy_v3_v3(depth_point, shape_origin);
435 }
436 else {
437 madd_v3_v3v3fl(depth_point, shape_origin, shape_normal, depth_front);
438 }
439
440 for (const int i : screen_points.index_range()) {
441 float new_point[3];
442 if (trim_operation->orientation == OrientationType::View) {
443 ED_view3d_win_to_3d(vc.v3d, region, depth_point, screen_points[i], new_point);
444
445 /* For fixed mode we add the shape normal here to avoid projection errors. */
446 if (trim_operation->extrude_mode == ExtrudeMode::Fixed) {
447 madd_v3_v3fl(new_point, shape_normal, depth_front);
448 }
449 }
450 else {
451 ED_view3d_win_to_3d_on_plane(region, shape_plane, screen_points[i], false, new_point);
452 madd_v3_v3fl(new_point, shape_normal, depth_front);
453 }
454
455 copy_v3_v3(positions[i], new_point);
456 }
457
458 /* Write vertices coordinates for the back face. */
459 madd_v3_v3v3fl(depth_point, shape_origin, shape_normal, depth_back);
460 for (const int i : screen_points.index_range()) {
461 float new_point[3];
462
463 if (trim_operation->extrude_mode == ExtrudeMode::Project) {
464 if (trim_operation->orientation == OrientationType::View) {
465 ED_view3d_win_to_3d(vc.v3d, region, depth_point, screen_points[i], new_point);
466 }
467 else {
468 ED_view3d_win_to_3d_on_plane(region, shape_plane, screen_points[i], false, new_point);
469 madd_v3_v3fl(new_point, shape_normal, depth_back);
470 }
471 }
472 else {
473 copy_v3_v3(new_point, positions[i]);
474 float dist = dist_signed_to_plane_v3(new_point, shape_plane);
475
476 madd_v3_v3fl(new_point, shape_normal, depth_back - dist);
477 }
478
479 copy_v3_v3(positions[i + screen_points.size()], new_point);
480 }
481
482 /* Project to object space. */
483 for (int i = 0; i < screen_points.size() * 2; i++) {
484 float new_point[3];
485
486 copy_v3_v3(new_point, positions[i]);
487 mul_v3_m4v3(positions[i], ob_imat, new_point);
488 mul_v3_m4v3(trim_operation->true_mesh_co[i], ob_imat, new_point);
489 }
490
491 /* Get the triangulation for the front/back poly. */
492 const int face_tris_num = bke::mesh::face_triangles_num(screen_points.size());
493 Array<uint3> tris(face_tris_num);
494 BLI_polyfill_calc(reinterpret_cast<const float (*)[2]>(screen_points.data()),
495 screen_points.size(),
496 0,
497 reinterpret_cast<uint(*)[3]>(tris.data()));
498
499 /* Write the front face triangle indices. */
500 MutableSpan<int> face_offsets = trim_operation->mesh->face_offsets_for_write();
501 MutableSpan<int> corner_verts = trim_operation->mesh->corner_verts_for_write();
502 int face_index = 0;
503 int corner = 0;
504 for (const int i : tris.index_range()) {
505 face_offsets[face_index] = corner;
506 corner_verts[corner + 0] = tris[i][0];
507 corner_verts[corner + 1] = tris[i][1];
508 corner_verts[corner + 2] = tris[i][2];
509 face_index++;
510 corner += 3;
511 }
512
513 /* Write the back face triangle indices. */
514 for (const int i : tris.index_range()) {
515 face_offsets[face_index] = corner;
516 corner_verts[corner + 0] = tris[i][0] + screen_points.size();
517 corner_verts[corner + 1] = tris[i][1] + screen_points.size();
518 corner_verts[corner + 2] = tris[i][2] + screen_points.size();
519 face_index++;
520 corner += 3;
521 }
522
523 /* Write the indices for the lateral triangles. */
524 for (const int i : screen_points.index_range()) {
525 face_offsets[face_index] = corner;
526 int current_index = i;
527 int next_index = current_index + 1;
528 if (next_index >= screen_points.size()) {
529 next_index = 0;
530 }
531 corner_verts[corner + 0] = next_index + screen_points.size();
532 corner_verts[corner + 1] = next_index;
533 corner_verts[corner + 2] = current_index;
534 face_index++;
535 corner += 3;
536 }
537
538 for (const int i : screen_points.index_range()) {
539 face_offsets[face_index] = corner;
540 int current_index = i;
541 int next_index = current_index + 1;
542 if (next_index >= screen_points.size()) {
543 next_index = 0;
544 }
545 corner_verts[corner + 0] = current_index;
546 corner_verts[corner + 1] = current_index + screen_points.size();
547 corner_verts[corner + 2] = next_index + screen_points.size();
548 face_index++;
549 corner += 3;
550 }
551
552 bke::mesh_smooth_set(*trim_operation->mesh, false);
553 bke::mesh_calc_edges(*trim_operation->mesh, false, false);
554 update_normals(gesture_data);
555}
556
557static void gesture_begin(bContext &C, wmOperator &op, gesture::GestureData &gesture_data)
558{
559 const Scene &scene = *CTX_data_scene(&C);
560 Object *object = gesture_data.vc.obact;
561 SculptSession &ss = *object->runtime->sculpt_session;
562 const bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(*object);
563
564 switch (pbvh.type()) {
567 break;
568 default:
570 }
571
572 generate_geometry(gesture_data);
574 undo::geometry_begin(scene, *gesture_data.vc.obact, &op);
575}
576
586
587static void apply_trim(gesture::GestureData &gesture_data)
588{
589 TrimOperation *trim_operation = reinterpret_cast<TrimOperation *>(gesture_data.operation);
590 Object *object = gesture_data.vc.obact;
591 Mesh &sculpt_mesh = *id_cast<Mesh *>(object->data);
592 Mesh &trim_mesh = *trim_operation->mesh;
593
595 switch (trim_operation->mode) {
598 break;
601 break;
604 break;
606 apply_join_operation(*object, sculpt_mesh, trim_mesh);
607 return;
608 }
609
611 op_params.boolean_mode = boolean_op;
612 op_params.no_self_intersections = true;
613 op_params.watertight = false;
614 op_params.no_nested_components = true;
617 {&sculpt_mesh, &trim_mesh},
620 op_params,
621 trim_operation->solver_mode,
622 nullptr,
623 &error);
625 BKE_report(trim_operation->reports, RPT_ERROR, "Solver requires a manifold mesh");
626 return;
627 }
630 trim_operation->reports, RPT_ERROR, "Boolean result is too big for solver to handle");
631 return;
632 }
635 trim_operation->reports, RPT_ERROR, "Boolean solver not available (compiled without it)");
636 return;
637 }
639 BKE_report(trim_operation->reports, RPT_ERROR, "Unknown boolean error");
640 return;
641 }
642
643 BKE_mesh_nomain_to_mesh(result, &sculpt_mesh, object);
644}
645
647{
648 TrimOperation *trim_operation = reinterpret_cast<TrimOperation *>(gesture_data.operation);
649 Mesh *trim_mesh = trim_operation->mesh;
650 MutableSpan<float3> positions = trim_mesh->vert_positions_for_write();
651 for (int i = 0; i < trim_mesh->verts_num; i++) {
652 positions[i] = symmetry_flip(trim_operation->true_mesh_co[i], gesture_data.symmpass);
653 }
654 update_normals(gesture_data);
655 apply_trim(gesture_data);
656}
657
658static void free_geometry(gesture::GestureData &gesture_data)
659{
660 TrimOperation *trim_operation = reinterpret_cast<TrimOperation *>(gesture_data.operation);
661 BKE_id_free(nullptr, trim_operation->mesh);
662 MEM_delete(trim_operation->true_mesh_co);
663}
664
665static void gesture_end(bContext & /*C*/, gesture::GestureData &gesture_data)
666{
667 Object *object = gesture_data.vc.obact;
668 Mesh *mesh = id_cast<Mesh *>(object->data);
669
670 /* Assign a new face set ID to the new faces created by the trim operation. */
671 const int next_face_set_id = face_set::find_next_available_id(*object);
672 face_set::initialize_none_to_id(mesh, next_face_set_id);
673
674 free_geometry(gesture_data);
675
676 undo::geometry_end(*object);
680}
681
682static void init_operation(gesture::GestureData &gesture_data, wmOperator &op)
683{
684 TrimOperation *trim_operation = reinterpret_cast<TrimOperation *>(gesture_data.operation);
685 trim_operation->reports = op.reports;
686 trim_operation->op.begin = gesture_begin;
688 trim_operation->op.end = gesture_end;
689
690 trim_operation->mode = OperationType(RNA_enum_get(op.ptr, "trim_mode"));
691 trim_operation->use_cursor_depth = RNA_boolean_get(op.ptr, "use_cursor_depth");
692 trim_operation->orientation = OrientationType(RNA_enum_get(op.ptr, "trim_orientation"));
693 trim_operation->extrude_mode = ExtrudeMode(RNA_enum_get(op.ptr, "trim_extrude_mode"));
695 RNA_enum_get(op.ptr, "trim_solver"));
696
697 /* If the cursor was not over the mesh, force the orientation to view. */
698 if (!trim_operation->initial_hit) {
699 trim_operation->orientation = OrientationType::View;
700 }
701
702 if (gesture_data.shape_type == gesture::ShapeType::Line) {
703 /* Line gestures only support Difference, no extrusion. */
704 trim_operation->mode = OperationType::Difference;
705 }
706}
707
709{
710 PropertyRNA *prop;
711
712 prop = RNA_def_int_vector(ot->srna,
713 "location",
714 2,
715 nullptr,
716 INT_MIN,
717 INT_MAX,
718 "Location",
719 "Mouse location",
720 INT_MIN,
721 INT_MAX);
723
724 RNA_def_enum(ot->srna,
725 "trim_mode",
728 "Trim Mode",
729 nullptr);
731 ot->srna,
732 "use_cursor_depth",
733 false,
734 "Use Cursor for Depth",
735 "Use cursor location and radius for the dimensions and position of the trimming shape");
736 RNA_def_enum(ot->srna,
737 "trim_orientation",
740 "Shape Orientation",
741 nullptr);
742 RNA_def_enum(ot->srna,
743 "trim_extrude_mode",
746 "Extrude Mode",
747 nullptr);
748
749 RNA_def_enum(ot->srna,
750 "trim_solver",
753 "Solver",
754 nullptr);
755}
756
757static bool can_invoke(const bContext &C)
758{
759 const View3D &v3d = *CTX_wm_view3d(&C);
760 const Base &base = *CTX_data_active_base(&C);
761 if (!BKE_base_is_visible(&v3d, &base)) {
762 return false;
763 }
764
765 return true;
766}
767
768static void report_invalid_mode(const bke::pbvh::Type pbvh_type, ReportList &reports)
769{
770 if (pbvh_type == bke::pbvh::Type::BMesh) {
771 BKE_report(&reports, RPT_ERROR, "Not supported in dynamic topology mode");
772 }
773 else if (pbvh_type == bke::pbvh::Type::Grids) {
774 BKE_report(&reports, RPT_ERROR, "Not supported in multi-resolution mode");
775 }
776 else {
778 }
779}
780
781static bool can_exec(const bContext &C, ReportList &reports)
782{
783 const Object &object = *CTX_data_active_object(&C);
785 if (pbvh.type() != bke::pbvh::Type::Mesh) {
786 /* Not supported in Multires and Dyntopo. */
787 report_invalid_mode(pbvh.type(), reports);
788 return false;
789 }
790
791 if (id_cast<const Mesh *>(object.data)->faces_num == 0) {
792 /* No geometry to trim or to detect a valid position for the trimming shape. */
793 return false;
794 }
795
796 return true;
797}
798
800 const wmOperator &op,
801 gesture::GestureData &gesture_data)
802{
804
806
807 int mval[2];
808 RNA_int_get_array(op.ptr, "location", mval);
809
810 const float mval_fl[2] = {float(mval[0]), float(mval[1])};
811
812 TrimOperation *trim_operation = reinterpret_cast<TrimOperation *>(gesture_data.operation);
813 const std::optional<CursorGeometryInfo> cgi = cursor_geometry_info_update(&C, mval_fl, false);
814
815 trim_operation->initial_hit = cgi.has_value();
816 if (trim_operation->initial_hit) {
817 copy_v3_v3(trim_operation->initial_location, cgi->location);
818 copy_v3_v3(trim_operation->initial_normal, cgi->normal);
819 }
820}
821
823{
824 if (!can_exec(*C, *op->reports)) {
825 return OPERATOR_CANCELLED;
826 }
827
828 std::unique_ptr<gesture::GestureData> gesture_data = gesture::init_from_box(C, op);
829 if (!gesture_data) {
830 return OPERATOR_CANCELLED;
831 }
832
833 gesture_data->operation = reinterpret_cast<gesture::Operation *>(
835 initialize_cursor_info(*C, *op, *gesture_data);
836 init_operation(*gesture_data, *op);
837
838 gesture::apply(*C, *gesture_data, *op);
839 return OPERATOR_FINISHED;
840}
841
843{
844 if (!can_invoke(*C)) {
845 return OPERATOR_CANCELLED;
846 }
847
848 RNA_int_set_array(op->ptr, "location", event->mval);
849
850 return WM_gesture_box_invoke(C, op, event);
851}
852
854{
855 if (!can_exec(*C, *op->reports)) {
856 return OPERATOR_CANCELLED;
857 }
858
859 std::unique_ptr<gesture::GestureData> gesture_data = gesture::init_from_lasso(C, op);
860 if (!gesture_data) {
861 return OPERATOR_CANCELLED;
862 }
863
864 gesture_data->operation = reinterpret_cast<gesture::Operation *>(
866 initialize_cursor_info(*C, *op, *gesture_data);
867 init_operation(*gesture_data, *op);
868
869 gesture::apply(*C, *gesture_data, *op);
870 return OPERATOR_FINISHED;
871}
872
874{
875 if (!can_invoke(*C)) {
876 return OPERATOR_CANCELLED;
877 }
878
879 RNA_int_set_array(op->ptr, "location", event->mval);
880
881 return WM_gesture_lasso_invoke(C, op, event);
882}
883
885{
886 if (!can_exec(*C, *op->reports)) {
887 return OPERATOR_CANCELLED;
888 }
889
890 std::unique_ptr<gesture::GestureData> gesture_data = gesture::init_from_line(C, op);
891 if (!gesture_data) {
892 return OPERATOR_CANCELLED;
893 }
894
895 gesture_data->operation = reinterpret_cast<gesture::Operation *>(
897
898 initialize_cursor_info(*C, *op, *gesture_data);
899 init_operation(*gesture_data, *op);
900 gesture::apply(*C, *gesture_data, *op);
901 return OPERATOR_FINISHED;
902}
903
905{
906 if (!can_invoke(*C)) {
907 return OPERATOR_CANCELLED;
908 }
909
910 RNA_int_set_array(op->ptr, "location", event->mval);
911
913}
914
916{
917 if (!can_exec(*C, *op->reports)) {
918 return OPERATOR_CANCELLED;
919 }
920
921 std::unique_ptr<gesture::GestureData> gesture_data = gesture::init_from_polyline(C, op);
922 if (!gesture_data) {
923 return OPERATOR_CANCELLED;
924 }
925
926 gesture_data->operation = reinterpret_cast<gesture::Operation *>(
928 initialize_cursor_info(*C, *op, *gesture_data);
929 init_operation(*gesture_data, *op);
930
931 gesture::apply(*C, *gesture_data, *op);
932 return OPERATOR_FINISHED;
933}
934
936{
937 if (!can_invoke(*C)) {
938 return OPERATOR_CANCELLED;
939 }
940
941 RNA_int_set_array(op->ptr, "location", event->mval);
942
943 return WM_gesture_polyline_invoke(C, op, event);
944}
945
947{
948 ot->name = "Trim Lasso Gesture";
949 ot->idname = "SCULPT_OT_trim_lasso_gesture";
950 ot->description = "Execute a boolean operation on the mesh and a shape defined by the cursor";
951
955
957
959
960 /* Properties. */
963
965}
966
968{
969 ot->name = "Trim Box Gesture";
970 ot->idname = "SCULPT_OT_trim_box_gesture";
971 ot->description =
972 "Execute a boolean operation on the mesh and a rectangle defined by the cursor";
973
977
979
980 ot->flag = OPTYPE_REGISTER;
981
982 /* Properties. */
985
987}
988
990{
991 ot->name = "Trim Line Gesture";
992 ot->idname = "SCULPT_OT_trim_line_gesture";
993 ot->description = "Remove a portion of the mesh on one side of a line";
994
998
1000
1001 ot->flag = OPTYPE_REGISTER;
1002
1003 /* Properties. */
1006
1008}
1009
1011{
1012 ot->name = "Trim Polyline Gesture";
1013 ot->idname = "SCULPT_OT_trim_polyline_gesture";
1014 ot->description =
1015 "Execute a boolean operation on the mesh and a polygonal shape defined by the cursor";
1016
1020
1022
1023 ot->flag = OPTYPE_REGISTER;
1024
1025 /* Properties. */
1028
1030}
1031} // namespace blender::ed::sculpt_paint::trim
General operations, lookup, etc. for blender objects.
#define C
Definition RandGen.cpp:29
#define BLI_assert_unreachable()
#define BMALLOC_TEMPLATE_FROM_ME(...)
#define BMO_FLAG_DEFAULTS
DBVT_INLINE bool Intersect(const btDbvtAabbMm &a, const btDbvtAabbMm &b)
Definition btDbvt.h:621
int64_t size() const
Definition BLI_array.hh:267
Array(Allocator allocator={}) noexcept
Definition BLI_array.hh:78
const T * data() const
Definition BLI_array.hh:323
IndexRange index_range() const
Definition BLI_array.hh:371
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
#define FLT_MAX
#define INT_MAX
int face_triangles_num(const int face_size)
Definition BKE_mesh.hh:350
pbvh::Tree * pbvh_get(Object &object)
Span< float3 > vert_positions_eval(const Depsgraph &depsgraph, const Object &object_orig)
Definition pbvh.cc:933
void mesh_smooth_set(Mesh &mesh, bool use_smooth, bool keep_sharp_edges=false)
void mesh_calc_edges(Mesh &mesh, bool keep_existing_edges, bool select_new_edges)
void initialize_none_to_id(Mesh *mesh, int new_id)
int find_next_available_id(const Mesh &mesh)
std::unique_ptr< GestureData > init_from_box(bContext *C, wmOperator *op)
void operator_properties(wmOperatorType *ot, ShapeType shapeType)
std::unique_ptr< GestureData > init_from_polyline(bContext *C, wmOperator *op)
std::unique_ptr< GestureData > init_from_line(bContext *C, const wmOperator *op)
void apply(bContext &C, GestureData &gesture_data, wmOperator &op)
std::unique_ptr< GestureData > init_from_lasso(bContext *C, wmOperator *op)
void invalidate(SculptSession &ss)
Definition sculpt.cc:6325
void SCULPT_OT_trim_lasso_gesture(wmOperatorType *ot)
static void init_operation(gesture::GestureData &gesture_data, wmOperator &op)
static bool paint_convert_bb_to_rect(rcti *rect, const float bb_min[3], const float bb_max[3], const ARegion &region, const RegionView3D &rv3d, const Object &ob)
static void operator_properties(wmOperatorType *ot)
static void generate_geometry(gesture::GestureData &gesture_data)
static void initialize_cursor_info(bContext &C, const wmOperator &op, gesture::GestureData &gesture_data)
static EnumPropertyItem extrude_modes[]
static void get_origin_and_normal(gesture::GestureData &gesture_data, float *r_origin, float *r_normal)
static void apply_join_operation(Object &object, Mesh &sculpt_mesh, Mesh &trim_mesh)
static void gesture_end(bContext &, gesture::GestureData &gesture_data)
static void gesture_begin(bContext &C, wmOperator &op, gesture::GestureData &gesture_data)
static void apply_trim(gesture::GestureData &gesture_data)
static wmOperatorStatus gesture_line_invoke(bContext *C, wmOperator *op, const wmEvent *event)
static bool can_exec(const bContext &C, ReportList &reports)
void SCULPT_OT_trim_box_gesture(wmOperatorType *ot)
static float calc_expand_factor(const gesture::GestureData &gesture_data)
static void gesture_apply_for_symmetry_pass(bContext &, gesture::GestureData &gesture_data)
static bool can_invoke(const bContext &C)
static void calculate_depth(gesture::GestureData &gesture_data, float &r_depth_front, float &r_depth_back)
static void report_invalid_mode(const bke::pbvh::Type pbvh_type, ReportList &reports)
static void free_geometry(gesture::GestureData &gesture_data)
static EnumPropertyItem operation_types[]
static wmOperatorStatus gesture_box_invoke(bContext *C, wmOperator *op, const wmEvent *event)
static wmOperatorStatus gesture_polyline_invoke(bContext *C, wmOperator *op, const wmEvent *event)
void SCULPT_OT_trim_line_gesture(wmOperatorType *ot)
static wmOperatorStatus gesture_lasso_exec(bContext *C, wmOperator *op)
static wmOperatorStatus gesture_line_exec(bContext *C, wmOperator *op)
static wmOperatorStatus gesture_lasso_invoke(bContext *C, wmOperator *op, const wmEvent *event)
static wmOperatorStatus gesture_box_exec(bContext *C, wmOperator *op)
static const EnumPropertyItem solver_items[]
static EnumPropertyItem orientation_types[]
static wmOperatorStatus gesture_polyline_exec(bContext *C, wmOperator *op)
void SCULPT_OT_trim_polyline_gesture(wmOperatorType *ot)
static void update_normals(gesture::GestureData &gesture_data)
static Array< float2 > gesture_to_screen_points(gesture::GestureData &gesture_data)
void geometry_begin(const Scene &scene, Object &ob, const wmOperator *op)
void vert_random_access_ensure(Object &object)
Definition sculpt.cc:154
float3 symmetry_flip(const float3 &src, const ePaintSymmetryFlags symm)
bool sculpt_mode_poll_view3d(bContext *C)
Definition sculpt.cc:3806
float object_space_radius_get(const ViewContext &vc, const Paint &paint, const Brush &brush, const float3 &location, const float scale_factor)
Definition sculpt.cc:121
std::optional< CursorGeometryInfo > cursor_geometry_info_update(bContext *C, const float2 &mval, const bool use_sampled_normal)
Definition sculpt.cc:4584
Mesh * mesh_boolean(Span< const Mesh * > meshes, Span< float4x4 > transforms, Span< Array< short > > material_remaps, BooleanOpParameters op_params, Solver solver, Vector< int > *r_intersecting_edges, BooleanError *r_error)
bke::GeometrySet join_geometries(Span< bke::GeometrySet > geometries, const bke::AttributeFilter &attribute_filter, const std::optional< Span< bke::GeometryComponent::Type > > &component_types_to_join=std::nullopt, bool allow_merging_instance_references=true)
T distance(const T &a, const T &b)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
MINLINE void madd_v3_v3v3fl(float r[3], const float a[3], const float b[3], float f)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BM_mesh_elem_hflag_enable_all(BMesh *bm, const char htype, const char hflag, const bool respecthide)
int RNA_enum_get(PointerRNA *ptr, const char *name)
wmOperatorStatus WM_gesture_polyline_invoke(bContext *C, wmOperator *op, const wmEvent *event)
void BM_mesh_free(BMesh *bm)
BMesh Free Mesh.
wmOperatorStatus WM_gesture_box_invoke(bContext *C, wmOperator *op, const wmEvent *event)
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
bool BKE_base_is_visible(const View3D *v3d, const Base *base)
float4x4 ED_view3d_ob_project_mat_get(const RegionView3D *rv3d, const Object *ob)
void mul_v3_m4v3(float r[3], const float mat[4][4], const float vec[3])
float dist_signed_to_plane_v3(const float p[3], const float plane[4])
Definition math_geom.cc:511
void BLI_polyfill_calc(const float(*coords)[2], unsigned int coords_num, int coords_sign, unsigned int(*r_tris)[3])
void BKE_mesh_nomain_to_mesh(Mesh *mesh_src, Mesh *mesh_dst, Object *ob, bool process_shape_keys=true)
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
PropertyRNA * RNA_def_int_vector(StructOrFunctionRNA *cont_, const char *identifier, const int len, const int *default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
Mesh * BKE_mesh_from_bmesh_nomain(BMesh *bm, const BMeshToMeshParams *params, const Mesh *me_settings)
void BLI_rcti_init_minmax(struct rcti *rect)
Definition rct.cc:488
void WM_operator_properties_border(wmOperatorType *ot)
float2 ED_view3d_project_float_v2_m4(const ARegion *region, const float co[3], const float4x4 &mat)
void BM_mesh_elem_hflag_disable_all(BMesh *bm, const char htype, const char hflag, const bool respecthide)
wmOperatorStatus WM_gesture_straightline_active_side_invoke(bContext *C, wmOperator *op, const wmEvent *event)
wmOperatorStatus WM_gesture_lasso_modal(bContext *C, wmOperator *op, const wmEvent *event)
MatBase< float, 4, 4 > float4x4
void BLI_rcti_do_minmax_v(struct rcti *rect, const int xy[2])
Definition rct.cc:500
Base * CTX_data_active_base(const bContext *C)
void plane_from_point_normal_v3(float r_plane[4], const float plane_co[3], const float plane_no[3])
Definition math_geom.cc:225
void BKE_sculptsession_free_pbvh(Object &object)
void BM_mesh_bm_from_me(BMesh *bm, const Mesh *mesh, const BMeshFromMeshParams *params)
Mesh -> BMesh.
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
void RNA_int_set_array(PointerRNA *ptr, const char *name, const int *values)
VecBase< float, 2 > float2
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
BMesh const char void * data
void RNA_int_get_array(PointerRNA *ptr, const char *name, int *values)
bool ED_view3d_win_to_3d_on_plane(const ARegion *region, const float plane[4], const float mval[2], bool do_clip, float r_out[3])
void BKE_mesh_batch_cache_dirty_tag(Mesh *mesh, eMeshBatchDirtyMode mode)
void WM_operator_properties_gesture_straightline(wmOperatorType *ot, int cursor)
Mesh * BKE_mesh_new_nomain(int verts_num, int edges_num, int faces_num, int corners_num)
MINLINE void negate_v3(float r[3])
bool BMO_op_callf(BMesh *bm, int flag, const char *fmt,...)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
void BKE_id_free(Main *bmain, void *idv)
wmOperatorStatus WM_gesture_polyline_modal(bContext *C, wmOperator *op, const wmEvent *event)
void mul_mat3_m4_v3(const float mat[4][4], float r[3])
wmOperatorStatus WM_gesture_box_modal(bContext *C, wmOperator *op, const wmEvent *event)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
@ WM_CURSOR_EDIT
Definition wm_cursors.hh:21
wmOperatorStatus WM_gesture_lasso_invoke(bContext *C, wmOperator *op, const wmEvent *event)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
@ PROP_SKIP_SAVE
Definition RNA_types.hh:414
@ PROP_HIDDEN
Definition RNA_types.hh:408
Scene * CTX_data_scene(const bContext *C)
std::optional< Bounds< float3 > > BKE_object_boundbox_get(const Object *ob)
Definition object.cc:3726
wmOperatorStatus WM_gesture_straightline_oneshot_modal(bContext *C, wmOperator *op, const wmEvent *event)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_DEPENDS_ON_CURSOR
Definition WM_types.hh:228
VecBase< float, 3 > float3
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
void WM_operator_properties_gesture_polyline(wmOperatorType *ot)
@ BKE_MESH_BATCH_DIRTY_ALL
Definition BKE_mesh.h:48
void ED_view3d_win_to_3d(const View3D *v3d, const ARegion *region, const float depth_pt[3], const float mval[2], float r_out[3])
BMesh * BM_mesh_create(const BMAllocTemplate *allocsize, const BMeshCreateParams *params)
BMesh Make Mesh.
Object * CTX_data_active_object(const bContext *C)
View3D * CTX_wm_view3d(const bContext *C)
void WM_operator_properties_gesture_lasso(wmOperatorType *ot)
@ Union
Definition types.hh:109
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
RegionView3D * rv3d
Definition ED_view3d.hh:86
Depsgraph * depsgraph
Definition ED_view3d.hh:78
GeometryComponent & get_component_for_write(GeometryComponent::Type component_type)
static GeometrySet from_mesh(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
void(*) end(bContext &, GestureData &)
void(*) apply_for_symmetry_pass(bContext &, GestureData &)
void(*) begin(bContext &, wmOperator &, GestureData &)
blender::geometry::boolean::Solver solver_mode
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
wmOperatorStatus(*) modal(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1157
struct ReportList * reports