Skip to content

Commit e59af19

Browse files
authored
RSDK-9337: Introduce proto API bridge and insulate various geometry and math types from API (#332)
1 parent b9f2345 commit e59af19

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+1218
-1134
lines changed

src/viam/sdk/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ target_sources(viamsdk
142142
../../viam/sdk/common/exception.hpp
143143
../../viam/sdk/common/linear_algebra.hpp
144144
../../viam/sdk/common/pose.hpp
145+
../../viam/sdk/common/proto_convert.hpp
145146
../../viam/sdk/common/proto_value.hpp
146147
../../viam/sdk/common/service_helper.hpp
147148
../../viam/sdk/common/utils.hpp

src/viam/sdk/common/client_helper.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class ClientHelper {
5959
ProtoValue value = key->second;
6060
debug_key_ = *value.get<std::string>();
6161
}
62-
*request_.mutable_extra() = map_to_struct(extra);
62+
*request_.mutable_extra() = v2::to_proto(extra);
6363
return with(std::forward<RequestSetupCallable>(rsc));
6464
}
6565

src/viam/sdk/common/linear_algebra.cpp

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,55 +7,45 @@
77
namespace viam {
88
namespace sdk {
99

10-
Vector3::Vector3(scalar_type x, scalar_type y, scalar_type z) : data_{x, y, z} {};
11-
Vector3::Vector3() : Vector3(0.0, 0.0, 0.0){};
12-
1310
double Vector3::x() const {
14-
return this->data_[0];
11+
return this->data[0];
1512
}
1613

1714
double Vector3::y() const {
18-
return this->data_[1];
15+
return this->data[1];
1916
}
2017

2118
double Vector3::z() const {
22-
return this->data_[2];
19+
return this->data[2];
2320
}
2421

2522
Vector3& Vector3::set_x(double x) {
26-
this->data_[0] = x;
23+
this->data[0] = x;
2724
return *this;
2825
}
2926

3027
Vector3& Vector3::set_y(double y) {
31-
this->data_[1] = y;
28+
this->data[1] = y;
3229
return *this;
3330
}
3431

3532
Vector3& Vector3::set_z(double z) {
36-
this->data_[2] = z;
33+
this->data[2] = z;
3734
return *this;
3835
}
3936

40-
const std::array<double, 3>& Vector3::data() const {
41-
return this->data_;
42-
}
37+
namespace proto_convert_details {
4338

44-
std::array<double, 3>& Vector3::data() {
45-
return this->data_;
39+
void to_proto<Vector3>::operator()(const Vector3& self, common::v1::Vector3* proto) const {
40+
proto->set_x(self.x());
41+
proto->set_y(self.y());
42+
proto->set_z(self.z());
4643
}
4744

48-
viam::common::v1::Vector3 Vector3::to_proto() const {
49-
viam::common::v1::Vector3 result;
50-
result.set_x(x());
51-
result.set_y(y());
52-
result.set_z(z());
53-
return result;
54-
};
55-
56-
Vector3 Vector3::from_proto(const viam::common::v1::Vector3& vec) {
57-
return {vec.x(), vec.y(), vec.z()};
45+
Vector3 from_proto<common::v1::Vector3>::operator()(const common::v1::Vector3* proto) const {
46+
return {proto->x(), proto->y(), proto->z()};
5847
}
5948

49+
} // namespace proto_convert_details
6050
} // namespace sdk
6151
} // namespace viam

src/viam/sdk/common/linear_algebra.hpp

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,25 @@
55
#include <boost/qvm/vec.hpp>
66
#include <boost/qvm/vec_traits.hpp>
77

8-
#include <viam/api/common/v1/common.pb.h>
8+
#include <viam/sdk/common/proto_convert.hpp>
9+
10+
namespace viam {
11+
namespace common {
12+
namespace v1 {
13+
14+
class Vector3;
15+
}
16+
} // namespace common
17+
} // namespace viam
18+
919
namespace viam {
1020
namespace sdk {
1121

1222
// In the future, we may wish to inline this whole class
1323
// for performance reasons.
1424

15-
class Vector3 {
16-
public:
25+
struct Vector3 {
1726
using scalar_type = double;
18-
Vector3(scalar_type x, scalar_type y, scalar_type z);
19-
Vector3();
2027

2128
scalar_type x() const;
2229
scalar_type y() const;
@@ -28,15 +35,22 @@ class Vector3 {
2835
/// Set the z value of the vector (can be chained)
2936
Vector3& set_z(scalar_type z);
3037

31-
const std::array<scalar_type, 3>& data() const;
32-
std::array<scalar_type, 3>& data();
33-
viam::common::v1::Vector3 to_proto() const;
34-
static Vector3 from_proto(const viam::common::v1::Vector3& vec);
38+
std::array<scalar_type, 3> data;
39+
};
3540

36-
private:
37-
std::array<scalar_type, 3> data_;
41+
namespace proto_convert_details {
42+
43+
template <>
44+
struct to_proto<Vector3> {
45+
void operator()(const Vector3&, common::v1::Vector3*) const;
46+
};
47+
48+
template <>
49+
struct from_proto<common::v1::Vector3> {
50+
Vector3 operator()(const common::v1::Vector3*) const;
3851
};
3952

53+
} // namespace proto_convert_details
4054
} // namespace sdk
4155
} // namespace viam
4256

@@ -51,20 +65,20 @@ struct vec_traits<viam::sdk::Vector3> {
5165

5266
template <int I>
5367
static inline scalar_type& write_element(vec_type& v) {
54-
return v.data()[I];
68+
return v.data[I];
5569
}
5670

5771
template <int I>
5872
static inline scalar_type read_element(vec_type const& v) {
59-
return v.data()[I];
73+
return v.data[I];
6074
}
6175

6276
static inline scalar_type& write_element_idx(int i, vec_type& v) {
63-
return v.data()[i];
77+
return v.data[i];
6478
}
6579

6680
static inline scalar_type read_element_idx(int i, vec_type const& v) {
67-
return v.data()[i];
81+
return v.data[i];
6882
}
6983
};
7084

src/viam/sdk/common/pose.cpp

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,6 @@
55
namespace viam {
66
namespace sdk {
77

8-
common::v1::PoseInFrame pose_in_frame::to_proto() const {
9-
common::v1::PoseInFrame pif;
10-
*pif.mutable_reference_frame() = reference_frame;
11-
common::v1::Pose proto_pose;
12-
proto_pose.set_x(pose.coordinates.x);
13-
proto_pose.set_y(pose.coordinates.y);
14-
proto_pose.set_z(pose.coordinates.z);
15-
proto_pose.set_o_x(pose.orientation.o_x);
16-
proto_pose.set_o_y(pose.orientation.o_y);
17-
proto_pose.set_o_z(pose.orientation.o_z);
18-
proto_pose.set_theta(pose.theta);
19-
*pif.mutable_pose() = std::move(proto_pose);
20-
return pif;
21-
};
22-
23-
pose_in_frame pose_in_frame::from_proto(const common::v1::PoseInFrame& proto) {
24-
pose_in_frame pif;
25-
pif.reference_frame = proto.reference_frame();
26-
const auto& proto_pose = proto.pose();
27-
pif.pose.orientation.o_x = proto_pose.o_x();
28-
pif.pose.orientation.o_y = proto_pose.o_y();
29-
pif.pose.orientation.o_z = proto_pose.o_z();
30-
pif.pose.coordinates.x = proto_pose.x();
31-
pif.pose.coordinates.y = proto_pose.y();
32-
pif.pose.coordinates.z = proto_pose.z();
33-
pif.pose.theta = proto_pose.theta();
34-
35-
return pif;
36-
}
37-
388
bool operator==(const pose_in_frame& lhs, const pose_in_frame& rhs) {
399
return lhs.pose == rhs.pose && lhs.reference_frame == rhs.reference_frame;
4010
}
@@ -44,5 +14,48 @@ std::ostream& operator<<(std::ostream& os, const pose_in_frame& v) {
4414
return os;
4515
}
4616

17+
namespace proto_convert_details {
18+
19+
void to_proto<pose>::operator()(const pose& self, common::v1::Pose* proto) const {
20+
proto->set_x(self.coordinates.x);
21+
proto->set_y(self.coordinates.y);
22+
proto->set_z(self.coordinates.z);
23+
proto->set_o_x(self.orientation.o_x);
24+
proto->set_o_y(self.orientation.o_y);
25+
proto->set_o_z(self.orientation.o_z);
26+
proto->set_theta(self.theta);
27+
}
28+
29+
pose from_proto<common::v1::Pose>::operator()(const common::v1::Pose* proto) const {
30+
pose pose;
31+
pose.coordinates.x = proto->x();
32+
pose.coordinates.y = proto->y();
33+
pose.coordinates.z = proto->z();
34+
pose.orientation.o_x = proto->o_x();
35+
pose.orientation.o_y = proto->o_y();
36+
pose.orientation.o_z = proto->o_z();
37+
pose.theta = proto->theta();
38+
39+
return pose;
40+
}
41+
42+
void to_proto<pose_in_frame>::operator()(const pose_in_frame& self,
43+
common::v1::PoseInFrame* pif) const {
44+
*(pif->mutable_reference_frame()) = self.reference_frame;
45+
common::v1::Pose proto_pose;
46+
to_proto<pose>{}(self.pose, &proto_pose);
47+
*(pif->mutable_pose()) = std::move(proto_pose);
48+
};
49+
50+
pose_in_frame from_proto<common::v1::PoseInFrame>::operator()(
51+
const common::v1::PoseInFrame* proto) const {
52+
pose_in_frame pif;
53+
pif.reference_frame = proto->reference_frame();
54+
pif.pose = from_proto<common::v1::Pose>{}(&(proto->pose()));
55+
56+
return pif;
57+
}
58+
59+
} // namespace proto_convert_details
4760
} // namespace sdk
4861
} // namespace viam

src/viam/sdk/common/pose.hpp

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
#pragma once
22

3-
#include <common/v1/common.pb.h>
3+
#include <viam/sdk/common/proto_convert.hpp>
4+
5+
#include <ostream>
6+
7+
namespace viam {
8+
namespace common {
9+
namespace v1 {
10+
11+
class Pose;
12+
class PoseInFrame;
13+
} // namespace v1
14+
} // namespace common
15+
} // namespace viam
416

517
namespace viam {
618
namespace sdk {
@@ -20,16 +32,11 @@ struct pose {
2032
pose_orientation orientation;
2133
double theta;
2234

23-
static pose from_proto(const viam::common::v1::Pose& proto);
24-
viam::common::v1::Pose to_proto() const;
25-
2635
friend bool operator==(const pose& lhs, const pose& rhs);
2736
friend std::ostream& operator<<(std::ostream& os, const pose& v);
2837
};
2938

3039
struct pose_in_frame {
31-
viam::common::v1::PoseInFrame to_proto() const;
32-
static pose_in_frame from_proto(const viam::common::v1::PoseInFrame& proto);
3340
pose_in_frame(std::string reference_frame_, struct pose pose_)
3441
: reference_frame(std::move(reference_frame_)), pose(std::move(pose_)) {}
3542
pose_in_frame() {}
@@ -40,5 +47,29 @@ struct pose_in_frame {
4047
friend std::ostream& operator<<(std::ostream& os, const pose_in_frame& v);
4148
};
4249

50+
namespace proto_convert_details {
51+
52+
template <>
53+
struct to_proto<pose> {
54+
void operator()(const pose&, common::v1::Pose*) const;
55+
};
56+
57+
template <>
58+
struct from_proto<common::v1::Pose> {
59+
pose operator()(const common::v1::Pose*) const;
60+
};
61+
62+
template <>
63+
struct to_proto<pose_in_frame> {
64+
void operator()(const pose_in_frame&, common::v1::PoseInFrame*) const;
65+
};
66+
67+
template <>
68+
struct from_proto<common::v1::PoseInFrame> {
69+
pose_in_frame operator()(const common::v1::PoseInFrame*) const;
70+
};
71+
72+
} // namespace proto_convert_details
73+
4374
} // namespace sdk
4475
} // namespace viam

src/viam/sdk/common/private/proto_utils.hpp

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)