-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstl-reader.js
More file actions
136 lines (103 loc) · 3.19 KB
/
Copy pathstl-reader.js
File metadata and controls
136 lines (103 loc) · 3.19 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
;(function() {
var StlReader = (function() {
if (typeof DataStream === 'undefined') {
DataStream = require('./DataStream.js/DataStream.js');
}
if (typeof StlAsciiReader === 'undefined') {
StlAsciiReader = require('./stl-ascii-reader.js');
}
if (typeof StlBinaryReader === 'undefined') {
StlBinaryReader = require('./stl-binary-reader.js');
}
var StlReader = function(options) {
};
/* Header size is 80 bytes */
var HEADER_SIZE = 80;
/* Triangle count size is a 4 byte integer */
var TRI_COUNT_SIZE = 4;
/* Per-triangle data size is 50 bytes - 4 vectors (normal plus three
vertices) each with three 4 byte floats plus an additional 2 byte
attribute count at the end */
var PER_TRI_SIZE = 4*(4*3)+2;
/**
* Check if the file is too small to be a valid STL file.
*
* @param {DataStream} ds the file data as a DataStream
* @returns {Boolean} true if the file is too small to be valid
*/
function isTooSmallToBeValid(ds) {
ds.seek(0);
if (ds.byteLength < HEADER_SIZE + TRI_COUNT_SIZE) {
return true;
}
return false;
}
/**
* Check if the file is a binary STL file.
*
* @param {DataStream} ds the file data as a DataStream
* @returns {Boolean} true if the file is a binary STL file
*/
function isBinary(ds) {
ds.seek(0);
var fileSize = ds.byteLength;
var skipHeader = ds.readUint8Array(HEADER_SIZE);
var numTriangles = ds.readUint32();
if (fileSize == HEADER_SIZE + TRI_COUNT_SIZE + numTriangles*PER_TRI_SIZE) {
return true;
}
return false;
}
/**
* Checks if the ArrayBuffer is valid - that is, the data could
* theoretically be an STL file
*
* @param {ArrayBuffer} fileData The file as an ArrayBuffer
* @return {DataStream} the DataStream for the file is valid else null
*/
function checkValidity(fileData) {
if (!fileData) {
return null;
}
if (fileData.byteLength === 0) {
return null;
}
var ds = new DataStream(fileData);
if (isTooSmallToBeValid(ds)) {
return null;
}
return ds;
}
/**
* Reads the triangle vertices of an STL file into a Float32Array. The
* type of the file - binary/ASCII - is automatically determined and
* the file is appropriately parsed.
*
* @param {ArrayBuffer} fileData The file as an ArrayBuffer
* @param {Object} returns an object with vn, vertices amd normals properties
*/
StlReader.prototype.read = function(fileData) {
var ds = checkValidity(fileData);
if (!ds) {
return null;
}
var reader;
if (isBinary(ds)) {
reader = new StlBinaryReader();
ds.seek(0);
return reader.read(ds);
} else {
reader = new StlAsciiReader();
ds.seek(0);
return reader.read(ds.readString());
}
};
return StlReader;
})();
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = StlReader;
}
if (typeof window !== 'undefined') {
window.StlReader = StlReader;
}
})();