|
| 1 | +/* |
| 2 | +LibBoubek/BoundingBox.h |
| 3 | +Copyright (c) 2003-2008, Tamy Boubekeur |
| 4 | +
|
| 5 | +All rights reserved. |
| 6 | +
|
| 7 | +Redistribution and use in source and binary forms, with or without |
| 8 | +modification, are permitted provided that the following conditions are met: |
| 9 | +
|
| 10 | +* Redistributions of source code must retain the above copyright notice, |
| 11 | + this list of conditions and the following disclaimer. |
| 12 | +
|
| 13 | +* Redistributions in binary form must reproduce the above copyright notice, |
| 14 | + this list of conditions and the following disclaimer in the documentation |
| 15 | + and/or other materials provided with the distribution. |
| 16 | +
|
| 17 | +* The name of the source code copyright owner cannot be used to endorse or |
| 18 | + promote products derived from this software without specific prior |
| 19 | + written permission. |
| 20 | +
|
| 21 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 | +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 | +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 | +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 25 | +CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 26 | +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 27 | +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 | +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 | +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 | +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 | +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | +*/ |
| 33 | + |
| 34 | +#ifndef BOUNDINGBOX_H |
| 35 | +#define BOUNDINGBOX_H |
| 36 | + |
| 37 | +#include <vector> |
| 38 | +#include <algorithm> |
| 39 | + |
| 40 | +#include "Vec3D.h" |
| 41 | + |
| 42 | +const float BOUNDINGBOX_EPSILON = 0.0001f; |
| 43 | + |
| 44 | +class BoundingBox { |
| 45 | +public: |
| 46 | + BoundingBox () : minBb (Vec3Df (0.0f, 0.0f, 0.0f)), maxBb (Vec3Df (0.0f, 0.0f, 0.0f)) {} |
| 47 | + BoundingBox (const Vec3Df & p) : minBb (p), maxBb (p) {} |
| 48 | + BoundingBox (const Vec3Df & min, const Vec3Df & max) : minBb (min), maxBb (max) {} |
| 49 | + |
| 50 | + inline void init (const Vec3Df & p) { |
| 51 | + minBb = maxBb = p; |
| 52 | + } |
| 53 | + inline void init (float x, float y, float z) { |
| 54 | + init (Vec3Df (x, y, z)); |
| 55 | + } |
| 56 | + inline float getWidth () const { |
| 57 | + return (maxBb[0] - minBb[0]); |
| 58 | + } |
| 59 | + inline float getHeight () const { |
| 60 | + return (maxBb[1] - minBb[1]); |
| 61 | + } |
| 62 | + inline float getLength () const { |
| 63 | + return (maxBb[2] - minBb[2]); |
| 64 | + } |
| 65 | + inline float getSize () const { |
| 66 | + return std::max (getWidth (), std::max (getHeight (), getLength ())); |
| 67 | + } |
| 68 | + inline float getRadius () const { |
| 69 | + return Vec3Df::distance (minBb, maxBb) / 2.0; |
| 70 | + } |
| 71 | + inline void extendTo (const Vec3Df & p) { |
| 72 | + for (unsigned int i = 0; i < 3; i++) { |
| 73 | + if (p[i] > maxBb[i]) maxBb[i] = p[i]; |
| 74 | + if (p[i] < minBb[i]) minBb[i] = p[i]; |
| 75 | + } |
| 76 | + } |
| 77 | + inline void extendTo (const BoundingBox & b) { |
| 78 | + extendTo (b.minBb); |
| 79 | + extendTo (b.maxBb); |
| 80 | + } |
| 81 | + inline bool contains (const Vec3Df & p) const { |
| 82 | + for (unsigned int i = 0; i < 3; i++) |
| 83 | + if (!(p[i] >= minBb[i] && p[i] <= maxBb[i])) |
| 84 | + return false; |
| 85 | + return true; |
| 86 | + } |
| 87 | + inline bool contains (const BoundingBox & b) const { |
| 88 | + for (unsigned int i = 0; i < 3; i++) |
| 89 | + if (fabs (getMiddle (i) - b.getMiddle (i)) - BOUNDINGBOX_EPSILON > (getWHL (i) + b.getWHL (i)) / 2.0) |
| 90 | + return false; |
| 91 | + return true; |
| 92 | + } |
| 93 | + inline bool scaleContains (const Vec3Df & p, float scale) const { |
| 94 | + float limitW = scale * getWidth () / 2.0; |
| 95 | + float limitH = scale * getHeight () / 2.0; |
| 96 | + float limitL = scale * getLength () / 2.0; |
| 97 | + Vec3Df c = getCenter (); |
| 98 | + if ((fabs (c[0] - p[0]) <= limitW) && (fabs (c[1] - p[1]) <= limitH) && (fabs (c[2] - p[2]) <= limitL)) |
| 99 | + return true; |
| 100 | + return false; |
| 101 | + } |
| 102 | + inline void scale (float factor) { |
| 103 | + Vec3Df center = getCenter (); |
| 104 | + Vec3Df diffMin, diffMax; |
| 105 | + diffMin.fromTo (center, minBb); |
| 106 | + diffMax.fromTo (center, maxBb); |
| 107 | + diffMin *= factor; |
| 108 | + diffMax *= factor; |
| 109 | + minBb = center + diffMin; |
| 110 | + maxBb = center + diffMax; |
| 111 | + } |
| 112 | + inline Vec3Df getCenter () const { |
| 113 | + return (minBb + maxBb) / 2; |
| 114 | + } |
| 115 | + inline const Vec3Df & getMin () const { |
| 116 | + return minBb; |
| 117 | + } |
| 118 | + inline const Vec3Df & getMax () const { |
| 119 | + return maxBb; |
| 120 | + } |
| 121 | + inline void subdivide (std::vector<BoundingBox> & splitBoundingBoxArray) const { |
| 122 | + Vec3Df med = (minBb + maxBb) / 2; |
| 123 | + float x_2 = (maxBb[0] - minBb [0]) / 2; |
| 124 | + float y_2 = (maxBb[1] - minBb [1]) / 2; |
| 125 | + float z_2 = (maxBb[2] - minBb [2]) / 2; |
| 126 | + splitBoundingBoxArray.resize (8); |
| 127 | + splitBoundingBoxArray[0] = BoundingBox (minBb, med); |
| 128 | + splitBoundingBoxArray[1] = BoundingBox (minBb + Vec3Df (x_2, 0.0, 0.0), med + Vec3Df (x_2, 0.0, 0.0)); |
| 129 | + splitBoundingBoxArray[2] = BoundingBox (minBb + Vec3Df (0.0, y_2, 0.0), med + Vec3Df (0.0, y_2, 0.0)); |
| 130 | + splitBoundingBoxArray[3] = BoundingBox (minBb + Vec3Df (x_2, y_2, 0.0), med + Vec3Df (x_2, y_2, 0.0)); |
| 131 | + splitBoundingBoxArray[4] = BoundingBox (minBb + Vec3Df (0.0, 0.0, z_2), med + Vec3Df (0.0, 0.0, z_2)); |
| 132 | + splitBoundingBoxArray[5] = BoundingBox (minBb + Vec3Df (x_2, 0.0, z_2), med + Vec3Df (x_2, 0.0, z_2)); |
| 133 | + splitBoundingBoxArray[6] = BoundingBox (minBb + Vec3Df (0.0, y_2, z_2), med + Vec3Df (0.0, y_2, z_2)); |
| 134 | + splitBoundingBoxArray[7] = BoundingBox (minBb + Vec3Df (x_2, y_2, z_2), med + Vec3Df (x_2, y_2, z_2)); |
| 135 | + } |
| 136 | + bool intersectRay (const Vec3Df & origin, const Vec3Df & direction, Vec3Df & intersection) const; |
| 137 | + |
| 138 | +private: |
| 139 | + inline float getWHL (unsigned int i) const { |
| 140 | + return (maxBb[i] - minBb[i]); |
| 141 | + } |
| 142 | + inline float getMiddle (unsigned int i) const { |
| 143 | + return ((minBb[i] + maxBb[i]) / 2.0); |
| 144 | + } |
| 145 | + static inline bool isIn (float x, float min, float max) { |
| 146 | + return (x >= min && x <= max); |
| 147 | + } |
| 148 | + |
| 149 | + Vec3Df minBb, maxBb; |
| 150 | +}; |
| 151 | + |
| 152 | +#endif // BOUNDINGBOX_H |
| 153 | + |
| 154 | +// Some Emacs-Hints -- please don't remove: |
| 155 | +// |
| 156 | +// Local Variables: |
| 157 | +// mode:C++ |
| 158 | +// tab-width:4 |
| 159 | +// End: |
0 commit comments