Back
Geometry Functions
Overview
The geometry module provides functions for computing geometric properties of meshes and point sets, including areas, normals, lengths, curvatures, distance fields, and sphere generation. These functions work with the library's vector and mesh types.
Types
Geometry Types
// Fixed-size vector (alias for std::array)
template <size_t N>
using Vector = std::array<double, N>;
// Common position types
using Positions2 = Serie<Vector<2>>; // 2D point cloud
using Positions3 = Serie<Vector<3>>; // 3D point cloud
// Connectivity types
using Segments = Serie<std::array<uint32_t, 2>>; // Line segments
using Triangles = Serie<std::array<uint32_t, 3>>; // Triangle faces
// Concept for vector types
template <typename T>
concept VectorType = requires { T::size(); };
Area
Triangle Area
// Compute the area of each triangle
Serie<double> area(const Positions3 &vertices, const Triangles &triangles);
Area Example
Positions3 vertices{
{0.0, 0.0, 0.0},
{1.0, 0.0, 0.0},
{0.0, 1.0, 0.0},
{1.0, 1.0, 0.0}
};
Triangles tris{
{0, 1, 2},
{1, 3, 2}
};
auto areas = df::geo::area(vertices, tris);
// areas = {0.5, 0.5}
Normals
Normal Computation
// Compute normals of 2D line segments (returns outward-pointing 2D normals)
Serie<Vector<2>> normals(const Positions2 &vertices, const Segments &segments);
// Compute normals of 3D triangles
Serie<Vector<3>> normals(const Positions3 &vertices, const Triangles &triangles);
Normals Example
// 3D triangle normals
Positions3 vertices{
{0, 0, 0}, {1, 0, 0}, {0, 1, 0}
};
Triangles tris{{0, 1, 2}};
auto norms = df::geo::normals(vertices, tris);
// norms[0] = {0, 0, 1} (unit normal pointing in +z)
// 2D segment normals
Positions2 pts2d{{0, 0}, {1, 0}, {1, 1}};
Segments segs{{0, 1}, {1, 2}};
auto norms2d = df::geo::normals(pts2d, segs);
// norms2d[0] = {0, -1} or {0, 1} depending on orientation
Length
Segment Length
// Compute the length of each segment
template <size_t N>
Serie<double> length(const Serie<Vector<N>> &vertices, const Segments &segments);
Length Example
Positions3 pts{{0,0,0}, {3,0,0}, {3,4,0}};
Segments segs{{0, 1}, {1, 2}};
auto lens = df::geo::length(pts, segs);
// lens = {3.0, 4.0}
Surface Curvature
Surface Curvature
// Compute surface curvature at each vertex
// Returns a DataFrame with columns:
// "mean" - Serie<double> mean curvature
// "gaussian" - Serie<double> Gaussian curvature
// "k1" - Serie<double> first principal curvature
// "k2" - Serie<double> second principal curvature
Dataframe surface_curvature(const Positions3 &vertices, const Triangles &triangles);
Surface Curvature Example
// Compute curvatures on a triangulated surface
auto curvatures = df::geo::surface_curvature(vertices, triangles);
auto mean_curv = curvatures.get<double>("mean");
auto gaussian_curv = curvatures.get<double>("gaussian");
auto k1 = curvatures.get<double>("k1");
auto k2 = curvatures.get<double>("k2");
Distance Field
Distance Field (KDTree-based)
// Compute the distance from each target point to the nearest source point
// Uses a KDTree for efficient nearest-neighbor lookups
template <size_t N>
Serie<double> distance_field(
const Serie<Vector<N>> &source_points,
const Serie<Vector<N>> &target_points
);
Distance Field Example
// Fault trace points
Positions3 fault_points{
{0, 0, 0}, {1, 0, 0}, {2, 0, 0}
};
// Grid points where we want the distance
Positions3 grid_points{
{0, 1, 0}, {1, 1, 0}, {2, 1, 0},
{0, 2, 0}, {1, 2, 0}, {2, 2, 0}
};
auto dists = df::geo::distance_field(fault_points, grid_points);
// dists[0] = 1.0, dists[3] = 2.0, etc.
Sphere Generation
Generate Sphere
// Generate an icosphere with the given number of subdivisions
// Returns a pair of {positions, triangles}
std::pair<Positions3, Triangles> generateSphere(
size_t subdivisions = 3,
double radius = 1.0
);
Sphere Example
// Generate a unit sphere with 3 levels of subdivision
auto [positions, triangles] = df::geo::generateSphere(3, 1.0);
std::cout << "Vertices: " << positions.size() << std::endl;
std::cout << "Triangles: " << triangles.size() << std::endl;
// Compute normals on the sphere
auto sphere_normals = df::geo::normals(positions, triangles);