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.partitioning;
018
019import org.apache.commons.math3.geometry.Space;
020
021/** This interface represents the remaining parts of an hyperplane after
022 * other parts have been chopped off.
023
024 * <p>sub-hyperplanes are obtained when parts of an {@link
025 * Hyperplane hyperplane} are chopped off by other hyperplanes that
026 * intersect it. The remaining part is a convex region. Such objects
027 * appear in {@link BSPTree BSP trees} as the intersection of a cut
028 * hyperplane with the convex region which it splits, the chopping
029 * hyperplanes are the cut hyperplanes closer to the tree root.</p>
030
031 * <p>
032 * Note that this interface is <em>not</em> intended to be implemented
033 * by Apache Commons Math users, it is only intended to be implemented
034 * within the library itself. New methods may be added even for minor
035 * versions, which breaks compatibility for external implementations.
036 * </p>
037
038 * @param <S> Type of the embedding space.
039
040 * @since 3.0
041 */
042public interface SubHyperplane<S extends Space> {
043
044    /** Copy the instance.
045     * <p>The instance created is completely independent of the original
046     * one. A deep copy is used, none of the underlying objects are
047     * shared (except for the nodes attributes and immutable
048     * objects).</p>
049     * @return a new sub-hyperplane, copy of the instance
050     */
051    SubHyperplane<S> copySelf();
052
053    /** Get the underlying hyperplane.
054     * @return underlying hyperplane
055     */
056    Hyperplane<S> getHyperplane();
057
058    /** Check if the instance is empty.
059     * @return true if the instance is empty
060     */
061    boolean isEmpty();
062
063    /** Get the size of the instance.
064     * @return the size of the instance (this is a length in 1D, an area
065     * in 2D, a volume in 3D ...)
066     */
067    double getSize();
068
069    /** Compute the relative position of the instance with respect
070     * to an hyperplane.
071     * @param hyperplane hyperplane to check instance against
072     * @return one of {@link Side#PLUS}, {@link Side#MINUS}, {@link Side#BOTH},
073     * {@link Side#HYPER}
074     */
075    Side side(Hyperplane<S> hyperplane);
076
077    /** Split the instance in two parts by an hyperplane.
078     * @param hyperplane splitting hyperplane
079     * @return an object containing both the part of the instance
080     * on the plus side of the hyperplane and the part of the
081     * instance on the minus side of the hyperplane
082     */
083    SplitSubHyperplane<S> split(Hyperplane<S> hyperplane);
084
085    /** Compute the union of the instance and another sub-hyperplane.
086     * @param other other sub-hyperplane to union (<em>must</em> be in the
087     * same hyperplane as the instance)
088     * @return a new sub-hyperplane, union of the instance and other
089     */
090    SubHyperplane<S> reunite(SubHyperplane<S> other);
091
092    /** Class holding the results of the {@link #split split} method.
093     * @param <U> Type of the embedding space.
094     */
095    public static class SplitSubHyperplane<U extends Space> {
096
097        /** Part of the sub-hyperplane on the plus side of the splitting hyperplane. */
098        private final SubHyperplane<U> plus;
099
100        /** Part of the sub-hyperplane on the minus side of the splitting hyperplane. */
101        private final SubHyperplane<U> minus;
102
103        /** Build a SplitSubHyperplane from its parts.
104         * @param plus part of the sub-hyperplane on the plus side of the
105         * splitting hyperplane
106         * @param minus part of the sub-hyperplane on the minus side of the
107         * splitting hyperplane
108         */
109        public SplitSubHyperplane(final SubHyperplane<U> plus,
110                                  final SubHyperplane<U> minus) {
111            this.plus  = plus;
112            this.minus = minus;
113        }
114
115        /** Get the part of the sub-hyperplane on the plus side of the splitting hyperplane.
116         * @return part of the sub-hyperplane on the plus side of the splitting hyperplane
117         */
118        public SubHyperplane<U> getPlus() {
119            return plus;
120        }
121
122        /** Get the part of the sub-hyperplane on the minus side of the splitting hyperplane.
123         * @return part of the sub-hyperplane on the minus side of the splitting hyperplane
124         */
125        public SubHyperplane<U> getMinus() {
126            return minus;
127        }
128
129    }
130
131}