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.oned;
    18
    19  import org.apache.commons.numbers.core.Precision;
    20
    21  /** Class containing factory methods for constructing {@link OrientedPoint} instances.
    22   */
    23  public final class OrientedPoints {
    24
    25      /** Utility class; no instantiation. */
    26      private OrientedPoints() {
    27      }
    28
    29      /** Create a new instance from the given location and boolean direction value.
    30       * @param location the location of the hyperplane
    31       * @param positiveFacing if true, the hyperplane will face toward positive infinity;
    32       *      otherwise, it will point toward negative infinity.
    33       * @param precision precision context used to compare floating point values
    34       * @return a new instance
    35       */
    36      public static OrientedPoint fromLocationAndDirection(final double location, final boolean positiveFacing,
    37              final Precision.DoubleEquivalence precision) {
    38          return fromPointAndDirection(Vector1D.of(location), positiveFacing, precision);
    39      }
    40
    41      /** Create a new instance from the given point and boolean direction value.
    42       * @param point the location of the hyperplane
    43       * @param positiveFacing if true, the hyperplane will face toward positive infinity;
    44       *      otherwise, it will point toward negative infinity.
    45       * @param precision precision context used to compare floating point values
    46       * @return a new instance
    47       */
    48      public static OrientedPoint fromPointAndDirection(final Vector1D point, final boolean positiveFacing,
    49              final Precision.DoubleEquivalence precision) {
    50          return new OrientedPoint(point, positiveFacing, precision);
    51      }
    52
    53      /** Create a new instance from the given point and direction.
    54       * @param point the location of the hyperplane
    55       * @param direction the direction of the plus side of the hyperplane
    56       * @param precision precision context used to compare floating point values
    57       * @return a new instance oriented in the given direction
    58       * @throws IllegalArgumentException if the direction is zero as evaluated by the
    59       *      given precision context
    60       */
    61      public static OrientedPoint fromPointAndDirection(final Vector1D point, final Vector1D direction,
    62              final Precision.DoubleEquivalence precision) {
    63          if (direction.isZero(precision)) {
    64              throw new IllegalArgumentException("Oriented point direction cannot be zero");
    65          }
    66
    67          final boolean positiveFacing = direction.getX() > 0;
    68
    69          return new OrientedPoint(point, positiveFacing, precision);
    70      }
    71
    72      /** Create a new instance at the given point, oriented so that it is facing positive infinity.
    73       * @param point the location of the hyperplane
    74       * @param precision precision context used to compare floating point values
    75       * @return a new instance oriented toward positive infinity
    76       */
    77      public static OrientedPoint createPositiveFacing(final Vector1D point,
    78              final Precision.DoubleEquivalence precision) {
    79          return new OrientedPoint(point, true, precision);
    80      }
    81
    82      /** Create a new instance at the given location, oriented so that it is facing positive infinity.
    83       * @param location the location of the hyperplane
    84       * @param precision precision context used to compare floating point values
    85       * @return a new instance oriented toward positive infinity
    86       */
    87      public static OrientedPoint createPositiveFacing(final double location,
    88              final Precision.DoubleEquivalence precision) {
    89          return new OrientedPoint(Vector1D.of(location), true, precision);
    90      }
    91
    92      /** Create a new instance at the given point, oriented so that it is facing negative infinity.
    93       * @param point the location of the hyperplane
    94       * @param precision precision context used to compare floating point values
    95       * @return a new instance oriented toward negative infinity
    96       */
    97      public static OrientedPoint createNegativeFacing(final Vector1D point,
    98              final Precision.DoubleEquivalence precision) {
    99          return new OrientedPoint(point, false, precision);
    100     }
    101
    102     /** Create a new instance at the given location, oriented so that it is facing negative infinity.
    103      * @param location the location of the hyperplane
    104      * @param precision precision context used to compare floating point values
    105      * @return a new instance oriented toward negative infinity
    106      */
    107     public static OrientedPoint createNegativeFacing(final double location,
    108             final Precision.DoubleEquivalence precision) {
    109         return new OrientedPoint(Vector1D.of(location), false, precision);
    110     }
    111 }