LinearRing.java 9.11 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
/*-
 *  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.datastructure;

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

import de.hft.stuttgart.citydoctor2.check.Check;
25
26
27
import de.hft.stuttgart.citydoctor2.math.ProjectionAxis;
import de.hft.stuttgart.citydoctor2.math.Ring2d;
import de.hft.stuttgart.citydoctor2.math.UnitVector3d;
28
29
import de.hft.stuttgart.citydoctor2.math.Vector2d;
import de.hft.stuttgart.citydoctor2.math.Vector3d;
Matthias Betz's avatar
Matthias Betz committed
30
31
import de.hft.stuttgart.citydoctor2.utils.CopyHandler;
import de.hft.stuttgart.citydoctor2.utils.Copyable;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

/**
 * Represents a linear ring used in polygons. The ring contains the vertices.
 * 
 * @author Matthias Betz
 *
 */
public class LinearRing extends GmlElement {

	private static final long serialVersionUID = -2488180830514940722L;

	private LinearRingType type;
	private Polygon parent;

	private List<Vertex> vertices = new ArrayList<>();

	public enum LinearRingType {
		EXTERIOR, INTERIOR
	}

	public LinearRing(LinearRingType type) {
		this.type = type;
	}

	/**
	 * Checks whether a point is inside this ring. A point on the edge does count as
	 * inside.
	 * 
	 * @param v the point.
	 * @return true if the point is inside or on an edge, false if it is outside.
	 */
	public boolean isPointInside(Vector3d v) {
		// project to 2d ring
65
66
67
		ProjectionAxis axis = ProjectionAxis.of(this);
		Vector2d point = axis.project(v);
		Ring2d ring = Ring2d.of(this, axis);
68
69

		int t = -1;
70
71
		for (int i = 0; i < ring.getVertices().size() - 1; i++) {
			t = t * crossProdTest(point, ring.getVertices().get(i), ring.getVertices().get(i + 1));
72
73
74
75
76
77
78
79
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
			if (t == 0) {
				return true;
			}
		}
		return t >= 0;
	}

	private int crossProdTest(Vector2d a, Vector2d b, Vector2d c) {
		if (a.getY() == b.getY() && a.getY() == c.getY()) {
			if ((b.getX() <= a.getX() && a.getX() <= c.getX()) || (c.getX() <= a.getX() && a.getX() <= b.getX())) {
				return 0;
			} else {
				return 1;
			}
		}
		if (a.getY() == b.getY() && a.getX() == b.getX()) {
			return 0;
		}
		if (b.getY() > c.getY()) {
			Vector2d temp = b;
			b = c;
			c = temp;
		}
		if (a.getY() <= b.getY() || a.getY() > c.getY()) {
			return 1;
		}
		return calculateDelta(a, b, c);
	}

	private int calculateDelta(Vector2d a, Vector2d b, Vector2d c) {
		double delta = (b.getX() - a.getX()) * (c.getY() - a.getY()) - (b.getY() - a.getY()) * (c.getX() - a.getX());
		if (delta > 0) {
			return -1;
		} else if (delta < 0) {
			return 1;
		} else {
			return 0;
		}
	}

	/**
	 * Calculates the normal vector of the ring. Method by Newell. If the Newell
	 * method would return a (0, 0, 0) vector a cross product is formed from the
	 * first 3 vertices. If there are no 3 vertices available (1, 0, 0) is returned.
	 * 
	 * @return the normal as a normalized vector
	 */
119
120
	public UnitVector3d calculateNormalNormalized() {
		return calculateNormal().normalize();
121
	}
Matthias Betz's avatar
Matthias Betz committed
122

123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
	/**
	 * Calculates the normal vector of the ring. Method by Newell. If the Newell
	 * method would return a (0, 0, 0) vector a cross product is formed from the
	 * first 3 vertices. If there are no 3 vertices available (1, 0, 0) is returned.
	 * 
	 * @return the normal vector
	 */
	public Vector3d calculateNormal() {
		double[] coords = new double[3];
		for (int i = 0; i < vertices.size() - 1; i++) {
			Vertex current = vertices.get(i + 0);
			Vertex next = vertices.get(i + 1);
			coords[0] += (current.getZ() + next.getZ()) * (current.getY() - next.getY());
			coords[1] += (current.getX() + next.getX()) * (current.getZ() - next.getZ());
			coords[2] += (current.getY() + next.getY()) * (current.getX() - next.getX());
		}

		if (coords[0] == 0 && coords[1] == 0 && coords[2] == 0) {
			// no valid normal vector found
			if (vertices.size() < 3) {
				// no three points, return x-axis
144
				return UnitVector3d.X_AXIS;
145
146
147
148
149
150
151
152
153
			}

			Vertex v1 = vertices.get(0);
			Vertex v2 = vertices.get(1);
			Vertex v3 = vertices.get(2);
			return calculateNormalWithCross(v1, v2, v3);
		}
		return new Vector3d(coords);
	}
154

Matthias Betz's avatar
Matthias Betz committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
	public double getArea() {
		int n = vertices.size();
		if (n < 3)
			return 0.0; // a degenerated polygon

		// prepare an vertex array with n+2 vertices:
		// V[0] ... V[n-1] - the n different vertices
		// V[n]=V[0]
		// V[n+1]=V[1]
		Vertex[] vertexArray = new Vertex[n + 2];
		for (int i = 0; i < n; i++) {
			vertexArray[i] = vertices.get(i);
		}
		vertexArray[n] = vertices.get(0);
		vertexArray[n + 1] = vertices.get(1);
		double area = 0;

		// make sure the normal has been calculated
		Vector3d normal = calculateNormal();
		// select largest abs coordinate to ignore for projection
		double ax = Math.abs(normal.getX()); // abs x-coord
		double ay = Math.abs(normal.getY()); // abs y-coord
		double az = Math.abs(normal.getZ()); // abs z-coord

		// coord to ignore: 1=x, 2=y, 3=z
		int coord = 3; // ignore z-coord
		if (ax > ay) {
			if (ax > az) {
				coord = 1; // ignore x-coord
			}
		} else if (ay > az) {
			coord = 2; // ignore y-coord
		}

		// compute area of the 2D projection
		for (int i = 1, j = 2, k = 0; i <= vertexArray.length - 2; i++, j++, k++) {
			switch (coord) {
			case 1:
				area += (vertexArray[i].getY() * (vertexArray[j].getZ() - vertexArray[k].getZ()));
				break;
			case 2:
				area += (vertexArray[i].getX() * (vertexArray[j].getZ() - vertexArray[k].getZ()));
				break;
			case 3:
				area += (vertexArray[i].getX() * (vertexArray[j].getY() - vertexArray[k].getY()));
				break;
			default:
				throw new IllegalStateException();
			}
		}

		// scale to get area before projection
		double an = Math.sqrt(ax * ax + ay * ay + az * az); // length of normal vector

		switch (coord) {
		case 1:
			area *= (an / (2 * ax));
			break;
		case 2:
			area *= (an / (2 * ay));
			break;
		case 3:
			area *= (an / (2 * az));
			break;
		default:
			throw new IllegalStateException();
		}

		return Math.abs(area);
	}

226
	private UnitVector3d calculateNormalWithCross(Vertex v1, Vertex v2, Vertex v3) {
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
		Vector3d dir1 = v2.minus(v1);
		Vector3d dir2 = v3.minus(v1);
		Vector3d cross = dir1.cross(dir2);
		return cross.normalize();
	}

	@Override
	public void accept(Check c) {
		super.accept(c);
		if (c.canExecute(this)) {
			c.check(this);
		}
	}

	@Override
	public void clearAllContainedCheckResults() {
		super.clearCheckResults();
	}

	public void setParent(Polygon polygon) {
		parent = polygon;
	}

	public Polygon getParent() {
		return parent;
	}

	public LinearRingType getType() {
		return type;
	}

	public List<Vertex> getVertices() {
		return vertices;
	}

	public void addVertex(Vertex v) {
		vertices.add(v);
		if (parent == null) {
			return;
		}
		if (parent.isLinkedTo()) {
268
			v.addAdjacentRing(this, parent.getLinkedFromPolygon().getParent());
269
		}
270
		v.addAdjacentRing(this, parent.getParent());
271
272
273
274
275
	}

	public void addVertex(int i, Vertex v) {
		vertices.add(i, v);
		if (parent.isLinkedTo()) {
276
			v.addAdjacentRing(this, parent.getLinkedFromPolygon().getParent());
277
		}
278
		v.addAdjacentRing(this, parent.getParent());
279
	}
Matthias Betz's avatar
Matthias Betz committed
280

281
282
283
284
285
286
287
	public void setVertex(int i, Vertex v) {
		vertices.set(i, v);
		if (parent.isLinkedTo()) {
			v.addAdjacentRing(this, parent.getLinkedFromPolygon().getParent());
		}
		v.addAdjacentRing(this, parent.getParent());
	}
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317

	@Override
	public String toString() {
		return "LinearRing [type=" + type + ", gmlId=" + getGmlId() + "]";
	}

	void anonymize() {
		setGmlId(GmlId.generateId());
	}

	public boolean isRingConnectedViaPoint(LinearRing other) {
		for (Vertex v : vertices) {
			if (other.getVertices().contains(v)) {
				return true;
			}
		}
		return false;
	}

	public boolean hasPointAsCorner(Vertex v) {
		return vertices.contains(v);
	}

	public void setType(LinearRingType type) {
		this.type = type;
	}

	public void addAllVertices(List<Vertex> extRing) {
		vertices.addAll(extRing);
	}
Matthias Betz's avatar
Matthias Betz committed
318

319
320
321
322
	@Override
	public void prepareForChecking() {
		parent.getParent().prepareForChecking();
	}
Matthias Betz's avatar
Matthias Betz committed
323

324
325
326
327
	@Override
	public void clearMetaInformation() {
		parent.getParent().clearMetaInformation();
	}
Matthias Betz's avatar
Matthias Betz committed
328

Matthias Betz's avatar
Matthias Betz committed
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
	@Override
	public void fillValues(Copyable original, CopyHandler handler) {
		super.fillValues(original, handler);
		LinearRing originalRing = (LinearRing) original;
		type = originalRing.type;
		parent = handler.getCopyInstance(originalRing.parent);
		for (Vertex v : originalRing.vertices) {
			vertices.add(handler.getCopyInstance(v));
		}
	}

	@Override
	public void collectInstances(CopyHandler handler) {
		for (Vertex v : vertices) {
			handler.addInstance(v);
		}
		handler.addInstance(parent);
	}

	@Override
	public Copyable createCopyInstance() {
		return new LinearRing(type);
	}
352
}