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  import java.util.HashMap;
22  import java.util.Map;
23  
24  import org.apache.commons.geometry.euclidean.twod.Bounds2D;
25  import org.apache.commons.geometry.euclidean.twod.Lines;
26  import org.apache.commons.geometry.euclidean.twod.RegionBSPTree2D;
27  import org.apache.commons.geometry.euclidean.twod.RegionBSPTree2D.RegionNode2D;
28  import org.apache.commons.geometry.euclidean.twod.Segment;
29  import org.apache.commons.geometry.euclidean.twod.Vector2D;
30  import org.apache.commons.geometry.euclidean.twod.path.LinePath;
31  import org.apache.commons.numbers.core.Precision;
32  
33  /** Class containing tutorial code for constructing a 2D BSP tree using
34   * a top-down approach.
35   */
36  public final class TopDownBSPTreeConstruction {
37  
38      /** String defining the name format of output svg files. */
39      private static final String OUTPUT_FILE_FORMAT = "td-cut-%d.svg";
40  
41      /** No instantiation. */
42      private TopDownBSPTreeConstruction() {}
43  
44      /** Tutorial code entry point.
45       * @param args command arguments; if given, the first argument is used as the location of
46       *      output folder
47       */
48      public static void main(final String[] args) {
49          final File outputFolder = new File(args.length > 0 ? args[0] : ".");
50          final BSPTreeSVGWriter svgWriter = new BSPTreeSVGWriter(Bounds2D.from(Vector2D.of(-8, -8), Vector2D.of(8, 8)));
51  
52          final Map<RegionNode2D, String> nodeNames = new HashMap<>();
53          int cutCount = 0;
54  
55          // create a precision context for floating point comparisons
56          final Precision.DoubleEquivalence precision = Precision.doubleEquivalenceOfEpsilon(1e-6);
57  
58          // construct an empty tree
59          final RegionBSPTree2D tree = RegionBSPTree2D.empty();
60  
61          final Segment firstBoundary = Lines.segmentFromPoints(Vector2D.of(-5, 0), Vector2D.of(-1, 0), precision);
62          tree.insert(firstBoundary);
63  
64          final RegionNode2D a = tree.getRoot();
65          final RegionNode2D b = a.getMinus();
66          final RegionNode2D c = a.getPlus();
67  
68          nodeNames.put(a, "a");
69          nodeNames.put(b, "b");
70          nodeNames.put(c, "c");
71          svgWriter.write(tree, nodeNames, new File(outputFolder, String.format(OUTPUT_FILE_FORMAT, ++cutCount)));
72  
73          tree.insert(Lines.segmentFromPoints(Vector2D.of(1, 0), Vector2D.of(-5, 3), precision));
74          tree.insert(Lines.segmentFromPoints(Vector2D.of(-5, 3), Vector2D.of(-5, 0), precision));
75  
76          final RegionNode2D d = b.getMinus();
77          final RegionNode2D e = b.getPlus();
78          final RegionNode2D f = d.getMinus();
79          final RegionNode2D g = d.getPlus();
80  
81          nodeNames.put(d, "d");
82          nodeNames.put(e, "e");
83          nodeNames.put(f, "f");
84          nodeNames.put(g, "g");
85          svgWriter.write(tree, nodeNames, new File(outputFolder, String.format(OUTPUT_FILE_FORMAT, ++cutCount)));
86  
87          final LinePath path = LinePath.fromVertices(Arrays.asList(
88                  Vector2D.of(-1, 0),
89                  Vector2D.of(5, -3),
90                  Vector2D.of(5, 0),
91                  Vector2D.of(1, 0)), precision);
92          tree.insert(path);
93  
94          final RegionNode2D h = c.getMinus();
95          final RegionNode2D i = c.getPlus();
96          final RegionNode2D j = h.getMinus();
97          final RegionNode2D k = h.getPlus();
98  
99          nodeNames.put(h, "h");
100         nodeNames.put(i, "i");
101         nodeNames.put(j, "j");
102         nodeNames.put(k, "k");
103         svgWriter.write(tree, nodeNames, new File(outputFolder, String.format(OUTPUT_FILE_FORMAT, ++cutCount)));
104     }
105 }