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.oned;
018
019import org.apache.commons.geometry.core.precision.DoublePrecisionContext;
020
021/** Class containing factory methods for constructing {@link OrientedPoint} instances.
022 */
023public final class OrientedPoints {
024
025    /** Utility class; no instantiation. */
026    private OrientedPoints() {
027    }
028
029    /** Create a new instance from the given location and boolean direction value.
030     * @param location the location of the hyperplane
031     * @param positiveFacing if true, the hyperplane will face toward positive infinity;
032     *      otherwise, it will point toward negative infinity.
033     * @param precision precision context used to compare floating point values
034     * @return a new instance
035     */
036    public static OrientedPoint fromLocationAndDirection(final double location, final boolean positiveFacing,
037            final DoublePrecisionContext precision) {
038        return fromPointAndDirection(Vector1D.of(location), positiveFacing, precision);
039    }
040
041    /** Create a new instance from the given point and boolean direction value.
042     * @param point the location of the hyperplane
043     * @param positiveFacing if true, the hyperplane will face toward positive infinity;
044     *      otherwise, it will point toward negative infinity.
045     * @param precision precision context used to compare floating point values
046     * @return a new instance
047     */
048    public static OrientedPoint fromPointAndDirection(final Vector1D point, final boolean positiveFacing,
049            final DoublePrecisionContext precision) {
050        return new OrientedPoint(point, positiveFacing, precision);
051    }
052
053    /** Create a new instance from the given point and direction.
054     * @param point the location of the hyperplane
055     * @param direction the direction of the plus side of the hyperplane
056     * @param precision precision context used to compare floating point values
057     * @return a new instance oriented in the given direction
058     * @throws IllegalArgumentException if the direction is zero as evaluated by the
059     *      given precision context
060     */
061    public static OrientedPoint fromPointAndDirection(final Vector1D point, final Vector1D direction,
062            final DoublePrecisionContext precision) {
063        if (direction.isZero(precision)) {
064            throw new IllegalArgumentException("Oriented point direction cannot be zero");
065        }
066
067        final boolean positiveFacing = direction.getX() > 0;
068
069        return new OrientedPoint(point, positiveFacing, precision);
070    }
071
072    /** Create a new instance at the given point, oriented so that it is facing positive infinity.
073     * @param point the location of the hyperplane
074     * @param precision precision context used to compare floating point values
075     * @return a new instance oriented toward positive infinity
076     */
077    public static OrientedPoint createPositiveFacing(final Vector1D point, final DoublePrecisionContext precision) {
078        return new OrientedPoint(point, true, precision);
079    }
080
081    /** Create a new instance at the given location, oriented so that it is facing positive infinity.
082     * @param location the location of the hyperplane
083     * @param precision precision context used to compare floating point values
084     * @return a new instance oriented toward positive infinity
085     */
086    public static OrientedPoint createPositiveFacing(final double location, final DoublePrecisionContext precision) {
087        return new OrientedPoint(Vector1D.of(location), true, precision);
088    }
089
090    /** Create a new instance at the given point, oriented so that it is facing negative infinity.
091     * @param point the location of the hyperplane
092     * @param precision precision context used to compare floating point values
093     * @return a new instance oriented toward negative infinity
094     */
095    public static OrientedPoint createNegativeFacing(final Vector1D point, final DoublePrecisionContext precision) {
096        return new OrientedPoint(point, false, precision);
097    }
098
099    /** Create a new instance at the given location, oriented so that it is facing negative infinity.
100     * @param location the location of the hyperplane
101     * @param precision precision context used to compare floating point values
102     * @return a new instance oriented toward negative infinity
103     */
104    public static OrientedPoint createNegativeFacing(final double location, final DoublePrecisionContext precision) {
105        return new OrientedPoint(Vector1D.of(location), false, precision);
106    }
107}