Class NodeBase

  1  
  2 /*
  3  * Copyright (c) 2016 Vivid Solutions.
  4  *
  5  * All rights reserved. This program and the accompanying materials
  6  * are made available under the terms of the Eclipse Public License 2.0
  7  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
  8  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
  9  * and the Eclipse Distribution License is available at
 10  *
 11  * http://www.eclipse.org/org/documents/edl-v10.php.
 12  */
 13 package org.locationtech.jts.index.quadtree;
 14  
 15 import java.io.Serializable;
 16 import java.util.ArrayList;
 17 import java.util.Iterator;
 18 import java.util.List;
 19  
 20 import org.locationtech.jts.geom.Envelope;
 21 import org.locationtech.jts.index.ItemVisitor;
 22  
 23  
 24 /**
 25  * The base class for nodes in a {@link Quadtree}.
 26  *
 27  * @version 1.7
 28  */
 29 public abstract class NodeBase implements Serializable {
 30  
 31 //DEBUG private static int itemCount = 0;  // debugging
 32   
 33   /**
 34    * Gets the index of the subquad that wholly contains the given envelope.
 35    * If none does, returns -1.
 36    * 
 37    * @return the index of the subquad that wholly contains the given envelope
 38    * or -1 if no subquad wholly contains the envelope
 39    */
 40   public static int getSubnodeIndex(Envelope env, double centrex, double centrey)
 41   {
 42     int subnodeIndex = -1;
 43     if (env.getMinX() >= centrex) {
 44       if (env.getMinY() >= centrey) subnodeIndex = 3;
 45       if (env.getMaxY() <= centrey) subnodeIndex = 1;
 46     }
 47     if (env.getMaxX() <= centrex) {
 48       if (env.getMinY() >= centrey) subnodeIndex = 2;
 49       if (env.getMaxY() <= centrey) subnodeIndex = 0;
 50     }
 51     return subnodeIndex;
 52   }
 53  
 54   protected List items = new ArrayList();
 55  
 56   /**
 57    * subquads are numbered as follows:
 58    * <pre>
 59    *  2 | 3
 60    *  --+--
 61    *  0 | 1
 62    * </pre>
 63    */
 64   protected Node[] subnode = new Node[4];
 65  
 66   public NodeBase() {
 67   }
 68  
 69   public List getItems() { return items; }
 70  
 71   public boolean hasItems() { return ! items.isEmpty(); }
 72  
 73   public void add(Object item)
 74   {
 75     items.add(item);
 76 //DEBUG itemCount++;
 77 //DEBUG System.out.print(itemCount);
 78   }
 79  
 80   /**
 81    * Removes a single item from this subtree.
 82    *
 83    * @param itemEnv the envelope containing the item
 84    * @param item the item to remove
 85    * @return <code>true</code> if the item was found and removed
 86    */
 87   public boolean remove(Envelope itemEnv, Object item)
 88   {
 89     // use envelope to restrict nodes scanned
 90     if (! isSearchMatch(itemEnv))
 91       return false;
 92  
 93     boolean found = false;
 94     for (int i = 0; i < 4; i++) {
 95       if (subnode[i] != null) {
 96         found = subnode[i].remove(itemEnv, item);
 97         if (found) {
 98           // trim subtree if empty
 99           if (subnode[i].isPrunable())
100             subnode[i] = null;
101           break;
102         }
103       }
104     }
105     // if item was found lower down, don't need to search for it here
106     if (found) return found;
107     // otherwise, try and remove the item from the list of items in this node
108     found = items.remove(item);
109     return found;
110   }
111  
112   public boolean isPrunable()
113   {
114     return ! (hasChildren() || hasItems());
115   }
116  
117   public boolean hasChildren()
118   {
119     for (int i = 0; i < 4; i++) {
120       if (subnode[i] != null)
121         return true;
122     }
123     return false;
124   }
125  
126   public boolean isEmpty()
127   {
128     boolean isEmpty = true;
129     if (! items.isEmpty()) isEmpty = false;
130     else {
131       for (int i = 0; i < 4; i++) {
132         if (subnode[i] != null) {
133           if (!subnode[i].isEmpty()) {
134             isEmpty = false;
135             break;
136           }
137         }
138       }
139     }
140     return isEmpty;
141   }
142  
143   //<<TODO:RENAME?>> Sounds like this method adds resultItems to items
144   //(like List#addAll). Perhaps it should be renamed to "addAllItemsTo" [Jon Aquino]
145   public List addAllItems(List resultItems)
146   {
147     // this node may have items as well as subnodes (since items may not
148     // be wholely contained in any single subnode
149     resultItems.addAll(this.items);
150     for (int i = 0; i < 4; i++) {
151       if (subnode[i] != null) {
152         subnode[i].addAllItems(resultItems);
153       }
154     }
155     return resultItems;
156   }
157   protected abstract boolean isSearchMatch(Envelope searchEnv);
158  
159   public void addAllItemsFromOverlapping(Envelope searchEnv, List resultItems)
160   {
161     if (! isSearchMatch(searchEnv))
162       return;
163  
164     // this node may have items as well as subnodes (since items may not
165     // be wholely contained in any single subnode
166     resultItems.addAll(items);
167  
168     for (int i = 0; i < 4; i++) {
169       if (subnode[i] != null) {
170         subnode[i].addAllItemsFromOverlapping(searchEnv, resultItems);
171       }
172     }
173   }
174  
175   public void visit(Envelope searchEnv, ItemVisitor visitor)
176   {
177     if (! isSearchMatch(searchEnv))
178       return;
179  
180     // this node may have items as well as subnodes (since items may not
181     // be wholely contained in any single subnode
182     visitItems(searchEnv, visitor);
183  
184     for (int i = 0; i < 4; i++) {
185       if (subnode[i] != null) {
186         subnode[i].visit(searchEnv, visitor);
187       }
188     }
189   }
190  
191   private void visitItems(Envelope searchEnv, ItemVisitor visitor)
192   {
193     // would be nice to filter items based on search envelope, but can't until they contain an envelope
194     for (Iterator i = items.iterator(); i.hasNext(); ) {
195       visitor.visitItem(i.next());
196     }
197   }
198  
199 //<<TODO:RENAME?>> In Samet's terminology, I think what we're returning here is
200 //actually level+1 rather than depth. (See p. 4 of his book) [Jon Aquino]
201   int depth()
202   {
203     int maxSubDepth = 0;
204     for (int i = 0; i < 4; i++) {
205       if (subnode[i] != null) {
206         int sqd = subnode[i].depth();
207         if (sqd > maxSubDepth)
208           maxSubDepth = sqd;
209       }
210     }
211     return maxSubDepth + 1;
212   }
213  
214   int size()
215   {
216     int subSize = 0;
217     for (int i = 0; i < 4; i++) {
218       if (subnode[i] != null) {
219         subSize += subnode[i].size();
220       }
221     }
222     return subSize + items.size();
223   }
224  
225   int getNodeCount()
226   {
227     int subSize = 0;
228     for (int i = 0; i < 4; i++) {
229       if (subnode[i] != null) {
230         subSize += subnode[i].size();
231       }
232     }
233     return subSize + 1;
234   }
235  
236 }
237