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