Blender V5.3
node_geo_input_mesh_face_is_planar.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 <algorithm>
6
7#include "BLI_math_vector.hh"
8
9#include "DNA_mesh_types.h"
10
11#include "node_geometry_util.hh"
12
14
16{
17 b.add_input<decl::Float>("Threshold"_ustr)
18 .default_value(0.01f)
19 .min(0.0f)
21 .structure_type(StructureType::Field)
22 .description(
23 "The distance a point can be from the surface before the face is no longer "
24 "considered planar");
25 b.add_output<decl::Bool>("Planar"_ustr)
26 .translation_context(BLT_I18NCONTEXT_ID_NODETREE)
27 .structure_type(StructureType::Field);
28}
29
31 private:
32 Field<float> threshold_;
33
34 public:
36 : bke::MeshFieldInput(CPPType::get<bool>(), "Planar"), threshold_(threshold)
37 {
38 }
39
41 const AttrDomain domain,
42 const IndexMask & /*mask*/) const final
43 {
44 const Span<float3> positions = mesh.vert_positions();
45 const OffsetIndices faces = mesh.faces();
46 const Span<int> corner_verts = mesh.corner_verts();
47 const Span<float3> face_normals = mesh.face_normals();
48
49 const bke::MeshFieldContext context{mesh, AttrDomain::Face};
50 fn::FieldEvaluator evaluator{context, faces.size()};
51 evaluator.add(threshold_);
52 evaluator.evaluate();
53 const VArray<float> thresholds = evaluator.get_evaluated<float>(0);
54
55 auto planar_fn =
56 [positions, faces, corner_verts, thresholds, face_normals](const int i) -> bool {
57 const IndexRange face = faces[i];
58 if (face.size() <= 3) {
59 return true;
60 }
61 const float3 &reference_normal = face_normals[i];
62
63 float min = FLT_MAX;
64 float max = -FLT_MAX;
65
66 for (const int vert : corner_verts.slice(face)) {
67 float dot = math::dot(reference_normal, positions[vert]);
68 max = std::max(dot, max);
69 min = std::min(dot, min);
70 }
71 return max - min < thresholds[i] / 2.0f;
72 };
73
74 return mesh.attributes().adapt_domain<bool>(
75 VArray<bool>::from_func(faces.size(), planar_fn), AttrDomain::Face, domain);
76 }
77
78 void foreach_recursive_field(FunctionRef<void(const GField &)> fn) const override
79 {
80 fn(threshold_);
81 }
82
83 void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep &deep_hash_cache) const override
84 {
85 static constexpr int8_t id = 0;
86 hash.add(&id);
87 hash.add(deep_hash_cache.ensure(threshold_));
88 }
89
90 std::optional<AttrDomain> preferred_domain(const Mesh & /*mesh*/) const override
91 {
92 return AttrDomain::Face;
93 }
94
95 bke::NativeFieldDomain native_domain_info(const Mesh & /*mesh*/) const override
96 {
97 return bke::NativeFieldDomain::Domain{AttrDomain::Face};
98 }
99};
100
102{
103 Field<float> threshold = params.extract_input<Field<float>>("Threshold"_ustr);
104 params.set_output("Planar"_ustr, Field<bool>::from_input<PlanarFieldInput>(threshold));
105}
106
107static void node_register()
108{
109 static bke::bNodeType ntype;
110
112 &ntype, "GeometryNodeInputMeshFaceIsPlanar"_ustr, GEO_NODE_INPUT_MESH_FACE_IS_PLANAR);
113 ntype.ui_name = "Is Face Planar";
114 ntype.ui_description =
115 "Retrieve whether all triangles in a face are on the same plane, i.e. whether they have the "
116 "same normal";
117 ntype.enum_name_legacy = "MESH_FACE_IS_PLANAR";
118 ntype.nclass = NODE_CLASS_INPUT;
120 ntype.declare = node_declare;
122}
124
125} // namespace blender::nodes::node_geo_input_mesh_face_is_planar_cc
#define NODE_CLASS_INPUT
Definition BKE_node.hh:478
#define GEO_NODE_INPUT_MESH_FACE_IS_PLANAR
#define final(a, b, c)
Definition BLI_hash_c.hh:21
#define BLT_I18NCONTEXT_ID_NODETREE
#define NOD_REGISTER_NODE(REGISTER_FUNC)
btScalar max
constexpr int64_t size() const
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)
int add(GField field, GVArray *varray_ptr)
const GVArray & get_evaluated(const int field_index) const
uint64_t hash() const
Definition field.cc:185
static Field from_input(Args &&...args)
T extract_input(const UString identifier)
void set_output(const UString identifier, T &&value)
GVArray get_varray_for_context(const Mesh &mesh, const AttrDomain domain, const IndexMask &) const final
void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep &deep_hash_cache) const override
void foreach_recursive_field(FunctionRef< void(const GField &)> fn) const override
#define FLT_MAX
float dot(VecOp< float, D >, VecOp< float, D >) RET
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
VecBase< float, 3 > float3
@ PROP_DISTANCE
Definition RNA_types.hh:313
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
#define min(a, b)
Definition sort.cc:38
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
UniqueHash ensure(const GFieldRef &field)
Definition field.cc:113