ADTF Device Toolbox SDK
Loading...
Searching...
No Matches
pointcloud_access.h
Go to the documentation of this file.
1
16
17#pragma once
18
19#include "pointcloud_half_float.h"
20#include "pointcloud_types.h"
21
22#include <algorithm>
23#include <cmath>
24#include <cstddef>
25#include <limits>
26#include <memory>
27#include <stdexcept>
28#include <variant>
29
33namespace adtf
34{
38namespace devicetb
39{
43namespace sdk
44{
48namespace pointcloud
49{
50
51namespace navigation
52{
53
54struct float16_t
55{
56 uint16_t bytes;
57
58 float16_t() = default;
59 float16_t(std::nullptr_t, uint16_t bytes): bytes(bytes)
60 {
61 }
62 float16_t(const float16_t& value) = default;
63 float16_t(double value): bytes(half_float::encode_flt16(value))
64 {
65 }
66
67 float16_t& operator=(const float16_t& value) = default;
68 float16_t& operator=(double value)
69 {
70 bytes = half_float::encode_flt16(value);
71 return *this;
72 }
73
74 operator double() const
75 {
76 return half_float::decode_flt16<double>(bytes);
77 }
78};
79
80namespace detail
81{
82
83inline constexpr tAttribute::tInMemory::eEndianess get_byteorder()
84{
85#ifdef _WIN32
86 return tAttribute::tInMemory::eEndianess::endian_little;
87#else
88 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
89 return tAttribute::tInMemory::eEndianess::endian_little;
90 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
91 return tAttribute::tInMemory::eEndianess::endian_big;
92 #else
93 static_assert(false, "Unsupported byte order");
94 #endif
95#endif
96}
97
98template <typename T>
99T byte_swap(T value)
100{
101 if constexpr (sizeof(T) == 1)
102 {
103 return value;
104 }
105 else if constexpr (sizeof(T) == 2)
106 {
107 #if defined(__llvm__) || defined(__GNUC__)
108 return __builtin_bswap16(value);
109 #elif defined(_MSC_VER)
110 return _byteswap_ushort(value);
111 #else
112 static_assert(false, "Missing intrinsic for this compiler?");
113 #endif
114 }
115 else if constexpr (sizeof(T) == 4)
116 {
117 #if defined(__llvm__) || defined(__GNUC__)
118 return __builtin_bswap32(value);
119 #elif defined(_MSC_VER)
120 return _byteswap_ulong(value);
121 #else
122 static_assert(false, "Missing intrinsic for this compiler?");
123 #endif
124 }
125 else if constexpr (sizeof(T) == 8)
126 {
127 #if defined(__llvm__) || defined(__GNUC__)
128 return __builtin_bswap64(value);
129 #elif defined(_MSC_VER)
130 return _byteswap_uint64(value);
131 #else
132 static_assert(false, "Missing intrinsic for this compiler?");
133 #endif
134 }
135}
136
137template <>
138inline float16_t byte_swap<float16_t>(float16_t value)
139{
140 return half_float::detail::bit_cast<float16_t>(byte_swap(half_float::detail::bit_cast<uint16_t>(value)));
141}
142
143template <>
144inline float byte_swap<float>(float value)
145{
146 return half_float::detail::bit_cast<float>(byte_swap(half_float::detail::bit_cast<uint32_t>(value)));
147}
148
149template <>
150inline double byte_swap<double>(double value)
151{
152 return half_float::detail::bit_cast<double>(byte_swap(half_float::detail::bit_cast<uint64_t>(value)));
153}
154
155template <typename T, bool swap>
157{
158 static T access(const std::byte* pReadPosition)
159 {
160 if constexpr(swap)
161 {
162 return byte_swap(*(reinterpret_cast<const T*>(pReadPosition)));
163 }
164 else
165 {
166 return *(reinterpret_cast<const T*>(pReadPosition));
167 }
168 }
169};
170
171template <bool swap>
172struct Accessor<float16_t, swap>
173{
174 static double access(const std::byte* pReadPosition)
175 {
176 return float16_t(nullptr, Accessor<uint16_t, swap>::access(pReadPosition));
177 }
178};
179
180template <typename T, bool swap>
181T access(const std::byte* pReadPosition)
182{
183 return Accessor<T, swap>::access(pReadPosition);
184}
185
186}
187
188template <typename T>
189T get_attribute_value(const tAttribute::tInMemory& oSource, const detail::tConstBuffer& sBuffer, size_t nIndex)
190{
191 const auto pReadPosition = static_cast<const std::byte*>(sBuffer.pData) + oSource.offset + oSource.stride * nIndex;
192 if (oSource.endianess == detail::get_byteorder())
193 {
194 return detail::access<T, false>(pReadPosition);
195 }
196 else
197 {
198 return detail::access<T, true>(pReadPosition);
199 }
200}
201
202template <typename T>
203void set_attribute_value(const tAttribute::tInMemory& oDestination, const detail::tBuffer& sBuffer, size_t nIndex, T value)
204{
205 const auto pWritePosition = reinterpret_cast<T*>(static_cast<std::byte*>(sBuffer.pData) + oDestination.offset + oDestination.stride * nIndex);
206 if (oDestination.endianess == detail::get_byteorder())
207 {
208 *pWritePosition = value;
209 }
210 else
211 {
212 *pWritePosition = detail::byte_swap(value);
213 }
214}
215
216using AttributeValue = std::variant<bool,
217 uint8_t,
218 int8_t,
219 uint16_t,
220 int16_t,
221 uint32_t,
222 int32_t,
223 uint64_t,
224 int64_t,
225 float16_t,
226 float,
227 double>;
228
229inline AttributeValue get_attribute_value(const tAttribute& oSource, const detail::tConstBuffer& sBuffer, size_t nIndex)
230{
231 switch (oSource.type)
232 {
233 case tAttribute::tDataType::dt_boolean: return get_attribute_value<bool>(oSource.sInMemory, sBuffer, nIndex);
234 case tAttribute::tDataType::dt_int8: return get_attribute_value<int8_t>(oSource.sInMemory, sBuffer, nIndex);
235 case tAttribute::tDataType::dt_uint8: return get_attribute_value<uint8_t>(oSource.sInMemory, sBuffer, nIndex);
236 case tAttribute::tDataType::dt_int16: return get_attribute_value<int16_t>(oSource.sInMemory, sBuffer, nIndex);
237 case tAttribute::tDataType::dt_uint16: return get_attribute_value<uint16_t>(oSource.sInMemory, sBuffer, nIndex);
238 case tAttribute::tDataType::dt_int32: return get_attribute_value<int32_t>(oSource.sInMemory, sBuffer, nIndex);
239 case tAttribute::tDataType::dt_uint32: return get_attribute_value<uint32_t>(oSource.sInMemory, sBuffer, nIndex);
240 case tAttribute::tDataType::dt_int64: return get_attribute_value<int64_t>(oSource.sInMemory, sBuffer, nIndex);
241 case tAttribute::tDataType::dt_uint64: return get_attribute_value<uint64_t>(oSource.sInMemory, sBuffer, nIndex);
242 case tAttribute::tDataType::dt_float16: return get_attribute_value<float16_t>(oSource.sInMemory, sBuffer, nIndex);
243 case tAttribute::tDataType::dt_float32: return get_attribute_value<float>(oSource.sInMemory, sBuffer, nIndex);
244 case tAttribute::tDataType::dt_float64: return get_attribute_value<double>(oSource.sInMemory, sBuffer, nIndex);
245 case tAttribute::tDataType::dt_computed_from_index: return oSource.sComputed.Get(nIndex);
246 }
247
248 throw std::runtime_error("unexpected");
249}
250
251inline void set_attribute_value(const tAttribute& oDestination, const detail::tBuffer& sBuffer, size_t nIndex, AttributeValue value)
252{
253 std::visit([&](const auto& typed_value)
254 {
255 switch (oDestination.type)
256 {
257 case tAttribute::tDataType::dt_boolean: set_attribute_value<bool>(oDestination.sInMemory, sBuffer, nIndex, static_cast<bool>(typed_value)); break;
258 case tAttribute::tDataType::dt_int8:
259 set_attribute_value<int8_t>(oDestination.sInMemory, sBuffer, nIndex, static_cast<int8_t>(typed_value));
260 break;
261 case tAttribute::tDataType::dt_uint8:
262 set_attribute_value<uint8_t>(oDestination.sInMemory, sBuffer, nIndex, static_cast<uint8_t>( typed_value));
263 break;
264 case tAttribute::tDataType::dt_int16:
265 set_attribute_value<int16_t>(oDestination.sInMemory, sBuffer, nIndex, static_cast<int16_t>( typed_value));
266 break;
267 case tAttribute::tDataType::dt_uint16:
268 set_attribute_value<uint16_t>(oDestination.sInMemory, sBuffer, nIndex, static_cast<uint16_t>(typed_value));
269 break;
270 case tAttribute::tDataType::dt_int32:
271 set_attribute_value<int32_t>(oDestination.sInMemory, sBuffer, nIndex, static_cast<int32_t>(typed_value));
272 break;
273 case tAttribute::tDataType::dt_uint32:
274 set_attribute_value<uint32_t>(oDestination.sInMemory, sBuffer, nIndex, static_cast<uint32_t>(typed_value));
275 break;
276 case tAttribute::tDataType::dt_int64:
277 set_attribute_value<int64_t>(oDestination.sInMemory, sBuffer, nIndex, static_cast<int64_t>(typed_value));
278 break;
279 case tAttribute::tDataType::dt_uint64:
280 set_attribute_value<uint64_t>(oDestination.sInMemory, sBuffer, nIndex, static_cast<uint64_t>(typed_value));
281 break;
282 case tAttribute::tDataType::dt_float16:
283 set_attribute_value<float16_t>(oDestination.sInMemory, sBuffer, nIndex, float16_t(static_cast<double>(typed_value)));
284 break;
285 case tAttribute::tDataType::dt_float32:
286 set_attribute_value<float>(oDestination.sInMemory, sBuffer, nIndex, static_cast<float>(typed_value));
287 break;
288 case tAttribute::tDataType::dt_float64:
289 set_attribute_value<double>(oDestination.sInMemory, sBuffer, nIndex, static_cast<double>(typed_value));
290 break;
291 case tAttribute::tDataType::dt_computed_from_index: throw std::runtime_error("cannot write to computed attribute");
292 }
293 }, value);
294}
295
296namespace detail
297{
298
299template <typename T>
300inline T to_type(AttributeValue value)
301{
302 if constexpr (std::is_same_v<AttributeValue, T>)
303 {
304 return value;
305 }
306 else
307 {
308 return std::visit([](const auto& typed_value) -> T
309 {
310 return static_cast<T>(typed_value);
311 }, value);
312 }
313}
314
315}
316
317} // namespace navigation
318
319using navigation::get_attribute_value;
320using navigation::set_attribute_value;
321using navigation::AttributeValue;
322
323} // namespace pointcloud
324} // namespace sdk
325} // namespace devicetb
326} // namespace adtf
Namespace for functionality provided by 3.13.0.
Definition pointcloud_access.h:52
Namespace for pointcloud in ADTF-Devicetoolbox.
Definition pointcloud_access.h:49
Namespace for SDK of ADTF-Devicetoolbox.
Definition bus_database_intf.h:42
Devicetoolbox - Namespace.
Definition bus_database_intf.h:36
ADTF - Namespace.
Definition bus_database_intf.h:30