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.geometry.euclidean.twod;
018
019import java.util.ArrayList;
020import java.util.Comparator;
021import java.util.List;
022import java.util.ListIterator;
023
024import org.apache.commons.geometry.euclidean.AbstractLinecastPoint;
025import org.apache.commons.numbers.core.Precision;
026
027/** Class representing intersections resulting from linecast operations in Euclidean
028 * 2D space. This class contains the intersection point along with the boundary normal
029 * of the target at the point of intersection.
030 * @see Linecastable2D
031 */
032public class LinecastPoint2D extends AbstractLinecastPoint<Vector2D, Vector2D.Unit, Line> {
033
034    /** Comparator that sorts intersection instances by increasing abscissa order. If two abscissa
035     * values are equal, the comparison uses {@link Vector2D#COORDINATE_ASCENDING_ORDER} with the
036     * intersection normals.
037     */
038    public static final Comparator<LinecastPoint2D> ABSCISSA_ORDER = (a, b) -> {
039        int cmp = Double.compare(a.getAbscissa(), b.getAbscissa());
040        if (cmp == 0) {
041            cmp = Vector2D.COORDINATE_ASCENDING_ORDER.compare(a.getNormal(), b.getNormal());
042        }
043        return cmp;
044    };
045
046    /** Construct a new instance from its components.
047     * @param point the linecast intersection point
048     * @param normal the surface of the linecast target at the intersection point
049     * @param line intersecting line
050     */
051    public LinecastPoint2D(final Vector2D point, final Vector2D normal, final Line line) {
052        super(point, normal.normalize(), line);
053    }
054
055    /** Return true if this instance should be considered equivalent to the argument, using the
056     * given precision context for comparison. Instances are considered equivalent if they have equivalent
057     * points, normals, and lines.
058     * @param other point to compare with
059     * @param precision precision context to use for the comparison
060     * @return true if this instance should be considered equivalent to the argument
061     */
062    public boolean eq(final LinecastPoint2D other, final Precision.DoubleEquivalence precision) {
063        return getPoint().eq(other.getPoint(), precision) &&
064                getNormal().eq(other.getNormal(), precision);
065    }
066
067    /** Sort the given list of linecast points by increasing abscissa value and filter to remove
068     * duplicate entries (as determined by the {@link #eq(LinecastPoint2D, Precision.DoubleEquivalence)} method).
069     * The argument is modified.
070     * @param pts list of points to sort and filter
071     */
072    public static void sortAndFilter(final List<LinecastPoint2D> pts) {
073        pts.sort(ABSCISSA_ORDER);
074
075        double currentAbscissa = Double.POSITIVE_INFINITY;
076        final List<LinecastPoint2D> abscissaList = new ArrayList<>();
077
078        final ListIterator<LinecastPoint2D> it = pts.listIterator();
079        LinecastPoint2D pt;
080        while (it.hasNext()) {
081            pt = it.next();
082            if (!pt.getLine().getPrecision().eq(currentAbscissa, pt.getAbscissa())) {
083                // new abscissa value
084                currentAbscissa = pt.getAbscissa();
085                abscissaList.clear();
086
087                abscissaList.add(pt);
088            } else if (containsEq(pt, abscissaList)) {
089                // duplicate found for this abscissa value
090                it.remove();
091            } else {
092                // not a duplicate
093                abscissaList.add(pt);
094            }
095        }
096    }
097
098    /** Return true if the given linecast point is equivalent to any of those in the given list.
099     * @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<? extends LinecastPoint2D> list) {
104        final Precision.DoubleEquivalence 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}