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.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  /** This class represents a sub-hyperplane for {@link Line}.
42   */
43  public class SubLine extends AbstractSubHyperplane<Euclidean2D, Euclidean1D> {
44  
45      /** Simple constructor.
46       * @param hyperplane underlying hyperplane
47       * @param remainingRegion remaining region of the hyperplane
48       */
49      public SubLine(final Hyperplane<Euclidean2D> hyperplane,
50                     final Region<Euclidean1D> remainingRegion) {
51          super(hyperplane, remainingRegion);
52      }
53  
54      /** Create a sub-line from two endpoints.
55       * @param start start point
56       * @param end end point
57       * @param tolerance tolerance below which points are considered identical
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      /** Create a sub-line from a segment.
64       * @param segment single segment forming the sub-line
65       */
66      public SubLine(final Segment segment) {
67          super(segment.getLine(),
68                buildIntervalSet(segment.getStart(), segment.getEnd(), segment.getLine().getTolerance()));
69      }
70  
71      /** Get the endpoints of the sub-line.
72       * <p>
73       * A subline may be any arbitrary number of disjoints segments, so the endpoints
74       * are provided as a list of endpoint pairs. Each element of the list represents
75       * one segment, and each segment contains a start point at index 0 and an end point
76       * at index 1. If the sub-line is unbounded in the negative infinity direction,
77       * the start point of the first segment will have infinite coordinates. If the
78       * sub-line is unbounded in the positive infinity direction, the end point of the
79       * last segment will have infinite coordinates. So a sub-line covering the whole
80       * line will contain just one row and both elements of this row will have infinite
81       * coordinates. If the sub-line is empty, the returned list will contain 0 segments.
82       * </p>
83       * @return list of segments endpoints
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     /** Get the intersection of the instance and another sub-line.
102      * <p>
103      * This method is related to the {@link Line#intersection(Line)
104      * intersection} method in the {@link Line Line} class, but in addition
105      * to compute the point along infinite lines, it also checks the point
106      * lies on both sub-line ranges.
107      * </p>
108      * @param subLine other sub-line which may intersect instance
109      * @param includeEndPoints if true, endpoints are considered to belong to
110      * instance (i.e. they are closed sets) and may be returned, otherwise endpoints
111      * are considered to not belong to instance (i.e. they are open sets) and intersection
112      * occurring on endpoints lead to null being returned
113      * @return the intersection point if there is one, null if the sub-lines don't intersect
114      */
115     public Vector2D intersection(final SubLine subLine, final boolean includeEndPoints) {
116 
117         // retrieve the underlying lines
118         Line line1 = (Line) getHyperplane();
119         Line line2 = (Line) subLine.getHyperplane();
120 
121         // compute the intersection on infinite line
122         Vector2D v2D = line1.intersection(line2);
123         if (v2D == null) {
124             return null;
125         }
126 
127         // check location of point with respect to first sub-line
128         Location loc1 = getRemainingRegion().checkPoint(line1.toSubSpace((Point<Euclidean2D>) v2D));
129 
130         // check location of point with respect to second sub-line
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     /** Build an interval set from two points.
142      * @param start start point
143      * @param end end point
144      * @param tolerance tolerance below which points are considered identical
145      * @return an interval set
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
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             // the lines are parallel
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         // the lines do intersect
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 }