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