okgeometry-api 0.5.0 → 0.5.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.
- package/dist/Mesh.d.ts +0 -32
- package/dist/Mesh.d.ts.map +1 -1
- package/dist/Mesh.js +8 -91
- package/dist/Mesh.js.map +1 -1
- package/dist/engine.d.ts +0 -1
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +0 -37
- package/dist/engine.js.map +1 -1
- package/dist/mesh-boolean.protocol.d.ts +2 -2
- package/dist/mesh-boolean.protocol.d.ts.map +1 -1
- package/dist/wasm-base64.d.ts +1 -1
- package/dist/wasm-base64.d.ts.map +1 -1
- package/dist/wasm-base64.js +1 -1
- package/dist/wasm-base64.js.map +1 -1
- package/package.json +46 -45
- package/src/Arc.ts +117 -0
- package/src/BufferCodec.ts +181 -0
- package/src/Circle.ts +153 -0
- package/src/Geometry.ts +39 -0
- package/src/Line.ts +144 -0
- package/src/Mesh.ts +1476 -0
- package/src/NurbsCurve.ts +240 -0
- package/src/NurbsSurface.ts +267 -0
- package/src/Plane.ts +132 -0
- package/src/Point.ts +145 -0
- package/src/PolyCurve.ts +306 -0
- package/src/Polygon.ts +75 -0
- package/src/Polyline.ts +153 -0
- package/src/Ray.ts +90 -0
- package/src/Vec3.ts +159 -0
- package/src/engine.ts +92 -0
- package/src/index.ts +45 -0
- package/src/mesh-boolean.pool.ts +481 -0
- package/src/mesh-boolean.protocol.ts +122 -0
- package/src/mesh-boolean.worker.ts +160 -0
- package/src/types.ts +84 -0
- package/src/wasm-base64.ts +2 -0
- package/wasm/okgeometrycore.d.ts +19 -20
- package/wasm/okgeometrycore.js +62 -26
- package/wasm/okgeometrycore_bg.js +47 -0
- package/wasm/okgeometrycore_bg.wasm +0 -0
- package/wasm/okgeometrycore_bg.wasm.d.ts +2 -2
package/dist/wasm-base64.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"wasm-base64.js","sourceRoot":"","sources":["../src/wasm-base64.ts"],"names":[],"mappings":"AAAA,+BAA+B;AAC/B,MAAM,CAAC,MAAM,QAAQ,GAAG,
|
|
1
|
+
{"version":3,"file":"wasm-base64.js","sourceRoot":"","sources":["../src/wasm-base64.ts"],"names":[],"mappings":"AAAA,+BAA+B;AAC/B,MAAM,CAAC,MAAM,QAAQ,GAAG,ku5hCAAku5hC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,45 +1,46 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "okgeometry-api",
|
|
3
|
-
"version": "0.5.
|
|
4
|
-
"description": "Geometry engine API for AEC applications — NURBS, meshes, booleans, intersections. Powered by Rust/WASM.",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"main": "dist/index.js",
|
|
7
|
-
"types": "dist/index.d.ts",
|
|
8
|
-
"exports": {
|
|
9
|
-
".": {
|
|
10
|
-
"types": "./dist/index.d.ts",
|
|
11
|
-
"import": "./dist/index.js"
|
|
12
|
-
}
|
|
13
|
-
},
|
|
14
|
-
"files": [
|
|
15
|
-
"dist/",
|
|
16
|
-
"
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
|
|
39
|
-
"
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
"
|
|
44
|
-
|
|
45
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "okgeometry-api",
|
|
3
|
+
"version": "0.5.2",
|
|
4
|
+
"description": "Geometry engine API for AEC applications — NURBS, meshes, booleans, intersections. Powered by Rust/WASM.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist/",
|
|
16
|
+
"src/",
|
|
17
|
+
"wasm/"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"inline-wasm": "tsx scripts/inline-wasm.ts",
|
|
21
|
+
"build": "npm run inline-wasm && tsc",
|
|
22
|
+
"bench:boolean-nextgen": "npm run build && tsx scripts/gate-boolean-nextgen.ts",
|
|
23
|
+
"gate:boolean-nextgen": "npm run build && tsx scripts/gate-boolean-nextgen.ts",
|
|
24
|
+
"prepublishOnly": "npm run build"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"geometry",
|
|
28
|
+
"wasm",
|
|
29
|
+
"nurbs",
|
|
30
|
+
"mesh",
|
|
31
|
+
"boolean",
|
|
32
|
+
"cad",
|
|
33
|
+
"aec",
|
|
34
|
+
"3d"
|
|
35
|
+
],
|
|
36
|
+
"author": "Mostafa El Ayoubi",
|
|
37
|
+
"license": "Proprietary License",
|
|
38
|
+
"repository": {
|
|
39
|
+
"type": "git",
|
|
40
|
+
"url": "https://github.com/moel-ai/okgeometrycore"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"typescript": "^5.4.0",
|
|
44
|
+
"tsx": "^4.7.0"
|
|
45
|
+
}
|
|
46
|
+
}
|
package/src/Arc.ts
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { ensureInit } from "./engine.js";
|
|
2
|
+
import { Point } from "./Point.js";
|
|
3
|
+
import { Vec3 } from "./Vec3.js";
|
|
4
|
+
import { Polyline } from "./Polyline.js";
|
|
5
|
+
import type { Plane } from "./Plane.js";
|
|
6
|
+
import type { RotationAxis } from "./types.js";
|
|
7
|
+
import * as wasm from "../wasm/okgeometrycore_bg.js";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Immutable circular arc defined by center, radius, and angular span.
|
|
11
|
+
* Angles are in radians, measured counter-clockwise from the positive X axis
|
|
12
|
+
* in the plane defined by the normal.
|
|
13
|
+
*/
|
|
14
|
+
export class Arc {
|
|
15
|
+
/**
|
|
16
|
+
* Create a new arc.
|
|
17
|
+
* @param center - Center point
|
|
18
|
+
* @param radius - Arc radius
|
|
19
|
+
* @param startAngle - Start angle in radians
|
|
20
|
+
* @param endAngle - End angle in radians
|
|
21
|
+
* @param normal - Normal vector defining the arc's plane (default Z axis)
|
|
22
|
+
*/
|
|
23
|
+
constructor(
|
|
24
|
+
public readonly center: Point,
|
|
25
|
+
public readonly radius: number,
|
|
26
|
+
public readonly startAngle: number,
|
|
27
|
+
public readonly endAngle: number,
|
|
28
|
+
public readonly normal: Vec3 = Vec3.Z
|
|
29
|
+
) {}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Evaluate a point on the arc at normalized parameter t.
|
|
33
|
+
* @param t - Parameter in [0, 1] (0 = start angle, 1 = end angle)
|
|
34
|
+
* @returns Point on arc at parameter t
|
|
35
|
+
*/
|
|
36
|
+
pointAt(t: number): Point {
|
|
37
|
+
ensureInit();
|
|
38
|
+
const r = wasm.arc_point_at(
|
|
39
|
+
this.center.x, this.center.y, this.center.z,
|
|
40
|
+
this.normal.x, this.normal.y, this.normal.z,
|
|
41
|
+
this.radius, this.startAngle, this.endAngle, t
|
|
42
|
+
);
|
|
43
|
+
return new Point(r[0], r[1], r[2]);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Get the arc length.
|
|
48
|
+
* @returns Arc length = radius * angular span
|
|
49
|
+
*/
|
|
50
|
+
length(): number {
|
|
51
|
+
ensureInit();
|
|
52
|
+
return wasm.arc_length(this.radius, this.startAngle, this.endAngle);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Translate this arc by an offset vector.
|
|
57
|
+
* @param offset - Translation vector
|
|
58
|
+
* @returns New translated arc
|
|
59
|
+
*/
|
|
60
|
+
translate(offset: Vec3): Arc {
|
|
61
|
+
return new Arc(this.center.add(offset), this.radius, this.startAngle, this.endAngle, this.normal);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Rotate this arc around an axis.
|
|
66
|
+
* @param axis - Rotation axis (Vec3 through origin, or Line for arbitrary axis)
|
|
67
|
+
* @param angle - Rotation angle in radians
|
|
68
|
+
* @returns New rotated arc
|
|
69
|
+
*/
|
|
70
|
+
rotate(axis: RotationAxis, angle: number): Arc {
|
|
71
|
+
const n = this.normal;
|
|
72
|
+
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;
|
|
73
|
+
const rn = new Point(n.x, n.y, n.z).rotate(dirAxis, angle);
|
|
74
|
+
return new Arc(this.center.rotate(axis, angle), this.radius, this.startAngle, this.endAngle, new Vec3(rn.x, rn.y, rn.z));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Offset this arc by changing its radius.
|
|
79
|
+
* @param distance - Distance to offset (positive = larger, negative = smaller)
|
|
80
|
+
* @returns New arc with adjusted radius
|
|
81
|
+
*/
|
|
82
|
+
offset(distance: number): Arc {
|
|
83
|
+
return new Arc(this.center, this.radius + distance, this.startAngle, this.endAngle, this.normal);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Project onto plane. Returns a Polyline because a tilted arc projects to an elliptical arc.
|
|
88
|
+
* @param plane - Target plane
|
|
89
|
+
* @param direction - Optional projection direction (default: perpendicular to plane)
|
|
90
|
+
* @param samples - Number of sample points for approximation (default 64)
|
|
91
|
+
* @returns Polyline approximating the projected arc
|
|
92
|
+
*/
|
|
93
|
+
projectOntoPlane(plane: Plane, direction?: Vec3, samples = 64): Polyline {
|
|
94
|
+
const pts = this.sample(samples);
|
|
95
|
+
const proj = (p: Point) => direction ? plane.projectPointAlongDirection(p, direction) : plane.projectPoint(p);
|
|
96
|
+
return new Polyline(pts.map(proj));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Sample the arc into evenly-spaced points.
|
|
101
|
+
* @param n - Number of points to generate
|
|
102
|
+
* @returns Array of n points along the arc
|
|
103
|
+
*/
|
|
104
|
+
sample(n: number): Point[] {
|
|
105
|
+
ensureInit();
|
|
106
|
+
const buf = wasm.create_arc(
|
|
107
|
+
this.center.x, this.center.y, this.center.z,
|
|
108
|
+
this.normal.x, this.normal.y, this.normal.z,
|
|
109
|
+
this.radius, this.startAngle, this.endAngle, n
|
|
110
|
+
);
|
|
111
|
+
const pts: Point[] = [];
|
|
112
|
+
for (let i = 0; i < buf.length; i += 3) {
|
|
113
|
+
pts.push(new Point(buf[i], buf[i + 1], buf[i + 2]));
|
|
114
|
+
}
|
|
115
|
+
return pts;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Buffer encoding/decoding utilities for WASM communication.
|
|
3
|
+
*
|
|
4
|
+
* # WASM Buffer Formats
|
|
5
|
+
*
|
|
6
|
+
* ## Mesh Buffer
|
|
7
|
+
* `[vertexCount, x1,y1,z1, ..., i1,i2,i3, ...]`
|
|
8
|
+
* - Element 0: Number of vertices
|
|
9
|
+
* - Elements 1 to vertexCount*3: Vertex coordinates (xyz triplets)
|
|
10
|
+
* - Remaining elements: Triangle indices (groups of 3)
|
|
11
|
+
*
|
|
12
|
+
* ## Polyline Buffer (multi-polyline result)
|
|
13
|
+
* `[numPolylines, count1, x,y,z,..., count2, x,y,z,...]`
|
|
14
|
+
* - Element 0: Number of polylines
|
|
15
|
+
* - For each polyline: point count followed by xyz coordinates
|
|
16
|
+
*
|
|
17
|
+
* ## Intersection Points Buffer
|
|
18
|
+
* `[numPoints, x1,y1,z1, x2,y2,z2, ...]`
|
|
19
|
+
* - Element 0: Number of intersection points
|
|
20
|
+
* - Remaining: xyz triplets for each point
|
|
21
|
+
*
|
|
22
|
+
* ## Curve Encoding (for sweep/loft)
|
|
23
|
+
* See CurveTypeCode enum in types.ts for format details.
|
|
24
|
+
*
|
|
25
|
+
* ## NURBS Curve Data
|
|
26
|
+
* `[degree, numControlPoints, x1,y1,z1,..., w1,w2,..., k1,k2,...]`
|
|
27
|
+
*
|
|
28
|
+
* ## NURBS Surface Data
|
|
29
|
+
* `[degU, degV, numU, numV, cp_xyz..., weights..., knotsU..., knotsV...]`
|
|
30
|
+
*
|
|
31
|
+
* @module BufferCodec
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
import { Point } from "./Point.js";
|
|
35
|
+
import { Vec3 } from "./Vec3.js";
|
|
36
|
+
|
|
37
|
+
// ── Point Array Conversion ─────────────────────────────────────────
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Convert array of Points to flat coordinate buffer.
|
|
41
|
+
* @param points - Array of Point objects
|
|
42
|
+
* @returns Float64Array with interleaved [x1,y1,z1, x2,y2,z2, ...]
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* const pts = [new Point(0,0,0), new Point(1,2,3)];
|
|
47
|
+
* const coords = pointsToCoords(pts);
|
|
48
|
+
* // coords = Float64Array [0, 0, 0, 1, 2, 3]
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export function pointsToCoords(points: Point[]): Float64Array {
|
|
52
|
+
const coords = new Float64Array(points.length * 3);
|
|
53
|
+
for (let i = 0; i < points.length; i++) {
|
|
54
|
+
coords[i * 3] = points[i].x;
|
|
55
|
+
coords[i * 3 + 1] = points[i].y;
|
|
56
|
+
coords[i * 3 + 2] = points[i].z;
|
|
57
|
+
}
|
|
58
|
+
return coords;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Convert flat coordinate buffer to array of Points.
|
|
63
|
+
* @param coords - Float64Array with interleaved xyz coordinates
|
|
64
|
+
* @returns Array of Point objects
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```typescript
|
|
68
|
+
* const coords = new Float64Array([0, 0, 0, 1, 2, 3]);
|
|
69
|
+
* const pts = coordsToPoints(coords);
|
|
70
|
+
* // pts = [Point(0,0,0), Point(1,2,3)]
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
export function coordsToPoints(coords: Float64Array | number[]): Point[] {
|
|
74
|
+
const data = coords instanceof Float64Array ? coords : new Float64Array(coords);
|
|
75
|
+
const pts: Point[] = [];
|
|
76
|
+
for (let i = 0; i < data.length; i += 3) {
|
|
77
|
+
pts.push(new Point(data[i], data[i + 1], data[i + 2]));
|
|
78
|
+
}
|
|
79
|
+
return pts;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Read a single Point from buffer at specified offset.
|
|
84
|
+
* @param buf - Source buffer
|
|
85
|
+
* @param offset - Starting index in buffer
|
|
86
|
+
* @returns Tuple of [Point, newOffset] where newOffset = offset + 3
|
|
87
|
+
*/
|
|
88
|
+
export function readPoint(buf: Float64Array, offset: number): [Point, number] {
|
|
89
|
+
return [new Point(buf[offset], buf[offset + 1], buf[offset + 2]), offset + 3];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Read a single Vec3 from buffer at specified offset.
|
|
94
|
+
* @param buf - Source buffer
|
|
95
|
+
* @param offset - Starting index in buffer
|
|
96
|
+
* @returns Tuple of [Vec3, newOffset] where newOffset = offset + 3
|
|
97
|
+
*/
|
|
98
|
+
export function readVec3(buf: Float64Array, offset: number): [Vec3, number] {
|
|
99
|
+
return [new Vec3(buf[offset], buf[offset + 1], buf[offset + 2]), offset + 3];
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// ── WASM Result Decoding ───────────────────────────────────────────
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Decode WASM result buffer as single Point.
|
|
106
|
+
* Common pattern for pointAt(), closestPoint() results.
|
|
107
|
+
* @param r - WASM result array (must have at least 3 elements)
|
|
108
|
+
* @returns Point from first 3 elements
|
|
109
|
+
*/
|
|
110
|
+
export function decodePointResult(r: Float64Array): Point {
|
|
111
|
+
return new Point(r[0], r[1], r[2]);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Decode WASM result buffer as single Vec3.
|
|
116
|
+
* Common pattern for tangent(), normal() results.
|
|
117
|
+
* @param r - WASM result array (must have at least 3 elements)
|
|
118
|
+
* @returns Vec3 from first 3 elements
|
|
119
|
+
*/
|
|
120
|
+
export function decodeVec3Result(r: Float64Array): Vec3 {
|
|
121
|
+
return new Vec3(r[0], r[1], r[2]);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// ── Polyline Buffer Format ─────────────────────────────────────────
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Parse WASM multi-polyline buffer into array of point arrays.
|
|
128
|
+
* Used by mesh intersection operations that return multiple polylines.
|
|
129
|
+
*
|
|
130
|
+
* Buffer format: [numPolylines, count1, x,y,z,..., count2, x,y,z,...]
|
|
131
|
+
*
|
|
132
|
+
* @param buf - WASM result buffer
|
|
133
|
+
* @returns Array of Point arrays, one per polyline
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* ```typescript
|
|
137
|
+
* // Buffer: [2, 3, 0,0,0, 1,0,0, 1,1,0, 2, 5,5,5, 6,6,6]
|
|
138
|
+
* // = 2 polylines: first has 3 points, second has 2 points
|
|
139
|
+
* const polylines = parsePolylineBuffer(buf);
|
|
140
|
+
* // polylines[0] = [Point(0,0,0), Point(1,0,0), Point(1,1,0)]
|
|
141
|
+
* // polylines[1] = [Point(5,5,5), Point(6,6,6)]
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
export function parsePolylineBuffer(buf: Float64Array): Point[][] {
|
|
145
|
+
if (buf.length === 0) return [];
|
|
146
|
+
const numPolylines = buf[0];
|
|
147
|
+
const result: Point[][] = [];
|
|
148
|
+
let idx = 1;
|
|
149
|
+
for (let i = 0; i < numPolylines; i++) {
|
|
150
|
+
const count = buf[idx++];
|
|
151
|
+
const pts: Point[] = [];
|
|
152
|
+
for (let j = 0; j < count; j++) {
|
|
153
|
+
pts.push(new Point(buf[idx], buf[idx + 1], buf[idx + 2]));
|
|
154
|
+
idx += 3;
|
|
155
|
+
}
|
|
156
|
+
result.push(pts);
|
|
157
|
+
}
|
|
158
|
+
return result;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// ── Intersection Result Format ─────────────────────────────────────
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Parse WASM intersection points buffer.
|
|
165
|
+
* Used by curve-curve and curve-plane intersection operations.
|
|
166
|
+
*
|
|
167
|
+
* Buffer format: [numPoints, x1,y1,z1, x2,y2,z2, ...]
|
|
168
|
+
*
|
|
169
|
+
* @param buf - WASM result buffer
|
|
170
|
+
* @returns Array of intersection Points
|
|
171
|
+
*/
|
|
172
|
+
export function parseIntersectionPoints(buf: Float64Array): Point[] {
|
|
173
|
+
if (buf.length === 0) return [];
|
|
174
|
+
const numPts = buf[0];
|
|
175
|
+
const pts: Point[] = [];
|
|
176
|
+
for (let i = 0; i < numPts; i++) {
|
|
177
|
+
const off = 1 + i * 3;
|
|
178
|
+
pts.push(new Point(buf[off], buf[off + 1], buf[off + 2]));
|
|
179
|
+
}
|
|
180
|
+
return pts;
|
|
181
|
+
}
|
package/src/Circle.ts
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
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/okgeometrycore_bg.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.
|
|
67
|
+
* @param n - Number of points to generate
|
|
68
|
+
* @returns Array of n points around the circle
|
|
69
|
+
*/
|
|
70
|
+
sample(n: number): Point[] {
|
|
71
|
+
ensureInit();
|
|
72
|
+
const buf = wasm.create_circle(
|
|
73
|
+
this.center.x, this.center.y, this.center.z,
|
|
74
|
+
this.normal.x, this.normal.y, this.normal.z,
|
|
75
|
+
this.radius, n
|
|
76
|
+
);
|
|
77
|
+
const pts: Point[] = [];
|
|
78
|
+
for (let i = 0; i < buf.length; i += 3) {
|
|
79
|
+
pts.push(new Point(buf[i], buf[i + 1], buf[i + 2]));
|
|
80
|
+
}
|
|
81
|
+
return pts;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Translate this circle by an offset vector.
|
|
86
|
+
* @param offset - Translation vector
|
|
87
|
+
* @returns New translated circle
|
|
88
|
+
*/
|
|
89
|
+
translate(offset: Vec3): Circle {
|
|
90
|
+
return new Circle(this.center.add(offset), this.radius, this.normal, this.uAxis);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Rotate this circle around an axis.
|
|
95
|
+
* @param axis - Rotation axis (Vec3 through origin, or Line for arbitrary axis)
|
|
96
|
+
* @param angle - Rotation angle in radians
|
|
97
|
+
* @returns New rotated circle
|
|
98
|
+
*/
|
|
99
|
+
rotate(axis: RotationAxis, angle: number): Circle {
|
|
100
|
+
const n = this.normal;
|
|
101
|
+
// Normal is a direction — always rotate around origin regardless of axis type
|
|
102
|
+
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;
|
|
103
|
+
const rn = new Point(n.x, n.y, n.z).rotate(dirAxis, angle);
|
|
104
|
+
// Also rotate the uAxis if present
|
|
105
|
+
let rotatedUAxis: Vec3 | undefined;
|
|
106
|
+
if (this.uAxis) {
|
|
107
|
+
const ru = new Point(this.uAxis.x, this.uAxis.y, this.uAxis.z).rotate(dirAxis, angle);
|
|
108
|
+
rotatedUAxis = new Vec3(ru.x, ru.y, ru.z);
|
|
109
|
+
}
|
|
110
|
+
return new Circle(this.center.rotate(axis, angle), this.radius, new Vec3(rn.x, rn.y, rn.z), rotatedUAxis);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Offset this circle by changing its radius.
|
|
115
|
+
* @param distance - Distance to offset (positive = larger, negative = smaller)
|
|
116
|
+
* @returns New circle with adjusted radius
|
|
117
|
+
*/
|
|
118
|
+
offset(distance: number): Circle {
|
|
119
|
+
return new Circle(this.center, this.radius + distance, this.normal, this.uAxis);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Project onto plane. Returns a Polygon because a tilted circle projects to an ellipse.
|
|
124
|
+
* @param plane - Target plane
|
|
125
|
+
* @param direction - Optional projection direction (default: perpendicular to plane)
|
|
126
|
+
* @param samples - Number of sample points for approximation (default 64)
|
|
127
|
+
* @returns Polygon approximating the projected circle
|
|
128
|
+
*/
|
|
129
|
+
projectOntoPlane(plane: Plane, direction?: Vec3, samples = 64): Polygon {
|
|
130
|
+
const pts = this.sample(samples);
|
|
131
|
+
const proj = (p: Point) => direction ? plane.projectPointAlongDirection(p, direction) : plane.projectPoint(p);
|
|
132
|
+
return new Polygon(pts.map(proj));
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Extrude this circle along a direction vector to create a cylindrical mesh.
|
|
137
|
+
* @param direction - Extrusion direction and magnitude
|
|
138
|
+
* @param segments - Number of circumferential segments (default 16)
|
|
139
|
+
* @param caps - Whether to cap the ends (default true)
|
|
140
|
+
* @returns Mesh representing the extruded cylinder
|
|
141
|
+
*/
|
|
142
|
+
extrude(direction: Vec3, segments = 16, caps = true): Mesh {
|
|
143
|
+
ensureInit();
|
|
144
|
+
const buf = wasm.extrude_circle(
|
|
145
|
+
this.center.x, this.center.y, this.center.z,
|
|
146
|
+
this.normal.x, this.normal.y, this.normal.z,
|
|
147
|
+
this.radius,
|
|
148
|
+
direction.x, direction.y, direction.z,
|
|
149
|
+
segments, caps
|
|
150
|
+
);
|
|
151
|
+
return Mesh.fromBuffer(buf);
|
|
152
|
+
}
|
|
153
|
+
}
|
package/src/Geometry.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Mesh } from "./Mesh.js";
|
|
2
|
+
import { Plane } from "./Plane.js";
|
|
3
|
+
import { Point } from "./Point.js";
|
|
4
|
+
import { Polyline } from "./Polyline.js";
|
|
5
|
+
import { NurbsCurve } from "./NurbsCurve.js";
|
|
6
|
+
import { NurbsSurface } from "./NurbsSurface.js";
|
|
7
|
+
|
|
8
|
+
type GeometryType = Mesh | Plane | NurbsCurve | NurbsSurface;
|
|
9
|
+
type IntersectResult = Point[] | Polyline[];
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* General-purpose geometry intersection dispatcher.
|
|
13
|
+
* Returns intersection curves (Polyline[]) or intersection points (Point[])
|
|
14
|
+
* depending on the input types.
|
|
15
|
+
*/
|
|
16
|
+
export function intersect(a: GeometryType, b: GeometryType): IntersectResult {
|
|
17
|
+
// Mesh + Plane
|
|
18
|
+
if (a instanceof Mesh && b instanceof Plane) return a.intersectPlane(b);
|
|
19
|
+
if (a instanceof Plane && b instanceof Mesh) return b.intersectPlane(a);
|
|
20
|
+
|
|
21
|
+
// Mesh + Mesh
|
|
22
|
+
if (a instanceof Mesh && b instanceof Mesh) return a.intersectMesh(b);
|
|
23
|
+
|
|
24
|
+
// NurbsCurve + Plane
|
|
25
|
+
if (a instanceof NurbsCurve && b instanceof Plane) return a.intersectPlane(b);
|
|
26
|
+
if (a instanceof Plane && b instanceof NurbsCurve) return b.intersectPlane(a);
|
|
27
|
+
|
|
28
|
+
// NurbsCurve + NurbsCurve
|
|
29
|
+
if (a instanceof NurbsCurve && b instanceof NurbsCurve) return a.intersectCurve(b);
|
|
30
|
+
|
|
31
|
+
// NurbsSurface + Plane
|
|
32
|
+
if (a instanceof NurbsSurface && b instanceof Plane) return a.intersectPlane(b);
|
|
33
|
+
if (a instanceof Plane && b instanceof NurbsSurface) return b.intersectPlane(a);
|
|
34
|
+
|
|
35
|
+
// NurbsSurface + NurbsSurface
|
|
36
|
+
if (a instanceof NurbsSurface && b instanceof NurbsSurface) return a.intersectSurface(b);
|
|
37
|
+
|
|
38
|
+
throw new Error(`Unsupported intersection: ${a.constructor.name} + ${b.constructor.name}`);
|
|
39
|
+
}
|