Class LastFoundQuadEdgeLocator

 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  
17 /**
18  * Locates {@link QuadEdge}s in a {@link QuadEdgeSubdivision},
19  * optimizing the search by starting in the
20  * locality of the last edge found.
21  * 
22  * @author Martin Davis
23  */
24 public class LastFoundQuadEdgeLocator implements QuadEdgeLocator {
25     private QuadEdgeSubdivision subdiv;
26     private QuadEdge            lastEdge = null;
27  
28     public LastFoundQuadEdgeLocator(QuadEdgeSubdivision subdiv) {
29         this.subdiv = subdiv;
30         init();
31     }
32  
33     private void init() {
34         lastEdge = findEdge();
35     }
36  
37     private QuadEdge findEdge() {
38         Collection edges = subdiv.getEdges();
39         // assume there is an edge - otherwise will get an exception
40         return (QuadEdge) edges.iterator().next();
41     }
42  
43     /**
44      * Locates an edge e, such that either v is on e, or e is an edge of a triangle containing v.
45      * The search starts from the last located edge and proceeds on the general direction of v.
46      */
47     public QuadEdge locate(Vertex v) {
48         if (! lastEdge.isLive()) {
49             init();
50         }
51  
52         QuadEdge e = subdiv.locateFromEdge(v, lastEdge);
53         lastEdge = e;
54         return e;
55     }
56 }
57