001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.geometry.examples.tutorials.bsp;
018
019import java.io.File;
020import java.util.Arrays;
021
022import org.apache.commons.geometry.euclidean.twod.Bounds2D;
023import org.apache.commons.geometry.euclidean.twod.RegionBSPTree2D;
024import org.apache.commons.geometry.euclidean.twod.Vector2D;
025import org.apache.commons.geometry.euclidean.twod.path.LinePath;
026import org.apache.commons.numbers.core.Precision;
027
028/** Class containing tutorial code for computing the union of two trees.
029 */
030public final class BSPTreeUnion {
031
032    /** No instantiation. */
033    private BSPTreeUnion() {}
034
035    /** Tutorial code entry point.
036     * @param args command arguments; if given, the first argument is used as the location of
037     *      output folder
038     */
039    public static void main(final String[] args) {
040        final File outputFolder = new File(args.length > 0 ? args[0] : ".");
041        final BSPTreeSVGWriter svgWriter = new BSPTreeSVGWriter(Bounds2D.from(Vector2D.of(-8, -8), Vector2D.of(8, 8)));
042
043        final Precision.DoubleEquivalence precision = Precision.doubleEquivalenceOfEpsilon(1e-6);
044
045        final RegionBSPTree2D a = LinePath.fromVertexLoop(Arrays.asList(
046                    Vector2D.of(2, 0),
047                    Vector2D.of(-4, 3),
048                    Vector2D.of(-4, -3)
049                ), precision).toTree();
050
051        final RegionBSPTree2D b = LinePath.fromVertexLoop(Arrays.asList(
052                Vector2D.of(-2, 0),
053                Vector2D.of(4, -3),
054                Vector2D.of(4, 3)
055            ), precision).toTree();
056
057        final RegionBSPTree2D result = RegionBSPTree2D.empty();
058
059        result.union(a, b);
060
061        svgWriter.write(result, new File(outputFolder, "union.svg"));
062    }
063}