Blender V5.3
node_geo_input_mesh_edge_angle.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
7#include "BLI_ordered_edge.hh"
8
9#include "BKE_mesh.hh"
10
11#include "node_geometry_util.hh"
12
14
16{
17 b.add_output<decl::Float>("Unsigned Angle"_ustr)
18 .structure_type(StructureType::Field)
20 "The shortest angle in radians between two faces where they meet at an edge. Flat edges "
21 "and Non-manifold edges have an angle of zero. Computing this value is faster than the "
22 "signed angle");
23 b.add_output<decl::Float>("Signed Angle"_ustr)
24 .structure_type(StructureType::Field)
26 "The signed angle in radians between two faces where they meet at an edge. Flat edges "
27 "and Non-manifold edges have an angle of zero. Concave angles are positive and convex "
28 "angles are negative. Computing this value is slower than the unsigned angle");
29}
30
32 const Span<int> corner_edges,
33 const int total_edges)
34{
35 Array<int2> edge_map(total_edges, int2(-1));
36
37 for (const int i_face : faces.index_range()) {
38 for (const int edge : corner_edges.slice(faces[i_face])) {
39 int2 &entry = edge_map[edge];
40 if (entry[0] == -1) {
41 entry[0] = i_face;
42 }
43 else if (entry[1] == -1) {
44 entry[1] = i_face;
45 }
46 else {
47 entry = int2(-2);
48 }
49 }
50 }
51 return edge_map;
52}
53
55 public:
56 AngleFieldInput() : bke::MeshFieldInput(CPPType::get<float>(), "Unsigned Angle Field") {}
57
59 const AttrDomain domain,
60 const IndexMask & /*mask*/) const final
61 {
62 const Span<float3> positions = mesh.vert_positions();
63 const OffsetIndices faces = mesh.faces();
64 const Span<int> corner_verts = mesh.corner_verts();
65 const Span<int> corner_edges = mesh.corner_edges();
66 Array<int2> edge_map = create_edge_map(faces, corner_edges, mesh.edges_num);
67
68 auto angle_fn =
69 [edge_map = std::move(edge_map), positions, faces, corner_verts](const int i) -> float {
70 if (edge_map[i][0] < 0 || edge_map[i][1] < 0) {
71 return 0.0f;
72 }
73 const IndexRange face_1 = faces[edge_map[i][0]];
74 const IndexRange face_2 = faces[edge_map[i][1]];
75 const float3 normal_1 = bke::mesh::face_normal_calc(positions, corner_verts.slice(face_1));
76 const float3 normal_2 = bke::mesh::face_normal_calc(positions, corner_verts.slice(face_2));
77 return angle_normalized_v3v3(normal_1, normal_2);
78 };
79
80 VArray<float> angles = VArray<float>::from_func(mesh.edges_num, angle_fn);
81 return mesh.attributes().adapt_domain<float>(std::move(angles), AttrDomain::Edge, domain);
82 }
83
84 void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep & /*deep_hash_cache*/) const override
85 {
86 static constexpr int8_t id = 0;
87 hash.add(&id);
88 }
89
90 std::optional<AttrDomain> preferred_domain(const Mesh & /*mesh*/) const override
91 {
92 return AttrDomain::Edge;
93 }
94};
95
97 const Span<int> corner_verts,
98 const Span<int3> corner_tris,
99 const int face_index,
100 const int2 edge)
101{
102 const OrderedEdge ordered_edge(edge);
103 for (const int tri_index : bke::mesh::face_triangles_range(faces, face_index)) {
104 const int3 &tri = corner_tris[tri_index];
105 const int3 vert_tri(corner_verts[tri[0]], corner_verts[tri[1]], corner_verts[tri[2]]);
106 if (ordered_edge == OrderedEdge(vert_tri[0], vert_tri[1])) {
107 return vert_tri[2];
108 }
109 if (ordered_edge == OrderedEdge(vert_tri[1], vert_tri[2])) {
110 return vert_tri[0];
111 }
112 if (ordered_edge == OrderedEdge(vert_tri[2], vert_tri[0])) {
113 return vert_tri[1];
114 }
115 }
117 return -1;
118}
119
121 public:
122 SignedAngleFieldInput() : bke::MeshFieldInput(CPPType::get<float>(), "Signed Angle Field") {}
123
125 const AttrDomain domain,
126 const IndexMask & /*mask*/) const final
127 {
128 const Span<float3> positions = mesh.vert_positions();
129 const Span<int2> edges = mesh.edges();
130 const OffsetIndices faces = mesh.faces();
131 const Span<int> corner_verts = mesh.corner_verts();
132 const Span<int> corner_edges = mesh.corner_edges();
133 const Span<int3> corner_tris = mesh.corner_tris();
134 Array<int2> edge_map = create_edge_map(faces, corner_edges, mesh.edges_num);
135
136 auto angle_fn =
137 [edge_map = std::move(edge_map), positions, edges, faces, corner_verts, corner_tris](
138 const int i) -> float {
139 if (edge_map[i][0] < 0 || edge_map[i][1] < 0) {
140 return 0.0f;
141 }
142 const int face_index_1 = edge_map[i][0];
143 const int face_index_2 = edge_map[i][1];
144 const IndexRange face_1 = faces[face_index_1];
145 const IndexRange face_2 = faces[face_index_2];
146
147 /* Find the normals of the 2 faces. */
148 const float3 face_1_normal = bke::mesh::face_normal_calc(positions,
149 corner_verts.slice(face_1));
150 const float3 face_2_normal = bke::mesh::face_normal_calc(positions,
151 corner_verts.slice(face_2));
152
153 /* Find the centerpoint of the axis edge */
154 const float3 edge_centerpoint = math::midpoint(positions[edges[i][0]],
155 positions[edges[i][1]]);
156
157 /* Use the third point of the triangle connected to the edge in face 2 to determine a
158 * reference point for the concavity test. */
159 const int tri_other_vert = find_other_vert_of_edge_triangle(
160 faces, corner_verts, corner_tris, face_index_2, edges[i]);
161 const float3 face_2_tangent = math::normalize(positions[tri_other_vert] - edge_centerpoint);
162 const float concavity = math::dot(face_1_normal, face_2_tangent);
163
164 /* Get the unsigned angle between the two faces */
165 const float angle = angle_normalized_v3v3(face_1_normal, face_2_normal);
166
167 if (angle == 0.0f || angle == 2.0f * M_PI || concavity < 0) {
168 return angle;
169 }
170 return -angle;
171 };
172
173 VArray<float> angles = VArray<float>::from_func(mesh.edges_num, angle_fn);
174 return mesh.attributes().adapt_domain<float>(std::move(angles), AttrDomain::Edge, domain);
175 }
176
177 void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep & /*deep_hash_cache*/) const override
178 {
179 static constexpr int8_t id = 0;
180 hash.add(&id);
181 }
182
183 std::optional<AttrDomain> preferred_domain(const Mesh & /*mesh*/) const override
184 {
185 return AttrDomain::Edge;
186 }
187};
188
190{
191 if (params.output_is_required("Unsigned Angle"_ustr)) {
192 params.set_output("Unsigned Angle"_ustr, Field<float>::from_input<AngleFieldInput>());
193 }
194 if (params.output_is_required("Signed Angle"_ustr)) {
195 params.set_output("Signed Angle"_ustr, Field<float>::from_input<SignedAngleFieldInput>());
196 }
197}
198
199static void node_register()
200{
201 static bke::bNodeType ntype;
203 &ntype, "GeometryNodeInputMeshEdgeAngle"_ustr, GEO_NODE_INPUT_MESH_EDGE_ANGLE);
204 ntype.ui_name = "Edge Angle";
205 ntype.ui_description = "The angle between the normals of connected manifold faces";
206 ntype.enum_name_legacy = "MESH_EDGE_ANGLE";
207 ntype.nclass = NODE_CLASS_INPUT;
208 ntype.declare = node_declare;
211}
213
214} // namespace blender::nodes::node_geo_input_mesh_edge_angle_cc
#define NODE_CLASS_INPUT
Definition BKE_node.hh:478
#define GEO_NODE_INPUT_MESH_EDGE_ANGLE
#define final(a, b, c)
Definition BLI_hash_c.hh:21
#define M_PI
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define BLI_assert_unreachable()
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:141
static VArray from_func(const int64_t size, GetFunc get_func)
uint64_t hash() const
Definition field.cc:185
static Field from_input(Args &&...args)
bool output_is_required(const UString identifier) const
void set_output(const UString identifier, T &&value)
std::optional< AttrDomain > preferred_domain(const Mesh &) const override
GVArray get_varray_for_context(const Mesh &mesh, const AttrDomain domain, const IndexMask &) const final
void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep &) const override
GVArray get_varray_for_context(const Mesh &mesh, const AttrDomain domain, const IndexMask &) const final
void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep &) const override
float3 face_normal_calc(Span< float3 > vert_positions, Span< int > face_verts)
IndexRange face_triangles_range(OffsetIndices< int > faces, int face_i)
Definition BKE_mesh.hh:359
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
T midpoint(const T &a, const T &b)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
static int find_other_vert_of_edge_triangle(const OffsetIndices< int > faces, const Span< int > corner_verts, const Span< int3 > corner_tris, const int face_index, const int2 edge)
static Array< int2 > create_edge_map(const OffsetIndices< int > faces, const Span< int > corner_edges, const int total_edges)
float angle_normalized_v3v3(const float v1[3], const float v2[3]) ATTR_WARN_UNUSED_RESULT
VecBase< int32_t, 2 > int2
VecBase< int32_t, 3 > int3
VecBase< float, 3 > float3
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
signed char int8_t
Definition BKE_text.h:76
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
OrderedEdge(const int v1, const int v2)
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