34 package eu.mihosoft.vrl.v3d.ext.org.poly2tri;
36 import eu.mihosoft.vrl.v3d.Debug3dProvider;
37 import eu.mihosoft.vrl.v3d.Extrude;
38 import eu.mihosoft.vrl.v3d.Plane;
39 import eu.mihosoft.vrl.v3d.Polygon;
40 import eu.mihosoft.vrl.v3d.Transform;
41 import eu.mihosoft.vrl.v3d.Vector3d;
42 import eu.mihosoft.vrl.v3d.Vertex;
43 import java.util.ArrayList;
44 import java.util.Collections;
45 import java.util.List;
47 import org.locationtech.jts.geom.Coordinate;
48 import org.locationtech.jts.geom.Geometry;
49 import org.locationtech.jts.geom.GeometryFactory;
50 import org.locationtech.jts.triangulate.polygon.ConstrainedDelaunayTriangulator;
64 throw new AssertionError(
"Don't instantiate me!",
null);
104 public static List<eu.mihosoft.vrl.v3d.Polygon>
concaveToConvex(eu.mihosoft.vrl.v3d.Polygon incoming) {
107 public static List<eu.mihosoft.vrl.v3d.Polygon>
concaveToConvex(eu.mihosoft.vrl.v3d.Polygon incoming,
boolean strictTriangulation) {
109 List<Polygon> result =
new ArrayList<>();
111 if (incoming ==
null)
113 if (incoming.vertices.size() < 3)
115 eu.mihosoft.vrl.v3d.Polygon concave= incoming;;
116 Vector3d normalOfPlane = incoming.plane.normal;
119 boolean debug =
false;
122 double degreesToRotate = Math.toDegrees(Math.atan2(normalOfPlane.
x,normalOfPlane.
z));
125 eu.mihosoft.vrl.v3d.Polygon tmp = incoming.transformed(orentation);
128 double degreesToRotate2 =90+Math.toDegrees(Math.atan2(normal.
z,normal.
y));
135 concave = incoming.transformed(orentation2);
136 normal2 = concave.plane.normal;
137 orentationInv = orentation2.
inverse();
138 if(concave.plane.normal.z <0) {
140 concave = incoming.transformed(orentation3);
141 orentationInv = orentation3.
inverse();
164 Coordinate[] coordinates =
new Coordinate[concave.vertices.size()+1];
165 double zplane =concave.vertices.get(0).pos.z;
166 for(
int i=0;i<concave.vertices.size();i++) {
167 Vector3d v = concave.vertices.get(i).pos;
168 coordinates[i]=
new Coordinate(v.
x,v.
y,zplane);
170 Vector3d v = concave.vertices.get(0).pos;
171 coordinates[concave.vertices.size()]=
new Coordinate(v.
x,v.
y,zplane);
176 Geometry geom =
new GeometryFactory().createPolygon(coordinates);
177 triangles= ConstrainedDelaunayTriangulator.triangulate(geom);
179 }
catch(Exception ex) {
180 ex.printStackTrace();
189 ArrayList<Vertex> triPoints =
new ArrayList<>();
191 for (
int i=0;i<triangles.getNumGeometries();i++) {
192 Geometry tri = triangles.getGeometryN(i);
193 Coordinate[] coords = tri.getCoordinates();
196 throw new RuntimeException(
"Failed to triangulate");
197 for (
int j=0;j<3;j++) {
198 Coordinate tp = coords[j];
200 triPoints.add(
new Vertex(pos, normal));
204 Collections.reverse(triPoints);
206 eu.mihosoft.vrl.v3d.Polygon poly =
new eu.mihosoft.vrl.v3d.Polygon(triPoints, concave.getStorage(),
true);
208 poly.plane.normal = concave.plane.normal;
212 Collections.reverse(triPoints);
213 poly =
new eu.mihosoft.vrl.v3d.Polygon(triPoints, concave.getStorage(),
true);
216 System.out.println(
"Error, polygon is reversed!");
226 poly = poly.transform(orentationInv);
228 poly.plane.normal = normalOfPlane;
237 triPoints =
new ArrayList<>();
350 ArrayList<Vertex> newPoints =
new ArrayList<Vertex>();
351 for (
int i = 0; i < incoming.
vertices.size(); i++) {
353 boolean duplicate =
false;
354 for (
Vertex vx : newPoints) {
364 if(newPoints.size()<3)
static void clearScreen()
static void addObject(Object o)
static boolean isCCW(Polygon polygon)
static final double EPSILON
static final double EPSILON_duplicate
final List< Vertex > vertices
static Vector3d y(double y)
static Vector3d z(double z)
static Vector3d x(double x)
static List< eu.mihosoft.vrl.v3d.Polygon > concaveToConvex(eu.mihosoft.vrl.v3d.Polygon incoming)
static Polygon pruneDuplicatePoints(Polygon incoming)
static List< eu.mihosoft.vrl.v3d.Polygon > concaveToConvex(eu.mihosoft.vrl.v3d.Polygon incoming, boolean strictTriangulation)