View Javadoc
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.partitioning.test;
18  
19  import org.apache.commons.geometry.core.partitioning.BoundarySource;
20  import org.apache.commons.geometry.core.partitioning.Hyperplane;
21  import org.apache.commons.geometry.core.partitioning.HyperplaneConvexSubset;
22  import org.apache.commons.geometry.core.partitioning.HyperplaneSubset;
23  import org.apache.commons.geometry.core.partitioning.bsp.AbstractBSPTree;
24  
25  /** BSP Tree implementation class for testing purposes.
26   */
27  public final class TestBSPTree extends AbstractBSPTree<TestPoint2D, TestBSPTree.TestNode> {
28  
29      /** {@inheritDoc} */
30      @Override
31      protected TestNode createNode() {
32          return new TestNode(this);
33      }
34  
35      public void insert(final HyperplaneSubset<TestPoint2D> sub) {
36          insert(sub.toConvex());
37      }
38  
39      public void insert(final HyperplaneConvexSubset<TestPoint2D> sub) {
40          insert(sub, root -> { });
41      }
42  
43      public void insert(final Iterable<? extends HyperplaneConvexSubset<TestPoint2D>> subs) {
44          subs.forEach(this::insert);
45      }
46  
47      public void insert(final BoundarySource<TestLineSegment> src) {
48          src.boundaryStream().forEach(this::insert);
49      }
50  
51      /** {@inheritDoc}
52       *
53       * <p>Exposed as public for testing.</p>
54       */
55      @Override
56      public void splitIntoTrees(final Hyperplane<TestPoint2D> splitter,
57              final AbstractBSPTree<TestPoint2D, TestBSPTree.TestNode> minus,
58              final AbstractBSPTree<TestPoint2D, TestBSPTree.TestNode> plus) {
59  
60          super.splitIntoTrees(splitter, minus, plus);
61      }
62  
63      @Override
64      protected void copyNodeProperties(final TestNode src, final TestNode dst) {
65          // do nothing
66      }
67  
68      /** BSP Tree node class for {@link TestBSPTree}.
69       */
70      public static class TestNode extends AbstractBSPTree.AbstractNode<TestPoint2D, TestNode> {
71          public TestNode(final AbstractBSPTree<TestPoint2D, TestNode> tree) {
72              super(tree);
73          }
74  
75          /** Cut this node with the given hyperplane. If the hyperplane intersects the node's region,
76           * then the node becomes an internal node with two child leaf node. If the hyperplane does
77           * not intersect the node's region, then the node is made a leaf node. The same node is
78           * returned, regardless of the outcome of the cut operation.
79           * @param cutter hyperplane to cut the node with
80           * @return this node
81           */
82          public TestNode cut(final Hyperplane<TestPoint2D> cutter) {
83              insertCut(cutter);
84  
85              return this;
86          }
87  
88          public boolean insertCut(final Hyperplane<TestPoint2D> cutter) {
89              return ((TestBSPTree) getTree()).cutNode(getSelf(), cutter, root -> { });
90          }
91  
92          public boolean clearCut() {
93              return ((TestBSPTree) getTree()).removeNodeCut(getSelf());
94          }
95  
96          /** {@inheritDoc} */
97          @Override
98          protected TestNode getSelf() {
99              return this;
100         }
101     }
102 }