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.core.partitioning;
18  
19  import org.apache.commons.geometry.core.Point;
20  import org.apache.commons.numbers.core.Precision;
21  
22  /** Base class for hyperplane implementations.
23   * @param <P> Point implementation type
24   */
25  public abstract class AbstractHyperplane<P extends Point<P>> implements Hyperplane<P> {
26      /** Precision object used to perform floating point comparisons. */
27      private final Precision.DoubleEquivalence precision;
28  
29      /** Construct an instance using the given precision context.
30       * @param precision object used to perform floating point comparisons
31       */
32      protected AbstractHyperplane(final Precision.DoubleEquivalence precision) {
33          this.precision = precision;
34      }
35  
36      /** {@inheritDoc} */
37      @Override
38      public HyperplaneLocation classify(final P point) {
39          final double offsetValue = offset(point);
40  
41          final double cmp = precision.signum(offsetValue);
42          if (cmp > 0) {
43              return HyperplaneLocation.PLUS;
44          } else if (cmp < 0) {
45              return HyperplaneLocation.MINUS;
46          }
47          return HyperplaneLocation.ON;
48      }
49  
50      /** {@inheritDoc} */
51      @Override
52      public boolean contains(final P point) {
53          final HyperplaneLocation loc = classify(point);
54          return loc == HyperplaneLocation.ON;
55      }
56  
57      /** Get the precision object used to perform floating point
58       * comparisons for this instance.
59       * @return the precision object for this instance
60       */
61      public Precision.DoubleEquivalence getPrecision() {
62          return precision;
63      }
64  }