point_field_conversion.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Robot Operating System code by the University of Osnabrück
5  * Copyright (c) 2015, University of Osnabrück
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above
13  * copyright notice, this list of conditions and the following
14  * disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  *
21  * 3. Neither the name of the copyright holder nor the names of its
22  * contributors may be used to endorse or promote products derived
23  * from this software without specific prior written permission.
24  *
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
30  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
33  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
34  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
35  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  *
38  *
39  *
40  * point_field_conversion.h
41  *
42  * Created on: 16.07.2015
43  * Authors: Sebastian Pütz <spuetz@uni-osnabrueck.de>
44  */
45 
46 #ifndef SENSOR_MSGS_POINT_FIELD_CONVERSION_H
47 #define SENSOR_MSGS_POINT_FIELD_CONVERSION_H
48 
55 namespace sensor_msgs{
59  template<int> struct pointFieldTypeAsType {};
60  template<> struct pointFieldTypeAsType<sensor_msgs::PointField::INT8> { typedef int8_t type; };
61  template<> struct pointFieldTypeAsType<sensor_msgs::PointField::UINT8> { typedef uint8_t type; };
62  template<> struct pointFieldTypeAsType<sensor_msgs::PointField::INT16> { typedef int16_t type; };
63  template<> struct pointFieldTypeAsType<sensor_msgs::PointField::UINT16> { typedef uint16_t type; };
64  template<> struct pointFieldTypeAsType<sensor_msgs::PointField::INT32> { typedef int32_t type; };
65  template<> struct pointFieldTypeAsType<sensor_msgs::PointField::UINT32> { typedef uint32_t type; };
66  template<> struct pointFieldTypeAsType<sensor_msgs::PointField::FLOAT32> { typedef float type; };
67  template<> struct pointFieldTypeAsType<sensor_msgs::PointField::FLOAT64> { typedef double type; };
68 
72  template<typename T> struct typeAsPointFieldType {};
73  template<> struct typeAsPointFieldType<int8_t> { static const uint8_t value = sensor_msgs::PointField::INT8; };
74  template<> struct typeAsPointFieldType<uint8_t> { static const uint8_t value = sensor_msgs::PointField::UINT8; };
75  template<> struct typeAsPointFieldType<int16_t> { static const uint8_t value = sensor_msgs::PointField::INT16; };
76  template<> struct typeAsPointFieldType<uint16_t> { static const uint8_t value = sensor_msgs::PointField::UINT16; };
77  template<> struct typeAsPointFieldType<int32_t> { static const uint8_t value = sensor_msgs::PointField::INT32; };
78  template<> struct typeAsPointFieldType<uint32_t> { static const uint8_t value = sensor_msgs::PointField::UINT32; };
79  template<> struct typeAsPointFieldType<float> { static const uint8_t value = sensor_msgs::PointField::FLOAT32; };
80  template<> struct typeAsPointFieldType<double> { static const uint8_t value = sensor_msgs::PointField::FLOAT64; };
81 
90  template<int point_field_type, typename T>
91  inline T readPointCloud2BufferValue(const unsigned char* data_ptr){
93  return static_cast<T>(*(reinterpret_cast<type const *>(data_ptr)));
94  }
95 
103  template<typename T>
104  inline T readPointCloud2BufferValue(const unsigned char* data_ptr, const unsigned char datatype){
105  switch(datatype){
106  case sensor_msgs::PointField::INT8:
107  return readPointCloud2BufferValue<sensor_msgs::PointField::INT8, T>(data_ptr);
108  case sensor_msgs::PointField::UINT8:
109  return readPointCloud2BufferValue<sensor_msgs::PointField::UINT8, T>(data_ptr);
110  case sensor_msgs::PointField::INT16:
111  return readPointCloud2BufferValue<sensor_msgs::PointField::INT16, T>(data_ptr);
112  case sensor_msgs::PointField::UINT16:
113  return readPointCloud2BufferValue<sensor_msgs::PointField::UINT16, T>(data_ptr);
114  case sensor_msgs::PointField::INT32:
115  return readPointCloud2BufferValue<sensor_msgs::PointField::INT32, T>(data_ptr);
116  case sensor_msgs::PointField::UINT32:
117  return readPointCloud2BufferValue<sensor_msgs::PointField::UINT32, T>(data_ptr);
118  case sensor_msgs::PointField::FLOAT32:
119  return readPointCloud2BufferValue<sensor_msgs::PointField::FLOAT32, T>(data_ptr);
120  case sensor_msgs::PointField::FLOAT64:
121  return readPointCloud2BufferValue<sensor_msgs::PointField::FLOAT64, T>(data_ptr);
122  }
123  // This should never be reached, but return statement added to avoid compiler warning. (#84)
124  return T();
125  }
126 
135  template<int point_field_type, typename T>
136  inline void writePointCloud2BufferValue(unsigned char* data_ptr, T value){
137  typedef typename pointFieldTypeAsType<point_field_type>::type type;
138  *(reinterpret_cast<type*>(data_ptr)) = static_cast<type>(value);
139  }
140 
149  template<typename T>
150  inline void writePointCloud2BufferValue(unsigned char* data_ptr, const unsigned char datatype, T value){
151  switch(datatype){
152  case sensor_msgs::PointField::INT8:
153  writePointCloud2BufferValue<sensor_msgs::PointField::INT8, T>(data_ptr, value);
154  break;
155  case sensor_msgs::PointField::UINT8:
156  writePointCloud2BufferValue<sensor_msgs::PointField::UINT8, T>(data_ptr, value);
157  break;
158  case sensor_msgs::PointField::INT16:
159  writePointCloud2BufferValue<sensor_msgs::PointField::INT16, T>(data_ptr, value);
160  break;
161  case sensor_msgs::PointField::UINT16:
162  writePointCloud2BufferValue<sensor_msgs::PointField::UINT16, T>(data_ptr, value);
163  break;
164  case sensor_msgs::PointField::INT32:
165  writePointCloud2BufferValue<sensor_msgs::PointField::INT32, T>(data_ptr, value);
166  break;
167  case sensor_msgs::PointField::UINT32:
168  writePointCloud2BufferValue<sensor_msgs::PointField::UINT32, T>(data_ptr, value);
169  break;
170  case sensor_msgs::PointField::FLOAT32:
171  writePointCloud2BufferValue<sensor_msgs::PointField::FLOAT32, T>(data_ptr, value);
172  break;
173  case sensor_msgs::PointField::FLOAT64:
174  writePointCloud2BufferValue<sensor_msgs::PointField::FLOAT64, T>(data_ptr, value);
175  break;
176  }
177  }
178 }
179 
180 #endif /* point_field_conversion.h */
sensor_msgs::pointFieldTypeAsType
Definition: point_field_conversion.h:59
sensor_msgs::pointFieldTypeAsType< sensor_msgs::PointField::INT16 >::type
int16_t type
Definition: point_field_conversion.h:62
sensor_msgs::pointFieldTypeAsType< sensor_msgs::PointField::UINT8 >::type
uint8_t type
Definition: point_field_conversion.h:61
sensor_msgs::writePointCloud2BufferValue
void writePointCloud2BufferValue(unsigned char *data_ptr, T value)
Definition: point_field_conversion.h:136
sensor_msgs::typeAsPointFieldType
Definition: point_field_conversion.h:72
sensor_msgs::pointFieldTypeAsType< sensor_msgs::PointField::INT8 >::type
int8_t type
Definition: point_field_conversion.h:60
sensor_msgs::pointFieldTypeAsType< sensor_msgs::PointField::FLOAT32 >::type
float type
Definition: point_field_conversion.h:66
datatype
const char * datatype()
sensor_msgs::pointFieldTypeAsType< sensor_msgs::PointField::FLOAT64 >::type
double type
Definition: point_field_conversion.h:67
sensor_msgs
Tools for manipulating sensor_msgs.
Definition: distortion_models.h:41
sensor_msgs::readPointCloud2BufferValue
T readPointCloud2BufferValue(const unsigned char *data_ptr)
Definition: point_field_conversion.h:91
sensor_msgs::pointFieldTypeAsType< sensor_msgs::PointField::UINT16 >::type
uint16_t type
Definition: point_field_conversion.h:63
sensor_msgs::pointFieldTypeAsType< sensor_msgs::PointField::UINT32 >::type
uint32_t type
Definition: point_field_conversion.h:65
sensor_msgs::pointFieldTypeAsType< sensor_msgs::PointField::INT32 >::type
int32_t type
Definition: point_field_conversion.h:64


sensor_msgs
Author(s): Tully Foote
autogenerated on Wed Mar 2 2022 00:06:55