@jscad/modeling 2.9.4 → 2.9.5

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 (54) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/dist/jscad-modeling.min.js +125 -128
  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/type.d.ts +3 -2
  7. package/src/geometries/path2/type.d.ts +3 -2
  8. package/src/geometries/poly3/measureBoundingSphere.d.ts +2 -2
  9. package/src/geometries/poly3/measureBoundingSphere.js +46 -8
  10. package/src/geometries/poly3/measureBoundingSphere.test.js +16 -26
  11. package/src/geometries/poly3/type.d.ts +3 -2
  12. package/src/geometries/types.d.ts +4 -2
  13. package/src/maths/mat4/fromRotation.js +9 -7
  14. package/src/maths/mat4/fromTaitBryanRotation.js +8 -6
  15. package/src/maths/mat4/fromXRotation.js +4 -2
  16. package/src/maths/mat4/fromYRotation.js +4 -2
  17. package/src/maths/mat4/fromZRotation.js +4 -2
  18. package/src/maths/mat4/rotate.js +9 -5
  19. package/src/maths/mat4/rotateX.js +4 -2
  20. package/src/maths/mat4/rotateY.js +4 -2
  21. package/src/maths/mat4/rotateZ.js +4 -2
  22. package/src/maths/mat4/translate.test.js +2 -3
  23. package/src/maths/utils/index.d.ts +1 -0
  24. package/src/maths/utils/index.js +2 -0
  25. package/src/{utils → maths/utils}/trigonometry.d.ts +0 -0
  26. package/src/{utils → maths/utils}/trigonometry.js +1 -1
  27. package/src/{utils → maths/utils}/trigonometry.test.js +0 -0
  28. package/src/maths/vec2/distance.js +1 -1
  29. package/src/maths/vec2/fromAngleRadians.js +4 -2
  30. package/src/maths/vec2/length.js +1 -1
  31. package/src/maths/vec2/length.test.js +0 -10
  32. package/src/maths/vec3/angle.js +2 -2
  33. package/src/maths/vec3/angle.test.js +0 -12
  34. package/src/maths/vec3/distance.js +1 -1
  35. package/src/maths/vec3/length.js +1 -1
  36. package/src/maths/vec3/length.test.js +0 -10
  37. package/src/operations/booleans/trees/PolygonTreeNode.js +2 -2
  38. package/src/operations/extrusions/extrudeRotate.test.js +42 -42
  39. package/src/operations/extrusions/project.test.js +2 -2
  40. package/src/operations/extrusions/slice/repair.js +1 -1
  41. package/src/primitives/circle.test.js +7 -0
  42. package/src/primitives/cylinderElliptic.js +4 -2
  43. package/src/primitives/cylinderElliptic.test.js +7 -1
  44. package/src/primitives/ellipse.js +1 -1
  45. package/src/primitives/ellipse.test.js +7 -0
  46. package/src/primitives/ellipsoid.js +1 -1
  47. package/src/primitives/geodesicSphere.js +3 -2
  48. package/src/primitives/roundedCuboid.js +4 -2
  49. package/src/primitives/roundedCylinder.js +1 -1
  50. package/src/primitives/torus.test.js +7 -3
  51. package/src/utils/index.d.ts +0 -1
  52. package/src/utils/index.js +1 -3
  53. package/src/maths/mat4/constants.d.ts +0 -1
  54. package/src/maths/mat4/constants.js +0 -5
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jscad/modeling",
3
- "version": "2.9.4",
3
+ "version": "2.9.5",
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": "662965f40a1ce628aa97f30b814586e72a3acb36"
64
+ "gitHead": "225b034db0d94f748992da72b269833954a2e212"
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
  }
@@ -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,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,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
@@ -1,4 +1,4 @@
1
- const { NEPS } = require('../maths/constants')
1
+ const { NEPS } = require('../constants')
2
2
 
3
3
  /*
4
4
  * Returns zero if n is within epsilon of zero, otherwise return n
File without changes
@@ -9,7 +9,7 @@
9
9
  const distance = (a, b) => {
10
10
  const x = b[0] - a[0]
11
11
  const y = b[1] - a[1]
12
- return Math.hypot(x, y)
12
+ return Math.sqrt(x * x + y * y)
13
13
  }
14
14
 
15
15
  module.exports = distance
@@ -1,3 +1,5 @@
1
+ const { sin, cos } = require('../utils/trigonometry')
2
+
1
3
  /**
2
4
  * Create a new vector in the direction of the given angle.
3
5
  *
@@ -7,8 +9,8 @@
7
9
  * @alias module:modeling/maths/vec2.fromAngleRadians
8
10
  */
9
11
  const fromAngleRadians = (out, radians) => {
10
- out[0] = Math.cos(radians)
11
- out[1] = Math.sin(radians)
12
+ out[0] = cos(radians)
13
+ out[1] = sin(radians)
12
14
  return out
13
15
  }
14
16
 
@@ -5,6 +5,6 @@
5
5
  * @returns {Number} length
6
6
  * @alias module:modeling/maths/vec2.length
7
7
  */
8
- const length = (vector) => Math.hypot(vector[0], vector[1])
8
+ const length = (vector) => Math.sqrt(vector[0] * vector[0] + vector[1] * vector[1])
9
9
 
10
10
  module.exports = length
@@ -25,15 +25,5 @@ test('vec2: length() should return correct values', (t) => {
25
25
  const length5 = length(vec5)
26
26
  nearlyEqual(t, length5, 2.23606, EPS)
27
27
 
28
- // huge vector
29
- const vec6 = fromValues(1e200, 1e200)
30
- const length6 = length(vec6)
31
- nearlyEqual(t, length6, Math.SQRT2 * 1e200, EPS)
32
-
33
- // tiny vector
34
- const vec7 = fromValues(1e-200, 1e-200)
35
- const length7 = length(vec7)
36
- nearlyEqual(t, length7, Math.SQRT2 * 1e-200, EPS)
37
-
38
28
  t.true(true)
39
29
  })
@@ -15,8 +15,8 @@ const angle = (a, b) => {
15
15
  const bx = b[0]
16
16
  const by = b[1]
17
17
  const bz = b[2]
18
- const mag1 = Math.hypot(ax, ay, az)
19
- const mag2 = Math.hypot(bx, by, bz)
18
+ const mag1 = Math.sqrt(ax * ax + ay * ay + az * az)
19
+ const mag2 = Math.sqrt(bx * bx + by * by + bz * bz)
20
20
  const mag = mag1 * mag2
21
21
  const cosine = mag && dot(a, b) / mag
22
22
  return Math.acos(Math.min(Math.max(cosine, -1), 1))
@@ -30,17 +30,5 @@ test('vec3: angle() should return correct values', (t) => {
30
30
  const angle5 = angle(vec5a, vec5b)
31
31
  nearlyEqual(t, angle5, 0.785398, EPS)
32
32
 
33
- // tiny values
34
- const vec6a = fromValues(1, 0, 0)
35
- const vec6b = fromValues(1e-200, 1e-200, 0)
36
- const angle6 = angle(vec6a, vec6b)
37
- nearlyEqual(t, angle6, 0.785398, EPS)
38
-
39
- // huge values
40
- const vec7a = fromValues(1, 0, 0)
41
- const vec7b = fromValues(1e200, 1e200, 0)
42
- const angle7 = angle(vec7a, vec7b)
43
- nearlyEqual(t, angle7, 0.785398, EPS)
44
-
45
33
  t.true(true)
46
34
  })
@@ -10,7 +10,7 @@ const distance = (a, b) => {
10
10
  const x = b[0] - a[0]
11
11
  const y = b[1] - a[1]
12
12
  const z = b[2] - a[2]
13
- return Math.hypot(x, y, z)
13
+ return Math.sqrt(x * x + y * y + z * z)
14
14
  }
15
15
 
16
16
  module.exports = distance
@@ -9,7 +9,7 @@ const length = (vector) => {
9
9
  const x = vector[0]
10
10
  const y = vector[1]
11
11
  const z = vector[2]
12
- return Math.hypot(x, y, z)
12
+ return Math.sqrt(x * x + y * y + z * z)
13
13
  }
14
14
 
15
15
  module.exports = length
@@ -41,15 +41,5 @@ test('vec3: length() should return correct values', (t) => {
41
41
  const length9 = length(vec9)
42
42
  nearlyEqual(t, length9, 3.74165, EPS)
43
43
 
44
- // huge vector
45
- const vec10 = fromValues(1e200, 0, 1e200)
46
- const length10 = length(vec10)
47
- nearlyEqual(t, length10, Math.SQRT2 * 1e200, EPS)
48
-
49
- // tiny vector
50
- const vec11 = fromValues(1e-200, 0, 1e-200)
51
- const length11 = length(vec11)
52
- nearlyEqual(t, length11, Math.SQRT2 * 1e-200, EPS)
53
-
54
44
  t.true(true)
55
45
  })
@@ -132,8 +132,8 @@ class PolygonTreeNode {
132
132
  const polygon = this.polygon
133
133
  if (polygon) {
134
134
  const bound = poly3.measureBoundingSphere(polygon)
135
- const sphereradius = bound[1] + EPS // ensure radius is LARGER then polygon
136
- const spherecenter = bound[0]
135
+ const sphereradius = bound[3] + EPS // ensure radius is LARGER then polygon
136
+ const spherecenter = bound
137
137
  const d = vec3.dot(splane, spherecenter) - splane[3]
138
138
  if (d > sphereradius) {
139
139
  frontnodes.push(this)