stl-exporter
Version:
Convert face-vertex, or polygon meshes to ascii and binary STLs
88 lines (75 loc) • 3.51 kB
JavaScript
// Generated by CoffeeScript 1.12.7
(function() {
var BinarySerializer, bufferConverter, stream, writeStringToBufferView,
extend = 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; },
hasProp = {}.hasOwnProperty;
stream = require('stream');
bufferConverter = require('buffer-converter');
writeStringToBufferView = require('./helpers/writeStringToBufferView');
BinarySerializer = (function(superClass) {
extend(BinarySerializer, superClass);
function BinarySerializer(options) {
var base, base1;
this.options = options != null ? options : {};
if ((base = this.options).writableObjectMode == null) {
base.writableObjectMode = true;
}
if ((base1 = this.options).readableObjectMode == null) {
base1.readableObjectMode = false;
}
BinarySerializer.__super__.constructor.call(this, this.options);
this.internalBuffer = [];
this.name = '';
this.headerLength = 80;
this.facetsCounterLength = 4;
this.vectorLength = 12;
this.attributeByteCountLength = 2;
this.facetLength = this.vectorLength * 4 + this.attributeByteCountLength;
this.littleEndian = true;
}
BinarySerializer.prototype._createFacetString = function(face) {
var arrayBuffer, dataView, i, index, normal, offset, vertices;
normal = face.normal, vertices = face.vertices;
arrayBuffer = new ArrayBuffer(this.facetLength);
dataView = new DataView(arrayBuffer);
offset = 0;
dataView.setFloat32(offset, normal.x, this.littleEndian);
dataView.setFloat32(offset += 4, normal.y, this.littleEndian);
dataView.setFloat32(offset += 4, normal.z, this.littleEndian);
for (index = i = 0; i <= 2; index = ++i) {
dataView.setFloat32(offset += 4, vertices[index].x, this.littleEndian);
dataView.setFloat32(offset += 4, vertices[index].y, this.littleEndian);
dataView.setFloat32(offset += 4, vertices[index].z, this.littleEndian);
}
dataView.setUint16(offset += 4, 0, this.littleEndian);
offset += 2;
return bufferConverter.toBuffer(arrayBuffer);
};
BinarySerializer.prototype._flush = function(done) {
return done();
};
BinarySerializer.prototype._transform = function(chunk, encoding, done) {
var arrayBuffer, data, dataView, headerView;
this.internalBuffer = this.internalBuffer.concat(chunk.split('\n'));
while (data = this.internalBuffer.shift()) {
data = JSON.parse(data);
if (data.hasOwnProperty('name')) {
this.name = data.name;
arrayBuffer = new ArrayBuffer(this.headerLength + this.facetsCounterLength);
headerView = new DataView(arrayBuffer, 0, this.headerLength);
dataView = new DataView(arrayBuffer, this.headerLength);
writeStringToBufferView(this.name, headerView);
dataView.setUint32(0, data.faceCount || 0, this.littleEndian);
this.push(bufferConverter.toBuffer(arrayBuffer));
continue;
}
if (data.hasOwnProperty('vertices')) {
this.push(this._createFacetString(data));
}
}
return done();
};
return BinarySerializer;
})(stream.Transform);
module.exports = BinarySerializer;
}).call(this);