json_conversions.cpp
Go to the documentation of this file.
1#include "json_conversions.h"
2
3#include <SimoxUtility/json/eigen_conversion.h>
4#include <SimoxUtility/shapes/json_conversions.h>
5
6#include "ObjectID.h"
7#include "ObjectPose.h"
8#include "Scene.h"
9#include "ice_conversions.h"
10
11void
12armarx::to_json(simox::json::json& j, const ObjectID& id)
13{
14 j["dataset"] = id.dataset();
15 j["className"] = id.className();
16 j["instanceName"] = id.instanceName();
17 j["str"] = id.str();
18}
19
20void
21armarx::from_json(const simox::json::json& j, ObjectID& id)
22{
23 id = {j.at("dataset").get<std::string>(),
24 j.at("className").get<std::string>(),
25 j.at("instanceName").get<std::string>()};
26}
27
28void
29armarx::objpose::to_json(simox::json::json& j, const ObjectPose& op)
30{
31 j["providerName"] = op.providerName;
32 j["objectType"] = ObjectTypeNames.to_name(op.objectType);
33
34 j["objectID"] = op.objectID;
35
36 j["objectPoseRobot"] = op.objectPoseRobot;
37 j["objectPoseGlobal"] = op.objectPoseGlobal;
38 j["objectPoseOriginal"] = op.objectPoseOriginal;
39 j["objectPoseOriginalFrame"] = op.objectPoseOriginalFrame;
40
41 j["robotConfig"] = op.robotConfig;
42 j["robotPose"] = op.robotPose;
43
44 j["confidence"] = op.confidence;
45 j["timestampMicroSeconds"] = op.timestamp.toMicroSecondsSinceEpoch();
46
47 if (op.localOOBB)
48 {
49 j["localOOBB"] = *op.localOOBB;
50 }
51}
52
53void
54armarx::objpose::from_json(const simox::json::json& j, ObjectPose& op)
55{
56 op.providerName = j.at("providerName");
57 op.objectType = ObjectTypeNames.from_name(j.at("objectType"));
58
59 op.objectID = j.at("objectID");
60
61 op.objectPoseRobot = j.at("objectPoseRobot");
62 op.objectPoseGlobal = j.at("objectPoseGlobal");
63 op.objectPoseOriginal = j.at("objectPoseOriginal");
64 op.objectPoseOriginalFrame = j.at("objectPoseOriginalFrame");
65
66 op.robotConfig = j.at("robotConfig").get<std::map<std::string, float>>();
67 op.robotPose = j.at("robotPose");
68
69 op.confidence = j.at("confidence");
70 op.timestamp = DateTime(Duration::MicroSeconds(j.at("timestampMicroSeconds")));
71
72 if (j.count("localOOBB"))
73 {
74 op.localOOBB = j.at("localOOBB").get<simox::OrientedBoxf>();
75 }
76}
77
78void
79armarx::objects::to_json(simox::json::json& j, const SceneObject& rhs)
80{
81 j["class"] = rhs.className;
82 j["instanceName"] = rhs.instanceName;
83 j["collection"] = rhs.collection;
84 j["position"] = rhs.position;
85 j["orientation"] = rhs.orientation.normalized();
86 if (rhs.isStatic.has_value())
87 {
88 j["isStatic"] = rhs.isStatic.value();
89 }
90 j["jointValues"] = rhs.jointValues;
91}
92
93void
94armarx::objects::from_json(const simox::json::json& j, SceneObject& rhs)
95{
96 j.at("class").get_to(rhs.className);
97 if (j.count("instanceName"))
98 {
99 j["instanceName"].get_to(rhs.instanceName);
100 }
101 else
102 {
103 rhs.instanceName.clear();
104 }
105 j.at("collection").get_to(rhs.collection);
106 j.at("position").get_to(rhs.position);
107 j.at("orientation").get_to(rhs.orientation);
108 rhs.orientation.normalize();
109
110 if (j.count("isStatic"))
111 {
112 rhs.isStatic = j.at("isStatic").get<bool>();
113 }
114 else
115 {
116 rhs.isStatic = std::nullopt;
117 }
118
119 if (j.count("jointValues"))
120 {
121 j.at("jointValues").get_to(rhs.jointValues);
122 }
123 else
124 {
125 rhs.jointValues.clear();
126 }
127}
128
129void
130armarx::objects::to_json(simox::json::json& j, const Scene& rhs)
131{
132 j["objects"] = rhs.objects;
133}
134
135void
136armarx::objects::from_json(const simox::json::json& j, Scene& rhs)
137{
138 j.at("objects").get_to(rhs.objects);
139}
static Duration MicroSeconds(std::int64_t microSeconds)
Constructs a duration in microseconds.
Definition Duration.cpp:24
A known object ID of the form "Dataset/ClassName" or "Dataset/ClassName/InstanceName".
Definition ObjectID.h:11
Represents a point in time.
Definition DateTime.h:25
std::int64_t toMicroSecondsSinceEpoch() const
Definition DateTime.cpp:87
void to_json(simox::json::json &j, const SceneObject &rhs)
void from_json(const simox::json::json &j, SceneObject &rhs)
void to_json(simox::json::json &j, const ObjectPose &op)
void from_json(const simox::json::json &j, ObjectPose &op)
const simox::meta::EnumNames< objpose::ObjectType > ObjectTypeNames
void from_json(const nlohmann::json &j, Vector2f &value)
void to_json(nlohmann::json &j, const Vector2f &value)
std::optional< bool > isStatic
Definition Scene.h:46
Eigen::Quaternionf orientation
Definition Scene.h:44
std::map< std::string, float > jointValues
Definition Scene.h:47
std::string instanceName
Definition Scene.h:40
Eigen::Vector3f position
Definition Scene.h:43
std::vector< SceneObject > objects
Definition Scene.h:57
An object pose as stored by the ObjectPoseStorage.
Definition ObjectPose.h:34
float confidence
Confidence in [0, 1] (1 = full, 0 = none).
Definition ObjectPose.h:96
armarx::ObjectID objectID
The object ID, i.e. dataset, class name and instance name.
Definition ObjectPose.h:56
std::map< std::string, float > robotConfig
The robot config when the object was observed.
Definition ObjectPose.h:86
std::string providerName
Name of the providing component.
Definition ObjectPose.h:59
DateTime timestamp
Source timestamp.
Definition ObjectPose.h:98
Eigen::Matrix4f objectPoseRobot
The object pose in the robot root frame.
Definition ObjectPose.h:66
Eigen::Matrix4f robotPose
The robot pose when the object was observed.
Definition ObjectPose.h:88
Eigen::Matrix4f objectPoseOriginal
The object pose in the frame it was originally localized in.
Definition ObjectPose.h:76
ObjectType objectType
Known or unknown object.
Definition ObjectPose.h:61
std::optional< simox::OrientedBoxf > localOOBB
Object bounding box in object's local coordinate frame.
Definition ObjectPose.h:102
Eigen::Matrix4f objectPoseGlobal
The object pose in the global frame.
Definition ObjectPose.h:71
std::string objectPoseOriginalFrame
The frame the object was originally localized in.
Definition ObjectPose.h:78