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