|
| 1 | +/* |
| 2 | + * Copyright (C) 2021 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 | +#ifndef IGNITION_GAZEBO_COMPONENTS_REFERENCEMODELS_HH_ |
| 18 | +#define IGNITION_GAZEBO_COMPONENTS_REFERENCEMODELS_HH_ |
| 19 | + |
| 20 | +#include <iostream> |
| 21 | +#include <set> |
| 22 | + |
| 23 | +#include <ignition/gazebo/Entity.hh> |
| 24 | +#include <ignition/gazebo/components/Factory.hh> |
| 25 | +#include <ignition/gazebo/components/Component.hh> |
| 26 | +#include <ignition/gazebo/config.hh> |
| 27 | + |
| 28 | +namespace ignition |
| 29 | +{ |
| 30 | +namespace gazebo |
| 31 | +{ |
| 32 | +// Inline bracket to help doxygen filtering. |
| 33 | +inline namespace IGNITION_GAZEBO_VERSION_NAMESPACE { |
| 34 | +namespace components |
| 35 | +{ |
| 36 | + /// \brief Data structure that holds information about which models for |
| 37 | + /// a given link view this link as the model's canonical link. An example |
| 38 | + /// of multiple models having the same canonical link could be nested models. |
| 39 | + struct ReferenceModelsInfo |
| 40 | + { |
| 41 | + /// \brief The models which this link serves as a canonical link for. |
| 42 | + std::set<Entity> models; |
| 43 | + |
| 44 | + /// \brief Add a model that references this link as its canonical link. |
| 45 | + /// This should be called when a canonical link is assigned to a model. |
| 46 | + /// \param[in] _model The model to be added. |
| 47 | + public: void AddModel(const Entity &_model) |
| 48 | + { |
| 49 | + this->models.insert(_model); |
| 50 | + } |
| 51 | + |
| 52 | + /// \brief Remove a model that no longer references this link as its |
| 53 | + /// canonical link. This should be called when a model is removed/deleted. |
| 54 | + /// \param[in] _model The model to be removed. |
| 55 | + public: void RemoveModel(const Entity &_model) |
| 56 | + { |
| 57 | + this->models.erase(_model); |
| 58 | + } |
| 59 | + |
| 60 | + public: bool operator==(const ReferenceModelsInfo &_info) const |
| 61 | + { |
| 62 | + return this->models == _info.models; |
| 63 | + } |
| 64 | + |
| 65 | + public: bool operator!=(const ReferenceModelsInfo &_info) const |
| 66 | + { |
| 67 | + return !(*this == _info); |
| 68 | + } |
| 69 | + }; |
| 70 | +} |
| 71 | + |
| 72 | +namespace serializers |
| 73 | +{ |
| 74 | + /// \brief Serializer for ReferenceModelsInfo object |
| 75 | + class ReferenceModelsInfoSerializer |
| 76 | + { |
| 77 | + /// \brief Serialization for ReferenceModelsInfo. |
| 78 | + /// \param[out] _out Output stream. |
| 79 | + /// \param[in] _info ReferenceModelsInfo object to stream. |
| 80 | + /// \return The stream. |
| 81 | + public: static std::ostream &Serialize(std::ostream &_out, |
| 82 | + const components::ReferenceModelsInfo &_info) |
| 83 | + { |
| 84 | + for (const auto &model : _info.models) |
| 85 | + _out << model << " "; |
| 86 | + |
| 87 | + return _out; |
| 88 | + } |
| 89 | + |
| 90 | + /// \brief Deserialization for ReferenceModelsInfo. |
| 91 | + /// \param[in] _in Input stream. |
| 92 | + /// \param[out] _info ReferenceModelsInfo object to populate. |
| 93 | + /// \return The stream. |
| 94 | + public: static std::istream &Deserialize(std::istream &_in, |
| 95 | + components::ReferenceModelsInfo &_info) |
| 96 | + { |
| 97 | + _info.models.clear(); |
| 98 | + Entity model; |
| 99 | + while (_in >> model) |
| 100 | + _info.AddModel(model); |
| 101 | + |
| 102 | + return _in; |
| 103 | + }; |
| 104 | + }; |
| 105 | +} |
| 106 | + |
| 107 | +namespace components |
| 108 | +{ |
| 109 | + /// \brief A component that gives a mapping between a link and all of the |
| 110 | + /// models this link serves as a canonical link for. This component should |
| 111 | + /// only be applied to links. |
| 112 | + using ReferenceModels = |
| 113 | + Component<ReferenceModelsInfo, class ReferenceModelsTag, |
| 114 | + serializers::ReferenceModelsInfoSerializer>; |
| 115 | + IGN_GAZEBO_REGISTER_COMPONENT("ign_gazebo_components.ReferenceModels", |
| 116 | + ReferenceModels) |
| 117 | +} |
| 118 | +} |
| 119 | +} |
| 120 | +} |
| 121 | + |
| 122 | +#endif |
0 commit comments