2 // Java Spatial Index Library
3 // Copyright (C) 2002 Infomatiq Limited
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 package com.infomatiq.jsi.rtree;
21 import com.infomatiq.jsi.Rectangle;
24 * <p>Used by RTree. There are no public methods in this class.</p>
26 * @author aled.morris@infomatiq.co.uk
32 Rectangle[] entries = null;
37 Node(int nodeId, int level, int maxNodeEntries) {
40 entries = new Rectangle[maxNodeEntries];
41 ids = new int[maxNodeEntries];
44 void addEntry(Rectangle r, int id) {
46 entries[entryCount] = r.copy();
55 void addEntryNoCopy(Rectangle r, int id) {
57 entries[entryCount] = r;
66 // Return the index of the found entry, or -1 if not found
67 int findEntry(Rectangle r, int id) {
68 for (int i = 0; i < entryCount; i++) {
69 if (id == ids[i] && r.equals(entries[i])) {
76 // delete entry. This is done by setting it to null and copying the last entry into its space.
77 void deleteEntry(int i, int minNodeEntries) {
78 int lastIndex = entryCount - 1;
79 Rectangle deletedRectangle = entries[i];
82 entries[i] = entries[lastIndex];
83 ids[i] = ids[lastIndex];
84 entries[lastIndex] = null;
88 // if there are at least minNodeEntries, adjust the MBR.
89 // otherwise, don't bother, as the node will be
91 if (entryCount >= minNodeEntries) {
92 recalculateMBR(deletedRectangle);
96 // oldRectangle is a rectangle that has just been deleted or made smaller.
97 // Thus, the MBR is only recalculated if the OldRectangle influenced the old MBR
98 void recalculateMBR(Rectangle deletedRectangle) {
99 if (mbr.edgeOverlaps(deletedRectangle)) {
100 mbr.set(entries[0].min, entries[0].max);
102 for (int i = 1; i < entryCount; i++) {
108 public int getEntryCount() {
112 public Rectangle getEntry(int index) {
113 if (index < entryCount) {
114 return entries[index];
119 public int getId(int index) {
120 if (index < entryCount) {
127 * eliminate null entries, move all entries to the start of the source node
129 void reorganize(RTree rtree) {
130 int countdownIndex = rtree.maxNodeEntries - 1;
131 for (int index = 0; index < entryCount; index++) {
132 if (entries[index] == null) {
133 while (entries[countdownIndex] == null && countdownIndex > index) {
136 entries[index] = entries[countdownIndex];
137 ids[index] = ids[countdownIndex];
138 entries[countdownIndex] = null;
147 public int getLevel() {
151 public Rectangle getMBR() {