Blender V5.3
sculpt_gesture.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
9#include "sculpt_gesture.hh"
10
11#include "MEM_guardedalloc.h"
12
13#include "DNA_vec_types.h"
14
15#include "BLI_bitmap_draw_2d.hh"
16#include "BLI_lasso_2d.hh"
17#include "BLI_math_geom_c.hh"
18#include "BLI_math_matrix.hh"
19#include "BLI_math_matrix_c.hh"
21#include "BLI_math_vector.hh"
22#include "BLI_math_vector_c.hh"
24#include "BLI_rect.hh"
25
26#include "BKE_context.hh"
27#include "BKE_object_types.hh"
28#include "BKE_paint.hh"
29
30#include "ED_view3d.hh"
31
32#include "RNA_access.hh"
33#include "RNA_define.hh"
34
35#include "WM_api.hh"
36#include "WM_types.hh"
37
38#include "../paint_intern.hh"
39#include "sculpt_intern.hh"
40
42
44{
46 "use_front_faces_only",
47 false,
48 "Front Faces Only",
49 "Affect only faces facing towards the view");
50
51 if (shapeType == ShapeType::Line) {
53 "use_limit_to_segment",
54 false,
55 "Limit to Segment",
56 "Apply the gesture action only to the area that is contained within the "
57 "segment without extending its effect to the entire line");
58 }
59}
60
61static void init_common(bContext *C, const wmOperator *op, GestureData &gesture_data)
62{
63 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
64 gesture_data.vc = ED_view3d_viewcontext_init(C, depsgraph);
65 const Object &object = *gesture_data.vc.obact;
66
67 /* Operator properties. */
68 gesture_data.front_faces_only = RNA_boolean_get(op->ptr, "use_front_faces_only");
69 gesture_data.selection_type = SelectionType::Inside;
70
72 gesture_data.brush = BKE_paint_brush_for_read(gesture_data.paint);
73
74 /* SculptSession */
75 gesture_data.ss = object.runtime->sculpt_session;
76
77 /* Symmetry. */
78 gesture_data.symm = ePaintSymmetryFlags(mesh_symmetry_xyz_get(object));
79
80 /* View Normal. */
81 const float3x3 view_inv(float4x4(gesture_data.vc.rv3d->viewinv));
82 const float3 view_dir = math::transform_direction(view_inv, {0.0f, 0.0f, 1.0f});
83
84 gesture_data.world_space_view_normal = math::normalize(view_dir);
85 gesture_data.true_view_normal = math::normalize(
86 math::transform_direction(object.world_to_object(), view_dir));
87
88 /* View Origin. */
89 gesture_data.world_space_view_origin = gesture_data.vc.rv3d->viewinv[3];
90 gesture_data.true_view_origin = gesture_data.vc.rv3d->viewinv[3];
91}
92
93static void lasso_px_cb(const int x, const int x_end, const int y, void *user_data)
94{
95 GestureData *gesture_data = static_cast<GestureData *>(user_data);
96 LassoData *lasso = &gesture_data->lasso;
97 int index = (y * lasso->width) + x;
98 const int index_end = (y * lasso->width) + x_end;
99 do {
100 lasso->mask_px[index].set();
101 } while (++index != index_end);
102}
103
104std::unique_ptr<GestureData> init_from_polyline(bContext *C, wmOperator *op)
105{
106 return init_from_lasso(C, op);
107}
108
109std::unique_ptr<GestureData> init_from_lasso(bContext *C, wmOperator *op)
110{
111 const Array<int2> mcoords = WM_gesture_lasso_path_to_array(C, op);
112 if (mcoords.size() <= 1) {
113 return nullptr;
114 }
115
116 std::unique_ptr<GestureData> gesture_data = std::make_unique<GestureData>();
117 gesture_data->shape_type = ShapeType::Lasso;
118
119 init_common(C, op, *gesture_data);
120
121 gesture_data->lasso.projviewobjmat = ED_view3d_ob_project_mat_get(gesture_data->vc.rv3d,
122 gesture_data->vc.obact);
123 BLI_lasso_boundbox(&gesture_data->lasso.boundbox, mcoords);
124 const int lasso_width = 1 + gesture_data->lasso.boundbox.xmax -
125 gesture_data->lasso.boundbox.xmin;
126 const int lasso_height = 1 + gesture_data->lasso.boundbox.ymax -
127 gesture_data->lasso.boundbox.ymin;
128 gesture_data->lasso.width = lasso_width;
129 gesture_data->lasso.mask_px.resize(lasso_width * lasso_height);
130
131 BLI_bitmap_draw_2d_poly_v2i_n(gesture_data->lasso.boundbox.xmin,
132 gesture_data->lasso.boundbox.ymin,
133 gesture_data->lasso.boundbox.xmax,
134 gesture_data->lasso.boundbox.ymax,
135 mcoords,
137 gesture_data.get());
138
139 BoundBox bb;
141 gesture_data->true_clip_planes,
142 gesture_data->vc.region,
143 gesture_data->vc.obact,
144 &gesture_data->lasso.boundbox);
145
146 gesture_data->gesture_points.reinitialize(mcoords.size());
147 for (const int i : mcoords.index_range()) {
148 gesture_data->gesture_points[i][0] = mcoords[i][0];
149 gesture_data->gesture_points[i][1] = mcoords[i][1];
150 }
151
152 return gesture_data;
153}
154
155std::unique_ptr<GestureData> init_from_box(bContext *C, wmOperator *op)
156{
157 std::unique_ptr<GestureData> gesture_data = std::make_unique<GestureData>();
158 gesture_data->shape_type = ShapeType::Box;
159
160 init_common(C, op, *gesture_data);
161
162 rcti rect;
164
165 BoundBox bb;
167 &bb, gesture_data->true_clip_planes, gesture_data->vc.region, gesture_data->vc.obact, &rect);
168
169 gesture_data->gesture_points.reinitialize(4);
170
171 gesture_data->gesture_points[0][0] = rect.xmax;
172 gesture_data->gesture_points[0][1] = rect.ymax;
173
174 gesture_data->gesture_points[1][0] = rect.xmax;
175 gesture_data->gesture_points[1][1] = rect.ymin;
176
177 gesture_data->gesture_points[2][0] = rect.xmin;
178 gesture_data->gesture_points[2][1] = rect.ymin;
179
180 gesture_data->gesture_points[3][0] = rect.xmin;
181 gesture_data->gesture_points[3][1] = rect.ymax;
182 return gesture_data;
183}
184
185static void line_plane_from_tri(float *r_plane,
186 const GestureData &gesture_data,
187 const bool flip,
188 const float3 &p1,
189 const float3 &p2,
190 const float3 &p3)
191{
192 float3 normal;
193 normal_tri_v3(normal, p1, p2, p3);
194 normal = math::normalize(
195 math::transform_direction(gesture_data.vc.obact->world_to_object(), normal));
196 if (flip) {
197 normal *= -1.0f;
198 }
199 const float3 plane_point_object_space = math::transform_point(
200 gesture_data.vc.obact->world_to_object(), p1);
201 plane_from_point_normal_v3(r_plane, plane_point_object_space, normal);
202}
203
204/* Creates 4 points in the plane defined by the line and 2 extra points with an offset relative to
205 * this plane. */
206static void line_calculate_plane_points(const GestureData &gesture_data,
207 const Span<float2> line_points,
208 std::array<float3, 4> &r_plane_points,
209 std::array<float3, 2> &r_offset_plane_points)
210{
211 const float3 depth_point = gesture_data.true_view_origin + gesture_data.true_view_normal;
213 gesture_data.vc.v3d, gesture_data.vc.region, depth_point, line_points[0], r_plane_points[0]);
215 gesture_data.vc.v3d, gesture_data.vc.region, depth_point, line_points[1], r_plane_points[3]);
216
217 const float3 offset_depth_point = gesture_data.true_view_origin +
218 gesture_data.true_view_normal * 10.0f;
219 ED_view3d_win_to_3d(gesture_data.vc.v3d,
220 gesture_data.vc.region,
221 offset_depth_point,
222 line_points[0],
223 r_plane_points[1]);
224 ED_view3d_win_to_3d(gesture_data.vc.v3d,
225 gesture_data.vc.region,
226 offset_depth_point,
227 line_points[1],
228 r_plane_points[2]);
229
230 float3 normal;
231 normal_tri_v3(normal, r_plane_points[0], r_plane_points[1], r_plane_points[2]);
232 r_offset_plane_points[0] = r_plane_points[0] + normal;
233 r_offset_plane_points[1] = r_plane_points[3] + normal;
234}
235
236std::unique_ptr<GestureData> init_from_line(bContext *C, const wmOperator *op)
237{
238 std::unique_ptr<GestureData> gesture_data = std::make_unique<GestureData>();
239 gesture_data->shape_type = ShapeType::Line;
240 gesture_data->line.use_side_planes = RNA_boolean_get(op->ptr, "use_limit_to_segment");
241
242 init_common(C, op, *gesture_data);
243
244 gesture_data->gesture_points.reinitialize(2);
245 gesture_data->gesture_points[0] = {float(RNA_int_get(op->ptr, "xstart")),
246 float(RNA_int_get(op->ptr, "ystart"))};
247 gesture_data->gesture_points[1] = {float(RNA_int_get(op->ptr, "xend")),
248 float(RNA_int_get(op->ptr, "yend"))};
249
250 gesture_data->line.flip = RNA_boolean_get(op->ptr, "flip");
251
252 std::array<float3, 4> plane_points;
253 std::array<float3, 2> offset_plane_points;
255 *gesture_data, gesture_data->gesture_points, plane_points, offset_plane_points);
256
257 /* Calculate line plane and normal. */
258 const bool flip = gesture_data->line.flip ^ (!gesture_data->vc.rv3d->is_persp);
259 line_plane_from_tri(gesture_data->line.true_plane,
260 *gesture_data,
261 flip,
262 plane_points[0],
263 plane_points[1],
264 plane_points[2]);
265
266 /* Calculate the side planes. */
267 line_plane_from_tri(gesture_data->line.true_side_plane[0],
268 *gesture_data,
269 false,
270 plane_points[1],
271 plane_points[0],
272 offset_plane_points[0]);
273 line_plane_from_tri(gesture_data->line.true_side_plane[1],
274 *gesture_data,
275 false,
276 plane_points[3],
277 plane_points[2],
278 offset_plane_points[1]);
279
280 return gesture_data;
281}
282
284{
285 MEM_SAFE_DELETE(this->operation);
286}
287
288static void flip_plane(float out[4], const float in[4], const char symm)
289{
290 if (symm & PAINT_SYMM_X) {
291 out[0] = -in[0];
292 }
293 else {
294 out[0] = in[0];
295 }
296 if (symm & PAINT_SYMM_Y) {
297 out[1] = -in[1];
298 }
299 else {
300 out[1] = in[1];
301 }
302 if (symm & PAINT_SYMM_Z) {
303 out[2] = -in[2];
304 }
305 else {
306 out[2] = in[2];
307 }
308
309 out[3] = in[3];
310}
311
312static void flip_for_symmetry_pass(GestureData &gesture_data, const ePaintSymmetryFlags symmpass)
313{
314 gesture_data.symmpass = symmpass;
315 for (int j = 0; j < 4; j++) {
316 flip_plane(gesture_data.clip_planes[j], gesture_data.true_clip_planes[j], symmpass);
317 }
318
319 negate_m4(gesture_data.clip_planes);
320
321 gesture_data.view_normal = symmetry_flip(gesture_data.true_view_normal, symmpass);
322 gesture_data.view_origin = symmetry_flip(gesture_data.true_view_origin, symmpass);
323 flip_plane(gesture_data.line.plane, gesture_data.line.true_plane, symmpass);
324 flip_plane(gesture_data.line.side_plane[0], gesture_data.line.true_side_plane[0], symmpass);
325 flip_plane(gesture_data.line.side_plane[1], gesture_data.line.true_side_plane[1], symmpass);
326}
327
329{
330 const bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(*gesture_data.vc.obact);
331 std::array<float4, 3> clip_planes;
332 copy_v4_v4(clip_planes[0], gesture_data.line.plane);
333 copy_v4_v4(clip_planes[1], gesture_data.line.side_plane[0]);
334 copy_v4_v4(clip_planes[2], gesture_data.line.side_plane[1]);
335
336 gesture_data.node_mask = bke::pbvh::search_nodes(
337 pbvh, gesture_data.node_mask_memory, [&](const bke::pbvh::Node &node) {
338 return bke::pbvh::node_frustum_contain_aabb(
339 node, Span(clip_planes).take_front(gesture_data.line.use_side_planes ? 3 : 1));
340 });
341}
342
344{
345 const bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(*gesture_data.vc.obact);
346 float clip_planes[4][4];
347 copy_m4_m4(clip_planes, gesture_data.clip_planes);
348 negate_m4(clip_planes);
349
350 Span planes(reinterpret_cast<float4 *>(clip_planes), 4);
351
352 gesture_data.node_mask = bke::pbvh::search_nodes(
353 pbvh, gesture_data.node_mask_memory, [&](const bke::pbvh::Node &node) {
354 switch (gesture_data.selection_type) {
355 case SelectionType::Inside:
356 return bke::pbvh::node_frustum_contain_aabb(node, planes);
357 case SelectionType::Outside:
358 /* Certain degenerate cases of a lasso shape can cause the resulting
359 * frustum planes to enclose a node's AABB, therefore we must submit it
360 * to be more thoroughly evaluated. */
361 if (gesture_data.shape_type == ShapeType::Lasso) {
362 return true;
363 }
364 return bke::pbvh::node_frustum_exclude_aabb(node, planes);
365 }
367 return false;
368 });
369}
370
371static void update_affected_nodes(GestureData &gesture_data)
372{
373 switch (gesture_data.shape_type) {
374 case ShapeType::Box:
375 case ShapeType::Lasso:
377 break;
378 case ShapeType::Line:
380 break;
381 }
382}
383
384static bool is_affected_lasso(const GestureData &gesture_data, const float3 &position)
385{
386 const float3 co_final = symmetry_flip(position, gesture_data.symmpass);
387
388 /* First project point to 2d space. */
389 const float2 scr_co_f = ED_view3d_project_float_v2_m4(
390 gesture_data.vc.region, co_final, gesture_data.lasso.projviewobjmat);
391
392 int2 screen_coords = {int(scr_co_f[0]), int(scr_co_f[1])};
393
394 /* Clip against lasso boundbox. */
395 const LassoData &lasso = gesture_data.lasso;
396 if (!BLI_rcti_isect_pt_v(&lasso.boundbox, screen_coords)) {
397 return gesture_data.selection_type == SelectionType::Outside;
398 }
399
400 screen_coords[0] -= lasso.boundbox.xmin;
401 screen_coords[1] -= lasso.boundbox.ymin;
402
403 const bool bitmap_result =
404 lasso.mask_px[screen_coords[1] * lasso.width + screen_coords[0]].test();
405 switch (gesture_data.selection_type) {
406 case SelectionType::Inside:
407 return bitmap_result;
408 case SelectionType::Outside:
409 return !bitmap_result;
410 }
412 return false;
413}
414
415bool is_affected(const GestureData &gesture_data, const float3 &position, const float3 &normal)
416{
417 const float dot = math::dot(gesture_data.view_normal, normal);
418 const bool is_affected_front_face = !(gesture_data.front_faces_only && dot < 0.0f);
419
420 if (!is_affected_front_face) {
421 return false;
422 }
423
424 switch (gesture_data.shape_type) {
425 case ShapeType::Box: {
426 const bool is_contained = isect_point_planes_v3(gesture_data.clip_planes, 4, position);
427 return ((is_contained && gesture_data.selection_type == SelectionType::Inside) ||
428 (!is_contained && gesture_data.selection_type == SelectionType::Outside));
429 }
430 case ShapeType::Lasso:
431 return is_affected_lasso(gesture_data, position);
432 case ShapeType::Line:
433 if (gesture_data.line.use_side_planes) {
434 return plane_point_side_v3(gesture_data.line.plane, position) > 0.0f &&
435 plane_point_side_v3(gesture_data.line.side_plane[0], position) > 0.0f &&
436 plane_point_side_v3(gesture_data.line.side_plane[1], position) > 0.0f;
437 }
438 return plane_point_side_v3(gesture_data.line.plane, position) > 0.0f;
439 }
440 return false;
441}
442
443void filter_factors(const GestureData &gesture_data,
444 const Span<float3> positions,
445 const Span<float3> normals,
446 const MutableSpan<float> factors)
447{
448 for (const int i : positions.index_range()) {
449 if (!is_affected(gesture_data, positions[i], normals[i])) {
450 factors[i] = 0.0f;
451 }
452 }
453}
454
455void apply(bContext &C, GestureData &gesture_data, wmOperator &op)
456{
457 const Operation *operation = gesture_data.operation;
458
459 operation->begin(C, op, gesture_data);
460
461 for (int symmpass = 0; symmpass <= gesture_data.symm; symmpass++) {
462 if (is_symmetry_iteration_valid(symmpass, gesture_data.symm)) {
463 flip_for_symmetry_pass(gesture_data, ePaintSymmetryFlags(symmpass));
464 update_affected_nodes(gesture_data);
465
466 operation->apply_for_symmetry_pass(C, gesture_data);
467 }
468 }
469
470 operation->end(C, gesture_data);
471
473}
474} // namespace blender::ed::sculpt_paint::gesture
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define BLI_assert_unreachable()
int64_t size() const
Definition BLI_array.hh:267
IndexRange index_range() const
Definition BLI_array.hh:371
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
#define position
float dot(VecOp< float, D >, VecOp< float, D >) RET
pbvh::Tree * pbvh_get(Object &object)
IndexMask search_nodes(const Tree &pbvh, IndexMaskMemory &memory, FunctionRef< bool(const Node &)> filter_fn)
Definition pbvh.cc:2556
static void update_affected_nodes_by_line_plane(GestureData &gesture_data)
static void init_common(bContext *C, const wmOperator *op, GestureData &gesture_data)
static void lasso_px_cb(const int x, const int x_end, const int y, void *user_data)
std::unique_ptr< GestureData > init_from_box(bContext *C, wmOperator *op)
void operator_properties(wmOperatorType *ot, ShapeType shapeType)
static void flip_plane(float out[4], const float in[4], const char symm)
std::unique_ptr< GestureData > init_from_polyline(bContext *C, wmOperator *op)
static void update_affected_nodes(GestureData &gesture_data)
std::unique_ptr< GestureData > init_from_line(bContext *C, const wmOperator *op)
static void line_plane_from_tri(float *r_plane, const GestureData &gesture_data, const bool flip, const float3 &p1, const float3 &p2, const float3 &p3)
static void flip_for_symmetry_pass(GestureData &gesture_data, const ePaintSymmetryFlags symmpass)
void filter_factors(const GestureData &gesture_data, const Span< float3 > positions, const Span< float3 > normals, const MutableSpan< float > factors)
void apply(bContext &C, GestureData &gesture_data, wmOperator &op)
static bool is_affected_lasso(const GestureData &gesture_data, const float3 &position)
std::unique_ptr< GestureData > init_from_lasso(bContext *C, wmOperator *op)
static void update_affected_nodes_by_clip_planes(GestureData &gesture_data)
bool is_affected(const GestureData &gesture_data, const float3 &position, const float3 &normal)
static void line_calculate_plane_points(const GestureData &gesture_data, const Span< float2 > line_points, std::array< float3, 4 > &r_plane_points, std::array< float3, 2 > &r_offset_plane_points)
float3 symmetry_flip(const float3 &src, const ePaintSymmetryFlags symm)
ePaintSymmetryFlags mesh_symmetry_xyz_get(const Object &object)
Definition sculpt.cc:192
void tag_update_overlays(bContext *C)
Definition sculpt.cc:895
bool is_symmetry_iteration_valid(const char i, const char symm)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
VecBase< T, 3 > transform_direction(const MatBase< T, 3, 3 > &mat, const VecBase< T, 3 > &direction)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
void negate_m4(float R[4][4])
ViewContext ED_view3d_viewcontext_init(bContext *C, Depsgraph *depsgraph)
float4x4 ED_view3d_ob_project_mat_get(const RegionView3D *rv3d, const Object *ob)
float2 ED_view3d_project_float_v2_m4(const ARegion *region, const float co[3], const float4x4 &mat)
int RNA_int_get(PointerRNA *ptr, const char *name)
float normal_tri_v3(float n[3], const float v1[3], const float v2[3], const float v3[3])
Definition math_geom.cc:45
MatBase< float, 4, 4 > float4x4
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
VecBase< float, 4 > float4
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
void BLI_bitmap_draw_2d_poly_v2i_n(int xmin, int ymin, int xmax, int ymax, Span< int2 > verts, void(*callback)(int x, int x_end, int y, void *), void *user_data)
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
void WM_operator_properties_border_to_rcti(wmOperator *op, rcti *r_rect)
MINLINE float plane_point_side_v3(const float plane[4], const float co[3])
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
MatBase< float, 3, 3 > float3x3
const Brush * BKE_paint_brush_for_read(const Paint *paint)
void ED_view3d_clipping_calc(BoundBox *bb, float planes[4][4], const ARegion *region, const Object *ob, const rcti *rect)
Paint * BKE_paint_get_active_from_context(const bContext *C)
Array< int2 > WM_gesture_lasso_path_to_array(bContext *, wmOperator *op)
bool isect_point_planes_v3(const float(*planes)[4], int totplane, const float p[3])
bool BLI_rcti_isect_pt_v(const struct rcti *rect, const int xy[2])
MINLINE void copy_v4_v4(float r[4], const float a[4])
void copy_m4_m4(float m1[4][4], const float m2[4][4])
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 ED_view3d_win_to_3d(const View3D *v3d, const ARegion *region, const float depth_pt[3], const float mval[2], float r_out[3])
void BLI_lasso_boundbox(rcti *rect, Span< int2 > mcoords)
Definition lasso_2d.cc:21
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
RegionView3D * rv3d
Definition ED_view3d.hh:86
void(*) end(bContext &, GestureData &)
void(*) apply_for_symmetry_pass(bContext &, GestureData &)
void(*) begin(bContext &, wmOperator &, GestureData &)