-
Notifications
You must be signed in to change notification settings - Fork 21
/
tinymatwriter.h
1415 lines (1091 loc) · 52.7 KB
/
tinymatwriter.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2008-2015 Jan W. Krieger (<jan@jkrieger.de>, <j.krieger@dkfz.de>), German Cancer Research Center (DKFZ) & IWR, University of Heidelberg
This software is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License (LGPL) as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TINYMATWRITER_H
#define TINYMATWRITER_H
#include "tinymatwriter_export.h"
#include <stdint.h>
#include <memory>
#include <list>
#include <vector>
#include <string>
#include <map>
#ifdef TINYMAT_USES_QVARIANT
# include <QVariant>
# include <QList>
# include <QStringList>
# include <QMap>
# include <QVector>
# include <QString>
#endif
#ifdef TINYMAT_USES_OPENCV
#pragma warning(push, 0)
# include <opencv2/opencv.hpp>
#pragma warning(pop)
#endif
/*! \defgroup tinymatwriter Tiny Matlab(r) MAT writer library
\code
TinyMATWriterFile* mat=TinyMATWriter_open("test.mat");
if (mat) {
double mat1[4]={1,2,3,4};
double vec1[8]={1,2,3,4,5,6,7,8};
TinyMATWriter_writeMatrix2D_rowmajor(mat, "vector1", vec1, 1,8);
TinyMATWriter_writeMatrix2D_rowmajor(mat, "matrix1", mat1, 2,2);
TinyMATWriter_writeMatrix2D_rowmajor(mat, "vector2", vec1, 8,1);
TinyMATWriter_close(mat);
}
\endcode
Here is Octave/Matlab code to test the output and create a comparable MAT-file:
\code
mat1=[1,2;3,4];
vec1=[1,2,3,4,5,6,7,8];
vec2=vec1';
save("-v6", "./test_octave.mat", "vec1", "mat1", "vec2")
load("test.mat", "-v6")
if (vec1==vector1)
disp('vec1 OK');
end
if (mat1==matrix1)
disp('mat1 OK');
end
if (vec2==vector2)
disp('vec2 OK');
end
\endcode
*/
/*! \defgroup tinymatwriter_qt TinyMATWriter-Support for Qt Datatypes
\ingroup tinymatwriter
Functions in this group allow to directly store Qt-datatypes into MAT-files.
*/
/*! \defgroup tinymatwriter_opencv TinyMATWriter-Support for OpenCV Datatypes
\ingroup tinymatwriter
Functions in this group allow to directly store OpenCV-datatypes into MAT-files.
*/
/** \brief struct used to describe a TIFF file
* \ingroup tinymatwriter
*/
struct TinyMATWriterFile; // forward
#ifndef TRUE
# define TRUE (0==0)
#endif
#ifndef FALSE
# define FALSE (0==1)
#endif
/*! \brief returns \c TRUE (non-zero) if the given TinyMATWriterFile has been opened successfully and is OK
\ingroup tinymatwriter
\param mat the MAT-file
\return \c TRUE if the file is OK and can be written to or \c FALSE
*/
TINYMATWRITER_EXPORT int TinyMATWriter_fOK(const TinyMATWriterFile* mat);
/*! \brief create a new MAT file
\ingroup tinymatwriter
\param filename name of the new TIFF file
\param description description of the file (max. 115 characters)
\param bufSize size of the IO-Buffer used for the MAT-file ... Choosing a size
in the range of the final file size may improve performance!
The default-size is 100kB.
\return a new TinyMATWriterFile pointer on success, or NULL on errors
*/
TINYMATWRITER_EXPORT TinyMATWriterFile* TinyMATWriter_open(const char* filename, const char* description=NULL, size_t bufSize=1024*100);
/*! \brief write a string into a MAT-file
\ingroup tinymatwriter
\param mat the MAT-file to write into
\param name variable name for the new array
\param data the string to write
*/
TINYMATWRITER_EXPORT void TinyMATWriter_writeString(TinyMATWriterFile* mat, const char* name, const char* data);
/*! \brief write a string into a MAT-file
\ingroup tinymatwriter
\param mat the MAT-file to write into
\param name variable name for the new array
\param data the string to write
*/
TINYMATWRITER_EXPORT void TinyMATWriter_writeString(TinyMATWriterFile* mat, const char* name, const std::string& data);
/*! \brief write a string into a MAT-file
\ingroup tinymatwriter
\param mat the MAT-file to write into
\param name variable name for the new array
\param data the string to write
\param slen length of the string in bytes
*/
TINYMATWRITER_EXPORT void TinyMATWriter_writeString(TinyMATWriterFile* mat, const char* name, const char* data, uint32_t slen);
/*! \brief write an empty (double) matrix into a MAT-file
\ingroup tinymatwriter
\param mat the MAT-file to write into
\param name variable name for the new array
*/
TINYMATWRITER_EXPORT void TinyMATWriter_writeEmptyMatrix(TinyMATWriterFile* mat, const char* name);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// METHODS TO WRITE MATRICES
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*! \brief write a N-dimensional double matrix in column-major form into a MAT-file
\ingroup tinymatwriter
\param mat the MAT-file to write into
\param name variable name for the new array
\param data_real the array to write (in column-major order)
\param sizes number of entries in each dimension {rows, cols, matrices, ...}
\param ndims number of dimensions
*/
TINYMATWRITER_EXPORT void TinyMATWriter_writeMatrixND_colmajor(TinyMATWriterFile* mat, const char* name, const double* data_real, const int32_t* sizes, uint32_t ndims) ;
/*! \brief write a N-dimensional float matrix in column-major form into a MAT-file
\ingroup tinymatwriter
\param mat the MAT-file to write into
\param name variable name for the new array
\param data_real the array to write (in column-major order)
\param sizes number of entries in each dimension {rows, cols, matrices, ...}
\param ndims number of dimensions
*/
TINYMATWRITER_EXPORT void TinyMATWriter_writeMatrixND_colmajor(TinyMATWriterFile* mat, const char* name, const float* data_real, const int32_t* sizes, uint32_t ndims) ;
/*! \brief write a N-dimensional uint64_t matrix in column-major form into a MAT-file
\ingroup tinymatwriter
\param mat the MAT-file to write into
\param name variable name for the new array
\param data_real the array to write (in column-major order)
\param sizes number of entries in each dimension {rows, cols, matrices, ...}
\param ndims number of dimensions
*/
TINYMATWRITER_EXPORT void TinyMATWriter_writeMatrixND_colmajor(TinyMATWriterFile* mat, const char* name, const uint64_t* data_real, const int32_t* sizes, uint32_t ndims) ;
/*! \brief write a N-dimensional int64_t matrix in column-major form into a MAT-file
\ingroup tinymatwriter
\param mat the MAT-file to write into
\param name variable name for the new array
\param data_real the array to write (in column-major order)
\param sizes number of entries in each dimension {rows, cols, matrices, ...}
\param ndims number of dimensions
*/
TINYMATWRITER_EXPORT void TinyMATWriter_writeMatrixND_colmajor(TinyMATWriterFile* mat, const char* name, const int64_t* data_real, const int32_t* sizes, uint32_t ndims) ;
/*! \brief write a N-dimensional uint32_t matrix in column-major form into a MAT-file
\ingroup tinymatwriter
\param mat the MAT-file to write into
\param name variable name for the new array
\param data_real the array to write (in column-major order)
\param sizes number of entries in each dimension {rows, cols, matrices, ...}
\param ndims number of dimensions
*/
TINYMATWRITER_EXPORT void TinyMATWriter_writeMatrixND_colmajor(TinyMATWriterFile* mat, const char* name, const uint32_t* data_real, const int32_t* sizes, uint32_t ndims) ;
/*! \brief write a N-dimensional int32_t matrix in column-major form into a MAT-file
\ingroup tinymatwriter
\param mat the MAT-file to write into
\param name variable name for the new array
\param data_real the array to write (in column-major order)
\param sizes number of entries in each dimension {rows, cols, matrices, ...}
\param ndims number of dimensions
*/
TINYMATWRITER_EXPORT void TinyMATWriter_writeMatrixND_colmajor(TinyMATWriterFile* mat, const char* name, const int32_t* data_real, const int32_t* sizes, uint32_t ndims) ;
/*! \brief write a N-dimensional uint16_t matrix in column-major form into a MAT-file
\ingroup tinymatwriter
\param mat the MAT-file to write into
\param name variable name for the new array
\param data_real the array to write (in column-major order)
\param sizes number of entries in each dimension {rows, cols, matrices, ...}
\param ndims number of dimensions
*/
TINYMATWRITER_EXPORT void TinyMATWriter_writeMatrixND_colmajor(TinyMATWriterFile* mat, const char* name, const uint16_t* data_real, const int32_t* sizes, uint32_t ndims) ;
/*! \brief write a N-dimensional int16_t matrix in column-major form into a MAT-file
\ingroup tinymatwriter
\param mat the MAT-file to write into
\param name variable name for the new array
\param data_real the array to write (in column-major order)
\param sizes number of entries in each dimension {rows, cols, matrices, ...}
\param ndims number of dimensions
*/
TINYMATWRITER_EXPORT void TinyMATWriter_writeMatrixND_colmajor(TinyMATWriterFile* mat, const char* name, const int16_t* data_real, const int32_t* sizes, uint32_t ndims) ;
/*! \brief write a N-dimensional uint8_t matrix in column-major form into a MAT-file
\ingroup tinymatwriter
\param mat the MAT-file to write into
\param name variable name for the new array
\param data_real the array to write (in column-major order)
\param sizes number of entries in each dimension {rows, cols, matrices, ...}
\param ndims number of dimensions
*/
TINYMATWRITER_EXPORT void TinyMATWriter_writeMatrixND_colmajor(TinyMATWriterFile* mat, const char* name, const uint8_t* data_real, const int32_t* sizes, uint32_t ndims) ;
/*! \brief write a N-dimensional int8_t matrix in column-major form into a MAT-file
\ingroup tinymatwriter
\param mat the MAT-file to write into
\param name variable name for the new array
\param data_real the array to write (in column-major order)
\param sizes number of entries in each dimension {rows, cols, matrices, ...}
\param ndims number of dimensions
*/
TINYMATWRITER_EXPORT void TinyMATWriter_writeMatrixND_colmajor(TinyMATWriterFile* mat, const char* name, const int8_t* data_real, const int32_t* sizes, uint32_t ndims) ;
/*! \brief write a N-dimensional bool matrix in column-major form into a MAT-file
\ingroup tinymatwriter
\param mat the MAT-file to write into
\param name variable name for the new array
\param data_real the array to write (in column-major order)
\param sizes number of entries in each dimension {rows, cols, matrices, ...}
\param ndims number of dimensions
*/
TINYMATWRITER_EXPORT void TinyMATWriter_writeMatrixND_colmajor(TinyMATWriterFile* mat, const char* name, const bool* data_real, const int32_t* sizes, uint32_t ndims) ;
/*! \brief write a single (numeric) value (as 1x1 matrix) into a MAT-file
\ingroup tinymatwriter
\param mat the MAT-file to write into
\param name variable name for the new array
\param data_real the value to write
*/
template<typename T>
inline void TinyMATWriter_writeValue(TinyMATWriterFile* mat, const char* name, T data_real) {
TinyMATWriter_writeVectorAsColumn(mat, name, &data_real, 1);
}
/*! \brief write a N-dimensional double matrix into a MAT-file
\ingroup tinymatwriter
\param mat the MAT-file to write into
\param name variable name for the new array
\param data_real the array to write (in row-major order) {M1row1, M1row2, ..., M1rowC, M2row1, M2row2, ... }
\param sizes number of entries in each dimension {cols, rows, matrices, ...}
\param ndims number of dimensions
*/
template<typename T>
inline void TinyMATWriter_writeMatrixND_rowmajor(TinyMATWriterFile* mat, const char* name, const T* data_real, const int32_t* sizes, uint32_t ndims) {
T* dat=NULL;
const T* datOut=NULL;
int32_t* siz=NULL;
bool freeDat=false;
if (data_real && sizes && ndims>1) {
uint32_t nentries=1;
uint32_t cols=1;
uint32_t rows=1;
uint32_t nmatrices=1;
uint32_t nonSingularDimensions=0;
for (uint32_t i=0; i<ndims; i++) {
if (i==0) {
nentries=sizes[0];
} else {
nentries=nentries*sizes[i];
}
if (i==0) cols=sizes[i];
else if (i==1) rows=sizes[i];
else {
nmatrices=nmatrices*sizes[i];
}
if (sizes[i]>1) nonSingularDimensions++;
}
if (nentries>0) {
siz=new int32_t[ndims];//=(int32_t*)malloc(ndims*sizeof(int32_t));
for (uint32_t i=0; i<ndims; i++) siz[i]=sizes[i];
if (ndims>1) {
siz[0]=sizes[1];
siz[1]=sizes[0];
}
if (nonSingularDimensions<=1) {
// this is not a matrix, but a simple vector
datOut=data_real;
freeDat=false;
} else {
dat=new T[nentries];//(T*)malloc(nentries*sizeof(T));
datOut=dat;
freeDat=true;
for (uint32_t m=0; m<nmatrices; m++) {
for(uint32_t r=0; r<rows; r++) {
for (uint32_t c=0; c<cols; c++) {
dat[m*cols*rows+c*rows+r]=data_real[m*cols*rows+r*cols+c];
}
}
}
}
}
}
if (dat) {
TinyMATWriter_writeMatrixND_colmajor(mat, name, datOut, siz, ndims);
//free(dat);
//free(siz);
} else {
TinyMATWriter_writeMatrixND_colmajor(mat, name, data_real, sizes, ndims);
}
if (freeDat && dat) delete[] dat;
if (siz) delete[] siz;
}
/*! \brief write a N-dimensional matrix with C color channels (e.g. C=3 RGBRGBRGB...) into a MAT-file
\ingroup tinymatwriter
\param mat the MAT-file to write into
\param name variable name for the new array
\param data_real the array to write (in row-major order) {M1row1, M1row2, ..., M1rowC, M2row1, M2row2, ... },
where each elements has the channels in order C2C2C3C1C2C3...
\param sizes number of entries in each dimension {cols, rows, matrices, ...}
\param ndims number of dimensions
\param c number of channels
This function will actually write an ndims+1-dimensional matrix, where the outer-most dimension is the number
of channels. The input data is then separated into planes!
*/
template<typename T>
inline void TinyMATWriter_writeMultiChannelMatrixND_rowmajor(TinyMATWriterFile* mat, const char* name, const T* data_real, const int32_t* sizes, uint32_t ndims, uint32_t c) {
if (c==1) {
TinyMATWriter_writeMatrixND_rowmajor( mat, name, data_real, sizes, ndims);
} else {
uint32_t nentries=1;
int32_t* siz=(int32_t*)malloc((ndims+1)*sizeof(int32_t));
for (uint32_t i=0; i<ndims; i++) {
if (i==0) {
nentries=sizes[0];
} else {
nentries=nentries*sizes[i];
}
siz[i]=sizes[i];
}
siz[ndims]=c;
T* dat=(T*)calloc(nentries*c,sizeof(T));
uint32_t j=0;
// resort array into planes
for (uint32_t i=0; i<nentries; i++) {
for (uint32_t ci=0; ci<c; ci++) {
dat[ci*nentries+i]=data_real[j];
j++;
}
}
TinyMATWriter_writeMatrixND_rowmajor( mat, name, dat, siz, ndims+1);
free(dat);
free(siz);
}
}
/*! \brief write a N-dimensional matrix with C color channels (e.g. C=3 RGBRGBRGB...) into a MAT-file
\ingroup tinymatwriter
\param mat the MAT-file to write into
\param name variable name for the new array
\param data_real the array to write (in row-major order) {M1row1, M1row2, ..., M1rowC, M2row1, M2row2, ... },
where each elements has the channels in order C2C2C3C1C2C3...
\param sizes number of entries in each dimension {cols, rows, matrices, ...}
\param ndims number of dimensions
\param c number of channels
This function will actually write an ndims+1-dimensional matrix, where the outer-most dimension is the number
of channels. The input data is then separated into planes!
*/
template<typename T>
inline void TinyMATWriter_writeMultiChannelMatrix2D_rowmajor(TinyMATWriterFile* mat, const char* name, const T* data_real, int32_t cols, int32_t rows, uint32_t c) {
int32_t dims[2]={rows, cols};
TinyMATWriter_writeMultiChannelMatrix2D_rowmajor(mat, name, data_real, dims, 2, c);
}
/*! \brief write a 2-dimensional double matrix in column-major order into a MAT-file
\ingroup tinymatwriter
\param mat the MAT-file to write into
\param name variable name for the new array
\param data_real the array to write (in column-major order)
\param cols number of columns
\param rows number of rows
*/
template<typename T>
inline void TinyMATWriter_writeMatrix2D_colmajor(TinyMATWriterFile* mat, const char* name, const T* data_real, int32_t cols, int32_t rows) {
int32_t siz[2]={rows, cols};
TinyMATWriter_writeMatrixND_colmajor(mat, name, data_real, siz, 2);
}
/*! \brief write a 2-dimensional double matrix in row-major order into a MAT-file
\ingroup tinymatwriter
\param mat the MAT-file to write into
\param name variable name for the new array
\param data_real the array to write (in row-major order)
\param cols number of columns
\param rows number of rows
*/
template<typename T>
inline void TinyMATWriter_writeMatrix2D_rowmajor(TinyMATWriterFile* mat, const char* name, const T* data_real, int32_t cols, int32_t rows) {
int32_t siz[2]={cols, rows};
TinyMATWriter_writeMatrixND_rowmajor(mat, name, data_real, siz, 2);
}
/*! \brief write a 2x2-dimensional double matrix with entries given in row-major order directly as parameters into a MAT-file
\ingroup tinymatwriter
\param mat the MAT-file to write into
\param name variable name for the new array
*/
template<typename T>
inline void TinyMATWriter_writeMatrix2x2(TinyMATWriterFile* mat, const char* name, T m11, T m12, T m21, T m22) {
int32_t siz[2]={2, 2};
T data[4]={m11,m12,m21,m22};
TinyMATWriter_writeMatrixND_rowmajor(mat, name, data, siz, 2);
}
/*! \brief write a 3x3-dimensional double matrix with entries given in row-major order directly as parameters into a MAT-file
\ingroup tinymatwriter
\param mat the MAT-file to write into
\param name variable name for the new array
*/
template<typename T>
inline void TinyMATWriter_writeMatrix3x3(TinyMATWriterFile* mat, const char* name, T m11, T m12, T m13, T m21, T m22, T m23, T m31, T m32, T m33) {
int32_t siz[2]={3,3};
T data[9]={m11,m12,m13,m21,m22,m23,m31,m32,m33};
TinyMATWriter_writeMatrixND_rowmajor(mat, name, data, siz, 2);
}
/*! \brief write a 1-dimensional double vector as a row-vector into a MAT-file
\ingroup tinymatwriter
\param mat the MAT-file to write into
\param name variable name for the new array
\param data_real the array to write (in row-major order)
\param cols number of columns
\param rows number of rows
*/
template<typename T>
inline void TinyMATWriter_writeVectorAsRow(TinyMATWriterFile* mat, const char* name, const T* data_real, int32_t rows) {
int32_t siz[2]={1, rows};
TinyMATWriter_writeMatrixND_rowmajor(mat, name, data_real, siz, 2);
}
/*! \brief write a 1-dimensional double vector as a row-vector into a MAT-file with data directly as parameter direct
\ingroup tinymatwriter
\param mat the MAT-file to write into
\param name variable name for the new array
\param d1 first entry
\param d2 second entry
*/
template<typename T>
inline void TinyMATWriter_writeVectorAsRow(TinyMATWriterFile* mat, const char* name, T d1, T d2) {
int32_t siz[2]={1,2};
T data[2]={d1, d2};
TinyMATWriter_writeMatrixND_rowmajor(mat, name, data, siz, 2);
}
template<typename T>
inline void TinyMATWriter_writeVectorAsRow(TinyMATWriterFile* mat, const char* name, T d1, T d2, T d3) {
int32_t siz[2]={1,3};
T data[]={d1, d2, d3};
TinyMATWriter_writeMatrixND_rowmajor(mat, name, data, siz, 2);
}
template<typename T>
inline void TinyMATWriter_writeVectorAsRow(TinyMATWriterFile* mat, const char* name, T d1, T d2, T d3, T d4) {
int32_t siz[2]={1,4};
T data[]={d1, d2, d3, d4};
TinyMATWriter_writeMatrixND_rowmajor(mat, name, data, siz, 2);
}
template<typename T>
inline void TinyMATWriter_writeVectorAsRow(TinyMATWriterFile* mat, const char* name, T d1, T d2, T d3, T d4, T d5) {
int32_t siz[2]={1,5};
T data[]={d1, d2, d3, d4, d5};
TinyMATWriter_writeMatrixND_rowmajor(mat, name, data, siz, 2);
}
template<typename T>
inline void TinyMATWriter_writeVectorAsRow(TinyMATWriterFile* mat, const char* name, T d1, T d2, T d3, T d4, T d5, T d6) {
int32_t siz[2]={1,6};
T data[]={d1, d2, d3, d4, d5, d6};
TinyMATWriter_writeMatrixND_rowmajor(mat, name, data, siz, 2);
}
template<typename T>
inline void TinyMATWriter_writeVectorAsRow(TinyMATWriterFile* mat, const char* name, T d1, T d2, T d3, T d4, T d5, T d6, T d7) {
int32_t siz[2]={1,7};
T data[]={d1, d2, d3, d4, d5, d6, d7};
TinyMATWriter_writeMatrixND_rowmajor(mat, name, data, siz, 2);
}
template<typename T>
inline void TinyMATWriter_writeVectorAsRow(TinyMATWriterFile* mat, const char* name, T d1, T d2, T d3, T d4, T d5, T d6, T d7, T d8) {
int32_t siz[2]={1,8};
T data[]={d1, d2, d3, d4, d5, d6, d7, d8};
TinyMATWriter_writeMatrixND_rowmajor(mat, name, data, siz, 2);
}
template<typename T>
inline void TinyMATWriter_writeVectorAsRow(TinyMATWriterFile* mat, const char* name, T d1, T d2, T d3, T d4, T d5, T d6, T d7, T d8, T d9) {
int32_t siz[2]={1,9};
T data[]={d1, d2, d3, d4, d5, d6, d7, d8, d9};
TinyMATWriter_writeMatrixND_rowmajor(mat, name, data, siz, 2);
}
template<typename T>
inline void TinyMATWriter_writeVectorAsRow(TinyMATWriterFile* mat, const char* name, T d1, T d2, T d3, T d4, T d5, T d6, T d7, T d8, T d9, T d10) {
int32_t siz[2]={1, 10};
T data[]={d1, d2, d3, d4, d5, d6, d7, d8, d9, d10};
TinyMATWriter_writeMatrixND_rowmajor(mat, name, data, siz, 2);
}
template<typename T>
inline void TinyMATWriter_writeVectorAsRow(TinyMATWriterFile* mat, const char* name, T d1, T d2, T d3, T d4, T d5, T d6, T d7, T d8, T d9, T d10, T d11) {
int32_t siz[2]={1,11};
T data[]={d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11};
TinyMATWriter_writeMatrixND_rowmajor(mat, name, data, siz, 2);
}
template<typename T>
inline void TinyMATWriter_writeVectorAsRow(TinyMATWriterFile* mat, const char* name, T d1, T d2, T d3, T d4, T d5, T d6, T d7, T d8, T d9, T d10, T d11, T d12) {
int32_t siz[2]={1,12};
T data[]={d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12};
TinyMATWriter_writeMatrixND_rowmajor(mat, name, data, siz, 2);
}
/*! \brief write a 1-dimensional double vector as a column-vector into a MAT-file
\ingroup tinymatwriter
\param mat the MAT-file to write into
\param name variable name for the new array
\param data_real the array to write (in row-major order)
\param cols number of columns
\param rows number of rows
*/
template<typename T>
inline void TinyMATWriter_writeVectorAsColumn(TinyMATWriterFile* mat, const char* name, const T* data_real, int32_t rows) {
int32_t siz[2]={rows, 1};
TinyMATWriter_writeMatrixND_rowmajor(mat, name, data_real, siz, 2);
}
/*! \brief write a 1-dimensional double vector as a row-vector into a MAT-file with data directly as parameter direct
\ingroup tinymatwriter
\param mat the MAT-file to write into
\param name variable name for the new array
\param d1 first entry
\param d2 second entry
*/
template<typename T>
inline void TinyMATWriter_writeVectorAsColumn(TinyMATWriterFile* mat, const char* name, T d1, T d2) {
int32_t siz[2]={2,1};
T data[2]={d1, d2};
TinyMATWriter_writeMatrixND_rowmajor(mat, name, data, siz, 2);
}
template<typename T>
inline void TinyMATWriter_writeVectorAsColumn(TinyMATWriterFile* mat, const char* name, T d1, T d2, T d3) {
int32_t siz[2]={3,1};
T data[]={d1, d2, d3};
TinyMATWriter_writeMatrixND_rowmajor(mat, name, data, siz, 2);
}
template<typename T>
inline void TinyMATWriter_writeVectorAsColumn(TinyMATWriterFile* mat, const char* name, T d1, T d2, T d3, T d4) {
int32_t siz[2]={4,1};
T data[]={d1, d2, d3, d4};
TinyMATWriter_writeMatrixND_rowmajor(mat, name, data, siz, 2);
}
template<typename T>
inline void TinyMATWriter_writeVectorAsColumn(TinyMATWriterFile* mat, const char* name, T d1, T d2, T d3, T d4, T d5) {
int32_t siz[2]={5,1};
T data[]={d1, d2, d3, d4, d5};
TinyMATWriter_writeMatrixND_rowmajor(mat, name, data, siz, 2);
}
template<typename T>
inline void TinyMATWriter_writeVectorAsColumn(TinyMATWriterFile* mat, const char* name, T d1, T d2, T d3, T d4, T d5, T d6) {
int32_t siz[2]={6,1};
T data[]={d1, d2, d3, d4, d5, d6};
TinyMATWriter_writeMatrixND_rowmajor(mat, name, data, siz, 2);
}
template<typename T>
inline void TinyMATWriter_writeVectorAsColumn(TinyMATWriterFile* mat, const char* name, T d1, T d2, T d3, T d4, T d5, T d6, T d7) {
int32_t siz[2]={7,1};
T data[]={d1, d2, d3, d4, d5, d6, d7};
TinyMATWriter_writeMatrixND_rowmajor(mat, name, data, siz, 2);
}
template<typename T>
inline void TinyMATWriter_writeVectorAsColumn(TinyMATWriterFile* mat, const char* name, T d1, T d2, T d3, T d4, T d5, T d6, T d7, T d8) {
int32_t siz[2]={8,1};
T data[]={d1, d2, d3, d4, d5, d6, d7, d8};
TinyMATWriter_writeMatrixND_rowmajor(mat, name, data, siz, 2);
}
template<typename T>
inline void TinyMATWriter_writeVectorAsColumn(TinyMATWriterFile* mat, const char* name, T d1, T d2, T d3, T d4, T d5, T d6, T d7, T d8, T d9) {
int32_t siz[2]={9,1};
T data[]={d1, d2, d3, d4, d5, d6, d7, d8, d9};
TinyMATWriter_writeMatrixND_rowmajor(mat, name, data, siz, 2);
}
template<typename T>
inline void TinyMATWriter_writeVectorAsColumn(TinyMATWriterFile* mat, const char* name, T d1, T d2, T d3, T d4, T d5, T d6, T d7, T d8, T d9, T d10) {
int32_t siz[2]={10,1};
T data[]={d1, d2, d3, d4, d5, d6, d7, d8, d9, d10};
TinyMATWriter_writeMatrixND_rowmajor(mat, name, data, siz, 2);
}
/*! \brief write a 1-dimensional vector/list/... of values as a row-vector into a MAT-file
\ingroup tinymatwriter
\internal
\param mat the MAT-file to write into
\param name variable name for the new array
\param data_vec the array to write. This container has to implement the function size() and
an iterator with tool-functions begin() and end()
\note This function copies the contents of the container into a temporary array, which is then saved to
the file. Use TinyMATWriter_writeContainerAsColumn() to use potential performance-optimizations for some
container types.
*/
template<typename T>
inline void TinyMATWriter_writeContainerAsRow_internalCopy(TinyMATWriterFile* mat, const char* name, const T& data_vec) {
int32_t siz[2]={1, (int32_t)data_vec.size()};
auto tmp=static_cast<typename T::value_type*>(malloc(data_vec.size()*sizeof(typename T::value_type)));
int i=0;
for (auto it=data_vec.begin(); it!=data_vec.end(); ++it) {
tmp[i]=*it;
i++;
}
TinyMATWriter_writeMatrixND_rowmajor(mat, name, tmp, siz, 2);
free(tmp);
}
/*! \brief write a 1-dimensional vector/list/... of values as a column-vector into a MAT-file
\ingroup tinymatwriter
\internal
\param mat the MAT-file to write into
\param name variable name for the new array
\param data_vec the array to write. This container has to implement the function size() and
an iterator with tool-functions begin() and end(). Data-Items (T::value_type)
has to be integer numbers or floats
\note This function copies the contents of the container into a temporary array, which is then saved to
the file. Use TinyMATWriter_writeContainerAsColumn() to use potential performance-optimizations for some
container types.
*/
template<typename T>
inline void TinyMATWriter_writeContainerAsColumn_internalCopy(TinyMATWriterFile* mat, const char* name, const T& data_vec) {
int32_t siz[2]={(int32_t)data_vec.size(), 1};
auto tmp=static_cast<typename T::value_type*>(malloc(data_vec.size()*sizeof(typename T::value_type)));
int i=0;
for (auto it=data_vec.begin(); it!=data_vec.end(); ++it) {
tmp[i]=*it;
i++;
}
TinyMATWriter_writeMatrixND_rowmajor(mat, name, tmp, siz, 2);
free(tmp);
}
/*! \brief write a 1-dimensional vector/list/... of values as a row-vector into a MAT-file
\ingroup tinymatwriter
\param mat the MAT-file to write into
\param name variable name for the new array
\param data_vec the array to write. This container has to implement the function size() and
an iterator with tool-functions begin() and end()
*/
template<typename T>
inline void TinyMATWriter_writeContainerAsRow(TinyMATWriterFile* mat, const char* name, const T& data_vec) {
TinyMATWriter_writeContainerAsRow_internalCopy<T>(mat, name, data_vec);
}
/*! \brief write a 1-dimensional vector/list/... of values as a column-vector into a MAT-file
\ingroup tinymatwriter
\param mat the MAT-file to write into
\param name variable name for the new array
\param data_vec the array to write. This container has to implement the function size() and
an iterator with tool-functions begin() and end(). Data-Items (T::value_type)
has to be integer numbers or floats
*/
template<typename T>
inline void TinyMATWriter_writeContainerAsColumn(TinyMATWriterFile* mat, const char* name, const T& data_vec) {
TinyMATWriter_writeContainerAsColumn_internalCopy(mat, name, data_vec);
}
#if (__cplusplus > 199711L) || ( defined(_MSC_VER) && ( _MSC_VER >= 1700 ) )
/*! \brief write a 1-dimensional std::vector of values as a row-vector into a MAT-file
\ingroup tinymatwriter
This is a performance-optimized specialization.
\param mat the MAT-file to write into
\param name variable name for the new array
\param data_vec the array to write. This container has to implement the function size() and
an iterator with tool-functions begin() and end()
*/
template<typename T>
inline void TinyMATWriter_writeContainerAsRow(TinyMATWriterFile* mat, const char* name, const std::vector<T>& data_vec) {
if (data_vec.size()<=0) TinyMATWriter_writeEmptyMatrix(mat, name);
int32_t siz[2]={1, (int32_t)data_vec.size()};
const T* tmp=data_vec.data();
TinyMATWriter_writeMatrixND_rowmajor(mat, name, tmp, siz, 2);
}
/*! \brief write a 1-dimensional std::vector<bool> of values as a row-vector into a MAT-file
\ingroup tinymatwriter
\param mat the MAT-file to write into
\param name variable name for the new array
\param data_vec the array to write. This container has to implement the function size() and
an iterator with tool-functions begin() and end(). Data-Items (T::value_type)
has to be integer numbers or floats
*/
template<>
inline void TinyMATWriter_writeContainerAsRow(TinyMATWriterFile* mat, const char* name, const std::vector<bool>& data_vec) {
TinyMATWriter_writeContainerAsRow_internalCopy<std::vector<bool>>(mat, name, data_vec);
}
/*! \brief write a 1-dimensional std::vector of values as a column-vector into a MAT-file
\ingroup tinymatwriter
This is a performance-optimized specialization.
\param mat the MAT-file to write into
\param name variable name for the new array
\param data_vec the array to write. This container has to implement the function size() and
an iterator with tool-functions begin() and end(). Data-Items (T::value_type)
has to be integer numbers or floats
*/
template<typename T>
inline void TinyMATWriter_writeContainerAsColumn(TinyMATWriterFile* mat, const char* name, const std::vector<T>& data_vec) {
if (data_vec.size() <= 0) {
TinyMATWriter_writeEmptyMatrix(mat, name);
return;
} // if
int32_t siz[2] = { (int32_t)data_vec.size(), 1 };
const T* tmp = data_vec.data();
TinyMATWriter_writeMatrixND_rowmajor(mat, name, tmp, siz, 2);
}
/*! \brief write a 1-dimensional std::vector<bool> of values as a column-vector into a MAT-file
\ingroup tinymatwriter
\param mat the MAT-file to write into
\param name variable name for the new array
\param data_vec the array to write. This container has to implement the function size() and
an iterator with tool-functions begin() and end(). Data-Items (T::value_type)
has to be integer numbers or floats
*/
template<>
inline void TinyMATWriter_writeContainerAsColumn(TinyMATWriterFile* mat, const char* name, const std::vector<bool>& data_vec) {
TinyMATWriter_writeContainerAsColumn_internalCopy(mat, name, data_vec);
}
/*! \brief write a std::vector of std::vector (vecor of row-vectors) as a matrix into a MAT-file
\ingroup tinymatwriter
\param mat the MAT-file to write into
\param name variable name for the new array
\param data_mat the array to write.
*/
template<class T>
inline void TinyMATWriter_writeRowsContainerAsMatrix(TinyMATWriterFile* mat, const char* name, const std::vector<std::vector<T> >& data_mat) {
if (data_mat.size() <= 0) TinyMATWriter_writeEmptyMatrix(mat, name);
int32_t rows = data_mat.size();
int32_t cols = 0;
for (size_t i = 0; i < data_mat.size(); i++) cols = std::max<int32_t>(cols, data_mat[i].size());
T* tmp = (T*)malloc(rows*cols * sizeof(T));
for (int32_t i = 0; i<rows*cols; i++) tmp[i] = 0;
int r = 0;
int c = 0;
for (auto it = data_mat.begin(); it != data_mat.end(); ++it) {
c = 0;
for (auto itc = it->begin(); itc != it->end(); ++itc) {
tmp[c*rows + r] = *itc;
c++;
}
r++;
}
int32_t siz[2] = { rows, cols };
TinyMATWriter_writeMatrixND_colmajor(mat, name, tmp, siz, 2);
free(tmp);
}
/*! \brief write a std::vector of std::vector (vecor of col-vectors) as a matrix into a MAT-file
\ingroup tinymatwriter
\param mat the MAT-file to write into
\param name variable name for the new array
\param data_mat the array to write.
*/
template<class T>
inline void TinyMATWriter_writeColsContainerAsMatrix(TinyMATWriterFile* mat, const char* name, const std::vector<std::vector<T> >& data_mat) {
if (data_mat.size() <= 0) TinyMATWriter_writeEmptyMatrix(mat, name);
int32_t rows = 0;
int32_t cols = data_mat.size();
for (size_t i = 0; i < data_mat.size(); i++) rows = std::max<int32_t>(rows, data_mat[i].size());
T* tmp = (T*)malloc(rows*cols * sizeof(T));
for (int32_t i = 0; i<rows*cols; i++) tmp[i] = 0;
int r = 0;
int c = 0;
for (auto it = data_mat.begin(); it != data_mat.end(); ++it) {
r = 0;
for (auto itc = it->begin(); itc != it->end(); ++itc) {
tmp[c*rows + r] = *itc;
r++;
}
c++;
}
int32_t siz[2] = { rows, cols };
TinyMATWriter_writeMatrixND_colmajor(mat, name, tmp, siz, 2);
free(tmp);
}
#endif
/*! \brief write a 1-dimensional std::list<std::string> into a MAT-file as a cell array
\ingroup tinymatwriter
\param mat the MAT-file to write into
\param name variable name for the new array
\param data the array to write
*/
TINYMATWRITER_EXPORT void TinyMATWriter_writeStringList(TinyMATWriterFile* mat, const char* name, const std::list<std::string>& data);
/*! \brief write a 1-dimensional std::vector<std::string> into a MAT-file as a cell array
\ingroup tinymatwriter
\param mat the MAT-file to write into
\param name variable name for the new array
\param data the array to write
*/
TINYMATWRITER_EXPORT void TinyMATWriter_writeStringVector(TinyMATWriterFile* mat, const char* name, const std::vector<std::string>& data);
/*! \brief write a 1-dimensional std::list<double> into a MAT-file as a 1D matrix
\ingroup tinymatwriter
\param mat the MAT-file to write into
\param name variable name for the new array
\param data the array to write
\param columnVector if \C true, data is stored as a column vector ... otherwise as a row-vetor
*/
TINYMATWRITER_EXPORT void TinyMATWriter_writeDoubleList(TinyMATWriterFile* mat, const char* name, const std::list<double>& data, bool columnVector=false);
/*! \brief write a 1-dimensional std::vector<double> into a MAT-file as a 1D matrix
\ingroup tinymatwriter
\param mat the MAT-file to write into
\param name variable name for the new array
\param data the array to write
\param columnVector if \C true, data is stored as a column vector ... otherwise as a row-vetor
*/
TINYMATWRITER_EXPORT void TinyMATWriter_writeDoubleVector(TinyMATWriterFile* mat, const char* name, const std::vector<double>& data, bool columnVector=false);
/*! \brief write a a std::map<std::string,double> into a MAT-file as a struct
\ingroup tinymatwriter
\param mat the MAT-file to write into
\param name variable name for the new array