okgeometry-api 1.26.1 → 1.27.0

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 (99) hide show
  1. package/dist/Arc.js +1 -1
  2. package/dist/Arc.js.map +1 -1
  3. package/dist/Brep.js +1 -1
  4. package/dist/Brep.js.map +1 -1
  5. package/dist/Circle.js +1 -1
  6. package/dist/Circle.js.map +1 -1
  7. package/dist/Line.js +1 -1
  8. package/dist/Line.js.map +1 -1
  9. package/dist/Mesh.d.ts +41 -0
  10. package/dist/Mesh.d.ts.map +1 -1
  11. package/dist/Mesh.js +235 -21
  12. package/dist/Mesh.js.map +1 -1
  13. package/dist/MeshSurface.js +1 -1
  14. package/dist/MeshSurface.js.map +1 -1
  15. package/dist/NurbsCurve.js +1 -1
  16. package/dist/NurbsCurve.js.map +1 -1
  17. package/dist/NurbsSurface.js +1 -1
  18. package/dist/NurbsSurface.js.map +1 -1
  19. package/dist/PolyCurve.js +1 -1
  20. package/dist/PolyCurve.js.map +1 -1
  21. package/dist/Polyline.js +1 -1
  22. package/dist/Polyline.js.map +1 -1
  23. package/dist/Ray.js +1 -1
  24. package/dist/Ray.js.map +1 -1
  25. package/dist/SculptSession.js +1 -1
  26. package/dist/SculptSession.js.map +1 -1
  27. package/dist/engine-threaded.d.ts +2 -0
  28. package/dist/engine-threaded.d.ts.map +1 -0
  29. package/dist/engine-threaded.js +33 -0
  30. package/dist/engine-threaded.js.map +1 -0
  31. package/dist/engine.d.ts +21 -0
  32. package/dist/engine.d.ts.map +1 -1
  33. package/dist/engine.js +42 -0
  34. package/dist/engine.js.map +1 -1
  35. package/dist/index.d.ts +1 -1
  36. package/dist/index.d.ts.map +1 -1
  37. package/dist/index.js +1 -1
  38. package/dist/index.js.map +1 -1
  39. package/dist/mesh-boolean.pool.d.ts +23 -1
  40. package/dist/mesh-boolean.pool.d.ts.map +1 -1
  41. package/dist/mesh-boolean.pool.js +97 -12
  42. package/dist/mesh-boolean.pool.js.map +1 -1
  43. package/dist/mesh-boolean.protocol.d.ts +21 -1
  44. package/dist/mesh-boolean.protocol.d.ts.map +1 -1
  45. package/dist/mesh-boolean.protocol.js.map +1 -1
  46. package/dist/mesh-boolean.worker.d.ts.map +1 -1
  47. package/dist/mesh-boolean.worker.js +94 -1
  48. package/dist/mesh-boolean.worker.js.map +1 -1
  49. package/dist/wasm-active.d.ts +20 -0
  50. package/dist/wasm-active.d.ts.map +1 -0
  51. package/dist/wasm-active.js +26 -0
  52. package/dist/wasm-active.js.map +1 -0
  53. package/dist/wasm-base64-threaded.d.ts +2 -0
  54. package/dist/wasm-base64-threaded.d.ts.map +1 -0
  55. package/dist/wasm-base64-threaded.js +3 -0
  56. package/dist/wasm-base64-threaded.js.map +1 -0
  57. package/dist/wasm-base64.d.ts +1 -1
  58. package/dist/wasm-base64.d.ts.map +1 -1
  59. package/dist/wasm-base64.js +1 -1
  60. package/dist/wasm-base64.js.map +1 -1
  61. package/dist/wasm-bindings-threaded.d.ts +2664 -0
  62. package/dist/wasm-bindings-threaded.d.ts.map +1 -0
  63. package/dist/wasm-bindings-threaded.js +5008 -0
  64. package/dist/wasm-bindings-threaded.js.map +1 -0
  65. package/dist/wasm-bindings.d.ts +130 -0
  66. package/dist/wasm-bindings.d.ts.map +1 -1
  67. package/dist/wasm-bindings.js +208 -0
  68. package/dist/wasm-bindings.js.map +1 -1
  69. package/dist/workerHelpers-threaded.d.ts +2 -0
  70. package/dist/workerHelpers-threaded.d.ts.map +1 -0
  71. package/dist/workerHelpers-threaded.js +100 -0
  72. package/dist/workerHelpers-threaded.js.map +1 -0
  73. package/package.json +8 -3
  74. package/src/Arc.ts +117 -117
  75. package/src/Brep.ts +1 -1
  76. package/src/Circle.ts +166 -166
  77. package/src/Line.ts +171 -171
  78. package/src/Mesh.ts +300 -45
  79. package/src/MeshSurface.ts +1 -1
  80. package/src/NurbsCurve.ts +655 -655
  81. package/src/NurbsSurface.ts +721 -721
  82. package/src/PolyCurve.ts +203 -203
  83. package/src/Polyline.ts +316 -316
  84. package/src/Ray.ts +90 -90
  85. package/src/SculptSession.ts +377 -377
  86. package/src/engine-threaded.ts +39 -0
  87. package/src/engine.ts +46 -0
  88. package/src/index.ts +1 -1
  89. package/src/mesh-boolean.pool.ts +208 -86
  90. package/src/mesh-boolean.protocol.ts +44 -1
  91. package/src/mesh-boolean.worker.ts +170 -56
  92. package/src/wasm-active.ts +31 -0
  93. package/src/wasm-base64-threaded.ts +2 -0
  94. package/src/wasm-base64.ts +1 -1
  95. package/src/wasm-bindings-threaded.d.ts +1989 -0
  96. package/src/wasm-bindings-threaded.js +5235 -0
  97. package/src/wasm-bindings.d.ts +104 -0
  98. package/src/wasm-bindings.js +225 -0
  99. package/src/workerHelpers-threaded.js +107 -0
package/src/Circle.ts CHANGED
@@ -1,166 +1,166 @@
1
- import { ensureInit } from "./engine.js";
2
- import { Point } from "./Point.js";
3
- import { Vec3 } from "./Vec3.js";
4
- import { Mesh } from "./Mesh.js";
5
- import { Polygon } from "./Polygon.js";
6
- import { Plane } from "./Plane.js";
7
- import type { RotationAxis } from "./types.js";
8
- import * as wasm from "./wasm-bindings.js";
9
-
10
- /**
11
- * Immutable circle defined by center, radius, and normal.
12
- * Supports evaluation, transformation, extrusion, and projection.
13
- */
14
- export class Circle {
15
- /**
16
- * Create a new circle.
17
- * @param center - Center point
18
- * @param radius - Circle radius
19
- * @param normal - Normal vector defining the circle's plane (default Z axis)
20
- * @param uAxis - Optional reference direction defining where t=0 starts.
21
- * Critical for sweep operations to maintain consistent profile orientation.
22
- */
23
- constructor(
24
- public readonly center: Point,
25
- public readonly radius: number,
26
- public readonly normal: Vec3 = Vec3.Z,
27
- public readonly uAxis?: Vec3
28
- ) {}
29
-
30
- /**
31
- * Create a Circle from a Plane, automatically using the plane's xAxis
32
- * as the circle's uAxis for consistent orientation in sweep operations.
33
- * @param plane - Plane defining circle's position and orientation
34
- * @param radius - Circle radius
35
- * @returns New Circle at plane origin with plane normal
36
- */
37
- static fromPlane(plane: Plane, radius: number): Circle {
38
- return new Circle(plane.origin, radius, plane.normal, plane.getXAxis());
39
- }
40
-
41
- /**
42
- * Evaluate a point on the circle at normalized parameter t.
43
- * @param t - Parameter in [0, 1] (0 and 1 are the same point for closed circle)
44
- * @returns Point on circle at parameter t
45
- */
46
- pointAt(t: number): Point {
47
- ensureInit();
48
- const r = wasm.circle_point_at(
49
- this.center.x, this.center.y, this.center.z,
50
- this.normal.x, this.normal.y, this.normal.z,
51
- this.radius, t
52
- );
53
- return new Point(r[0], r[1], r[2]);
54
- }
55
-
56
- /**
57
- * Get the circumference of this circle.
58
- * @returns 2 * PI * radius
59
- */
60
- length(): number {
61
- ensureInit();
62
- return wasm.circle_length(this.radius);
63
- }
64
-
65
- /**
66
- * Sample the circle into evenly-spaced points across the full closed
67
- * parameter range INCLUSIVE of both ends: the last point coincides with
68
- * the first (the seam). Consumers building a closed polygon from a circle
69
- * must drop the final duplicate (see Mesh.buildPlanarCircle).
70
- * @param n - Number of points to generate
71
- * @returns Array of n points around the circle, first == last
72
- */
73
- sample(n: number): Point[] {
74
- ensureInit();
75
- const buf = wasm.create_circle(
76
- this.center.x, this.center.y, this.center.z,
77
- this.normal.x, this.normal.y, this.normal.z,
78
- this.radius, n
79
- );
80
- const pts: Point[] = [];
81
- for (let i = 0; i < buf.length; i += 3) {
82
- pts.push(new Point(buf[i], buf[i + 1], buf[i + 2]));
83
- }
84
- return pts;
85
- }
86
-
87
- /**
88
- * Translate this circle by an offset vector.
89
- * @param offset - Translation vector
90
- * @returns New translated circle
91
- */
92
- translate(offset: Vec3): Circle {
93
- return new Circle(this.center.add(offset), this.radius, this.normal, this.uAxis);
94
- }
95
-
96
- /**
97
- * Rotate this circle around an axis.
98
- * @param axis - Rotation axis (Vec3 through origin, or Line for arbitrary axis)
99
- * @param angle - Rotation angle in radians
100
- * @returns New rotated circle
101
- */
102
- rotate(axis: RotationAxis, angle: number): Circle {
103
- const n = this.normal;
104
- // Normal is a direction — always rotate around origin regardless of axis type
105
- const dirAxis = 'start' in axis ? new Vec3(axis.end.x - axis.start.x, axis.end.y - axis.start.y, axis.end.z - axis.start.z) : axis;
106
- const rn = new Point(n.x, n.y, n.z).rotate(dirAxis, angle);
107
- // Also rotate the uAxis if present
108
- let rotatedUAxis: Vec3 | undefined;
109
- if (this.uAxis) {
110
- const ru = new Point(this.uAxis.x, this.uAxis.y, this.uAxis.z).rotate(dirAxis, angle);
111
- rotatedUAxis = new Vec3(ru.x, ru.y, ru.z);
112
- }
113
- return new Circle(this.center.rotate(axis, angle), this.radius, new Vec3(rn.x, rn.y, rn.z), rotatedUAxis);
114
- }
115
-
116
- /**
117
- * Offset this circle by changing its radius.
118
- * @param distance - Distance to offset (positive = larger, negative = smaller)
119
- * @returns New circle with adjusted radius
120
- */
121
- offset(distance: number): Circle {
122
- return new Circle(this.center, this.radius + distance, this.normal, this.uAxis);
123
- }
124
-
125
- /**
126
- * Project onto plane. Returns a Polygon because a tilted circle projects to an ellipse.
127
- * @param plane - Target plane
128
- * @param direction - Optional projection direction (default: perpendicular to plane)
129
- * @param samples - Number of sample points for approximation (default 64)
130
- * @returns Polygon approximating the projected circle
131
- */
132
- projectOntoPlane(plane: Plane, direction?: Vec3, samples = 64): Polygon {
133
- const pts = this.sample(samples);
134
- const proj = (p: Point) => direction ? plane.projectPointAlongDirection(p, direction) : plane.projectPoint(p);
135
- return new Polygon(pts.map(proj));
136
- }
137
-
138
- /**
139
- * Extrude this circle along a direction vector to create a cylindrical mesh.
140
- * @param direction - Extrusion direction and magnitude
141
- * @param segments - Number of circumferential segments (default 16)
142
- * @param caps - Whether to cap the ends (default true)
143
- * @returns Mesh representing the extruded cylinder
144
- */
145
- extrude(direction: Vec3, segments = 16, caps = true): Mesh {
146
- ensureInit();
147
- const buf = wasm.extrude_circle(
148
- this.center.x, this.center.y, this.center.z,
149
- this.normal.x, this.normal.y, this.normal.z,
150
- this.radius,
151
- direction.x, direction.y, direction.z,
152
- segments, caps
153
- );
154
- return Mesh.fromBuffer(buf);
155
- }
156
-
157
- /**
158
- * Extrude this closed circle into a trusted solid suitable for booleans.
159
- * @param direction - Extrusion direction and magnitude
160
- * @param segments - Number of boundary samples used for tessellation (default 32)
161
- * @returns Closed mesh solid ready for boolean operations
162
- */
163
- extrudeAsSolid(direction: Vec3, segments = 32): Mesh {
164
- return Mesh.extrudeCurveAsSolid(this, direction, segments);
165
- }
166
- }
1
+ import { ensureInit } from "./engine.js";
2
+ import { Point } from "./Point.js";
3
+ import { Vec3 } from "./Vec3.js";
4
+ import { Mesh } from "./Mesh.js";
5
+ import { Polygon } from "./Polygon.js";
6
+ import { Plane } from "./Plane.js";
7
+ import type { RotationAxis } from "./types.js";
8
+ import { wasm } from "./wasm-active.js";
9
+
10
+ /**
11
+ * Immutable circle defined by center, radius, and normal.
12
+ * Supports evaluation, transformation, extrusion, and projection.
13
+ */
14
+ export class Circle {
15
+ /**
16
+ * Create a new circle.
17
+ * @param center - Center point
18
+ * @param radius - Circle radius
19
+ * @param normal - Normal vector defining the circle's plane (default Z axis)
20
+ * @param uAxis - Optional reference direction defining where t=0 starts.
21
+ * Critical for sweep operations to maintain consistent profile orientation.
22
+ */
23
+ constructor(
24
+ public readonly center: Point,
25
+ public readonly radius: number,
26
+ public readonly normal: Vec3 = Vec3.Z,
27
+ public readonly uAxis?: Vec3
28
+ ) {}
29
+
30
+ /**
31
+ * Create a Circle from a Plane, automatically using the plane's xAxis
32
+ * as the circle's uAxis for consistent orientation in sweep operations.
33
+ * @param plane - Plane defining circle's position and orientation
34
+ * @param radius - Circle radius
35
+ * @returns New Circle at plane origin with plane normal
36
+ */
37
+ static fromPlane(plane: Plane, radius: number): Circle {
38
+ return new Circle(plane.origin, radius, plane.normal, plane.getXAxis());
39
+ }
40
+
41
+ /**
42
+ * Evaluate a point on the circle at normalized parameter t.
43
+ * @param t - Parameter in [0, 1] (0 and 1 are the same point for closed circle)
44
+ * @returns Point on circle at parameter t
45
+ */
46
+ pointAt(t: number): Point {
47
+ ensureInit();
48
+ const r = wasm.circle_point_at(
49
+ this.center.x, this.center.y, this.center.z,
50
+ this.normal.x, this.normal.y, this.normal.z,
51
+ this.radius, t
52
+ );
53
+ return new Point(r[0], r[1], r[2]);
54
+ }
55
+
56
+ /**
57
+ * Get the circumference of this circle.
58
+ * @returns 2 * PI * radius
59
+ */
60
+ length(): number {
61
+ ensureInit();
62
+ return wasm.circle_length(this.radius);
63
+ }
64
+
65
+ /**
66
+ * Sample the circle into evenly-spaced points across the full closed
67
+ * parameter range INCLUSIVE of both ends: the last point coincides with
68
+ * the first (the seam). Consumers building a closed polygon from a circle
69
+ * must drop the final duplicate (see Mesh.buildPlanarCircle).
70
+ * @param n - Number of points to generate
71
+ * @returns Array of n points around the circle, first == last
72
+ */
73
+ sample(n: number): Point[] {
74
+ ensureInit();
75
+ const buf = wasm.create_circle(
76
+ this.center.x, this.center.y, this.center.z,
77
+ this.normal.x, this.normal.y, this.normal.z,
78
+ this.radius, n
79
+ );
80
+ const pts: Point[] = [];
81
+ for (let i = 0; i < buf.length; i += 3) {
82
+ pts.push(new Point(buf[i], buf[i + 1], buf[i + 2]));
83
+ }
84
+ return pts;
85
+ }
86
+
87
+ /**
88
+ * Translate this circle by an offset vector.
89
+ * @param offset - Translation vector
90
+ * @returns New translated circle
91
+ */
92
+ translate(offset: Vec3): Circle {
93
+ return new Circle(this.center.add(offset), this.radius, this.normal, this.uAxis);
94
+ }
95
+
96
+ /**
97
+ * Rotate this circle around an axis.
98
+ * @param axis - Rotation axis (Vec3 through origin, or Line for arbitrary axis)
99
+ * @param angle - Rotation angle in radians
100
+ * @returns New rotated circle
101
+ */
102
+ rotate(axis: RotationAxis, angle: number): Circle {
103
+ const n = this.normal;
104
+ // Normal is a direction — always rotate around origin regardless of axis type
105
+ const dirAxis = 'start' in axis ? new Vec3(axis.end.x - axis.start.x, axis.end.y - axis.start.y, axis.end.z - axis.start.z) : axis;
106
+ const rn = new Point(n.x, n.y, n.z).rotate(dirAxis, angle);
107
+ // Also rotate the uAxis if present
108
+ let rotatedUAxis: Vec3 | undefined;
109
+ if (this.uAxis) {
110
+ const ru = new Point(this.uAxis.x, this.uAxis.y, this.uAxis.z).rotate(dirAxis, angle);
111
+ rotatedUAxis = new Vec3(ru.x, ru.y, ru.z);
112
+ }
113
+ return new Circle(this.center.rotate(axis, angle), this.radius, new Vec3(rn.x, rn.y, rn.z), rotatedUAxis);
114
+ }
115
+
116
+ /**
117
+ * Offset this circle by changing its radius.
118
+ * @param distance - Distance to offset (positive = larger, negative = smaller)
119
+ * @returns New circle with adjusted radius
120
+ */
121
+ offset(distance: number): Circle {
122
+ return new Circle(this.center, this.radius + distance, this.normal, this.uAxis);
123
+ }
124
+
125
+ /**
126
+ * Project onto plane. Returns a Polygon because a tilted circle projects to an ellipse.
127
+ * @param plane - Target plane
128
+ * @param direction - Optional projection direction (default: perpendicular to plane)
129
+ * @param samples - Number of sample points for approximation (default 64)
130
+ * @returns Polygon approximating the projected circle
131
+ */
132
+ projectOntoPlane(plane: Plane, direction?: Vec3, samples = 64): Polygon {
133
+ const pts = this.sample(samples);
134
+ const proj = (p: Point) => direction ? plane.projectPointAlongDirection(p, direction) : plane.projectPoint(p);
135
+ return new Polygon(pts.map(proj));
136
+ }
137
+
138
+ /**
139
+ * Extrude this circle along a direction vector to create a cylindrical mesh.
140
+ * @param direction - Extrusion direction and magnitude
141
+ * @param segments - Number of circumferential segments (default 16)
142
+ * @param caps - Whether to cap the ends (default true)
143
+ * @returns Mesh representing the extruded cylinder
144
+ */
145
+ extrude(direction: Vec3, segments = 16, caps = true): Mesh {
146
+ ensureInit();
147
+ const buf = wasm.extrude_circle(
148
+ this.center.x, this.center.y, this.center.z,
149
+ this.normal.x, this.normal.y, this.normal.z,
150
+ this.radius,
151
+ direction.x, direction.y, direction.z,
152
+ segments, caps
153
+ );
154
+ return Mesh.fromBuffer(buf);
155
+ }
156
+
157
+ /**
158
+ * Extrude this closed circle into a trusted solid suitable for booleans.
159
+ * @param direction - Extrusion direction and magnitude
160
+ * @param segments - Number of boundary samples used for tessellation (default 32)
161
+ * @returns Closed mesh solid ready for boolean operations
162
+ */
163
+ extrudeAsSolid(direction: Vec3, segments = 32): Mesh {
164
+ return Mesh.extrudeCurveAsSolid(this, direction, segments);
165
+ }
166
+ }
package/src/Line.ts CHANGED
@@ -1,171 +1,171 @@
1
- import { ensureInit } from "./engine.js";
2
- import { Point } from "./Point.js";
3
- import { Vec3 } from "./Vec3.js";
4
- import { Mesh } from "./Mesh.js";
5
- import { PolyCurve } from "./PolyCurve.js";
6
- import { Polyline } from "./Polyline.js";
7
- import type { Plane } from "./Plane.js";
8
- import type { CurveOffsetOptions, RotationAxis } from "./types.js";
9
- import * as wasm from "./wasm-bindings.js";
10
-
11
- /**
12
- * Immutable line segment defined by start and end points.
13
- * Supports evaluation, transformation, and extrusion operations.
14
- */
15
- export class Line {
16
- constructor(
17
- public readonly start: Point,
18
- public readonly end: Point
19
- ) {}
20
-
21
- /**
22
- * Evaluate a point on the line at normalized parameter t.
23
- * @param t - Parameter in [0, 1] (0 = start, 1 = end)
24
- * @returns Point on line at parameter t
25
- */
26
- pointAt(t: number): Point {
27
- ensureInit();
28
- const r = wasm.line_point_at(
29
- this.start.x, this.start.y, this.start.z,
30
- this.end.x, this.end.y, this.end.z,
31
- t
32
- );
33
- return new Point(r[0], r[1], r[2]);
34
- }
35
-
36
- /**
37
- * Get the length of this line segment.
38
- * @returns Euclidean distance from start to end
39
- */
40
- length(): number {
41
- ensureInit();
42
- return wasm.line_length(
43
- this.start.x, this.start.y, this.start.z,
44
- this.end.x, this.end.y, this.end.z
45
- );
46
- }
47
-
48
- /**
49
- * Get the unit direction vector of this line.
50
- * @returns Normalized vector from start to end
51
- */
52
- tangent(): Vec3 {
53
- ensureInit();
54
- const r = wasm.line_tangent(
55
- this.start.x, this.start.y, this.start.z,
56
- this.end.x, this.end.y, this.end.z
57
- );
58
- return new Vec3(r[0], r[1], r[2]);
59
- }
60
-
61
- /**
62
- * Sample the line into evenly-spaced points.
63
- * @param n - Number of points to generate
64
- * @returns Array of n points along the line
65
- */
66
- sample(n: number): Point[] {
67
- ensureInit();
68
- const buf = wasm.create_line(
69
- this.start.x, this.start.y, this.start.z,
70
- this.end.x, this.end.y, this.end.z,
71
- n
72
- );
73
- const pts: Point[] = [];
74
- for (let i = 0; i < buf.length; i += 3) {
75
- pts.push(new Point(buf[i], buf[i + 1], buf[i + 2]));
76
- }
77
- return pts;
78
- }
79
-
80
- /**
81
- * Translate this line by an offset vector.
82
- * @param offset - Translation vector
83
- * @returns New translated line
84
- */
85
- translate(offset: Vec3): Line {
86
- return new Line(this.start.add(offset), this.end.add(offset));
87
- }
88
-
89
- /**
90
- * Apply a row-major 4x4 matrix to both endpoints.
91
- * @param matrix - Row-major 4x4 matrix (16 entries)
92
- * @returns New transformed line
93
- */
94
- applyMatrix(matrix: number[] | Float64Array): Line {
95
- return new Line(this.start.applyMatrix(matrix), this.end.applyMatrix(matrix));
96
- }
97
-
98
- /**
99
- * Rotate this line around an axis.
100
- * @param axis - Rotation axis (Vec3 through origin, or Line for arbitrary axis)
101
- * @param angle - Rotation angle in radians
102
- * @returns New rotated line
103
- */
104
- rotate(axis: RotationAxis, angle: number): Line {
105
- return new Line(this.start.rotate(axis, angle), this.end.rotate(axis, angle));
106
- }
107
-
108
- /**
109
- * Offset this line perpendicular to its direction.
110
- * @param distance - Offset distance (positive = along normal cross direction)
111
- * @param normal - Reference normal for determining offset direction (default Z axis)
112
- * @returns New offset line
113
- */
114
- offset(distance: number, normal: Vec3 = Vec3.Z): Line {
115
- ensureInit();
116
- const r = wasm.line_offset(
117
- this.start.x, this.start.y, this.start.z,
118
- this.end.x, this.end.y, this.end.z,
119
- distance,
120
- normal.x, normal.y, normal.z,
121
- );
122
- return new Line(
123
- new Point(r[0], r[1], r[2]),
124
- new Point(r[3], r[4], r[5]),
125
- );
126
- }
127
-
128
- /**
129
- * Thicken this line into a closed PolyCurve suitable for extrusion.
130
- * When `bothSides` is false, the source line forms one side of the result.
131
- * When true, the thickness is added on each side of the line.
132
- *
133
- * If `normal` is omitted, the kernel will infer a stable fallback plane.
134
- */
135
- thicken(
136
- distance: number,
137
- bothSides = false,
138
- normal?: Vec3,
139
- options: CurveOffsetOptions = {},
140
- ): PolyCurve {
141
- return new Polyline([this.start, this.end]).thicken(distance, bothSides, normal, options);
142
- }
143
-
144
- /**
145
- * Project this line onto a plane.
146
- * @param plane - Target plane
147
- * @param direction - Optional projection direction (default: perpendicular to plane)
148
- * @returns New projected line
149
- */
150
- projectOntoPlane(plane: Plane, direction?: Vec3): Line {
151
- const proj = (p: Point) => direction ? plane.projectPointAlongDirection(p, direction) : plane.projectPoint(p);
152
- return new Line(proj(this.start), proj(this.end));
153
- }
154
-
155
- /**
156
- * Extrude this line into a mesh surface.
157
- * @param direction - Extrusion direction vector
158
- * @param segments - Number of segments along extrusion (default 1)
159
- * @returns Mesh representing the extruded surface
160
- */
161
- extrude(direction: Vec3, segments = 1): Mesh {
162
- ensureInit();
163
- const buf = wasm.extrude_line(
164
- this.start.x, this.start.y, this.start.z,
165
- this.end.x, this.end.y, this.end.z,
166
- direction.x, direction.y, direction.z,
167
- segments
168
- );
169
- return Mesh.fromBuffer(buf);
170
- }
171
- }
1
+ import { ensureInit } from "./engine.js";
2
+ import { Point } from "./Point.js";
3
+ import { Vec3 } from "./Vec3.js";
4
+ import { Mesh } from "./Mesh.js";
5
+ import { PolyCurve } from "./PolyCurve.js";
6
+ import { Polyline } from "./Polyline.js";
7
+ import type { Plane } from "./Plane.js";
8
+ import type { CurveOffsetOptions, RotationAxis } from "./types.js";
9
+ import { wasm } from "./wasm-active.js";
10
+
11
+ /**
12
+ * Immutable line segment defined by start and end points.
13
+ * Supports evaluation, transformation, and extrusion operations.
14
+ */
15
+ export class Line {
16
+ constructor(
17
+ public readonly start: Point,
18
+ public readonly end: Point
19
+ ) {}
20
+
21
+ /**
22
+ * Evaluate a point on the line at normalized parameter t.
23
+ * @param t - Parameter in [0, 1] (0 = start, 1 = end)
24
+ * @returns Point on line at parameter t
25
+ */
26
+ pointAt(t: number): Point {
27
+ ensureInit();
28
+ const r = wasm.line_point_at(
29
+ this.start.x, this.start.y, this.start.z,
30
+ this.end.x, this.end.y, this.end.z,
31
+ t
32
+ );
33
+ return new Point(r[0], r[1], r[2]);
34
+ }
35
+
36
+ /**
37
+ * Get the length of this line segment.
38
+ * @returns Euclidean distance from start to end
39
+ */
40
+ length(): number {
41
+ ensureInit();
42
+ return wasm.line_length(
43
+ this.start.x, this.start.y, this.start.z,
44
+ this.end.x, this.end.y, this.end.z
45
+ );
46
+ }
47
+
48
+ /**
49
+ * Get the unit direction vector of this line.
50
+ * @returns Normalized vector from start to end
51
+ */
52
+ tangent(): Vec3 {
53
+ ensureInit();
54
+ const r = wasm.line_tangent(
55
+ this.start.x, this.start.y, this.start.z,
56
+ this.end.x, this.end.y, this.end.z
57
+ );
58
+ return new Vec3(r[0], r[1], r[2]);
59
+ }
60
+
61
+ /**
62
+ * Sample the line into evenly-spaced points.
63
+ * @param n - Number of points to generate
64
+ * @returns Array of n points along the line
65
+ */
66
+ sample(n: number): Point[] {
67
+ ensureInit();
68
+ const buf = wasm.create_line(
69
+ this.start.x, this.start.y, this.start.z,
70
+ this.end.x, this.end.y, this.end.z,
71
+ n
72
+ );
73
+ const pts: Point[] = [];
74
+ for (let i = 0; i < buf.length; i += 3) {
75
+ pts.push(new Point(buf[i], buf[i + 1], buf[i + 2]));
76
+ }
77
+ return pts;
78
+ }
79
+
80
+ /**
81
+ * Translate this line by an offset vector.
82
+ * @param offset - Translation vector
83
+ * @returns New translated line
84
+ */
85
+ translate(offset: Vec3): Line {
86
+ return new Line(this.start.add(offset), this.end.add(offset));
87
+ }
88
+
89
+ /**
90
+ * Apply a row-major 4x4 matrix to both endpoints.
91
+ * @param matrix - Row-major 4x4 matrix (16 entries)
92
+ * @returns New transformed line
93
+ */
94
+ applyMatrix(matrix: number[] | Float64Array): Line {
95
+ return new Line(this.start.applyMatrix(matrix), this.end.applyMatrix(matrix));
96
+ }
97
+
98
+ /**
99
+ * Rotate this line around an axis.
100
+ * @param axis - Rotation axis (Vec3 through origin, or Line for arbitrary axis)
101
+ * @param angle - Rotation angle in radians
102
+ * @returns New rotated line
103
+ */
104
+ rotate(axis: RotationAxis, angle: number): Line {
105
+ return new Line(this.start.rotate(axis, angle), this.end.rotate(axis, angle));
106
+ }
107
+
108
+ /**
109
+ * Offset this line perpendicular to its direction.
110
+ * @param distance - Offset distance (positive = along normal cross direction)
111
+ * @param normal - Reference normal for determining offset direction (default Z axis)
112
+ * @returns New offset line
113
+ */
114
+ offset(distance: number, normal: Vec3 = Vec3.Z): Line {
115
+ ensureInit();
116
+ const r = wasm.line_offset(
117
+ this.start.x, this.start.y, this.start.z,
118
+ this.end.x, this.end.y, this.end.z,
119
+ distance,
120
+ normal.x, normal.y, normal.z,
121
+ );
122
+ return new Line(
123
+ new Point(r[0], r[1], r[2]),
124
+ new Point(r[3], r[4], r[5]),
125
+ );
126
+ }
127
+
128
+ /**
129
+ * Thicken this line into a closed PolyCurve suitable for extrusion.
130
+ * When `bothSides` is false, the source line forms one side of the result.
131
+ * When true, the thickness is added on each side of the line.
132
+ *
133
+ * If `normal` is omitted, the kernel will infer a stable fallback plane.
134
+ */
135
+ thicken(
136
+ distance: number,
137
+ bothSides = false,
138
+ normal?: Vec3,
139
+ options: CurveOffsetOptions = {},
140
+ ): PolyCurve {
141
+ return new Polyline([this.start, this.end]).thicken(distance, bothSides, normal, options);
142
+ }
143
+
144
+ /**
145
+ * Project this line onto a plane.
146
+ * @param plane - Target plane
147
+ * @param direction - Optional projection direction (default: perpendicular to plane)
148
+ * @returns New projected line
149
+ */
150
+ projectOntoPlane(plane: Plane, direction?: Vec3): Line {
151
+ const proj = (p: Point) => direction ? plane.projectPointAlongDirection(p, direction) : plane.projectPoint(p);
152
+ return new Line(proj(this.start), proj(this.end));
153
+ }
154
+
155
+ /**
156
+ * Extrude this line into a mesh surface.
157
+ * @param direction - Extrusion direction vector
158
+ * @param segments - Number of segments along extrusion (default 1)
159
+ * @returns Mesh representing the extruded surface
160
+ */
161
+ extrude(direction: Vec3, segments = 1): Mesh {
162
+ ensureInit();
163
+ const buf = wasm.extrude_line(
164
+ this.start.x, this.start.y, this.start.z,
165
+ this.end.x, this.end.y, this.end.z,
166
+ direction.x, direction.y, direction.z,
167
+ segments
168
+ );
169
+ return Mesh.fromBuffer(buf);
170
+ }
171
+ }