Blender V5.3
sculpt_brush.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include <algorithm>
6
7#include "BKE_bvh.hh"
8#include "sculpt_intern.hh"
9
10#include "BLI_math_geom_c.hh"
11
12#include "DNA_mesh_types.h"
13
14#include "BKE_bvhutils.hh"
15#include "BKE_context.hh"
16#include "BKE_curves.hh"
17#include "BKE_object.hh"
18#include "BKE_paint.hh"
19#include "BKE_paint_types.hh"
20#include "BKE_report.hh"
21
22#include "ED_view3d.hh"
23
24#include "UI_interface.hh"
25
27#include "BLI_task.hh"
28
30
32
40
42
51
55static std::optional<float3> find_curves_brush_position(const CurvesGeometry &curves,
56 const float3 &ray_start_cu,
57 const float3 &ray_end_cu,
58 const float brush_radius_re,
59 const ARegion &region,
60 const RegionView3D &rv3d,
61 const Object &object,
62 const Span<float3> positions)
63{
64 /* This value might have to be adjusted based on user feedback. */
65 const float brush_inner_radius_re = std::min<float>(brush_radius_re, float(UI_UNIT_X) / 3.0f);
66 const float brush_inner_radius_sq_re = pow2f(brush_inner_radius_re);
67
68 const float4x4 projection = ED_view3d_ob_project_mat_get(&rv3d, &object);
69 const float2 brush_pos_re = ED_view3d_project_float_v2_m4(&region, ray_start_cu, projection);
70
71 const float max_depth_sq_cu = math::distance_squared(ray_start_cu, ray_end_cu);
72
73 /* Contains the logic that checks if `b` is a better candidate than `a`. */
74 auto is_better_candidate = [&](const BrushPositionCandidate &a,
76 if (b.distance_sq_re <= brush_inner_radius_sq_re) {
77 if (a.distance_sq_re > brush_inner_radius_sq_re) {
78 /* New candidate is in inner radius while old one is not. */
79 return true;
80 }
81 if (b.depth_sq_cu < a.depth_sq_cu) {
82 /* Both candidates are in inner radius, but new one is closer to the camera. */
83 return true;
84 }
85 }
86 else if (b.distance_sq_re < a.distance_sq_re) {
87 /* Both candidates are outside of inner radius, but new on is closer to the brush center. */
88 return true;
89 }
90 return false;
91 };
92
93 auto update_if_better = [&](BrushPositionCandidate &a, const BrushPositionCandidate &b) {
94 if (is_better_candidate(a, b)) {
95 a = b;
96 }
97 };
98
99 const OffsetIndices points_by_curve = curves.points_by_curve();
100
102 curves.curves_range(),
103 128,
105 [&](IndexRange curves_range, const BrushPositionCandidate &init) {
106 BrushPositionCandidate best_candidate = init;
107
108 for (const int curve_i : curves_range) {
109 const IndexRange points = points_by_curve[curve_i];
110
111 if (points.size() == 1) {
112 const float3 &pos_cu = positions[points.first()];
113
114 const float depth_sq_cu = math::distance_squared(ray_start_cu, pos_cu);
115 if (depth_sq_cu > max_depth_sq_cu) {
116 continue;
117 }
118
119 const float2 pos_re = ED_view3d_project_float_v2_m4(&region, pos_cu, projection);
120
121 BrushPositionCandidate candidate;
122 candidate.position_cu = pos_cu;
123 candidate.depth_sq_cu = depth_sq_cu;
124 candidate.distance_sq_re = math::distance_squared(brush_pos_re, pos_re);
125
126 update_if_better(best_candidate, candidate);
127 continue;
128 }
129
130 for (const int segment_i : points.drop_back(1)) {
131 const float3 &p1_cu = positions[segment_i];
132 const float3 &p2_cu = positions[segment_i + 1];
133
134 const float2 p1_re = ED_view3d_project_float_v2_m4(&region, p1_cu, projection);
135 const float2 p2_re = ED_view3d_project_float_v2_m4(&region, p2_cu, projection);
136
137 float2 closest_re;
138 const float lambda = closest_to_line_segment_v2(
139 closest_re, brush_pos_re, p1_re, p2_re);
140
141 const float3 closest_cu = math::interpolate(p1_cu, p2_cu, lambda);
142 const float depth_sq_cu = math::distance_squared(ray_start_cu, closest_cu);
143 if (depth_sq_cu > max_depth_sq_cu) {
144 continue;
145 }
146
147 const float distance_sq_re = math::distance_squared(brush_pos_re, closest_re);
148
149 float3 brush_position_cu;
150 closest_to_line_segment_v3(brush_position_cu, closest_cu, ray_start_cu, ray_end_cu);
151
152 BrushPositionCandidate candidate;
153 candidate.position_cu = brush_position_cu;
154 candidate.depth_sq_cu = depth_sq_cu;
155 candidate.distance_sq_re = distance_sq_re;
156
157 update_if_better(best_candidate, candidate);
158 }
159 }
160 return best_candidate;
161 },
162 [&](const BrushPositionCandidate &a, const BrushPositionCandidate &b) {
163 return is_better_candidate(a, b) ? b : a;
164 });
165
166 if (best_candidate.distance_sq_re == FLT_MAX) {
167 /* Nothing found. */
168 return std::nullopt;
169 }
170
171 return best_candidate.position_cu;
172}
173
174std::optional<CurvesBrush3D> sample_curves_3d_brush(const Depsgraph &depsgraph,
175 const ARegion &region,
176 const View3D &v3d,
177 const RegionView3D &rv3d,
178 const Object &curves_object,
179 const float2 &brush_pos_re,
180 const float brush_radius_re)
181{
182 const eEvaluationMode deg_eval_mode = DEG_get_mode(&depsgraph);
183 const bool xray_enabled = XRAY_ENABLED(&v3d);
184 const Curves &curves_id = *id_cast<Curves *>(curves_object.data);
185 const CurvesGeometry &curves = curves_id.geometry.wrap();
186 Object *surface_object = curves_id.surface;
187 Object *surface_object_eval = DEG_get_evaluated(&depsgraph, surface_object);
188
189 float3 center_ray_start_wo, center_ray_end_wo;
191 &depsgraph, &region, &v3d, brush_pos_re, center_ray_start_wo, center_ray_end_wo, true);
192
193 /* Shorten ray when the surface object is hit. */
194 const bool use_surface_object_clip = surface_object_eval &&
195 (BKE_object_visibility(surface_object_eval, deg_eval_mode) &
197 if (use_surface_object_clip && !xray_enabled) {
198 const float4x4 surface_to_world_mat(surface_object->object_to_world().ptr());
199 const float4x4 world_to_surface_mat = math::invert(surface_to_world_mat);
200
201 Mesh *surface_eval = BKE_object_get_evaluated_mesh(surface_object_eval);
202 const bke::bvh::Tree &surface_bvh = surface_eval->bvh_tris();
203
204 const float3 center_ray_start_su = math::transform_point(world_to_surface_mat,
205 center_ray_start_wo);
206 float3 center_ray_end_su = math::transform_point(world_to_surface_mat, center_ray_end_wo);
207 const float3 center_ray_direction_su = math::normalize(center_ray_end_su -
208 center_ray_start_su);
209
210 const bke::bvh::Ray center_ray(center_ray_start_su, center_ray_direction_su);
211 if (const std::optional<bke::bvh::RayHit> hit = surface_bvh.ray_intersect(center_ray)) {
212 const float3 hit_position_su = hit->position(center_ray);
213 if (math::distance(center_ray_start_su, center_ray_end_su) >
214 math::distance(center_ray_start_su, hit_position_su))
215 {
216 center_ray_end_su = hit_position_su;
217 center_ray_end_wo = math::transform_point(surface_to_world_mat, center_ray_end_su);
218 }
219 }
220 }
221
222 const float4x4 &curves_to_world_mat = curves_object.object_to_world();
223 const float4x4 world_to_curves_mat = math::invert(curves_to_world_mat);
224
225 const float3 center_ray_start_cu = math::transform_point(world_to_curves_mat,
226 center_ray_start_wo);
227 const float3 center_ray_end_cu = math::transform_point(world_to_curves_mat, center_ray_end_wo);
228
229 const bke::crazyspace::GeometryDeformation deformation =
231
232 const std::optional<float3> brush_position_optional_cu = find_curves_brush_position(
233 curves,
234 center_ray_start_cu,
235 center_ray_end_cu,
236 brush_radius_re,
237 region,
238 rv3d,
239 curves_object,
240 deformation.positions);
241 if (!brush_position_optional_cu.has_value()) {
242 /* Nothing found. */
243 return std::nullopt;
244 }
245 const float3 brush_position_cu = *brush_position_optional_cu;
246
247 /* Determine the 3D brush radius. */
248 float3 radius_ray_start_wo, radius_ray_end_wo;
250 &region,
251 &v3d,
252 brush_pos_re + float2(brush_radius_re, 0.0f),
253 radius_ray_start_wo,
254 radius_ray_end_wo,
255 true);
256 const float3 radius_ray_start_cu = math::transform_point(world_to_curves_mat,
257 radius_ray_start_wo);
258 const float3 radius_ray_end_cu = math::transform_point(world_to_curves_mat, radius_ray_end_wo);
259
260 CurvesBrush3D brush_3d;
261 brush_3d.position_cu = brush_position_cu;
262 brush_3d.radius_cu = dist_to_line_v3(brush_position_cu, radius_ray_start_cu, radius_ray_end_cu);
263 return brush_3d;
264}
265
266std::optional<CurvesBrush3D> sample_curves_surface_3d_brush(
267 const Depsgraph &depsgraph,
268 const ARegion &region,
269 const View3D &v3d,
270 const CurvesSurfaceTransforms &transforms,
271 const bke::bvh::Tree &surface_bvh,
272 const float2 &brush_pos_re,
273 const float brush_radius_re)
274{
275 float3 brush_ray_start_wo, brush_ray_end_wo;
277 &depsgraph, &region, &v3d, brush_pos_re, brush_ray_start_wo, brush_ray_end_wo, true);
278 const float3 brush_ray_start_su = math::transform_point(transforms.world_to_surface,
279 brush_ray_start_wo);
280 const float3 brush_ray_end_su = math::transform_point(transforms.world_to_surface,
281 brush_ray_end_wo);
282
283 const float3 brush_ray_direction_su = brush_ray_end_su - brush_ray_start_su;
284
285 const bke::bvh::Ray brush_ray(brush_ray_start_su, brush_ray_direction_su);
286 const std::optional<bke::bvh::RayHit> ray_hit = surface_bvh.ray_intersect(brush_ray);
287 if (!ray_hit) {
288 return std::nullopt;
289 }
290
291 float3 brush_radius_ray_start_wo, brush_radius_ray_end_wo;
293 &region,
294 &v3d,
295 brush_pos_re + float2(brush_radius_re, 0),
296 brush_radius_ray_start_wo,
297 brush_radius_ray_end_wo,
298 true);
299 const float3 brush_radius_ray_start_cu = math::transform_point(transforms.world_to_curves,
300 brush_radius_ray_start_wo);
301 const float3 brush_radius_ray_end_cu = math::transform_point(transforms.world_to_curves,
302 brush_radius_ray_end_wo);
303
304 const float3 brush_pos_su = ray_hit->position(brush_ray);
305 const float3 brush_pos_cu = math::transform_point(transforms.surface_to_curves, brush_pos_su);
306 const float brush_radius_cu = dist_to_line_v3(
307 brush_pos_cu, brush_radius_ray_start_cu, brush_radius_ray_end_cu);
308 return CurvesBrush3D{brush_pos_cu, brush_radius_cu};
309}
310
312{
313 Vector<float4x4> matrices;
314
315 auto symmetry_to_factors = [&](const eCurvesSymmetryType type) -> Span<float> {
316 if (symmetry & type) {
317 static std::array<float, 2> values = {1.0f, -1.0f};
318 return values;
319 }
320 static std::array<float, 1> values = {1.0f};
321 return values;
322 };
323
324 for (const float x : symmetry_to_factors(CURVES_SYMMETRY_X)) {
325 for (const float y : symmetry_to_factors(CURVES_SYMMETRY_Y)) {
326 for (const float z : symmetry_to_factors(CURVES_SYMMETRY_Z)) {
327 float4x4 matrix = float4x4::identity();
328 matrix.ptr()[0][0] = x;
329 matrix.ptr()[1][1] = y;
330 matrix.ptr()[2][2] = z;
331 matrices.append(matrix);
332 }
333 }
334 }
335
336 return matrices;
337}
338
339void remember_stroke_position(CurvesSculpt &curves_sculpt, const float3 &brush_position_wo)
340{
341 bke::paint::stroke_set_location(curves_sculpt.paint, brush_position_wo);
342}
343
345 const float3 &brush_position,
346 const float old_radius)
347{
348 const float3 offset_position = brush_position + float3(old_radius, 0.0f, 0.0f);
349 const float3 new_position = math::transform_point(transform, brush_position);
350 const float3 new_offset_position = math::transform_point(transform, offset_position);
351 return math::distance(new_position, new_offset_position);
352}
353
355 MutableSpan<float3> positions,
356 const float3 &new_last_position)
357{
358 if (positions.size() <= 2) {
359 /* Curve does not need to be resampled if there are zero points between the start and end. */
360 positions.last() = new_last_position;
361 return;
362 }
363
364 /* Find the accumulated length of each point in the original curve,
365 * treating it as a poly curve for performance reasons and simplicity. */
366 buffer.orig_lengths.resize(length_parameterize::segments_num(positions.size(), false));
368 const float orig_total_length = buffer.orig_lengths.last();
369
370 /* Find the factor by which the new curve is shorter or longer than the original. */
371 const float new_last_segment_length = math::distance(positions.last(1), new_last_position);
372 const float new_total_length = buffer.orig_lengths.last(1) + new_last_segment_length;
373 const float length_factor = math::safe_divide(new_total_length, orig_total_length);
374
375 /* Calculate the lengths to sample the original curve with by scaling the original lengths. */
376 buffer.new_lengths.resize(positions.size() - 1);
377 buffer.new_lengths.first() = 0.0f;
378 for (const int i : buffer.new_lengths.index_range().drop_front(1)) {
379 buffer.new_lengths[i] = buffer.orig_lengths[i - 1] * length_factor;
380 }
381
382 buffer.sample_indices.resize(positions.size() - 1);
383 buffer.sample_factors.resize(positions.size() - 1);
385 buffer.orig_lengths, buffer.new_lengths, buffer.sample_indices, buffer.sample_factors);
386
387 buffer.new_positions.resize(positions.size() - 1);
389 positions, buffer.sample_indices, buffer.sample_factors, buffer.new_positions);
390 positions.drop_back(1).copy_from(buffer.new_positions);
391 positions.last() = new_last_position;
392}
393
395{
396 this->depsgraph = stroke.vc.depsgraph;
397 this->scene = stroke.vc.scene;
398 this->region = stroke.vc.region;
399 this->v3d = stroke.vc.v3d;
400 this->rv3d = stroke.vc.rv3d;
401 this->object = stroke.object;
402}
403
405{
406 BKE_report(reports, RPT_WARNING, "Original surface mesh is empty");
407}
408
410{
411 BKE_report(reports, RPT_WARNING, "Evaluated surface mesh is empty");
412}
413
415{
416 BKE_report(reports, RPT_WARNING, "Missing surface mesh");
417}
418
420{
421 BKE_report(reports, RPT_WARNING, "Missing UV map for attaching curves on original surface");
422}
423
425{
426 BKE_report(reports, RPT_WARNING, "Missing UV map for attaching curves on evaluated surface");
427}
428
430{
431 BKE_report(reports, RPT_WARNING, "Invalid UV map: UV islands must not overlap");
432}
433
435{
436 BKE_report(reports, RPT_WARNING, "Cyclic curves are not supported");
437}
438
440 const IndexMask &curve_selection,
441 const bool use_surface_collision,
442 const float surface_collision_distance)
443{
444 use_surface_collision_ = use_surface_collision;
445 surface_collision_distance_ = surface_collision_distance;
446 segment_lengths_.reinitialize(curves.points_num());
448 curves.points_by_curve(), curves.positions(), curve_selection, segment_lengths_);
449 if (use_surface_collision_) {
450 start_positions_ = curves.positions();
451 }
452}
453
455 const IndexMask &curve_selection,
456 const Mesh *surface,
457 const CurvesSurfaceTransforms &transforms)
458{
459 if (use_surface_collision_ && surface != nullptr) {
461 curves.points_by_curve(),
462 curve_selection,
463 segment_lengths_,
464 start_positions_,
465 *surface,
466 transforms,
467 curves.positions_for_write(),
468 surface_collision_distance_);
469 start_positions_ = curves.positions();
470 }
471 else {
473 curves.points_by_curve(), curve_selection, segment_lengths_, curves.positions_for_write());
474 }
475 curves.tag_positions_changed();
476}
477
478} // namespace blender::ed::sculpt_paint
Low-level operations for curves.
General operations, lookup, etc. for blender objects.
#define XRAY_ENABLED(v3d)
#define UI_UNIT_X
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
constexpr int64_t first() const
constexpr IndexRange drop_back(int64_t n) const
constexpr int64_t size() const
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr MutableSpan drop_back(const int64_t n) const
Definition BLI_span.hh:624
constexpr void copy_from(Span< T > values) const
Definition BLI_span.hh:745
constexpr T & last(const int64_t n=0) const
Definition BLI_span.hh:695
void append(const T &value)
void resize(const int64_t new_size)
std::optional< RayHit > ray_intersect(const Ray &ray) const
Definition bvh.cc:439
#define FLT_MAX
GeometryDeformation get_evaluated_curves_deformation(const Object *ob_eval, const Object &ob_orig)
void stroke_set_location(Paint &paint, float3 location)
void report_invalid_uv_map(ReportList *reports)
void report_cyclic_not_supported(ReportList *reports)
void report_empty_evaluated_surface(ReportList *reports)
std::optional< CurvesBrush3D > sample_curves_3d_brush(const Depsgraph &depsgraph, const ARegion &region, const View3D &v3d, const RegionView3D &rv3d, const Object &curves_object, const float2 &brush_pos_re, const float brush_radius_re)
std::optional< CurvesBrush3D > sample_curves_surface_3d_brush(const Depsgraph &depsgraph, const ARegion &region, const View3D &v3d, const CurvesSurfaceTransforms &transforms, const bke::bvh::Tree &surface_bvh, const float2 &brush_pos_re, const float brush_radius_re)
void report_missing_uv_map_on_original_surface(ReportList *reports)
void remember_stroke_position(CurvesSculpt &curves_sculpt, const float3 &brush_position_wo)
void report_missing_uv_map_on_evaluated_surface(ReportList *reports)
void report_missing_surface(ReportList *reports)
void report_empty_original_surface(ReportList *reports)
static std::optional< float3 > find_curves_brush_position(const CurvesGeometry &curves, const float3 &ray_start_cu, const float3 &ray_end_cu, const float brush_radius_re, const ARegion &region, const RegionView3D &rv3d, const Object &object, const Span< float3 > positions)
Vector< float4x4 > get_symmetry_brush_transforms(const eCurvesSymmetryType symmetry)
void move_last_point_and_resample(MoveAndResampleBuffers &buffer, MutableSpan< float3 > positions, const float3 &new_last_position)
float transform_brush_radius(const float4x4 &transform, const float3 &brush_position, const float old_radius)
void solve_length_and_collision_constraints(OffsetIndices< int > points_by_curve, const IndexMask &curve_selection, Span< float > segment_lengths, Span< float3 > start_positions, const Mesh &surface, const bke::CurvesSurfaceTransforms &transforms, MutableSpan< float3 > positions, const float surface_collision_distance)
void solve_length_constraints(OffsetIndices< int > points_by_curve, const IndexMask &curve_selection, Span< float > segment_lenghts, MutableSpan< float3 > positions)
void compute_segment_lengths(OffsetIndices< int > points_by_curve, Span< float3 > positions, const IndexMask &curve_selection, MutableSpan< float > r_segment_lengths)
int segments_num(const int points_num, const bool cyclic)
void accumulate_lengths(const Span< T > values, const bool cyclic, MutableSpan< float > lengths)
void sample_at_lengths(Span< float > accumulated_segment_lengths, Span< float > sample_lengths, MutableSpan< int > r_segment_indices, MutableSpan< float > r_factors)
void interpolate(const Span< T > src, const Span< int > indices, const Span< float > factors, MutableSpan< T > dst)
T safe_divide(const T &a, const T &b)
T distance(const T &a, const T &b)
CartesianBasis invert(const CartesianBasis &basis)
T interpolate(const T &a, const T &b, const FactorT &t)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
T distance_squared(const VecBase< T, Size > &a, const VecBase< T, Size > &b)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
Value parallel_reduce(IndexRange range, int64_t grain_size, const Value &identity, const Function &function, const Reduction &reduction)
Definition BLI_task.hh:127
@ RPT_WARNING
Definition BKE_report.hh:40
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)
float closest_to_line_segment_v3(float r_close[3], const float p[3], const float l1[3], const float l2[3])
Definition math_geom.cc:403
MatBase< float, 4, 4 > float4x4
MINLINE float pow2f(float x)
float dist_to_line_v3(const float p[3], const float l1[3], const float l2[3])
Definition math_geom.cc:557
VecBase< float, 2 > float2
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
int BKE_object_visibility(const Object *ob, int dag_eval_mode)
Definition object.cc:2063
eEvaluationMode DEG_get_mode(const Depsgraph *graph)
bool ED_view3d_win_to_segment_clipped(const Depsgraph *depsgraph, const ARegion *region, const View3D *v3d, const float mval[2], float r_ray_start[3], float r_ray_end[3], bool do_clip_planes)
VecBase< float, 3 > float3
float closest_to_line_segment_v2(float r_close[2], const float p[2], const float l1[2], const float l2[2])
Definition math_geom.cc:381
@ OB_VISIBLE_SELF
Mesh * BKE_object_get_evaluated_mesh(const Object *object_eval)
Definition object.cc:4436
const c_style_mat & ptr() const
RegionView3D * rv3d
Definition ED_view3d.hh:86
Depsgraph * depsgraph
Definition ED_view3d.hh:78
void solve_step(bke::CurvesGeometry &curves, const IndexMask &curve_selection, const Mesh *surface, const CurvesSurfaceTransforms &transforms)
void initialize(const bke::CurvesGeometry &curves, const IndexMask &curve_selection, const bool use_surface_collision, const float surface_collision_distance)
void resize(int new_size)
Definition vector.hh:94