Class EdgeConnectedTriangleTraversal

 1 /*
 2  * Copyright (c) 2016 Vivid Solutions.
 3  *
 4  * All rights reserved. This program and the accompanying materials
 5  * are made available under the terms of the Eclipse Public License 2.0
 6  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
 7  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
 8  * and the Eclipse Distribution License is available at
 9  *
10  * http://www.eclipse.org/org/documents/edl-v10.php.
11  */
12  
13 package org.locationtech.jts.triangulate.quadedge;
14  
15 import java.util.Collection;
16 import java.util.LinkedList;
17  
18 /**
19  * A framework to visit sets of edge-connected {@link QuadEdgeTriangle}s in breadth-first order
20  * 
21  * @author Martin Davis
22  * @version 1.0
23  */
24 public class EdgeConnectedTriangleTraversal {
25     private LinkedList triQueue = new LinkedList();
26  
27     public EdgeConnectedTriangleTraversal() {}
28  
29     public void init(QuadEdgeTriangle tri) {
30         triQueue.addLast(tri);
31     }
32  
33     /**
34      * Called to initialize the traversal queue with a given set of {@link QuadEdgeTriangle}s
35      * 
36      * @param tris a collection of QuadEdgeTriangle
37      */
38     public void init(Collection tris) {
39         triQueue.addAll(tris);
40     }
41  
42     /**
43      * Subclasses can call this method to add a triangle to the end of the queue. This is useful for
44      * initializing the queue to a chosen set of triangles.
45      * 
46      * @param tri a triangle
47      */
48     /*
49      * protected void addLast(QuadEdgeTriangle tri) { triQueue.addLast(tri); }
50      */
51  
52     /**
53      * Subclasses call this method to perform the visiting process.
54      */
55     public void visitAll(TraversalVisitor visitor) {
56         while (!triQueue.isEmpty()) {
57             QuadEdgeTriangle tri = (QuadEdgeTriangle) triQueue.removeFirst();
58             process(tri, visitor);
59         }
60     }
61  
62     private void process(QuadEdgeTriangle currTri, TraversalVisitor visitor) {
63         currTri.getNeighbours();
64         for (int i = 0; i < 3; i++) {
65             QuadEdgeTriangle neighTri = (QuadEdgeTriangle) currTri.getEdge(i).sym().getData();
66             if (neighTri == null)
67                 continue;
68             if (visitor.visit(currTri, i, neighTri))
69                 triQueue.addLast(neighTri);
70         }
71     }
72  
73 }
74