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.bsp;
18  
19  import org.apache.commons.geometry.core.Point;
20  import org.apache.commons.geometry.core.partitioning.bsp.BSPTree.Node;
21  
22  /** Internal class for creating simple string representations of BSP trees.
23   * @param <P> Point implementation type
24   * @param <N> Node implementation type
25   */
26  final class BSPTreePrinter<P extends Point<P>, N extends Node<P, N>>
27      implements BSPTreeVisitor<P, N> {
28  
29      /** Line indent string. */
30      private static final String INDENT = "    ";
31  
32      /** New line character. */
33      private static final String NEW_LINE = "\n";
34  
35      /** Entry prefix for nodes on the minus side of their parent. */
36      private static final String MINUS_CHILD = "[-] ";
37  
38      /** Entry prefix for nodes on the plus side of their parent. */
39      private static final String PLUS_CHILD = "[+] ";
40  
41      /** Ellipsis for truncated representations. */
42      private static final String ELLIPSIS = "...";
43  
44      /** Maximum depth of nodes that will be printed. */
45      private final int maxDepth;
46  
47      /** Contains the string output. */
48      private final StringBuilder output = new StringBuilder();
49  
50      /** Simple constructor.
51       * @param maxDepth maximum depth of nodes to be printed
52       */
53      BSPTreePrinter(final int maxDepth) {
54          this.maxDepth = maxDepth;
55      }
56  
57      /** {@inheritDoc} */
58      @Override
59      public Result visit(final N node) {
60          final int depth = node.depth();
61  
62          if (depth <= maxDepth) {
63              startLine(node);
64              writeNode(node);
65          } else if (depth == maxDepth + 1 && node.isPlus()) {
66              startLine(node);
67              write(ELLIPSIS);
68          }
69  
70          return Result.CONTINUE;
71      }
72  
73      /** {@inheritDoc} */
74      @Override
75      public Order visitOrder(final N node) {
76          if (node.depth() > maxDepth + 1) {
77              return Order.NONE;
78          }
79          return Order.NODE_MINUS_PLUS;
80      }
81  
82      /** Start a line for the given node.
83       * @param node the node to begin a line for
84       */
85      private void startLine(final N node) {
86          if (node.getParent() != null) {
87              write(NEW_LINE);
88          }
89  
90          final int depth = node.depth();
91          for (int i = 0; i < depth; ++i) {
92              write(INDENT);
93          }
94      }
95  
96      /** Writes the given node to the output.
97       * @param node the node to write
98       */
99      private void writeNode(final N node) {
100         if (node.getParent() != null) {
101             if (node.isMinus()) {
102                 write(MINUS_CHILD);
103             } else {
104                 write(PLUS_CHILD);
105             }
106         }
107 
108         write(node.toString());
109     }
110 
111     /** Add the given string to the output.
112      * @param str the string to add
113      */
114     private void write(final String str) {
115         output.append(str);
116     }
117 
118     /** Return the string representation of the visited tree. */
119     @Override
120     public String toString() {
121         return output.toString();
122     }
123 }