InteriorPointFinder.java

  1. /*
  2.  * Licensed to the Hipparchus project 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 Hipparchus project 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.  *      https://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.hipparchus.geometry.partitioning;

  18. import org.hipparchus.geometry.Point;
  19. import org.hipparchus.geometry.Space;

  20. /**
  21.  * Finder for interior points.
  22.  * @param <S> Type of the space.
  23.  * @param <P> Type of the points in space.
  24.  * @param <H> Type of the hyperplane.
  25.  * @param <I> Type of the sub-hyperplane.
  26.  * @since 4.0
  27.  */
  28. public class InteriorPointFinder<S extends Space,
  29.                                  P extends Point<S, P>,
  30.                                  H extends Hyperplane<S, P, H, I>,
  31.                                  I extends SubHyperplane<S, P, H, I>>
  32.     implements BSPTreeVisitor<S, P, H, I> {

  33.     /** Default point to use for whole space. */
  34.     private final P defaultPoint;

  35.     /** Selected point. */
  36.     private BSPTree.InteriorPoint<S, P> selected;

  37.     /**
  38.      * Simple constructor.
  39.      * @param defaultPoint default point to use for whole space
  40.      */
  41.     public InteriorPointFinder(final P defaultPoint) {
  42.         this.defaultPoint = defaultPoint;
  43.         this.selected     = null;
  44.     }

  45.     /** {@inheritDoc} */
  46.     @Override
  47.     public Order visitOrder(final BSPTree<S, P, H, I> node) {
  48.         return Order.MINUS_PLUS_SUB;
  49.     }

  50.     /** {@inheritDoc} */
  51.     @Override
  52.     public void visitInternalNode(final BSPTree<S, P, H, I> node) {
  53.         // nothing to do
  54.     }

  55.     /** {@inheritDoc} */
  56.     @Override
  57.     public void visitLeafNode(final BSPTree<S, P, H, I> node) {
  58.         if ((Boolean) node.getAttribute()) {
  59.             // this is an inside cell, look for the barycenter of edges/facets interior points
  60.             final BSPTree.InteriorPoint<S, P> interior = node.getInteriorPoint(defaultPoint);
  61.             if (selected == null || interior.getDistance() > selected.getDistance()) {
  62.                 // this new point is farther away to edges/facets than the selected one, change selection
  63.                 selected = interior;
  64.             }
  65.         }
  66.     }

  67.     /**
  68.      * Get the point found.
  69.      * @return found point (null if tree was empty)
  70.      */
  71.     public BSPTree.InteriorPoint<S, P> getPoint() {
  72.         return selected;
  73.     }

  74. }