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.spherical.twod;
018
019import java.util.List;
020
021import org.apache.commons.geometry.core.RegionEmbedding;
022import org.apache.commons.geometry.core.RegionLocation;
023import org.apache.commons.geometry.core.internal.HyperplaneSubsets;
024import org.apache.commons.geometry.core.partitioning.HyperplaneBoundedRegion;
025import org.apache.commons.geometry.core.partitioning.HyperplaneSubset;
026import org.apache.commons.geometry.spherical.oned.Point1S;
027import org.apache.commons.numbers.core.Precision;
028
029/** Class representing a subset of the points in a great circle.
030 * @see GreatCircles
031 */
032public abstract class GreatCircleSubset implements HyperplaneSubset<Point2S>, RegionEmbedding<Point2S, Point1S> {
033    /** The great circle defining this instance. */
034    private final GreatCircle circle;
035
036    /** Simple constructor.
037     * @param circle great circle defining this instance
038     */
039    GreatCircleSubset(final GreatCircle circle) {
040        this.circle = circle;
041    }
042
043    /** Get the great circle defining this instance.
044     * @return the great circle defining this instance
045     * @see #getHyperplane()
046     */
047    public GreatCircle getCircle() {
048        return circle;
049    }
050
051    /** {@inheritDoc} */
052    @Override
053    public GreatCircle getHyperplane() {
054        return getCircle();
055    }
056
057    /** {@inheritDoc} */
058    @Override
059    public Point1S toSubspace(final Point2S pt) {
060        return circle.toSubspace(pt);
061    }
062
063    /** {@inheritDoc} */
064    @Override
065    public Point2S toSpace(final Point1S pt) {
066        return circle.toSpace(pt);
067    }
068
069    /** {@inheritDoc} */
070    @Override
071    public boolean isFull() {
072        return getSubspaceRegion().isFull();
073    }
074
075    /** {@inheritDoc} */
076    @Override
077    public boolean isEmpty() {
078        return getSubspaceRegion().isEmpty();
079    }
080
081    /** {@inheritDoc} */
082    @Override
083    public double getSize() {
084        return getSubspaceRegion().getSize();
085    }
086
087    /** {@inheritDoc} */
088    @Override
089    public Point2S getCentroid() {
090        final Point1S subspaceCentroid = getSubspaceRegion().getCentroid();
091        if (subspaceCentroid != null) {
092            return getCircle().toSpace(subspaceCentroid);
093        }
094        return null;
095    }
096
097    /** {@inheritDoc} */
098    @Override
099    public RegionLocation classify(final Point2S pt) {
100        return HyperplaneSubsets.classifyAgainstEmbeddedRegion(pt, circle, getSubspaceRegion());
101    }
102
103    /** {@inheritDoc} */
104    @Override
105    public Point2S closest(final Point2S pt) {
106        return HyperplaneSubsets.closestToEmbeddedRegion(pt, circle, getSubspaceRegion());
107    }
108
109    /** {@inheritDoc} */
110    @Override
111    public abstract List<GreatArc> toConvex();
112
113    /** {@inheritDoc} */
114    @Override
115    public abstract HyperplaneBoundedRegion<Point1S> getSubspaceRegion();
116
117    /** Return the object used to perform floating point comparisons, which is the
118     * same object used by the underlying {@link GreatCircle}.
119     * @return precision object used to perform floating point comparisons.
120     */
121    public Precision.DoubleEquivalence getPrecision() {
122        return circle.getPrecision();
123    }
124}