Polygon2d.java 4.58 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*-
 *  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 <https://www.gnu.org/licenses/>.
 */
package de.hft.stuttgart.citydoctor2.math;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import de.hft.stuttgart.citydoctor2.datastructure.LinearRing;
import de.hft.stuttgart.citydoctor2.datastructure.Polygon;
import de.hft.stuttgart.citydoctor2.datastructure.Vertex;

/**
 * A two dimensional polygon. Has an 2d exterior ring and interior rings
 * 
 * @author Matthias Betz
 *
 */
public class Polygon2d {

	private Ring2d exterior;
	private List<Ring2d> innerRings;

	public static Polygon2d withProjection(Polygon poly) {
41
		Vector3d normal = poly.calculateNormalNormalized();
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
		int[] axis = getProjectionAxis(normal);
		return projectTo2D(poly, axis);
	}

	public static Polygon2d withProjection(MovedPolygon poly, int[] projectionAxis) {
		return projectTo2D(poly, projectionAxis);
	}

	public static Polygon2d withProjection(Polygon poly, int[] projectionAxis) {
		return projectTo2D(poly, projectionAxis);
	}

	private static Polygon2d projectTo2D(Polygon p, int[] axis) {
		List<Ring2d> interior = new ArrayList<>();
		Ring2d exterior = projectRing(p.getExteriorRing(), axis);
		for (LinearRing innerRing : p.getInnerRings()) {
			interior.add(projectRing(innerRing, axis));
		}
		return new Polygon2d(exterior, interior);
	}

	private static Polygon2d projectTo2D(MovedPolygon p, int[] axis) {
		List<Ring2d> interior = new ArrayList<>();
		Ring2d exterior = projectRing(p.getExteriorRing(), axis);
		for (MovedRing innerRing : p.getInnerRings()) {
			interior.add(projectRing(innerRing, axis));
		}
		return new Polygon2d(exterior, interior);
	}

	public static int[] getProjectionAxis(Vector3d normal) {
		double nx = Math.abs(normal.getX());
		double ny = Math.abs(normal.getY());
		double nz = Math.abs(normal.getZ());
		int[] axis;
77
		if (nx >= ny && nx >= nz) {
78
			axis = new int[] { 1, 2 };
79
		} else if (ny >= nx && ny >= nz) {
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
			axis = new int[] { 0, 2 };
		} else {
			axis = new int[] { 0, 1 };
		}
		return axis;
	}

	private static Ring2d projectRing(LinearRing r, int[] axis) {
		List<Vector2d> projectedVertices = new ArrayList<>();
		for (Vertex v : r.getVertices()) {
			projectedVertices.add(new Vector2d(v.getCoordinate(axis[0]), v.getCoordinate(axis[1])));
		}
		return new Ring2d(projectedVertices, r);
	}

	private static Ring2d projectRing(MovedRing r, int[] axis) {
		List<Vector2d> projectedVertices = new ArrayList<>();
		for (Vector3d v : r.getVertices()) {
			projectedVertices.add(new Vector2d(v.getCoordinate(axis[0]), v.getCoordinate(axis[1])));
		}
		return new Ring2d(projectedVertices, r.getOriginal());
	}

	public static Polygon2d withRotationMatrix(Polygon poly) {
		Matrix3x3d rotMatrix = PolygonUtils.calculateRotationMatrix(poly);

		LinearRing lr = poly.getExteriorRing();
		Ring2d exterior = createRing2d(rotMatrix, lr);

		List<Ring2d> innerRings = new ArrayList<>();
		for (LinearRing innerRing : poly.getInnerRings()) {
			innerRings.add(createRing2d(rotMatrix, innerRing));
		}
		return new Polygon2d(exterior, innerRings);
	}

	/**
	 * Converts a Polygon to a 2d polygon. This will change the area of the polygon.
	 * 
	 * @param ring a linear ring
	 */
	private Polygon2d(Ring2d exterior, List<Ring2d> interior) {
		if (interior == null) {
			interior = Collections.emptyList();
		}
		this.exterior = exterior;
		this.innerRings = interior;
	}

	private static Ring2d createRing2d(Matrix3x3d rotMatrix, LinearRing lr) {
		List<Vector2d> ringVertices = new ArrayList<>();
		for (Vertex v : lr.getVertices()) {
			Vector3d rotated = rotMatrix.mult(v);
			Vector2d v2d = new Vector2d(rotated.getX(), rotated.getY());
			ringVertices.add(v2d);
		}
		return new Ring2d(ringVertices, lr);
	}

	public Ring2d getExterior() {
		return exterior;
	}

	public List<Ring2d> getInteriorRings() {
		if (innerRings == null) {
			return Collections.emptyList();
		}
		return innerRings;
	}

}