ifm3d
organizer_utils.h
1 /*
2  * Copyright 2022-present ifm electronic, gmbh
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #ifndef IFM3D_FG_ORGANIZER_UTILS_H
7 #define IFM3D_FG_ORGANIZER_UTILS_H
8 
9 #include <ifm3d/device/device.h>
10 #include <ifm3d/fg/buffer.h>
11 #include <ifm3d/fg/distance_image_info.h>
12 #include <ifm3d/fg/frame.h>
13 #include <map>
14 #include <optional>
15 #include <set>
16 #include <tuple>
17 #include <vector>
18 
19 namespace ifm3d
20 {
21  constexpr std::size_t IMG_BUFF_START = 8;
22 
23  std::map<ImageChunk, std::set<std::size_t>> get_image_chunks(
24  const std::vector<std::uint8_t>& data,
25  std::size_t start_idx,
26  std::optional<size_t> end_idx = std::nullopt);
27 
28  std::size_t get_format_size(PixelFormat fmt);
29  std::size_t get_format_channels(PixelFormat fmt);
30 
31  Buffer create_1d_buffer(const std::vector<std::uint8_t>& data,
32  std::size_t idx);
33 
34  Buffer create_buffer(const std::vector<std::uint8_t>& data,
35  std::size_t idx,
36  std::size_t width,
37  std::size_t height);
38 
39  Buffer create_buffer(const std::vector<std::uint8_t>& data,
40  std::size_t idx,
41  std::size_t width,
42  std::size_t height,
43  PixelFormat fmt,
44  const std::optional<json>& metadata = std::nullopt);
45 
46  Buffer create_xyz_buffer(const std::vector<std::uint8_t>& data,
47  std::size_t xidx,
48  std::size_t yidx,
49  std::size_t zidx,
50  std::size_t width,
51  std::size_t height,
52  PixelFormat fmt,
53  const std::optional<Buffer>& mask);
54 
55  auto find_metadata_chunk(
56  const std::map<ImageChunk, std::set<std::size_t>>& chunks)
57  -> decltype(chunks.end());
58 
59  std::tuple<uint32_t, uint32_t> get_image_size(
60  const std::vector<std::uint8_t>& data,
61  std::size_t idx);
62 
63  PixelFormat get_chunk_format(const std::vector<std::uint8_t>& data,
64  std::size_t idx);
65 
66  uint32_t get_chunk_frame_count(const std::vector<std::uint8_t>& data,
67  std::size_t idx);
68 
69  std::vector<TimePointT> get_chunk_timestamps(
70  const std::vector<uint8_t>& data,
71  std::size_t idx);
72 
73  std::size_t get_chunk_pixeldata_offset(const std::vector<std::uint8_t>& data,
74  std::size_t idx);
75 
76  std::size_t get_chunk_size(const std::vector<std::uint8_t>& data,
77  std::size_t idx);
78 
79  std::size_t get_chunk_pixeldata_size(const std::vector<std::uint8_t>& data,
80  std::size_t idx);
81 
82  std::size_t get_chunk_header_version(const std::vector<std::uint8_t>& data,
83  std::size_t idx);
84 
85  void mask_buffer(Buffer& image, const Buffer& mask);
86 
87  bool is_probably_blob(const std::vector<std::uint8_t>& data,
88  std::size_t idx,
89  std::size_t width,
90  std::size_t height);
91 
92  Buffer create_pixel_mask(Buffer& confidence);
93 
94  void parse_data(
95  const std::vector<uint8_t>& data,
96  const std::set<buffer_id>& requested_images,
97  const std::map<ifm3d::ImageChunk, std::set<std::size_t>>& chunks,
98  size_t width,
99  size_t height,
100  std::map<buffer_id, BufferList>& data_blob,
101  std::map<buffer_id, BufferList>& data_image);
102 
103  void mask_images(
104  std::map<ifm3d::buffer_id, ifm3d::BufferList>& images,
105  ifm3d::Buffer& mask,
106  const std::function<bool(ifm3d::buffer_id id)>& should_mask);
107 
108  bool has_metadata(const std::vector<std::uint8_t>& data, std::size_t idx);
109 
110  ifm3d::json create_metadata(const std::vector<std::uint8_t>& data,
111  std::size_t idx);
112  buffer_id map_metadata_to_buffer_id(const ifm3d::Buffer& buffer);
113 
114  std::map<ifm3d::buffer_id, ifm3d::BufferList> map_logical_buffers(
115  const std::map<ifm3d::buffer_id, ifm3d::BufferList>& images,
116  const std::set<ifm3d::buffer_id>& requested_images);
117 
130  template <typename T>
131  T
132  mkval(const unsigned char* buff)
133  {
134  union
135  {
136  T v;
137  unsigned char bytes[sizeof(T)]; // NOLINT(modernize-avoid-c-arrays)
138  } value;
139 
140 #if !defined(_WIN32) && __BYTE_ORDER == __BIG_ENDIAN
141  std::reverse_copy(buff, buff + sizeof(T), value.bytes);
142 #else
143  std::copy(buff, buff + sizeof(T), value.bytes);
144 #endif
145 
146  return value.v;
147  }
151  template <typename T>
153  create_buffer_from_vector(const std::vector<T>& vec)
154  {
155  ifm3d::Buffer buf =
156  Buffer(vec.size(),
157  1,
158  ifm3d::FormatType<T>::NumChannels,
159  static_cast<ifm3d::PixelFormat>(ifm3d::FormatType<T>::Format));
160  std::copy(vec.begin(), vec.end(), buf.begin<T>());
161  return buf;
162  }
166  template <typename T>
168  create_buffer_from_struct(const T& struct_object)
169  {
170  ifm3d::Buffer buf = Buffer(sizeof(T), 1, 1, ifm3d::PixelFormat::FORMAT_8U);
171  const auto* start = reinterpret_cast<const uint8_t*>(&struct_object);
172  auto* ptr = buf.Ptr<uint8_t>(0);
173  std::copy(start, start + sizeof(T), ptr);
174  return buf;
175  }
179  template <typename T>
180  T
181  convert_buffer_to_struct(const ifm3d::Buffer& buf)
182  {
183  T struct_object;
184  const auto* ptr = buf.Ptr<uint8_t>(0);
185  std::copy(ptr,
186  ptr + sizeof(T),
187  reinterpret_cast<uint8_t*>(&struct_object));
188  return struct_object;
189  }
193  template <typename T>
194  inline ifm3d::Buffer
195  create_xyz_buffer(const std::vector<std::uint8_t>& data,
196  std::size_t xidx,
197  std::size_t yidx,
198  std::size_t zidx,
199  std::size_t width,
200  std::size_t height,
201  ifm3d::PixelFormat fmt,
202  const std::optional<ifm3d::Buffer>& mask)
203  {
204  std::size_t const incr = sizeof(T);
205  std::size_t const npts = width * height;
206 
207  ifm3d::Buffer im(width, height, 3, fmt);
208 
209  int col = 0;
210  int row = -1;
211  int xyz_col = 0;
212 
213  T* xyz_ptr = NULL;
214  T x;
215  T y;
216  T z;
217 
218  constexpr T bad_pixel = 0;
219 
220  for (std::size_t i = 0; i < npts;
221  ++i, xidx += incr, yidx += incr, zidx += incr)
222  {
223  col = static_cast<int>(i % static_cast<size_t>(width));
224  xyz_col = col * 3;
225  if (col == 0)
226  {
227  row += 1;
228  xyz_ptr = im.Ptr<T>(row);
229  }
230 
231  x = ifm3d::mkval<T>(data.data() + xidx);
232  y = ifm3d::mkval<T>(data.data() + yidx);
233  z = ifm3d::mkval<T>(data.data() + zidx);
234 
235  if (mask.has_value() && mask.value().At<uint8_t>(row, col) != 0)
236  {
237  xyz_ptr[xyz_col] = bad_pixel;
238  xyz_ptr[xyz_col + 1] = bad_pixel;
239  xyz_ptr[xyz_col + 2] = bad_pixel;
240  }
241  else
242  {
243  xyz_ptr[xyz_col] = x;
244  xyz_ptr[xyz_col + 1] = y;
245  xyz_ptr[xyz_col + 2] = z;
246  }
247  }
248 
249  return im;
250  }
251 
252 } // end: namespace ifm3d
253 
254 #endif // IFM3D_FG_ORGANIZER_UTILS_H
The class Buffer represent a STL container to store data from the ifm devices in 2 dimension and supp...
Definition: buffer.h:99
T * Ptr(std::uint32_t row)
returns a pointer to the specified Buffer row.
Definition: json.hpp:131
BufferId
BufferIds available for use with the default Organizer.
Definition: buffer_id.h:19