JavaCAD
HullUtil.java
Go to the documentation of this file.
1 /*
2  * To change this license header, choose License Headers in Project Properties.
3  * To change this template file, choose Tools | Templates
4  * and open the template in the editor.
5  */
6 package eu.mihosoft.vrl.v3d.ext.quickhull3d;
7 
8 import eu.mihosoft.vrl.v3d.CSG;
9 import eu.mihosoft.vrl.v3d.Vector3d;
10 import eu.mihosoft.vrl.v3d.Polygon;
11 import eu.mihosoft.vrl.v3d.PropertyStorage;
12 import java.util.ArrayList;
13 import java.util.List;
14 
20 public class HullUtil {
21 
25  private HullUtil() {
26  throw new AssertionError("Don't instantiate me!", null);
27  }
28 
36  public static CSG hull(List<?> points) {
37  List<Vector3d> plist = new ArrayList<>();
38  if (Vector3d.class.isInstance(points.get(0))) {
39  points.stream().forEach((pobj) -> plist.add((Vector3d) pobj));
40  return hull(plist, new PropertyStorage());
41  }
42  if (CSG.class.isInstance(points.get(0))) {
43  for (Object csg : points)
44  ((CSG) csg).getPolygons().forEach((p) -> p.vertices.forEach((v) -> plist.add(v.pos)));
45 
46  return hull(plist, new PropertyStorage());
47  }
48  throw new RuntimeException("Objects in list are of unknown type: " + points.get(0).getClass().getName()+"\r\nExpected CSG or Vector3d ");
49  }
50 
60  public static CSG hull(List<Vector3d> points, PropertyStorage storage) {
61 
62  Point3d[] hullPoints = points.stream().map((vec) -> new Point3d(vec.x, vec.y, vec.z)).toArray(Point3d[]::new);
63 
64  QuickHull3D hull = new QuickHull3D();
65  hull.build(hullPoints);
66  hull.triangulate();
67 
68  int[][] faces = hull.getFaces();
69 
70  List<Polygon> polygons = new ArrayList<>();
71 
72  List<Vector3d> vertices = new ArrayList<>();
73 
74  for (int[] verts : faces) {
75 
76  for (int i : verts) {
77  vertices.add(points.get(hull.getVertexPointIndices()[i]));
78  }
79 
80  polygons.add(Polygon.fromPoints(vertices, storage));
81 
82  vertices.clear();
83  }
84 
85  return CSG.fromPolygons(polygons);
86  }
87 
97  public static CSG hull(CSG csg, PropertyStorage storage) {
98 
99  List<Vector3d> points = new ArrayList<>(csg.getPolygons().size() * 3);
100 
101  csg.getPolygons().forEach((p) -> p.vertices.forEach((v) -> points.add(v.pos)));
102 
103  return hull(points, storage);
104  }
105 
113  public static CSG hull(CSG... csgList) {
114 
115  List<Vector3d> points = new ArrayList<>();
116  for (CSG csg : csgList)
117  csg.getPolygons().forEach((p) -> p.vertices.forEach((v) -> points.add(v.pos)));
118 
119  return hull(points, new PropertyStorage());
120  }
121 }
List< Polygon > getPolygons()
Definition: CSG.java:698
static CSG fromPolygons(List< Polygon > polygons)
Definition: CSG.java:621
static Polygon fromPoints(List< Vector3d > points, PropertyStorage shared)
Definition: Polygon.java:386
static CSG hull(CSG csg, PropertyStorage storage)
Definition: HullUtil.java:97
static CSG hull(List< Vector3d > points, PropertyStorage storage)
Definition: HullUtil.java:60