ice_conversions.cpp
Go to the documentation of this file.
1#include "ice_conversions.h"
2
3#include <SimoxUtility/algorithm/string.h>
4#include <SimoxUtility/shapes/AxisAlignedBoundingBox.h>
5#include <SimoxUtility/shapes/OrientedBox.h>
6#include <VirtualRobot/Robot.h>
7#include <VirtualRobot/RobotConfig.h>
8
10
15
16namespace armarx
17{
18 std::ostream&
19 data::operator<<(std::ostream& os, const ObjectID& id)
20 {
21 os << "'" << id.dataset << "/" << id.className;
22 if (!id.instanceName.empty())
23 {
24 os << "/" << id.instanceName;
25 }
26 return os << "'";
27 }
28
29} // namespace armarx
30
31void
32armarx::fromIce(const data::ObjectID& ice, ObjectID& id)
33{
34 id = fromIce(ice);
35}
36
38armarx::fromIce(const data::ObjectID& ice)
39{
40 return {ice.dataset, ice.className, ice.instanceName};
41}
42
43void
44armarx::toIce(data::ObjectID& ice, const ObjectID& id)
45{
46 ice.dataset = id.dataset();
47 ice.className = id.className();
48 ice.instanceName = id.instanceName();
49}
50
51armarx::data::ObjectID
53{
54 data::ObjectID ice;
55 toIce(ice, id);
56 return ice;
57}
58
59void
60armarx::fromIce(const data::ObjectIDSeq& ice, std::vector<ObjectID>& ids)
61{
62 ids.clear();
63 std::transform(ice.begin(),
64 ice.end(),
65 std::back_inserter(ids),
66 static_cast<ObjectID (*)(const data::ObjectID&)>(&fromIce));
67}
68
69std::vector<armarx::ObjectID>
70armarx::fromIce(const data::ObjectIDSeq& ice)
71{
72 std::vector<ObjectID> ids;
73 fromIce(ice, ids);
74 return ids;
75}
76
77void
78armarx::toIce(data::ObjectIDSeq& ice, const std::vector<ObjectID>& ids)
79{
80 ice.clear();
81 std::transform(ids.begin(),
82 ids.end(),
83 std::back_inserter(ice),
84 static_cast<data::ObjectID (*)(const ObjectID&)>(&toIce));
85}
86
87armarx::data::ObjectIDSeq
88armarx::toIce(const std::vector<ObjectID>& ids)
89{
90 data::ObjectIDSeq ice;
91 toIce(ice, ids);
92 return ice;
93}
94
95namespace armarx
96{
97 const simox::meta::EnumNames<objpose::ObjectType> objpose::ObjectTypeNames = {
98 {objpose::ObjectType::AnyObject, "AnyObject"},
99 {objpose::ObjectType::KnownObject, "KnownObject"},
100 {objpose::ObjectType::UnknownObject, "UnknownObject"}};
101
102 objpose::AABB
103 objpose::toIce(const simox::AxisAlignedBoundingBox& aabb)
104 {
105 objpose::AABB ice;
106 ice.center = new Vector3(aabb.center());
107 ice.extents = new Vector3(aabb.extents());
108 return ice;
109 }
110
111 void
112 objpose::fromIce(const Box& box, simox::OrientedBoxf& oobb)
113 {
114 try
115 {
116 Eigen::Vector3f pos = armarx::fromIce(box.position);
117 Eigen::Matrix3f ori = armarx::fromIce(box.orientation).toRotationMatrix();
118 Eigen::Vector3f extents = armarx::fromIce(box.extents);
119 Eigen::Vector3f corner = pos - ori * extents / 2;
120
122 corner, ori.col(0) * extents(0), ori.col(1) * extents(1), ori.col(2) * extents(2));
123 }
124 catch (const armarx::LocalException&)
125 {
126 // No OOBB information.
127 oobb = {};
128 }
129 }
130
131 void
132 objpose::fromIce(const BoxPtr& box, std::optional<simox::OrientedBox<float>>& oobb)
133 {
134 if (box)
135 {
136 oobb = fromIce(*box);
137 }
138 else
139 {
140 oobb = std::nullopt;
141 }
142 }
143
144 simox::OrientedBoxf
146 {
147 simox::OrientedBoxf oobb;
148 fromIce(box, oobb);
149 return oobb;
150 }
151
152 void
153 objpose::toIce(Box& box, const simox::OrientedBoxf& oobb)
154 {
155 box.position = new Vector3(oobb.center());
156 box.orientation = new Quaternion(oobb.rotation().eval());
157 box.extents = new Vector3(oobb.dimensions());
158 }
159
160 void
161 objpose::toIce(BoxPtr& box, const std::optional<simox::OrientedBox<float>>& oobb)
162 {
163 if (oobb)
164 {
165 box = new Box();
166 toIce(*box, *oobb);
167 }
168 }
169
170 objpose::Box
171 objpose::toIce(const simox::OrientedBoxf& oobb)
172 {
173 objpose::Box box;
174 toIce(box, oobb);
175 return box;
176 }
177
178 void
179 objpose::fromIce(const data::PoseManifoldGaussian& ice, PoseManifoldGaussian& cov)
180 {
181 // Only construct error message if necessary.
182 if (static_cast<Eigen::Index>(ice.covariance6x6.size()) != cov.covariance.size())
183 {
184 ARMARX_CHECK_EQUAL(static_cast<Eigen::Index>(ice.covariance6x6.size()),
185 cov.covariance.size())
186 << "Float sequence representing 6x6 covariance matrix must have "
187 << cov.covariance.size() << " values, but has " << ice.covariance6x6.size()
188 << ": \n"
189 << "[" << simox::alg::join(simox::alg::multi_to_string(ice.covariance6x6), ", ")
190 << "]";
191 }
192
193 armarx::fromIce(ice.mean, cov.mean);
194 cov.covariance = Eigen::MatrixXf::Map(
195 ice.covariance6x6.data(), cov.covariance.rows(), cov.covariance.cols());
196 }
197
198 void
199 objpose::fromIce(const data::PoseManifoldGaussianPtr& ice,
200 std::optional<PoseManifoldGaussian>& cov)
201 {
202 if (ice)
203 {
204 cov = PoseManifoldGaussian();
205 fromIce(*ice, cov.value());
206 }
207 else
208 {
209 cov = std::nullopt;
210 }
211 }
212
213 std::optional<objpose::PoseManifoldGaussian>
214 objpose::fromIce(const data::PoseManifoldGaussianPtr& ice)
215 {
216 std::optional<objpose::PoseManifoldGaussian> cov;
217 fromIce(ice, cov);
218 return cov;
219 }
220
221 void
222 objpose::toIce(data::PoseManifoldGaussian& ice, const PoseManifoldGaussian& cov)
223 {
224 armarx::toIce(ice.mean, cov.mean);
225
226 ice.covariance6x6.resize(cov.covariance.size());
227 Eigen::MatrixXf::Map(ice.covariance6x6.data(),
228 cov.covariance.rows(),
229 cov.covariance.cols()) = cov.covariance;
230 }
231
232 void
233 objpose::toIce(data::PoseManifoldGaussianPtr& ice,
234 const std::optional<PoseManifoldGaussian>& cov)
235 {
236 if (cov.has_value())
237 {
238 ice = new data::PoseManifoldGaussian;
239 toIce(*ice, cov.value());
240 }
241 else
242 {
243 ice = nullptr;
244 }
245 }
246
247 objpose::data::PoseManifoldGaussianPtr
248 objpose::toIce(const std::optional<PoseManifoldGaussian>& cov)
249 {
250 data::PoseManifoldGaussianPtr ice;
251 toIce(ice, cov);
252 return ice;
253 }
254
255} // namespace armarx
A known object ID of the form "Dataset/ClassName" or "Dataset/ClassName/InstanceName".
Definition ObjectID.h:11
std::string dataset() const
Definition ObjectID.h:24
The Quaternion class.
Definition Pose.h:174
The Vector3 class.
Definition Pose.h:113
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
std::ostream & operator<<(std::ostream &os, const ObjectID &id)
void fromIce(const Box &box, simox::OrientedBox< float > &oobb)
objpose::AABB toIce(const simox::AxisAlignedBoundingBox &aabb)
const simox::meta::EnumNames< objpose::ObjectType > ObjectTypeNames
This file offers overloads of toIce() and fromIce() functions for STL container types.
void fromIce(const std::map< IceKeyT, IceValueT > &iceMap, boost::container::flat_map< CppKeyT, CppValueT > &cppMap)
void toIce(std::map< IceKeyT, IceValueT > &iceMap, const boost::container::flat_map< CppKeyT, CppValueT > &cppMap)
A "gaussian" distribution in pose space (i.e.
Eigen::Matrix4f mean
The mean (i.e. a pose).
Eigen::Matrix< float, 6, 6 > covariance
The covariance matrix.