Blender V5.3
node_geo_transform_geometry.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 "BLI_math_base_c.hh"
6#include "BLI_math_matrix.hh"
8
9#include "GEO_transform.hh"
10
11#include "node_geometry_util.hh"
12
14
17 "COMPONENTS",
18 0,
19 N_("Components"),
20 N_("Provide separate location, rotation and scale")},
21 {GEO_NODE_TRANSFORM_MODE_MATRIX, "MATRIX", 0, N_("Matrix"), N_("Use a transformation matrix")},
22 {0, nullptr, 0, nullptr, nullptr},
23};
24
26{
27 b.use_custom_socket_order();
28 b.allow_any_socket_order();
29
30 b.add_input<decl::Geometry>("Geometry"_ustr)
31 .is_default_link_socket()
32 .description("Geometry to transform");
33 b.add_output<decl::Geometry>("Geometry"_ustr).propagate_all_geometry().align_with_previous();
34 b.add_input<decl::Menu>("Mode"_ustr)
35 .static_items(mode_items)
37 .description("How the transformation is specified");
38 b.add_input<decl::Vector>("Translation"_ustr)
39 .subtype(PROP_TRANSLATION)
40 .usage_by_single_menu(GEO_NODE_TRANSFORM_MODE_COMPONENTS);
41 b.add_input<decl::Rotation>("Rotation"_ustr)
42 .usage_by_single_menu(GEO_NODE_TRANSFORM_MODE_COMPONENTS);
43 b.add_input<decl::Vector>("Scale"_ustr)
44 .default_value({1, 1, 1})
45 .subtype(PROP_XYZ)
46 .usage_by_single_menu(GEO_NODE_TRANSFORM_MODE_COMPONENTS);
47 b.add_input<decl::Matrix>("Transform"_ustr).usage_by_single_menu(GEO_NODE_TRANSFORM_MODE_MATRIX);
48}
49
50static bool use_translate(const math::Quaternion &rotation, const float3 scale)
51{
52 if (math::length_squared(rotation.imaginary_part()) > 1e-10f) {
53 return false;
54 }
55 if (compare_ff(scale.x, 1.0f, 1e-9f) != 1 || compare_ff(scale.y, 1.0f, 1e-9f) != 1 ||
56 compare_ff(scale.z, 1.0f, 1e-9f) != 1)
57 {
58 return false;
59 }
60 return true;
61}
62
63static void report_errors(GeoNodeExecParams &params,
65{
66 if (errors.bad_volume_transform) {
68 TIP_("Invalid transformation for volume grids"));
69 }
70 else if (errors.volume_too_small) {
72 TIP_("Volume scale is lower than permitted by OpenVDB"));
73 }
74}
75
77{
78 const auto mode = params.get_input<NodeGeometryTransformMode>("Mode"_ustr);
79 GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry"_ustr);
80
82 const float4x4 transform = params.extract_input<float4x4>("Transform"_ustr);
83 if (auto errors = geometry::transform_geometry(geometry_set, transform)) {
84 report_errors(params, *errors);
85 }
86 }
87 else {
88 const float3 translation = params.extract_input<float3>("Translation"_ustr);
89 const math::Quaternion rotation = params.extract_input<math::Quaternion>("Rotation"_ustr);
90 const float3 scale = params.extract_input<float3>("Scale"_ustr);
91
92 /* Use only translation if rotation and scale don't apply. */
93 if (use_translate(rotation, scale)) {
94 geometry::translate_geometry(geometry_set, translation);
95 }
96 else {
97 if (auto errors = geometry::transform_geometry(
98 geometry_set, math::from_loc_rot_scale<float4x4>(translation, rotation, scale)))
99 {
100 report_errors(params, *errors);
101 }
102 }
103 }
104
105 params.set_output("Geometry"_ustr, std::move(geometry_set));
106}
107
108static void register_node()
109{
110 static bke::bNodeType ntype;
111 geo_node_type_base(&ntype, "GeometryNodeTransform"_ustr, GEO_NODE_TRANSFORM_GEOMETRY);
112 ntype.ui_name = "Transform Geometry";
113 ntype.ui_description = "Translate, rotate or scale the geometry";
114 ntype.enum_name_legacy = "TRANSFORM_GEOMETRY";
116 ntype.declare = node_declare;
120}
122
123} // namespace blender::nodes::node_geo_transform_geometry_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define GEO_NODE_TRANSFORM_GEOMETRY
#define TIP_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
void error_message_add(const NodeWarningType type, StringRef message) const
T extract_input(const UString identifier)
T get_input(const UString identifier) const
void set_output(const UString identifier, T &&value)
float3x3 scale(float3x3 mat, float2 scale)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void translate_geometry(bke::GeometrySet &geometry, const float3 translation)
std::optional< TransformGeometryErrors > transform_geometry(bke::GeometrySet &geometry, const float4x4 &transform)
QuaternionBase< float > Quaternion
T length_squared(const VecBase< T, Size > &a)
MatT from_loc_rot_scale(const typename MatT::loc_type &location, const RotationT &rotation, const VecBase< typename MatT::base_type, ScaleDim > &scale)
static bool use_translate(const math::Quaternion &rotation, const float3 scale)
static void node_declare(NodeDeclarationBuilder &b)
static void report_errors(GeoNodeExecParams &params, const geometry::TransformGeometryErrors &errors)
MatBase< float, 4, 4 > float4x4
VecBase< float, 3 > float3
@ GEO_NODE_TRANSFORM_MODE_MATRIX
@ GEO_NODE_TRANSFORM_MODE_COMPONENTS
@ PROP_TRANSLATION
Definition RNA_types.hh:318
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
MINLINE int compare_ff(float a, float b, float max_diff)
static constexpr int _160
Definition BKE_node.hh:246
Defines a node type.
Definition BKE_node.hh:265
std::string ui_description
Definition BKE_node.hh:271
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:385
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393
#define N_(msgid)