1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.geometry.spherical.twod;
18
19 import java.util.List;
20 import java.util.stream.Collectors;
21
22 import org.apache.commons.geometry.core.Transform;
23 import org.apache.commons.geometry.core.partitioning.Hyperplane;
24 import org.apache.commons.geometry.core.partitioning.Split;
25 import org.apache.commons.geometry.core.partitioning.SplitLocation;
26 import org.apache.commons.geometry.spherical.oned.CutAngle;
27 import org.apache.commons.geometry.spherical.oned.CutAngles;
28 import org.apache.commons.geometry.spherical.oned.RegionBSPTree1S;
29
30
31
32
33
34
35 public final class EmbeddedTreeGreatCircleSubset extends GreatCircleSubset {
36
37 private final RegionBSPTree1S region;
38
39
40
41
42 public EmbeddedTreeGreatCircleSubset(final GreatCircle greatCircle) {
43 this(greatCircle, false);
44 }
45
46
47
48
49
50
51
52
53 public EmbeddedTreeGreatCircleSubset(final GreatCircle circle, final boolean full) {
54 this(circle, new RegionBSPTree1S(full));
55 }
56
57
58
59
60
61 public EmbeddedTreeGreatCircleSubset(final GreatCircle circle, final RegionBSPTree1S region) {
62 super(circle);
63
64 this.region = region;
65 }
66
67
68 @Override
69 public RegionBSPTree1S getSubspaceRegion() {
70 return region;
71 }
72
73
74 @Override
75 public EmbeddedTreeGreatCircleSubset transform(final Transform<Point2S> transform) {
76 final GreatCircle circle = getCircle().transform(transform);
77
78 return new EmbeddedTreeGreatCircleSubset(circle, region.copy());
79 }
80
81
82 @Override
83 public List<GreatArc> toConvex() {
84 return region.toIntervals().stream()
85 .flatMap(i -> i.toConvex().stream())
86 .map(i -> GreatCircles.arcFromInterval(getCircle(), i))
87 .collect(Collectors.toList());
88 }
89
90
91
92
93
94
95
96
97
98
99
100 @Override
101 public Split<EmbeddedTreeGreatCircleSubset> split(final Hyperplane<Point2S> splitter) {
102
103 final GreatCircle splitterCircle = (GreatCircle) splitter;
104 final GreatCircle thisCircle = getCircle();
105
106 final Point2S intersection = splitterCircle.intersection(thisCircle);
107
108 EmbeddedTreeGreatCircleSubset minus = null;
109 EmbeddedTreeGreatCircleSubset plus = null;
110
111 if (intersection != null) {
112 final CutAngle subSplitter = CutAngles.createPositiveFacing(
113 thisCircle.toSubspace(intersection), splitterCircle.getPrecision());
114
115 final Split<RegionBSPTree1S> subSplit = region.splitDiameter(subSplitter);
116 final SplitLocation subLoc = subSplit.getLocation();
117
118 if (subLoc == SplitLocation.MINUS) {
119 minus = this;
120 } else if (subLoc == SplitLocation.PLUS) {
121 plus = this;
122 } else if (subLoc == SplitLocation.BOTH) {
123 minus = new EmbeddedTreeGreatCircleSubset(thisCircle, subSplit.getMinus());
124 plus = new EmbeddedTreeGreatCircleSubset(thisCircle, subSplit.getPlus());
125 }
126 }
127
128 return new Split<>(minus, plus);
129 }
130
131
132
133
134
135
136 public void add(final GreatArc arc) {
137 GreatCircles.validateGreatCirclesEquivalent(getCircle(), arc.getCircle());
138
139 region.add(arc.getSubspaceRegion());
140 }
141
142
143
144
145
146
147
148 public void add(final EmbeddedTreeGreatCircleSubset subcircle) {
149 GreatCircles.validateGreatCirclesEquivalent(getCircle(), subcircle.getCircle());
150
151 region.union(subcircle.getSubspaceRegion());
152 }
153
154
155 @Override
156 public String toString() {
157 final StringBuilder sb = new StringBuilder();
158 sb.append(this.getClass().getSimpleName())
159 .append('[')
160 .append("circle= ")
161 .append(getCircle())
162 .append(", region= ")
163 .append(region)
164 .append(']');
165
166 return sb.toString();
167 }
168 }