Edge.java 4.49 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
41
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
/*-
 *  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.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Representing an edge between two consecutive points of a linear ring
 * 
 * @author Matthias Betz
 *
 */
public class Edge implements Serializable {

	private static final long serialVersionUID = -504694863498128296L;

	private List<Polygon> adjacentPolygons = new ArrayList<>(2);
	private List<LinearRing> adjacentRings = new ArrayList<>(2);

	private Vertex from;
	private Vertex to;

	// start with 1 for a new edge
	private int numberOfHalfEdges = 1;
	private int numberOfOppositeHalfEdges;

	public Edge(Vertex from, Vertex to) {
		this.from = from;
		this.to = to;
	}

	public Vertex getFrom() {
		return from;
	}

	public Vertex getTo() {
		return to;
	}

	public void addHalfEdge(Vertex v0, Vertex v1) {
		if (v0 == null || v1 == null) {
			return;
		}
		if (isPointOfEdge(v0, from) && isPointOfEdge(v1, to)) {
			numberOfHalfEdges++;
			return;
		}
		if (isPointOfEdge(v1, from) && isPointOfEdge(v0, to)) {
			numberOfOppositeHalfEdges++;
			return;
		}
70
		throw new IllegalStateException("The given vertices do not form this edge\n v0=" + v0 + ", v1=" + v1 + ", edge" + this);
71
72
73
74
75
76
	}

	private boolean isPointOfEdge(Vertex v0, Vertex edgePoint) {
		if (v0.equals(edgePoint)) {
			return true;
		}
77
78
79
80
81
//		for (Vertex v : v0.getNeighbors()) {
//			if (v.equals(edgePoint)) {
//				return true;
//			}
//		}
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
151
152
153
154
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
		return false;
	}

	public boolean formedByIgnoreDirection(Vertex v1, Vertex v2) {
		return (v1 == from && v2 == to) || (v1 == to && v2 == from);
	}

	public boolean equalsIgnoreDirection(Edge other) {
		return (other.from.equals(from) && other.to.equals(to)) || (other.from.equals(to) && other.to.equals(from));
	}

	public int getNumberOfHalfEdges() {
		return numberOfHalfEdges;
	}

	public int getNumberOppositeHalfEdges() {
		return numberOfOppositeHalfEdges;
	}

	public int getNumberOfAllHalfEdges() {
		return numberOfHalfEdges + numberOfOppositeHalfEdges;
	}

	public List<Polygon> getAdjacentPolygons() {
		return adjacentPolygons;
	}

	public List<LinearRing> getAdjacentRings() {
		return adjacentRings;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((from == null) ? 0 : from.hashCode());
		result = prime * result + ((to == null) ? 0 : to.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		Edge other = (Edge) obj;
		if (from == null) {
			if (other.from != null) {
				return false;
			}
		} else if (!from.equals(other.from)) {
			return false;
		}
		if (to == null) {
			if (other.to != null) {
				return false;
			}
		} else if (!to.equals(other.to)) {
			return false;
		}
		return true;
	}

	public void addAdjacentPolygon(Polygon p) {
		adjacentPolygons.add(p);
	}

	public void addAdjacentRing(LinearRing lr) {
		adjacentRings.add(lr);
	}

	@Override
	public String toString() {
		return "Edge [from=" + from + ", to=" + to + "]";
	}

	public boolean contains(Vertex v) {
		return v == from || v == to;
	}

	public Vertex getConnectionPoint(Edge e) {
		if (from == e.getTo() || from == e.getFrom()) {
			return from;
		}
		if (to == e.getTo() || to == e.getFrom()) {
			return to;
		}
		return null;
	}

	/**
	 * finds the point on the other end of the edge if the given vertex is one of
	 * the end points.
	 * 
	 * @param c the given vertex
	 * @return the opposite vertex or null if the given vertex is not an end point.
	 */
	public Vertex getOppositeVertex(Vertex c) {
		if (c == from) {
			return to;
		}
		if (c == to) {
			return from;
		}
		return null;
	}

}