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
|
// CTensor4D
// A four-dimensional array
//
// Author: Thomas Brox
// Last change: 05.11.2001
//-------------------------------------------------------------------------
// Note:
// There is a difference between the GNU Compiler's STL and the standard
// concerning the definition and usage of string streams as well as substrings.
// Thus if using a GNU Compiler you should write #define GNU_COMPILER at the
// beginning of your program.
//
// Another Note:
// Linker problems occured in connection with <vector> from the STL.
// In this case you should include this file in a namespace.
// Example:
// namespace NTensor4D {
// #include <CTensor4D.h>
// }
// After including other packages you can then write:
// using namespace NTensor4D;
#ifndef CTENSOR4D_H
#define CTENSOR4D_H
#include <iostream>
#include <fstream>
#include <string>
#ifdef GNU_COMPILER
#include <strstream>
#else
#include <sstream>
#endif
#include "CTensor.h"
template <class T>
class CTensor4D {
public:
// constructor
inline CTensor4D();
inline CTensor4D(const int aXSize, const int aYSize, const int aZSize, const int aASize);
// copy constructor
CTensor4D(const CTensor4D<T>& aCopyFrom);
// constructor with implicit filling
CTensor4D(const int aXSize, const int aYSize, const int aZSize, const int aASize, const T aFillValue);
// destructor
virtual ~CTensor4D();
// Changes the size of the tensor, data will be lost
void setSize(int aXSize, int aYSize, int aZSize, int aASize);
// Downsamples the tensor
void downsample(int aNewXSize, int aNewYSize);
void downsample(int aNewXSize, int aNewYSize, int aNewZSize);
// Upsamples the tensor
void upsample(int aNewXSize, int aNewYSize);
void upsampleBilinear(int aNewXSize, int aNewYSize);
void upsampleTrilinear(int aNewXSize, int aNewYSize, int aNewZSize);
// Fills the tensor with the value aValue (see also operator =)
void fill(const T aValue);
// Copies a box from the tensor into aResult, the size of aResult will be adjusted
void cut(CTensor4D<T>& aResult, int x1, int y1, int z1, int a1, int x2, int y2, int z2, int a2);
// Reads data from a list of PPM or PGM files given in a text file
void readFromFile(char* aFilename);
// Writes a set of colour images to a large PPM image
void writeToPPM(const char* aFilename, int aCols = 0, int aRows = 0);
// Gives full access to tensor's values
inline T& operator()(const int ax, const int ay, const int az, const int aa) const;
// Read access with bilinear interpolation
CVector<T> operator()(const float ax, const float ay, const int aa) const;
// Fills the tensor with the value aValue (equivalent to fill())
inline CTensor4D<T>& operator=(const T aValue);
// Copies the tensor aCopyFrom to this tensor (size of tensor might change)
CTensor4D<T>& operator=(const CTensor4D<T>& aCopyFrom);
// Multiplication with a scalar
CTensor4D<T>& operator*=(const T aValue);
// Component-wise addition
CTensor4D<T>& operator+=(const CTensor4D<T>& aTensor);
// Gives access to the tensor's size
inline int xSize() const;
inline int ySize() const;
inline int zSize() const;
inline int aSize() const;
inline int size() const;
// Returns the aath layer of the 4D-tensor as 3D-tensor
CTensor<T> getTensor3D(const int aa) const;
// Removes one dimension and returns the resulting 3D-tensor
void getTensor3D(CTensor<T>& aTensor, int aIndex, int aDim = 3) const;
// Copies the components of a 3D-tensor in the aDimth layer of the 4D-tensor
void putTensor3D(CTensor<T>& aTensor, int aIndex, int aDim = 3);
// Removes two dimensions and returns the resulting matrix
void getMatrix(CMatrix<T>& aMatrix, int aZIndex, int aAIndex) const;
// Copies the components of a 3D-tensor in the aDimth layer of the 4D-tensor
void putMatrix(CMatrix<T>& aMatrix, int aZIndex, int aAIndex);
// Gives access to the internal data representation (use sparingly)
inline T* data() const;
protected:
int mXSize,mYSize,mZSize,mASize;
T *mData;
};
// Provides basic output functionality (only appropriate for very small tensors)
template <class T> std::ostream& operator<<(std::ostream& aStream, const CTensor4D<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 ETensor4DRangeOverflow {
ETensor4DRangeOverflow(const int ax, const int ay, const int az, const int aa) {
using namespace std;
cerr << "Exception ETensor4DRangeOverflow: x = " << ax << ", y = " << ay << ", z = " << az << ", a = " << aa << endl;
}
};
// Thrown from getTensor3D if the parameter's size does not match with the size
// of this tensor
struct ETensor4DIncompatibleSize {
ETensor4DIncompatibleSize(int ax, int ay, int az, int ax2, int ay2, int az2) {
using namespace std;
cerr << "Exception ETensor4DIncompatibleSize: x = " << ax << ":" << ax2;
cerr << ", y = " << ay << ":" << ay2;
cerr << ", z = " << az << ":" << az2 << endl;
}
};
// Thrown from readFromFile if the file format is unknown
struct ETensor4DInvalidFileFormat {
ETensor4DInvalidFileFormat() {
using namespace std;
cerr << "Exception ETensor4DInvalidFileFormat" << 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 CTensor4D should ignore everything that's beyond this line :)
// ------------------------------------------------------------------------
// P U B L I C ------------------------------------------------------------
// constructor
template <class T>
inline CTensor4D<T>::CTensor4D() {
mData = 0; mXSize = 0; mYSize = 0; mZSize = 0; mASize = 0;
}
// constructor
template <class T>
inline CTensor4D<T>::CTensor4D(const int aXSize, const int aYSize, const int aZSize, const int aASize)
: mXSize(aXSize), mYSize(aYSize), mZSize(aZSize), mASize(aASize) {
mData = new T[aXSize*aYSize*aZSize*aASize];
}
// copy constructor
template <class T>
CTensor4D<T>::CTensor4D(const CTensor4D<T>& aCopyFrom)
: mXSize(aCopyFrom.mXSize), mYSize(aCopyFrom.mYSize), mZSize(aCopyFrom.mZSize), mASize(aCopyFrom.mASize) {
int wholeSize = mXSize*mYSize*mZSize*mASize;
mData = new T[wholeSize];
for (register int i = 0; i < wholeSize; i++)
mData[i] = aCopyFrom.mData[i];
}
// constructor with implicit filling
template <class T>
CTensor4D<T>::CTensor4D(const int aXSize, const int aYSize, const int aZSize, const int aASize, const T aFillValue)
: mXSize(aXSize), mYSize(aYSize), mZSize(aZSize), mASize(aASize) {
mData = new T[aXSize*aYSize*aZSize*aASize];
fill(aFillValue);
}
// destructor
template <class T>
CTensor4D<T>::~CTensor4D() {
delete[] mData;
}
// setSize
template <class T>
void CTensor4D<T>::setSize(int aXSize, int aYSize, int aZSize, int aASize) {
if (mData != 0) delete[] mData;
mData = new T[aXSize*aYSize*aZSize*aASize];
mXSize = aXSize;
mYSize = aYSize;
mZSize = aZSize;
mASize = aASize;
}
//downsample
template <class T>
void CTensor4D<T>::downsample(int aNewXSize, int aNewYSize) {
T* mData2 = new T[aNewXSize*aNewYSize*mZSize*mASize];
int aSize = aNewXSize*aNewYSize;
for (int a = 0; a < mASize; a++)
for (int z = 0; z < mZSize; z++) {
CMatrix<T> aTemp(mXSize,mYSize);
getMatrix(aTemp,z,a);
aTemp.downsample(aNewXSize,aNewYSize);
for (int i = 0; i < aSize; i++)
mData2[i+(a*mZSize+z)*aSize] = aTemp.data()[i];
}
delete[] mData;
mData = mData2;
mXSize = aNewXSize;
mYSize = aNewYSize;
}
template <class T>
void CTensor4D<T>::downsample(int aNewXSize, int aNewYSize, int aNewZSize) {
T* mData2 = new T[aNewXSize*aNewYSize*aNewZSize*mASize];
int aSize = aNewXSize*aNewYSize*aNewZSize;
for (int a = 0; a < mASize; a++) {
CTensor<T> aTemp(mXSize,mYSize,mZSize);
getTensor3D(aTemp,a);
aTemp.downsample(aNewXSize,aNewYSize,aNewZSize);
for (int i = 0; i < aSize; i++)
mData2[i+a*aSize] = aTemp.data()[i];
}
delete[] mData;
mData = mData2;
mXSize = aNewXSize;
mYSize = aNewYSize;
mZSize = aNewZSize;
}
// upsample
template <class T>
void CTensor4D<T>::upsample(int aNewXSize, int aNewYSize) {
T* mData2 = new T[aNewXSize*aNewYSize*mZSize*mASize];
int aSize = aNewXSize*aNewYSize;
for (int a = 0; a < mASize; a++)
for (int z = 0; z < mZSize; z++) {
CMatrix<T> aTemp(mXSize,mYSize);
getMatrix(aTemp,z,a);
aTemp.upsample(aNewXSize,aNewYSize);
for (int i = 0; i < aSize; i++)
mData2[i+(a*mZSize+z)*aSize] = aTemp.data()[i];
}
delete[] mData;
mData = mData2;
mXSize = aNewXSize;
mYSize = aNewYSize;
}
// upsampleBilinear
template <class T>
void CTensor4D<T>::upsampleBilinear(int aNewXSize, int aNewYSize) {
T* mData2 = new T[aNewXSize*aNewYSize*mZSize*mASize];
int aSize = aNewXSize*aNewYSize;
for (int a = 0; a < mASize; a++)
for (int z = 0; z < mZSize; z++) {
CMatrix<T> aTemp(mXSize,mYSize);
getMatrix(aTemp,z,a);
aTemp.upsampleBilinear(aNewXSize,aNewYSize);
for (int i = 0; i < aSize; i++)
mData2[i+(a*mZSize+z)*aSize] = aTemp.data()[i];
}
delete[] mData;
mData = mData2;
mXSize = aNewXSize;
mYSize = aNewYSize;
}
// upsampleTrilinear
template <class T>
void CTensor4D<T>::upsampleTrilinear(int aNewXSize, int aNewYSize, int aNewZSize) {
T* mData2 = new T[aNewXSize*aNewYSize*aNewZSize*mASize];
int aSize = aNewXSize*aNewYSize*aNewZSize;
for (int a = 0; a < mASize; a++) {
CTensor<T> aTemp(mXSize,mYSize,mZSize);
getTensor3D(aTemp,a);
aTemp.upsampleTrilinear(aNewXSize,aNewYSize,aNewZSize);
for (int i = 0; i < aSize; i++)
mData2[i+a*aSize] = aTemp.data()[i];
}
delete[] mData;
mData = mData2;
mXSize = aNewXSize;
mYSize = aNewYSize;
mZSize = aNewZSize;
}
// fill
template <class T>
void CTensor4D<T>::fill(const T aValue) {
int wholeSize = mXSize*mYSize*mZSize*mASize;
for (register int i = 0; i < wholeSize; i++)
mData[i] = aValue;
}
// cut
template <class T>
void CTensor4D<T>::cut(CTensor4D<T>& aResult, int x1, int y1, int z1, int a1, int x2, int y2, int z2, int a2) {
aResult.mXSize = x2-x1+1;
aResult.mYSize = y2-y1+1;
aResult.mZSize = z2-z1+1;
aResult.mASize = a2-a1+1;
delete[] aResult.mData;
aResult.mData = new T[aResult.mXSize*aResult.mYSize*aResult.mZSize*aResult.mASize];
for (int a = a1; a <= a2; a++)
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,a-a1) = operator()(x,y,z,a);
}
// readFromFile
template <class T>
void CTensor4D<T>::readFromFile(char* aFilename) {
if (mData != 0) delete[] mData;
std::string s;
std::string aPath = aFilename;
aPath.erase(aPath.find_last_of('\\')+1,100);
mASize = 0;
{
std::ifstream aStream(aFilename);
while (!aStream.eof()) {
aStream >> s;
if (s != "") {
mASize++;
if (mASize == 1) {
s.erase(0,s.find_last_of('.'));
if (s == ".ppm" || s == ".PPM") mZSize = 3;
else if (s == ".pgm" || s == ".PGM") mZSize = 1;
else throw ETensor4DInvalidFileFormat();
}
}
}
}
std::ifstream aStream(aFilename);
aStream >> s;
s = aPath+s;
// PGM
if (mZSize == 1) {
CMatrix<float> aTemp;
aTemp.readFromPGM(s.c_str());
mXSize = aTemp.xSize();
mYSize = aTemp.ySize();
int aSize = mXSize*mYSize;
mData = new T[aSize*mASize];
for (int i = 0; i < aSize; i++)
mData[i] = aTemp.data()[i];
for (int a = 1; a < mASize; a++) {
aStream >> s;
s = aPath+s;
aTemp.readFromPGM(s.c_str());
for (int i = 0; i < aSize; i++)
mData[i+a*aSize] = aTemp.data()[i];
}
}
// PPM
else {
CTensor<float> aTemp;
aTemp.readFromPPM(s.c_str());
mXSize = aTemp.xSize();
mYSize = aTemp.ySize();
int aSize = 3*mXSize*mYSize;
mData = new T[aSize*mASize];
for (int i = 0; i < aSize; i++)
mData[i] = aTemp.data()[i];
for (int a = 1; a < mASize; a++) {
aStream >> s;
s = aPath+s;
aTemp.readFromPPM(s.c_str());
for (int i = 0; i < aSize; i++)
mData[i+a*aSize] = aTemp.data()[i];
}
}
}
// writeToPPM
template <class T>
void CTensor4D<T>::writeToPPM(const char* aFilename, int aCols, int aRows) {
int rows = (int)floor(sqrt(mASize));
if (aRows != 0) rows = aRows;
int cols = (int)ceil(mASize*1.0/rows);
if (aCols != 0) cols = aCols;
FILE* outimage = fopen(aFilename, "wb");
fprintf(outimage, "P6 \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 >= mASize) aHelp = 0;
else aHelp = (unsigned char)operator()(x,y,0,r*cols+c);
fwrite (&aHelp, sizeof(unsigned char), 1, outimage);
if (r*cols+c >= mASize) aHelp = 0;
else aHelp = (unsigned char)operator()(x,y,1,r*cols+c);
fwrite (&aHelp, sizeof(unsigned char), 1, outimage);
if (r*cols+c >= mASize) aHelp = 0;
else aHelp = (unsigned char)operator()(x,y,2,r*cols+c);
fwrite (&aHelp, sizeof(unsigned char), 1, outimage);
}
fclose(outimage);
}
// operator ()
template <class T>
inline T& CTensor4D<T>::operator()(const int ax, const int ay, const int az, const int aa) const {
#ifdef DEBUG
if (ax >= mXSize || ay >= mYSize || az >= mZSize || aa >= mASize || ax < 0 || ay < 0 || az < 0 || aa < 0)
throw ETensorRangeOverflow(ax,ay,az,aa);
#endif
return mData[mXSize*(mYSize*(mZSize*aa+az)+ay)+ax];
}
template <class T>
CVector<T> CTensor4D<T>::operator()(const float ax, const float ay, const int aa) 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,aa)+alphaX*operator()(x2,y1,k,aa);
float b = alphaXTrans*operator()(x1,y2,k,aa)+alphaX*operator()(x2,y2,k,aa);
aResult(k) = alphaYTrans*a+alphaY*b;
}
return aResult;
}
// operator =
template <class T>
inline CTensor4D<T>& CTensor4D<T>::operator=(const T aValue) {
fill(aValue);
return *this;
}
template <class T>
CTensor4D<T>& CTensor4D<T>::operator=(const CTensor4D<T>& aCopyFrom) {
if (this != &aCopyFrom) {
if (mData != 0) delete[] mData;
mXSize = aCopyFrom.mXSize;
mYSize = aCopyFrom.mYSize;
mZSize = aCopyFrom.mZSize;
mASize = aCopyFrom.mASize;
int wholeSize = mXSize*mYSize*mZSize*mASize;
mData = new T[wholeSize];
for (register int i = 0; i < wholeSize; i++)
mData[i] = aCopyFrom.mData[i];
}
return *this;
}
// operator *=
template <class T>
CTensor4D<T>& CTensor4D<T>::operator*=(const T aValue) {
int wholeSize = mXSize*mYSize*mZSize*mASize;
for (int i = 0; i < wholeSize; i++)
mData[i] *= aValue;
return *this;
}
// operator +=
template <class T>
CTensor4D<T>& CTensor4D<T>::operator+=(const CTensor4D<T>& aTensor) {
#ifdef _DEBUG
if (mXSize != aTensor.mXSize || mYSize != aTensor.mYSize || mZSize != aTensor.mZSize || mASize != aTensor.mASize)
throw ETensorIncompatibleSize(mXSize,mYSize,mZSize);
#endif
int wholeSize = size();
for (int i = 0; i < wholeSize; i++)
mData[i] += aTensor.mData[i];
return *this;
}
// xSize
template <class T>
inline int CTensor4D<T>::xSize() const {
return mXSize;
}
// ySize
template <class T>
inline int CTensor4D<T>::ySize() const {
return mYSize;
}
// zSize
template <class T>
inline int CTensor4D<T>::zSize() const {
return mZSize;
}
// aSize
template <class T>
inline int CTensor4D<T>::aSize() const {
return mASize;
}
// size
template <class T>
inline int CTensor4D<T>::size() const {
return mXSize*mYSize*mZSize*mASize;
}
// getTensor3D
template <class T>
CTensor<T> CTensor4D<T>::getTensor3D(const int aa) const {
CTensor<T> aTemp(mXSize,mYSize,mZSize);
int aTensorSize = mXSize*mYSize*mZSize;
int aOffset = aa*aTensorSize;
for (int i = 0; i < aTensorSize; i++)
aTemp.data()[i] = mData[i+aOffset];
return aTemp;
}
// getTensor3D
template <class T>
void CTensor4D<T>::getTensor3D(CTensor<T>& aTensor, int aIndex, int aDim) const {
int aSize;
int aOffset;
switch (aDim) {
case 3:
if (aTensor.xSize() != mXSize || aTensor.ySize() != mYSize || aTensor.zSize() != mZSize)
throw ETensor4DIncompatibleSize(aTensor.xSize(),aTensor.ySize(),aTensor.zSize(),mXSize,mYSize,mZSize);
aSize = mXSize*mYSize*mZSize;
aOffset = aIndex*aSize;
for (int i = 0; i < aSize; i++)
aTensor.data()[i] = mData[i+aOffset];
break;
case 2:
if (aTensor.xSize() != mXSize || aTensor.ySize() != mYSize || aTensor.zSize() != mASize)
throw ETensor4DIncompatibleSize(aTensor.xSize(),aTensor.ySize(),aTensor.zSize(),mXSize,mYSize,mASize);
aSize = mXSize*mYSize;
aOffset = aIndex*aSize;
for (int a = 0; a < mASize; a++)
for (int i = 0; i < aSize; i++)
aTensor.data()[i+a*aSize] = mData[i+aOffset+a*aSize*mZSize];
break;
case 1:
if (aTensor.xSize() != mXSize || aTensor.ySize() != mZSize || aTensor.zSize() != mASize)
throw ETensor4DIncompatibleSize(aTensor.xSize(),aTensor.ySize(),aTensor.zSize(),mXSize,mZSize,mASize);
for (int a = 0; a < mASize; a++)
for (int z = 0; z < mZSize; z++)
for (int x = 0; x < mXSize; x++)
aTensor(x,z,a) = operator()(x,aIndex,z,a);
break;
case 0:
if (aTensor.xSize() != mYSize || aTensor.ySize() != mZSize || aTensor.zSize() != mASize)
throw ETensor4DIncompatibleSize(aTensor.xSize(),aTensor.ySize(),aTensor.zSize(),mYSize,mZSize,mASize);
for (int a = 0; a < mASize; a++)
for (int z = 0; z < mZSize; z++)
for (int y = 0; y < mYSize; y++)
aTensor(y,z,a) = operator()(aIndex,y,z,a);
break;
default: getTensor3D(aTensor,aIndex);
}
}
// putTensor3D
template <class T>
void CTensor4D<T>::putTensor3D(CTensor<T>& aTensor, int aIndex, int aDim) {
int aSize;
int aOffset;
switch (aDim) {
case 3:
if (aTensor.xSize() != mXSize || aTensor.ySize() != mYSize || aTensor.zSize() != mZSize)
throw ETensor4DIncompatibleSize(aTensor.xSize(),aTensor.ySize(),aTensor.zSize(),mXSize,mYSize,mZSize);
aSize = mXSize*mYSize*mZSize;
aOffset = aIndex*aSize;
for (int i = 0; i < aSize; i++)
mData[i+aOffset] = aTensor.data()[i];
break;
case 2:
if (aTensor.xSize() != mXSize || aTensor.ySize() != mYSize || aTensor.zSize() != mASize)
throw ETensor4DIncompatibleSize(aTensor.xSize(),aTensor.ySize(),aTensor.zSize(),mXSize,mYSize,mASize);
aSize = mXSize*mYSize;
aOffset = aIndex*aSize;
for (int a = 0; a < mASize; a++)
for (int i = 0; i < aSize; i++)
mData[i+aOffset+a*aSize*mZSize] = aTensor.data()[i+a*aSize];
break;
case 1:
if (aTensor.xSize() != mXSize || aTensor.ySize() != mZSize || aTensor.zSize() != mASize)
throw ETensor4DIncompatibleSize(aTensor.xSize(),aTensor.ySize(),aTensor.zSize(),mXSize,mZSize,mASize);
for (int a = 0; a < mASize; a++)
for (int z = 0; z < mZSize; z++)
for (int x = 0; x < mXSize; x++)
operator()(x,aIndex,z,a) = aTensor(x,z,a);
break;
case 0:
if (aTensor.xSize() != mYSize || aTensor.ySize() != mZSize || aTensor.zSize() != mASize)
throw ETensor4DIncompatibleSize(aTensor.xSize(),aTensor.ySize(),aTensor.zSize(),mYSize,mZSize,mASize);
for (int a = 0; a < mASize; a++)
for (int z = 0; z < mZSize; z++)
for (int y = 0; y < mYSize; y++)
operator()(aIndex,y,z,a) = aTensor(y,z,a);
break;
default: putTensor3D(aTensor,aIndex);
}
}
// getMatrix
template <class T>
void CTensor4D<T>::getMatrix(CMatrix<T>& aMatrix, int aZIndex, int aAIndex) const {
if (aMatrix.xSize() != mXSize || aMatrix.ySize() != mYSize)
throw ETensor4DIncompatibleSize(aMatrix.xSize(),aMatrix.ySize(),1,mXSize,mYSize,1);
int aSize = mXSize*mYSize;
int aOffset = aSize*(aAIndex*mZSize+aZIndex);
for (int i = 0; i < aSize; i++)
aMatrix.data()[i] = mData[i+aOffset];
}
// putMatrix
template <class T>
void CTensor4D<T>::putMatrix(CMatrix<T>& aMatrix, int aZIndex, int aAIndex) {
if (aMatrix.xSize() != mXSize || aMatrix.ySize() != mYSize)
throw ETensor4DIncompatibleSize(aMatrix.xSize(),aMatrix.ySize(),1,mXSize,mYSize,1);
int aSize = mXSize*mYSize;
int aOffset = aSize*(aAIndex*mZSize+aZIndex);
for (int i = 0; i < aSize; i++)
mData[i+aOffset] = aMatrix.data()[i];
}
// data()
template <class T>
inline T* CTensor4D<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 CTensor4D<T>& aTensor) {
for (int a = 0; a < aTensor.aSize(); a++) {
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;
}
aStream << std::endl;
}
return aStream;
}
#endif
|