EstervQrCode 1.1.1
Library for qr code manipulation
utility.hpp
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 
43 #ifndef OPENCV_CUDA_UTILITY_HPP
44 #define OPENCV_CUDA_UTILITY_HPP
45 
46 #include "saturate_cast.hpp"
47 #include "datamov_utils.hpp"
48 
54 
55 namespace cv { namespace cuda { namespace device
56 {
57  struct CV_EXPORTS ThrustAllocator
58  {
59  typedef uchar value_type;
60  virtual ~ThrustAllocator();
61  virtual __device__ __host__ uchar* allocate(size_t numBytes) = 0;
62  virtual __device__ __host__ void deallocate(uchar* ptr, size_t numBytes) = 0;
63  static ThrustAllocator& getAllocator();
64  static void setAllocator(ThrustAllocator* allocator);
65  };
66  #define OPENCV_CUDA_LOG_WARP_SIZE (5)
67  #define OPENCV_CUDA_WARP_SIZE (1 << OPENCV_CUDA_LOG_WARP_SIZE)
68  #define OPENCV_CUDA_LOG_MEM_BANKS ((__CUDA_ARCH__ >= 200) ? 5 : 4) // 32 banks on fermi, 16 on tesla
69  #define OPENCV_CUDA_MEM_BANKS (1 << OPENCV_CUDA_LOG_MEM_BANKS)
70 
72  // swap
73 
74  template <typename T> void __device__ __host__ __forceinline__ swap(T& a, T& b)
75  {
76  const T temp = a;
77  a = b;
78  b = temp;
79  }
80 
82  // Mask Reader
83 
84  struct SingleMask
85  {
86  explicit __host__ __device__ __forceinline__ SingleMask(PtrStepb mask_) : mask(mask_) {}
87  __host__ __device__ __forceinline__ SingleMask(const SingleMask& mask_): mask(mask_.mask){}
88 
89  __device__ __forceinline__ bool operator()(int y, int x) const
90  {
91  return mask.ptr(y)[x] != 0;
92  }
93 
94  PtrStepb mask;
95  };
96 
97  struct SingleMaskChannels
98  {
99  __host__ __device__ __forceinline__ SingleMaskChannels(PtrStepb mask_, int channels_)
100  : mask(mask_), channels(channels_) {}
101  __host__ __device__ __forceinline__ SingleMaskChannels(const SingleMaskChannels& mask_)
102  :mask(mask_.mask), channels(mask_.channels){}
103 
104  __device__ __forceinline__ bool operator()(int y, int x) const
105  {
106  return mask.ptr(y)[x / channels] != 0;
107  }
108 
109  PtrStepb mask;
110  int channels;
111  };
112 
113  struct MaskCollection
114  {
115  explicit __host__ __device__ __forceinline__ MaskCollection(PtrStepb* maskCollection_)
116  : maskCollection(maskCollection_) {}
117 
118  __device__ __forceinline__ MaskCollection(const MaskCollection& masks_)
119  : maskCollection(masks_.maskCollection), curMask(masks_.curMask){}
120 
121  __device__ __forceinline__ void next()
122  {
123  curMask = *maskCollection++;
124  }
125  __device__ __forceinline__ void setMask(int z)
126  {
127  curMask = maskCollection[z];
128  }
129 
130  __device__ __forceinline__ bool operator()(int y, int x) const
131  {
132  uchar val;
133  return curMask.data == 0 || (ForceGlob<uchar>::Load(curMask.ptr(y), x, val), (val != 0));
134  }
135 
136  const PtrStepb* maskCollection;
137  PtrStepb curMask;
138  };
139 
140  struct WithOutMask
141  {
142  __host__ __device__ __forceinline__ WithOutMask(){}
143  __host__ __device__ __forceinline__ WithOutMask(const WithOutMask&){}
144 
145  __device__ __forceinline__ void next() const
146  {
147  }
148  __device__ __forceinline__ void setMask(int) const
149  {
150  }
151 
152  __device__ __forceinline__ bool operator()(int, int) const
153  {
154  return true;
155  }
156 
157  __device__ __forceinline__ bool operator()(int, int, int) const
158  {
159  return true;
160  }
161 
162  static __device__ __forceinline__ bool check(int, int)
163  {
164  return true;
165  }
166 
167  static __device__ __forceinline__ bool check(int, int, int)
168  {
169  return true;
170  }
171  };
172 
174  // Solve linear system
175 
176  // solve 2x2 linear system Ax=b
177  template <typename T> __device__ __forceinline__ bool solve2x2(const T A[2][2], const T b[2], T x[2])
178  {
179  T det = A[0][0] * A[1][1] - A[1][0] * A[0][1];
180 
181  if (det != 0)
182  {
183  double invdet = 1.0 / det;
184 
185  x[0] = saturate_cast<T>(invdet * (b[0] * A[1][1] - b[1] * A[0][1]));
186 
187  x[1] = saturate_cast<T>(invdet * (A[0][0] * b[1] - A[1][0] * b[0]));
188 
189  return true;
190  }
191 
192  return false;
193  }
194 
195  // solve 3x3 linear system Ax=b
196  template <typename T> __device__ __forceinline__ bool solve3x3(const T A[3][3], const T b[3], T x[3])
197  {
198  T det = A[0][0] * (A[1][1] * A[2][2] - A[1][2] * A[2][1])
199  - A[0][1] * (A[1][0] * A[2][2] - A[1][2] * A[2][0])
200  + A[0][2] * (A[1][0] * A[2][1] - A[1][1] * A[2][0]);
201 
202  if (det != 0)
203  {
204  double invdet = 1.0 / det;
205 
206  x[0] = saturate_cast<T>(invdet *
207  (b[0] * (A[1][1] * A[2][2] - A[1][2] * A[2][1]) -
208  A[0][1] * (b[1] * A[2][2] - A[1][2] * b[2] ) +
209  A[0][2] * (b[1] * A[2][1] - A[1][1] * b[2] )));
210 
211  x[1] = saturate_cast<T>(invdet *
212  (A[0][0] * (b[1] * A[2][2] - A[1][2] * b[2] ) -
213  b[0] * (A[1][0] * A[2][2] - A[1][2] * A[2][0]) +
214  A[0][2] * (A[1][0] * b[2] - b[1] * A[2][0])));
215 
216  x[2] = saturate_cast<T>(invdet *
217  (A[0][0] * (A[1][1] * b[2] - b[1] * A[2][1]) -
218  A[0][1] * (A[1][0] * b[2] - b[1] * A[2][0]) +
219  b[0] * (A[1][0] * A[2][1] - A[1][1] * A[2][0])));
220 
221  return true;
222  }
223 
224  return false;
225  }
226 }}} // namespace cv { namespace cuda { namespace cudev
227 
229 
230 #endif // OPENCV_CUDA_UTILITY_HPP
InputArrayOfArrays InputArrayOfArrays InputOutputArray InputOutputArray InputOutputArray InputOutputArray Size InputOutputArray InputOutputArray T
Definition: calib3d.hpp:1867
int int channels
Definition: core_c.h:100
Cv_iplAllocateImageData Cv_iplDeallocate deallocate
Definition: core_c.h:1964
const CvArr CvArr * x
Definition: core_c.h:1195
const CvArr * y
Definition: core_c.h:1187
unsigned char uchar
Definition: interface.h:51
#define CV_EXPORTS
Definition: cvdef.h:435
CvArr CvArr * temp
Definition: imgproc_c.h:329
CV_EXPORTS OutputArray int double double InputArray mask
Definition: imgproc.hpp:2132
"black box" representation of the file storage associated with a file on disk.
Definition: calib3d.hpp:441
static CV__DEBUG_NS_BEGIN void swap(MatExpr &a, MatExpr &b)
Definition: mat.inl.hpp:3409
T next(T... args)