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