Banuba SDK
Loading...
Searching...
No Matches
base_types.hpp
1#pragma once
2
3#if BNB_SDK_PROJECT
4 #include <bnb/utils/defs.hpp>
5 #include <bnb/types/config.hpp>
6 #include <bnb/utils/assert.hpp>
7 #include <bnb/utils/logger.hpp>
8#endif
9
10#include <vector>
11#include <string>
12#include <chrono>
13#include <cstdint>
14#include <cstring>
15#include <memory>
16#include <functional>
17
18namespace bnb
19{
20 /**
21 * @addtogroup Types
22 * @{
23 */
24
25 /// camera image layout is top-left, 0 orientation is portrait, rotation is counterclockwise
26 enum class camera_orientation : unsigned int
27 {
28 deg_0 = 0,
29 deg_90 = 1,
30 deg_180 = 2,
31 deg_270 = 3,
32 up = deg_0,
33 left = deg_90,
34 down = deg_180,
35 right = deg_270,
36 };
37 using high_res_timer = std::chrono::high_resolution_clock;
38 using time_stamp_t = std::chrono::time_point<std::chrono::high_resolution_clock>;
39
40 using orientation = camera_orientation;
41
42 union point2d
43 {
44 struct
45 {
46 float x, y;
47 };
48 float data[2];
49
50 float sum()
51 {
52 return x + y;
53 }
54
55 point2d sqr() const noexcept
56 {
57 return *this * *this;
58 }
59
60 point2d operator/(const point2d& p) const noexcept
61 {
62 return point2d{x / p.x, y / p.y};
63 }
64
65 point2d operator/(float f) const noexcept
66 {
67 return point2d{x / f, y / f};
68 }
69
70 point2d operator*(float a) const noexcept
71 {
72 return point2d{x * a, y * a};
73 }
74
75 point2d operator+(float a) const noexcept
76 {
77 return point2d{x + a, y + a};
78 }
79
80 point2d operator*(const point2d& p) const noexcept
81 {
82 return point2d{x * p.x, y * p.y};
83 }
84
85 point2d operator-(const point2d& p) const noexcept
86 {
87 return point2d{x - p.x, y - p.y};
88 }
89
90 point2d operator+(const point2d& p) const noexcept
91 {
92 return point2d{x + p.x, y + p.y};
93 }
94 };
95
96 union point3d
97 {
98 struct
99 {
100 float x, y, z;
101 };
102 float data[3];
103
104 void clear()
105 {
106 x = y = z = 0.0f;
107 }
108
109 float sum()
110 {
111 return x + y + z;
112 }
113
114 point3d sqr() const noexcept
115 {
116 return *this * *this;
117 }
118
119 point3d operator/(const point3d& p) const noexcept
120 {
121 return point3d{x / p.x, y / p.y, z / p.z};
122 }
123
124 point3d operator/(float f) const noexcept
125 {
126 return point3d{x / f, y / f, z / f};
127 }
128
129 point3d operator*(const point3d& p) const noexcept
130 {
131 return point3d{x * p.x, y * p.y, z * p.z};
132 }
133
134 point3d operator*(float a) const noexcept
135 {
136 return point3d{x * a, y * a, z * a};
137 }
138
139 point3d operator-(const point3d& p) const noexcept
140 {
141 return point3d{x - p.x, y - p.y, z - p.z};
142 }
143
144 point3d operator+(const point3d& p) const noexcept
145 {
146 return point3d{x + p.x, y + p.y, z + p.z};
147 }
148 };
149
150 struct size
151 {
152 uint32_t width;
153 uint32_t height;
154 };
155
156 struct data_t
157 {
158 using type = uint8_t[];
159 using pointer = uint8_t*;
160 using uptr = std::unique_ptr<type, std::function<void(pointer)>>;
161 uptr data;
162 size_t size;
163
164 static data_t create_non_owning(std::shared_ptr<uint8_t> datum, size_t size)
165 {
166 return data_t{
167 uptr(datum.get(), [](pointer) { /* don't release */ }),
168 size};
169 }
170
171 static data_t create_non_owning(uint8_t* datum, size_t size)
172 {
173 return data_t{
174 uptr(datum, [](pointer) {}),
175 size};
176 }
177
178 void reserve(size_t reserve_size)
179 {
180 if (size < reserve_size) {
181 data = std::make_unique<type>(reserve_size);
182 size = reserve_size;
183 }
184 }
185 data_t() = default;
186
187 data_t(uptr d, size_t s)
188 : data(std::move(d))
189 , size(s)
190 {
191 }
192
193 data_t(data_t&&) = default;
194 data_t& operator=(data_t&&) = default;
195
196#if BNB_PYBIND_BUILD || BNB_OS_EMSCRIPTEN
197 // WARNING: it is NOT deep copy constructor, it provides weak (non-owning) copy
198 data_t(const data_t& other)
199 : data(other.data.get(), [](pointer) { /* DO NOTHING */ })
200 , size(other.size)
201 {
202 BNB_ASSERT_MSG(false, "bnb::data_t copy constructor!");
203 bnb::logger::print(bnb::severity_level::warning, "bnb::data_t", "copy constructor called, possible memory issue!");
204 }
205
206 // WARNING: it is NOT deep copy operator, it provides weak (non-owning) copy
207 data_t& operator=(const data_t& other)
208 {
209 BNB_ASSERT_MSG(false, "bnb::data_t copy operator!");
210 bnb::logger::print(bnb::severity_level::warning, "bnb::data_t", "copy operator called, possible memory issue!");
211 data = uptr(other.data.get(), [](pointer) { /* DO NOTHING */ });
212 size = other.size;
213 return *this;
214 }
215#endif
216 };
217
218 using color_plane_data_t = std::uint8_t;
219 using color_plane = std::shared_ptr<color_plane_data_t>;
220
221 inline color_plane color_plane_vector(std::vector<color_plane_data_t> vector)
222 {
223 // please, don't do stuff like ->> again.. //return color_plane(vector.data(), [v =
224 // std::move(vector)](color_plane_data_t*) {}) are you REALLY sure no one is going to copy
225 // this deleter internally? (like shared_ptr does)
226 auto* ptr = new std::vector<color_plane_data_t>(std::move(vector));
227 return color_plane(ptr->data(), [ptr](color_plane_data_t*) { delete ptr; });
228 }
229
230 inline color_plane color_plane_string(std::string str)
231 {
232 auto* ptr = new std::string(std::move(str));
233 return color_plane(reinterpret_cast<color_plane_data_t*>(ptr->data()), [ptr](color_plane_data_t*) { delete ptr; });
234 }
235
236 inline color_plane color_plane_char_arr(const char* ptr, size_t size)
237 {
238 std::vector<color_plane_data_t> plane(size);
239 memcpy(plane.data(), reinterpret_cast<const color_plane_data_t*>(ptr), size);
240 return color_plane_vector(plane);
241 }
242
243 inline color_plane color_plane_weak(const color_plane_data_t* ptr)
244 {
245 return color_plane(const_cast<color_plane_data_t*>(ptr), [](color_plane_data_t*) { /* DO NOTHING */ });
246 }
247
248
249 inline color_plane color_plane_data(data_t&& data)
250 {
251 auto* ptr = new data_t(std::move(data));
252 return color_plane(ptr->data.get(), [ptr](color_plane_data_t*) { delete ptr; });
253 }
254
255 /** @} */ // endgroup types
256} // namespace bnb
camera_orientation
camera image layout is top-left, 0 orientation is portrait, rotation is counterclockwise