|
| 1 | +/* |
| 2 | + * Copyright (C) 2024 Open Source Robotics Foundation |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | +*/ |
| 17 | + |
| 18 | +#ifndef GZ_SIM_COMPONENTS_RAYCASTDATA_HH_ |
| 19 | +#define GZ_SIM_COMPONENTS_RAYCASTDATA_HH_ |
| 20 | + |
| 21 | +#include <gz/math/Vector3.hh> |
| 22 | +#include <gz/sim/components/Factory.hh> |
| 23 | +#include <gz/sim/components/Component.hh> |
| 24 | +#include <gz/sim/components/Serialization.hh> |
| 25 | +#include <gz/sim/config.hh> |
| 26 | + |
| 27 | +#include <istream> |
| 28 | +#include <ostream> |
| 29 | +#include <vector> |
| 30 | + |
| 31 | +namespace gz |
| 32 | +{ |
| 33 | +namespace sim |
| 34 | +{ |
| 35 | +// Inline bracket to help doxygen filtering. |
| 36 | +inline namespace GZ_SIM_VERSION_NAMESPACE { |
| 37 | +namespace components |
| 38 | +{ |
| 39 | +/// \brief A struct that holds the information of a ray. |
| 40 | +struct RayInfo |
| 41 | +{ |
| 42 | + /// \brief Starting point of the ray in entity frame |
| 43 | + gz::math::Vector3d start; |
| 44 | + |
| 45 | + /// \brief Ending point of the ray in entity frame |
| 46 | + gz::math::Vector3d end; |
| 47 | +}; |
| 48 | + |
| 49 | +/// \brief A struct that holds the result of a raycasting operation. |
| 50 | +struct RaycastResultInfo |
| 51 | +{ |
| 52 | + /// \brief The hit point in entity frame |
| 53 | + gz::math::Vector3d point; |
| 54 | + |
| 55 | + /// \brief The fraction of the ray length at the intersection/hit point. |
| 56 | + double fraction; |
| 57 | + |
| 58 | + /// \brief The normal at the hit point in entity frame |
| 59 | + gz::math::Vector3d normal; |
| 60 | +}; |
| 61 | + |
| 62 | +/// @brief A struct that holds the raycasting data, including ray and results |
| 63 | +struct RaycastDataInfo |
| 64 | +{ |
| 65 | + /// @brief The rays to cast from the entity. |
| 66 | + std::vector<RayInfo> rays; |
| 67 | + |
| 68 | + /// @brief The results of the raycasting. |
| 69 | + std::vector<RaycastResultInfo> results; |
| 70 | +}; |
| 71 | +} |
| 72 | + |
| 73 | +namespace serializers |
| 74 | +{ |
| 75 | + /// \brief Specialization of DefaultSerializer for RaycastDataInfo |
| 76 | + template<> class DefaultSerializer<components::RaycastDataInfo> |
| 77 | + { |
| 78 | + public: static std::ostream &Serialize( |
| 79 | + std::ostream &_out, const components::RaycastDataInfo &) |
| 80 | + { |
| 81 | + return _out; |
| 82 | + } |
| 83 | + |
| 84 | + public: static std::istream &Deserialize( |
| 85 | + std::istream &_in, components::RaycastDataInfo &) |
| 86 | + { |
| 87 | + return _in; |
| 88 | + } |
| 89 | + }; |
| 90 | +} |
| 91 | + |
| 92 | +namespace components |
| 93 | +{ |
| 94 | +/// \brief A component type that contains the rays traced from an entity |
| 95 | +/// into a physics world, along with the results of the raycasting operation. |
| 96 | +/// |
| 97 | +/// This component is primarily used for applications that require raycasting. |
| 98 | +/// The target application defines the rays, and the physics system plugin |
| 99 | +/// updates the raycasting results during each update loop. |
| 100 | +using RaycastData = Component<RaycastDataInfo, class RaycastDataTag, |
| 101 | + serializers::DefaultSerializer<RaycastDataInfo>>; |
| 102 | + |
| 103 | +GZ_SIM_REGISTER_COMPONENT("gz_sim_components.RaycastData", RaycastData) |
| 104 | +} |
| 105 | +} |
| 106 | +} |
| 107 | +} |
| 108 | +#endif // GZ_SIM_COMPONENTS_RAYCASTDATA_HH_ |
0 commit comments