| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- //
- // Copyright (c) 2008-2017 the Urho3D project.
- //
- // 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.
- //
- #include "../Precompiled.h"
- #include "../Math/Frustum.h"
- #include "../Math/Polyhedron.h"
- #include "../DebugNew.h"
- namespace Atomic
- {
- void Sphere::Define(const Vector3* vertices, unsigned count)
- {
- if (!count)
- return;
- Clear();
- Merge(vertices, count);
- }
- void Sphere::Define(const BoundingBox& box)
- {
- const Vector3& min = box.min_;
- const Vector3& max = box.max_;
- Clear();
- Merge(min);
- Merge(Vector3(max.x_, min.y_, min.z_));
- Merge(Vector3(min.x_, max.y_, min.z_));
- Merge(Vector3(max.x_, max.y_, min.z_));
- Merge(Vector3(min.x_, min.y_, max.z_));
- Merge(Vector3(max.x_, min.y_, max.z_));
- Merge(Vector3(min.x_, max.y_, max.z_));
- Merge(max);
- }
- void Sphere::Define(const Frustum& frustum)
- {
- Define(frustum.vertices_, NUM_FRUSTUM_VERTICES);
- }
- void Sphere::Define(const Polyhedron& poly)
- {
- Clear();
- Merge(poly);
- }
- void Sphere::Merge(const Vector3* vertices, unsigned count)
- {
- while (count--)
- Merge(*vertices++);
- }
- void Sphere::Merge(const BoundingBox& box)
- {
- const Vector3& min = box.min_;
- const Vector3& max = box.max_;
- Merge(min);
- Merge(Vector3(max.x_, min.y_, min.z_));
- Merge(Vector3(min.x_, max.y_, min.z_));
- Merge(Vector3(max.x_, max.y_, min.z_));
- Merge(Vector3(min.x_, min.y_, max.z_));
- Merge(Vector3(max.x_, min.y_, max.z_));
- Merge(Vector3(min.x_, max.y_, max.z_));
- Merge(max);
- }
- void Sphere::Merge(const Frustum& frustum)
- {
- const Vector3* vertices = frustum.vertices_;
- Merge(vertices, NUM_FRUSTUM_VERTICES);
- }
- void Sphere::Merge(const Polyhedron& poly)
- {
- for (unsigned i = 0; i < poly.faces_.Size(); ++i)
- {
- const PODVector<Vector3>& face = poly.faces_[i];
- if (!face.Empty())
- Merge(&face[0], face.Size());
- }
- }
- void Sphere::Merge(const Sphere& sphere)
- {
- if (radius_ < 0.0f)
- {
- center_ = sphere.center_;
- radius_ = sphere.radius_;
- return;
- }
- Vector3 offset = sphere.center_ - center_;
- float dist = offset.Length();
- // If sphere fits inside, do nothing
- if (dist + sphere.radius_ < radius_)
- return;
- // If we fit inside the other sphere, become it
- if (dist + radius_ < sphere.radius_)
- {
- center_ = sphere.center_;
- radius_ = sphere.radius_;
- }
- else
- {
- Vector3 NormalizedOffset = offset / dist;
- Vector3 min = center_ - radius_ * NormalizedOffset;
- Vector3 max = sphere.center_ + sphere.radius_ * NormalizedOffset;
- center_ = (min + max) * 0.5f;
- radius_ = (max - center_).Length();
- }
- }
- Intersection Sphere::IsInside(const BoundingBox& box) const
- {
- float radiusSquared = radius_ * radius_;
- float distSquared = 0;
- float temp;
- Vector3 min = box.min_;
- Vector3 max = box.max_;
- if (center_.x_ < min.x_)
- {
- temp = center_.x_ - min.x_;
- distSquared += temp * temp;
- }
- else if (center_.x_ > max.x_)
- {
- temp = center_.x_ - max.x_;
- distSquared += temp * temp;
- }
- if (center_.y_ < min.y_)
- {
- temp = center_.y_ - min.y_;
- distSquared += temp * temp;
- }
- else if (center_.y_ > max.y_)
- {
- temp = center_.y_ - max.y_;
- distSquared += temp * temp;
- }
- if (center_.z_ < min.z_)
- {
- temp = center_.z_ - min.z_;
- distSquared += temp * temp;
- }
- else if (center_.z_ > max.z_)
- {
- temp = center_.z_ - max.z_;
- distSquared += temp * temp;
- }
- if (distSquared >= radiusSquared)
- return OUTSIDE;
- min -= center_;
- max -= center_;
- Vector3 tempVec = min; // - - -
- if (tempVec.LengthSquared() >= radiusSquared)
- return INTERSECTS;
- tempVec.x_ = max.x_; // + - -
- if (tempVec.LengthSquared() >= radiusSquared)
- return INTERSECTS;
- tempVec.y_ = max.y_; // + + -
- if (tempVec.LengthSquared() >= radiusSquared)
- return INTERSECTS;
- tempVec.x_ = min.x_; // - + -
- if (tempVec.LengthSquared() >= radiusSquared)
- return INTERSECTS;
- tempVec.z_ = max.z_; // - + +
- if (tempVec.LengthSquared() >= radiusSquared)
- return INTERSECTS;
- tempVec.y_ = min.y_; // - - +
- if (tempVec.LengthSquared() >= radiusSquared)
- return INTERSECTS;
- tempVec.x_ = max.x_; // + - +
- if (tempVec.LengthSquared() >= radiusSquared)
- return INTERSECTS;
- tempVec.y_ = max.y_; // + + +
- if (tempVec.LengthSquared() >= radiusSquared)
- return INTERSECTS;
- return INSIDE;
- }
- Intersection Sphere::IsInsideFast(const BoundingBox& box) const
- {
- float radiusSquared = radius_ * radius_;
- float distSquared = 0;
- float temp;
- Vector3 min = box.min_;
- Vector3 max = box.max_;
- if (center_.x_ < min.x_)
- {
- temp = center_.x_ - min.x_;
- distSquared += temp * temp;
- }
- else if (center_.x_ > max.x_)
- {
- temp = center_.x_ - max.x_;
- distSquared += temp * temp;
- }
- if (center_.y_ < min.y_)
- {
- temp = center_.y_ - min.y_;
- distSquared += temp * temp;
- }
- else if (center_.y_ > max.y_)
- {
- temp = center_.y_ - max.y_;
- distSquared += temp * temp;
- }
- if (center_.z_ < min.z_)
- {
- temp = center_.z_ - min.z_;
- distSquared += temp * temp;
- }
- else if (center_.z_ > max.z_)
- {
- temp = center_.z_ - max.z_;
- distSquared += temp * temp;
- }
- if (distSquared >= radiusSquared)
- return OUTSIDE;
- else
- return INSIDE;
- }
- Vector3 Sphere::GetLocalPoint(float theta, float phi) const
- {
- return Vector3(
- radius_ * Sin(theta) * Sin(phi),
- radius_ * Cos(phi),
- radius_ * Cos(theta) * Sin(phi)
- );
- }
- }
|