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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
|
// CTensor
// A three-dimensional array
//
// Author: Thomas Brox
#ifndef CTENSOR_H
#define CTENSOR_H
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <CMatrix.h>
#include <NMath.h>
inline int int_min(int x, int& y) { return (x<y)?x:y; }
inline int int_max(int x, int& y) { return (x<y)?y:x; }
template <class T>
class CTensor {
public:
// standard constructor
inline CTensor();
// constructor
inline CTensor(const int aXSize, const int aYSize, const int aZSize);
// copy constructor
CTensor(const CTensor<T>& aCopyFrom);
// constructor with implicit filling
CTensor(const int aXSize, const int aYSize, const int aZSize, const T aFillValue);
// destructor
virtual ~CTensor();
// Changes the size of the tensor, data will be lost
void setSize(int aXSize, int aYSize, int aZSize);
// Downsamples the tensor
void downsample(int aNewXSize, int aNewYSize);
void downsample(int aNewXSize, int aNewYSize, CMatrix<float>& aConfidence);
void downsample(int aNewXSize, int aNewYSize, CTensor<float>& aConfidence);
// Upsamples the tensor
void upsample(int aNewXSize, int aNewYSize);
void upsampleBilinear(int aNewXSize, int aNewYSize);
// Fills the tensor with the value aValue (see also operator =)
void fill(const T aValue);
// Fills a rectangular area with the value aValue
void fillRect(const CVector<T>& aValue, int ax1, int ay1, int ax2, int ay2);
// Copies a box from the tensor into aResult, the size of aResult will be adjusted
void cut(CTensor<T>& aResult, int x1, int y1, int z1, int x2, int y2, int z2);
// Copies aCopyFrom at a certain position of the tensor
void paste(CTensor<T>& aCopyFrom, int ax, int ay, int az);
// Mirrors the boundaries, aFrom is the distance from the boundaries where the pixels are copied from,
// aTo is the distance from the boundaries they are copied to
void mirrorLayers(int aFrom, int aTo);
// Transforms the values so that they are all between aMin and aMax
// aInitialMin/Max are initializations for seeking the minimum and maximum, change if your
// data is not in this range or the data type T cannot hold these values
void normalizeEach(T aMin, T aMax, T aInitialMin = -30000, T aInitialMax = 30000);
void normalize(T aMin, T aMax, int aChannel, T aInitialMin = -30000, T aInitialMax = 30000);
void normalize(T aMin, T aMax, T aInitialMin = -30000, T aInitialMax = 30000);
// Converts from RGB to CIELab color space and vice-versa
void rgbToCielab();
void cielabToRGB();
// Draws a line into the image (only for mZSize = 3)
void drawLine(int dStartX, int dStartY, int dEndX, int dEndY, T aValue1 = 255, T aValue2 = 255, T aValue3 = 255);
void drawRect(int dStartX, int dStartY, int dEndX, int dEndY, T aValue1 = 255, T aValue2 = 255, T aValue3 = 255);
// Applies a similarity transform (translation, rotation, scaling) to the image
void applySimilarityTransform(CTensor<T>& aWarped, CMatrix<bool>& aOutside, float tx, float ty, float cx, float cy, float phi, float scale);
// Applies a homography (linear projective transformation) to the image
void applyHomography(CTensor<T>& aWarped, CMatrix<bool>& aOutside, const CMatrix<float>& H);
// Reads the tensor from a file in Mathematica format
void readFromMathematicaFile(const char* aFilename);
// Writes the tensor to a file in Mathematica format
void writeToMathematicaFile(const char* aFilename);
// Reads the tensor from a movie file in IM format
void readFromIMFile(const char* aFilename);
// Writes the tensor to a movie file in IM format
void writeToIMFile(const char* aFilename);
// Reads an image from a PGM file
void readFromPGM(const char* aFilename);
// Writes the tensor in PGM-Format
void writeToPGM(const char* aFilename);
// Extends a XxYx1 tensor to a XxYx3 tensor with three identical layers
void makeColorTensor();
// Reads a color image from a PPM file
void readFromPPM(const char* aFilename);
// Writes the tensor in PPM-Format
void writeToPPM(const char* aFilename);
// Reads the tensor from a PDM file
void readFromPDM(const char* aFilename);
// Writes the tensor in PDM-Format
void writeToPDM(const char* aFilename, char aFeatureType);
// Gives full access to tensor's values
inline T& operator()(const int ax, const int ay, const int az) const;
// Read access with bilinear interpolation
CVector<T> operator()(const float ax, const float ay) const;
// Fills the tensor with the value aValue (equivalent to fill())
inline CTensor<T>& operator=(const T aValue);
// Copies the tensor aCopyFrom to this tensor (size of tensor might change)
CTensor<T>& operator=(const CTensor<T>& aCopyFrom);
// Adds a tensor of same size
CTensor<T>& operator+=(const CTensor<T>& aMatrix);
// Adds a constant to the tensor
CTensor<T>& operator+=(const T aValue);
// Multiplication with a scalar
CTensor<T>& operator*=(const T aValue);
// Returns the minimum value
T min() const;
// Returns the maximum value
T max() const;
// Returns the average value
T avg() const;
// Returns the average value of a specific layer
T avg(int az) const;
// Gives access to the tensor's size
inline int xSize() const;
inline int ySize() const;
inline int zSize() const;
inline int size() const;
// Returns the az layer of the tensor as matrix (slow and fast version)
CMatrix<T> getMatrix(const int az) const;
void getMatrix(CMatrix<T>& aMatrix, const int az) const;
// Copies the matrix components of aMatrix into the az layer of the tensor
void putMatrix(CMatrix<T>& aMatrix, const int az);
// Gives access to the internal data representation (use sparingly)
inline T* data() const;
// Possible interpretations of the third tensor dimension for PDM format
static const char cSpacial = 'S';
static const char cVector = 'V';
static const char cColor = 'C';
static const char cSymmetricMatrix = 'Y';
protected:
int mXSize,mYSize,mZSize;
T *mData;
};
// Provides basic output functionality (only appropriate for very small tensors)
template <class T> std::ostream& operator<<(std::ostream& aStream, const CTensor<T>& aTensor);
// Exceptions thrown by CTensor-------------------------------------------------
// Thrown when one tries to access an element of a tensor which is out of
// the tensor's bounds
struct ETensorRangeOverflow {
ETensorRangeOverflow(const int ax, const int ay, const int az) {
using namespace std;
cerr << "Exception ETensorRangeOverflow: x = " << ax << ", y = " << ay << ", z = " << az << endl;
}
};
// Thrown when the size of a tensor does not match the needed size for a certain operation
struct ETensorIncompatibleSize {
ETensorIncompatibleSize(int ax, int ay, int ax2, int ay2) {
using namespace std;
cerr << "Exception ETensorIncompatibleSize: x = " << ax << ":" << ax2;
cerr << ", y = " << ay << ":" << ay2 << endl;
}
ETensorIncompatibleSize(int ax, int ay, int az) {
std::cerr << "Exception ETensorIncompatibleTensorSize: x = " << ax << ", y = " << ay << ", z= " << az << std::endl;
}
};
// I M P L E M E N T A T I O N --------------------------------------------
//
// You might wonder why there is implementation code in a header file.
// The reason is that not all C++ compilers yet manage separate compilation
// of templates. Inline functions cannot be compiled separately anyway.
// So in this case the whole implementation code is added to the header
// file.
// Users of CTensor should ignore everything that's beyond this line :)
// ------------------------------------------------------------------------
// P U B L I C ------------------------------------------------------------
// standard constructor
template <class T>
inline CTensor<T>::CTensor() {
mData = 0;
mXSize = mYSize = mZSize = 0;
}
// constructor
template <class T>
inline CTensor<T>::CTensor(const int aXSize, const int aYSize, const int aZSize)
: mXSize(aXSize), mYSize(aYSize), mZSize(aZSize) {
mData = new T[aXSize*aYSize*aZSize];
}
// copy constructor
template <class T>
CTensor<T>::CTensor(const CTensor<T>& aCopyFrom)
: mXSize(aCopyFrom.mXSize), mYSize(aCopyFrom.mYSize), mZSize(aCopyFrom.mZSize) {
int wholeSize = mXSize*mYSize*mZSize;
mData = new T[wholeSize];
for (register int i = 0; i < wholeSize; i++)
mData[i] = aCopyFrom.mData[i];
}
// constructor with implicit filling
template <class T>
CTensor<T>::CTensor(const int aXSize, const int aYSize, const int aZSize, const T aFillValue)
: mXSize(aXSize), mYSize(aYSize), mZSize(aZSize) {
mData = new T[aXSize*aYSize*aZSize];
fill(aFillValue);
}
// destructor
template <class T>
CTensor<T>::~CTensor() {
delete[] mData;
}
// setSize
template <class T>
void CTensor<T>::setSize(int aXSize, int aYSize, int aZSize) {
if (mData != 0) delete[] mData;
mData = new T[aXSize*aYSize*aZSize];
mXSize = aXSize;
mYSize = aYSize;
mZSize = aZSize;
}
//downsample
template <class T>
void CTensor<T>::downsample(int aNewXSize, int aNewYSize) {
T* mData2 = new T[aNewXSize*aNewYSize*mZSize];
int aSize = aNewXSize*aNewYSize;
for (int z = 0; z < mZSize; z++) {
CMatrix<T> aTemp(mXSize,mYSize);
getMatrix(aTemp,z);
aTemp.downsample(aNewXSize,aNewYSize);
for (int i = 0; i < aSize; i++)
mData2[i+z*aSize] = aTemp.data()[i];
}
delete[] mData;
mData = mData2;
mXSize = aNewXSize;
mYSize = aNewYSize;
}
template <class T>
void CTensor<T>::downsample(int aNewXSize, int aNewYSize, CMatrix<float>& aConfidence) {
T* mData2 = new T[aNewXSize*aNewYSize*mZSize];
int aSize = aNewXSize*aNewYSize;
for (int z = 0; z < mZSize; z++) {
CMatrix<T> aTemp(mXSize,mYSize);
getMatrix(aTemp,z);
aTemp.downsample(aNewXSize,aNewYSize,aConfidence);
for (int i = 0; i < aSize; i++)
mData2[i+z*aSize] = aTemp.data()[i];
}
delete[] mData;
mData = mData2;
mXSize = aNewXSize;
mYSize = aNewYSize;
}
template <class T>
void CTensor<T>::downsample(int aNewXSize, int aNewYSize, CTensor<float>& aConfidence) {
T* mData2 = new T[aNewXSize*aNewYSize*mZSize];
int aSize = aNewXSize*aNewYSize;
CMatrix<float> aConf(mXSize,mYSize);
for (int z = 0; z < mZSize; z++) {
CMatrix<T> aTemp(mXSize,mYSize);
getMatrix(aTemp,z);
aConfidence.getMatrix(aConf,z);
aTemp.downsample(aNewXSize,aNewYSize,aConf);
for (int i = 0; i < aSize; i++)
mData2[i+z*aSize] = aTemp.data()[i];
}
delete[] mData;
mData = mData2;
mXSize = aNewXSize;
mYSize = aNewYSize;
}
// upsample
template <class T>
void CTensor<T>::upsample(int aNewXSize, int aNewYSize) {
T* mData2 = new T[aNewXSize*aNewYSize*mZSize];
int aSize = aNewXSize*aNewYSize;
for (int z = 0; z < mZSize; z++) {
CMatrix<T> aTemp(mXSize,mYSize);
getMatrix(aTemp,z);
aTemp.upsample(aNewXSize,aNewYSize);
for (int i = 0; i < aSize; i++)
mData2[i+z*aSize] = aTemp.data()[i];
}
delete[] mData;
mData = mData2;
mXSize = aNewXSize;
mYSize = aNewYSize;
}
// upsampleBilinear
template <class T>
void CTensor<T>::upsampleBilinear(int aNewXSize, int aNewYSize) {
T* mData2 = new T[aNewXSize*aNewYSize*mZSize];
int aSize = aNewXSize*aNewYSize;
for (int z = 0; z < mZSize; z++) {
CMatrix<T> aTemp(mXSize,mYSize);
getMatrix(aTemp,z);
aTemp.upsampleBilinear(aNewXSize,aNewYSize);
for (int i = 0; i < aSize; i++)
mData2[i+z*aSize] = aTemp.data()[i];
}
delete[] mData;
mData = mData2;
mXSize = aNewXSize;
mYSize = aNewYSize;
}
// fill
template <class T>
void CTensor<T>::fill(const T aValue) {
int wholeSize = mXSize*mYSize*mZSize;
for (register int i = 0; i < wholeSize; i++)
mData[i] = aValue;
}
// fillRect
template <class T>
void CTensor<T>::fillRect(const CVector<T>& aValue, int ax1, int ay1, int ax2, int ay2) {
for (int z = 0; z < mZSize; z++) {
T val = aValue(z);
for (int y = int_max(0,ay1); y <= int_min(ySize()-1,ay2); y++)
for (register int x = int_max(0,ax1); x <= int_min(xSize()-1,ax2); x++)
operator()(x,y,z) = val;
}
}
// cut
template <class T>
void CTensor<T>::cut(CTensor<T>& aResult, int x1, int y1, int z1, int x2, int y2, int z2) {
aResult.mXSize = x2-x1+1;
aResult.mYSize = y2-y1+1;
aResult.mZSize = z2-z1+1;
delete[] aResult.mData;
aResult.mData = new T[aResult.mXSize*aResult.mYSize*aResult.mZSize];
for (int z = z1; z <= z2; z++)
for (int y = y1; y <= y2; y++)
for (int x = x1; x <= x2; x++)
aResult(x-x1,y-y1,z-z1) = operator()(x,y,z);
}
// paste
template <class T>
void CTensor<T>::paste(CTensor<T>& aCopyFrom, int ax, int ay, int az) {
for (int z = 0; z < aCopyFrom.zSize(); z++)
for (int y = 0; y < aCopyFrom.ySize(); y++)
for (int x = 0; x < aCopyFrom.xSize(); x++)
operator()(ax+x,ay+y,az+z) = aCopyFrom(x,y,z);
}
// mirrorLayers
template <class T>
void CTensor<T>::mirrorLayers(int aFrom, int aTo) {
for (int z = 0; z < mZSize; z++) {
int aToXIndex = mXSize-aTo-1;
int aToYIndex = mYSize-aTo-1;
int aFromXIndex = mXSize-aFrom-1;
int aFromYIndex = mYSize-aFrom-1;
for (int y = aFrom; y <= aFromYIndex; y++) {
operator()(aTo,y,z) = operator()(aFrom,y,z);
operator()(aToXIndex,y,z) = operator()(aFromXIndex,y,z);
}
for (int x = aTo; x <= aToXIndex; x++) {
operator()(x,aTo,z) = operator()(x,aFrom,z);
operator()(x,aToYIndex,z) = operator()(x,aFromYIndex,z);
}
}
}
// normalize
template <class T>
void CTensor<T>::normalizeEach(T aMin, T aMax, T aInitialMin, T aInitialMax) {
for (int k = 0; k < mZSize; k++)
normalize(aMin,aMax,k,aInitialMin,aInitialMax);
}
template <class T>
void CTensor<T>::normalize(T aMin, T aMax, int aChannel, T aInitialMin, T aInitialMax) {
int aChannelSize = mXSize*mYSize;
T aCurrentMin = aInitialMax;
T aCurrentMax = aInitialMin;
int aIndex = aChannelSize*aChannel;
for (int i = 0; i < aChannelSize; i++) {
if (mData[aIndex] > aCurrentMax) aCurrentMax = mData[aIndex];
else if (mData[aIndex] < aCurrentMin) aCurrentMin = mData[aIndex];
aIndex++;
}
T aTemp1 = aCurrentMin - aMin;
T aTemp2 = (aCurrentMax-aCurrentMin);
if (aTemp2 == 0) aTemp2 = 1;
else aTemp2 = (aMax-aMin)/aTemp2;
aIndex = aChannelSize*aChannel;
for (int i = 0; i < aChannelSize; i++) {
mData[aIndex] -= aTemp1;
mData[aIndex] *= aTemp2;
aIndex++;
}
}
// drawLine
template <class T>
void CTensor<T>::drawLine(int dStartX, int dStartY, int dEndX, int dEndY, T aValue1, T aValue2, T aValue3) {
int aOffset1 = mXSize*mYSize;
int aOffset2 = 2*aOffset1;
// vertical line
if (dStartX == dEndX) {
if (dStartX < 0 || dStartX >= mXSize) return;
int x = dStartX;
if (dStartY < dEndY) {
for (int y = dStartY; y <= dEndY; y++)
if (y >= 0 && y < mYSize) {
mData[x+y*mXSize] = aValue1;
mData[x+y*mXSize+aOffset1] = aValue2;
mData[x+y*mXSize+aOffset2] = aValue3;
}
}
else {
for (int y = dStartY; y >= dEndY; y--)
if (y >= 0 && y < mYSize) {
mData[x+y*mXSize] = aValue1;
mData[x+y*mXSize+aOffset1] = aValue2;
mData[x+y*mXSize+aOffset2] = aValue3;
}
}
return;
}
// horizontal line
if (dStartY == dEndY) {
if (dStartY < 0 || dStartY >= mYSize) return;
int y = dStartY;
if (dStartX < dEndX) {
for (int x = dStartX; x <= dEndX; x++)
if (x >= 0 && x < mXSize) {
mData[x+y*mXSize] = aValue1;
mData[x+y*mXSize+aOffset1] = aValue2;
mData[x+y*mXSize+aOffset2] = aValue3;
}
}
else {
for (int x = dStartX; x >= dEndX; x--)
if (x >= 0 && x < mXSize) {
mData[x+y*mXSize] = aValue1;
mData[x+y*mXSize+aOffset1] = aValue2;
mData[x+y*mXSize+aOffset2] = aValue3;
}
}
return;
}
float m = float(dStartY - dEndY) / float(dStartX - dEndX);
float invm = 1.0/m;
if (fabs(m) > 1.0) {
if (dEndY > dStartY) {
for (int y = dStartY; y <= dEndY; y++) {
int x = (int)(0.5+dStartX+(y-dStartY)*invm);
if (x >= 0 && x < mXSize && y >= 0 && y < mYSize) {
mData[x+y*mXSize] = aValue1;
mData[x+y*mXSize+aOffset1] = aValue2;
mData[x+y*mXSize+aOffset2] = aValue3;
}
}
}
else {
for (int y = dStartY; y >= dEndY; y--) {
int x = (int)(0.5+dStartX+(y-dStartY)*invm);
if (x >= 0 && x < mXSize && y >= 0 && y < mYSize) {
mData[x+y*mXSize] = aValue1;
mData[x+y*mXSize+aOffset1] = aValue2;
mData[x+y*mXSize+aOffset2] = aValue3;
}
}
}
}
else {
if (dEndX > dStartX) {
for (int x = dStartX; x <= dEndX; x++) {
int y = (int)(0.5+dStartY+(x-dStartX)*m);
if (x >= 0 && x < mXSize && y >= 0 && y < mYSize) {
mData[x+y*mXSize] = aValue1;
mData[x+y*mXSize+aOffset1] = aValue2;
mData[x+y*mXSize+aOffset2] = aValue3;
}
}
}
else {
for (int x = dStartX; x >= dEndX; x--) {
int y = (int)(0.5+dStartY+(x-dStartX)*m);
if (x >= 0 && x < mXSize && y >= 0 && y < mYSize) {
mData[x+y*mXSize] = aValue1;
mData[x+y*mXSize+aOffset1] = aValue2;
mData[x+y*mXSize+aOffset2] = aValue3;
}
}
}
}
}
// drawRect
template <class T>
void CTensor<T>::drawRect(int dStartX, int dStartY, int dEndX, int dEndY, T aValue1, T aValue2, T aValue3) {
drawLine(dStartX,dStartY,dEndX,dStartY,aValue1,aValue2,aValue3);
drawLine(dStartX,dEndY,dEndX,dEndY,aValue1,aValue2,aValue3);
drawLine(dStartX,dStartY,dStartX,dEndY,aValue1,aValue2,aValue3);
drawLine(dEndX,dStartY,dEndX,dEndY,aValue1,aValue2,aValue3);
}
template <class T>
void CTensor<T>::normalize(T aMin, T aMax, T aInitialMin, T aInitialMax) {
int aSize = mXSize*mYSize*mZSize;
T aCurrentMin = aInitialMax;
T aCurrentMax = aInitialMin;
for (int i = 0; i < aSize; i++) {
if (mData[i] > aCurrentMax) aCurrentMax = mData[i];
else if (mData[i] < aCurrentMin) aCurrentMin = mData[i];
}
T aTemp1 = aCurrentMin - aMin;
T aTemp2 = (aCurrentMax-aCurrentMin);
if (aTemp2 == 0) aTemp2 = 1;
else aTemp2 = (aMax-aMin)/aTemp2;
for (int i = 0; i < aSize; i++) {
mData[i] -= aTemp1;
mData[i] *= aTemp2;
}
}
template <class T>
void CTensor<T>::rgbToCielab() {
for (int y = 0; y < mYSize; y++)
for (int x = 0; x < mXSize; x++) {
float R = operator()(x,y,0)*0.003921569;
float G = operator()(x,y,1)*0.003921569;
float B = operator()(x,y,2)*0.003921569;
if (R>0.0031308) R = pow((R + 0.055)*0.9478673, 2.4); else R *= 0.077399381;
if (G>0.0031308) G = pow((G + 0.055)*0.9478673, 2.4); else G *= 0.077399381;
if (B>0.0031308) B = pow((B + 0.055)*0.9478673, 2.4); else B *= 0.077399381;
//Observer. = 2?, Illuminant = D65
float X = R * 0.4124 + G * 0.3576 + B * 0.1805;
float Y = R * 0.2126 + G * 0.7152 + B * 0.0722;
float Z = R * 0.0193 + G * 0.1192 + B * 0.9505;
X *= 1.052111;
Z *= 0.918417;
if (X > 0.008856) X = pow(X,0.33333333333); else X = 7.787*X + 0.137931034;
if (Y > 0.008856) Y = pow(Y,0.33333333333); else Y = 7.787*Y + 0.137931034;
if (Z > 0.008856) Z = pow(Z,0.33333333333); else Z = 7.787*Z + 0.137931034;
operator()(x,y,0) = 1000.0*((295.8*Y) - 40.8)/255.0;
operator()(x,y,1) = 128.0+637.5*(X-Y);
operator()(x,y,2) = 128.0+255.0*(Y-Z);
}
}
template <class T>
void CTensor<T>::cielabToRGB() {
for (int y = 0; y < mYSize; y++)
for (int x = 0; x < mXSize; x++) {
float L = operator()(x,y,0)*0.255;
float A = operator()(x,y,1);
float B = operator()(x,y,2);
float Y = (L+40.8)*0.00338066;
float X = (A-128.0+637.5*Y)*0.0015686;
float Z = (128.0+255.0*Y-B)*0.00392157;
float temp = Y*Y*Y;
if (temp > 0.008856) Y = temp;
else Y = (Y-0.137931034)*0.12842;
temp = X*X*X;
if (temp > 0.008856) X = temp;
else X = (X-0.137931034)*0.12842;
temp = Z*Z*Z;
if (temp > 0.008856) Z = temp;
else Z = (Z-0.137931034)*0.12842;
X *= 0.95047;
Y *= 1.0;
Z *= 1.08883;
float r = 3.2406*X-1.5372*Y-0.4986*Z;
float g = -0.9689*X+1.8758*Y+0.0415*Z;
float b = 0.0557*X-0.204*Y+1.057*Z;
if (r < 0) r = 0;
temp = 1.055*pow(r,0.41667)-0.055;
if (temp > 0.0031308) r = temp;
else r *= 12.92;
if (g < 0) g = 0;
temp = 1.055*pow(g,0.41667)-0.055;
if (temp > 0.0031308) g = temp;
else g *= 12.92;
if (b < 0) b = 0;
temp = 1.055*pow(b,0.41667)-0.055;
if (temp > 0.0031308) b = temp;
else b *= 12.92;
operator()(x,y,0) = 255.0*r;
operator()(x,y,1) = 255.0*g;
operator()(x,y,2) = 255.0*b;
}
}
// applySimilarityTransform
template <class T>
void CTensor<T>::applySimilarityTransform(CTensor<T>& aWarped, CMatrix<bool>& aOutside, float tx, float ty, float cx, float cy, float phi, float scale) {
float cosphi = scale*cos(phi);
float sinphi = scale*sin(phi);
int aSize = mXSize*mYSize;
int aWarpedSize = aWarped.xSize()*aWarped.ySize();
float ctx = cx+tx-cx*cosphi+cy*sinphi;
float cty = cy+ty-cy*cosphi-cx*sinphi;
aOutside = false;
int i = 0;
for (int y = 0; y < aWarped.ySize(); y++)
for (int x = 0; x < aWarped.xSize(); x++,i++) {
float xf = x; float yf = y;
float ax = xf*cosphi-yf*sinphi+ctx;
float ay = yf*cosphi+xf*sinphi+cty;
int x1 = (int)ax; int y1 = (int)ay;
float alphaX = ax-x1; float alphaY = ay-y1;
float betaX = 1.0-alphaX; float betaY = 1.0-alphaY;
if (x1 < 0 || y1 < 0 || x1+1 >= mXSize || y1+1 >= mYSize) aOutside.data()[i] = true;
else {
int j = y1*mXSize+x1;
for (int k = 0; k < mZSize; k++) {
float a = betaX*mData[j] +alphaX*mData[j+1];
float b = betaX*mData[j+mXSize]+alphaX*mData[j+1+mXSize];
aWarped.data()[i+k*aWarpedSize] = betaY*a+alphaY*b;
j += aSize;
}
}
}
}
// applyHomography
template <class T>
void CTensor<T>::applyHomography(CTensor<T>& aWarped, CMatrix<bool>& aOutside, const CMatrix<float>& H) {
int aSize = mXSize*mYSize;
int aWarpedSize = aWarped.xSize()*aWarped.ySize();
aOutside = false;
int i = 0;
for (int y = 0; y < aWarped.ySize(); y++)
for (int x = 0; x < aWarped.xSize(); x++,i++) {
float xf = x; float yf = y;
float ax = H.data()[0]*xf+H.data()[1]*yf+H.data()[2];
float ay = H.data()[3]*xf+H.data()[4]*yf+H.data()[5];
float az = H.data()[6]*xf+H.data()[7]*yf+H.data()[8];
float invaz = 1.0/az;
ax *= invaz; ay *= invaz;
int x1 = (int)ax; int y1 = (int)ay;
float alphaX = ax-x1; float alphaY = ay-y1;
float betaX = 1.0-alphaX; float betaY = 1.0-alphaY;
if (x1 < 0 || y1 < 0 || x1+1 >= mXSize || y1+1 >= mYSize) aOutside.data()[i] = true;
else {
int j = y1*mXSize+x1;
for (int k = 0; k < mZSize; k++) {
float a = betaX*mData[j] +alphaX*mData[j+1];
float b = betaX*mData[j+mXSize]+alphaX*mData[j+1+mXSize];
aWarped.data()[i+k*aWarpedSize] = betaY*a+alphaY*b;
j += aSize;
}
}
}
}
// -----------------------------------------------------------------------------
// File I/O
// -----------------------------------------------------------------------------
// readFromMathematicaFile
template <class T>
void CTensor<T>::readFromMathematicaFile(const char* aFilename) {
using namespace std;
// Read the whole file and store data in aData
// Ignore blanks, tabs and lines
// Also ignore Mathematica comments (* ... *)
ifstream aStream(aFilename);
string aData;
char aChar;
bool aBracketFound = false;
bool aStarFound = false;
bool aCommentFound = false;
while (aStream.get(aChar))
if (aChar != ' ' && aChar != '\t' && aChar != '\n') {
if (aCommentFound) {
if (!aStarFound && aChar == '*') aStarFound = true;
else {
if (aStarFound && aChar == ')') aCommentFound = false;
aStarFound = false;
}
}
else {
if (!aBracketFound && aChar == '(') aBracketFound = true;
else {
if (aBracketFound && aChar == '*') aCommentFound = true;
else aData += aChar;
aBracketFound = false;
}
}
}
// Count the number of braces and double braces to figure out z- and y-Size of tensor
int aDoubleBraceCount = 0;
int aBraceCount = 0;
int aPos = 0;
while ((aPos = aData.find_first_of('{',aPos)+1) > 0) {
aBraceCount++;
if (aData[aPos] == '{' && aData[aPos+1] != '{') aDoubleBraceCount++;
}
// Count the number of commas in the first section to figure out xSize of tensor
int aCommaCount = 0;
aPos = 0;
while (aData[aPos] != '}') {
if (aData[aPos] == ',') aCommaCount++;
aPos++;
}
// Adapt size of tensor
if (mData != 0) delete[] mData;
mXSize = aCommaCount+1;
mYSize = (aBraceCount-1-aDoubleBraceCount) / aDoubleBraceCount;
mZSize = aDoubleBraceCount;
mData = new T[mXSize*mYSize*mZSize];
// Analyse file ---------------
aPos = 0;
if (aData[aPos] != '{') throw EInvalidFileFormat("Mathematica");
aPos++;
for (int z = 0; z < mZSize; z++) {
if (aData[aPos] != '{') throw EInvalidFileFormat("Mathematica");
aPos++;
for (int y = 0; y < mYSize; y++) {
if (aData[aPos] != '{') throw EInvalidFileFormat("Mathematica");
aPos++;
for (int x = 0; x < mXSize; x++) {
int oldPos = aPos;
if (x+1 < mXSize) aPos = aData.find_first_of(',',aPos);
else aPos = aData.find_first_of('}',aPos);
#ifdef GNU_COMPILER
string s = aData.substr(oldPos,aPos-oldPos);
istrstream is(s.c_str());
#else
string s = aData.substr(oldPos,aPos-oldPos);
istringstream is(s);
#endif
T aItem;
is >> aItem;
operator()(x,y,z) = aItem;
aPos++;
}
if (y+1 < mYSize) {
if (aData[aPos] != ',') throw EInvalidFileFormat("Mathematica");
aPos++;
while (aData[aPos] != '{')
aPos++;
}
}
aPos++;
if (z+1 < mZSize) {
if (aData[aPos] != ',') throw EInvalidFileFormat("Mathematica");
aPos++;
while (aData[aPos] != '{')
aPos++;
}
}
}
// writeToMathematicaFile
template <class T>
void CTensor<T>::writeToMathematicaFile(const char* aFilename) {
using namespace std;
ofstream aStream(aFilename);
aStream << '{';
for (int z = 0; z < mZSize; z++) {
aStream << '{';
for (int y = 0; y < mYSize; y++) {
aStream << '{';
for (int x = 0; x < mXSize; x++) {
aStream << operator()(x,y,z);
if (x+1 < mXSize) aStream << ',';
}
aStream << '}';
if (y+1 < mYSize) aStream << ",\n";
}
aStream << '}';
if (z+1 < mZSize) aStream << ",\n";
}
aStream << '}';
}
// readFromIMFile
template <class T>
void CTensor<T>::readFromIMFile(const char* aFilename) {
FILE *aStream;
aStream = fopen(aFilename,"rb");
// Read image data
for (int i = 0; i < mXSize*mYSize*mZSize; i++)
mData[i] = getc(aStream);
fclose(aStream);
}
// writeToIMFile
template <class T>
void CTensor<T>::writeToIMFile(const char *aFilename) {
FILE *aStream;
aStream = fopen(aFilename,"wb");
// write data
for (int i = 0; i < mXSize*mYSize*mZSize; i++) {
char dummy = (char)mData[i];
fwrite(&dummy,1,1,aStream);
}
fclose(aStream);
}
// readFromPGM
template <class T>
void CTensor<T>::readFromPGM(const char* aFilename) {
FILE *aStream;
aStream = fopen(aFilename,"rb");
if (aStream == 0) std::cerr << "File not found: " << aFilename << std::endl;
int dummy;
// Find beginning of file (P5)
while (getc(aStream) != 'P');
if (getc(aStream) != '5') throw EInvalidFileFormat("PGM");
do
dummy = getc(aStream);
while (dummy != '\n' && dummy != ' ');
// Remove comments and empty lines
dummy = getc(aStream);
while (dummy == '#') {
while (getc(aStream) != '\n');
dummy = getc(aStream);
}
while (dummy == '\n')
dummy = getc(aStream);
// Read image size
mXSize = dummy-48;
while ((dummy = getc(aStream)) >= 48 && dummy < 58)
mXSize = 10*mXSize+dummy-48;
while ((dummy = getc(aStream)) < 48 || dummy >= 58);
mYSize = dummy-48;
while ((dummy = getc(aStream)) >= 48 && dummy < 58)
mYSize = 10*mYSize+dummy-48;
mZSize = 1;
while (dummy != '\n' && dummy != ' ')
dummy = getc(aStream);
while (dummy != '\n' && dummy != ' ')
dummy = getc(aStream);
// Adjust size of data structure
delete[] mData;
mData = new T[mXSize*mYSize];
// Read image data
for (int i = 0; i < mXSize*mYSize; i++)
mData[i] = getc(aStream);
fclose(aStream);
}
// writeToPGM
template <class T>
void CTensor<T>::writeToPGM(const char* aFilename) {
int rows = (int)floor(sqrt(mZSize));
int cols = (int)ceil(mZSize*1.0/rows);
FILE* outimage = fopen(aFilename, "wb");
fprintf(outimage, "P5 \n");
fprintf(outimage, "%ld %ld \n255\n", cols*mXSize,rows*mYSize);
for (int r = 0; r < rows; r++)
for (int y = 0; y < mYSize; y++)
for (int c = 0; c < cols; c++)
for (int x = 0; x < mXSize; x++) {
unsigned char aHelp;
if (r*cols+c >= mZSize) aHelp = 0;
else aHelp = (unsigned char)operator()(x,y,r*cols+c);
fwrite (&aHelp, sizeof(unsigned char), 1, outimage);
}
fclose(outimage);
}
// makeColorTensor
template <class T>
void CTensor<T>::makeColorTensor() {
if (mZSize != 1) return;
int aSize = mXSize*mYSize;
int a2Size = 2*aSize;
T* aNewData = new T[aSize*3];
for (int i = 0; i < aSize; i++)
aNewData[i] = aNewData[i+aSize] = aNewData[i+a2Size] = mData[i];
mZSize = 3;
delete[] mData;
mData = aNewData;
}
// readFromPPM
template <class T>
void CTensor<T>::readFromPPM(const char* aFilename) {
FILE *aStream;
aStream = fopen(aFilename,"rb");
if (aStream == 0)
std::cerr << "File not found: " << aFilename << std::endl;
int dummy;
// Find beginning of file (P6)
while (getc(aStream) != 'P');
dummy = getc(aStream);
if (dummy == '5') mZSize = 1;
else if (dummy == '6') mZSize = 3;
else throw EInvalidFileFormat("PPM");
do dummy = getc(aStream); while (dummy != '\n' && dummy != ' ');
// Remove comments and empty lines
dummy = getc(aStream);
while (dummy == '#') {
while (getc(aStream) != '\n');
dummy = getc(aStream);
}
while (dummy == '\n')
dummy = getc(aStream);
// Read image size
mXSize = dummy-48;
while ((dummy = getc(aStream)) >= 48 && dummy < 58)
mXSize = 10*mXSize+dummy-48;
while ((dummy = getc(aStream)) < 48 || dummy >= 58);
mYSize = dummy-48;
while ((dummy = getc(aStream)) >= 48 && dummy < 58)
mYSize = 10*mYSize+dummy-48;
while (dummy != '\n' && dummy != ' ')
dummy = getc(aStream);
while (dummy < 48 || dummy >= 58) dummy = getc(aStream);
while ((dummy = getc(aStream)) >= 48 && dummy < 58);
if (dummy != '\n') while (getc(aStream) != '\n');
// Adjust size of data structure
delete[] mData;
mData = new T[mXSize*mYSize*mZSize];
// Read image data
int aSize = mXSize*mYSize;
if (mZSize == 1)
for (int i = 0; i < aSize; i++)
mData[i] = getc(aStream);
else {
int aSizeTwice = aSize+aSize;
for (int i = 0; i < aSize; i++) {
mData[i] = getc(aStream);
mData[i+aSize] = getc(aStream);
mData[i+aSizeTwice] = getc(aStream);
}
}
fclose(aStream);
}
// writeToPPM
template <class T>
void CTensor<T>::writeToPPM(const char* aFilename) {
FILE* outimage = fopen(aFilename, "wb");
fprintf(outimage, "P6 \n");
fprintf(outimage, "%d %d \n255\n", mXSize,mYSize);
for (int y = 0; y < mYSize; y++)
for (int x = 0; x < mXSize; x++) {
unsigned char aHelp = (unsigned char)operator()(x,y,0);
fwrite (&aHelp, sizeof(unsigned char), 1, outimage);
aHelp = (unsigned char)operator()(x,y,1);
fwrite (&aHelp, sizeof(unsigned char), 1, outimage);
aHelp = (unsigned char)operator()(x,y,2);
fwrite (&aHelp, sizeof(unsigned char), 1, outimage);
}
fclose(outimage);
}
// readFromPDM
template <class T>
void CTensor<T>::readFromPDM(const char* aFilename) {
std::ifstream aStream(aFilename);
std::string s;
// Read header
aStream >> s;
if (s != "P9") throw EInvalidFileFormat("PDM");
char aFeatureType;
aStream >> aFeatureType;
aStream >> s;
aStream >> mXSize;
aStream >> mYSize;
aStream >> mZSize;
aStream >> s;
// Adjust size of data structure
delete[] mData;
mData = new T[mXSize*mYSize*mZSize];
// Read data
for (int i = 0; i < mXSize*mYSize*mZSize; i++)
aStream >> mData[i];
}
// writeToPDM
template <class T>
void CTensor<T>::writeToPDM(const char* aFilename, char aFeatureType) {
std::ofstream aStream(aFilename);
// write header
aStream << "P9" << std::endl;
aStream << aFeatureType << "SS" << std::endl;
aStream << mZSize << ' ' << mYSize << ' ' << mXSize << std::endl;
aStream << "F" << std::endl;
// write data
for (int i = 0; i < mXSize*mYSize*mZSize; i++) {
aStream << mData[i];
if (i % 8 == 0) aStream << std::endl;
else aStream << ' ';
}
}
// operator ()
template <class T>
inline T& CTensor<T>::operator()(const int ax, const int ay, const int az) const {
#ifdef _DEBUG
if (ax >= mXSize || ay >= mYSize || az >= mZSize || ax < 0 || ay < 0 || az < 0)
throw ETensorRangeOverflow(ax,ay,az);
#endif
return mData[mXSize*(mYSize*az+ay)+ax];
}
template <class T>
CVector<T> CTensor<T>::operator()(const float ax, const float ay) const {
CVector<T> aResult(mZSize);
int x1 = (int)ax;
int y1 = (int)ay;
int x2 = x1+1;
int y2 = y1+1;
#ifdef _DEBUG
if (x2 >= mXSize || y2 >= mYSize || x1 < 0 || y1 < 0) throw ETensorRangeOverflow(ax,ay,0);
#endif
float alphaX = ax-x1; float alphaXTrans = 1.0-alphaX;
float alphaY = ay-y1; float alphaYTrans = 1.0-alphaY;
for (int k = 0; k < mZSize; k++) {
float a = alphaXTrans*operator()(x1,y1,k)+alphaX*operator()(x2,y1,k);
float b = alphaXTrans*operator()(x1,y2,k)+alphaX*operator()(x2,y2,k);
aResult(k) = alphaYTrans*a+alphaY*b;
}
return aResult;
}
// operator =
template <class T>
inline CTensor<T>& CTensor<T>::operator=(const T aValue) {
fill(aValue);
return *this;
}
template <class T>
CTensor<T>& CTensor<T>::operator=(const CTensor<T>& aCopyFrom) {
if (this != &aCopyFrom) {
delete[] mData;
if (aCopyFrom.mData == 0) {
mData = 0; mXSize = 0; mYSize = 0; mZSize = 0;
}
else {
mXSize = aCopyFrom.mXSize;
mYSize = aCopyFrom.mYSize;
mZSize = aCopyFrom.mZSize;
int wholeSize = mXSize*mYSize*mZSize;
mData = new T[wholeSize];
for (register int i = 0; i < wholeSize; i++)
mData[i] = aCopyFrom.mData[i];
}
}
return *this;
}
// operator +=
template <class T>
CTensor<T>& CTensor<T>::operator+=(const CTensor<T>& aTensor) {
#ifdef _DEBUG
if (mXSize != aTensor.mXSize || mYSize != aTensor.mYSize || mZSize != aTensor.mZSize)
throw ETensorIncompatibleSize(mXSize,mYSize,mZSize);
#endif
int wholeSize = size();
for (int i = 0; i < wholeSize; i++)
mData[i] += aTensor.mData[i];
return *this;
}
// operator +=
template <class T>
CTensor<T>& CTensor<T>::operator+=(const T aValue) {
int wholeSize = mXSize*mYSize*mZSize;
for (int i = 0; i < wholeSize; i++)
mData[i] += aValue;
return *this;
}
// operator *=
template <class T>
CTensor<T>& CTensor<T>::operator*=(const T aValue) {
int wholeSize = mXSize*mYSize*mZSize;
for (int i = 0; i < wholeSize; i++)
mData[i] *= aValue;
return *this;
}
// min
template <class T>
T CTensor<T>::min() const {
T aMin = mData[0];
int aSize = mXSize*mYSize*mZSize;
for (int i = 1; i < aSize; i++)
if (mData[i] < aMin) aMin = mData[i];
return aMin;
}
// max
template <class T>
T CTensor<T>::max() const {
T aMax = mData[0];
int aSize = mXSize*mYSize*mZSize;
for (int i = 1; i < aSize; i++)
if (mData[i] > aMax) aMax = mData[i];
return aMax;
}
// avg
template <class T>
T CTensor<T>::avg() const {
T aAvg = 0;
for (int z = 0; z < mZSize; z++)
aAvg += avg(z);
return aAvg/mZSize;
}
template <class T>
T CTensor<T>::avg(int az) const {
T aAvg = 0;
int aSize = mXSize*mYSize;
int aTemp = (az+1)*aSize;
for (int i = az*aSize; i < aTemp; i++)
aAvg += mData[i];
return aAvg/aSize;
}
// xSize
template <class T>
inline int CTensor<T>::xSize() const {
return mXSize;
}
// ySize
template <class T>
inline int CTensor<T>::ySize() const {
return mYSize;
}
// zSize
template <class T>
inline int CTensor<T>::zSize() const {
return mZSize;
}
// size
template <class T>
inline int CTensor<T>::size() const {
return mXSize*mYSize*mZSize;
}
// getMatrix
template <class T>
CMatrix<T> CTensor<T>::getMatrix(const int az) const {
CMatrix<T> aTemp(mXSize,mYSize);
int aMatrixSize = mXSize*mYSize;
int aOffset = az*aMatrixSize;
for (int i = 0; i < aMatrixSize; i++)
aTemp.data()[i] = mData[i+aOffset];
return aTemp;
}
// getMatrix
template <class T>
void CTensor<T>::getMatrix(CMatrix<T>& aMatrix, const int az) const {
if (aMatrix.xSize() != mXSize || aMatrix.ySize() != mYSize)
throw ETensorIncompatibleSize(aMatrix.xSize(),aMatrix.ySize(),mXSize,mYSize);
int aMatrixSize = mXSize*mYSize;
int aOffset = az*aMatrixSize;
for (int i = 0; i < aMatrixSize; i++)
aMatrix.data()[i] = mData[i+aOffset];
}
// putMatrix
template <class T>
void CTensor<T>::putMatrix(CMatrix<T>& aMatrix, const int az) {
if (aMatrix.xSize() != mXSize || aMatrix.ySize() != mYSize)
throw ETensorIncompatibleSize(aMatrix.xSize(),aMatrix.ySize(),mXSize,mYSize);
int aMatrixSize = mXSize*mYSize;
int aOffset = az*aMatrixSize;
for (int i = 0; i < aMatrixSize; i++)
mData[i+aOffset] = aMatrix.data()[i];
}
// data()
template <class T>
inline T* CTensor<T>::data() const {
return mData;
}
// N O N - M E M B E R F U N C T I O N S --------------------------------------
// operator <<
template <class T>
std::ostream& operator<<(std::ostream& aStream, const CTensor<T>& aTensor) {
for (int z = 0; z < aTensor.zSize(); z++) {
for (int y = 0; y < aTensor.ySize(); y++) {
for (int x = 0; x < aTensor.xSize(); x++)
aStream << aTensor(x,y,z) << ' ';
aStream << std::endl;
}
aStream << std::endl;
}
return aStream;
}
#endif
|