Enpose API — C++
Enpose 6-DoF tracking API
Loading...
Searching...
No Matches
enpose_api.hpp
1
17#pragma once
18
19#include "enpose_api.h"
20
21#include <cstddef>
22#include <cstdint>
23#include <stdexcept>
24#include <string>
25#include <utility>
26#include <vector>
27
28namespace enpose {
29
36using MarkerPose = ::EnposeMarkerPose;
37
39class Error : public std::runtime_error {
40public:
41 explicit Error(const std::string& message) : std::runtime_error(message) {}
42};
43
45struct DeviceInfo {
46 std::string ip;
47 std::uint32_t serial;
49};
50
55inline std::vector<DeviceInfo> discover() {
56 EnposeDeviceInfo* devices = nullptr;
57 std::size_t count = 0;
58 if (enpose_discover(&devices, &count) != ENPOSE_OK) {
59 throw Error("enpose_discover failed");
60 }
61 std::vector<DeviceInfo> result;
62 result.reserve(count);
63 for (std::size_t i = 0; i < count; ++i) {
64 result.push_back(DeviceInfo{std::string(devices[i].ip), devices[i].serial,
65 devices[i].compatible});
66 }
67 enpose_device_info_array_free(devices, count);
68 return result;
69}
70
75public:
80 explicit PoseStream(const std::string& ip, bool create_thread = true)
81 : handle_(enpose_pose_stream_connect(ip.c_str(), create_thread)) {
82 if (handle_ == nullptr) {
83 throw Error("failed to connect pose stream to " + ip);
84 }
85 }
86
88 explicit PoseStream(const DeviceInfo& device, bool create_thread = true)
89 : PoseStream(device.ip, create_thread) {}
90
92 if (handle_ != nullptr) {
93 enpose_pose_stream_free(handle_);
94 }
95 }
96
97 PoseStream(const PoseStream&) = delete;
98 PoseStream& operator=(const PoseStream&) = delete;
99
100 PoseStream(PoseStream&& other) noexcept
101 : handle_(std::exchange(other.handle_, nullptr)) {}
102
103 PoseStream& operator=(PoseStream&& other) noexcept {
104 if (this != &other) {
105 if (handle_ != nullptr) {
106 enpose_pose_stream_free(handle_);
107 }
108 handle_ = std::exchange(other.handle_, nullptr);
109 }
110 return *this;
111 }
112
120 std::vector<MarkerPose> receive(bool block = false) {
121 EnposeMarkerPose* poses = nullptr;
122 std::size_t count = 0;
123 if (enpose_pose_stream_receive(handle_, block, &poses, &count) != ENPOSE_OK) {
124 throw Error("enpose_pose_stream_receive failed");
125 }
126 std::vector<MarkerPose> result(poses, poses + count);
127 enpose_marker_pose_array_free(poses, count);
128 return result;
129 }
130
131private:
132 EnposePoseStream* handle_;
133};
134
135} // namespace enpose
Exception thrown by the wrapper when a C call fails.
Definition enpose_api.hpp:39
Error(const std::string &message)
Definition enpose_api.hpp:41
RAII handle to a live pose stream from one device.
Definition enpose_api.hpp:74
PoseStream(const DeviceInfo &device, bool create_thread=true)
Connect to a discovered device.
Definition enpose_api.hpp:88
PoseStream(const PoseStream &)=delete
PoseStream & operator=(const PoseStream &)=delete
PoseStream(const std::string &ip, bool create_thread=true)
Connect to the device at ip (an IPv4 string; the Enpose API is IPv4-only).
Definition enpose_api.hpp:80
PoseStream(PoseStream &&other) noexcept
Definition enpose_api.hpp:100
std::vector< MarkerPose > receive(bool block=false)
Return poses received from the stream.
Definition enpose_api.hpp:120
~PoseStream()
Definition enpose_api.hpp:91
PoseStream & operator=(PoseStream &&other) noexcept
Definition enpose_api.hpp:103
Definition enpose_api.hpp:28
std::vector< DeviceInfo > discover()
Discover Enpose devices on the local network.
Definition enpose_api.hpp:55
::EnposeMarkerPose MarkerPose
Pose of one tracked marker in world coordinates.
Definition enpose_api.hpp:36
One device discovered on the local network.
Definition enpose_api.hpp:45
std::uint32_t serial
Factory serial number.
Definition enpose_api.hpp:47
std::string ip
Device IPv4 address.
Definition enpose_api.hpp:46
bool compatible
True if the device's protocol version matches.
Definition enpose_api.hpp:48