-
Notifications
You must be signed in to change notification settings - Fork 0
/
_matrix.h
405 lines (306 loc) · 11.1 KB
/
_matrix.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// Intel License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of Intel Corporation may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#ifndef _CV_MATRIX_H_
#define _CV_MATRIX_H_
#define icvCopyVector( src, dst, len ) memcpy( (dst), (src), (len)*sizeof((dst)[0]))
#define icvSetZero( dst, len ) memset( (dst), 0, (len)*sizeof((dst)[0]))
#define icvCopyVector_32f( src, len, dst ) memcpy((dst),(src),(len)*sizeof(float))
#define icvSetZero_32f( dst, cols, rows ) memset((dst),0,(rows)*(cols)*sizeof(float))
#define icvCopyVector_64d( src, len, dst ) memcpy((dst),(src),(len)*sizeof(double))
#define icvSetZero_64d( dst, cols, rows ) memset((dst),0,(rows)*(cols)*sizeof(double))
#define icvCopyMatrix_32f( src, w, h, dst ) memcpy((dst),(src),(w)*(h)*sizeof(float))
#define icvCopyMatrix_64d( src, w, h, dst ) memcpy((dst),(src),(w)*(h)*sizeof(double))
#define icvCreateVector_32f( len ) (float*)cvAlloc( (len)*sizeof(float))
#define icvCreateVector_64d( len ) (double*)cvAlloc( (len)*sizeof(double))
#define icvCreateMatrix_32f( w, h ) (float*)cvAlloc( (w)*(h)*sizeof(float))
#define icvCreateMatrix_64d( w, h ) (double*)cvAlloc( (w)*(h)*sizeof(double))
#define icvDeleteVector( vec ) cvFree( &(vec) )
#define icvDeleteMatrix icvDeleteVector
#define icvAddMatrix_32f( src1, src2, dst, w, h ) \
icvAddVector_32f( (src1), (src2), (dst), (w)*(h))
#define icvSubMatrix_32f( src1, src2, dst, w, h ) \
icvSubVector_32f( (src1), (src2), (dst), (w)*(h))
#define icvNormVector_32f( src, len ) \
sqrt(icvDotProduct_32f( src, src, len ))
#define icvNormVector_64d( src, len ) \
sqrt(icvDotProduct_64d( src, src, len ))
#define icvDeleteMatrix icvDeleteVector
#define icvCheckVector_64f( ptr, len )
#define icvCheckVector_32f( ptr, len )
CV_INLINE double icvSum_32f( const float* src, int len )
{
double s = 0;
for( int i = 0; i < len; i++ ) s += src[i];
icvCheckVector_64f( &s, 1 );
return s;
}
CV_INLINE double icvDotProduct_32f( const float* src1, const float* src2, int len )
{
double s = 0;
for( int i = 0; i < len; i++ ) s += src1[i]*src2[i];
icvCheckVector_64f( &s, 1 );
return s;
}
CV_INLINE double icvDotProduct_64f( const double* src1, const double* src2, int len )
{
double s = 0;
for( int i = 0; i < len; i++ ) s += src1[i]*src2[i];
icvCheckVector_64f( &s, 1 );
return s;
}
CV_INLINE void icvMulVectors_32f( const float* src1, const float* src2,
float* dst, int len )
{
int i;
for( i = 0; i < len; i++ )
dst[i] = src1[i] * src2[i];
icvCheckVector_32f( dst, len );
}
CV_INLINE void icvMulVectors_64d( const double* src1, const double* src2,
double* dst, int len )
{
int i;
for( i = 0; i < len; i++ )
dst[i] = src1[i] * src2[i];
icvCheckVector_64f( dst, len );
}
CV_INLINE void icvAddVector_32f( const float* src1, const float* src2,
float* dst, int len )
{
int i;
for( i = 0; i < len; i++ )
dst[i] = src1[i] + src2[i];
icvCheckVector_32f( dst, len );
}
CV_INLINE void icvAddVector_64d( const double* src1, const double* src2,
double* dst, int len )
{
int i;
for( i = 0; i < len; i++ )
dst[i] = src1[i] + src2[i];
icvCheckVector_64f( dst, len );
}
CV_INLINE void icvSubVector_32f( const float* src1, const float* src2,
float* dst, int len )
{
int i;
for( i = 0; i < len; i++ )
dst[i] = src1[i] - src2[i];
icvCheckVector_32f( dst, len );
}
CV_INLINE void icvSubVector_64d( const double* src1, const double* src2,
double* dst, int len )
{
int i;
for( i = 0; i < len; i++ )
dst[i] = src1[i] - src2[i];
icvCheckVector_64f( dst, len );
}
#define icvAddMatrix_64d( src1, src2, dst, w, h ) \
icvAddVector_64d( (src1), (src2), (dst), (w)*(h))
#define icvSubMatrix_64d( src1, src2, dst, w, h ) \
icvSubVector_64d( (src1), (src2), (dst), (w)*(h))
CV_INLINE void icvSetIdentity_32f( float* dst, int w, int h )
{
int i, len = MIN( w, h );
icvSetZero_32f( dst, w, h );
for( i = 0; len--; i += w+1 )
dst[i] = 1.f;
}
CV_INLINE void icvSetIdentity_64d( double* dst, int w, int h )
{
int i, len = MIN( w, h );
icvSetZero_64d( dst, w, h );
for( i = 0; len--; i += w+1 )
dst[i] = 1.;
}
CV_INLINE void icvTrace_32f( const float* src, int w, int h, float* trace )
{
int i, len = MIN( w, h );
double sum = 0;
for( i = 0; len--; i += w+1 )
sum += src[i];
*trace = (float)sum;
icvCheckVector_64f( &sum, 1 );
}
CV_INLINE void icvTrace_64d( const double* src, int w, int h, double* trace )
{
int i, len = MIN( w, h );
double sum = 0;
for( i = 0; len--; i += w+1 )
sum += src[i];
*trace = sum;
icvCheckVector_64f( &sum, 1 );
}
CV_INLINE void icvScaleVector_32f( const float* src, float* dst,
int len, double scale )
{
int i;
for( i = 0; i < len; i++ )
dst[i] = (float)(src[i]*scale);
icvCheckVector_32f( dst, len );
}
CV_INLINE void icvScaleVector_64d( const double* src, double* dst,
int len, double scale )
{
int i;
for( i = 0; i < len; i++ )
dst[i] = src[i]*scale;
icvCheckVector_64f( dst, len );
}
CV_INLINE void icvTransposeMatrix_32f( const float* src, int w, int h, float* dst )
{
int i, j;
for( i = 0; i < w; i++ )
for( j = 0; j < h; j++ )
*dst++ = src[j*w + i];
icvCheckVector_32f( dst, w*h );
}
CV_INLINE void icvTransposeMatrix_64d( const double* src, int w, int h, double* dst )
{
int i, j;
for( i = 0; i < w; i++ )
for( j = 0; j < h; j++ )
*dst++ = src[j*w + i];
icvCheckVector_64f( dst, w*h );
}
CV_INLINE void icvDetMatrix3x3_64d( const double* mat, double* det )
{
#define m(y,x) mat[(y)*3 + (x)]
*det = m(0,0)*(m(1,1)*m(2,2) - m(1,2)*m(2,1)) -
m(0,1)*(m(1,0)*m(2,2) - m(1,2)*m(2,0)) +
m(0,2)*(m(1,0)*m(2,1) - m(1,1)*m(2,0));
#undef m
icvCheckVector_64f( det, 1 );
}
CV_INLINE void icvMulMatrix_32f( const float* src1, int w1, int h1,
const float* src2, int w2, int h2,
float* dst )
{
int i, j, k;
if( w1 != h2 )
{
assert(0);
return;
}
for( i = 0; i < h1; i++, src1 += w1, dst += w2 )
for( j = 0; j < w2; j++ )
{
double s = 0;
for( k = 0; k < w1; k++ )
s += src1[k]*src2[j + k*w2];
dst[j] = (float)s;
}
icvCheckVector_32f( dst, h1*w2 );
}
CV_INLINE void icvMulMatrix_64d( const double* src1, int w1, int h1,
const double* src2, int w2, int h2,
double* dst )
{
int i, j, k;
if( w1 != h2 )
{
assert(0);
return;
}
for( i = 0; i < h1; i++, src1 += w1, dst += w2 )
for( j = 0; j < w2; j++ )
{
double s = 0;
for( k = 0; k < w1; k++ )
s += src1[k]*src2[j + k*w2];
dst[j] = s;
}
icvCheckVector_64f( dst, h1*w2 );
}
#define icvTransformVector_32f( matr, src, dst, w, h ) \
icvMulMatrix_32f( matr, w, h, src, 1, w, dst )
#define icvTransformVector_64d( matr, src, dst, w, h ) \
icvMulMatrix_64d( matr, w, h, src, 1, w, dst )
#define icvScaleMatrix_32f( src, dst, w, h, scale ) \
icvScaleVector_32f( (src), (dst), (w)*(h), (scale) )
#define icvScaleMatrix_64d( src, dst, w, h, scale ) \
icvScaleVector_64d( (src), (dst), (w)*(h), (scale) )
#define icvDotProduct_64d icvDotProduct_64f
CV_INLINE void icvInvertMatrix_64d( double* A, int n, double* invA )
{
CvMat Am = cvMat( n, n, CV_64F, A );
CvMat invAm = cvMat( n, n, CV_64F, invA );
cvInvert( &Am, &invAm, CV_SVD );
}
CV_INLINE void icvMulTransMatrixR_64d( double* src, int width, int height, double* dst )
{
CvMat srcMat = cvMat( height, width, CV_64F, src );
CvMat dstMat = cvMat( width, width, CV_64F, dst );
cvMulTransposed( &srcMat, &dstMat, 1 );
}
CV_INLINE void icvMulTransMatrixL_64d( double* src, int width, int height, double* dst )
{
CvMat srcMat = cvMat( height, width, CV_64F, src );
CvMat dstMat = cvMat( height, height, CV_64F, dst );
cvMulTransposed( &srcMat, &dstMat, 0 );
}
CV_INLINE void icvMulTransMatrixR_32f( float* src, int width, int height, float* dst )
{
CvMat srcMat = cvMat( height, width, CV_32F, src );
CvMat dstMat = cvMat( width, width, CV_32F, dst );
cvMulTransposed( &srcMat, &dstMat, 1 );
}
CV_INLINE void icvMulTransMatrixL_32f( float* src, int width, int height, float* dst )
{
CvMat srcMat = cvMat( height, width, CV_32F, src );
CvMat dstMat = cvMat( height, height, CV_32F, dst );
cvMulTransposed( &srcMat, &dstMat, 0 );
}
CV_INLINE void icvCvt_32f_64d( const float* src, double* dst, int len )
{
int i;
for( i = 0; i < len; i++ )
dst[i] = src[i];
}
CV_INLINE void icvCvt_64d_32f( const double* src, float* dst, int len )
{
int i;
for( i = 0; i < len; i++ )
dst[i] = (float)src[i];
}
#endif/*_CV_MATRIX_H_*/
/* End of file. */