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    *      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
    19  import java.util.ArrayList;
    20  import java.util.Comparator;
    21  import java.util.List;
    22  import java.util.ListIterator;
    23
    24  import org.apache.commons.geometry.core.precision.DoublePrecisionContext;
    25  import org.apache.commons.geometry.euclidean.AbstractLinecastPoint;
    26
    27  /** Class representing intersections resulting from linecast operations in Euclidean
    28   * 2D space. This class contains the intersection point along with the boundary normal
    29   * of the target at the point of intersection.
    30   * @see Linecastable2D
    31   */
    32  public class LinecastPoint2D extends AbstractLinecastPoint<Vector2D, Vector2D.Unit, Line> {
    33
    34      /** Comparator that sorts intersection instances by increasing abscissa order. If two abscissa
    35       * values are equal, the comparison uses {@link Vector2D#COORDINATE_ASCENDING_ORDER} with the
    36       * intersection normals.
    37       */
    38      public static final Comparator<LinecastPoint2D> ABSCISSA_ORDER = (a, b) -> {
    39          int cmp = Double.compare(a.getAbscissa(), b.getAbscissa());
    40          if (cmp == 0) {
    41              cmp = Vector2D.COORDINATE_ASCENDING_ORDER.compare(a.getNormal(), b.getNormal());
    42          }
    43          return cmp;
    44      };
    45
    46      /** Construct a new instance from its components.
    47       * @param point the linecast intersection point
    48       * @param normal the surface of the linecast target at the intersection point
    49       * @param line intersecting line
    50       */
    51      public LinecastPoint2D(final Vector2D point, final Vector2D normal, final Line line) {
    52          super(point, normal.normalize(), line);
    53      }
    54
    55      /** Return true if this instance should be considered equivalent to the argument, using the
    56       * given precision context for comparison. Instances are considered equivalent if they have equivalent
    57       * points, normals, and lines.
    58       * @param other point to compare with
    59       * @param precision precision context to use for the comparison
    60       * @return true if this instance should be considered equivalent to the argument
    61       */
    62      public boolean eq(final LinecastPoint2D other, final DoublePrecisionContext precision) {
    63          return getPoint().eq(other.getPoint(), precision) &&
    64                  getNormal().eq(other.getNormal(), precision);
    65      }
    66
    67      /** Sort the given list of linecast points by increasing abscissa value and filter to remove
    68       * duplicate entries (as determined by the {@link #eq(LinecastPoint2D, DoublePrecisionContext)} method).
    69       * The argument is modified.
    70       * @param pts list of points to sort and filter
    71       */
    72      public static void sortAndFilter(final List<LinecastPoint2D> pts) {
    73          pts.sort(ABSCISSA_ORDER);
    74
    75          double currentAbscissa = Double.POSITIVE_INFINITY;
    76          final List<LinecastPoint2D> abscissaList = new ArrayList<>();
    77
    78          final ListIterator<LinecastPoint2D> it = pts.listIterator();
    79          LinecastPoint2D pt;
    80          while (it.hasNext()) {
    81              pt = it.next();
    82              if (!pt.getLine().getPrecision().eq(currentAbscissa, pt.getAbscissa())) {
    83                  // new abscissa value
    84                  currentAbscissa = pt.getAbscissa();
    85                  abscissaList.clear();
    86
    87                  abscissaList.add(pt);
    88              } else if (containsEq(pt, abscissaList)) {
    89                  // duplicate found for this abscissa value
    90                  it.remove();
    91              } else {
    92                  // not a duplicate
    93                  abscissaList.add(pt);
    94              }
    95          }
    96      }
    97
    98      /** Return true if the given linecast point is equivalent to any of those in the given list.
    99       * @param pt point to test
    100      * @param list list to test against
    101      * @return true if the given linecast point is equivalent to any of those in the given list
    102      */
    103     private static boolean containsEq(final LinecastPoint2D pt, final List<LinecastPoint2D> list) {
    104         final DoublePrecisionContext precision = pt.getLine().getPrecision();
    105
    106         for (final LinecastPoint2D listPt : list) {
    107             if (listPt.eq(pt, precision)) {
    108                 return true;
    109             }
    110         }
    111
    112         return false;
    113     }
    114 }