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