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

Commit abf3040

Browse files
authored
feat(deserializers): added csg output capabilities to amf & obj deserializers & basic tests (#47)
* feat(deserializers): added csg output support for amf & obj deserializers * test(): added tests for gcode, amf & obj deserializers * refactor(deserializers): heavy restructuring of the above * chore(gcode-deserializer): clearly marked gcode-deserializer as not implemented * updated docstrings
1 parent f5df997 commit abf3040

File tree

21 files changed

+1330
-958
lines changed

21 files changed

+1330
-958
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const inchMM = (1 / 0.039370) // used for scaling AMF (inch) to CAG coordinates(MM)
2+
3+
module.exports = {inchMM}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const {CSG} = require('@jscad/csg')
2+
3+
const createObject = require('./objectBuilder')
4+
const parse = require('./parse')
5+
6+
const deserializeToCSG = function (src, filename, options) {
7+
filename = filename || 'amf'
8+
const defaults = {pxPmm: require('./constants').pxPmm, version: '0.0.0', addMetaData: true}
9+
options = Object.assign({}, defaults, options)
10+
const {pxPmm} = options
11+
12+
// parse the AMF data
13+
const {amfObj, amfMaterials, amfTextures, amfConstels} = parse(src, pxPmm)
14+
if (!amfObj) {
15+
throw new Error('AMF parsing failed, no valid amf data retrieved')
16+
}
17+
18+
return objectify(amfObj, {amfMaterials, amfTextures, amfConstels})
19+
}
20+
21+
const objectify = (amf, data) => {
22+
let objects = amf.objects
23+
const csgs = objects.map((object, index) => object.type === 'object' ? createObject(object, index, data, {amf, csg: true}) : undefined)
24+
return new CSG().union(csgs)
25+
}
26+
27+
module.exports = deserializeToCSG
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
function amfMesh (element) {
2+
let obj = {type: 'mesh'}
3+
obj.objects = []
4+
return obj
5+
}
6+
7+
// Note: TBD Vertices can have a color, which is used to interpolate a face color (from the 3 vertices)
8+
function amfVertices (element) {
9+
let obj = {type: 'vertices'}
10+
obj.objects = []
11+
return obj
12+
}
13+
14+
function amfCoordinates (element) {
15+
let obj = {type: 'coordinates'}
16+
obj.objects = []
17+
return obj
18+
}
19+
function amfNormal (element) {
20+
let obj = {type: 'normal'}
21+
obj.objects = []
22+
return obj
23+
}
24+
function amfX (element) {
25+
return {type: 'x', value: '0'}
26+
}
27+
function amfY (element) {
28+
return {type: 'y', value: '0'}
29+
}
30+
function amfZ (element) {
31+
return {type: 'z', value: '0'}
32+
}
33+
34+
function amfVolume (element) {
35+
let obj = {type: 'volume'}
36+
37+
if ('MATERIALID' in element) { obj.materialid = element.MATERIALID }
38+
39+
obj.objects = []
40+
return obj
41+
}
42+
43+
function amfTriangle (element) {
44+
let obj = {type: 'triangle'}
45+
obj.objects = []
46+
return obj
47+
}
48+
function amfV1 (element) {
49+
return {type: 'v1', value: '0'}
50+
}
51+
function amfV2 (element) {
52+
return {type: 'v2', value: '0'}
53+
}
54+
function amfV3 (element) {
55+
return {type: 'v3', value: '0'}
56+
}
57+
58+
function amfVertex (element) {
59+
let obj = {type: 'vertex'}
60+
obj.objects = []
61+
return obj
62+
}
63+
64+
function amfEdge (element) {
65+
let obj = {type: 'edge'}
66+
67+
obj.objects = []
68+
return obj
69+
}
70+
71+
function amfMetadata (element) {
72+
let obj = {type: 'metadata'}
73+
74+
if ('TYPE' in element) { obj.mtype = element.TYPE }
75+
if ('ID' in element) { obj.id = element.ID }
76+
77+
return obj
78+
}
79+
80+
function amfMaterial (element) {
81+
let obj = {type: 'material'}
82+
83+
if ('ID' in element) { obj.id = element.ID }
84+
85+
obj.objects = []
86+
return obj
87+
}
88+
89+
function amfColor (element) {
90+
let obj = {type: 'color'}
91+
92+
obj.objects = []
93+
return obj
94+
}
95+
function amfR (element) {
96+
return {type: 'r', value: '1'}
97+
}
98+
function amfG (element) {
99+
return {type: 'g', value: '1'}
100+
}
101+
function amfB (element) {
102+
return {type: 'b', value: '1'}
103+
}
104+
function amfA (element) {
105+
return {type: 'a', value: '1'}
106+
}
107+
108+
function amfMap (element) {
109+
let obj = {type: 'map'}
110+
111+
if ('GTEXID' in element) { obj.gtexid = element.GTEXID }
112+
if ('BTEXID' in element) { obj.btexid = element.BTEXID }
113+
if ('RTEXID' in element) { obj.rtexid = element.RTEXID }
114+
115+
obj.objects = []
116+
return obj
117+
}
118+
119+
function amfU1 (element) {
120+
return {type: 'u1', value: '0'}
121+
}
122+
function amfU2 (element) {
123+
return {type: 'u2', value: '0'}
124+
}
125+
function amfU3 (element) {
126+
return {type: 'u3', value: '0'}
127+
}
128+
129+
module.exports = {
130+
amfMesh,
131+
amfVertices,
132+
amfCoordinates,
133+
amfX,
134+
amfY,
135+
amfZ,
136+
amfNormal,
137+
amfVolume,
138+
amfTriangle,
139+
amfV1,
140+
amfV2,
141+
amfV3,
142+
amfVertex,
143+
amfEdge,
144+
amfMetadata,
145+
amfMaterial,
146+
amfColor,
147+
amfR,
148+
amfG,
149+
amfB,
150+
amfA,
151+
amfMap,
152+
amfU1,
153+
amfU2,
154+
amfU3
155+
}

0 commit comments

Comments
 (0)