001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.geometry.core.partitioning;
018
019import org.apache.commons.geometry.core.Point;
020import org.apache.commons.numbers.core.Precision;
021
022/** Base class for hyperplane implementations.
023 * @param <P> Point implementation type
024 */
025public abstract class AbstractHyperplane<P extends Point<P>> implements Hyperplane<P> {
026    /** Precision object used to perform floating point comparisons. */
027    private final Precision.DoubleEquivalence precision;
028
029    /** Construct an instance using the given precision context.
030     * @param precision object used to perform floating point comparisons
031     */
032    protected AbstractHyperplane(final Precision.DoubleEquivalence precision) {
033        this.precision = precision;
034    }
035
036    /** {@inheritDoc} */
037    @Override
038    public HyperplaneLocation classify(final P point) {
039        final double offsetValue = offset(point);
040
041        final double cmp = precision.signum(offsetValue);
042        if (cmp > 0) {
043            return HyperplaneLocation.PLUS;
044        } else if (cmp < 0) {
045            return HyperplaneLocation.MINUS;
046        }
047        return HyperplaneLocation.ON;
048    }
049
050    /** {@inheritDoc} */
051    @Override
052    public boolean contains(final P point) {
053        final HyperplaneLocation loc = classify(point);
054        return loc == HyperplaneLocation.ON;
055    }
056
057    /** Get the precision object used to perform floating point
058     * comparisons for this instance.
059     * @return the precision object for this instance
060     */
061    public Precision.DoubleEquivalence getPrecision() {
062        return precision;
063    }
064}