in commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/hull/ConvexHull3D.java [328:381]
private Simplex createSimplex(Collection<Vector3D> points) {
// First vertex of the simplex
Vector3D vertex1 = points.stream().min(Vector3D.COORDINATE_ASCENDING_ORDER).get();
// Find a point with maximal distance to the second.
Vector3D vertex2 = points.stream().max(Comparator.comparingDouble(vertex1::distance)).get();
// The point is degenerate if all points are equivalent.
if (vertex1.eq(vertex2, precision)) {
return new Simplex(Collections.emptyList());
}
// First and second vertex form a line.
Line3D line = Lines3D.fromPoints(vertex1, vertex2, precision);
// Find a point with maximal distance from the line.
Vector3D vertex3 = points.stream().max(Comparator.comparingDouble(line::distance)).get();
// The point set is degenerate because all points are collinear.
if (line.contains(vertex3)) {
return new Simplex(Collections.emptyList());
}
// Form a triangle with the first three vertices.
ConvexPolygon3D facet1 = Planes.triangleFromVertices(vertex1, vertex2, vertex3, precision);
// Find a point with maximal distance to the plane formed by the triangle.
Plane plane = facet1.getPlane();
Vector3D vertex4 = points.stream().max(Comparator.comparingDouble(d -> Math.abs(plane.offset(d)))).get();
// The point set is degenerate, because all points are coplanar.
if (plane.contains(vertex4)) {
return new Simplex(Collections.emptyList());
}
// Construct the other three facets.
ConvexPolygon3D facet2 = Planes.convexPolygonFromVertices(Arrays.asList(vertex1, vertex2, vertex4),
precision);
ConvexPolygon3D facet3 = Planes.convexPolygonFromVertices(Arrays.asList(vertex1, vertex3, vertex4),
precision);
ConvexPolygon3D facet4 = Planes.convexPolygonFromVertices(Arrays.asList(vertex2, vertex3, vertex4),
precision);
List<Facet> facets = new ArrayList<>();
// Choose the right orientation for all facets.
facets.add(new Facet(facet1, vertex4, precision));
facets.add(new Facet(facet2, vertex3, precision));
facets.add(new Facet(facet3, vertex2, precision));
facets.add(new Facet(facet4, vertex1, precision));
return new Simplex(facets);
}