[Bf-blender-cvs] [ed62b65474f] master: Cleanup: Use const in curves sculpt code

Hans Goudey noreply at git.blender.org
Tue May 17 13:06:21 CEST 2022


Commit: ed62b65474f007025bc19d1f8758257b12cbc8b3
Author: Hans Goudey
Date:   Tue May 17 13:06:14 2022 +0200
Branches: master
https://developer.blender.org/rBed62b65474f007025bc19d1f8758257b12cbc8b3

Cleanup: Use const in curves sculpt code

This makes it much clearer what data is supposed to be modified
and what data is just used to influence the operation. The new
`BKE_paint_brush_for_read` function isn't great design, but it
can be removed or renamed if similar changes are applied to
more places.

Also pass pointers explicitly to `sample_curves_3d_brush` rather
than reusing the `bContext`. This makes it clearer what data the
function actually needs.

Differential Revision: https://developer.blender.org/D14967

===================================================================

M	source/blender/blenkernel/BKE_paint.h
M	source/blender/blenkernel/intern/paint.c
M	source/blender/editors/sculpt_paint/curves_sculpt_add.cc
M	source/blender/editors/sculpt_paint/curves_sculpt_brush.cc
M	source/blender/editors/sculpt_paint/curves_sculpt_comb.cc
M	source/blender/editors/sculpt_paint/curves_sculpt_delete.cc
M	source/blender/editors/sculpt_paint/curves_sculpt_grow_shrink.cc
M	source/blender/editors/sculpt_paint/curves_sculpt_intern.hh
M	source/blender/editors/sculpt_paint/curves_sculpt_ops.cc
M	source/blender/editors/sculpt_paint/curves_sculpt_snake_hook.cc

===================================================================

diff --git a/source/blender/blenkernel/BKE_paint.h b/source/blender/blenkernel/BKE_paint.h
index 03cbcf575c3..c39ab22ce3a 100644
--- a/source/blender/blenkernel/BKE_paint.h
+++ b/source/blender/blenkernel/BKE_paint.h
@@ -180,6 +180,7 @@ struct Paint *BKE_paint_get_active_from_context(const struct bContext *C);
 ePaintMode BKE_paintmode_get_active_from_context(const struct bContext *C);
 ePaintMode BKE_paintmode_get_from_tool(const struct bToolRef *tref);
 struct Brush *BKE_paint_brush(struct Paint *paint);
+const struct Brush *BKE_paint_brush_for_read(const struct Paint *p);
 void BKE_paint_brush_set(struct Paint *paint, struct Brush *br);
 struct Palette *BKE_paint_palette(struct Paint *paint);
 void BKE_paint_palette_set(struct Paint *p, struct Palette *palette);
diff --git a/source/blender/blenkernel/intern/paint.c b/source/blender/blenkernel/intern/paint.c
index caada8da0de..cff7eb20b05 100644
--- a/source/blender/blenkernel/intern/paint.c
+++ b/source/blender/blenkernel/intern/paint.c
@@ -605,6 +605,11 @@ ePaintMode BKE_paintmode_get_from_tool(const struct bToolRef *tref)
 }
 
 Brush *BKE_paint_brush(Paint *p)
+{
+  return (Brush *)BKE_paint_brush_for_read((const Paint *)p);
+}
+
+const Brush *BKE_paint_brush_for_read(const Paint *p)
 {
   return p ? p->brush : NULL;
 }
diff --git a/source/blender/editors/sculpt_paint/curves_sculpt_add.cc b/source/blender/editors/sculpt_paint/curves_sculpt_add.cc
index 5539fda750f..04ae6c62aee 100644
--- a/source/blender/editors/sculpt_paint/curves_sculpt_add.cc
+++ b/source/blender/editors/sculpt_paint/curves_sculpt_add.cc
@@ -66,7 +66,7 @@ class AddOperation : public CurvesSculptStrokeOperation {
     }
   }
 
-  void on_stroke_extended(bContext *C, const StrokeExtension &stroke_extension) override;
+  void on_stroke_extended(const bContext &C, const StrokeExtension &stroke_extension) override;
 };
 
 static void initialize_straight_curve_positions(const float3 &p1,
@@ -85,11 +85,12 @@ static void initialize_straight_curve_positions(const float3 &p1,
  */
 struct AddOperationExecutor {
   AddOperation *self_ = nullptr;
-  Depsgraph *depsgraph_ = nullptr;
-  Scene *scene_ = nullptr;
-  Object *object_ = nullptr;
+  const Depsgraph *depsgraph_ = nullptr;
+  const Scene *scene_ = nullptr;
   ARegion *region_ = nullptr;
-  View3D *v3d_ = nullptr;
+  const View3D *v3d_ = nullptr;
+
+  Object *object_ = nullptr;
   Curves *curves_id_ = nullptr;
   CurvesGeometry *curves_ = nullptr;
 
@@ -98,9 +99,9 @@ struct AddOperationExecutor {
   Span<MLoopTri> surface_looptris_;
   Span<float3> corner_normals_su_;
 
-  CurvesSculpt *curves_sculpt_ = nullptr;
-  Brush *brush_ = nullptr;
-  BrushCurvesSculptSettings *brush_settings_ = nullptr;
+  const CurvesSculpt *curves_sculpt_ = nullptr;
+  const Brush *brush_ = nullptr;
+  const BrushCurvesSculptSettings *brush_settings_ = nullptr;
 
   float brush_radius_re_;
   float2 brush_pos_re_;
@@ -142,14 +143,14 @@ struct AddOperationExecutor {
   static constexpr int max_neighbors = 5;
   using NeighborsVector = Vector<NeighborInfo, max_neighbors>;
 
-  void execute(AddOperation &self, bContext *C, const StrokeExtension &stroke_extension)
+  void execute(AddOperation &self, const bContext &C, const StrokeExtension &stroke_extension)
   {
     self_ = &self;
-    depsgraph_ = CTX_data_depsgraph_pointer(C);
-    scene_ = CTX_data_scene(C);
-    object_ = CTX_data_active_object(C);
-    region_ = CTX_wm_region(C);
-    v3d_ = CTX_wm_view3d(C);
+    depsgraph_ = CTX_data_depsgraph_pointer(&C);
+    scene_ = CTX_data_scene(&C);
+    object_ = CTX_data_active_object(&C);
+    region_ = CTX_wm_region(&C);
+    v3d_ = CTX_wm_view3d(&C);
 
     curves_id_ = static_cast<Curves *>(object_->data);
     curves_ = &CurvesGeometry::wrap(curves_id_->geometry);
@@ -176,7 +177,7 @@ struct AddOperationExecutor {
         surface_->totloop};
 
     curves_sculpt_ = scene_->toolsettings->curves_sculpt;
-    brush_ = BKE_paint_brush(&curves_sculpt_->paint);
+    brush_ = BKE_paint_brush_for_read(&curves_sculpt_->paint);
     brush_settings_ = brush_->curves_sculpt_settings;
     brush_radius_re_ = BKE_brush_size_get(scene_, brush_);
     brush_pos_re_ = stroke_extension.mouse_position;
@@ -864,17 +865,18 @@ struct AddOperationExecutor {
   }
 };
 
-void AddOperation::on_stroke_extended(bContext *C, const StrokeExtension &stroke_extension)
+void AddOperation::on_stroke_extended(const bContext &C, const StrokeExtension &stroke_extension)
 {
   AddOperationExecutor executor;
   executor.execute(*this, C, stroke_extension);
 }
 
-std::unique_ptr<CurvesSculptStrokeOperation> new_add_operation(bContext &C, ReportList *reports)
+std::unique_ptr<CurvesSculptStrokeOperation> new_add_operation(const bContext &C,
+                                                               ReportList *reports)
 {
-  Object &ob_active = *CTX_data_active_object(&C);
+  const Object &ob_active = *CTX_data_active_object(&C);
   BLI_assert(ob_active.type == OB_CURVES);
-  Curves &curves_id = *static_cast<Curves *>(ob_active.data);
+  const Curves &curves_id = *static_cast<Curves *>(ob_active.data);
   if (curves_id.surface == nullptr || curves_id.surface->type != OB_MESH) {
     BKE_report(reports, RPT_WARNING, "Can not use Add brush when there is no surface mesh");
     return {};
diff --git a/source/blender/editors/sculpt_paint/curves_sculpt_brush.cc b/source/blender/editors/sculpt_paint/curves_sculpt_brush.cc
index dee9615ce76..9ebbf72806e 100644
--- a/source/blender/editors/sculpt_paint/curves_sculpt_brush.cc
+++ b/source/blender/editors/sculpt_paint/curves_sculpt_brush.cc
@@ -141,23 +141,21 @@ static std::optional<float3> find_curves_brush_position(const CurvesGeometry &cu
   return best_candidate.position_cu;
 }
 
-std::optional<CurvesBrush3D> sample_curves_3d_brush(bContext &C,
-                                                    Object &curves_object,
+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)
 {
-  const Depsgraph *depsgraph = CTX_data_depsgraph_pointer(&C);
-  const ARegion *region = CTX_wm_region(&C);
-  const View3D *v3d = CTX_wm_view3d(&C);
-  const RegionView3D *rv3d = CTX_wm_region_view3d(&C);
-
   const Curves &curves_id = *static_cast<Curves *>(curves_object.data);
   const CurvesGeometry &curves = CurvesGeometry::wrap(curves_id.geometry);
   const Object *surface_object = curves_id.surface;
 
   float3 center_ray_start_wo, center_ray_end_wo;
   ED_view3d_win_to_segment_clipped(
-      depsgraph, region, v3d, brush_pos_re, center_ray_start_wo, center_ray_end_wo, true);
+      &depsgraph, &region, &v3d, brush_pos_re, center_ray_start_wo, center_ray_end_wo, true);
 
   /* Shorten ray when the surface object is hit. */
   if (surface_object != nullptr) {
@@ -205,8 +203,8 @@ std::optional<CurvesBrush3D> sample_curves_3d_brush(bContext &C,
       center_ray_start_cu,
       center_ray_end_cu,
       brush_radius_re,
-      *region,
-      *rv3d,
+      region,
+      rv3d,
       curves_object);
   if (!brush_position_optional_cu.has_value()) {
     /* Nothing found. */
@@ -216,9 +214,9 @@ std::optional<CurvesBrush3D> sample_curves_3d_brush(bContext &C,
 
   /* Determine the 3D brush radius. */
   float3 radius_ray_start_wo, radius_ray_end_wo;
-  ED_view3d_win_to_segment_clipped(depsgraph,
-                                   region,
-                                   v3d,
+  ED_view3d_win_to_segment_clipped(&depsgraph,
+                                   &region,
+                                   &v3d,
                                    brush_pos_re + float2(brush_radius_re, 0.0f),
                                    radius_ray_start_wo,
                                    radius_ray_end_wo,
diff --git a/source/blender/editors/sculpt_paint/curves_sculpt_comb.cc b/source/blender/editors/sculpt_paint/curves_sculpt_comb.cc
index 41199be8886..72257659c52 100644
--- a/source/blender/editors/sculpt_paint/curves_sculpt_comb.cc
+++ b/source/blender/editors/sculpt_paint/curves_sculpt_comb.cc
@@ -69,7 +69,7 @@ class CombOperation : public CurvesSculptStrokeOperation {
   friend struct CombOperationExecutor;
 
  public:
-  void on_stroke_extended(bContext *C, const StrokeExtension &stroke_extension) override;
+  void on_stroke_extended(const bContext &C, const StrokeExtension &stroke_extension) override;
 };
 
 /**
@@ -78,21 +78,20 @@ class CombOperation : public CurvesSculptStrokeOperation {
  */
 struct CombOperationExecutor {
   CombOperation *self_ = nullptr;
-  bContext *C_ = nullptr;
-  Depsgraph *depsgraph_ = nullptr;
-  Scene *scene_ = nullptr;
-  Object *object_ = nullptr;
+  const Depsgraph *depsgraph_ = nullptr;
+  const Scene *scene_ = nullptr;
   ARegion *region_ = nullptr;
-  View3D *v3d_ = nullptr;
-  RegionView3D *rv3d_ = nullptr;
+  const View3D *v3d_ = nullptr;
+  const RegionView3D *rv3d_ = nullptr;
 
-  CurvesSculpt *curves_sculpt_ = nullptr;
-  Brush *brush_ = nullptr;
+  const CurvesSculpt *curves_sculpt_ = nullptr;
+  const Brush *brush_ = nullptr;
   float brush_radius_re_;
   float brush_strength_;
 
   eBrushFalloffShape falloff_shape_;
 
+  Object *object_ = nullptr;
   Curves *curves_id_ = nullptr;
   CurvesGeometry *curves_ = nullptr;
 
@@ -112,22 +111,21 @@ struct CombOperationExecutor {
 
   BVHTreeFromMesh surface_bvh_;
 
-  void execute(CombOperation &self, bContext *C, const StrokeExtension &stroke_extension)
+  void execute(CombOperation &self, const bContext &C, const StrokeExtension &stroke_extension)
   {
     self_ = &self;
 
     BLI_SCOPED_DEFER([&]() { self_->brush_pos_last_re_ = stroke_extension.mouse_position; });
 
-    C_ = C;
-    depsgraph_ = CTX_data_depsgraph_pointer(C);
-    scene_ = CTX_data_scene(C);
-    object_ = CTX_data_active_object(C);
-    region_ = CTX_wm_region(

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list