PolyLine2d.java 4.23 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
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
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
/*-
 *  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;

public class PolyLine2d extends BaseEntity {

	private PolyLineSegment2d mpFirst;
	private PolyLineSegment2d mpLast;

	public PolyLine2d(PolyLineSegment2d pStart) {
		mpFirst = mpLast = pStart;
		addChild(mpFirst);

		while (mpLast.getNext() != null) {
			addChild(mpLast.getNext());
			mpLast = mpLast.getNext();
		}
	}

	public PolyLine2d(Coordinate2d pVertex1, Coordinate2d pVertex2) {
		PolyLineSegment2d pSegment = new PolyLineSegment2d(pVertex1, pVertex2);
		mpFirst = mpLast = pSegment;
		addChild(pSegment);
	}

	public List<Coordinate2d> getCoordinates() {
		List<Coordinate2d> coordinates = new ArrayList<>();
		if (mpFirst != null) {
			PolyLineSegment2d pSegment = mpFirst;
			coordinates.add(pSegment.getStart());
			while (pSegment != null) {
				coordinates.add(pSegment.getEnd());

				pSegment = pSegment.getNext();
			}
		}
		return coordinates;
	}

	public PolyLineSegment2d getFirstSegment() {
		return mpFirst;
	}

	public PolyLineSegment2d getLastSegment() {
		return mpLast;
	}

	public Coordinate2d getStart() {
		return mpFirst.getStart();
	}

	public Coordinate2d getEnd() {
		return mpLast.getEnd();
	}

	public List<PolyLineSegment2d> getSegments() {
		List<PolyLineSegment2d> segments = new ArrayList<>();

		PolyLineSegment2d pSegment = mpFirst;
		while (pSegment != null) {
			segments.add(pSegment);
			pSegment = pSegment.getNext();
		}

		return segments;
	}

	public PolyLine2d append(PolyLine2d pSecondLine) {
		// append the end-vertices to the current line
		PolyLineSegment2d pSecondStart = pSecondLine.getFirstSegment();
		while (pSecondStart != null) {
			append(pSecondStart.getEnd());
			pSecondStart = pSecondStart.getNext();
		}
		return pSecondLine;
	}

	private Coordinate2d append(Coordinate2d pVertex) {
		// check if the polyline already contains segments
		// -----------------------------------------------

		PolyLineSegment2d pNewSegment = new PolyLineSegment2d(getEnd(), pVertex);
		addChild(pNewSegment);

		mpLast.setNext(pNewSegment);
		mpLast = pNewSegment;

		return pVertex;
	}

	/**
	 * Split a polyline at a vertex that is already part of the polyline. The
	 * original PolyLine will be split, and the right fragment will be returned. The
	 * original PolyLine will hold the left fragment
	 * 
	 * @param pVertex
	 * @return
	 */
	public PolyLine2d split(Coordinate2d pVertex) {
		PolyLineSegment2d seg = mpFirst;
		PolyLineSegment2d leftLast = null;
		PolyLineSegment2d rightFirst = null;

		for (; seg != null; seg = seg.getNext()) {
			if (seg.getEnd() == pVertex) {
				leftLast = seg;
				rightFirst = seg.getNext();
				break;
			}
		}

		if (leftLast == null) {
			throw new IllegalArgumentException("vertex is not part of polyline");
		}

		List<PolyLineSegment2d> segmentsToRemove = new ArrayList<>();

		PolyLineSegment2d pToRemove = rightFirst;
		while (pToRemove != null) {
			segmentsToRemove.add(pToRemove);
			pToRemove = pToRemove.getNext();
		}

		// terminate this polyline
		// -----------------------

		leftLast.setNext(null);

		mpLast = leftLast;

		PolyLine2d pRightLine = new PolyLine2d(rightFirst);

		// De-refernence all unused segments from this line
		// ------------------------------------------------

		for (PolyLineSegment2d itTR : segmentsToRemove) {
			removeChild(itTR);
		}

		return pRightLine;
	}

Matthias Betz's avatar
Matthias Betz committed
162
163
164
165
166
	@Override
	public String toString() {
		return "PolyLine2d [mpFirst=" + mpFirst + ", mpLast=" + mpLast + "]";
	}

167
}