LibMultiSense
LibMultiSense Documentation
DisparityMessage.hh
Go to the documentation of this file.
1 
38 #ifndef LibMultiSense_DisparityMessage
39 #define LibMultiSense_DisparityMessage
40 
41 #include <typeinfo>
42 #include <cmath>
43 
44 #include "utility/Portability.hh"
45 
46 namespace crl {
47 namespace multisense {
48 namespace details {
49 namespace wire {
50 
52 public:
53 
55  static CRL_CONSTEXPR VersionType VERSION = 1;
56 
57  static CRL_CONSTEXPR uint8_t WIRE_BITS_PER_PIXEL = MULTISENSE_WIRE_BITS_PER_PIXEL;
58  static CRL_CONSTEXPR uint8_t WIRE_BYTE_ALIGNMENT = 3;
59  static CRL_CONSTEXPR uint8_t API_BITS_PER_PIXEL = MULTISENSE_API_BITS_PER_PIXEL; // after custom assemble()
60  static CRL_CONSTEXPR uint32_t META_LENGTH = 16; // packed, includes type/version
61 
62 #ifdef SENSORPOD_FIRMWARE
63  IdType id;
64  VersionType version;
65 #endif // SENSORPOD_FIRMWARE
66 
67  int64_t frameId;
68  uint16_t width;
69  uint16_t height;
70 
72  :
73 #ifdef SENSORPOD_FIRMWARE
74  id(ID),
75  version(VERSION),
76 #endif // SENSORPOD_FIRMWARE
77  frameId(0),
78  width(0),
79  height(0) {};
80 };
81 
82 #ifndef SENSORPOD_FIRMWARE
83 
84 class Disparity : public DisparityHeader {
85 public:
86 
87  void *dataP;
88 
89  //
90  // Constructors
91 
93  Disparity() : dataP(NULL) {};
94 
95  //
96  // Serialization routine
97 
98  template<class Archive>
99  void serialize(Archive& message,
100  const VersionType version)
101  {
102  (void) version;
103  message & frameId;
104  message & width;
105  message & height;
106 
107  const uint32_t imageSize = static_cast<uint32_t> (std::ceil(((double) API_BITS_PER_PIXEL / 8.0) * width * height));
108 
109  if (typeid(Archive) == typeid(utility::BufferStreamWriter)) {
110 
111  message.write(dataP, imageSize);
112 
113  } else {
114 
115  dataP = message.peek();
116  message.seek(message.tell() + imageSize);
117  }
118  }
119 
120  //
121  // UDP assembler
122 
124  const uint8_t *dataP,
125  uint32_t offset,
126  uint32_t length)
127  {
128  //
129  // Special case, 1st packet contains header only. Firmware
130  // does not have to worry about the header length being
131  // byte-aligned on a WIRE_BITS_PER_PIXEL boundary
132 
133  if (0 == offset) {
134  stream.seek(0);
135  stream.write(dataP, META_LENGTH);
136  return;
137  }
138 
139  //
140  // The data section of each incoming packet is byte-aligned
141  // on a WIRE_BITS_PER_PIXEL boundary
142 
143  const uint8_t *sP = dataP;
144  const uint32_t sourceOffset = offset - META_LENGTH;
145  const uint32_t count = (8 * length) / WIRE_BITS_PER_PIXEL;
146  const uint32_t destOffset = META_LENGTH + (((8 * sourceOffset) / WIRE_BITS_PER_PIXEL) *
147  (API_BITS_PER_PIXEL / 8));
148  //
149  // Seek to the proper location
150 
151  stream.seek(destOffset);
152 
153 #if MULTISENSE_WIRE_BITS_PER_PIXEL == 12 && MULTISENSE_API_BITS_PER_PIXEL == 16
154  //
155  // This conversion is for (WIRE == 12bits), (API == 16bits, 1/16th pixel, unsigned integer)
156 
157  uint16_t *dP = reinterpret_cast<uint16_t*>(stream.peek());
158 
159  for(uint32_t i=0; i<count; i+=2, sP+=3) {
160  dP[i] = ((sP[0] ) | ((sP[1] & 0x0F) << 8));
161  dP[i+1] = ((sP[1] >> 4) | (sP[2] << 4) );
162  }
163 
164 #elif MULTISENSE_WIRE_BITS_PER_PIXEL == 12 && MULTISENSE_API_BITS_PER_PIXEL == 32
165  //
166  // This conversion is for (WIRE == 12bits), (API == 32bits, floating point)
167 
168  float *dP = reinterpret_cast<float*>(stream.peek());
169 
170  for(uint32_t i=0; i<count; i+=2, sP+=3) {
171 
172  dP[i] = static_cast<float>((sP[0] ) | ((sP[1] & 0x0F) << 8)) / 16.0f;
173  dP[i+1] = static_cast<float>((sP[1] >> 4) | (sP[2] << 4) ) / 16.0f;
174  }
175 
176 #else
177 #error MULTISENSE_WIRE_BITS_PER_PIXEL and MULTISENSE_API_BITS_PER_PIXEL not supported
178 #endif
179 
180  }
181 };
182 
183 #endif // !SENSORPOD_FIRMWARE
184 
185 }}}} // namespaces
186 
187 #endif
crl::multisense::details::wire::Disparity::Disparity
Disparity(utility::BufferStreamReader &r, VersionType v)
Definition: DisparityMessage.hh:92
crl::multisense::details::wire::Disparity::assembler
static void assembler(utility::BufferStreamWriter &stream, const uint8_t *dataP, uint32_t offset, uint32_t length)
Definition: DisparityMessage.hh:123
crl::multisense::details::wire::DisparityHeader::frameId
int64_t frameId
Definition: DisparityMessage.hh:67
Portability.hh
WIRE_HEADER_ATTRIBS_
#define WIRE_HEADER_ATTRIBS_
Definition: Protocol.hh:65
crl::multisense::details::wire::DisparityHeader::width
uint16_t width
Definition: DisparityMessage.hh:68
CRL_CONSTEXPR
#define CRL_CONSTEXPR
Definition: Portability.hh:49
crl::multisense::details::wire::DisparityHeader::API_BITS_PER_PIXEL
static CRL_CONSTEXPR uint8_t API_BITS_PER_PIXEL
Definition: DisparityMessage.hh:59
MULTISENSE_WIRE_BITS_PER_PIXEL
#define MULTISENSE_WIRE_BITS_PER_PIXEL
Definition: Protocol.hh:47
crl
Definition: BufferStream.hh:51
crl::multisense::details::wire::ID_DATA_DISPARITY
static CRL_CONSTEXPR IdType ID_DATA_DISPARITY
Definition: Protocol.hh:218
crl::multisense::details::wire::DisparityHeader::DisparityHeader
DisparityHeader()
Definition: DisparityMessage.hh:71
crl::multisense::details::wire::DisparityHeader
Definition: DisparityMessage.hh:51
crl::multisense::details::wire::DisparityHeader::height
uint16_t height
Definition: DisparityMessage.hh:69
crl::multisense::details::wire::VersionType
uint16_t VersionType
Definition: Protocol.hh:137
crl::multisense::details::utility::BufferStreamWriter
Definition: BufferStream.hh:259
crl::multisense::details::wire::DisparityHeader::WIRE_BITS_PER_PIXEL
static CRL_CONSTEXPR uint8_t WIRE_BITS_PER_PIXEL
Definition: DisparityMessage.hh:57
MULTISENSE_API_BITS_PER_PIXEL
#define MULTISENSE_API_BITS_PER_PIXEL
Definition: Protocol.hh:51
crl::multisense::details::wire::Disparity
Definition: DisparityMessage.hh:84
crl::multisense::details::wire::Disparity::serialize
void serialize(Archive &message, const VersionType version)
Definition: DisparityMessage.hh:99
crl::multisense::details::utility::BufferStreamReader
Definition: BufferStream.hh:192
multisense
Definition: MultiSenseChannel.hh:44
crl::multisense::details::utility::BufferStreamWriter::write
virtual void write(const void *bufferP, std::size_t length)
Definition: BufferStream.hh:267
crl::multisense::details::utility::BufferStream::peek
void * peek() const
Definition: BufferStream.hh:73
crl::multisense::details::wire::DisparityHeader::META_LENGTH
static CRL_CONSTEXPR uint32_t META_LENGTH
Definition: DisparityMessage.hh:60
crl::multisense::details::wire::Disparity::Disparity
Disparity()
Definition: DisparityMessage.hh:93
crl::multisense::details::wire::IdType
uint16_t IdType
Definition: Protocol.hh:136
crl::multisense::details::utility::BufferStream::seek
void seek(std::size_t idx)
Definition: BufferStream.hh:93
crl::multisense::details::wire::Disparity::dataP
void * dataP
Definition: DisparityMessage.hh:87