Class SimpleEdgeSetIntersector

 1  
 2  
 3  
 4 /*
 5  * Copyright (c) 2016 Vivid Solutions.
 6  *
 7  * All rights reserved. This program and the accompanying materials
 8  * are made available under the terms of the Eclipse Public License 2.0
 9  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
10  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
11  * and the Eclipse Distribution License is available at
12  *
13  * http://www.eclipse.org/org/documents/edl-v10.php.
14  */
15 package org.locationtech.jts.geomgraph.index;
16  
17 import java.util.Iterator;
18 import java.util.List;
19  
20 import org.locationtech.jts.geom.Coordinate;
21 import org.locationtech.jts.geomgraph.Edge;
22  
23 /**
24  * Finds all intersections in one or two sets of edges,
25  * using the straightforward method of
26  * comparing all segments.
27  * This algorithm is too slow for production use, but is useful for testing purposes.
28  * @version 1.7
29  */
30 public class SimpleEdgeSetIntersector
31   extends EdgeSetIntersector
32 {
33   // statistics information
34   int nOverlaps;
35  
36   public SimpleEdgeSetIntersector() {
37   }
38  
39   public void computeIntersections(List edges, SegmentIntersector si, boolean testAllSegments)
40   {
41     nOverlaps = 0;
42  
43     for (Iterator i0 = edges.iterator(); i0.hasNext(); ) {
44       Edge edge0 = (Edge) i0.next();
45       for (Iterator i1 = edges.iterator(); i1.hasNext(); ) {
46         Edge edge1 = (Edge) i1.next();
47         if (testAllSegments || edge0 != edge1)
48           computeIntersects(edge0, edge1, si);
49       }
50     }
51   }
52  
53  
54   public void computeIntersections(List edges0, List edges1, SegmentIntersector si)
55   {
56     nOverlaps = 0;
57  
58     for (Iterator i0 = edges0.iterator(); i0.hasNext(); ) {
59       Edge edge0 = (Edge) i0.next();
60       for (Iterator i1 = edges1.iterator(); i1.hasNext(); ) {
61         Edge edge1 = (Edge) i1.next();
62         computeIntersects(edge0, edge1, si);
63       }
64     }
65   }
66  
67   /**
68    * Performs a brute-force comparison of every segment in each Edge.
69    * This has n^2 performance, and is about 100 times slower than using
70    * monotone chains.
71    */
72   private void computeIntersects(Edge e0, Edge e1, SegmentIntersector si)
73   {
74    Coordinate[] pts0 = e0.getCoordinates();
75     Coordinate[] pts1 = e1.getCoordinates();
76     for (int i0 = 0; i0 < pts0.length - 1; i0++) {
77       for (int i1 = 0; i1 < pts1.length - 1; i1++) {
78         si.addIntersections(e0, i0, e1, i1);
79       }
80     }
81   }
82 }
83