1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.geometry.partitioning;
23
24 import java.util.Formatter;
25 import java.util.Locale;
26
27 import org.hipparchus.geometry.Space;
28 import org.hipparchus.geometry.euclidean.oned.Euclidean1D;
29 import org.hipparchus.geometry.euclidean.oned.IntervalsSet;
30 import org.hipparchus.geometry.euclidean.oned.OrientedPoint;
31 import org.hipparchus.geometry.euclidean.oned.Vector1D;
32 import org.hipparchus.geometry.euclidean.threed.Euclidean3D;
33 import org.hipparchus.geometry.euclidean.threed.Plane;
34 import org.hipparchus.geometry.euclidean.threed.PolyhedronsSet;
35 import org.hipparchus.geometry.euclidean.threed.Vector3D;
36 import org.hipparchus.geometry.euclidean.twod.Euclidean2D;
37 import org.hipparchus.geometry.euclidean.twod.Line;
38 import org.hipparchus.geometry.euclidean.twod.PolygonsSet;
39 import org.hipparchus.geometry.euclidean.twod.Vector2D;
40 import org.hipparchus.geometry.spherical.oned.ArcsSet;
41 import org.hipparchus.geometry.spherical.oned.LimitAngle;
42 import org.hipparchus.geometry.spherical.oned.Sphere1D;
43 import org.hipparchus.geometry.spherical.twod.Circle;
44 import org.hipparchus.geometry.spherical.twod.Sphere2D;
45 import org.hipparchus.geometry.spherical.twod.SphericalPolygonsSet;
46
47
48
49
50
51
52
53 public class RegionDumper {
54
55
56
57 private RegionDumper() {
58 }
59
60
61
62
63
64 public static String dump(final ArcsSet arcsSet) {
65 final TreeDumper<Sphere1D> visitor = new TreeDumper<Sphere1D>("ArcsSet", arcsSet.getTolerance()) {
66
67
68 @Override
69 protected void formatHyperplane(final Hyperplane<Sphere1D> hyperplane) {
70 final LimitAngle h = (LimitAngle) hyperplane;
71 getFormatter().format("%22.15e %b %22.15e",
72 h.getLocation().getAlpha(), h.isDirect(), h.getTolerance());
73 }
74
75 };
76 arcsSet.getTree(false).visit(visitor);
77 return visitor.getDump();
78 }
79
80
81
82
83
84 public static String dump(final SphericalPolygonsSet sphericalPolygonsSet) {
85 final TreeDumper<Sphere2D> visitor = new TreeDumper<Sphere2D>("SphericalPolygonsSet", sphericalPolygonsSet.getTolerance()) {
86
87
88 @Override
89 protected void formatHyperplane(final Hyperplane<Sphere2D> hyperplane) {
90 final Circle h = (Circle) hyperplane;
91 getFormatter().format("%22.15e %22.15e %22.15e %22.15e",
92 h.getPole().getX(), h.getPole().getY(), h.getPole().getZ(),
93 h.getTolerance());
94 }
95
96 };
97 sphericalPolygonsSet.getTree(false).visit(visitor);
98 return visitor.getDump();
99 }
100
101
102
103
104
105 public static String dump(final IntervalsSet intervalsSet) {
106 final TreeDumper<Euclidean1D> visitor = new TreeDumper<Euclidean1D>("IntervalsSet", intervalsSet.getTolerance()) {
107
108
109 @Override
110 protected void formatHyperplane(final Hyperplane<Euclidean1D> hyperplane) {
111 final OrientedPoint h = (OrientedPoint) hyperplane;
112 getFormatter().format("%22.15e %b %22.15e",
113 h.getLocation().getX(), h.isDirect(), h.getTolerance());
114 }
115
116 };
117 intervalsSet.getTree(false).visit(visitor);
118 return visitor.getDump();
119 }
120
121
122
123
124
125 public static String dump(final PolygonsSet polygonsSet) {
126 final TreeDumper<Euclidean2D> visitor = new TreeDumper<Euclidean2D>("PolygonsSet", polygonsSet.getTolerance()) {
127
128
129 @Override
130 protected void formatHyperplane(final Hyperplane<Euclidean2D> hyperplane) {
131 final Line h = (Line) hyperplane;
132 final Vector2D p = h.toSpace(Vector1D.ZERO);
133 getFormatter().format("%22.15e %22.15e %22.15e %22.15e",
134 p.getX(), p.getY(), h.getAngle(), h.getTolerance());
135 }
136
137 };
138 polygonsSet.getTree(false).visit(visitor);
139 return visitor.getDump();
140 }
141
142
143
144
145
146 public static String dump(final PolyhedronsSet polyhedronsSet) {
147 final TreeDumper<Euclidean3D> visitor = new TreeDumper<Euclidean3D>("PolyhedronsSet", polyhedronsSet.getTolerance()) {
148
149
150 @Override
151 protected void formatHyperplane(final Hyperplane<Euclidean3D> hyperplane) {
152 final Plane h = (Plane) hyperplane;
153 final Vector3D p = h.toSpace(Vector2D.ZERO);
154 getFormatter().format("%22.15e %22.15e %22.15e %22.15e %22.15e %22.15e %22.15e",
155 p.getX(), p.getY(), p.getZ(),
156 h.getNormal().getX(), h.getNormal().getY(), h.getNormal().getZ(),
157 h.getTolerance());
158 }
159
160 };
161 polyhedronsSet.getTree(false).visit(visitor);
162 return visitor.getDump();
163 }
164
165
166
167
168 private abstract static class TreeDumper<S extends Space> implements BSPTreeVisitor<S> {
169
170
171 private final StringBuilder dump;
172
173
174 private final Formatter formatter;
175
176
177 private String prefix;
178
179
180
181
182
183 public TreeDumper(final String type, final double tolerance) {
184 this.dump = new StringBuilder();
185 this.formatter = new Formatter(dump, Locale.US);
186 this.prefix = "";
187 formatter.format("%s%n", type);
188 formatter.format("tolerance %22.15e%n", tolerance);
189 }
190
191
192
193
194 public String getDump() {
195 return dump.toString();
196 }
197
198
199
200
201 protected Formatter getFormatter() {
202 return formatter;
203 }
204
205
206
207
208 protected abstract void formatHyperplane(Hyperplane<S> hyperplane);
209
210
211 @Override
212 public Order visitOrder(final BSPTree<S> node) {
213 return Order.SUB_MINUS_PLUS;
214 }
215
216
217 @Override
218 public void visitInternalNode(final BSPTree<S> node) {
219 formatter.format("%s %s internal ", prefix, type(node));
220 formatHyperplane(node.getCut().getHyperplane());
221 formatter.format("%n");
222 prefix = prefix + " ";
223 }
224
225
226 @Override
227 public void visitLeafNode(final BSPTree<S> node) {
228 formatter.format("%s %s leaf %s%n",
229 prefix, type(node), node.getAttribute());
230 for (BSPTree<S> n = node;
231 n.getParent() != null && n == n.getParent().getPlus();
232 n = n.getParent()) {
233 prefix = prefix.substring(0, prefix.length() - 2);
234 }
235 }
236
237
238
239
240
241
242 private String type(final BSPTree<S> node) {
243 return (node.getParent() != null && node == node.getParent().getMinus()) ? "minus" : "plus ";
244 }
245
246 }
247
248 }