Fix many svace issues at dali-toolkit (integer overflow + etc)
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / public-api / algorithm / navigation-mesh.h
index 12f3387..34088dd 100644 (file)
@@ -2,7 +2,7 @@
 #define DALI_SCENE3D_NAVIGATION_MESH_H
 
 /*
- * Copyright (c) 2023 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2024 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * limitations under the License.
  */
 
-// INTERNAL INCLUDES
-#include <dali-scene3d/public-api/api.h>
-
 // EXTERNAL INCLUDES
+#include <dali/public-api/common/vector-wrapper.h>
 #include <dali/public-api/math/matrix.h>
 #include <dali/public-api/math/vector3.h>
 #include <dali/public-api/math/vector4.h>
 
 #include <cinttypes>
 #include <cstdio>
-#include <vector>
+#include <limits>
 #include <memory>
 
+// INTERNAL INCLUDES
+#include <dali-scene3d/public-api/api.h>
+
 namespace Dali::Scene3D::Internal::Algorithm
 {
 class NavigationMesh;
@@ -41,15 +42,20 @@ class NavigationMeshFactory;
 }
 
 constexpr auto NAVIGATION_MESH_MAX_VERTICES_PER_FACE = 3u;
-constexpr auto NAVIGATION_MESH_MAX_EDGES_PER_FACE = 3u;
-constexpr auto NAVIGATION_MESH_MAX_COMPONENTS_3D = 3u;
-constexpr auto NAVIGATION_MESH_MAX_COMPONENTS_2D = 2u;
+constexpr auto NAVIGATION_MESH_MAX_EDGES_PER_FACE    = 3u;
+constexpr auto NAVIGATION_MESH_MAX_COMPONENTS_3D     = 3u;
+constexpr auto NAVIGATION_MESH_MAX_COMPONENTS_2D     = 2u;
 
 namespace Dali::Scene3D::Algorithm
 {
 // Using PImpling but not usual DALi handles as this object isn't supposed to be refcounted
 using NavigationMeshImpl = Dali::Scene3D::Internal::Algorithm::NavigationMesh;
 
+// Make each to change each index value's type here.
+using VertexIndex = uint16_t;
+using EdgeIndex   = uint16_t;
+using FaceIndex   = uint16_t;
+
 /**
  * @class NavigationMesh
  *
@@ -72,18 +78,17 @@ using NavigationMeshImpl = Dali::Scene3D::Internal::Algorithm::NavigationMesh;
 class DALI_SCENE3D_API NavigationMesh
 {
 public:
-
   /**
    * @struct Face
    *
-   * Describes a single polygon
+   * Describes a single polygon's face
    */
   struct Face
   {
-    uint16_t vertex[NAVIGATION_MESH_MAX_VERTICES_PER_FACE]; ///< Vertices per face
-    uint16_t edge[NAVIGATION_MESH_MAX_EDGES_PER_FACE]; ///< Edges per face
-    float    normal[NAVIGATION_MESH_MAX_COMPONENTS_3D]; ///< Normal vector
-    float    center[NAVIGATION_MESH_MAX_COMPONENTS_3D]; ///< Barycentric coordinates
+    VertexIndex vertex[NAVIGATION_MESH_MAX_VERTICES_PER_FACE]; ///< Vertices per face
+    EdgeIndex   edge[NAVIGATION_MESH_MAX_EDGES_PER_FACE];      ///< Edges per face
+    float       normal[NAVIGATION_MESH_MAX_COMPONENTS_3D];     ///< Normal vector
+    float       center[NAVIGATION_MESH_MAX_COMPONENTS_3D];     ///< Barycentric coordinates
   };
 
   /**
@@ -93,8 +98,8 @@ public:
    */
   struct Edge
   {
-    uint16_t vertex[NAVIGATION_MESH_MAX_COMPONENTS_2D]; ///< Vertices making the edge
-    uint16_t face[NAVIGATION_MESH_MAX_COMPONENTS_2D];   ///< Faces on both sides of edge
+    VertexIndex vertex[NAVIGATION_MESH_MAX_COMPONENTS_2D]; ///< Vertices making the edge
+    FaceIndex   face[NAVIGATION_MESH_MAX_COMPONENTS_2D];   ///< Faces on both sides of edge
   };
 
   /**
@@ -107,7 +112,7 @@ public:
   {
     union
     {
-      float co[NAVIGATION_MESH_MAX_COMPONENTS_3D]; ///< Coordinates of vertex
+      float coordinates[NAVIGATION_MESH_MAX_COMPONENTS_3D]; ///< Coordinates of vertex
       struct
       {
         float x, y, z;
@@ -118,7 +123,6 @@ public:
   NavigationMesh() = delete;
 
 public:
-
   /**
    * @brief Destructor
    */
@@ -148,12 +152,12 @@ public:
   /**
    * @brief Looks for the floor under specified position
    * @param[in] position Position to investigate
-   * @param[in] outPosition Position on the floor in found
-   * @param[in] faceIndex Index of NavigationMesh face associated with floor
+   * @param[out] outPosition Position on the floor in found
+   * @param[out] outFaceIndex Index of NavigationMesh face associated with floor
    *
    * @return True if floor has been found, False otherwise
    */
-  bool FindFloor(const Dali::Vector3& position, Dali::Vector3& outPosition, uint32_t& faceIndex);
+  bool FindFloor(const Dali::Vector3& position, Dali::Vector3& outPosition, FaceIndex& outFaceIndex);
 
   /**
    * @brief Looks for a floor starting from specified face
@@ -169,29 +173,28 @@ public:
    *
    * @return True on success, false otherwise
    */
-  bool FindFloorForFace(const Dali::Vector3& position, uint32_t faceIndex, bool dontCheckNeighbours, Dali::Vector3& outPosition);
-
+  bool FindFloorForFace(const Dali::Vector3& position, FaceIndex faceIndex, bool dontCheckNeighbours, Dali::Vector3& outPosition);
 
   /**
    * @brief Returns pointer to Face structure
    * @param[in] index Index of face to retrieve
    * @return Pointer to valid Face structure or nullptr
    */
-  [[nodiscard]] const Face* GetFace(int index) const;
+  [[nodiscard]] const Face* GetFace(FaceIndex index) const;
 
   /**
    * @brief Returns edge structure
    * @param[in] index Index of edge to retrieve
    * @return Pointer to valid Edge structure or nullptr
    */
-  [[nodiscard]] const Edge* GetEdge(int index) const;
+  [[nodiscard]] const Edge* GetEdge(EdgeIndex index) const;
 
   /**
    * @brief Returns vertex structure
    * @param[in] index Index of vertex to retrieve
    * @return Pointer to valid Vertex structure or nullptr
    */
-  [[nodiscard]] const Vertex* GetVertex(int index) const;
+  [[nodiscard]] const Vertex* GetVertex(VertexIndex index) const;
 
   /**
    * @brief Sets static transform for the navigation mesh object
@@ -228,7 +231,7 @@ public:
    * @param[in] point Point to transform
    * @return Point transformed to the local space
    */
-  Dali::Vector3 PointSceneToLocal(const Dali::Vector3& point);
+  Dali::Vector3 PointSceneToLocal(const Dali::Vector3& point) const;
 
   /**
    * @brief Transforms point into the parent transform space
@@ -238,7 +241,7 @@ public:
    * @param[in] point Point to transform
    * @return Point transformed into the parent space
    */
-  Dali::Vector3 PointLocalToScene(const Dali::Vector3& point);
+  Dali::Vector3 PointLocalToScene(const Dali::Vector3& point) const;
 
   /**
    * @brief Returns direction of the gravity vector
@@ -249,14 +252,31 @@ public:
    */
   Dali::Vector3 GetGravityVector() const;
 
-  static constexpr uint16_t NULL_FACE{0xffff}; ///< Represents null polygon
-  static constexpr uint16_t NULL_EDGE{0xffff}; ///< represents null edge
+  /**
+   * @brief Performs ray/face intersect test
+   * @param[in] origin Origin of ray
+   * @param[in] direction Direction of ray
+   *
+   * @SINCE_2_2.53
+   * @return Valid FaceIndex on hit or NULL_FACE on miss
+   */
+  [[nodiscard]] FaceIndex RayFaceIntersect(const Vector3& origin, const Vector3& direction) const;
+
+  static constexpr FaceIndex NULL_FACE{std::numeric_limits<FaceIndex>::max()}; ///< Represents null face
+  static constexpr EdgeIndex NULL_EDGE{std::numeric_limits<EdgeIndex>::max()}; ///< Represents null edge
 
 public:
-
-  DALI_INTERNAL explicit NavigationMesh( NavigationMeshImpl* impl );
+  DALI_INTERNAL explicit NavigationMesh(NavigationMeshImpl* impl);
 
   std::unique_ptr<NavigationMeshImpl> mImpl;
 };
+
+// Alias name for collider mesh
+// TODO: currently ColliderMesh is NavigationMesh however
+//       there should be separation from data and algorithms.
+//       Both, NavigationMesh and ColliderMesh use the same
+//       data structures but differ in the way they use data.
+using ColliderMesh = NavigationMesh;
+
 } // namespace Dali::Scene3D::Algorithm
 #endif // DALI_SCENE3D_NAVIGATION_MESH_H