DebugUtils.java 8.53 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
/*-
 *  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.text.NumberFormat;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

public class DebugUtils {
	
	private static AtomicInteger polygon3dCounter = new AtomicInteger(1);
	private static AtomicInteger coordListCounter = new AtomicInteger(1);
	
	private DebugUtils() {
	}

	public static void printPolyLineSegment2dList(List<PolyLineSegment2d> rSegments) {
		Locale.setDefault(Locale.US);
		for (PolyLineSegment2d seg : rSegments) {
			Point2d start = seg.getStart().getPoint();
			Point2d end = seg.getEnd().getPoint();
			System.out.println(String.format(
					"segmentList.push_back(new PolyLineSegment2d(new Coordinate2d(%f, %f), new Coordinate2d(%f, %f)));",
					start.getX(), start.getY(), end.getX(), end.getY()));
		}
	}

	public static void printPolygon2d(Polygon2d mcpPolygon1) {
		Locale.setDefault(Locale.US);
		System.out.println("Coordinate2dList coords;");
		HalfEdge2d firstHalfEdge = mcpPolygon1.getFirstHalfEdge();
		Point2d start = firstHalfEdge.getStart().getPoint();
		System.out.println(String.format("coords.push_back(new Coordinate2d(%f, %f));", start.getX(), start.getY()));
		HalfEdge2d next = firstHalfEdge.getNext();
		while (next != null && next != firstHalfEdge) {
			start = next.getStart().getPoint();
			System.out.println(String.format("coords.push_back(new Coordinate2d(%f, %f));", start.getX(), start.getY()));
			next = next.getNext();
		}
		System.out.println("Polygon2d* mcpPolygon1 = new Polygon2dNs(coords);");
	}

	public static void printPolygon3d(EdgePolygon p) {
		Locale.setDefault(Locale.US);
		int counter = polygon3dCounter.getAndIncrement();
		System.out.println("Coordinate3dList coords" + counter + ";");
		HalfEdge firstHalfEdge = p.getFirstHalfEdge();
		Point3d start = firstHalfEdge.getStart().getPoint();
		System.out.println(String.format("coords%d.push_back(new Coordinate3d(%f, %f, %f));", counter, start.getX(), start.getY(), start.getZ()));
		HalfEdge next = firstHalfEdge.getNext();
		while (next != null && next != firstHalfEdge) {
			start = next.getStart().getPoint();
			System.out.println(String.format("coords%d.push_back(new Coordinate3d(%f, %f, %f));", counter, start.getX(), start.getY(), start.getZ()));
			next = next.getNext();
		}
		System.out.println("PolygonNs* p" + counter + " = new PolygonNs(coords" + counter+ ");");
	}

	public static void printPolygon3d(CDPolygonNs edgePoly1, CDPolygonNs edgePoly2) {
		Locale.setDefault(Locale.US);
		Map<Point3d, String> pointMap = new IdentityHashMap<>();
		int startCounter = 1;
		startCounter = printPolygon3d(edgePoly1, pointMap, startCounter);
		printPolygon3d(edgePoly2, pointMap, startCounter);
	}
	
	public static void printGeoknechtPolygon(CDPolygonNs... polys) {
		Locale.setDefault(Locale.US);
		NumberFormat nf = NumberFormat.getNumberInstance();
		nf.setMaximumFractionDigits(30);
		for (CDPolygonNs poly : polys) {
			List<Coordinate3d> coordinates = poly.getCoordinates();
			System.out.print("polygon(");
			for (Coordinate3d coord : coordinates) {
				Point3d point = coord.getPoint();
				writeGeoknechtPoint(nf, point);
			}
			writeGeoknechtPoint(nf, coordinates.get(0).getPoint());
			System.out.println(")");
			for (List<HalfEdge> innerRing : poly.getInnerHalfEdges()) {
				System.out.print("polygon(");
				for (HalfEdge he : innerRing) {
					Point3d point = he.getStart().getPoint();
					writeGeoknechtPoint(nf, point);
				}
				writeGeoknechtPoint(nf, innerRing.get(innerRing.size() - 1).getEnd().getPoint());
				System.out.println(")");
			}
		}
	}
Matthias Betz's avatar
Matthias Betz committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
	
	public static void printGeoknechtPolygon(EdgePolygon... polys) {
		Locale.setDefault(Locale.US);
		NumberFormat nf = NumberFormat.getNumberInstance();
		nf.setMaximumFractionDigits(30);
		for (EdgePolygon poly : polys) {
			List<Coordinate3d> coordinates = poly.getCoordinates();
			System.out.print("polygon(");
			for (Coordinate3d coord : coordinates) {
				Point3d point = coord.getPoint();
				writeGeoknechtPoint(nf, point);
			}
			writeGeoknechtPoint(nf, coordinates.get(0).getPoint());
			System.out.println(")");
		}
	}
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

	private static void writeGeoknechtPoint(NumberFormat nf, Point3d point) {
		System.out.print(nf.format(point.getX()));
		System.out.print("|");
		System.out.print(nf.format(point.getY()));
		System.out.print("|");
		System.out.print(nf.format(point.getZ()));
		System.out.print(" ");
	}

	private static int printPolygon3d(CDPolygonNs p, Map<Point3d, String> pointMap, int startCounter) {
		NumberFormat nf = NumberFormat.getNumberInstance();
		nf.setMaximumFractionDigits(30);
		int counter = polygon3dCounter.getAndIncrement();
		int listCounter = coordListCounter.getAndIncrement();
		int firstListCounter = listCounter;
		HalfEdge firstHalfEdge = p.getFirstHalfEdge();
		int[] counterArr = new int[] {startCounter};
		System.out.println("Coordinate3dList coords" + firstListCounter + ";");
		writeRing(pointMap, nf, listCounter, firstHalfEdge, counterArr);
		for (List<HalfEdge> innerRing : p.getInnerHalfEdges()) {
			listCounter = coordListCounter.getAndIncrement();
			System.out.println("Coordinate3dList coords" + listCounter + ";");
			writeRing(pointMap, nf, listCounter, innerRing.get(0), counterArr);
		}
		int lastListCounter = coordListCounter.get();
		System.out.println("std::list<Coordinate3dList> coordLists" + counter + ";");
		for (int i = firstListCounter; i < lastListCounter; i++) {
			System.out.println("coordLists" + counter + ".push_back(coords" + i +");");
		}
		System.out.println("CDPolygonNs* p" + counter + " = new CDPolygonNs(coordLists" + counter+ ");");
		return counterArr[0];
	}

	private static void writeRing(Map<Point3d, String> pointMap, NumberFormat nf, int counter, HalfEdge firstHalfEdge,
			int[] counterArr) {
		final Point3d start = firstHalfEdge.getStart().getPoint();
		String coordVarName = getCoordName(pointMap, nf, start, counterArr);
		System.out.println(String.format("coords%d.push_back(%s);", counter, coordVarName));
		HalfEdge next = firstHalfEdge.getNext();
		while (next != null && next != firstHalfEdge) {
			Point3d nextPoint = next.getStart().getPoint();
			coordVarName = getCoordName(pointMap, nf, nextPoint, counterArr);

			System.out.println(String.format("coords%d.push_back(%s);", counter, coordVarName));
			next = next.getNext();
		}
	}

	private static String getCoordName(Map<Point3d, String> pointMap, NumberFormat nf, final Point3d start,
			int[] counterArr) {
		String coordVarName = pointMap.computeIfAbsent(start, k -> {
			System.out.println(String.format("Coordinate3d* c%d = new Coordinate3d(%s, %s, %s);", counterArr[0], nf.format(start.getX()), nf.format(start.getY()), nf.format(start.getZ())));
			String name = "c" + counterArr[0];
			counterArr[0]++;
			return name;
		});
		return coordVarName;
	}

Matthias Betz's avatar
Matthias Betz committed
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
	public static void printPolygon3d(EdgePolygon polygon1, EdgePolygon polygon2) {
		Locale.setDefault(Locale.US);
		Map<Point3d, String> pointMap = new IdentityHashMap<>();
		int startCounter = 1;
		startCounter = printPolygon3d(polygon1, pointMap, startCounter);
		printPolygon3d(polygon2, pointMap, startCounter);
	}
	
	private static int printPolygon3d(EdgePolygon p, Map<Point3d, String> pointMap, int startCounter) {
		NumberFormat nf = NumberFormat.getNumberInstance();
		nf.setMaximumFractionDigits(30);
		int counter = polygon3dCounter.getAndIncrement();
		int listCounter = coordListCounter.getAndIncrement();
		int firstListCounter = listCounter;
		HalfEdge firstHalfEdge = p.getFirstHalfEdge();
		int[] counterArr = new int[] {startCounter};
		System.out.println("Coordinate3dList coords" + firstListCounter + ";");
		writeRing(pointMap, nf, listCounter, firstHalfEdge, counterArr);
		System.out.println("CDPolygonNs* p" + counter + " = new CDPolygonNs(coords" + firstListCounter + ");");
		return counterArr[0];
	}

208
}