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

  18. import java.util.Collection;
  19. import java.util.List;
  20. import java.util.stream.Collectors;

  21. /** This interface defines mappings between a space and one of its subspaces.

  22.  * <p>Subspaces are the lower-dimension subsets of a space. For example,
  23.  * in an n-dimension space, the subspaces are the (n-1) dimension space,
  24.  * the (n-2) dimension space, and so on. This interface can be used regardless
  25.  * of the difference in number of dimensions between the space and the target
  26.  * subspace. For example, a line in 3D Euclidean space can use this interface
  27.  * to map directly from 3D Euclidean space to 1D Euclidean space (ie, the location
  28.  * along the line).</p>
  29.  *
  30.  * @param <P> Point type defining the embedding space.
  31.  * @param <S> Point type defining the embedded subspace.
  32.  */
  33. public interface Embedding<P extends Point<P>, S extends Point<S>> {

  34.     /** Transform a space point into a subspace point.
  35.      * @param pt n-dimension point of the space
  36.      * @return lower-dimension point of the subspace corresponding to
  37.      *      the specified space point
  38.      * @see #toSpace
  39.      */
  40.     S toSubspace(P pt);

  41.     /** Transform a collection of space points into subspace points.
  42.      * @param pts collection of n-dimension points to transform
  43.      * @return collection of transformed lower-dimension points.
  44.      * @see #toSubspace(Point)
  45.      */
  46.     default List<S> toSubspace(final Collection<P> pts) {
  47.         return pts.stream().map(this::toSubspace).collect(Collectors.toList());
  48.     }

  49.     /** Transform a subspace point into a space point.
  50.      * @param pt lower-dimension point of the subspace
  51.      * @return n-dimension point of the space corresponding to the
  52.      *      specified subspace point
  53.      * @see #toSubspace(Point)
  54.      */
  55.     P toSpace(S pt);

  56.     /** Transform a collection of subspace points into space points.
  57.      * @param pts collection of lower-dimension points to transform
  58.      * @return collection of transformed n-dimension points.
  59.      * @see #toSpace(Point)
  60.      */
  61.     default List<P> toSpace(final Collection<S> pts) {
  62.         return pts.stream().map(this::toSpace).collect(Collectors.toList());
  63.     }
  64. }