ifm3d
struct_o3r_ods_occupancy_grid_v1.hpp
1 // -*- c++ -*-
2 /*
3  * Copyright 2022-present ifm electronic, gmbh
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef IFM3D_DESERIALIZE_STRUCT_O3R_ODS_OCCUPANCY_GRID_V1_HPP
8 #define IFM3D_DESERIALIZE_STRUCT_O3R_ODS_OCCUPANCY_GRID_V1_HPP
9 
10 #include <array>
11 #include <chrono>
12 #include <ifm3d/device/device.h>
13 #include <ifm3d/device/err.h>
14 #include <ifm3d/fg/organizer_utils.h>
15 #include <ifm3d/fg/buffer.h>
16 #include <ifm3d/deserialize/deserialize_utils.hpp>
17 
18 namespace ifm3d
19 {
20  namespace
21  {
22  constexpr auto ODS_OCCUPANCY_GRID_TIMESTAMP_NS_INDEX = 0x0000;
23  constexpr auto ODS_OCCUPANCY_GRID_WIDTH_INDEX = 0x0008;
24  constexpr auto ODS_OCCUPANCY_GRID_HEIGHT_INDEX = 0x000C;
25  constexpr auto ODS_OCCUPANCY_GRID_TRANSFORM_CELL_CENTER_TO_USER_INDEX =
26  0x0010;
27  constexpr auto ODS_OCCUPANCY_GRID_IMAGE_INDEX = 0x0028;
28  };
29 
32  {
33  public:
34  using Ptr = std::shared_ptr<ODSOccupancyGridV1>;
35 
36  void
37  Read(const uint8_t* data, size_t size)
38  {
39  if (size < ods_occupancy_grid_v1_minimum_size)
40  {
41  throw ifm3d::Error(IFM3D_CORRUPTED_STRUCT);
42  }
43  const uint8_t* start_ptr = data;
44  timestamp_ns = mkval<std::uint64_t>(
45  start_ptr + ODS_OCCUPANCY_GRID_TIMESTAMP_NS_INDEX);
46  width = mkval<std::uint32_t>(start_ptr + ODS_OCCUPANCY_GRID_WIDTH_INDEX);
47  height =
48  mkval<std::uint32_t>(start_ptr + ODS_OCCUPANCY_GRID_HEIGHT_INDEX);
49  mkarray<float, 6>(
50  start_ptr + ODS_OCCUPANCY_GRID_TRANSFORM_CELL_CENTER_TO_USER_INDEX,
51  transform_cell_center_to_user);
52  ods_occupancy_grid_v1_size =
53  ods_occupancy_grid_v1_minimum_size + width * height;
54  if (size < ods_occupancy_grid_v1_size)
55  {
56  throw ifm3d::Error(IFM3D_CORRUPTED_STRUCT);
57  }
58 
59  image = ifm3d::Buffer(height, width, 1, ifm3d::pixel_format::FORMAT_8U);
60  std::memcpy(image.ptr<uint8_t>(0),
61  start_ptr + ODS_OCCUPANCY_GRID_IMAGE_INDEX,
62  image.size());
63  };
64  /*@brief Timestamp of occupany grid in [ns]*/
65  uint64_t timestamp_ns;
66  /*@brief Number of grid cells*/
67  uint32_t width;
68  /*@brief number of grid cells*/
69  uint32_t height;
70  /*@brief Values of matrix 2x3
71  * affine mapping between grid cell and user coordinate system
72  * e.g, multiplying the matrix with [0,0,1] gives the user cordinate
73  * of the center of upper left cell
74  */
75  std::array<float, 6> transform_cell_center_to_user;
76  /*@brief Buffer of width* height of type uint8_t */
77  ifm3d::Buffer image;
78  /*@brief size of ODS_OCCUPANCY_GRID in bytes*/
79  size_t ods_occupancy_grid_v1_size;
80 
81  private:
82  const size_t ods_occupancy_grid_v1_minimum_size = 40;
83 
84  public:
85  static ODSOccupancyGridV1
86  Deserialize(const Buffer& ods_occupancy_buffer_grid)
87  {
88  ODSOccupancyGridV1 ods_occupancy_grid_v1;
89 
90  ods_occupancy_grid_v1.Read(ods_occupancy_buffer_grid.ptr<uint8_t>(0),
91  ods_occupancy_buffer_grid.size());
92  return ods_occupancy_grid_v1;
93  }
94  };
95 } // end namespace ifm3d
96 
97 #endif // IFM3D_DESERIALIZE_STRUCT_O3R_ODS_OCCUPANCY_GRID_V1_HPP
ifm3d::Buffer::size
size_t size() const
Return the size of the buffer in bytes.
ifm3d::Buffer
The class Buffer represent a STL container to store data from the ifm devices in 2 dimension and supp...
Definition: buffer.h:98
ifm3d::Buffer::ptr
T * ptr(const std::uint32_t row)
returns a pointer to the specified Buffer row.
ifm3d::Error
Definition: err.h:112
ifm3d::ODSOccupancyGridV1
Definition: struct_o3r_ods_occupancy_grid_v1.hpp:31