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