-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbox.js
More file actions
63 lines (52 loc) · 1.7 KB
/
box.js
File metadata and controls
63 lines (52 loc) · 1.7 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
'use strict'
const PrimitiveCube = require('primitive-cube')
const { Geometry } = require('axis3d')
module.exports = BoxGeometry
function BoxGeometry(opts) {
// ensure object
if (null == opts || 'object' != typeof opts) {
opts = {}
}
let { segments, x, y, z } = opts
// defaults
if (null == segments) { segments = 1 }
if (null == x) { x = 1 }
if (null == y) { y = 1 }
if (null == z) { z = 1 }
if ('number' != typeof x) {
throw new TypeError(`Expecting '.x' to a be a number. Got ${typeof x}.`)
}
if ('number' != typeof y) {
throw new TypeError(`Expecting '.y' to a be a number. Got ${typeof y}.`)
}
if ('number' != typeof z) {
throw new TypeError(`Expecting '.z' to a be a number. Got ${typeof z}.`)
}
if ('number' != typeof segments && 'object' != typeof segments) {
throw new TypeError(
`Expecting '.segments' to be an object or 'number'. ` +
`Got ${typeof segments}`
)
}
if ('number' == typeof segments) {
segments = {x: segments, y: segments, z: segments}
} else if ('object' == typeof segments) {
if ('number' != typeof segments.x) {
throw new TypeError(
`Expecting '.segments.x' to a be a number. Got ${typeof segments.x}.`
)
} else if ('number' != typeof segments.y) {
throw new TypeError(
`Expecting '.segments.y' to a be a number. Got ${typeof segments.y}.`
)
} else if ('number' != typeof segments.z) {
throw new TypeError(
`Expecting '.segments.z' to a be a number. Got ${typeof segments.z}.`
)
}
}
const box = new Geometry({
complex: PrimitiveCube(x, y, z, segments.x, segments.y, segments.z)
})
return Object.assign(box, {size: {x, y, z}, segments})
}