-
Notifications
You must be signed in to change notification settings - Fork 1
/
w10n_XYZ-extractor-023.html
873 lines (733 loc) · 31.5 KB
/
w10n_XYZ-extractor-023.html
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
<!DOCTYPE html>
<html>
<head>
<meta name="generator" content="HTML Tidy for HTML5 for Windows version 5.6.0">
<style>
body {
font-family: monospace;
}
#btnGo {
margin-top: 20px;
line-height: 25px;
}
</style>
<script type='text/javascript' src='https://code.jquery.com/jquery-3.5.1.min.js'></script>
<title></title>
</head>
<body>
<script>
// 0.2.2 Funziona tutto il processing dei .ht, inclusa scelta banda e scelta esagerazione altezza; da sistemare XYZ e gli altri.
// 0.2.1 Sistemati float, ma cambiato formato array, da risistemare il prcocessing successivo
// 0.2.0 Implementata lettura 3 file, ma da sistemare intepretazione dati RGB (una banda sola?!? forse è b/n?)) e da verificare validità dati RGB e Normals con sito JSON
// 0.0.3/0.1.0: First version successullying creating PLY file
// 0.0.2: First version properly creating output string in debug mode
// 0.0.1: First version with "float decoding" implemented
//
// Online PLY viewers:
// http://lidarview.com/
// Offline PLY viewers:
// Meshlab (can add mesh)
//
var debugCounter = 0;
var errorsArr = [];
var PLY_HEADER_BASE = 'ply\n'+
'format ascii 1.0\n'+
'element vertex VVVVVV\n'+
'property float x\n'+
'property float y\n'+
'property float z\n';
var PLY_HEADER_VISIBLE = "" +
'property uchar red\n'+
'property uchar green\n'+
'property uchar blue\n';
var PLY_HEADER_NORMALS = "" +
'property float nx\n'+
'property float ny\n'+
'property float nz\n'+
'property list uchar float vertex_indices\n';
var PLY_HEADER_END= 'element face 0\n'+
'end_header\n';
var FACTOR = 10;
const TOSKIP = 1;
const START_MARKER = '<A HREF="';
const END_MARKER = '">';
const PROXY = "http://win98.altervista.org/space/exploration/myp.php?pass=miapass&mode=native&url=";
const SERVER_URL = "https://pds-imaging.jpl.nasa.gov";
var PAGE_URL = "/w10n/mer/spirit/mer2no_0xxx/data/sol1866/rdr/2n292022791xylb0ozf0006l0m1.img/0/raster/data[]?output=json"; // provare con THUMBNAIL (DEBUG)
var toolsList = [];
var toolsListTable = [];
fileDataXYZ = null; // XYZ products
fileDataVisible = null; // RAD or FFL products
fileDataNormals = null; // UV products
fileDataHeightMap = null; // .HT files: https://pds-imaging.jpl.nasa.gov/data/mer/spirit/mer2mw_0xxx/data/navcam/site0137/
const MAXFLOAT = 1000000;
HTSelectedBand = 0;
function startProcessing() {
processData(/*fileDataXYZ, fileDataVisible, fileDataNormals*/);
console.log("Processing ended.");
}
function loadFile(fileHandler, dataType) {
console.log(1);
console.log("DataType in LOADFILE:", dataType);
const reader = new FileReader();
reader.addEventListener('load', (event) => {
stringContents = event.target.result;
console.log("Loaded " + dataType + ": ", stringContents.length);
if (dataType === "XYZ") {
fileDataXYZ = processFile(stringContents, dataType); // processFile returns data from file converted to JSON format
console.log("Returned from XYZ processing: ", fileDataXYZ);
}
if (dataType === "visible") {
fileDataVisible = processFile(stringContents, dataType); // processFile returns data from file converted to JSON format
console.log("Returned from VISIBLE processing", fileDataVisible);
}
if (dataType === "normals") {
fileDataNormals = processFile(stringContents, dataType); // processFile returns data from file converted to JSON format
console.log("Returned from NORMALS processing", fileDataNormals);
}
if (dataType === "height") {
fileDataHeightMap = processFile(stringContents, dataType); // processFile returns data from file converted to JSON format
console.log("Returned from HEIGHT processing", fileDataHeightMap);
}
document.getElementById("status").innerHTML = "First processing completed.";
});
reader.readAsBinaryString(fileHandler);
console.log(2);
}
function saveFile(fileContent, filename) {
myBlob = new Blob([fileContent], {type: "text/plain"});
var url = window.URL.createObjectURL(myBlob);
var anchor = document.createElement("a");
anchor.href = url;
anchor.download = filename;
anchor.click();
window.URL.revokeObjectURL(url);
//document.removeChild(anchor);
}
function handleEvent(e) {
log.textContent = `${e.type}: ${e.loaded} bytes transferred\n` + log.textContent;
}
function addListeners(xhr) {
xhr.addEventListener('loadstart', handleEvent);
xhr.addEventListener('load', handleEvent);
xhr.addEventListener('loadend', handleEvent);
xhr.addEventListener('progress', handleEvent);
xhr.addEventListener('error', handleEvent);
xhr.addEventListener('abort', handleEvent);
console.log("Progress listeners added.");
}
function writeCanvas(x,y,r,g,b) {
//var x = Math.floor(Math.random() * canvasWidth);
//var y = Math.floor(Math.random() * canvasHeight);
//var r = Math.floor(Math.random() * 256);
//var g = Math.floor(Math.random() * 256);
//var b = Math.floor(Math.random() * 256);
ctx.fillStyle = 'rgb(' + r + ',' + g + ',' + b + ')';
ctx.fillRect(x,y,x+1,y+1);
/* var off = (y * id.width + x) * 4;
pixels[off] = r;
pixels[off + 1] = g;
pixels[off + 2] = b;
pixels[off + 3] = 255;
ctx.putImageData(id, 0, 0);*/
}
httpRequest = (PROXY, rawurl, method = 'GET', type="XML") => {
url = PROXY + rawurl;
console.log(">>>FILE:",url);
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
addListeners(xhr);
xhr.open(method, url);
xhr.onload = () => {
if (xhr.status === 200) {
console.log("OK!");
rawResponse = xhr.responseText;
try {
jsonResponse = JSON.parse(rawResponse);
} catch (e) {
console.log("JSON error:" , e);
console.log(rawResponse);
}
console.log(jsonResponse);
processData(jsonResponse);
} else {
console.log("ERROR!");
reject(new Error(xhr.responseText));
}
};
xhr.progress = (a) => {
console.log("PROG:",a);
document.getElementById("status").innerHTML = "Progress: " + a;
};
xhr.send();
});
}
function processFile(fileContents, dataType) {
console.log("Start processing...", fileContents.length);
testami = fileContents;
endianness = "BE";
if (dataType === "height") endianness = "LE"; // debug - get from label(s)
const IMAGE_MARKER = "/* IMAGE DATA ELEMENTS */";
const START_OBJECT_MARKER = "OBJECT";
const END_OBJECT_MARKER = "END_OBJECT";
const BYTE_LENGTH = 8;
if(fileContents.indexOf("PDS_VERSION_ID") >=0 ) {
/////// Read PDS label data:
recordBytesLineStartPos = fileContents.indexOf("RECORD_BYTES");
recordBytesLineEndPos = fileContents.indexOf("\n",recordBytesLineStartPos);
recordBytesLine = fileContents.substr(recordBytesLineStartPos, recordBytesLineEndPos - recordBytesLineStartPos + 1);
recordBytesArr = recordBytesLine.split("=");
recordBytes = parseInt(recordBytesArr[1]);
console.log("recordBytes=",recordBytes);
fileRecordsLineStartPos = fileContents.indexOf("FILE_RECORDS");
fileRecordsLineEndPos = fileContents.indexOf("\n",fileRecordsLineStartPos);
fileRecordsLine = fileContents.substr(fileRecordsLineStartPos, fileRecordsLineEndPos - fileRecordsLineStartPos + 1);
fileRecordsArr = fileRecordsLine.split("=");
fileRecords = parseInt(fileRecordsArr[1]);
console.log("fileRecords=",fileRecords);
labelRecordsLineStartPos = fileContents.indexOf("LABEL_RECORDS");
labelRecordsLineEndPos = fileContents.indexOf("\n",labelRecordsLineStartPos);
labelRecordsLine = fileContents.substr(labelRecordsLineStartPos, labelRecordsLineEndPos - labelRecordsLineStartPos + 1);
labelRecordsArr = labelRecordsLine.split("=");
labelRecords = parseInt(labelRecordsArr[1]);
console.log("labelRecords=",labelRecords);
PDSlabelLength = recordBytes * labelRecords;
PDSlabel = fileContents.substr(0,PDSlabelLength);
console.log("PDS label:", PDSlabelLength);
} else {
console.log(">>>> NO PDS LABEL, looking for VICAR label...");
PDSlabelLength = 0;
PDSlabel = "";
}
// "The total of the label (in bytes) is an integral multiple (x LABEL_RECORDS) of the record length (RECORD_BYTES) of the data: PDS_length = RECORD_BYTES * LABEL_RECORDS"
////// Read VICAR label data:
pippo = fileContents;
// VICAR label values
//
// FORMAT:
// BYTE: 8bit
// HALF: 16 bit, signed int (also WORD)
// FULL: 32 bit, signed int (also LONG)
//
// REAL: single precision float (32 bit?)
// DOUB: double precision float (64 bit?)
//
// COMP: complex, made of 2 REAL numbers (also COMPLEX)
//
// INTFMT:
// HIGH = high byte first, big endian
// LOW = low byte first, little endian
//
// REALFMT:
// IEEE: IEE754 format, high order bytes first
// RIEEE: Reverse IEEE format, exponent last
// VAX: VAX format
//
// FFL (visible image):
// FORMAT = HALF (16 bit signed int, -32768 / +32767)
// INTFMT = HIGH (high byte first, big endian)
// REALFMT = RIEEE (Reverse IEEE format, exponent last)
//
// XYZ (point cloud):
// FORMAT = REAL (32 bit real)
// INTFMT = HIGH (high byte first, big endian)
// REALFMT = IEEE (IEE754 format, high order bytes first)
//
// UV (normals):
// FORMAT = REAL (32 bit real)
// INTFMT = HIGH (high byte first, big endian)
// REALFMT = IEEE (IEE754 format, high order bytes first)
//
// HT (Height Map):
// FORMAT = REAL (32 bit real)
// INTFMT = LOW (low byte first, little endian)
// REALFMT = RIEEE (Reverse IEEE format, exponent last)
//
vicarLabelLengthLineStartPos = fileContents.indexOf("LBLSIZE=")*1;
vicarLabelLength = readVicarParam(fileContents,"LBLSIZE=")*1;
vicarLines = readVicarParam(fileContents,"NL=")*1;
vicarColumns = readVicarParam(fileContents,"NS=")*1;
vicarFormat = readVicarParam(fileContents,"FORMAT=");
vicarFormatRealLength = -1;
vicarRealFormat = readVicarParam(fileContents,"REALFMT=");
if (vicarFormat.indexOf("'REAL'") >= 0 ) {
vicarFormatRealLength = 4;
if ( vicarRealFormat = "'IEEE'") { // IEE754 format, high order bytes first, i.e. Big-Endian
endianness = "BE"; // debug
} else { // 'RIEEE' = reverse IEEE, exponent last
endianness = "LE"; // debug
}
} else {
vicarFormatRealLength = -2;
}
vicarIntFormat = "[empty]";
vicarIntFormat = readVicarParam(fileContents,"INTFMT=");
vicarBands = readVicarParam(fileContents,"NB=")*1;
vicarLabel = fileContents.substr(vicarLabelLengthLineStartPos, vicarLabelLength).replace('\x00',"");
vicarLabel=vicarLabel.split(" ").join("=========");
vicarLabel=vicarLabel.split(" ").join("---");
vicarLabel=vicarLabel.split(" ").join("\r\n");
console.log("vicarLabel:", vicarLabelLength, vicarLabel);
console.log("Data begin at ", vicarLabelLength, "0x" + vicarLabelLength.toString(16));
console.log("vicarLines:", vicarLines);
console.log("vicarColumns:", vicarColumns);
console.log("vicarFormat:", vicarFormat);
console.log("vicarFormatRealLength:", vicarFormatRealLength);
console.log("vicarBands:", vicarBands);
totalLabelLength = PDSlabelLength + vicarLabelLength;
onlyData = fileContents.substr(totalLabelLength , fileContents.length - totalLabelLength + 1);
console.log("onlyData length=", onlyData.length, onlyData.length/3);
///// Read image/band metadata:
imageDataStartPos = fileContents.indexOf(IMAGE_MARKER);
imageDataStartPos = fileContents.indexOf(START_OBJECT_MARKER,imageDataStartPos);
imageDataEndPos = fileContents.indexOf(END_OBJECT_MARKER,imageDataStartPos);
imageDataLabel = fileContents.substr(imageDataStartPos, imageDataEndPos - imageDataStartPos);
imageDataLabel = imageDataLabel.split(" ").join("");
imageDataLabelArr = imageDataLabel.split("\r\n");
imageDataLabelObj = {}
imageDataLabelArr.forEach((elem) => {
var pair = elem.split("=");
imageDataLabelObj[pair[0]] = pair[1];
});
imageLines = imageDataLabelObj["LINES"];
imageSamplesPerLine = imageDataLabelObj["LINE_SAMPLES"];
bytesPerSample = imageDataLabelObj["SAMPLE_BITS"]/BYTE_LENGTH;
sampleTypePDS = imageDataLabelObj["SAMPLE_TYPE"];
lineLength = imageSamplesPerLine * bytesPerSample;
bandsNum = imageDataLabelObj["BANDS"] * 1
bandLength = imageLines * lineLength;
imageLength = bandLength * bandsNum;
console.log("PDSlabelLength", "vicarLabelLength", "totalLabelLength");
console.log(PDSlabelLength, vicarLabelLength, totalLabelLength);
console.log(PDSlabelLength.toString(16), vicarLabelLength.toString(16), totalLabelLength.toString(16));
console.log(" ============= PRIMA ==========");
console.log("imageLines",imageLines);
console.log("imageSamplesPerLine",imageSamplesPerLine);
console.log("bytesPerSample",bytesPerSample);
console.log("sampleTypePDS",sampleTypePDS);
console.log("lineLength",lineLength);
console.log("bandsNum",bandsNum);
console.log("bandLength",bandLength);
console.log("imageLength",imageLength);
console.log("file length",fileContents.length);
if ((totalLabelLength === NaN) || (!totalLabelLength)) totalLabelLength = 0;
if ((imageLines === NaN) || (!imageLines)) imageLines = vicarLines;
if ((imageSamplesPerLine === NaN) || (!imageSamplesPerLine)) imageSamplesPerLine = vicarColumns;
if ((bytesPerSample === NaN) || (!bytesPerSample)) bytesPerSample = vicarFormatRealLength;
if ((lineLength === NaN) || (!lineLength)) lineLength = vicarColumns * vicarFormatRealLength;
if ((bandsNum === NaN) || (!bandsNum)) bandsNum = vicarBands*1 ;
if ((bandLength === NaN) || (!bandLength)) bandLength = vicarLines * lineLength;
if ((imageLength === NaN) || (!imageLength)) imageLength = bandLength * bandsNum ;
console.log(" ============= DOPO ==========");
console.log("imageLines",imageLines, "0x"+ imageLines.toString(16));
console.log("imageSamplesPerLine",imageSamplesPerLine, "0x"+ imageSamplesPerLine.toString(16));
console.log("bytesPerSample",bytesPerSample, "0x"+ bytesPerSample.toString(16));
console.log("sampleTypePDS",sampleTypePDS);
console.log("lineLength",lineLength, "0x"+ lineLength.toString(16));
console.log("bandsNum",bandsNum, "0x"+ bandsNum.toString(16));
console.log("bandLength",bandLength, "0x"+ bandLength.toString(16));
console.log("imageLength",imageLength, "0x"+ imageLength.toString(16));
console.log("totalLabelLength",totalLabelLength*1, "0x"+ totalLabelLength.toString(16));
console.log("file length",fileContents.length, "0x"+ fileContents.length.toString(16));
console.log("fileContents:", fileContents.length, "0x"+ fileContents.length.toString(16));
console.log("Processing data...");
imageTable = [null, null, null]; // Source data (bytes)
bandArray = [null, null, null]; // Processed data
bandArrayArr = [null, null, null]; // Processed data
bandArrayFloat = [null, null, null]; // Processed data (floats)
for (var band = 0; band < bandsNum; band++) {
maxFloatVal = 0; //-3.40282e+38;
minFloatVal = 0; //3.4028237E38;
console.log(" Processing band " , band, "...");
document.getElementById("status").innerHTML = "Processing band " , band, "...";
console.log("Leggo banda " + band + " da " + (totalLabelLength + band * bandLength) + " a " + (totalLabelLength + band * bandLength + bandLength ) + " (escluso ultimo)");
// BSQ format:
// A file is just a long sequence of bytes without an actual structure, just a logical structure; let's create a javascript structure corresponding to this logical structure:
// A file contains a sequence of images;
// Each image is a "band"; the file is hence a "multiband image" or "multispectral image"; all bands/images have same length.
// An image is made of lines;
// A line is made of "samples";
// A sample is a group of one or more bytes ("bytesPerSample")
//
// So our structure will be as follows:
// { File begin
// [ image/band begin
// [ line begin
// [n bytes], [n bytes], [n bytes] ,... // this is a line, made of groups of bytes; such groups are "samples"
// ], line end
// [ another line],
// [ another line],
// ...
// ], image/band end
// [ another band/image],
// [ another band/image]
// ....
// } file end
// Separate the n bands/images into n elements of an array:
imageTable[band] = fileContents.slice(totalLabelLength + band * bandLength, totalLabelLength + band * bandLength + bandLength);
console.log( "Lunghezza imageTable[" + band + "]:" , imageTable[band].length, "0x"+ imageTable[band].length.toString(16));
// Let's setup a new table, where each sample is actually an array of bytes rather than a sequence of bytes insed a sequence of bytes
bandArray[band] = []; // Each element of the array will contain the structured data of one band
bandArrayArr[band] = []; //
bandArrayFloat[band] = [];
console.log("Elaboro elementi di imageTable[" + band + "] da 0 a " + (imageTable[band].length-1));
rowsCount = 0;
for (var linePointer = 0; linePointer < imageTable[band].length; linePointer += lineLength) { // Parse all image line by line
const imageLine = imageTable[band].slice(linePointer, linePointer + lineLength);
colsCount = 0;
samplesArray = [];
samplesArrayArr = [];
samplesArrayFloat = [];
for (var samplePointer = 0; samplePointer < imageLine.length; samplePointer += bytesPerSample) { // rows
const sample = imageLine.slice(samplePointer, samplePointer + bytesPerSample); // takes the group of bytes which makes a sample
sampleArr = []; // Turn the sequence of characters into an array of bytes
for (var bytePointer = 0; bytePointer < bytesPerSample; bytePointer++) {
sampleArr.push(sample.charCodeAt(bytePointer));
}
samplesArray.push(sample); // Store the group into an "array of samples", which is an image line.
samplesArrayArr.push(sampleArr);
floatVal = arrayToFloat(sampleArr, endianness);
if (maxFloatVal < floatVal)
maxFloatVal = floatVal;
if (minFloatVal > floatVal)
minFloatVal = floatVal;
if (sampleArr.length === 4) {
samplesArrayFloat.push(floatVal);
} else {
samplesArrayFloat.push(0);
}
colsCount++;
}
bandArray[band].push(samplesArray);
bandArrayArr[band].push(samplesArrayArr);
bandArrayFloat[band].push(samplesArrayFloat);
rowsCount++;
}
console.log("Length of calculated array for band " + band + ":" + bandArray[band].length);
console.log("Min float:",minFloatVal, ", maxFloat=",maxFloatVal);
}
console.log(bandArrayFloat);
return bandArrayFloat; //debug
console.log("Data processed, converting to JSON...");
totalStr = '{ "data":[';
totalJson = {data : [] };
///// user configurable (to do):
startRow = 0;
endRow = imageLines;
startCol = 0;
endCol = imageSamplesPerLine;
//////
for (var row = startRow; row < endRow ; row++) {
rowJson = [];
rowStr = "[";
//console.log("Processing row " + row + " (" + (100*row/endRow).toFixed(0) + "%)");
// document.getElementById("status").innerHTML = "Processing row " + row + " (" + (100*row/endRow).toFixed(0) + "%)";
for (var col = startCol ; col < endCol; col++) {
element = "[";
jsonElement = [col, row];
if ((dataType === "XYZ") || (dataType === "normals") || (dataType === "visible")) {
for (var band = 0; band < bandsNum; band++) {
if ((dataType === "XYZ") || (dataType === "normals") ){ // XYZ, Normals and height map are float32, but height map is LSB-based rather tna MSB-based
//element += arrayToFloat(bandArray[band][row][col], endianness) + ",";
jsonElement.push(bandArrayFloat[band][row][col]);
}
if (dataType === "visible") { // visible data are integer (16 bit)
//element += arrayToInt(bandArray[band][row][col]) + ",";
jsonElement.push(arrayToInt(bandArray[band][row][col]));
}
rowJson.push(jsonElement);
}
} else {
textVal = bandArray[HTSelectedBand][row][col];
hexVal = textVal.charCodeAt(0).toString(16) + "." + textVal.charCodeAt(1).toString(16) + "." + textVal.charCodeAt(2).toString(16) + "." + textVal.charCodeAt(3).toString(16);//debug
floatValue1 = arrayToFloat(textVal, "BE");
floatValue2 = arrayToFloat(textVal, "LE" /*endianness*/ );
if (floatValue1 === undefined) floatValue1 = -1;
if (floatValue2 === undefined) floatValue2 = -1;
//element += row + "," + col + "," + floatValue1 + "," + floatValue2 + ","; // + ",{a:\"" + textVal + "\", b:\"0x" + hexVal + "\"},"; // band 0: raw; band 1: holes filled - to do
rowJson.push([row, col, floatValue1, floatValue2]);
}
//element = element.substr(0,element.length-1) + "],"; // remove final comma and add "]"
//rowStr += element;
}
//rowStr = rowStr.substr(0,rowStr.length-1) + "],"; // remove final comma and add "]"
//totalStr += rowStr;
totalJson.data.push(rowJson);
}
//totalStr = totalStr.substr(0,totalStr.length-1) + "]}"; // remove final comma and add "]}"
console.log("JSON result:", totalJson);
return totalJson; // debug
}
function readVicarParam(fileContents, paramName) {
StartPos = fileContents.indexOf(paramName);
EndPos = fileContents.indexOf(" ",StartPos);
valueLine = fileContents.substr(StartPos, EndPos - StartPos + 1);
lineArr = valueLine.split("=");
return lineArr[1];
}
function arrayToFloat(arr, SB) {
if (arr.toString() === [255, 255, 127, 127].toString()) {
return 0
}
try {
// Create a buffer
buf = new ArrayBuffer(4);
// Create a data view of it
view = new DataView(buf);
//console.log("VIEW PRIMA:", view);
// set bytes
//arr=Array.from(data);
arr.forEach(function (b, i) {
view.setUint8(i, b);
});
//console.log("VIEW DOPO:", view);
// Read the bits as a float; note that by doing this, we're implicitly
// converting it from a 32-bit float into JavaScript's native 64-bit double
// little-endian = true --> smallest position = least significant
// little-endian = false --> smallest position = most significant
if (SB === "BE") {
littleEndian = true;
} else {
littleEndian = false; // debug
}
num = view.getFloat32(0,littleEndian);
if (num == 0) {
return 0;
}
} catch (e) {
errorsArr.push(arr);
return arr;
}
if (Number.isNaN(num)) {
errorsArr.push(arr);
console.log("Number ", num ," extracted from array ", arr, ", is not a number. Hex: 0x" + arr[0].toString(16) + arr[1].toString(16) + arr[2].toString(16) + arr[3].toString(16) );
debugCounter++;
if (debugCounter>20) {
throw("uffa");
}
return 0;
}
return num;
}
function arrayToInt(data) {
// Create a buffer
buf = new ArrayBuffer(2);
// Create a data view of it
view = new DataView(buf);
num = data.charCodeAt(0) * 256 + data.charCodeAt(1) * 1; // debug test
/*
// set bytes
arr=Array.from(data);
arr.forEach(function (b, i) {
view.setUint8(i, b.charCodeAt());
});
// Read the bits as a float; note that by doing this, we're implicitly
// converting it from a 32-bit float into JavaScript's native 64-bit double
num = view.getInt16(0);
*/
// Done
if (num*1 === 0){
return ("0");
} else {
return ((num/1).toFixed(0));
}
}
function stringToArray(str) {
arr = [];
for (var i=0; i<str.length; i++) {
arr.push(str[i].charCodeAt())
}
console.log("STR ", str , "= ARR ", arr);
}
function testDebug() {
console.log(arrayToFloat(bandArray[15][32]));
}
function processData(/*objXYZ, objVisible, objNormals*/) {
console.log("Creating .PLY 3d file...");
objXYZ = fileDataXYZ;
objVisible = fileDataVisible;
objNormals = fileDataNormals;
objHeightMap = fileDataHeightMap;
console.log(objXYZ, objVisible, objNormals, objHeightMap);
// Setup XYZ data:
if (objXYZ) {
rows = objXYZ.data;
rowsNum = rows.length;
cols = rows[0];
colsNum = cols.length;
bands = cols[0];
bandsNum = bands.length;
}
// Setup Visible data if available:
if (objVisible) {
visRows = objVisible.data;
visRowsNum = visRows.length;
visCols = visRows[0];
visColsNum = visCols.length;
visBands = visCols[0];
visBandsNum = visBands.length;
}
// Setup Normals data if available:
if (objNormals) {
normRows = objNormals.data;
normRowsNum = normRows.length;
normCols = normRows[0];
normColsNum = normCols.length;
normBands = normCols[0];
normBandsNum = normBands.length;
}
// Setup Height Map data if available:
if (objHeightMap) {
heightBand = document.getElementById("txtBand").value * 1;
htRows = objHeightMap[heightBand];//.data; // debug: band 0 = raw, band 1 = filled holes
htRowsNum = htRows.length;
htCols = htRows[0];
htColsNum = htCols.length;
//htBands = htCols[0];
htBandsNum = bandsNum; // htBands.length; // debug, global var!
}
/* if (bandsNum) {*/
if (objXYZ) console.log("XYZ Rows x Columns =",rowsNum + "x" + colsNum + ", Bands: ", bandsNum);
if (objVisible) console.log("Vis Rows x Columns =",visRowsNum + "x" + visColsNum + ", Bands: ", visBandsNum);
if (objNormals) console.log("Norm Rows x Columns =",normRowsNum + "x" + normColsNum + ", Bands: ", normBandsNum);
if (objHeightMap) console.log("Norm Rows x Columns =",htRowsNum + "x" + htColsNum + ", Bands: ", htBandsNum);
PLY_HEADER = PLY_HEADER_BASE;
if (objVisible) {
PLY_HEADER += PLY_HEADER_VISIBLE;
}
if (objNormals) {
PLY_HEADER += PLY_HEADER_NORMALS;
}
PLY_HEADER += PLY_HEADER_END;
console.log("PLY_HEADER:",PLY_HEADER);
pixelsCount = 0;
if (objXYZ) {
rowsCount = 0;
finalText = "";
finalTextHeight = "";
rows.forEach( (row) => {
//console.log("RIGA ",rowsCount);
colsCount = 0;
rowElements = row
rowElements.forEach( (XYZelement) => {
//console.log("COLONNA ",colsCount);
if ( (XYZelement[0] != 0) || (XYZelement[1] != 0) || (XYZelement[2] != 0)) { // Debug / to do: values (0,0,0) mean "no value", but this can vary, it's specified in label
//console.log("" + XYZelement[0] + "," + XYZelement[1] + "," + XYZelement[2]);
XYZpart = XYZelement[0]*1 + " " + XYZelement[1]*1 + " " + XYZelement[2]*1;
finalText += XYZpart;
if (objVisible) {
visRow = visRows[rowsCount];
visElement = visRow[colsCount];
colorLevel = visElement[0];
visPart = colorLevel + " " + colorLevel + " " + colorLevel;// + " DEBUG " + "0x" + colorLevel.toString(6);
finalText += " " + visPart;
writeCanvas(colsCount, rowsCount, colorLevel, colorLevel, colorLevel);
}
if (objNormals) {
normRow = normRows[rowsCount];
normElement = normRow[colsCount];
UVpart = normElement[0] + " " + normElement[1] + " " + normElement[2];
finalText += " " + UVpart;
}
finalText +="\n"; // Final PLY line
pixelsCount++;
//console.log("Processed rows: " + (100*(rowsCount / rowsNum)).toFixed(0));
}
colsCount++;
});
rowsCount++;
});
}
/// check debug
// Line = Math.int((FileOffset - 4000) / 4000)
// col = (((FileOffset - 4000) / 4000 - Line) * 4000)/4
if (objHeightMap) {
FACTOR = document.getElementById("factor").value * 1.0;
rowsCount = 0;
finalText = "";
finalTextHeight = "";
console.log("Height data:", htRows);
htRows.forEach( (row) => {
//console.log("RIGA ",rowsCount);
colsCount = 0;
rowElements = row
rowElements.forEach( (HTelement) => {
//console.log("COLONNA ",colsCount);
HTpart = colsCount + " " + rowsCount + " " + (HTelement * FACTOR).toFixed(2);
finalText += HTpart;
finalText +="\n"; // Final PLY line
colorLevel= (HTelement * FACTOR).toFixed(2)*1;
writeCanvas(colsCount, rowsCount, colorLevel, colorLevel, colorLevel);
pixelsCount++;
colsCount++;
});
rowsCount++;
});
}
console.log("Processed pixels:",pixelsCount);
PLY_FINAL_HEADER = PLY_HEADER.replace("VVVVVV",pixelsCount)
output.textContent = "Ready for download...";
saveFile(PLY_FINAL_HEADER + finalText, "mytest.ply");
console.log("PLY creation completed.");
}
function resolve(a) {
console.log("OK",a);
}
function reject(a) {
console.log("ERROR",a);
}
$(document).ready(function () {
log = document.querySelector('.event-log');
canvas = document.getElementById('depthmap');
ctx = canvas.getContext('2d');
canvasWidth = canvas.width;
canvasHeight = canvas.height;
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
id = ctx.getImageData(0, 0, canvasWidth, canvasHeight);
pixels = id.data;
const fileSelectorXYZ = document.getElementById('inpFileXYZ');
fileSelectorXYZ.addEventListener('change', (event) => loadFile(event.target.files[0], "XYZ"));
const fileSelectorVisible = document.getElementById('inpFileVisible');
fileSelectorVisible.addEventListener('change', (event) => loadFile(event.target.files[0], "visible"));
const fileSelectorNormals = document.getElementById('inpFileNormals');
fileSelectorNormals.addEventListener('change', (event) => loadFile(event.target.files[0], "normals"));
const fileSelectorHeight = document.getElementById('inpFileHeight');
fileSelectorHeight.addEventListener('change', (event) => loadFile(event.target.files[0], "height"));
FACTOR = document.getElementById("factor").value * 1.0;
console.log("Ready.");
document.getElementById("status").innerHTML = "READY.";
$("#btnGo").click(function () {
var MY_URL_1=SERVER_URL + PAGE_URL;
var url_1 = PROXY + encodeURIComponent(MY_URL_1);
console.log("1");
httpRequest(PROXY, MY_URL_1,"GET","list");
console.log("2");
});
});
</script>
- <a href="https://an.rsl.wustl.edu/mera/merxbrowser/an3.aspx">Notebook</a><br>
- <a href="https://pds-imaging.jpl.nasa.gov/w10n/mer/spirit/mer2no_0xxx/data/sol1866/rdr/">JSON
converter</a><br>
- <a href="https://pds-imaging.jpl.nasa.gov/data/mer/spirit/mer2no_0xxx/data/sol1866/rdr/">Raw
download</a><br>
- <a href="http://lidarview.com/">Online PLY viewer</a><br>
<a href="https://gregstoll.com/~gregstoll/floattohex/">Hex to Float converter - 1 (LSB/MSB)</a><br>
<a href="https://memorystomp.com/floattohex/">Hex to Float converter - 2 </a><br>
<br>
<div>
<textarea id="output" cols="55" rows="15"></textarea>
</div><button id="btnGo">Go</button><br>
Status: <span id="status" name="status">-</span><br>
<br>
XYZ: <input type="file" id="inpFileXYZ" width="100"><br>
FFL/RAD: <input type="file" id="inpFileVisible"><br>
UV: <input type="file" id="inpFileNormals"><br>
<br>
.ht: <input type="file" id="inpFileHeight"> - Height x <input type=text" id="factor" name="factor" value="10"> - Band: <input type=text" id="txtBand" name="txtBand" value="0"><br>
<br>
<br>
<button id="btnStart" name="btnStart" onclick="startProcessing();">Start processing</button><br>
<textarea readonly class="event-log" cols="150" rows="3"></textarea><br>
<canvas id="depthmap" name="depthmap" width="1024" height="1024"></canvas><br>
</body>
</html>