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.examples.tutorials.bsp;
18  
19  import java.io.File;
20  import java.util.Arrays;
21  
22  import org.apache.commons.geometry.core.partitioning.bsp.RegionCutRule;
23  import org.apache.commons.geometry.euclidean.twod.Bounds2D;
24  import org.apache.commons.geometry.euclidean.twod.Lines;
25  import org.apache.commons.geometry.euclidean.twod.RegionBSPTree2D;
26  import org.apache.commons.geometry.euclidean.twod.Vector2D;
27  import org.apache.commons.geometry.euclidean.twod.path.LinePath;
28  import org.apache.commons.numbers.core.Precision;
29  
30  /** Class containing tutorial code for constructing a balanced tree using a single
31   * structural cut.
32   */
33  public final class HexagonStructuralCut {
34  
35      /** No instantiation. */
36      private HexagonStructuralCut() {}
37  
38      /** Tutorial code entry point.
39       * @param args command arguments; if given, the first argument is used as the location of
40       *      output folder
41       */
42      public static void main(final String[] args) {
43          final File outputFolder = new File(args.length > 0 ? args[0] : ".");
44          final BSPTreeSVGWriter svgWriter = new BSPTreeSVGWriter(Bounds2D.from(Vector2D.of(-8, -8), Vector2D.of(8, 8)));
45  
46          final Precision.DoubleEquivalence precision = Precision.doubleEquivalenceOfEpsilon(1e-6);
47  
48          final RegionBSPTree2D tree = RegionBSPTree2D.empty();
49  
50          tree.insert(Lines.fromPointAndDirection(Vector2D.ZERO, Vector2D.Unit.PLUS_X, precision).span(),
51                  RegionCutRule.INHERIT);
52  
53          svgWriter.write(tree, new File(outputFolder, "hex-struct-0.svg"));
54  
55          final LinePath path = LinePath.fromVertexLoop(Arrays.asList(
56                      Vector2D.of(-4, 0),
57                      Vector2D.of(-2, -3),
58                      Vector2D.of(2, -3),
59                      Vector2D.of(4, 0),
60                      Vector2D.of(2, 3),
61                      Vector2D.of(-2, 3)
62                  ), precision);
63          tree.insert(path);
64  
65          svgWriter.write(tree, new File(outputFolder, "hex-struct-1.svg"));
66      }
67  }