-
Notifications
You must be signed in to change notification settings - Fork 558
Expand file tree
/
Copy pathindex.js
More file actions
225 lines (192 loc) · 5.92 KB
/
index.js
File metadata and controls
225 lines (192 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
const { colors, primitives } = require('@jscad/modeling')
const { ensureString } = require('@jscad/io-utils')
const version = require('./package.json').version
/**
* Deserializer of OBJ data to JSCAD geometries.
* @module io/obj-deserializer
* @example
* const { deserializer, extension } = require('@jscad/obj-deserializer')
*/
/**
* Parse the given OBJ data and return either a JSCAD script or a set of geometry
* @see http://en.wikipedia.org/wiki/Wavefront_.obj_file
* @param {Object} options - options used during deserializing, REQUIRED
* @param {string} [options.filename='obj'] - filename of the original obj data
* @param {string} [options.version='0.0.0'] - version number to add to the metadata
* @param {boolean} [options.addMetadata=true] - toggle injection of metadata at the start of the script
* @param {string} [options.output='script'] - either 'script' or 'geometry' to set desired output
* @param {string} input - obj data
* @return {[object]/string} either a script (script) or a set of objects (geometry)
* @alias module:io/obj-deserializer.deserialize
*/
const deserialize = (options, input) => {
const defaults = {
filename: 'obj',
output: 'script',
orientation: 'outward',
version,
addMetaData: true
}
options = Object.assign({}, defaults, options)
const { output } = options
input = ensureString(input)
options && options.statusCallback && options.statusCallback({ progress: 0 })
const { positions, groups } = getGroups(input, options)
const result = output === 'script' ? stringify(positions, groups, options) : objectify(positions, groups, options)
options && options.statusCallback && options.statusCallback({ progress: 100 })
return result
}
const getGroups = (data, options) => {
let groups = []
const positions = []
let material = null
groups.push({ faces: [], colors: [], name: 'default', line: 0 })
const handleG = (command, values) => {
const group = { faces: [], colors: [], name: '' }
if (values && values.length > 0) group.name = values.join(' ')
groups.push(group)
}
const handleV = (command, values) => {
const x = parseFloat(values[0])
const y = parseFloat(values[1])
const z = parseFloat(values[2])
positions.push([x, y, z])
}
const handleF = (command, values) => {
// values : v/vt/vn
const facerefs = values.map((value) => {
const refs = value.match(/[0-9+\-eE]+/g)
let ref = parseInt(refs[0])
if (ref < 0) {
ref = positions.length + ref
} else {
ref--
}
return ref
})
const group = groups.pop()
group.faces.push(facerefs)
group.colors.push(material)
groups.push(group)
}
const handleMtl = (command, values) => {
material = null
if (values && values.length > 0) {
// try to convert the material to a color by name
const c = colors.colorNameToRgb(values[0])
if (c) material = [c[0], c[1], c[2], 1] // add alpha
}
}
// parse the input into groups of vertices and faces
const lines = data.split(/\n/)
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim()
if (line && line.length > 0) {
let values = line.match(/\S+/g)
if (values) {
const command = values[0]
values = values.slice(1)
switch (command) {
case 'g':
handleG(command, values)
break
case 'v':
handleV(command, values)
break
case 'f':
handleF(command, values)
break
case 'usemtl':
handleMtl(command, values)
break
}
}
}
}
// filter out groups without geometry
groups = groups.filter((group) => (group.faces.length > 0))
return { positions, groups }
}
const objectify = (points, groups, options) => {
const geometries = groups.map((group) => primitives.polyhedron({ orientation: options.orientation, points, faces: group.faces, colors: group.colors }))
return geometries
}
const translatePoints = (points) => {
let code = ' let points = [\n'
points.forEach((point) => (code += ` [${point}],\n`))
code += ' ]'
return code
}
const translateFaces = (faces) => {
let code = ' let faces = [\n'
faces.forEach((face) => (code += ` [${face}],\n`))
code += ' ]'
return code
}
const translateColors = (colors) => {
let code = ' let colors = [\n'
colors.forEach((c) => {
if (c) {
code += ` [${c}],\n`
} else {
code += ' null,\n'
}
})
code += ' ]'
return code
}
const translateGroupsToCalls = (groups) => {
let code = ''
groups.forEach((group, index) => (code += ` group${index}(points), // ${group.name}\n`))
return code
}
const translateGroupsToFunctions = (groups, options) => {
let code = ''
groups.forEach((group, index) => {
const faces = group.faces
const colors = group.colors
code += `
// group : ${group.name}
// faces: ${faces.length}
`
code += `const group${index} = (points) => {
${translateFaces(faces)}
${translateColors(colors)}
return primitives.polyhedron({ orientation: '${options.orientation}', points, faces, colors })
}
`
})
return code
}
const stringify = (positions, groups, options) => {
const { filename, addMetaData, version } = options
let code = addMetaData
? `//
// Produced by JSCAD IO Library : OBJ Deserializer (${version})
// date: ${new Date()}
// source: ${filename}
//
`
: ''
// create the main function, with a list of points and translated groups
code += `const {primitives} = require('@jscad/modeling')
// groups: ${groups.length}
// points: ${positions.length}
const main = () => {
// points are common to all geometries
${translatePoints(positions)}
let geometries = [
${translateGroupsToCalls(groups)} ]
return geometries
}
${translateGroupsToFunctions(groups, options)}
module.exports = {main}
`
// create a function for each group
return code
}
const extension = 'obj'
module.exports = {
deserialize,
extension
}