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 <cstdint>
66 #include <iostream>
67 
68 #include <boost/cstdint.hpp>
69 
70 //Eigen has an enum that clashes with X11 Success define, which is ultimately included by pcl
71 #ifdef Success
72  #undef Success
73 #endif
74 #include <Eigen/Core>
75 
76 #include <pcl/pcl_config.h>
77 
78 namespace pcl
79 {
80  using uint8_t [[deprecated("use std::uint8_t instead of pcl::uint8_t")]] = std::uint8_t;
81  using int8_t [[deprecated("use std::int8_t instead of pcl::int8_t")]] = std::int8_t;
82  using uint16_t [[deprecated("use std::uint16_t instead of pcl::uint16_t")]] = std::uint16_t;
83  using int16_t [[deprecated("use std::uint16_t instead of pcl::int16_t")]] = std::int16_t;
84  using uint32_t [[deprecated("use std::uint32_t instead of pcl::uint32_t")]] = std::uint32_t;
85  using int32_t [[deprecated("use std::int32_t instead of pcl::int32_t")]] = std::int32_t;
86  using uint64_t [[deprecated("use std::uint64_t instead of pcl::uint64_t")]] = std::uint64_t;
87  using int64_t [[deprecated("use std::int64_t instead of pcl::int64_t")]] = std::int64_t;
88  using int_fast16_t [[deprecated("use std::int_fast16_t instead of pcl::int_fast16_t")]] = std::int_fast16_t;
89 }
90 
91 #if defined _WIN32 && defined _MSC_VER
92 
93 // Define math constants, without including math.h, to prevent polluting global namespace with old math methods
94 // Copied from math.h
95 #ifndef _MATH_DEFINES_DEFINED
96  #define _MATH_DEFINES_DEFINED
97 
98  #define M_E 2.71828182845904523536 // e
99  #define M_LOG2E 1.44269504088896340736 // log2(e)
100  #define M_LOG10E 0.434294481903251827651 // log10(e)
101  #define M_LN2 0.693147180559945309417 // ln(2)
102  #define M_LN10 2.30258509299404568402 // ln(10)
103  #define M_PI 3.14159265358979323846 // pi
104  #define M_PI_2 1.57079632679489661923 // pi/2
105  #define M_PI_4 0.785398163397448309616 // pi/4
106  #define M_1_PI 0.318309886183790671538 // 1/pi
107  #define M_2_PI 0.636619772367581343076 // 2/pi
108  #define M_2_SQRTPI 1.12837916709551257390 // 2/sqrt(pi)
109  #define M_SQRT2 1.41421356237309504880 // sqrt(2)
110  #define M_SQRT1_2 0.707106781186547524401 // 1/sqrt(2)
111 #endif
112 
113 // Stupid. This should be removed when all the PCL dependencies have min/max fixed.
114 #ifndef NOMINMAX
115 # define NOMINMAX
116 #endif
117 
118 # define __PRETTY_FUNCTION__ __FUNCTION__
119 # define __func__ __FUNCTION__
120 
121 #endif //defined _WIN32 && defined _MSC_VER
122 
123 
124 template<typename T>
125 [[deprecated("use std::isnan instead of pcl_isnan")]]
126 bool pcl_isnan (T&& x) { return std::isnan (std::forward<T> (x)); }
127 
128 template<typename T>
129 [[deprecated("use std::isfinite instead of pcl_isfinite")]]
130 bool pcl_isfinite (T&& x) { return std::isfinite (std::forward<T> (x)); }
131 
132 template<typename T>
133 [[deprecated("use std::isinf instead of pcl_isinf")]]
134 bool pcl_isinf (T&& x) { return std::isinf (std::forward<T> (x)); }
135 
136 
137 #ifndef DEG2RAD
138 #define DEG2RAD(x) ((x)*0.017453293)
139 #endif
140 
141 #ifndef RAD2DEG
142 #define RAD2DEG(x) ((x)*57.29578)
143 #endif
144 
145 /** \brief Macro that maps version information given by major.minor.patch to a linear integer value to enable easy comparison
146  */
147 #define PCL_LINEAR_VERSION(major,minor,patch) ((major)<<16|(minor)<<8|(patch))
148 
149 /** Win32 doesn't seem to have rounding functions.
150  * Therefore implement our own versions of these functions here.
151  */
152 
153 __inline double
154 pcl_round (double number)
155 {
156  return (number < 0.0 ? std::ceil (number - 0.5) : std::floor (number + 0.5));
157 }
158 __inline float
159 pcl_round (float number)
160 {
161  return (number < 0.0f ? std::ceil (number - 0.5f) : std::floor (number + 0.5f));
162 }
163 
164 #ifdef __GNUC__
165 #define pcl_lrint(x) (lrint(static_cast<double> (x)))
166 #define pcl_lrintf(x) (lrintf(static_cast<float> (x)))
167 #else
168 #define pcl_lrint(x) (static_cast<long int>(pcl_round(x)))
169 #define pcl_lrintf(x) (static_cast<long int>(pcl_round(x)))
170 #endif
171 
172 #ifdef WIN32
173 #define pcl_sleep(x) Sleep(1000*(x))
174 #else
175 #define pcl_sleep(x) sleep(x)
176 #endif
177 
178 #ifndef PVAR
179  #define PVAR(s) \
180  #s << " = " << (s) << std::flush
181 #endif
182 #ifndef PVARN
183 #define PVARN(s) \
184  #s << " = " << (s) << "\n"
185 #endif
186 #ifndef PVARC
187 #define PVARC(s) \
188  #s << " = " << (s) << ", " << std::flush
189 #endif
190 #ifndef PVARS
191 #define PVARS(s) \
192  #s << " = " << (s) << " " << std::flush
193 #endif
194 #ifndef PVARA
195 #define PVARA(s) \
196  #s << " = " << RAD2DEG(s) << "deg" << std::flush
197 #endif
198 #ifndef PVARAN
199 #define PVARAN(s) \
200  #s << " = " << RAD2DEG(s) << "deg\n"
201 #endif
202 #ifndef PVARAC
203 #define PVARAC(s) \
204  #s << " = " << RAD2DEG(s) << "deg, " << std::flush
205 #endif
206 #ifndef PVARAS
207 #define PVARAS(s) \
208  #s << " = " << RAD2DEG(s) << "deg " << std::flush
209 #endif
210 
211 #define FIXED(s) \
212  std::fixed << s << std::resetiosflags(std::ios_base::fixed)
213 
214 #ifndef ERASE_STRUCT
215 #define ERASE_STRUCT(var) memset(&var, 0, sizeof(var))
216 #endif
217 
218 #ifndef ERASE_ARRAY
219 #define ERASE_ARRAY(var, size) memset(var, 0, size*sizeof(*var))
220 #endif
221 
222 #ifndef SET_ARRAY
223 #define SET_ARRAY(var, value, size) { for (decltype(size) i = 0; i < size; ++i) var[i]=value; }
224 #endif
225 
226 #ifndef PCL_EXTERN_C
227  #ifdef __cplusplus
228  #define PCL_EXTERN_C extern "C"
229  #else
230  #define PCL_EXTERN_C
231  #endif
232 #endif
233 
234 #if defined WIN32 || defined _WIN32 || defined WINCE || defined __MINGW32__
235  #ifdef PCLAPI_EXPORTS
236  #define PCL_EXPORTS __declspec(dllexport)
237  #else
238  #define PCL_EXPORTS
239  #endif
240 #else
241  #define PCL_EXPORTS
242 #endif
243 
244 #if defined WIN32 || defined _WIN32
245  #define PCL_CDECL __cdecl
246  #define PCL_STDCALL __stdcall
247 #else
248  #define PCL_CDECL
249  #define PCL_STDCALL
250 #endif
251 
252 #ifndef PCLAPI
253  #define PCLAPI(rettype) PCL_EXTERN_C PCL_EXPORTS rettype PCL_CDECL
254 #endif
255 
256 // Macro for pragma operator
257 #if (defined (__GNUC__) || defined(__clang__))
258  #define PCL_PRAGMA(x) _Pragma (#x)
259 #elif _MSC_VER
260  #define PCL_PRAGMA(x) __pragma (#x)
261 #else
262  #define PCL_PRAGMA
263 #endif
264 
265 // Macro for emitting pragma warning
266 #if (defined (__GNUC__) || defined(__clang__))
267  #define PCL_PRAGMA_WARNING(x) PCL_PRAGMA (GCC warning x)
268 #elif _MSC_VER
269  #define PCL_PRAGMA_WARNING(x) PCL_PRAGMA (warning (x))
270 #else
271  #define PCL_PRAGMA_WARNING
272 #endif
273 
274 //for clang cf. http://clang.llvm.org/docs/LanguageExtensions.html
275 #ifndef __has_extension
276  #define __has_extension(x) 0 // Compatibility with pre-3.0 compilers.
277 #endif
278 
279 #if defined (__GNUC__) || defined (__PGI) || defined (__IBMCPP__) || defined (__SUNPRO_CC)
280  #define PCL_ALIGN(alignment) __attribute__((aligned(alignment)))
281 #elif defined (_MSC_VER)
282  #define PCL_ALIGN(alignment) __declspec(align(alignment))
283 #else
284  #error Alignment not supported on your platform
285 #endif
286 
287 #if defined(__GLIBC__) && PCL_LINEAR_VERSION(__GLIBC__,__GLIBC_MINOR__,0)>PCL_LINEAR_VERSION(2,8,0)
288  #define GLIBC_MALLOC_ALIGNED 1
289 #else
290  #define GLIBC_MALLOC_ALIGNED 0
291 #endif
292 
293 #if defined(__FreeBSD__) && !defined(__arm__) && !defined(__mips__)
294  #define FREEBSD_MALLOC_ALIGNED 1
295 #else
296  #define FREEBSD_MALLOC_ALIGNED 0
297 #endif
298 
299 #if defined(__APPLE__) || defined(_WIN64) || GLIBC_MALLOC_ALIGNED || FREEBSD_MALLOC_ALIGNED
300  #define MALLOC_ALIGNED 1
301 #endif
302 
303 #if defined (HAVE_MM_MALLOC)
304  // Intel compiler defines an incompatible _mm_malloc signature
305  #if defined(__INTEL_COMPILER)
306  #include <malloc.h>
307  #else
308  #include <mm_malloc.h>
309  #endif
310 #endif
311 
312 inline void*
313 aligned_malloc (std::size_t size)
314 {
315  void *ptr;
316 #if defined (MALLOC_ALIGNED)
317  ptr = std::malloc (size);
318 #elif defined (HAVE_POSIX_MEMALIGN)
319  if (posix_memalign (&ptr, 16, size))
320  ptr = 0;
321 #elif defined (HAVE_MM_MALLOC)
322  ptr = _mm_malloc (size, 16);
323 #elif defined (_MSC_VER)
324  ptr = _aligned_malloc (size, 16);
325 #elif defined (ANDROID)
326  ptr = memalign (16, size);
327 #else
328  #error aligned_malloc not supported on your platform
329  ptr = 0;
330 #endif
331  return (ptr);
332 }
333 
334 inline void
335 aligned_free (void* ptr)
336 {
337 #if defined (MALLOC_ALIGNED) || defined (HAVE_POSIX_MEMALIGN)
338  std::free (ptr);
339 #elif defined (HAVE_MM_MALLOC)
340  _mm_free (ptr);
341 #elif defined (_MSC_VER)
342  _aligned_free (ptr);
343 #elif defined (ANDROID)
344  free (ptr);
345 #else
346  #error aligned_free not supported on your platform
347 #endif
348 }
349 
350 /**
351  * \brief Macro to signal a class requires a custom allocator
352  *
353  * It's an implementation detail to have pcl::has_custom_allocator work, a
354  * thin wrapper over Eigen's own macro
355  *
356  * \see pcl::has_custom_allocator, pcl::make_shared
357  * \ingroup common
358  */
359 #define PCL_MAKE_ALIGNED_OPERATOR_NEW \
360  EIGEN_MAKE_ALIGNED_OPERATOR_NEW \
361  using _custom_allocator_type_trait = void;
362 
363 /**
364  * \brief Macro to add a no-op or a fallthrough attribute based on compiler feature
365  *
366  * \ingroup common
367  */
368 #if __has_cpp_attribute(fallthrough) && !(defined(__clang__) && __cplusplus < 201703L)
369  #define PCL_FALLTHROUGH [[fallthrough]];
370 #elif defined(__clang__)
371  #define PCL_FALLTHROUGH [[clang::fallthrough]];
372 #elif defined(__GNUC__)
373  #if __GNUC__ >= 7
374  #define PCL_FALLTHROUGH [[gnu::fallthrough]];
375  #else
376  #define PCL_FALLTHROUGH ;
377  #endif
378 #else
379  #define PCL_FALLTHROUGH ;
380 #endif
381 
382 #if __has_cpp_attribute(nodiscard)
383  #define PCL_NODISCARD [[nodiscard]]
384 #else
385  #define PCL_NODISCARD
386 #endif
bool pcl_isinf(T &&x)
Definition: pcl_macros.h:134
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:154
bool pcl_isfinite(T &&x)
Definition: pcl_macros.h:130
bool pcl_isnan(T &&x)
Definition: pcl_macros.h:126
void aligned_free(void *ptr)
Definition: pcl_macros.h:335
void * aligned_malloc(std::size_t size)
Definition: pcl_macros.h:313