curvy 0.1.1 → 0.3.2

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.
@@ -0,0 +1,38 @@
1
+ type Monotonicity = 'none' | 'increasing' | 'decreasing' | 'constant';
2
+ type Interval = readonly [number, number];
3
+ type LinearCoefficients = readonly [number, number];
4
+ type LinearPolynomial = {
5
+ readonly domain: Interval;
6
+ readonly range: Interval;
7
+ readonly monotonicity: Monotonicity;
8
+ readonly coefficients: LinearCoefficients;
9
+ readonly root: number | null;
10
+ readonly solve: (x: number) => number;
11
+ readonly solveInverse: (y: number) => number;
12
+ };
13
+ type QuadraticCoefficients = readonly [number, number, number];
14
+ type QuadraticPolynomial = {
15
+ readonly domain: Interval;
16
+ readonly range: Interval;
17
+ readonly monotonicity: Monotonicity;
18
+ readonly coefficients: QuadraticCoefficients;
19
+ readonly extreme: number | null;
20
+ readonly roots: ReadonlyArray<number>;
21
+ readonly derivative: LinearPolynomial;
22
+ readonly solve: (x: number) => number;
23
+ readonly solveInverse: (y: number, domain?: Interval) => ReadonlyArray<number>;
24
+ };
25
+ type CubicCoefficients = readonly [number, number, number, number];
26
+ type CubicPolynomial = {
27
+ readonly domain: Interval;
28
+ readonly range: Interval;
29
+ readonly monotonicity: Monotonicity;
30
+ readonly coefficients: CubicCoefficients;
31
+ readonly extrema: ReadonlyArray<number>;
32
+ readonly roots: ReadonlyArray<number>;
33
+ readonly derivative: QuadraticPolynomial;
34
+ readonly solve: (x: number) => number;
35
+ readonly solveInverse: (y: number, domain?: Interval) => ReadonlyArray<number>;
36
+ };
37
+
38
+ export type { CubicPolynomial as C, Interval as I, Monotonicity as M, CubicCoefficients as a };
@@ -0,0 +1,38 @@
1
+ type Monotonicity = 'none' | 'increasing' | 'decreasing' | 'constant';
2
+ type Interval = readonly [number, number];
3
+ type LinearCoefficients = readonly [number, number];
4
+ type LinearPolynomial = {
5
+ readonly domain: Interval;
6
+ readonly range: Interval;
7
+ readonly monotonicity: Monotonicity;
8
+ readonly coefficients: LinearCoefficients;
9
+ readonly root: number | null;
10
+ readonly solve: (x: number) => number;
11
+ readonly solveInverse: (y: number) => number;
12
+ };
13
+ type QuadraticCoefficients = readonly [number, number, number];
14
+ type QuadraticPolynomial = {
15
+ readonly domain: Interval;
16
+ readonly range: Interval;
17
+ readonly monotonicity: Monotonicity;
18
+ readonly coefficients: QuadraticCoefficients;
19
+ readonly extreme: number | null;
20
+ readonly roots: ReadonlyArray<number>;
21
+ readonly derivative: LinearPolynomial;
22
+ readonly solve: (x: number) => number;
23
+ readonly solveInverse: (y: number, domain?: Interval) => ReadonlyArray<number>;
24
+ };
25
+ type CubicCoefficients = readonly [number, number, number, number];
26
+ type CubicPolynomial = {
27
+ readonly domain: Interval;
28
+ readonly range: Interval;
29
+ readonly monotonicity: Monotonicity;
30
+ readonly coefficients: CubicCoefficients;
31
+ readonly extrema: ReadonlyArray<number>;
32
+ readonly roots: ReadonlyArray<number>;
33
+ readonly derivative: QuadraticPolynomial;
34
+ readonly solve: (x: number) => number;
35
+ readonly solveInverse: (y: number, domain?: Interval) => ReadonlyArray<number>;
36
+ };
37
+
38
+ export type { CubicPolynomial as C, Interval as I, Monotonicity as M, CubicCoefficients as a };
@@ -0,0 +1,150 @@
1
+ 'use strict';
2
+
3
+ const invariant = require('tiny-invariant');
4
+
5
+ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
6
+
7
+ const invariant__default = /*#__PURE__*/_interopDefaultCompat(invariant);
8
+
9
+ function chunkPointsBy(values, stride) {
10
+ const chunks = [];
11
+ invariant__default(values.length >= 4, "Values must have at least 4 elements");
12
+ invariant__default(
13
+ stride === 1 || stride === 2 || stride === 3,
14
+ "Stride must be 1, 2, or 3"
15
+ );
16
+ if (stride === 3) {
17
+ invariant__default(
18
+ values.length % stride === 1,
19
+ "Values length must be a multiple of 3 plus 1"
20
+ );
21
+ } else {
22
+ invariant__default(
23
+ values.length % stride === 0,
24
+ `Values length must be a multiple of ${stride}`
25
+ );
26
+ }
27
+ for (let i = 0; i <= values.length - 4; i += stride) {
28
+ chunks.push([
29
+ values[i],
30
+ values[i + 1],
31
+ values[i + 2],
32
+ values[i + 3]
33
+ ]);
34
+ }
35
+ return chunks;
36
+ }
37
+ function pointsToCoefficients(matrix, parameters) {
38
+ return [
39
+ parameters[0] * matrix[0][0] + parameters[1] * matrix[0][1] + parameters[2] * matrix[0][2] + parameters[3] * matrix[0][3],
40
+ parameters[0] * matrix[1][0] + parameters[1] * matrix[1][1] + parameters[2] * matrix[1][2] + parameters[3] * matrix[1][3],
41
+ parameters[0] * matrix[2][0] + parameters[1] * matrix[2][1] + parameters[2] * matrix[2][2] + parameters[3] * matrix[2][3],
42
+ parameters[0] * matrix[3][0] + parameters[1] * matrix[3][1] + parameters[2] * matrix[3][2] + parameters[3] * matrix[3][3]
43
+ ];
44
+ }
45
+ const bezier = {
46
+ matrix: [
47
+ [-1, 3, -3, 1],
48
+ [3, -6, 3, 0],
49
+ [-3, 3, 0, 0],
50
+ [1, 0, 0, 0]
51
+ ],
52
+ chunkCoefficients(parameters) {
53
+ const coefficients = [];
54
+ for (const chunk of chunkPointsBy(parameters, 3)) {
55
+ coefficients.push(pointsToCoefficients(this.matrix, chunk));
56
+ }
57
+ return coefficients;
58
+ }
59
+ };
60
+ const hermite = {
61
+ matrix: [
62
+ [2, 1, -2, 1],
63
+ [-3, -2, 3, -1],
64
+ [0, 1, 0, 0],
65
+ [1, 0, 0, 0]
66
+ ],
67
+ chunkCoefficients(parameters) {
68
+ const coefficients = [];
69
+ for (const chunk of chunkPointsBy(parameters, 2)) {
70
+ coefficients.push(pointsToCoefficients(this.matrix, chunk));
71
+ }
72
+ return coefficients;
73
+ }
74
+ };
75
+ const cardinal = (a, duplicateEndpoints = true) => ({
76
+ matrix: [
77
+ [-a, 2 - a, a - 2, a],
78
+ [2 * a, a - 3, 3 - 2 * a, -a],
79
+ [-a, 0, a, 0],
80
+ [0, 1, 0, 0]
81
+ ],
82
+ chunkCoefficients(parameters) {
83
+ const coefficients = [];
84
+ for (const chunk of chunkPointsBy(
85
+ duplicateEndpoints ? [parameters[0], ...parameters, parameters[parameters.length - 1]] : parameters,
86
+ 1
87
+ )) {
88
+ coefficients.push(pointsToCoefficients(this.matrix, chunk));
89
+ }
90
+ return coefficients;
91
+ }
92
+ });
93
+ const catmullRom = (duplicateEndpoints = true) => ({
94
+ matrix: [
95
+ [-0.5, 1.5, -1.5, 0.5],
96
+ [1, -2.5, 2, -0.5],
97
+ [-0.5, 0, 0.5, 0],
98
+ [0, 1, 0, 0]
99
+ ],
100
+ chunkCoefficients(parameters) {
101
+ const coefficients = [];
102
+ for (const chunk of chunkPointsBy(
103
+ duplicateEndpoints ? [parameters[0], ...parameters, parameters[parameters.length - 1]] : parameters,
104
+ 1
105
+ )) {
106
+ coefficients.push(pointsToCoefficients(this.matrix, chunk));
107
+ }
108
+ return coefficients;
109
+ }
110
+ });
111
+ const ONE_SIXTH = 1 / 6;
112
+ const basis = (triplicateEndpoints = true) => ({
113
+ matrix: [
114
+ [-ONE_SIXTH, 0.5, -0.5, ONE_SIXTH],
115
+ [0.5, -1, 0.5, 0],
116
+ [-0.5, 0, 0.5, 0],
117
+ [ONE_SIXTH, 4 * ONE_SIXTH, ONE_SIXTH, 0]
118
+ ],
119
+ chunkCoefficients(parameters) {
120
+ const coefficients = [];
121
+ for (const chunk of chunkPointsBy(
122
+ triplicateEndpoints ? [
123
+ parameters[0],
124
+ parameters[0],
125
+ ...parameters,
126
+ parameters[parameters.length - 1],
127
+ parameters[parameters.length - 1]
128
+ ] : parameters,
129
+ 1
130
+ )) {
131
+ coefficients.push(pointsToCoefficients(this.matrix, chunk));
132
+ }
133
+ return coefficients;
134
+ }
135
+ });
136
+ const splines = {
137
+ basis,
138
+ bezier,
139
+ cardinal,
140
+ catmullRom,
141
+ hermite
142
+ };
143
+
144
+ exports.basis = basis;
145
+ exports.bezier = bezier;
146
+ exports.cardinal = cardinal;
147
+ exports.catmullRom = catmullRom;
148
+ exports.hermite = hermite;
149
+ exports.pointsToCoefficients = pointsToCoefficients;
150
+ exports.splines = splines;
@@ -0,0 +1,27 @@
1
+ import { a as CubicCoefficients } from './shared/curvy.df73166c.cjs';
2
+
3
+ type Matrix4x4 = readonly [
4
+ CubicCoefficients,
5
+ CubicCoefficients,
6
+ CubicCoefficients,
7
+ CubicCoefficients
8
+ ];
9
+ type Spline = {
10
+ matrix: Matrix4x4;
11
+ chunkCoefficients: (parameters: ReadonlyArray<number>) => ReadonlyArray<CubicCoefficients>;
12
+ };
13
+ declare function pointsToCoefficients(matrix: Matrix4x4, parameters: CubicCoefficients): CubicCoefficients;
14
+ declare const bezier: Spline;
15
+ declare const hermite: Spline;
16
+ declare const cardinal: (a: number, duplicateEndpoints?: boolean) => Spline;
17
+ declare const catmullRom: (duplicateEndpoints?: boolean) => Spline;
18
+ declare const basis: (triplicateEndpoints?: boolean) => Spline;
19
+ declare const splines: {
20
+ readonly basis: (triplicateEndpoints?: boolean) => Spline;
21
+ readonly bezier: Spline;
22
+ readonly cardinal: (a: number, duplicateEndpoints?: boolean) => Spline;
23
+ readonly catmullRom: (duplicateEndpoints?: boolean) => Spline;
24
+ readonly hermite: Spline;
25
+ };
26
+
27
+ export { type Matrix4x4, type Spline, basis, bezier, cardinal, catmullRom, hermite, pointsToCoefficients, splines };
@@ -0,0 +1,27 @@
1
+ import { a as CubicCoefficients } from './shared/curvy.df73166c.mjs';
2
+
3
+ type Matrix4x4 = readonly [
4
+ CubicCoefficients,
5
+ CubicCoefficients,
6
+ CubicCoefficients,
7
+ CubicCoefficients
8
+ ];
9
+ type Spline = {
10
+ matrix: Matrix4x4;
11
+ chunkCoefficients: (parameters: ReadonlyArray<number>) => ReadonlyArray<CubicCoefficients>;
12
+ };
13
+ declare function pointsToCoefficients(matrix: Matrix4x4, parameters: CubicCoefficients): CubicCoefficients;
14
+ declare const bezier: Spline;
15
+ declare const hermite: Spline;
16
+ declare const cardinal: (a: number, duplicateEndpoints?: boolean) => Spline;
17
+ declare const catmullRom: (duplicateEndpoints?: boolean) => Spline;
18
+ declare const basis: (triplicateEndpoints?: boolean) => Spline;
19
+ declare const splines: {
20
+ readonly basis: (triplicateEndpoints?: boolean) => Spline;
21
+ readonly bezier: Spline;
22
+ readonly cardinal: (a: number, duplicateEndpoints?: boolean) => Spline;
23
+ readonly catmullRom: (duplicateEndpoints?: boolean) => Spline;
24
+ readonly hermite: Spline;
25
+ };
26
+
27
+ export { type Matrix4x4, type Spline, basis, bezier, cardinal, catmullRom, hermite, pointsToCoefficients, splines };
@@ -0,0 +1,27 @@
1
+ import { a as CubicCoefficients } from './shared/curvy.df73166c.js';
2
+
3
+ type Matrix4x4 = readonly [
4
+ CubicCoefficients,
5
+ CubicCoefficients,
6
+ CubicCoefficients,
7
+ CubicCoefficients
8
+ ];
9
+ type Spline = {
10
+ matrix: Matrix4x4;
11
+ chunkCoefficients: (parameters: ReadonlyArray<number>) => ReadonlyArray<CubicCoefficients>;
12
+ };
13
+ declare function pointsToCoefficients(matrix: Matrix4x4, parameters: CubicCoefficients): CubicCoefficients;
14
+ declare const bezier: Spline;
15
+ declare const hermite: Spline;
16
+ declare const cardinal: (a: number, duplicateEndpoints?: boolean) => Spline;
17
+ declare const catmullRom: (duplicateEndpoints?: boolean) => Spline;
18
+ declare const basis: (triplicateEndpoints?: boolean) => Spline;
19
+ declare const splines: {
20
+ readonly basis: (triplicateEndpoints?: boolean) => Spline;
21
+ readonly bezier: Spline;
22
+ readonly cardinal: (a: number, duplicateEndpoints?: boolean) => Spline;
23
+ readonly catmullRom: (duplicateEndpoints?: boolean) => Spline;
24
+ readonly hermite: Spline;
25
+ };
26
+
27
+ export { type Matrix4x4, type Spline, basis, bezier, cardinal, catmullRom, hermite, pointsToCoefficients, splines };
@@ -0,0 +1,138 @@
1
+ import invariant from 'tiny-invariant';
2
+
3
+ function chunkPointsBy(values, stride) {
4
+ const chunks = [];
5
+ invariant(values.length >= 4, "Values must have at least 4 elements");
6
+ invariant(
7
+ stride === 1 || stride === 2 || stride === 3,
8
+ "Stride must be 1, 2, or 3"
9
+ );
10
+ if (stride === 3) {
11
+ invariant(
12
+ values.length % stride === 1,
13
+ "Values length must be a multiple of 3 plus 1"
14
+ );
15
+ } else {
16
+ invariant(
17
+ values.length % stride === 0,
18
+ `Values length must be a multiple of ${stride}`
19
+ );
20
+ }
21
+ for (let i = 0; i <= values.length - 4; i += stride) {
22
+ chunks.push([
23
+ values[i],
24
+ values[i + 1],
25
+ values[i + 2],
26
+ values[i + 3]
27
+ ]);
28
+ }
29
+ return chunks;
30
+ }
31
+ function pointsToCoefficients(matrix, parameters) {
32
+ return [
33
+ parameters[0] * matrix[0][0] + parameters[1] * matrix[0][1] + parameters[2] * matrix[0][2] + parameters[3] * matrix[0][3],
34
+ parameters[0] * matrix[1][0] + parameters[1] * matrix[1][1] + parameters[2] * matrix[1][2] + parameters[3] * matrix[1][3],
35
+ parameters[0] * matrix[2][0] + parameters[1] * matrix[2][1] + parameters[2] * matrix[2][2] + parameters[3] * matrix[2][3],
36
+ parameters[0] * matrix[3][0] + parameters[1] * matrix[3][1] + parameters[2] * matrix[3][2] + parameters[3] * matrix[3][3]
37
+ ];
38
+ }
39
+ const bezier = {
40
+ matrix: [
41
+ [-1, 3, -3, 1],
42
+ [3, -6, 3, 0],
43
+ [-3, 3, 0, 0],
44
+ [1, 0, 0, 0]
45
+ ],
46
+ chunkCoefficients(parameters) {
47
+ const coefficients = [];
48
+ for (const chunk of chunkPointsBy(parameters, 3)) {
49
+ coefficients.push(pointsToCoefficients(this.matrix, chunk));
50
+ }
51
+ return coefficients;
52
+ }
53
+ };
54
+ const hermite = {
55
+ matrix: [
56
+ [2, 1, -2, 1],
57
+ [-3, -2, 3, -1],
58
+ [0, 1, 0, 0],
59
+ [1, 0, 0, 0]
60
+ ],
61
+ chunkCoefficients(parameters) {
62
+ const coefficients = [];
63
+ for (const chunk of chunkPointsBy(parameters, 2)) {
64
+ coefficients.push(pointsToCoefficients(this.matrix, chunk));
65
+ }
66
+ return coefficients;
67
+ }
68
+ };
69
+ const cardinal = (a, duplicateEndpoints = true) => ({
70
+ matrix: [
71
+ [-a, 2 - a, a - 2, a],
72
+ [2 * a, a - 3, 3 - 2 * a, -a],
73
+ [-a, 0, a, 0],
74
+ [0, 1, 0, 0]
75
+ ],
76
+ chunkCoefficients(parameters) {
77
+ const coefficients = [];
78
+ for (const chunk of chunkPointsBy(
79
+ duplicateEndpoints ? [parameters[0], ...parameters, parameters[parameters.length - 1]] : parameters,
80
+ 1
81
+ )) {
82
+ coefficients.push(pointsToCoefficients(this.matrix, chunk));
83
+ }
84
+ return coefficients;
85
+ }
86
+ });
87
+ const catmullRom = (duplicateEndpoints = true) => ({
88
+ matrix: [
89
+ [-0.5, 1.5, -1.5, 0.5],
90
+ [1, -2.5, 2, -0.5],
91
+ [-0.5, 0, 0.5, 0],
92
+ [0, 1, 0, 0]
93
+ ],
94
+ chunkCoefficients(parameters) {
95
+ const coefficients = [];
96
+ for (const chunk of chunkPointsBy(
97
+ duplicateEndpoints ? [parameters[0], ...parameters, parameters[parameters.length - 1]] : parameters,
98
+ 1
99
+ )) {
100
+ coefficients.push(pointsToCoefficients(this.matrix, chunk));
101
+ }
102
+ return coefficients;
103
+ }
104
+ });
105
+ const ONE_SIXTH = 1 / 6;
106
+ const basis = (triplicateEndpoints = true) => ({
107
+ matrix: [
108
+ [-ONE_SIXTH, 0.5, -0.5, ONE_SIXTH],
109
+ [0.5, -1, 0.5, 0],
110
+ [-0.5, 0, 0.5, 0],
111
+ [ONE_SIXTH, 4 * ONE_SIXTH, ONE_SIXTH, 0]
112
+ ],
113
+ chunkCoefficients(parameters) {
114
+ const coefficients = [];
115
+ for (const chunk of chunkPointsBy(
116
+ triplicateEndpoints ? [
117
+ parameters[0],
118
+ parameters[0],
119
+ ...parameters,
120
+ parameters[parameters.length - 1],
121
+ parameters[parameters.length - 1]
122
+ ] : parameters,
123
+ 1
124
+ )) {
125
+ coefficients.push(pointsToCoefficients(this.matrix, chunk));
126
+ }
127
+ return coefficients;
128
+ }
129
+ });
130
+ const splines = {
131
+ basis,
132
+ bezier,
133
+ cardinal,
134
+ catmullRom,
135
+ hermite
136
+ };
137
+
138
+ export { basis, bezier, cardinal, catmullRom, hermite, pointsToCoefficients, splines };
package/package.json CHANGED
@@ -1,25 +1,38 @@
1
1
  {
2
2
  "name": "curvy",
3
- "version": "0.1.1",
3
+ "version": "0.3.2",
4
4
  "description": "Spline and Curve utilities",
5
5
  "author": "Tim Morris <timthewebguy@gmail.com>",
6
6
  "homepage": "https://github.com/tkofh/curvy#readme",
7
7
  "license": "MIT",
8
8
  "sideEffects": false,
9
9
  "type": "module",
10
- "main": "./dist/index.cjs",
11
- "module": "./dist/index.mjs",
12
- "types": "./dist/index.d.ts",
10
+ "main": "./dist/curve.cjs",
11
+ "module": "./dist/curve.mjs",
12
+ "types": "./dist/curve.d.ts",
13
13
  "exports": {
14
14
  ".": {
15
- "require": "./dist/index.cjs",
16
- "import": "./dist/index.mjs",
17
- "types": "./dist/index.d.ts"
15
+ "require": "./dist/curve.cjs",
16
+ "import": "./dist/curve.mjs",
17
+ "types": "./dist/curve.d.ts"
18
+ },
19
+ "./axis": {
20
+ "require": "./dist/axis.cjs",
21
+ "import": "./dist/axis.mjs",
22
+ "types": "./dist/axis.d.ts"
23
+ },
24
+ "./convert": {
25
+ "require": "./dist/convert.cjs",
26
+ "import": "./dist/convert.mjs",
27
+ "types": "./dist/convert.d.ts"
28
+ },
29
+ "./splines": {
30
+ "require": "./dist/splines.cjs",
31
+ "import": "./dist/splines.mjs",
32
+ "types": "./dist/splines.d.ts"
18
33
  }
19
34
  },
20
- "files": [
21
- "dist"
22
- ],
35
+ "files": ["dist"],
23
36
  "publishConfig": {
24
37
  "access": "public"
25
38
  },
@@ -45,6 +58,5 @@
45
58
  },
46
59
  "dependencies": {
47
60
  "tiny-invariant": "^1.3.3"
48
- },
49
- "gitHead": "d2a3088660eb3cadd190c1c6c48ae7757cea9433"
61
+ }
50
62
  }
package/LICENSE.md DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2022 Tim Morris
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.