Spec-Zone .ru
спецификации, руководства, описания, API
001/*
002 * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package javafx.geometry;
027
028/**
029 * A rectangular bounding box which is used to describe the bounds of a node
030 * or other scene graph object.
031 */
032public class BoundingBox extends Bounds {
033    /**
034     * Cache the hash code to make computing hashes faster.
035     */
036    private int hash = 0;
037
038    /**
039     * Creates a new instance of 3D {@code BoundingBox}.
040     * @param minX the X coordinate of the upper-left corner
041     * @param minY the Y coordinate of the upper-left corner
042     * @param minZ the minimum z coordinate of the {@code BoundingBox}
043     * @param width the width of the {@code BoundingBox}
044     * @param height the height of the {@code BoundingBox}
045     * @param depth the depth of the {@code BoundingBox}
046     */
047    public BoundingBox(double minX, double minY, double minZ, double width, double height, double depth) {
048        super(minX, minY, minZ, width, height, depth);
049    }
050
051    /**
052     * Creates a new instance of 2D {@code BoundingBox}.
053     * @param minX the X coordinate of the upper-left corner
054     * @param minY the Y coordinate of the upper-left corner
055     * @param width the width of the {@code BoundingBox}
056     * @param height the height of the {@code BoundingBox}
057     */
058    public BoundingBox(double minX, double minY, double width, double height) {
059        super(minX, minY, 0, width, height, 0);
060    }
061
062    /**
063     * {@inheritDoc}
064     */
065    @Override
066    public boolean isEmpty() {
067        return getMaxX() < getMinX() || getMaxY() < getMinY() || getMaxZ() < getMinZ();
068    }
069
070    /**
071     * {@inheritDoc}
072     * The points on the boundary are considered to lie inside the {@code BoundingBox}.
073     */
074    @Override public boolean contains(Point2D p) {
075        if (p == null) return false;
076        return contains(p.getX(), p.getY(), 0.0f);
077    }
078
079    /**
080     * {@inheritDoc}
081     * The points on the boundary are considered to lie inside the {@code BoundingBox}.
082     */
083    @Override public boolean contains(Point3D p) {
084        if (p == null) return false;
085        return contains(p.getX(), p.getY(), p.getZ());
086    }
087
088    /**
089     * {@inheritDoc}
090     * The points on the boundary are considered to lie inside the {@code BoundingBox}.
091     */
092    @Override public boolean contains(double x, double y) {
093        return contains(x, y, 0.0f);
094    }
095
096    /**
097     * {@inheritDoc}
098     * The points on the boundary are considered to lie inside the {@code BoundingBox}.
099     */
100    @Override public boolean contains(double x, double y, double z) {
101        if (isEmpty()) return false;
102        return x >= getMinX() && x <= getMaxX() && y >= getMinY() && y <= getMaxY()
103                && z >= getMinZ() && z <= getMaxZ();
104    }
105
106    /**
107     * {@inheritDoc}
108     * The points on the boundary are considered to lie inside the {@code BoundingBox}.
109     */
110    @Override public boolean contains(Bounds b) {
111        if ((b == null) || b.isEmpty()) return false;
112        return contains(b.getMinX(), b.getMinY(), b.getMinZ(),
113                b.getWidth(), b.getHeight(), b.getDepth());
114    }
115
116    /**
117     * {@inheritDoc}
118     * The points on the boundary are considered to lie inside the {@code BoundingBox}.
119     */
120    @Override public boolean contains(double x, double y, double w, double h) {
121        return contains(x, y) && contains(x + w, y + h);
122
123    }
124
125    /**
126     * {@inheritDoc}
127     * The points on the boundary are considered to lie inside the {@code BoundingBox}.
128     */
129    @Override public boolean contains(double x, double y, double z,
130            double w, double h, double d) {
131        return contains(x, y, z) && contains(x + w, y + h, z + d);
132    }
133
134    /**
135     * {@inheritDoc}
136     */
137    @Override public boolean intersects(Bounds b) {
138        if ((b == null) || b.isEmpty()) return false;
139        return intersects(b.getMinX(), b.getMinY(), b.getMinZ(),
140                b.getWidth(), b.getHeight(), b.getDepth());
141    }
142
143    /**
144     * {@inheritDoc}
145     */
146    @Override public boolean intersects(double x, double y, double w, double h) {
147        return intersects(x, y, 0, w, h, 0);
148    }
149
150    /**
151     * {@inheritDoc}
152     */
153    @Override public boolean intersects(double x, double y, double z,
154            double w, double h, double d) {
155        if (isEmpty() || w < 0 || h < 0 || d < 0) return false;
156        return (x + w >= getMinX() &&
157                y + h >= getMinY() &&
158                z + d >= getMinZ() &&
159                x <= getMaxX() &&
160                y <= getMaxY() &&
161                z <= getMaxZ());
162    }
163
164    /**
165     * Indicates whether some other object is "equal to" this one.
166     * 
167     * @param obj the reference object with which to compare
168     * @return true if this object is the same as the obj argument; false otherwise
169     */
170    @Override public boolean equals(Object obj) {
171        if (obj == this) return true;
172        if (obj instanceof BoundingBox) {
173            BoundingBox other = (BoundingBox) obj;
174            return getMinX() == other.getMinX()
175                && getMinY() == other.getMinY()
176                && getMinZ() == other.getMinZ()
177                && getWidth() == other.getWidth()
178                && getHeight() == other.getHeight()
179                && getDepth() == other.getDepth();
180        } else return false;
181    }
182
183    /**
184     * Returns a hash code value for the object.
185     * @return a hash code value for the object.
186     */
187    @Override public int hashCode() {
188        if (hash == 0) {
189            long bits = 7L;
190            bits = 31L * bits + Double.doubleToLongBits(getMinX());
191            bits = 31L * bits + Double.doubleToLongBits(getMinY());
192            bits = 31L * bits + Double.doubleToLongBits(getMinZ());
193            bits = 31L * bits + Double.doubleToLongBits(getWidth());
194            bits = 31L * bits + Double.doubleToLongBits(getHeight());
195            bits = 31L * bits + Double.doubleToLongBits(getDepth());
196            hash = (int) (bits ^ (bits >> 32));
197        }
198        return hash;
199    }
200
201    /**
202     * Returns a string representation of this {@code BoundingBox}.
203     * This method is intended to be used only for informational purposes.
204     * The content and format of the returned string might getMary between
205     * implementations.
206     * The returned string might be empty but cannot be {@code null}.
207     */
208    @Override public String toString() {
209        return "BoundingBox ["
210                + "minX:" + getMinX()
211                + ", minY:" + getMinY()
212                + ", minZ:" + getMinZ()
213                + ", width:" + getWidth()
214                + ", height:" + getHeight()
215                + ", depth:" + getDepth()
216                + ", maxX:" + getMaxX()
217                + ", maxY:" + getMaxY()
218                + ", maxZ:" + getMaxZ()
219                + "]";
220    }
221}