-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstl-ascii-reader.js
More file actions
181 lines (145 loc) · 4.67 KB
/
stl-ascii-reader.js
File metadata and controls
181 lines (145 loc) · 4.67 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
;(function() {
var StlAsciiReader = (function() {
var StlAsciiReader = function(options) {
};
/* Number of lines per facet in the STL file */
var LINES_PER_FACET = 7;
/* Number of floating point values in a triangle */
var NUM_FLOATS_IN_TRI = 18;
/**
* Split line into words
*
* @param {String} line containing words
* @returns {Array} containing the words
*/
function splitLineIntoWords(line) {
var lineWords = line.trim().split(' ');
for (var i = lineWords.length-1; i >= 0; i--) {
if (lineWords[i].length === 0) {
lineWords.splice(i, 1);
}
}
return lineWords;
}
/**
* Read a vector
*
* @param {String} line containing the vector
* @param {Number} idx index of word from where to read
* @returns {Array} containing the vertex coordinates
*/
function readVector(line, idx) {
var lineWords = splitLineIntoWords(line);
var vector = [];
for (var i = 0; i< 3; i++) {
vector.push(parseFloat(lineWords[idx+i]));
}
return vector;
}
/**
* Read a single vertex
*
* @param {String} line containing the vertex
* @returns {Array} containing the vertex coordinates
*/
function readVertex(line) {
return readVector(line, 1);
}
/**
* Read a single normal
*
* @param {String} line containing the normal
* @returns {Array} contaning the normal coordinates
*/
function readNormal(line) {
return readVector(line, 2);
}
/**
* Read a single facet
*
* @param {Array} lines of the STL file
* @param {Number} idx the index at which the facet starts
* @returns {Object} an object with the normal and the verts
*/
function readFacet(lines, idx) {
var facet = {};
facet.normal = readNormal(lines[idx]);
facet.verts = [];
facet.verts.push(readVertex(lines[idx+2]));
facet.verts.push(readVertex(lines[idx+3]));
facet.verts.push(readVertex(lines[idx+4]));
return facet;
}
/**
* Converts the facets into a Float32Array with interleaved vertex and
* normal data.
*
* @param {Object} facet the facet object with the vertices and normal
* @param {Float32Array} vn the interleaved vertex normal data to be populated
* @param {Float32Array} v the vertex data to be populated
* @param {Float32Array} n the normal data to be populated
* @param {Number} idx the index at which to insert the facet data
*/
function pushFacetIntoFloat32Array(facet, vn, v, n, idx) {
var normal = facet.normal;
for (var j = 0; j < 3; j++) {
for (var k = 0; k < 3; k++) {
vn[idx+j*6+k] = facet.verts[j][k];
v[idx/2+j*3+k] = facet.verts[j][k];
}
// copy the normal after each vertex
for (k = 0; k < 3; k++) {
vn[idx+j*6+3+k] = facet.normal[k];
n[idx/2+j*3+k] = facet.normal[k];
}
}
}
/**
* Read the STL solid
*
* @param {Array} lines the lines of the STL file
* @param {Float32Array} vn the interleaved vertex normal data to be populated
* @param {Float32Array} v the vertex data to be populated
* @param {Float32Array} n the normal data to be populated
*/
function readSolid(lines, vn, v, n) {
var facetCount = 0;
for (var i = 0; i< lines.length; i++) {
var lineWords = lines[i].trim().split(' ');
if (lineWords[0] == 'facet') {
var facet = readFacet(lines, i);
pushFacetIntoFloat32Array(facet, vn, v, n, facetCount*NUM_FLOATS_IN_TRI);
facetCount += 1;
// skip to the next facet
i += 6;
}
}
}
/**
* Reads the triangle vertices of an ASCII STL file into a Float32Array
*
* @param {string} fileData The file data as a string
* @return {Float23Array} contains interleaved vertex normal data
*/
StlAsciiReader.prototype.read = function(fileData) {
var lines = fileData.split('\n');
var numTriangles = parseInt(lines.length/LINES_PER_FACET);
var vn = new Float32Array(numTriangles*NUM_FLOATS_IN_TRI);
var v = new Float32Array(numTriangles*NUM_FLOATS_IN_TRI/2);
var n = new Float32Array(numTriangles*NUM_FLOATS_IN_TRI/2);
readSolid(lines, vn, v, n);
return {
vn: vn,
vertices: v,
normals: n
};
};
return StlAsciiReader;
})();
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = StlAsciiReader;
}
if (typeof window !== 'undefined') {
window.StlAsciiReader = StlAsciiReader;
}
})();