@snaptrude/plugin-core 0.0.4 → 0.0.6
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/api/core/geom/arc.d.ts +50 -1
- package/dist/api/core/geom/arc.d.ts.map +1 -1
- package/dist/api/core/geom/curve.d.ts +26 -1
- package/dist/api/core/geom/curve.d.ts.map +1 -1
- package/dist/api/core/geom/index.d.ts +16 -0
- package/dist/api/core/geom/index.d.ts.map +1 -1
- package/dist/api/core/geom/line.d.ts +35 -1
- package/dist/api/core/geom/line.d.ts.map +1 -1
- package/dist/api/core/geom/profile.d.ts +66 -2
- package/dist/api/core/geom/profile.d.ts.map +1 -1
- package/dist/api/core/index.d.ts +8 -0
- package/dist/api/core/index.d.ts.map +1 -1
- package/dist/api/core/math/index.d.ts +8 -0
- package/dist/api/core/math/index.d.ts.map +1 -1
- package/dist/api/core/math/quat.d.ts +156 -15
- package/dist/api/core/math/quat.d.ts.map +1 -1
- package/dist/api/core/math/vec3.d.ts +131 -14
- package/dist/api/core/math/vec3.d.ts.map +1 -1
- package/dist/api/entity/index.d.ts +8 -0
- package/dist/api/entity/index.d.ts.map +1 -1
- package/dist/api/entity/space.d.ts +217 -4
- package/dist/api/entity/space.d.ts.map +1 -1
- package/dist/api/entity/story.d.ts +166 -0
- package/dist/api/entity/story.d.ts.map +1 -1
- package/dist/api/index.d.ts +14 -0
- package/dist/api/index.d.ts.map +1 -1
- package/dist/api/tools/index.d.ts +8 -0
- package/dist/api/tools/index.d.ts.map +1 -1
- package/dist/api/tools/selection.d.ts +37 -0
- package/dist/api/tools/selection.d.ts.map +1 -1
- package/dist/api/tools/transform.d.ts +82 -0
- package/dist/api/tools/transform.d.ts.map +1 -1
- package/dist/api/units/index.d.ts +72 -13
- package/dist/api/units/index.d.ts.map +1 -1
- package/dist/index.cjs +322 -36
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +321 -35
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +8 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/api/core/geom/arc.ts +50 -1
- package/src/api/core/geom/curve.ts +26 -1
- package/src/api/core/geom/index.ts +16 -0
- package/src/api/core/geom/line.ts +35 -1
- package/src/api/core/geom/profile.ts +66 -2
- package/src/api/core/index.ts +8 -0
- package/src/api/core/math/index.ts +8 -0
- package/src/api/core/math/quat.ts +156 -15
- package/src/api/core/math/vec3.ts +131 -14
- package/src/api/entity/index.ts +8 -0
- package/src/api/entity/space.ts +218 -5
- package/src/api/entity/story.ts +166 -0
- package/src/api/index.ts +14 -0
- package/src/api/tools/index.ts +8 -0
- package/src/api/tools/selection.ts +37 -0
- package/src/api/tools/transform.ts +82 -0
- package/src/api/units/index.ts +72 -13
- package/src/types.ts +8 -1
|
@@ -1,20 +1,57 @@
|
|
|
1
1
|
import * as z from "zod"
|
|
2
2
|
import type { PVec3 } from "./vec3"
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Quaternion operations for 3D rotations.
|
|
6
|
+
*
|
|
7
|
+
* Quaternions avoid gimbal lock and provide smooth interpolation
|
|
8
|
+
* compared to Euler angles. All methods are **pure** — they return
|
|
9
|
+
* new {@linkcode PQuat} values without mutating the inputs.
|
|
10
|
+
*
|
|
11
|
+
* Accessed via `snaptrude.core.math.quat`.
|
|
12
|
+
*/
|
|
4
13
|
export abstract class PluginQuatApi {
|
|
5
14
|
constructor() {}
|
|
6
15
|
|
|
7
|
-
/**
|
|
16
|
+
/**
|
|
17
|
+
* Create a new quaternion from raw components.
|
|
18
|
+
*
|
|
19
|
+
* For most use cases prefer {@linkcode PluginQuatApi.fromAxisAngle}
|
|
20
|
+
* or {@linkcode PluginQuatApi.fromEuler} instead.
|
|
21
|
+
*
|
|
22
|
+
* @param x - The X component
|
|
23
|
+
* @param y - The Y component
|
|
24
|
+
* @param z - The Z component
|
|
25
|
+
* @param w - The W (scalar) component
|
|
26
|
+
* @returns A new {@linkcode PQuat}
|
|
27
|
+
*/
|
|
8
28
|
new(x: number, y: number, z: number, w: number): PQuat {
|
|
9
29
|
return { x, y, z, w }
|
|
10
30
|
}
|
|
11
31
|
|
|
12
|
-
/**
|
|
32
|
+
/**
|
|
33
|
+
* Create an identity quaternion representing no rotation.
|
|
34
|
+
*
|
|
35
|
+
* @returns `{ x: 0, y: 0, z: 0, w: 1 }`
|
|
36
|
+
*/
|
|
13
37
|
identity(): PQuat {
|
|
14
38
|
return { x: 0, y: 0, z: 0, w: 1 }
|
|
15
39
|
}
|
|
16
40
|
|
|
17
|
-
/**
|
|
41
|
+
/**
|
|
42
|
+
* Create a quaternion from an axis and an angle.
|
|
43
|
+
*
|
|
44
|
+
* @param axis - The rotation axis as a {@linkcode PVec3} (will be normalized internally)
|
|
45
|
+
* @param angle - The rotation angle in **radians**
|
|
46
|
+
* @returns A new unit {@linkcode PQuat}, or identity if {@linkcode axis} has zero length
|
|
47
|
+
*
|
|
48
|
+
* # Example
|
|
49
|
+
* ```ts
|
|
50
|
+
* const { vec3, quat } = snaptrude.core.math
|
|
51
|
+
* // 90° rotation around the Y axis
|
|
52
|
+
* const rot = quat.fromAxisAngle(vec3.new(0, 1, 0), Math.PI / 2)
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
18
55
|
fromAxisAngle(axis: PVec3, angle: number): PQuat {
|
|
19
56
|
const halfAngle = angle / 2
|
|
20
57
|
const s = Math.sin(halfAngle)
|
|
@@ -28,7 +65,14 @@ export abstract class PluginQuatApi {
|
|
|
28
65
|
}
|
|
29
66
|
}
|
|
30
67
|
|
|
31
|
-
/**
|
|
68
|
+
/**
|
|
69
|
+
* Create a quaternion from Euler angles in **XYZ** rotation order.
|
|
70
|
+
*
|
|
71
|
+
* @param x - Rotation around X axis in **radians**
|
|
72
|
+
* @param y - Rotation around Y axis in **radians**
|
|
73
|
+
* @param z - Rotation around Z axis in **radians**
|
|
74
|
+
* @returns A new {@linkcode PQuat}
|
|
75
|
+
*/
|
|
32
76
|
fromEuler(x: number, y: number, z: number): PQuat {
|
|
33
77
|
const cx = Math.cos(x / 2)
|
|
34
78
|
const sx = Math.sin(x / 2)
|
|
@@ -44,7 +88,16 @@ export abstract class PluginQuatApi {
|
|
|
44
88
|
}
|
|
45
89
|
}
|
|
46
90
|
|
|
47
|
-
/**
|
|
91
|
+
/**
|
|
92
|
+
* Multiply two quaternions, combining their rotations.
|
|
93
|
+
*
|
|
94
|
+
* Quaternion multiplication is **not commutative**: `a * b ≠ b * a`.
|
|
95
|
+
* The result applies rotation {@linkcode b} first, then {@linkcode a}.
|
|
96
|
+
*
|
|
97
|
+
* @param a - First quaternion (applied second)
|
|
98
|
+
* @param b - Second quaternion (applied first)
|
|
99
|
+
* @returns A new {@linkcode PQuat} representing the combined rotation
|
|
100
|
+
*/
|
|
48
101
|
multiply(a: PQuat, b: PQuat): PQuat {
|
|
49
102
|
return {
|
|
50
103
|
x: a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y,
|
|
@@ -54,31 +107,72 @@ export abstract class PluginQuatApi {
|
|
|
54
107
|
}
|
|
55
108
|
}
|
|
56
109
|
|
|
57
|
-
/**
|
|
110
|
+
/**
|
|
111
|
+
* Compute the conjugate of a quaternion.
|
|
112
|
+
*
|
|
113
|
+
* For unit quaternions the conjugate equals the inverse.
|
|
114
|
+
*
|
|
115
|
+
* @param q - The quaternion
|
|
116
|
+
* @returns A new {@linkcode PQuat} with negated vector part `(-x, -y, -z, w)`
|
|
117
|
+
*/
|
|
58
118
|
conjugate(q: PQuat): PQuat {
|
|
59
119
|
return { x: -q.x, y: -q.y, z: -q.z, w: q.w }
|
|
60
120
|
}
|
|
61
121
|
|
|
62
|
-
/**
|
|
122
|
+
/**
|
|
123
|
+
* Compute the length (magnitude) of a quaternion.
|
|
124
|
+
*
|
|
125
|
+
* @param q - The quaternion
|
|
126
|
+
* @returns The magnitude `√(x² + y² + z² + w²)`
|
|
127
|
+
*/
|
|
63
128
|
length(q: PQuat): number {
|
|
64
129
|
return Math.sqrt(q.x * q.x + q.y * q.y + q.z * q.z + q.w * q.w)
|
|
65
130
|
}
|
|
66
131
|
|
|
67
|
-
/**
|
|
132
|
+
/**
|
|
133
|
+
* Normalize a quaternion to unit length.
|
|
134
|
+
*
|
|
135
|
+
* @param q - The quaternion to normalize
|
|
136
|
+
* @returns A new unit-length {@linkcode PQuat}, or identity if the input has zero length
|
|
137
|
+
*/
|
|
68
138
|
normalize(q: PQuat): PQuat {
|
|
69
139
|
const len = this.length(q)
|
|
70
140
|
if (len === 0) return { x: 0, y: 0, z: 0, w: 1 }
|
|
71
141
|
return { x: q.x / len, y: q.y / len, z: q.z / len, w: q.w / len }
|
|
72
142
|
}
|
|
73
143
|
|
|
74
|
-
/**
|
|
144
|
+
/**
|
|
145
|
+
* Compute the inverse of a quaternion.
|
|
146
|
+
*
|
|
147
|
+
* The inverse satisfies `q * inverse(q) = identity`.
|
|
148
|
+
*
|
|
149
|
+
* @param q - The quaternion to invert
|
|
150
|
+
* @returns A new {@linkcode PQuat} that is the multiplicative inverse,
|
|
151
|
+
* or identity if the input has zero length
|
|
152
|
+
*/
|
|
75
153
|
inverse(q: PQuat): PQuat {
|
|
76
154
|
const lenSq = q.x * q.x + q.y * q.y + q.z * q.z + q.w * q.w
|
|
77
155
|
if (lenSq === 0) return { x: 0, y: 0, z: 0, w: 1 }
|
|
78
156
|
return { x: -q.x / lenSq, y: -q.y / lenSq, z: -q.z / lenSq, w: q.w / lenSq }
|
|
79
157
|
}
|
|
80
158
|
|
|
81
|
-
/**
|
|
159
|
+
/**
|
|
160
|
+
* Rotate a 3D vector by a quaternion.
|
|
161
|
+
*
|
|
162
|
+
* Computes `q * v * conjugate(q)` using the Hamilton product.
|
|
163
|
+
*
|
|
164
|
+
* @param q - The rotation quaternion (should be unit-length)
|
|
165
|
+
* @param v - The vector to rotate as a {@linkcode PVec3}
|
|
166
|
+
* @returns A new rotated {@linkcode PVec3}
|
|
167
|
+
*
|
|
168
|
+
* # Example
|
|
169
|
+
* ```ts
|
|
170
|
+
* const { vec3, quat } = snaptrude.core.math
|
|
171
|
+
* const rot = quat.fromAxisAngle(vec3.new(0, 1, 0), Math.PI / 2)
|
|
172
|
+
* const rotated = quat.rotateVec3(rot, vec3.new(1, 0, 0))
|
|
173
|
+
* // ≈ { x: 0, y: 0, z: -1 }
|
|
174
|
+
* ```
|
|
175
|
+
*/
|
|
82
176
|
rotateVec3(q: PQuat, v: PVec3): PVec3 {
|
|
83
177
|
const vq: PQuat = { x: v.x, y: v.y, z: v.z, w: 0 }
|
|
84
178
|
const conj = this.conjugate(q)
|
|
@@ -86,7 +180,15 @@ export abstract class PluginQuatApi {
|
|
|
86
180
|
return { x: result.x, y: result.y, z: result.z }
|
|
87
181
|
}
|
|
88
182
|
|
|
89
|
-
/**
|
|
183
|
+
/**
|
|
184
|
+
* Convert a quaternion to axis-angle representation.
|
|
185
|
+
*
|
|
186
|
+
* @param q - The quaternion to convert
|
|
187
|
+
* @returns An object with:
|
|
188
|
+
* - `axis` — The rotation axis as a unit {@linkcode PVec3}
|
|
189
|
+
* (defaults to `(1, 0, 0)` when angle is zero)
|
|
190
|
+
* - `angle` — The rotation angle in **radians**
|
|
191
|
+
*/
|
|
90
192
|
toAxisAngle(q: PQuat): { axis: PVec3; angle: number } {
|
|
91
193
|
const nq = this.normalize(q)
|
|
92
194
|
const angle = 2 * Math.acos(Math.min(1, Math.max(-1, nq.w)))
|
|
@@ -100,7 +202,16 @@ export abstract class PluginQuatApi {
|
|
|
100
202
|
}
|
|
101
203
|
}
|
|
102
204
|
|
|
103
|
-
/**
|
|
205
|
+
/**
|
|
206
|
+
* Spherical linear interpolation between two quaternions.
|
|
207
|
+
*
|
|
208
|
+
* Produces smooth constant-speed rotation between {@linkcode a} and {@linkcode b}.
|
|
209
|
+
*
|
|
210
|
+
* @param a - Start quaternion (returned when `t = 0`)
|
|
211
|
+
* @param b - End quaternion (returned when `t = 1`)
|
|
212
|
+
* @param t - Interpolation factor, typically in `[0, 1]`
|
|
213
|
+
* @returns A new interpolated {@linkcode PQuat}
|
|
214
|
+
*/
|
|
104
215
|
slerp(a: PQuat, b: PQuat, t: number): PQuat {
|
|
105
216
|
let cosHalf = a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w
|
|
106
217
|
let bx = b.x, by = b.y, bz = b.z, bw = b.w
|
|
@@ -123,17 +234,41 @@ export abstract class PluginQuatApi {
|
|
|
123
234
|
}
|
|
124
235
|
}
|
|
125
236
|
|
|
126
|
-
/**
|
|
237
|
+
/**
|
|
238
|
+
* Compute the dot product of two quaternions.
|
|
239
|
+
*
|
|
240
|
+
* A value close to `1` or `-1` indicates similar orientations;
|
|
241
|
+
* a value close to `0` indicates maximally different orientations.
|
|
242
|
+
*
|
|
243
|
+
* @param a - First quaternion
|
|
244
|
+
* @param b - Second quaternion
|
|
245
|
+
* @returns The scalar dot product
|
|
246
|
+
*/
|
|
127
247
|
dot(a: PQuat, b: PQuat): number {
|
|
128
248
|
return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w
|
|
129
249
|
}
|
|
130
250
|
|
|
131
|
-
/**
|
|
251
|
+
/**
|
|
252
|
+
* Check if two quaternions are exactly equal (strict equality per component).
|
|
253
|
+
*
|
|
254
|
+
* For floating-point comparisons, prefer {@linkcode PluginQuatApi.equalsApprox}.
|
|
255
|
+
*
|
|
256
|
+
* @param a - First quaternion
|
|
257
|
+
* @param b - Second quaternion
|
|
258
|
+
* @returns `true` if all components match exactly
|
|
259
|
+
*/
|
|
132
260
|
equals(a: PQuat, b: PQuat): boolean {
|
|
133
261
|
return a.x === b.x && a.y === b.y && a.z === b.z && a.w === b.w
|
|
134
262
|
}
|
|
135
263
|
|
|
136
|
-
/**
|
|
264
|
+
/**
|
|
265
|
+
* Check if two quaternions are approximately equal within a tolerance.
|
|
266
|
+
*
|
|
267
|
+
* @param a - First quaternion
|
|
268
|
+
* @param b - Second quaternion
|
|
269
|
+
* @param epsilon - Maximum allowed difference per component (default `1e-6`)
|
|
270
|
+
* @returns `true` if `|a[i] - b[i]| < epsilon` for all components
|
|
271
|
+
*/
|
|
137
272
|
equalsApprox(a: PQuat, b: PQuat, epsilon: number = 1e-6): boolean {
|
|
138
273
|
return (
|
|
139
274
|
Math.abs(a.x - b.x) < epsilon &&
|
|
@@ -144,6 +279,12 @@ export abstract class PluginQuatApi {
|
|
|
144
279
|
}
|
|
145
280
|
}
|
|
146
281
|
|
|
282
|
+
/**
|
|
283
|
+
* A quaternion with `x`, `y`, `z`, and `w` components.
|
|
284
|
+
*
|
|
285
|
+
* Used for representing 3D rotations without gimbal lock.
|
|
286
|
+
* Equivalent to `BABYLON.Quaternion`.
|
|
287
|
+
*/
|
|
147
288
|
export const PQuat = z.object({
|
|
148
289
|
x: z.number(),
|
|
149
290
|
y: z.number(),
|
|
@@ -1,34 +1,93 @@
|
|
|
1
1
|
import * as z from "zod"
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* 3D vector operations for positions, directions, and displacements.
|
|
5
|
+
*
|
|
6
|
+
* All methods are **pure** — they return new {@linkcode PVec3} values
|
|
7
|
+
* without mutating the inputs.
|
|
8
|
+
*
|
|
9
|
+
* Accessed via `snaptrude.core.math.vec3`.
|
|
10
|
+
*/
|
|
3
11
|
export abstract class PluginVec3Api {
|
|
4
12
|
constructor() {}
|
|
5
13
|
|
|
6
|
-
/**
|
|
14
|
+
/**
|
|
15
|
+
* Create a new 3D vector.
|
|
16
|
+
*
|
|
17
|
+
* @param x - The X component
|
|
18
|
+
* @param y - The Y component
|
|
19
|
+
* @param z - The Z component
|
|
20
|
+
* @returns A new {@linkcode PVec3}
|
|
21
|
+
*
|
|
22
|
+
* # Example
|
|
23
|
+
* ```ts
|
|
24
|
+
* const position = snaptrude.core.math.vec3.new(1, 2, 3)
|
|
25
|
+
* // { x: 1, y: 2, z: 3 }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
7
28
|
new(x: number, y: number, z: number): PVec3 {
|
|
8
29
|
return { x, y, z }
|
|
9
30
|
}
|
|
10
31
|
|
|
11
|
-
/**
|
|
32
|
+
/**
|
|
33
|
+
* Add two vectors component-wise.
|
|
34
|
+
*
|
|
35
|
+
* @param a - First vector
|
|
36
|
+
* @param b - Second vector
|
|
37
|
+
* @returns A new {@linkcode PVec3} where each component is `a[i] + b[i]`
|
|
38
|
+
*/
|
|
12
39
|
add(a: PVec3, b: PVec3): PVec3 {
|
|
13
40
|
return { x: a.x + b.x, y: a.y + b.y, z: a.z + b.z }
|
|
14
41
|
}
|
|
15
42
|
|
|
16
|
-
/**
|
|
43
|
+
/**
|
|
44
|
+
* Subtract vector {@linkcode b} from vector {@linkcode a} component-wise.
|
|
45
|
+
*
|
|
46
|
+
* @param a - Vector to subtract from
|
|
47
|
+
* @param b - Vector to subtract
|
|
48
|
+
* @returns A new {@linkcode PVec3} where each component is `a[i] - b[i]`
|
|
49
|
+
*/
|
|
17
50
|
subtract(a: PVec3, b: PVec3): PVec3 {
|
|
18
51
|
return { x: a.x - b.x, y: a.y - b.y, z: a.z - b.z }
|
|
19
52
|
}
|
|
20
53
|
|
|
21
|
-
/**
|
|
54
|
+
/**
|
|
55
|
+
* Scale a vector by a scalar value.
|
|
56
|
+
*
|
|
57
|
+
* @param v - The vector to scale
|
|
58
|
+
* @param scalar - The scalar multiplier
|
|
59
|
+
* @returns A new {@linkcode PVec3} where each component is `v[i] * scalar`
|
|
60
|
+
*/
|
|
22
61
|
scale(v: PVec3, scalar: number): PVec3 {
|
|
23
62
|
return { x: v.x * scalar, y: v.y * scalar, z: v.z * scalar }
|
|
24
63
|
}
|
|
25
64
|
|
|
26
|
-
/**
|
|
65
|
+
/**
|
|
66
|
+
* Compute the dot product of two vectors.
|
|
67
|
+
*
|
|
68
|
+
* The dot product equals `|a| * |b| * cos(θ)` where `θ` is the angle
|
|
69
|
+
* between the vectors. Useful for checking orthogonality (result = 0)
|
|
70
|
+
* or projection.
|
|
71
|
+
*
|
|
72
|
+
* @param a - First vector
|
|
73
|
+
* @param b - Second vector
|
|
74
|
+
* @returns The scalar dot product
|
|
75
|
+
*/
|
|
27
76
|
dot(a: PVec3, b: PVec3): number {
|
|
28
77
|
return a.x * b.x + a.y * b.y + a.z * b.z
|
|
29
78
|
}
|
|
30
79
|
|
|
31
|
-
/**
|
|
80
|
+
/**
|
|
81
|
+
* Compute the cross product of two vectors.
|
|
82
|
+
*
|
|
83
|
+
* The result is a vector perpendicular to both inputs, with magnitude
|
|
84
|
+
* equal to the area of the parallelogram they span. Useful for computing
|
|
85
|
+
* surface normals.
|
|
86
|
+
*
|
|
87
|
+
* @param a - First vector
|
|
88
|
+
* @param b - Second vector
|
|
89
|
+
* @returns A new {@linkcode PVec3} perpendicular to both {@linkcode a} and {@linkcode b}
|
|
90
|
+
*/
|
|
32
91
|
cross(a: PVec3, b: PVec3): PVec3 {
|
|
33
92
|
return {
|
|
34
93
|
x: a.y * b.z - a.z * b.y,
|
|
@@ -37,29 +96,61 @@ export abstract class PluginVec3Api {
|
|
|
37
96
|
}
|
|
38
97
|
}
|
|
39
98
|
|
|
40
|
-
/**
|
|
99
|
+
/**
|
|
100
|
+
* Compute the length (magnitude) of a vector.
|
|
101
|
+
*
|
|
102
|
+
* @param v - The vector
|
|
103
|
+
* @returns The Euclidean length `√(x² + y² + z²)`
|
|
104
|
+
*/
|
|
41
105
|
length(v: PVec3): number {
|
|
42
106
|
return Math.sqrt(v.x * v.x + v.y * v.y + v.z * v.z)
|
|
43
107
|
}
|
|
44
108
|
|
|
45
|
-
/**
|
|
109
|
+
/**
|
|
110
|
+
* Compute the squared length of a vector.
|
|
111
|
+
*
|
|
112
|
+
* Avoids the `sqrt` call — prefer this over {@linkcode PluginVec3Api.length}
|
|
113
|
+
* when comparing relative magnitudes.
|
|
114
|
+
*
|
|
115
|
+
* @param v - The vector
|
|
116
|
+
* @returns The squared length `x² + y² + z²`
|
|
117
|
+
*/
|
|
46
118
|
lengthSquared(v: PVec3): number {
|
|
47
119
|
return v.x * v.x + v.y * v.y + v.z * v.z
|
|
48
120
|
}
|
|
49
121
|
|
|
50
|
-
/**
|
|
122
|
+
/**
|
|
123
|
+
* Normalize a vector to unit length.
|
|
124
|
+
*
|
|
125
|
+
* @param v - The vector to normalize
|
|
126
|
+
* @returns A new unit-length {@linkcode PVec3} in the same direction,
|
|
127
|
+
* or a zero vector `(0, 0, 0)` if the input has zero length
|
|
128
|
+
*/
|
|
51
129
|
normalize(v: PVec3): PVec3 {
|
|
52
130
|
const len = this.length(v)
|
|
53
131
|
if (len === 0) return { x: 0, y: 0, z: 0 }
|
|
54
132
|
return { x: v.x / len, y: v.y / len, z: v.z / len }
|
|
55
133
|
}
|
|
56
134
|
|
|
57
|
-
/**
|
|
135
|
+
/**
|
|
136
|
+
* Compute the Euclidean distance between two points.
|
|
137
|
+
*
|
|
138
|
+
* @param a - First point
|
|
139
|
+
* @param b - Second point
|
|
140
|
+
* @returns The distance `|a - b|`
|
|
141
|
+
*/
|
|
58
142
|
distance(a: PVec3, b: PVec3): number {
|
|
59
143
|
return this.length(this.subtract(a, b))
|
|
60
144
|
}
|
|
61
145
|
|
|
62
|
-
/**
|
|
146
|
+
/**
|
|
147
|
+
* Linearly interpolate between two vectors.
|
|
148
|
+
*
|
|
149
|
+
* @param a - Start vector (returned when `t = 0`)
|
|
150
|
+
* @param b - End vector (returned when `t = 1`)
|
|
151
|
+
* @param t - Interpolation factor, typically in `[0, 1]`
|
|
152
|
+
* @returns A new {@linkcode PVec3} at `a + (b - a) * t`
|
|
153
|
+
*/
|
|
63
154
|
lerp(a: PVec3, b: PVec3, t: number): PVec3 {
|
|
64
155
|
return {
|
|
65
156
|
x: a.x + (b.x - a.x) * t,
|
|
@@ -68,17 +159,37 @@ export abstract class PluginVec3Api {
|
|
|
68
159
|
}
|
|
69
160
|
}
|
|
70
161
|
|
|
71
|
-
/**
|
|
162
|
+
/**
|
|
163
|
+
* Negate a vector (reverse its direction).
|
|
164
|
+
*
|
|
165
|
+
* @param v - The vector to negate
|
|
166
|
+
* @returns A new {@linkcode PVec3} with all components negated
|
|
167
|
+
*/
|
|
72
168
|
negate(v: PVec3): PVec3 {
|
|
73
169
|
return { x: -v.x, y: -v.y, z: -v.z }
|
|
74
170
|
}
|
|
75
171
|
|
|
76
|
-
/**
|
|
172
|
+
/**
|
|
173
|
+
* Check if two vectors are exactly equal (strict equality per component).
|
|
174
|
+
*
|
|
175
|
+
* For floating-point comparisons, prefer {@linkcode PluginVec3Api.equalsApprox}.
|
|
176
|
+
*
|
|
177
|
+
* @param a - First vector
|
|
178
|
+
* @param b - Second vector
|
|
179
|
+
* @returns `true` if all components match exactly
|
|
180
|
+
*/
|
|
77
181
|
equals(a: PVec3, b: PVec3): boolean {
|
|
78
182
|
return a.x === b.x && a.y === b.y && a.z === b.z
|
|
79
183
|
}
|
|
80
184
|
|
|
81
|
-
/**
|
|
185
|
+
/**
|
|
186
|
+
* Check if two vectors are approximately equal within a tolerance.
|
|
187
|
+
*
|
|
188
|
+
* @param a - First vector
|
|
189
|
+
* @param b - Second vector
|
|
190
|
+
* @param epsilon - Maximum allowed difference per component (default `1e-6`)
|
|
191
|
+
* @returns `true` if `|a[i] - b[i]| < epsilon` for all components
|
|
192
|
+
*/
|
|
82
193
|
equalsApprox(a: PVec3, b: PVec3, epsilon: number = 1e-6): boolean {
|
|
83
194
|
return (
|
|
84
195
|
Math.abs(a.x - b.x) < epsilon &&
|
|
@@ -88,6 +199,12 @@ export abstract class PluginVec3Api {
|
|
|
88
199
|
}
|
|
89
200
|
}
|
|
90
201
|
|
|
202
|
+
/**
|
|
203
|
+
* A 3D vector with `x`, `y`, and `z` components.
|
|
204
|
+
*
|
|
205
|
+
* Used throughout the plugin API for positions, directions, displacements,
|
|
206
|
+
* and Euler rotation angles. Equivalent to `BABYLON.Vector3`.
|
|
207
|
+
*/
|
|
91
208
|
export const PVec3 = z.object({
|
|
92
209
|
x: z.number(),
|
|
93
210
|
y: z.number(),
|
package/src/api/entity/index.ts
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
1
|
import { PluginSpaceApi } from "./space"
|
|
2
2
|
import { PluginStoryApi } from "./story"
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* CRUD operations on Snaptrude building entities.
|
|
6
|
+
*
|
|
7
|
+
* - {@linkcode PluginEntityApi.space} — Create, query, and delete spaces (rooms/masses)
|
|
8
|
+
* - {@linkcode PluginEntityApi.story} — Create, query, and update stories (floors)
|
|
9
|
+
*/
|
|
4
10
|
export abstract class PluginEntityApi {
|
|
11
|
+
/** Space (room/mass) operations. See {@linkcode PluginSpaceApi}. */
|
|
5
12
|
public abstract space: PluginSpaceApi
|
|
13
|
+
/** Story (floor/storey) operations. See {@linkcode PluginStoryApi}. */
|
|
6
14
|
public abstract story: PluginStoryApi
|
|
7
15
|
|
|
8
16
|
constructor() {}
|