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 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
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.
25 //
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.
28 //
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.
39 //
40 //M*/
41 
42 #ifndef _CV_MATRIX_H_
43 #define _CV_MATRIX_H_
44 
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]))
47 
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))
54 
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))
59 
60 #define icvDeleteVector( vec )  cvFree( &(vec) )
61 #define icvDeleteMatrix icvDeleteVector
62 
63 #define icvAddMatrix_32f( src1, src2, dst, w, h ) \
64     icvAddVector_32f( (src1), (src2), (dst), (w)*(h))
65 
66 #define icvSubMatrix_32f( src1, src2, dst, w, h ) \
67     icvSubVector_32f( (src1), (src2), (dst), (w)*(h))
68 
69 #define icvNormVector_32f( src, len )  \
70     sqrt(icvDotProduct_32f( src, src, len ))
71 
72 #define icvNormVector_64d( src, len )  \
73     sqrt(icvDotProduct_64d( src, src, len ))
74 
75 
76 #define icvDeleteMatrix icvDeleteVector
77 
78 #define icvCheckVector_64f( ptr, len )
79 #define icvCheckVector_32f( ptr, len )
80 
81 CV_INLINE double icvSum_32f( const float* src, int len )
82 {
83     double s = 0;
84     for( int i = 0; i < len; i++ ) s += src[i];
85 
86     icvCheckVector_64f( &s, 1 );
87 
88     return s;
89 }
90 
91 CV_INLINE double icvDotProduct_32f( const float* src1, const float* src2, int len )
92 {
93     double s = 0;
94     for( int i = 0; i < len; i++ ) s += src1[i]*src2[i];
95 
96     icvCheckVector_64f( &s, 1 );
97 
98     return s;
99 }
100 
101 
102 CV_INLINE double icvDotProduct_64f( const double* src1, const double* src2, int len )
103 {
104     double s = 0;
105     for( int i = 0; i < len; i++ ) s += src1[i]*src2[i];
106 
107     icvCheckVector_64f( &s, 1 );
108 
109     return s;
110 }
111 
112 
113 CV_INLINE void icvMulVectors_32f( const float* src1, const float* src2,
114                                   float* dst, int len )
115 {
116     int i;
117     for( i = 0; i < len; i++ )
118         dst[i] = src1[i] * src2[i];
119 
120     icvCheckVector_32f( dst, len );
121 }
122 
123 CV_INLINE void icvMulVectors_64d( const double* src1, const double* src2,
124                                   double* dst, int len )
125 {
126     int i;
127     for( i = 0; i < len; i++ )
128         dst[i] = src1[i] * src2[i];
129 
130     icvCheckVector_64f( dst, len );
131 }
132 
133 
134 CV_INLINE void icvAddVector_32f( const float* src1, const float* src2,
135                                   float* dst, int len )
136 {
137     int i;
138     for( i = 0; i < len; i++ )
139         dst[i] = src1[i] + src2[i];
140 
141     icvCheckVector_32f( dst, len );
142 }
143 
144 CV_INLINE void icvAddVector_64d( const double* src1, const double* src2,
145                                   double* dst, int len )
146 {
147     int i;
148     for( i = 0; i < len; i++ )
149         dst[i] = src1[i] + src2[i];
150 
151     icvCheckVector_64f( dst, len );
152 }
153 
154 
155 CV_INLINE void icvSubVector_32f( const float* src1, const float* src2,
156                                   float* dst, int len )
157 {
158     int i;
159     for( i = 0; i < len; i++ )
160         dst[i] = src1[i] - src2[i];
161 
162     icvCheckVector_32f( dst, len );
163 }
164 
165 CV_INLINE void icvSubVector_64d( const double* src1, const double* src2,
166                                   double* dst, int len )
167 {
168     int i;
169     for( i = 0; i < len; i++ )
170         dst[i] = src1[i] - src2[i];
171 
172     icvCheckVector_64f( dst, len );
173 }
174 
175 
176 #define icvAddMatrix_64d( src1, src2, dst, w, h ) \
177     icvAddVector_64d( (src1), (src2), (dst), (w)*(h))
178 
179 #define icvSubMatrix_64d( src1, src2, dst, w, h ) \
180     icvSubVector_64d( (src1), (src2), (dst), (w)*(h))
181 
182 
183 CV_INLINE void icvSetIdentity_32f( float* dst, int w, int h )
184 {
185     int i, len = MIN( w, h );
186     icvSetZero_32f( dst, w, h );
187     for( i = 0; len--; i += w+1 )
188         dst[i] = 1.f;
189 }
190 
191 
192 CV_INLINE void icvSetIdentity_64d( double* dst, int w, int h )
193 {
194     int i, len = MIN( w, h );
195     icvSetZero_64d( dst, w, h );
196     for( i = 0; len--; i += w+1 )
197         dst[i] = 1.;
198 }
199 
200 
201 CV_INLINE void icvTrace_32f( const float* src, int w, int h, float* trace )
202 {
203     int i, len = MIN( w, h );
204     double sum = 0;
205     for( i = 0; len--; i += w+1 )
206         sum += src[i];
207     *trace = (float)sum;
208 
209     icvCheckVector_64f( &sum, 1 );
210 }
211 
212 
213 CV_INLINE void icvTrace_64d( const double* src, int w, int h, double* trace )
214 {
215     int i, len = MIN( w, h );
216     double sum = 0;
217     for( i = 0; len--; i += w+1 )
218         sum += src[i];
219     *trace = sum;
220 
221     icvCheckVector_64f( &sum, 1 );
222 }
223 
224 
225 CV_INLINE void icvScaleVector_32f( const float* src, float* dst,
226                                    int len, double scale )
227 {
228     int i;
229     for( i = 0; i < len; i++ )
230         dst[i] = (float)(src[i]*scale);
231 
232     icvCheckVector_32f( dst, len );
233 }
234 
235 
236 CV_INLINE void icvScaleVector_64d( const double* src, double* dst,
237                                    int len, double scale )
238 {
239     int i;
240     for( i = 0; i < len; i++ )
241         dst[i] = src[i]*scale;
242 
243     icvCheckVector_64f( dst, len );
244 }
245 
246 
247 CV_INLINE void icvTransposeMatrix_32f( const float* src, int w, int h, float* dst )
248 {
249     int i, j;
250 
251     for( i = 0; i < w; i++ )
252         for( j = 0; j < h; j++ )
253             *dst++ = src[j*w + i];
254 
255     icvCheckVector_32f( dst, w*h );
256 }
257 
258 CV_INLINE void icvTransposeMatrix_64d( const double* src, int w, int h, double* dst )
259 {
260     int i, j;
261 
262     for( i = 0; i < w; i++ )
263         for( j = 0; j < h; j++ )
264             *dst++ = src[j*w + i];
265 
266     icvCheckVector_64f( dst, w*h );
267 }
268 
269 CV_INLINE void icvDetMatrix3x3_64d( const double* mat, double* det )
270 {
271     #define m(y,x) mat[(y)*3 + (x)]
272 
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));
276 
277     #undef m
278 
279     icvCheckVector_64f( det, 1 );
280 }
281 
282 
283 CV_INLINE void icvMulMatrix_32f( const float* src1, int w1, int h1,
284                                  const float* src2, int w2, int h2,
285                                  float* dst )
286 {
287     int i, j, k;
288 
289     if( w1 != h2 )
290     {
291         assert(0);
292         return;
293     }
294 
295     for( i = 0; i < h1; i++, src1 += w1, dst += w2 )
296         for( j = 0; j < w2; j++ )
297         {
298             double s = 0;
299             for( k = 0; k < w1; k++ )
300                 s += src1[k]*src2[j + k*w2];
301             dst[j] = (float)s;
302         }
303 
304     icvCheckVector_32f( dst, h1*w2 );
305 }
306 
307 
308 CV_INLINE void icvMulMatrix_64d( const double* src1, int w1, int h1,
309                                  const double* src2, int w2, int h2,
310                                  double* dst )
311 {
312     int i, j, k;
313 
314     if( w1 != h2 )
315     {
316         assert(0);
317         return;
318     }
319 
320     for( i = 0; i < h1; i++, src1 += w1, dst += w2 )
321         for( j = 0; j < w2; j++ )
322         {
323             double s = 0;
324             for( k = 0; k < w1; k++ )
325                 s += src1[k]*src2[j + k*w2];
326             dst[j] = s;
327         }
328 
329     icvCheckVector_64f( dst, h1*w2 );
330 }
331 
332 
333 #define icvTransformVector_32f( matr, src, dst, w, h ) \
334     icvMulMatrix_32f( matr, w, h, src, 1, w, dst )
335 
336 #define icvTransformVector_64d( matr, src, dst, w, h ) \
337     icvMulMatrix_64d( matr, w, h, src, 1, w, dst )
338 
339 
340 #define icvScaleMatrix_32f( src, dst, w, h, scale ) \
341     icvScaleVector_32f( (src), (dst), (w)*(h), (scale) )
342 
343 #define icvScaleMatrix_64d( src, dst, w, h, scale ) \
344     icvScaleVector_64d( (src), (dst), (w)*(h), (scale) )
345 
346 #define icvDotProduct_64d icvDotProduct_64f
347 
348 
349 CV_INLINE void icvInvertMatrix_64d( double* A, int n, double* invA )
350 {
351     CvMat Am = cvMat( n, n, CV_64F, A );
352     CvMat invAm = cvMat( n, n, CV_64F, invA );
353 
354     cvInvert( &Am, &invAm, CV_SVD );
355 }
356 
357 CV_INLINE void icvMulTransMatrixR_64d( double* src, int width, int height, double* dst )
358 {
359     CvMat srcMat = cvMat( height, width, CV_64F, src );
360     CvMat dstMat = cvMat( width, width, CV_64F, dst );
361 
362     cvMulTransposed( &srcMat, &dstMat, 1 );
363 }
364 
365 CV_INLINE void icvMulTransMatrixL_64d( double* src, int width, int height, double* dst )
366 {
367     CvMat srcMat = cvMat( height, width, CV_64F, src );
368     CvMat dstMat = cvMat( height, height, CV_64F, dst );
369 
370     cvMulTransposed( &srcMat, &dstMat, 0 );
371 }
372 
373 CV_INLINE void icvMulTransMatrixR_32f( float* src, int width, int height, float* dst )
374 {
375     CvMat srcMat = cvMat( height, width, CV_32F, src );
376     CvMat dstMat = cvMat( width, width, CV_32F, dst );
377 
378     cvMulTransposed( &srcMat, &dstMat, 1 );
379 }
380 
381 CV_INLINE void icvMulTransMatrixL_32f( float* src, int width, int height, float* dst )
382 {
383     CvMat srcMat = cvMat( height, width, CV_32F, src );
384     CvMat dstMat = cvMat( height, height, CV_32F, dst );
385 
386     cvMulTransposed( &srcMat, &dstMat, 0 );
387 }
388 
389 CV_INLINE void icvCvt_32f_64d( const float* src, double* dst, int len )
390 {
391     int i;
392     for( i = 0; i < len; i++ )
393         dst[i] = src[i];
394 }
395 
396 CV_INLINE void icvCvt_64d_32f( const double* src, float* dst, int len )
397 {
398     int i;
399     for( i = 0; i < len; i++ )
400         dst[i] = (float)src[i];
401 }
402 
403 #endif/*_CV_MATRIX_H_*/
404 
405 /* End of file. */
406