okgeometry-api 1.1.6 → 1.1.7

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 (52) hide show
  1. package/dist/Arc.js +1 -1
  2. package/dist/Arc.js.map +1 -1
  3. package/dist/Circle.js +1 -1
  4. package/dist/Circle.js.map +1 -1
  5. package/dist/Line.js +1 -1
  6. package/dist/Line.js.map +1 -1
  7. package/dist/Mesh.d.ts +101 -4
  8. package/dist/Mesh.d.ts.map +1 -1
  9. package/dist/Mesh.js +103 -5
  10. package/dist/Mesh.js.map +1 -1
  11. package/dist/NurbsCurve.js +1 -1
  12. package/dist/NurbsCurve.js.map +1 -1
  13. package/dist/NurbsSurface.d.ts.map +1 -1
  14. package/dist/NurbsSurface.js +10 -7
  15. package/dist/NurbsSurface.js.map +1 -1
  16. package/dist/PolyCurve.js +1 -1
  17. package/dist/PolyCurve.js.map +1 -1
  18. package/dist/Polyline.js +1 -1
  19. package/dist/Polyline.js.map +1 -1
  20. package/dist/Ray.js +1 -1
  21. package/dist/Ray.js.map +1 -1
  22. package/dist/engine.d.ts.map +1 -1
  23. package/dist/engine.js +1 -3
  24. package/dist/engine.js.map +1 -1
  25. package/dist/index.d.ts +1 -1
  26. package/dist/index.d.ts.map +1 -1
  27. package/dist/index.js.map +1 -1
  28. package/dist/wasm-base64.d.ts +1 -1
  29. package/dist/wasm-base64.d.ts.map +1 -1
  30. package/dist/wasm-base64.js +1 -1
  31. package/dist/wasm-base64.js.map +1 -1
  32. package/package.json +7 -6
  33. package/src/Arc.ts +117 -117
  34. package/src/Circle.ts +153 -153
  35. package/src/Line.ts +144 -144
  36. package/src/Mesh.ts +671 -452
  37. package/src/NurbsCurve.ts +240 -240
  38. package/src/NurbsSurface.ts +249 -245
  39. package/src/PolyCurve.ts +306 -306
  40. package/src/Polyline.ts +153 -153
  41. package/src/Ray.ts +90 -90
  42. package/src/engine.ts +9 -11
  43. package/src/index.ts +6 -0
  44. package/src/wasm-base64.ts +1 -1
  45. package/wasm/README.md +0 -104
  46. package/wasm/okgeometrycore.d.ts +0 -754
  47. package/wasm/okgeometrycore.js +0 -2005
  48. package/wasm/okgeometrycore_bg.d.ts +0 -3
  49. package/wasm/okgeometrycore_bg.js +0 -1686
  50. package/wasm/okgeometrycore_bg.wasm +0 -0
  51. package/wasm/okgeometrycore_bg.wasm.d.ts +0 -100
  52. package/wasm/package.json +0 -19
package/src/NurbsCurve.ts CHANGED
@@ -1,240 +1,240 @@
1
- import { ensureInit } from "./engine.js";
2
- import { Point } from "./Point.js";
3
- import { Vec3 } from "./Vec3.js";
4
- import { Plane } from "./Plane.js";
5
- import { Polyline } from "./Polyline.js";
6
- import type { Line } from "./Line.js";
7
- import type { Arc } from "./Arc.js";
8
- import type { RotationAxis } from "./types.js";
9
- import { parseIntersectionPoints } from "./BufferCodec.js";
10
- import * as wasm from "../wasm/okgeometrycore_bg.js";
11
-
12
- /**
13
- * Non-Uniform Rational B-Spline (NURBS) curve backed by WASM.
14
- * Provides exact representation for complex curves including conics.
15
- *
16
- * WASM data format: [degree, num_pts, x0,y0,z0, ..., w0,w1,..., k0,k1,...]
17
- */
18
- export class NurbsCurve {
19
- private _data: Float64Array;
20
-
21
- /**
22
- * Create a new NURBS curve.
23
- * @param degree - Polynomial degree (1 = linear, 2 = quadratic, 3 = cubic)
24
- * @param controlPoints - Control points defining the curve shape
25
- * @param weights - Rational weights for each control point (use 1 for non-rational)
26
- * @param knots - Knot vector (length = num_points + degree + 1)
27
- */
28
- constructor(
29
- public readonly degree: number,
30
- public readonly controlPoints: Point[],
31
- public readonly weights: number[],
32
- public readonly knots: number[]
33
- ) {
34
- this._data = NurbsCurve.encode(degree, controlPoints, weights, knots);
35
- }
36
-
37
- /**
38
- * Evaluate a point on the curve at normalized parameter t.
39
- * @param t - Parameter in [0, 1] (0 = start, 1 = end)
40
- * @returns Point on curve at parameter t
41
- */
42
- pointAt(t: number): Point {
43
- ensureInit();
44
- const r = wasm.evaluate_nurbs_curve_at(this._data, t);
45
- return new Point(r[0], r[1], r[2]);
46
- }
47
-
48
- /**
49
- * Sample the curve into evenly-spaced points.
50
- * @param n - Number of points to generate
51
- * @returns Array of n points along the curve
52
- */
53
- sample(n: number): Point[] {
54
- ensureInit();
55
- const buf = wasm.sample_nurbs_curve(this._data, n);
56
- const pts: Point[] = [];
57
- for (let i = 0; i < buf.length; i += 3) {
58
- pts.push(new Point(buf[i], buf[i + 1], buf[i + 2]));
59
- }
60
- return pts;
61
- }
62
-
63
- /**
64
- * Find intersection points with a plane.
65
- * @param plane - Cutting plane
66
- * @returns Array of intersection points
67
- */
68
- intersectPlane(plane: Plane): Point[] {
69
- ensureInit();
70
- const buf = wasm.nurbs_curve_plane_intersect(
71
- this._data,
72
- plane.normal.x, plane.normal.y, plane.normal.z,
73
- plane.d
74
- );
75
- if (buf.length === 0) return [];
76
- const numPts = buf[0];
77
- const pts: Point[] = [];
78
- for (let i = 0; i < numPts; i++) {
79
- const off = 1 + i * 3;
80
- pts.push(new Point(buf[off], buf[off + 1], buf[off + 2]));
81
- }
82
- return pts;
83
- }
84
-
85
- /**
86
- * Find intersection points with another NURBS curve.
87
- * @param other - Other curve to intersect with
88
- * @returns Array of intersection points
89
- */
90
- intersectCurve(other: NurbsCurve): Point[] {
91
- ensureInit();
92
- const buf = wasm.nurbs_curve_curve_intersect(this._data, other._data);
93
- if (buf.length === 0) return [];
94
- const numPts = buf[0];
95
- const pts: Point[] = [];
96
- for (let i = 0; i < numPts; i++) {
97
- const off = 1 + i * 3;
98
- pts.push(new Point(buf[off], buf[off + 1], buf[off + 2]));
99
- }
100
- return pts;
101
- }
102
-
103
- /**
104
- * Translate this curve by an offset vector.
105
- * @param offset - Translation vector
106
- * @returns New translated curve
107
- */
108
- translate(offset: Vec3): NurbsCurve {
109
- return new NurbsCurve(
110
- this.degree,
111
- this.controlPoints.map(p => p.add(offset)),
112
- this.weights,
113
- this.knots
114
- );
115
- }
116
-
117
- /**
118
- * Rotate this curve around an axis.
119
- * @param axis - Rotation axis (Vec3 through origin, or Line for arbitrary axis)
120
- * @param angle - Rotation angle in radians
121
- * @returns New rotated curve
122
- */
123
- rotate(axis: RotationAxis, angle: number): NurbsCurve {
124
- return new NurbsCurve(
125
- this.degree,
126
- this.controlPoints.map(p => p.rotate(axis, angle)),
127
- this.weights,
128
- this.knots
129
- );
130
- }
131
-
132
- /**
133
- * Project this curve onto a plane.
134
- * @param plane - Target plane
135
- * @param direction - Optional projection direction (default: perpendicular to plane)
136
- * @returns New projected curve
137
- */
138
- projectOntoPlane(plane: Plane, direction?: Vec3): NurbsCurve {
139
- const proj = (p: Point) => direction ? plane.projectPointAlongDirection(p, direction) : plane.projectPoint(p);
140
- return new NurbsCurve(
141
- this.degree,
142
- this.controlPoints.map(proj),
143
- this.weights,
144
- this.knots
145
- );
146
- }
147
-
148
- /**
149
- * Offset this curve by a distance.
150
- * Note: NURBS offset is approximated by sampling and offsetting as polyline.
151
- * @param distance - Offset distance
152
- * @param normal - Reference normal for determining offset direction
153
- * @param samples - Number of sample points (default 64)
154
- * @returns Offset polyline approximation
155
- */
156
- offset(distance: number, normal?: Vec3, samples = 64): Polyline {
157
- const pts = this.sample(samples);
158
- const pl = new Polyline(pts);
159
- return pl.offset(distance, normal);
160
- }
161
-
162
- /**
163
- * Decode a NurbsCurve from WASM flat buffer.
164
- * @param data - Buffer in format [degree, num_cp, xyz..., w..., k...]
165
- * @returns New NurbsCurve instance
166
- */
167
- static fromData(data: Float64Array | number[]): NurbsCurve {
168
- const d = data instanceof Float64Array ? data : new Float64Array(data);
169
- const degree = d[0];
170
- const n = d[1];
171
- let idx = 2;
172
- const pts: Point[] = [];
173
- for (let i = 0; i < n; i++) {
174
- pts.push(new Point(d[idx], d[idx + 1], d[idx + 2]));
175
- idx += 3;
176
- }
177
- const weights = Array.from(d.slice(idx, idx + n)); idx += n;
178
- const numKnots = n + degree + 1;
179
- const knots = Array.from(d.slice(idx, idx + numKnots));
180
- return new NurbsCurve(degree, pts, weights, knots);
181
- }
182
-
183
- /**
184
- * Create exact degree-1 NURBS from a Line.
185
- * @param line - Source line
186
- * @returns Degree-1 NURBS representing the line exactly
187
- */
188
- static fromLine(line: Line): NurbsCurve {
189
- return new NurbsCurve(1, [line.start, line.end], [1, 1], [0, 0, 1, 1]);
190
- }
191
-
192
- /**
193
- * Create exact degree-2 rational NURBS from an Arc.
194
- * Uses WASM circle_arc for exact conic representation.
195
- * @param arc - Source arc
196
- * @returns Degree-2 rational NURBS representing the arc exactly
197
- */
198
- static fromArc(arc: Arc): NurbsCurve {
199
- ensureInit();
200
- const buf = wasm.polycurve_to_nurbs(new Float64Array([
201
- 1, // 1 segment
202
- 1, // type = Arc
203
- arc.center.x, arc.center.y, arc.center.z,
204
- arc.normal.x, arc.normal.y, arc.normal.z,
205
- arc.radius, arc.startAngle, arc.endAngle
206
- ]));
207
- if (buf.length < 2) throw new Error("Failed to convert Arc to NURBS");
208
- return NurbsCurve.fromData(buf);
209
- }
210
-
211
- /**
212
- * Get internal WASM data buffer.
213
- * For advanced use cases requiring direct WASM interop.
214
- */
215
- get data(): Float64Array {
216
- return this._data;
217
- }
218
-
219
- /** Encode to WASM format */
220
- private static encode(
221
- degree: number,
222
- controlPoints: Point[],
223
- weights: number[],
224
- knots: number[]
225
- ): Float64Array {
226
- const n = controlPoints.length;
227
- const data = new Float64Array(2 + n * 3 + weights.length + knots.length);
228
- data[0] = degree;
229
- data[1] = n;
230
- let idx = 2;
231
- for (const p of controlPoints) {
232
- data[idx++] = p.x;
233
- data[idx++] = p.y;
234
- data[idx++] = p.z;
235
- }
236
- for (const w of weights) data[idx++] = w;
237
- for (const k of knots) data[idx++] = k;
238
- return data;
239
- }
240
- }
1
+ import { ensureInit } from "./engine.js";
2
+ import { Point } from "./Point.js";
3
+ import { Vec3 } from "./Vec3.js";
4
+ import { Plane } from "./Plane.js";
5
+ import { Polyline } from "./Polyline.js";
6
+ import type { Line } from "./Line.js";
7
+ import type { Arc } from "./Arc.js";
8
+ import type { RotationAxis } from "./types.js";
9
+ import { parseIntersectionPoints } from "./BufferCodec.js";
10
+ import * as wasm from "../wasm/okgeometrycore.js";
11
+
12
+ /**
13
+ * Non-Uniform Rational B-Spline (NURBS) curve backed by WASM.
14
+ * Provides exact representation for complex curves including conics.
15
+ *
16
+ * WASM data format: [degree, num_pts, x0,y0,z0, ..., w0,w1,..., k0,k1,...]
17
+ */
18
+ export class NurbsCurve {
19
+ private _data: Float64Array;
20
+
21
+ /**
22
+ * Create a new NURBS curve.
23
+ * @param degree - Polynomial degree (1 = linear, 2 = quadratic, 3 = cubic)
24
+ * @param controlPoints - Control points defining the curve shape
25
+ * @param weights - Rational weights for each control point (use 1 for non-rational)
26
+ * @param knots - Knot vector (length = num_points + degree + 1)
27
+ */
28
+ constructor(
29
+ public readonly degree: number,
30
+ public readonly controlPoints: Point[],
31
+ public readonly weights: number[],
32
+ public readonly knots: number[]
33
+ ) {
34
+ this._data = NurbsCurve.encode(degree, controlPoints, weights, knots);
35
+ }
36
+
37
+ /**
38
+ * Evaluate a point on the curve at normalized parameter t.
39
+ * @param t - Parameter in [0, 1] (0 = start, 1 = end)
40
+ * @returns Point on curve at parameter t
41
+ */
42
+ pointAt(t: number): Point {
43
+ ensureInit();
44
+ const r = wasm.evaluate_nurbs_curve_at(this._data, t);
45
+ return new Point(r[0], r[1], r[2]);
46
+ }
47
+
48
+ /**
49
+ * Sample the curve into evenly-spaced points.
50
+ * @param n - Number of points to generate
51
+ * @returns Array of n points along the curve
52
+ */
53
+ sample(n: number): Point[] {
54
+ ensureInit();
55
+ const buf = wasm.sample_nurbs_curve(this._data, n);
56
+ const pts: Point[] = [];
57
+ for (let i = 0; i < buf.length; i += 3) {
58
+ pts.push(new Point(buf[i], buf[i + 1], buf[i + 2]));
59
+ }
60
+ return pts;
61
+ }
62
+
63
+ /**
64
+ * Find intersection points with a plane.
65
+ * @param plane - Cutting plane
66
+ * @returns Array of intersection points
67
+ */
68
+ intersectPlane(plane: Plane): Point[] {
69
+ ensureInit();
70
+ const buf = wasm.nurbs_curve_plane_intersect(
71
+ this._data,
72
+ plane.normal.x, plane.normal.y, plane.normal.z,
73
+ plane.d
74
+ );
75
+ if (buf.length === 0) return [];
76
+ const numPts = buf[0];
77
+ const pts: Point[] = [];
78
+ for (let i = 0; i < numPts; i++) {
79
+ const off = 1 + i * 3;
80
+ pts.push(new Point(buf[off], buf[off + 1], buf[off + 2]));
81
+ }
82
+ return pts;
83
+ }
84
+
85
+ /**
86
+ * Find intersection points with another NURBS curve.
87
+ * @param other - Other curve to intersect with
88
+ * @returns Array of intersection points
89
+ */
90
+ intersectCurve(other: NurbsCurve): Point[] {
91
+ ensureInit();
92
+ const buf = wasm.nurbs_curve_curve_intersect(this._data, other._data);
93
+ if (buf.length === 0) return [];
94
+ const numPts = buf[0];
95
+ const pts: Point[] = [];
96
+ for (let i = 0; i < numPts; i++) {
97
+ const off = 1 + i * 3;
98
+ pts.push(new Point(buf[off], buf[off + 1], buf[off + 2]));
99
+ }
100
+ return pts;
101
+ }
102
+
103
+ /**
104
+ * Translate this curve by an offset vector.
105
+ * @param offset - Translation vector
106
+ * @returns New translated curve
107
+ */
108
+ translate(offset: Vec3): NurbsCurve {
109
+ return new NurbsCurve(
110
+ this.degree,
111
+ this.controlPoints.map(p => p.add(offset)),
112
+ this.weights,
113
+ this.knots
114
+ );
115
+ }
116
+
117
+ /**
118
+ * Rotate this curve around an axis.
119
+ * @param axis - Rotation axis (Vec3 through origin, or Line for arbitrary axis)
120
+ * @param angle - Rotation angle in radians
121
+ * @returns New rotated curve
122
+ */
123
+ rotate(axis: RotationAxis, angle: number): NurbsCurve {
124
+ return new NurbsCurve(
125
+ this.degree,
126
+ this.controlPoints.map(p => p.rotate(axis, angle)),
127
+ this.weights,
128
+ this.knots
129
+ );
130
+ }
131
+
132
+ /**
133
+ * Project this curve onto a plane.
134
+ * @param plane - Target plane
135
+ * @param direction - Optional projection direction (default: perpendicular to plane)
136
+ * @returns New projected curve
137
+ */
138
+ projectOntoPlane(plane: Plane, direction?: Vec3): NurbsCurve {
139
+ const proj = (p: Point) => direction ? plane.projectPointAlongDirection(p, direction) : plane.projectPoint(p);
140
+ return new NurbsCurve(
141
+ this.degree,
142
+ this.controlPoints.map(proj),
143
+ this.weights,
144
+ this.knots
145
+ );
146
+ }
147
+
148
+ /**
149
+ * Offset this curve by a distance.
150
+ * Note: NURBS offset is approximated by sampling and offsetting as polyline.
151
+ * @param distance - Offset distance
152
+ * @param normal - Reference normal for determining offset direction
153
+ * @param samples - Number of sample points (default 64)
154
+ * @returns Offset polyline approximation
155
+ */
156
+ offset(distance: number, normal?: Vec3, samples = 64): Polyline {
157
+ const pts = this.sample(samples);
158
+ const pl = new Polyline(pts);
159
+ return pl.offset(distance, normal);
160
+ }
161
+
162
+ /**
163
+ * Decode a NurbsCurve from WASM flat buffer.
164
+ * @param data - Buffer in format [degree, num_cp, xyz..., w..., k...]
165
+ * @returns New NurbsCurve instance
166
+ */
167
+ static fromData(data: Float64Array | number[]): NurbsCurve {
168
+ const d = data instanceof Float64Array ? data : new Float64Array(data);
169
+ const degree = d[0];
170
+ const n = d[1];
171
+ let idx = 2;
172
+ const pts: Point[] = [];
173
+ for (let i = 0; i < n; i++) {
174
+ pts.push(new Point(d[idx], d[idx + 1], d[idx + 2]));
175
+ idx += 3;
176
+ }
177
+ const weights = Array.from(d.slice(idx, idx + n)); idx += n;
178
+ const numKnots = n + degree + 1;
179
+ const knots = Array.from(d.slice(idx, idx + numKnots));
180
+ return new NurbsCurve(degree, pts, weights, knots);
181
+ }
182
+
183
+ /**
184
+ * Create exact degree-1 NURBS from a Line.
185
+ * @param line - Source line
186
+ * @returns Degree-1 NURBS representing the line exactly
187
+ */
188
+ static fromLine(line: Line): NurbsCurve {
189
+ return new NurbsCurve(1, [line.start, line.end], [1, 1], [0, 0, 1, 1]);
190
+ }
191
+
192
+ /**
193
+ * Create exact degree-2 rational NURBS from an Arc.
194
+ * Uses WASM circle_arc for exact conic representation.
195
+ * @param arc - Source arc
196
+ * @returns Degree-2 rational NURBS representing the arc exactly
197
+ */
198
+ static fromArc(arc: Arc): NurbsCurve {
199
+ ensureInit();
200
+ const buf = wasm.polycurve_to_nurbs(new Float64Array([
201
+ 1, // 1 segment
202
+ 1, // type = Arc
203
+ arc.center.x, arc.center.y, arc.center.z,
204
+ arc.normal.x, arc.normal.y, arc.normal.z,
205
+ arc.radius, arc.startAngle, arc.endAngle
206
+ ]));
207
+ if (buf.length < 2) throw new Error("Failed to convert Arc to NURBS");
208
+ return NurbsCurve.fromData(buf);
209
+ }
210
+
211
+ /**
212
+ * Get internal WASM data buffer.
213
+ * For advanced use cases requiring direct WASM interop.
214
+ */
215
+ get data(): Float64Array {
216
+ return this._data;
217
+ }
218
+
219
+ /** Encode to WASM format */
220
+ private static encode(
221
+ degree: number,
222
+ controlPoints: Point[],
223
+ weights: number[],
224
+ knots: number[]
225
+ ): Float64Array {
226
+ const n = controlPoints.length;
227
+ const data = new Float64Array(2 + n * 3 + weights.length + knots.length);
228
+ data[0] = degree;
229
+ data[1] = n;
230
+ let idx = 2;
231
+ for (const p of controlPoints) {
232
+ data[idx++] = p.x;
233
+ data[idx++] = p.y;
234
+ data[idx++] = p.z;
235
+ }
236
+ for (const w of weights) data[idx++] = w;
237
+ for (const k of knots) data[idx++] = k;
238
+ return data;
239
+ }
240
+ }