Utils.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 <AzCore/std/smart_ptr/make_shared.h>
  10. #include <ROS2/Utilities/ROS2Conversions.h>
  11. #include <SimulationInterfaces/SimulationEntityManagerRequestBus.h>
  12. #include <simulation_interfaces/msg/bounds.hpp>
  13. namespace ROS2SimulationInterfaces::Utils
  14. {
  15. template<typename RequestType>
  16. AZ::Outcome<SimulationInterfaces::EntityFilters, AZStd::string> GetEntityFiltersFromRequest(const RequestType& request)
  17. {
  18. SimulationInterfaces::EntityFilters filter;
  19. filter.m_nameFilter = request.filters.filter.c_str();
  20. uint8_t type = request.filters.bounds.type;
  21. if (type == simulation_interfaces::msg::Bounds::TYPE_BOX)
  22. {
  23. if (request.filters.bounds.points.size() != 2)
  24. {
  25. return AZ::Failure("Invalid number of points! Type 'TYPE_BOX' should have exactly 2 points.");
  26. }
  27. const auto& p1 = request.filters.bounds.points.front();
  28. const auto& p2 = request.filters.bounds.points.back();
  29. if (p1.x > p2.x || p1.y > p2.y || p1.z > p2.z)
  30. {
  31. return AZ::Failure("Invalid points! The first point should be lower than the second point.");
  32. }
  33. const auto upperRight = ROS2::ROS2Conversions::FromROS2Vector3(p2);
  34. const auto lowerLeft = ROS2::ROS2Conversions::FromROS2Vector3(p1);
  35. const AZ::Aabb aabb = AZ::Aabb::CreateFromMinMax(lowerLeft, upperRight);
  36. filter.m_boundsShape = AZStd::make_shared<Physics::BoxShapeConfiguration>(aabb.GetExtents());
  37. }
  38. else if (type == simulation_interfaces::msg::Bounds::TYPE_CONVEX_HULL)
  39. {
  40. if (request.filters.bounds.points.size() < 3)
  41. {
  42. return AZ::Failure("Invalid number of points! Type 'TYPE_CONVEX_HULL' should have at least 3 points.");
  43. }
  44. filter.m_boundsShape = AZStd::make_shared<Physics::ConvexHullShapeConfiguration>();
  45. }
  46. else if (type == simulation_interfaces::msg::Bounds::TYPE_SPHERE)
  47. {
  48. if (request.filters.bounds.points.size() != 2)
  49. {
  50. return AZ::Failure("Invalid number of points! Type 'TYPE_SPHERE' should have exactly 2 points.");
  51. }
  52. filter.m_boundsShape = AZStd::make_shared<Physics::SphereShapeConfiguration>(request.filters.bounds.points.back().x);
  53. filter.m_boundsPose = { ROS2::ROS2Conversions::FromROS2Vector3(request.filters.bounds.points.front()),
  54. AZ::Quaternion::CreateIdentity(),
  55. 1.0f };
  56. }
  57. return AZ::Success(AZStd::move(filter));
  58. }
  59. } // namespace ROS2SimulationInterfaces::Utils