1 | const Vector3D = require('./math/Vector3')
|
2 | const Vertex = require('./math/Vertex3')
|
3 | const Plane = require('./math/Plane')
|
4 | const Polygon2D = require('./math/Polygon2')
|
5 | const Polygon3D = require('./math/Polygon3')
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | function 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 |
|
22 |
|
23 |
|
24 |
|
25 | function 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 |
|
37 |
|
38 |
|
39 |
|
40 | function fromCompactBinary (bin) {
|
41 | const CSG = require('./CSG')
|
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
|
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 |
|
106 | module.exports = {
|
107 |
|
108 | fromSlices,
|
109 | fromObject,
|
110 | fromCompactBinary
|
111 | }
|