Geometry Nodes: Face is Planar Node

This adds a node with a boolean field output which returns true if all of the
points of the evaluated face are on the same plane. A float field input allows
for the threshold of the face/point comparison to be adjusted on a per face basis.

One clear use case is to only triangulate faces that are not planar.

Differential Revision: https://developer.blender.org/D13906
This commit is contained in:
Johnny Matthews 2022-02-23 14:19:08 -06:00
parent 86fc63c4ca
commit 756f7fb23e
7 changed files with 122 additions and 0 deletions

View File

@ -142,6 +142,7 @@ def mesh_node_items(context):
yield NodeItem("GeometryNodeInputMeshEdgeVertices")
yield NodeItem("GeometryNodeInputMeshFaceArea")
yield NodeItem("GeometryNodeInputMeshFaceNeighbors")
yield NodeItem("GeometryNodeInputMeshFaceIsPlanar")
yield NodeItem("GeometryNodeInputMeshIsland")
yield NodeItem("GeometryNodeInputShadeSmooth")
yield NodeItem("GeometryNodeInputMeshVertexNeighbors")

View File

@ -1515,6 +1515,7 @@ struct TexResult;
#define GEO_NODE_EXTRUDE_MESH 1152
#define GEO_NODE_MERGE_BY_DISTANCE 1153
#define GEO_NODE_DUPLICATE_ELEMENTS 1154
#define GEO_NODE_INPUT_MESH_FACE_IS_PLANAR 1155
/** \} */

View File

@ -4770,6 +4770,7 @@ static void registerGeometryNodes()
register_node_type_geo_input_mesh_edge_neighbors();
register_node_type_geo_input_mesh_edge_vertices();
register_node_type_geo_input_mesh_face_area();
register_node_type_geo_input_mesh_face_is_planar();
register_node_type_geo_input_mesh_face_neighbors();
register_node_type_geo_input_mesh_island();
register_node_type_geo_input_mesh_vertex_neighbors();

View File

@ -101,6 +101,7 @@ void register_node_type_geo_input_mesh_edge_angle(void);
void register_node_type_geo_input_mesh_edge_neighbors(void);
void register_node_type_geo_input_mesh_edge_vertices(void);
void register_node_type_geo_input_mesh_face_area(void);
void register_node_type_geo_input_mesh_face_is_planar(void);
void register_node_type_geo_input_mesh_face_neighbors(void);
void register_node_type_geo_input_mesh_island(void);
void register_node_type_geo_input_mesh_vertex_neighbors(void);

View File

@ -359,6 +359,7 @@ DefNode(GeometryNode, GEO_NODE_INPUT_MESH_EDGE_ANGLE, 0, "MESH_EDGE_ANGLE", Inpu
DefNode(GeometryNode, GEO_NODE_INPUT_MESH_EDGE_NEIGHBORS, 0, "MESH_EDGE_NEIGHBORS", InputMeshEdgeNeighbors, "Edge Neighbors", "")
DefNode(GeometryNode, GEO_NODE_INPUT_MESH_EDGE_VERTICES, 0, "MESH_EDGE_VERTICES", InputMeshEdgeVertices, "Edge Vertices", "")
DefNode(GeometryNode, GEO_NODE_INPUT_MESH_FACE_AREA, 0, "MESH_FACE_AREA", InputMeshFaceArea, "Face Area", "")
DefNode(GeometryNode, GEO_NODE_INPUT_MESH_FACE_IS_PLANAR, 0, "MESH_FACE_IS_PLANAR", InputMeshFaceIsPlanar, "Face is Planar", "")
DefNode(GeometryNode, GEO_NODE_INPUT_MESH_FACE_NEIGHBORS, 0, "MESH_FACE_NEIGHBORS", InputMeshFaceNeighbors, "Face Neighbors", "")
DefNode(GeometryNode, GEO_NODE_INPUT_MESH_ISLAND, 0, "MESH_ISLAND", InputMeshIsland, "Mesh Island", "")
DefNode(GeometryNode, GEO_NODE_INPUT_MESH_VERTEX_NEIGHBORS, 0, "MESH_VERTEX_NEIGHBORS", InputMeshVertexNeighbors, "Vertex Neighbors", "")

View File

@ -117,6 +117,7 @@ set(SRC
nodes/node_geo_input_mesh_edge_neighbors.cc
nodes/node_geo_input_mesh_edge_vertices.cc
nodes/node_geo_input_mesh_face_area.cc
nodes/node_geo_input_mesh_face_is_planar.cc
nodes/node_geo_input_mesh_face_neighbors.cc
nodes/node_geo_input_mesh_island.cc
nodes/node_geo_input_mesh_vertex_neighbors.cc

View File

@ -0,0 +1,116 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
#include "node_geometry_util.hh"
namespace blender::nodes::node_geo_input_mesh_face_is_planar_cc {
static void node_declare(NodeDeclarationBuilder &b)
{
b.add_input<decl::Float>("Threshold")
.field_source()
.default_value(0.01f)
.subtype(PROP_DISTANCE)
.supports_field()
.description(N_("The distance a point can be from the surface before the face is no longer "
"considered planar"))
.min(0.0f);
b.add_output<decl::Bool>("Planar").field_source();
}
class PlanarFieldInput final : public GeometryFieldInput {
private:
Field<float> threshold_;
public:
PlanarFieldInput(Field<float> threshold)
: GeometryFieldInput(CPPType::get<bool>(), "Planar"), threshold_(threshold)
{
category_ = Category::Generated;
}
GVArray get_varray_for_context(const GeometryComponent &component,
const AttributeDomain domain,
[[maybe_unused]] IndexMask mask) const final
{
if (component.type() != GEO_COMPONENT_TYPE_MESH) {
return {};
}
const MeshComponent &mesh_component = static_cast<const MeshComponent &>(component);
const Mesh *mesh = mesh_component.get_for_read();
if (mesh == nullptr) {
return {};
}
GeometryComponentFieldContext context{mesh_component, ATTR_DOMAIN_FACE};
fn::FieldEvaluator evaluator{context, mesh->totpoly};
evaluator.add(threshold_);
evaluator.evaluate();
const VArray<float> &thresholds = evaluator.get_evaluated<float>(0);
Span<float3> poly_normals{(float3 *)BKE_mesh_poly_normals_ensure(mesh), mesh->totpoly};
auto planar_fn = [mesh, thresholds, poly_normals](const int i_poly) -> bool {
if (mesh->mpoly[i_poly].totloop <= 3) {
return true;
}
const int loopstart = mesh->mpoly[i_poly].loopstart;
const int loops = mesh->mpoly[i_poly].totloop;
Span<MLoop> poly_loops(&mesh->mloop[loopstart], loops);
float3 reference_normal = poly_normals[i_poly];
float min = FLT_MAX;
float max = FLT_MIN;
for (const int i_loop : poly_loops.index_range()) {
const float3 vert = mesh->mvert[poly_loops[i_loop].v].co;
float dot = math::dot(reference_normal, vert);
if (dot > max) {
max = dot;
}
if (dot < min) {
min = dot;
}
}
return max - min < thresholds[i_poly] / 2.0f;
};
return component.attribute_try_adapt_domain<bool>(
VArray<bool>::ForFunc(mesh->totpoly, planar_fn), ATTR_DOMAIN_FACE, domain);
}
uint64_t hash() const override
{
/* Some random constant hash. */
return 2356235652;
}
bool is_equal_to(const fn::FieldNode &other) const override
{
return dynamic_cast<const PlanarFieldInput *>(&other) != nullptr;
}
};
static void geo_node_exec(GeoNodeExecParams params)
{
Field<float> threshold = params.extract_input<Field<float>>("Threshold");
Field<bool> planar_field{std::make_shared<PlanarFieldInput>(threshold)};
params.set_output("Planar", std::move(planar_field));
}
} // namespace blender::nodes::node_geo_input_mesh_face_is_planar_cc
void register_node_type_geo_input_mesh_face_is_planar()
{
namespace file_ns = blender::nodes::node_geo_input_mesh_face_is_planar_cc;
static bNodeType ntype;
geo_node_type_base(
&ntype, GEO_NODE_INPUT_MESH_FACE_IS_PLANAR, "Face is Planar", NODE_CLASS_INPUT);
ntype.geometry_node_execute = file_ns::geo_node_exec;
ntype.declare = file_ns::node_declare;
nodeRegisterType(&ntype);
}