-
Notifications
You must be signed in to change notification settings - Fork 0
/
TensorField.cpp
671 lines (596 loc) · 18.2 KB
/
TensorField.cpp
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
#include "TensorField.h"
#include "math.h"
#include "iostream"
#include "eigen3/Eigen/Dense"
#include <QPainter>
#include <QPen>
#include <QFileDialog>
#include <QCoreApplication>
#include <QProgressDialog>
TensorField::TensorField(QSize fieldSize, QObject *parent) :
QObject(parent), mFieldSize(fieldSize)
{
mData.resize(fieldSize.height());
for(int i=0 ; i < fieldSize.height() ; i++)
{
mData[i].resize(fieldSize.width());
}
mFieldIsFilled = false;
mEigenIsComputed = false;
mWaterMapIsLoaded = false;
}
QVector4D TensorField::getTensor(int i, int j)
{
return mData[i][j];
}
void TensorField::setTensor(int i, int j, QVector4D tensor)
{
mData[i][j] = tensor;
}
void TensorField::setFieldSize(QSize fieldSize)
{
mFieldSize = fieldSize;
mData.resize(fieldSize.height());
for(int i=0 ; i < fieldSize.height() ; i++)
{
mData[i].resize(fieldSize.width());
}
mFieldIsFilled = false;
}
void TensorField::applyWaterMap(QString filename)
{
QImage waterMap = QImage(filename);
if(waterMap.isNull())
{
qCritical()<<"applyWaterMap(): File "<<filename<<" not found";
return;
}
if(waterMap.size() != mFieldSize)
{
qCritical()<<"applyWaterMap(): Watermap must be of same size as the tensor field";
return;
}
for(int i=0; i<waterMap.height() ; i++)
{
for(int j=0; j<waterMap.width() ; j++)
{
if(qBlue(waterMap.pixel(j,i)) > 0)
{
mData[mFieldSize.height()-1-i][j] = QVector4D(0,0,0,0);
}
}
}
mWatermapFilename = filename;
mWaterMapIsLoaded = true;
}
void TensorField::fillGridBasisField(float theta, float l)
{
for(int i=0; i<mFieldSize.height() ; i++)
{
for(int j=0; j<mFieldSize.width() ; j++)
{
QVector4D tensor;
tensor.setX(cos(2.0*theta));
tensor.setY(sin(2.0*theta));
tensor.setZ(sin(2.0*theta));
tensor.setW(-cos(2.0*theta));
tensor *= l;
mData[i][j] = tensor;
}
}
mFieldIsFilled = true;
}
void TensorField::fillRotatingField()
{
for(int i=0; i<mFieldSize.height() ; i++)
{
for(int j=0; j<mFieldSize.width() ; j++)
{
float theta = M_PI*j/(mFieldSize.width()-1) + i*M_PI/4/(mFieldSize.height()-1);
QVector4D tensor;
tensor.setX(cos(2.0*theta));
tensor.setY(sin(2.0*theta));
tensor.setZ(sin(2.0*theta));
tensor.setW(-cos(2.0*theta));
mData[i][j] = tensor;
}
}
mFieldIsFilled = true;
}
void TensorField::fillGridBasisField(QVector2D direction)
{
float theta = std::atan2(direction.y(),direction.x());
this->fillGridBasisField(theta, direction.length());
}
void TensorField::fillHeightBasisField(QString filename)
{
QImage mHeightMap = QImage(filename);
if(mHeightMap.isNull())
{
qCritical()<<"fillHeightBasisField(): File "<<filename<<" not found";
return;
}
this->setFieldSize(mHeightMap.size());
QRgb currentPixel, nextPixelHoriz, nextPixelVert;
QVector2D grad;
float theta, r;
// Origin is top-left in the image
// Origin is bottom-left in the tensor matrix
// We have to swap the vertical axis
for(int i=0; i<mFieldSize.height()-1 ; i++)
{
for(int j=0; j<mFieldSize.width()-1 ; j++)
{
QVector4D tensor;
currentPixel = mHeightMap.pixel(j,i);
nextPixelHoriz = mHeightMap.pixel(j+1,i);
nextPixelVert = mHeightMap.pixel(j,i+1);
// If gradient is null, set tensor to default instead
// of degenerate
if(nextPixelHoriz == currentPixel && nextPixelVert == currentPixel)
{
mData[mFieldSize.width()-1-i][j] = QVector4D(1,0,0,-1);
}
else
{
grad.setX(qBlue(currentPixel)-qBlue(nextPixelHoriz));
grad.setY(qBlue(currentPixel)-qBlue(nextPixelVert));
// Invert y
theta = std::atan2(-grad.y(), grad.x()) + M_PI/2.0;
r = std::sqrt(std::pow(grad.y(),2.0) + std::pow(grad.x(),2.0));
tensor.setX(cos(2.0*theta));
tensor.setY(sin(2.0*theta));
tensor.setZ(sin(2.0*theta));
tensor.setW(-cos(2.0*theta));
tensor *= r;
mData[mFieldSize.height()-1-i][j] = tensor;
}
}
}
mFieldIsFilled = true;
}
void TensorField::fillHeightBasisFieldSobel(QString filename)
{
QImage mHeightMap = QImage(filename);
if(mHeightMap.isNull())
{
qCritical()<<"fillHeightBasisField(): File "<<filename<<" not found";
return;
}
this->setFieldSize(mHeightMap.size());
QImage mapSobelX, mapSobelY;
QColor pixSobelX, pixSobelY;
float theta, r;
mapSobelX = applySobelX(mHeightMap);
mapSobelY = applySobelY(mHeightMap);
for(int i=0; i<mFieldSize.width()-1 ; i++)
{
for(int j=0; j<mFieldSize.height()-1 ; j++)
{
QVector4D tensor;
pixSobelX = mapSobelX.pixel(j,i);
pixSobelY = mapSobelY.pixel(j,i);
theta = std::atan2(abs(pixSobelY.blue()),abs(pixSobelX.blue()))+ M_PI/2.0;
r = std::sqrt(std::pow(pixSobelY.blue(),2.0) + std::pow(pixSobelX.blue(),2.0));
tensor.setX(cos(2.0*theta));
tensor.setY(sin(2.0*theta));
tensor.setZ(sin(2.0*theta));
tensor.setW(-cos(2.0*theta));
tensor *= r;
mData[mFieldSize.width() -1 -i][j] = tensor;
}
}
mFieldIsFilled = true;
}
void TensorField::fillRadialBasisField(QPointF center)
{
float x;
float y;
for(int i=0; i<mFieldSize.height() ; i++)
{
for(int j=0; j<mFieldSize.width() ; j++)
{
x = ((float)j/(mFieldSize.height()-1) - center.x());
y = ((float)i/(mFieldSize.width()-1) - center.y());
QVector4D tensor;
tensor.setX((std::pow(y,2.0)-std::pow(x,2.0)));
tensor.setY(-2*x*y);
tensor.setZ(-2*x*y);
tensor.setW(-(std::pow(y,2.0)-std::pow(x,2.0)));
mData[i][j] = tensor;
}
}
mFieldIsFilled = true;
}
void TensorField::actionAddWatermap()
{
if(!mFieldIsFilled)
{
qCritical()<<"actionApplyWatermap(): Tensor field is null. Initialize it first";
return;
}
QString filename = QFileDialog::getOpenFileName(0, QString("Open Image"));
if(filename.isEmpty())
{
return;
}
this->applyWaterMap(filename);
this->computeTensorsEigenDecomposition();
this->exportEigenVectorsImage(true, true);
}
void TensorField::generateGridTensorField()
{
this->fillGridBasisField(M_PI/3, 1);
this->computeTensorsEigenDecomposition();
this->exportEigenVectorsImage(true, true);
}
void TensorField::generateHeightmapTensorField()
{
QString filename = QFileDialog::getOpenFileName(0, QString("Open Image"));
if(filename.isEmpty())
{
return;
}
this->fillHeightBasisField(filename);
this->computeTensorsEigenDecomposition();
this->exportEigenVectorsImage(true, true);
}
void TensorField::generateMultiRotationTensorField()
{
this->fillRotatingField();
this->computeTensorsEigenDecomposition();
this->exportEigenVectorsImage(true, true);
}
void TensorField::generateRadialTensorField()
{
this->fillRadialBasisField(QPointF(0.5,0.5));
this->computeTensorsEigenDecomposition();
this->exportEigenVectorsImage(true, true);
}
void TensorField::outputTensorField()
{
for(int i=0; i<mFieldSize.height() ; i++)
{
for(int j=0; j<mFieldSize.width() ; j++)
{
qDebug()<<mData[i][j];
}
qDebug();
}
}
void TensorField::smoothTensorField()
{
if(!mFieldIsFilled)
{
qCritical()<<"smoothTensorField(): Tensor field is null. Initialize it first";
return;
}
QVector<QVector<QVector4D> > mDataSmooth;
mDataSmooth = mData;
for(int i=1; i<mFieldSize.height()-1 ; i++)
{
for(int j=1; j<mFieldSize.width()-1 ; j++)
{
mDataSmooth[i][j] = 1.0f/9.0f*(mData[i+1][j-1] + mData[i+1][j] + mData[i+1][j+1] +
mData[i][j-1] + mData[i][j] + mData[i][j+1] +
mData[i-1][j-1] + mData[i-1][j] + mData[i-1][j+1]);
}
}
mData = mDataSmooth;
this->computeTensorsEigenDecomposition();
this->exportEigenVectorsImage(true, true);
}
QPixmap TensorField::exportEigenVectorsImage(bool drawVector1, bool drawVector2,
QColor color1, QColor color2)
{
int imageSize = 512;
QPixmap pixmap(imageSize,imageSize);
pixmap.fill();
if(!mFieldIsFilled)
{
qCritical()<<"exportEigenVectorsImage(): Tensor field is empty";
return pixmap;
}
QPainter painter(&pixmap);
QPen pen1(color1);
QPen pen2(color2);
float dv = imageSize/(float)mFieldSize.height();
float du = imageSize/(float)mFieldSize.width();
QVector2D origin(du/2.0f, dv/2.0f);
int numberOfTensorsToDisplay = 32;
int scaleI = mFieldSize.height()/numberOfTensorsToDisplay;
int scaleJ = mFieldSize.width()/numberOfTensorsToDisplay;
for(int i=0; i<mFieldSize.width() ; i=i+scaleI)
{
for(int j=0; j<mFieldSize.height() ; j=j+scaleJ)
{
if(drawVector1)
{
painter.setPen(pen1);
QVector2D base = origin + QVector2D(j*dv, i*du);
QVector2D eigenVector = getTensorMajorEigenVector(mData[i][j]);
eigenVector.setX(eigenVector.x()*du/2.0f*scaleI*0.8);
eigenVector.setY(eigenVector.y()*dv/2.0f*scaleJ*0.8);
QVector2D tip = base + eigenVector;
base -= eigenVector;
roundVector2D(base);
roundVector2D(tip);
// Flip the y axis because the painter system has its origin
// on the top left corner and the y axis points down
painter.drawLine(base.x(),imageSize - base.y(),tip.x(),imageSize - tip.y());
}
if(drawVector2)
{
painter.setPen(pen2);
QVector2D base = origin + QVector2D(j*dv, i*du);
QVector2D eigenVector = getTensorMinorEigenVector(mData[i][j]);
eigenVector.setX(eigenVector.x()*du/2.0f*scaleI*0.8);
eigenVector.setY(eigenVector.y()*dv/2.0f*scaleJ*0.8);
QVector2D tip = base + eigenVector;
base -= eigenVector;
roundVector2D(base);
roundVector2D(tip);
// Flip the y axis because the painter system has its origin
// on the top left corner and the y axis points down
painter.drawLine(base.x(),imageSize - base.y(),tip.x(),imageSize - tip.y());
}
}
}
emit newTensorFieldImage(pixmap);
return pixmap;
}
int TensorField::computeTensorsEigenDecomposition()
{
if(!mFieldIsFilled)
{
qCritical()<<"computeTensorsEigenDecomposition(): Fill the tensor field before computing the eigen vectors";
return -1;
}
// Initialize the vectors and values internal containers if they aren't already
if(mEigenVectors.size() ==0 || mEigenValues.size() == 0)
{
mEigenVectors.resize(mFieldSize.height());
mEigenValues.resize(mFieldSize.height());
for(int i=0 ; i < mFieldSize.height() ; i++)
{
mEigenVectors[i].resize(mFieldSize.width());
mEigenValues[i].resize(mFieldSize.width());
}
}
QProgressDialog progress("Loading...",NULL, 0, mFieldSize.height()-1);
progress.setMinimumDuration(0);
// Fill the internal containers
int numberOfDegeneratePoints = 0;
for(int i=0; i<mFieldSize.height() ; i++)
{
progress.setValue(i);
QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
for(int j=0; j<mFieldSize.width() ; j++)
{
mEigenVectors[i][j] = getTensorEigenVectors(mData[i][j]);
mEigenValues[i][j] = getTensorEigenValues(mData[i][j]);
if(isDegenerate(mEigenVectors[i][j]))
{
numberOfDegeneratePoints++;
}
}
}
mEigenIsComputed = true;
return numberOfDegeneratePoints;
}
QVector4D TensorField::getEigenVectors(int i, int j)
{
if(!mEigenIsComputed)
{
qCritical()<<"Unable to get the eigen vectors."
<<"First compute tensors Eigen decomposition";
return QVector4D();
}
else
{
return mEigenVectors[i][j];
}
}
QVector2D TensorField::getEigenValues(int i, int j)
{
if(!mEigenIsComputed)
{
qCritical()<<"getEigenValues(): Unable to get the eigen values."
<<"First compute tensors Eigen decomposition";
return QVector2D();
}
else
{
return mEigenValues[i][j];
}
}
QVector2D TensorField::getMajorEigenVector(int i, int j)
{
return getFirstVector(this->getEigenVectors(i,j));
}
QVector2D TensorField::getMinorEigenVector(int i, int j)
{
return getSecondVector(this->getEigenVectors(i,j));
}
/** ******************** */
/** Non-member Functions */
/** ******************** */
void roundVector2D(QVector2D& vec)
{
vec.setX(round(vec.x()));
vec.setY(round(vec.y()));
}
QVector2D getFirstVector(QVector4D matrix)
{
return QVector2D(matrix.x(),matrix.y());
}
QVector2D getSecondVector(QVector4D matrix)
{
return QVector2D(matrix.z(),matrix.w());
}
QVector4D getTensorEigenVectors(QVector4D tensor)
{
if(!isSymetricalAndTraceless(tensor))
{
qCritical()<<"getTensorEigenVectors(): The tensor must be traceless and symetrical";
return QVector4D();
}
if(isDegenerate(tensor))
{
return QVector4D(0,0,0,0);
}
if(isFuzzyEqual(tensor.x(), 1) && isFuzzyEqual(tensor.y(), 0))
{
return QVector4D(1,0,0,1);
}
Eigen::Matrix2f m(2,2);
m(0,0) = tensor.x();
m(1,0) = tensor.y();
m(0,1) = tensor.z();
m(1,1) = tensor.w();
Eigen::EigenSolver<Eigen::Matrix2f> es(m);
QVector2D vec1(es.eigenvectors().col(0).real()[0],es.eigenvectors().col(0).real()[1]);
QVector2D vec2(es.eigenvectors().col(1).real()[0],es.eigenvectors().col(1).real()[1]);
QVector2D val(es.eigenvalues()[0].real(),es.eigenvalues()[1].real());
if(isFuzzyEqual(val.x(), fmax(val.x(),val.y())))
{
return QVector4D(vec1.x(), vec1.y(),vec2.x(), vec2.y());
}
else
{
return QVector4D(vec2.x(), vec2.y(),vec1.x(), vec1.y());
}
}
QVector2D getTensorEigenValues(QVector4D tensor)
{
if(isDegenerate(tensor))
{
return QVector2D(0,0);
}
if(isFuzzyEqual(tensor.x(), 1) && isFuzzyEqual(tensor.y(), 0))
{
return QVector2D(1,-1);
}
Eigen::Matrix2f m(2,2);
m(0,0) = tensor.x();
m(1,0) = tensor.y();
m(0,1) = tensor.z();
m(1,1) = tensor.w();
Eigen::EigenSolver<Eigen::Matrix2f> es(m);
return QVector2D(es.eigenvalues()[0].real(),es.eigenvalues()[1].real());
}
QVector2D getTensorMajorEigenVector(QVector4D tensor)
{
return getFirstVector(getTensorEigenVectors(tensor));
}
QVector2D getTensorMinorEigenVector(QVector4D tensor)
{
return getSecondVector(getTensorEigenVectors(tensor));
}
QImage applySobelX(QImage map)
{
QSize size;
size = map.size();
QImage sobelX(size,QImage::Format_RGB32);
sobelX.fill(0);
float kii[9], mii[9];
kii[0] = -1.0f;
kii[1] = 0.0f;
kii[2] = 1.0f;
kii[3] = -2.0f;
kii[4] = 0.0f;
kii[5] = 2.0f;
kii[6] = -1.0f;
kii[7] = 0.0f;
kii[8] = 1.0f;
QMatrix3x3 kernel(kii);
for (int i=1; i<size.width()-2; i++)
{
for (int j=1; j<size.height()-2; j++)
{
mii[0] = qBlue(map.pixel(i-1,j-1));
mii[1] = qBlue(map.pixel(i,j-1));
mii[2] = qBlue(map.pixel(i+1,j-1));
mii[3] = qBlue(map.pixel(i-1,j));
mii[4] = qBlue(map.pixel(i,j));
mii[5] = qBlue(map.pixel(i+1,j));
mii[6] = qBlue(map.pixel(i-1,j+1));
mii[7] = qBlue(map.pixel(i,j+1));
mii[8] = qBlue(map.pixel(i+1,j+1));
QMatrix3x3 matrix(mii);
sobelX.setPixel(i,j,(sumMat3D(matrix,kernel)));
}
}
sobelX.save("testx.png");
return sobelX;
}
QImage applySobelY(QImage map)
{
QSize size;
size = map.size();
QImage sobelY(size,QImage::Format_RGB32);
sobelY.fill(0);
float kii[9], mii[9];
kii[0] = -1.0f;
kii[1] = -2.0f;
kii[2] = -1.0f;
kii[3] = 0.0f;
kii[4] = 0.0f;
kii[5] = 0.0f;
kii[6] = 1.0f;
kii[7] = 2.0f;
kii[8] = 1.0f;
QMatrix3x3 kernel(kii);
for (int i=1; i<size.width()-2; i++)
{
for (int j=1; j<size.height()-2; j++)
{
mii[0] = qBlue(map.pixel(i-1,j-1));
mii[1] = qBlue(map.pixel(i,j-1));
mii[2] = qBlue(map.pixel(i+1,j-1));
mii[3] = qBlue(map.pixel(i-1,j));
mii[4] = qBlue(map.pixel(i,j));
mii[5] = qBlue(map.pixel(i+1,j));
mii[6] = qBlue(map.pixel(i-1,j+1));
mii[7] = qBlue(map.pixel(i,j+1));
mii[8] = qBlue(map.pixel(i+1,j+1));
QMatrix3x3 matrix(mii);
sobelY.setPixel(i,j,(sumMat3D(matrix,kernel)));
}
}
sobelY.save("testy.png");
return sobelY;
}
int sumMat3D(QMatrix3x3 matrix, QMatrix3x3 kernel)
{
int sum;
sum = 0;
for (int i=0; i<3; i++)
{
for (int j=0; j<3; j++)
{
sum += matrix(i,j)*kernel(i,j);
}
}
return sum;
}
bool isSymetricalAndTraceless(QVector4D tensor)
{
return isFuzzyEqual(tensor.y(), tensor.z())
&& isFuzzyNull(tensor.x() + tensor.w());
}
bool isDegenerate(QVector4D tensor)
{
return isFuzzyNull(tensor.x())
&& isFuzzyNull(tensor.y())
&& isFuzzyNull(tensor.z())
&& isFuzzyNull(tensor.w());
}
bool isFuzzyNull(float a)
{
return (fabs(a) < FLOAT_COMPARISON_EPSILON);
}
bool isFuzzyEqual(float a, float b)
{
return (fabs(a - b) / FLOAT_COMPARISON_EPSILON <= fmin(fabs(a), fabs(b)));
}