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 <cstddef>
12 #include <ifm3d/deserialize/deserialize_utils.hpp>
13 #include <ifm3d/device/device.h>
14 #include <ifm3d/device/err.h>
15 #include <ifm3d/fg/buffer.h>
16 #include <ifm3d/fg/organizer_utils.h>
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  bool
37  IsValid(const uint8_t*, size_t size)
38  {
39  return size >= ODS_OCCUPANCY_GRID_V1_MINIMUM_SIZE;
40  }
41 
42  void
43  Read(const uint8_t* data, size_t size)
44  {
45  if (!IsValid(data, size))
46  {
47  throw ifm3d::Error(IFM3D_CORRUPTED_STRUCT);
48  }
49  const uint8_t* start_ptr = data;
50  timestamp_ns = mkval<std::uint64_t>(
51  start_ptr + ODS_OCCUPANCY_GRID_TIMESTAMP_NS_INDEX);
52  width = mkval<std::uint32_t>(start_ptr + ODS_OCCUPANCY_GRID_WIDTH_INDEX);
53  height =
54  mkval<std::uint32_t>(start_ptr + ODS_OCCUPANCY_GRID_HEIGHT_INDEX);
55  mkarray<float, 6>(
56  start_ptr + ODS_OCCUPANCY_GRID_TRANSFORM_CELL_CENTER_TO_USER_INDEX,
57  transform_cell_center_to_user);
58  ods_occupancy_grid_v1_size = ODS_OCCUPANCY_GRID_V1_MINIMUM_SIZE +
59  static_cast<size_t>(width) * height;
60  if (!IsValid(data, size))
61  {
62  throw ifm3d::Error(IFM3D_CORRUPTED_STRUCT);
63  }
64 
65  image = ifm3d::Buffer(height, width, 1, ifm3d::PixelFormat::FORMAT_8U);
66  std::memcpy(image.Ptr<uint8_t>(0),
67  start_ptr + ODS_OCCUPANCY_GRID_IMAGE_INDEX,
68  image.Size());
69  };
70  /*@brief Timestamp of occupany grid in [ns]*/
71  uint64_t timestamp_ns{};
72  /*@brief Number of grid cells*/
73  uint32_t width{};
74  /*@brief number of grid cells*/
75  uint32_t height{};
76  /*@brief Values of matrix 2x3
77  * affine mapping between grid cell and user coordinate system
78  * e.g, multiplying the matrix with [0,0,1] gives the user cordinate
79  * of the center of upper left cell
80  */
81  std::array<float, 6> transform_cell_center_to_user{};
82  /*@brief Buffer of width* height of type uint8_t */
83  ifm3d::Buffer image;
84  /*@brief size of ODS_OCCUPANCY_GRID in bytes*/
85  size_t ods_occupancy_grid_v1_size{};
86 
87  private:
88  static constexpr size_t ODS_OCCUPANCY_GRID_V1_MINIMUM_SIZE = 40;
89 
90  public:
91  static ODSOccupancyGridV1
92  Deserialize(const Buffer& ods_occupancy_buffer_grid)
93  {
94  ODSOccupancyGridV1 ods_occupancy_grid_v1;
95 
96  ods_occupancy_grid_v1.Read(ods_occupancy_buffer_grid.Ptr<uint8_t>(0),
97  ods_occupancy_buffer_grid.Size());
98  return ods_occupancy_grid_v1;
99  }
100  };
101 } // end namespace ifm3d
102 
103 #endif // IFM3D_DESERIALIZE_STRUCT_O3R_ODS_OCCUPANCY_GRID_V1_HPP
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::Error
Definition: err.h:114
ifm3d::Buffer::Size
size_t Size() const
Return the size of the buffer in bytes.
ifm3d::Buffer::Ptr
T * Ptr(std::uint32_t row)
returns a pointer to the specified Buffer row.
ifm3d::ODSOccupancyGridV1
Definition: struct_o3r_ods_occupancy_grid_v1.hpp:31