Blender V5.3
curves/sculpt_smooth.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 "BKE_brush.hh"
6#include "BKE_context.hh"
7#include "BKE_crazyspace.hh"
8#include "BKE_paint.hh"
9
10#include "ED_screen.hh"
11#include "ED_view3d.hh"
12
13#include "DEG_depsgraph.hh"
14
15#include "DNA_brush_types.h"
16
17#include "WM_api.hh"
18
19#include "BLI_task.hh"
20
21#include "sculpt_intern.hh"
22
24
26 private:
28 CurvesBrush3D brush_3d_;
29
31
32 public:
33 void on_stroke_extended(const PaintStroke &stroke,
34 const StrokeExtension &stroke_extension) override;
35};
36
44
45 Object *object_ = nullptr;
46 Curves *curves_id_ = nullptr;
48
52
54 const Brush *brush_ = nullptr;
59
61
62 SmoothOperationExecutor(const PaintStroke &stroke) : ctx_(stroke) {}
63
64 void execute(SmoothOperation &self, const StrokeExtension &stroke_extension)
65 {
66 self_ = &self;
67
69 curves_id_ = id_cast<Curves *>(object_->data);
70 curves_ = &curves_id_->geometry.wrap();
71 if (curves_->is_empty()) {
72 return;
73 }
74
80 brush_pos_re_ = stroke_extension.mouse_position;
81
83 ".selection", bke::AttrDomain::Point, 1.0f);
86
88 if (stroke_extension.is_first) {
89 if (falloff_shape == PAINT_FALLOFF_SHAPE_SPHERE || (U.uiflag & USER_ORBIT_SELECTION)) {
90 self.brush_3d_ = *sample_curves_3d_brush(*ctx_.depsgraph,
91 *ctx_.region,
92 *ctx_.v3d,
93 *ctx_.rv3d,
94 *object_,
100 }
101 }
102
103 Array<float> point_smooth_factors(curves_->points_num(), 0.0f);
104
105 if (falloff_shape == PAINT_FALLOFF_SHAPE_TUBE) {
106 this->find_projected_smooth_factors_with_symmetry(point_smooth_factors);
107 }
108 else if (falloff_shape == PAINT_FALLOFF_SHAPE_SPHERE) {
109 this->find_spherical_smooth_factors_with_symmetry(point_smooth_factors);
110 }
111 else {
113 }
114
115 this->smooth(point_smooth_factors);
120 }
121
123 {
124 const Vector<float4x4> symmetry_brush_transforms = get_symmetry_brush_transforms(
126 for (const float4x4 &brush_transform : symmetry_brush_transforms) {
127 this->find_projected_smooth_factors(brush_transform, r_point_smooth_factors);
128 }
129 }
130
131 void find_projected_smooth_factors(const float4x4 &brush_transform,
132 MutableSpan<float> r_point_smooth_factors)
133 {
134 const float4x4 brush_transform_inv = math::invert(brush_transform);
135
136 const float brush_radius_re = brush_radius_base_re_ * brush_radius_factor_;
137 const float brush_radius_sq_re = pow2f(brush_radius_re);
138
140
141 const bke::crazyspace::GeometryDeformation deformation =
143 const OffsetIndices points_by_curve = curves_->points_by_curve();
144
146 [&](const int curve_i) {
147 const IndexRange points = points_by_curve[curve_i];
148 for (const int point_i : points) {
149 const float3 &pos_cu = math::transform_point(brush_transform_inv,
150 deformation.positions[point_i]);
151 const float2 pos_re = ED_view3d_project_float_v2_m4(ctx_.region, pos_cu, projection);
152 const float dist_to_brush_sq_re = math::distance_squared(pos_re, brush_pos_re_);
153 if (dist_to_brush_sq_re > brush_radius_sq_re) {
154 continue;
155 }
156
157 const float dist_to_brush_re = std::sqrt(dist_to_brush_sq_re);
158 const float radius_falloff = BKE_brush_curve_strength(
159 brush_, dist_to_brush_re, brush_radius_re);
160 /* Used to make the brush easier to use. Otherwise a strength of 1 would be way too
161 * large. */
162 const float weight_factor = 0.1f;
163 const float weight = weight_factor * brush_strength_ * radius_falloff *
164 point_factors_[point_i];
165 math::max_inplace(r_point_smooth_factors[point_i], weight);
166 }
167 },
169 }
170
172 {
173 float3 brush_pos_wo;
175 ctx_.v3d,
176 ctx_.region,
179 brush_pos_wo);
180 const float3 brush_pos_cu = math::transform_point(transforms_.world_to_curves, brush_pos_wo);
181 const float brush_radius_cu = self_->brush_3d_.radius_cu * brush_radius_factor_;
182
183 const Vector<float4x4> symmetry_brush_transforms = get_symmetry_brush_transforms(
185 for (const float4x4 &brush_transform : symmetry_brush_transforms) {
186 this->find_spherical_smooth_factors(math::transform_point(brush_transform, brush_pos_cu),
187 brush_radius_cu,
188 r_point_smooth_factors);
189 }
190 }
191
192 void find_spherical_smooth_factors(const float3 &brush_pos_cu,
193 const float brush_radius_cu,
194 MutableSpan<float> r_point_smooth_factors)
195 {
196 const float brush_radius_sq_cu = pow2f(brush_radius_cu);
197 const bke::crazyspace::GeometryDeformation deformation =
199 const OffsetIndices points_by_curve = curves_->points_by_curve();
200
202 [&](const int curve_i) {
203 const IndexRange points = points_by_curve[curve_i];
204 for (const int point_i : points) {
205 const float3 &pos_cu = deformation.positions[point_i];
206 const float dist_to_brush_sq_cu = math::distance_squared(pos_cu, brush_pos_cu);
207 if (dist_to_brush_sq_cu > brush_radius_sq_cu) {
208 continue;
209 }
210
211 const float dist_to_brush_cu = std::sqrt(dist_to_brush_sq_cu);
212 const float radius_falloff = BKE_brush_curve_strength(
213 brush_, dist_to_brush_cu, brush_radius_cu);
214 /* Used to make the brush easier to use. Otherwise a strength of 1 would be way too
215 * large. */
216 const float weight_factor = 0.1f;
217 const float weight = weight_factor * brush_strength_ * radius_falloff *
218 point_factors_[point_i];
219 math::max_inplace(r_point_smooth_factors[point_i], weight);
220 }
221 },
223 }
224
225 void smooth(const Span<float> point_smooth_factors)
226 {
227 const OffsetIndices points_by_curve = curves_->points_by_curve();
229
231 [&](const IndexMaskSegment segment) {
232 Vector<float3> old_positions;
233 for (const int curve_i : segment) {
234 const IndexRange points = points_by_curve[curve_i];
235 old_positions.clear();
236 old_positions.extend(positions.slice(points));
237 for (const int i : IndexRange(points.size()).drop_front(1).drop_back(1)) {
238 const int point_i = points[i];
239 const float smooth_factor = point_smooth_factors[point_i];
240 if (smooth_factor == 0.0f) {
241 continue;
242 }
243 /* Move towards the middle of the neighboring points. */
244 const float3 old_pos = old_positions[i];
245 const float3 &prev_pos = old_positions[i - 1];
246 const float3 &next_pos = old_positions[i + 1];
247 const float3 goal_pos = math::midpoint(prev_pos, next_pos);
248 const float3 new_pos = math::interpolate(old_pos, goal_pos, smooth_factor);
249 positions[point_i] = new_pos;
250 }
251 }
252 },
254 }
255};
256
258 const StrokeExtension &stroke_extension)
259{
260 SmoothOperationExecutor executor{stroke};
261 executor.execute(*this, stroke_extension);
262}
263
264std::unique_ptr<CurvesSculptStrokeOperation> new_smooth_operation()
265{
266 return std::make_unique<SmoothOperation>();
267}
268
269} // namespace blender::ed::sculpt_paint
#define NC_GEOM
Definition WM_types.hh:406
#define ND_DATA
Definition WM_types.hh:552
#define BLI_assert_unreachable()
constexpr int64_t size() const
constexpr IndexRange()=default
constexpr MutableSpan slice(const int64_t start, const int64_t size) const
Definition BLI_span.hh:579
void extend(Span< T > array)
GAttributeReader lookup_or_default(StringRef name, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
MutableSpan< float3 > positions_for_write()
AttributeAccessor attributes() const
OffsetIndices< int > points_by_curve() const
void on_stroke_extended(const PaintStroke &stroke, const StrokeExtension &stroke_extension) override
void foreach_segment(Fn &&fn, Mode mode=exec_mode::serial) const
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
GeometryDeformation get_evaluated_curves_deformation(const Object *ob_eval, const Object &ob_orig)
IndexMask retrieve_selected_curves(const bke::CurvesGeometry &curves, LinearAllocator<> &memory)
float brush_strength_get(const Paint &paint, const Brush &brush, const StrokeExtension &stroke_extension)
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)
void remember_stroke_position(CurvesSculpt &curves_sculpt, const float3 &brush_position_wo)
std::unique_ptr< CurvesSculptStrokeOperation > new_smooth_operation()
Vector< float4x4 > get_symmetry_brush_transforms(const eCurvesSymmetryType symmetry)
float brush_radius_factor(const Brush &brush, const StrokeExtension &stroke_extension)
constexpr ParallelGrainSize grain_size(int grain_size)
CartesianBasis invert(const CartesianBasis &basis)
T midpoint(const T &a, const T &b)
T interpolate(const T &a, const T &b, const FactorT &t)
T distance_squared(const VecBase< T, Size > &a, const VecBase< T, Size > &b)
void max_inplace(T &a, const T &b)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
void WM_main_add_notifier(uint type, void *reference)
UserDef U
Definition blender.cc:58
float4x4 ED_view3d_ob_project_mat_get(const RegionView3D *rv3d, const Object *ob)
float BKE_brush_radius_get(const Paint *paint, const Brush *brush)
Definition brush.cc:1366
@ PAINT_FALLOFF_SHAPE_SPHERE
@ PAINT_FALLOFF_SHAPE_TUBE
float2 ED_view3d_project_float_v2_m4(const ARegion *region, const float co[3], const float4x4 &mat)
MatBase< float, 4, 4 > float4x4
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:681
MINLINE float pow2f(float x)
VecBase< float, 2 > float2
void DEG_id_tag_update(ID *id, unsigned int flags)
float BKE_brush_curve_strength(eBrushCurvePreset preset, const CurveMapping *cumap, float distance, float brush_radius)
Definition brush.cc:1691
const Brush * BKE_paint_brush_for_read(const Paint *paint)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
VecBase< float, 3 > float3
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])
eBrushFalloffShape falloff_shape
struct ToolSettings * toolsettings
CurvesSculpt * curves_sculpt
void find_spherical_smooth_factors(const float3 &brush_pos_cu, const float brush_radius_cu, MutableSpan< float > r_point_smooth_factors)
void find_projected_smooth_factors(const float4x4 &brush_transform, MutableSpan< float > r_point_smooth_factors)
void smooth(const Span< float > point_smooth_factors)
void find_spherical_smooth_factors_with_symmetry(MutableSpan< float > r_point_smooth_factors)
void execute(SmoothOperation &self, const StrokeExtension &stroke_extension)
void find_projected_smooth_factors_with_symmetry(MutableSpan< float > r_point_smooth_factors)