-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVICAR-utils.js
640 lines (540 loc) · 23.4 KB
/
VICAR-utils.js
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
// WARNING! - Experimental functions, bery buggish
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 loadFileAsBinaryString(fileHandler) {
const reader = new FileReader();
reader.addEventListener('load', (event) => {
rawTextureContents = event.target.result;
document.getElementById("texture_file_status").innerHTML = "Texture file loaded.";
document.getElementById("texture_file_length").innerHTML = rawTextureContents.length;
});
reader.readAsBinaryString(fileHandler);
}
function readVicarHeaderData(fileContents) {
endianness = "BE";
//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 [length] 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;
if (vicarLabelLengthLineStartPos >= 0) {
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);
} else {
vicarLabelLength = 0;
document.getElementById("vicar_header_status").innerHTML = "Unsupported";
document.getElementById("vicar_header_data").innerHTML = "No PDS label, no VICAR label";
magicNumber = fileContents.substr(0,2);
if((magicNumber[0].charCodeAt(0) === 1) && (magicNumber[1].charCodeAt(0) === 218)) { // 01 DA
console.log("This is a Silicon Graphis RGB file");
// https://en.wikipedia.org/wiki/Silicon_Graphics_Image
document.getElementById("vicar_header_data").innerHTML = "SGI RGB texture not supported";
RGBcompression = fileContents.substr(2,1).charCodeAt(0);
RGBbytesPerChannel = fileContents.substr(3,1).charCodeAt(0);
RGBbandsNum = fileContents.substr(4,1).charCodeAt(0) * 256 + fileContents.substr(4,1).charCodeAt(0) * 1;
RGBsizeX = fileContents.substr(6,1).charCodeAt(0) * 256 + fileContents.substr(7,1).charCodeAt(0) * 1;
RGBsizeY = fileContents.substr(8,1).charCodeAt(0) * 256 + fileContents.substr(9,1).charCodeAt(0) * 1;
document.getElementById("vicar_header_data").innerHTML += "<br>" +
"RGBcompression=" + RGBcompression + "<br>" +
"RGBbytesPerChannel=" + RGBbytesPerChannel + "<br>" +
"RGBbandsNum=" + RGBbandsNum + "<br>" +
"RGBsizeX=" + RGBsizeX + "<br>" +
"RGBsizeY=" + RGBsizeY;
} else {
console.log("Unknown magic number ", magicNumber[0].charCodeAt(0).toString(16), magicNumber[1].charCodeAt(0).toString(16));
}
return -1;
}
totalLabelLength = PDSlabelLength + vicarLabelLength;
onlyData = fileContents.substr(totalLabelLength , fileContents.length - totalLabelLength + 1);
console.log("onlyData length=", onlyData.length, onlyData.length/vicarBands);
///// 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 (vicarLabelLength>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 ;
try {
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("done");
console.log("imageLines=" + imageLines + "<br>" +
"imageSamplesPerLine=" + imageSamplesPerLine + "<br>" +
"bandsNum=" + bandsNum + "<br>" +
"bandLength=" + bandLength + "<br>" +
"imageLength=" + imageLength + "<br>");
} catch (e) {
console.log("File error");
console.log("Wrong format?");
}
}
function loadVICAR_BSQ(fileContents, band) {
console.log("Processing bands...");
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);
console.log(" File data length=", fileContents.length);
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(sample, 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
}
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 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.");
}