LineConvexSubset.java

  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.  *      http://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. package org.apache.commons.geometry.euclidean.twod;

  18. import java.util.Collections;
  19. import java.util.List;

  20. import org.apache.commons.geometry.core.Transform;
  21. import org.apache.commons.geometry.core.partitioning.Hyperplane;
  22. import org.apache.commons.geometry.core.partitioning.HyperplaneConvexSubset;
  23. import org.apache.commons.geometry.core.partitioning.Split;
  24. import org.apache.commons.geometry.euclidean.oned.Interval;

  25. /** Class representing a convex subset of a line in 2D Euclidean space. Instances
  26.  * need not be finite, in which case the start or end point (or both) will be null.
  27.  * Line segments and rays are examples of convex line subsets.
  28.  * @see Lines
  29.  */
  30. public abstract class LineConvexSubset extends LineSubset implements HyperplaneConvexSubset<Vector2D> {

  31.     /** Construct a new instance for the given line.
  32.      * @param line line containing this line subset
  33.      */
  34.     LineConvexSubset(final Line line) {
  35.         super(line);
  36.     }

  37.     /** {@inheritDoc} */
  38.     @Override
  39.     public List<LineConvexSubset> toConvex() {
  40.         return Collections.singletonList(this);
  41.     }

  42.     /** {@inheritDoc}
  43.      *
  44.      * <p>This method always returns {@code false}.</p>
  45.      */
  46.     @Override
  47.     public boolean isEmpty() {
  48.         return false;
  49.     }

  50.     /** Get the start point for the subset.
  51.      * @return the start point for the subset, or null if no start point exists
  52.      */
  53.     public abstract Vector2D getStartPoint();

  54.     /** Get the 1D start location of the subset or {@link Double#NEGATIVE_INFINITY} if
  55.      * no start location exists.
  56.      * @return the 1D start location of the subset or {@link Double#NEGATIVE_INFINITY} if
  57.      *      no start location exists.
  58.      */
  59.     public abstract double getSubspaceStart();

  60.     /** Get the end point for the subset.
  61.      * @return the end point for the subset, or null if no end point exists.
  62.      */
  63.     public abstract Vector2D getEndPoint();

  64.     /** Get the 1D end location of the subset or {@link Double#POSITIVE_INFINITY} if
  65.      * no end location exists.
  66.      * @return the 1D end location of the subset or {@link Double#POSITIVE_INFINITY} if
  67.      *      no end location exists
  68.      */
  69.     public abstract double getSubspaceEnd();

  70.     /** {@inheritDoc} */
  71.     @Override
  72.     public Interval getSubspaceRegion() {
  73.         final double start = getSubspaceStart();
  74.         final double end = getSubspaceEnd();

  75.         return Interval.of(start, end, getPrecision());
  76.     }

  77.     /** Get the 1D interval for the region. This method is an alias for {@link #getSubspaceRegion()}.
  78.      * @return the 1D interval for the region.
  79.      */
  80.     public Interval getInterval() {
  81.         return getSubspaceRegion();
  82.     }

  83.     /** {@inheritDoc} */
  84.     @Override
  85.     public Split<LineConvexSubset> split(final Hyperplane<Vector2D> splitter) {
  86.         final Line thisLine = getLine();
  87.         final Line splitterLine = (Line) splitter;

  88.         final Vector2D intersection = splitterLine.intersection(thisLine);
  89.         if (intersection == null) {
  90.             return getNonIntersectingSplitResult(splitterLine, this);
  91.         }
  92.         return splitOnIntersection(splitterLine, intersection);
  93.     }

  94.     /** {@inheritDoc} */
  95.     @Override
  96.     public Vector2D closest(final Vector2D pt) {
  97.         final Line line = getLine();
  98.         final double abscissa = line.abscissa(pt);

  99.         return line.toSpace(closestAbscissa(abscissa));
  100.     }

  101.     /** {@inheritDoc} */
  102.     @Override
  103.     public abstract LineConvexSubset transform(Transform<Vector2D> transform);

  104.     /** {@inheritDoc} */
  105.     @Override
  106.     public abstract LineConvexSubset reverse();

  107.     /** Get the closest value in the subspace region to the given abscissa.
  108.      * @param abscissa input abscissa
  109.      * @return the closest value in the subspace region to the given abscissa
  110.      */
  111.     abstract double closestAbscissa(double abscissa);

  112.     /** Split this instance using the given splitter line and intersection point.
  113.      * @param splitter splitter line
  114.      * @param intersection intersection point between the splitter line and the line
  115.      *      for this instance
  116.      * @return the result of splitting this instance with the given splitter line and intersection
  117.      *      point
  118.      */
  119.     abstract Split<LineConvexSubset> splitOnIntersection(Line splitter, Vector2D intersection);
  120. }