Class EdgeEndBuilder

  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.operation.relate;
 16  
 17 /**
 18  * An EdgeEndBuilder creates EdgeEnds for all the "split edges"
 19  * created by the
 20  * intersections determined for an Edge.
 21  *
 22  * @version 1.7
 23  */
 24 import java.util.ArrayList;
 25 import java.util.Iterator;
 26 import java.util.List;
 27  
 28 import org.locationtech.jts.geom.Coordinate;
 29 import org.locationtech.jts.geomgraph.Edge;
 30 import org.locationtech.jts.geomgraph.EdgeEnd;
 31 import org.locationtech.jts.geomgraph.EdgeIntersection;
 32 import org.locationtech.jts.geomgraph.EdgeIntersectionList;
 33 import org.locationtech.jts.geomgraph.Label;
 34  
 35 /**
 36  * Computes the {@link EdgeEnd}s which arise from a noded {@link Edge}.
 37  *
 38  * @version 1.7
 39  */
 40 public class EdgeEndBuilder {
 41  
 42   public EdgeEndBuilder() {
 43   }
 44  
 45   public List computeEdgeEnds(Iterator edges)
 46   {
 47     List l = new ArrayList();
 48     for (Iterator i = edges; i.hasNext(); ) {
 49       Edge e = (Edge) i.next();
 50       computeEdgeEnds(e, l);
 51     }
 52     return l;
 53   }
 54  
 55   /**
 56    * Creates stub edges for all the intersections in this
 57    * Edge (if any) and inserts them into the graph.
 58    */
 59   public void computeEdgeEnds(Edge edge, List l)
 60   {
 61     EdgeIntersectionList eiList = edge.getEdgeIntersectionList();
 62 //Debug.print(eiList);
 63     // ensure that the list has entries for the first and last point of the edge
 64     eiList.addEndpoints();
 65  
 66     Iterator it = eiList.iterator();
 67     EdgeIntersection eiPrev = null;
 68     EdgeIntersection eiCurr = null;
 69     // no intersections, so there is nothing to do
 70     if (! it.hasNext()) return;
 71     EdgeIntersection eiNext = (EdgeIntersection) it.next();
 72     do {
 73       eiPrev = eiCurr;
 74       eiCurr = eiNext;
 75       eiNext = null;
 76       if (it.hasNext()) eiNext = (EdgeIntersection) it.next();
 77  
 78       if (eiCurr != null) {
 79         createEdgeEndForPrev(edge, l, eiCurr, eiPrev);
 80         createEdgeEndForNext(edge, l, eiCurr, eiNext);
 81       }
 82  
 83     } while (eiCurr != null);
 84  
 85   }
 86  
 87   /**
 88    * Create a EdgeStub for the edge before the intersection eiCurr.
 89    * The previous intersection is provided
 90    * in case it is the endpoint for the stub edge.
 91    * Otherwise, the previous point from the parent edge will be the endpoint.
 92    * <br>
 93    * eiCurr will always be an EdgeIntersection, but eiPrev may be null.
 94    */
 95   void createEdgeEndForPrev(
 96                       Edge edge,
 97                       List l,
 98                       EdgeIntersection eiCurr,
 99                       EdgeIntersection eiPrev)
100   {
101  
102     int iPrev = eiCurr.segmentIndex;
103     if (eiCurr.dist == 0.0) {
104       // if at the start of the edge there is no previous edge
105       if (iPrev == 0return;
106       iPrev--;
107     }
108     Coordinate pPrev = edge.getCoordinate(iPrev);
109     // if prev intersection is past the previous vertex, use it instead
110     if (eiPrev != null && eiPrev.segmentIndex >= iPrev)
111       pPrev = eiPrev.coord;
112  
113     Label label = new Label(edge.getLabel());
114     // since edgeStub is oriented opposite to it's parent edge, have to flip sides for edge label
115     label.flip();
116     EdgeEnd e = new EdgeEnd(edge, eiCurr.coord, pPrev, label);
117 //e.print(System.out);  System.out.println();
118     l.add(e);
119   }
120     /**
121      * Create a StubEdge for the edge after the intersection eiCurr.
122      * The next intersection is provided
123      * in case it is the endpoint for the stub edge.
124      * Otherwise, the next point from the parent edge will be the endpoint.
125      * <br>
126      * eiCurr will always be an EdgeIntersection, but eiNext may be null.
127      */
128   void createEdgeEndForNext(
129                       Edge edge,
130                       List l,
131                       EdgeIntersection eiCurr,
132                       EdgeIntersection eiNext)
133   {
134  
135     int iNext = eiCurr.segmentIndex + 1;
136     // if there is no next edge there is nothing to do
137     if (iNext >= edge.getNumPoints() && eiNext == nullreturn;
138  
139     Coordinate pNext = edge.getCoordinate(iNext);
140  
141     // if the next intersection is in the same segment as the current, use it as the endpoint
142     if (eiNext != null && eiNext.segmentIndex == eiCurr.segmentIndex)
143       pNext = eiNext.coord;
144  
145     EdgeEnd e = new EdgeEnd(edge, eiCurr.coord, pNext, new Label(edge.getLabel()));
146 //Debug.println(e);
147     l.add(e);
148   }
149  
150 }
151