From 2f581a779c1adfedc35d3ae72b52023e5d7587f1 Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Sun, 23 Apr 2023 14:03:50 -0400 Subject: [PATCH] Cleanup: Use utility constructor to create field operations --- .../blenkernel/intern/type_conversions.cc | 2 +- source/blender/functions/intern/field.cc | 2 +- .../blender/functions/tests/FN_field_test.cc | 31 ++++++++----------- .../geometry/intern/resample_curves.cc | 9 ++---- .../geometry/nodes/node_geo_extrude_mesh.cc | 5 ++- .../geometry/nodes/node_geo_image_texture.cc | 3 +- .../geometry/nodes/node_geo_mesh_to_points.cc | 10 +++--- .../geometry/nodes/node_geo_proximity.cc | 11 +++---- .../nodes/geometry/nodes/node_geo_raycast.cc | 4 +-- .../intern/geometry_nodes_lazy_function.cc | 4 +-- 10 files changed, 34 insertions(+), 47 deletions(-) diff --git a/source/blender/blenkernel/intern/type_conversions.cc b/source/blender/blenkernel/intern/type_conversions.cc index 094fb628698..ded789272e6 100644 --- a/source/blender/blenkernel/intern/type_conversions.cc +++ b/source/blender/blenkernel/intern/type_conversions.cc @@ -625,7 +625,7 @@ fn::GField DataTypeConversions::try_convert(fn::GField field, const CPPType &to_ } const mf::MultiFunction &fn = *this->get_conversion_multi_function( mf::DataType::ForSingle(from_type), mf::DataType::ForSingle(to_type)); - return {std::make_shared(fn, Vector{std::move(field)})}; + return {fn::FieldOperation::Create(fn, {std::move(field)})}; } } // namespace blender::bke diff --git a/source/blender/functions/intern/field.cc b/source/blender/functions/intern/field.cc index bc55e023950..bbe45af1e00 100644 --- a/source/blender/functions/intern/field.cc +++ b/source/blender/functions/intern/field.cc @@ -522,7 +522,7 @@ Field invert_boolean_field(const Field &field) { static auto not_fn = mf::build::SI1_SO( "Not", [](bool a) { return !a; }, mf::build::exec_presets::AllSpanOrSingle()); - auto not_op = std::make_shared(FieldOperation(not_fn, {field})); + auto not_op = FieldOperation::Create(not_fn, {field}); return Field(not_op); } diff --git a/source/blender/functions/tests/FN_field_test.cc b/source/blender/functions/tests/FN_field_test.cc index 202bc77d959..52786ae5709 100644 --- a/source/blender/functions/tests/FN_field_test.cc +++ b/source/blender/functions/tests/FN_field_test.cc @@ -11,10 +11,8 @@ namespace blender::fn::tests { TEST(field, ConstantFunction) { - /* TODO: Figure out how to not use another "FieldOperation(" inside of std::make_shared. */ - GField constant_field{std::make_shared( - FieldOperation(std::make_unique>(10), {})), - 0}; + GField constant_field{ + FieldOperation::Create(std::make_unique>(10), {}), 0}; Array result(4); @@ -103,8 +101,7 @@ TEST(field, InputAndFunction) GField index_field{std::make_shared()}; auto add_fn = mf::build::SI2_SO("add", [](int a, int b) { return a + b; }); - GField output_field{ - std::make_shared(FieldOperation(add_fn, {index_field, index_field})), 0}; + GField output_field{FieldOperation::Create(add_fn, {index_field, index_field}), 0}; Array result(10); @@ -126,11 +123,10 @@ TEST(field, TwoFunctions) GField index_field{std::make_shared()}; auto add_fn = mf::build::SI2_SO("add", [](int a, int b) { return a + b; }); - GField add_field{ - std::make_shared(FieldOperation(add_fn, {index_field, index_field})), 0}; + GField add_field{FieldOperation::Create(add_fn, {index_field, index_field}), 0}; auto add_10_fn = mf::build::SI1_SO("add_10", [](int a) { return a + 10; }); - GField result_field{std::make_shared(FieldOperation(add_10_fn, {add_field})), 0}; + GField result_field{FieldOperation::Create(add_10_fn, {add_field}), 0}; Array result(10); @@ -181,8 +177,8 @@ TEST(field, FunctionTwoOutputs) GField index_field_1{std::make_shared()}; GField index_field_2{std::make_shared()}; - std::shared_ptr fn = std::make_shared( - FieldOperation(std::make_unique(), {index_field_1, index_field_2})); + std::shared_ptr fn = FieldOperation::Create( + std::make_unique(), {index_field_1, index_field_2}); GField result_field_1{fn, 0}; GField result_field_2{fn, 1}; @@ -212,8 +208,8 @@ TEST(field, TwoFunctionsTwoOutputs) { GField index_field{std::make_shared()}; - std::shared_ptr fn = std::make_shared( - FieldOperation(std::make_unique(), {index_field, index_field})); + std::shared_ptr fn = FieldOperation::Create( + std::make_unique(), {index_field, index_field}); Array mask_indices = {2, 4, 6, 8}; IndexMask mask = mask_indices.as_span(); @@ -222,8 +218,7 @@ TEST(field, TwoFunctionsTwoOutputs) Field intermediate_field{fn, 1}; auto add_10_fn = mf::build::SI1_SO("add_10", [](int a) { return a + 10; }); - Field result_field_2{ - std::make_shared(FieldOperation(add_10_fn, {intermediate_field})), 0}; + Field result_field_2{FieldOperation::Create(add_10_fn, {intermediate_field}), 0}; FieldContext field_context; FieldEvaluator field_evaluator{field_context, &mask}; @@ -245,8 +240,8 @@ TEST(field, TwoFunctionsTwoOutputs) TEST(field, SameFieldTwice) { - GField constant_field{ - std::make_shared(std::make_unique>(10)), 0}; + GField constant_field{FieldOperation::Create(std::make_unique>(10)), + 0}; FieldContext field_context; IndexMask mask{IndexRange(2)}; @@ -266,7 +261,7 @@ TEST(field, SameFieldTwice) TEST(field, IgnoredOutput) { static mf::tests::OptionalOutputsFunction fn; - Field field{std::make_shared(fn), 0}; + Field field{FieldOperation::Create(fn), 0}; FieldContext field_context; FieldEvaluator field_evaluator{field_context, 10}; diff --git a/source/blender/geometry/intern/resample_curves.cc b/source/blender/geometry/intern/resample_curves.cc index 218928c6027..1ea955be68b 100644 --- a/source/blender/geometry/intern/resample_curves.cc +++ b/source/blender/geometry/intern/resample_curves.cc @@ -21,10 +21,7 @@ static fn::Field get_count_input_max_one(const fn::Field &count_field) "Clamp Above One", [](int value) { return std::max(1, value); }, mf::build::exec_presets::AllSpanOrSingle()); - auto clamp_op = std::make_shared( - fn::FieldOperation(max_one_fn, {count_field})); - - return fn::Field(std::move(clamp_op)); + return fn::Field(fn::FieldOperation::Create(max_one_fn, {count_field})); } static fn::Field get_count_input_from_length(const fn::Field &length_field) @@ -39,9 +36,9 @@ static fn::Field get_count_input_from_length(const fn::Field &length }, mf::build::exec_presets::AllSpanOrSingle()); - auto get_count_op = std::make_shared(fn::FieldOperation( + auto get_count_op = fn::FieldOperation::Create( get_count_fn, - {fn::Field(std::make_shared()), length_field})); + {fn::Field(std::make_shared()), length_field}); return fn::Field(std::move(get_count_op)); } diff --git a/source/blender/nodes/geometry/nodes/node_geo_extrude_mesh.cc b/source/blender/nodes/geometry/nodes/node_geo_extrude_mesh.cc index a3d89e2f796..8e93e42f5fd 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_extrude_mesh.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_extrude_mesh.cc @@ -1343,9 +1343,8 @@ static void node_geo_exec(GeoNodeExecParams params) "Scale", [](const float3 &offset, const float scale) { return offset * scale; }, mf::build::exec_presets::AllSpanOrSingle()); - std::shared_ptr multiply_op = std::make_shared( - FieldOperation(multiply_fn, {std::move(offset_field), std::move(scale_field)})); - const Field final_offset{std::move(multiply_op)}; + const Field final_offset{ + FieldOperation::Create(multiply_fn, {std::move(offset_field), std::move(scale_field)})}; AttributeOutputs attribute_outputs; attribute_outputs.top_id = params.get_output_anonymous_attribute_id_if_needed("Top"); diff --git a/source/blender/nodes/geometry/nodes/node_geo_image_texture.cc b/source/blender/nodes/geometry/nodes/node_geo_image_texture.cc index d98b8d12065..1ec2383ffb9 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_image_texture.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_image_texture.cc @@ -415,8 +415,7 @@ static void node_geo_exec(GeoNodeExecParams params) Field vector_field = params.extract_input>("Vector"); - auto image_op = std::make_shared( - FieldOperation(std::move(image_fn), {std::move(vector_field)})); + auto image_op = FieldOperation::Create(std::move(image_fn), {std::move(vector_field)}); params.set_output("Color", Field(image_op, 0)); params.set_output("Alpha", Field(image_op, 1)); diff --git a/source/blender/nodes/geometry/nodes/node_geo_mesh_to_points.cc b/source/blender/nodes/geometry/nodes/node_geo_mesh_to_points.cc index c4c0d4b1517..2df6b70cae6 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_mesh_to_points.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_mesh_to_points.cc @@ -47,9 +47,9 @@ static void node_init(bNodeTree * /*tree*/, bNode *node) } static void geometry_set_mesh_to_points(GeometrySet &geometry_set, - Field &position_field, - Field &radius_field, - Field &selection_field, + const Field &position_field, + const Field &radius_field, + const Field &selection_field, const eAttrDomain domain, const AnonymousAttributePropagationInfo &propagation_info) { @@ -151,9 +151,7 @@ static void node_geo_exec(GeoNodeExecParams params) __func__, [](float value) { return std::max(0.0f, value); }, mf::build::exec_presets::AllSpanOrSingle()); - auto max_zero_op = std::make_shared( - FieldOperation(max_zero_fn, {std::move(radius)})); - Field positive_radius(std::move(max_zero_op), 0); + const Field positive_radius(FieldOperation::Create(max_zero_fn, {std::move(radius)}), 0); const NodeGeometryMeshToPoints &storage = node_storage(params.node()); const GeometryNodeMeshToPointsMode mode = (GeometryNodeMeshToPointsMode)storage.mode; diff --git a/source/blender/nodes/geometry/nodes/node_geo_proximity.cc b/source/blender/nodes/geometry/nodes/node_geo_proximity.cc index 006efa0ad27..99b9832913c 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_proximity.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_proximity.cc @@ -198,10 +198,10 @@ class ProximityFunction : public mf::MultiFunction { static void node_geo_exec(GeoNodeExecParams params) { - GeometrySet geometry_set_target = params.extract_input("Target"); - geometry_set_target.ensure_owns_direct_data(); + GeometrySet target = params.extract_input("Target"); + target.ensure_owns_direct_data(); - if (!geometry_set_target.has_mesh() && !geometry_set_target.has_pointcloud()) { + if (!target.has_mesh() && !target.has_pointcloud()) { params.set_default_remaining_outputs(); return; } @@ -210,9 +210,8 @@ static void node_geo_exec(GeoNodeExecParams params) Field position_field = params.extract_input>("Source Position"); auto proximity_fn = std::make_unique( - std::move(geometry_set_target), GeometryNodeProximityTargetType(storage.target_element)); - auto proximity_op = std::make_shared( - FieldOperation(std::move(proximity_fn), {std::move(position_field)})); + std::move(target), GeometryNodeProximityTargetType(storage.target_element)); + auto proximity_op = FieldOperation::Create(std::move(proximity_fn), {std::move(position_field)}); params.set_output("Position", Field(proximity_op, 0)); params.set_output("Distance", Field(proximity_op, 1)); diff --git a/source/blender/nodes/geometry/nodes/node_geo_raycast.cc b/source/blender/nodes/geometry/nodes/node_geo_raycast.cc index 666a10079d3..287b18fb28f 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_raycast.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_raycast.cc @@ -407,9 +407,9 @@ static void node_geo_exec(GeoNodeExecParams params) Field length_field = params.extract_input>("Ray Length"); auto fn = std::make_unique(std::move(target), std::move(field), mapping); - auto op = std::make_shared(FieldOperation( + auto op = FieldOperation::Create( std::move(fn), - {std::move(position_field), std::move(direction_field), std::move(length_field)})); + {std::move(position_field), std::move(direction_field), std::move(length_field)}); params.set_output("Is Hit", Field(op, 0)); params.set_output("Hit Position", Field(op, 1)); diff --git a/source/blender/nodes/intern/geometry_nodes_lazy_function.cc b/source/blender/nodes/intern/geometry_nodes_lazy_function.cc index 663e48c6116..0af6bd940a8 100644 --- a/source/blender/nodes/intern/geometry_nodes_lazy_function.cc +++ b/source/blender/nodes/intern/geometry_nodes_lazy_function.cc @@ -341,10 +341,10 @@ static void execute_multi_function_on_value_or_field( /* Construct the new field node. */ std::shared_ptr operation; if (owned_fn) { - operation = std::make_shared(owned_fn, std::move(input_fields)); + operation = fn::FieldOperation::Create(owned_fn, std::move(input_fields)); } else { - operation = std::make_shared(fn, std::move(input_fields)); + operation = fn::FieldOperation::Create(fn, std::move(input_fields)); } /* Store the new fields in the output. */