Merge branch 'devel/master' into tizen
[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) 2023 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 class NavigationRay;
44
45 /**
46  * @class NavigationMesh
47  */
48 class NavigationMesh
49 {
50 public:
51   using Face   = Dali::Scene3D::Algorithm::NavigationMesh::Face;
52   using Edge   = Dali::Scene3D::Algorithm::NavigationMesh::Edge;
53   using Vertex = Dali::Scene3D::Algorithm::NavigationMesh::Vertex;
54
55 private:
56   friend class Scene3D::Loader::NavigationMeshFactory;
57
58   /**
59    * Constructor
60    */
61   NavigationMesh(const std::vector<uint8_t>& buffer);
62
63 public:
64   /**
65    * Destructor
66    */
67   ~NavigationMesh() = default;
68
69   /**
70    * Result of Ray/Polygon intersection
71    */
72   struct IntersectResult
73   {
74     Dali::Vector3 point;
75     float         distance;
76     uint16_t      faceIndex;
77     bool          result;
78   };
79
80   /**
81    * @copydoc Dali::Scene3D::Algorithm::NavigationMesh::GetFaceCount()
82    */
83   [[nodiscard]] uint32_t GetFaceCount() const;
84
85   /**
86    * @copydoc Dali::Scene3D::Algorithm::NavigationMesh::GetEdgeCount()
87    */
88   [[nodiscard]] uint32_t GetEdgeCount() const;
89
90   /**
91    * @copydoc Dali::Scene3D::Algorithm::NavigationMesh::GetVertexCount()
92    */
93   [[nodiscard]] uint32_t GetVertexCount() const;
94
95   /**
96    * Looks for floor only within the face
97    * @param[in] position Position to be projected onto the face
98    * @param[in] faceIndex Face index
99    * @param[in] dontCheckNeighbours states whether to traverse onto neighbouring faces
100    * @param[out] outPosition Output position
101    *
102    * @return true if success
103    */
104   bool FindFloorForFace(const Dali::Vector3& position, uint32_t faceIndex, bool dontCheckNeighbours, Dali::Vector3& outPosition);
105
106   /**
107    * @copydoc Dali::Scene3D::Algorithm::NavigationMesh::FindFloor()
108    */
109   bool FindFloor(const Dali::Vector3& position, Dali::Vector3& outPosition);
110
111   /**
112    * @copydoc Dali::Scene3D::Algorithm::NavigationMesh::FindFloor()
113    */
114   bool FindFloor(const Dali::Vector3& position, Dali::Vector3& outPosition, uint32_t& faceIndex);
115
116   /**
117    * @copydoc Dali::Scene3D::Algorithm::NavigationMesh::GetFace()
118    */
119   [[nodiscard]] const Face* GetFace(int index) const;
120
121   /**
122    * @copydoc Dali::Scene3D::Algorithm::NavigationMesh::GetEdge()
123    */
124   [[nodiscard]] const Edge* GetEdge(int index) const;
125
126   /**
127    * @copydoc Dali::Scene3D::Algorithm::NavigationMesh::GetVertex()
128    */
129   [[nodiscard]] const Vertex* GetVertex(int index) const;
130
131   /**
132    * @copydoc Dali::Scene3D::Algorithm::NavigationMesh::SetSceneTransform()
133    */
134   void SetTransform(const Dali::Matrix& transform);
135
136   /**
137    * Tests intersection between navigation ray and face
138    */
139   IntersectResult NavigationRayFaceIntersection(NavigationRay& ray, const Face& face);
140
141   /**
142    * @copydoc Dali::Scene3D::Algorithm::NavigationMesh::PointSceneToLocal()
143    */
144   Dali::Vector3 PointSceneToLocal(const Dali::Vector3& point);
145
146   /**
147    * @copydoc Dali::Scene3D::Algorithm::NavigationMesh::PointLocalToScene()
148    */
149   Dali::Vector3 PointLocalToScene(const Dali::Vector3& point);
150
151   /**
152    * @copydoc Dali::Scene3D::Algorithm::NavigationMesh::GetGravityVector()
153    */
154   [[nodiscard]] Dali::Vector3 GetGravityVector() const;
155
156 private:
157   std::vector<uint8_t>     mBuffer;           //< Data buffer
158   NavigationMeshHeader_V10 mHeader;           //< Navigation mesh header
159   uint16_t                 mCurrentFace;      //< Current face (last floor position)
160   Dali::Matrix             mTransform;        //< Transform matrix
161   Dali::Matrix             mTransformInverse; //< Inverse of the transform matrix
162 };
163
164 inline Internal::Algorithm::NavigationMesh& GetImplementation(Dali::Scene3D::Algorithm::NavigationMesh& navigationMesh)
165 {
166   return *navigationMesh.mImpl;
167 }
168
169 } // namespace Dali::Scene3D::Internal::Algorithm
170
171 #endif // DALI_SCENE3D_INTERNAL_NAVIGATION_MESH_H