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 */
017 package org.apache.commons.math3.geometry.euclidean.threed;
018
019 import java.util.ArrayList;
020 import java.util.List;
021
022 import org.apache.commons.math3.exception.MathIllegalArgumentException;
023 import org.apache.commons.math3.geometry.euclidean.oned.Interval;
024 import org.apache.commons.math3.geometry.euclidean.oned.IntervalsSet;
025 import org.apache.commons.math3.geometry.euclidean.oned.Vector1D;
026 import org.apache.commons.math3.geometry.partitioning.Region.Location;
027
028 /** This class represents a subset of a {@link Line}.
029 * @version $Id: SubLine.java 1416643 2012-12-03 19:37:14Z tn $
030 * @since 3.0
031 */
032 public class SubLine {
033
034 /** Underlying line. */
035 private final Line line;
036
037 /** Remaining region of the hyperplane. */
038 private final IntervalsSet remainingRegion;
039
040 /** Simple constructor.
041 * @param line underlying line
042 * @param remainingRegion remaining region of the line
043 */
044 public SubLine(final Line line, final IntervalsSet remainingRegion) {
045 this.line = line;
046 this.remainingRegion = remainingRegion;
047 }
048
049 /** Create a sub-line from two endpoints.
050 * @param start start point
051 * @param end end point
052 * @exception MathIllegalArgumentException if the points are equal
053 */
054 public SubLine(final Vector3D start, final Vector3D end)
055 throws MathIllegalArgumentException {
056 this(new Line(start, end), buildIntervalSet(start, end));
057 }
058
059 /** Create a sub-line from a segment.
060 * @param segment single segment forming the sub-line
061 * @exception MathIllegalArgumentException if the segment endpoints are equal
062 */
063 public SubLine(final Segment segment) throws MathIllegalArgumentException {
064 this(segment.getLine(), buildIntervalSet(segment.getStart(), segment.getEnd()));
065 }
066
067 /** Get the endpoints of the sub-line.
068 * <p>
069 * A subline may be any arbitrary number of disjoints segments, so the endpoints
070 * are provided as a list of endpoint pairs. Each element of the list represents
071 * one segment, and each segment contains a start point at index 0 and an end point
072 * at index 1. If the sub-line is unbounded in the negative infinity direction,
073 * the start point of the first segment will have infinite coordinates. If the
074 * sub-line is unbounded in the positive infinity direction, the end point of the
075 * last segment will have infinite coordinates. So a sub-line covering the whole
076 * line will contain just one row and both elements of this row will have infinite
077 * coordinates. If the sub-line is empty, the returned list will contain 0 segments.
078 * </p>
079 * @return list of segments endpoints
080 */
081 public List<Segment> getSegments() {
082
083 final List<Interval> list = remainingRegion.asList();
084 final List<Segment> segments = new ArrayList<Segment>();
085
086 for (final Interval interval : list) {
087 final Vector3D start = line.toSpace(new Vector1D(interval.getInf()));
088 final Vector3D end = line.toSpace(new Vector1D(interval.getSup()));
089 segments.add(new Segment(start, end, line));
090 }
091
092 return segments;
093
094 }
095
096 /** Get the intersection of the instance and another sub-line.
097 * <p>
098 * This method is related to the {@link Line#intersection(Line)
099 * intersection} method in the {@link Line Line} class, but in addition
100 * to compute the point along infinite lines, it also checks the point
101 * lies on both sub-line ranges.
102 * </p>
103 * @param subLine other sub-line which may intersect instance
104 * @param includeEndPoints if true, endpoints are considered to belong to
105 * instance (i.e. they are closed sets) and may be returned, otherwise endpoints
106 * are considered to not belong to instance (i.e. they are open sets) and intersection
107 * occurring on endpoints lead to null being returned
108 * @return the intersection point if there is one, null if the sub-lines don't intersect
109 */
110 public Vector3D intersection(final SubLine subLine, final boolean includeEndPoints) {
111
112 // compute the intersection on infinite line
113 Vector3D v1D = line.intersection(subLine.line);
114
115 // check location of point with respect to first sub-line
116 Location loc1 = remainingRegion.checkPoint(line.toSubSpace(v1D));
117
118 // check location of point with respect to second sub-line
119 Location loc2 = subLine.remainingRegion.checkPoint(subLine.line.toSubSpace(v1D));
120
121 if (includeEndPoints) {
122 return ((loc1 != Location.OUTSIDE) && (loc2 != Location.OUTSIDE)) ? v1D : null;
123 } else {
124 return ((loc1 == Location.INSIDE) && (loc2 == Location.INSIDE)) ? v1D : null;
125 }
126
127 }
128
129 /** Build an interval set from two points.
130 * @param start start point
131 * @param end end point
132 * @return an interval set
133 * @exception MathIllegalArgumentException if the points are equal
134 */
135 private static IntervalsSet buildIntervalSet(final Vector3D start, final Vector3D end)
136 throws MathIllegalArgumentException {
137 final Line line = new Line(start, end);
138 return new IntervalsSet(line.toSubSpace(start).getX(),
139 line.toSubSpace(end).getX());
140 }
141
142 }