AbstractHyperplane.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.core.partitioning;

  18. import org.apache.commons.geometry.core.Point;
  19. import org.apache.commons.numbers.core.Precision;

  20. /** Base class for hyperplane implementations.
  21.  * @param <P> Point implementation type
  22.  */
  23. public abstract class AbstractHyperplane<P extends Point<P>> implements Hyperplane<P> {
  24.     /** Precision object used to perform floating point comparisons. */
  25.     private final Precision.DoubleEquivalence precision;

  26.     /** Construct an instance using the given precision context.
  27.      * @param precision object used to perform floating point comparisons
  28.      */
  29.     protected AbstractHyperplane(final Precision.DoubleEquivalence precision) {
  30.         this.precision = precision;
  31.     }

  32.     /** {@inheritDoc} */
  33.     @Override
  34.     public HyperplaneLocation classify(final P point) {
  35.         final double offsetValue = offset(point);

  36.         final double cmp = precision.signum(offsetValue);
  37.         if (cmp > 0) {
  38.             return HyperplaneLocation.PLUS;
  39.         } else if (cmp < 0) {
  40.             return HyperplaneLocation.MINUS;
  41.         }
  42.         return HyperplaneLocation.ON;
  43.     }

  44.     /** {@inheritDoc} */
  45.     @Override
  46.     public boolean contains(final P point) {
  47.         final HyperplaneLocation loc = classify(point);
  48.         return loc == HyperplaneLocation.ON;
  49.     }

  50.     /** Get the precision object used to perform floating point
  51.      * comparisons for this instance.
  52.      * @return the precision object for this instance
  53.      */
  54.     public Precision.DoubleEquivalence getPrecision() {
  55.         return precision;
  56.     }
  57. }