| 1 | //************************************ bs::framework - Copyright 2018 Marko Pintera **************************************// |
| 2 | //*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********// |
| 3 | #pragma once |
| 4 | |
| 5 | #include "Prerequisites/BsPrerequisitesUtil.h" |
| 6 | #include "Math/BsVector3.h" |
| 7 | |
| 8 | namespace bs |
| 9 | { |
| 10 | /** @addtogroup Math |
| 11 | * @{ |
| 12 | */ |
| 13 | |
| 14 | /** A sphere represented by a center point and a radius. */ |
| 15 | class BS_UTILITY_EXPORT Sphere |
| 16 | { |
| 17 | public: |
| 18 | /** Default constructor. Creates a unit sphere around the origin. */ |
| 19 | Sphere() = default; |
| 20 | |
| 21 | Sphere(const Vector3& center, float radius) |
| 22 | :mRadius(radius), mCenter(center) |
| 23 | { } |
| 24 | |
| 25 | /** Returns the radius of the sphere. */ |
| 26 | float getRadius() const { return mRadius; } |
| 27 | |
| 28 | /** Sets the radius of the sphere. */ |
| 29 | void setRadius(float radius) { mRadius = radius; } |
| 30 | |
| 31 | /** Returns the center point of the sphere. */ |
| 32 | const Vector3& getCenter() const { return mCenter; } |
| 33 | |
| 34 | /** Sets the center point of the sphere. */ |
| 35 | void setCenter(const Vector3& center) { mCenter = center; } |
| 36 | |
| 37 | /** Merges the two spheres, creating a new sphere that encapsulates them both. */ |
| 38 | void merge(const Sphere& rhs); |
| 39 | |
| 40 | /** Expands the sphere so it includes the provided point. */ |
| 41 | void merge(const Vector3& point); |
| 42 | |
| 43 | /** Transforms the sphere by the given matrix. */ |
| 44 | void transform(const Matrix4& matrix); |
| 45 | |
| 46 | /** Returns whether or not this sphere contains the provided point. */ |
| 47 | inline bool contains(const Vector3& v) const; |
| 48 | |
| 49 | /** Returns whether or not this sphere intersects another sphere. */ |
| 50 | bool intersects(const Sphere& s) const; |
| 51 | |
| 52 | /** Returns whether or not this sphere intersects a box. */ |
| 53 | bool intersects(const AABox& box) const; |
| 54 | |
| 55 | /** Returns whether or not this sphere intersects a plane. */ |
| 56 | bool intersects(const Plane& plane) const; |
| 57 | |
| 58 | /** |
| 59 | * Ray/sphere intersection, returns boolean result and distance to nearest intersection. |
| 60 | * |
| 61 | * @param[in] ray Ray to intersect with the sphere. |
| 62 | * @param[in] discardInside (optional) If true the intersection will be discarded if ray origin |
| 63 | * is located within the sphere. |
| 64 | */ |
| 65 | std::pair<bool, float> intersects(const Ray& ray, bool discardInside = true) const; |
| 66 | |
| 67 | private: |
| 68 | float mRadius = 1.0f; |
| 69 | Vector3 mCenter{Vector3::ZERO}; |
| 70 | }; |
| 71 | |
| 72 | /** @} */ |
| 73 | |
| 74 | /** @cond SPECIALIZATIONS */ |
| 75 | BS_ALLOW_MEMCPY_SERIALIZATION(Sphere); |
| 76 | /** @endcond */ |
| 77 | } |
| 78 | |