Class Depth

  1  
  2  
  3 /*
  4  * Copyright (c) 2016 Vivid Solutions.
  5  *
  6  * All rights reserved. This program and the accompanying materials
  7  * are made available under the terms of the Eclipse Public License 2.0
  8  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
  9  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
 10  * and the Eclipse Distribution License is available at
 11  *
 12  * http://www.eclipse.org/org/documents/edl-v10.php.
 13  */
 14 package org.locationtech.jts.geomgraph;
 15  
 16 import org.locationtech.jts.geom.Location;
 17  
 18 /**
 19  * A Depth object records the topological depth of the sides
 20  * of an Edge for up to two Geometries.
 21  * @version 1.7
 22  */
 23 public class Depth {
 24  
 25   private final static int NULL_VALUE = -1;
 26  
 27   public static int depthAtLocation(int location)
 28   {
 29     if (location == Location.EXTERIOR) return 0;
 30     if (location == Location.INTERIOR) return 1;
 31     return NULL_VALUE;
 32   }
 33  
 34   private int[][] depth = new int[2][3];
 35  
 36   public Depth() {
 37     // initialize depth array to a sentinel value
 38     for (int i = 0; i < 2; i++) {
 39       for (int j = 0; j < 3; j++) {
 40         depth[i][j] = NULL_VALUE;
 41       }
 42     }
 43   }
 44  
 45   public int getDepth(int geomIndex, int posIndex)
 46   {
 47     return depth[geomIndex][posIndex];
 48   }
 49   public void setDepth(int geomIndex, int posIndex, int depthValue)
 50   {
 51     depth[geomIndex][posIndex] = depthValue;
 52   }
 53   public int getLocation(int geomIndex, int posIndex)
 54   {
 55     if (depth[geomIndex][posIndex] <= 0return Location.EXTERIOR;
 56     return Location.INTERIOR;
 57   }
 58   public void add(int geomIndex, int posIndex, int location)
 59   {
 60     if (location == Location.INTERIOR)
 61       depth[geomIndex][posIndex]++;
 62   }
 63   /**
 64    * A Depth object is null (has never been initialized) if all depths are null.
 65    */
 66   public boolean isNull()
 67   {
 68     for (int i = 0; i < 2; i++) {
 69       for (int j = 0; j < 3; j++) {
 70         if (depth[i][j] != NULL_VALUE)
 71           return false;
 72       }
 73     }
 74     return true;
 75   }
 76   public boolean isNull(int geomIndex)
 77   {
 78     return depth[geomIndex][1] == NULL_VALUE;
 79   }
 80   public boolean isNull(int geomIndex, int posIndex)
 81   {
 82     return depth[geomIndex][posIndex] == NULL_VALUE;
 83   }
 84   public void add(Label lbl)
 85   {
 86     for (int i = 0; i < 2; i++) {
 87       for (int j = 1; j < 3; j++) {
 88         int loc = lbl.getLocation(i, j);
 89         if (loc == Location.EXTERIOR || loc == Location.INTERIOR) {
 90           // initialize depth if it is null, otherwise add this location value
 91           if (isNull(i, j)) {
 92             depth[i][j] = depthAtLocation(loc);
 93           }
 94           else
 95             depth[i][j] += depthAtLocation(loc);
 96         }
 97       }
 98     }
 99   }
100   public int getDelta(int geomIndex)
101   {
102     return depth[geomIndex][Position.RIGHT] - depth[geomIndex][Position.LEFT];
103   }
104   /**
105    * Normalize the depths for each geometry, if they are non-null.
106    * A normalized depth
107    * has depth values in the set { 0, 1 }.
108    * Normalizing the depths
109    * involves reducing the depths by the same amount so that at least
110    * one of them is 0.  If the remaining value is > 0, it is set to 1.
111    */
112   public void normalize()
113   {
114     for (int i = 0; i < 2; i++) {
115       if (! isNull(i)) {
116         int minDepth = depth[i][1];
117         if (depth[i][2] < minDepth)
118           minDepth = depth[i][2];
119  
120         if (minDepth < 0minDepth = 0;
121         for (int j = 1; j < 3; j++) {
122           int newValue = 0;
123           if (depth[i][j] > minDepth)
124             newValue = 1;
125           depth[i][j] = newValue;
126         }
127       }
128     }
129   }
130  
131   public String toString()
132   {
133     return
134         "A: " + depth[0][1] + "," + depth[0][2]
135       + " B: " + depth[1][1] + "," + depth[1][2];
136   }
137 }
138