WhiteBoxModifierUtil.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 "EditorWhiteBoxComponentModeTypes.h"
  9. #include "WhiteBoxModifierUtil.h"
  10. #include <AzCore/std/sort.h>
  11. namespace WhiteBox
  12. {
  13. template<typename Intersection>
  14. static float IntersectionDistance(const AZStd::optional<Intersection>& intersection)
  15. {
  16. return intersection.has_value() ? intersection->m_intersection.m_closestDistance
  17. : std::numeric_limits<float>::max();
  18. }
  19. GeometryIntersection FindClosestGeometryIntersection(
  20. const AZStd::optional<EdgeIntersection>& edgeIntersection,
  21. const AZStd::optional<PolygonIntersection>& polygonIntersection,
  22. const AZStd::optional<VertexIntersection>& vertexIntersection)
  23. {
  24. if (!edgeIntersection.has_value() && !polygonIntersection.has_value() && !vertexIntersection.has_value())
  25. {
  26. return GeometryIntersection::None;
  27. }
  28. // simple wrapper type to help sort values
  29. struct IntersectionType
  30. {
  31. float m_distance;
  32. GeometryIntersection m_type;
  33. };
  34. AZStd::array<IntersectionType, 3> intersections{
  35. IntersectionType{IntersectionDistance(edgeIntersection), GeometryIntersection::Edge},
  36. IntersectionType{IntersectionDistance(polygonIntersection), GeometryIntersection::Polygon},
  37. IntersectionType{IntersectionDistance(vertexIntersection), GeometryIntersection::Vertex}};
  38. AZStd::sort(
  39. AZStd::begin(intersections), AZStd::end(intersections),
  40. [](const IntersectionType& lhs, const IntersectionType& rhs)
  41. {
  42. return lhs.m_distance < rhs.m_distance;
  43. });
  44. return intersections.front().m_type;
  45. }
  46. } // namespace WhiteBox