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/src/Line.ts ADDED
@@ -0,0 +1,144 @@
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 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 line segment defined by start and end points.
11
+ * Supports evaluation, transformation, and extrusion operations.
12
+ */
13
+ export class Line {
14
+ constructor(
15
+ public readonly start: Point,
16
+ public readonly end: Point
17
+ ) {}
18
+
19
+ /**
20
+ * Evaluate a point on the line at normalized parameter t.
21
+ * @param t - Parameter in [0, 1] (0 = start, 1 = end)
22
+ * @returns Point on line at parameter t
23
+ */
24
+ pointAt(t: number): Point {
25
+ ensureInit();
26
+ const r = wasm.line_point_at(
27
+ this.start.x, this.start.y, this.start.z,
28
+ this.end.x, this.end.y, this.end.z,
29
+ t
30
+ );
31
+ return new Point(r[0], r[1], r[2]);
32
+ }
33
+
34
+ /**
35
+ * Get the length of this line segment.
36
+ * @returns Euclidean distance from start to end
37
+ */
38
+ length(): number {
39
+ ensureInit();
40
+ return wasm.line_length(
41
+ this.start.x, this.start.y, this.start.z,
42
+ this.end.x, this.end.y, this.end.z
43
+ );
44
+ }
45
+
46
+ /**
47
+ * Get the unit direction vector of this line.
48
+ * @returns Normalized vector from start to end
49
+ */
50
+ tangent(): Vec3 {
51
+ ensureInit();
52
+ const r = wasm.line_tangent(
53
+ this.start.x, this.start.y, this.start.z,
54
+ this.end.x, this.end.y, this.end.z
55
+ );
56
+ return new Vec3(r[0], r[1], r[2]);
57
+ }
58
+
59
+ /**
60
+ * Sample the line into evenly-spaced points.
61
+ * @param n - Number of points to generate
62
+ * @returns Array of n points along the line
63
+ */
64
+ sample(n: number): Point[] {
65
+ ensureInit();
66
+ const buf = wasm.create_line(
67
+ this.start.x, this.start.y, this.start.z,
68
+ this.end.x, this.end.y, this.end.z,
69
+ n
70
+ );
71
+ const pts: Point[] = [];
72
+ for (let i = 0; i < buf.length; i += 3) {
73
+ pts.push(new Point(buf[i], buf[i + 1], buf[i + 2]));
74
+ }
75
+ return pts;
76
+ }
77
+
78
+ /**
79
+ * Translate this line by an offset vector.
80
+ * @param offset - Translation vector
81
+ * @returns New translated line
82
+ */
83
+ translate(offset: Vec3): Line {
84
+ return new Line(this.start.add(offset), this.end.add(offset));
85
+ }
86
+
87
+ /**
88
+ * Rotate this line around an axis.
89
+ * @param axis - Rotation axis (Vec3 through origin, or Line for arbitrary axis)
90
+ * @param angle - Rotation angle in radians
91
+ * @returns New rotated line
92
+ */
93
+ rotate(axis: RotationAxis, angle: number): Line {
94
+ return new Line(this.start.rotate(axis, angle), this.end.rotate(axis, angle));
95
+ }
96
+
97
+ /**
98
+ * Offset this line perpendicular to its direction.
99
+ * @param distance - Offset distance (positive = along normal cross direction)
100
+ * @param normal - Reference normal for determining offset direction (default Z axis)
101
+ * @returns New offset line
102
+ */
103
+ offset(distance: number, normal: Vec3 = Vec3.Z): Line {
104
+ ensureInit();
105
+ const r = wasm.line_offset(
106
+ this.start.x, this.start.y, this.start.z,
107
+ this.end.x, this.end.y, this.end.z,
108
+ distance,
109
+ normal.x, normal.y, normal.z,
110
+ );
111
+ return new Line(
112
+ new Point(r[0], r[1], r[2]),
113
+ new Point(r[3], r[4], r[5]),
114
+ );
115
+ }
116
+
117
+ /**
118
+ * Project this line onto a plane.
119
+ * @param plane - Target plane
120
+ * @param direction - Optional projection direction (default: perpendicular to plane)
121
+ * @returns New projected line
122
+ */
123
+ projectOntoPlane(plane: Plane, direction?: Vec3): Line {
124
+ const proj = (p: Point) => direction ? plane.projectPointAlongDirection(p, direction) : plane.projectPoint(p);
125
+ return new Line(proj(this.start), proj(this.end));
126
+ }
127
+
128
+ /**
129
+ * Extrude this line into a mesh surface.
130
+ * @param direction - Extrusion direction vector
131
+ * @param segments - Number of segments along extrusion (default 1)
132
+ * @returns Mesh representing the extruded surface
133
+ */
134
+ extrude(direction: Vec3, segments = 1): Mesh {
135
+ ensureInit();
136
+ const buf = wasm.extrude_line(
137
+ this.start.x, this.start.y, this.start.z,
138
+ this.end.x, this.end.y, this.end.z,
139
+ direction.x, direction.y, direction.z,
140
+ segments
141
+ );
142
+ return Mesh.fromBuffer(buf);
143
+ }
144
+ }