UNPKG

3.4 kBJavaScriptView Raw
1const Vector3D = require('./math/Vector3')
2const Vertex = require('./math/Vertex3')
3const Plane = require('./math/Plane')
4const Polygon2D = require('./math/Polygon2')
5const Polygon3D = require('./math/Polygon3')
6
7/** Construct a CSG solid from a list of pre-generated slices.
8 * See Polygon.prototype.solidFromSlices() for details.
9 * @param {Object} options - options passed to solidFromSlices()
10 * @returns {CSG} new CSG object
11 */
12function fromSlices (options) {
13 return (new Polygon2D.createFromPoints([
14 [0, 0, 0],
15 [1, 0, 0],
16 [1, 1, 0],
17 [0, 1, 0]
18 ])).solidFromSlices(options)
19}
20
21/** Reconstruct a CSG solid from an object with identical property names.
22 * @param {Object} obj - anonymous object, typically from JSON
23 * @returns {CSG} new CSG object
24 */
25function fromObject (obj) {
26 const CSG = require('./CSG')
27 let polygons = obj.polygons.map(function (p) {
28 return Polygon3D.fromObject(p)
29 })
30 let csg = CSG.fromPolygons(polygons)
31 csg.isCanonicalized = obj.isCanonicalized
32 csg.isRetesselated = obj.isRetesselated
33 return csg
34}
35
36/** Reconstruct a CSG from the output of toCompactBinary().
37 * @param {CompactBinary} bin - see toCompactBinary().
38 * @returns {CSG} new CSG object
39 */
40function fromCompactBinary (bin) {
41 const CSG = require('./CSG') // FIXME: circular dependency ??
42
43 if (bin['class'] !== 'CSG') throw new Error('Not a CSG')
44 let planes = []
45 let planeData = bin.planeData
46 let numplanes = planeData.length / 4
47 let arrayindex = 0
48 let x, y, z, w, normal, plane
49 for (let planeindex = 0; planeindex < numplanes; planeindex++) {
50 x = planeData[arrayindex++]
51 y = planeData[arrayindex++]
52 z = planeData[arrayindex++]
53 w = planeData[arrayindex++]
54 normal = Vector3D.Create(x, y, z)
55 plane = new Plane(normal, w)
56 planes.push(plane)
57 }
58
59 let vertices = []
60 const vertexData = bin.vertexData
61 const numvertices = vertexData.length / 3
62 let pos
63 let vertex
64 arrayindex = 0
65 for (let vertexindex = 0; vertexindex < numvertices; vertexindex++) {
66 x = vertexData[arrayindex++]
67 y = vertexData[arrayindex++]
68 z = vertexData[arrayindex++]
69 pos = Vector3D.Create(x, y, z)
70 vertex = new Vertex(pos)
71 vertices.push(vertex)
72 }
73
74 let shareds = bin.shared.map(function (shared) {
75 return Polygon3D.Shared.fromObject(shared)
76 })
77
78 let polygons = []
79 let numpolygons = bin.numPolygons
80 let numVerticesPerPolygon = bin.numVerticesPerPolygon
81 let polygonVertices = bin.polygonVertices
82 let polygonPlaneIndexes = bin.polygonPlaneIndexes
83 let polygonSharedIndexes = bin.polygonSharedIndexes
84 let numpolygonvertices
85 let polygonvertices
86 let shared
87 let polygon // already defined plane,
88 arrayindex = 0
89 for (let polygonindex = 0; polygonindex < numpolygons; polygonindex++) {
90 numpolygonvertices = numVerticesPerPolygon[polygonindex]
91 polygonvertices = []
92 for (let i = 0; i < numpolygonvertices; i++) {
93 polygonvertices.push(vertices[polygonVertices[arrayindex++]])
94 }
95 plane = planes[polygonPlaneIndexes[polygonindex]]
96 shared = shareds[polygonSharedIndexes[polygonindex]]
97 polygon = new Polygon3D(polygonvertices, shared, plane)
98 polygons.push(polygon)
99 }
100 let csg = CSG.fromPolygons(polygons)
101 csg.isCanonicalized = true
102 csg.isRetesselated = true
103 return csg
104}
105
106module.exports = {
107 //fromPolygons,
108 fromSlices,
109 fromObject,
110 fromCompactBinary
111}