@@ -34,6 +34,7 @@ TESSERACT_COMMON_IGNORE_WARNINGS_POP
3434
3535#include < tesseract_common/plugin_info.h>
3636#include < tesseract_common/calibration_info.h>
37+ #include < tesseract_common/collision_margin_data.h>
3738
3839namespace YAML
3940{
@@ -669,6 +670,194 @@ struct convert<tesseract_common::Toolpath>
669670 return true ;
670671 }
671672};
673+
674+ // =========================== CollisionMarginPairOverrideType Enum ===========================
675+ template <>
676+ struct convert <tesseract_common::CollisionMarginPairOverrideType>
677+ {
678+ static Node encode (const tesseract_common::CollisionMarginPairOverrideType& rhs)
679+ {
680+ // LCOV_EXCL_START
681+ static const std::map<tesseract_common::CollisionMarginPairOverrideType, std::string> m = {
682+ { tesseract_common::CollisionMarginPairOverrideType::NONE, " NONE" },
683+ { tesseract_common::CollisionMarginPairOverrideType::MODIFY, " MODIFY" },
684+ { tesseract_common::CollisionMarginPairOverrideType::REPLACE, " REPLACE" }
685+ };
686+ // LCOV_EXCL_STOP
687+ return Node (m.at (rhs));
688+ }
689+
690+ static bool decode (const Node& node, tesseract_common::CollisionMarginPairOverrideType& rhs)
691+ {
692+ // LCOV_EXCL_START
693+ static const std::map<std::string, tesseract_common::CollisionMarginPairOverrideType> inv = {
694+ { " NONE" , tesseract_common::CollisionMarginPairOverrideType::NONE },
695+ { " MODIFY" , tesseract_common::CollisionMarginPairOverrideType::MODIFY },
696+ { " REPLACE" , tesseract_common::CollisionMarginPairOverrideType::REPLACE }
697+ };
698+ // LCOV_EXCL_STOP
699+
700+ if (!node.IsScalar ())
701+ return false ;
702+
703+ auto it = inv.find (node.Scalar ());
704+ if (it == inv.end ())
705+ return false ;
706+
707+ rhs = it->second ;
708+ return true ;
709+ }
710+ };
711+
712+ // ============================ PairsCollisionMarginData ============================
713+ template <>
714+ struct convert <tesseract_common::PairsCollisionMarginData>
715+ {
716+ static Node encode (const tesseract_common::PairsCollisionMarginData& rhs)
717+ {
718+ Node node (NodeType::Map);
719+ for (const auto & pair : rhs)
720+ {
721+ Node key_node (NodeType::Sequence);
722+ key_node.push_back (pair.first .first );
723+ key_node.push_back (pair.first .second );
724+
725+ // tell yaml-cpp “emit this sequence in [a, b] inline style”
726+ key_node.SetStyle (YAML::EmitterStyle::Flow);
727+
728+ node[key_node] = pair.second ;
729+ }
730+
731+ return node;
732+ }
733+
734+ static bool decode (const Node& node, tesseract_common::PairsCollisionMarginData& rhs)
735+ {
736+ if (!node.IsMap ())
737+ return false ;
738+
739+ for (auto it = node.begin (); it != node.end (); ++it)
740+ {
741+ Node key_node = it->first ;
742+ if (!key_node.IsSequence () || key_node.size () != 2 )
743+ return false ;
744+
745+ std::pair<std::string, std::string> key;
746+ key.first = key_node[0 ].as <std::string>();
747+ key.second = key_node[1 ].as <std::string>();
748+
749+ auto v = it->second .as <double >();
750+ rhs.emplace (std::move (key), v);
751+ }
752+ return true ;
753+ }
754+ };
755+
756+ // ================================== CollisionMarginPairData =================================
757+ template <>
758+ struct convert <tesseract_common::CollisionMarginPairData>
759+ {
760+ static Node encode (const tesseract_common::CollisionMarginPairData& rhs)
761+ {
762+ const tesseract_common::PairsCollisionMarginData& data = rhs.getCollisionMargins ();
763+ return Node (data);
764+ }
765+
766+ static bool decode (const Node& node, tesseract_common::CollisionMarginPairData& rhs)
767+ {
768+ auto data = node.as <tesseract_common::PairsCollisionMarginData>();
769+ rhs = tesseract_common::CollisionMarginPairData (data);
770+ return true ;
771+ }
772+ };
773+
774+ // ============================ AllowedCollisionEntries ============================
775+ template <>
776+ struct convert <tesseract_common::AllowedCollisionEntries>
777+ {
778+ static Node encode (const tesseract_common::AllowedCollisionEntries& rhs)
779+ {
780+ Node node (NodeType::Map);
781+ for (const auto & pair : rhs)
782+ {
783+ Node key_node (NodeType::Sequence);
784+ key_node.push_back (pair.first .first );
785+ key_node.push_back (pair.first .second );
786+
787+ // tell yaml-cpp “emit this sequence in [a, b] inline style”
788+ key_node.SetStyle (YAML::EmitterStyle::Flow);
789+
790+ node[key_node] = pair.second ;
791+ }
792+
793+ return node;
794+ }
795+
796+ static bool decode (const Node& node, tesseract_common::AllowedCollisionEntries& rhs)
797+ {
798+ if (!node.IsMap ())
799+ return false ;
800+
801+ for (auto it = node.begin (); it != node.end (); ++it)
802+ {
803+ Node key_node = it->first ;
804+ if (!key_node.IsSequence () || key_node.size () != 2 )
805+ return false ;
806+
807+ std::pair<std::string, std::string> key;
808+ key.first = key_node[0 ].as <std::string>();
809+ key.second = key_node[1 ].as <std::string>();
810+
811+ auto v = it->second .as <std::string>();
812+ rhs.emplace (std::move (key), v);
813+ }
814+ return true ;
815+ }
816+ };
817+
818+ // ================================== AllowedCollisionMatrix =================================
819+ template <>
820+ struct convert <tesseract_common::AllowedCollisionMatrix>
821+ {
822+ static Node encode (const tesseract_common::AllowedCollisionMatrix& rhs)
823+ {
824+ const tesseract_common::AllowedCollisionEntries& data = rhs.getAllAllowedCollisions ();
825+ return Node (data);
826+ }
827+
828+ static bool decode (const Node& node, tesseract_common::AllowedCollisionMatrix& rhs)
829+ {
830+ auto data = node.as <tesseract_common::AllowedCollisionEntries>();
831+ rhs = tesseract_common::AllowedCollisionMatrix (data);
832+ return true ;
833+ }
834+ };
835+
836+ // ============================== std::unordered_map<std::string, bool> =============================
837+ template <>
838+ struct convert <std::unordered_map<std::string, bool >>
839+ {
840+ static Node encode (const std::unordered_map<std::string, bool >& rhs)
841+ {
842+ Node node (NodeType::Map);
843+ for (const auto & pair : rhs)
844+ node[pair.first ] = pair.second ;
845+
846+ return node;
847+ }
848+
849+ static bool decode (const Node& node, std::unordered_map<std::string, bool >& rhs)
850+ {
851+ if (!node.IsMap ())
852+ return false ;
853+
854+ for (auto it = node.begin (); it != node.end (); ++it)
855+ rhs[it->first .as <std::string>()] = it->second .as <bool >();
856+
857+ return true ;
858+ }
859+ };
860+
672861} // namespace YAML
673862
674863#endif // TESSERACT_COMMON_YAML_EXTENSTIONS_H
0 commit comments