OrientedPoints.java

  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. import org.apache.commons.numbers.core.Precision;

  19. /** Class containing factory methods for constructing {@link OrientedPoint} instances.
  20.  */
  21. public final class OrientedPoints {

  22.     /** Utility class; no instantiation. */
  23.     private OrientedPoints() {
  24.     }

  25.     /** Create a new instance from the given location and boolean direction value.
  26.      * @param location the location of the hyperplane
  27.      * @param positiveFacing if true, the hyperplane will face toward positive infinity;
  28.      *      otherwise, it will point toward negative infinity.
  29.      * @param precision precision context used to compare floating point values
  30.      * @return a new instance
  31.      */
  32.     public static OrientedPoint fromLocationAndDirection(final double location, final boolean positiveFacing,
  33.             final Precision.DoubleEquivalence precision) {
  34.         return fromPointAndDirection(Vector1D.of(location), positiveFacing, precision);
  35.     }

  36.     /** Create a new instance from the given point and boolean direction value.
  37.      * @param point the location of the hyperplane
  38.      * @param positiveFacing if true, the hyperplane will face toward positive infinity;
  39.      *      otherwise, it will point toward negative infinity.
  40.      * @param precision precision context used to compare floating point values
  41.      * @return a new instance
  42.      */
  43.     public static OrientedPoint fromPointAndDirection(final Vector1D point, final boolean positiveFacing,
  44.             final Precision.DoubleEquivalence precision) {
  45.         return new OrientedPoint(point, positiveFacing, precision);
  46.     }

  47.     /** Create a new instance from the given point and direction.
  48.      * @param point the location of the hyperplane
  49.      * @param direction the direction of the plus side of the hyperplane
  50.      * @param precision precision context used to compare floating point values
  51.      * @return a new instance oriented in the given direction
  52.      * @throws IllegalArgumentException if the direction is zero as evaluated by the
  53.      *      given precision context
  54.      */
  55.     public static OrientedPoint fromPointAndDirection(final Vector1D point, final Vector1D direction,
  56.             final Precision.DoubleEquivalence precision) {
  57.         if (direction.isZero(precision)) {
  58.             throw new IllegalArgumentException("Oriented point direction cannot be zero");
  59.         }

  60.         final boolean positiveFacing = direction.getX() > 0;

  61.         return new OrientedPoint(point, positiveFacing, precision);
  62.     }

  63.     /** Create a new instance at the given point, oriented so that it is facing positive infinity.
  64.      * @param point the location of the hyperplane
  65.      * @param precision precision context used to compare floating point values
  66.      * @return a new instance oriented toward positive infinity
  67.      */
  68.     public static OrientedPoint createPositiveFacing(final Vector1D point,
  69.             final Precision.DoubleEquivalence precision) {
  70.         return new OrientedPoint(point, true, precision);
  71.     }

  72.     /** Create a new instance at the given location, oriented so that it is facing positive infinity.
  73.      * @param location 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 double location,
  78.             final Precision.DoubleEquivalence precision) {
  79.         return new OrientedPoint(Vector1D.of(location), true, precision);
  80.     }

  81.     /** Create a new instance at the given point, oriented so that it is facing negative infinity.
  82.      * @param point the location of the hyperplane
  83.      * @param precision precision context used to compare floating point values
  84.      * @return a new instance oriented toward negative infinity
  85.      */
  86.     public static OrientedPoint createNegativeFacing(final Vector1D point,
  87.             final Precision.DoubleEquivalence precision) {
  88.         return new OrientedPoint(point, false, precision);
  89.     }

  90.     /** Create a new instance at the given location, oriented so that it is facing negative infinity.
  91.      * @param location 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 double location,
  96.             final Precision.DoubleEquivalence precision) {
  97.         return new OrientedPoint(Vector1D.of(location), false, precision);
  98.     }
  99. }