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