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