Point Cloud Library (PCL)  1.9.1-dev
pcl_macros.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2012, Willow Garage, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of the copyright holder(s) nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  */
36 
37 #pragma once
38 
39 /**
40  * \file pcl/pcl_macros.h
41  *
42  * \brief Defines all the PCL and non-PCL macros used
43  * \ingroup common
44  */
45 
46 #if defined __INTEL_COMPILER
47  #pragma warning disable 2196 2536 279
48 #endif
49 
50 #if defined _MSC_VER
51  // 4244 : conversion from 'type1' to 'type2', possible loss of data
52  // 4661 : no suitable definition provided for explicit template instantiation reques
53  // 4503 : decorated name length exceeded, name was truncated
54  // 4146 : unary minus operator applied to unsigned type, result still unsigned
55  #pragma warning (disable: 4018 4244 4267 4521 4251 4661 4305 4503 4146)
56 #endif
57 
58 #ifndef _USE_MATH_DEFINES
59 #define _USE_MATH_DEFINES
60 #endif
61 #include <cmath>
62 #include <cstdarg>
63 #include <cstdio>
64 #include <cstdlib>
65 #include <iostream>
66 
67 #include <boost/cstdint.hpp>
68 
69 //Eigen has an enum that clashes with X11 Success define, which is ultimately included by pcl
70 #ifdef Success
71  #undef Success
72 #endif
73 #include <Eigen/Core>
74 
75 #include <pcl/pcl_config.h>
76 
77 namespace pcl
78 {
79  using boost::uint8_t;
80  using boost::int8_t;
81  using boost::int16_t;
82  using boost::uint16_t;
83  using boost::int32_t;
84  using boost::uint32_t;
85  using boost::int64_t;
86  using boost::uint64_t;
87  using boost::int_fast16_t;
88 }
89 
90 #if defined _WIN32 && defined _MSC_VER
91 
92 // Define math constants, without including math.h, to prevent polluting global namespace with old math methods
93 // Copied from math.h
94 #ifndef _MATH_DEFINES_DEFINED
95  #define _MATH_DEFINES_DEFINED
96 
97  #define M_E 2.71828182845904523536 // e
98  #define M_LOG2E 1.44269504088896340736 // log2(e)
99  #define M_LOG10E 0.434294481903251827651 // log10(e)
100  #define M_LN2 0.693147180559945309417 // ln(2)
101  #define M_LN10 2.30258509299404568402 // ln(10)
102  #define M_PI 3.14159265358979323846 // pi
103  #define M_PI_2 1.57079632679489661923 // pi/2
104  #define M_PI_4 0.785398163397448309616 // pi/4
105  #define M_1_PI 0.318309886183790671538 // 1/pi
106  #define M_2_PI 0.636619772367581343076 // 2/pi
107  #define M_2_SQRTPI 1.12837916709551257390 // 2/sqrt(pi)
108  #define M_SQRT2 1.41421356237309504880 // sqrt(2)
109  #define M_SQRT1_2 0.707106781186547524401 // 1/sqrt(2)
110 #endif
111 
112 // Stupid. This should be removed when all the PCL dependencies have min/max fixed.
113 #ifndef NOMINMAX
114 # define NOMINMAX
115 #endif
116 
117 # define __PRETTY_FUNCTION__ __FUNCTION__
118 # define __func__ __FUNCTION__
119 
120 #endif
121 
122 #ifndef DEG2RAD
123 #define DEG2RAD(x) ((x)*0.017453293)
124 #endif
125 
126 #ifndef RAD2DEG
127 #define RAD2DEG(x) ((x)*57.29578)
128 #endif
129 
130 /** \brief Macro that maps version information given by major.minor.patch to a linear integer value to enable easy comparison
131  */
132 #define PCL_LINEAR_VERSION(major,minor,patch) ((major)<<16|(minor)<<8|(patch))
133 
134 /** Win32 doesn't seem to have rounding functions.
135  * Therefore implement our own versions of these functions here.
136  */
137 
138 __inline double
139 pcl_round (double number)
140 {
141  return (number < 0.0 ? std::ceil (number - 0.5) : std::floor (number + 0.5));
142 }
143 __inline float
144 pcl_round (float number)
145 {
146  return (number < 0.0f ? std::ceil (number - 0.5f) : std::floor (number + 0.5f));
147 }
148 
149 #ifdef __GNUC__
150 #define pcl_lrint(x) (lrint(static_cast<double> (x)))
151 #define pcl_lrintf(x) (lrintf(static_cast<float> (x)))
152 #else
153 #define pcl_lrint(x) (static_cast<long int>(pcl_round(x)))
154 #define pcl_lrintf(x) (static_cast<long int>(pcl_round(x)))
155 #endif
156 
157 #ifdef WIN32
158 #define pcl_sleep(x) Sleep(1000*(x))
159 #else
160 #define pcl_sleep(x) sleep(x)
161 #endif
162 
163 #ifndef PVAR
164  #define PVAR(s) \
165  #s << " = " << (s) << std::flush
166 #endif
167 #ifndef PVARN
168 #define PVARN(s) \
169  #s << " = " << (s) << "\n"
170 #endif
171 #ifndef PVARC
172 #define PVARC(s) \
173  #s << " = " << (s) << ", " << std::flush
174 #endif
175 #ifndef PVARS
176 #define PVARS(s) \
177  #s << " = " << (s) << " " << std::flush
178 #endif
179 #ifndef PVARA
180 #define PVARA(s) \
181  #s << " = " << RAD2DEG(s) << "deg" << std::flush
182 #endif
183 #ifndef PVARAN
184 #define PVARAN(s) \
185  #s << " = " << RAD2DEG(s) << "deg\n"
186 #endif
187 #ifndef PVARAC
188 #define PVARAC(s) \
189  #s << " = " << RAD2DEG(s) << "deg, " << std::flush
190 #endif
191 #ifndef PVARAS
192 #define PVARAS(s) \
193  #s << " = " << RAD2DEG(s) << "deg " << std::flush
194 #endif
195 
196 #define FIXED(s) \
197  std::fixed << s << std::resetiosflags(std::ios_base::fixed)
198 
199 #ifndef ERASE_STRUCT
200 #define ERASE_STRUCT(var) memset(&var, 0, sizeof(var))
201 #endif
202 
203 #ifndef ERASE_ARRAY
204 #define ERASE_ARRAY(var, size) memset(var, 0, size*sizeof(*var))
205 #endif
206 
207 #ifndef SET_ARRAY
208 #define SET_ARRAY(var, value, size) { for (int i = 0; i < static_cast<int> (size); ++i) var[i]=value; }
209 #endif
210 
211 #ifndef PCL_EXTERN_C
212  #ifdef __cplusplus
213  #define PCL_EXTERN_C extern "C"
214  #else
215  #define PCL_EXTERN_C
216  #endif
217 #endif
218 
219 #if defined WIN32 || defined _WIN32 || defined WINCE || defined __MINGW32__
220  #ifdef PCLAPI_EXPORTS
221  #define PCL_EXPORTS __declspec(dllexport)
222  #else
223  #define PCL_EXPORTS
224  #endif
225 #else
226  #define PCL_EXPORTS
227 #endif
228 
229 #if defined WIN32 || defined _WIN32
230  #define PCL_CDECL __cdecl
231  #define PCL_STDCALL __stdcall
232 #else
233  #define PCL_CDECL
234  #define PCL_STDCALL
235 #endif
236 
237 #ifndef PCLAPI
238  #define PCLAPI(rettype) PCL_EXTERN_C PCL_EXPORTS rettype PCL_CDECL
239 #endif
240 
241 // Macro for pragma operator
242 #if (defined (__GNUC__) || defined(__clang__))
243  #define PCL_PRAGMA(x) _Pragma (#x)
244 #elif _MSC_VER
245  #define PCL_PRAGMA(x) __pragma (#x)
246 #else
247  #define PCL_PRAGMA
248 #endif
249 
250 // Macro for emitting pragma warning
251 #if (defined (__GNUC__) || defined(__clang__))
252  #define PCL_PRAGMA_WARNING(x) PCL_PRAGMA (GCC warning x)
253 #elif _MSC_VER
254  #define PCL_PRAGMA_WARNING(x) PCL_PRAGMA (warning (x))
255 #else
256  #define PCL_PRAGMA_WARNING
257 #endif
258 
259 //for clang cf. http://clang.llvm.org/docs/LanguageExtensions.html
260 #ifndef __has_extension
261  #define __has_extension(x) 0 // Compatibility with pre-3.0 compilers.
262 #endif
263 
264 #if defined (__GNUC__) || defined (__PGI) || defined (__IBMCPP__) || defined (__SUNPRO_CC)
265  #define PCL_ALIGN(alignment) __attribute__((aligned(alignment)))
266 #elif defined (_MSC_VER)
267  #define PCL_ALIGN(alignment) __declspec(align(alignment))
268 #else
269  #error Alignment not supported on your platform
270 #endif
271 
272 #if defined(__GLIBC__) && PCL_LINEAR_VERSION(__GLIBC__,__GLIBC_MINOR__,0)>PCL_LINEAR_VERSION(2,8,0)
273  #define GLIBC_MALLOC_ALIGNED 1
274 #else
275  #define GLIBC_MALLOC_ALIGNED 0
276 #endif
277 
278 #if defined(__FreeBSD__) && !defined(__arm__) && !defined(__mips__)
279  #define FREEBSD_MALLOC_ALIGNED 1
280 #else
281  #define FREEBSD_MALLOC_ALIGNED 0
282 #endif
283 
284 #if defined(__APPLE__) || defined(_WIN64) || GLIBC_MALLOC_ALIGNED || FREEBSD_MALLOC_ALIGNED
285  #define MALLOC_ALIGNED 1
286 #endif
287 
288 #if defined (HAVE_MM_MALLOC)
289  // Intel compiler defines an incompatible _mm_malloc signature
290  #if defined(__INTEL_COMPILER)
291  #include <malloc.h>
292  #else
293  #include <mm_malloc.h>
294  #endif
295 #endif
296 
297 inline void*
298 aligned_malloc (size_t size)
299 {
300  void *ptr;
301 #if defined (MALLOC_ALIGNED)
302  ptr = std::malloc (size);
303 #elif defined (HAVE_POSIX_MEMALIGN)
304  if (posix_memalign (&ptr, 16, size))
305  ptr = 0;
306 #elif defined (HAVE_MM_MALLOC)
307  ptr = _mm_malloc (size, 16);
308 #elif defined (_MSC_VER)
309  ptr = _aligned_malloc (size, 16);
310 #elif defined (ANDROID)
311  ptr = memalign (16, size);
312 #else
313  #error aligned_malloc not supported on your platform
314  ptr = 0;
315 #endif
316  return (ptr);
317 }
318 
319 inline void
320 aligned_free (void* ptr)
321 {
322 #if defined (MALLOC_ALIGNED) || defined (HAVE_POSIX_MEMALIGN)
323  std::free (ptr);
324 #elif defined (HAVE_MM_MALLOC)
325  _mm_free (ptr);
326 #elif defined (_MSC_VER)
327  _aligned_free (ptr);
328 #elif defined (ANDROID)
329  free (ptr);
330 #else
331  #error aligned_free not supported on your platform
332 #endif
333 }
334 
335 /**
336  * \brief Macro to signal a class requires a custom allocator
337  *
338  * It's an implementation detail to have pcl::has_custom_allocator work, a
339  * thin wrapper over Eigen's own macro
340  *
341  * \see pcl::has_custom_allocator, pcl::make_shared
342  * \ingroup common
343  */
344 #define PCL_MAKE_ALIGNED_OPERATOR_NEW \
345  EIGEN_MAKE_ALIGNED_OPERATOR_NEW \
346  using _custom_allocator_type_trait = void;
347 
348 /**
349  * \brief Macro to add a no-op or a fallthrough attribute based on compiler feature
350  *
351  * \ingroup common
352  */
353 #if __has_cpp_attribute(fallthrough) && !(defined(__clang__) && __cplusplus < 201703L)
354  #define PCL_FALLTHROUGH [[fallthrough]];
355 #elif defined(__clang__)
356  #define PCL_FALLTHROUGH [[clang::fallthrough]];
357 #elif defined(__GNUC__)
358  #if __GNUC__ >= 7
359  #define PCL_FALLTHROUGH [[gnu::fallthrough]];
360  #else
361  #define PCL_FALLTHROUGH ;
362  #endif
363 #else
364  #define PCL_FALLTHROUGH ;
365 #endif
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
__inline double pcl_round(double number)
Win32 doesn&#39;t seem to have rounding functions.
Definition: pcl_macros.h:139
void aligned_free(void *ptr)
Definition: pcl_macros.h:320
void * aligned_malloc(size_t size)
Definition: pcl_macros.h:298