Skip to content

Commit 9427ed1

Browse files
mattmacf98lia-viam
andauthored
Feat implement get 3d meshes (#504)
Co-authored-by: Lia Stratopoulos <[email protected]>
1 parent 8b4cfe3 commit 9427ed1

File tree

11 files changed

+139
-1
lines changed

11 files changed

+139
-1
lines changed

src/viam/sdk/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ target_sources(viamsdk
6666
common/exception.cpp
6767
common/instance.cpp
6868
common/linear_algebra.cpp
69+
common/mesh.cpp
6970
common/pose.cpp
7071
common/proto_value.cpp
7172
common/utils.cpp
@@ -177,6 +178,7 @@ target_sources(viamsdk
177178
../../viam/sdk/common/exception.hpp
178179
../../viam/sdk/common/instance.hpp
179180
../../viam/sdk/common/linear_algebra.hpp
181+
../../viam/sdk/common/mesh.hpp
180182
../../viam/sdk/common/pose.hpp
181183
../../viam/sdk/common/proto_convert.hpp
182184
../../viam/sdk/common/proto_value.hpp

src/viam/sdk/common/mesh.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <viam/sdk/common/mesh.hpp>
2+
3+
#include <viam/sdk/common/utils.hpp>
4+
5+
#include <common/v1/common.pb.h>
6+
7+
namespace viam {
8+
namespace sdk {
9+
10+
namespace proto_convert_details {
11+
void to_proto_impl<mesh>::operator()(const mesh& self, common::v1::Mesh* proto) const {
12+
proto->set_content_type(self.content_type);
13+
proto->set_mesh(bytes_to_string(self.data));
14+
}
15+
16+
mesh from_proto_impl<common::v1::Mesh>::operator()(const common::v1::Mesh* proto) const {
17+
return mesh{proto->content_type(), string_to_bytes(proto->mesh())};
18+
}
19+
20+
} // namespace proto_convert_details
21+
} // namespace sdk
22+
} // namespace viam

src/viam/sdk/common/mesh.hpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#pragma once
2+
3+
#include <viam/sdk/common/proto_convert.hpp>
4+
5+
#include <string>
6+
#include <vector>
7+
8+
namespace viam {
9+
namespace common {
10+
namespace v1 {
11+
12+
class Mesh;
13+
14+
} // namespace v1
15+
} // namespace common
16+
} // namespace viam
17+
18+
namespace viam {
19+
namespace sdk {
20+
21+
struct mesh {
22+
std::string content_type;
23+
std::vector<unsigned char> data;
24+
};
25+
26+
namespace proto_convert_details {
27+
28+
template <>
29+
struct to_proto_impl<mesh> {
30+
void operator()(const mesh&, common::v1::Mesh*) const;
31+
};
32+
33+
template <>
34+
struct from_proto_impl<common::v1::Mesh> {
35+
mesh operator()(const common::v1::Mesh*) const;
36+
};
37+
38+
} // namespace proto_convert_details
39+
40+
} // namespace sdk
41+
} // namespace viam

src/viam/sdk/components/arm.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <boost/optional/optional.hpp>
99
#include <boost/variant/variant.hpp>
1010

11+
#include <viam/sdk/common/mesh.hpp>
1112
#include <viam/sdk/common/pose.hpp>
1213
#include <viam/sdk/resource/stoppable.hpp>
1314
#include <viam/sdk/spatialmath/geometry.hpp>
@@ -142,6 +143,17 @@ class Arm : public Component, public Stoppable {
142143
return get_kinematics({});
143144
}
144145

146+
/// @brief Returns `3DModel`s associated with the calling arm
147+
/// @param extra Any additional arguments to the method
148+
/// @return A map of `3DModel`s associated with the calling arm
149+
virtual std::map<std::string, mesh> get_3d_models(const ProtoStruct& extra) = 0;
150+
151+
/// @brief Returns `3DModel`s associated with the calling arm
152+
/// @return A map of `3DModel`s associated with the calling arm
153+
inline std::map<std::string, mesh> get_3d_models() {
154+
return get_3d_models({});
155+
}
156+
145157
/// @brief Returns `GeometryConfig`s associated with the calling arm
146158
inline std::vector<GeometryConfig> get_geometries() {
147159
return get_geometries({});

src/viam/sdk/components/private/arm_client.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,18 @@ Arm::KinematicsData ArmClient::get_kinematics(const ProtoStruct& extra) {
106106
});
107107
}
108108

109+
std::map<std::string, mesh> ArmClient::get_3d_models(const ProtoStruct& extra) {
110+
return make_client_helper(this, *stub_, &StubType::Get3DModels)
111+
.with(extra)
112+
.invoke([](auto& response) {
113+
std::map<std::string, mesh> models;
114+
for (const auto& entry : response.models()) {
115+
models.emplace(entry.first, from_proto(entry.second));
116+
}
117+
return models;
118+
});
119+
}
120+
109121
std::vector<GeometryConfig> ArmClient::get_geometries(const ProtoStruct& extra) {
110122
return make_client_helper(this, *stub_, &StubType::GetGeometries)
111123
.with(extra)

src/viam/sdk/components/private/arm_client.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ class ArmClient : public Arm {
3333
void stop(const ProtoStruct& extra) override;
3434
ProtoStruct do_command(const ProtoStruct& command) override;
3535
Arm::KinematicsData get_kinematics(const ProtoStruct& extra) override;
36+
std::map<std::string, mesh> get_3d_models(const ProtoStruct& extra) override;
3637
std::vector<GeometryConfig> get_geometries(const ProtoStruct& extra) override;
3738

3839
// Using declarations to introduce convenience overloads of interface which do not need to be
3940
// passed the ProtoStruct parameter.
41+
using Arm::get_3d_models;
4042
using Arm::get_end_position;
4143
using Arm::get_geometries;
4244
using Arm::get_joint_positions;

src/viam/sdk/components/private/arm_server.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,19 @@ ::grpc::Status ArmServer::GetKinematics(
135135
});
136136
}
137137

138+
::grpc::Status ArmServer::Get3DModels(::grpc::ServerContext*,
139+
const ::viam::common::v1::Get3DModelsRequest* request,
140+
::viam::common::v1::Get3DModelsResponse* response) noexcept {
141+
return make_service_helper<Arm>(
142+
"ArmServer::Get3DModels", this, request)([&](auto& helper, auto& arm) {
143+
const std::map<std::string, mesh> models = arm->get_3d_models(helper.getExtra());
144+
145+
for (const auto& entry : models) {
146+
response->mutable_models()->insert({entry.first, to_proto(entry.second)});
147+
}
148+
});
149+
}
150+
138151
::grpc::Status ArmServer::GetGeometries(
139152
::grpc::ServerContext*,
140153
const ::viam::common::v1::GetGeometriesRequest* request,

src/viam/sdk/components/private/arm_server.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ class ArmServer : public ResourceServer, public viam::component::arm::v1::ArmSer
6767
const ::viam::common::v1::GetKinematicsRequest* request,
6868
::viam::common::v1::GetKinematicsResponse* response) noexcept override;
6969

70+
::grpc::Status Get3DModels(::grpc::ServerContext* context,
71+
const ::viam::common::v1::Get3DModelsRequest* request,
72+
::viam::common::v1::Get3DModelsResponse* response) noexcept override;
73+
7074
::grpc::Status GetGeometries(
7175
::grpc::ServerContext* context,
7276
const ::viam::common::v1::GetGeometriesRequest* request,

src/viam/sdk/tests/mocks/mock_arm.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "viam/sdk/common/mesh.hpp"
12
#include <viam/sdk/tests/mocks/mock_arm.hpp>
23

34
#include "mock_arm.hpp"
@@ -7,6 +8,10 @@ namespace viam {
78
namespace sdktests {
89
namespace arm {
910

11+
std::map<std::string, sdk::mesh> fake_3d_models() {
12+
return {{"model1", sdk::mesh{"model1", {1, 2, 3, 4}}}};
13+
}
14+
1015
sdk::Arm::KinematicsData fake_kinematics() {
1116
return sdk::Arm::KinematicsDataSVA{{std::vector<unsigned char>{1, 2, 3, 4}}};
1217
}
@@ -59,6 +64,10 @@ std::vector<sdk::GeometryConfig> MockArm::get_geometries(const sdk::ProtoStruct&
5964
return fake_geometries();
6065
}
6166

67+
std::map<std::string, sdk::mesh> MockArm::get_3d_models(const sdk::ProtoStruct&) {
68+
return fake_3d_models();
69+
}
70+
6271
} // namespace arm
6372
} // namespace sdktests
6473
} // namespace viam

src/viam/sdk/tests/mocks/mock_arm.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace sdktests {
77
namespace arm {
88

99
sdk::Arm::KinematicsData fake_kinematics();
10+
std::map<std::string, sdk::mesh> fake_3d_models();
1011

1112
class MockArm : public sdk::Arm {
1213
public:
@@ -29,7 +30,7 @@ class MockArm : public sdk::Arm {
2930
sdk::ProtoStruct do_command(const sdk::ProtoStruct& command) override;
3031
sdk::Arm::KinematicsData get_kinematics(const sdk::ProtoStruct&) override;
3132
std::vector<sdk::GeometryConfig> get_geometries(const sdk::ProtoStruct&) override;
32-
33+
std::map<std::string, sdk::mesh> get_3d_models(const sdk::ProtoStruct&) override;
3334
sdk::pose current_location;
3435
std::vector<double> joint_positions;
3536
std::vector<std::vector<double>> move_thru_positions;

0 commit comments

Comments
 (0)