forked from gero3/threejs_STLLoader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSTLLoader.js
More file actions
155 lines (144 loc) · 7.13 KB
/
STLLoader.js
File metadata and controls
155 lines (144 loc) · 7.13 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
/*global THREE:true BinaryReader:true */
(function () {
var __hasProp = {}.hasOwnProperty,
__extends = function (child, parent) {
for (var key in parent) {
if (__hasProp.call(parent, key)) child[key] = parent[key];
}
function ctor() {
this.constructor = child;
}
ctor.prototype = parent.prototype;
child.prototype = new ctor();
child.__super__ = parent.prototype;
return child;
};
THREE.STLLoader = (function (_super) {
__extends(STLLoader, _super);
function STLLoader() {
var _this = this;
this.parseBinary = function (data) {
return STLLoader.prototype.parseBinary.apply(_this, arguments);
};
this.parse = function (data) {
return STLLoader.prototype.parse.apply(_this, arguments);
};
STLLoader.__super__.constructor.call(this);
}
STLLoader.prototype.load = function (url) {
var _this = this;
console.log("Attempting to load URL: [" + url + "]");
return $.ajax({
method: 'GET',
url: url,
crossDomain: true,
beforeSend: function (xhr) {
return xhr.overrideMimeType('text/plain; charset=x-user-defined');
},
success: function (data, text, xhr) {
var buffer, i, _i, _ref;
buffer = new Uint8Array(data.length);
for (i = _i = 0, _ref = data.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) {
buffer[i] = data.charCodeAt(i);
}
buffer = buffer.buffer;
return _this.dispatchEvent({
type: 'load',
content: _this.parse(data)
});
},
error: function (xhr, status, error) {
return _this.dispatchEvent({
type: 'error',
message: "Could not load URL [" + url + "] [" + error + "]"
});
}
});
};
STLLoader.prototype.parse = function (data) {
var isBinary,
_this = this;
isBinary = function (data) {
var expect, face_size, n_faces, reader;
reader = new BinaryReader(data);
reader.seek(80);
face_size = (32 / 8 * 3) + ((32 / 8 * 3) * 3) + (16 / 8);
n_faces = reader.readUInt32();
expect = 80 + (32 / 8) + (n_faces * face_size);
return expect === reader.getSize();
};
if (isBinary(data)) {
return this.parseBinary(data);
} else {
return this.parseASCII(data);
}
};
STLLoader.prototype.parseBinary = function (data) {
var face, geometry, n_faces, readFloat3, reader, _fn, _i,
_this = this;
reader = new BinaryReader(data);
readFloat3 = function () {
return [reader.readFloat(), reader.readFloat(), reader.readFloat()];
};
reader.seek(80);
n_faces = reader.readUInt32();
geometry = new THREE.Geometry();
_fn = function (face) {
var attr, length, newNormal, normal, v1, v2, _j;
normal = (function (func, args, ctor) {
ctor.prototype = func.prototype;
var child = new ctor(),
result = func.apply(child, args);
return Object(result) === result ? result : child;
})(THREE.Vector3, readFloat3(), function () {});
for (_j = 1; _j <= 3; _j++) {
geometry.vertices.push((function (func, args, ctor) {
ctor.prototype = func.prototype;
var child = new ctor(),
result = func.apply(child, args);
return Object(result) === result ? result : child;
})(THREE.Vector3, readFloat3(), function() {}));
}
attr = reader.readUInt16();
length = geometry.vertices.length;
v1 = new THREE.Vector3().subVectors(geometry.vertices[length - 3], geometry.vertices[length - 2]);
v2 = new THREE.Vector3().subVectors(geometry.vertices[length - 3], geometry.vertices[length - 1]);
newNormal = new THREE.Vector3().crossVectors(v1, v2).normalize();
return geometry.faces.push(new THREE.Face3(length - 3, length - 2, length - 1, newNormal));
};
for (face = _i = 0; 0 <= n_faces ? _i < n_faces : _i > n_faces; face = 0 <= n_faces ? ++_i : --_i) {
_fn(face);
}
geometry.computeCentroids();
geometry.computeBoundingBox();
geometry.computeBoundingSphere();
return geometry;
};
STLLoader.prototype.parseASCII = function (data) {
var geometry, length, newNormal, normal, patternFace, patternNormal, patternVertex, result, text, v1, v2;
geometry = new THREE.Geometry();
patternFace = /facet([\s\S]*?)endfacet/g;
while (((result = patternFace.exec(data)) != null)) {
text = result[0];
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;
while (((result = patternNormal.exec(text)) != null)) {
normal = new THREE.Vector3(parseFloat(result[1]), parseFloat(result[3]), parseFloat(result[5]));
}
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;
while (((result = patternVertex.exec(text)) != null)) {
geometry.vertices.push(new THREE.Vector3(parseFloat(result[1]), parseFloat(result[3]), parseFloat(result[5])));
}
length = geometry.vertices.length;
v1 = new THREE.Vector3().subVectors(geometry.vertices[length - 3], geometry.vertices[length - 2]);
v2 = new THREE.Vector3().subVectors(geometry.vertices[length - 3], geometry.vertices[length - 1]);
newNormal = new THREE.Vector3().crossVectors(v1, v2).normalize();
geometry.faces.push(new THREE.Face3(length - 3, length - 2, length - 1, newNormal));
}
geometry.computeCentroids();
geometry.computeBoundingBox();
geometry.computeBoundingSphere();
return geometry;
};
return STLLoader;
})(THREE.EventDispatcher);
}).call(this);