This repository has been archived by the owner on Jul 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(stl-deserializer): add ability to deserialize stl to csg (#32)
- Loading branch information
1 parent
3cd0ae4
commit a90dcf4
Showing
10 changed files
with
641 additions
and
358 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// BinaryReader | ||
// Refactored by Vjeux <vjeuxx@gmail.com> | ||
// http://blog.vjeux.com/2010/javascript/javascript-binary-reader.html | ||
|
||
// Original | ||
// + Jonas Raoni Soares Silva | ||
// @ http://jsfromhell.com/classes/binary-deserializer [rev. #1] | ||
|
||
function BinaryReader (data) { | ||
this._buffer = data | ||
this._pos = 0 | ||
}; | ||
|
||
BinaryReader.prototype = { | ||
|
||
/* Public */ | ||
|
||
readInt8: function () { return this._decodeInt(8, true) }, | ||
readUInt8: function () { return this._decodeInt(8, false) }, | ||
readInt16: function () { return this._decodeInt(16, true) }, | ||
readUInt16: function () { return this._decodeInt(16, false) }, | ||
readInt32: function () { return this._decodeInt(32, true) }, | ||
readUInt32: function () { return this._decodeInt(32, false) }, | ||
|
||
readFloat: function () { return this._decodeFloat(23, 8) }, | ||
readDouble: function () { return this._decodeFloat(52, 11) }, | ||
|
||
readChar: function () { return this.readString(1) }, | ||
readString: function (length) { | ||
this._checkSize(length * 8) | ||
var result = this._buffer.substr(this._pos, length) | ||
this._pos += length | ||
return result | ||
}, | ||
|
||
seek: function (pos) { | ||
this._pos = pos | ||
this._checkSize(0) | ||
}, | ||
|
||
getPosition: function () { | ||
return this._pos | ||
}, | ||
|
||
getSize: function () { | ||
return this._buffer.length | ||
}, | ||
|
||
/* Private */ | ||
|
||
_decodeFloat: function (precisionBits, exponentBits) { | ||
var length = precisionBits + exponentBits + 1 | ||
var size = length >> 3 | ||
this._checkSize(length) | ||
|
||
var bias = Math.pow(2, exponentBits - 1) - 1 | ||
var signal = this._readBits(precisionBits + exponentBits, 1, size) | ||
var exponent = this._readBits(precisionBits, exponentBits, size) | ||
var significand = 0 | ||
var divisor = 2 | ||
var curByte = 0 // length + (-precisionBits >> 3) - 1; | ||
do { | ||
var byteValue = this._readByte(++curByte, size) | ||
var startBit = precisionBits % 8 || 8 | ||
var mask = 1 << startBit | ||
while (mask >>= 1) { | ||
if (byteValue & mask) { | ||
significand += 1 / divisor | ||
} | ||
divisor *= 2 | ||
} | ||
} while (precisionBits -= startBit) | ||
|
||
this._pos += size | ||
|
||
return exponent == (bias << 1) + 1 ? significand ? NaN : signal ? -Infinity : +Infinity | ||
: (1 + signal * -2) * (exponent || significand ? !exponent ? Math.pow(2, -bias + 1) * significand | ||
: Math.pow(2, exponent - bias) * (1 + significand) : 0) | ||
}, | ||
|
||
_decodeInt: function (bits, signed) { | ||
var x = this._readBits(0, bits, bits / 8), max = Math.pow(2, bits) | ||
var result = signed && x >= max / 2 ? x - max : x | ||
|
||
this._pos += bits / 8 | ||
return result | ||
}, | ||
|
||
// shl fix: Henri Torgemane ~1996 (compressed by Jonas Raoni) | ||
_shl: function (a, b) { | ||
for (++b; --b; a = ((a %= 0x7fffffff + 1) & 0x40000000) == 0x40000000 ? a * 2 : (a - 0x40000000) * 2 + 0x7fffffff + 1); | ||
return a | ||
}, | ||
|
||
_readByte: function (i, size) { | ||
return this._buffer.charCodeAt(this._pos + size - i - 1) & 0xff | ||
}, | ||
|
||
_readBits: function (start, length, size) { | ||
var offsetLeft = (start + length) % 8 | ||
var offsetRight = start % 8 | ||
var curByte = size - (start >> 3) - 1 | ||
var lastByte = size + (-(start + length) >> 3) | ||
var diff = curByte - lastByte | ||
|
||
var sum = (this._readByte(curByte, size) >> offsetRight) & ((1 << (diff ? 8 - offsetRight : length)) - 1) | ||
|
||
if (diff && offsetLeft) { | ||
sum += (this._readByte(lastByte++, size) & ((1 << offsetLeft) - 1)) << (diff-- << 3) - offsetRight | ||
} | ||
|
||
while (diff) { | ||
sum += this._shl(this._readByte(lastByte++, size), (diff-- << 3) - offsetRight) | ||
} | ||
|
||
return sum | ||
}, | ||
|
||
_checkSize: function (neededBits) { | ||
if (!(this._pos + Math.ceil(neededBits / 8) < this._buffer.length)) { | ||
// throw new Error("Index out of bound"); | ||
} | ||
} | ||
} | ||
|
||
module.exports = BinaryReader |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,114 +1,4 @@ | ||
/* | ||
* Blob.js | ||
* See https://developer.mozilla.org/en-US/docs/Web/API/Blob | ||
* | ||
* Node and Browserify Compatible | ||
* | ||
* Copyright (c) 2015 by Z3 Dev (@zdev/www.z3dev.jp) | ||
* License: MIT License | ||
* | ||
* This implementation uses the Buffer class for all storage. | ||
* See https://nodejs.org/api/buffer.html | ||
* | ||
* URL.createObjectURL(blob) | ||
* | ||
* History: | ||
* 2015/07/02: 0.0.1: contributed to OpenJSCAD.org CLI openjscad | ||
*/ | ||
const makeBlob = require('./makeBlob') | ||
const BinaryReader = require('./BinaryReader') | ||
|
||
function makeBlob (contents, options) { | ||
const blob = typeof window !== 'undefined' ? window.Blob : Blob | ||
return blob | ||
} | ||
|
||
function Blob (contents, options) { | ||
// make the optional options non-optional | ||
options = options || {} | ||
// number of bytes | ||
this.size = 0 // contents, not allocation | ||
// media type | ||
this.type = '' | ||
// readability state (CLOSED: true, OPENED: false) | ||
this.isClosed = false | ||
// encoding of given strings | ||
this.encoding = 'utf8' | ||
// storage | ||
this.buffer = null | ||
this.length = 32e+6 // allocation, not contents | ||
|
||
if (!contents) return | ||
if (!Array.isArray(contents)) return | ||
|
||
// process options if any | ||
if (options.type) { | ||
// TBD if type contains any chars outside range U+0020 to U+007E, then set type to the empty string | ||
// Convert every character in type to lowercase | ||
this.type = options.type.toLowerCase() | ||
} | ||
if (options.endings) { | ||
// convert the EOL on strings | ||
} | ||
if (options.encoding) { | ||
this.encoding = options.encoding.toLowerCase() | ||
} | ||
if (options.length) { | ||
this.length = options.length | ||
} | ||
|
||
let wbytes | ||
let object | ||
// convert the contents (String, ArrayBufferView, ArrayBuffer, Blob) | ||
this.buffer = new Buffer(this.length) | ||
var index = 0 | ||
for (index = 0; index < contents.length; index++) { | ||
switch (typeof (contents[index])) { | ||
case 'string': | ||
wbytes = this.buffer.write(contents[index], this.size, this.encoding) | ||
this.size = this.size + wbytes | ||
break | ||
case 'object': | ||
object = contents[index] // this should be a reference to an object | ||
if (Buffer.isBuffer(object)) { | ||
} | ||
if (object instanceof ArrayBuffer) { | ||
var view = new DataView(object) | ||
var bindex = 0 | ||
for (bindex = 0; bindex < object.byteLength; bindex++) { | ||
var xbyte = view.getUint8(bindex) | ||
wbytes = this.buffer.writeUInt8(xbyte, this.size, false) | ||
this.size++ | ||
} | ||
} | ||
break | ||
default: | ||
break | ||
} | ||
} | ||
return this | ||
} | ||
|
||
Blob.prototype = { | ||
asBuffer: function () { | ||
return this.buffer.slice(0, this.size) | ||
}, | ||
|
||
slice: function (start, end, type) { | ||
start = start || 0 | ||
end = end || this.size | ||
type = type || '' | ||
return new Blob() | ||
}, | ||
|
||
close: function () { | ||
// if state of context objext is already CLOSED then return | ||
if (this.isClosed) return | ||
// set the readbility state of the context object to CLOSED and remove storage | ||
this.isClosed = true | ||
}, | ||
|
||
toString: function () { | ||
return 'blob blob blob' | ||
} | ||
} | ||
|
||
module.exports = {makeBlob} | ||
module.exports = {makeBlob, BinaryReader} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Blob.js | ||
* See https://developer.mozilla.org/en-US/docs/Web/API/Blob | ||
* | ||
* Node and Browserify Compatible | ||
* | ||
* Copyright (c) 2015 by Z3 Dev (@zdev/www.z3dev.jp) | ||
* License: MIT License | ||
* | ||
* This implementation uses the Buffer class for all storage. | ||
* See https://nodejs.org/api/buffer.html | ||
* | ||
* URL.createObjectURL(blob) | ||
* | ||
* History: | ||
* 2015/07/02: 0.0.1: contributed to OpenJSCAD.org CLI openjscad | ||
*/ | ||
|
||
function makeBlob (contents, options) { | ||
const blob = typeof window !== 'undefined' ? window.Blob : Blob | ||
return blob | ||
} | ||
|
||
function Blob (contents, options) { | ||
// make the optional options non-optional | ||
options = options || {} | ||
// number of bytes | ||
this.size = 0 // contents, not allocation | ||
// media type | ||
this.type = '' | ||
// readability state (CLOSED: true, OPENED: false) | ||
this.isClosed = false | ||
// encoding of given strings | ||
this.encoding = 'utf8' | ||
// storage | ||
this.buffer = null | ||
this.length = 32e+6 // allocation, not contents | ||
|
||
if (!contents) return | ||
if (!Array.isArray(contents)) return | ||
|
||
// process options if any | ||
if (options.type) { | ||
// TBD if type contains any chars outside range U+0020 to U+007E, then set type to the empty string | ||
// Convert every character in type to lowercase | ||
this.type = options.type.toLowerCase() | ||
} | ||
if (options.endings) { | ||
// convert the EOL on strings | ||
} | ||
if (options.encoding) { | ||
this.encoding = options.encoding.toLowerCase() | ||
} | ||
if (options.length) { | ||
this.length = options.length | ||
} | ||
|
||
let wbytes | ||
let object | ||
// convert the contents (String, ArrayBufferView, ArrayBuffer, Blob) | ||
this.buffer = new Buffer(this.length) | ||
var index = 0 | ||
for (index = 0; index < contents.length; index++) { | ||
switch (typeof (contents[index])) { | ||
case 'string': | ||
wbytes = this.buffer.write(contents[index], this.size, this.encoding) | ||
this.size = this.size + wbytes | ||
break | ||
case 'object': | ||
object = contents[index] // this should be a reference to an object | ||
if (Buffer.isBuffer(object)) { | ||
} | ||
if (object instanceof ArrayBuffer) { | ||
var view = new DataView(object) | ||
var bindex = 0 | ||
for (bindex = 0; bindex < object.byteLength; bindex++) { | ||
var xbyte = view.getUint8(bindex) | ||
wbytes = this.buffer.writeUInt8(xbyte, this.size, false) | ||
this.size++ | ||
} | ||
} | ||
break | ||
default: | ||
break | ||
} | ||
} | ||
return this | ||
} | ||
|
||
Blob.prototype = { | ||
asBuffer: function () { | ||
return this.buffer.slice(0, this.size) | ||
}, | ||
|
||
slice: function (start, end, type) { | ||
start = start || 0 | ||
end = end || this.size | ||
type = type || '' | ||
return new Blob() | ||
}, | ||
|
||
close: function () { | ||
// if state of context objext is already CLOSED then return | ||
if (this.isClosed) return | ||
// set the readbility state of the context object to CLOSED and remove storage | ||
this.isClosed = true | ||
}, | ||
|
||
toString: function () { | ||
return 'blob blob blob' | ||
} | ||
} | ||
|
||
module.exports = makeBlob |
Oops, something went wrong.