1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef API_CORE_UTIL_FRUSTUM_UTIL_H
17 #define API_CORE_UTIL_FRUSTUM_UTIL_H
18 
19 #include <base/containers/refcnt_ptr.h>
20 #include <base/math/vector.h>
21 #include <base/namespace.h>
22 #include <base/util/uid.h>
23 #include <core/namespace.h>
24 #include <core/plugin/intf_interface.h>
25 
BASE_BEGIN_NAMESPACE()26 BASE_BEGIN_NAMESPACE()
27 namespace Math {
28 class Mat4X4;
29 } // namespace Math
30 BASE_END_NAMESPACE()
31 
32 CORE_BEGIN_NAMESPACE()
33 /** \addtogroup group_util_frustumutil
34  *  @{
35  */
36 /** Frustum */
37 struct Frustum {
38     /** Frustum plane ID's */
39     enum FrustumPlaneId {
40         /** Left */
41         PLANE_LEFT = 0,
42         /** Right */
43         PLANE_RIGHT = 1,
44         /** Bottom */
45         PLANE_BOTTOM = 2,
46         /** Top */
47         PLANE_TOP = 3,
48         /** Near */
49         PLANE_NEAR = 4,
50         /** Far */
51         PLANE_FAR = 5,
52         /** Count */
53         PLANE_COUNT = 6,
54     };
55 
56     /** Planes vector4 array */
57     BASE_NS::Math::Vec4 planes[PLANE_COUNT];
58 };
59 
60 class IFrustumUtil : public IInterface {
61 public:
62     static constexpr auto UID = BASE_NS::Uid { "3defee25-af81-4c20-b8d0-e1c3419556b2" };
63 
64     using Ptr = BASE_NS::refcnt_ptr<IFrustumUtil>;
65 
66     /** Create frustum planes from input matrix.
67      * @param matrix Matrix to create planes from. (Usually view-proj matrix)
68      * @return Frustum with normalized planes, normals pointing towards the center of the frustum.
69      */
70     virtual Frustum CreateFrustum(const BASE_NS::Math::Mat4X4& matrix) const = 0;
71 
72     /** Test sphere against frustum planes.
73      * @param frustum Frustum to test against.
74      * @param pos Center position of a bounding sphere.
75      * @param radius Radius of a bounding sphere.
76      * @return Boolean TRUE if sphere is inside partially or fully. FALSE otherwise.
77      */
78     virtual bool SphereFrustumCollision(
79         const Frustum& frustum, const BASE_NS::Math::Vec3 pos, const float radius) const = 0;
80 
81 protected:
82     IFrustumUtil() = default;
83     virtual ~IFrustumUtil() = default;
84 };
85 
86 /** @} */
87 CORE_END_NAMESPACE()
88 
89 #endif // API_CORE_UTIL_FRUSTUM_UTIL_H
90