Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 Willow Garage, Inc. 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 #ifndef PCL_MACROS_H_
38 #define PCL_MACROS_H_
39 
40 #include <pcl/pcl_config.h>
41 #include <boost/cstdint.hpp>
42 #include <cstdlib>
43 
44 namespace pcl
45 {
46  using boost::uint8_t;
47  using boost::int8_t;
48  using boost::int16_t;
49  using boost::uint16_t;
50  using boost::int32_t;
51  using boost::uint32_t;
52  using boost::int64_t;
53  using boost::uint64_t;
54 }
55 
56 #if defined __INTEL_COMPILER
57  #pragma warning disable 2196 2536 279
58 #endif
59 
60 #if defined _MSC_VER
61  #pragma warning (disable: 4521 4251)
62 #endif
63 
64 #include <iostream>
65 #include <stdarg.h>
66 #include <stdio.h>
67 #define _USE_MATH_DEFINES
68 #include <math.h>
69 
70 // MSCV doesn't have std::{isnan,isfinite}
71 #if defined _WIN32 && defined _MSC_VER
72 
73 // If M_PI is not defined, then probably all of them are undefined
74 #ifndef M_PI
75 // Copied from math.h
76 # define M_PI 3.14159265358979323846 // pi
77 # define M_PI_2 1.57079632679489661923 // pi/2
78 # define M_PI_4 0.78539816339744830962 // pi/4
79 # define M_PIl 3.1415926535897932384626433832795029L // pi
80 # define M_PI_2l 1.5707963267948966192313216916397514L // pi/2
81 # define M_PI_4l 0.7853981633974483096156608458198757L // pi/4
82 #endif
83 
84 // Stupid. This should be removed when all the PCL dependencies have min/max fixed.
85 #ifndef NOMINMAX
86 # define NOMINMAX
87 #endif
88 
89 # define pcl_isnan(x) _isnan(x)
90 # define pcl_isfinite(x) (_finite(x) != 0)
91 # define pcl_isinf(x) (_finite(x) == 0)
92 
93 # define __PRETTY_FUNCTION__ __FUNCTION__
94 # define __func__ __FUNCTION__
95 
96 #elif ANDROID
97 // Use the math.h macros
98 # include <math.h>
99 # define pcl_isnan(x) isnan(x)
100 # define pcl_isfinite(x) isfinite(x)
101 # define pcl_isinf(x) isinf(x)
102 
103 #elif _GLIBCXX_USE_C99_MATH
104 // Are the C++ cmath functions enabled?
105 # include <cmath>
106 # define pcl_isnan(x) std::isnan(x)
107 # define pcl_isfinite(x) std::isfinite(x)
108 # define pcl_isinf(x) std::isinf(x)
109 
110 #elif __PATHCC__
111 # include <cmath>
112 # include <stdio.h>
113 template <typename T> int
114 pcl_isnan (T &val)
115 {
116  return (val != val);
117 }
118 //# define pcl_isnan(x) std::isnan(x)
119 # define pcl_isfinite(x) std::isfinite(x)
120 # define pcl_isinf(x) std::isinf(x)
121 
122 #else
123 // Use the math.h macros
124 # include <math.h>
125 # define pcl_isnan(x) isnan(x)
126 # define pcl_isfinite(x) isfinite(x)
127 # define pcl_isinf(x) isinf(x)
128 
129 #endif
130 
131 #ifndef DEG2RAD
132 #define DEG2RAD(x) ((x)*0.017453293)
133 #endif
134 
135 #ifndef RAD2DEG
136 #define RAD2DEG(x) ((x)*57.29578)
137 #endif
138 
143 __inline double
144 pcl_round (double number)
145 {
146  return (number < 0.0 ? ceil (number - 0.5) : floor (number + 0.5));
147 }
148 __inline float
149 pcl_round (float number)
150 {
151  return (number < 0.0f ? ceilf (number - 0.5f) : floorf (number + 0.5f));
152 }
153 
154 #define pcl_lrint(x) (static_cast<long int>(pcl_round(x)))
155 #define pcl_lrintf(x) (static_cast<long int>(pcl_round(x)))
156 
157 #ifdef _WIN32
158 __inline float
159 log2f (float x)
160 {
161  return (static_cast<float> (logf (x) * M_LOG2E));
162 }
163 #endif
164 
165 #ifdef WIN32
166 #define pcl_sleep(x) Sleep(1000*(x))
167 #else
168 #define pcl_sleep(x) sleep(x)
169 #endif
170 
171 #ifndef PVAR
172  #define PVAR(s) \
173  #s << " = " << (s) << std::flush
174 #endif
175 #ifndef PVARN
176 #define PVARN(s) \
177  #s << " = " << (s) << "\n"
178 #endif
179 #ifndef PVARC
180 #define PVARC(s) \
181  #s << " = " << (s) << ", " << std::flush
182 #endif
183 #ifndef PVARS
184 #define PVARS(s) \
185  #s << " = " << (s) << " " << std::flush
186 #endif
187 #ifndef PVARA
188 #define PVARA(s) \
189  #s << " = " << RAD2DEG(s) << "deg" << std::flush
190 #endif
191 #ifndef PVARAN
192 #define PVARAN(s) \
193  #s << " = " << RAD2DEG(s) << "deg\n"
194 #endif
195 #ifndef PVARAC
196 #define PVARAC(s) \
197  #s << " = " << RAD2DEG(s) << "deg, " << std::flush
198 #endif
199 #ifndef PVARAS
200 #define PVARAS(s) \
201  #s << " = " << RAD2DEG(s) << "deg " << std::flush
202 #endif
203 
204 #define FIXED(s) \
205  std::fixed << s << std::resetiosflags(std::ios_base::fixed)
206 
207 #ifndef ERASE_STRUCT
208 #define ERASE_STRUCT(var) memset(&var, 0, sizeof(var))
209 #endif
210 
211 #ifndef ERASE_ARRAY
212 #define ERASE_ARRAY(var, size) memset(var, 0, size*sizeof(*var))
213 #endif
214 
215 #ifndef SET_ARRAY
216 #define SET_ARRAY(var, value, size) { for (int i = 0; i < static_cast<int> (size); ++i) var[i]=value; }
217 #endif
218 
219 /* //This is copy/paste from http://gcc.gnu.org/wiki/Visibility */
220 /* #if defined _WIN32 || defined __CYGWIN__ */
221 /* #ifdef BUILDING_DLL */
222 /* #ifdef __GNUC__ */
223 /* #define DLL_PUBLIC __attribute__((dllexport)) */
224 /* #else */
225 /* #define DLL_PUBLIC __declspec(dllexport) // Note: actually gcc seems to also supports this syntax. */
226 /* #endif */
227 /* #else */
228 /* #ifdef __GNUC__ */
229 /* #define DLL_PUBLIC __attribute__((dllimport)) */
230 /* #else */
231 /* #define DLL_PUBLIC __declspec(dllimport) // Note: actually gcc seems to also supports this syntax. */
232 /* #endif */
233 /* #endif */
234 /* #define DLL_LOCAL */
235 /* #else */
236 /* #if __GNUC__ >= 4 */
237 /* #define DLL_PUBLIC __attribute__ ((visibility("default"))) */
238 /* #define DLL_LOCAL __attribute__ ((visibility("hidden"))) */
239 /* #else */
240 /* #define DLL_PUBLIC */
241 /* #define DLL_LOCAL */
242 /* #endif */
243 /* #endif */
244 
245 #ifndef PCL_EXTERN_C
246  #ifdef __cplusplus
247  #define PCL_EXTERN_C extern "C"
248  #else
249  #define PCL_EXTERN_C
250  #endif
251 #endif
252 
253 #if defined WIN32 || defined _WIN32 || defined WINCE || defined __MINGW32__
254  #ifdef PCLAPI_EXPORTS
255  #define PCL_EXPORTS __declspec(dllexport)
256  #else
257  #define PCL_EXPORTS
258  #endif
259 #else
260  #define PCL_EXPORTS
261 #endif
262 
263 #if defined WIN32 || defined _WIN32
264  #define PCL_CDECL __cdecl
265  #define PCL_STDCALL __stdcall
266 #else
267  #define PCL_CDECL
268  #define PCL_STDCALL
269 #endif
270 
271 #ifndef PCLAPI
272  #define PCLAPI(rettype) PCL_EXTERN_C PCL_EXPORTS rettype PCL_CDECL
273 #endif
274 
275 // Macro to deprecate old functions
276 //
277 // Usage:
278 // don't use me any more
279 // PCL_DEPRECATED(void OldFunc(int a, float b), "Use newFunc instead, this functions will be gone in the next major release");
280 // use me instead
281 // void NewFunc(int a, double b);
282 
283 // gcc supports this starting from 4.5 : http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43666
284 #ifdef __GNUC__
285 #define GCC_VERSION (__GNUC__ * 10000 \
286  + __GNUC_MINOR__ * 100 \
287  + __GNUC_PATCHLEVEL__)
288 #if GCC_VERSION < 40500
289 #define PCL_DEPRECATED(func, message) func __attribute__ ((deprecated))
290 #else
291 #define PCL_DEPRECATED(func, message) func __attribute__ ((deprecated(message)))
292 #endif
293 
294 #elif defined(_MSC_VER)
295 #define PCL_DEPRECATED(func, message) __declspec(deprecated(message)) func
296 #else
297 #pragma message("WARNING: You need to implement PCL_DEPRECATED for this compiler")
298 #define PCL_DEPRECATED(func) func
299 #endif
300 
301 // Macro to deprecate old classes/structs
302 //
303 // Usage:
304 // don't use me any more
305 // class PCL_DEPRECATED_CLASS(OldClass, "Use newClass instead, this class will be gone in the next major release")
306 // {
307 // public:
308 // OldClass() {}
309 // };
310 // use me instead
311 // class NewFunc
312 // {
313 // public:
314 // NewClass() {}
315 // };
316 
317 // gcc supports this starting from 4.5 : http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43666
318 #ifdef __GNUC__
319 #define GCC_VERSION (__GNUC__ * 10000 \
320  + __GNUC_MINOR__ * 100 \
321  + __GNUC_PATCHLEVEL__)
322 #if GCC_VERSION < 40500
323 #define PCL_DEPRECATED_CLASS(func, message) __attribute__ ((deprecated)) func
324 #else
325 #define PCL_DEPRECATED_CLASS(func, message) __attribute__ ((deprecated(message))) func
326 #endif
327 
328 #elif defined(_MSC_VER)
329 #define PCL_DEPRECATED_CLASS(func, message) __declspec(deprecated(message)) func
330 #else
331 #pragma message("WARNING: You need to implement PCL_DEPRECATED for this compiler")
332 #define PCL_DEPRECATED_CLASS(func) func
333 #endif
334 
335 #if defined (__GNUC__) || defined (__PGI) || defined (__IBMCPP__) || defined (__SUNPRO_CC)
336  #define PCL_ALIGN(alignment) __attribute__((aligned(alignment)))
337 #elif defined (_MSC_VER)
338  #define PCL_ALIGN(alignment) __declspec(align(alignment))
339 #else
340  #error Alignment not supported on your platform
341 #endif
342 
343 #if defined(__GLIBC__) && ((__GLIBC__>=2 && __GLIBC_MINOR__ >= 8) || __GLIBC__>2) \
344  && defined(__LP64__)
345  #define GLIBC_MALLOC_ALIGNED 1
346 #else
347  #define GLIBC_MALLOC_ALIGNED 0
348 #endif
349 
350 #if defined(__FreeBSD__) && !defined(__arm__) && !defined(__mips__)
351  #define FREEBSD_MALLOC_ALIGNED 1
352 #else
353  #define FREEBSD_MALLOC_ALIGNED 0
354 #endif
355 
356 #if defined(__APPLE__) || defined(_WIN64) || GLIBC_MALLOC_ALIGNED || FREEBSD_MALLOC_ALIGNED
357  #define MALLOC_ALIGNED 1
358 #else
359  #define MALLOC_ALIGNED 0
360 #endif
361 
362 inline void*
363 aligned_malloc (size_t size)
364 {
365  void *ptr;
366 #if defined (MALLOC_ALIGNED)
367  ptr = std::malloc (size);
368 #elif defined (HAVE_POSIX_MEMALIGN)
369  if (posix_memalign (&ptr, 16, size))
370  ptr = 0;
371 #elif defined (HAVE_MM_MALLOC)
372  ptr = _mm_malloc (size, 16);
373 #elif defined (_MSC_VER)
374  ptr = _aligned_malloc (size, 16);
375 #else
376  #error aligned_malloc not supported on your platform
377  ptr = 0;
378 #endif
379  return (ptr);
380 }
381 
382 inline void
383 aligned_free (void* ptr)
384 {
385 #if defined (MALLOC_ALIGNED) || defined (HAVE_POSIX_MEMALIGN)
386  std::free (ptr);
387 #elif defined (HAVE_MM_MALLOC)
388  ptr = _mm_free (ptr);
389 #elif defined (_MSC_VER)
390  _aligned_free (ptr);
391 #else
392  #error aligned_free not supported on your platform
393 #endif
394 }
395 
396 #endif //#ifndef PCL_MACROS_H_