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
8namespace bs
9{
10 /** @addtogroup Math
11 * @{
12 */
13
14 /**
15 * Represents a rectangle in three dimensional space. It is represented by two axes that extend from the specified
16 * origin. Axes should be perpendicular to each other and they extend in both positive and negative directions from the
17 * origin by the amount specified by extents.
18 */
19 class BS_UTILITY_EXPORT Rect3
20 {
21 public:
22 Rect3() = default;
23
24 Rect3(const Vector3& center, const std::array<Vector3, 2>& axes,
25 const std::array<float, 2>& extents)
26 :mCenter(center), mAxisHorz(axes[0]), mAxisVert(axes[1]),
27 mExtentHorz(extents[0]), mExtentVert(extents[1])
28 { }
29
30 /**
31 * Find the nearest points of the provided ray and the rectangle.
32 *
33 * @return A set of nearest points and nearest distance. First value in the set corresponds to nearest point on
34 * the ray, and the second to the nearest point on the rectangle. They are same in the case of intersection.
35 * When ray is parallel to the rectangle there are two sets of nearest points but only one the set nearest
36 * to the ray origin is returned.
37 */
38 std::pair<std::array<Vector3, 2>, float> getNearestPoint(const Ray& ray) const;
39
40 /**
41 * Find the nearest point on the rectangle to the provided point.
42 *
43 * @return Nearest point and distance to nearest point.
44 */
45 std::pair<Vector3, float> getNearestPoint(const Vector3& point) const;
46
47 /**
48 * Ray/rectangle intersection.
49 *
50 * @return Boolean result and distance to intersection point.
51 */
52 std::pair<bool, float> intersects(const Ray& ray) const;
53
54 /** Gets the origin of the rectangle. */
55 const Vector3& getCenter() const { return mCenter; }
56
57 /** Returns the rectangle's horizontal axis. */
58 const Vector3& getAxisHorz() const { return mAxisHorz; }
59
60 /** Returns the rectangle's vertical axis. */
61 const Vector3& getAxisVert() const { return mAxisVert; }
62
63 /** Gets the extent of the rectangle along its horizontal axis. */
64 const float& getExtentHorz() const { return mExtentHorz; }
65
66 /** Gets the extent of the rectangle along its vertical axis. */
67 const float& getExtentVertical() const { return mExtentVert; }
68
69 private:
70 Vector3 mCenter{BsZero};
71 Vector3 mAxisHorz{BsZero};
72 Vector3 mAxisVert{BsZero};
73 float mExtentHorz = 0.0f;
74 float mExtentVert = 0.0f;
75 };
76
77 /** @} */
78}
79