-
Notifications
You must be signed in to change notification settings - Fork 0
/
ply.js
103 lines (94 loc) · 3.08 KB
/
ply.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
function point3D(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
}
function Model(vertices, vertexNormals, faces)
{
this.vertices = vertices;
this.normals = vertexNormals;
this.faces = faces;
}
function ply_reader(data) {
var hasNormal = false;
// Read header
while(data.length)
{
var retval = data.match(/.*/);
var str = retval[0];
data = data.substr(str.length+1);
var retval = str.match(/element (\w+) (\d+)/);
if(retval)
{
if(retval[1] == "vertex")
var npoints = parseInt(retval[2]);
if(retval[1] == "face")
var npolys = parseInt(retval[2]);
}
if(str == "property float nx")
hasNormal = true;
if(str == "end_header")
break;
}
// Read points
var minPoint = new point3D(Infinity, Infinity, Infinity);
var maxPoint = new point3D(-Infinity, -Infinity, -Infinity);
var vertices = [];
var vertexNormals = [];
for (var i = 0; i < npoints; i++)
{
var retval = data.match(/([\d.-]+ ?)+/);
var str = retval[0];
data = data.substr(str.length+1);
var retval = str.match(/([\d.-]+)/g);
var point = new point3D(parseFloat(retval[0]), parseFloat(retval[1]), parseFloat(retval[2]));
vertices.push(point.x, point.y, point.z);
if(hasNormal)
vertexNormals.push(parseFloat(retval[3]), parseFloat(retval[4]), parseFloat(retval[5]));
minPoint.x = Math.min(minPoint.x, point.x);
minPoint.y = Math.min(minPoint.y, point.y);
minPoint.z = Math.min(minPoint.z, point.z);
maxPoint.x = Math.max(maxPoint.x, point.x);
maxPoint.y = Math.max(maxPoint.y, point.y);
maxPoint.z = Math.max(maxPoint.z, point.z);
}
// Polygons
var polys = [];
var index = 0;
for (var i = 0; i < npolys; i++)
{
var retval = data.match(/(\d+ ?)+/);
var str = retval[0];
data = data.substr(str.length+1);
var retval = str.match(/(\d+)/g);
var nvertex = parseInt(retval[0]);
var aIndex = parseInt(retval[1]);
var bIndex = parseInt(retval[2]);
var cIndex = parseInt(retval[3]);
if(nvertex == 3 || nvertex == 4)
{
polys.push(aIndex, bIndex, cIndex);
}
if(nvertex == 4)
{
var dIndex = parseInt(retval[4]);
polys.push(aIndex, cIndex, dIndex);
}
}
// Move to center of object
var centerMove = new point3D(-(maxPoint.x + minPoint.x)/2, -(maxPoint.y + minPoint.y)/2, -(maxPoint.z + minPoint.z)/2);
var scaleY = (maxPoint.y - minPoint.y) / 2;
for (var i = 0; i < vertices.length; i+=3)
{
vertices[i+0] += centerMove.x;
vertices[i+1] += centerMove.y;
vertices[i+2] += centerMove.z;
vertices[i+0] /= scaleY;
vertices[i+1] /= scaleY;
vertices[i+2] /= scaleY;
}
// Create model
model = new Model(vertices, vertexNormals, polys);
// Return moving to center model
return model;
}