const { ensureManifoldness } = require('@jscad/io-utils') const mimeType = 'application/amf+xml' function serialize (CSG, m) { CSG = ensureManifoldness(CSG) var result = '\n\n' for (var k in m) { result += '' + m[k] + '\n' } result += '\n\n\n' 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]) } }) result += '\n' var n = 0 CSG.polygons.map(function (p) { // then we dump all polygons result += '\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[0] + '' + color[1] + '' + color[2] + '' + color[3] + '' } for (var i = 0; i < p.vertices.length - 2; i++) { // making sure they are all triangles (triangular polygons) result += '' result += '' + (n) + '' result += '' + (n + i + 1) + '' result += '' + (n + i + 2) + '' result += '\n' } n += p.vertices.length result += '\n' }) result += '\n\n' result += '\n' return [result] } function CSGVectortoAMFString (v) { return '' + v._x + '' + v._y + '' + v._z + '' } function CSGVertextoAMFString (vertex) { return '' + CSGVectortoAMFString(vertex.pos) + '\n' } /* CSG.Vector3D.prototype.toAMFString = function () { return '' + this._x + '' + this._y + '' + this._z + '' } CSG.Vertex.prototype.toAMFString = function () { return '' + this.pos.toAMFString() + '\n' } */ module.exports = { serialize, mimeType }