Skip to content

Commit 27f147c

Browse files
committed
also do translation and linkconfig
1 parent 8479d34 commit 27f147c

File tree

6 files changed

+71
-46
lines changed

6 files changed

+71
-46
lines changed

src/viam/sdk/config/resource.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ ResourceConfig ResourceConfig::from_proto(const viam::app::v1::ComponentConfig&
104104
resource.fix_api();
105105

106106
if (proto_cfg.has_frame()) {
107-
resource.frame_ = LinkConfig::from_proto(proto_cfg.frame());
107+
resource.frame_ = v2::from_proto(proto_cfg.frame());
108108
}
109109

110110
return resource;
@@ -133,12 +133,13 @@ viam::app::v1::ComponentConfig ResourceConfig::to_proto() const {
133133
for (const auto& dep : depends_on_) {
134134
*proto_cfg.mutable_depends_on()->Add() = dep;
135135
}
136-
*proto_cfg.mutable_frame() = frame_.to_proto();
136+
*proto_cfg.mutable_frame() = v2::to_proto(frame_);
137137

138138
return proto_cfg;
139139
}
140140

141-
ResourceConfig::ResourceConfig(std::string type) : api_({kRDK, type, ""}), type_(std::move(type)){};
141+
ResourceConfig::ResourceConfig(std::string type)
142+
: api_({kRDK, type, ""}), type_(std::move(type)) {};
142143

143144
} // namespace sdk
144145
} // namespace viam

src/viam/sdk/referenceframe/frame.cpp

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,50 +12,45 @@
1212
namespace viam {
1313
namespace sdk {
1414

15-
viam::app::v1::Frame LinkConfig::to_proto() const {
16-
viam::app::v1::Frame frame;
15+
LinkConfig::LinkConfig(translation t, Orientation o, GeometryConfig gcfg, std::string parent)
16+
: translation_(std::move(t)),
17+
orientation_(std::move(o)),
18+
geometry_(std::move(gcfg)),
19+
parent_(std::move(parent)) {}
1720

18-
*frame.mutable_parent() = parent_;
19-
*frame.mutable_geometry() = v2::to_proto(geometry_);
20-
*frame.mutable_orientation() = v2::to_proto(orientation_);
21-
*frame.mutable_translation() = v2::to_proto(translation_);
22-
return frame;
23-
};
24-
25-
LinkConfig LinkConfig::from_proto(const viam::app::v1::Frame& proto) {
26-
LinkConfig lc;
27-
28-
lc.parent_ = proto.parent();
29-
lc.translation_.x = proto.translation().x();
30-
lc.translation_.y = proto.translation().y();
31-
lc.translation_.z = proto.translation().z();
32-
33-
if (proto.has_orientation()) {
34-
lc.orientation_ = v2::from_proto(proto.orientation());
35-
}
36-
37-
if (proto.has_geometry()) {
38-
lc.geometry_ = v2::from_proto(proto.geometry());
39-
}
40-
41-
return lc;
42-
};
43-
44-
translation LinkConfig::get_translation() const {
21+
const translation& LinkConfig::get_translation() const {
4522
return translation_;
4623
}
4724

4825
const Orientation& LinkConfig::get_orientation() const {
4926
return orientation_;
5027
}
5128

52-
GeometryConfig LinkConfig::get_geometry_config() const {
29+
const GeometryConfig& LinkConfig::get_geometry_config() const {
5330
return geometry_;
5431
}
5532

56-
std::string LinkConfig::get_parent() const {
33+
const std::string& LinkConfig::get_parent() const {
5734
return parent_;
5835
}
5936

37+
namespace proto_convert_details {
38+
39+
void to_proto<LinkConfig>::operator()(const LinkConfig& self, app::v1::Frame* proto) const {
40+
*(proto->mutable_parent()) = self.get_parent();
41+
*(proto->mutable_geometry()) = v2::to_proto(self.get_geometry_config());
42+
*(proto->mutable_orientation()) = v2::to_proto(self.get_orientation());
43+
*(proto->mutable_translation()) = v2::to_proto(self.get_translation());
44+
}
45+
46+
LinkConfig from_proto<app::v1::Frame>::operator()(const app::v1::Frame* proto) const {
47+
return LinkConfig(
48+
v2::from_proto(proto->translation()),
49+
proto->has_orientation() ? v2::from_proto(proto->orientation()) : Orientation{},
50+
proto->has_geometry() ? v2::from_proto(proto->geometry()) : GeometryConfig{},
51+
proto->parent());
52+
}
53+
54+
} // namespace proto_convert_details
6055
} // namespace sdk
6156
} // namespace viam

src/viam/sdk/referenceframe/frame.hpp

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,48 @@
22

33
#include <string>
44

5-
#include <viam/api/app/v1/robot.pb.h>
6-
5+
#include <viam/sdk/common/proto_convert.hpp>
76
#include <viam/sdk/spatialmath/geometry.hpp>
87
#include <viam/sdk/spatialmath/orientation.hpp>
98

9+
VIAM_SDK_API_FWD_NAMESPACE_BEGIN(app)
10+
11+
class Frame;
12+
13+
VIAM_SDK_API_FWD_NAMESPACE_END
14+
1015
namespace viam {
1116
namespace sdk {
1217

1318
class LinkConfig {
1419
public:
15-
viam::app::v1::Frame to_proto() const;
16-
static LinkConfig from_proto(const viam::app::v1::Frame& proto);
17-
translation get_translation() const;
20+
LinkConfig() = default;
21+
LinkConfig(translation t, Orientation o, GeometryConfig gcfg, std::string parent);
22+
23+
const translation& get_translation() const;
1824
const Orientation& get_orientation() const;
19-
GeometryConfig get_geometry_config() const;
20-
std::string get_parent() const;
25+
const GeometryConfig& get_geometry_config() const;
26+
const std::string& get_parent() const;
2127

2228
private:
23-
std::string id_;
2429
translation translation_;
2530
Orientation orientation_;
2631
GeometryConfig geometry_;
2732
std::string parent_;
2833
};
2934

35+
namespace proto_convert_details {
36+
37+
template <>
38+
struct to_proto<LinkConfig> {
39+
void operator()(const LinkConfig&, app::v1::Frame*) const;
40+
};
41+
42+
template <>
43+
struct from_proto<app::v1::Frame> {
44+
LinkConfig operator()(const app::v1::Frame*) const;
45+
};
46+
47+
} // namespace proto_convert_details
3048
} // namespace sdk
3149
} // namespace viam

src/viam/sdk/spatialmath/orientation_types.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@
55
namespace viam {
66
namespace sdk {
77

8-
void proto_convert_details::to_proto<translation>::operator()(const translation& self,
9-
app::v1::Translation* t) const {
8+
namespace proto_convert_details {
9+
10+
void to_proto<translation>::operator()(const translation& self, app::v1::Translation* t) const {
1011
t->set_x(self.x);
1112
t->set_y(self.y);
1213
t->set_z(self.z);
1314
}
1415

16+
translation from_proto<app::v1::Translation>::operator()(const app::v1::Translation* proto) const {
17+
return {proto->x(), proto->y(), proto->z()};
18+
}
19+
20+
} // namespace proto_convert_details
1521
} // namespace sdk
1622
} // namespace viam

src/viam/sdk/spatialmath/orientation_types.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ struct to_proto<translation> {
5050
void operator()(const translation&, app::v1::Translation*) const;
5151
};
5252

53+
template <>
54+
struct from_proto<app::v1::Translation> {
55+
translation operator()(const app::v1::Translation*) const;
56+
};
57+
5358
} // namespace proto_convert_details
5459
} // namespace sdk
5560
} // namespace viam

src/viam/sdk/tests/test_resource.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ BOOST_AUTO_TEST_CASE(test_linkconfig) {
134134
*frame.mutable_orientation() = o;
135135
*frame.mutable_translation() = t;
136136

137-
LinkConfig lc = LinkConfig::from_proto(frame);
137+
LinkConfig lc = v2::from_proto(frame);
138138
BOOST_CHECK_EQUAL(lc.get_parent(), "parent");
139139
BOOST_CHECK_EQUAL(lc.get_translation().x, t.x());
140140
BOOST_CHECK_EQUAL(lc.get_translation().y, t.y());
@@ -151,7 +151,7 @@ BOOST_AUTO_TEST_CASE(test_linkconfig) {
151151
BOOST_CHECK_EQUAL(gs.box().dims_mm().y(), box.dims_mm().y());
152152
BOOST_CHECK_EQUAL(gs.box().dims_mm().z(), box.dims_mm().z());
153153

154-
viam::app::v1::Frame proto_lc = lc.to_proto();
154+
viam::app::v1::Frame proto_lc = v2::to_proto(lc);
155155
BOOST_CHECK_EQUAL(proto_lc.parent(), "parent");
156156
BOOST_CHECK_EQUAL(proto_lc.translation().x(), t.x());
157157
BOOST_CHECK_EQUAL(proto_lc.translation().y(), t.y());

0 commit comments

Comments
 (0)