Point Cloud Library (PCL)  1.8.0
NCV.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (C) 2009-2010, NVIDIA Corporation, all rights reserved.
6  * Third party copyrights are property of their respective owners.
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * 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  * * Neither the name of Willow Garage, Inc. nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * $Id: $
38  * Ported to PCL by Koen Buys : Attention Work in progress!
39  */
40 
41 #ifndef PCL_GPU_PEOPLE__NCV_HPP_
42 #define PCL_GPU_PEOPLE__NCV_HPP_
43 
44 #if (defined WIN32 || defined _WIN32 || defined WINCE) && defined CVAPI_EXPORTS
45  #define NCV_EXPORTS __declspec(dllexport)
46 #else
47  #define NCV_EXPORTS
48 #endif
49 
50 #ifdef _WIN32
51  #define WIN32_LEAN_AND_MEAN
52 #endif
53 
54 #include <cuda_runtime.h>
55 #include <sstream>
56 #include <iostream>
57 #include <pcl/console/print.h>
58 
59 //==============================================================================
60 //
61 // Compile-time assert functionality
62 //
63 //==============================================================================
64 
65 /**
66 * Compile-time assert namespace
67 */
68 namespace NcvCTprep
69 {
70  template <bool x>
72 
73  template <>
74  struct CT_ASSERT_FAILURE<true> {};
75 
76  template <int x>
77  struct assertTest{};
78 }
79 
80 #define NCV_CT_PREP_PASTE_AUX(a,b) a##b ///< Concatenation indirection macro
81 #define NCV_CT_PREP_PASTE(a,b) NCV_CT_PREP_PASTE_AUX(a, b) ///< Concatenation macro
82 
83 /**
84 * Performs compile-time assertion of a condition on the file scope
85 */
86 #define NCV_CT_ASSERT(X) \
87  typedef NcvCTprep::assertTest<sizeof(NcvCTprep::CT_ASSERT_FAILURE< (bool)(X) >)> \
88  NCV_CT_PREP_PASTE(__ct_assert_typedef_, __LINE__)
89 
90 //==============================================================================
91 //
92 // Alignment macros
93 //
94 //==============================================================================
95 
96 #if !defined(__align__) && !defined(__CUDACC__)
97  #if defined(_WIN32) || defined(_WIN64)
98  #define __align__(n) __declspec(align(n))
99  #elif defined(__unix__)
100  #define __align__(n) __attribute__((__aligned__(n)))
101  #endif
102 #endif
103 
104 //==============================================================================
105 //
106 // Integral and compound types of guaranteed size
107 //
108 //==============================================================================
109 
110 typedef bool NcvBool;
111 typedef long long Ncv64s;
112 
113 #if defined(__APPLE__) && !defined(__CUDACC__)
114  typedef uint64_t Ncv64u;
115 #else
116  typedef unsigned long long Ncv64u;
117 #endif
118 
119 typedef int Ncv32s;
120 typedef unsigned int Ncv32u;
121 typedef short Ncv16s;
122 typedef unsigned short Ncv16u;
123 typedef char Ncv8s;
124 typedef unsigned char Ncv8u;
125 typedef float Ncv32f;
126 typedef double Ncv64f;
127 
128 struct NcvRect8u
129 {
130  Ncv8u x;
131  Ncv8u y;
132  Ncv8u width;
133  Ncv8u height;
134  __host__ __device__ NcvRect8u() : x(0), y(0), width(0), height(0) {};
135  __host__ __device__ NcvRect8u(Ncv8u x, Ncv8u y, Ncv8u width, Ncv8u height) : x(x), y(y), width(width), height(height) {}
136 };
137 
139 {
140  Ncv32s x; ///< x-coordinate of upper left corner.
141  Ncv32s y; ///< y-coordinate of upper left corner.
142  Ncv32s width; ///< Rectangle width.
143  Ncv32s height; ///< Rectangle height.
144  __host__ __device__ NcvRect32s() : x(0), y(0), width(0), height(0) {};
145  __host__ __device__ NcvRect32s(Ncv32s x, Ncv32s y, Ncv32s width, Ncv32s height) : x(x), y(y), width(width), height(height) {}
146 };
147 
149 {
150  Ncv32u x; ///< x-coordinate of upper left corner.
151  Ncv32u y; ///< y-coordinate of upper left corner.
152  Ncv32u width; ///< Rectangle width.
153  Ncv32u height; ///< Rectangle height.
154  __host__ __device__ NcvRect32u() : x(0), y(0), width(0), height(0) {};
155  __host__ __device__ NcvRect32u(Ncv32u x, Ncv32u y, Ncv32u width, Ncv32u height) : x(x), y(y), width(width), height(height) {}
156 };
157 
159 {
160  Ncv32s width; ///< Rectangle width.
161  Ncv32s height; ///< Rectangle height.
162  __host__ __device__ NcvSize32s() : width(0), height(0) {};
163  __host__ __device__ NcvSize32s(Ncv32s width, Ncv32s height) : width(width), height(height) {}
164 };
165 
167 {
168  Ncv32u width; ///< Rectangle width.
169  Ncv32u height; ///< Rectangle height.
170  __host__ __device__ NcvSize32u() : width(0), height(0) {};
171  __host__ __device__ NcvSize32u(Ncv32u width, Ncv32u height) : width(width), height(height) {}
172  __host__ __device__ bool operator == (const NcvSize32u &another) const {return this->width == another.width && this->height == another.height;}
173 };
174 
176 {
177  Ncv32s x; ///< Point X.
178  Ncv32s y; ///< Point Y.
179  __host__ __device__ NcvPoint2D32s() : x(0), y(0) {};
180  __host__ __device__ NcvPoint2D32s(Ncv32s x, Ncv32s y) : x(x), y(y) {}
181 };
182 
184 {
185  Ncv32u x; ///< Point X.
186  Ncv32u y; ///< Point Y.
187  __host__ __device__ NcvPoint2D32u() : x(0), y(0) {};
188  __host__ __device__ NcvPoint2D32u(Ncv32u x, Ncv32u y) : x(x), y(y) {}
189 };
190 
191 NCV_CT_ASSERT(sizeof(NcvBool) <= 4);
192 NCV_CT_ASSERT(sizeof(Ncv64s) == 8);
193 NCV_CT_ASSERT(sizeof(Ncv64u) == 8);
194 NCV_CT_ASSERT(sizeof(Ncv32s) == 4);
195 NCV_CT_ASSERT(sizeof(Ncv32u) == 4);
196 NCV_CT_ASSERT(sizeof(Ncv16s) == 2);
197 NCV_CT_ASSERT(sizeof(Ncv16u) == 2);
198 NCV_CT_ASSERT(sizeof(Ncv8s) == 1);
199 NCV_CT_ASSERT(sizeof(Ncv8u) == 1);
200 NCV_CT_ASSERT(sizeof(Ncv32f) == 4);
201 NCV_CT_ASSERT(sizeof(Ncv64f) == 8);
202 NCV_CT_ASSERT(sizeof(NcvRect8u) == sizeof(Ncv32u));
203 NCV_CT_ASSERT(sizeof(NcvRect32s) == 4 * sizeof(Ncv32s));
204 NCV_CT_ASSERT(sizeof(NcvRect32u) == 4 * sizeof(Ncv32u));
205 NCV_CT_ASSERT(sizeof(NcvSize32u) == 2 * sizeof(Ncv32u));
206 NCV_CT_ASSERT(sizeof(NcvPoint2D32u) == 2 * sizeof(Ncv32u));
207 
208 
209 //==============================================================================
210 //
211 // Persistent constants
212 //
213 //==============================================================================
214 
215 const Ncv32u K_WARP_SIZE = 32;
216 const Ncv32u K_LOG2_WARP_SIZE = 5;
217 
218 //==============================================================================
219 //
220 // Error handling
221 //
222 //==============================================================================
223 
224 NCV_EXPORTS void ncvDebugOutput(const std::string &msg);
225 
226 typedef void NCVDebugOutputHandler(const std::string &msg);
227 
228 NCV_EXPORTS void ncvSetDebugOutputHandler(NCVDebugOutputHandler* func);
229 
230 #define ncvAssertPrintCheck(pred, msg) \
231  do \
232  { \
233  if (!(pred)) \
234  { \
235  std::ostringstream oss; \
236  oss << "NCV Assertion Failed: " << msg << ", file=" << __FILE__ << ", line=" << __LINE__ << std::endl; \
237  ncvDebugOutput(oss.str()); \
238  } \
239  } while (0)
240 
241 #define ncvAssertPrintReturn(pred, msg, err) \
242  do \
243  { \
244  ncvAssertPrintCheck(pred, msg); \
245  if (!(pred)) return err; \
246  } while (0)
247 
248 #define ncvAssertReturn(pred, err) \
249  ncvAssertPrintReturn(pred, "retcode=" << (int)err, err)
250 
251 #define ncvAssertReturnNcvStat(ncvOp) \
252  do \
253  { \
254  NCVStatus _ncvStat = ncvOp; \
255  ncvAssertPrintReturn(NCV_SUCCESS==_ncvStat, "NcvStat=" << (int)_ncvStat, _ncvStat); \
256  } while (0)
257 
258 #define ncvAssertCUDAReturn(cudacall, errCode) \
259  do \
260  { \
261  cudaError_t res = cudacall; \
262  ncvAssertPrintReturn(cudaSuccess==res, "cudaError_t=" << res, errCode); \
263  } while (0)
264 
265 #define ncvAssertCUDALastErrorReturn(errCode) \
266  do \
267  { \
268  cudaError_t res = cudaGetLastError(); \
269  ncvAssertPrintReturn(cudaSuccess==res, "cudaError_t=" << res, errCode); \
270  } while (0)
271 
272 /**
273 * \brief Return-codes for status notification, errors and warnings
274 */
275 enum
276 {
277  //NCV statuses
278  NCV_SUCCESS,
279  NCV_UNKNOWN_ERROR,
280 
281  NCV_CUDA_ERROR,
282  NCV_NPP_ERROR,
283  NCV_FILE_ERROR,
284 
285  NCV_NULL_PTR,
286  NCV_INCONSISTENT_INPUT,
287  NCV_TEXTURE_BIND_ERROR,
288  NCV_DIMENSIONS_INVALID,
289 
290  NCV_INVALID_ROI,
291  NCV_INVALID_STEP,
292  NCV_INVALID_SCALE,
293 
294  NCV_ALLOCATOR_NOT_INITIALIZED,
295  NCV_ALLOCATOR_BAD_ALLOC,
296  NCV_ALLOCATOR_BAD_DEALLOC,
297  NCV_ALLOCATOR_INSUFFICIENT_CAPACITY,
298  NCV_ALLOCATOR_DEALLOC_ORDER,
299  NCV_ALLOCATOR_BAD_REUSE,
300 
301  NCV_MEM_COPY_ERROR,
302  NCV_MEM_RESIDENCE_ERROR,
303  NCV_MEM_INSUFFICIENT_CAPACITY,
304 
305  NCV_HAAR_INVALID_PIXEL_STEP,
306  NCV_HAAR_TOO_MANY_FEATURES_IN_CLASSIFIER,
307  NCV_HAAR_TOO_MANY_FEATURES_IN_CASCADE,
308  NCV_HAAR_TOO_LARGE_FEATURES,
309  NCV_HAAR_XML_LOADING_EXCEPTION,
310 
311  NCV_NOIMPL_HAAR_TILTED_FEATURES,
312  NCV_NOT_IMPLEMENTED,
313 
314  NCV_WARNING_HAAR_DETECTIONS_VECTOR_OVERFLOW,
315 
316  //NPP statuses
317  NPPST_SUCCESS = NCV_SUCCESS, ///< Successful operation (same as NPP_NO_ERROR)
318  NPPST_ERROR, ///< Unknown error
319  NPPST_CUDA_KERNEL_EXECUTION_ERROR, ///< CUDA kernel execution error
320  NPPST_NULL_POINTER_ERROR, ///< NULL pointer argument error
321  NPPST_TEXTURE_BIND_ERROR, ///< CUDA texture binding error or non-zero offset returned
322  NPPST_MEMCPY_ERROR, ///< CUDA memory copy error
323  NPPST_MEM_ALLOC_ERR, ///< CUDA memory allocation error
324  NPPST_MEMFREE_ERR, ///< CUDA memory deallocation error
325 
326  //NPPST statuses
327  NPPST_INVALID_ROI, ///< Invalid region of interest argument
328  NPPST_INVALID_STEP, ///< Invalid image lines step argument (check sign, alignment, relation to image width)
329  NPPST_INVALID_SCALE, ///< Invalid scale parameter passed
330  NPPST_MEM_INSUFFICIENT_BUFFER, ///< Insufficient user-allocated buffer
331  NPPST_MEM_RESIDENCE_ERROR, ///< Memory residence error detected (check if pointers should be device or pinned)
332  NPPST_MEM_INTERNAL_ERROR, ///< Internal memory management error
333 
334  NCV_LAST_STATUS ///< Marker to continue error numeration in other files
335 };
336 
337 typedef Ncv32u NCVStatus;
338 
339 #define NCV_SET_SKIP_COND(x) \
340  bool __ncv_skip_cond = x
341 
342 #define NCV_RESET_SKIP_COND(x) \
343  __ncv_skip_cond = x
344 
345 #define NCV_SKIP_COND_BEGIN \
346  if (!__ncv_skip_cond) {
347 
348 #define NCV_SKIP_COND_END \
349  }
350 
351 
352 //==============================================================================
353 //
354 // Timer
355 //
356 //==============================================================================
357 
358 
359 typedef struct _NcvTimer *NcvTimer;
360 
361 NCV_EXPORTS NcvTimer ncvStartTimer(void);
362 
363 NCV_EXPORTS double ncvEndQueryTimerUs(NcvTimer t);
364 
365 NCV_EXPORTS double ncvEndQueryTimerMs(NcvTimer t);
366 
367 
368 //==============================================================================
369 //
370 // Memory management classes template compound types
371 //
372 //==============================================================================
373 
374 
375 /**
376 * Calculates the aligned top bound value
377 */
378 NCV_EXPORTS Ncv32u alignUp(Ncv32u what, Ncv32u alignment);
379 
380 
381 /**
382 * NCVMemoryType
383 */
384 enum NCVMemoryType
385 {
386  NCVMemoryTypeNone,
387  NCVMemoryTypeHostPageable,
388  NCVMemoryTypeHostPinned,
389  NCVMemoryTypeDevice
390 };
391 
392 
393 /**
394 * NCVMemPtr
395 */
396 struct NCV_EXPORTS NCVMemPtr
397 {
398  void *ptr;
399  NCVMemoryType memtype;
400  void clear();
401 };
402 
403 
404 /**
405 * NCVMemSegment
406 */
407 struct NCV_EXPORTS NCVMemSegment
408 {
410  size_t size;
411  void clear();
412 };
413 
414 
415 /**
416 * INCVMemAllocator (Interface)
417 */
418 class NCV_EXPORTS INCVMemAllocator
419 {
420 public:
421  virtual ~INCVMemAllocator() = 0;
422 
423  virtual NCVStatus alloc(NCVMemSegment &seg, size_t size) = 0;
424  virtual NCVStatus dealloc(NCVMemSegment &seg) = 0;
425 
426  virtual NcvBool isInitialized(void) const = 0;
427  virtual NcvBool isCounting(void) const = 0;
428 
429  virtual NCVMemoryType memType(void) const = 0;
430  virtual Ncv32u alignment(void) const = 0;
431  virtual size_t maxSize(void) const = 0;
432 };
433 
435 
436 
437 /**
438 * NCVMemStackAllocator
439 */
440 class NCV_EXPORTS NCVMemStackAllocator : public INCVMemAllocator
441 {
443  NCVMemStackAllocator(const NCVMemStackAllocator &);
444 
445 public:
446 
447  explicit NCVMemStackAllocator(Ncv32u alignment);
448  NCVMemStackAllocator(NCVMemoryType memT, size_t capacity, Ncv32u alignment, void *reusePtr=NULL);
449  virtual ~NCVMemStackAllocator();
450 
451  virtual NCVStatus alloc(NCVMemSegment &seg, size_t size);
452  virtual NCVStatus dealloc(NCVMemSegment &seg);
453 
454  virtual NcvBool isInitialized(void) const;
455  virtual NcvBool isCounting(void) const;
456 
457  virtual NCVMemoryType memType(void) const;
458  virtual Ncv32u alignment(void) const;
459  virtual size_t maxSize(void) const;
460 
461 private:
462 
463  NCVMemoryType _memType;
464  Ncv32u _alignment;
465  Ncv8u *allocBegin;
466  Ncv8u *begin;
467  Ncv8u *end;
468  size_t currentSize;
469  size_t _maxSize;
470  NcvBool bReusesMemory;
471 };
472 
473 
474 /**
475 * NCVMemNativeAllocator
476 */
477 class NCV_EXPORTS NCVMemNativeAllocator : public INCVMemAllocator
478 {
479 public:
480 
481  NCVMemNativeAllocator(NCVMemoryType memT, Ncv32u alignment);
482  virtual ~NCVMemNativeAllocator();
483 
484  virtual NCVStatus alloc(NCVMemSegment &seg, size_t size);
485  virtual NCVStatus dealloc(NCVMemSegment &seg);
486 
487  virtual NcvBool isInitialized(void) const;
488  virtual NcvBool isCounting(void) const;
489 
490  virtual NCVMemoryType memType(void) const;
491  virtual Ncv32u alignment(void) const;
492  virtual size_t maxSize(void) const;
493 
494 private:
495 
498 
499  NCVMemoryType _memType;
500  Ncv32u _alignment;
501  size_t currentSize;
502  size_t _maxSize;
503 };
504 
505 
506 /**
507 * Copy dispatchers
508 */
509 NCV_EXPORTS NCVStatus memSegCopyHelper(void *dst, NCVMemoryType dstType,
510  const void *src, NCVMemoryType srcType,
511  size_t sz, cudaStream_t cuStream);
512 
513 
514 NCV_EXPORTS NCVStatus memSegCopyHelper2D(void *dst, Ncv32u dstPitch, NCVMemoryType dstType,
515  const void *src, Ncv32u srcPitch, NCVMemoryType srcType,
516  Ncv32u widthbytes, Ncv32u height, cudaStream_t cuStream);
517 
518 
519 /**
520 * NCVVector (1D)
521 */
522 template <class T>
524 {
525  NCVVector(const NCVVector &);
526 
527 public:
528 
530  {
531  clear();
532  }
533 
534  virtual ~NCVVector() {}
535 
536  void clear()
537  {
538  _ptr = NULL;
539  _length = 0;
540  _memtype = NCVMemoryTypeNone;
541  }
542 
543  NCVStatus copySolid(NCVVector<T> &dst, cudaStream_t cuStream, size_t howMuch=0) const
544  {
545  if (howMuch == 0)
546  {
547  ncvAssertReturn(dst._length == this->_length, NCV_MEM_COPY_ERROR);
548  howMuch = this->_length * sizeof(T);
549  }
550  else
551  {
552  ncvAssertReturn(dst._length * sizeof(T) >= howMuch &&
553  this->_length * sizeof(T) >= howMuch &&
554  howMuch > 0, NCV_MEM_COPY_ERROR);
555  }
556  ncvAssertReturn((this->_ptr != NULL || this->_memtype == NCVMemoryTypeNone) &&
557  (dst._ptr != NULL || dst._memtype == NCVMemoryTypeNone), NCV_NULL_PTR);
558 
559  NCVStatus ncvStat = NCV_SUCCESS;
560  if (this->_memtype != NCVMemoryTypeNone)
561  {
562  ncvStat = memSegCopyHelper(dst._ptr, dst._memtype,
563  this->_ptr, this->_memtype,
564  howMuch, cuStream);
565  }
566 
567  return ncvStat;
568  }
569 
570  T *ptr() const {return this->_ptr;}
571  size_t length() const {return this->_length;}
572  NCVMemoryType memType() const {return this->_memtype;}
573 
574 protected:
575 
576  T *_ptr;
577  size_t _length;
578  NCVMemoryType _memtype;
579 };
580 
581 
582 /**
583 * NCVVectorAlloc
584 */
585 template <class T>
586 class NCVVectorAlloc : public NCVVector<T>
587 {
588  NCVVectorAlloc();
590  NCVVectorAlloc& operator=(const NCVVectorAlloc<T>&);
591 
592 public:
593 
594  NCVVectorAlloc(INCVMemAllocator &allocator, Ncv32u length)
595  :
596  allocator(allocator)
597  {
598  NCVStatus ncvStat;
599 
600  this->clear();
601  this->allocatedMem.clear();
602 
603  ncvStat = allocator.alloc(this->allocatedMem, length * sizeof(T));
604  ncvAssertPrintReturn(ncvStat == NCV_SUCCESS, "NCVVectorAlloc ctor:: alloc failed", );
605 
606  this->_ptr = (T *)this->allocatedMem.begin.ptr;
607  this->_length = length;
608  this->_memtype = this->allocatedMem.begin.memtype;
609  }
610 
612  {
613  NCVStatus ncvStat;
614 
615  ncvStat = allocator.dealloc(this->allocatedMem);
616  ncvAssertPrintCheck(ncvStat == NCV_SUCCESS, "NCVVectorAlloc dtor:: dealloc failed");
617 
618  this->clear();
619  }
620 
621  NcvBool isMemAllocated() const
622  {
623  return (this->allocatedMem.begin.ptr != NULL) || (this->allocator.isCounting());
624  }
625 
626  Ncv32u getAllocatorsAlignment() const
627  {
628  return allocator.alignment();
629  }
630 
632  {
633  return allocatedMem;
634  }
635 
636 private:
637  INCVMemAllocator &allocator;
638  NCVMemSegment allocatedMem;
639 };
640 
641 
642 /**
643 * NCVVectorReuse
644 */
645 template <class T>
646 class NCVVectorReuse : public NCVVector<T>
647 {
648  NCVVectorReuse();
650 
651 public:
652 
653  explicit NCVVectorReuse(const NCVMemSegment &memSegment)
654  {
655  this->bReused = false;
656  this->clear();
657 
658  this->_length = memSegment.size / sizeof(T);
659  this->_ptr = (T *)memSegment.begin.ptr;
660  this->_memtype = memSegment.begin.memtype;
661 
662  this->bReused = true;
663  }
664 
665  NCVVectorReuse(const NCVMemSegment &memSegment, Ncv32u length)
666  {
667  this->bReused = false;
668  this->clear();
669 
670  ncvAssertPrintReturn(length * sizeof(T) <= memSegment.size, \
671  "NCVVectorReuse ctor:: memory binding failed due to size mismatch", );
672 
673  this->_length = length;
674  this->_ptr = (T *)memSegment.begin.ptr;
675  this->_memtype = memSegment.begin.memtype;
676 
677  this->bReused = true;
678  }
679 
680  NcvBool isMemReused() const
681  {
682  return this->bReused;
683  }
684 
685 private:
686 
687  NcvBool bReused;
688 };
689 
690 
691 /**
692 * NCVMatrix (2D)
693 */
694 template <class T>
696 {
697  NCVMatrix(const NCVMatrix &);
698 
699 public:
700 
702  {
703  clear();
704  }
705 
706  virtual ~NCVMatrix() {}
707 
708  void clear()
709  {
710  _ptr = NULL;
711  _pitch = 0;
712  _width = 0;
713  _height = 0;
714  _memtype = NCVMemoryTypeNone;
715  }
716 
717  Ncv32u stride() const
718  {
719  return _pitch / sizeof(T);
720  }
721 
722  //a side effect of this function is that it copies everything in a single chunk, so the "padding" will be overwritten
723  NCVStatus copySolid(NCVMatrix<T> &dst, cudaStream_t cuStream, size_t howMuch=0) const
724  {
725  if (howMuch == 0)
726  {
727  ncvAssertReturn(dst._pitch == this->_pitch &&
728  dst._height == this->_height, NCV_MEM_COPY_ERROR);
729  howMuch = this->_pitch * this->_height;
730  }
731  else
732  {
733  ncvAssertReturn(dst._pitch * dst._height >= howMuch &&
734  this->_pitch * this->_height >= howMuch &&
735  howMuch > 0, NCV_MEM_COPY_ERROR);
736  }
737  ncvAssertReturn((this->_ptr != NULL || this->_memtype == NCVMemoryTypeNone) &&
738  (dst._ptr != NULL || dst._memtype == NCVMemoryTypeNone), NCV_NULL_PTR);
739 
740  NCVStatus ncvStat = NCV_SUCCESS;
741  if (this->_memtype != NCVMemoryTypeNone)
742  {
743  ncvStat = memSegCopyHelper(dst._ptr, dst._memtype,
744  this->_ptr, this->_memtype,
745  howMuch, cuStream);
746  }
747 
748  return ncvStat;
749  }
750 
751  NCVStatus copy2D(NCVMatrix<T> &dst, NcvSize32u roi, cudaStream_t cuStream) const
752  {
753  ncvAssertReturn(this->width() >= roi.width && this->height() >= roi.height &&
754  dst.width() >= roi.width && dst.height() >= roi.height, NCV_MEM_COPY_ERROR);
755  ncvAssertReturn((this->_ptr != NULL || this->_memtype == NCVMemoryTypeNone) &&
756  (dst._ptr != NULL || dst._memtype == NCVMemoryTypeNone), NCV_NULL_PTR);
757 
758  NCVStatus ncvStat = NCV_SUCCESS;
759  if (this->_memtype != NCVMemoryTypeNone)
760  {
761  ncvStat = memSegCopyHelper2D(dst._ptr, dst._pitch, dst._memtype,
762  this->_ptr, this->_pitch, this->_memtype,
763  roi.width * sizeof(T), roi.height, cuStream);
764  }
765 
766  return ncvStat;
767  }
768 
769  T &at(Ncv32u x, Ncv32u y) const
770  {
771  NcvBool bOutRange = (x >= this->_width || y >= this->_height);
772  ncvAssertPrintCheck(!bOutRange, "Error addressing matrix at [" << x << ", " << y << "]");
773  if (bOutRange)
774  {
775  return *this->_ptr;
776  }
777  return ((T *)((Ncv8u *)this->_ptr + y * this->_pitch))[x];
778  }
779 
780  T *ptr() const {return this->_ptr;}
781  Ncv32u width() const {return this->_width;}
782  Ncv32u height() const {return this->_height;}
783  NcvSize32u size() const {return NcvSize32u(this->_width, this->_height);}
784  Ncv32u pitch() const {return this->_pitch;}
785  NCVMemoryType memType() const {return this->_memtype;}
786 
787 protected:
788 
789  T *_ptr;
790  Ncv32u _width;
791  Ncv32u _height;
792  Ncv32u _pitch;
793  NCVMemoryType _memtype;
794 };
795 
796 
797 /**
798 * NCVMatrixAlloc
799 */
800 template <class T>
801 class NCVMatrixAlloc : public NCVMatrix<T>
802 {
803  NCVMatrixAlloc();
805  NCVMatrixAlloc& operator=(const NCVMatrixAlloc &);
806 public:
807 
808  NCVMatrixAlloc(INCVMemAllocator &allocator, Ncv32u width, Ncv32u height, Ncv32u pitch=0)
809  :
810  allocator(allocator)
811  {
812  NCVStatus ncvStat;
813 
814  this->clear();
815  this->allocatedMem.clear();
816 
817  Ncv32u widthBytes = width * sizeof(T);
818  Ncv32u pitchBytes = alignUp(widthBytes, allocator.alignment());
819 
820  if (pitch != 0)
821  {
822  ncvAssertPrintReturn(pitch >= pitchBytes &&
823  (pitch & (allocator.alignment() - 1)) == 0,
824  "NCVMatrixAlloc ctor:: incorrect pitch passed", );
825  pitchBytes = pitch;
826  }
827 
828  Ncv32u requiredAllocSize = pitchBytes * height;
829 
830  ncvStat = allocator.alloc(this->allocatedMem, requiredAllocSize);
831  ncvAssertPrintReturn(ncvStat == NCV_SUCCESS, "NCVMatrixAlloc ctor:: alloc failed", );
832 
833  this->_ptr = (T *)this->allocatedMem.begin.ptr;
834  this->_width = width;
835  this->_height = height;
836  this->_pitch = pitchBytes;
837  this->_memtype = this->allocatedMem.begin.memtype;
838  }
839 
841  {
842  NCVStatus ncvStat;
843 
844  ncvStat = allocator.dealloc(this->allocatedMem);
845  ncvAssertPrintCheck(ncvStat == NCV_SUCCESS, "NCVMatrixAlloc dtor:: dealloc failed");
846 
847  this->clear();
848  }
849 
850  NcvBool isMemAllocated() const
851  {
852  return (this->allocatedMem.begin.ptr != NULL) || (this->allocator.isCounting());
853  }
854 
855  Ncv32u getAllocatorsAlignment() const
856  {
857  return allocator.alignment();
858  }
859 
861  {
862  return allocatedMem;
863  }
864 
865 private:
866 
867  INCVMemAllocator &allocator;
868  NCVMemSegment allocatedMem;
869 };
870 
871 
872 /**
873 * NCVMatrixReuse
874 */
875 template <class T>
876 class NCVMatrixReuse : public NCVMatrix<T>
877 {
878  NCVMatrixReuse();
880 
881 public:
882 
883  NCVMatrixReuse(const NCVMemSegment &memSegment, Ncv32u alignment, Ncv32u width, Ncv32u height, Ncv32u pitch=0, NcvBool bSkipPitchCheck=false)
884  {
885  this->bReused = false;
886  this->clear();
887 
888  Ncv32u widthBytes = width * sizeof(T);
889  Ncv32u pitchBytes = alignUp(widthBytes, alignment);
890 
891  if (pitch != 0)
892  {
893  if (!bSkipPitchCheck)
894  {
895  ncvAssertPrintReturn(pitch >= pitchBytes &&
896  (pitch & (alignment - 1)) == 0,
897  "NCVMatrixReuse ctor:: incorrect pitch passed", );
898  }
899  else
900  {
901  ncvAssertPrintReturn(pitch >= widthBytes, "NCVMatrixReuse ctor:: incorrect pitch passed", );
902  }
903  pitchBytes = pitch;
904  }
905 
906  ncvAssertPrintReturn(pitchBytes * height <= memSegment.size, \
907  "NCVMatrixReuse ctor:: memory binding failed due to size mismatch", );
908 
909  this->_width = width;
910  this->_height = height;
911  this->_pitch = pitchBytes;
912  this->_ptr = (T *)memSegment.begin.ptr;
913  this->_memtype = memSegment.begin.memtype;
914 
915  this->bReused = true;
916  }
917 
919  {
920  this->bReused = false;
921  this->clear();
922 
923  ncvAssertPrintReturn(roi.x < mat.width() && roi.y < mat.height() && \
924  roi.x + roi.width <= mat.width() && roi.y + roi.height <= mat.height(),
925  "NCVMatrixReuse ctor:: memory binding failed due to mismatching ROI and source matrix dims", );
926 
927  this->_width = roi.width;
928  this->_height = roi.height;
929  this->_pitch = mat.pitch();
930  this->_ptr = &mat.at(roi.x, roi.y);
931  this->_memtype = mat.memType();
932 
933  this->bReused = true;
934  }
935 
936  NcvBool isMemReused() const
937  {
938  return this->bReused;
939  }
940 
941 private:
942 
943  NcvBool bReused;
944 };
945 
946 /**
947 * Operations with rectangles
948 */
949 NCV_EXPORTS NCVStatus ncvGroupRectangles_host(NCVVector<NcvRect32u> &hypotheses, Ncv32u &numHypotheses,
950  Ncv32u minNeighbors, Ncv32f intersectEps, NCVVector<Ncv32u> *hypothesesWeights);
951 
952 NCV_EXPORTS NCVStatus ncvDrawRects_8u_host(Ncv8u *h_dst, Ncv32u dstStride, Ncv32u dstWidth, Ncv32u dstHeight,
953  NcvRect32u *h_rects, Ncv32u numRects, Ncv8u color);
954 
955 NCV_EXPORTS NCVStatus ncvDrawRects_32u_host(Ncv32u *h_dst, Ncv32u dstStride, Ncv32u dstWidth, Ncv32u dstHeight,
956  NcvRect32u *h_rects, Ncv32u numRects, Ncv32u color);
957 
958 NCV_EXPORTS NCVStatus ncvDrawRects_8u_device(Ncv8u *d_dst, Ncv32u dstStride, Ncv32u dstWidth, Ncv32u dstHeight,
959  NcvRect32u *d_rects, Ncv32u numRects, Ncv8u color, cudaStream_t cuStream);
960 
961 NCV_EXPORTS NCVStatus ncvDrawRects_32u_device(Ncv32u *d_dst, Ncv32u dstStride, Ncv32u dstWidth, Ncv32u dstHeight,
962  NcvRect32u *d_rects, Ncv32u numRects, Ncv32u color, cudaStream_t cuStream);
963 
964 #define CLAMP(x,a,b) ( (x) > (b) ? (b) : ( (x) < (a) ? (a) : (x) ) )
965 #define CLAMP_TOP(x, a) (((x) > (a)) ? (a) : (x))
966 #define CLAMP_BOTTOM(x, a) (((x) < (a)) ? (a) : (x))
967 #define CLAMP_0_255(x) CLAMP(x,0,255)
968 
969 #define SUB_BEGIN(type, name) struct { __inline type name
970 #define SUB_END(name) } name;
971 #define SUB_CALL(name) name.name
972 
973 #define SQR(x) ((x)*(x))
974 
975 #define ncvSafeMatAlloc(name, type, alloc, width, height, err) \
976  NCVMatrixAlloc<type> name(alloc, width, height); \
977  ncvAssertReturn(name.isMemAllocated(), err);
978 
979 #endif // PCL_GPU_PEOPLE__NCV_HPP_
__host__ __device__ NcvRect32u()
Definition: NCV.hpp:154
NCVMatrixAlloc(INCVMemAllocator &allocator, Ncv32u width, Ncv32u height, Ncv32u pitch=0)
Definition: NCV.hpp:808
NcvBool isMemAllocated() const
Definition: NCV.hpp:621
__host__ __device__ NcvSize32s(Ncv32s width, Ncv32s height)
Definition: NCV.hpp:163
Ncv32s y
Point Y.
Definition: NCV.hpp:178
Ncv8u y
Definition: NCV.hpp:131
NcvBool isMemReused() const
Definition: NCV.hpp:680
NCVMemStackAllocator.
Definition: NCV.hpp:440
Compile-time assert namespace.
Definition: NCV.hpp:68
NCVVector()
Definition: NCV.hpp:529
NCVMatrixAlloc.
Definition: NCV.hpp:801
NCVMemoryType _memtype
Definition: NCV.hpp:793
Ncv32s height
Rectangle height.
Definition: NCV.hpp:143
virtual NCVStatus alloc(NCVMemSegment &seg, size_t size)=0
size_t length() const
Definition: NCV.hpp:571
T * _ptr
Definition: NCV.hpp:576
NcvBool isMemReused() const
Definition: NCV.hpp:936
NCVMemoryType _memtype
Definition: NCV.hpp:578
NCVMemoryType memType() const
Definition: NCV.hpp:785
Ncv32s width
Rectangle width.
Definition: NCV.hpp:160
NCVStatus copySolid(NCVVector< T > &dst, cudaStream_t cuStream, size_t howMuch=0) const
Definition: NCV.hpp:543
NCVVectorReuse(const NCVMemSegment &memSegment)
Definition: NCV.hpp:653
Ncv32u height
Rectangle height.
Definition: NCV.hpp:169
NCVMemSegment.
Definition: NCV.hpp:407
Ncv32u x
Point X.
Definition: NCV.hpp:185
Ncv8u width
Definition: NCV.hpp:132
NCVMemNativeAllocator.
Definition: NCV.hpp:477
Ncv32u getAllocatorsAlignment() const
Definition: NCV.hpp:626
Ncv32s width
Rectangle width.
Definition: NCV.hpp:142
T * ptr() const
Definition: NCV.hpp:780
T * _ptr
Definition: NCV.hpp:789
Ncv8u x
Definition: NCV.hpp:130
__host__ __device__ NcvPoint2D32s(Ncv32s x, Ncv32s y)
Definition: NCV.hpp:180
Ncv32u width
Rectangle width.
Definition: NCV.hpp:152
NcvSize32u size() const
Definition: NCV.hpp:783
NCVVectorAlloc.
Definition: NCV.hpp:586
Ncv32s x
Point X.
Definition: NCV.hpp:177
virtual ~NCVVector()
Definition: NCV.hpp:534
__host__ __device__ NcvSize32s()
Definition: NCV.hpp:162
NCVStatus copy2D(NCVMatrix< T > &dst, NcvSize32u roi, cudaStream_t cuStream) const
Definition: NCV.hpp:751
Ncv32u _height
Definition: NCV.hpp:791
NCVVector (1D)
Definition: NCV.hpp:523
NCVVectorReuse.
Definition: NCV.hpp:646
void * ptr
Definition: NCV.hpp:398
size_t _length
Definition: NCV.hpp:577
Ncv32u y
Point Y.
Definition: NCV.hpp:186
__host__ __device__ NcvPoint2D32u(Ncv32u x, Ncv32u y)
Definition: NCV.hpp:188
void clear()
Definition: NCV.hpp:536
NCVMatrixReuse(const NCVMemSegment &memSegment, Ncv32u alignment, Ncv32u width, Ncv32u height, Ncv32u pitch=0, NcvBool bSkipPitchCheck=false)
Definition: NCV.hpp:883
Ncv32u y
y-coordinate of upper left corner.
Definition: NCV.hpp:151
NCVVectorReuse(const NCVMemSegment &memSegment, Ncv32u length)
Definition: NCV.hpp:665
NCVMatrix()
Definition: NCV.hpp:701
Ncv8u height
Definition: NCV.hpp:133
__host__ __device__ NcvPoint2D32u()
Definition: NCV.hpp:187
Ncv32s height
Rectangle height.
Definition: NCV.hpp:161
__host__ __device__ NcvSize32u(Ncv32u width, Ncv32u height)
Definition: NCV.hpp:171
__host__ __device__ NcvPoint2D32s()
Definition: NCV.hpp:179
Ncv32u pitch() const
Definition: NCV.hpp:784
~NCVVectorAlloc()
Definition: NCV.hpp:611
T & at(Ncv32u x, Ncv32u y) const
Definition: NCV.hpp:769
NcvBool isMemAllocated() const
Definition: NCV.hpp:850
__host__ __device__ NcvSize32u()
Definition: NCV.hpp:170
NCVMemSegment getSegment() const
Definition: NCV.hpp:860
INCVMemAllocator (Interface)
Definition: NCV.hpp:418
NCVMemPtr.
Definition: NCV.hpp:396
Ncv32u height
Rectangle height.
Definition: NCV.hpp:153
NCVMemSegment getSegment() const
Definition: NCV.hpp:631
NCVVectorAlloc(INCVMemAllocator &allocator, Ncv32u length)
Definition: NCV.hpp:594
size_t size
Definition: NCV.hpp:410
void clear()
Definition: NCV.hpp:708
Ncv32u height() const
Definition: NCV.hpp:782
virtual ~INCVMemAllocator()=0
Definition: NCV.hpp:434
T * ptr() const
Definition: NCV.hpp:570
__host__ __device__ NcvRect32s()
Definition: NCV.hpp:144
NCVMemoryType memType() const
Definition: NCV.hpp:572
~NCVMatrixAlloc()
Definition: NCV.hpp:840
NCVMemoryType memtype
Definition: NCV.hpp:399
Ncv32s y
y-coordinate of upper left corner.
Definition: NCV.hpp:141
virtual Ncv32u alignment(void) const =0
NCVMatrix (2D)
Definition: NCV.hpp:695
__host__ __device__ NcvRect8u(Ncv8u x, Ncv8u y, Ncv8u width, Ncv8u height)
Definition: NCV.hpp:135
Ncv32u x
x-coordinate of upper left corner.
Definition: NCV.hpp:150
Ncv32u width() const
Definition: NCV.hpp:781
Ncv32u width
Rectangle width.
Definition: NCV.hpp:168
Ncv32s x
x-coordinate of upper left corner.
Definition: NCV.hpp:140
NCVMatrixReuse.
Definition: NCV.hpp:876
__host__ __device__ NcvRect8u()
Definition: NCV.hpp:134
virtual ~NCVMatrix()
Definition: NCV.hpp:706
Ncv32u _pitch
Definition: NCV.hpp:792
Ncv32u stride() const
Definition: NCV.hpp:717
__host__ __device__ NcvRect32s(Ncv32s x, Ncv32s y, Ncv32s width, Ncv32s height)
Definition: NCV.hpp:145
Ncv32u getAllocatorsAlignment() const
Definition: NCV.hpp:855
Ncv32u _width
Definition: NCV.hpp:790
NCVMemPtr begin
Definition: NCV.hpp:409
NCVStatus copySolid(NCVMatrix< T > &dst, cudaStream_t cuStream, size_t howMuch=0) const
Definition: NCV.hpp:723
__host__ __device__ NcvRect32u(Ncv32u x, Ncv32u y, Ncv32u width, Ncv32u height)
Definition: NCV.hpp:155
NCVMatrixReuse(const NCVMatrix< T > &mat, NcvRect32u roi)
Definition: NCV.hpp:918