1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
23
24
25
26 final class BSPTreePrinter<P extends Point<P>, N extends Node<P, N>>
27 implements BSPTreeVisitor<P, N> {
28
29
30 private static final String INDENT = " ";
31
32
33 private static final String NEW_LINE = "\n";
34
35
36 private static final String MINUS_CHILD = "[-] ";
37
38
39 private static final String PLUS_CHILD = "[+] ";
40
41
42 private static final String ELLIPSIS = "...";
43
44
45 private final int maxDepth;
46
47
48 private final StringBuilder output = new StringBuilder();
49
50
51
52
53 BSPTreePrinter(final int maxDepth) {
54 this.maxDepth = maxDepth;
55 }
56
57
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
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
83
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
97
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
112
113
114 private void write(final String str) {
115 output.append(str);
116 }
117
118
119 @Override
120 public String toString() {
121 return output.toString();
122 }
123 }