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.geometry.core.precision.DoublePrecisionContext;
    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 DoublePrecisionContext 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 DoublePrecisionContext 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 DoublePrecisionContext 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, final DoublePrecisionContext precision) {
    78          return new OrientedPoint(point, true, precision);
    79      }
    80
    81      /** Create a new instance at the given location, oriented so that it is facing positive infinity.
    82       * @param location the location of the hyperplane
    83       * @param precision precision context used to compare floating point values
    84       * @return a new instance oriented toward positive infinity
    85       */
    86      public static OrientedPoint createPositiveFacing(final double location, final DoublePrecisionContext precision) {
    87          return new OrientedPoint(Vector1D.of(location), true, precision);
    88      }
    89
    90      /** Create a new instance at the given point, oriented so that it is facing negative infinity.
    91       * @param point the location of the hyperplane
    92       * @param precision precision context used to compare floating point values
    93       * @return a new instance oriented toward negative infinity
    94       */
    95      public static OrientedPoint createNegativeFacing(final Vector1D point, final DoublePrecisionContext precision) {
    96          return new OrientedPoint(point, false, precision);
    97      }
    98
    99      /** 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 }