@matboks/utilities 0.0.1

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 (177) hide show
  1. package/dist/constants.d.ts +1 -0
  2. package/dist/constants.js +1 -0
  3. package/dist/geometry/aabb.d.ts +40 -0
  4. package/dist/geometry/aabb.js +143 -0
  5. package/dist/geometry/cut-polygon.d.ts +7 -0
  6. package/dist/geometry/cut-polygon.js +116 -0
  7. package/dist/geometry/hollowgon.d.ts +24 -0
  8. package/dist/geometry/hollowgon.js +156 -0
  9. package/dist/geometry/index.d.ts +13 -0
  10. package/dist/geometry/index.js +14 -0
  11. package/dist/geometry/line.d.ts +10 -0
  12. package/dist/geometry/line.js +26 -0
  13. package/dist/geometry/polygon-operations.d.ts +31 -0
  14. package/dist/geometry/polygon-operations.js +159 -0
  15. package/dist/geometry/polygon.d.ts +55 -0
  16. package/dist/geometry/polygon.js +353 -0
  17. package/dist/geometry/polyline.d.ts +23 -0
  18. package/dist/geometry/polyline.js +100 -0
  19. package/dist/geometry/ray.d.ts +6 -0
  20. package/dist/geometry/ray.js +6 -0
  21. package/dist/geometry/ray3.d.ts +7 -0
  22. package/dist/geometry/ray3.js +10 -0
  23. package/dist/geometry/segment.d.ts +16 -0
  24. package/dist/geometry/segment.js +50 -0
  25. package/dist/geometry/shared.d.ts +5 -0
  26. package/dist/geometry/shared.js +60 -0
  27. package/dist/geometry/transformations.d.ts +7 -0
  28. package/dist/geometry/transformations.js +28 -0
  29. package/dist/geometry/vec2.d.ts +71 -0
  30. package/dist/geometry/vec2.js +225 -0
  31. package/dist/geometry/vec3.d.ts +102 -0
  32. package/dist/geometry/vec3.js +256 -0
  33. package/dist/geometry/vec4.d.ts +71 -0
  34. package/dist/geometry/vec4.js +238 -0
  35. package/dist/index.d.ts +11 -0
  36. package/dist/index.js +11 -0
  37. package/dist/math/clamp.d.ts +1 -0
  38. package/dist/math/clamp.js +5 -0
  39. package/dist/math/fract.d.ts +1 -0
  40. package/dist/math/fract.js +3 -0
  41. package/dist/math/index.d.ts +8 -0
  42. package/dist/math/index.js +8 -0
  43. package/dist/math/mix.d.ts +1 -0
  44. package/dist/math/mix.js +3 -0
  45. package/dist/math/mod.d.ts +1 -0
  46. package/dist/math/mod.js +5 -0
  47. package/dist/math/signed.d.ts +1 -0
  48. package/dist/math/signed.js +3 -0
  49. package/dist/math/smoothstep.d.ts +3 -0
  50. package/dist/math/smoothstep.js +24 -0
  51. package/dist/math/split-into-int-and-fract.d.ts +1 -0
  52. package/dist/math/split-into-int-and-fract.js +5 -0
  53. package/dist/math/units.d.ts +2 -0
  54. package/dist/math/units.js +6 -0
  55. package/dist/noise/idw.d.ts +11 -0
  56. package/dist/noise/idw.js +10 -0
  57. package/dist/noise/index.d.ts +6 -0
  58. package/dist/noise/index.js +6 -0
  59. package/dist/noise/perlin.d.ts +5 -0
  60. package/dist/noise/perlin.js +29 -0
  61. package/dist/noise/random.d.ts +13 -0
  62. package/dist/noise/random.js +39 -0
  63. package/dist/noise/value.d.ts +4 -0
  64. package/dist/noise/value.js +20 -0
  65. package/dist/noise/voronoise.d.ts +10 -0
  66. package/dist/noise/voronoise.js +26 -0
  67. package/dist/noise/worley.d.ts +4 -0
  68. package/dist/noise/worley.js +39 -0
  69. package/dist/shader-modules/index.d.ts +2 -0
  70. package/dist/shader-modules/index.js +2 -0
  71. package/dist/shader-modules/library.d.ts +2 -0
  72. package/dist/shader-modules/library.js +36 -0
  73. package/dist/shader-modules/modules/camera.d.ts +1 -0
  74. package/dist/shader-modules/modules/camera.js +43 -0
  75. package/dist/shader-modules/modules/color/blend.d.ts +1 -0
  76. package/dist/shader-modules/modules/color/blend.js +108 -0
  77. package/dist/shader-modules/modules/color/index.d.ts +1 -0
  78. package/dist/shader-modules/modules/color/index.js +135 -0
  79. package/dist/shader-modules/modules/constants.d.ts +1 -0
  80. package/dist/shader-modules/modules/constants.js +14 -0
  81. package/dist/shader-modules/modules/geometry.d.ts +1 -0
  82. package/dist/shader-modules/modules/geometry.js +110 -0
  83. package/dist/shader-modules/modules/math.d.ts +1 -0
  84. package/dist/shader-modules/modules/math.js +19 -0
  85. package/dist/shader-modules/modules/noise.d.ts +1 -0
  86. package/dist/shader-modules/modules/noise.js +410 -0
  87. package/dist/shader-modules/modules/random.d.ts +1 -0
  88. package/dist/shader-modules/modules/random.js +147 -0
  89. package/dist/shader-modules/modules/ray-marching.d.ts +1 -0
  90. package/dist/shader-modules/modules/ray-marching.js +54 -0
  91. package/dist/shader-modules/modules/sdf/index.d.ts +1 -0
  92. package/dist/shader-modules/modules/sdf/index.js +183 -0
  93. package/dist/shader-modules/modules/sdf/operations.d.ts +1 -0
  94. package/dist/shader-modules/modules/sdf/operations.js +77 -0
  95. package/dist/shader-modules/modules/utils.d.ts +1 -0
  96. package/dist/shader-modules/modules/utils.js +115 -0
  97. package/dist/shader-modules/registry.d.ts +2 -0
  98. package/dist/shader-modules/registry.js +7 -0
  99. package/dist/shader-modules/shaders.d.ts +1 -0
  100. package/dist/shader-modules/shaders.js +109 -0
  101. package/dist/shader-renderer/helpers.d.ts +10 -0
  102. package/dist/shader-renderer/helpers.js +19 -0
  103. package/dist/shader-renderer/index.d.ts +2 -0
  104. package/dist/shader-renderer/index.js +2 -0
  105. package/dist/shader-renderer/pixel-shader.d.ts +32 -0
  106. package/dist/shader-renderer/pixel-shader.js +172 -0
  107. package/dist/shader-renderer/pixel-vertex-shader.d.ts +1 -0
  108. package/dist/shader-renderer/pixel-vertex-shader.js +14 -0
  109. package/dist/shader-renderer/texture.d.ts +28 -0
  110. package/dist/shader-renderer/texture.js +97 -0
  111. package/dist/shader-renderer/types.d.ts +17 -0
  112. package/dist/shader-renderer/types.js +4 -0
  113. package/dist/shader-renderer/uniform.d.ts +16 -0
  114. package/dist/shader-renderer/uniform.js +134 -0
  115. package/dist/tilings/delaunay.d.ts +11 -0
  116. package/dist/tilings/delaunay.js +28 -0
  117. package/dist/tilings/grid.d.ts +5 -0
  118. package/dist/tilings/grid.js +29 -0
  119. package/dist/tilings/hexagononal.d.ts +4 -0
  120. package/dist/tilings/hexagononal.js +40 -0
  121. package/dist/tilings/index.d.ts +12 -0
  122. package/dist/tilings/index.js +12 -0
  123. package/dist/tilings/line.d.ts +5 -0
  124. package/dist/tilings/line.js +19 -0
  125. package/dist/tilings/mediterranean.d.ts +3 -0
  126. package/dist/tilings/mediterranean.js +49 -0
  127. package/dist/tilings/pythagorean.d.ts +3 -0
  128. package/dist/tilings/pythagorean.js +40 -0
  129. package/dist/tilings/random-cuts.d.ts +6 -0
  130. package/dist/tilings/random-cuts.js +16 -0
  131. package/dist/tilings/recursive-cuts.d.ts +5 -0
  132. package/dist/tilings/recursive-cuts.js +11 -0
  133. package/dist/tilings/rhombille.d.ts +3 -0
  134. package/dist/tilings/rhombille.js +16 -0
  135. package/dist/tilings/rightangled-triangle.d.ts +3 -0
  136. package/dist/tilings/rightangled-triangle.js +29 -0
  137. package/dist/tilings/triangle.d.ts +3 -0
  138. package/dist/tilings/triangle.js +17 -0
  139. package/dist/tilings/voronoi.d.ts +11 -0
  140. package/dist/tilings/voronoi.js +29 -0
  141. package/dist/tilings/weaving.d.ts +1 -0
  142. package/dist/tilings/weaving.js +8 -0
  143. package/dist/transforms/camera/camera.d.ts +32 -0
  144. package/dist/transforms/camera/camera.js +60 -0
  145. package/dist/transforms/camera/index.d.ts +3 -0
  146. package/dist/transforms/camera/index.js +3 -0
  147. package/dist/transforms/camera/orthographic.d.ts +15 -0
  148. package/dist/transforms/camera/orthographic.js +31 -0
  149. package/dist/transforms/camera/perspective.d.ts +16 -0
  150. package/dist/transforms/camera/perspective.js +40 -0
  151. package/dist/transforms/index.d.ts +2 -0
  152. package/dist/transforms/index.js +2 -0
  153. package/dist/transforms/normalize-transform.d.ts +12 -0
  154. package/dist/transforms/normalize-transform.js +20 -0
  155. package/dist/types.d.ts +1 -0
  156. package/dist/types.js +1 -0
  157. package/dist/utilities/create-frame.d.ts +2 -0
  158. package/dist/utilities/create-frame.js +6 -0
  159. package/dist/utilities/ensure-array.d.ts +1 -0
  160. package/dist/utilities/ensure-array.js +3 -0
  161. package/dist/utilities/idw-interpolator.d.ts +9 -0
  162. package/dist/utilities/idw-interpolator.js +18 -0
  163. package/dist/utilities/index.d.ts +8 -0
  164. package/dist/utilities/index.js +8 -0
  165. package/dist/utilities/marching-squares.d.ts +51 -0
  166. package/dist/utilities/marching-squares.js +177 -0
  167. package/dist/utilities/point-sampler.d.ts +12 -0
  168. package/dist/utilities/point-sampler.js +24 -0
  169. package/dist/utilities/poisson/grid.d.ts +21 -0
  170. package/dist/utilities/poisson/grid.js +51 -0
  171. package/dist/utilities/poisson/poissonnier.d.ts +50 -0
  172. package/dist/utilities/poisson/poissonnier.js +118 -0
  173. package/dist/utilities/resolution.d.ts +10 -0
  174. package/dist/utilities/resolution.js +14 -0
  175. package/dist/utilities/rng.d.ts +13 -0
  176. package/dist/utilities/rng.js +80 -0
  177. package/package.json +28 -0
@@ -0,0 +1,100 @@
1
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
2
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
3
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
4
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
5
+ };
6
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
7
+ if (kind === "m") throw new TypeError("Private method is not writable");
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
9
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
10
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
11
+ };
12
+ var _Polyline_instances, _Polyline_edges, _Polyline_computeEdges, _Polyline_hull, _Polyline_center, _Polyline_aabb, _Polyline_modifyVertices;
13
+ import { AABB } from "./aabb.js";
14
+ import { Segment } from "./segment.js";
15
+ import { computeConvexHull } from "./shared.js";
16
+ export class Polyline {
17
+ edges() { return __classPrivateFieldSet(this, _Polyline_edges, __classPrivateFieldGet(this, _Polyline_edges, "f") ?? __classPrivateFieldGet(this, _Polyline_instances, "m", _Polyline_computeEdges).call(this), "f"); }
18
+ ;
19
+ hull() { return __classPrivateFieldSet(this, _Polyline_hull, __classPrivateFieldGet(this, _Polyline_hull, "f") ?? computeConvexHull(this.vertices), "f"); }
20
+ center() { return __classPrivateFieldSet(this, _Polyline_center, __classPrivateFieldGet(this, _Polyline_center, "f") ?? this.hull().centroid(), "f"); }
21
+ aabb() { return __classPrivateFieldSet(this, _Polyline_aabb, __classPrivateFieldGet(this, _Polyline_aabb, "f") ?? AABB.fromVertices(this.vertices), "f"); }
22
+ numVertices() { return this.vertices.length; }
23
+ constructor(vertices) {
24
+ _Polyline_instances.add(this);
25
+ _Polyline_edges.set(this, void 0);
26
+ _Polyline_hull.set(this, void 0);
27
+ _Polyline_center.set(this, void 0);
28
+ _Polyline_aabb.set(this, void 0);
29
+ this.vertices = vertices;
30
+ }
31
+ clone() {
32
+ const clonedVertices = this.vertices.map(vertex => vertex.clone());
33
+ return new Polyline(clonedVertices);
34
+ }
35
+ translate(translation) {
36
+ return __classPrivateFieldGet(this, _Polyline_instances, "m", _Polyline_modifyVertices).call(this, vertex => vertex.plus(translation));
37
+ }
38
+ rotate(rotation, center) {
39
+ center ?? (center = this.center());
40
+ return __classPrivateFieldGet(this, _Polyline_instances, "m", _Polyline_modifyVertices).call(this, vertex => vertex.rotate(rotation, center));
41
+ }
42
+ scale(scale, center) {
43
+ center ?? (center = this.center());
44
+ return __classPrivateFieldGet(this, _Polyline_instances, "m", _Polyline_modifyVertices).call(this, vertex => vertex.minus(center).times(scale).plus(center));
45
+ }
46
+ nearestPointOn(point) {
47
+ const nearestSegment = this.nearestSegmentToPoint(point);
48
+ return nearestSegment.getNearestPoint(point);
49
+ }
50
+ nearestSegmentToPoint(point) {
51
+ const edges = this.edges();
52
+ let nearestSegment = edges[0];
53
+ let nearestDistance = nearestSegment.distanceToPoint(point);
54
+ for (let i = 1, n = edges.length; i < n; i++) {
55
+ const segment = edges[i];
56
+ const currentDistance = segment.distanceToPoint(point);
57
+ if (currentDistance < nearestDistance) {
58
+ nearestDistance = currentDistance;
59
+ nearestSegment = segment;
60
+ }
61
+ }
62
+ return nearestSegment;
63
+ }
64
+ distanceToPoint(point) {
65
+ return point.distance(this.nearestPointOn(point));
66
+ }
67
+ render(ctx, beginPath = true) {
68
+ const { vertices } = this;
69
+ if (beginPath)
70
+ ctx.beginPath();
71
+ ctx.moveTo(vertices[0].x, vertices[0].y);
72
+ for (let i = 1, n = vertices.length; i < n; i++) {
73
+ const { x, y } = vertices[i];
74
+ ctx.lineTo(x, y);
75
+ }
76
+ }
77
+ resetMemoized() {
78
+ __classPrivateFieldSet(this, _Polyline_aabb, undefined, "f");
79
+ __classPrivateFieldSet(this, _Polyline_edges, undefined, "f");
80
+ __classPrivateFieldSet(this, _Polyline_center, undefined, "f");
81
+ __classPrivateFieldSet(this, _Polyline_hull, undefined, "f");
82
+ }
83
+ }
84
+ _Polyline_edges = new WeakMap(), _Polyline_hull = new WeakMap(), _Polyline_center = new WeakMap(), _Polyline_aabb = new WeakMap(), _Polyline_instances = new WeakSet(), _Polyline_computeEdges = function _Polyline_computeEdges() {
85
+ const { vertices } = this;
86
+ const edges = [];
87
+ for (let i = 0, n = vertices.length - 1; i < n; i++) {
88
+ edges.push(new Segment(vertices[i], vertices[i + 1]));
89
+ }
90
+ return edges;
91
+ }, _Polyline_modifyVertices = function _Polyline_modifyVertices(modification) {
92
+ if (typeof modification === "function") {
93
+ this.vertices.forEach(modification);
94
+ }
95
+ else {
96
+ this.vertices = modification;
97
+ }
98
+ this.resetMemoized();
99
+ return this;
100
+ };
@@ -0,0 +1,6 @@
1
+ import { Vec2 } from "./vec2.js";
2
+ export declare class Ray {
3
+ point: Vec2;
4
+ direction: Vec2;
5
+ constructor(point: Vec2, direction: Vec2);
6
+ }
@@ -0,0 +1,6 @@
1
+ export class Ray {
2
+ constructor(point, direction) {
3
+ this.point = point;
4
+ this.direction = direction;
5
+ }
6
+ }
@@ -0,0 +1,7 @@
1
+ import { Vec3 } from "./vec3.js";
2
+ export declare class Ray3 {
3
+ point: Vec3;
4
+ direction: Vec3;
5
+ constructor(point: Vec3, direction: Vec3);
6
+ interpolate(time: number): Vec3;
7
+ }
@@ -0,0 +1,10 @@
1
+ import { Vec3 } from "./vec3.js";
2
+ export class Ray3 {
3
+ constructor(point, direction) {
4
+ this.point = point;
5
+ this.direction = direction;
6
+ }
7
+ interpolate(time) {
8
+ return Vec3.add(this.point, Vec3.multiply(this.direction, time));
9
+ }
10
+ }
@@ -0,0 +1,16 @@
1
+ import { AABB } from "./aabb.js";
2
+ import { Vec2 } from "./vec2.js";
3
+ export declare class Segment {
4
+ #private;
5
+ readonly start: Vec2;
6
+ readonly end: Vec2;
7
+ readonly vector: Vec2;
8
+ aabb(): AABB;
9
+ constructor(start: Vec2, end: Vec2);
10
+ length(): number;
11
+ interpolate(position: number): Vec2;
12
+ scalarProjectOnto(position: Vec2): number;
13
+ getNearestPoint(position: Vec2): Vec2;
14
+ distanceToPoint(point: Vec2): number;
15
+ intersects(other: Segment): boolean;
16
+ }
@@ -0,0 +1,50 @@
1
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
2
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
3
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
4
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
5
+ };
6
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
7
+ if (kind === "m") throw new TypeError("Private method is not writable");
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
9
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
10
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
11
+ };
12
+ var _Segment_aabb;
13
+ import { EPS } from "../constants.js";
14
+ import { clamp } from "../math/clamp.js";
15
+ import { AABB } from "./aabb.js";
16
+ import { Vec2 } from "./vec2.js";
17
+ const { abs } = Math;
18
+ export class Segment {
19
+ aabb() { return __classPrivateFieldSet(this, _Segment_aabb, __classPrivateFieldGet(this, _Segment_aabb, "f") ?? AABB.fromVertices([this.start, this.end]), "f"); }
20
+ constructor(start, end) {
21
+ _Segment_aabb.set(this, void 0);
22
+ this.start = start;
23
+ this.end = end;
24
+ this.vector = Vec2.subtract(this.end, this.start);
25
+ }
26
+ length() { return this.vector.length(); }
27
+ interpolate(position) {
28
+ return Vec2.interpolate(this.start, this.end, clamp(position));
29
+ }
30
+ scalarProjectOnto(position) {
31
+ return Vec2.subtract(position, this.start).scalarProjectOnto(this.vector);
32
+ }
33
+ getNearestPoint(position) {
34
+ return this.interpolate(this.scalarProjectOnto(position));
35
+ }
36
+ distanceToPoint(point) {
37
+ const nearestPoint = this.getNearestPoint(point);
38
+ return point.distance(nearestPoint);
39
+ }
40
+ intersects(other) {
41
+ const cross = this.vector.cross(other.vector);
42
+ if (abs(cross) < EPS)
43
+ return false;
44
+ const startDifference = Vec2.subtract(other.start, this.start);
45
+ const time1 = startDifference.cross(other.vector) / cross;
46
+ const time2 = startDifference.cross(this.vector) / cross;
47
+ return time1 > 0 && time1 < 1 && time2 > 0 && time2 < 1;
48
+ }
49
+ }
50
+ _Segment_aabb = new WeakMap();
@@ -0,0 +1,5 @@
1
+ import { Line } from "./line.js";
2
+ import { Polygon } from "./polygon.js";
3
+ import { Vec2 } from "./vec2.js";
4
+ export declare function computeConvexHull(vertices: Vec2[]): Polygon;
5
+ export declare function computeLineIntersection(line1: Line, line2: Line): [number, number] | null;
@@ -0,0 +1,60 @@
1
+ import { EPS } from "../constants.js";
2
+ import { Polygon } from "./polygon.js";
3
+ import { Vec2 } from "./vec2.js";
4
+ export function computeConvexHull(vertices) {
5
+ let pivot = vertices[0];
6
+ const numVertices = vertices.length;
7
+ for (let i = 1; i < numVertices; i++) {
8
+ const vertex = vertices[i];
9
+ if (vertex.y < pivot.y || (Math.abs(vertex.y - pivot.y) < EPS && vertex.x > pivot.x)) {
10
+ pivot = vertex;
11
+ }
12
+ }
13
+ const indices = vertices.map((_, i) => i);
14
+ const angles = vertices.map((vertex) => Vec2.subtract(vertex, pivot).angle());
15
+ const distances = vertices.map((vertex) => vertex.distanceSquared(pivot));
16
+ indices.sort((i, j) => {
17
+ const angleI = angles[i];
18
+ const angleJ = angles[j];
19
+ if (Math.abs(angleI - angleJ) < EPS)
20
+ return distances[i] - distances[j];
21
+ return angleI - angleJ;
22
+ });
23
+ for (let i = 1; i < numVertices - 1; i++) {
24
+ if (angles[indices[i]] === angles[indices[i + 1]])
25
+ indices[i] = -1;
26
+ }
27
+ const hull = [];
28
+ for (let i = 0; i < numVertices; i++) {
29
+ const index = indices[i];
30
+ if (index === -1)
31
+ continue;
32
+ const vertex = vertices[index];
33
+ if (hull.length < 3) {
34
+ hull.push(vertex);
35
+ }
36
+ else {
37
+ while (true) {
38
+ const n = hull.length;
39
+ if (n < 3)
40
+ break;
41
+ const secondLast = hull[n - 2];
42
+ const last = hull[n - 1];
43
+ if (Vec2.subtract(last, secondLast).cross(Vec2.subtract(vertex, secondLast)) > 0)
44
+ break;
45
+ hull.pop();
46
+ }
47
+ hull.push(vertex);
48
+ }
49
+ }
50
+ return new Polygon(hull);
51
+ }
52
+ export function computeLineIntersection(line1, line2) {
53
+ const denominator = line1.direction.cross(line2.direction);
54
+ if (Math.abs(denominator) < EPS)
55
+ return null;
56
+ const positionDiff = Vec2.subtract(line2.point, line1.point);
57
+ let line1Time = positionDiff.cross(line2.direction) / denominator;
58
+ let line2Time = positionDiff.cross(line1.direction) / denominator;
59
+ return [line1Time, line2Time];
60
+ }
@@ -0,0 +1,7 @@
1
+ import { Vec2 } from "./vec2.js";
2
+ import { Vec3 } from "./vec3.js";
3
+ export declare function create2DBasis(normal: Vec3, rotation?: number): readonly [Vec3, Vec3];
4
+ export declare function create3DTo2DMapper(origin: Vec3, normal: Vec3): {
5
+ to2D: (v: Vec3) => Vec2;
6
+ to3D: (v: Vec2) => Vec3;
7
+ };
@@ -0,0 +1,28 @@
1
+ import { EPS } from "../constants.js";
2
+ import { vec2 } from "./vec2.js";
3
+ import { Vec3, vec3 } from "./vec3.js";
4
+ // Creates a coordinate system with unit vectors (i, j, k), where k is specified as normal
5
+ export function create2DBasis(normal, rotation) {
6
+ normal = normal.clone().normalize();
7
+ const i = normal.cross(Math.abs(normal.z) > (1 - EPS) ? vec3(1, 0, 0) : vec3(0, 0, 1)).normalize();
8
+ const j = normal.cross(i);
9
+ const vectors = [i, j];
10
+ if (rotation) {
11
+ for (const v of vectors)
12
+ v.axisAngleRotate(normal, rotation);
13
+ }
14
+ return vectors;
15
+ }
16
+ export function create3DTo2DMapper(origin, normal) {
17
+ const [i, j] = create2DBasis(normal);
18
+ const to2D = (v) => {
19
+ const vCentered = Vec3.subtract(v, origin);
20
+ return vec2(i.dot(vCentered), j.dot(vCentered));
21
+ };
22
+ const to3D = (v) => {
23
+ return Vec3.multiply(i, v.x).plus(Vec3.multiply(j, v.y)).plus(origin);
24
+ };
25
+ return {
26
+ to2D, to3D
27
+ };
28
+ }
@@ -0,0 +1,71 @@
1
+ export declare function vec2(vector: Vec2): Vec2;
2
+ export declare function vec2(x: number, y?: number): Vec2;
3
+ export declare class Vec2 {
4
+ x: number;
5
+ y: number;
6
+ length(): number;
7
+ lengthSquared(): number;
8
+ angle(): number;
9
+ get xx(): Vec2;
10
+ get xy(): Vec2;
11
+ get yx(): Vec2;
12
+ get yy(): Vec2;
13
+ constructor(vector: Vec2);
14
+ constructor(x: number, y?: number);
15
+ static fromPolar: (radius: number, angle: number) => Vec2;
16
+ static add(left: Vec2, ...right: Array<Vec2>): Vec2;
17
+ static subtract(left: Vec2, right: Vec2): Vec2;
18
+ static multiply(vector: Vec2, value: number | Vec2): Vec2;
19
+ static dot(a: Vec2, b: Vec2): number;
20
+ static cross(a: Vec2, b: Vec2): number;
21
+ static interpolate(start: Vec2, end: Vec2, position: number): Vec2;
22
+ static midpoint(start: Vec2, end: Vec2): Vec2;
23
+ static absolute(vector: Vec2): Vec2;
24
+ static distance(a: Vec2, b: Vec2): number;
25
+ static angle(start: Vec2, end: Vec2): number;
26
+ static bisector(vector1: Vec2, vector2: Vec2): Vec2;
27
+ static toBuffer(vertices: Array<Vec2>): number[];
28
+ static smoothstep(vector: Vec2, lower?: number | Vec2, upper?: number | Vec2): Vec2;
29
+ setX(x: number): this;
30
+ setY(y: number): this;
31
+ setCoordinates(vector: Vec2): Vec2;
32
+ setCoordinates(x: number, y?: number): Vec2;
33
+ clone(): Vec2;
34
+ copy(vector: Vec2): Vec2;
35
+ plus(vector: Vec2): Vec2;
36
+ minus(vector: Vec2): Vec2;
37
+ times(value: number | Vec2): Vec2;
38
+ distance(vector: Vec2): number;
39
+ distanceSquared(vector: Vec2): number;
40
+ perpendicular(): Vec2;
41
+ rotate(rotation: number, centerOfRotation?: Vec2): this;
42
+ dot(vector: Vec2): number;
43
+ cross(vector: Vec2): number;
44
+ scalarProjectOnto(target: Vec2): number;
45
+ projectOnto(target: Vec2): Vec2;
46
+ rejectOnto(target: Vec2): Vec2;
47
+ scalarRejectOnto(target: Vec2): number;
48
+ reflectByVector(vector: Vec2): Vec2;
49
+ reflectByPoint(point: Vec2): Vec2;
50
+ normalize(): Vec2;
51
+ absolute(): Vec2;
52
+ negate(): Vec2;
53
+ floor(): Vec2;
54
+ round(): Vec2;
55
+ fract(): Vec2;
56
+ signed(): Vec2;
57
+ toVec3(z?: number): import("./vec3.js").Vec3;
58
+ sum(): number;
59
+ product(): number;
60
+ smoothstep(lower?: number | Vec2, upper?: number | Vec2): Vec2;
61
+ min(): number;
62
+ max(): number;
63
+ mod(modulo: number | Vec2): Vec2;
64
+ clamp(lower?: number | Vec2, upper?: number | Vec2): Vec2;
65
+ pow(power: number | Vec2): Vec2;
66
+ reflect(vector: Vec2): Vec2;
67
+ splitIntoIntAndFract(): [Vec2, Vec2];
68
+ toString(precision?: number): string;
69
+ toArray(): [number, number];
70
+ }
71
+ export declare function ensureVec2(value: number | Vec2): Vec2;
@@ -0,0 +1,225 @@
1
+ import { clamp, fract, smoothstep, mod, splitIntoIntAndFract } from "../math/index.js";
2
+ import { vec3 } from "./vec3.js";
3
+ const { cos, sin, abs, floor, round, hypot, atan2, min, max, pow, PI } = Math;
4
+ export function vec2(...args) {
5
+ return new Vec2(args[0], args[1]);
6
+ }
7
+ export class Vec2 {
8
+ length() { return hypot(this.x, this.y); }
9
+ lengthSquared() { const x = this.x, y = this.y; return x * x + y * y; }
10
+ angle() { return atan2(this.y, this.x); }
11
+ get xx() { return vec2(this.x); }
12
+ get xy() { return vec2(this.x, this.y); }
13
+ get yx() { return vec2(this.y, this.x); }
14
+ get yy() { return vec2(this.y); }
15
+ constructor(...args) {
16
+ this.setCoordinates(args[0], args[1]);
17
+ }
18
+ static add(left, ...right) {
19
+ left = left.clone();
20
+ for (let vector of right)
21
+ left.plus(vector);
22
+ return left;
23
+ }
24
+ static subtract(left, right) {
25
+ return left.clone().minus(right);
26
+ }
27
+ static multiply(vector, value) {
28
+ return vector.clone().times(value);
29
+ }
30
+ static dot(a, b) {
31
+ return a.dot(b);
32
+ }
33
+ static cross(a, b) {
34
+ return a.cross(b);
35
+ }
36
+ static interpolate(start, end, position) {
37
+ return start.clone().times(1 - position).plus(Vec2.multiply(end, position));
38
+ }
39
+ static midpoint(start, end) {
40
+ return Vec2.interpolate(start, end, 0.5);
41
+ }
42
+ static absolute(vector) {
43
+ return vector.clone().absolute();
44
+ }
45
+ static distance(a, b) {
46
+ return Vec2.subtract(b, a).length();
47
+ }
48
+ static angle(start, end) {
49
+ return mod(end.angle() - start.angle() + PI, 2 * PI) - PI; // returns in range [-PI, PI]
50
+ }
51
+ static bisector(vector1, vector2) {
52
+ const angle = Vec2.angle(vector1, vector2);
53
+ return vector1.clone().rotate(angle / 2);
54
+ }
55
+ static toBuffer(vertices) {
56
+ const data = [];
57
+ for (const vertex of vertices) {
58
+ data.push(vertex.x, vertex.y);
59
+ }
60
+ return data;
61
+ }
62
+ static smoothstep(vector, lower = 0, upper = 1) {
63
+ return vector.clone().smoothstep(lower, upper);
64
+ }
65
+ setX(x) {
66
+ this.x = x;
67
+ return this;
68
+ }
69
+ setY(y) {
70
+ this.y = y;
71
+ return this;
72
+ }
73
+ setCoordinates(...args) {
74
+ const [a, b] = args;
75
+ if (typeof a === "object") {
76
+ this.x = a.x;
77
+ this.y = a.y;
78
+ }
79
+ else {
80
+ this.x = a;
81
+ this.y = b ?? a;
82
+ }
83
+ return this;
84
+ }
85
+ clone() {
86
+ return vec2(this.x, this.y);
87
+ }
88
+ copy(vector) {
89
+ return this.setCoordinates(vector);
90
+ }
91
+ plus(vector) {
92
+ return this.setCoordinates(this.x + vector.x, this.y + vector.y);
93
+ }
94
+ minus(vector) {
95
+ return this.setCoordinates(this.x - vector.x, this.y - vector.y);
96
+ }
97
+ times(value) {
98
+ value = ensureVec2(value);
99
+ return this.setCoordinates(value.x * this.x, value.y * this.y);
100
+ }
101
+ distance(vector) {
102
+ return hypot(vector.x - this.x, vector.y - this.y);
103
+ }
104
+ distanceSquared(vector) {
105
+ let dx = vector.x - this.x;
106
+ let dy = vector.y - this.y;
107
+ return dx * dx + dy * dy;
108
+ }
109
+ perpendicular() {
110
+ return vec2(this.y, -this.x);
111
+ }
112
+ rotate(rotation, centerOfRotation) {
113
+ if (centerOfRotation)
114
+ this.minus(centerOfRotation);
115
+ let x = this.x, y = this.y;
116
+ const c = cos(rotation);
117
+ const s = sin(rotation);
118
+ this.setCoordinates(x * c - y * s, x * s + y * c);
119
+ if (centerOfRotation)
120
+ this.plus(centerOfRotation);
121
+ return this;
122
+ }
123
+ dot(vector) {
124
+ return this.x * vector.x + this.y * vector.y;
125
+ }
126
+ cross(vector) {
127
+ return this.x * vector.y - vector.x * this.y;
128
+ }
129
+ scalarProjectOnto(target) {
130
+ return this.dot(target) / target.dot(target);
131
+ }
132
+ projectOnto(target) {
133
+ return target.clone().times(this.scalarProjectOnto(target));
134
+ }
135
+ rejectOnto(target) {
136
+ return Vec2.subtract(this, this.projectOnto(target));
137
+ }
138
+ scalarRejectOnto(target) {
139
+ return this.rejectOnto(target).length();
140
+ }
141
+ reflectByVector(vector) {
142
+ const rejection = this.rejectOnto(vector);
143
+ return this.clone().minus(rejection.times(2));
144
+ }
145
+ reflectByPoint(point) {
146
+ const translation = Vec2.subtract(point, this).times(2);
147
+ return this.clone().plus(translation);
148
+ }
149
+ normalize() {
150
+ return this.times(1 / this.length());
151
+ }
152
+ absolute() {
153
+ return this.setCoordinates(abs(this.x), abs(this.y));
154
+ }
155
+ negate() {
156
+ return this.times(-1);
157
+ }
158
+ floor() {
159
+ return this.setCoordinates(floor(this.x), floor(this.y));
160
+ }
161
+ round() {
162
+ return this.setCoordinates(round(this.x), round(this.y));
163
+ }
164
+ fract() {
165
+ return this.setCoordinates(fract(this.x), fract(this.y));
166
+ }
167
+ signed() {
168
+ return this.times(2).plus(vec2(-1));
169
+ }
170
+ toVec3(z) {
171
+ return vec3(this.x, this.y, z);
172
+ }
173
+ sum() {
174
+ return this.x + this.y;
175
+ }
176
+ product() {
177
+ return this.x * this.y;
178
+ }
179
+ smoothstep(lower = 0, upper = 1) {
180
+ lower = ensureVec2(lower);
181
+ upper = ensureVec2(upper);
182
+ return this.setCoordinates(smoothstep(this.x, lower.x, upper.x), smoothstep(this.y, lower.y, upper.y));
183
+ }
184
+ min() {
185
+ return min(this.x, this.y);
186
+ }
187
+ max() {
188
+ return max(this.x, this.y);
189
+ }
190
+ mod(modulo) {
191
+ modulo = ensureVec2(modulo);
192
+ return this.setCoordinates(mod(this.x, modulo.y), mod(this.y, modulo.x));
193
+ }
194
+ clamp(lower = 0, upper = 1) {
195
+ lower = ensureVec2(lower);
196
+ upper = ensureVec2(upper);
197
+ return this.setCoordinates(clamp(this.x, lower.x, upper.x), clamp(this.y, lower.y, upper.y));
198
+ }
199
+ pow(power) {
200
+ power = ensureVec2(power);
201
+ return this.setCoordinates(pow(this.x, power.x), pow(this.y, power.y));
202
+ }
203
+ reflect(vector) {
204
+ const normalizedVector = vector.clone().normalize();
205
+ const reflectedVector = Vec2.subtract(this, Vec2.multiply(normalizedVector, 2 * this.dot(normalizedVector)));
206
+ return this.setCoordinates(reflectedVector);
207
+ }
208
+ splitIntoIntAndFract() {
209
+ const [xi, xf] = splitIntoIntAndFract(this.x);
210
+ const [yi, yf] = splitIntoIntAndFract(this.y);
211
+ return [vec2(xi, yi), vec2(xf, yf)];
212
+ }
213
+ toString(precision = 3) {
214
+ return `(${this.x.toFixed(precision)}, ${this.y.toFixed(precision)})`;
215
+ }
216
+ toArray() {
217
+ return [this.x, this.y];
218
+ }
219
+ }
220
+ Vec2.fromPolar = function (radius, angle) {
221
+ return vec2(radius * cos(angle), radius * sin(angle));
222
+ };
223
+ export function ensureVec2(value) {
224
+ return value = typeof value === "number" ? vec2(value) : value;
225
+ }