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.core.internal; 018 019import org.apache.commons.geometry.core.Point; 020import org.apache.commons.geometry.core.Region; 021import org.apache.commons.geometry.core.RegionLocation; 022import org.apache.commons.geometry.core.partitioning.EmbeddingHyperplane; 023 024/** Utility methods for {@link org.apache.commons.geometry.core.partitioning.HyperplaneSubset} 025 * implementations. 026 */ 027public final class HyperplaneSubsets { 028 029 /** Utility class; no instantiation. */ 030 private HyperplaneSubsets() { 031 } 032 033 /** Classify a point against a region embedded in a hyperplane. 034 * @param <P> Point implementation class 035 * @param <S> Subspace point implementation class 036 * @param <H> Hyperplane implementation class 037 * @param <R> Region implementation class 038 * @param pt the point to classify 039 * @param hyperplane hyperplane containing the embedded region 040 * @param embeddedRegion embedded region to classify against 041 * @return the region location of the given point 042 */ 043 public static < 044 P extends Point<P>, 045 S extends Point<S>, 046 H extends EmbeddingHyperplane<P, S>, 047 R extends Region<S>> RegionLocation classifyAgainstEmbeddedRegion(final P pt, 048 final H hyperplane, final R embeddedRegion) { 049 050 if (hyperplane.contains(pt)) { 051 final S subPoint = hyperplane.toSubspace(pt); 052 053 return embeddedRegion.classify(subPoint); 054 } 055 056 return RegionLocation.OUTSIDE; 057 } 058 059 /** Return the closest point to a given point in a region embedded in a hyperplane. 060 * @param <P> Point implementation class 061 * @param <S> Subspace point implementation class 062 * @param <H> Hyperplane implementation class 063 * @param <R> Region implementation class 064 * @param pt point to find the closest point to 065 * @param hyperplane hyperplane containing the embedded region 066 * @param embeddedRegion embedded region to find the closest point in 067 * @return the closest point to {@code pt} in the embedded region 068 */ 069 public static < 070 P extends Point<P>, 071 S extends Point<S>, 072 H extends EmbeddingHyperplane<P, S>, 073 R extends Region<S>> P closestToEmbeddedRegion(final P pt, 074 final H hyperplane, final R embeddedRegion) { 075 076 final S subPt = hyperplane.toSubspace(pt); 077 078 if (embeddedRegion.contains(subPt)) { 079 return hyperplane.toSpace(subPt); 080 } 081 082 final S subProjected = embeddedRegion.project(subPt); 083 if (subProjected != null) { 084 return hyperplane.toSpace(subProjected); 085 } 086 087 return null; 088 } 089}