| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- //
- // Urho3D Engine
- // Copyright (c) 2008-2011 Lasse Öörni
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #pragma once
- #include "Rect.h"
- #include "Vector3.h"
- class Frustum;
- class Matrix3;
- class Matrix4;
- class Matrix3x4;
- class Ray;
- class Sphere;
- /// Three-dimensional axis-aligned bounding box
- class BoundingBox
- {
- public:
- /// Construct with zero size
- BoundingBox() :
- min_(Vector3::ZERO),
- max_(Vector3::ZERO),
- defined_(false)
- {
- }
-
- /// Copy-construct from another bounding box
- BoundingBox(const BoundingBox& box) :
- min_(box.min_),
- max_(box.max_),
- defined_(box.defined_)
- {
- }
-
- /// Construct from a rect, with the Z dimension left zero
- BoundingBox(const Rect& rect) :
- min_(Vector3(rect.min_, 0.0f)),
- max_(Vector3(rect.max_, 0.0f)),
- defined_(true)
- {
- }
-
- /// Construct from minimum and maximum vectors
- BoundingBox(const Vector3& min, const Vector3& max) :
- min_(min),
- max_(max),
- defined_(true)
- {
- }
-
- /// Construct from minimum and maximum floats (all dimensions same)
- BoundingBox(float min, float max) :
- min_(Vector3(min, min, min)),
- max_(Vector3(max, max, max)),
- defined_(true)
- {
- }
-
- /// Assign from another bounding box
- BoundingBox& operator = (const BoundingBox& rhs)
- {
- min_ = rhs.min_;
- max_ = rhs.max_;
- defined_ = rhs.defined_;
-
- return *this;
- }
-
- /// Assign from a Rect, with the Z dimension left zero
- BoundingBox& operator = (const Rect& rhs)
- {
- min_ = Vector3(rhs.min_, 0.0f);
- max_ = Vector3(rhs.max_, 0.0f);
- defined_ = true;
-
- return *this;
- }
-
- /// Test for equality with another bounding box
- bool operator == (const BoundingBox& rhs) const
- {
- return ((min_ == rhs.min_) && (max_ == rhs.max_));
- }
-
- /// Test for inequality with another bounding box
- bool operator != (const BoundingBox& rhs) const
- {
- return ((min_ != rhs.min_) || (max_ != rhs.max_));
- }
-
- /// Define from minimum and maximum vectors
- void Define(const Vector3& min, const Vector3& max)
- {
- min_ = min;
- max_ = max;
- defined_ = true;
- }
-
- /// Define from a point
- void Define(const Vector3& point)
- {
- min_ = max_ = point;
- defined_ = true;
- }
-
- /// Define from minimum and maximum floats (all dimensions same)
- void Define(float min, float max)
- {
- min_ = Vector3(min, min, min);
- max_ = Vector3(max, max, max);
- defined_ = true;
- }
-
- /// Merge a point
- void Merge(const Vector3& point)
- {
- if (!defined_)
- {
- min_ = max_ = point;
- defined_ = true;
- return;
- }
-
- if (point.x_ < min_.x_) min_.x_ = point.x_;
- if (point.y_ < min_.y_) min_.y_ = point.y_;
- if (point.z_ < min_.z_) min_.z_ = point.z_;
- if (point.x_ > max_.x_) max_.x_ = point.x_;
- if (point.y_ > max_.y_) max_.y_ = point.y_;
- if (point.z_ > max_.z_) max_.z_ = point.z_;
- }
-
- /// Merge another bounding box
- void Merge(const BoundingBox& box)
- {
- if (!defined_)
- {
- min_ = box.min_;
- max_ = box.max_;
- defined_ = true;
- return;
- }
-
- if (box.min_.x_ < min_.x_) min_.x_ = box.min_.x_;
- if (box.min_.y_ < min_.y_) min_.y_ = box.min_.y_;
- if (box.min_.z_ < min_.z_) min_.z_ = box.min_.z_;
- if (box.max_.x_ > max_.x_) max_.x_ = box.max_.x_;
- if (box.max_.y_ > max_.y_) max_.y_ = box.max_.y_;
- if (box.max_.z_ > max_.z_) max_.z_ = box.max_.z_;
- }
-
- /// Define from an array of vertices
- void Define(const Vector3* vertices, unsigned count);
- /// Define from a frustum
- void Define(const Frustum& frustum);
- /// Define from a sphere
- void Define(const Sphere& sphere);
- /// Merge an array of vertices
- void Merge(const Vector3* vertices, unsigned count);
- /// Merge a frustum
- void Merge(const Frustum& frustum);
- /// Merge a sphere
- void Merge(const Sphere& sphere);
- /// Intersect with another bounding box
- void Intersect(const BoundingBox& box);
- /// Transform with a 3x3 matrix
- void Transform(const Matrix3& transform);
- /// Transform with a 4x3 matrix
- void Transform(const Matrix3x4& transform);
-
- /// Return center
- Vector3 Center() const { return (max_ + min_) * 0.5f; }
- /// Return size
- Vector3 Size() const { return max_ - min_; }
- /// Return half-size
- Vector3 HalfSize() const { return (max_ - min_) * 0.5f; }
-
- /// Return transformed by a 3x3 matrix
- BoundingBox Transformed(const Matrix3& transform) const;
- /// Return transformed by a 4x3 matrix
- BoundingBox Transformed(const Matrix3x4& transform) const;
- /// Return projected by a 4x4 projection matrix
- Rect Projected(const Matrix4& projection) const;
-
- /// Test if a point is inside
- Intersection IsInside(const Vector3& point) const
- {
- if ((point.x_ < min_.x_) || (point.x_ > max_.x_))
- return OUTSIDE;
- if ((point.y_ < min_.y_) || (point.y_ > max_.y_))
- return OUTSIDE;
- if ((point.z_ < min_.z_) || (point.z_ > max_.z_))
- return OUTSIDE;
-
- return INSIDE;
- }
-
- /// Test if another bounding box is inside, outside or intersects
- Intersection IsInside(const BoundingBox& box) const
- {
- if ((box.max_.x_ < min_.x_) || (box.min_.x_ > max_.x_))
- return OUTSIDE;
- if ((box.max_.y_ < min_.y_) || (box.min_.y_ > max_.y_))
- return OUTSIDE;
- if ((box.max_.z_ < min_.z_) || (box.min_.z_ > max_.z_))
- return OUTSIDE;
-
- if ((box.min_.x_ < min_.x_) || (box.max_.x_ > max_.x_))
- return INTERSECTS;
- if ((box.min_.y_ < min_.y_) || (box.max_.y_ > max_.y_))
- return INTERSECTS;
- if ((box.min_.z_ < min_.z_) || (box.max_.z_ > max_.z_))
- return INTERSECTS;
-
- return INSIDE;
- }
-
- /// Test if another bounding box is (partially) inside or outside
- Intersection IsInsideFast(const BoundingBox& box) const
- {
- if ((box.max_.x_ < min_.x_) || (box.min_.x_ > max_.x_))
- return OUTSIDE;
- if ((box.max_.y_ < min_.y_) || (box.min_.y_ > max_.y_))
- return OUTSIDE;
- if ((box.max_.z_ < min_.z_) || (box.min_.z_ > max_.z_))
- return OUTSIDE;
-
- return INSIDE;
- }
-
- /// Test if a sphere is inside, outside or intersects
- Intersection IsInside(const Sphere& sphere) const;
- /// Test if a sphere is (partially) inside or outside
- Intersection IsInsideFast(const Sphere& sphere) const;
- /// Return ray hit distance, or infinity if no hit
- float Distance(const Ray& ray) const;
-
- /// Minimum vector
- Vector3 min_;
- /// Maximum vector
- Vector3 max_;
- /// Defined flag
- bool defined_;
- };
|