Blender V4.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 "BLI_math_vector.hh"
6
7#include "BKE_mesh.hh"
8
10
12
14{
15 b.add_input<decl::Float>("Threshold")
16 .default_value(0.01f)
17 .min(0.0f)
19 .supports_field()
20 .description(
21 "The distance a point can be from the surface before the face is no longer "
22 "considered planar");
23 b.add_output<decl::Bool>("Planar")
24 .translation_context(BLT_I18NCONTEXT_ID_NODETREE)
25 .field_source();
26}
27
29 private:
30 Field<float> threshold_;
31
32 public:
34 : bke::MeshFieldInput(CPPType::get<bool>(), "Planar"), threshold_(threshold)
35 {
37 }
38
40 const AttrDomain domain,
41 const IndexMask & /*mask*/) const final
42 {
43 const Span<float3> positions = mesh.vert_positions();
44 const OffsetIndices faces = mesh.faces();
45 const Span<int> corner_verts = mesh.corner_verts();
46 const Span<float3> face_normals = mesh.face_normals();
47
48 const bke::MeshFieldContext context{mesh, AttrDomain::Face};
49 fn::FieldEvaluator evaluator{context, faces.size()};
50 evaluator.add(threshold_);
51 evaluator.evaluate();
52 const VArray<float> thresholds = evaluator.get_evaluated<float>(0);
53
54 auto planar_fn =
55 [positions, faces, corner_verts, thresholds, face_normals](const int i) -> bool {
56 const IndexRange face = faces[i];
57 if (face.size() <= 3) {
58 return true;
59 }
60 const float3 &reference_normal = face_normals[i];
61
62 float min = FLT_MAX;
63 float max = -FLT_MAX;
64
65 for (const int vert : corner_verts.slice(face)) {
66 float dot = math::dot(reference_normal, positions[vert]);
67 if (dot > max) {
68 max = dot;
69 }
70 if (dot < min) {
71 min = dot;
72 }
73 }
74 return max - min < thresholds[i] / 2.0f;
75 };
76
77 return mesh.attributes().adapt_domain<bool>(
78 VArray<bool>::ForFunc(faces.size(), planar_fn), AttrDomain::Face, domain);
79 }
80
81 void for_each_field_input_recursive(FunctionRef<void(const FieldInput &)> fn) const override
82 {
83 threshold_.node().for_each_field_input_recursive(fn);
84 }
85
86 uint64_t hash() const override
87 {
88 /* Some random constant hash. */
89 return 2356235652;
90 }
91
92 bool is_equal_to(const fn::FieldNode &other) const override
93 {
94 return dynamic_cast<const PlanarFieldInput *>(&other) != nullptr;
95 }
96
97 std::optional<AttrDomain> preferred_domain(const Mesh & /*mesh*/) const override
98 {
99 return AttrDomain::Face;
100 }
101};
102
104{
105 Field<float> threshold = params.extract_input<Field<float>>("Threshold");
106 Field<bool> planar_field{std::make_shared<PlanarFieldInput>(threshold)};
107 params.set_output("Planar", std::move(planar_field));
108}
109
110static void node_register()
111{
112 static blender::bke::bNodeType ntype;
113
115 &ntype, GEO_NODE_INPUT_MESH_FACE_IS_PLANAR, "Is Face Planar", NODE_CLASS_INPUT);
117 ntype.declare = node_declare;
119}
121
122} // namespace blender::nodes::node_geo_input_mesh_face_is_planar_cc
#define NODE_CLASS_INPUT
Definition BKE_node.hh:404
#define BLT_I18NCONTEXT_ID_NODETREE
#define NOD_REGISTER_NODE(REGISTER_FUNC)
@ PROP_DISTANCE
Definition RNA_types.hh:159
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:138
static VArray ForFunc(const int64_t size, GetFunc get_func)
int add(GField field, GVArray *varray_ptr)
Definition field.cc:756
virtual void for_each_field_input_recursive(FunctionRef< void(const FieldInput &)> fn) const
Definition field.cc:587
const FieldNode & node() const
Definition FN_field.hh:137
GVArray get_varray_for_context(const Mesh &mesh, const AttrDomain domain, const IndexMask &) const final
void for_each_field_input_recursive(FunctionRef< void(const FieldInput &)> fn) const override
local_group_size(16, 16) .push_constant(Type b
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
static char faces[256]
void node_register_type(bNodeType *ntype)
Definition node.cc:1708
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
void geo_node_type_base(blender::bke::bNodeType *ntype, int type, const char *name, short nclass)
#define min(a, b)
Definition sort.c:32
#define FLT_MAX
Definition stdcycles.h:14
unsigned __int64 uint64_t
Definition stdint.h:90
Defines a node type.
Definition BKE_node.hh:218
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:339
NodeDeclareFunction declare
Definition BKE_node.hh:347