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 19 import org.apache.commons.geometry.core.Point; 20 import org.apache.commons.geometry.core.Region; 21 import org.apache.commons.geometry.core.RegionLocation; 22 import org.apache.commons.geometry.core.partitioning.EmbeddingHyperplane; 23 24 /** Utility methods for {@link org.apache.commons.geometry.core.partitioning.HyperplaneSubset} 25 * implementations. 26 */ 27 public final class HyperplaneSubsets { 28 29 /** Utility class; no instantiation. */ 30 private HyperplaneSubsets() { 31 } 32 33 /** Classify a point against a region embedded in a hyperplane. 34 * @param <P> Point implementation class 35 * @param <S> Subspace point implementation class 36 * @param <H> Hyperplane implementation class 37 * @param <R> Region implementation class 38 * @param pt the point to classify 39 * @param hyperplane hyperplane containing the embedded region 40 * @param embeddedRegion embedded region to classify against 41 * @return the region location of the given point 42 */ 43 public static < 44 P extends Point<P>, 45 S extends Point<S>, 46 H extends EmbeddingHyperplane<P, S>, 47 R extends Region<S>> RegionLocation classifyAgainstEmbeddedRegion(final P pt, 48 final H hyperplane, final R embeddedRegion) { 49 50 if (hyperplane.contains(pt)) { 51 final S subPoint = hyperplane.toSubspace(pt); 52 53 return embeddedRegion.classify(subPoint); 54 } 55 56 return RegionLocation.OUTSIDE; 57 } 58 59 /** Return the closest point to a given point in a region embedded in a hyperplane. 60 * @param <P> Point implementation class 61 * @param <S> Subspace point implementation class 62 * @param <H> Hyperplane implementation class 63 * @param <R> Region implementation class 64 * @param pt point to find the closest point to 65 * @param hyperplane hyperplane containing the embedded region 66 * @param embeddedRegion embedded region to find the closest point in 67 * @return the closest point to {@code pt} in the embedded region 68 */ 69 public static < 70 P extends Point<P>, 71 S extends Point<S>, 72 H extends EmbeddingHyperplane<P, S>, 73 R extends Region<S>> P closestToEmbeddedRegion(final P pt, 74 final H hyperplane, final R embeddedRegion) { 75 76 final S subPt = hyperplane.toSubspace(pt); 77 78 if (embeddedRegion.contains(subPt)) { 79 return hyperplane.toSpace(subPt); 80 } 81 82 final S subProjected = embeddedRegion.project(subPt); 83 if (subProjected != null) { 84 return hyperplane.toSpace(subProjected); 85 } 86 87 return null; 88 } 89 }