BoundaryBuilder.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.  *      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. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.geometry.partitioning;

  22. import org.hipparchus.geometry.Point;
  23. import org.hipparchus.geometry.Space;

  24. /** Visitor building boundary shell tree.
  25.  * <p>
  26.  * The boundary shell is represented as {@link BoundaryAttribute boundary attributes}
  27.  * at each internal node.
  28.  * </p>
  29.  * @param <S> Type of the space.
  30.  * @param <P> Type of the points in space.
  31.  * @param <H> Type of the hyperplane.
  32.  * @param <I> Type of the sub-hyperplane.
  33.  */
  34. class BoundaryBuilder<S extends Space, P extends Point<S, P>, H extends Hyperplane<S, P, H, I>, I extends SubHyperplane<S, P, H, I>>
  35.     implements BSPTreeVisitor<S, P, H, I> {

  36.     /** {@inheritDoc} */
  37.     @Override
  38.     public Order visitOrder(BSPTree<S, P, H, I> node) {
  39.         return Order.PLUS_MINUS_SUB;
  40.     }

  41.     /** {@inheritDoc} */
  42.     @Override
  43.     public void visitInternalNode(BSPTree<S, P, H, I> node) {

  44.         I                    plusOutside = null;
  45.         I                    plusInside  = null;
  46.         NodesSet<S, P, H, I> splitters   = null;

  47.         // characterize the cut sub-hyperplane,
  48.         // first with respect to the plus sub-tree
  49.         final Characterization<S, P, H, I> plusChar = new Characterization<>(node.getPlus(), node.getCut().copySelf());

  50.         if (plusChar.touchOutside()) {
  51.             // plusChar.outsideTouching() corresponds to a subset of the cut sub-hyperplane
  52.             // known to have outside cells on its plus side, we want to check if parts
  53.             // of this subset do have inside cells on their minus side
  54.             final Characterization<S, P, H, I> minusChar = new Characterization<>(node.getMinus(), plusChar.outsideTouching());
  55.             if (minusChar.touchInside()) {
  56.                 // this part belongs to the boundary,
  57.                 // it has the outside on its plus side and the inside on its minus side
  58.                 plusOutside = minusChar.insideTouching();
  59.                 splitters = new NodesSet<>();
  60.                 splitters.addAll(minusChar.getInsideSplitters());
  61.                 splitters.addAll(plusChar.getOutsideSplitters());
  62.             }
  63.         }

  64.         if (plusChar.touchInside()) {
  65.             // plusChar.insideTouching() corresponds to a subset of the cut sub-hyperplane
  66.             // known to have inside cells on its plus side, we want to check if parts
  67.             // of this subset do have outside cells on their minus side
  68.             final Characterization<S, P, H, I> minusChar = new Characterization<>(node.getMinus(), plusChar.insideTouching());
  69.             if (minusChar.touchOutside()) {
  70.                 // this part belongs to the boundary,
  71.                 // it has the inside on its plus side and the outside on its minus side
  72.                 plusInside = minusChar.outsideTouching();
  73.                 if (splitters == null) {
  74.                     splitters = new NodesSet<>();
  75.                 }
  76.                 splitters.addAll(minusChar.getOutsideSplitters());
  77.                 splitters.addAll(plusChar.getInsideSplitters());
  78.             }
  79.         }

  80.         if (splitters != null) {
  81.             // the parent nodes are natural splitters for boundary sub-hyperplanes
  82.             for (BSPTree<S, P, H, I> up = node.getParent(); up != null; up = up.getParent()) {
  83.                 splitters.add(up);
  84.             }
  85.         }

  86.         // set the boundary attribute at non-leaf nodes
  87.         node.setAttribute(new BoundaryAttribute<>(plusOutside, plusInside, splitters));

  88.     }

  89.     /** {@inheritDoc} */
  90.     @Override
  91.     public void visitLeafNode(BSPTree<S, P, H, I> node) {
  92.     }

  93. }