Skip to content

Commit 148572a

Browse files
committed
Init
1 parent 177911c commit 148572a

File tree

8 files changed

+92
-13
lines changed

8 files changed

+92
-13
lines changed

BUILDING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ that they are pre-populated.
441441
cmake -DVIAMCPPSDK_USE_DYNAMIC_PROTOS=ON ...
442442
ninja all # FAILS!
443443
ninja generate-dynamic-protos
444-
nina all # OK!
444+
ninja all # OK!
445445
```
446446

447447
### Build Issue: Proto Mismatch

src/viam/sdk/components/camera.hpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,26 @@ class Camera : public Component {
160160
/// @brief Get the next images from the camera as a vector of raw images with names and
161161
/// metadata.
162162
/// @return a vector of raw_images and associated response metadata.
163-
virtual image_collection get_images() = 0;
163+
inline image_collection get_images() {
164+
return get_images({}, {});
165+
}
166+
167+
/// @brief Get the next images from specific sources as a vector of raw images with names and
168+
/// metadata.
169+
/// @param filter_source_names the names of sources to receive images from. If empty, all
170+
/// sources are returned.
171+
/// @return a vector of raw_images and associated response metadata.
172+
inline image_collection get_images(std::vector<std::string> filter_source_names) {
173+
return get_images(std::move(filter_source_names), {});
174+
}
175+
176+
/// @brief Get the next images from the camera as a vector of raw images with names and
177+
/// metadata.
178+
/// @param filter_source_names the names of sources to receive images from. If empty, all
179+
/// sources are returned.
180+
/// @param extra any additional arguments to the method.
181+
/// @return a vector of raw_images and associated response metadata.
182+
virtual image_collection get_images(std::vector<std::string> filter_source_names, const ProtoStruct& extra) = 0;
164183

165184
/// @brief Get the next `point_cloud` from the camera.
166185
/// @param mime_type the desired mime_type of the point_cloud (does not guarantee output type).

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ Camera::image_collection from_proto(const viam::component::camera::v1::GetImages
5454
std::string img_string = img.image();
5555
const std::vector<unsigned char> bytes(img_string.begin(), img_string.end());
5656
raw_image.bytes = bytes;
57-
raw_image.mime_type = format_to_MIME_string(img.format());
57+
if (!img.mime_type().empty()) {
58+
raw_image.mime_type = img.mime_type();
59+
} else {
60+
raw_image.mime_type = format_to_MIME_string(img.format());
61+
}
5862
raw_image.source_name = img.source_name();
5963
images.push_back(raw_image);
6064
}
@@ -122,8 +126,12 @@ Camera::raw_image CameraClient::get_image(std::string mime_type, const ProtoStru
122126
.invoke([](auto& response) { return from_proto(response); });
123127
};
124128

125-
Camera::image_collection CameraClient::get_images() {
126-
return make_client_helper(this, *stub_, &StubType::GetImages).invoke([](auto& response) {
129+
Camera::image_collection CameraClient::get_images(std::vector<std::string> filter_source_names, const ProtoStruct& extra) {
130+
return make_client_helper(this, *stub_, &StubType::GetImages).with(extra, [&](auto& request) {
131+
for (const auto& source_name : filter_source_names) {
132+
*request.add_filter_source_names() = source_name;
133+
}
134+
}).invoke([](auto& response) {
127135
return from_proto(response);
128136
});
129137
};

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class CameraClient : public Camera {
2626
CameraClient(std::string name, std::shared_ptr<grpc::Channel> channel);
2727
ProtoStruct do_command(const ProtoStruct& command) override;
2828
raw_image get_image(std::string mime_type, const ProtoStruct& extra) override;
29-
image_collection get_images() override;
29+
image_collection get_images(std::vector<std::string> filter_source_names, const ProtoStruct& extra) override;
3030
point_cloud get_point_cloud(std::string mime_type, const ProtoStruct& extra) override;
3131
properties get_properties() override;
3232
std::vector<GeometryConfig> get_geometries(const ProtoStruct& extra) override;
@@ -42,6 +42,7 @@ class CameraClient : public Camera {
4242
// we need to include these `using` lines.
4343
using Camera::get_geometries;
4444
using Camera::get_image;
45+
using Camera::get_images;
4546
using Camera::get_point_cloud;
4647

4748
protected:

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,14 @@ ::grpc::Status CameraServer::GetImages(
8383
const ::viam::component::camera::v1::GetImagesRequest* request,
8484
::viam::component::camera::v1::GetImagesResponse* response) noexcept {
8585
return make_service_helper<Camera>(
86-
"CameraServer::GetImages", this, request)([&](auto&, auto& camera) {
87-
const Camera::image_collection image_coll = camera->get_images();
86+
"CameraServer::GetImages", this, request)([&](auto& helper, auto& camera) {
87+
const Camera::image_collection image_coll =
88+
camera->get_images({request->filter_source_names().begin(), request->filter_source_names().end()}, helper.getExtra());
8889
for (const auto& img : image_coll.images) {
8990
::viam::component::camera::v1::Image proto_image;
9091
const std::string img_string = bytes_to_string(img.bytes);
9192
proto_image.set_source_name(img.source_name);
93+
proto_image.set_mime_type(img.mime_type);
9294
proto_image.set_format(
9395
MIME_string_to_format(Camera::normalize_mime_type(img.mime_type)));
9496
proto_image.set_image(img_string);

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <viam/sdk/tests/mocks/camera_mocks.hpp>
2+
#include <algorithm>
23

34
#include <viam/sdk/common/proto_value.hpp>
45
#include <viam/sdk/components/camera.hpp>
@@ -16,8 +17,20 @@ ProtoStruct MockCamera::do_command(const ProtoStruct&) {
1617
Camera::raw_image MockCamera::get_image(std::string, const ProtoStruct&) {
1718
return image_;
1819
}
19-
Camera::image_collection MockCamera::get_images() {
20-
return images_;
20+
Camera::image_collection MockCamera::get_images(std::vector<std::string> filter_source_names, const ProtoStruct& extra) {
21+
last_filter_source_names_ = std::move(filter_source_names);
22+
last_extra_ = extra;
23+
if (last_filter_source_names_.empty()) {
24+
return images_;
25+
}
26+
Camera::image_collection filtered = images_;
27+
filtered.images.clear();
28+
for (const auto& img : images_.images) {
29+
if (std::find(last_filter_source_names_.begin(), last_filter_source_names_.end(), img.source_name) != last_filter_source_names_.end()) {
30+
filtered.images.push_back(img);
31+
}
32+
}
33+
return filtered;
2134
}
2235
Camera::point_cloud MockCamera::get_point_cloud(std::string, const ProtoStruct&) {
2336
return pc_;
@@ -43,13 +56,13 @@ Camera::image_collection fake_raw_images() {
4356
std::vector<Camera::raw_image> images;
4457
Camera::raw_image image1;
4558
image1.mime_type = "image/jpeg";
46-
image1.source_name = "color_sensor";
59+
image1.source_name = "color";
4760
std::vector<unsigned char> bytes1 = {'a', 'b', 'c'};
4861
image1.bytes = bytes1;
4962
images.push_back(image1);
5063
Camera::raw_image image2;
5164
image2.mime_type = "image/vnd.viam.dep";
52-
image2.source_name = "depth_sensor";
65+
image2.source_name = "depth";
5366
std::vector<unsigned char> bytes2 = {'d', 'e', 'f'};
5467
image2.bytes = bytes2;
5568
images.push_back(image2);

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@ class MockCamera : public Camera {
1212
public:
1313
ProtoStruct do_command(const ProtoStruct& command) override;
1414
raw_image get_image(std::string mime_type, const sdk::ProtoStruct& extra) override;
15-
image_collection get_images() override;
15+
image_collection get_images(std::vector<std::string> filter_source_names, const sdk::ProtoStruct& extra) override;
1616
point_cloud get_point_cloud(std::string mime_type, const sdk::ProtoStruct& extra) override;
1717
std::vector<GeometryConfig> get_geometries(const sdk::ProtoStruct& extra) override;
1818
properties get_properties() override;
1919
static std::shared_ptr<MockCamera> get_mock_camera();
2020
MockCamera(std::string name) : Camera(std::move(name)) {}
2121

22+
const std::vector<std::string>& last_filter_source_names() const { return last_filter_source_names_; }
23+
const ProtoStruct& last_extra() const { return last_extra_; }
24+
2225
private:
2326
Camera::intrinsic_parameters intrinsic_parameters_;
2427
Camera::distortion_parameters distortion_parameters_;
@@ -28,6 +31,8 @@ class MockCamera : public Camera {
2831
Camera::point_cloud pc_;
2932
ProtoStruct map_;
3033
std::vector<GeometryConfig> geometries_;
34+
std::vector<std::string> last_filter_source_names_;
35+
ProtoStruct last_extra_;
3136
};
3237

3338
Camera::raw_image fake_raw_image();

src/viam/sdk/tests/test_camera.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,37 @@ BOOST_AUTO_TEST_CASE(test_get_images) {
5151
});
5252
}
5353

54+
BOOST_AUTO_TEST_CASE(test_get_images_filtering) {
55+
std::shared_ptr<MockCamera> mock = MockCamera::get_mock_camera();
56+
client_to_mock_pipeline<Camera>(mock, [&](Camera& client) {
57+
// request only color
58+
Camera::image_collection images = client.get_images({"color"});
59+
BOOST_CHECK_EQUAL(images.images.size(), 1);
60+
BOOST_CHECK_EQUAL(images.images[0].source_name, "color");
61+
62+
// empty filter returns all
63+
Camera::image_collection all_images = client.get_images({});
64+
BOOST_CHECK_EQUAL(all_images.images.size(), 2);
65+
66+
// verify filter propagated to mock
67+
auto last_filter = mock->last_filter_source_names();
68+
BOOST_CHECK_EQUAL(last_filter.size(), 0);
69+
});
70+
}
71+
72+
BOOST_AUTO_TEST_CASE(test_get_images_with_extra) {
73+
std::shared_ptr<MockCamera> mock = MockCamera::get_mock_camera();
74+
client_to_mock_pipeline<Camera>(mock, [&](Camera& client) {
75+
ProtoStruct extra;
76+
extra["foo"] = ProtoValue("bar");
77+
auto images = client.get_images({}, extra);
78+
(void)images; // unused variable in test body
79+
const auto& last_extra = mock->last_extra();
80+
BOOST_CHECK(last_extra.at("foo").is_a<std::string>());
81+
BOOST_CHECK_EQUAL(last_extra.at("foo").get_unchecked<std::string>(), "bar");
82+
});
83+
}
84+
5485
BOOST_AUTO_TEST_CASE(test_get_point_cloud) {
5586
std::shared_ptr<MockCamera> mock = MockCamera::get_mock_camera();
5687
client_to_mock_pipeline<Camera>(mock, [](Camera& client) {

0 commit comments

Comments
 (0)