Welcome to mirror list, hosted at ThFree Co, Russian Federation.

curves_sculpt_selection_paint.cc « sculpt_paint « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b40aebcaaf130f78ea1a941d2976a1a3a2485f93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/* SPDX-License-Identifier: GPL-2.0-or-later */

#include <algorithm>
#include <numeric>

#include "BLI_memory_utils.hh"
#include "BLI_task.hh"

#include "DNA_brush_types.h"

#include "BKE_brush.h"
#include "BKE_context.h"
#include "BKE_curves.hh"

#include "DEG_depsgraph.h"

#include "ED_screen.h"
#include "ED_view3d.h"

#include "WM_api.h"

#include "curves_sculpt_intern.hh"

/**
 * The code below uses a suffix naming convention to indicate the coordinate space:
 * cu: Local space of the curves object that is being edited.
 * wo: World space.
 * re: 2D coordinates within the region.
 */

namespace blender::ed::sculpt_paint {

using bke::CurvesGeometry;

class SelectionPaintOperation : public CurvesSculptStrokeOperation {
 private:
  bool use_select_;
  bool clear_selection_;

  CurvesBrush3D brush_3d_;

  friend struct SelectionPaintOperationExecutor;

 public:
  SelectionPaintOperation(const bool use_select, const bool clear_selection)
      : use_select_(use_select), clear_selection_(clear_selection)
  {
  }
  void on_stroke_extended(const bContext &C, const StrokeExtension &stroke_extension) override;
};

struct SelectionPaintOperationExecutor {
  SelectionPaintOperation *self_ = nullptr;
  CurvesSculptCommonContext ctx_;

  Object *object_ = nullptr;
  Curves *curves_id_ = nullptr;
  CurvesGeometry *curves_ = nullptr;

  const Brush *brush_ = nullptr;
  float brush_radius_base_re_;
  float brush_radius_factor_;
  float brush_strength_;

  float selection_goal_;

  float2 brush_pos_re_;

  CurvesSculptTransforms transforms_;

  SelectionPaintOperationExecutor(const bContext &C) : ctx_(C)
  {
  }

  void execute(SelectionPaintOperation &self,
               const bContext &C,
               const StrokeExtension &stroke_extension)
  {
    self_ = &self;
    object_ = CTX_data_active_object(&C);

    curves_id_ = static_cast<Curves *>(object_->data);
    curves_ = &CurvesGeometry::wrap(curves_id_->geometry);
    curves_id_->flag |= CV_SCULPT_SELECTION_ENABLED;
    if (curves_->curves_num() == 0) {
      return;
    }

    brush_ = BKE_paint_brush_for_read(&ctx_.scene->toolsettings->curves_sculpt->paint);
    brush_radius_base_re_ = BKE_brush_size_get(ctx_.scene, brush_);
    brush_radius_factor_ = brush_radius_factor(*brush_, stroke_extension);
    brush_strength_ = BKE_brush_alpha_get(ctx_.scene, brush_);

    brush_pos_re_ = stroke_extension.mouse_position;

    if (self.clear_selection_) {
      if (stroke_extension.is_first) {
        if (curves_id_->selection_domain == ATTR_DOMAIN_POINT) {
          curves_->selection_point_float_for_write().fill(0.0f);
        }
        else if (curves_id_->selection_domain == ATTR_DOMAIN_CURVE) {
          curves_->selection_curve_float_for_write().fill(0.0f);
        }
      }
    }

    transforms_ = CurvesSculptTransforms(*object_, curves_id_->surface);

    const eBrushFalloffShape falloff_shape = static_cast<eBrushFalloffShape>(
        brush_->falloff_shape);

    selection_goal_ = self_->use_select_ ? 1.0f : 0.0f;

    if (stroke_extension.is_first) {
      if (falloff_shape == PAINT_FALLOFF_SHAPE_SPHERE) {
        this->initialize_spherical_brush_reference_point();
      }
    }

    if (curves_id_->selection_domain == ATTR_DOMAIN_POINT) {
      MutableSpan<float> selection = curves_->selection_point_float_for_write();
      if (falloff_shape == PAINT_FALLOFF_SHAPE_TUBE) {
        this->paint_point_selection_projected_with_symmetry(selection);
      }
      else if (falloff_shape == PAINT_FALLOFF_SHAPE_SPHERE) {
        this->paint_point_selection_spherical_with_symmetry(selection);
      }
    }
    else {
      MutableSpan<float> selection = curves_->selection_curve_float_for_write();
      if (falloff_shape == PAINT_FALLOFF_SHAPE_TUBE) {
        this->paint_curve_selection_projected_with_symmetry(selection);
      }
      else if (falloff_shape == PAINT_FALLOFF_SHAPE_SPHERE) {
        this->paint_curve_selection_spherical_with_symmetry(selection);
      }
    }

    /* Use #ID_RECALC_GEOMETRY instead of #ID_RECALC_SELECT because
     * selection is handled as a generic attribute for now. */
    DEG_id_tag_update(&curves_id_->id, ID_RECALC_GEOMETRY);
    WM_main_add_notifier(NC_GEOM | ND_DATA, &curves_id_->id);
    ED_region_tag_redraw(ctx_.region);
  }

  void paint_point_selection_projected_with_symmetry(MutableSpan<float> selection)
  {
    const Vector<float4x4> symmetry_brush_transforms = get_symmetry_brush_transforms(
        eCurvesSymmetryType(curves_id_->symmetry));
    for (const float4x4 &brush_transform : symmetry_brush_transforms) {
      this->paint_point_selection_projected(brush_transform, selection);
    }
  }

  void paint_point_selection_projected(const float4x4 &brush_transform,
                                       MutableSpan<float> selection)
  {
    const float4x4 brush_transform_inv = brush_transform.inverted();

    float4x4 projection;
    ED_view3d_ob_project_mat_get(ctx_.rv3d, object_, projection.values);

    Span<float3> positions_cu = curves_->positions();

    const float brush_radius_re = brush_radius_base_re_ * brush_radius_factor_;
    const float brush_radius_sq_re = pow2f(brush_radius_re);

    threading::parallel_for(curves_->points_range(), 1024, [&](const IndexRange point_range) {
      for (const int point_i : point_range) {
        const float3 pos_cu = brush_transform_inv * positions_cu[point_i];

        /* Find the position of the point in screen space. */
        float2 pos_re;
        ED_view3d_project_float_v2_m4(ctx_.region, pos_cu, pos_re, projection.values);

        const float distance_to_brush_sq_re = math::distance_squared(pos_re, brush_pos_re_);
        if (distance_to_brush_sq_re > brush_radius_sq_re) {
          /* Ignore the point because it's too far away. */
          continue;
        }

        const float distance_to_brush_re = std::sqrt(distance_to_brush_sq_re);
        /* A falloff that is based on how far away the point is from the stroke. */
        const float radius_falloff = BKE_brush_curve_strength(
            brush_, distance_to_brush_re, brush_radius_re);
        /* Combine the falloff and brush strength. */
        const float weight = brush_strength_ * radius_falloff;

        selection[point_i] = math::interpolate(selection[point_i], selection_goal_, weight);
      }
    });
  }

  void paint_point_selection_spherical_with_symmetry(MutableSpan<float> selection)
  {
    float4x4 projection;
    ED_view3d_ob_project_mat_get(ctx_.rv3d, object_, projection.values);

    float3 brush_wo;
    ED_view3d_win_to_3d(ctx_.v3d,
                        ctx_.region,
                        transforms_.curves_to_world * self_->brush_3d_.position_cu,
                        brush_pos_re_,
                        brush_wo);
    const float3 brush_cu = transforms_.world_to_curves * brush_wo;

    const Vector<float4x4> symmetry_brush_transforms = get_symmetry_brush_transforms(
        eCurvesSymmetryType(curves_id_->symmetry));

    for (const float4x4 &brush_transform : symmetry_brush_transforms) {
      this->paint_point_selection_spherical(selection, brush_transform * brush_cu);
    }
  }

  void paint_point_selection_spherical(MutableSpan<float> selection, const float3 &brush_cu)
  {
    Span<float3> positions_cu = curves_->positions();

    const float brush_radius_cu = self_->brush_3d_.radius_cu;
    const float brush_radius_sq_cu = pow2f(brush_radius_cu);

    threading::parallel_for(curves_->points_range(), 1024, [&](const IndexRange point_range) {
      for (const int i : point_range) {
        const float3 pos_old_cu = positions_cu[i];

        /* Compute distance to the brush. */
        const float distance_to_brush_sq_cu = math::distance_squared(pos_old_cu, brush_cu);
        if (distance_to_brush_sq_cu > brush_radius_sq_cu) {
          /* Ignore the point because it's too far away. */
          continue;
        }

        const float distance_to_brush_cu = std::sqrt(distance_to_brush_sq_cu);

        /* A falloff that is based on how far away the point is from the stroke. */
        const float radius_falloff = BKE_brush_curve_strength(
            brush_, distance_to_brush_cu, brush_radius_cu);
        /* Combine the falloff and brush strength. */
        const float weight = brush_strength_ * radius_falloff;

        selection[i] = math::interpolate(selection[i], selection_goal_, weight);
      }
    });
  }

  void paint_curve_selection_projected_with_symmetry(MutableSpan<float> selection)
  {
    const Vector<float4x4> symmetry_brush_transforms = get_symmetry_brush_transforms(
        eCurvesSymmetryType(curves_id_->symmetry));
    for (const float4x4 &brush_transform : symmetry_brush_transforms) {
      this->paint_curve_selection_projected(brush_transform, selection);
    }
  }

  void paint_curve_selection_projected(const float4x4 &brush_transform,
                                       MutableSpan<float> selection)
  {
    const Span<float3> positions_cu = curves_->positions();
    const float4x4 brush_transform_inv = brush_transform.inverted();

    float4x4 projection;
    ED_view3d_ob_project_mat_get(ctx_.rv3d, object_, projection.values);

    const float brush_radius_re = brush_radius_base_re_ * brush_radius_factor_;
    const float brush_radius_sq_re = pow2f(brush_radius_re);

    threading::parallel_for(curves_->curves_range(), 1024, [&](const IndexRange curves_range) {
      for (const int curve_i : curves_range) {
        const float max_weight = threading::parallel_reduce(
            curves_->points_for_curve(curve_i).drop_back(1),
            1024,
            0.0f,
            [&](const IndexRange segment_range, const float init) {
              float max_weight = init;
              for (const int segment_i : segment_range) {
                const float3 pos1_cu = brush_transform_inv * positions_cu[segment_i];
                const float3 pos2_cu = brush_transform_inv * positions_cu[segment_i + 1];

                float2 pos1_re;
                float2 pos2_re;
                ED_view3d_project_float_v2_m4(ctx_.region, pos1_cu, pos1_re, projection.values);
                ED_view3d_project_float_v2_m4(ctx_.region, pos2_cu, pos2_re, projection.values);

                const float distance_sq_re = dist_squared_to_line_segment_v2(
                    brush_pos_re_, pos1_re, pos2_re);
                if (distance_sq_re > brush_radius_sq_re) {
                  continue;
                }
                const float radius_falloff = BKE_brush_curve_strength(
                    brush_, std::sqrt(distance_sq_re), brush_radius_re);
                const float weight = brush_strength_ * radius_falloff;
                max_weight = std::max(max_weight, weight);
              }
              return max_weight;
            },
            [](float a, float b) { return std::max(a, b); });
        selection[curve_i] = math::interpolate(selection[curve_i], selection_goal_, max_weight);
      }
    });
  }

  void paint_curve_selection_spherical_with_symmetry(MutableSpan<float> selection)
  {
    float4x4 projection;
    ED_view3d_ob_project_mat_get(ctx_.rv3d, object_, projection.values);

    float3 brush_wo;
    ED_view3d_win_to_3d(ctx_.v3d,
                        ctx_.region,
                        transforms_.curves_to_world * self_->brush_3d_.position_cu,
                        brush_pos_re_,
                        brush_wo);
    const float3 brush_cu = transforms_.world_to_curves * brush_wo;

    const Vector<float4x4> symmetry_brush_transforms = get_symmetry_brush_transforms(
        eCurvesSymmetryType(curves_id_->symmetry));

    for (const float4x4 &brush_transform : symmetry_brush_transforms) {
      this->paint_curve_selection_spherical(selection, brush_transform * brush_cu);
    }
  }

  void paint_curve_selection_spherical(MutableSpan<float> selection, const float3 &brush_cu)
  {
    const Span<float3> positions_cu = curves_->positions();

    const float brush_radius_cu = self_->brush_3d_.radius_cu;
    const float brush_radius_sq_cu = pow2f(brush_radius_cu);

    threading::parallel_for(curves_->curves_range(), 1024, [&](const IndexRange curves_range) {
      for (const int curve_i : curves_range) {
        const float max_weight = threading::parallel_reduce(
            curves_->points_for_curve(curve_i).drop_back(1),
            1024,
            0.0f,
            [&](const IndexRange segment_range, const float init) {
              float max_weight = init;
              for (const int segment_i : segment_range) {
                const float3 &pos1_cu = positions_cu[segment_i];
                const float3 &pos2_cu = positions_cu[segment_i + 1];

                const float distance_sq_cu = dist_squared_to_line_segment_v3(
                    brush_cu, pos1_cu, pos2_cu);
                if (distance_sq_cu > brush_radius_sq_cu) {
                  continue;
                }
                const float radius_falloff = BKE_brush_curve_strength(
                    brush_, std::sqrt(distance_sq_cu), brush_radius_cu);
                const float weight = brush_strength_ * radius_falloff;
                max_weight = std::max(max_weight, weight);
              }
              return max_weight;
            },
            [](float a, float b) { return std::max(a, b); });
        selection[curve_i] = math::interpolate(selection[curve_i], selection_goal_, max_weight);
      }
    });
  }

  void initialize_spherical_brush_reference_point()
  {
    std::optional<CurvesBrush3D> brush_3d = sample_curves_3d_brush(*ctx_.depsgraph,
                                                                   *ctx_.region,
                                                                   *ctx_.v3d,
                                                                   *ctx_.rv3d,
                                                                   *object_,
                                                                   brush_pos_re_,
                                                                   brush_radius_base_re_);
    if (brush_3d.has_value()) {
      self_->brush_3d_ = *brush_3d;
    }
  }
};

void SelectionPaintOperation::on_stroke_extended(const bContext &C,
                                                 const StrokeExtension &stroke_extension)
{
  SelectionPaintOperationExecutor executor{C};
  executor.execute(*this, C, stroke_extension);
}

std::unique_ptr<CurvesSculptStrokeOperation> new_selection_paint_operation(
    const BrushStrokeMode brush_mode, const bContext &C)
{
  Scene &scene = *CTX_data_scene(&C);
  Brush &brush = *BKE_paint_brush(&scene.toolsettings->curves_sculpt->paint);
  const bool use_select = ELEM(brush_mode, BRUSH_STROKE_INVERT) ==
                          ((brush.flag & BRUSH_DIR_IN) != 0);
  const bool clear_selection = use_select && brush_mode != BRUSH_STROKE_SMOOTH;

  return std::make_unique<SelectionPaintOperation>(use_select, clear_selection);
}

}  // namespace blender::ed::sculpt_paint