View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
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  /** This class represents a sub-hyperplane for {@link Line}.
40   */
41  public class SubLine
42      extends AbstractSubHyperplane<Euclidean2D, Vector2D, Line, SubLine, Euclidean1D, Vector1D, OrientedPoint, SubOrientedPoint> {
43  
44      /** Simple constructor.
45       * @param hyperplane underlying hyperplane
46       * @param remainingRegion remaining region of the hyperplane
47       */
48      public SubLine(final Line hyperplane,
49                     final Region<Euclidean1D, Vector1D, OrientedPoint, SubOrientedPoint> remainingRegion) {
50          super(hyperplane, remainingRegion);
51      }
52  
53      /** Create a sub-line from two endpoints.
54       * @param start start point
55       * @param end end point
56       * @param tolerance tolerance below which points are considered identical
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      /** Create a sub-line from a segment.
63       * @param segment single segment forming the sub-line
64       */
65      public SubLine(final Segment segment) {
66          super(segment.getLine(),
67                buildIntervalSet(segment.getStart(), segment.getEnd(), segment.getLine().getTolerance()));
68      }
69  
70      /** Get the endpoints of the sub-line.
71       * <p>
72       * A subline may be any arbitrary number of disjoints segments, so the endpoints
73       * are provided as a list of endpoint pairs. Each element of the list represents
74       * one segment, and each segment contains a start point at index 0 and an end point
75       * at index 1. If the sub-line is unbounded in the negative infinity direction,
76       * the start point of the first segment will have infinite coordinates. If the
77       * sub-line is unbounded in the positive infinity direction, the end point of the
78       * last segment will have infinite coordinates. So a sub-line covering the whole
79       * line will contain just one row and both elements of this row will have infinite
80       * coordinates. If the sub-line is empty, the returned list will contain 0 segments.
81       * </p>
82       * @return list of segments endpoints
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      /** Get the intersection of the instance and another sub-line.
100      * <p>
101      * This method is related to the {@link Line#intersection(Line)
102      * intersection} method in the {@link Line Line} class, but in addition
103      * to compute the point along infinite lines, it also checks the point
104      * lies on both sub-line ranges.
105      * </p>
106      * @param subLine other sub-line which may intersect instance
107      * @param includeEndPoints if true, endpoints are considered to belong to
108      * instance (i.e. they are closed sets) and may be returned, otherwise endpoints
109      * are considered to not belong to instance (i.e. they are open sets) and intersection
110      * occurring on endpoints lead to null being returned
111      * @return the intersection point if there is one, null if the sub-lines don't intersect
112      */
113     public Vector2D intersection(final SubLine subLine, final boolean includeEndPoints) {
114 
115         // retrieve the underlying lines
116         Line line1 = getHyperplane();
117         Line line2 = subLine.getHyperplane();
118 
119         // compute the intersection on infinite line
120         Vector2D v2D = line1.intersection(line2);
121         if (v2D == null) {
122             return null;
123         }
124 
125         // check location of point with respect to first sub-line
126         Location loc1 = getRemainingRegion().checkPoint(line1.toSubSpace(v2D));
127 
128         // check location of point with respect to second sub-line
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     /** Build an interval set from two points.
140      * @param start start point
141      * @param end end point
142      * @param tolerance tolerance below which points are considered identical
143      * @return an interval set
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
160     @Override
161     public Vector2D getInteriorPoint() {
162         final Vector1D v = getRemainingRegion().getInteriorPoint();
163         return isEmpty() ? null : getHyperplane().toSpace(v);
164     }
165 
166     /** {@inheritDoc} */
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             // the lines are parallel
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         // the lines do intersect
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 }