@jscad/obj-deserializer 2.0.30 → 3.0.1-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -212
- package/LICENSE +1 -1
- package/README.md +3 -1
- package/dist/jscad-obj-deserializer.es.js +13 -0
- package/dist/jscad-obj-deserializer.min.js +13 -0
- package/package.json +22 -12
- package/rollup.config.js +27 -0
- package/{index.js → src/index.js} +13 -15
- package/tests/instantiate.test.js +9 -8
- package/tests/translate.test.js +18 -20
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { ensureString } from '@jscad/io-utils'
|
|
2
|
+
import { colorNameToRgb, polyhedron } from '@jscad/modeling'
|
|
3
3
|
|
|
4
|
-
const version =
|
|
4
|
+
const version = '[VI]{version}[/VI]' // version is injected by rollup
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Deserializer of OBJ data to JSCAD geometries.
|
|
@@ -33,10 +33,9 @@ const deserialize = (options, input) => {
|
|
|
33
33
|
options = Object.assign({}, defaults, options)
|
|
34
34
|
const { output } = options
|
|
35
35
|
|
|
36
|
-
input = ensureString(input)
|
|
37
|
-
|
|
38
36
|
options && options.statusCallback && options.statusCallback({ progress: 0 })
|
|
39
37
|
|
|
38
|
+
input = ensureString(input)
|
|
40
39
|
const { positions, groups } = getGroups(input, options)
|
|
41
40
|
|
|
42
41
|
const result = output === 'script' ? stringify(positions, groups, options) : objectify(positions, groups, options)
|
|
@@ -88,7 +87,7 @@ const getGroups = (data, options) => {
|
|
|
88
87
|
material = null
|
|
89
88
|
if (values && values.length > 0) {
|
|
90
89
|
// try to convert the material to a color by name
|
|
91
|
-
const c =
|
|
90
|
+
const c = colorNameToRgb(values[0])
|
|
92
91
|
if (c) material = [c[0], c[1], c[2], 1] // add alpha
|
|
93
92
|
}
|
|
94
93
|
}
|
|
@@ -127,7 +126,7 @@ const getGroups = (data, options) => {
|
|
|
127
126
|
}
|
|
128
127
|
|
|
129
128
|
const objectify = (points, groups, options) => {
|
|
130
|
-
const geometries = groups.map((group) =>
|
|
129
|
+
const geometries = groups.map((group) => polyhedron({ orientation: options.orientation, points, faces: group.faces, colors: group.colors }))
|
|
131
130
|
return geometries
|
|
132
131
|
}
|
|
133
132
|
|
|
@@ -176,7 +175,7 @@ const translateGroupsToFunctions = (groups, options) => {
|
|
|
176
175
|
code += `const group${index} = (points) => {
|
|
177
176
|
${translateFaces(faces)}
|
|
178
177
|
${translateColors(colors)}
|
|
179
|
-
return
|
|
178
|
+
return polyhedron({ orientation: '${options.orientation}', points, faces, colors })
|
|
180
179
|
}
|
|
181
180
|
`
|
|
182
181
|
})
|
|
@@ -196,11 +195,11 @@ const stringify = (positions, groups, options) => {
|
|
|
196
195
|
: ''
|
|
197
196
|
|
|
198
197
|
// create the main function, with a list of points and translated groups
|
|
199
|
-
code += `
|
|
198
|
+
code += `import * from '@jscad/modeling'
|
|
200
199
|
|
|
201
200
|
// groups: ${groups.length}
|
|
202
201
|
// points: ${positions.length}
|
|
203
|
-
const main = () => {
|
|
202
|
+
export const main = () => {
|
|
204
203
|
// points are common to all geometries
|
|
205
204
|
${translatePoints(positions)}
|
|
206
205
|
|
|
@@ -210,16 +209,15 @@ ${translateGroupsToCalls(groups)} ]
|
|
|
210
209
|
}
|
|
211
210
|
|
|
212
211
|
${translateGroupsToFunctions(groups, options)}
|
|
213
|
-
module.exports = {main}
|
|
214
212
|
`
|
|
215
213
|
|
|
216
214
|
// create a function for each group
|
|
217
215
|
return code
|
|
218
216
|
}
|
|
219
217
|
|
|
220
|
-
const
|
|
218
|
+
const mimeType = 'model/obj'
|
|
221
219
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
220
|
+
export {
|
|
221
|
+
mimeType,
|
|
222
|
+
deserialize
|
|
225
223
|
}
|
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const test = require('ava')
|
|
1
|
+
import fs from 'fs'
|
|
2
|
+
import path from 'path'
|
|
4
3
|
|
|
5
|
-
|
|
4
|
+
import test from 'ava'
|
|
6
5
|
|
|
7
|
-
|
|
6
|
+
import { geom3 } from '@jscad/modeling'
|
|
8
7
|
|
|
9
|
-
|
|
8
|
+
import { deserialize } from '../src/index.js'
|
|
9
|
+
|
|
10
|
+
const samplesPath = '../../../node_modules/@jscad/sample-files'
|
|
10
11
|
|
|
11
12
|
const toArray = (polygons) => polygons.map((p) => p.vertices.map((v) => ([v[0], v[1], v[2]])))
|
|
12
13
|
|
|
@@ -14,9 +15,9 @@ test('deserialize simple obj to geometry', (t) => {
|
|
|
14
15
|
const inputPath = path.resolve(samplesPath, 'obj/cube.obj')
|
|
15
16
|
const inputFile = fs.readFileSync(inputPath, 'utf8')
|
|
16
17
|
|
|
17
|
-
const observed =
|
|
18
|
+
const observed = deserialize({ output: 'geometry', addMetaData: false }, inputFile)
|
|
18
19
|
t.is(observed.length, 1)
|
|
19
|
-
const polygons =
|
|
20
|
+
const polygons = geom3.toPolygons(observed[0])
|
|
20
21
|
t.deepEqual(polygons.length, 6)
|
|
21
22
|
|
|
22
23
|
const observedVertices = toArray(polygons)
|
package/tests/translate.test.js
CHANGED
|
@@ -1,21 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const test = require('ava')
|
|
1
|
+
import fs from 'fs'
|
|
2
|
+
import path from 'path'
|
|
4
3
|
|
|
5
|
-
|
|
4
|
+
import test from 'ava'
|
|
6
5
|
|
|
7
|
-
|
|
6
|
+
import { deserialize } from '../src/index.js'
|
|
7
|
+
|
|
8
|
+
const samplesPath = '../../../node_modules/@jscad/sample-files'
|
|
8
9
|
|
|
9
10
|
test('translate simple obj file to jscad script', (t) => {
|
|
10
11
|
const inputPath = path.resolve(samplesPath, 'obj/cube.obj')
|
|
11
12
|
const inputFile = fs.readFileSync(inputPath, 'utf8')
|
|
12
13
|
|
|
13
|
-
const observed =
|
|
14
|
-
const expected = `
|
|
14
|
+
const observed = deserialize({ output: 'script', addMetaData: false }, inputFile)
|
|
15
|
+
const expected = `import * from '@jscad/modeling'
|
|
15
16
|
|
|
16
17
|
// groups: 1
|
|
17
18
|
// points: 8
|
|
18
|
-
const main = () => {
|
|
19
|
+
export const main = () => {
|
|
19
20
|
// points are common to all geometries
|
|
20
21
|
let points = [
|
|
21
22
|
[-0.5,-0.5,0.5],
|
|
@@ -54,10 +55,9 @@ const group0 = (points) => {
|
|
|
54
55
|
null,
|
|
55
56
|
null,
|
|
56
57
|
]
|
|
57
|
-
return
|
|
58
|
+
return polyhedron({ orientation: 'outward', points, faces, colors })
|
|
58
59
|
}
|
|
59
60
|
|
|
60
|
-
module.exports = {main}
|
|
61
61
|
`
|
|
62
62
|
|
|
63
63
|
t.deepEqual(observed, expected)
|
|
@@ -82,12 +82,12 @@ f 5 1 4 8
|
|
|
82
82
|
f 5 6 2 1
|
|
83
83
|
f 2 6 7 3
|
|
84
84
|
`
|
|
85
|
-
const observed =
|
|
86
|
-
const expected = `
|
|
85
|
+
const observed = deserialize({ filename: 'absolute', output: 'script', addMetaData: false }, data)
|
|
86
|
+
const expected = `import * from '@jscad/modeling'
|
|
87
87
|
|
|
88
88
|
// groups: 1
|
|
89
89
|
// points: 8
|
|
90
|
-
const main = () => {
|
|
90
|
+
export const main = () => {
|
|
91
91
|
// points are common to all geometries
|
|
92
92
|
let points = [
|
|
93
93
|
[0,2,2],
|
|
@@ -126,10 +126,9 @@ const group0 = (points) => {
|
|
|
126
126
|
[0,0.5019607843137255,0.5019607843137255,1],
|
|
127
127
|
[0,0.5019607843137255,0.5019607843137255,1],
|
|
128
128
|
]
|
|
129
|
-
return
|
|
129
|
+
return polyhedron({ orientation: 'outward', points, faces, colors })
|
|
130
130
|
}
|
|
131
131
|
|
|
132
|
-
module.exports = {main}
|
|
133
132
|
`
|
|
134
133
|
|
|
135
134
|
t.deepEqual(observed, expected)
|
|
@@ -168,12 +167,12 @@ v 2.000000 0.000000 0.000000
|
|
|
168
167
|
v 2.000000 0.000000 2.000000
|
|
169
168
|
f -4 -3 -2 -1
|
|
170
169
|
`
|
|
171
|
-
const observed =
|
|
172
|
-
const expected = `
|
|
170
|
+
const observed = deserialize({ output: 'script', addMetaData: false }, data)
|
|
171
|
+
const expected = `import * from '@jscad/modeling'
|
|
173
172
|
|
|
174
173
|
// groups: 1
|
|
175
174
|
// points: 24
|
|
176
|
-
const main = () => {
|
|
175
|
+
export const main = () => {
|
|
177
176
|
// points are common to all geometries
|
|
178
177
|
let points = [
|
|
179
178
|
[0,2,2],
|
|
@@ -228,10 +227,9 @@ const group0 = (points) => {
|
|
|
228
227
|
null,
|
|
229
228
|
null,
|
|
230
229
|
]
|
|
231
|
-
return
|
|
230
|
+
return polyhedron({ orientation: 'outward', points, faces, colors })
|
|
232
231
|
}
|
|
233
232
|
|
|
234
|
-
module.exports = {main}
|
|
235
233
|
`
|
|
236
234
|
t.deepEqual(observed, expected)
|
|
237
235
|
})
|