Skip to content

Commit 3960542

Browse files
committed
use builtin types instead of protos
1 parent 1f51fa8 commit 3960542

File tree

4 files changed

+33
-59
lines changed

4 files changed

+33
-59
lines changed

src/viam/sdk/services/navigation.hpp

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
#include <viam/api/service/navigation/v1/navigation.pb.h>
1010

1111
#include <viam/sdk/services/service.hpp>
12+
#include <viam/sdk/spatialmath/geometry.hpp>
13+
#include <viam/sdk/common/pose.hpp>
14+
#include <viam/sdk/common/utils.hpp>
1215

1316
namespace viam {
1417
namespace sdk {
1518

16-
using namespace viam::common::v1;
17-
1819
class Navigation : public Service {
1920
public:
2021
enum class Mode : uint8_t {
@@ -31,47 +32,40 @@ class Navigation : public Service {
3132
};
3233

3334
struct LocationResponse {
34-
public:
35-
GeoPoint location;
35+
geo_point location;
3636
double compass_heading;
3737
};
3838

3939
struct Waypoint {
40-
Waypoint(const viam::service::navigation::v1::Waypoint& proto) {
41-
this->id = proto.id();
42-
this->location = proto.location();
43-
}
40+
Waypoint(const viam::service::navigation::v1::Waypoint& proto)
41+
: id(proto.id()), location(geo_point::from_proto(proto.location())) {}
4442

4543
operator viam::service::navigation::v1::Waypoint() {
4644
viam::service::navigation::v1::Waypoint ret;
4745
*ret.mutable_id() = id;
48-
*ret.mutable_location() = location;
46+
*ret.mutable_location() = location.to_proto();
4947
return ret;
5048
}
5149

5250
std::string id;
53-
GeoPoint location;
51+
geo_point location;
5452
};
5553

5654
struct Path {
57-
Path(const viam::service::navigation::v1::Path& proto) {
58-
this->destination_waypoint_id = proto.destination_waypoint_id();
59-
this->geopoints =
60-
std::vector<GeoPoint>(proto.geopoints().begin(), proto.geopoints().end());
61-
}
55+
Path(const viam::service::navigation::v1::Path& proto)
56+
: destination_waypoint_id(proto.destination_waypoint_id()) {
57+
repeatedPtrToVec(proto.geopoints(), geopoints);
58+
}
6259

6360
operator viam::service::navigation::v1::Path() {
6461
viam::service::navigation::v1::Path ret;
6562
*ret.mutable_destination_waypoint_id() = destination_waypoint_id;
66-
auto dest_points = ret.mutable_geopoints();
67-
for (const auto& x : geopoints) {
68-
*dest_points->Add() = x;
69-
}
63+
vecToRepeatedPtr(geopoints, *ret.mutable_geopoints());
7064
return ret;
7165
}
7266

7367
std::string destination_waypoint_id;
74-
std::vector<GeoPoint> geopoints;
68+
std::vector<geo_point> geopoints;
7569
};
7670

7771
API api() const override;
@@ -82,12 +76,12 @@ class Navigation : public Service {
8276
virtual std::unique_ptr<std::vector<Waypoint>> get_waypoints(const std::string name,
8377
const ProtoStruct& extra) = 0;
8478
virtual void add_waypoint(const std::string name,
85-
const GeoPoint& location,
79+
const geo_point& location,
8680
const ProtoStruct& extra) = 0;
8781
virtual void remove_waypoint(const std::string name,
8882
const std::string id,
8983
const ProtoStruct& extra) = 0;
90-
virtual std::unique_ptr<std::vector<GeoGeometry>> get_obstacles(const std::string name,
84+
virtual std::unique_ptr<std::vector<geo_geometry>> get_obstacles(const std::string name,
9185
const ProtoStruct& extra) = 0;
9286
virtual std::unique_ptr<std::vector<Path>> get_paths(const std::string name,
9387
const ProtoStruct& extra) = 0;

src/viam/sdk/services/private/navigation_client.cpp

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,12 @@ Navigation::LocationResponse NavigationClient::get_location(const std::string na
5353
})
5454
.invoke([](auto& response) {
5555
return Navigation::LocationResponse{
56-
response.location(),
56+
geo_point::from_proto(response.location()),
5757
response.compass_heading(),
5858
};
5959
});
6060
}
6161

62-
// helper; unpacks a repeated pointer to a vector of destination type.
63-
// Dst must have a constructor that takes Src*.
64-
template <typename Src, typename Dst>
65-
std::unique_ptr<std::vector<Dst>> repeatedPtrToVector(
66-
const ::google::protobuf::RepeatedPtrField<Src>& items) {
67-
auto vec = std::make_unique<std::vector<Dst>>();
68-
vec->reserve(items.size());
69-
for (auto& x : items) {
70-
vec->push_back(x);
71-
}
72-
return vec;
73-
}
74-
7562
std::unique_ptr<std::vector<Navigation::Waypoint>> NavigationClient::get_waypoints(
7663
const std::string name, const ProtoStruct& extra) {
7764
return make_client_helper(this, *stub_, &StubType::GetWaypoints)
@@ -80,18 +67,19 @@ std::unique_ptr<std::vector<Navigation::Waypoint>> NavigationClient::get_waypoin
8067
*request.mutable_extra() = map_to_struct(extra);
8168
})
8269
.invoke([](auto& response) {
83-
return repeatedPtrToVector<viam::service::navigation::v1::Waypoint,
84-
Navigation::Waypoint>(response.waypoints());
70+
auto ret = std::make_unique<std::vector<Navigation::Waypoint>>();
71+
repeatedPtrToVec(response.waypoints(), *ret);
72+
return ret;
8573
});
8674
}
8775

8876
void NavigationClient::add_waypoint(const std::string name,
89-
const GeoPoint& location,
77+
const geo_point& location,
9078
const ProtoStruct& extra) {
9179
return make_client_helper(this, *stub_, &StubType::AddWaypoint)
9280
.with([&](auto& request) {
9381
*request.mutable_name() = name;
94-
*request.mutable_location() = location;
82+
*request.mutable_location() = location.to_proto();
9583
*request.mutable_extra() = map_to_struct(extra);
9684
})
9785
.invoke([](auto& response) {});
@@ -109,16 +97,17 @@ void NavigationClient::remove_waypoint(const std::string name,
10997
.invoke([](auto& response) {});
11098
}
11199

112-
std::unique_ptr<std::vector<GeoGeometry>> NavigationClient::get_obstacles(
100+
std::unique_ptr<std::vector<geo_geometry>> NavigationClient::get_obstacles(
113101
const std::string name, const ProtoStruct& extra) {
114102
return make_client_helper(this, *stub_, &StubType::GetObstacles)
115103
.with([&](auto& request) {
116104
*request.mutable_name() = name;
117105
*request.mutable_extra() = map_to_struct(extra);
118106
})
119107
.invoke([](auto& response) {
120-
return std::make_unique<std::vector<GeoGeometry>>(response.obstacles().begin(),
121-
response.obstacles().end());
108+
auto ret = std::make_unique<std::vector<geo_geometry>>();
109+
repeatedPtrToVec(response.obstacles(), *ret);
110+
return ret;
122111
});
123112
}
124113

src/viam/sdk/services/private/navigation_client.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ class NavigationClient : public Navigation {
2727
std::unique_ptr<std::vector<Waypoint>> get_waypoints(const std::string name,
2828
const ProtoStruct& extra) override;
2929
void add_waypoint(const std::string name,
30-
const GeoPoint& location,
30+
const geo_point& location,
3131
const ProtoStruct& extra) override;
3232
void remove_waypoint(const std::string name,
3333
const std::string id,
3434
const ProtoStruct& extra) override;
35-
std::unique_ptr<std::vector<GeoGeometry>> get_obstacles(const std::string name,
35+
std::unique_ptr<std::vector<geo_geometry>> get_obstacles(const std::string name,
3636
const ProtoStruct& extra) override;
3737
std::unique_ptr<std::vector<Path>> get_paths(const std::string name,
3838
const ProtoStruct& extra) override;

src/viam/sdk/services/private/navigation_server.cpp

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,18 @@ ::grpc::Status NavigationServer::GetLocation(::grpc::ServerContext*,
4040
return make_service_helper<Navigation>(
4141
"NavigationServer::GetLocation", this, request)([&](auto& helper, auto& nav) {
4242
const auto& loc = nav->get_location(request->name(), helper.getExtra());
43-
*response->mutable_location() = loc.location;
43+
*response->mutable_location() = loc.location.to_proto();
4444
response->set_compass_heading(loc.compass_heading);
4545
});
4646
}
4747

48-
// helper; copies vec<Src> into RepeatedPtrField<Dst>.
49-
template <typename Src, typename Dst>
50-
void vecToRepeatedPtr(std::vector<Src>& vec, google::protobuf::RepeatedPtrField<Dst>* dest) {
51-
dest->Reserve(vec.size());
52-
for (auto& x : vec) {
53-
*dest->Add() = x;
54-
}
55-
}
56-
5748
::grpc::Status NavigationServer::GetWaypoints(::grpc::ServerContext*,
5849
const GetWaypointsRequest* request,
5950
GetWaypointsResponse* response) noexcept {
6051
return make_service_helper<Navigation>(
6152
"NavigationServer::GetWaypoints", this, request)([&](auto& helper, auto& nav) {
6253
const auto& waypoints = nav->get_waypoints(request->name(), helper.getExtra());
63-
vecToRepeatedPtr(*waypoints, response->mutable_waypoints());
54+
vecToRepeatedPtr(*waypoints, *response->mutable_waypoints());
6455
});
6556
}
6657

@@ -69,7 +60,7 @@ ::grpc::Status NavigationServer::AddWaypoint(::grpc::ServerContext*,
6960
AddWaypointResponse*) noexcept {
7061
return make_service_helper<Navigation>(
7162
"NavigationServer::AddWaypoint", this, request)([&](auto& helper, auto& nav) {
72-
nav->add_waypoint(request->name(), request->location(), helper.getExtra());
63+
nav->add_waypoint(request->name(), geo_point::from_proto(request->location()), helper.getExtra());
7364
});
7465
}
7566

@@ -88,7 +79,7 @@ ::grpc::Status NavigationServer::GetObstacles(::grpc::ServerContext*,
8879
return make_service_helper<Navigation>(
8980
"NavigationServer::GetObstacles", this, request)([&](auto& helper, auto& nav) {
9081
const auto& obstacles = nav->get_obstacles(request->name(), helper.getExtra());
91-
vecToRepeatedPtr(*obstacles, response->mutable_obstacles());
82+
vecToRepeatedPtr(*obstacles, *response->mutable_obstacles());
9283
});
9384
}
9485

@@ -98,7 +89,7 @@ ::grpc::Status NavigationServer::GetPaths(::grpc::ServerContext*,
9889
return make_service_helper<Navigation>(
9990
"NavigationServer::GetPaths", this, request)([&](auto& helper, auto& nav) {
10091
const auto& paths = nav->get_paths(request->name(), helper.getExtra());
101-
vecToRepeatedPtr(*paths, response->mutable_paths());
92+
vecToRepeatedPtr(*paths, *response->mutable_paths());
10293
});
10394
}
10495

0 commit comments

Comments
 (0)