forked from asalga/XB-PointStream
/
psi.js
881 lines (727 loc) · 28.3 KB
/
psi.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
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
/*
Copyright (c) 2010 Seneca College
MIT LICENSE
*/
/**
@class This parser parses .PSI filetypes. These files are Arius3D
proprietary files which have their data stored in one of the following
ways:
<pre>
<xml tags>
<that have relevant>
<information= about the file>
Binary Data...
(3 bytes for x, 3 bytes for y, 3 bytes for z and 3 bytes for rgb)
...
...
...
...
location and color data end for points
normal data start
(every 3 bytes is compressed normal data)
...
...
...
<more tags>
<to close opening tags>
<and provide more information>
</pre>
@version: 0.7
@author: Mickael Medel
asydik.wordpress.com
Created: February 2011
Updated: April 2011
Maintained by: Andor Salga
*/
var PSIParser = (function() {
/**
@private
*/
function PSIParser(config) {
// Declare tags
var bgnDocument = "<PsDocument>",
endDocument = "</PsDocument>",
bgnComposite = "<PsComposite>",
endComposite = "</PsComposite>",
bgnSubject = "<PsSubject",
endSubject = "</PsSubject>",
bgnInstance = "<PsInstance>",
endInstance = "</PsInstance>",
bgnEnv = "<PsEnvironment>",
endEnv = "</PsEnvironment>",
bgnName = "<Name>",
endName = "</Name>",
intMatIdStr = "<Identity>", // Internal Matrix Identity
extMatIdStr = "<ExtIdentity>"; // External Matrix Identity
var bgnCompList = "<PsList>",
endCompList = "</PsList>",
compositeModelStr = "CompositeModel:",
instanceModelStr = "InstanceModel:",
nurbSurfaceStr = "NurbSurface",
nurbCurveStr = "NurbCurve",
lineModelStr = "LineModel",
quadMeshStr = "QuadMeshModel",
triMeshStr = "TriMeshModel",
cloudModelStr = "CloudModel",
imgCloudModelStr = "ImageCloudModel",
txtObjStr = "TextObject",
imgObjStr = "ImageObject",
soundObjStr = "SoundObject",
octTreeModelStr = "OctTreeModel",
openBracket = "<",
closeBracket = ">",
slashEndMark = "/",
psPrefix = "Ps";
// Shuffle Config Variable Strings
var bgnShuffle = "<Shuffle>",
endShuffle = "<\\Shuffle>",
bgnTempLight = "<PsTempLight>",
endTempLight = "<\\PsTempLight>",
bgnTempLightVec = "<PsTempLightVec>",
endTempLightVec = "<\\PsTempLightVec>",
bgnGlblAmbLight = "<PsGlobalAmbientLight>",
endGlblAmbLight = "<\\PsGlobalAmbientLight>",
bgnLightType = "<PsLightType>",
endLightType = "<\\PsLightType>";
// Environment Variable Strings - <PsEnvironment>
var posDataStr = "XYZData=",
colDataStr = "RGBData=",
normDataStr = "IJKData=",
fileTypeStr = "FileType=",
compStr = "Compression=",
encStr = "Encryption=",
wtrMrkStr = "WaterMark=",
lenUnitStr = "LengthUnit=";
// View Variable Strings
var bgnViewDef = "<PointStream_View_Definition>",
endViewDef = "</PointStream_View_Definition>",
quartStr = "<Q", // Quarternion Matrix
pivotStr = "<P", // Pivot
transStr = "<T", // Translation
angStr = "<A", // FOV Angle
scrnSizeStr = "<S", // Screen Size
bgColStr = "<B", // Background Color
cv1Str = "<Cv1";
// Material Variable Strings
var bgnTemp1Mat = "<PsTemp1Material>",
endTemp1Mat = "<\\PsTemp1Material>";
// Parent Variable Strings
var bgnParentTag = "<PsParent= '";
endParentTag = "'>";
// PsSubject Variables Strings
var selStr = "Sel=",
visStr = "Vis=",
lockStr = "Lok=",
actStr = "Act=";
// Token Model Variable Strings
var bgnLineModel = "<PsLineModel>",
bgnCloudModel = "<PsCloudModel>",
bgnImgCldModel = "<PsImageCloudModel>",
bgnTriMeshModel = "<PsTriMeshModel>",
bgnNurbCurve = "<PsNurbCurve>",
bgnNurbSurface = "<PsNurbSurface>",
bgnTextObject = "<PsTextObject>",
bgnImageObject = "<PsImageObject>",
bgnSoundObject = "<PsSoundObject>",
bgnOctTreeModel = "<PsOctTreeModel>";
// Level of Detail Variable Strings
var numLvlStr = "<NumLevels=",
scnMatStr = "<ScanMatrix:",
bgnLvlStr = "<Level=",
endLvlStr = "</Level=",
binCloudStr = "<BinaryCloud>",
ascCloudStr = "<AsciiCloud>",
fmtStr = "<Format=",
formatTag = "<Format=";
var numPtStr = "<NumPoints=",
sptSzStr = "<SpotSize=",
posMinStr = "<Min=",
posMaxStr = "<Max=",
endXMLStr = ">";
var undef;
var __empty_func = function(){};
var start = config.start || __empty_func;
var parse = config.parse || __empty_func;
var end = config.end || __empty_func;
var version = "0.6";
const UNKNOWN = -1;
const XHR_DONE = 4;
const STARTED = 1;
var pathToFile = null;
var fileSize = 0;
//
var numParsedPoints = 0;
var numTotalPoints = 0;
var progress = 0;
//
var numValuesPerLine = -1;
var normalsPresent = false;
var colorsPresent = true;
var layoutCode = UNKNOWN;
// Length of the arrays we'll be sending the library.
var BUFFER_SIZE = 30000;
//
var tempBufferV;
var tempBufferOffsetV = 0;
var tempBufferC;
var tempBufferOffsetC = 0;
var tempBufferN;
var tempBufferOffsetN = 0;
//
var parsedVerts = [];
var parsedCols = [];
var parsedNorms = [];
var firstRun = true;
var normFlag = false;
// values to be used in decompression of PSI
var diffX, diffY, diffZ;
var scaleX, scaleY, scaleZ;
const SFACTOR = Math.pow(2, 24);
const NFACTOR = -0.5 + Math.pow(2, 10);
//
var bgnTag = "";
var endTag = "";
var tagExists;
var endTagExists;
// keep track if onprogress event handler was called to
// handle Chrome/WebKit vs. Firefox differences.
//
// Firefox will call onprogress zero or many times
// Chrome/WebKit will call onprogress one or many times
var onProgressCalled = false;
var AJAX = null;
/**
@private
Functions to deal with specific bytes in the stream
@returns normalized value of byte
*/
var getByteAt = function(str, iOffset){
return str.charCodeAt(iOffset) & 0xFF;
};
/**
@private
@param {} str
@param {} iOffset
@returns
*/
var getXYZ = function(str, iOffset){
return (((getByteAt(str, iOffset + 2) << 8) + getByteAt(str, iOffset + 1)) << 8) + getByteAt(str, iOffset);
};
/**
@private
@param {} str
@param {} iOffset
@returns
*/
var getRGB = function(str, iOffset){
return getByteAt(str, iOffset);
};
/**
@private
This function takes in a variable length array and chops it into
equal sized parts since the library requires the array of attributes
to be of equal size.
Any excess values which don't entirely fit into the buffers created will
be returned along with their length so the next iteration can fill them
up from there.
@param {} arr
@param {} tempBuffer
@param {} tempBufferOffset
@param {} Which attribute are we sending in? 1 = vertex, 2 = color, 3 = normal
@returns {Object}
*/
var partitionArray = function(arr, tempBuffer, tempBufferOffset, AttribID){
// If we don't have enough for one buffer, just add it and wait for the next call.
if(arr.length + tempBufferOffset < BUFFER_SIZE){
// if this is the start of a new buffer
if(!tempBuffer){
tempBuffer = new Float32Array(BUFFER_SIZE);
tempBuffer.set(arr);
}
// If the buffer already exists, we're going to be adding to it. Don't worry about
// over filling the buffer since we already know at this point that won't happen.
else{
tempBuffer.set(arr, tempBufferOffset);
}
tempBufferOffset += arr.length;
}
// If what we have in the temp buffer and what we just parsed is too large for one buffer
else if(arr.length + tempBufferOffset >= BUFFER_SIZE){
// if temp buffer offset is zero, Find out how many buffers we can fill up with this set of vertices
var counter = 0;
var numBuffersToFill = parseInt(arr.length/BUFFER_SIZE);
// If there is something already in the buffer, fill up the rest.
if(tempBufferOffset > 0){
// Add the vertices from the last offset to however much we need to fill the temp buffer.
var amtToFill = BUFFER_SIZE - tempBufferOffset;
tempBuffer.set(arr.subarray(0, amtToFill), tempBufferOffset);
switch(AttribID){
case 1: numParsedPoints += BUFFER_SIZE/3;
parse(AJAX.parser, {"ps_Vertex": tempBuffer});break;
case 2: parse(AJAX.parser, {"ps_Color": tempBuffer});break;
case 3: parse(AJAX.parser, {"ps_Normal": tempBuffer});break;
}
// now find out how many other buffers we can fill
numBuffersToFill = parseInt((arr.length - amtToFill)/BUFFER_SIZE);
counter = amtToFill;
}
// Create and send as many buffers as we can with
// this chunk of data.
for(var buffIter = 0; buffIter < numBuffersToFill; buffIter++){
var buffer = new Float32Array(BUFFER_SIZE);
buffer.set(arr.subarray(counter, counter + BUFFER_SIZE));
switch(AttribID){
case 1: numParsedPoints += BUFFER_SIZE/3;
parse(AJAX.parser, {"ps_Vertex": buffer});break;
case 2: parse(AJAX.parser, {"ps_Color": buffer});break;
case 3: parse(AJAX.parser, {"ps_Normal": buffer});break;
}
counter += BUFFER_SIZE;
}
// put the end of the attributes in the first part of the temp buffer
tempBuffer = new Float32Array(BUFFER_SIZE);
tempBuffer.set(arr.subarray(counter, counter + arr.length));
tempBufferOffset = arr.length - counter;
}
// return the changes
return {
buffer: tempBuffer,
offset: tempBufferOffset
};
}
/**
Returns the version of this parser
@name PSIParser#version
@returns {String} parser version
*/
this.__defineGetter__("version", function(){
return version;
});
/**
Get the number of parsed points so far
@name PSIParser#numParsedPoints
@returns {Number} number of points parsed.
*/
this.__defineGetter__("numParsedPoints", function(){
return numParsedPoints;
});
/**
Get the total number of points in the point cloud.
@name PSIParser#numTotalPoints
@returns {Number}
*/
this.__defineGetter__("numTotalPoints", function(){
return numTotalPoints;
});
/**
Returns the progress of downloading the point cloud
@name PSIParser#progress
@returns {Number} value from zero to one or -1 if unknown.
*/
this.__defineGetter__("progress", function(){
return progress;
});
/**
Returns the file size of the resource in bytes.
@name PSIParser#fileSize
@returns {Number} size of resource in bytes.
*/
this.__defineGetter__("fileSize", function(){
return fileSize;
});
/**
Stop downloading and parsing the associated point cloud.
*/
this.stop = function(){
if(AJAX){
AJAX.abort();
}
};
/**
@param {String} pathToFile
*/
this.load = function(path){
pathToFile = path;
AJAX = new XMLHttpRequest();
AJAX.startOfNextChunk = 0;
AJAX.last12Index = 0;
// put a reference to the parser in the AJAX object
// so we can give the library a reference to the
// parser within the AJAX event handler scope.
// !! eventually need to fix this
AJAX.parser = this;
/**
@private
Occurs exactly once when the resource begins to be downloaded.
*/
AJAX.onloadstart = function(evt){
start(AJAX.parser);
};
/*
*/
AJAX.parseVertsCols = function(chunk, numBytes, byteIdx, verts, cols){
for(var point = 0; point < numBytes/12; byteIdx += 12, point++){
verts[point*3 + 0] = (diffX * getXYZ(chunk, byteIdx )) / scaleX;
verts[point*3 + 1] = (diffY * getXYZ(chunk, byteIdx + 3)) / scaleY;
verts[point*3 + 2] = (diffZ * getXYZ(chunk, byteIdx + 6)) / scaleZ;
cols[point*3 + 0] = getRGB(chunk, byteIdx + 9 ) / 255;
cols[point*3 + 1] = getRGB(chunk, byteIdx + 10) / 255;
cols[point*3 + 2] = getRGB(chunk, byteIdx + 11) / 255;
}
};
/*
*/
AJAX.parseNorms = function(chunk, numBytes, byteIdx, norms){
var nzsign, nx11bits, ny11bits, ivalue;
var nvec = new Float32Array(3);
// Start reading the normals where we left off reading the
// vertex positions and colors.
// Each normal is 3 bytes.
for(var point = 0; byteIdx < numBytes; byteIdx += 3, point += 3){
ivalue = getXYZ(chunk, byteIdx);
nzsign = ((ivalue >> 22) & 0x0001);
nx11bits = ((ivalue) & 0x07ff);
ny11bits = ((ivalue >> 11) & 0x07ff);
if(nx11bits >= 0 && nx11bits < 2048){
if(ny11bits >= 0 && ny11bits < 2048){
nvec[0] = (nx11bits/NFACTOR) - 1.0;
nvec[1] = (ny11bits/NFACTOR) - 1.0;
var nxnymag = (nvec[0]*nvec[0] + nvec[1]*nvec[1]);
// clamp values
if (nxnymag > 1){ nxnymag = 1; }
if (nxnymag < -1){ nxnymag = -1; }
nxnymag = 1 - nxnymag;
if (nxnymag > 1){ nxnymag = 1; }
if (nxnymag < -1){ nxnymag = -1; }
nvec[2] = Math.sqrt(nxnymag);
if (nzsign){
nvec[2] = -nvec[2];
}
var dNorm = (nvec[0]*nvec[0] + nvec[1]*nvec[1] + nvec[2]*nvec[2]);
dNorm = (dNorm > 0) ? Math.sqrt(dNorm) : 1;
norms[point] = nvec[0]/dNorm;
norms[point+1] = nvec[1]/dNorm;
norms[point+2] = nvec[2]/dNorm;
}
}
}
};
/*
Occurs exactly once, when the file is done being downloaded.
Firefox/Minefield sometimes skips calling onprogress and
jumps to onload.
@param {} evt
*/
AJAX.onload = function(evt){
var textData = AJAX.responseText;
var chunkLength = textData.length;
// If we downloaded the file in one request.
if(firstRun){
// First, read in the important bounding box data.
AJAX.firstLoad(textData);
// first get the number of points in the cloud
// <NumPoints= 11158 2 0 11158 0 0 >\r\n<Spot
// <NumPoints= 11158 0 >
// Find the start and end tags for NumPoints
var numPointsTagIndex = textData.indexOf(numPtStr);
var numPointsTagEndIndex = textData.indexOf(">", numPointsTagIndex+1);
var numPointsContents = textData.substring(numPointsTagIndex, numPointsTagEndIndex+1);
// Set the parser's attribute
// Multiply by 1 to convert to a Number type
// ["<NumPoints=", "11158", "0", ">"]
numParsedPoints = numTotalPoints = numPointsContents.split(" ")[1] * 1;
var hasNormals = numPointsContents.split(" ")[2] * 1 == 0 ? false : true;
// Read the position and color data
// <Level=0>
// <BinaryCloud>
// <Format=1>
// <NumPoints= 11158 0 >
// <SpotSize= 0.134696 >
// <Min= -24.1075 -28.9434 -16.8786 >
// <Max= -12.4364 -14.8525 -18.72375 >
// ...\
// ... }- binary data (vertices & colors)
// .../
// ...\
// ... }- possibly more binary data (normals)
// .../
// </Level=0>
// </PsCloudModel>
// Checks if begin or end tags can be found using regex.
var tagExists = textData.indexOf(bgnTag);
var infoEnd = textData.indexOf(endLvlStr);
var infoStart;
// If the bgnTag exists then set the startOfNextChunk
// to the end of the bgnTag + 2 for offset values.
if(tagExists !== -1){
// +2 for offset values
tagLen = bgnTag.length + 2;
infoStart = tagExists + tagLen;
}
// This contains our raw binary data.
binData = textData.substring(infoStart, infoEnd);
var numBytes = binData.length;
var verts = new Float32Array(numTotalPoints * 3);
var cols = new Float32Array(numTotalPoints * 3);
AJAX.parseVertsCols(binData, numBytes, 0, verts, cols);
// Parse the normals if we have them.
var norms;
if(hasNormals){
norms = new Float32Array(numTotalPoints * 3);
AJAX.parseNorms(binData, numBytes, numTotalPoints * 12, norms);
}
var attributes = {};
if(verts){attributes["ps_Vertex"] = verts;}
if(cols){ attributes["ps_Color"] = cols;}
if(norms){attributes["ps_Normal"] = norms;}
// Indicate parsing is done. Ranges from 0 to 1.
progress = 1;
parse(AJAX.parser, attributes);
end(AJAX.parser);
return;
}
// If we didn't get the entire file in one request, continue on...
var infoEnd = textData.indexOf(endLvlStr);
var chunk = textData.substring(AJAX.startOfNextChunk, infoEnd);
// If the file has normals as indicated at the start of the file.
if(normFlag){
normalsPresent = true;
colorsPresent = false;
}
AJAX.parseChunk(chunk);
// Get the last remaining bits from the temp buffers
// and parse those too.
if(tempBufferV && tempBufferOffsetV > 0){
// Only send the data if there's actually something to send.
var lastBufferV = tempBufferV.subarray(0, tempBufferOffsetV);
numParsedPoints += tempBufferOffsetV/3;
parse(AJAX.parser, {"ps_Vertex": lastBufferV});
}
if(tempBufferC && tempBufferOffsetC > 0){
// Only send the data if there's actually something to send.
var lastBufferC = tempBufferC.subarray(0, tempBufferOffsetC);
parse(AJAX.parser, {"ps_Color": lastBufferC});
}
if(tempBufferN && tempBufferOffsetN > 0){
// Only send the data if there's actually something to send.
var lastBufferN = tempBufferN.subarray(0, tempBufferOffsetN);
parse(AJAX.parser, {"ps_Normal": lastBufferN});
}
progress = 1;
end(AJAX.parser);
}
/**
@private
*/
AJAX.parseChunk = function(chunk){
// !! Fix this.
// This occurs over network connections, but not locally.
if(chunk){
var numVerts = chunk.length/12;
var numBytes = chunk.length;
//
var verts, cols, norms;
// !!! COMMENT
if(onProgressCalled === true){
// !!! this needs to be changed.
// if colors are present, we know we're still
// dealing with vertices.
if(numVerts > 0 && colorsPresent){
// !!! only for debugging, remove on prduction
if(numVerts !== Math.floor(numVerts)){
numVerts = Math.floor(numVerts);
console.log("invalid numVerts: " + numVerts);
}
verts = new Float32Array(numVerts * 3);
cols = new Float32Array(numVerts * 3);
}
// parsing normal values, not sure the logic behind it (as it was never provided)
// we take 3 bytes and apply some bit shifting operations on it
// we then take the results and multiply it to some set values
// the normals are the resulting values
if(numVerts > 0 && normalsPresent){
if(numBytes !== Math.floor(numBytes)){
console.log('invalid num bytes');
}
norms = new Float32Array(numBytes);
AJAX.parseNorms(chunk, numBytes, 0, norms);
}
// parsing xyz and rgb values, not sure behind the logic either
// 3 bytes are used for each x, y, z values
// each of the last 3 bytes of the 12 correspond to an rgb value
else{
var byteIdx = 0;
AJAX.parseVertsCols(chunk, numBytes, byteIdx, verts, cols);
}
}
if(verts){
var o = partitionArray(verts, tempBufferV, tempBufferOffsetV, 1);
tempBufferV = o.buffer;
tempBufferOffsetV = o.offset;
}
if(cols){
var o = partitionArray(cols, tempBufferC, tempBufferOffsetC, 2);
tempBufferC = o.buffer;
tempBufferOffsetC = o.offset;
}
if(norms){
var o = partitionArray(norms, tempBufferN, tempBufferOffsetN, 3);
tempBufferN = o.buffer;
tempBufferOffsetN = o.offset;
}
}
};
/**
@private
*/
AJAX.firstLoad = function(textData){
var temp;
var xMax, xMin, yMax;
var yMin, zMax, zMin;
// numPtStr - number of points in the file
tagExists = textData.indexOf(numPtStr);
if(tagExists !== -1){
endTagExists = textData.indexOf(endXMLStr, tagExists);
temp = textData.substring((tagExists + numPtStr.length), endTagExists);
var numPtArr = temp.split(" ");
// Multiply by 1 to convert to a Number type.
numTotalPoints = numPtArr[1] * 1;
// We can find out if there are normals by inspecting <NumPoints>
// <NumPoints= 6 1 >
// <NumPoints= 6 2 >
// If the second value is 0, the file does not contain normals.
if((numPtArr[2] * 1) !== 0){
normFlag = true;
}
}
// posMinStr - lowest value in the file (used for decompression)
tagExists = textData.indexOf(posMinStr);
if(tagExists !== -1){
endTagExists = textData.indexOf(endXMLStr, tagExists);
temp = textData.substring((tagExists + posMinStr.length), endTagExists);
var posMinArr = temp.split(" ");
// Multiply by 1 to convert to a Number type.
xMin = posMinArr[1] * 1;
yMin = posMinArr[2] * 1;
zMin = posMinArr[3] * 1;
}
// posMaxStr - highest value in the file (used for decompression)
tagExists = textData.indexOf(posMaxStr);
if(tagExists !== -1){
endTagExists = textData.indexOf(endXMLStr, tagExists);
temp = textData.substring((tagExists + posMaxStr.length), endTagExists);
var posMaxArr = temp.split(" ");
// Multiply by 1 to convert to a Number type.
xMax = posMaxArr[1] * 1;
yMax = posMaxArr[2] * 1;
zMax = posMaxArr[3] * 1;
bgnTag = textData.substring(tagExists, (endTagExists + 1));
diffX = xMax - xMin;
diffY = yMax - yMin;
diffZ = zMax - zMin;
scaleX = SFACTOR + xMin;
scaleY = SFACTOR + yMin;
scaleZ = SFACTOR + zMin;
// If we got this far, we can start parsing values and we don't
// have to try running this function again.
firstRun = false;
}
// There is a chance that in the first XHR request we didn't get
// as far as the <Max> tag. Since <Min> and <Max> are necessary to
// read before parsing the file, force the PSI parser to wait for
// the next request and try again.
else{
firstRun = true;
}
}
/**
@private
On Firefox/Minefield, this will occur zero or many times
On Chrome/WebKit this will occur one or many times
*/
AJAX.onprogress = function(evt){
//
onProgressCalled = true;
// Update the file's progress.
if(evt.lengthComputable){
fileSize = evt.total;
progress = evt.loaded/evt.total;
}
// If we have something to actually parse.
if(AJAX.responseText){
var textData = AJAX.responseText;
var chunkLength = textData.length;
// If this is the first call to onprogress.
if(firstRun){
AJAX.firstLoad(textData);
// firstLoad() will have attempted to read at least the <Min> and <Max>
// tags, if we aren't that far in the file, we'll need to try again
if(firstRun){
return;
}
}
// Try to find the <Level> and </Level=0> tags which means we would
// have all the data.
endTag = endLvlStr;
tagExists = textData.indexOf(bgnTag);
var infoEnd = textData.indexOf(endTag);
var infoStart;
// If the bgnTag exists then set the startOfNextChunk
// to the end of the bgnTag + 2 for offset values.
if(tagExists !== -1){
// +2 for offset values
tagLen = bgnTag.length + 2;
infoStart = tagExists + tagLen;
if(AJAX.startOfNextChunk === 0){
AJAX.startOfNextChunk = infoStart;
}
}
// Find the last multiple of 12 in the chunk
// this is because of the format shown at the top of this parser.
var last12 = Math.floor((chunkLength - infoStart) / 12);
AJAX.last12Index = (last12 * 12) + infoStart;
// If the end tag was found.
if(infoEnd !== -1){
AJAX.last12Index = infoEnd;
}
var totalPointsInBytes = (numTotalPoints * 12) + infoStart;
// Handles parsing up to the end of position and colors.
// Sets the next chunk at the start of normals.
if((totalPointsInBytes > AJAX.startOfNextChunk) && (totalPointsInBytes < AJAX.last12Index)){
var chunk = textData.substring(AJAX.startOfNextChunk, totalPointsInBytes);
if(chunk.length > 0){
AJAX.startOfNextChunk = totalPointsInBytes;
AJAX.parseChunk(chunk);
}
}
// Parse the normals.
else if((AJAX.last12Index > totalPointsInBytes) && (AJAX.startOfNextChunk >= totalPointsInBytes)){
var chunk = textData.substring(AJAX.startOfNextChunk, AJAX.last12Index);
normalsPresent = true;
colorsPresent = false;
if(chunk.length > 0){
AJAX.startOfNextChunk = AJAX.last12Index;
AJAX.parseChunk(chunk);
}
}
// Parse position and colors.
else{
var chunk = textData.substring(AJAX.startOfNextChunk, AJAX.last12Index);
if(chunk.length > 0){
AJAX.startOfNextChunk = AJAX.last12Index;
AJAX.parseChunk(chunk);
}
}
}// AJAX.responseText
};// onprogress
// This line is required since we are parsing binary data.
if(AJAX.overrideMimeType){
AJAX.overrideMimeType('text/plain; charset=x-user-defined');
}
// open an asynchronous request to the path
AJAX.open("GET", path, true);
AJAX.send(null);
};// load
}// ctor
return PSIParser;
}());