HyperplaneSubsets.java

  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.internal;

  18. import org.apache.commons.geometry.core.Point;
  19. import org.apache.commons.geometry.core.Region;
  20. import org.apache.commons.geometry.core.RegionLocation;
  21. import org.apache.commons.geometry.core.partitioning.EmbeddingHyperplane;

  22. /** Utility methods for {@link org.apache.commons.geometry.core.partitioning.HyperplaneSubset}
  23.  * implementations.
  24.  */
  25. public final class HyperplaneSubsets {

  26.     /** Utility class; no instantiation. */
  27.     private HyperplaneSubsets() {
  28.     }

  29.     /** Classify a point against a region embedded in a hyperplane.
  30.      * @param <P> Point implementation class
  31.      * @param <S> Subspace point implementation class
  32.      * @param <H> Hyperplane implementation class
  33.      * @param <R> Region implementation class
  34.      * @param pt the point to classify
  35.      * @param hyperplane hyperplane containing the embedded region
  36.      * @param embeddedRegion embedded region to classify against
  37.      * @return the region location of the given point
  38.      */
  39.     public static <
  40.         P extends Point<P>,
  41.         S extends Point<S>,
  42.         H extends EmbeddingHyperplane<P, S>,
  43.         R extends Region<S>> RegionLocation classifyAgainstEmbeddedRegion(final P pt,
  44.                 final H hyperplane, final R embeddedRegion) {

  45.         if (hyperplane.contains(pt)) {
  46.             final S subPoint = hyperplane.toSubspace(pt);

  47.             return embeddedRegion.classify(subPoint);
  48.         }

  49.         return RegionLocation.OUTSIDE;
  50.     }

  51.     /** Return the closest point to a given point in a region embedded in a hyperplane.
  52.      * @param <P> Point implementation class
  53.      * @param <S> Subspace point implementation class
  54.      * @param <H> Hyperplane implementation class
  55.      * @param <R> Region implementation class
  56.      * @param pt point to find the closest point to
  57.      * @param hyperplane hyperplane containing the embedded region
  58.      * @param embeddedRegion embedded region to find the closest point in
  59.      * @return the closest point to {@code pt} in the embedded region
  60.      */
  61.     public static <
  62.         P extends Point<P>,
  63.         S extends Point<S>,
  64.         H extends EmbeddingHyperplane<P, S>,
  65.         R extends Region<S>> P closestToEmbeddedRegion(final P pt,
  66.                 final H hyperplane, final R embeddedRegion) {

  67.         final S subPt = hyperplane.toSubspace(pt);

  68.         if (embeddedRegion.contains(subPt)) {
  69.             return hyperplane.toSpace(subPt);
  70.         }

  71.         final S subProjected = embeddedRegion.project(subPt);
  72.         if (subProjected != null) {
  73.             return hyperplane.toSpace(subProjected);
  74.         }

  75.         return null;
  76.     }
  77. }