WhiteBoxManipulatorBounds.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <AzToolsFramework/Picking/BoundInterface.h>
  10. #include <AzToolsFramework/Picking/ContextBoundAPI.h>
  11. #include <WhiteBox/WhiteBoxToolApi.h>
  12. namespace WhiteBox
  13. {
  14. //! Represents all triangles composing a polygon that can have intersection
  15. //! queries performed against it.
  16. //! @note Each triangle must be defined in CCW order.
  17. struct PolygonBound
  18. {
  19. AZStd::vector<AZ::Vector3> m_triangles;
  20. };
  21. //! Provides a mapping between a polygon handle and the bound it represents.
  22. //! @note This is a cache to save computing the polygon bound each time from the polygon handle.
  23. struct PolygonBoundWithHandle
  24. {
  25. PolygonBound m_bound;
  26. Api::PolygonHandle m_handle;
  27. };
  28. //! Represents the beginning and end of an edge that can
  29. //! have intersection queries performed against it.
  30. struct EdgeBound
  31. {
  32. AZ::Vector3 m_start;
  33. AZ::Vector3 m_end;
  34. float m_radius;
  35. };
  36. //! Provides a mapping between an edge handle and the bound it represents.
  37. //! @note This is a cache to save computing the edge bound each time from the edge handle.
  38. struct EdgeBoundWithHandle
  39. {
  40. EdgeBound m_bound;
  41. Api::EdgeHandle m_handle;
  42. };
  43. //! Represents a vertex that can have intersection
  44. //! queries performed against it.
  45. struct VertexBound
  46. {
  47. AZ::Vector3 m_center;
  48. float m_radius;
  49. };
  50. //! Provides a mapping between a vertex handle and the bound it represents.
  51. //! @note This is a cache to save computing the vertex bound each time from the vertex handle.
  52. struct VertexBoundWithHandle
  53. {
  54. VertexBound m_bound;
  55. Api::VertexHandle m_handle;
  56. };
  57. //! Perform a ray intersection against a polygon, returning true or false if the intersection was successful.
  58. //! @param rayIntersectionDistance The distance from the origin to where the intersection occurs.
  59. //! @note The ray length is internally bounded (1000m) - this call is intended for editor functionality
  60. //! and anything greater than that distance away can usually safely be ignored.
  61. bool IntersectRayPolygon(
  62. const PolygonBound& polygonBound, const AZ::Vector3& rayOrigin, const AZ::Vector3& rayDirection,
  63. float& rayIntersectionDistance, int64_t& intersectedTriangleIndex);
  64. //! Perform a ray intersection against an edge, returning true if the ray intersects the
  65. //! edge within a tolerance defined by edgeScreenWidth, or false if it does not intersect.
  66. //! @param rayIntersectionDistance The distance from the origin to where the intersection occurs.
  67. //! @param edgeScreenWidth The width of the edge in screen space (adjusted based on the
  68. //! view distance from the camera).
  69. //! @note The ray length is internally bounded (1000m) - this call is intended for editor functionality
  70. //! and anything greater than that distance away can usually safely be ignored.
  71. bool IntersectRayEdge(
  72. const EdgeBound& edgeBound, float edgeScreenWidth, const AZ::Vector3& rayOrigin,
  73. const AZ::Vector3& rayDirection, float& rayIntersectionDistance);
  74. //! Perform a ray intersection against a vertex, returning true if the ray intersects the
  75. //! vertex within a tolerance defined by vertexScreenRadius, or false if it does not intersect.
  76. //! @param vertexScreenRadius The radius of the vertex in screen space (adjusted based on the
  77. //! view distance from the camera).
  78. //! @param rayIntersectionDistance The distance from the origin to where the intersection occurs.
  79. bool IntersectRayVertex(
  80. const VertexBound& vertexBound, float vertexScreenRadius, const AZ::Vector3& rayOrigin,
  81. const AZ::Vector3& rayDirection, float& rayIntersectionDistance);
  82. //! Performs intersection for a manipulator using a polygon bound.
  83. class ManipulatorBoundPolygon : public AzToolsFramework::Picking::BoundShapeInterface
  84. {
  85. public:
  86. AZ_RTTI(
  87. ManipulatorBoundPolygon, "{C662AE0A-B299-485F-8BF0-C2DFBB019B80}",
  88. AzToolsFramework::Picking::BoundShapeInterface);
  89. AZ_CLASS_ALLOCATOR_DECL
  90. explicit ManipulatorBoundPolygon(AzToolsFramework::Picking::RegisteredBoundId boundId)
  91. : AzToolsFramework::Picking::BoundShapeInterface(boundId)
  92. {
  93. }
  94. bool IntersectRay(
  95. const AZ::Vector3& rayOrigin, const AZ::Vector3& rayDirection, float& rayIntersectionDistance) const override;
  96. void SetShapeData(const AzToolsFramework::Picking::BoundRequestShapeBase& shapeData) override;
  97. PolygonBound m_polygonBound;
  98. };
  99. //! Implementation of BoundShapeInterfaces to create a concrete polygon bound.
  100. class BoundShapePolygon : public AzToolsFramework::Picking::BoundRequestShapeBase
  101. {
  102. public:
  103. AZ_RTTI(
  104. BoundShapePolygon, "{2FC93606-9E3A-47C8-A2DA-7C21ECA2190A}",
  105. AzToolsFramework::Picking::BoundRequestShapeBase)
  106. AZ_CLASS_ALLOCATOR_DECL
  107. AZStd::shared_ptr<AzToolsFramework::Picking::BoundShapeInterface> MakeShapeInterface(
  108. AzToolsFramework::Picking::RegisteredBoundId id) const override;
  109. AZStd::vector<AZ::Vector3> m_triangles;
  110. };
  111. //! Performs intersection for a manipulator using an edge bound.
  112. class ManipulatorBoundEdge : public AzToolsFramework::Picking::BoundShapeInterface
  113. {
  114. public:
  115. AZ_RTTI(
  116. ManipulatorBoundEdge, "{9CFB51B7-1631-42F4-AE92-613651A1D2F4}",
  117. AzToolsFramework::Picking::BoundShapeInterface)
  118. AZ_CLASS_ALLOCATOR_DECL
  119. explicit ManipulatorBoundEdge(AzToolsFramework::Picking::RegisteredBoundId boundId)
  120. : AzToolsFramework::Picking::BoundShapeInterface(boundId)
  121. {
  122. }
  123. bool IntersectRay(
  124. const AZ::Vector3& rayOrigin, const AZ::Vector3& rayDirection, float& rayIntersectionDistance) const override;
  125. void SetShapeData(const AzToolsFramework::Picking::BoundRequestShapeBase& shapeData) override;
  126. EdgeBound m_edgeBound;
  127. };
  128. //! Implementation of BoundShapeInterfaces to create a concrete edge bound.
  129. class BoundShapeEdge : public AzToolsFramework::Picking::BoundRequestShapeBase
  130. {
  131. public:
  132. AZ_RTTI(
  133. BoundShapeEdge, "{7DE957A8-383D-4699-A3A1-795E345ED818}", AzToolsFramework::Picking::BoundRequestShapeBase)
  134. AZ_CLASS_ALLOCATOR_DECL
  135. AZStd::shared_ptr<AzToolsFramework::Picking::BoundShapeInterface> MakeShapeInterface(
  136. AzToolsFramework::Picking::RegisteredBoundId id) const override;
  137. AZ::Vector3 m_start;
  138. AZ::Vector3 m_end;
  139. float m_radius;
  140. };
  141. } // namespace WhiteBox