Skip to content
This repository has been archived by the owner on Jul 24, 2022. It is now read-only.

Commit

Permalink
feat(scad-deserializer): added old openscad-openjscad-translator to t…
Browse files Browse the repository at this point in the history
…he packages (#75)

* feat(scad-deserializer): added old openscad-openjscad-translator to the packages
* refactor(scad-deserializer):
  * removed soon to be obsolete build steps & tools
  * remove big pre-built files for now
  * updated dependencies where applicable
  * minor tweaks here & there
  * removed import from io for now until code is updated
  • Loading branch information
kaosat-dev authored Nov 24, 2018
1 parent 0002489 commit 0a20323
Show file tree
Hide file tree
Showing 206 changed files with 25,082 additions and 373 deletions.
212 changes: 161 additions & 51 deletions packages/amf-serializer/index.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,183 @@
const { ensureManifoldness } = require('@jscad/io-utils')
/*
JSCAD Object to AMF (XML) Format Serialization
## License
Copyright (c) 2018 JSCAD Organization https://github.com/jscad
All code released under MIT license
Notes:
1) CAG conversion to:
none
2) CSG conversion to:
mesh
3) Path2D conversion to:
none
TBD
1) support zip output
*/

const {isCSG} = require('@jscad/csg')
const {ensureManifoldness} = require('@jscad/io-utils')
const {toArray} = require('@jscad/io-utils/arrays')
const stringify = require('onml/lib/stringify')

const mimeType = 'application/amf+xml'

function serialize (CSG, m, options) {
options && options.statusCallback && options.statusCallback({progress: 0})
CSG = ensureManifoldness(CSG)
var result = '<?xml version="1.0" encoding="UTF-8"?>\n<amf' + (m && m.unit ? ' unit="+m.unit"' : '') + '>\n'
for (var k in m) {
result += '<metadata type="' + k + '">' + m[k] + '</metadata>\n'
/** Serialize the give objects to AMF (xml) format.
* @param {Object} [options] - options for serialization
* @param {Object|Array} objects - objects to serialize as AMF
* @returns {Array} serialized contents, AMF format
*/
const serialize = (...params) => {
let options = {}
let objects
if (params.length === 0) {
throw new Error('no arguments supplied to serialize function !')
} else if (params.length === 1) {
// assumed to be object(s)
objects = Array.isArray(params[0]) ? params[0] : params
} else if (params.length > 1) {
options = params[0]
objects = params[1]
}
// make sure we always deal with arrays of objects as inputs
objects = toArray(objects)

const defaults = {
statusCallback: null,
unit: 'millimeter', // millimeter, inch, feet, meter or micrometer
metadata: null
}
result += '<object id="0">\n<mesh>\n<vertices>\n'
options = Object.assign({}, defaults, options)

options.statusCallback && options.statusCallback({progress: 0})

// construct the contents of the XML
var body = ['amf',
{
unit: options.unit,
version: '1.1'
},
['metadata', {type: 'author'}, 'Created using JSCAD']
]
body = body.concat(translateObjects(objects, options))

CSG.polygons.map(function (p) { // first we dump all vertices of all polygons
for (var i = 0; i < p.vertices.length; i++) {
result += CSGVertextoAMFString(p.vertices[i])
// convert the contents to AMF (XML) format
var amf = `<?xml version="1.0" encoding="UTF-8"?>
${stringify(body)}`

options && options.statusCallback && options.statusCallback({progress: 100})

return [amf]
}

const translateObjects = (objects, options) => {
let contents = []
objects.forEach(function (object, i) {
if (isCSG(object) && object.polygons.length > 0) {
object = ensureManifoldness(object)
options.id = i
contents.push(convertCSG(object, options))
}
})
result += '</vertices>\n'
return contents
}

var n = 0
CSG.polygons.map(function (p, i) { // then we dump all polygons
result += '<volume>\n'
if (p.vertices.length < 3) {
return
}
var color = null
if (p.shared && p.shared.color) {
color = p.shared.color
} else if (p.color) {
color = p.color
}
if (color != null) {
if (color.length < 4) color.push(1.0)
result += '<color><r>' + color[0] + '</r><g>' + color[1] + '</g><b>' + color[2] + '</b><a>' + color[3] + '</a></color>'
}
const convertCSG = (object, options) => {
var contents = ['object', {id: options.id}, convertToMesh(object, options)]
return contents
}

const convertToMesh = (object, options) => {
var contents = ['mesh', {}, convertToVertices(object, options)]
contents = contents.concat(convertToVolumes(object, options))
return contents
}

/*
* This section converts each CSG object to a list of vertex / coordinates
*/

for (var i = 0; i < p.vertices.length - 2; i++) { // making sure they are all triangles (triangular polygons)
result += '<triangle>'
result += '<v1>' + (n) + '</v1>'
result += '<v2>' + (n + i + 1) + '</v2>'
result += '<v3>' + (n + i + 2) + '</v3>'
result += '</triangle>\n'
const convertToVertices = (object, options) => {
var contents = ['vertices', {}]

let vertices = []
object.polygons.forEach(function (polygon) {
for (let i = 0; i < polygon.vertices.length; i++) {
vertices.push(convertToVertex(polygon.vertices[i], options))
}
n += p.vertices.length
result += '</volume>\n'
options && options.statusCallback && options.statusCallback({progress: 100 * i / CSG.polygons.length})
})
result += '</mesh>\n</object>\n'
result += '</amf>\n'

options && options.statusCallback && options.statusCallback({progress: 100})

return [result]
return contents.concat(vertices)
}

function CSGVectortoAMFString (v) {
return '<x>' + v._x + '</x><y>' + v._y + '</y><z>' + v._z + '</z>'
const convertToVertex = (vertex, options) => {
let contents = ['vertex', {}, convertToCoordinates(vertex, options)]
return contents
}

function CSGVertextoAMFString (vertex) {
return '<vertex><coordinates>' + CSGVectortoAMFString(vertex.pos) + '</coordinates></vertex>\n'
const convertToCoordinates = (vertex, options) => {
let position = vertex.pos
let contents = ['coordinates', {}, ['x', {}, position._x], ['y', {}, position._y], ['z', {}, position._z]]
return contents
}

/*
CSG.Vector3D.prototype.toAMFString = function () {
return '<x>' + this._x + '</x><y>' + this._y + '</y><z>' + this._z + '</z>'
* This section converts each CSG object to a list of volumes consisting of indexes into the list of vertices
*/

const convertToVolumes = (object, options) => {
let contents = []

let n = 0
object.polygons.forEach(function (polygon) {
if (polygon.vertices.length < 3) {
return
}

let volume = ['volume', {}]
let color = convertToColor(polygon, options)
let triangles = convertToTriangles(polygon, n)

if (color) {
volume.push(color)
}
volume = volume.concat(triangles)

contents.push(volume)

n += polygon.vertices.length
})
return contents
}

const convertToColor = (polygon, options) => {
let color = null
if (polygon.shared && polygon.shared.color) {
color = polygon.shared.color
} else if (polygon.color) {
color = polygon.color
}
if (color != null) {
if (color.length < 4) color.push(1.0)
return ['color', {}, ['r', {}, color[0]], ['g', {}, color[1]], ['b', {}, color[2]], ['a', {}, color[3]]]
}
return null
}

CSG.Vertex.prototype.toAMFString = function () {
return '<vertex><coordinates>' + this.pos.toAMFString() + '</coordinates></vertex>\n'
} */
const convertToTriangles = (polygon, index) => {
let contents = []

// making sure they are all triangles (triangular polygons)
for (var i = 0; i < polygon.vertices.length - 2; i++) {
let triangle = ['triangle', {}, ['v1', {}, index], ['v2', {}, (index + i + 1)], ['v3', {}, (index + i + 2)]]
contents.push(triangle)
}
return contents
}

module.exports = {
serialize,
Expand Down
8 changes: 4 additions & 4 deletions packages/amf-serializer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"repository": "https://github.com/jscad/io",
"main": "index.js",
"scripts": {
"test": "ava './test.js' --verbose --timeout 10000",
"test": "ava './test.js' --verbose --timeout 20000",
"release-patch": "git checkout master && npm version patch && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags ",
"release-minor": "git checkout master && npm version minor && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags ",
"release-major": "git checkout master && npm version major && git commit -a -m 'chore(dist): built dist/'; git push origin master --tags "
Expand Down Expand Up @@ -33,11 +33,11 @@
],
"license": "MIT",
"dependencies": {
"@jscad/csg": "0.7.0",
"@jscad/io-utils": "^0.1.3",
"sax": "^1.2.1",
"xmldom": "^0.1.27"
"onml": "^0.4.1"
},
"devDependencies": {
"@jscad/csg": "0.7.0"
"ava": "^0.19.1"
}
}
Loading

0 comments on commit 0a20323

Please sign in to comment.