utility.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Third party copyrights are property of their respective owners.
  16. //
  17. // Redistribution and use in source and binary forms, with or without modification,
  18. // are permitted provided that the following conditions are met:
  19. //
  20. // * Redistribution's of source code must retain the above copyright notice,
  21. // this list of conditions and the following disclaimer.
  22. //
  23. // * Redistribution's in binary form must reproduce the above copyright notice,
  24. // this list of conditions and the following disclaimer in the documentation
  25. // and/or other materials provided with the distribution.
  26. //
  27. // * The name of the copyright holders may not be used to endorse or promote products
  28. // derived from this software without specific prior written permission.
  29. //
  30. // This software is provided by the copyright holders and contributors "as is" and
  31. // any express or implied warranties, including, but not limited to, the implied
  32. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  33. // In no event shall the Intel Corporation or contributors be liable for any direct,
  34. // indirect, incidental, special, exemplary, or consequential damages
  35. // (including, but not limited to, procurement of substitute goods or services;
  36. // loss of use, data, or profits; or business interruption) however caused
  37. // and on any theory of liability, whether in contract, strict liability,
  38. // or tort (including negligence or otherwise) arising in any way out of
  39. // the use of this software, even if advised of the possibility of such damage.
  40. //
  41. //M*/
  42. #ifndef OPENCV_CUDA_UTILITY_HPP
  43. #define OPENCV_CUDA_UTILITY_HPP
  44. #include "saturate_cast.hpp"
  45. #include "datamov_utils.hpp"
  46. /** @file
  47. * @deprecated Use @ref cudev instead.
  48. */
  49. //! @cond IGNORED
  50. namespace cv { namespace cuda { namespace device
  51. {
  52. struct CV_EXPORTS ThrustAllocator
  53. {
  54. typedef uchar value_type;
  55. virtual ~ThrustAllocator();
  56. virtual __device__ __host__ uchar* allocate(size_t numBytes) = 0;
  57. virtual __device__ __host__ void deallocate(uchar* ptr, size_t numBytes) = 0;
  58. static ThrustAllocator& getAllocator();
  59. static void setAllocator(ThrustAllocator* allocator);
  60. };
  61. #define OPENCV_CUDA_LOG_WARP_SIZE (5)
  62. #define OPENCV_CUDA_WARP_SIZE (1 << OPENCV_CUDA_LOG_WARP_SIZE)
  63. #define OPENCV_CUDA_LOG_MEM_BANKS ((__CUDA_ARCH__ >= 200) ? 5 : 4) // 32 banks on fermi, 16 on tesla
  64. #define OPENCV_CUDA_MEM_BANKS (1 << OPENCV_CUDA_LOG_MEM_BANKS)
  65. ///////////////////////////////////////////////////////////////////////////////
  66. // swap
  67. template <typename T> void __device__ __host__ __forceinline__ swap(T& a, T& b)
  68. {
  69. const T temp = a;
  70. a = b;
  71. b = temp;
  72. }
  73. ///////////////////////////////////////////////////////////////////////////////
  74. // Mask Reader
  75. struct SingleMask
  76. {
  77. explicit __host__ __device__ __forceinline__ SingleMask(PtrStepb mask_) : mask(mask_) {}
  78. __host__ __device__ __forceinline__ SingleMask(const SingleMask& mask_): mask(mask_.mask){}
  79. __device__ __forceinline__ bool operator()(int y, int x) const
  80. {
  81. return mask.ptr(y)[x] != 0;
  82. }
  83. PtrStepb mask;
  84. };
  85. struct SingleMaskChannels
  86. {
  87. __host__ __device__ __forceinline__ SingleMaskChannels(PtrStepb mask_, int channels_)
  88. : mask(mask_), channels(channels_) {}
  89. __host__ __device__ __forceinline__ SingleMaskChannels(const SingleMaskChannels& mask_)
  90. :mask(mask_.mask), channels(mask_.channels){}
  91. __device__ __forceinline__ bool operator()(int y, int x) const
  92. {
  93. return mask.ptr(y)[x / channels] != 0;
  94. }
  95. PtrStepb mask;
  96. int channels;
  97. };
  98. struct MaskCollection
  99. {
  100. explicit __host__ __device__ __forceinline__ MaskCollection(PtrStepb* maskCollection_)
  101. : maskCollection(maskCollection_) {}
  102. __device__ __forceinline__ MaskCollection(const MaskCollection& masks_)
  103. : maskCollection(masks_.maskCollection), curMask(masks_.curMask){}
  104. __device__ __forceinline__ void next()
  105. {
  106. curMask = *maskCollection++;
  107. }
  108. __device__ __forceinline__ void setMask(int z)
  109. {
  110. curMask = maskCollection[z];
  111. }
  112. __device__ __forceinline__ bool operator()(int y, int x) const
  113. {
  114. uchar val;
  115. return curMask.data == 0 || (ForceGlob<uchar>::Load(curMask.ptr(y), x, val), (val != 0));
  116. }
  117. const PtrStepb* maskCollection;
  118. PtrStepb curMask;
  119. };
  120. struct WithOutMask
  121. {
  122. __host__ __device__ __forceinline__ WithOutMask(){}
  123. __host__ __device__ __forceinline__ WithOutMask(const WithOutMask&){}
  124. __device__ __forceinline__ void next() const
  125. {
  126. }
  127. __device__ __forceinline__ void setMask(int) const
  128. {
  129. }
  130. __device__ __forceinline__ bool operator()(int, int) const
  131. {
  132. return true;
  133. }
  134. __device__ __forceinline__ bool operator()(int, int, int) const
  135. {
  136. return true;
  137. }
  138. static __device__ __forceinline__ bool check(int, int)
  139. {
  140. return true;
  141. }
  142. static __device__ __forceinline__ bool check(int, int, int)
  143. {
  144. return true;
  145. }
  146. };
  147. ///////////////////////////////////////////////////////////////////////////////
  148. // Solve linear system
  149. // solve 2x2 linear system Ax=b
  150. template <typename T> __device__ __forceinline__ bool solve2x2(const T A[2][2], const T b[2], T x[2])
  151. {
  152. T det = A[0][0] * A[1][1] - A[1][0] * A[0][1];
  153. if (det != 0)
  154. {
  155. double invdet = 1.0 / det;
  156. x[0] = saturate_cast<T>(invdet * (b[0] * A[1][1] - b[1] * A[0][1]));
  157. x[1] = saturate_cast<T>(invdet * (A[0][0] * b[1] - A[1][0] * b[0]));
  158. return true;
  159. }
  160. return false;
  161. }
  162. // solve 3x3 linear system Ax=b
  163. template <typename T> __device__ __forceinline__ bool solve3x3(const T A[3][3], const T b[3], T x[3])
  164. {
  165. T det = A[0][0] * (A[1][1] * A[2][2] - A[1][2] * A[2][1])
  166. - A[0][1] * (A[1][0] * A[2][2] - A[1][2] * A[2][0])
  167. + A[0][2] * (A[1][0] * A[2][1] - A[1][1] * A[2][0]);
  168. if (det != 0)
  169. {
  170. double invdet = 1.0 / det;
  171. x[0] = saturate_cast<T>(invdet *
  172. (b[0] * (A[1][1] * A[2][2] - A[1][2] * A[2][1]) -
  173. A[0][1] * (b[1] * A[2][2] - A[1][2] * b[2] ) +
  174. A[0][2] * (b[1] * A[2][1] - A[1][1] * b[2] )));
  175. x[1] = saturate_cast<T>(invdet *
  176. (A[0][0] * (b[1] * A[2][2] - A[1][2] * b[2] ) -
  177. b[0] * (A[1][0] * A[2][2] - A[1][2] * A[2][0]) +
  178. A[0][2] * (A[1][0] * b[2] - b[1] * A[2][0])));
  179. x[2] = saturate_cast<T>(invdet *
  180. (A[0][0] * (A[1][1] * b[2] - b[1] * A[2][1]) -
  181. A[0][1] * (A[1][0] * b[2] - b[1] * A[2][0]) +
  182. b[0] * (A[1][0] * A[2][1] - A[1][1] * A[2][0])));
  183. return true;
  184. }
  185. return false;
  186. }
  187. }}} // namespace cv { namespace cuda { namespace cudev
  188. //! @endcond
  189. #endif // OPENCV_CUDA_UTILITY_HPP