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 intersection of cube with a rotated version of itself. See the user guide for more details.

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

// create a binary space partitioning tree representing the unit cube
// centered on the origin
RegionBSPTree3D region = Parallelepiped.builder(precision)
        .setPosition(Vector3D.ZERO)
        .build()
        .toTree();

// create a rotated copy of the region
RegionBSPTree3D copy = region.copy();
copy.transform(QuaternionRotation.fromAxisAngle(Vector3D.Unit.PLUS_Z, 0.25 * Math.PI));

// compute the intersection of the regions, storing the result back into the caller
// (the result could also have been placed into a third region)
region.intersection(copy);

// compute some properties of the intersection region
double size = region.getSize(); // 0.8284271247461903
Vector3D center = region.getBarycenter(); // (0, 0, 0)

Download Apache Commons Geometry

Releases

Download the latest release of Apache Commons Geometry.