Apache Commons Geometry 用于几何处理的通用 Java 库

Apache Commons Geometry 是一个用于几何处理的通用 Java 库。该项目的主要目标是提供一组几何类型和实用程序:

  • 在数学上是正确的
  • 数值上是准确的
  • 易于使用
  • 优良性能

该代码起源于 commons-math 项目的 org.apache.commons.math3.geometry 包, 但为了更好的可维护性被拉到一个单独的项目中。从那以后,它经历了许多改进,包括对核心接口和类的重大重构。

该库的主要特点包括:

  • 支持一、二、三维的欧几里得空间
  • 支持一维和二维的球面空间
  • 支持无限大小的几何元素
  • 支持对区域的布尔运算(并集、交集、差集、xor)
  • 支持读写常见的几何数据格式,如 STL 和 OBJ
  • 单一的外部依赖(common-numbers)

下面的代码通过计算一个立方体和一个球体的近似值的差值,并使用 OBJ 数据格式将结果写入文件,给出了一个 API 的小样本。

// construct a precision instance to handle floating-point comparisons
Precision.DoubleEquivalence precision = Precision.doubleEquivalenceOfEpsilon(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
tree.difference(sphere.toTree(3));

// 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
TriangleMesh mesh = tree.toTriangleMesh(precision);

// save as an OBJ file
IO3D.write(mesh, Paths.get(\"target/cube-minus-sphere.obj\"));

© 版权声明
THE END
喜欢就支持一下吧
点赞675 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容