1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
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.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
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.
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.
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.
43 #if !defined CUDA_DISABLER
45 #include "internal_shared.hpp"
47 namespace cv { namespace gpu { namespace device
51 //////////////////////////////////////////////////////////////////////////////////////
56 static __device__ __forceinline__ void calc(int, int, float, float, float*, size_t, float)
62 static __device__ __forceinline__ void calc(int x, int y, float x_data, float y_data, float* dst, size_t dst_step, float)
64 dst[y * dst_step + x] = ::sqrtf(x_data * x_data + y_data * y_data);
69 static __device__ __forceinline__ void calc(int x, int y, float x_data, float y_data, float* dst, size_t dst_step, float)
71 dst[y * dst_step + x] = x_data * x_data + y_data * y_data;
76 static __device__ __forceinline__ void calc(int x, int y, float x_data, float y_data, float* dst, size_t dst_step, float scale)
78 float angle = ::atan2f(y_data, x_data);
79 angle += (angle < 0) * 2.0f * CV_PI_F;
80 dst[y * dst_step + x] = scale * angle;
83 template <typename Mag, typename Angle>
84 __global__ void cartToPolar(const float* xptr, size_t x_step, const float* yptr, size_t y_step,
85 float* mag, size_t mag_step, float* angle, size_t angle_step, float scale, int width, int height)
87 const int x = blockDim.x * blockIdx.x + threadIdx.x;
88 const int y = blockDim.y * blockIdx.y + threadIdx.y;
90 if (x < width && y < height)
92 float x_data = xptr[y * x_step + x];
93 float y_data = yptr[y * y_step + x];
95 Mag::calc(x, y, x_data, y_data, mag, mag_step, scale);
96 Angle::calc(x, y, x_data, y_data, angle, angle_step, scale);
102 static __device__ __forceinline__ float get(const float* mag, size_t mag_step, int x, int y)
104 return mag[y * mag_step + x];
109 static __device__ __forceinline__ float get(const float*, size_t, int, int)
114 template <typename Mag>
115 __global__ void polarToCart(const float* mag, size_t mag_step, const float* angle, size_t angle_step, float scale,
116 float* xptr, size_t x_step, float* yptr, size_t y_step, int width, int height)
118 const int x = blockDim.x * blockIdx.x + threadIdx.x;
119 const int y = blockDim.y * blockIdx.y + threadIdx.y;
121 if (x < width && y < height)
123 float mag_data = Mag::get(mag, mag_step, x, y);
124 float angle_data = angle[y * angle_step + x];
127 ::sincosf(scale * angle_data, &sin_a, &cos_a);
129 xptr[y * x_step + x] = mag_data * cos_a;
130 yptr[y * y_step + x] = mag_data * sin_a;
134 template <typename Mag, typename Angle>
135 void cartToPolar_caller(PtrStepSzf x, PtrStepSzf y, PtrStepSzf mag, PtrStepSzf angle, bool angleInDegrees, cudaStream_t stream)
137 dim3 threads(32, 8, 1);
140 grid.x = divUp(x.cols, threads.x);
141 grid.y = divUp(x.rows, threads.y);
143 const float scale = angleInDegrees ? (180.0f / CV_PI_F) : 1.f;
145 cartToPolar<Mag, Angle><<<grid, threads, 0, stream>>>(
146 x.data, x.step/x.elemSize(), y.data, y.step/y.elemSize(),
147 mag.data, mag.step/mag.elemSize(), angle.data, angle.step/angle.elemSize(), scale, x.cols, x.rows);
148 cudaSafeCall( cudaGetLastError() );
151 cudaSafeCall( cudaDeviceSynchronize() );
154 void cartToPolar_gpu(PtrStepSzf x, PtrStepSzf y, PtrStepSzf mag, bool magSqr, PtrStepSzf angle, bool angleInDegrees, cudaStream_t stream)
156 typedef void (*caller_t)(PtrStepSzf x, PtrStepSzf y, PtrStepSzf mag, PtrStepSzf angle, bool angleInDegrees, cudaStream_t stream);
157 static const caller_t callers[2][2][2] =
161 cartToPolar_caller<Magnitude, Atan2>,
162 cartToPolar_caller<Magnitude, Nothing>
165 cartToPolar_caller<MagnitudeSqr, Atan2>,
166 cartToPolar_caller<MagnitudeSqr, Nothing>,
171 cartToPolar_caller<Nothing, Atan2>,
172 cartToPolar_caller<Nothing, Nothing>
175 cartToPolar_caller<Nothing, Atan2>,
176 cartToPolar_caller<Nothing, Nothing>,
181 callers[mag.data == 0][magSqr][angle.data == 0](x, y, mag, angle, angleInDegrees, stream);
184 template <typename Mag>
185 void polarToCart_caller(PtrStepSzf mag, PtrStepSzf angle, PtrStepSzf x, PtrStepSzf y, bool angleInDegrees, cudaStream_t stream)
187 dim3 threads(32, 8, 1);
190 grid.x = divUp(mag.cols, threads.x);
191 grid.y = divUp(mag.rows, threads.y);
193 const float scale = angleInDegrees ? (CV_PI_F / 180.0f) : 1.0f;
195 polarToCart<Mag><<<grid, threads, 0, stream>>>(mag.data, mag.step/mag.elemSize(),
196 angle.data, angle.step/angle.elemSize(), scale, x.data, x.step/x.elemSize(), y.data, y.step/y.elemSize(), mag.cols, mag.rows);
197 cudaSafeCall( cudaGetLastError() );
200 cudaSafeCall( cudaDeviceSynchronize() );
203 void polarToCart_gpu(PtrStepSzf mag, PtrStepSzf angle, PtrStepSzf x, PtrStepSzf y, bool angleInDegrees, cudaStream_t stream)
205 typedef void (*caller_t)(PtrStepSzf mag, PtrStepSzf angle, PtrStepSzf x, PtrStepSzf y, bool angleInDegrees, cudaStream_t stream);
206 static const caller_t callers[2] =
208 polarToCart_caller<NonEmptyMag>,
209 polarToCart_caller<EmptyMag>
212 callers[mag.data == 0](mag, angle, x, y, angleInDegrees, stream);
214 } // namespace mathfunc
215 }}} // namespace cv { namespace gpu { namespace device
217 #endif /* CUDA_DISABLER */