EdgePolygon.java 3.79 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.edge;


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

25
26
import de.hft.stuttgart.citydoctor2.datastructure.Polygon;

27
28
public class EdgePolygon extends BaseEntity {

29
	private Polygon original;
30
31
	private List<HalfEdge> halfEdges;
	
32
	public EdgePolygon(List<HalfEdge> halfEdges, Polygon original) {
33
34
35
36
		for (HalfEdge he : halfEdges) {
			addChild(he);
		}
		this.halfEdges = halfEdges;
37
38
39
40
41
		this.original = original;
	}
	
	public Polygon getOriginal() {
		return original;
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
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
119
120
121
122
123
124
125
	}

	public GmPlane getPlane() {
		Point3d midPoint = getMidPoint();

		List<HalfEdge> children = getChildren(HalfEdge.class);
		Vector3d averageNormalVector = new Vector3d(0.0, 0.0, 0.0);

		for (HalfEdge he : children) {

			Point3d start = he.getStart().getPoint();
			Point3d end = he.getEnd().getPoint();

			Vector3d mid2Start = start.minus(midPoint);
			Vector3d mid2End = end.minus(midPoint);

			averageNormalVector = averageNormalVector.plus(mid2Start.cross(mid2End));
		}

		UnitVector3d normalVector = averageNormalVector.toUnitVector();
		return new GmPlane(midPoint, normalVector);
	}

	private Point3d getMidPoint() {
		Point3d midPoint = new Point3d(0, 0, 0);
		List<HalfEdge> children = getChildren(HalfEdge.class);

		for (HalfEdge he : children) {
			midPoint = midPoint.plus(he.getStart().getPoint());
		}
		return (midPoint.div(children.size()));
	}

	public List<HalfEdge> getHalfEdges() {
		return halfEdges;
	}

	public List<Coordinate3d> getCoordinates() {
		List<Coordinate3d> coords = new ArrayList<>();
		HalfEdge firstHE = Objects.requireNonNull(getFirstHalfEdge());
		HalfEdge currHE = firstHE;
		do {
			coords.add(currHE.getStart());
			currHE = currHE.getNext();
		} while (currHE != firstHE);
		return coords;
	}

	public HalfEdge getFirstHalfEdge() {
		if (!halfEdges.isEmpty()) {
			return halfEdges.get(0);
		}
		return null;
	}

	/**
	 * Test whether the given point is lying in- or outside of the non self
	 * intersecting, planar polygon. It's believed, that point and polygon are lying
	 * in the same plane. <br>
	 * <br>
	 * It's assumed, that the polygon is non self intersecting and planar.
	 * Additionally it is assumed, that the point and the polygon lying in the same
	 * plane. The given point is projected on the plane of the polygon. If the point
	 * is lying on an edge of the polygon it's supposed that the point is inside of
	 * the polygon
	 * 
	 * @param rcPoint The point that should be checked
	 * 
	 * @return true, if the point is inside the polygon, false otherwise
	 * 
	 */
	public boolean isPointInsidePolygon(Point3d rcPoint, double eps) {
		// Project the coordinate of the point on the plane
		GmPlane plane = getPlane();
		Point2d projectedPoint = plane.project(rcPoint);
		Polygon2d pP = plane.projectOn2dPolygon(this);
		return pP.isPointInsidePolygon(projectedPoint, eps);
	}

	public int getNrHalfEdges() {
		return halfEdges.size();
	}

}