GeometryTest.java 15 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
/*-
 *  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 static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
26
27
28
29
30
31
32
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.List;
33
34
35
36
37
38

import org.citygml4j.factory.GMLGeometryFactory;
import org.citygml4j.model.citygml.building.AbstractBoundarySurface;
import org.citygml4j.model.citygml.building.WallSurface;
import org.junit.Test;

39
40
41
42
43
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.CheckResult;
import de.hft.stuttgart.citydoctor2.check.ResultStatus;
44
import de.hft.stuttgart.citydoctor2.datastructure.LinearRing.LinearRingType;
45
import de.hft.stuttgart.citydoctor2.math.Vector3d;
46
47
48
49
50
51
52
53
54
55
56
57
import de.hft.stuttgart.citydoctor2.parser.ParserConfiguration;

/**
 * 
 * @author Matthias Betz
 *
 */
public class GeometryTest {

	@Test
	public void testCreate() {
		Geometry geom = new Geometry(GeometryType.SOLID, Lod.LOD0);
58

59
60
61
62
		assertNotNull(geom);
		assertSame(GeometryType.SOLID, geom.getType());
		assertSame(Lod.LOD0, geom.getLod());
	}
63

64
65
66
	@Test
	public void testAddPolygon() {
		Geometry geom = new Geometry(GeometryType.SOLID, Lod.LOD0);
67

68
69
		Polygon p = new ConcretePolygon();
		geom.addPolygon(p);
70

71
72
73
74
		assertEquals(1, geom.getPolygons().size());
		assertSame(geom, p.getParent());
		assertSame(geom.getPolygons().get(0), p);
	}
75

76
	@Test
77
	public void testRemovePolygonConcreteWithLink() {
78
79
80
81
82
83
		ParserConfiguration config = new ParserConfiguration(4, false);
		AbstractBoundarySurface abs = new WallSurface();
		BoundarySurface bs = new BoundarySurface(SurfaceFeatureType.BUILDING, BoundarySurfaceType.WALL, abs);
		org.citygml4j.model.citygml.building.BuildingInstallation gmlBi = new org.citygml4j.model.citygml.building.BuildingInstallation();
		BuildingInstallation bi = new BuildingInstallation();
		bi.setGmlObject(gmlBi);
84
		Geometry geom = new Geometry(GeometryType.MULTI_SURFACE, Lod.LOD2);
85

86
		ConcretePolygon p = new ConcretePolygon();
87
88
89
		LinearRing lr = new LinearRing(LinearRingType.EXTERIOR);
		p.setExteriorRing(lr);
		lr.addVertex(new Vertex(0, 0, 0));
90

91
92
93
		geom.addPolygon(p);
		p.setPartOfSurface(bs);
		p.setPartOfInstallation(bi);
94

95
96
97
98
99
100
		bs.addGeometry(geom);

		Geometry biGeom = new Geometry(GeometryType.COMPOSITE_SURFACE, Lod.LOD2);
		biGeom.addPolygon(new LinkedPolygon(p, biGeom));
		bi.addGeometry(biGeom);

101
		geom.removePolygon(p);
102

103
		assertEquals(0, geom.getPolygons().size());
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
		assertEquals(0, biGeom.getPolygons().size());

		bs.reCreateGeometries(new GMLGeometryFactory(), config);
		assertNull(abs.getLod2MultiSurface());

		bi.reCreateGeometries(new GMLGeometryFactory(), config);
		assertNull(gmlBi.getLod2Geometry());
	}

	@Test
	public void testRemovePolygonOnlyConcrete() {
		ParserConfiguration config = new ParserConfiguration(4, false);
		AbstractBoundarySurface abs = new WallSurface();
		BoundarySurface bs = new BoundarySurface(SurfaceFeatureType.BUILDING, BoundarySurfaceType.WALL, abs);
		Geometry geom = new Geometry(GeometryType.MULTI_SURFACE, Lod.LOD2);

		ConcretePolygon p = new ConcretePolygon();
		LinearRing lr = new LinearRing(LinearRingType.EXTERIOR);
		p.setExteriorRing(lr);
		lr.addVertex(new Vertex(0, 0, 0));

		geom.addPolygon(p);
		p.setPartOfSurface(bs);

		bs.addGeometry(geom);

		geom.removePolygon(p);

		bs.reCreateGeometries(new GMLGeometryFactory(), config);
		assertNull(abs.getLod2MultiSurface());
	}

	@Test
	public void testRemovePolygonLinked() {
		ParserConfiguration config = new ParserConfiguration(4, false);
		AbstractBoundarySurface abs = new WallSurface();
		BoundarySurface bs = new BoundarySurface(SurfaceFeatureType.BUILDING, BoundarySurfaceType.WALL, abs);
		org.citygml4j.model.citygml.building.BuildingInstallation gmlBi = new org.citygml4j.model.citygml.building.BuildingInstallation();
		BuildingInstallation bi = new BuildingInstallation();
		bi.setGmlObject(gmlBi);
		Geometry geom = new Geometry(GeometryType.MULTI_SURFACE, Lod.LOD2);

		ConcretePolygon p = new ConcretePolygon();
		LinearRing lr = new LinearRing(LinearRingType.EXTERIOR);
		p.setExteriorRing(lr);
		lr.addVertex(new Vertex(0, 0, 0));
		bs.addGeometry(geom);

		geom.addPolygon(p);
		p.setPartOfSurface(bs);
		p.setPartOfInstallation(bi);

		Geometry biGeom = new Geometry(GeometryType.COMPOSITE_SURFACE, Lod.LOD2);
		LinkedPolygon linkedPolygon = new LinkedPolygon(p, biGeom);
		biGeom.addPolygon(linkedPolygon);
		bi.addGeometry(biGeom);

		biGeom.removePolygon(linkedPolygon);

		assertEquals(0, geom.getPolygons().size());
		assertEquals(0, biGeom.getPolygons().size());
165

166
167
		bs.reCreateGeometries(new GMLGeometryFactory(), config);
		assertNull(abs.getLod2MultiSurface());
168

169
170
171
		bi.reCreateGeometries(new GMLGeometryFactory(), config);
		assertNull(gmlBi.getLod2Geometry());
	}
172

173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
	@Test
	public void testReplacePolygon() {
		ParserConfiguration config = new ParserConfiguration(4, false);
		AbstractBoundarySurface abs = new WallSurface();
		BoundarySurface bs = new BoundarySurface(SurfaceFeatureType.BUILDING, BoundarySurfaceType.WALL, abs);
		org.citygml4j.model.citygml.building.BuildingInstallation gmlBi = new org.citygml4j.model.citygml.building.BuildingInstallation();
		BuildingInstallation bi = new BuildingInstallation();
		bi.setGmlObject(gmlBi);
		bi.addBoundarySurface(bs);
		Geometry geom2 = new Geometry(GeometryType.MULTI_SURFACE, Lod.LOD2);
		ConcretePolygon p = new ConcretePolygon();
		geom2.addPolygon(p);
		LinearRing lr = new LinearRing(LinearRingType.EXTERIOR);
		p.setExteriorRing(lr);
		lr.addVertex(new Vertex(0, 0, 0));
		p.setPartOfSurface(bs);
		p.setPartOfInstallation(bi);
		bs.addGeometry(geom2);
191

192
193
194
		Geometry geom = new Geometry(GeometryType.SOLID, Lod.LOD2);
		Polygon linkedPoly = new LinkedPolygon(p, geom);
		geom.addPolygon(linkedPoly);
195

196
197
198
199
200
		ConcretePolygon p2 = new ConcretePolygon();
		p2.setExteriorRing(lr);

		ConcretePolygon p3 = new ConcretePolygon();
		p3.setExteriorRing(lr);
201

202
		geom.replacePolygon(linkedPoly, p2, p3);
203

204
205
		assertEquals(2, geom.getPolygons().size());
		assertEquals(2, geom2.getPolygons().size());
206

207
208
209
210
		bi.reCreateGeometries(new GMLGeometryFactory(), config);
		assertNull(gmlBi.getLod2Geometry());
		assertNull(gmlBi.getLod3Geometry());
		assertNull(gmlBi.getLod4Geometry());
211

212
213
214
215
216
217
218
219
220
		bs.reCreateGeometries(new GMLGeometryFactory(), config);
		assertNotNull(abs.getLod2MultiSurface());
		assertNotNull(abs.getLod2MultiSurface().getMultiSurface());
		assertNotNull(abs.getLod2MultiSurface().getMultiSurface().getSurfaceMember());
		assertFalse(abs.getLod2MultiSurface().getMultiSurface().getSurfaceMember().isEmpty());
		assertEquals(2, abs.getLod2MultiSurface().getMultiSurface().getSurfaceMember().size());
		assertNull(gmlBi.getLod2Geometry());
	}

221
222
223
224
225
226
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
	@Test
	public void testSetParent() {
		Geometry geom = new Geometry(GeometryType.MULTI_SURFACE, Lod.LOD2);
		CityObject co = mock(CityObject.class);
		geom.setParent(co);
		assertEquals(co, geom.getParent());
	}

	@Test
	public void testUpdateEdges() {
		Geometry geom = GeometryTestUtils.createDummyGeometryWithInnerRingWithNeighboringPolygon(GeometryType.SOLID,
				Lod.LOD2);
		geom.clearMetaInformation();
		assertNull(geom.getEdges());
		geom.updateEdges();
		List<Edge> edges = geom.getEdges();
		assertNotNull(edges);
		assertFalse(edges.isEmpty());
		assertEquals(10, edges.size());
	}

	@Test
	public void testSetType() {
		Geometry geom = new Geometry(GeometryType.MULTI_SURFACE, Lod.LOD2);
		geom.setType(GeometryType.SOLID);
		assertEquals(GeometryType.SOLID, geom.getType());
	}

	@Test
	public void testGetCenter() {
		Geometry geom = GeometryTestUtils.createDummyGeometry(GeometryType.SOLID);
		Vector3d center = geom.getCenter();
		assertEquals(427583.3025, center.getX(), 0.000001);
		assertEquals(6003502.5725, center.getY(), 0.000001);
		assertEquals(6.905, center.getZ(), 0.000001);
	}

	@Test
	public void testContainsError() {
		Geometry geom = new Geometry(GeometryType.SOLID, Lod.LOD1);
		assertFalse(geom.containsError(CheckId.C_GE_P_HOLE_OUTSIDE));
262
		geom.addCheckResult(new CheckResult(CheckId.C_GE_P_HOLE_OUTSIDE, ResultStatus.ERROR, mock(CheckError.class)));
263
264
265
266
267
268
269
270
271
		assertTrue(geom.containsError(CheckId.C_GE_P_HOLE_OUTSIDE));
		ConcretePolygon p = new ConcretePolygon();
		p.setExteriorRing(mock(LinearRing.class));
		geom.addPolygon(p);
		assertFalse(geom.containsError(CheckId.C_GE_P_INNER_RINGS_NESTED));
		p.addCheckResult(
				new CheckResult(CheckId.C_GE_P_INNER_RINGS_NESTED, ResultStatus.ERROR, mock(CheckError.class)));
		assertTrue(geom.containsError(CheckId.C_GE_P_INNER_RINGS_NESTED));
	}
272

273
274
275
276
	@Test
	public void testClearAllContainedCheckResults() {
		Geometry geom = new Geometry(GeometryType.SOLID, Lod.LOD1);
		assertFalse(geom.containsError(CheckId.C_GE_P_HOLE_OUTSIDE));
277
		geom.addCheckResult(new CheckResult(CheckId.C_GE_P_HOLE_OUTSIDE, ResultStatus.ERROR, mock(CheckError.class)));
278
279
280
281
282
283
284
		assertTrue(geom.containsError(CheckId.C_GE_P_HOLE_OUTSIDE));
		Polygon p = mock(Polygon.class);
		geom.addPolygon(p);
		geom.clearAllContainedCheckResults();
		verify(p).clearAllContainedCheckResults();
		assertFalse(geom.containsError(CheckId.C_GE_P_HOLE_OUTSIDE));
	}
285

286
287
288
289
	@Test
	public void testCollectContainedErrors() {
		Geometry geom = new Geometry(GeometryType.SOLID, Lod.LOD1);
		assertFalse(geom.containsError(CheckId.C_GE_P_HOLE_OUTSIDE));
290
		geom.addCheckResult(new CheckResult(CheckId.C_GE_P_HOLE_OUTSIDE, ResultStatus.ERROR, mock(CheckError.class)));
291
292
293
294
295
296
297
298
		assertTrue(geom.containsError(CheckId.C_GE_P_HOLE_OUTSIDE));
		Polygon p = mock(Polygon.class);
		geom.addPolygon(p);
		List<CheckError> errors = new ArrayList<>();
		geom.collectContainedErrors(errors);
		verify(p).collectContainedErrors(errors);
		assertEquals(1, errors.size());
	}
299

300
301
302
303
	@Test
	public void testContainsAnyError() {
		Geometry geom = new Geometry(GeometryType.SOLID, Lod.LOD1);
		assertFalse(geom.containsAnyError());
304
		geom.addCheckResult(new CheckResult(CheckId.C_GE_P_HOLE_OUTSIDE, ResultStatus.ERROR, mock(CheckError.class)));
305
306
307
308
309
310
311
312
313
		assertTrue(geom.containsAnyError());
		Polygon p = mock(Polygon.class);
		geom.addPolygon(p);
		geom.clearAllContainedCheckResults();
		assertFalse(geom.containsAnyError());
		verify(p).containsAnyError();
		when(p.containsAnyError()).thenReturn(true);
		assertTrue(geom.containsAnyError());
	}
314

315
316
317
318
319
320
321
322
323
324
325
326
327
	@Test
	public void testAccept() {
		Geometry geom = new Geometry(GeometryType.SOLID, Lod.LOD1);
		Check c = mock(Check.class);
		geom.accept(c);
		when(c.canExecute(geom)).thenReturn(true);
		geom.accept(c);
		verify(c).check(geom);
		Polygon p = mock(Polygon.class);
		geom.addPolygon(p);
		geom.accept(c);
		verify(p).accept(c);
	}
328

329
330
331
332
333
334
	@Test
	public void testGetVertices() {
		Geometry geom = GeometryTestUtils.createDummyGeometry(GeometryType.SOLID);
		List<Vertex> vertices = geom.getVertices();
		assertEquals(4, vertices.size());
	}
335

336
337
	@Test
	public void testGetEdgesAdjacentToVertex() {
338
339
		Geometry geom = GeometryTestUtils.createDummyGeometryWithInnerRingWithNeighboringPolygon(GeometryType.SOLID,
				Lod.LOD1);
340
341
342
		Polygon polygon = geom.getPolygons().get(1);
		Vertex vertex = polygon.getExteriorRing().getVertices().get(0);
		List<Edge> edges = geom.getEdgesAdjacentTo(vertex);
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
		assertEquals(3, edges.size());
	}

	@Test
	public void testGetEdge() {
		Geometry geom = GeometryTestUtils.createDummyGeometry(GeometryType.SOLID);
		Polygon poly = geom.getPolygons().get(0);
		LinearRing ext = poly.getExteriorRing();
		Vertex v0 = ext.getVertices().get(0);
		Vertex v1 = ext.getVertices().get(1);
		Edge e = geom.getEdge(v0, v1);
		assertNotNull(e);
		assertEquals(v0, e.getFrom());
		assertEquals(v1, e.getTo());

		Edge e2 = geom.getEdge(v1, v0);
		assertEquals(e, e2);

		Vertex v2 = ext.getVertices().get(2);
		Edge e3 = geom.getEdge(v0, v2);
		assertNull(e3);
	}

	@Test
	public void testUpdateVertices() {
		Geometry geom = GeometryTestUtils.createDummyGeometryWithInnerRingWithNeighboringPolygon(GeometryType.SOLID,
				Lod.LOD0);
		geom.updateVertices();
		Polygon polygon = geom.getPolygons().get(0);
		Vertex v1 = polygon.getExteriorRing().getVertices().get(0);
		assertTrue(v1.getAdjacentRings(geom).contains(geom.getPolygons().get(0).getExteriorRing()));
		assertTrue(v1.getAdjacentRings(geom).contains(geom.getPolygons().get(1).getExteriorRing()));
		assertEquals(9, geom.getVertices().size());
	}

	@Test
	public void testGetEdgesAdjacentToPolygon() {
		Geometry geom = GeometryTestUtils.createDummyGeometryWithInnerRingWithNeighboringPolygon(GeometryType.SOLID,
				Lod.LOD0);
		Polygon polygon = geom.getPolygons().get(0);

		List<Edge> edges = new ArrayList<>();
		for (int i = 0; i < polygon.getExteriorRing().getVertices().size() - 1; i++) {
			Vertex v1 = polygon.getExteriorRing().getVertices().get(i);
			Vertex v2 = polygon.getExteriorRing().getVertices().get(i + 1);
			Edge e = geom.getEdge(v1, v2);
			edges.add(e);
		}

		for (LinearRing lr : polygon.getInnerRings()) {
			for (int i = 0; i < lr.getVertices().size() - 1; i++) {
				Vertex v1 = lr.getVertices().get(i);
				Vertex v2 = lr.getVertices().get(i + 1);
				Edge e = geom.getEdge(v1, v2);
				edges.add(e);
			}
		}
		List<Edge> edgesAdjacentTo = geom.getEdgesAdjacentTo(polygon);
		for (Edge e : edges) {
			assertTrue(edgesAdjacentTo.contains(e));
		}
	}

	@Test
	public void testContainsPolygon() {
		Geometry geom = new Geometry(GeometryType.SOLID, Lod.LOD0);
		ConcretePolygon cdPoly = new ConcretePolygon();
		geom.addPolygon(cdPoly);
		cdPoly.setGmlId(new GmlId("test1"));

		ConcretePolygon cdPoly2 = new ConcretePolygon();
		geom.addPolygon(cdPoly2);
		cdPoly2.setGmlId(new GmlId("test2"));

		ConcretePolygon cdPoly3 = new ConcretePolygon();
		cdPoly3.setGmlId(new GmlId("test1"));

		ConcretePolygon cdPoly4 = new ConcretePolygon();
		cdPoly4.setGmlId(new GmlId("test3"));

		assertTrue(geom.containsPolygon(cdPoly3));
		assertFalse(geom.containsPolygon(cdPoly4));
	}

	@Test
	public void testClearMetaInformation() {
		Geometry geom = GeometryTestUtils.createDummyGeometryWithInnerRingWithNeighboringPolygon(GeometryType.SOLID,
				Lod.LOD0);
		assertNotNull(geom.getVertices());
		geom.clearMetaInformation();
		assertNull(geom.getVertices());
		assertNull(geom.getEdges());
		geom.clearMetaInformation();
		assertNull(geom.getVertices());
		assertNull(geom.getEdges());
438
439
	}

440
}