Class TopologyLocation

  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;
 16  
 17  
 18  
 19 import org.locationtech.jts.geom.Location;
 20  
 21 /**
 22   * A TopologyLocation is the labelling of a
 23   * GraphComponent's topological relationship to a single Geometry.
 24   * <p>
 25   * If the parent component is an area edge, each side and the edge itself
 26   * have a topological location.  These locations are named
 27   * <ul>
 28   * <li> ON: on the edge
 29   * <li> LEFT: left-hand side of the edge
 30   * <li> RIGHT: right-hand side
 31   * </ul>
 32   * If the parent component is a line edge or node, there is a single
 33   * topological relationship attribute, ON.
 34   * <p>
 35   * The possible values of a topological location are
 36   * {Location.NONE, Location.EXTERIOR, Location.BOUNDARY, Location.INTERIOR}
 37   * <p>
 38   * The labelling is stored in an array location[j] where
 39   * where j has the values ON, LEFT, RIGHT
 40   * @version 1.7
 41  */
 42 public class TopologyLocation {
 43  
 44   int location[];
 45  
 46   public TopologyLocation(int[] location)
 47   {
 48     init(location.length);
 49   }
 50   /**
 51    * Constructs a TopologyLocation specifying how points on, to the left of, and to the
 52    * right of some GraphComponent relate to some Geometry. Possible values for the
 53    * parameters are Location.NULL, Location.EXTERIOR, Location.BOUNDARY,
 54    * and Location.INTERIOR.
 55    * @see Location
 56    */
 57   public TopologyLocation(int on, int left, int right) {
 58    init(3);
 59    location[Position.ON] = on;
 60    location[Position.LEFT] = left;
 61    location[Position.RIGHT] = right;
 62   }
 63  
 64   public TopologyLocation(int on) {
 65    init(1);
 66    location[Position.ON] = on;
 67   }
 68   public TopologyLocation(TopologyLocation gl) {
 69     init(gl.location.length);
 70     if (gl != null) {
 71       for (int i = 0; i < location.length; i++) {
 72         location[i] = gl.location[i];
 73       }
 74     }
 75   }
 76   private void init(int size)
 77   {
 78     location = new int[size];
 79     setAllLocations(Location.NONE);
 80   }
 81   public int get(int posIndex)
 82   {
 83     if (posIndex < location.length) return location[posIndex];
 84     return Location.NONE;
 85   }
 86   /**
 87    * @return true if all locations are NULL
 88    */
 89   public boolean isNull()
 90   {
 91     for (int i = 0; i < location.length; i++) {
 92       if (location[i] != Location.NONE) return false;
 93     }
 94     return true;
 95   }
 96   /**
 97    * @return true if any locations are NULL
 98    */
 99   public boolean isAnyNull()
100   {
101     for (int i = 0; i < location.length; i++) {
102       if (location[i] == Location.NONE) return true;
103     }
104     return false;
105   }
106   public boolean isEqualOnSide(TopologyLocation le, int locIndex)
107   {
108     return location[locIndex] == le.location[locIndex];
109   }
110   public boolean isArea() { return location.length > 1; }
111   public boolean isLine() { return location.length == 1; }
112  
113   public void flip()
114   {
115     if (location.length <= 1return;
116     int temp = location[Position.LEFT];
117     location[Position.LEFT] = location[Position.RIGHT];
118     location[Position.RIGHT] = temp;
119   }
120  
121  
122   public void setAllLocations(int locValue)
123   {
124     for (int i = 0; i < location.length; i++) {
125       location[i]     = locValue;
126     }
127   }
128   public void setAllLocationsIfNull(int locValue)
129   {
130     for (int i = 0; i < location.length; i++) {
131       if (location[i] == Location.NONE) location[i]     = locValue;
132     }
133   }
134  
135   public void setLocation(int locIndex, int locValue)
136   {
137       location[locIndex] = locValue;
138   }
139   public void setLocation(int locValue)
140   {
141     setLocation(Position.ON, locValue);
142   }
143   public int[] getLocations() { return location; }
144   public void setLocations(int on, int left, int right) {
145       location[Position.ON] = on;
146       location[Position.LEFT] = left;
147       location[Position.RIGHT] = right;
148   }
149   public boolean allPositionsEqual(int loc)
150   {
151     for (int i = 0; i < location.length; i++) {
152       if (location[i] != loc) return false;
153     }
154     return true;
155   }
156  
157   /**
158    * merge updates only the NULL attributes of this object
159    * with the attributes of another.
160    */
161   public void merge(TopologyLocation gl)
162   {
163     // if the src is an Area label & and the dest is not, increase the dest to be an Area
164     if (gl.location.length > location.length) {
165       int [] newLoc = new int[3];
166       newLoc[Position.ON] = location[Position.ON];
167       newLoc[Position.LEFT] = Location.NONE;
168       newLoc[Position.RIGHT] = Location.NONE;
169       location = newLoc;
170     }
171     for (int i = 0; i < location.length; i++) {
172       if (location[i] == Location.NONE && i < gl.location.length)
173         location[i] = gl.location[i];
174     }
175   }
176  
177   public String toString()
178   {
179     StringBuffer buf = new StringBuffer();
180     if (location.length > 1) buf.append(Location.toLocationSymbol(location[Position.LEFT]));
181     buf.append(Location.toLocationSymbol(location[Position.ON]));
182     if (location.length > 1) buf.append(Location.toLocationSymbol(location[Position.RIGHT]));
183     return buf.toString();
184   }
185 }
186