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.math3.geometry.spherical.twod;
018
019import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;
020import org.apache.commons.math3.geometry.partitioning.AbstractSubHyperplane;
021import org.apache.commons.math3.geometry.partitioning.Hyperplane;
022import org.apache.commons.math3.geometry.partitioning.Region;
023import org.apache.commons.math3.geometry.spherical.oned.Arc;
024import org.apache.commons.math3.geometry.spherical.oned.ArcsSet;
025import org.apache.commons.math3.geometry.spherical.oned.Sphere1D;
026import org.apache.commons.math3.util.FastMath;
027
028/** This class represents a sub-hyperplane for {@link Circle}.
029 * @since 3.3
030 */
031public class SubCircle extends AbstractSubHyperplane<Sphere2D, Sphere1D> {
032
033    /** Simple constructor.
034     * @param hyperplane underlying hyperplane
035     * @param remainingRegion remaining region of the hyperplane
036     */
037    public SubCircle(final Hyperplane<Sphere2D> hyperplane,
038                     final Region<Sphere1D> remainingRegion) {
039        super(hyperplane, remainingRegion);
040    }
041
042    /** {@inheritDoc} */
043    @Override
044    protected AbstractSubHyperplane<Sphere2D, Sphere1D> buildNew(final Hyperplane<Sphere2D> hyperplane,
045                                                                 final Region<Sphere1D> remainingRegion) {
046        return new SubCircle(hyperplane, remainingRegion);
047    }
048
049    /** {@inheritDoc} */
050    @Override
051    public SplitSubHyperplane<Sphere2D> split(final Hyperplane<Sphere2D> hyperplane) {
052
053        final Circle thisCircle   = (Circle) getHyperplane();
054        final Circle otherCircle  = (Circle) hyperplane;
055        final double angle = Vector3D.angle(thisCircle.getPole(), otherCircle.getPole());
056
057        if (angle < thisCircle.getTolerance() || angle > FastMath.PI - thisCircle.getTolerance()) {
058            // the two circles are aligned or opposite
059            return new SplitSubHyperplane<Sphere2D>(null, null);
060        } else {
061            // the two circles intersect each other
062            final Arc    arc          = thisCircle.getInsideArc(otherCircle);
063            final ArcsSet.Split split = ((ArcsSet) getRemainingRegion()).split(arc);
064            final ArcsSet plus        = split.getPlus();
065            final ArcsSet minus       = split.getMinus();
066            return new SplitSubHyperplane<Sphere2D>(plus  == null ? null : new SubCircle(thisCircle.copySelf(), plus),
067                                                    minus == null ? null : new SubCircle(thisCircle.copySelf(), minus));
068        }
069
070    }
071
072}