This repository has been archived by the owner on Jul 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest.js
73 lines (65 loc) · 2.05 KB
/
test.js
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
const fs = require('fs')
const path = require('path')
const test = require('ava')
const deserializer = require('./index.js')
const filesPath = path.resolve('../../node_modules/@jscad/sample-files') // require.resolve('@jscad/sample-files')
const polygonsFromCsg = csg => csg.polygons.map(x => x.vertices.map(vert => ([vert.pos.x, vert.pos.y, vert.pos.z])))
test('translate simple obj file to jscad code', function (t) {
const inputPath = path.resolve(filesPath, 'obj/cube.obj')
const inputFile = fs.readFileSync(inputPath, 'utf8')
const expected = `// objects: 1
// object #1: polygons: 6
function main() { return
polyhedron({ points: [
[-0.5,-0.5,0.5],
[-0.5,-0.5,-0.5],
[-0.5,0.5,-0.5],
[-0.5,0.5,0.5],
[0.5,-0.5,0.5],
[0.5,-0.5,-0.5],
[0.5,0.5,-0.5],
[0.5,0.5,0.5]],
polygons: [
[3,2,1,0],
[1,5,4,0],
[2,6,5,1],
[7,6,2,3],
[4,7,3,0],
[5,6,7,4]] })
}
`
const observed = deserializer.deserialize(inputFile, undefined, {output: 'jscad', addMetaData: false})
t.deepEqual(observed, expected)
})
test('deserialize simple obj to cag/csg objects', function (t) {
const inputPath = path.resolve(filesPath, 'obj/cube.obj')
const inputFile = fs.readFileSync(inputPath, 'utf8')
const observed = deserializer.deserialize(inputFile, undefined, {output: 'csg', addMetaData: false})
t.deepEqual(observed.polygons.length, 6)
const observedVertices = polygonsFromCsg(observed)
const expectedVertices = [ [ [ -0.5, -0.5, 0.5 ],
[ -0.5, -0.5, -0.5 ],
[ -0.5, 0.5, -0.5 ],
[ -0.5, 0.5, 0.5 ] ],
[ [ -0.5, -0.5, 0.5 ],
[ 0.5, -0.5, 0.5 ],
[ 0.5, -0.5, -0.5 ],
[ -0.5, -0.5, -0.5 ] ],
[ [ -0.5, -0.5, -0.5 ],
[ 0.5, -0.5, -0.5 ],
[ 0.5, 0.5, -0.5 ],
[ -0.5, 0.5, -0.5 ] ],
[ [ -0.5, 0.5, 0.5 ],
[ -0.5, 0.5, -0.5 ],
[ 0.5, 0.5, -0.5 ],
[ 0.5, 0.5, 0.5 ] ],
[ [ -0.5, -0.5, 0.5 ],
[ -0.5, 0.5, 0.5 ],
[ 0.5, 0.5, 0.5 ],
[ 0.5, -0.5, 0.5 ] ],
[ [ 0.5, -0.5, 0.5 ],
[ 0.5, 0.5, 0.5 ],
[ 0.5, 0.5, -0.5 ],
[ 0.5, -0.5, -0.5 ] ] ]
t.deepEqual(observedVertices, expectedVertices)
})