Blender V4.3
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
5#include <algorithm>
6
8
10#include "BLI_math_rotation.h"
11#include "BLI_task.hh"
12#include "BLI_vector.hh"
13
14#include "DEG_depsgraph.hh"
15
16#include "BKE_attribute_math.hh"
17#include "BKE_brush.hh"
18#include "BKE_bvhutils.hh"
19#include "BKE_context.hh"
20#include "BKE_curves.hh"
21#include "BKE_mesh.hh"
22#include "BKE_mesh_sample.hh"
23#include "BKE_object.hh"
24#include "BKE_paint.hh"
25#include "BKE_report.hh"
26
27#include "DNA_brush_enums.h"
28#include "DNA_curves_types.h"
29#include "DNA_screen_types.h"
30#include "DNA_space_types.h"
31
32#include "ED_screen.hh"
33#include "ED_view3d.hh"
34
35#include "WM_api.hh"
36
38
41
43
44using geometry::ReverseUVSampler;
45
57
63
65 private:
66 float2 initial_brush_pos_re_;
68 Vector<SlideInfo> slide_info_;
70 Array<float3> initial_positions_cu_;
72 Array<float3> initial_deformed_positions_cu_;
73
75
76 public:
77 void on_stroke_extended(const bContext &C, const StrokeExtension &stroke_extension) override;
78};
79
87
88 const CurvesSculpt *curves_sculpt_ = nullptr;
89 const Brush *brush_ = nullptr;
93
97
99 const Mesh *surface_orig_ = nullptr;
103
105 Mesh *surface_eval_ = nullptr;
111
115
117
119
120 std::atomic<bool> found_invalid_uv_mapping_{false};
121
123
124 void execute(SlideOperation &self, const bContext &C, const StrokeExtension &stroke_extension)
125 {
126 UNUSED_VARS(C, stroke_extension);
127 self_ = &self;
128
130 curves_id_orig_ = static_cast<Curves *>(curves_ob_orig_->data);
132 if (curves_id_orig_->surface == nullptr || curves_id_orig_->surface->type != OB_MESH) {
133 report_missing_surface(stroke_extension.reports);
134 return;
135 }
136 if (curves_orig_->curves_num() == 0) {
137 return;
138 }
139 if (curves_id_orig_->surface_uv_map == nullptr) {
141 return;
142 }
143 if (curves_orig_->surface_uv_coords().is_empty()) {
144 BKE_report(stroke_extension.reports,
146 "Curves do not have surface attachment information");
147 return;
148 }
149 const StringRefNull uv_map_name = curves_id_orig_->surface_uv_map;
150
154 brush_radius_factor_ = brush_radius_factor(*brush_, stroke_extension);
155 brush_strength_ = brush_strength_get(*ctx_.scene, *brush_, stroke_extension);
156
158 ".selection", bke::AttrDomain::Curve, 1.0f);
160
161 brush_pos_re_ = stroke_extension.mouse_position;
162
164
166 surface_orig_ = static_cast<const Mesh *>(surface_ob_orig_->data);
167 if (surface_orig_->faces_num == 0) {
168 report_empty_original_surface(stroke_extension.reports);
169 return;
170 }
172 corner_normals_orig_su_ = surface_orig_->corner_normals();
173 surface_uv_map_orig_ = *surface_orig_->attributes().lookup<float2>(uv_map_name,
175 if (surface_uv_map_orig_.is_empty()) {
177 return;
178 }
180 if (surface_ob_eval_ == nullptr) {
181 return;
182 }
184 if (surface_eval_ == nullptr) {
185 return;
186 }
187 if (surface_eval_->faces_num == 0) {
188 report_empty_evaluated_surface(stroke_extension.reports);
189 return;
190 }
192 surface_positions_eval_ = surface_eval_->vert_positions();
194 surface_uv_map_eval_ = *surface_eval_->attributes().lookup<float2>(uv_map_name,
196 if (surface_uv_map_eval_.is_empty()) {
198 return;
199 }
202
203 if (stroke_extension.is_first) {
204 self_->initial_brush_pos_re_ = brush_pos_re_;
205 /* Remember original and deformed positions of all points. Otherwise this information is lost
206 * when sliding starts, but it's still used. */
207 const bke::crazyspace::GeometryDeformation deformation =
209 self_->initial_positions_cu_ = curves_orig_->positions();
210 self_->initial_deformed_positions_cu_ = deformation.positions;
211
212 /* First find all curves to slide. When the mouse moves, only those curves will be moved. */
214 return;
215 }
216 this->slide_with_symmetry();
217
219 BKE_report(stroke_extension.reports, RPT_WARNING, "UV map or surface attachment is invalid");
220 }
221
226 }
227
229 {
230 const Vector<float4x4> brush_transforms = get_symmetry_brush_transforms(
232 const float brush_radius_re = brush_radius_base_re_ * brush_radius_factor_;
233 const std::optional<CurvesBrush3D> brush_3d = sample_curves_surface_3d_brush(*ctx_.depsgraph,
234 *ctx_.region,
235 *ctx_.v3d,
239 brush_radius_re);
240 if (!brush_3d.has_value()) {
241 return;
242 }
243 const ReverseUVSampler reverse_uv_sampler_orig{surface_uv_map_orig_,
245 for (const float4x4 &brush_transform : brush_transforms) {
246 self_->slide_info_.append_as();
247 SlideInfo &slide_info = self_->slide_info_.last();
248 slide_info.brush_transform = brush_transform;
249 this->find_curves_to_slide(math::transform_point(brush_transform, brush_3d->position_cu),
250 brush_3d->radius_cu,
251 reverse_uv_sampler_orig,
252 slide_info.curves_to_slide);
253 }
254 }
255
256 void find_curves_to_slide(const float3 &brush_pos_cu,
257 const float brush_radius_cu,
258 const ReverseUVSampler &reverse_uv_sampler_orig,
259 Vector<SlideCurveInfo> &r_curves_to_slide)
260 {
261 const Span<float2> surface_uv_coords = curves_orig_->surface_uv_coords();
262 const float brush_radius_sq_cu = pow2f(brush_radius_cu);
263
264 const Span<int> offsets = curves_orig_->offsets();
266 for (const int curve_i : segment) {
267 const int first_point_i = offsets[curve_i];
268 const float3 old_pos_cu = self_->initial_deformed_positions_cu_[first_point_i];
269 const float dist_to_brush_sq_cu = math::distance_squared(old_pos_cu, brush_pos_cu);
270 if (dist_to_brush_sq_cu > brush_radius_sq_cu) {
271 /* Root point is too far away from curve center. */
272 continue;
273 }
274 const float dist_to_brush_cu = std::sqrt(dist_to_brush_sq_cu);
275 const float radius_falloff = BKE_brush_curve_strength(
276 brush_, dist_to_brush_cu, brush_radius_cu);
277
278 const float2 uv = surface_uv_coords[curve_i];
279 ReverseUVSampler::Result result = reverse_uv_sampler_orig.sample(uv);
280 if (result.type != ReverseUVSampler::ResultType::Ok) {
281 /* The curve does not have a valid surface attachment. */
282 found_invalid_uv_mapping_.store(true);
283 continue;
284 }
285 /* Compute the normal at the initial surface position. */
287 surface_corner_tris_orig_[result.tri_index],
288 result.bary_weights,
290 const float3 normal_cu = math::normalize(
292
293 r_curves_to_slide.append({curve_i, radius_falloff, normal_cu});
294 }
295 });
296 }
297
299 {
300 const ReverseUVSampler reverse_uv_sampler_orig{surface_uv_map_orig_,
302 for (const SlideInfo &slide_info : self_->slide_info_) {
303 this->slide(slide_info.curves_to_slide, reverse_uv_sampler_orig, slide_info.brush_transform);
304 }
305 }
306
307 void slide(const Span<SlideCurveInfo> slide_curves,
308 const ReverseUVSampler &reverse_uv_sampler_orig,
309 const float4x4 &brush_transform)
310 {
311 const float4x4 brush_transform_inv = math::invert(brush_transform);
312
313 const Span<float3> positions_orig_su = surface_orig_->vert_positions();
314 const Span<int> corner_verts_orig = surface_orig_->corner_verts();
315 const OffsetIndices points_by_curve = curves_orig_->points_by_curve();
316
319
321
322 const float2 brush_pos_diff_re = brush_pos_re_ - self_->initial_brush_pos_re_;
323
324 /* The brush transformation has to be applied in curves space. */
325 const float4x4 world_to_surface_with_symmetry_mat = transforms_.curves_to_surface *
326 brush_transform *
328
329 threading::parallel_for(slide_curves.index_range(), 256, [&](const IndexRange range) {
330 for (const SlideCurveInfo &slide_curve_info : slide_curves.slice(range)) {
331 const int curve_i = slide_curve_info.curve_i;
332 const IndexRange points = points_by_curve[curve_i];
333 const int first_point_i = points[0];
334
335 const float3 old_first_pos_eval_cu = self_->initial_deformed_positions_cu_[first_point_i];
336 const float3 old_first_symm_pos_eval_cu = math::transform_point(brush_transform_inv,
337 old_first_pos_eval_cu);
338 const float3 old_first_pos_eval_su = math::transform_point(transforms_.curves_to_surface,
339 old_first_pos_eval_cu);
340
341 const float2 old_first_symm_pos_eval_re = ED_view3d_project_float_v2_m4(
342 ctx_.region, old_first_symm_pos_eval_cu, projection);
343
344 const float radius_falloff = slide_curve_info.radius_falloff;
345 const float curve_weight = brush_strength_ * radius_falloff * curve_factors_[curve_i];
346 const float2 new_first_symm_pos_eval_re = old_first_symm_pos_eval_re +
347 curve_weight * brush_pos_diff_re;
348
349 /* Compute the ray that will be used to find the new position on the surface. */
350 float3 ray_start_wo, ray_end_wo;
351 ED_view3d_win_to_segment_clipped(ctx_.depsgraph,
352 ctx_.region,
353 ctx_.v3d,
354 new_first_symm_pos_eval_re,
355 ray_start_wo,
356 ray_end_wo,
357 true);
358 const float3 ray_start_su = math::transform_point(world_to_surface_with_symmetry_mat,
359 ray_start_wo);
360 const float3 ray_end_su = math::transform_point(world_to_surface_with_symmetry_mat,
361 ray_end_wo);
362 const float3 ray_direction_su = math::normalize(ray_end_su - ray_start_su);
363
364 /* Find the ray hit that is closest to the initial curve root position. */
365 int tri_index_eval;
366 float3 hit_pos_eval_su;
367 if (!this->find_closest_ray_hit(ray_start_su,
368 ray_direction_su,
369 old_first_pos_eval_su,
370 tri_index_eval,
371 hit_pos_eval_su))
372 {
373 continue;
374 }
375
376 /* Compute the uv of the new surface position on the evaluated mesh. */
377 const int3 &tri_eval = surface_corner_tris_eval_[tri_index_eval];
378 const float3 bary_weights_eval = bke::mesh_surface_sample::compute_bary_coord_in_triangle(
379 surface_positions_eval_, surface_corner_verts_eval_, tri_eval, hit_pos_eval_su);
380 const float2 uv = bke::attribute_math::mix3(bary_weights_eval,
381 surface_uv_map_eval_[tri_eval[0]],
382 surface_uv_map_eval_[tri_eval[1]],
383 surface_uv_map_eval_[tri_eval[2]]);
384
385 /* Try to find the same uv on the original surface. */
386 const ReverseUVSampler::Result result = reverse_uv_sampler_orig.sample(uv);
387 if (result.type != ReverseUVSampler::ResultType::Ok) {
388 found_invalid_uv_mapping_.store(true);
389 continue;
390 }
391 const int3 &tri_orig = surface_corner_tris_orig_[result.tri_index];
392 const float3 &bary_weights_orig = result.bary_weights;
393
394 /* Gather old and new surface normal. */
395 const float3 &initial_normal_cu = slide_curve_info.initial_normal_cu;
396 const float3 new_normal_cu = math::normalize(
397 math::transform_point(transforms_.surface_to_curves_normal,
398 geometry::compute_surface_point_normal(
399 tri_orig, result.bary_weights, corner_normals_orig_su_)));
400
401 /* Gather old and new surface position. */
402 const float3 new_first_pos_orig_su = bke::attribute_math::mix3<float3>(
403 bary_weights_orig,
404 positions_orig_su[corner_verts_orig[tri_orig[0]]],
405 positions_orig_su[corner_verts_orig[tri_orig[1]]],
406 positions_orig_su[corner_verts_orig[tri_orig[2]]]);
407 const float3 old_first_pos_orig_cu = self_->initial_positions_cu_[first_point_i];
408 const float3 new_first_pos_orig_cu = math::transform_point(transforms_.surface_to_curves,
409 new_first_pos_orig_su);
410
411 /* Actually transform curve points. */
412 const float4x4 slide_transform = this->get_slide_transform(
413 old_first_pos_orig_cu, new_first_pos_orig_cu, initial_normal_cu, new_normal_cu);
414 for (const int point_i : points) {
415 positions_orig_cu[point_i] = math::transform_point(
416 slide_transform, self_->initial_positions_cu_[point_i]);
417 }
418 surface_uv_coords[curve_i] = uv;
419 }
420 });
421 }
422
423 bool find_closest_ray_hit(const float3 &ray_start_su,
424 const float3 &ray_direction_su,
425 const float3 &point_su,
426 int &r_tri_index,
427 float3 &r_hit_pos)
428 {
429 float best_dist_sq_su = FLT_MAX;
430 int best_tri_index_eval;
431 float3 best_hit_pos_su;
432 BLI_bvhtree_ray_cast_all_cpp(
433 *surface_bvh_eval_.tree,
434 ray_start_su,
435 ray_direction_su,
436 0.0f,
437 FLT_MAX,
438 [&](const int tri_index, const BVHTreeRay &ray, BVHTreeRayHit &hit) {
439 surface_bvh_eval_.raycast_callback(&surface_bvh_eval_, tri_index, &ray, &hit);
440 if (hit.index < 0) {
441 return;
442 }
443 const float3 &hit_pos_su = hit.co;
444 const float dist_sq_su = math::distance_squared(hit_pos_su, point_su);
445 if (dist_sq_su < best_dist_sq_su) {
446 best_dist_sq_su = dist_sq_su;
447 best_hit_pos_su = hit_pos_su;
448 best_tri_index_eval = hit.index;
449 }
450 });
451
452 if (best_dist_sq_su == FLT_MAX) {
453 return false;
454 }
455 r_tri_index = best_tri_index_eval;
456 r_hit_pos = best_hit_pos_su;
457 return true;
458 }
459
461 const float3 &new_root_pos,
462 const float3 &old_normal,
463 const float3 &new_normal)
464 {
465 float3x3 rotation_3x3;
466 rotation_between_vecs_to_mat3(rotation_3x3.ptr(), old_normal, new_normal);
467
468 float4x4 transform = float4x4::identity();
469 transform.location() -= old_root_pos;
470 transform = float4x4(rotation_3x3) * transform;
471 transform.location() += new_root_pos;
472 return transform;
473 }
474};
475
476void SlideOperation::on_stroke_extended(const bContext &C, const StrokeExtension &stroke_extension)
477{
478 SlideOperationExecutor executor{C};
479 executor.execute(*this, C, stroke_extension);
480}
481
482std::unique_ptr<CurvesSculptStrokeOperation> new_slide_operation()
483{
484 return std::make_unique<SlideOperation>();
485}
486
487} // namespace blender::ed::sculpt_paint
int BKE_brush_size_get(const Scene *scene, const Brush *brush)
Definition brush.cc:1075
float BKE_brush_curve_strength(eBrushCurvePreset preset, const CurveMapping *cumap, float distance, float brush_radius)
Definition brush.cc:1388
void free_bvhtree_from_mesh(BVHTreeFromMesh *data)
Definition bvhutils.cc:1160
BVHTree * BKE_bvhtree_from_mesh_get(BVHTreeFromMesh *data, const Mesh *mesh, BVHCacheType bvh_cache_type, int tree_type)
Definition bvhutils.cc:899
@ BVHTREE_FROM_CORNER_TRIS
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:654
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:125
MINLINE float pow2f(float x)
void rotation_between_vecs_to_mat3(float m[3][3], const float v1[3], const float v2[3])
#define BLI_SCOPED_DEFER(function_to_defer)
#define UNUSED_VARS(...)
void DEG_id_tag_update(ID *id, unsigned int flags)
Object * DEG_get_evaluated_object(const Depsgraph *depsgraph, Object *object)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1041
eCurvesSymmetryType
@ OB_MESH
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:634
blender::float4x4 ED_view3d_ob_project_mat_get(const RegionView3D *rv3d, const Object *ob)
#define NC_GEOM
Definition WM_types.hh:360
#define ND_DATA
Definition WM_types.hh:475
PyObject * self
SIMD_FORCE_INLINE btVector3 transform(const btVector3 &point) const
AttributeSet attributes
constexpr IndexRange index_range() const
Definition BLI_span.hh:402
void append(const T &value)
GAttributeReader lookup_or_default(StringRef attribute_id, AttrDomain domain, eCustomDataType data_type, const void *default_value=nullptr) const
MutableSpan< float3 > positions_for_write()
OffsetIndices< int > points_by_curve() const
MutableSpan< float2 > surface_uv_coords_for_write()
Span< float3 > positions() const
Span< float2 > surface_uv_coords() const
void on_stroke_extended(const bContext &C, const StrokeExtension &stroke_extension) override
Result sample(const float2 &query_uv) const
void foreach_segment(Fn &&fn) const
GeometryDeformation get_evaluated_curves_deformation(const Object *ob_eval, const Object &ob_orig)
IndexMask retrieve_selected_curves(const bke::CurvesGeometry &curves, IndexMaskMemory &memory)
std::optional< CurvesBrush3D > sample_curves_surface_3d_brush(const Depsgraph &depsgraph, const ARegion &region, const View3D &v3d, const CurvesSurfaceTransforms &transforms, const BVHTreeFromMesh &surface_bvh, const float2 &brush_pos_re, const float brush_radius_re)
void report_empty_evaluated_surface(ReportList *reports)
void report_missing_uv_map_on_original_surface(ReportList *reports)
void report_missing_uv_map_on_evaluated_surface(ReportList *reports)
void report_missing_surface(ReportList *reports)
void report_empty_original_surface(ReportList *reports)
float brush_strength_get(const Scene &scene, const Brush &brush, const StrokeExtension &stroke_extension)
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:95
#define FLT_MAX
Definition stdcycles.h:14
CurvesGeometry geometry
struct Object * surface
char * surface_uv_map
int faces_num
struct ToolSettings * toolsettings
CurvesSculpt * curves_sculpt
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)