| 1 | /** |
|---|
| 2 | @file Box.h |
|---|
| 3 | |
|---|
| 4 | Box class |
|---|
| 5 | |
|---|
| 6 | @maintainer Morgan McGuire, matrix@graphics3d.com |
|---|
| 7 | |
|---|
| 8 | @cite Portions based on Dave Eberly's Magic Software Library at <A HREF="http://www.magic-software.com">http://www.magic-software.com</A> |
|---|
| 9 | @created 2001-06-02 |
|---|
| 10 | @edited 2006-01-05 |
|---|
| 11 | |
|---|
| 12 | Copyright 2000-2006, Morgan McGuire. |
|---|
| 13 | All rights reserved. |
|---|
| 14 | */ |
|---|
| 15 | |
|---|
| 16 | #ifndef G3D_BOX_H |
|---|
| 17 | #define G3D_BOX_H |
|---|
| 18 | |
|---|
| 19 | #include "G3D/platform.h" |
|---|
| 20 | #include "G3D/Vector3.h" |
|---|
| 21 | #include "G3D/Array.h" |
|---|
| 22 | #include "G3D/Plane.h" |
|---|
| 23 | |
|---|
| 24 | namespace G3D { |
|---|
| 25 | |
|---|
| 26 | class CoordinateFrame; |
|---|
| 27 | |
|---|
| 28 | /** |
|---|
| 29 | An arbitrary 3D box, useful as a bounding box. |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | To construct a box from a coordinate frame, center and extent, use the idiom: |
|---|
| 33 | |
|---|
| 34 | <CODE>Box box = cframe.toObjectSpace(Box(center - extent/2, center + extent/2));</CODE> |
|---|
| 35 | */ |
|---|
| 36 | class Box { |
|---|
| 37 | private: |
|---|
| 38 | |
|---|
| 39 | static int32 dummy; |
|---|
| 40 | |
|---|
| 41 | friend class CoordinateFrame; |
|---|
| 42 | |
|---|
| 43 | /** |
|---|
| 44 | <PRE> |
|---|
| 45 | 3 2 7 6 |
|---|
| 46 | |
|---|
| 47 | 0 1 4 5 |
|---|
| 48 | |
|---|
| 49 | front back (seen through front) |
|---|
| 50 | </PRE> |
|---|
| 51 | */ |
|---|
| 52 | Vector3 _corner[8]; |
|---|
| 53 | |
|---|
| 54 | /** |
|---|
| 55 | Unit axes. |
|---|
| 56 | */ |
|---|
| 57 | Vector3 _axis[3]; |
|---|
| 58 | |
|---|
| 59 | Vector3 _center; |
|---|
| 60 | |
|---|
| 61 | /** |
|---|
| 62 | Extent along each axis. |
|---|
| 63 | */ |
|---|
| 64 | Vector3 _extent; |
|---|
| 65 | |
|---|
| 66 | float _area; |
|---|
| 67 | float _volume; |
|---|
| 68 | |
|---|
| 69 | void init( |
|---|
| 70 | const Vector3& min, |
|---|
| 71 | const Vector3& max); |
|---|
| 72 | |
|---|
| 73 | public: |
|---|
| 74 | |
|---|
| 75 | /** |
|---|
| 76 | Does not initialize the fields. |
|---|
| 77 | */ |
|---|
| 78 | Box(); |
|---|
| 79 | |
|---|
| 80 | /** |
|---|
| 81 | Constructs a box from two opposite corners. |
|---|
| 82 | */ |
|---|
| 83 | Box( |
|---|
| 84 | const Vector3& min, |
|---|
| 85 | const Vector3& max); |
|---|
| 86 | |
|---|
| 87 | Box(const class AABox& b); |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | /** |
|---|
| 91 | Returns the object to world transformation for |
|---|
| 92 | this box. localFrame().worldToObject(...) takes |
|---|
| 93 | objects into the space where the box axes are |
|---|
| 94 | (1,0,0), (0,1,0), (0,0,1). Note that there |
|---|
| 95 | is no scaling in this transformation. |
|---|
| 96 | */ |
|---|
| 97 | CoordinateFrame localFrame() const; |
|---|
| 98 | |
|---|
| 99 | void getLocalFrame(CoordinateFrame& frame) const; |
|---|
| 100 | |
|---|
| 101 | /** |
|---|
| 102 | Returns the centroid of the box. |
|---|
| 103 | */ |
|---|
| 104 | inline Vector3 center() const { |
|---|
| 105 | return _center; |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | inline Vector3 getCenter() const { |
|---|
| 109 | return center(); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | /** |
|---|
| 113 | Returns a corner (0 <= i < 8) |
|---|
| 114 | @deprecated |
|---|
| 115 | */ |
|---|
| 116 | inline Vector3 getCorner(int i) const { |
|---|
| 117 | debugAssert(i < 8); |
|---|
| 118 | return _corner[i]; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | inline Vector3 corner(int i) const { |
|---|
| 122 | debugAssert(i < 8); |
|---|
| 123 | return _corner[i]; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | /** |
|---|
| 127 | Unit length. |
|---|
| 128 | */ |
|---|
| 129 | inline Vector3 axis(int a) const { |
|---|
| 130 | debugAssert(a < 3); |
|---|
| 131 | return _axis[a]; |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | /** |
|---|
| 135 | Distance from corner(0) to the next corner |
|---|
| 136 | along the box's local axis a. |
|---|
| 137 | */ |
|---|
| 138 | inline float extent(int a) const { |
|---|
| 139 | debugAssert(a < 3); |
|---|
| 140 | return (float)_extent[a]; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | inline Vector3 extent() const { |
|---|
| 144 | return _extent; |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | /** |
|---|
| 148 | Returns the four corners of a face (0 <= f < 6). |
|---|
| 149 | The corners are returned to form a counter clockwise quad facing outwards. |
|---|
| 150 | */ |
|---|
| 151 | void getFaceCorners( |
|---|
| 152 | int f, |
|---|
| 153 | Vector3& v0, |
|---|
| 154 | Vector3& v1, |
|---|
| 155 | Vector3& v2, |
|---|
| 156 | Vector3& v3) const; |
|---|
| 157 | |
|---|
| 158 | /** |
|---|
| 159 | @deprecated Use culledBy(Array<Plane>&) |
|---|
| 160 | */ |
|---|
| 161 | bool culledBy( |
|---|
| 162 | const class Plane* plane, |
|---|
| 163 | int numPlanes, |
|---|
| 164 | int32& cullingPlaneIndex, |
|---|
| 165 | const uint32 testMask, |
|---|
| 166 | uint32& childMask) const; |
|---|
| 167 | |
|---|
| 168 | /** |
|---|
| 169 | @deprecated Use culledBy(Array<Plane>&) |
|---|
| 170 | */ |
|---|
| 171 | bool culledBy( |
|---|
| 172 | const class Plane* plane, |
|---|
| 173 | int numPlanes, |
|---|
| 174 | int32& cullingPlaneIndex = dummy, |
|---|
| 175 | const uint32 testMask = -1) const; |
|---|
| 176 | |
|---|
| 177 | /** |
|---|
| 178 | See AABox::culledBy |
|---|
| 179 | */ |
|---|
| 180 | bool culledBy( |
|---|
| 181 | const Array<Plane>& plane, |
|---|
| 182 | int32& cullingPlaneIndex, |
|---|
| 183 | const uint32 testMask, |
|---|
| 184 | uint32& childMask) const; |
|---|
| 185 | |
|---|
| 186 | /** |
|---|
| 187 | Conservative culling test that does not produce a mask for children. |
|---|
| 188 | */ |
|---|
| 189 | bool culledBy( |
|---|
| 190 | const Array<Plane>& plane, |
|---|
| 191 | int32& cullingPlaneIndex = dummy, |
|---|
| 192 | const uint32 testMask = -1) const; |
|---|
| 193 | |
|---|
| 194 | bool contains( |
|---|
| 195 | const Vector3& point) const; |
|---|
| 196 | |
|---|
| 197 | /** @deprecated */ |
|---|
| 198 | float surfaceArea() const; |
|---|
| 199 | |
|---|
| 200 | inline float area() const { |
|---|
| 201 | return surfaceArea(); |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | float volume() const; |
|---|
| 205 | |
|---|
| 206 | void getRandomSurfacePoint(Vector3& P, Vector3& N = Vector3::dummy) const; |
|---|
| 207 | |
|---|
| 208 | /** |
|---|
| 209 | @deprecated |
|---|
| 210 | Uniformly distributed on the surface. |
|---|
| 211 | */ |
|---|
| 212 | inline Vector3 randomSurfacePoint() const { |
|---|
| 213 | Vector3 V; |
|---|
| 214 | getRandomSurfacePoint(V); |
|---|
| 215 | return V; |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | /** |
|---|
| 219 | Uniformly distributed on the interior (includes surface) |
|---|
| 220 | */ |
|---|
| 221 | Vector3 randomInteriorPoint() const; |
|---|
| 222 | |
|---|
| 223 | void getBounds(class AABox&) const; |
|---|
| 224 | }; |
|---|
| 225 | |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | #endif |
|---|