-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlauscan.cpp
More file actions
1894 lines (1741 loc) · 96.6 KB
/
lauscan.cpp
File metadata and controls
1894 lines (1741 loc) · 96.6 KB
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) 2017, Dr. Daniel L. Lau *
* All rights reserved. *
* *
* Redistribution and use in source and binary forms, with or without *
* modification, are permitted provided that the following conditions are met: *
* 1. Redistributions of source code must retain the above copyright *
* notice, this list of conditions and the following disclaimer. *
* 2. Redistributions in binary form must reproduce the above copyright *
* notice, this list of conditions and the following disclaimer in the *
* documentation and/or other materials provided with the distribution. *
* 3. All advertising materials mentioning features or use of this software *
* must display the following acknowledgement: *
* This product includes software developed by the <organization>. *
* 4. Neither the name of the <organization> nor the *
* names of its contributors may be used to endorse or promote products *
* derived from this software without specific prior written permission. *
* *
* THIS SOFTWARE IS PROVIDED BY Dr. Daniel L. Lau ''AS IS'' AND ANY *
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED *
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE *
* DISCLAIMED. IN NO EVENT SHALL Dr. Daniel L. Lau BE LIABLE FOR ANY *
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; *
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND *
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
* *
*********************************************************************************/
#include "lauscan.h"
#include <locale.h>
#include <math.h>
using namespace libtiff;
using namespace LAU3DVideoParameters;
QString LAUScan::lastUsedDirectory;
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
LAUScan::LAUScan(unsigned int cols, unsigned int rows, LAUVideoPlaybackColor clr) : LAUMemoryObject(),
time(QTime::currentTime()), fileString(QString()), makeString(QString()), modelString(QString()),
softwareString(QString()), parentByteArray(QByteArray()), playbackColor(clr),
xMin(0.0f), xMax(0.0f), yMin(0.0f), yMax(0.0f), zMin(0.0f), zMax(0.0f),
com(QVector3D(0.0f, 0.0f, 0.0f)), fov(QPointF(0.0f, 0.0f))
{
data->numRows = rows;
data->numCols = cols;
data->numByts = sizeof(float);
data->numFrms = 1;
// SET THE NUMBER OF CHANNELS BASED ON THE DEVICE COLOR SPACE
switch (playbackColor) {
case ColorGray:
data->numChns = 1;
break;
case ColorRGB:
case ColorXYZ:
data->numChns = 3;
break;
case ColorRGBA:
case ColorXYZG:
case ColorXYZW:
data->numChns = 4;
break;
case ColorXYZRGB:
data->numChns = 6;
break;
case ColorXYZWRGBA:
data->numChns = 8;
break;
default:
data->numChns = 0;
}
// NOW ALLOCATE SPACE TO HOLD THE SCAN
data->allocateBuffer();
// POPULATE FILENAME STRING WITH RANDOM CHARACTERS
//QByteArray byteArray(30, 0x00);
//for (int n=0; n<30; n++){
// byteArray.data()[n] = (char)(48 + 80 * (float)qrand()/(float)RAND_MAX);
//}
//fileString = QString(byteArray);
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
LAUScan::LAUScan(QString fileName) : LAUMemoryObject(),
time(QTime::currentTime()), fileString(QString()), makeString(QString()), modelString(QString()),
softwareString(QString()), parentByteArray(QByteArray()), playbackColor(ColorUndefined),
xMin(0.0f), xMax(0.0f), yMin(0.0f), yMax(0.0f), zMin(0.0f), zMax(0.0f),
com(QVector3D(0.0f, 0.0f, 0.0f)), fov(QPointF(0.0f, 0.0f))
{
// GET A FILE TO OPEN FROM THE USER IF NOT ALREADY PROVIDED ONE
if (fileName.isNull()) {
QSettings settings;
QString directory = settings.value("LAUScan::lastUsedDirectory", QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)).toString();
fileName = QFileDialog::getOpenFileName(0, QString("Load image from disk (*.tif)"), directory, QString("*.tif *.tiff"));
if (fileName.isEmpty() == false) {
LAUScan::lastUsedDirectory = QFileInfo(fileName).absolutePath();
settings.setValue("LAUScan::lastUsedDirectory", LAUScan::lastUsedDirectory);
} else {
return;
}
}
// IF WE HAVE A VALID TIFF FILE, LOAD FROM DISK
// OTHERWISE TRY TO CONNECT TO SCANNER
if (QFile::exists(fileName)) {
// OPEN INPUT TIFF FILE FROM DISK
TIFF *inTiff = TIFFOpen(fileName.toLatin1(), "r");
if (!inTiff) {
return;
}
load(inTiff);
TIFFClose(inTiff);
fileString = fileName;
}
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
LAUScan::LAUScan(TIFF *inTiff) : LAUMemoryObject(),
time(QTime::currentTime()), fileString(QString()), makeString(QString()), modelString(QString()),
softwareString(QString()), parentByteArray(QByteArray()), playbackColor(ColorUndefined),
xMin(0.0f), xMax(0.0f), yMin(0.0f), yMax(0.0f), zMin(0.0f), zMax(0.0f),
com(QVector3D(0.0f, 0.0f, 0.0f)), fov(QPointF(0.0f, 0.0f))
{
load(inTiff);
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
LAUScan::LAUScan(const LAUScan &other) : LAUMemoryObject(other)
{
time = other.time;
fileString = other.fileString;
makeString = other.makeString;
modelString = other.modelString;
softwareString = other.softwareString;
parentByteArray = other.parentByteArray;
playbackColor = other.playbackColor;
xMin = other.xMin;
xMax = other.xMax;
yMin = other.yMin;
yMax = other.yMax;
zMin = other.zMin;
zMax = other.zMax;
com = other.com;
fov = other.fov;
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
LAUScan::LAUScan(const LAUMemoryObject &other, LAUVideoPlaybackColor clr) : LAUMemoryObject(other),
time(QTime::currentTime()), fileString(QString()), makeString(QString()), modelString(QString()),
softwareString(QString()), parentByteArray(QByteArray()), playbackColor(clr),
xMin(0.0f), xMax(0.0f), yMin(0.0f), yMax(0.0f), zMin(0.0f), zMax(0.0f),
com(QVector3D(0.0f, 0.0f, 0.0f)), fov(QPointF(0.0f, 0.0f))
{
// UPDATE THE XYZ LIMITS
updateLimits();
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
LAUScan &LAUScan::operator = (const LAUScan &other)
{
if (this != &other) {
*((LAUMemoryObject *)this) = (LAUMemoryObject)other;
time = other.time;
fileString = other.fileString;
makeString = other.makeString;
modelString = other.modelString;
softwareString = other.softwareString;
parentByteArray = other.parentByteArray;
playbackColor = other.playbackColor;
xMin = other.xMin;
xMax = other.xMax;
yMin = other.yMin;
yMax = other.yMax;
zMin = other.zMin;
zMax = other.zMax;
com = other.com;
fov = other.fov;
}
return (*this);
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
LAUScan &LAUScan::operator + (const LAUScan &other)
{
if (this != &other) {
setElapsed(other.elapsed());
setTransform(other.transform());
time = other.time;
fileString = other.fileString;
makeString = other.makeString;
modelString = other.modelString;
softwareString = other.softwareString;
parentByteArray = other.parentByteArray;
xMin = other.xMin;
xMax = other.xMax;
yMin = other.yMin;
yMax = other.yMax;
zMin = other.zMin;
zMax = other.zMax;
com = other.com;
fov = other.fov;
}
return (*this);
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
bool LAUScan::save(QString filename)
{
QSettings settings;
lastUsedDirectory = settings.value(QString("LAUScan::lastUsedDirectory"), QString()).toString();
if (filename.isNull()) {
int counter = 0;
do {
if (counter == 0) {
filename = QString("%1/Untitled.tif").arg(lastUsedDirectory);
} else {
filename = QString("%1/Untitled%2.tif").arg(lastUsedDirectory).arg(counter);
}
counter++;
} while (QFile::exists(filename));
filename = QFileDialog::getSaveFileName(0, QString("Save image to disk (*.tif)"), filename, QString("*.tif;*.tiff"));
if (!filename.isNull()) {
lastUsedDirectory = QFileInfo(filename).absolutePath();
settings.setValue(QString("LAUScan::lastUsedDirectory"), lastUsedDirectory);
if (!filename.toLower().endsWith(QString(".tiff"))) {
if (!filename.toLower().endsWith(QString(".tif"))) {
filename = QString("%1.tif").arg(filename);
}
}
} else {
return (false);
}
}
// OPEN TIFF FILE FOR SAVING THE IMAGE
TIFF *outputTiff = TIFFOpen(filename.toLatin1(), "w");
if (!outputTiff) {
return (false);
}
// WRITE IMAGE TO CURRENT DIRECTORY
if (save(outputTiff)) {
setFilename(filename);
}
// CLOSE TIFF FILE
TIFFClose(outputTiff);
return (true);
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
bool LAUScan::save(TIFF *otTiff, int index)
{
// CREATE THE XML DATA PACKET USING QT'S XML STREAM OBJECTS
QBuffer buffer;
buffer.open(QIODevice::WriteOnly);
QXmlStreamWriter writer(&buffer);
writer.setAutoFormatting(true);
writer.writeStartDocument();
writer.writeStartElement("scan");
// WRITE THE COLOR SPACE AND RANGE LIMITS FOR EACH DIMENSION
switch (playbackColor) {
case ColorGray:
writer.writeTextElement("playbackcolor", "ColorGray");
writer.writeTextElement("minimumvalues", QString("%1").arg(0.0f));
writer.writeTextElement("maximumvalues", QString("%1").arg(1.0f));
break;
case ColorRGB:
writer.writeTextElement("playbackcolor", "ColorRGB");
writer.writeTextElement("minimumvalues", QString("%1,%2,%3").arg(0.0f).arg(0.0f).arg(0.0f));
writer.writeTextElement("maximumvalues", QString("%1,%2,%3").arg(1.0f).arg(1.0f).arg(1.0f));
break;
case ColorRGBA:
writer.writeTextElement("playbackcolor", "ColorRGBA");
writer.writeTextElement("minimumvalues", QString("%1,%2,%3,%4").arg(0.0f).arg(0.0f).arg(0.0f).arg(0.0f));
writer.writeTextElement("maximumvalues", QString("%1,%2,%3,%4").arg(1.0f).arg(1.0f).arg(1.0f).arg(1.0f));
break;
case ColorXYZ:
writer.writeTextElement("playbackcolor", "ColorXYZ");
writer.writeTextElement("minimumvalues", QString("%1,%2,%3").arg(xMin).arg(yMin).arg(zMin));
writer.writeTextElement("maximumvalues", QString("%1,%2,%3").arg(xMax).arg(yMax).arg(zMax));
break;
case ColorXYZG:
writer.writeTextElement("playbackcolor", "ColorXYZG");
writer.writeTextElement("minimumvalues", QString("%1,%2,%3,%4").arg(xMin).arg(yMin).arg(zMin).arg(0.0f));
writer.writeTextElement("maximumvalues", QString("%1,%2,%3,%4").arg(xMax).arg(yMax).arg(zMax).arg(1.0f));
break;
case ColorXYZW:
writer.writeTextElement("playbackcolor", "ColorXYZW");
writer.writeTextElement("minimumvalues", QString("%1,%2,%3,%4").arg(xMin).arg(yMin).arg(zMin).arg(0.0f));
writer.writeTextElement("maximumvalues", QString("%1,%2,%3,%4").arg(xMax).arg(yMax).arg(zMax).arg(1.0f));
break;
case ColorXYZRGB:
writer.writeTextElement("playbackcolor", "ColorXYZRGB");
writer.writeTextElement("minimumvalues", QString("%1,%2,%3,%4,%5,%6").arg(xMin).arg(yMin).arg(zMin).arg(0.0f).arg(0.0f).arg(0.0f));
writer.writeTextElement("maximumvalues", QString("%1,%2,%3,%4,%5,%6").arg(xMax).arg(yMax).arg(zMax).arg(1.0f).arg(1.0f).arg(1.0f));
break;
case ColorXYZWRGBA:
writer.writeTextElement("playbackcolor", "ColorXYZWRGBA");
writer.writeTextElement("minimumvalues", QString("%1,%2,%3,%4,%5,%6,%7,%8").arg(xMin).arg(yMin).arg(zMin).arg(0.0f).arg(0.0f).arg(0.0f).arg(0.0f).arg(0.0f));
writer.writeTextElement("maximumvalues", QString("%1,%2,%3,%4,%5,%6,%7,%8").arg(xMax).arg(yMax).arg(zMax).arg(1.0f).arg(1.0f).arg(1.0f).arg(1.0f).arg(1.0f));
break;
case ColorUndefined:
writer.writeTextElement("playbackcolor", "ColorUndefined");
}
// WRITE THE CAMERA FIELD OF VIEW AND CENTER OF MASS
writer.writeTextElement("fieldofview", QString("%1,%2").arg(fov.x()).arg(fov.y()));
writer.writeTextElement("centerofmass", QString("%1,%2,%3").arg(com.x()).arg(com.y()).arg(com.z()));
// WRITE THE TRANSFORM MATRIX OUT TO THE XML FIELD
writer.writeTextElement("transform", QString("A = [ %1, %2, %3, %4; %5, %6, %7, %8; %9, %10, %11, %12; %13, %14, %15, %16 ];").arg(transformMatrix(0, 0)).arg(transformMatrix(0, 1)).arg(transformMatrix(0, 2)).arg(transformMatrix(0, 3)).arg(transformMatrix(1, 0)).arg(transformMatrix(1, 1)).arg(transformMatrix(1, 2)).arg(transformMatrix(1, 3)).arg(transformMatrix(2, 0)).arg(transformMatrix(2, 1)).arg(transformMatrix(2, 2)).arg(transformMatrix(2, 3)).arg(transformMatrix(3, 0)).arg(transformMatrix(3, 1)).arg(transformMatrix(3, 2)).arg(transformMatrix(3, 3)));
// CLOSE OUT THE XML BUFFER
writer.writeEndElement();
writer.writeEndDocument();
buffer.close();
// EXPORT THE XML BUFFER TO THE XMLPACKET FIELD OF THE TIFF IMAGE
QByteArray xmlByteArray = buffer.buffer();
TIFFSetField(otTiff, TIFFTAG_XMLPACKET, xmlByteArray.length(), xmlByteArray.data());
// SAVE CUSTOM FILESTRINGS FROM THE DOCUMENT NAME TIFFTAG
TIFFSetField(otTiff, TIFFTAG_DOCUMENTNAME, fileString.toLatin1().data());
TIFFSetField(otTiff, TIFFTAG_PAGENAME, parentByteArray.data());
TIFFSetField(otTiff, TIFFTAG_SOFTWARE, softwareString.toLatin1().data());
TIFFSetField(otTiff, TIFFTAG_MODEL, modelString.toLatin1().data());
TIFFSetField(otTiff, TIFFTAG_MAKE, makeString.toLatin1().data());
return (LAUMemoryObject::save(otTiff, index));
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
bool LAUScan::load(TIFF *inTiff)
{
int dataLength;
char *dataString;
// LOAD A CUSTOM FILESTRING FROM THE DOCUMENT NAME TIFFTAG
if (TIFFGetField(inTiff, TIFFTAG_DOCUMENTNAME, &dataString)) {
fileString = QString(QByteArray(dataString));
}
if (TIFFGetField(inTiff, TIFFTAG_PAGENAME, &dataString)) {
parentByteArray = QByteArray(dataString);
}
if (TIFFGetField(inTiff, TIFFTAG_SOFTWARE, &dataString)) {
softwareString = QByteArray(dataString);
}
if (TIFFGetField(inTiff, TIFFTAG_MODEL, &dataString)) {
modelString = QByteArray(dataString);
}
if (TIFFGetField(inTiff, TIFFTAG_MAKE, &dataString)) {
makeString = QByteArray(dataString);
}
// LOAD THE XML FIELD OF THE TIFF FILE, IF PROVIDED
if (TIFFGetField(inTiff, TIFFTAG_XMLPACKET, &dataLength, &dataString)) {
QXmlStreamReader reader(QByteArray(dataString, dataLength));
while (!reader.atEnd()) {
if (reader.readNext()) {
if (reader.name() == "playbackcolor") {
QString colorString = reader.readElementText();
if (colorString == QString("ColorGray")) {
playbackColor = ColorGray;
} else if (colorString == QString("ColorRGB")) {
playbackColor = ColorRGB;
} else if (colorString == QString("ColorRGBA")) {
playbackColor = ColorRGBA;
} else if (colorString == QString("ColorXYZ")) {
playbackColor = ColorXYZ;
} else if (colorString == QString("ColorXYZG")) {
playbackColor = ColorXYZG;
} else if (colorString == QString("ColorXYZW")) {
playbackColor = ColorXYZW;
} else if (colorString == QString("ColorXYZRGB")) {
playbackColor = ColorXYZRGB;
} else if (colorString == QString("ColorXYZWRGBA")) {
playbackColor = ColorXYZWRGBA;
} else {
playbackColor = ColorUndefined;
}
} else if (reader.name() == "minimumvalues") {
QString rangeString = reader.readElementText();
QStringList floatList = rangeString.split(",");
if (floatList.count() >= 3) {
xMin = floatList.at(0).toFloat();
yMin = floatList.at(1).toFloat();
zMin = floatList.at(2).toFloat();
}
} else if (reader.name() == "maximumvalues") {
QString rangeString = reader.readElementText();
QStringList floatList = rangeString.split(",");
if (floatList.count() >= 3) {
xMax = floatList.at(0).toFloat();
yMax = floatList.at(1).toFloat();
zMax = floatList.at(2).toFloat();
}
} else if (reader.name() == "fieldofview") {
QString fovString = reader.readElementText();
QStringList floatList = fovString.split(",");
if (floatList.count() == 2) {
fov.setX(floatList.at(0).toFloat());
fov.setY(floatList.at(1).toFloat());
}
} else if (reader.name() == "centerofmass") {
QString fovString = reader.readElementText();
QStringList floatList = fovString.split(",");
if (floatList.count() == 3) {
com.setX(floatList.at(0).toFloat());
com.setY(floatList.at(1).toFloat());
com.setZ(floatList.at(2).toFloat());
}
} else if (reader.name() == "transform") {
QString matrixString = reader.readElementText();
matrixString.remove(0, 5);
matrixString.chop(2);
QMatrix4x4 transform;
QStringList rowStringList = matrixString.split(";");
QString rowString = rowStringList.takeFirst();
QStringList floatList = rowString.split(",");
transform(0, 0) = floatList.at(0).toFloat();
transform(0, 1) = floatList.at(1).toFloat();
transform(0, 2) = floatList.at(2).toFloat();
transform(0, 3) = floatList.at(3).toFloat();
rowString = rowStringList.takeFirst();
floatList = rowString.split(",");
transform(1, 0) = floatList.at(0).toFloat();
transform(1, 1) = floatList.at(1).toFloat();
transform(1, 2) = floatList.at(2).toFloat();
transform(1, 3) = floatList.at(3).toFloat();
rowString = rowStringList.takeFirst();
floatList = rowString.split(",");
transform(2, 0) = floatList.at(0).toFloat();
transform(2, 1) = floatList.at(1).toFloat();
transform(2, 2) = floatList.at(2).toFloat();
transform(2, 3) = floatList.at(3).toFloat();
rowString = rowStringList.takeFirst();
floatList = rowString.split(",");
transform(3, 0) = floatList.at(0).toFloat();
transform(3, 1) = floatList.at(1).toFloat();
transform(3, 2) = floatList.at(2).toFloat();
transform(3, 3) = floatList.at(3).toFloat();
// STICK THE TRANSFORM INSIDE THE BUFFER OBJECT
setTransform(transform);
}
}
}
reader.clear();
}
// THIS IS NOT A SCAN FILE, SO RETURN FALSE
if (playbackColor == ColorUndefined) {
// LETS SEE IF WE CAN FIGURE A FEW THINGS OUT BASED ON DIMENSIONS
if (this->colors() == 8) {
playbackColor = ColorXYZWRGBA;
} else {
return (false);
}
}
// SO FAR SO GOOD, NOW LET'S SEE IF WE CAN
// LOAD THE MEMORY OBJECT WITHOUT ERROR
if (LAUMemoryObject::load(inTiff)) {
switch (playbackColor) {
case ColorGray:
return (this->colors() == 1);
case ColorRGB:
return (this->colors() == 3);
case ColorRGBA:
return (this->colors() == 4);
case ColorXYZ:
return (this->colors() == 3);
case ColorXYZG:
return (this->colors() == 4);
case ColorXYZW:
return (this->colors() == 4);
case ColorXYZRGB:
return (this->colors() == 6);
case ColorXYZWRGBA:
return (this->colors() == 8);
case ColorUndefined:
return (false);
}
}
return (false);
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
QVector<float> LAUScan::pixel(QPoint point) const
{
return (pixel(point.x(), point.y()));
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
QVector<float> LAUScan::pixel(int col, int row) const
{
QVector<float> pix(colors());
if (col < 0 || col >= (int)width() || row < 0 || row >= (int)height()) {
memset(pix.data(), 0xff, colors()*sizeof(float));
} else {
memcpy(pix.data(), ((float *)constScanLine(row) + colors()*col), colors()*sizeof(float));
}
return (pix);
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
void LAUScan::updateLimits()
{
if (playbackColor == ColorGray || playbackColor == ColorRGB || playbackColor == ColorRGBA) {
return;
}
__m128 minVec = _mm_set1_ps(1e9f);
__m128 maxVec = _mm_set1_ps(-1e9f);
__m128 menVec = _mm_set1_ps(0.0f);
int index = 0;
int pixelCount = 0;
float *buffer = (float *)constScanLine(0);
if (colors() % 4 == 0) {
for (unsigned int row = 0; row < height(); row++) {
for (unsigned int col = 0; col < width(); col++) {
__m128 pixVec = _mm_load_ps(buffer + index);
__m128 mskVec = _mm_cmpeq_ps(pixVec, pixVec);
if (_mm_test_all_ones(_mm_castps_si128(mskVec))) {
pixelCount++;
minVec = _mm_min_ps(minVec, pixVec);
maxVec = _mm_max_ps(maxVec, pixVec);
menVec = _mm_add_ps(menVec, pixVec);
}
index += colors();
}
}
} else {
for (unsigned int row = 0; row < height(); row++) {
for (unsigned int col = 0; col < width(); col++) {
__m128 pixVec = _mm_loadu_ps(buffer + index);
__m128 mskVec = _mm_cmpeq_ps(pixVec, pixVec);
if (_mm_test_all_ones(_mm_castps_si128(mskVec))) {
pixelCount++;
minVec = _mm_min_ps(minVec, pixVec);
maxVec = _mm_max_ps(maxVec, pixVec);
menVec = _mm_add_ps(menVec, pixVec);
}
index += colors();
}
}
}
*(int *)&xMin = _mm_extract_ps(minVec, 0);
*(int *)&xMax = _mm_extract_ps(maxVec, 0);
*(int *)&yMin = _mm_extract_ps(minVec, 1);
*(int *)&yMax = _mm_extract_ps(maxVec, 1);
*(int *)&zMin = _mm_extract_ps(minVec, 2);
*(int *)&zMax = _mm_extract_ps(maxVec, 2);
float val = 0.0;
*(int *)&val = _mm_extract_ps(menVec, 0);
com.setX(val / (float)pixelCount);
*(int *)&val = _mm_extract_ps(menVec, 1);
com.setY(val / (float)pixelCount);
*(int *)&val = _mm_extract_ps(menVec, 2);
com.setZ(val / (float)pixelCount);
if (qFabs(zMin) > qFabs(zMax)) {
float z = zMin;
zMin = zMax;
zMax = z;
}
if (zMin == 0.0f) {
zMin = 1.0f;
}
// CALCULATE THE FIELD OF VIEW OF THE BOUNDING BOX FROM THE ORIGIN
float deltaX = qMax(xMin, xMax) - qMin(xMin, xMax);
float deltaY = qMax(yMin, yMax) - qMin(yMin, yMax);
fov = QPointF(2.0f * qAtan2(deltaX / 2.0f, qFabs(zMax)), 2.0f * qAtan2(deltaY / 2.0f, qFabs(zMax)));
return;
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
QList<QImage> LAUScan::nearestNeighborMap()
{
QList<QImage> images;
QImage imageA(941, 941, QImage::Format_ARGB32);
imageA.fill(Qt::black);
updateLimits();
float xn = qMin(minX(), maxX());
float xx = qMax(minX(), maxX()) - xn;
float yn = qMin(minY(), maxY());
float yx = qMax(minY(), maxY()) - yn;
float zn = qMin(minZ(), maxZ());
float zx = qMax(minZ(), maxZ()) - zn;
for (unsigned int row = 0; row < height(); row++) {
for (unsigned int col = 0; col < width(); col++) {
QVector<float> vector = pixel(col, row);
if (!qIsNaN(vector[0]) && !qIsNaN(vector[1]) && !qIsNaN(vector[2])) {
int xi = qRound((vector[0] - xn) / xx * 95);
int yi = qRound((vector[1] - yn) / yx * 95);
int zi = qRound((vector[2] - zn) / zx * 95);
int index = zi * 96 * 96 + yi * 96 + xi;
imageA.setPixel(index % 941, index / 941, qRgb(255, 255, 255));
}
}
}
images << imageA;
QImage imageB(182, 182, QImage::Format_ARGB32);
imageB.fill(Qt::black);
for (unsigned int chn = 0; chn < 96; chn++) {
for (unsigned int row = 0; row < 96; row++) {
for (unsigned int col = 0; col < width(); col++) {
;
}
}
}
images << imageB;
return (images);
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
LAUScan LAUScan::mergeScans(QList<LAUScan> scans)
{
if (scans.count() == 0) {
return (LAUScan());
}
// KEEP A COPY OF THE NUMBER OF SCANS IN THE LIST
int N = scans.count();
// CREATE A COPY OF THE FIRST SCAN IN THE LIST
LAUScan scan = scans.takeFirst();
// NOW ITERATE THROUGH ENTIRE LIST MAKING SURE THEY ARE ALL THE SAME SIZE AND COLOR
while (scans.count() > 0) {
LAUScan scnp = scans.takeFirst();
// MAKE SURE NEW SCAN IS SAME COLOR AS REFERENCE SCAN
if (scnp.color() != scan.color()) {
scnp = scnp.convertToColor(scan.color());
}
// MAKE SURE NEW SCAN IS SAME SIZE AND REFERENCE SCAN
if (scnp.width() != scan.width() || scnp.height() != scan.height()) {
scnp = scnp.resize(scan.width(), scan.height());
}
// ADD NEW SCAN'S BUFFER TO EXIST BUFFER
for (unsigned int i = 0; i < scan.length(); i += 16) {
// LOAD THE NEXT 16 BYTES OF DATA FROM THE TWO BUFFERS
__m128 inVector = _mm_load_ps((float *)(scan.constPointer() + i));
__m128 otVector = _mm_load_ps((float *)(scnp.constPointer() + i));
// SAVE THE SUM OF THE TWO VECTORS BACK INTO THE REFERENCE BUFFER
_mm_store_ps((float *)(scan.constPointer() + i), _mm_add_ps(inVector, otVector));
}
}
// NOW WE NEED TO DIVIDE THE BUFFER BY THE NUMBER OF IMAGES
__m128 otVector = _mm_set1_ps(1.0f / (float)N);
for (unsigned int i = 0; i < scan.length(); i += 16) {
// LOAD THE NEXT 16 BYTES OF DATA FROM THE TWO BUFFERS
__m128 inVector = _mm_load_ps((float *)(scan.constPointer() + i));
// SAVE THE SUM OF THE TWO VECTORS BACK INTO THE REFERENCE BUFFER
_mm_store_ps((float *)(scan.constPointer() + i), _mm_mul_ps(inVector, otVector));
}
return (scan);
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
LAUScan LAUScan::maskChannel(unsigned int chn, float threshold) const
{
LAUScan image = LAUScan(width(), height(), color()) + *this;
unsigned int channels = colors();
for (unsigned int row = 0; row < height(); row++) {
float *fmBuffer = (float *)constScanLine(row);
float *toBuffer = (float *)image.scanLine(row);
for (unsigned int col = 0; col < width(); col++) {
if (fmBuffer[col * channels + chn] > threshold) {
memcpy(&toBuffer[col * channels], &fmBuffer[col * channels], channels * sizeof(float));
} else {
memset(&toBuffer[col * channels], 0xFF, channels * sizeof(float));
}
}
}
return (image);
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
int LAUScan::inspectImage()
{
#ifdef LAUSCANINSPECTOR_H
LAUScanInspector dialog(*this, false);
dialog.setWindowTitle(filename());
return (dialog.exec());
#else
return(0);
#endif
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
const QVector3D LAUScan::center()
{
QVector<float> bounds = boundingBox();
return (QVector3D(bounds[0] + bounds[1], bounds[2] + bounds[3], bounds[4] + bounds[5]) / 2.0f);
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
const QVector<float> LAUScan::boundingBox()
{
QVector<float> bounds(6, NAN);
// DERIVE THE TRANSFORM FROM SCAN SPACE TO VOXEL MAP SPACE
bounds[0] = qMin(maxX(), minX());
bounds[1] = qMax(maxX(), minX());
bounds[2] = qMin(maxY(), minY());
bounds[3] = qMax(maxY(), minY());
bounds[4] = qMin(maxZ(), minZ());
bounds[5] = qMax(maxZ(), minZ());
return (bounds);
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
bool LAUScan::approveImage(bool *doNotShowAgainCheckBoxEnabled, QWidget *parent)
{
#ifdef LAUSCANINSPECTOR_H
if (doNotShowAgainCheckBoxEnabled) {
LAUScanInspector dialog(*this, true, *doNotShowAgainCheckBoxEnabled, parent);
dialog.setWindowTitle(this->filename());
int ret = dialog.exec();
*doNotShowAgainCheckBoxEnabled = !dialog.doNotShowAgainChecked();
return (ret == QDialog::Accepted);
} else {
LAUScanInspector dialog(*this, true, false, parent);
dialog.setWindowTitle(this->filename());
return (dialog.exec() == QDialog::Accepted);
}
#else
return(false);
#endif
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
LAUScan LAUScan::extractChannel(unsigned int channel) const
{
LAUScan image = LAUScan(width(), height(), ColorGray) + *this;
for (unsigned int row = 0; row < height(); row++) {
float *toBuffer = (float *)constScanLine(row);
float *inBuffer = (float *)image.scanLine(row);
for (unsigned int col = 0; col < width(); col++) {
inBuffer[col] = toBuffer[col * colors() + channel];
}
}
return (image);
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
LAUScan LAUScan::crop(unsigned int x, unsigned int y, unsigned int w, unsigned int h)
{
if ((x + w) > width()) {
w = width() - x;
}
if ((y + h) > height()) {
h = height() - y;
}
LAUScan image = LAUScan(w, h, playbackColor) + *this;
for (unsigned int r = 0; r < image.height(); r++) {
float *toBuffer = (float *)image.scanLine(r);
float *inBuffer = (float *) & (((float *)constScanLine(y + r))[colors() * x]);
memcpy(toBuffer, inBuffer, w * colors()*sizeof(float));
}
return (image);
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
LAUScan LAUScan::resize(unsigned int cols, unsigned int rows)
{
// CREATE AN IMAGE TO HOLD THE OUTPUT SCAN
LAUScan image = LAUScan(cols, rows, color()) + *this;
// ITERATE THROUGH EACH ROW OF THE OUTPUT IMAGE
for (unsigned int r = 0; r < image.height(); r++) {
// CALCULATE THE INTERPOLATED ROW IN THE INPUT BUFFER
int row = qFloor((double)r / (double)rows * (double)this->height());
// GET POINTERS TO THE SCAN ROWS FROM THE INPUT AND OUTPUT SCANS
float *inBuffer = (float *)constScanLine(row);
float *otBuffer = (float *)image.scanLine(r);
// ITERATE ACROSS A ROW OF PIXELS
for (unsigned int c = 0; c < image.width(); c++) {
int col = qFloor((double)c / (double)cols * (double)this->width());
// COPY A COMPLETE PIXEL FROM THE IN BUFFER TO THE OUT BUFFER
memcpy(&otBuffer[c * colors()], &inBuffer[col * colors()], nugget());
}
}
return (image);
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
int LAUScan::extractXYZWVertices(float *toBuffer, int downSampleFactor) const
{
// CREATE POINTER TO OUTPUT BUFFER TO KEEP TRACK OF NUMBER OF VALID POINTS
int indexOt = 0;
// CREATE A VECTOR TO HOLD ONES THAT WE CAN INSERT INTO XYZW VECTOR
if (toBuffer) {
if (color() == ColorGray) {
return (0);
} else if (color() == ColorRGB) {
return (0);
} else if (color() == ColorRGBA) {
return (0);
} else if (color() == ColorXYZ) {
__m128 mskVec = _mm_castsi128_ps(_mm_set_epi32(0, -1, -1, -1));
__m128 oneVec = _mm_set_ps(1.0f, 0.0f, 0.0f, 0.0f);
for (unsigned int row = 0; row < height(); row += downSampleFactor) {
float *inBuffer = (float *)constScanLine(row);
for (unsigned int col = 0; col < width(); col += downSampleFactor) {
// GRAB COPY OF CURRENT PIXEL'S XYZW COORDINATES
__m128 vecIn = _mm_loadu_ps(inBuffer + 3 * col);
// INSERT A ONE IN THE W COORDINATE
vecIn = _mm_add_ps(oneVec, _mm_and_ps(vecIn, mskVec));
// TEST TO SEE IF ANY ELEMENTS IN THE INCOMING VECTOR ARE NANS
// AND IF NOT THEN COPY THE VALID PIXEL INTO THE OUTPUT BUFFER
if (_mm_test_all_ones(_mm_castps_si128(_mm_cmpeq_ps(vecIn, vecIn)))) {
_mm_store_ps(toBuffer + 4 * (indexOt++), vecIn);
}
}
}
} else if (color() == ColorXYZW) {
for (unsigned int row = 0; row < height(); row += downSampleFactor) {
float *inBuffer = (float *)constScanLine(row);
for (unsigned int col = 0; col < width(); col += downSampleFactor) {
// GRAB COPY OF CURRENT PIXEL'S XYZW COORDINATES
__m128 vecIn = _mm_load_ps(inBuffer + 4 * col);
// TEST TO SEE IF ANY ELEMENTS IN THE INCOMING VECTOR ARE NANS
// AND IF NOT THEN COPY THE VALID PIXEL INTO THE OUTPUT BUFFER
if (_mm_test_all_ones(_mm_castps_si128(_mm_cmpeq_ps(vecIn, vecIn)))) {
_mm_store_ps(toBuffer + 4 * (indexOt++), vecIn);
}
}
}
} else if (color() == ColorXYZG) {
__m128 mskVec = _mm_castsi128_ps(_mm_set_epi32(0, -1, -1, -1));
__m128 oneVec = _mm_set_ps(1.0f, 0.0f, 0.0f, 0.0f);
for (unsigned int row = 0; row < height(); row += downSampleFactor) {
float *fmBuffer = (float *)constScanLine(row);
for (unsigned int col = 0; col < width(); col += downSampleFactor) {
// GRAB COPY OF CURRENT PIXEL'S XYZW COORDINATES
__m128 vecIn = _mm_load_ps(fmBuffer + 4 * col);
// TEST TO SEE IF ANY ELEMENTS IN THE INCOMING VECTOR ARE NANS
// AND IF NOT THEN COPY THE VALID PIXEL INTO THE OUTPUT BUFFER
if (_mm_test_all_ones(_mm_castps_si128(_mm_cmpeq_ps(vecIn, vecIn)))) {
// INSERT A ONE IN THE W COORDINATE AND STORE
_mm_store_ps(toBuffer + 4 * (indexOt++), _mm_add_ps(oneVec, _mm_and_ps(vecIn, mskVec)));
}
}
}
} else if (color() == ColorXYZRGB) {
__m128 mskVec = _mm_castsi128_ps(_mm_set_epi32(0, -1, -1, -1));
__m128 oneVec = _mm_set_ps(1.0f, 0.0f, 0.0f, 0.0f);
for (unsigned int row = 0; row < height(); row += downSampleFactor) {
float *inBuffer = (float *)constScanLine(row);
for (unsigned int col = 0; col < width(); col += downSampleFactor) {
// GRAB COPY OF CURRENT PIXEL'S XYZW COORDINATES
__m128 vecIn = _mm_loadu_ps(inBuffer + 6 * col);
// TEST TO SEE IF ANY ELEMENTS IN THE INCOMING VECTOR ARE NANS
// AND IF NOT THEN COPY THE VALID PIXEL INTO THE OUTPUT BUFFER
if (_mm_test_all_ones(_mm_castps_si128(_mm_cmpeq_ps(vecIn, vecIn)))) {
// INSERT A ONE IN THE W COORDINATE AND STORE
_mm_store_ps(toBuffer + 4 * (indexOt++), _mm_add_ps(oneVec, _mm_and_ps(vecIn, mskVec)));
}
}
}
} else if (color() == ColorXYZWRGBA) {
__m128 mskVec = _mm_castsi128_ps(_mm_set_epi32(0, -1, -1, -1));
__m128 oneVec = _mm_set_ps(1.0f, 0.0f, 0.0f, 0.0f);
for (unsigned int row = 0; row < height(); row += downSampleFactor) {
float *inBuffer = (float *)constScanLine(row);
for (unsigned int col = 0; col < width(); col += downSampleFactor) {
// GRAB COPY OF CURRENT PIXEL'S XYZW COORDINATES
__m128 vecIn = _mm_load_ps(inBuffer + 8 * col);
// TEST TO SEE IF ANY ELEMENTS IN THE INCOMING VECTOR ARE NANS
// AND IF NOT THEN COPY THE VALID PIXEL INTO THE OUTPUT BUFFER
if (_mm_test_all_ones(_mm_castps_si128(_mm_cmpeq_ps(vecIn, vecIn)))) {
_mm_store_ps(toBuffer + 4 * (indexOt++), _mm_add_ps(oneVec, _mm_and_ps(vecIn, mskVec)));
}
}
}
}
}
// RETURN THE NUMBER OF VALID POINTS COPIED INTO THE OUTPUT BUFFER
return (indexOt);
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
int LAUScan::pointCount() const
{
// CREATE POINTER TO OUTPUT BUFFER TO KEEP TRACK OF NUMBER OF VALID POINTS
int indexOt = 0;
// CREATE A VECTOR TO HOLD ONES THAT WE CAN INSERT INTO XYZW VECTOR
if (color() == ColorGray) {