summaryrefslogtreecommitdiff
path: root/src/net/sourceforge/plantuml/graph2/InflationTransform2.java
blob: 97bda854629410d497e20260dd8321f7c872ba78 (plain)
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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* ========================================================================
 * PlantUML : a free UML diagram generator
 * ========================================================================
 *
 * (C) Copyright 2009-2017, Arnaud Roques
 *
 * Project Info:  http://plantuml.com
 * 
 * If you like this project or if you find it useful, you can support us at:
 * 
 * http://plantuml.com/patreon (only 1$ per month!)
 * http://plantuml.com/paypal
 * 
 * This file is part of PlantUML.
 *
 * PlantUML is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * PlantUML 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 General Public
 * License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA.
 *
 *
 * Original Author:  Arnaud Roques
 * 
 *
 */
package net.sourceforge.plantuml.graph2;

import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.ListIterator;
import java.util.SortedSet;
import java.util.TreeSet;

class Point2DComparatorDistance implements Comparator<Point2D> {

	private final Point2D center;

	public Point2DComparatorDistance(Point2D center) {
		this.center = center;
	}

	public int compare(Point2D p1, Point2D p2) {
		return Double.compare(p1.distance(center), p2.distance(center));
	}

}

public class InflationTransform2 implements IInflationTransform {

	private final List<InflateData2> inflateX = new ArrayList<InflateData2>();
	private final List<InflateData2> inflateY = new ArrayList<InflateData2>();

	public void addInflationX(double xpos, double inflation) {
		add(inflateX, xpos, inflation);
	}

	@Override
	public String toString() {
		return "inflateX = " + inflateX + " inflateY = " + inflateY;
	}

	public void addInflationY(double ypos, double inflation) {
		add(inflateY, ypos, inflation);
	}

	public double getTotalInflationX() {
		return sumInflation(inflateX);
	}

	public double getTotalInflationY() {
		return sumInflation(inflateY);
	}

	static private double sumInflation(List<InflateData2> list) {
		double result = 0;
		for (InflateData2 data : list) {
			result += data.getInflation();
		}
		return result;
	}

	static private void add(List<InflateData2> list, double ypos, double inflation) {
		for (final ListIterator<InflateData2> it = list.listIterator(); it.hasNext();) {
			final InflateData2 cur = it.next();
			if (cur.getPos() == ypos) {
				it.set(new InflateData2(ypos, Math.max(inflation, cur.getInflation())));
				return;
			}
		}
		list.add(new InflateData2(ypos, inflation));
		Collections.sort(list);
	}

	Collection<Point2D.Double> cutPoints(Line2D.Double original) {

		final SortedSet<Point2D.Double> result = new TreeSet<Point2D.Double>(new Point2DComparatorDistance(original
				.getP1()));

		if (GeomUtils.isHorizontal(original) == false) {
			for (InflateData2 x : inflateX) {
				final Line2D.Double vertical = new Line2D.Double(x.getPos(), GeomUtils.getMinY(original), x.getPos(),
						GeomUtils.getMaxY(original));
				final Point2D.Double inter = GeomUtils.getSegIntersection(original, vertical);
				if (inter != null) {
					result.add(inter);
				}
			}
		}
		if (GeomUtils.isVertical(original) == false) {
			for (InflateData2 y : inflateY) {
				final Line2D.Double horizontal = new Line2D.Double(GeomUtils.getMinX(original), y.getPos(), GeomUtils
						.getMaxX(original), y.getPos());
				final Point2D.Double inter = GeomUtils.getSegIntersection(original, horizontal);
				if (inter != null) {
					result.add(inter);
				}
			}
		}
		return result;
	}

	Collection<Line2D.Double> cutSegments(Line2D.Double original) {
		final List<Line2D.Double> result = new ArrayList<Line2D.Double>();
		Point2D.Double cur = (Point2D.Double) original.getP1();
		final Collection<Point2D.Double> cutPoints = cutPoints(original);
		for (Point2D.Double inter : cutPoints) {
			if (cur.equals(inter)) {
				continue;
			}
			result.add(new Line2D.Double(cur, inter));
			cur = inter;
		}
		if (cur.equals(original.getP2()) == false) {
			result.add(new Line2D.Double(cur, original.getP2()));
		}
		return result;
	}

	Collection<Line2D.Double> cutSegments(Collection<Line2D.Double> segments) {
		final List<Line2D.Double> result = new ArrayList<Line2D.Double>();
		for (Line2D.Double seg : segments) {
			result.addAll(cutSegments(seg));
		}
		return result;
	}

	// private Line2D.Double inflateSegment(Line2D.Double seg) {
	// // if (isOnGrid(seg.getP1()) && isOnGrid(seg.getP2())) {
	// // return new Line2D.Double(inflatePoint2D(seg.getP1()),
	// inflatePoint2D(seg.getP2()));
	// // }
	// // for (InflateData2 x : inflateX) {
	// // seg = x.inflateXAlpha(seg);
	// // }
	// // for (InflateData2 y : inflateY) {
	// // seg = y.inflateYAlpha(seg);
	// // }
	// // return seg;
	// return new Line2D.Double(inflatePoint2D(seg.getP1()),
	// inflatePoint2D(seg.getP2()));
	// }

	// private boolean isOnGrid(Point2D point) {
	// boolean onGrid = false;
	// for (InflateData2 x : inflateX) {
	// if (point.getX() == x.getPos()) {
	// onGrid = true;
	// }
	// }
	// if (onGrid == false) {
	// return false;
	// }
	// for (InflateData2 y : inflateY) {
	// if (point.getY() == y.getPos()) {
	// return true;
	// }
	// }
	// return false;
	//
	// }

	public Point2D inflatePoint2D(Point2D point) {
		return getAffineTransformAt(point).transform(point, null);
	}

	AffineTransform getAffineTransformAt(Point2D point) {
		double deltaX = 0;
		for (InflateData2 x : inflateX) {
			deltaX += x.inflateAt(point.getX());
		}
		double deltaY = 0;
		for (InflateData2 y : inflateY) {
			deltaY += y.inflateAt(point.getY());
		}
		return AffineTransform.getTranslateInstance(deltaX, deltaY);
	}

	List<Line2D.Double> inflateSegmentCollection(Collection<Line2D.Double> segments) {
		final List<Line2D.Double> result = new ArrayList<Line2D.Double>();
		for (Line2D.Double seg : segments) {
			final AffineTransform at = getAffineTransformAt(new Point2D.Double((seg.x1 + seg.x2) / 2,
					(seg.y1 + seg.y2) / 2));
			result.add(new Line2D.Double(at.transform(seg.getP1(), null), at.transform(seg.getP2(), null)));
		}
		return result;
	}

	public List<Line2D.Double> inflate(Collection<Line2D.Double> segments) {
		final Collection<Line2D.Double> cutSegments = cutSegments(segments);
		Line2D.Double last = null;
		final List<Line2D.Double> inflated = inflateSegmentCollection(cutSegments);
		final List<Line2D.Double> result = new ArrayList<Line2D.Double>();
		for (Line2D.Double seg : inflated) {
			if (last != null && last.getP2().equals(seg.getP1()) == false) {
				result.add(new Line2D.Double(last.getP2(), seg.getP1()));
			}
			result.add(seg);
			last = seg;

		}
		return result;
	}

}