001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.math3.geometry.euclidean.twod;
018
019import java.util.ArrayList;
020import java.util.List;
021
022import org.apache.commons.math3.geometry.Point;
023import org.apache.commons.math3.geometry.euclidean.oned.Euclidean1D;
024import org.apache.commons.math3.geometry.euclidean.oned.Interval;
025import org.apache.commons.math3.geometry.euclidean.oned.IntervalsSet;
026import org.apache.commons.math3.geometry.euclidean.oned.OrientedPoint;
027import org.apache.commons.math3.geometry.euclidean.oned.Vector1D;
028import org.apache.commons.math3.geometry.partitioning.AbstractSubHyperplane;
029import org.apache.commons.math3.geometry.partitioning.BSPTree;
030import org.apache.commons.math3.geometry.partitioning.Hyperplane;
031import org.apache.commons.math3.geometry.partitioning.Region;
032import org.apache.commons.math3.geometry.partitioning.Region.Location;
033import org.apache.commons.math3.geometry.partitioning.SubHyperplane;
034import org.apache.commons.math3.util.FastMath;
035
036/** This class represents a sub-hyperplane for {@link Line}.
037 * @since 3.0
038 */
039public class SubLine extends AbstractSubHyperplane<Euclidean2D, Euclidean1D> {
040
041    /** Default value for tolerance. */
042    private static final double DEFAULT_TOLERANCE = 1.0e-10;
043
044    /** Simple constructor.
045     * @param hyperplane underlying hyperplane
046     * @param remainingRegion remaining region of the hyperplane
047     */
048    public SubLine(final Hyperplane<Euclidean2D> hyperplane,
049                   final Region<Euclidean1D> remainingRegion) {
050        super(hyperplane, remainingRegion);
051    }
052
053    /** Create a sub-line from two endpoints.
054     * @param start start point
055     * @param end end point
056     * @param tolerance tolerance below which points are considered identical
057     * @since 3.3
058     */
059    public SubLine(final Vector2D start, final Vector2D end, final double tolerance) {
060        super(new Line(start, end, tolerance), buildIntervalSet(start, end, tolerance));
061    }
062
063    /** Create a sub-line from two endpoints.
064     * @param start start point
065     * @param end end point
066     * @deprecated as of 3.3, replaced with {@link #SubLine(Vector2D, Vector2D, double)}
067     */
068    @Deprecated
069    public SubLine(final Vector2D start, final Vector2D end) {
070        this(start, end, DEFAULT_TOLERANCE);
071    }
072
073    /** Create a sub-line from a segment.
074     * @param segment single segment forming the sub-line
075     */
076    public SubLine(final Segment segment) {
077        super(segment.getLine(),
078              buildIntervalSet(segment.getStart(), segment.getEnd(), segment.getLine().getTolerance()));
079    }
080
081    /** Get the endpoints of the sub-line.
082     * <p>
083     * A subline may be any arbitrary number of disjoints segments, so the endpoints
084     * are provided as a list of endpoint pairs. Each element of the list represents
085     * one segment, and each segment contains a start point at index 0 and an end point
086     * at index 1. If the sub-line is unbounded in the negative infinity direction,
087     * the start point of the first segment will have infinite coordinates. If the
088     * sub-line is unbounded in the positive infinity direction, the end point of the
089     * last segment will have infinite coordinates. So a sub-line covering the whole
090     * line will contain just one row and both elements of this row will have infinite
091     * coordinates. If the sub-line is empty, the returned list will contain 0 segments.
092     * </p>
093     * @return list of segments endpoints
094     */
095    public List<Segment> getSegments() {
096
097        final Line line = (Line) getHyperplane();
098        final List<Interval> list = ((IntervalsSet) getRemainingRegion()).asList();
099        final List<Segment> segments = new ArrayList<Segment>(list.size());
100
101        for (final Interval interval : list) {
102            final Vector2D start = line.toSpace((Point<Euclidean1D>) new Vector1D(interval.getInf()));
103            final Vector2D end   = line.toSpace((Point<Euclidean1D>) new Vector1D(interval.getSup()));
104            segments.add(new Segment(start, end, line));
105        }
106
107        return segments;
108
109    }
110
111    /** Get the intersection of the instance and another sub-line.
112     * <p>
113     * This method is related to the {@link Line#intersection(Line)
114     * intersection} method in the {@link Line Line} class, but in addition
115     * to compute the point along infinite lines, it also checks the point
116     * lies on both sub-line ranges.
117     * </p>
118     * @param subLine other sub-line which may intersect instance
119     * @param includeEndPoints if true, endpoints are considered to belong to
120     * instance (i.e. they are closed sets) and may be returned, otherwise endpoints
121     * are considered to not belong to instance (i.e. they are open sets) and intersection
122     * occurring on endpoints lead to null being returned
123     * @return the intersection point if there is one, null if the sub-lines don't intersect
124     */
125    public Vector2D intersection(final SubLine subLine, final boolean includeEndPoints) {
126
127        // retrieve the underlying lines
128        Line line1 = (Line) getHyperplane();
129        Line line2 = (Line) subLine.getHyperplane();
130
131        // compute the intersection on infinite line
132        Vector2D v2D = line1.intersection(line2);
133        if (v2D == null) {
134            return null;
135        }
136
137        // check location of point with respect to first sub-line
138        Location loc1 = getRemainingRegion().checkPoint(line1.toSubSpace((Point<Euclidean2D>) v2D));
139
140        // check location of point with respect to second sub-line
141        Location loc2 = subLine.getRemainingRegion().checkPoint(line2.toSubSpace((Point<Euclidean2D>) v2D));
142
143        if (includeEndPoints) {
144            return ((loc1 != Location.OUTSIDE) && (loc2 != Location.OUTSIDE)) ? v2D : null;
145        } else {
146            return ((loc1 == Location.INSIDE) && (loc2 == Location.INSIDE)) ? v2D : null;
147        }
148
149    }
150
151    /** Build an interval set from two points.
152     * @param start start point
153     * @param end end point
154     * @param tolerance tolerance below which points are considered identical
155     * @return an interval set
156     */
157    private static IntervalsSet buildIntervalSet(final Vector2D start, final Vector2D end, final double tolerance) {
158        final Line line = new Line(start, end, tolerance);
159        return new IntervalsSet(line.toSubSpace((Point<Euclidean2D>) start).getX(),
160                                line.toSubSpace((Point<Euclidean2D>) end).getX(),
161                                tolerance);
162    }
163
164    /** {@inheritDoc} */
165    @Override
166    protected AbstractSubHyperplane<Euclidean2D, Euclidean1D> buildNew(final Hyperplane<Euclidean2D> hyperplane,
167                                                                       final Region<Euclidean1D> remainingRegion) {
168        return new SubLine(hyperplane, remainingRegion);
169    }
170
171    /** {@inheritDoc} */
172    @Override
173    public SplitSubHyperplane<Euclidean2D> split(final Hyperplane<Euclidean2D> hyperplane) {
174
175        final Line    thisLine  = (Line) getHyperplane();
176        final Line    otherLine = (Line) hyperplane;
177        final Vector2D crossing = thisLine.intersection(otherLine);
178        final double tolerance  = thisLine.getTolerance();
179
180        if (crossing == null) {
181            // the lines are parallel
182            final double global = otherLine.getOffset(thisLine);
183            if (global < -tolerance) {
184                return new SplitSubHyperplane<Euclidean2D>(null, this);
185            } else if (global > tolerance) {
186                return new SplitSubHyperplane<Euclidean2D>(this, null);
187            } else {
188                return new SplitSubHyperplane<Euclidean2D>(null, null);
189            }
190        }
191
192        // the lines do intersect
193        final boolean direct = FastMath.sin(thisLine.getAngle() - otherLine.getAngle()) < 0;
194        final Vector1D x      = thisLine.toSubSpace((Point<Euclidean2D>) crossing);
195        final SubHyperplane<Euclidean1D> subPlus  =
196                new OrientedPoint(x, !direct, tolerance).wholeHyperplane();
197        final SubHyperplane<Euclidean1D> subMinus =
198                new OrientedPoint(x,  direct, tolerance).wholeHyperplane();
199
200        final BSPTree<Euclidean1D> splitTree = getRemainingRegion().getTree(false).split(subMinus);
201        final BSPTree<Euclidean1D> plusTree  = getRemainingRegion().isEmpty(splitTree.getPlus()) ?
202                                               new BSPTree<Euclidean1D>(Boolean.FALSE) :
203                                               new BSPTree<Euclidean1D>(subPlus, new BSPTree<Euclidean1D>(Boolean.FALSE),
204                                                                        splitTree.getPlus(), null);
205        final BSPTree<Euclidean1D> minusTree = getRemainingRegion().isEmpty(splitTree.getMinus()) ?
206                                               new BSPTree<Euclidean1D>(Boolean.FALSE) :
207                                               new BSPTree<Euclidean1D>(subMinus, new BSPTree<Euclidean1D>(Boolean.FALSE),
208                                                                        splitTree.getMinus(), null);
209        return new SplitSubHyperplane<Euclidean2D>(new SubLine(thisLine.copySelf(), new IntervalsSet(plusTree, tolerance)),
210                                                   new SubLine(thisLine.copySelf(), new IntervalsSet(minusTree, tolerance)));
211
212    }
213
214}