diff --git a/CityDoctorParent/CityDoctorModel/pom.xml b/CityDoctorParent/CityDoctorModel/pom.xml index ae6596624a1ea2d200a04a17e1621f461e0e6c79..e4ca0142e6daaa4afb434b88c33a50a563299546 100644 --- a/CityDoctorParent/CityDoctorModel/pom.xml +++ b/CityDoctorParent/CityDoctorModel/pom.xml @@ -30,14 +30,6 @@ gov.nist.math jama - - org.jogamp.gluegen - gluegen-rt-main - - - org.jogamp.jogl - jogl-all-main - junit junit diff --git a/CityDoctorParent/CityDoctorModel/src/main/java/de/hft/stuttgart/citydoctor2/datastructure/ConcretePolygon.java b/CityDoctorParent/CityDoctorModel/src/main/java/de/hft/stuttgart/citydoctor2/datastructure/ConcretePolygon.java index a654fd4cce5ea15e2d6f1ab1b1b4f13d26b750b0..28659da374dcbd3d133bb0611c59d34e1f7ccf63 100644 --- a/CityDoctorParent/CityDoctorModel/src/main/java/de/hft/stuttgart/citydoctor2/datastructure/ConcretePolygon.java +++ b/CityDoctorParent/CityDoctorModel/src/main/java/de/hft/stuttgart/citydoctor2/datastructure/ConcretePolygon.java @@ -18,21 +18,21 @@ */ package de.hft.stuttgart.citydoctor2.datastructure; +import java.io.Serial; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + import de.hft.stuttgart.citydoctor2.check.Check; import de.hft.stuttgart.citydoctor2.check.CheckError; import de.hft.stuttgart.citydoctor2.check.CheckId; import de.hft.stuttgart.citydoctor2.check.Checkable; import de.hft.stuttgart.citydoctor2.math.Vector3d; -import de.hft.stuttgart.citydoctor2.tesselation.JoglTesselator; +import de.hft.stuttgart.citydoctor2.tesselation.EarcutTesselator; import de.hft.stuttgart.citydoctor2.tesselation.TesselatedPolygon; import de.hft.stuttgart.citydoctor2.utils.CopyHandler; import de.hft.stuttgart.citydoctor2.utils.Copyable; -import java.io.Serial; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - /** * A polygon that actually contains the rings and points representing the * geometry @@ -77,7 +77,7 @@ public class ConcretePolygon extends Polygon { */ @Override public TesselatedPolygon tesselate() { - return JoglTesselator.tesselatePolygon(this); + return EarcutTesselator.tesselatePolygon(this); } /* diff --git a/CityDoctorParent/CityDoctorModel/src/main/java/de/hft/stuttgart/citydoctor2/math/Ray.java b/CityDoctorParent/CityDoctorModel/src/main/java/de/hft/stuttgart/citydoctor2/math/Ray.java index fb1f5d9cd0722d0ba3d685ca5b1916cc07ffc37e..0238f44e405fc669f318b7c0522f45fd29d3e899 100644 --- a/CityDoctorParent/CityDoctorModel/src/main/java/de/hft/stuttgart/citydoctor2/math/Ray.java +++ b/CityDoctorParent/CityDoctorModel/src/main/java/de/hft/stuttgart/citydoctor2/math/Ray.java @@ -18,9 +18,7 @@ */ package de.hft.stuttgart.citydoctor2.math; -import de.hft.stuttgart.citydoctor2.datastructure.Polygon; import de.hft.stuttgart.citydoctor2.tesselation.TesselatedPolygon; -import de.hft.stuttgart.citydoctor2.tesselation.JoglTesselator; /** * A ray constructed by a point and a direction. A ray has a starting point and @@ -97,8 +95,4 @@ public class Ray { } } - public Vector3d intersectsPolygon(Polygon p) { - return intersectsPolygon(JoglTesselator.tesselatePolygon(p)); - } - } diff --git a/CityDoctorParent/CityDoctorModel/src/main/java/de/hft/stuttgart/citydoctor2/tesselation/JoglTesselator.java b/CityDoctorParent/CityDoctorModel/src/main/java/de/hft/stuttgart/citydoctor2/tesselation/JoglTesselator.java deleted file mode 100644 index a98c330b5cb68851bd5ca60506e63ba086fdcf50..0000000000000000000000000000000000000000 --- a/CityDoctorParent/CityDoctorModel/src/main/java/de/hft/stuttgart/citydoctor2/tesselation/JoglTesselator.java +++ /dev/null @@ -1,169 +0,0 @@ -/*- - * Copyright 2020 Beuth Hochschule für Technik Berlin, Hochschule für Technik Stuttgart - * - * This file is part of CityDoctor2. - * - * CityDoctor2 is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * CityDoctor2 is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with CityDoctor2. If not, see . - */ -package de.hft.stuttgart.citydoctor2.tesselation; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import com.jogamp.opengl.GL; -import com.jogamp.opengl.glu.GLU; -import com.jogamp.opengl.glu.GLUtessellator; -import com.jogamp.opengl.glu.GLUtessellatorCallbackAdapter; - -import de.hft.stuttgart.citydoctor2.datastructure.LinearRing; -import de.hft.stuttgart.citydoctor2.datastructure.Polygon; -import de.hft.stuttgart.citydoctor2.datastructure.Vertex; -import de.hft.stuttgart.citydoctor2.math.Vector3d; - -/** - * Tesselator to create triangles out of polygons. Uses JOGL implementation. - * - * @author Matthias Betz - * - */ -public class JoglTesselator { - - private static final String DATA_WAS_NOT_A_LIST = "data was not a list"; - private static final GLU glu = new GLU(); - private static final GLUtessellator tess = GLU.gluNewTess(); - private static final GLUtessellatorCallbackAdapter callback; - private static final ArrayList primitives = new ArrayList<>(); - - static { - callback = new GLUtessellatorCallbackAdapter() { - - private Primitive lastPrimitive; - - @Override - public void beginData(int type, Object data) { - if (!(data instanceof ArrayList)) { - throw new IllegalStateException(DATA_WAS_NOT_A_LIST); - } - @SuppressWarnings("unchecked") - ArrayList vertices = (ArrayList) data; - lastPrimitive = new Primitive(type, vertices); - primitives.add(lastPrimitive); - } - - @Override - public void vertexData(Object vertex, Object data) { - if (!(data instanceof ArrayList)) { - throw new IllegalStateException(DATA_WAS_NOT_A_LIST); - } - if (!(vertex instanceof Integer)) { - throw new IllegalStateException("vertex has to be an Integer"); - } - lastPrimitive.addIndex((Integer) vertex); - } - - @Override - public void combineData(double[] coords, Object[] vertexIndices, float[] weights, Object[] outgoingData, - Object data) { - if (!(data instanceof ArrayList)) { - throw new IllegalStateException(DATA_WAS_NOT_A_LIST); - } - @SuppressWarnings("unchecked") - ArrayList vertices = (ArrayList) data; - // to ensure the coordinates are valid - double[] myCoords = Arrays.copyOf(coords, 3); - Vector3d newVertex = new Vector3d(myCoords); - outgoingData[0] = vertices.size(); - vertices.add(newVertex); - } - - @Override - public void errorData(int errnum, Object data) { - throw new TesselationException("GLU Error " + glu.gluErrorString(errnum)); - } - - }; - GLU.gluTessCallback(tess, GLU.GLU_TESS_BEGIN_DATA, callback); - GLU.gluTessCallback(tess, GLU.GLU_TESS_VERTEX_DATA, callback); - GLU.gluTessCallback(tess, GLU.GLU_TESS_ERROR_DATA, callback); - GLU.gluTessCallback(tess, GLU.GLU_TESS_COMBINE_DATA, callback); - GLU.gluTessCallback(tess, GLU.GLU_TESS_END_DATA, callback); - } - - public static TesselatedPolygon tesselatePolygon(Polygon p) { - return tesselatePolygon(p, true); - } - - public static TesselatedPolygon tesselatePolygon(Polygon p, boolean discardDegeneratedTriangles) { - ArrayList vertices = new ArrayList<>(); - Vector3d normal = p.calculateNormalNormalized(); - synchronized (tess) { - GLU.gluTessProperty(tess, GLU.GLU_TESS_BOUNDARY_ONLY, GL.GL_FALSE); - GLU.gluTessBeginPolygon(tess, vertices); - GLU.gluTessNormal(tess, normal.getX(), normal.getY(), normal.getZ()); - - // exterior ring - LinearRing ext = p.getExteriorRing(); - createContour(vertices, ext); - - // interior rings - for (LinearRing lr : p.getInnerRings()) { - createContour(vertices, lr); - } - - GLU.gluTessEndPolygon(tess); - ArrayList indices = new ArrayList<>(); - for (Primitive primitive : primitives) { - primitive.fillListWithIndices(indices, discardDegeneratedTriangles); - } - primitives.clear(); - return new TesselatedPolygon(vertices, indices, p); - } - } - - private static void createContour(List vertices, LinearRing lr) { - GLU.gluTessBeginContour(tess); - for (Vertex v : lr.getVertices()) { - vertices.add(v); - GLU.gluTessVertex(tess, v.getCoordinates(), 0, vertices.size() - 1); - } - GLU.gluTessEndContour(tess); - } - - public static TesselatedRing tesselateRing(LinearRing r, boolean discardDegeneratedTriangles) { - ArrayList vertices = new ArrayList<>(); - Vector3d normal = r.calculateNormal(); - synchronized (tess) { - GLU.gluTessBeginPolygon(tess, vertices); - GLU.gluTessNormal(tess, normal.getX(), normal.getY(), normal.getZ()); - createContour(vertices, r); - GLU.gluTessEndPolygon(tess); - ArrayList indices = new ArrayList<>(); - for (Primitive primitive : primitives) { - primitive.fillListWithIndices(indices, discardDegeneratedTriangles); - } - primitives.clear(); - return new TesselatedRing(vertices, indices, r); - } - } - - public static TesselatedRing tesselateRing(LinearRing r) { - return tesselateRing(r, true); - } - - private JoglTesselator() { - // only static useage - } - -} diff --git a/CityDoctorParent/CityDoctorModel/src/main/java/de/hft/stuttgart/citydoctor2/tesselation/Primitive.java b/CityDoctorParent/CityDoctorModel/src/main/java/de/hft/stuttgart/citydoctor2/tesselation/Primitive.java deleted file mode 100644 index 4e65d89076bb0cd6da5ca7b52326e00d57b1436a..0000000000000000000000000000000000000000 --- a/CityDoctorParent/CityDoctorModel/src/main/java/de/hft/stuttgart/citydoctor2/tesselation/Primitive.java +++ /dev/null @@ -1,118 +0,0 @@ -/*- - * Copyright 2020 Beuth Hochschule für Technik Berlin, Hochschule für Technik Stuttgart - * - * This file is part of CityDoctor2. - * - * CityDoctor2 is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * CityDoctor2 is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with CityDoctor2. If not, see . - */ -package de.hft.stuttgart.citydoctor2.tesselation; - -import java.util.ArrayList; -import java.util.List; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import com.jogamp.opengl.GL; - -import de.hft.stuttgart.citydoctor2.math.Vector3d; - -/** - * Result of the tesselation process. Primitives can be converted to triangles. - * - * @author Matthias Betz - * - */ -public class Primitive { - - private static final Logger logger = LogManager.getLogger(Primitive.class); - - private static final double AREA_EPSILON = 0.00001; - - private final int type; - private final List pIndices = new ArrayList<>(); - private final List vertices; - - public Primitive(int type, List vertices) { - this.vertices = vertices; - this.type = type; - } - - public void fillListWithIndices(List indices, boolean discardDegeneratedTriangles) { - switch (type) { - case GL.GL_TRIANGLES: - for (int i = 0; i < pIndices.size(); i += 3) { - addToListIfAreaNonNull(pIndices.get(i), pIndices.get(i + 1), pIndices.get(i + 2), indices, - discardDegeneratedTriangles); - } - break; - case GL.GL_TRIANGLE_STRIP: - for (int i = 0; i < pIndices.size() - 2; i++) { - if (i % 2 == 0) { - addToListIfAreaNonNull(pIndices.get(i), pIndices.get(i + 1), pIndices.get(i + 2), indices, - discardDegeneratedTriangles); - } else { - addToListIfAreaNonNull(pIndices.get(i), pIndices.get(i + 2), pIndices.get(i + 1), indices, - discardDegeneratedTriangles); - } - } - break; - case GL.GL_TRIANGLE_FAN: - Integer first = pIndices.get(0); - for (int i = 0; i < pIndices.size() - 2; i++) { - addToListIfAreaNonNull(first, pIndices.get(i + 1), pIndices.get(i + 2), indices, - discardDegeneratedTriangles); - } - break; - default: - throw new IllegalStateException("unknown triangle type found: " + type); - } - } - - public void addIndex(Integer i) { - pIndices.add(i); - } - - private void addToListIfAreaNonNull(Integer i1, Integer i2, Integer i3, List indices, - boolean discardDegeneratedTriangles) { - Vector3d v1 = vertices.get(i1); - Vector3d v2 = vertices.get(i2); - Vector3d v3 = vertices.get(i3); - if (discardDegeneratedTriangles && Math.abs(calculateAreaOfTriangle(v1, v2, v3)) <= AREA_EPSILON) { - // switch to discard degenerated triangles is enabled and triangle has nearly no area - logger.trace("Tesselator created degenerated triangle, ignoring"); - } else { - indices.add(i1); - indices.add(i2); - indices.add(i3); - } - } - - /** - * Calculates the area of a triangle given by the 3 points - * - * @param v1 the first point of the triangle - * @param v2 the second point of the triangle - * @param v3 the last point of the triangle - * @return the area of the triangle - */ - private static double calculateAreaOfTriangle(Vector3d v1, Vector3d v2, Vector3d v3) { - double a = v1.getDistance(v2); - double b = v2.getDistance(v3); - double c = v1.getDistance(v3); - double s = 0.5d * (a + b + c); - return Math.sqrt(s * (s - a) * (s - b) * (s - c)); - } - -}