@jscad/modeling 2.9.3 → 2.9.6

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.
Files changed (70) hide show
  1. package/CHANGELOG.md +46 -0
  2. package/dist/jscad-modeling.min.js +133 -136
  3. package/package.json +2 -2
  4. package/src/colors/colorize.d.ts +6 -5
  5. package/src/geometries/geom2/type.d.ts +3 -2
  6. package/src/geometries/geom3/applyTransforms.js +1 -2
  7. package/src/geometries/geom3/type.d.ts +3 -2
  8. package/src/geometries/path2/appendPoints.js +3 -12
  9. package/src/geometries/path2/appendPoints.test.js +16 -0
  10. package/src/geometries/path2/concat.js +9 -8
  11. package/src/geometries/path2/concat.test.js +13 -7
  12. package/src/geometries/path2/type.d.ts +3 -2
  13. package/src/geometries/poly3/measureBoundingSphere.d.ts +2 -2
  14. package/src/geometries/poly3/measureBoundingSphere.js +46 -8
  15. package/src/geometries/poly3/measureBoundingSphere.test.js +16 -26
  16. package/src/geometries/poly3/type.d.ts +3 -2
  17. package/src/geometries/types.d.ts +4 -2
  18. package/src/maths/mat4/fromRotation.js +9 -7
  19. package/src/maths/mat4/fromTaitBryanRotation.js +8 -6
  20. package/src/maths/mat4/fromXRotation.js +4 -2
  21. package/src/maths/mat4/fromYRotation.js +4 -2
  22. package/src/maths/mat4/fromZRotation.js +4 -2
  23. package/src/maths/mat4/isMirroring.js +11 -11
  24. package/src/maths/mat4/rotate.js +9 -5
  25. package/src/maths/mat4/rotateX.js +4 -2
  26. package/src/maths/mat4/rotateY.js +4 -2
  27. package/src/maths/mat4/rotateZ.js +4 -2
  28. package/src/maths/mat4/translate.test.js +2 -3
  29. package/src/maths/utils/index.d.ts +1 -0
  30. package/src/maths/utils/index.js +2 -0
  31. package/src/{utils → maths/utils}/trigonometry.d.ts +0 -0
  32. package/src/{utils → maths/utils}/trigonometry.js +1 -1
  33. package/src/{utils → maths/utils}/trigonometry.test.js +0 -0
  34. package/src/maths/vec2/distance.js +1 -1
  35. package/src/maths/vec2/fromAngleRadians.js +4 -2
  36. package/src/maths/vec2/length.js +1 -1
  37. package/src/maths/vec2/length.test.js +0 -10
  38. package/src/maths/vec3/angle.js +2 -2
  39. package/src/maths/vec3/angle.test.js +0 -12
  40. package/src/maths/vec3/distance.js +1 -1
  41. package/src/maths/vec3/length.js +1 -1
  42. package/src/maths/vec3/length.test.js +0 -10
  43. package/src/operations/booleans/intersectGeom2.test.js +69 -0
  44. package/src/operations/booleans/{intersect.test.js → intersectGeom3.test.js} +3 -71
  45. package/src/operations/booleans/subtractGeom2.test.js +72 -0
  46. package/src/operations/booleans/{subtract.test.js → subtractGeom3.test.js} +3 -74
  47. package/src/operations/booleans/trees/PolygonTreeNode.js +2 -2
  48. package/src/operations/booleans/unionGeom2.test.js +166 -0
  49. package/src/operations/booleans/{union.test.js → unionGeom3.test.js} +3 -168
  50. package/src/operations/extrusions/extrudeFromSlices.js +3 -2
  51. package/src/operations/extrusions/extrudeRotate.test.js +42 -42
  52. package/src/operations/extrusions/project.test.js +2 -2
  53. package/src/operations/extrusions/slice/repair.js +62 -0
  54. package/src/operations/modifiers/insertTjunctions.js +34 -35
  55. package/src/operations/modifiers/reTesselateCoplanarPolygons.js +32 -31
  56. package/src/primitives/circle.test.js +7 -0
  57. package/src/primitives/cylinderElliptic.js +4 -2
  58. package/src/primitives/cylinderElliptic.test.js +7 -1
  59. package/src/primitives/ellipse.js +1 -1
  60. package/src/primitives/ellipse.test.js +7 -0
  61. package/src/primitives/ellipsoid.js +1 -1
  62. package/src/primitives/geodesicSphere.js +3 -2
  63. package/src/primitives/roundedCuboid.js +4 -2
  64. package/src/primitives/roundedCylinder.js +1 -1
  65. package/src/primitives/torus.test.js +7 -3
  66. package/src/utils/index.d.ts +0 -1
  67. package/src/utils/index.js +1 -3
  68. package/src/maths/mat4/constants.d.ts +0 -1
  69. package/src/maths/mat4/constants.js +0 -5
  70. package/src/operations/extrusions/slice/repairSlice.js +0 -47
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jscad/modeling",
3
- "version": "2.9.3",
3
+ "version": "2.9.6",
4
4
  "description": "Constructive Solid Geometry (CSG) Library for JSCAD",
5
5
  "homepage": "https://openjscad.xyz/",
6
6
  "repository": "https://github.com/jscad/OpenJSCAD.org",
@@ -61,5 +61,5 @@
61
61
  "nyc": "15.1.0",
62
62
  "uglifyify": "5.0.2"
63
63
  },
64
- "gitHead": "85fa1fcdfb2d516a201ecf31da242e64b5b3274e"
64
+ "gitHead": "9768af96e5da00cd113c00ddeb0f6046707819b1"
65
65
  }
@@ -5,9 +5,10 @@ import { RGB, RGBA } from './types'
5
5
 
6
6
  export default colorize
7
7
 
8
- declare function colorize<T extends Geometry>(color: RGB | RGBA, object: T): T
9
- declare function colorize<T>(color: RGB | RGBA, object: T): T & Colored
8
+ // Single Geom3 returns Colored Geom3
9
+ declare function colorize<T extends Geometry>(color: RGB | RGBA, object: T): T & Colored
10
10
 
11
- declare function colorize<T extends Geometry>(color: RGB | RGBA, ...objects: RecursiveArray<T>): Array<T>
12
- declare function colorize<T>(color: RGB | RGBA, ...objects: RecursiveArray<T>): Array<T & Colored>
13
- declare function colorize(color: RGB | RGBA, ...objects: RecursiveArray<any>): Array<any & Colored>
11
+ // List of Geom3 returns list of Colored Geom3
12
+ declare function colorize<T extends Geometry>(color: RGB | RGBA, ...objects: RecursiveArray<T>): Array<T & Colored>
13
+ // List of mixed geometries returns list of colored geometries
14
+ declare function colorize(color: RGB | RGBA, ...objects: RecursiveArray<Geometry>): Array<Geometry & Colored>
@@ -1,10 +1,11 @@
1
1
  import Vec2 from '../../maths/vec2/type'
2
2
  import Mat4 from '../../maths/mat4/type'
3
- import { Colored } from '../types'
3
+ import { Color } from '../types'
4
4
 
5
5
  export default Geom2
6
6
 
7
- declare interface Geom2 extends Colored {
7
+ declare interface Geom2 {
8
8
  sides: Array<[Vec2, Vec2]>
9
9
  transforms: Mat4
10
+ color?: Color
10
11
  }
@@ -14,9 +14,8 @@ const applyTransforms = (geometry) => {
14
14
  if (mat4.isIdentity(geometry.transforms)) return geometry
15
15
 
16
16
  // apply transforms to each polygon
17
- // const isMirror = mat4.isMirroring(geometry.transforms)
18
- // TBD if (isMirror) newvertices.reverse()
19
17
  geometry.polygons = geometry.polygons.map((polygon) => poly3.transform(geometry.transforms, polygon))
18
+ // reset transforms
20
19
  geometry.transforms = mat4.create()
21
20
  return geometry
22
21
  }
@@ -1,10 +1,11 @@
1
1
  import Poly3 from '../poly3/type'
2
2
  import Mat4 from '../../maths/mat4/type'
3
- import { Colored } from '../types'
3
+ import { Color } from '../types'
4
4
 
5
5
  export default Geom3
6
6
 
7
- declare interface Geom3 extends Colored {
7
+ declare interface Geom3 {
8
8
  polygons: Array<Poly3>
9
9
  transforms: Mat4
10
+ color?: Color
10
11
  }
@@ -1,5 +1,5 @@
1
- const fromPoints = require('./fromPoints')
2
- const toPoints = require('./toPoints')
1
+ const concat = require('./concat')
2
+ const create = require('./create')
3
3
 
4
4
  /**
5
5
  * Append the given list of points to the end of the given geometry.
@@ -10,15 +10,6 @@ const toPoints = require('./toPoints')
10
10
  * @example
11
11
  * let newpath = appendPoints([[3, 4], [4, 5]], oldpath)
12
12
  */
13
- const appendPoints = (points, geometry) => {
14
- if (geometry.isClosed) {
15
- throw new Error('cannot append points to a closed path')
16
- }
17
-
18
- let newpoints = toPoints(geometry)
19
- newpoints = newpoints.concat(points)
20
-
21
- return fromPoints({}, newpoints)
22
- }
13
+ const appendPoints = (points, geometry) => concat(geometry, create(points))
23
14
 
24
15
  module.exports = appendPoints
@@ -17,3 +17,19 @@ test('appendPoints: appending to a path produces a new path with expected points
17
17
  t.not(p1, obs)
18
18
  t.is(pts.length, 4)
19
19
  })
20
+
21
+ test('appendPoints: appending empty points to a path produces a new path with expected points', (t) => {
22
+ const p1 = fromPoints({}, [[1, 1], [2, 2]])
23
+ const obs = appendPoints([], p1)
24
+ const pts = toPoints(obs)
25
+ t.not(p1, obs)
26
+ t.is(pts.length, 2)
27
+ })
28
+
29
+ test('appendPoints: appending same points to a path produces a new path with expected points', (t) => {
30
+ const p1 = fromPoints({}, [[1, 1], [2, 2]])
31
+ const obs = appendPoints([[2, 2], [3, 3]], p1)
32
+ const pts = toPoints(obs)
33
+ t.not(p1, obs)
34
+ t.is(pts.length, 3)
35
+ })
@@ -1,8 +1,11 @@
1
1
  const fromPoints = require('./fromPoints')
2
2
  const toPoints = require('./toPoints')
3
+
3
4
  const { equals } = require('../../maths/vec2')
5
+
4
6
  /**
5
7
  * Concatenate the given paths.
8
+ *
6
9
  * If both contain the same point at the junction, merge it into one.
7
10
  * A concatenation of zero paths is an empty, open path.
8
11
  * A concatenation of one closed path to a series of open paths produces a closed path.
@@ -17,16 +20,14 @@ const { equals } = require('../../maths/vec2')
17
20
  const concat = (...paths) => {
18
21
  // Only the last path can be closed, producing a closed path.
19
22
  let isClosed = false
20
- for (const path of paths) {
21
- if (isClosed) {
22
- throw new Error('Cannot concatenate to a closed path')
23
- }
24
- isClosed = path.isClosed
25
- }
26
23
  let newpoints = []
27
- paths.forEach((path) => {
28
- const tmp = toPoints(path)
24
+ paths.forEach((path, i) => {
25
+ const tmp = toPoints(path).slice()
29
26
  if (newpoints.length > 0 && tmp.length > 0 && equals(tmp[0], newpoints[newpoints.length - 1])) tmp.shift()
27
+ if (tmp.length > 0 && isClosed) {
28
+ throw new Error(`Cannot concatenate to a closed path; check the ${i}th path`)
29
+ }
30
+ isClosed = path.isClosed
30
31
  newpoints = newpoints.concat(tmp)
31
32
  })
32
33
  return fromPoints({ closed: isClosed }, newpoints)
@@ -10,10 +10,16 @@ test('concat: empty paths produces an empty open path', (t) => {
10
10
  t.true(equals(concat(fromPoints({}, []), fromPoints({}, [])), fromPoints({ closed: false }, [])))
11
11
  })
12
12
 
13
- test('concat: Two open paths produces a open path', (t) => {
14
- t.true(equals(concat(fromPoints({ closed: false }, [[0, 0]]),
15
- fromPoints({ closed: false }, [[1, 1]])),
16
- fromPoints({ closed: false }, [[0, 0], [1, 1]])))
13
+ test('concat: many open paths produces a open path', (t) => {
14
+ const p1 = fromPoints({ closed: false }, [[0, 0]])
15
+ const p2 = fromPoints({ closed: false }, [[1, 1]])
16
+ const p3 = fromPoints({ closed: false }, [[1, 1], [3, 3]])
17
+
18
+ const result = concat(p1, p2, p3)
19
+ t.true(equals(result, fromPoints({}, [[0, 0], [1, 1], [3, 3]])))
20
+ t.is(p1.points.length, 1)
21
+ t.is(p2.points.length, 1)
22
+ t.is(p3.points.length, 2)
17
23
  })
18
24
 
19
25
  test('concat: An open path and a closed path produces a closed path', (t) => {
@@ -23,7 +29,7 @@ test('concat: An open path and a closed path produces a closed path', (t) => {
23
29
  })
24
30
 
25
31
  test('concat: A closed path and an open path throws an error', (t) => {
26
- t.throws(() => concat(fromPoints({ closed: true }, [[0, 0]]),
27
- fromPoints({ closed: false }, [[1, 1]])),
28
- { message: 'Cannot concatenate to a closed path' })
32
+ const p1 = fromPoints({ closed: true }, [[0, 0]])
33
+ const p2 = fromPoints({ closed: false }, [[1, 1]])
34
+ t.throws(() => concat(p1, p2), { message: 'Cannot concatenate to a closed path; check the 1th path' })
29
35
  })
@@ -1,11 +1,12 @@
1
1
  import Vec2 from '../../maths/vec2/type'
2
2
  import Mat4 from '../../maths/mat4/type'
3
- import { Colored } from '../types'
3
+ import { Color } from '../types'
4
4
 
5
5
  export default Path2
6
6
 
7
- declare interface Path2 extends Colored {
7
+ declare interface Path2 {
8
8
  points: Array<Vec2>
9
9
  isClosed: boolean
10
10
  transforms: Mat4
11
+ color?: Color
11
12
  }
@@ -1,6 +1,6 @@
1
1
  import Poly3 from './type'
2
- import Vec3 from '../../maths/vec3/type'
2
+ import Vec4 from '../../maths/vec4/type'
3
3
 
4
4
  export default measureBoundingSphere
5
5
 
6
- declare function measureBoundingSphere(polygon: Poly3): [Vec3, Vec3]
6
+ declare function measureBoundingSphere(polygon: Poly3): Vec4
@@ -1,19 +1,57 @@
1
1
  const vec3 = require('../../maths/vec3')
2
- const measureBoundingBox = require('./measureBoundingBox')
2
+ const vec4 = require('../../maths/vec4')
3
+
4
+ const cache = new WeakMap()
3
5
 
4
6
  /**
5
7
  * Measure the bounding sphere of the given polygon.
6
8
  * @param {poly3} polygon - the polygon to measure
7
- * @returns {Array} the computed bounding sphere; center point (3D) and radius
9
+ * @returns {vec4} the computed bounding sphere; center point (3D) and radius
8
10
  * @alias module:modeling/geometries/poly3.measureBoundingSphere
9
11
  */
10
12
  const measureBoundingSphere = (polygon) => {
11
- const box = measureBoundingBox(polygon)
12
- const center = box[0]
13
- vec3.add(center, box[0], box[1])
14
- vec3.scale(center, center, 0.5)
15
- const radius = vec3.distance(center, box[1])
16
- return [center, radius]
13
+ let boundingSphere = cache.get(polygon)
14
+ if (boundingSphere) return boundingSphere
15
+
16
+ const vertices = polygon.vertices
17
+ const out = vec4.create()
18
+
19
+ if (vertices.length === 0) {
20
+ out[0] = 0
21
+ out[1] = 0
22
+ out[2] = 0
23
+ out[3] = 0
24
+ return out
25
+ }
26
+
27
+ // keep a list of min/max vertices by axis
28
+ let minx = vertices[0]
29
+ let miny = minx
30
+ let minz = minx
31
+ let maxx = minx
32
+ let maxy = minx
33
+ let maxz = minx
34
+
35
+ vertices.forEach((v) => {
36
+ if (minx[0] > v[0]) minx = v
37
+ if (miny[1] > v[1]) miny = v
38
+ if (minz[2] > v[2]) minz = v
39
+ if (maxx[0] < v[0]) maxx = v
40
+ if (maxy[1] < v[1]) maxy = v
41
+ if (maxz[2] < v[2]) maxz = v
42
+ })
43
+
44
+ out[0] = (minx[0] + maxx[0]) * 0.5 // center of sphere
45
+ out[1] = (miny[1] + maxy[1]) * 0.5
46
+ out[2] = (minz[2] + maxz[2]) * 0.5
47
+ const x = out[0] - maxx[0]
48
+ const y = out[1] - maxy[1]
49
+ const z = out[2] - maxz[2]
50
+ out[3] = Math.sqrt(x * x + y * y + z * z) // radius of sphere
51
+
52
+ cache.set(polygon, out)
53
+
54
+ return out
17
55
  }
18
56
 
19
57
  module.exports = measureBoundingSphere
@@ -3,28 +3,23 @@ const { measureBoundingSphere, create, fromPoints, transform } = require('./inde
3
3
 
4
4
  const mat4 = require('../../maths/mat4')
5
5
 
6
- const { compareVectors, nearlyEqual } = require('../../../test/helpers/index')
7
-
8
6
  test('poly3: measureBoundingSphere() should return correct values', (t) => {
9
7
  let ply1 = create()
10
- let exp1 = [[0, 0, 0], 0]
8
+ let exp1 = [0, 0, 0, 0]
11
9
  let ret1 = measureBoundingSphere(ply1)
12
- t.true(compareVectors(ret1[0], exp1[0]))
13
- nearlyEqual(t, ret1[1], exp1[1], Number.EPSILON)
10
+ t.deepEqual(ret1, exp1)
14
11
 
15
12
  // simple triangle
16
13
  let ply2 = fromPoints([[0, 0, 0], [0, 10, 0], [0, 10, 10]])
17
- let exp2 = [[0, 5, 5], 7.0710678118654755]
14
+ let exp2 = [0, 5, 5, 7.0710678118654755]
18
15
  let ret2 = measureBoundingSphere(ply2)
19
- t.true(compareVectors(ret2[0], exp2[0]))
20
- nearlyEqual(t, ret2[1], exp2[1], Number.EPSILON)
16
+ t.deepEqual(ret2, exp2)
21
17
 
22
18
  // simple square
23
19
  let ply3 = fromPoints([[0, 0, 0], [0, 10, 0], [0, 10, 10], [0, 0, 10]])
24
- let exp3 = [[0, 5, 5], 7.0710678118654755]
20
+ let exp3 = [0, 5, 5, 7.0710678118654755]
25
21
  let ret3 = measureBoundingSphere(ply3)
26
- t.true(compareVectors(ret3[0], exp3[0]))
27
- nearlyEqual(t, ret3[1], exp3[1], Number.EPSILON)
22
+ t.deepEqual(ret3, exp3)
28
23
 
29
24
  // V-shape
30
25
  const points = [
@@ -40,10 +35,9 @@ test('poly3: measureBoundingSphere() should return correct values', (t) => {
40
35
  [0, 3, 3]
41
36
  ]
42
37
  let ply4 = fromPoints(points)
43
- let exp4 = [[0, 4.5, 3], 4.6097722286464435]
38
+ let exp4 = [0, 4.5, 3, 4.6097722286464435]
44
39
  let ret4 = measureBoundingSphere(ply4)
45
- t.true(compareVectors(ret4[0], exp4[0]))
46
- nearlyEqual(t, ret4[1], exp4[1], Number.EPSILON)
40
+ t.deepEqual(ret4, exp4)
47
41
 
48
42
  // rotated to various angles
49
43
  const rotation = mat4.fromZRotation(mat4.create(), (45 * 0.017453292519943295))
@@ -55,16 +49,12 @@ test('poly3: measureBoundingSphere() should return correct values', (t) => {
55
49
  ret2 = measureBoundingSphere(ply2)
56
50
  ret3 = measureBoundingSphere(ply3)
57
51
  ret4 = measureBoundingSphere(ply4)
58
- exp1 = [[0, 0, 0], 0]
59
- t.true(compareVectors(ret1[0], exp1[0]))
60
- nearlyEqual(t, ret1[1], exp1[1], Number.EPSILON)
61
- exp2 = [[-3.5355339059327373, 3.5355339059327378, 5], 7.0710678118654755]
62
- t.true(compareVectors(ret2[0], exp2[0]))
63
- nearlyEqual(t, ret2[1], exp2[1], Number.EPSILON)
64
- exp3 = [[-3.5355339059327373, 3.5355339059327378, 5], 7.0710678118654755]
65
- t.true(compareVectors(ret3[0], exp3[0]))
66
- nearlyEqual(t, ret3[1], exp3[1], Number.EPSILON)
67
- exp4 = [[-3.181980515339464, 3.1819805153394642, 3], 4.6097722286464435]
68
- t.true(compareVectors(ret4[0], exp4[0]))
69
- nearlyEqual(t, ret4[1], exp4[1], Number.EPSILON)
52
+ exp1 = [0, 0, 0, 0]
53
+ t.deepEqual(ret1, exp1)
54
+ exp2 = [-3.5355339059327373, 3.5355339059327378, 5, 7.0710678118654755]
55
+ t.deepEqual(ret2, exp2)
56
+ exp3 = [-3.5355339059327373, 3.5355339059327378, 5, 7.0710678118654755]
57
+ t.deepEqual(ret3, exp3)
58
+ exp4 = [-3.181980515339464, 3.1819805153394642, 3, 4.6097722286464435]
59
+ t.deepEqual(ret4, exp4)
70
60
  })
@@ -1,8 +1,9 @@
1
1
  import Vec3 from '../../maths/vec3/type'
2
- import { Colored } from '../types'
2
+ import { Color } from '../types'
3
3
 
4
4
  export default Poly3
5
5
 
6
- declare interface Poly3 extends Colored {
6
+ declare interface Poly3 {
7
7
  vertices: Array<Vec3>
8
+ color?: Color
8
9
  }
@@ -8,8 +8,10 @@ import { RGB, RGBA } from '../colors'
8
8
  // see https://github.com/jscad/OpenJSCAD.org/pull/726#issuecomment-724575265
9
9
  export type Geometry = Geom2 | Geom3 | Poly3 | Path2
10
10
 
11
- export type Colored = {
12
- color?: RGB | RGBA
11
+ export type Color = RGB | RGBA
12
+
13
+ export interface Colored {
14
+ color: Color
13
15
  }
14
16
 
15
17
  export { default as Geom2 } from './geom2/type'
@@ -1,6 +1,8 @@
1
- const identity = require('./identity')
1
+ const { EPS } = require('../constants')
2
+
3
+ const { sin, cos } = require('../utils/trigonometry')
2
4
 
3
- const { EPSILON } = require('./constants')
5
+ const identity = require('./identity')
4
6
 
5
7
  /**
6
8
  * Creates a matrix from a given angle around a given axis
@@ -19,20 +21,20 @@ const { EPSILON } = require('./constants')
19
21
  */
20
22
  const fromRotation = (out, rad, axis) => {
21
23
  let [x, y, z] = axis
22
- let len = Math.hypot(x, y, z)
24
+ const lengthSquared = x * x + y * y + z * z
23
25
 
24
- if (Math.abs(len) < EPSILON) {
26
+ if (Math.abs(lengthSquared) < EPS) {
25
27
  // axis is 0,0,0 or almost
26
28
  return identity(out)
27
29
  }
28
30
 
29
- len = 1 / len
31
+ const len = 1 / Math.sqrt(lengthSquared)
30
32
  x *= len
31
33
  y *= len
32
34
  z *= len
33
35
 
34
- const s = Math.sin(rad)
35
- const c = Math.cos(rad)
36
+ const s = sin(rad)
37
+ const c = cos(rad)
36
38
  const t = 1 - c
37
39
 
38
40
  // Perform rotation-specific matrix multiplication
@@ -1,3 +1,5 @@
1
+ const { sin, cos } = require('../utils/trigonometry')
2
+
1
3
  /**
2
4
  * Creates a matrix from the given Tait–Bryan angles.
3
5
  *
@@ -15,12 +17,12 @@
15
17
  */
16
18
  const fromTaitBryanRotation = (out, yaw, pitch, roll) => {
17
19
  // precompute sines and cosines of Euler angles
18
- const sy = Math.sin(yaw)
19
- const cy = Math.cos(yaw)
20
- const sp = Math.sin(pitch)
21
- const cp = Math.cos(pitch)
22
- const sr = Math.sin(roll)
23
- const cr = Math.cos(roll)
20
+ const sy = sin(yaw)
21
+ const cy = cos(yaw)
22
+ const sp = sin(pitch)
23
+ const cp = cos(pitch)
24
+ const sr = sin(roll)
25
+ const cr = cos(roll)
24
26
 
25
27
  // create and populate rotation matrix
26
28
  // left-hand-rule rotation
@@ -1,3 +1,5 @@
1
+ const { sin, cos } = require('../utils/trigonometry')
2
+
1
3
  /**
2
4
  * Creates a matrix from the given angle around the X axis.
3
5
  * This is equivalent to (but much faster than):
@@ -13,8 +15,8 @@
13
15
  * let matrix = fromXRotation(create(), Math.PI / 2)
14
16
  */
15
17
  const fromXRotation = (out, radians) => {
16
- const s = Math.sin(radians)
17
- const c = Math.cos(radians)
18
+ const s = sin(radians)
19
+ const c = cos(radians)
18
20
 
19
21
  // Perform axis-specific matrix multiplication
20
22
  out[0] = 1
@@ -1,3 +1,5 @@
1
+ const { sin, cos } = require('../utils/trigonometry')
2
+
1
3
  /**
2
4
  * Creates a matrix from the given angle around the Y axis.
3
5
  * This is equivalent to (but much faster than):
@@ -13,8 +15,8 @@
13
15
  * let matrix = fromYRotation(create(), Math.PI / 2)
14
16
  */
15
17
  const fromYRotation = (out, radians) => {
16
- const s = Math.sin(radians)
17
- const c = Math.cos(radians)
18
+ const s = sin(radians)
19
+ const c = cos(radians)
18
20
 
19
21
  // Perform axis-specific matrix multiplication
20
22
  out[0] = c
@@ -1,3 +1,5 @@
1
+ const { sin, cos } = require('../utils/trigonometry')
2
+
1
3
  /**
2
4
  * Creates a matrix from the given angle around the Z axis.
3
5
  * This is equivalent to (but much faster than):
@@ -13,8 +15,8 @@
13
15
  * let matrix = fromZRotation(create(), Math.PI / 2)
14
16
  */
15
17
  const fromZRotation = (out, radians) => {
16
- const s = Math.sin(radians)
17
- const c = Math.cos(radians)
18
+ const s = sin(radians)
19
+ const c = cos(radians)
18
20
 
19
21
  // Perform axis-specific matrix multiplication
20
22
  out[0] = c
@@ -1,7 +1,3 @@
1
- const cross = require('../vec3/cross')
2
- const dot = require('../vec3/dot')
3
- const fromValues = require('../vec3/fromValues')
4
-
5
1
  /**
6
2
  * Determine whether the given matrix is a mirroring transformation.
7
3
  *
@@ -10,15 +6,19 @@ const fromValues = require('../vec3/fromValues')
10
6
  * @alias module:modeling/maths/mat4.isMirroring
11
7
  */
12
8
  const isMirroring = (matrix) => {
13
- const u = fromValues(matrix[0], matrix[4], matrix[8])
14
- const v = fromValues(matrix[1], matrix[5], matrix[9])
15
- const w = fromValues(matrix[2], matrix[6], matrix[10])
9
+ // const xVector = [matrix[0], matrix[4], matrix[8]]
10
+ // const yVector = [matrix[1], matrix[5], matrix[9]]
11
+ // const zVector = [matrix[2], matrix[6], matrix[10]]
16
12
 
17
- // for a true orthogonal, non-mirrored base, u.cross(v) == w
13
+ // for a true orthogonal, non-mirrored base, xVector.cross(yVector) == zVector
18
14
  // If they have an opposite direction then we are mirroring
19
- const mirrorvalue = dot(cross(u, u, v), w)
20
- const ismirror = (mirrorvalue < 0)
21
- return ismirror
15
+ // calcuate xVector.cross(yVector)
16
+ const x = matrix[4] * matrix[9] - matrix[8] * matrix[5]
17
+ const y = matrix[8] * matrix[1] - matrix[0] * matrix[9]
18
+ const z = matrix[0] * matrix[5] - matrix[4] * matrix[1]
19
+ // calcualte dot(cross, zVector)
20
+ const d = x * matrix[2] + y * matrix[6] + z * matrix[10]
21
+ return (d < 0)
22
22
  }
23
23
 
24
24
  module.exports = isMirroring
@@ -1,3 +1,7 @@
1
+ const { EPS } = require('../constants')
2
+
3
+ const { sin, cos } = require('../utils/trigonometry')
4
+
1
5
  const copy = require('./copy')
2
6
 
3
7
  /**
@@ -12,20 +16,20 @@ const copy = require('./copy')
12
16
  */
13
17
  const rotate = (out, matrix, radians, axis) => {
14
18
  let [x, y, z] = axis
15
- let len = Math.hypot(x, y, z)
19
+ const lengthSquared = x * x + y * y + z * z
16
20
 
17
- if (Math.abs(len) < 0.000001) {
21
+ if (Math.abs(lengthSquared) < EPS) {
18
22
  // axis is 0,0,0 or almost
19
23
  return copy(out, matrix)
20
24
  }
21
25
 
22
- len = 1 / len
26
+ const len = 1 / Math.sqrt(lengthSquared)
23
27
  x *= len
24
28
  y *= len
25
29
  z *= len
26
30
 
27
- const s = Math.sin(radians)
28
- const c = Math.cos(radians)
31
+ const s = sin(radians)
32
+ const c = cos(radians)
29
33
  const t = 1 - c
30
34
 
31
35
  const a00 = matrix[0]
@@ -1,3 +1,5 @@
1
+ const { sin, cos } = require('../utils/trigonometry')
2
+
1
3
  /**
2
4
  * Rotates a matrix by the given angle around the X axis.
3
5
  *
@@ -8,8 +10,8 @@
8
10
  * @alias module:modeling/maths/mat4.rotateX
9
11
  */
10
12
  const rotateX = (out, matrix, radians) => {
11
- const s = Math.sin(radians)
12
- const c = Math.cos(radians)
13
+ const s = sin(radians)
14
+ const c = cos(radians)
13
15
  const a10 = matrix[4]
14
16
  const a11 = matrix[5]
15
17
  const a12 = matrix[6]
@@ -1,3 +1,5 @@
1
+ const { sin, cos } = require('../utils/trigonometry')
2
+
1
3
  /**
2
4
  * Rotates a matrix by the given angle around the Y axis.
3
5
  *
@@ -8,8 +10,8 @@
8
10
  * @alias module:modeling/maths/mat4.rotateY
9
11
  */
10
12
  const rotateY = (out, matrix, radians) => {
11
- const s = Math.sin(radians)
12
- const c = Math.cos(radians)
13
+ const s = sin(radians)
14
+ const c = cos(radians)
13
15
  const a00 = matrix[0]
14
16
  const a01 = matrix[1]
15
17
  const a02 = matrix[2]
@@ -1,3 +1,5 @@
1
+ const { sin, cos } = require('../utils/trigonometry')
2
+
1
3
  /**
2
4
  * Rotates a matrix by the given angle around the Z axis.
3
5
  *
@@ -8,8 +10,8 @@
8
10
  * @alias module:modeling/maths/mat4.rotateZ
9
11
  */
10
12
  const rotateZ = (out, matrix, radians) => {
11
- const s = Math.sin(radians)
12
- const c = Math.cos(radians)
13
+ const s = sin(radians)
14
+ const c = cos(radians)
13
15
  const a00 = matrix[0]
14
16
  const a01 = matrix[1]
15
17
  const a02 = matrix[2]
@@ -52,10 +52,9 @@ test('mat4: translate() called with three parameters should update a mat4 with c
52
52
  t.true(compareVectors(obs4, [1, 0, 0, 0, 0, 3, 0, 0, 0, 0, 5, 0, 2, 9, 30, 1]))
53
53
  t.true(compareVectors(ret4, [1, 0, 0, 0, 0, 3, 0, 0, 0, 0, 5, 0, 2, 9, 30, 1]))
54
54
 
55
- const r = (90 * 0.017453292519943295)
56
55
  const rotateZMatrix = [
57
- Math.cos(r), -Math.sin(r), 0, 0,
58
- Math.sin(r), Math.cos(r), 0, 0,
56
+ 0, -1, 0, 0,
57
+ 1, 0, 0, 0,
59
58
  0, 0, 1, 0,
60
59
  0, 0, 0, 1
61
60
  ]
@@ -3,5 +3,6 @@ export { default as area } from './area'
3
3
  export { default as interpolateBetween2DPointsForY } from './interpolateBetween2DPointsForY'
4
4
  export { default as intersect } from './intersect'
5
5
  export { default as solve2Linear } from './solve2Linear'
6
+ export { sin, cos } from './trigonometry'
6
7
 
7
8
  export as namespace utils
@@ -7,7 +7,9 @@
7
7
  module.exports = {
8
8
  aboutEqualNormals: require('./aboutEqualNormals'),
9
9
  area: require('./area'),
10
+ cos: require('./trigonometry').cos,
10
11
  interpolateBetween2DPointsForY: require('./interpolateBetween2DPointsForY'),
11
12
  intersect: require('./intersect'),
13
+ sin: require('./trigonometry').sin,
12
14
  solve2Linear: require('./solve2Linear')
13
15
  }
File without changes