Debian Codegraph @

index.js

raw symbols SCIP
1 /**
2 * @author aleeper / http://adamleeper.com/
3 * @author mrdoob / http://mrdoob.com/
4 * @author gero3 / https://github.com/gero3
5 * @author Mugen87 / https://github.com/Mugen87
6 *
7 * Description: A THREE loader for STL ASCII files, as created by Solidworks and other CAD programs.
8 *
9 * Supports both binary and ASCII encoded files, with automatic detection of type.
10 *
11 * The loader returns a non-indexed buffer geometry.
12 *
13 * Limitations:
14 * Binary decoding supports "Magics" color format (http://en.wikipedia.org/wiki/STL_(file_format)#Color_in_binary_STL).
15 * There is perhaps some question as to how valid it is to always assume little-endian-ness.
16 * ASCII decoding assumes file is UTF-8.
17 *
18 * Usage:
19 * var loader = new THREE.STLLoader();
20 * loader.load( './models/stl/slotted_disk.stl', function ( geometry ) {
21 * scene.add( new THREE.Mesh( geometry ) );
22 * });
23 *
24 * For binary STLs geometry might contain colors for vertices. To use it:
25 * // use the same code to load STL as above
26 * if (geometry.hasColors) {
27 * material = new THREE.MeshPhongMaterial({ opacity: geometry.alpha, vertexColors: THREE.VertexColors });
28 * } else { .... }
29 * var mesh = new THREE.Mesh( geometry, material );
30 */
31
32 module.exports = function (THREE) {
33
34 var STLLoader = function ( manager ) {
35
36 this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
37
38 };
39
40 STLLoader.prototype = {
41
42 constructor: THREE.STLLoader,
43
44 load: function ( url, onLoad, onProgress, onError ) {
45
46 var scope = this;
47
48 var loader = new THREE.FileLoader( scope.manager );
49 loader.setResponseType( 'arraybuffer' );
50 loader.load( url, function ( text ) {
51
52 onLoad( scope.parse( text ) );
53
54 }, onProgress, onError );
55
56 },
57
58 parse: function ( data ) {
59
60 var isBinary = function () {
61
62 var expect, face_size, n_faces, reader;
63 reader = new DataView( binData );
64 face_size = ( 32 / 8 * 3 ) + ( ( 32 / 8 * 3 ) * 3 ) + ( 16 / 8 );
65 n_faces = reader.getUint32( 80, true );
66 expect = 80 + ( 32 / 8 ) + ( n_faces * face_size );
67
68 if ( expect === reader.byteLength ) {
69
70 return true;
71
72 }
73
74 // An ASCII STL data must begin with 'solid ' as the first six bytes.
75 // However, ASCII STLs lacking the SPACE after the 'd' are known to be
76 // plentiful. So, check the first 5 bytes for 'solid'.
77
78 // US-ASCII ordinal values for 's', 'o', 'l', 'i', 'd'
79 var solid = [ 115, 111, 108, 105, 100 ];
80
81 for ( var i = 0; i < 5; i ++ ) {
82
83 // If solid[ i ] does not match the i-th byte, then it is not an
84 // ASCII STL; hence, it is binary and return true.
85
86 if ( solid[ i ] != reader.getUint8( i, false ) ) return true;
87
88 }
89
90 // First 5 bytes read "solid"; declare it to be an ASCII STL
91 return false;
92
93 };
94
95 var binData = this.ensureBinary( data );
96
97 return isBinary() ? this.parseBinary( binData ) : this.parseASCII( this.ensureString( data ) );
98
99 },
100
101 parseBinary: function ( data ) {
102
103 var reader = new DataView( data );
104 var faces = reader.getUint32( 80, true );
105
106 var r, g, b, hasColors = false, colors;
107 var defaultR, defaultG, defaultB, alpha;
108
109 // process STL header
110 // check for default color in header ("COLOR=rgba" sequence).
111
112 for ( var index = 0; index < 80 - 10; index ++ ) {
113
114 if ( ( reader.getUint32( index, false ) == 0x434F4C4F /*COLO*/ ) &&
115 ( reader.getUint8( index + 4 ) == 0x52 /*'R'*/ ) &&
116 ( reader.getUint8( index + 5 ) == 0x3D /*'='*/ ) ) {
117
118 hasColors = true;
119 colors = [];
120
121 defaultR = reader.getUint8( index + 6 ) / 255;
122 defaultG = reader.getUint8( index + 7 ) / 255;
123 defaultB = reader.getUint8( index + 8 ) / 255;
124 alpha = reader.getUint8( index + 9 ) / 255;
125
126 }
127
128 }
129
130 var dataOffset = 84;
131 var faceLength = 12 * 4 + 2;
132
133 var geometry = new THREE.BufferGeometry();
134
135 var vertices = [];
136 var normals = [];
137
138 for ( var face = 0; face < faces; face ++ ) {
139
140 var start = dataOffset + face * faceLength;
141 var normalX = reader.getFloat32( start, true );
142 var normalY = reader.getFloat32( start + 4, true );
143 var normalZ = reader.getFloat32( start + 8, true );
144
145 if ( hasColors ) {
146
147 var packedColor = reader.getUint16( start + 48, true );
148
149 if ( ( packedColor & 0x8000 ) === 0 ) {
150
151 // facet has its own unique color
152
153 r = ( packedColor & 0x1F ) / 31;
154 g = ( ( packedColor >> 5 ) & 0x1F ) / 31;
155 b = ( ( packedColor >> 10 ) & 0x1F ) / 31;
156
157 } else {
158
159 r = defaultR;
160 g = defaultG;
161 b = defaultB;
162
163 }
164
165 }
166
167 for ( var i = 1; i <= 3; i ++ ) {
168
169 var vertexstart = start + i * 12;
170
171 vertices.push( reader.getFloat32( vertexstart, true ) );
172 vertices.push( reader.getFloat32( vertexstart + 4, true ) );
173 vertices.push( reader.getFloat32( vertexstart + 8, true ) );
174
175 normals.push( normalX, normalY, normalZ );
176
177 if ( hasColors ) {
178
179 colors.push( r, g, b );
180
181 }
182
183 }
184
185 }
186
187 geometry.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array( vertices ), 3 ) );
188 geometry.addAttribute( 'normal', new THREE.BufferAttribute( new Float32Array( normals ), 3 ) );
189
190 if ( hasColors ) {
191
192 geometry.addAttribute( 'color', new THREE.BufferAttribute( new Float32Array( colors ), 3 ) );
193 geometry.hasColors = true;
194 geometry.alpha = alpha;
195
196 }
197
198 return geometry;
199
200 },
201
202 parseASCII: function ( data ) {
203
204 var geometry, length, patternFace, patternNormal, patternVertex, result, text;
205 geometry = new THREE.BufferGeometry();
206 patternFace = /facet([\s\S]*?)endfacet/g;
207
208 var vertices = [];
209 var normals = [];
210
211 var normal = new THREE.Vector3();
212
213 while ( ( result = patternFace.exec( data ) ) !== null ) {
214
215 text = result[ 0 ];
216 patternNormal = /normal[\s]+([\-+]?[0-9]+\.?[0-9]*([eE][\-+]?[0-9]+)?)+[\s]+([\-+]?[0-9]*\.?[0-9]+([eE][\-+]?[0-9]+)?)+[\s]+([\-+]?[0-9]*\.?[0-9]+([eE][\-+]?[0-9]+)?)+/g;
217
218 while ( ( result = patternNormal.exec( text ) ) !== null ) {
219
220 normal.x = parseFloat( result[ 1 ] );
221 normal.y = parseFloat( result[ 3 ] );
222 normal.z = parseFloat( result[ 5 ] );
223
224 }
225
226 patternVertex = /vertex[\s]+([\-+]?[0-9]+\.?[0-9]*([eE][\-+]?[0-9]+)?)+[\s]+([\-+]?[0-9]*\.?[0-9]+([eE][\-+]?[0-9]+)?)+[\s]+([\-+]?[0-9]*\.?[0-9]+([eE][\-+]?[0-9]+)?)+/g;
227
228 while ( ( result = patternVertex.exec( text ) ) !== null ) {
229
230 vertices.push( parseFloat( result[ 1 ] ), parseFloat( result[ 3 ] ), parseFloat( result[ 5 ] ) );
231 normals.push( normal.x, normal.y, normal.z );
232
233 }
234
235 }
236
237 geometry.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array( vertices ), 3 ) );
238 geometry.addAttribute( 'normal', new THREE.BufferAttribute( new Float32Array( normals ), 3 ) );
239
240 return geometry;
241
242 },
243
244 ensureString: function ( buf ) {
245
246 if ( typeof buf !== "string" ) {
247
248 var array_buffer = new Uint8Array( buf );
249 var strArray = [];
250 for ( var i = 0; i < buf.byteLength; i ++ ) {
251
252 strArray.push(String.fromCharCode( array_buffer[ i ] )); // implicitly assumes little-endian
253
254 }
255 return strArray.join('');
256
257 } else {
258
259 return buf;
260
261 }
262
263 },
264
265 ensureBinary: function ( buf ) {
266
267 if ( typeof buf === "string" ) {
268
269 var array_buffer = new Uint8Array( buf.length );
270 for ( var i = 0; i < buf.length; i ++ ) {
271
272 array_buffer[ i ] = buf.charCodeAt( i ) & 0xff; // implicitly assumes little-endian
273
274 }
275 return array_buffer.buffer || array_buffer;
276
277 } else {
278
279 return buf;
280
281 }
282
283 }
284
285 }
286
287 return STLLoader
288 }