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.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
45 #define icvCopyVector( src, dst, len ) memcpy( (dst), (src), (len)*sizeof((dst)[0]))
46 #define icvSetZero( dst, len ) memset( (dst), 0, (len)*sizeof((dst)[0]))
48 #define icvCopyVector_32f( src, len, dst ) memcpy((dst),(src),(len)*sizeof(float))
49 #define icvSetZero_32f( dst, cols, rows ) memset((dst),0,(rows)*(cols)*sizeof(float))
50 #define icvCopyVector_64d( src, len, dst ) memcpy((dst),(src),(len)*sizeof(double))
51 #define icvSetZero_64d( dst, cols, rows ) memset((dst),0,(rows)*(cols)*sizeof(double))
52 #define icvCopyMatrix_32f( src, w, h, dst ) memcpy((dst),(src),(w)*(h)*sizeof(float))
53 #define icvCopyMatrix_64d( src, w, h, dst ) memcpy((dst),(src),(w)*(h)*sizeof(double))
55 #define icvCreateVector_32f( len ) (float*)cvAlloc( (len)*sizeof(float))
56 #define icvCreateVector_64d( len ) (double*)cvAlloc( (len)*sizeof(double))
57 #define icvCreateMatrix_32f( w, h ) (float*)cvAlloc( (w)*(h)*sizeof(float))
58 #define icvCreateMatrix_64d( w, h ) (double*)cvAlloc( (w)*(h)*sizeof(double))
60 #define icvDeleteVector( vec ) cvFree( &(vec) )
61 #define icvDeleteMatrix icvDeleteVector
63 #define icvAddMatrix_32f( src1, src2, dst, w, h ) \
64 icvAddVector_32f( (src1), (src2), (dst), (w)*(h))
66 #define icvSubMatrix_32f( src1, src2, dst, w, h ) \
67 icvSubVector_32f( (src1), (src2), (dst), (w)*(h))
69 #define icvNormVector_32f( src, len ) \
70 sqrt(icvDotProduct_32f( src, src, len ))
72 #define icvNormVector_64d( src, len ) \
73 sqrt(icvDotProduct_64d( src, src, len ))
76 #define icvDeleteMatrix icvDeleteVector
78 #define icvCheckVector_64f( ptr, len )
79 #define icvCheckVector_32f( ptr, len )
81 CV_INLINE double icvSum_32f( const float* src, int len )
84 for( int i = 0; i < len; i++ ) s += src[i];
86 icvCheckVector_64f( &s, 1 );
91 CV_INLINE double icvDotProduct_32f( const float* src1, const float* src2, int len )
94 for( int i = 0; i < len; i++ ) s += src1[i]*src2[i];
96 icvCheckVector_64f( &s, 1 );
102 CV_INLINE double icvDotProduct_64f( const double* src1, const double* src2, int len )
105 for( int i = 0; i < len; i++ ) s += src1[i]*src2[i];
107 icvCheckVector_64f( &s, 1 );
113 CV_INLINE void icvMulVectors_32f( const float* src1, const float* src2,
114 float* dst, int len )
117 for( i = 0; i < len; i++ )
118 dst[i] = src1[i] * src2[i];
120 icvCheckVector_32f( dst, len );
123 CV_INLINE void icvMulVectors_64d( const double* src1, const double* src2,
124 double* dst, int len )
127 for( i = 0; i < len; i++ )
128 dst[i] = src1[i] * src2[i];
130 icvCheckVector_64f( dst, len );
134 CV_INLINE void icvAddVector_32f( const float* src1, const float* src2,
135 float* dst, int len )
138 for( i = 0; i < len; i++ )
139 dst[i] = src1[i] + src2[i];
141 icvCheckVector_32f( dst, len );
144 CV_INLINE void icvAddVector_64d( const double* src1, const double* src2,
145 double* dst, int len )
148 for( i = 0; i < len; i++ )
149 dst[i] = src1[i] + src2[i];
151 icvCheckVector_64f( dst, len );
155 CV_INLINE void icvSubVector_32f( const float* src1, const float* src2,
156 float* dst, int len )
159 for( i = 0; i < len; i++ )
160 dst[i] = src1[i] - src2[i];
162 icvCheckVector_32f( dst, len );
165 CV_INLINE void icvSubVector_64d( const double* src1, const double* src2,
166 double* dst, int len )
169 for( i = 0; i < len; i++ )
170 dst[i] = src1[i] - src2[i];
172 icvCheckVector_64f( dst, len );
176 #define icvAddMatrix_64d( src1, src2, dst, w, h ) \
177 icvAddVector_64d( (src1), (src2), (dst), (w)*(h))
179 #define icvSubMatrix_64d( src1, src2, dst, w, h ) \
180 icvSubVector_64d( (src1), (src2), (dst), (w)*(h))
183 CV_INLINE void icvSetIdentity_32f( float* dst, int w, int h )
185 int i, len = MIN( w, h );
186 icvSetZero_32f( dst, w, h );
187 for( i = 0; len--; i += w+1 )
192 CV_INLINE void icvSetIdentity_64d( double* dst, int w, int h )
194 int i, len = MIN( w, h );
195 icvSetZero_64d( dst, w, h );
196 for( i = 0; len--; i += w+1 )
201 CV_INLINE void icvTrace_32f( const float* src, int w, int h, float* trace )
203 int i, len = MIN( w, h );
205 for( i = 0; len--; i += w+1 )
209 icvCheckVector_64f( &sum, 1 );
213 CV_INLINE void icvTrace_64d( const double* src, int w, int h, double* trace )
215 int i, len = MIN( w, h );
217 for( i = 0; len--; i += w+1 )
221 icvCheckVector_64f( &sum, 1 );
225 CV_INLINE void icvScaleVector_32f( const float* src, float* dst,
226 int len, double scale )
229 for( i = 0; i < len; i++ )
230 dst[i] = (float)(src[i]*scale);
232 icvCheckVector_32f( dst, len );
236 CV_INLINE void icvScaleVector_64d( const double* src, double* dst,
237 int len, double scale )
240 for( i = 0; i < len; i++ )
241 dst[i] = src[i]*scale;
243 icvCheckVector_64f( dst, len );
247 CV_INLINE void icvTransposeMatrix_32f( const float* src, int w, int h, float* dst )
251 for( i = 0; i < w; i++ )
252 for( j = 0; j < h; j++ )
253 *dst++ = src[j*w + i];
255 icvCheckVector_32f( dst, w*h );
258 CV_INLINE void icvTransposeMatrix_64d( const double* src, int w, int h, double* dst )
262 for( i = 0; i < w; i++ )
263 for( j = 0; j < h; j++ )
264 *dst++ = src[j*w + i];
266 icvCheckVector_64f( dst, w*h );
269 CV_INLINE void icvDetMatrix3x3_64d( const double* mat, double* det )
271 #define m(y,x) mat[(y)*3 + (x)]
273 *det = m(0,0)*(m(1,1)*m(2,2) - m(1,2)*m(2,1)) -
274 m(0,1)*(m(1,0)*m(2,2) - m(1,2)*m(2,0)) +
275 m(0,2)*(m(1,0)*m(2,1) - m(1,1)*m(2,0));
279 icvCheckVector_64f( det, 1 );
283 CV_INLINE void icvMulMatrix_32f( const float* src1, int w1, int h1,
284 const float* src2, int w2, int h2,
295 for( i = 0; i < h1; i++, src1 += w1, dst += w2 )
296 for( j = 0; j < w2; j++ )
299 for( k = 0; k < w1; k++ )
300 s += src1[k]*src2[j + k*w2];
304 icvCheckVector_32f( dst, h1*w2 );
308 CV_INLINE void icvMulMatrix_64d( const double* src1, int w1, int h1,
309 const double* src2, int w2, int h2,
320 for( i = 0; i < h1; i++, src1 += w1, dst += w2 )
321 for( j = 0; j < w2; j++ )
324 for( k = 0; k < w1; k++ )
325 s += src1[k]*src2[j + k*w2];
329 icvCheckVector_64f( dst, h1*w2 );
333 #define icvTransformVector_32f( matr, src, dst, w, h ) \
334 icvMulMatrix_32f( matr, w, h, src, 1, w, dst )
336 #define icvTransformVector_64d( matr, src, dst, w, h ) \
337 icvMulMatrix_64d( matr, w, h, src, 1, w, dst )
340 #define icvScaleMatrix_32f( src, dst, w, h, scale ) \
341 icvScaleVector_32f( (src), (dst), (w)*(h), (scale) )
343 #define icvScaleMatrix_64d( src, dst, w, h, scale ) \
344 icvScaleVector_64d( (src), (dst), (w)*(h), (scale) )
346 #define icvDotProduct_64d icvDotProduct_64f
349 CV_INLINE void icvInvertMatrix_64d( double* A, int n, double* invA )
351 CvMat Am = cvMat( n, n, CV_64F, A );
352 CvMat invAm = cvMat( n, n, CV_64F, invA );
354 cvInvert( &Am, &invAm, CV_SVD );
357 CV_INLINE void icvMulTransMatrixR_64d( double* src, int width, int height, double* dst )
359 CvMat srcMat = cvMat( height, width, CV_64F, src );
360 CvMat dstMat = cvMat( width, width, CV_64F, dst );
362 cvMulTransposed( &srcMat, &dstMat, 1 );
365 CV_INLINE void icvMulTransMatrixL_64d( double* src, int width, int height, double* dst )
367 CvMat srcMat = cvMat( height, width, CV_64F, src );
368 CvMat dstMat = cvMat( height, height, CV_64F, dst );
370 cvMulTransposed( &srcMat, &dstMat, 0 );
373 CV_INLINE void icvMulTransMatrixR_32f( float* src, int width, int height, float* dst )
375 CvMat srcMat = cvMat( height, width, CV_32F, src );
376 CvMat dstMat = cvMat( width, width, CV_32F, dst );
378 cvMulTransposed( &srcMat, &dstMat, 1 );
381 CV_INLINE void icvMulTransMatrixL_32f( float* src, int width, int height, float* dst )
383 CvMat srcMat = cvMat( height, width, CV_32F, src );
384 CvMat dstMat = cvMat( height, height, CV_32F, dst );
386 cvMulTransposed( &srcMat, &dstMat, 0 );
389 CV_INLINE void icvCvt_32f_64d( const float* src, double* dst, int len )
392 for( i = 0; i < len; i++ )
396 CV_INLINE void icvCvt_64d_32f( const double* src, float* dst, int len )
399 for( i = 0; i < len; i++ )
400 dst[i] = (float)src[i];
403 #endif/*_CV_MATRIX_H_*/