- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 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
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
- 342
- 343
- 344
- 345
- 346
- 347
- 348
- 349
- 350
- 351
- 352
- 353
- 354
- 355
- 356
- 357
- 358
- 359
- 360
- 361
- 362
- 363
- 364
- 365
- 366
- 367
- 368
- 369
- 370
- 371
- 372
- 373
- 374
- 375
- 376
- 377
- 378
- 379
- 380
- 381
- 382
- 383
- 384
- 385
- 386
- 387
- 388
- 389
- 390
- 391
- 392
- 393
- 394
- 395
- 396
- 397
- 398
- 399
- 400
- 401
- 402
- 403
- 404
- 405
- 406
- 407
- 408
- 409
- 410
- 411
- 412
- 413
- 414
- 415
- 416
- 417
- 418
- 419
- 420
- 421
- 422
- 423
- 424
- 425
- 426
- 427
- 428
- 429
- 430
- 431
- 432
- 433
- 434
- 435
- 436
- 437
- 438
- 439
- 440
- 441
- 442
- 443
- 444
- 445
- 446
- 447
- 448
- 449
- 450
- 451
- 452
- 453
- 454
- 455
- 456
- 457
- 458
- 459
- 460
- 461
- 462
- 463
- 464
- 465
- 466
- 467
- 468
- 469
- 470
- 471
- 472
- 473
- 474
- 475
- 476
- 477
- 478
- 479
- 480
- 481
- 482
- 483
- 484
- 485
- 486
- 487
- 488
- 489
- 490
- 491
- 492
- 493
- 494
- 495
- 496
- 497
- 498
- 499
- 500
- 501
- 502
- 503
- 504
- 505
- 506
- 507
- 508
- 509
- 510
- 511
- 512
- 513
- 514
- 515
- 516
- 517
- 518
- 519
- 520
- 521
- 522
- 523
- 524
- 525
- 526
- 527
- 528
- 529
- 530
- 531
- 532
- 533
- 534
- 535
- 536
- 537
- 538
- 539
- 540
- 541
- 542
- 543
- 544
- 545
- 546
- 547
- 548
- 549
- 550
- 551
import Vector3d from "./vector3.js";
import {clamp} from "./math.js";
import pool from "./../system/pooling.js";
/**
* @classdesc
* A Vector3d object that provide notification by executing the given callback when the vector is changed.
* @augments Vector3d
*/
export default class ObservableVector3d extends Vector3d {
/**
* @param {number} x - x value of the vector
* @param {number} y - y value of the vector
* @param {number} z - z value of the vector
* @param {object} settings - additional required parameters
* @param {Function} settings.onUpdate - the callback to be executed when the vector is changed
* @param {object} [settings.scope] - the value to use as this when calling onUpdate
*/
constructor(x = 0, y = 0, z = 0, settings) {
super(x, y, z);
if (typeof(settings) === "undefined") {
throw new Error(
"undefined `onUpdate` callback"
);
}
this.setCallback(settings.onUpdate, settings.scope);
}
/**
* @ignore
*/
onResetEvent(x = 0, y = 0, z = 0, settings) {
// init is call by the constructor and does not trigger the cb
this.setMuted(x, y, z);
if (typeof settings !== "undefined") {
this.setCallback(settings.onUpdate, settings.scope);
}
return this;
}
/**
* x value of the vector
* @public
* @type {number}
* @name x
* @memberof ObservableVector3d
*/
get x() {
return this._x;
}
set x(value) {
let ret = this.onUpdate.call(this.scope, value, this._y, this._z, this._x, this._y, this._z);
if (ret && "x" in ret) {
this._x = ret.x;
} else {
this._x = value;
}
}
/**
* y value of the vector
* @public
* @type {number}
* @name y
* @memberof ObservableVector3d
*/
get y() {
return this._y;
}
set y(value) {
let ret = this.onUpdate.call(this.scope, this._x, value, this._z, this._x, this._y, this._z);
if (ret && "y" in ret) {
this._y = ret.y;
} else {
this._y = value;
}
}
/**
* z value of the vector
* @public
* @type {number}
* @name z
* @memberof ObservableVector3d
*/
get z() {
return this._z;
}
set z(value) {
let ret = this.onUpdate.call(this.scope, this._x, this._y, value, this._x, this._y, this._z);
if (ret && "z" in ret) {
this._z = ret.z;
} else {
this._z = value;
}
}
/**
* @ignore
*/
_set(x, y, z) {
let ret = this.onUpdate.call(this.scope, x, y, z, this._x, this._y, this._z);
if (ret && "x" in ret && "y" in ret && "z" in ret) {
this._x = ret.x;
this._y = ret.y;
this._z = ret.z;
} else {
this._x = x;
this._y = y;
this._z = z || 0;
}
return this;
}
/**
* set the vector value without triggering the callback
* @name setMuted
* @memberof ObservableVector3d
* @param {number} x - x value of the vector
* @param {number} y - y value of the vector
* @param {number} [z=0] - z value of the vector
* @returns {ObservableVector3d} Reference to this object for method chaining
*/
setMuted(x, y, z) {
this._x = x;
this._y = y;
this._z = z || 0;
return this;
}
/**
* set the callback to be executed when the vector is changed
* @name setCallback
* @memberof ObservableVector3d
* @param {Function} fn - callback
* @param {Function} [scope=null] - scope
* @returns {ObservableVector3d} Reference to this object for method chaining
*/
setCallback(fn, scope = null) {
if (typeof(fn) !== "function") {
throw new Error(
"invalid `onUpdate` callback"
);
}
this.onUpdate = fn;
this.scope = scope;
return this;
}
/**
* Add the passed vector to this vector
* @name add
* @memberof ObservableVector3d
* @param {Vector2d|Vector3d|ObservableVector2d|ObservableVector3d} v
* @returns {ObservableVector3d} Reference to this object for method chaining
*/
add(v) {
return this._set(this._x + v.x, this._y + v.y, this._z + (v.z || 0));
}
/**
* Substract the passed vector to this vector
* @name sub
* @memberof ObservableVector3d
* @param {Vector2d|Vector3d|ObservableVector2d|ObservableVector3d} v
* @returns {ObservableVector3d} Reference to this object for method chaining
*/
sub(v) {
return this._set(this._x - v.x, this._y - v.y, this._z - (v.z || 0));
}
/**
* Multiply this vector values by the given scalar
* @name scale
* @memberof ObservableVector3d
* @param {number} x
* @param {number} [y=x]
* @param {number} [z=1]
* @returns {ObservableVector3d} Reference to this object for method chaining
*/
scale(x, y = x, z = 1) {
return this._set(this._x * x, this._y * y, this._z * z);
}
/**
* Multiply this vector values by the passed vector
* @name scaleV
* @memberof ObservableVector3d
* @param {Vector2d|Vector3d|ObservableVector2d|ObservableVector3d} v
* @returns {ObservableVector3d} Reference to this object for method chaining
*/
scaleV(v) {
return this._set(this._x * v.x, this._y * v.y, this._z * (v.z || 1));
}
/**
* Divide this vector values by the passed value
* @name div
* @memberof ObservableVector3d
* @param {number} n - the value to divide the vector by
* @returns {ObservableVector3d} Reference to this object for method chaining
*/
div(n) {
return this._set(this._x / n, this._y / n, this._z / n);
}
/**
* Update this vector values to absolute values
* @name abs
* @memberof ObservableVector3d
* @returns {ObservableVector3d} Reference to this object for method chaining
*/
abs() {
return this._set(
(this._x < 0) ? -this._x : this._x,
(this._y < 0) ? -this._y : this._y,
(this._Z < 0) ? -this._z : this._z
);
}
/**
* Clamp the vector value within the specified value range
* @name clamp
* @memberof ObservableVector3d
* @param {number} low
* @param {number} high
* @returns {ObservableVector3d} new me.ObservableVector3d
*/
clamp(low, high) {
return new ObservableVector3d(
clamp(this._x, low, high),
clamp(this._y, low, high),
clamp(this._z, low, high),
{onUpdate: this.onUpdate, scope: this.scope}
);
}
/**
* Clamp this vector value within the specified value range
* @name clampSelf
* @memberof ObservableVector3d
* @param {number} low
* @param {number} high
* @returns {ObservableVector3d} Reference to this object for method chaining
*/
clampSelf(low, high) {
return this._set(
clamp(this._x, low, high),
clamp(this._y, low, high),
clamp(this._z, low, high)
);
}
/**
* Update this vector with the minimum value between this and the passed vector
* @name minV
* @memberof ObservableVector3d
* @param {Vector2d|Vector3d|ObservableVector2d|ObservableVector3d} v
* @returns {ObservableVector3d} Reference to this object for method chaining
*/
minV(v) {
let _vz = v.z || 0;
return this._set(
(this._x < v.x) ? this._x : v.x,
(this._y < v.y) ? this._y : v.y,
(this._z < _vz) ? this._z : _vz
);
}
/**
* Update this vector with the maximum value between this and the passed vector
* @name maxV
* @memberof ObservableVector3d
* @param {Vector2d|Vector3d|ObservableVector2d|ObservableVector3d} v
* @returns {ObservableVector3d} Reference to this object for method chaining
*/
maxV(v) {
let _vz = v.z || 0;
return this._set(
(this._x > v.x) ? this._x : v.x,
(this._y > v.y) ? this._y : v.y,
(this._z > _vz) ? this._z : _vz
);
}
/**
* Floor the vector values
* @name floor
* @memberof ObservableVector3d
* @returns {ObservableVector3d} new me.ObservableVector3d
*/
floor() {
return new ObservableVector3d(
Math.floor(this._x),
Math.floor(this._y),
Math.floor(this._z),
{onUpdate: this.onUpdate, scope: this.scope}
);
}
/**
* Floor this vector values
* @name floorSelf
* @memberof ObservableVector3d
* @returns {ObservableVector3d} Reference to this object for method chaining
*/
floorSelf() {
return this._set(Math.floor(this._x), Math.floor(this._y), Math.floor(this._z));
}
/**
* Ceil the vector values
* @name ceil
* @memberof ObservableVector3d
* @returns {ObservableVector3d} new me.ObservableVector3d
*/
ceil() {
return new ObservableVector3d(
Math.ceil(this._x),
Math.ceil(this._y),
Math.ceil(this._z),
{onUpdate: this.onUpdate, scope: this.scope}
);
}
/**
* Ceil this vector values
* @name ceilSelf
* @memberof ObservableVector3d
* @returns {ObservableVector3d} Reference to this object for method chaining
*/
ceilSelf() {
return this._set(Math.ceil(this._x), Math.ceil(this._y), Math.ceil(this._z));
}
/**
* Negate the vector values
* @name negate
* @memberof ObservableVector3d
* @returns {ObservableVector3d} new me.ObservableVector3d
*/
negate() {
return new ObservableVector3d(
-this._x,
-this._y,
-this._z,
{onUpdate: this.onUpdate, scope: this.scope}
);
}
/**
* Negate this vector values
* @name negateSelf
* @memberof ObservableVector3d
* @returns {ObservableVector3d} Reference to this object for method chaining
*/
negateSelf() {
return this._set(-this._x, -this._y, -this._z);
}
/**
* Copy the components of the given vector into this one
* @name copy
* @memberof ObservableVector3d
* @param {Vector2d|Vector3d|ObservableVector2d|ObservableVector3d} v
* @returns {ObservableVector3d} Reference to this object for method chaining
*/
copy(v) {
return this._set(v.x, v.y, v.z || 0);
}
/**
* return true if the two vectors are the same
* @name equals
* @memberof ObservableVector3d
* @param {Vector2d|Vector3d|ObservableVector2d|ObservableVector3d} v
* @returns {boolean}
*/
equals(v) {
return ((this._x === v.x) && (this._y === v.y) && (this._z === (v.z || this._z)));
}
/**
* change this vector to be perpendicular to what it was before.<br>
* (Effectively rotates it 90 degrees in a clockwise direction)
* @name perp
* @memberof ObservableVector3d
* @returns {ObservableVector3d} Reference to this object for method chaining
*/
perp() {
return this._set(this._y, -this._x, this._z);
}
/**
* Rotate this vector (counter-clockwise) by the specified angle (in radians).
* @name rotate
* @memberof ObservableVector3d
* @param {number} angle - The angle to rotate (in radians)
* @param {Vector2d|ObservableVector2d} [v] - an optional point to rotate around (on the same z axis)
* @returns {ObservableVector3d} Reference to this object for method chaining
*/
rotate(angle, v) {
let cx = 0;
let cy = 0;
if (typeof v === "object") {
cx = v.x;
cy = v.y;
}
// TODO also rotate on the z axis if the given vector is a 3d one
let x = this.x - cx;
let y = this.y - cy;
let c = Math.cos(angle);
let s = Math.sin(angle);
return this._set(x * c - y * s + cx, x * s + y * c + cy, this.z);
}
/**
* return the dot product of this vector and the passed one
* @name dot
* @memberof ObservableVector3d
* @param {Vector2d|Vector3d|ObservableVector2d|ObservableVector3d} v
* @returns {number} The dot product.
*/
dot(v) {
return this._x * v.x + this._y * v.y + this._z * (v.z || 1);
}
/**
* calculate the cross product of this vector and the passed one
* @name cross
* @memberof ObservableVector3d
* @param {Vector3d|ObservableVector3d} v
* @returns {ObservableVector3d} Reference to this object for method chaining
*/
cross(v) {
let ax = this._x, ay = this._y, az = this._z;
let bx = v.x, by = v.y, bz = v.z;
return this._set(
ay * bz - az * by,
az * bx - ax * bz,
ax * by - ay * bx
);
}
/**
* Linearly interpolate between this vector and the given one.
* @name lerp
* @memberof ObservableVector3d
* @param {Vector3d|ObservableVector3d} v
* @param {number} alpha - distance along the line (alpha = 0 will be this vector, and alpha = 1 will be the given one).
* @returns {ObservableVector3d} Reference to this object for method chaining
*/
lerp(v, alpha) {
return this._set(
this._x + ( v.x - this._x ) * alpha,
this._y + ( v.y - this._y ) * alpha,
this._z + ( v.z - this._z ) * alpha
);
}
/**
* interpolate the position of this vector on the x and y axis towards the given one while ensure that the distance never exceeds the given step.
* @name moveTowards
* @memberof ObservableVector3d
* @param {Vector2d|ObservableVector2d|Vector3d|ObservableVector3d} target
* @param {number} step - the maximum step per iteration (Negative values will push the vector away from the target)
* @returns {ObservableVector3d} Reference to this object for method chaining
*/
moveTowards(target, step) {
let angle = Math.atan2(target.y - this._y, target.x - this._x);
let dx = this._x - target.x;
let dy = this._y - target.y;
let distance = Math.sqrt(dx * dx + dy * dy);
if (distance === 0 || (step >= 0 && distance <= step * step)) {
return target;
}
return this._set(
this._x + Math.cos(angle) * step,
this._y + Math.sin(angle) * step,
this._z
);
}
/**
* return the distance between this vector and the passed one
* @name distance
* @memberof ObservableVector3d
* @param {Vector2d|Vector3d|ObservableVector2d|ObservableVector3d} v
* @returns {number}
*/
distance(v) {
let dx = this._x - v.x;
let dy = this._y - v.y;
let dz = this._z - (v.z || 0);
return Math.sqrt(dx * dx + dy * dy + dz * dz);
}
/**
* return a clone copy of this vector
* @name clone
* @memberof ObservableVector3d
* @returns {ObservableVector3d} new me.ObservableVector3d
*/
clone() {
return pool.pull("ObservableVector3d",
this._x,
this._y,
this._z,
{onUpdate: this.onUpdate}
);
}
/**
* return a `me.Vector3d` copy of this `me.ObservableVector3d` object
* @name toVector3d
* @memberof ObservableVector3d
* @returns {Vector3d} new me.Vector3d
*/
toVector3d() {
return pool.pull("Vector3d", this._x, this._y, this._z);
}
/**
* convert the object to a string representation
* @name toString
* @memberof ObservableVector3d
* @returns {string}
*/
toString() {
return "x:" + this._x + ",y:" + this._y + ",z:" + this._z;
}
}