Blender V5.0
curves_sculpt_slide.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
6
8#include "BLI_math_rotation.h"
9#include "BLI_task.hh"
10#include "BLI_vector.hh"
11
12#include "DEG_depsgraph.hh"
13
14#include "BKE_attribute_math.hh"
15#include "BKE_brush.hh"
16#include "BKE_bvhutils.hh"
17#include "BKE_context.hh"
18#include "BKE_curves.hh"
19#include "BKE_mesh.hh"
20#include "BKE_mesh_sample.hh"
21#include "BKE_object.hh"
22#include "BKE_paint.hh"
23#include "BKE_report.hh"
24
25#include "DNA_curves_types.h"
26#include "DNA_screen_types.h"
27
28#include "ED_screen.hh"
29#include "ED_view3d.hh"
30
31#include "WM_api.hh"
32
34
37
39
40using geometry::ReverseUVSampler;
41
53
59
61 private:
62 float2 initial_brush_pos_re_;
64 Vector<SlideInfo> slide_info_;
66 Array<float3> initial_positions_cu_;
68 Array<float3> initial_deformed_positions_cu_;
69
71
72 public:
73 void on_stroke_extended(const bContext &C, const StrokeExtension &stroke_extension) override;
74};
75
83
85 const Brush *brush_ = nullptr;
89
93
95 const Mesh *surface_orig_ = nullptr;
99
101 Mesh *surface_eval_ = nullptr;
107
111
113
115
116 std::atomic<bool> found_invalid_uv_mapping_{false};
117
119
120 void execute(SlideOperation &self, const bContext &C, const StrokeExtension &stroke_extension)
121 {
122 UNUSED_VARS(C, stroke_extension);
123 self_ = &self;
124
126 curves_id_orig_ = static_cast<Curves *>(curves_ob_orig_->data);
127 curves_orig_ = &curves_id_orig_->geometry.wrap();
128 if (curves_id_orig_->surface == nullptr || curves_id_orig_->surface->type != OB_MESH) {
129 report_missing_surface(stroke_extension.reports);
130 return;
131 }
132 if (curves_orig_->is_empty()) {
133 return;
134 }
135 if (curves_id_orig_->surface_uv_map == nullptr) {
137 return;
138 }
139 if (!curves_orig_->surface_uv_coords()) {
140 BKE_report(stroke_extension.reports,
142 "Curves do not have surface attachment information");
143 return;
144 }
145 const StringRefNull uv_map_name = curves_id_orig_->surface_uv_map;
146
147 curves_sculpt_ = ctx_.scene->toolsettings->curves_sculpt;
150 brush_radius_factor_ = brush_radius_factor(*brush_, stroke_extension);
152
153 curve_factors_ = *curves_orig_->attributes().lookup_or_default(
154 ".selection", bke::AttrDomain::Curve, 1.0f);
156
157 brush_pos_re_ = stroke_extension.mouse_position;
158
160
162 surface_orig_ = static_cast<const Mesh *>(surface_ob_orig_->data);
163 if (surface_orig_->faces_num == 0) {
164 report_empty_original_surface(stroke_extension.reports);
165 return;
166 }
168 corner_normals_orig_su_ = surface_orig_->corner_normals();
169 surface_uv_map_orig_ = *surface_orig_->attributes().lookup<float2>(uv_map_name,
171 if (surface_uv_map_orig_.is_empty()) {
173 return;
174 }
176 if (surface_ob_eval_ == nullptr) {
177 return;
178 }
180 if (surface_eval_ == nullptr) {
181 return;
182 }
183 if (surface_eval_->faces_num == 0) {
184 report_empty_evaluated_surface(stroke_extension.reports);
185 return;
186 }
188 surface_positions_eval_ = surface_eval_->vert_positions();
190 surface_uv_map_eval_ = *surface_eval_->attributes().lookup<float2>(uv_map_name,
192 if (surface_uv_map_eval_.is_empty()) {
194 return;
195 }
196 surface_bvh_eval_ = surface_eval_->bvh_corner_tris();
197
198 if (stroke_extension.is_first) {
199 self_->initial_brush_pos_re_ = brush_pos_re_;
200 /* Remember original and deformed positions of all points. Otherwise this information is lost
201 * when sliding starts, but it's still used. */
202 const bke::crazyspace::GeometryDeformation deformation =
204 self_->initial_positions_cu_ = curves_orig_->positions();
205 self_->initial_deformed_positions_cu_ = deformation.positions;
206
207 /* First find all curves to slide. When the mouse moves, only those curves will be moved. */
209 return;
210 }
211 this->slide_with_symmetry();
212
213 if (found_invalid_uv_mapping_) {
214 BKE_report(stroke_extension.reports, RPT_WARNING, "UV map or surface attachment is invalid");
215 }
216
217 curves_orig_->tag_positions_changed();
221 }
222
224 {
225 const Vector<float4x4> brush_transforms = get_symmetry_brush_transforms(
227 const float brush_radius_re = brush_radius_base_re_ * brush_radius_factor_;
228 const std::optional<CurvesBrush3D> brush_3d = sample_curves_surface_3d_brush(*ctx_.depsgraph,
229 *ctx_.region,
230 *ctx_.v3d,
234 brush_radius_re);
235 if (!brush_3d.has_value()) {
236 return;
237 }
240 math::transform_point(transforms_.curves_to_world, brush_3d->position_cu));
241
242 const ReverseUVSampler reverse_uv_sampler_orig{surface_uv_map_orig_,
244 for (const float4x4 &brush_transform : brush_transforms) {
245 self_->slide_info_.append_as();
246 SlideInfo &slide_info = self_->slide_info_.last();
247 slide_info.brush_transform = brush_transform;
248 this->find_curves_to_slide(math::transform_point(brush_transform, brush_3d->position_cu),
249 brush_3d->radius_cu,
250 reverse_uv_sampler_orig,
251 slide_info.curves_to_slide);
252 }
253 }
254
255 void find_curves_to_slide(const float3 &brush_pos_cu,
256 const float brush_radius_cu,
257 const ReverseUVSampler &reverse_uv_sampler_orig,
258 Vector<SlideCurveInfo> &r_curves_to_slide)
259 {
260 const Span<float2> surface_uv_coords = *curves_orig_->surface_uv_coords();
261 const float brush_radius_sq_cu = pow2f(brush_radius_cu);
262
263 const Span<int> offsets = curves_orig_->offsets();
264 curve_selection_.foreach_segment([&](const IndexMaskSegment segment) {
265 for (const int curve_i : segment) {
266 const int first_point_i = offsets[curve_i];
267 const float3 old_pos_cu = self_->initial_deformed_positions_cu_[first_point_i];
268 const float dist_to_brush_sq_cu = math::distance_squared(old_pos_cu, brush_pos_cu);
269 if (dist_to_brush_sq_cu > brush_radius_sq_cu) {
270 /* Root point is too far away from curve center. */
271 continue;
272 }
273 const float dist_to_brush_cu = std::sqrt(dist_to_brush_sq_cu);
274 const float radius_falloff = BKE_brush_curve_strength(
275 brush_, dist_to_brush_cu, brush_radius_cu);
276
277 const float2 uv = surface_uv_coords[curve_i];
278 ReverseUVSampler::Result result = reverse_uv_sampler_orig.sample(uv);
279 if (result.type != ReverseUVSampler::ResultType::Ok) {
280 /* The curve does not have a valid surface attachment. */
281 found_invalid_uv_mapping_.store(true);
282 continue;
283 }
284 /* Compute the normal at the initial surface position. */
287 result.bary_weights,
289 const float3 normal_cu = math::normalize(
290 math::transform_point(transforms_.surface_to_curves_normal, point_no));
291
292 r_curves_to_slide.append({curve_i, radius_falloff, normal_cu});
293 }
294 });
295 }
296
298 {
299 const ReverseUVSampler reverse_uv_sampler_orig{surface_uv_map_orig_,
301 for (const SlideInfo &slide_info : self_->slide_info_) {
302 this->slide(slide_info.curves_to_slide, reverse_uv_sampler_orig, slide_info.brush_transform);
303 }
304 }
305
306 void slide(const Span<SlideCurveInfo> slide_curves,
307 const ReverseUVSampler &reverse_uv_sampler_orig,
308 const float4x4 &brush_transform)
309 {
310 const float4x4 brush_transform_inv = math::invert(brush_transform);
311
312 const Span<float3> positions_orig_su = surface_orig_->vert_positions();
313 const Span<int> corner_verts_orig = surface_orig_->corner_verts();
314 const OffsetIndices points_by_curve = curves_orig_->points_by_curve();
315
316 MutableSpan<float3> positions_orig_cu = curves_orig_->positions_for_write();
317 MutableSpan<float2> surface_uv_coords = curves_orig_->surface_uv_coords_for_write();
318
320
321 const float2 brush_pos_diff_re = brush_pos_re_ - self_->initial_brush_pos_re_;
322
323 /* The brush transformation has to be applied in curves space. */
324 const float4x4 world_to_surface_with_symmetry_mat = transforms_.curves_to_surface *
325 brush_transform *
326 transforms_.world_to_curves;
327
328 threading::parallel_for(slide_curves.index_range(), 256, [&](const IndexRange range) {
329 for (const SlideCurveInfo &slide_curve_info : slide_curves.slice(range)) {
330 const int curve_i = slide_curve_info.curve_i;
331 const IndexRange points = points_by_curve[curve_i];
332 const int first_point_i = points[0];
333
334 const float3 old_first_pos_eval_cu = self_->initial_deformed_positions_cu_[first_point_i];
335 const float3 old_first_symm_pos_eval_cu = math::transform_point(brush_transform_inv,
336 old_first_pos_eval_cu);
337 const float3 old_first_pos_eval_su = math::transform_point(transforms_.curves_to_surface,
338 old_first_pos_eval_cu);
339
340 const float2 old_first_symm_pos_eval_re = ED_view3d_project_float_v2_m4(
341 ctx_.region, old_first_symm_pos_eval_cu, projection);
342
343 const float radius_falloff = slide_curve_info.radius_falloff;
344 const float curve_weight = brush_strength_ * radius_falloff * curve_factors_[curve_i];
345 const float2 new_first_symm_pos_eval_re = old_first_symm_pos_eval_re +
346 curve_weight * brush_pos_diff_re;
347
348 /* Compute the ray that will be used to find the new position on the surface. */
349 float3 ray_start_wo, ray_end_wo;
350 ED_view3d_win_to_segment_clipped(ctx_.depsgraph,
351 ctx_.region,
352 ctx_.v3d,
353 new_first_symm_pos_eval_re,
354 ray_start_wo,
355 ray_end_wo,
356 true);
357 const float3 ray_start_su = math::transform_point(world_to_surface_with_symmetry_mat,
358 ray_start_wo);
359 const float3 ray_end_su = math::transform_point(world_to_surface_with_symmetry_mat,
360 ray_end_wo);
361 const float3 ray_direction_su = math::normalize(ray_end_su - ray_start_su);
362
363 /* Find the ray hit that is closest to the initial curve root position. */
364 int tri_index_eval;
365 float3 hit_pos_eval_su;
366 if (!this->find_closest_ray_hit(ray_start_su,
367 ray_direction_su,
368 old_first_pos_eval_su,
369 tri_index_eval,
370 hit_pos_eval_su))
371 {
372 continue;
373 }
374
375 /* Compute the uv of the new surface position on the evaluated mesh. */
376 const int3 &tri_eval = surface_corner_tris_eval_[tri_index_eval];
377 const float3 bary_weights_eval = bke::mesh_surface_sample::compute_bary_coord_in_triangle(
378 surface_positions_eval_, surface_corner_verts_eval_, tri_eval, hit_pos_eval_su);
379 const float2 uv = bke::attribute_math::mix3(bary_weights_eval,
380 surface_uv_map_eval_[tri_eval[0]],
381 surface_uv_map_eval_[tri_eval[1]],
382 surface_uv_map_eval_[tri_eval[2]]);
383
384 /* Try to find the same uv on the original surface. */
385 const ReverseUVSampler::Result result = reverse_uv_sampler_orig.sample(uv);
386 if (result.type != ReverseUVSampler::ResultType::Ok) {
387 found_invalid_uv_mapping_.store(true);
388 continue;
389 }
390 const int3 &tri_orig = surface_corner_tris_orig_[result.tri_index];
391 const float3 &bary_weights_orig = result.bary_weights;
392
393 /* Gather old and new surface normal. */
394 const float3 &initial_normal_cu = slide_curve_info.initial_normal_cu;
395 const float3 new_normal_cu = math::normalize(
396 math::transform_point(transforms_.surface_to_curves_normal,
397 geometry::compute_surface_point_normal(
398 tri_orig, result.bary_weights, corner_normals_orig_su_)));
399
400 /* Gather old and new surface position. */
401 const float3 new_first_pos_orig_su = bke::attribute_math::mix3<float3>(
402 bary_weights_orig,
403 positions_orig_su[corner_verts_orig[tri_orig[0]]],
404 positions_orig_su[corner_verts_orig[tri_orig[1]]],
405 positions_orig_su[corner_verts_orig[tri_orig[2]]]);
406 const float3 old_first_pos_orig_cu = self_->initial_positions_cu_[first_point_i];
407 const float3 new_first_pos_orig_cu = math::transform_point(transforms_.surface_to_curves,
408 new_first_pos_orig_su);
409
410 /* Actually transform curve points. */
411 const float4x4 slide_transform = this->get_slide_transform(
412 old_first_pos_orig_cu, new_first_pos_orig_cu, initial_normal_cu, new_normal_cu);
413 for (const int point_i : points) {
414 positions_orig_cu[point_i] = math::transform_point(
415 slide_transform, self_->initial_positions_cu_[point_i]);
416 }
417 surface_uv_coords[curve_i] = uv;
418 }
419 });
420 }
421
422 bool find_closest_ray_hit(const float3 &ray_start_su,
423 const float3 &ray_direction_su,
424 const float3 &point_su,
425 int &r_tri_index,
426 float3 &r_hit_pos)
427 {
428 float best_dist_sq_su = FLT_MAX;
429 int best_tri_index_eval;
430 float3 best_hit_pos_su;
432 *surface_bvh_eval_.tree,
433 ray_start_su,
434 ray_direction_su,
435 0.0f,
436 FLT_MAX,
437 [&](const int tri_index, const BVHTreeRay &ray, BVHTreeRayHit &hit) {
438 surface_bvh_eval_.raycast_callback(&surface_bvh_eval_, tri_index, &ray, &hit);
439 if (hit.index < 0) {
440 return;
441 }
442 const float3 hit_pos_su = hit.co;
443 const float dist_sq_su = math::distance_squared(hit_pos_su, point_su);
444 if (dist_sq_su < best_dist_sq_su) {
445 best_dist_sq_su = dist_sq_su;
446 best_hit_pos_su = hit_pos_su;
447 best_tri_index_eval = hit.index;
448 }
449 });
450
451 if (best_dist_sq_su == FLT_MAX) {
452 return false;
453 }
454 r_tri_index = best_tri_index_eval;
455 r_hit_pos = best_hit_pos_su;
456 return true;
457 }
458
460 const float3 &new_root_pos,
461 const float3 &old_normal,
462 const float3 &new_normal)
463 {
464 float3x3 rotation_3x3;
465 rotation_between_vecs_to_mat3(rotation_3x3.ptr(), old_normal, new_normal);
466
468 transform.location() -= old_root_pos;
469 transform = float4x4(rotation_3x3) * transform;
470 transform.location() += new_root_pos;
471 return transform;
472 }
473};
474
476{
477 SlideOperationExecutor executor{C};
478 executor.execute(*this, C, stroke_extension);
479}
480
481std::unique_ptr<CurvesSculptStrokeOperation> new_slide_operation()
482{
483 return std::make_unique<SlideOperation>();
484}
485
486} // namespace blender::ed::sculpt_paint
float BKE_brush_alpha_get(const Paint *paint, const Brush *brush)
Definition brush.cc:1357
float BKE_brush_curve_strength(eBrushCurvePreset preset, const CurveMapping *cumap, float distance, float brush_radius)
Definition brush.cc:1577
float BKE_brush_radius_get(const Paint *paint, const Brush *brush)
Definition brush.cc:1272
Object * CTX_data_active_object(const bContext *C)
Low-level operations for curves.
General operations, lookup, etc. for blender objects.
Mesh * BKE_object_get_evaluated_mesh(const Object *object_eval)
const Brush * BKE_paint_brush_for_read(const Paint *paint)
Definition paint.cc:650
@ RPT_WARNING
Definition BKE_report.hh:38
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:153
MINLINE float pow2f(float x)
void rotation_between_vecs_to_mat3(float m[3][3], const float v1[3], const float v2[3])
#define UNUSED_VARS(...)
void DEG_id_tag_update(ID *id, unsigned int flags)
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
eCurvesSymmetryType
@ OB_MESH
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:618
blender::float4x4 ED_view3d_ob_project_mat_get(const RegionView3D *rv3d, const Object *ob)
#define C
Definition RandGen.cpp:29
#define NC_GEOM
Definition WM_types.hh:393
#define ND_DATA
Definition WM_types.hh:509
PyObject * self
constexpr IndexRange index_range() const
Definition BLI_span.hh:401
void append(const T &value)
void on_stroke_extended(const bContext &C, const StrokeExtension &stroke_extension) override
Result sample(const float2 &query_uv) const
MatBase< 4, 4 > float4x4
GeometryDeformation get_evaluated_curves_deformation(const Object *ob_eval, const Object &ob_orig)
IndexMask retrieve_selected_curves(const bke::CurvesGeometry &curves, IndexMaskMemory &memory)
void report_empty_evaluated_surface(ReportList *reports)
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)
std::optional< CurvesBrush3D > sample_curves_surface_3d_brush(const Depsgraph &depsgraph, const ARegion &region, const View3D &v3d, const CurvesSurfaceTransforms &transforms, const bke::BVHTreeFromMesh &surface_bvh, const float2 &brush_pos_re, const float brush_radius_re)
void report_empty_original_surface(ReportList *reports)
Vector< float4x4 > get_symmetry_brush_transforms(const eCurvesSymmetryType symmetry)
std::unique_ptr< CurvesSculptStrokeOperation > new_slide_operation()
float brush_radius_factor(const Brush &brush, const StrokeExtension &stroke_extension)
float3 compute_surface_point_normal(const int3 &tri, const float3 &bary_coord, Span< float3 > corner_normals)
CartesianBasis invert(const CartesianBasis &basis)
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)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:93
MatBase< float, 4, 4 > float4x4
VecBase< float, 2 > float2
MatBase< float, 3, 3 > float3x3
void BLI_bvhtree_ray_cast_all_cpp(const BVHTree &tree, const float3 co, const float3 dir, float radius, float hit_dist, BVHTree_RayCastCallback_CPP fn)
VecBase< float, 3 > float3
#define FLT_MAX
Definition stdcycles.h:14
const c_style_mat & ptr() const
void execute(SlideOperation &self, const bContext &C, const StrokeExtension &stroke_extension)
void find_curves_to_slide(const float3 &brush_pos_cu, const float brush_radius_cu, const ReverseUVSampler &reverse_uv_sampler_orig, Vector< SlideCurveInfo > &r_curves_to_slide)
void slide(const Span< SlideCurveInfo > slide_curves, const ReverseUVSampler &reverse_uv_sampler_orig, const float4x4 &brush_transform)
bool find_closest_ray_hit(const float3 &ray_start_su, const float3 &ray_direction_su, const float3 &point_su, int &r_tri_index, float3 &r_hit_pos)
float4x4 get_slide_transform(const float3 &old_root_pos, const float3 &new_root_pos, const float3 &old_normal, const float3 &new_normal)
void WM_main_add_notifier(uint type, void *reference)