CylinderShape.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. #include "CylinderShapeComponent.h"
  9. #include <AzCore/Math/IntersectPoint.h>
  10. #include <AzCore/Math/IntersectSegment.h>
  11. #include <AzCore/Math/Transform.h>
  12. #include <AzCore/Serialization/EditContext.h>
  13. #include <AzCore/Serialization/SerializeContext.h>
  14. #include <AzCore/Math/MathUtils.h>
  15. #include <AzCore/Math/Random.h>
  16. #include <AzCore/Math/Sfmt.h>
  17. #include <AzFramework/Entity/EntityDebugDisplayBus.h>
  18. #include <Shape/ShapeDisplay.h>
  19. #include "Cry_GeoDistance.h"
  20. #include <random>
  21. namespace LmbrCentral
  22. {
  23. void CylinderShape::Reflect(AZ::ReflectContext* context)
  24. {
  25. CylinderShapeConfig::Reflect(context);
  26. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  27. {
  28. serializeContext->Class<CylinderShape>()
  29. ->Version(1)
  30. ->Field("Configuration", &CylinderShape::m_cylinderShapeConfig)
  31. ;
  32. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  33. {
  34. editContext->Class<CylinderShape>("Cylinder Shape", "Cylinder shape configuration parameters")
  35. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  36. ->DataElement(AZ::Edit::UIHandlers::Default, &CylinderShape::m_cylinderShapeConfig, "Cylinder Configuration", "Cylinder shape configuration")
  37. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
  38. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  39. ;
  40. }
  41. }
  42. }
  43. void CylinderShape::Activate(AZ::EntityId entityId)
  44. {
  45. m_entityId = entityId;
  46. m_currentTransform = AZ::Transform::CreateIdentity();
  47. AZ::TransformBus::EventResult(m_currentTransform, m_entityId, &AZ::TransformBus::Events::GetWorldTM);
  48. m_intersectionDataCache.InvalidateCache(InvalidateShapeCacheReason::ShapeChange);
  49. AZ::TransformNotificationBus::Handler::BusConnect(m_entityId);
  50. ShapeComponentRequestsBus::Handler::BusConnect(m_entityId);
  51. CylinderShapeComponentRequestsBus::Handler::BusConnect(m_entityId);
  52. }
  53. void CylinderShape::Deactivate()
  54. {
  55. CylinderShapeComponentRequestsBus::Handler::BusDisconnect();
  56. ShapeComponentRequestsBus::Handler::BusDisconnect();
  57. AZ::TransformNotificationBus::Handler::BusDisconnect();
  58. }
  59. void CylinderShape::InvalidateCache(InvalidateShapeCacheReason reason)
  60. {
  61. AZStd::unique_lock lock(m_mutex);
  62. m_intersectionDataCache.InvalidateCache(reason);
  63. }
  64. void CylinderShape::OnTransformChanged(const AZ::Transform& /*local*/, const AZ::Transform& world)
  65. {
  66. {
  67. AZStd::unique_lock lock(m_mutex);
  68. m_currentTransform = world;
  69. m_intersectionDataCache.InvalidateCache(InvalidateShapeCacheReason::TransformChange);
  70. }
  71. ShapeComponentNotificationsBus::Event(
  72. m_entityId, &ShapeComponentNotificationsBus::Events::OnShapeChanged,
  73. ShapeComponentNotifications::ShapeChangeReasons::TransformChanged);
  74. }
  75. float CylinderShape::GetHeight() const
  76. {
  77. AZStd::shared_lock lock(m_mutex);
  78. return m_cylinderShapeConfig.m_height;
  79. }
  80. float CylinderShape::GetRadius() const
  81. {
  82. AZStd::shared_lock lock(m_mutex);
  83. return m_cylinderShapeConfig.m_radius;
  84. }
  85. void CylinderShape::SetHeight(float height)
  86. {
  87. {
  88. AZStd::unique_lock lock(m_mutex);
  89. m_cylinderShapeConfig.m_height = height;
  90. m_intersectionDataCache.InvalidateCache(InvalidateShapeCacheReason::ShapeChange);
  91. }
  92. ShapeComponentNotificationsBus::Event(
  93. m_entityId, &ShapeComponentNotificationsBus::Events::OnShapeChanged,
  94. ShapeComponentNotifications::ShapeChangeReasons::ShapeChanged);
  95. }
  96. void CylinderShape::SetRadius(float radius)
  97. {
  98. {
  99. AZStd::unique_lock lock(m_mutex);
  100. m_cylinderShapeConfig.m_radius = radius;
  101. m_intersectionDataCache.InvalidateCache(InvalidateShapeCacheReason::ShapeChange);
  102. }
  103. ShapeComponentNotificationsBus::Event(
  104. m_entityId, &ShapeComponentNotificationsBus::Events::OnShapeChanged,
  105. ShapeComponentNotifications::ShapeChangeReasons::ShapeChanged);
  106. }
  107. static AZ::Vector3 SqrtVector3(const AZ::Vector3& v)
  108. {
  109. return AZ::Vector3(AZ::Simd::Vec3::Sqrt(v.GetSimdValue()));
  110. }
  111. // reference: http://www.iquilezles.org/www/articles/diskbbox/diskbbox.htm
  112. AZ::Aabb CylinderShape::GetEncompassingAabb() const
  113. {
  114. AZStd::shared_lock lock(m_mutex);
  115. m_intersectionDataCache.UpdateIntersectionParams(m_currentTransform, m_cylinderShapeConfig, &m_mutex);
  116. const AZ::Vector3 base = m_intersectionDataCache.m_baseCenterPoint;
  117. const AZ::Vector3 top = m_intersectionDataCache.m_baseCenterPoint + m_intersectionDataCache.m_axisVector;
  118. const AZ::Vector3 axis = m_intersectionDataCache.m_axisVector;
  119. if (m_cylinderShapeConfig.m_height <= 0.0f || m_cylinderShapeConfig.m_radius <= 0.0f)
  120. {
  121. return AZ::Aabb::CreateFromPoint(base);
  122. }
  123. else
  124. {
  125. const AZ::Vector3 e = m_intersectionDataCache.m_radius *
  126. SqrtVector3(AZ::Vector3::CreateOne() - axis * axis / axis.Dot(axis));
  127. return AZ::Aabb::CreateFromMinMax(
  128. (base - e).GetMin(top - e),
  129. (base + e).GetMax(top + e));
  130. }
  131. }
  132. void CylinderShape::GetTransformAndLocalBounds(AZ::Transform& transform, AZ::Aabb& bounds) const
  133. {
  134. AZStd::shared_lock lock(m_mutex);
  135. const AZ::Vector3 extent(m_cylinderShapeConfig.m_radius, m_cylinderShapeConfig.m_radius, m_cylinderShapeConfig.m_height * 0.5f);
  136. bounds = AZ::Aabb::CreateFromMinMax(-extent, extent);
  137. transform = m_currentTransform;
  138. }
  139. AZ::Vector3 CylinderShape::GenerateRandomPointInside(AZ::RandomDistributionType randomDistribution) const
  140. {
  141. AZStd::shared_lock lock(m_mutex);
  142. m_intersectionDataCache.UpdateIntersectionParams(m_currentTransform, m_cylinderShapeConfig, &m_mutex);
  143. const float minAngle = 0.0f;
  144. const float maxAngle = AZ::Constants::TwoPi;
  145. float halfHeight = m_intersectionDataCache.m_height * 0.5f;
  146. float maxRadius = m_intersectionDataCache.m_radius;
  147. // As std:normal_distribution requires a std:random_engine to be passed in, create one using a random seed that is guaranteed to be properly
  148. // random each time it is called
  149. time_t seedVal;
  150. seedVal = AZ::Sfmt::GetInstance().Rand64();
  151. std::default_random_engine generator;
  152. generator.seed(static_cast<unsigned int>(seedVal));
  153. float randomZ = 0.0f;
  154. float randomAngle = 0.0f;
  155. float randomRadius = 0.0f;
  156. // Points should be generated just inside the shape boundary
  157. halfHeight *= 0.999f;
  158. maxRadius *= 0.999f;
  159. switch (randomDistribution)
  160. {
  161. case AZ::RandomDistributionType::Normal:
  162. {
  163. const float meanRadius = 0.0f; //Mean for the radius should be 0. Negative radius is still valid
  164. const float meanZ = 0.0f; //We want the average height of generated points to be between the min height and the max height
  165. const float meanAngle = 0.0f; //There really isn't a good mean angle
  166. const float stdDevRadius = sqrtf(maxRadius); //StdDev of the radius will be the sqrt of the radius (the radius is the total variation)
  167. const float stdDevZ = sqrtf(halfHeight); //Same principle applied to the stdDev of the height
  168. const float stdDevAngle = sqrtf(maxAngle); //And the angle as well
  169. //Generate a random radius
  170. std::normal_distribution<float> normalDist = std::normal_distribution<float>(meanRadius, stdDevRadius);
  171. randomRadius = normalDist(generator);
  172. //Normal distributions can produce values higher than the desired max
  173. //This is very unlikely but we clamp anyway
  174. randomRadius = AZStd::clamp(randomRadius, -maxRadius, maxRadius);
  175. //Generate a random height
  176. normalDist = std::normal_distribution<float>(meanZ, stdDevZ);
  177. randomZ = normalDist(generator);
  178. randomZ = AZStd::clamp(randomZ, -halfHeight, halfHeight);
  179. //Generate a random angle along the circle
  180. normalDist = std::normal_distribution<float>(meanAngle, stdDevAngle);
  181. randomAngle = normalDist(generator);
  182. //Don't bother to clamp the angle because it doesn't matter if the angle is above 360 deg or below 0 deg
  183. }
  184. break;
  185. case AZ::RandomDistributionType::UniformReal:
  186. {
  187. std::uniform_real_distribution<float> uniformRealDist = std::uniform_real_distribution<float>(-maxRadius, maxRadius);
  188. randomRadius = uniformRealDist(generator);
  189. uniformRealDist = std::uniform_real_distribution<float>(-halfHeight, halfHeight);
  190. randomZ = uniformRealDist(generator);
  191. uniformRealDist = std::uniform_real_distribution<float>(minAngle, maxAngle);
  192. randomAngle = uniformRealDist(generator);
  193. }
  194. break;
  195. default:
  196. AZ_Warning("CylinderShape", false, "Unsupported random distribution type. Returning default vector (0,0,0)");
  197. break;
  198. }
  199. const AZ::Vector3 localRandomPoint = AZ::Vector3(
  200. randomRadius * cosf(randomAngle),
  201. randomRadius * sinf(randomAngle),
  202. randomZ);
  203. return m_currentTransform.TransformPoint(localRandomPoint);
  204. }
  205. bool CylinderShape::IsPointInside(const AZ::Vector3& point) const
  206. {
  207. AZStd::shared_lock lock(m_mutex);
  208. m_intersectionDataCache.UpdateIntersectionParams(m_currentTransform, m_cylinderShapeConfig, &m_mutex);
  209. return AZ::Intersect::PointCylinder(
  210. m_intersectionDataCache.m_baseCenterPoint,
  211. m_intersectionDataCache.m_axisVector,
  212. powf(m_intersectionDataCache.m_height, 2.0f),
  213. powf(m_intersectionDataCache.m_radius, 2.0f),
  214. point);
  215. }
  216. float CylinderShape::DistanceSquaredFromPoint(const AZ::Vector3& point) const
  217. {
  218. AZStd::shared_lock lock(m_mutex);
  219. m_intersectionDataCache.UpdateIntersectionParams(m_currentTransform, m_cylinderShapeConfig, &m_mutex);
  220. if (m_cylinderShapeConfig.m_height <= 0.0f || m_cylinderShapeConfig.m_radius <= 0.0f)
  221. {
  222. AZ::Vector3 diff = m_intersectionDataCache.m_baseCenterPoint - point;
  223. return diff.GetLengthSq();
  224. }
  225. return Distance::Point_CylinderSq(
  226. point, m_intersectionDataCache.m_baseCenterPoint,
  227. m_intersectionDataCache.m_baseCenterPoint + m_intersectionDataCache.m_axisVector,
  228. m_intersectionDataCache.m_radius);
  229. }
  230. bool CylinderShape::IntersectRay(const AZ::Vector3& src, const AZ::Vector3& dir, float& distance) const
  231. {
  232. AZStd::shared_lock lock(m_mutex);
  233. m_intersectionDataCache.UpdateIntersectionParams(m_currentTransform, m_cylinderShapeConfig, &m_mutex);
  234. float t1 = 0.0f, t2 = 0.0f;
  235. const bool intersection =
  236. AZ::Intersect::IntersectRayCappedCylinder(
  237. src, dir, m_intersectionDataCache.m_baseCenterPoint,
  238. m_intersectionDataCache.m_axisVector.GetNormalizedSafe(), m_intersectionDataCache.m_height,
  239. m_intersectionDataCache.m_radius, t1, t2) > 0;
  240. distance = AZ::GetMin(t1, t2);
  241. return intersection;
  242. }
  243. void CylinderShape::CylinderIntersectionDataCache::UpdateIntersectionParamsImpl(
  244. const AZ::Transform& currentTransform, const CylinderShapeConfig& configuration,
  245. [[maybe_unused]] const AZ::Vector3& currentNonUniformScale)
  246. {
  247. const float entityScale = currentTransform.GetUniformScale();
  248. m_axisVector = currentTransform.GetBasisZ().GetNormalizedSafe() * entityScale;
  249. m_baseCenterPoint = currentTransform.GetTranslation() - m_axisVector * (configuration.m_height * 0.5f);
  250. m_axisVector = m_axisVector * configuration.m_height;
  251. m_radius = configuration.m_radius * entityScale;
  252. m_height = configuration.m_height * entityScale;
  253. }
  254. void DrawCylinderShape(
  255. const ShapeDrawParams& shapeDrawParams, const CylinderShapeConfig& cylinderShapeConfig,
  256. AzFramework::DebugDisplayRequests& debugDisplay)
  257. {
  258. if (shapeDrawParams.m_filled)
  259. {
  260. debugDisplay.SetColor(shapeDrawParams.m_shapeColor.GetAsVector4());
  261. debugDisplay.DrawSolidCylinder(
  262. AZ::Vector3::CreateZero(), AZ::Vector3::CreateAxisZ(),
  263. cylinderShapeConfig.m_radius, cylinderShapeConfig.m_height, false);
  264. }
  265. debugDisplay.SetColor(shapeDrawParams.m_wireColor.GetAsVector4());
  266. debugDisplay.DrawWireCylinder(
  267. AZ::Vector3::CreateZero(), AZ::Vector3::CreateAxisZ(),
  268. cylinderShapeConfig.m_radius, cylinderShapeConfig.m_height);
  269. }
  270. } // namespace LmbrCentral