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.euclidean.twod;
23
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import org.hipparchus.geometry.Point;
28 import org.hipparchus.geometry.euclidean.oned.Euclidean1D;
29 import org.hipparchus.geometry.euclidean.oned.Interval;
30 import org.hipparchus.geometry.euclidean.oned.IntervalsSet;
31 import org.hipparchus.geometry.euclidean.oned.OrientedPoint;
32 import org.hipparchus.geometry.euclidean.oned.Vector1D;
33 import org.hipparchus.geometry.partitioning.AbstractSubHyperplane;
34 import org.hipparchus.geometry.partitioning.BSPTree;
35 import org.hipparchus.geometry.partitioning.Hyperplane;
36 import org.hipparchus.geometry.partitioning.Region;
37 import org.hipparchus.geometry.partitioning.SubHyperplane;
38 import org.hipparchus.geometry.partitioning.Region.Location;
39 import org.hipparchus.util.FastMath;
40
41
42
43 public class SubLine extends AbstractSubHyperplane<Euclidean2D, Euclidean1D> {
44
45
46
47
48
49 public SubLine(final Hyperplane<Euclidean2D> hyperplane,
50 final Region<Euclidean1D> remainingRegion) {
51 super(hyperplane, remainingRegion);
52 }
53
54
55
56
57
58
59 public SubLine(final Vector2D start, final Vector2D end, final double tolerance) {
60 super(new Line(start, end, tolerance), buildIntervalSet(start, end, tolerance));
61 }
62
63
64
65
66 public SubLine(final Segment segment) {
67 super(segment.getLine(),
68 buildIntervalSet(segment.getStart(), segment.getEnd(), segment.getLine().getTolerance()));
69 }
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 public List<Segment> getSegments() {
86
87 final Line line = (Line) getHyperplane();
88 final List<Interval> list = ((IntervalsSet) getRemainingRegion()).asList();
89 final List<Segment> segments = new ArrayList<>(list.size());
90
91 for (final Interval interval : list) {
92 final Vector2D start = line.toSpace((Point<Euclidean1D>) new Vector1D(interval.getInf()));
93 final Vector2D end = line.toSpace((Point<Euclidean1D>) new Vector1D(interval.getSup()));
94 segments.add(new Segment(start, end, line));
95 }
96
97 return segments;
98
99 }
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115 public Vector2D intersection(final SubLine subLine, final boolean includeEndPoints) {
116
117
118 Line line1 = (Line) getHyperplane();
119 Line line2 = (Line) subLine.getHyperplane();
120
121
122 Vector2D v2D = line1.intersection(line2);
123 if (v2D == null) {
124 return null;
125 }
126
127
128 Location loc1 = getRemainingRegion().checkPoint(line1.toSubSpace((Point<Euclidean2D>) v2D));
129
130
131 Location loc2 = subLine.getRemainingRegion().checkPoint(line2.toSubSpace((Point<Euclidean2D>) v2D));
132
133 if (includeEndPoints) {
134 return ((loc1 != Location.OUTSIDE) && (loc2 != Location.OUTSIDE)) ? v2D : null;
135 } else {
136 return ((loc1 == Location.INSIDE) && (loc2 == Location.INSIDE)) ? v2D : null;
137 }
138
139 }
140
141
142
143
144
145
146
147 private static IntervalsSet buildIntervalSet(final Vector2D start, final Vector2D end, final double tolerance) {
148 final Line line = new Line(start, end, tolerance);
149 return new IntervalsSet(line.toSubSpace((Point<Euclidean2D>) start).getX(),
150 line.toSubSpace((Point<Euclidean2D>) end).getX(),
151 tolerance);
152 }
153
154
155 @Override
156 protected AbstractSubHyperplane<Euclidean2D, Euclidean1D> buildNew(final Hyperplane<Euclidean2D> hyperplane,
157 final Region<Euclidean1D> remainingRegion) {
158 return new SubLine(hyperplane, remainingRegion);
159 }
160
161
162 @Override
163 public SplitSubHyperplane<Euclidean2D> split(final Hyperplane<Euclidean2D> hyperplane) {
164
165 final Line thisLine = (Line) getHyperplane();
166 final Line otherLine = (Line) hyperplane;
167 final Vector2D crossing = thisLine.intersection(otherLine);
168 final double tolerance = thisLine.getTolerance();
169
170 if (crossing == null) {
171
172 final double global = otherLine.getOffset(thisLine);
173 if (global < -tolerance) {
174 return new SplitSubHyperplane<Euclidean2D>(null, this);
175 } else if (global > tolerance) {
176 return new SplitSubHyperplane<Euclidean2D>(this, null);
177 } else {
178 return new SplitSubHyperplane<Euclidean2D>(null, null);
179 }
180 }
181
182
183 final boolean direct = FastMath.sin(thisLine.getAngle() - otherLine.getAngle()) < 0;
184 final Vector1D x = thisLine.toSubSpace((Point<Euclidean2D>) crossing);
185 final SubHyperplane<Euclidean1D> subPlus =
186 new OrientedPoint(x, !direct, tolerance).wholeHyperplane();
187 final SubHyperplane<Euclidean1D> subMinus =
188 new OrientedPoint(x, direct, tolerance).wholeHyperplane();
189
190 final BSPTree<Euclidean1D> splitTree = getRemainingRegion().getTree(false).split(subMinus);
191 final BSPTree<Euclidean1D> plusTree = getRemainingRegion().isEmpty(splitTree.getPlus()) ?
192 new BSPTree<>(Boolean.FALSE) :
193 new BSPTree<>(subPlus, new BSPTree<>(Boolean.FALSE),
194 splitTree.getPlus(), null);
195 final BSPTree<Euclidean1D> minusTree = getRemainingRegion().isEmpty(splitTree.getMinus()) ?
196 new BSPTree<>(Boolean.FALSE) :
197 new BSPTree<>(subMinus, new BSPTree<>(Boolean.FALSE),
198 splitTree.getMinus(), null);
199 return new SplitSubHyperplane<>(new SubLine(thisLine.copySelf(), new IntervalsSet(plusTree, tolerance)),
200 new SubLine(thisLine.copySelf(), new IntervalsSet(minusTree, tolerance)));
201
202 }
203
204 }