@rocon/balcan 0.0.1

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 (41) hide show
  1. package/dist/core/bitmap.balcan.d.ts +4 -0
  2. package/dist/core/bitmap.balcan.js +22 -0
  3. package/dist/core/core.balcan.d.ts +73 -0
  4. package/dist/core/core.balcan.js +136 -0
  5. package/dist/core/geo.balcan.d.ts +109 -0
  6. package/dist/core/geo.balcan.js +255 -0
  7. package/dist/core/math.balcan.d.ts +35 -0
  8. package/dist/core/math.balcan.js +90 -0
  9. package/dist/core/ts-matrix/Matrix.d.ts +111 -0
  10. package/dist/core/ts-matrix/Matrix.js +243 -0
  11. package/dist/core/ts-matrix/Quat.d.ts +168 -0
  12. package/dist/core/ts-matrix/Quat.js +435 -0
  13. package/dist/core/ts-matrix/Vector.d.ts +145 -0
  14. package/dist/core/ts-matrix/Vector.js +252 -0
  15. package/dist/core/ts-matrix/constants.d.ts +1 -0
  16. package/dist/core/ts-matrix/constants.js +1 -0
  17. package/dist/core/ts-matrix/ts-matrix.d.ts +3 -0
  18. package/dist/core/ts-matrix/ts-matrix.js +31 -0
  19. package/dist/core/types.balcan.d.ts +93 -0
  20. package/dist/core/types.balcan.js +1 -0
  21. package/dist/core/util.balcan.d.ts +23 -0
  22. package/dist/core/util.balcan.js +71 -0
  23. package/dist/core/viewport.balcan.d.ts +27 -0
  24. package/dist/core/viewport.balcan.js +60 -0
  25. package/dist/index.d.ts +13 -0
  26. package/dist/index.js +3 -0
  27. package/dist/mixin/mixin.d.ts +7 -0
  28. package/dist/mixin/mixin.js +7 -0
  29. package/dist/mixin/mixin.type.d.ts +2 -0
  30. package/dist/mixin/mixin.type.js +1 -0
  31. package/dist/staffs/image.staff.d.ts +27 -0
  32. package/dist/staffs/image.staff.js +74 -0
  33. package/dist/staffs/index.d.ts +3 -0
  34. package/dist/staffs/index.js +3 -0
  35. package/dist/staffs/wheelZoom.staff.d.ts +21 -0
  36. package/dist/staffs/wheelZoom.staff.js +45 -0
  37. package/dist/staffs/windowResizeObserver.staff.d.ts +8 -0
  38. package/dist/staffs/windowResizeObserver.staff.js +17 -0
  39. package/dist/types/geometry.d.ts +68 -0
  40. package/dist/types/index.d.ts +1 -0
  41. package/package.json +39 -0
@@ -0,0 +1,435 @@
1
+ import { EPSILON } from './constants';
2
+ /**
3
+ * Class representing a Quaternion for 3D rotations
4
+ */
5
+ export default class Quat {
6
+ /** Values of the quaternion as [x, y, z, w] */
7
+ _values;
8
+ constructor(values) {
9
+ // Create quaternion with identity by default [0, 0, 0, 1]
10
+ this._values = [0, 0, 0, 1];
11
+ if (values) {
12
+ this.xyzw = values;
13
+ }
14
+ }
15
+ get values() {
16
+ return this._values;
17
+ }
18
+ /**
19
+ * Set values into the quaternion.
20
+ * @param newValues Array of new values [x, y, z, w].
21
+ */
22
+ set values(newValues) {
23
+ const minSize = Math.min(4, newValues.length);
24
+ for (let i = 0; i < minSize; i++) {
25
+ this._values[i] = newValues[i];
26
+ }
27
+ }
28
+ get x() {
29
+ return this._values[0];
30
+ }
31
+ set x(value) {
32
+ this._values[0] = value;
33
+ }
34
+ get y() {
35
+ return this._values[1];
36
+ }
37
+ set y(value) {
38
+ this._values[1] = value;
39
+ }
40
+ get z() {
41
+ return this._values[2];
42
+ }
43
+ set z(value) {
44
+ this._values[2] = value;
45
+ }
46
+ get w() {
47
+ return this._values[3];
48
+ }
49
+ set w(value) {
50
+ this._values[3] = value;
51
+ }
52
+ get xy() {
53
+ return [this._values[0], this._values[1]];
54
+ }
55
+ set xy(values) {
56
+ this._values[0] = values[0];
57
+ this._values[1] = values[1];
58
+ }
59
+ get xyz() {
60
+ return [this._values[0], this._values[1], this._values[2]];
61
+ }
62
+ set xyz(values) {
63
+ this._values[0] = values[0];
64
+ this._values[1] = values[1];
65
+ this._values[2] = values[2];
66
+ }
67
+ get xyzw() {
68
+ return [this._values[0], this._values[1], this._values[2], this._values[3]];
69
+ }
70
+ set xyzw(values) {
71
+ this._values[0] = values[0];
72
+ this._values[1] = values[1];
73
+ this._values[2] = values[2];
74
+ this._values[3] = values[3];
75
+ }
76
+ /**
77
+ * Get an identity quaternion (rotation by 0 degrees)
78
+ * @return A new identity quaternion
79
+ */
80
+ static identity() {
81
+ return new Quat([0, 0, 0, 1]);
82
+ }
83
+ /**
84
+ * Get a quaternion value at a specific index
85
+ * @param index Index from 0 to 3
86
+ * @return The value at the specified index
87
+ */
88
+ at(index) {
89
+ return this._values[index];
90
+ }
91
+ /**
92
+ * Reset all quaternion values to 0
93
+ */
94
+ reset() {
95
+ for (let i = 0; i < 4; i++) {
96
+ this._values[i] = 0;
97
+ }
98
+ }
99
+ /**
100
+ * Copy the quaternion to another quaternion
101
+ * @param dest Destination quaternion (optional)
102
+ * @return The destination quaternion
103
+ */
104
+ copy(dest) {
105
+ if (!dest) {
106
+ dest = new Quat();
107
+ }
108
+ for (let i = 0; i < 4; i++) {
109
+ dest._values[i] = this._values[i];
110
+ }
111
+ return dest;
112
+ }
113
+ /**
114
+ * Calculate the roll angle (rotation around x-axis)
115
+ * @return Roll angle in radians
116
+ */
117
+ roll() {
118
+ const x = this.x;
119
+ const y = this.y;
120
+ const z = this.z;
121
+ const w = this.w;
122
+ return Math.atan2(2.0 * (x * y + w * z), w * w + x * x - y * y - z * z);
123
+ }
124
+ /**
125
+ * Calculate the pitch angle (rotation around y-axis)
126
+ * @return Pitch angle in radians
127
+ */
128
+ pitch() {
129
+ const x = this.x;
130
+ const y = this.y;
131
+ const z = this.z;
132
+ const w = this.w;
133
+ return Math.atan2(2.0 * (y * z + w * x), w * w - x * x - y * y + z * z);
134
+ }
135
+ /**
136
+ * Calculate the yaw angle (rotation around z-axis)
137
+ * @return Yaw angle in radians
138
+ */
139
+ yaw() {
140
+ return Math.asin(2.0 * (this.x * this.z - this.w * this.y));
141
+ }
142
+ /**
143
+ * Check if two quaternions are equal within a threshold
144
+ * @param quat The quaternion to compare against
145
+ * @param threshold The threshold for comparison (default: EPSILON)
146
+ * @return True if quaternions are equal within threshold
147
+ */
148
+ equals(quat, threshold = EPSILON) {
149
+ for (let i = 0; i < 4; i++) {
150
+ if (Math.abs(this._values[i] - quat.at(i)) > threshold) {
151
+ return false;
152
+ }
153
+ }
154
+ return true;
155
+ }
156
+ /**
157
+ * Set the quaternion as an identity quaternion
158
+ * @return This quaternion for method chaining
159
+ */
160
+ setIdentity() {
161
+ this.x = 0;
162
+ this.y = 0;
163
+ this.z = 0;
164
+ this.w = 1;
165
+ return this;
166
+ }
167
+ /**
168
+ * Calculate the w component based on x, y, z components
169
+ * @return This quaternion for method chaining
170
+ */
171
+ calculateW() {
172
+ const x = this.x;
173
+ const y = this.y;
174
+ const z = this.z;
175
+ this.w = -Math.sqrt(Math.abs(1.0 - x * x - y * y - z * z));
176
+ return this;
177
+ }
178
+ /**
179
+ * Compute the inverse of the quaternion
180
+ * @return This quaternion for method chaining
181
+ */
182
+ inverse() {
183
+ const dot = Quat.dot(this, this);
184
+ if (!dot) {
185
+ this.xyzw = [0, 0, 0, 0];
186
+ return this;
187
+ }
188
+ const invDot = dot ? 1.0 / dot : 0;
189
+ this.x *= -invDot;
190
+ this.y *= -invDot;
191
+ this.z *= -invDot;
192
+ this.w *= invDot;
193
+ return this;
194
+ }
195
+ /**
196
+ * Compute the conjugate of the quaternion
197
+ * @return This quaternion for method chaining
198
+ */
199
+ conjugate() {
200
+ this._values[0] *= -1;
201
+ this._values[1] *= -1;
202
+ this._values[2] *= -1;
203
+ return this;
204
+ }
205
+ /**
206
+ * Calculate the length (magnitude) of the quaternion
207
+ * @return The length of the quaternion
208
+ */
209
+ length() {
210
+ const x = this.x;
211
+ const y = this.y;
212
+ const z = this.z;
213
+ const w = this.w;
214
+ return Math.sqrt(x * x + y * y + z * z + w * w);
215
+ }
216
+ /**
217
+ * Normalize the quaternion to unit length
218
+ * @param dest Destination quaternion (optional)
219
+ * @return The normalized quaternion
220
+ */
221
+ normalize(dest) {
222
+ const target = dest || this;
223
+ const x = this.x;
224
+ const y = this.y;
225
+ const z = this.z;
226
+ const w = this.w;
227
+ let length = Math.sqrt(x * x + y * y + z * z + w * w);
228
+ if (!length) {
229
+ target.x = 0;
230
+ target.y = 0;
231
+ target.z = 0;
232
+ target.w = 0;
233
+ return target;
234
+ }
235
+ length = 1 / length;
236
+ target.x = x * length;
237
+ target.y = y * length;
238
+ target.z = z * length;
239
+ target.w = w * length;
240
+ return target;
241
+ }
242
+ /**
243
+ * Add another quaternion to this quaternion
244
+ * @param other The quaternion to add
245
+ * @return This quaternion for method chaining
246
+ */
247
+ add(other) {
248
+ for (let i = 0; i < 4; i++) {
249
+ this._values[i] += other.at(i);
250
+ }
251
+ return this;
252
+ }
253
+ /**
254
+ * Multiply this quaternion by another quaternion
255
+ * @param other The quaternion to multiply with
256
+ * @return This quaternion for method chaining
257
+ */
258
+ multiply(other) {
259
+ const q1x = this._values[0];
260
+ const q1y = this._values[1];
261
+ const q1z = this._values[2];
262
+ const q1w = this._values[3];
263
+ const q2x = other.x;
264
+ const q2y = other.y;
265
+ const q2z = other.z;
266
+ const q2w = other.w;
267
+ this.x = q1x * q2w + q1w * q2x + q1y * q2z - q1z * q2y;
268
+ this.y = q1y * q2w + q1w * q2y + q1z * q2x - q1x * q2z;
269
+ this.z = q1z * q2w + q1w * q2z + q1x * q2y - q1y * q2x;
270
+ this.w = q1w * q2w - q1x * q2x - q1y * q2y - q1z * q2z;
271
+ return this;
272
+ }
273
+ /**
274
+ * Calculate the dot product of two quaternions
275
+ * @param q1 First quaternion
276
+ * @param q2 Second quaternion
277
+ * @return The dot product
278
+ */
279
+ static dot(q1, q2) {
280
+ return q1.x * q2.x + q1.y * q2.y + q1.z * q2.z + q1.w * q2.w;
281
+ }
282
+ /**
283
+ * Calculate the sum of two quaternions
284
+ * @param q1 First quaternion
285
+ * @param q2 Second quaternion
286
+ * @return A new quaternion with the sum
287
+ */
288
+ static sum(q1, q2) {
289
+ const dest = new Quat();
290
+ dest.x = q1.x + q2.x;
291
+ dest.y = q1.y + q2.y;
292
+ dest.z = q1.z + q2.z;
293
+ dest.w = q1.w + q2.w;
294
+ return dest;
295
+ }
296
+ /**
297
+ * Calculate the product of two quaternions
298
+ * @param q1 First quaternion
299
+ * @param q2 Second quaternion
300
+ * @return A new quaternion with the product
301
+ */
302
+ static product(q1, q2) {
303
+ const dest = new Quat();
304
+ const q1x = q1.x;
305
+ const q1y = q1.y;
306
+ const q1z = q1.z;
307
+ const q1w = q1.w;
308
+ const q2x = q2.x;
309
+ const q2y = q2.y;
310
+ const q2z = q2.z;
311
+ const q2w = q2.w;
312
+ dest.x = q1x * q2w + q1w * q2x + q1y * q2z - q1z * q2y;
313
+ dest.y = q1y * q2w + q1w * q2y + q1z * q2x - q1x * q2z;
314
+ dest.z = q1z * q2w + q1w * q2z + q1x * q2y - q1y * q2x;
315
+ dest.w = q1w * q2w - q1x * q2x - q1y * q2y - q1z * q2z;
316
+ return dest;
317
+ }
318
+ /**
319
+ * Calculate the cross product of two quaternions
320
+ * @param q1 First quaternion
321
+ * @param q2 Second quaternion
322
+ * @return A new quaternion with the cross product
323
+ */
324
+ static cross(q1, q2) {
325
+ const dest = new Quat();
326
+ const q1x = q1.x;
327
+ const q1y = q1.y;
328
+ const q1z = q1.z;
329
+ const q1w = q1.w;
330
+ const q2x = q2.x;
331
+ const q2y = q2.y;
332
+ const q2z = q2.z;
333
+ const q2w = q2.w;
334
+ dest.x = q1w * q2z + q1z * q2w + q1x * q2y - q1y * q2x;
335
+ dest.y = q1w * q2w - q1x * q2x - q1y * q2y - q1z * q2z;
336
+ dest.z = q1w * q2x + q1x * q2w + q1y * q2z - q1z * q2y;
337
+ dest.w = q1w * q2y + q1y * q2w + q1z * q2x - q1x * q2z;
338
+ return dest;
339
+ }
340
+ /**
341
+ * Spherical linear interpolation between two quaternions with short path
342
+ * @param q1 First quaternion
343
+ * @param q2 Second quaternion
344
+ * @param time Interpolation parameter (0 to 1)
345
+ * @return A new interpolated quaternion
346
+ */
347
+ static shortMix(q1, q2, time) {
348
+ const dest = new Quat();
349
+ if (time <= 0.0) {
350
+ dest.xyzw = q1.xyzw;
351
+ return dest;
352
+ }
353
+ else if (time >= 1.0) {
354
+ dest.xyzw = q2.xyzw;
355
+ return dest;
356
+ }
357
+ let cos = Quat.dot(q1, q2);
358
+ const q2a = q2.copy();
359
+ if (cos < 0.0) {
360
+ q2a.inverse();
361
+ cos = -cos;
362
+ }
363
+ let k0;
364
+ let k1;
365
+ if (cos > 0.9999) {
366
+ k0 = 1 - time;
367
+ k1 = 0 + time;
368
+ }
369
+ else {
370
+ const sin = Math.sqrt(1 - cos * cos);
371
+ const angle = Math.atan2(sin, cos);
372
+ const oneOverSin = 1 / sin;
373
+ k0 = Math.sin((1 - time) * angle) * oneOverSin;
374
+ k1 = Math.sin((0 + time) * angle) * oneOverSin;
375
+ }
376
+ dest.x = k0 * q1.x + k1 * q2a.x;
377
+ dest.y = k0 * q1.y + k1 * q2a.y;
378
+ dest.z = k0 * q1.z + k1 * q2a.z;
379
+ dest.w = k0 * q1.w + k1 * q2a.w;
380
+ return dest;
381
+ }
382
+ /**
383
+ * Spherical linear interpolation between two quaternions
384
+ * @param q1 First quaternion
385
+ * @param q2 Second quaternion
386
+ * @param time Interpolation parameter (0 to 1)
387
+ * @return A new interpolated quaternion
388
+ */
389
+ static mix(q1, q2, time) {
390
+ const dest = new Quat();
391
+ const cosHalfTheta = q1.x * q2.x + q1.y * q2.y + q1.z * q2.z + q1.w * q2.w;
392
+ if (Math.abs(cosHalfTheta) >= 1.0) {
393
+ dest.xyzw = q1.xyzw;
394
+ return dest;
395
+ }
396
+ const halfTheta = Math.acos(cosHalfTheta);
397
+ const sinHalfTheta = Math.sqrt(1.0 - cosHalfTheta * cosHalfTheta);
398
+ if (Math.abs(sinHalfTheta) < 0.001) {
399
+ dest.x = q1.x * 0.5 + q2.x * 0.5;
400
+ dest.y = q1.y * 0.5 + q2.y * 0.5;
401
+ dest.z = q1.z * 0.5 + q2.z * 0.5;
402
+ dest.w = q1.w * 0.5 + q2.w * 0.5;
403
+ return dest;
404
+ }
405
+ const ratioA = Math.sin((1 - time) * halfTheta) / sinHalfTheta;
406
+ const ratioB = Math.sin(time * halfTheta) / sinHalfTheta;
407
+ dest.x = q1.x * ratioA + q2.x * ratioB;
408
+ dest.y = q1.y * ratioA + q2.y * ratioB;
409
+ dest.z = q1.z * ratioA + q2.z * ratioB;
410
+ dest.w = q1.w * ratioA + q2.w * ratioB;
411
+ return dest;
412
+ }
413
+ /**
414
+ * Create a quaternion from an axis and angle
415
+ * @param axis The rotation axis as a 3D vector
416
+ * @param angle The rotation angle in radians
417
+ * @return A new quaternion representing the rotation
418
+ * @throws Error if the axis vector is not 3D
419
+ */
420
+ static fromAxisAngle(axis, angle) {
421
+ if (axis.rows !== 3)
422
+ throw new Error('The axis vector must be in 3D!');
423
+ const dest = new Quat();
424
+ angle *= 0.5;
425
+ const sin = Math.sin(angle);
426
+ dest.x = axis.at(0) * sin;
427
+ dest.y = axis.at(1) * sin;
428
+ dest.z = axis.at(2) * sin;
429
+ dest.w = Math.cos(angle);
430
+ return dest;
431
+ }
432
+ toString() {
433
+ return `[${this._values.join(', ')}]`;
434
+ }
435
+ }
@@ -0,0 +1,145 @@
1
+ import type Matrix from './Matrix';
2
+ export default class Vector {
3
+ /** Values of the vector */
4
+ private _values;
5
+ constructor(values?: number[]);
6
+ get rows(): number;
7
+ get values(): number[];
8
+ /**
9
+ * Set values into the vector.
10
+ * If the parameters vector is to wide, the values are cropped to the current vector size.
11
+ * It the parameters vector is to small, remaining cells will be filled with 0.
12
+ * @param newValues Arrays of new values.
13
+ */
14
+ set values(newValues: number[]);
15
+ /**
16
+ * Calculates the angle in radians between vector and the receiver.
17
+ * @param vector The operand vector
18
+ * @return An angle, between 0 and +π inclusive.
19
+ */
20
+ angleFrom(vector: Vector): number;
21
+ /**
22
+ * Calculates the distance between vector and the receiver.
23
+ * Equivalent to |receiver - vector|
24
+ * @param vector The operand vector
25
+ * @return The distance (absolute value) between the two vectors
26
+ */
27
+ distanceFrom(vector: Vector): number;
28
+ /**
29
+ * Get a matrix value, from its position
30
+ * @param row Matrix line, from 0 to `rows`
31
+ */
32
+ at(row: number): number;
33
+ /**
34
+ * Get the position, from its matrix value
35
+ * @param value The value to search
36
+ * @return The position of the value, or -1 if not found
37
+ */
38
+ indexOf(value: number): number;
39
+ /**
40
+ * Sets all matrix values to 0
41
+ */
42
+ reset(): void;
43
+ /**
44
+ * Add an new row to the matrix, filled with 0
45
+ */
46
+ addAValue(): Vector;
47
+ /**
48
+ * Check if two matrix are equals, value by value
49
+ * @param mat The matrix against to check equality
50
+ */
51
+ equals(vec: Vector): boolean;
52
+ /**
53
+ * Negate all values of the vector (get the opposite sign)
54
+ * @return A new vector whose all values have the opposed sign
55
+ */
56
+ negate(): Vector;
57
+ /** Get the length of the vector */
58
+ length(): number;
59
+ /** Get the squared length of the vector */
60
+ squaredLength(): number;
61
+ /**
62
+ * Add all vector values with the same position value of the operand vector
63
+ * @param vector The operand vector
64
+ * @throws Error if the two vectors don't have the same dimension
65
+ * @return a new Vector with the result values
66
+ */
67
+ add(vector: Vector): Vector;
68
+ /**
69
+ * subtract all vector values with the same position value of the operand vector
70
+ * @param vector The operand vector
71
+ * @throws Error if the two vectors don't have the same dimension
72
+ * @return a new Vector with the result values
73
+ */
74
+ subtract(vector: Vector): Vector;
75
+ /**
76
+ * Multiply all vector values with the same position value of the operand vector
77
+ * @param vector The operand vector
78
+ * @throws Error if the two vectors don't have the same dimension
79
+ * @return a new Vector with the result values
80
+ */
81
+ multiply(vector: Vector): Vector;
82
+ /**
83
+ * Divide all vector values with the same position value of the operand vector
84
+ * Be aware of divisions by 0!
85
+ * @param vector The operand vector
86
+ * @throws Error if the two vectors don't have the same dimension
87
+ * @return a new Vector with the result values
88
+ */
89
+ divide(vector: Vector): Vector;
90
+ /**
91
+ * Computes the product of this vector with a matrix (vector * matrix)
92
+ * The vector is treated as a row vector
93
+ * @param matrix The matrix to multiply with
94
+ * @throws Error if vector.rows != matrix.rows
95
+ * @return A new Vector, result of the multiplication
96
+ */
97
+ multiplyMatrix(matrix: Matrix): Vector;
98
+ /**
99
+ * Computes the maximum value of the vector
100
+ * @return The maximum value
101
+ * @throws Error if the vector is empty
102
+ */
103
+ max(): number;
104
+ /**
105
+ * Computes the minimum value of the vector
106
+ * @return The minimum value
107
+ * @throws Error if the vector is empty
108
+ */
109
+ min(): number;
110
+ /**
111
+ * Rounds all vector values to the nearest integer
112
+ * @return A new vector with the rounded values
113
+ * @throws Error if the vector is empty
114
+ */
115
+ round(): Vector;
116
+ /**
117
+ * Multiply all vector values by the given number
118
+ * @param scale The number to multiply with the values
119
+ */
120
+ scale(scale: number): Vector;
121
+ /**
122
+ * Run a function on all vector values, as a map.
123
+ * @param operation The mapping method
124
+ * @return a new Vector with the operation done on all its values
125
+ */
126
+ private operateOnAllValues;
127
+ /**
128
+ * Computes the normalized vector
129
+ * @return The normalized vector
130
+ */
131
+ normalize(): Vector;
132
+ /**
133
+ * Computes the dot product of vectors
134
+ * @param vector The operand vector
135
+ */
136
+ dot(vector: Vector): number;
137
+ /**
138
+ * Computes the cross product of vectors
139
+ * @param vector The operand vector
140
+ */
141
+ cross(vector: Vector): Vector;
142
+ mix(vector: Vector, time: number): Vector;
143
+ static get360angle(Va: Vector, Vb: Vector): number;
144
+ toString(): string;
145
+ }