[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / algorithm / navigation-mesh-impl.h
1 #ifndef DALI_SCENE3D_INTERNAL_NAVIGATION_MESH_H
2 #define DALI_SCENE3D_INTERNAL_NAVIGATION_MESH_H
3
4 /*
5  * Copyright (c) 2024 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 // EXTERNAL EXTERNAL
21 #include <dali/public-api/actors/actor.h>
22 #include <dali/public-api/common/vector-wrapper.h>
23 #include <dali/public-api/math/matrix.h>
24 #include <dali/public-api/math/vector3.h>
25 #include <dali/public-api/math/vector4.h>
26
27 #include <cinttypes>
28 #include <cstdio>
29 #include <mutex>
30
31 // INTERNAL INCLUDES
32 #include <dali-scene3d/internal/algorithm/navigation-mesh-header.h>
33 #include <dali-scene3d/public-api/algorithm/navigation-mesh.h>
34 #include <dali-scene3d/public-api/algorithm/path-finder.h>
35
36 namespace Dali::Scene3D::Loader
37 {
38 class NavigationMeshFactory;
39 }
40
41 namespace Dali::Scene3D::Internal::Algorithm
42 {
43 // Internal Navigation ray structure
44 struct NavigationRay
45 {
46   Dali::Vector3 origin;    // Origin of ray
47   Dali::Vector3 direction; // Direction of ray
48 };
49
50 // Make each to change each index value's type here.
51 using VertexIndex = Dali::Scene3D::Algorithm::VertexIndex;
52 using EdgeIndex   = Dali::Scene3D::Algorithm::EdgeIndex;
53 using FaceIndex   = Dali::Scene3D::Algorithm::FaceIndex;
54
55 /**
56  * @class NavigationMesh
57  */
58 class NavigationMesh
59 {
60 public:
61   using Face   = Dali::Scene3D::Algorithm::NavigationMesh::Face;
62   using Edge   = Dali::Scene3D::Algorithm::NavigationMesh::Edge;
63   using Vertex = Dali::Scene3D::Algorithm::NavigationMesh::Vertex;
64
65 private:
66   friend class Scene3D::Loader::NavigationMeshFactory;
67
68   /**
69    * Constructor
70    */
71   NavigationMesh(const std::vector<uint8_t>& buffer);
72
73 public:
74   /**
75    * Destructor
76    */
77   ~NavigationMesh() = default;
78
79   /**
80    * Result of Ray/Polygon intersection
81    */
82   struct IntersectResult
83   {
84     Dali::Vector3 point;
85     float         distance;
86     FaceIndex     faceIndex;
87     bool          result;
88   };
89
90   /**
91    * @copydoc Dali::Scene3D::Algorithm::NavigationMesh::GetFaceCount()
92    */
93   [[nodiscard]] uint32_t GetFaceCount() const;
94
95   /**
96    * @copydoc Dali::Scene3D::Algorithm::NavigationMesh::GetEdgeCount()
97    */
98   [[nodiscard]] uint32_t GetEdgeCount() const;
99
100   /**
101    * @copydoc Dali::Scene3D::Algorithm::NavigationMesh::GetVertexCount()
102    */
103   [[nodiscard]] uint32_t GetVertexCount() const;
104
105   /**
106    * @copydoc Dali::Scene3D::Algorithm::NavigationMesh::FindFloorForFace()
107    */
108   bool FindFloorForFace(const Dali::Vector3& position, FaceIndex faceIndex, bool dontCheckNeighbours, Dali::Vector3& outPosition);
109
110   /**
111    * @copydoc Dali::Scene3D::Algorithm::NavigationMesh::FindFloor()
112    */
113   bool FindFloor(const Dali::Vector3& position, Dali::Vector3& outPosition);
114
115   /**
116    * @copydoc Dali::Scene3D::Algorithm::NavigationMesh::FindFloor()
117    */
118   bool FindFloor(const Dali::Vector3& position, Dali::Vector3& outPosition, FaceIndex& outFaceIndex);
119
120   /**
121    * @copydoc Dali::Scene3D::Algorithm::NavigationMesh::GetFace()
122    */
123   [[nodiscard]] const Face* GetFace(FaceIndex index) const;
124
125   /**
126    * @copydoc Dali::Scene3D::Algorithm::NavigationMesh::GetEdge()
127    */
128   [[nodiscard]] const Edge* GetEdge(EdgeIndex index) const;
129
130   /**
131    * @copydoc Dali::Scene3D::Algorithm::NavigationMesh::GetVertex()
132    */
133   [[nodiscard]] const Vertex* GetVertex(VertexIndex index) const;
134
135   /**
136    * @copydoc Dali::Scene3D::Algorithm::NavigationMesh::SetSceneTransform()
137    */
138   void SetTransform(const Dali::Matrix& transform);
139
140   /**
141    * Tests intersection between navigation ray and face
142    */
143   IntersectResult NavigationRayFaceIntersection(NavigationRay& ray, const Face& face) const;
144
145   /**
146    * @brief Test ray against the mesh and returns intersection result
147    * @param[in] rayOrig Input ray to test collision
148    *
149    * @return Valid IntersectResult structure
150    */
151   IntersectResult RayCastIntersect(NavigationRay& rayOrig) const;
152
153   /**
154    * @copydoc Dali::Scene3D::Algorithm::NavigationMesh::PointSceneToLocal()
155    */
156   Dali::Vector3 PointSceneToLocal(const Dali::Vector3& point) const;
157
158   /**
159    * @copydoc Dali::Scene3D::Algorithm::NavigationMesh::PointLocalToScene()
160    */
161   Dali::Vector3 PointLocalToScene(const Dali::Vector3& point) const;
162
163   /**
164    * @copydoc Dali::Scene3D::Algorithm::NavigationMesh::GetGravityVector()
165    */
166   [[nodiscard]] Dali::Vector3 GetGravityVector() const;
167
168   /**
169    * @brief Returns binary data of the mesh
170    * @return Reference to the binary buffer
171    */
172   [[nodiscard]] const std::vector<uint8_t>& GetData() const
173   {
174     return mBuffer;
175   }
176
177 private:
178   std::vector<uint8_t>     mBuffer;           //< Data buffer
179   NavigationMeshHeader_V10 mHeader;           //< Navigation mesh header
180   FaceIndex                mCurrentFace;      //< Current face (last floor position)
181   Dali::Matrix             mTransform;        //< Transform matrix
182   Dali::Matrix             mTransformInverse; //< Inverse of the transform matrix
183 };
184
185 inline Internal::Algorithm::NavigationMesh& GetImplementation(Dali::Scene3D::Algorithm::NavigationMesh& navigationMesh)
186 {
187   return *navigationMesh.mImpl;
188 }
189
190 inline const Internal::Algorithm::NavigationMesh& GetImplementation(const Dali::Scene3D::Algorithm::NavigationMesh& navigationMesh)
191 {
192   return *navigationMesh.mImpl;
193 }
194
195 } // namespace Dali::Scene3D::Internal::Algorithm
196
197 #endif // DALI_SCENE3D_INTERNAL_NAVIGATION_MESH_H