geometry3Sharp Documentation

repository·master·Indexed 23 days ago

https://github.com/gradientspace/geometry3sharp

An open-source C# library for high-performance geometric computing compatible with .NET and Unity. It provides tools for mesh manipulation (SimpleMesh, DMesh3), vector/matrix math, spatial queries via DMeshAABBTree3, and geometric solvers. Features include mesh remeshing, simplification using Quadric Error Metric (QEM), hole filling, and a comprehensive suite of 2D and 3D geometric primitives, curves, and generators.

Tokens
3.3K
Snippets
0
Records
19
Agent score
34%

What's inside geometry3Sharp

  1. Overview of geometry3Sharp main classes

    master

    geometry3Sharp is an open-source (Boost-license) C# library for geometric computing. Its core functionality is organized into several specialized modules:

    • Core: High-performance data structures and utilities (e.g., DVector, HBitArray, gParallel for multi-threading, gSerialization).
    • Math: Vector and matrix math, including Vector, Matrix, Quaternion, and Frame3f (position + orientation).
    • Approximation: Fitting algorithms like BiArcFit2, QuadraticFit2, and OrthogonalPlaneFit3.
    • Solvers: Linear algebra solvers including CholeskyDecomposition, SingularValueDecomposition, and SymmetricEigenSolver.
    • Color: Color representations including Colorf (float RGBA), Colorb (byte RGBA), and ColorHSV.
    • Distance Queries: Geometric distance computations for 2D and 3D (e.g., DistPoint3Triangle3, DistLine3Ray3).
    • Intersection Queries: Geometric intersection tests for 2D and 3D (e.g., IntrRay3Triangle3, IntrTriangle3Triangle3).
    • Containment: Algorithms for finding bounding volumes like ContMinCircle2 or ContBox3.
  2. Perform 2D Computational Geometry Tasks

    master

    Use the following classes for 2D geometric computations:

    • ConvexHull2: Computes a 2D convex hull using either doubles or 64-bit integers.
    • Arrangement2d: Computes a 2D line-segment arrangement by finding split points at intersections.
    • GraphSplitter2D: Bisects an existing DGraph with infinite lines (a simpler alternative to Arrangement2d).
    • GraphCells2D: Extracts enclosed regions ("cells") from a DGraph2 as boundary loops.
  3. Use 2D Curves and Planar Geometry

    master

    The library provides a wide range of 2D geometric primitives and complex planar structures:

    2D Curves

    • Circle2d, Arc2d, Ellipse2d, EllipseArc2d, PolyLine2d.
    • Polygon2d: A closed polyline supporting signed area, point-in-polygon tests, intersections, and simplification.
    • NURBSCurve2: Open, closed, and periodic uniform NURBS splines. Supports derivatives up to 3rd order, curvature, and arc-length sampling (uses BSplineBasis internally).
    • IParametricCurve2d: The common interface implemented by all curves and Segment2d.
    • ParametricCurveSequence2: A sequential set of connected parametric curves.
    • CurveSampler2: Handles parameter-space or arc-length sampling for IParametricCurve2d. The AutoSample function handles multi-segment sequential curves and manages sharp knots.
    • PlanarComplex2: An assembly of IParametricCurve2d curves and point-samplings, capable of extracting clean closed loops with interior holes.
    • GeneralPolygon2d: An outer polygon with configurable interior polygonal holes.
    • PlanarSolid2d: A parametric variant of GeneralPolygon2d.

    2D Graphs

    • DGraph2: A dynamic arbitrary-topology 2D graph (nodes and edges), similar to DMesh3 but in 2D.
    • DGraph2Resampler: A remesher for DGraph2.
    • DGraph2Util: Utilities for DGraph2, including ExtractCurves and DisconnectJunctions.
  4. Use 3D Curves and Solids

    master

    3D Curves

    • DCurve3: A 3D polyline.
    • CurveUtil: Provides queries such as Ray/curve intersection (based on thickness) and finding the nearest index.
    • CurveResampler: Performs edge split/collapse resampling of a 3D polyline.
    • DGraph3: A dynamic arbitrary-topology 3D graph (nodes and edges).
    • DGraph3Util: Utilities for DGraph3, including ExtractCurves and DisconnectJunctions.
    • Deformers: Includes InPlaceIterativeCurveSmooth, SculptMoveDeformation, and ArcLengthSoftTranslation for DCurve3 manipulation.

    3D Solids

    • Cylinder3d.
    • DenseGridTrilinearImplicit: A trilinear interpolant of a 3D grid.
    • CachingDenseGridTrilinearImplicit: A variant of DenseGridTrilinearImplicit that uses lazy evaluation of grid values based on an implicit function.
  5. Use Frame3f for position and orientation

    master

    The Frame3f class represents a position and orientation. It is recommended to use Frame3f instead of matrices for representing transformations.

    Capabilities:

    • Accessors for transformed x, y, and z axes.
    • Frame transformations.
    • Free and constrained axis alignment.
    • Projection to/from frames for points and directions.
    • Minimum-rotation frame-to-frame alignment.
    • Ray-plane intersection.
  6. Choose the right mesh class: SimpleMesh vs DMesh3

    master

    geometry3Sharp provides two primary mesh representations depending on your needs:

    • SimpleMesh: A standard indexed mesh class. It uses a dense index space and is backed by DVector buffers. Use this for static geometry where topology doesn't change.
    • DMesh3: A dynamic mesh class designed for active editing. It features a reference-counted sparse index space and includes full edge topology, allowing for neighbor queries. Data is stored in DVector buffers of POD-types (positions are double, while normals, colors, and UVs are float). It supports adding/removing vertices and triangles, manifold-preserving operators (Split, Flip, Collapse, PokeTriangle), and edge-welding via MergeEdges.
  7. Remesh a mesh using Remesher or RemesherPro

    master

    You can perform edge split/flip/collapse and vertex smoothing using the following classes:

    • Remesher: Performs standard remeshing. You can constrain the mesh to an IProjectionTarget (like a surface or curve) and use MeshConstraints to pin vertices or restrict edge operations.
    • RemesherPro: A high-performance extension of Remesher. It includes:
      • FastestRemesh(): Uses an active-set queue for faster convergence.
      • SharpEdgeReprojectionRemesh(): Aligns triangle face normals to a projection target to preserve sharp edges.
      • FastSplitIteration(): Quickly increases vertex resolution by splitting edges.
    • RegionRemesher: Applies remeshing to a specific sub-region of a DMesh3 (via DSubmesh3) while automatically preserving the sub-region's boundary. Use BackPropropagate() to integrate the modified submesh back into the original mesh.
  8. Enable Unity interop for geometry3Sharp

    master

    geometry3Sharp is compatible with Unity. To enable transparent interop between g3 and Unity vector types, set the G3_USING_UNITY Scripting Define in your Unity project.

    While the library is written for C# 4.5, it will still work with the .NET 3.5 Unity runtime, though some features may be missing.

  9. Read and Write Mesh Files

    master

    The library provides several I/O options for mesh data:

    • StandardMeshReader / StandardMeshWriter: Format-agnostic interfaces that allow registering additional format handlers. Use SimpleMeshBuilder or DMesh3Builder to construct meshes via these interfaces. Configuration is handled via ReadOptions and WriteOptions.
    • OBJReader/Writer: Supports vertex colors, face groups, and UVs. Note that it stores texture map paths but does not load images. It cannot produce meshes with multiple UVs per vertex; vertices will be duplicated along UV seams.
    • STLReader/Writer: Supports the STL format with basic vertex welding to reconstruct topology.
    • OFFReader/Writer: Supports the OFF file format.
    • gSerialization: Provides binary Store/Restore functions for many g3 types and data structures.
    • SVGWriter: Writes 2D geometric elements in SVG format.
  10. Fill holes in a mesh

    master

    Depending on your requirements for shape preservation and topology, use one of the following hole-filling algorithms:

    • SimpleHoleFiller: Performs topological filling of an open boundary edge loop. It does not attempt to preserve the original shape.
    • SmoothedHoleFill: Fills holes with approximate boundary tangent continuity.
    • MinimalHoleFill: Constructs a 'minimal' fill that is often developable, which is effective for recovering sharp edges.
    • PlanarHoleFiller: Maps planar holes to 2D to fill them; handles nested holes (e.g., from a plane cut through a torus).
  11. Enable Unity Interop

    master

    To enable transparent conversion between geometry3Sharp types and Unity types, you must define the G3_USING_UNITY symbol in your Unity project.

    Setup Steps:

    1. Open Player Settings in Unity.
    2. Locate the Scripting Define Symbols box.
    3. Add G3_USING_UNITY to the list.

    Usage Details:

    • Supported Types: Vector2, Vector3, Quaternion, Ray, Color, and Bounds (with AxisAlignedBox3f).
    • Type Conversions:
      • float $\leftrightarrow$ double conversions are transparent for Vector3 (e.g., Vector3 $\leftrightarrow$ Vector3f).
      • Converting double to float (e.g., Vector3d $\to$ Vector3) requires an explicit cast.
    • Limitations: Conversions do not work for equations; you must explicitly cast one side to the other when adding a Vector3f to a Vector3.