001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/io/rtree/HyperBoundingBox.java $
002: //
003: //RTree implementation.
004: //Copyright (C) 2002-2004 Wolfgang Baer - WBaer@gmx.de
005: //
006: //This library is free software; you can redistribute it and/or
007: //modify it under the terms of the GNU Lesser General Public
008: //License as published by the Free Software Foundation; either
009: //version 2.1 of the License, or (at your option) any later version.
010: //
011: //This library is distributed in the hope that it will be useful,
012: //but WITHOUT ANY WARRANTY; without even the implied warranty of
013: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: //Lesser General Public License for more details.
015: //
016: //You should have received a copy of the GNU Lesser General Public
017: //License along with this library; if not, write to the Free Software
018: //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019:
020: package org.deegree.io.rtree;
021:
022: import java.io.Serializable;
023:
024: /**
025: * <p>
026: * HyperBoundingBox implementing a bounding box
027: * object in the multidimensional space.
028: * </p>
029: * @author Wolfgang Baer - WBaer@gmx.de
030: */
031:
032: public class HyperBoundingBox implements Serializable {
033:
034: private HyperPoint pMin;
035: private HyperPoint pMax;
036:
037: /**
038: * Constructor.<br>
039: * Creates a HyperBoundingBox for given HyperPoints.
040: * @param pMin - min point
041: * @param pMax - max point
042: */
043: public HyperBoundingBox(HyperPoint pMin, HyperPoint pMax) {
044: if (pMin.getDimension() != pMax.getDimension())
045: throw new IllegalArgumentException(
046: "HyperPoints need same dimension: "
047: + pMin.getDimension() + " != "
048: + pMax.getDimension());
049:
050: this .pMin = pMin;
051: this .pMax = pMax;
052: }
053:
054: /**
055: * Creates a null HyperBoundingBox with null HyperPoints.
056: * Mostly used internal.
057: * @param dimension - int for dimension of point
058: * @return HyperBoundingBox
059: */
060: protected static HyperBoundingBox getNullHyperBoundingBox(
061: int dimension) {
062: return new HyperBoundingBox(HyperPoint
063: .getNullHyperPoint(dimension), HyperPoint
064: .getNullHyperPoint(dimension));
065: }
066:
067: /**
068: * Returns the minimum HyperPoint
069: * @return HyperPoint
070: *
071: */
072: public HyperPoint getPMin() {
073: return pMin;
074: }
075:
076: /**
077: * Returns the maximum HyperPoint
078: * @return HyperPoint
079: *
080: */
081: public HyperPoint getPMax() {
082: return pMax;
083: }
084:
085: /**
086: * Returns the dimension of this HyperBoundingBox.
087: * @return int
088: */
089: public int getDimension() {
090: return pMin.getDimension();
091: }
092:
093: /**
094: * Tests if this and the given HyperBoundingBox overlaps.
095: * @param box - HyperBoundingBox to test
096: * @return boolean
097: */
098: public boolean overlaps(HyperBoundingBox box) {
099: boolean intersect = true;
100:
101: for (int i = 0; i < getDimension(); i++) {
102: if ((pMin.getCoord(i) > box.getPMax().getCoord(i))
103: || (pMax.getCoord(i) < box.getPMin().getCoord(i))) {
104: intersect = false;
105: break;
106: }
107: }
108:
109: return intersect;
110: }
111:
112: /**
113: * Tests if this contains the given HyperBoundingBox overlaps.
114: * @param box - HyperBoundingBox to test
115: * @return boolean
116: */
117: public boolean contains(HyperBoundingBox box) {
118: boolean contains = true;
119:
120: for (int i = 0; i < getDimension(); i++) {
121: if ((pMin.getCoord(i) > box.getPMin().getCoord(i))
122: || (pMax.getCoord(i) < box.getPMax().getCoord(i))) {
123: contains = false;
124: break;
125: }
126: }
127:
128: return contains;
129: }
130:
131: /**
132: * Computes the area (over all dimension) of this.
133: * @return double
134: */
135: public double getArea() {
136: double area = 1;
137:
138: for (int i = 0; i < pMin.getDimension(); i++)
139: area = area * (pMax.getCoord(i) - pMin.getCoord(i));
140:
141: return area;
142: }
143:
144: /**
145: * Computes the union of this with the given HyperBoundingBox.
146: * @param box - given HyperBoundingBox
147: * @return HyperBoundingBox
148: */
149: public HyperBoundingBox unionBoundingBox(HyperBoundingBox box) {
150:
151: if (this .getDimension() != box.getDimension())
152: throw new IllegalArgumentException(
153: "HyperBoundingBoxes need same dimension "
154: + this .getDimension() + " != "
155: + box.getDimension());
156:
157: if (this .equals(HyperBoundingBox.getNullHyperBoundingBox(this
158: .getDimension())))
159: return box;
160: if (box.equals(HyperBoundingBox.getNullHyperBoundingBox(this
161: .getDimension())))
162: return this ;
163:
164: double[] min = new double[this .getDimension()];
165: double[] max = new double[this .getDimension()];
166:
167: for (int i = 0; i < this .getDimension(); i++) {
168:
169: if (this .getPMin().getCoord(i) <= box.getPMin().getCoord(i)) {
170: min[i] = this .getPMin().getCoord(i);
171: } else {
172: min[i] = box.getPMin().getCoord(i);
173: }
174: if (this .getPMax().getCoord(i) >= box.getPMax().getCoord(i)) {
175: max[i] = this .getPMax().getCoord(i);
176: } else {
177: max[i] = box.getPMax().getCoord(i);
178: }
179: }
180: return new HyperBoundingBox(new HyperPoint(min),
181: new HyperPoint(max));
182: }
183:
184: /**
185: * Computes the minimal distance square of this to the given HyperPoint.
186: * After Roussopoulos Nick: Nearest Neighbor Queries - MINDIST
187: * @param point - HyperPoint
188: * @return double
189: */
190: public double minDist(HyperPoint point) {
191: double min = 0;
192: double ri = 0;
193:
194: for (int i = 0; i < point.getDimension(); i++) {
195: if (point.getCoord(i) < this .pMin.getCoord(i)) {
196: ri = this .pMin.getCoord(i);
197: } else {
198: if (point.getCoord(i) > this .pMax.getCoord(i)) {
199: ri = this .pMax.getCoord(i);
200: } else {
201: ri = point.getCoord(i);
202: }
203: }
204: min = min + Math.pow(point.getCoord(i) - ri, 2);
205: }
206: return min;
207: }
208:
209: /**
210: * Deep copy.
211: * @see java.lang.Object#clone()
212: */
213: protected Object clone() {
214: return new HyperBoundingBox((HyperPoint) pMin.clone(),
215: (HyperPoint) pMax.clone());
216: }
217:
218: /**
219: * Builds a String representation of the HyperBoundingBox.
220: * @return String
221: */
222: public String toString() {
223: return "BOX: P-Min (" + pMin.toString() + "), P-Max ("
224: + pMax.toString() + ")";
225: }
226:
227: /**
228: * Implements equals
229: * @see java.lang.Object#equals(java.lang.Object)
230: */
231: public boolean equals(Object obj) {
232: HyperBoundingBox box = (HyperBoundingBox) obj;
233: return (this.pMin.equals(box.pMin) && this.pMax
234: .equals(box.pMax));
235: }
236: }
|