Geometry3Sharp
C# library for 2D/3D geometric computation, mesh algorithms, and so on. Boost license.
Install / Use
/learn @gradientspace/Geometry3SharpREADME
A Short Note about the future of geometry3Sharp (updated Jan 2026)
geometry3Sharp development was on hiatus for many years, however we have started working on major update for modern C#/dotnet (currently targeting .NET SDK 8.0), in the dotnet8 branch. This branch already has some nice feature additions like GLTF and partial USD support, and much more to come. There are also substantial breaking changes, like removing many of the "float" (ie 2f/3f) versions of geometric types and computation classes. See the Changes.md file in the branch for more info. Many of these changes are being made in support of our new Gradientspace Graph project (http://gradientspace.com/gsgraph).
There is a dedicated and actively maintained Unity-only branch developed for the Virgis Project at https://github.com/ViRGIS-Team/ViRGiS-Geometry.
geometry3Sharp
Open-Source (Boost-license) C# library for geometric computing.
geometry3Sharp is compatible with Unity. Set the G3_USING_UNITY Scripting Define and you will have transparent interop between g3 and Unity vector types (see details at the very bottom of this README). Although the library is written for C# 4.5, if you are using the .NET 3.5 Unity runtime, it will still work, just with a few missing features.
Currently there is a small amount of unsafe code, however this code is only used in a few fast-buffer-copy routines, which can be deleted if you need a safe version (eg for Unity web player).
A Nuget Package is available. This package is updated roughly monthly from the github master branch. So, it's "more" stable. Currently this package includes .NET 4.5 and .NET Standard 2.0 dlls. If you would like others, please email and they can be added.
Questions? Contact Ryan Schmidt @rms80 / gradientspace
Projects using g3Sharp
- Gradientspace Cotangent - 3D printing and Mesh Repair/Modeling Tool
- Nia Technologies NiaFit - 3D-printed prosthetic and orthotic design
- OrthoVR Project - 3D-printed lower-leg prosthetic design in VR
- Archform - Clear Dental Aligner design/planning app
- Your Project Here? - we are very excited to hear about your project!
Credits
Many, many data structures and algorithms have been ported from the WildMagic5 and GTEngine C++ libraries, which are developed by David Eberly at Geometric Tools. WildMagic5 and GTEngine are distributed under the Boost license as well, available here. Any errors in code marked as ported from WildMagic5/GTEngine are most certainly ours!
The MeshSignedDistanceGrid class was implemented based on the C++ SDFGen code written by Christopher Batty and Robert Bridson.
Tutorials
Several tutorials for using g3Sharp have been posted on the Gradientspace blog:
- Creating meshes, Mesh File I/O, Ray/Mesh Intersection and Nearest-Point - Explains DMesh3 basics, StandardMeshReader, DMeshAABBTree3 ray and point queries and custom traversals
- Mesh Simplification with Reducer class - Reducer class, DMesh3.CheckValidity, MeshConstraints
- Remeshing and Mesh Constraints - Remesher class, projection targets, MeshConstraints, Unity remeshing animations
- Voxelization/Signed Distance Fields and Marching Cubes Remeshing - MeshSignedDistanceGrid, MarchingCubes, DenseGridTrilinearImplicit, generating 3D lattices
- 3D Bitmaps, Minecraft Cubes, and Mesh Winding Numbers - Bitmap3, VoxelSurfaceGenerator, DMeshAABBTree3 Mesh Winding Number,
- Implicit Surface Modeling - Implicit primitives, voxel/levelset/functional booleans, offsets, and blending, lattice/lightweighting demo
- DMesh3: A Dynamic Indexed Triangle Mesh - deep dive into the DMesh3 class's internal data structures and operations
- Surfacing Point Sets with Fast Winding Numbers - tutorial on the Fast Mesh/PointSet Winding Number, and how to use the g3Sharp implementation
Main Classes
Core
- DVector: indexed list with vector-style interface, but internally stored as separate blocks of memory
- appending is amortized O(1), never a full buffer copy like normal list
- RefCountVector: track index reference counts, maintain list of free indices
- VectorArray2/VectorArray3: wrapper around regular array providing N-element access
- eg operator[] gets/sets Vector3d for VectorArray3d, internally is double[3*count]
- HBitArray: hierarchical BitArray, efficient iteration over large-but-sparse bitsets
- Units: enums, conversions, string representations
- gParallel: multi-threading utilities, including parallel ForEach that works w/ .Net 3.5
- gSerialization: binary serialization of core types (vectors, frames, polygons, DMesh3)
- CommandArgumentSet: string-based argument representation/parsing, useful for command line args, etc
- DynamicPriorityQueue: min-heap priority queue for sparse situations (ie subset of large graph).
- IndexPriorityQueue: min-heap priority queue for dense situations (ie small or large number of items in queue)
- DijkstraGraphDistance: compute shortest-path distances between nodes in graph, from seed points. Graph is defined externally by iterators and Func's, so this class can easily be applied to many situations.
- SmallListSet: efficient allocation of a large number of small lists, with initial fixed-size buffer and "spilling" into linked list.
- BufferUtil: utilities for working with arrays. Math on float/double arrays, automatic conversions, byte[] conversions, compression
- FileSystemUtils: utilities for filesystem stuff
- g3Iterators: IEnumerable utils ConstantItr, RemapItr, IList hacks MappedList, IntSequence
- HashUtil: HashBuilder util for constructing FNV hashes of g3 types
- MemoryPool: basic object pool
- ProfileUtil: code profiling utility LocalProfiler supports multiple timers, accumulating, etc
- SafeCollections: SafeListBuilder multi-threaded List construction and operator-apply
Math
- reasonably complete set of vector-math objects, implemented as structs
- Vector2d/3d/4d/2f/3f, Matrix2d/2f/3f/3d, Quaternionf/d
- Segment2d/3d/2f/3f, Line2d/3d/2f/3f, Ray3d/3f
- Triangle2d/3d/2f/3f, Plane3d/3f
- AxisAlignedBox2d/3d/2f/3f, (oriented) Box2d/3d/2f/3f
- Index2/3/4, int Vector2i/3i, int AxisAlignedBox3i
- 1D intervals Interval1d, and Interval1i which is IEnumerable
- VectorTuple 2/3/4 element 2d/3d vector-tuples (convenient())
- implicit float->double conversion operators between types, explicit double->float operators
- transparent Unity interop (see below)
- Frame3f: position+orientation representation
- accessors for transformed x/y/z axes
- frame transformations
- free and constrained axis alignment
- projection to/from frame for points, directions, other frames,
- minimum-rotation frame-to-frame alignment
- ray-plane intersection
- Frames are awesome and you should use them instead of matrices!!
- MathUtil: constants, IsFinite, EpsilonEqual, Clamp, RangeClamp, SignedClamp, ClampAngle (properly handles negative angles & zero-crossings!), 3-item Min/Max/MinMax, PlaneAngle, MostParallelAxis, Lerp, SmoothInterp, SmoothRise0To1, LinearRampT (with deadzone), Area and Normal of 3D triangle, FastNormal, VectorCot/VectorTan (fast co/tangent between 3D vectors), IsObtuse, IsLeft, SolveQuadratic
- TransformSequence: stack of affine transformations
- IndexUtil: utility functions for working with tuples/lists of indices (cycling, filtering, etc)
- BoundsUtil: construct bboxes from different data sources, containment tests
- QueryTuple2d: robust 2D triangle predicates (ported from GTEngine)
- Integrate1d: Romberg integration, Gaussian quadrature with legendre polynomials, trapezoid rule
- ScalarMap: 1D function reconstruction from sampled data
Approximation
- BiArcFit2: fit 2D bi-arc to pair of points and tangents
- QuadraticFit2: fit general quadratic or 2D circle to set of 2D points
- GaussPointsFit3: fit mean/covariance of gaussian distribution to set of 3D points
- OrthogonalPlaneFit3: fit of plane to 3D point set
Solvers
- basic arbitrary-size DenseMatrix, DenseVector, DiagonalMatrix, SymmetricSparseMatrix (based on Dictionary), PackedSparseMatrix (row arrays)
- CholeskyDecomposition dense-matrix Cholesky decomposition, optionally multi-threaded
- SparseSymmetricCG conjugate-gradient matrix solver w/ support for preconditioning, client-provided matrix/vector multiply
- SparseSymmetricCGMultipleRHS variant that supports multiple right-hand sides
- SingularValueDecomposition SVD for arbitrary matrices
- SymmetricEigenSolver eigensolver for symmetric matrices using Symmetric QR, ported from GTEngine.
Color
- Colorf: float rgba color, with many standard colors p
