-
Notifications
You must be signed in to change notification settings - Fork 3
/
HMeshTreeWalker.java
254 lines (227 loc) · 8.08 KB
/
HMeshTreeWalker.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package hgeom.hmesh.core;
import java.util.Collection;
import java.util.Iterator;
import java.util.Objects;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import hgeom.hmesh.core.HMeshWalker.Monitor;
import hgeom.hmesh.elements.HEdge;
import hgeom.hmesh.elements.HElement;
import hgeom.hmesh.elements.HFace;
import hgeom.hmesh.elements.HMesh;
import hgeom.hmesh.elements.HVertex;
import hgeom.hmesh.sequence.Sequence;
/**
* A walker able to walk inside a {@link HMesh half-edge data structure} along a
* tree-like path
*
* @author Pierre B.
*/
public final class HMeshTreeWalker {
/**
* Monitor for a {@link HMesh half-edge data structure} tree walker
*
* @param <E> type of elements being visited by the tree walker
*/
public interface TreeMonitor<E> extends Monitor<E> {
/**
* @return current depth from the tree root. a depth of 0 indicates the
* root itself ; a depth of 1 indicates that a root's neighbor
* is being visited ; etc
*/
int depth();
}
/**
* Type of path used by a {@link HMesh half-edge data structure} tree walker
*/
public enum TreePathType {
/**
* Walk around the tree root in depth order, starting from the closest
* depths then broadening
*/
BREADTH_FIRST,
/**
* Walk using a pre-order depth-first tree path
*/
DEPTH_FIRST
}
/**
* Tree node children supplier used by a half-edge mesh tree walker
*
* @param <E>
*/
@FunctionalInterface
public interface TreeChildrenSupplier<E> {
/**
* Function used by a tree walker to obtain the children of a tree node
* currently visited.
*
* @param parent the tree parent node whose children are seeked
* @param monitor a monitor that watch the walker's progress. It
* indicates which nodes have already been visited. May
* help the method to select the children
* @param children a collection in which the children should be put
*
* @see HMeshTreeWalker
*/
void get(E parent, TreeMonitor<E> monitor, Collection<E> children);
}
/**
*
*/
private final HMesh mesh;
/**
*
*/
private final TreePathType pathType;
/**
*
*/
private final int treeMaxDepth;
/**
* Constructs a walker intended to operate on the specified mesh. The walker
* will walk the mesh through a breadth-first tree path
*
* @param mesh
*/
public HMeshTreeWalker(HMesh mesh) {
this(mesh, TreePathType.BREADTH_FIRST, -1);
}
/**
* Constructs a walker intended to operate on the specified mesh and to walk
* along the specified type of tree-like path
*
* @param mesh
* @param pathType if {@link TreePathType#DEPTH_FIRST}, construct a walker
* that will walk the mesh along a depth-first pre-order
* tree path; if {@link TreePathType#BREADTH_FIRST},
* construct a walker that will walk the mesh along a
* breadth-first tree path
*/
public HMeshTreeWalker(HMesh mesh, TreePathType pathType) {
this(mesh, pathType, -1);
}
/**
* Constructs a walker intended to operate on the specified mesh and to walk
* along the specified type of tree-like path. Specifies also the maximum
* tree depth the walker is allowed
*
* @param mesh
* @param pathType if {@link TreePathType#DEPTH_FIRST}, construct a
* walker that will walk the along through a depth-first
* pre-order tree path; if
* {@link TreePathType#BREADTH_FIRST}, construct a
* walker that will walk the mesh along a breadth-first
* tree path
* @param treeMaxDepth exclusive max bound for the tree depth or {@code -1}
* if no bound should be given to the walker
*/
public HMeshTreeWalker(HMesh mesh, TreePathType pathType,
int treeMaxDepth) {
this.mesh = Objects.requireNonNull(mesh);
this.pathType = Objects.requireNonNull(pathType);
this.treeMaxDepth = treeMaxDepth;
}
/**
* Starts from the specified {@link HVertex vertex} and walks within the
* underlying {@link HMesh} along a tree-like path made up of vertices.
* Children of a {@link HVertex vertex} in the tree are its
* {@link HVertex#neighbors() neighbors} in the {@link HMesh}
*
* @param treeRoot the root of the tree
* @return a stream on all visited vertices
* @see HVertex#neighbors()
*/
public Stream<HVertex> walk(HVertex treeRoot) {
return walk(treeRoot, HVertex::neighbors);
}
/**
* Starts from the specified {@link HEdge edge} and walks within the
* underlying {@link HMesh} along a tree-like path made up of edges.
* Children of a {@link HEdge edge} in the tree are the
* {@link HEdge#outgoingEdges() edges going out} of it in the {@link HMesh}
*
* @param treeRoot the root of the tree
* @return a stream on all visited edges
* @see HEdge#outgoingEdges()
*/
public Stream<HEdge> walk(HEdge treeRoot) {
return walk(treeRoot, HEdge::outgoingEdges);
}
/**
* Starts from the specified {@link HFace face} and walks within the
* underlying {@link HMesh} along a tree-like path made up of faces.
* Children of a {@link HFace face} in the tree are its
* {@link HFace#neighbors() neighbors} in the {@link HMesh}
*
* @param treeRoot the root of the tree
* @return a stream on all visited faces
* @see HFace#neighbors()
*/
public Stream<HFace> walk(HFace treeRoot) {
return walk(treeRoot, HFace::neighbors);
}
/**
* Starts from the specified element and walks through a tree whose nodes
* are elements of the underlying mesh
*
* @param treeRoot the root of the tree
* @param childrenSupplier a operator for supplying the children of each
* node of the tree
* @return a stream on all visited element
* @throws NullPointerException if the root or the children operator is
* {@code null}
*/
public <E extends HElement> Stream<E> walk(E treeRoot,
TreeChildrenSupplier<E> childrenSupplier) {
return unknownSizeStream(HMeshTreeIterator.create(mesh, pathType,
treeMaxDepth, treeRoot, childrenSupplier));
}
/**
* Starts from the specified element and walks through a tree whose nodes
* are elements of the underlying mesh
*
* @param treeRoot the root of the tree
* @param childrenSupplier a operator for supplying the children of each
* node of the tree
* @return a stream on all visited element
* @throws NullPointerException if the root or the children operator is
* {@code null}
*/
public <E extends HElement> Stream<E> walk(E treeRoot,
Function<E, Sequence<E>> childrenSupplier) {
return unknownSizeStream(HMeshTreeIterator.create(mesh, pathType,
treeMaxDepth, treeRoot, childrenSupplier));
}
/**
* Starts from the specified element and walks through a tree whose nodes
* are elements of the underlying mesh
*
* @param treeRoot the root of the tree
* @param childrenSupplier a operator for supplying the children of each
* node of the tree
* @return a stream on all visited element
* @throws NullPointerException if the root or the children operator is
* {@code null}
*/
public <E extends HElement> Stream<E> walk(E treeRoot,
BiFunction<E, TreeMonitor<E>, Sequence<E>> childrenSupplier) {
return unknownSizeStream(HMeshTreeIterator.create(mesh, pathType,
treeMaxDepth, treeRoot, childrenSupplier));
}
/**
* @param iterator
* @return
*/
private static <E> Stream<E> unknownSizeStream(Iterator<E> iterator) {
return StreamSupport.stream(
Spliterators.spliteratorUnknownSize(iterator,
Spliterator.IMMUTABLE | Spliterator.NONNULL
| Spliterator.DISTINCT | Spliterator.ORDERED),
false);
}
}