Apache Commons logo Apache Commons Geometry

Apache Commons Geometry

Commons Geometry provides types and utilities for geometric processing. Key features include

  • Support for Euclidean space in 1, 2, and 3 dimensions
  • Support for spherical space in 1 and 2 dimensions
  • Support for geometric elements of infinite size
  • Support for boolean operations on regions (union, intersection, difference, xor)
  • Single external dependency (commons-numbers)

The code below gives a small sample of the API by computing the difference of cube and an approximation of a sphere. See the user guide for more details.

// construct a precision context to handle floating-point comparisons
DoublePrecisionContext precision = new EpsilonDoublePrecisionContext(1e-6);

// create a BSP tree representing the unit cube
RegionBSPTree3D tree = Parallelepiped.unitCube(precision).toTree();

// create a sphere centered on the origin
Sphere sphere = Sphere.from(Vector3D.ZERO, 0.65, precision);

// subtract a BSP tree approximation of the sphere containing 512 facets
// from the cube, modifying the cube tree in place

// compute some properties of the resulting region
double size = tree.getSize(); // 0.11509505362599505
Vector3D centroid = tree.getCentroid(); // (0, 0, 0)

// convert to a triangle mesh for output to other programs
TriangleMesh mesh = tree.toTriangleMesh(precision);

Below is an image of the triangle mesh rendered with Blender. The mesh was written as an OBJ file using the code in commons-geometry-examples-io.

Download Apache Commons Geometry


Download the latest release of Apache Commons Geometry.