Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 | 283x 283x 283x 283x 1x 1x 40x 39x 38x 38x 38x 38x 38x 38x 1x 69x 67x 67x 59x 67x 67x 67x 67x 67x 1x 13x 12x 12x 12x 2x 10x 2x 8x 7x 10x 28x 10x 1x 17x 16x 15x 1x 14x 14x 12x 2x 14x 38x 38x 14x 1x 15x 1x 14x 6x 8x 8x 8x 8x 8x 1x 1x 7x 6x 1x 3x 2x 1x 1x 1x 1x 1x 1x 1x 10x 9x 5x 4x 4x 4x 4x 4x 1x 5x 4x 3x 1x 1x 1x 1x 1x 1x 30x 29x 29x 29x 5x 24x 24x 24x 24x 24x 1x 3x 2x 1x 1x 1x 1x 1x 1x 1x 6x 5x 5x 4x 4x 1x 5x 4x 4x 3x 2x 2x 2x 2x 2x 2x 1x 6x 5x 5x 4x 3x 2x 2x 2x 2x 2x 1x 5x 4x 4x 3x 2x 2x 2x 2x 2x 1x 6x 5x 5x 4x 3x 2x 2x 2x 2x 1x 1x 4x 3x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 3x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 16x 14x 13x 13x 13x 1x 1x 2x 1x 1x 1x 4x 3x 1x 1x 1x 1x 1x 1x 1x 1x 5x 4x 3x 3x 3x 3x 3x 3x 1x 9x 8x 7x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 2x 2x 2x 2x 2x 2x 2x 2x 1x 2x 2x 2x 2x 2x 2x 2x 2x 1x 38x 37x 36x 35x 35x 35x 35x 35x 1x 4x 3x 2x 1x 1x 1x 1x 1x 1x 5x 4x 3x 2x 2x 2x 2x 2x 1x 5x 4x 2x 2x 2x 2x 2x 2x 1x 4x 3x 2x 2x 2x 2x 2x 1x 4x 3x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 6x 5x 4x 4x 4x 4x 4x 1x 95x 1x 1x 16x 16x 1x 1x 1x 1x 1x 1x 1x 8x 1x 3x 1x 87x 1x 4x 1x 169x | import Cartesian2 from "./Cartesian2.js";
import Check from "./Check.js";
import defined from "./defined.js";
import DeveloperError from "./DeveloperError.js";
/**
* A 2x2 matrix, indexable as a column-major order array.
* Constructor parameters are in row-major order for code readability.
* @alias Matrix2
* @constructor
* @implements {ArrayLike<number>}
*
* @param {number} [column0Row0=0.0] The value for column 0, row 0.
* @param {number} [column1Row0=0.0] The value for column 1, row 0.
* @param {number} [column0Row1=0.0] The value for column 0, row 1.
* @param {number} [column1Row1=0.0] The value for column 1, row 1.
*
* @see Matrix2.fromArray
* @see Matrix2.fromColumnMajorArray
* @see Matrix2.fromRowMajorArray
* @see Matrix2.fromScale
* @see Matrix2.fromUniformScale
* @see Matrix2.fromRotation
* @see Matrix3
* @see Matrix4
*/
function Matrix2(column0Row0, column1Row0, column0Row1, column1Row1) {
this[0] = column0Row0 ?? 0.0;
this[1] = column0Row1 ?? 0.0;
this[2] = column1Row0 ?? 0.0;
this[3] = column1Row1 ?? 0.0;
}
/**
* The number of elements used to pack the object into an array.
* @type {number}
*/
Matrix2.packedLength = 4;
/**
* Stores the provided instance into the provided array.
*
* @param {Matrix2} value The value to pack.
* @param {number[]} array The array to pack into.
* @param {number} [startingIndex=0] The index into the array at which to start packing the elements.
*
* @returns {number[]} The array that was packed into
*/
Matrix2.pack = function (value, array, startingIndex) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("value", value);
Check.defined("array", array);
//>>includeEnd('debug');
startingIndex = startingIndex ?? 0;
array[startingIndex++] = value[0];
array[startingIndex++] = value[1];
array[startingIndex++] = value[2];
array[startingIndex++] = value[3];
return array;
};
/**
* Retrieves an instance from a packed array.
*
* @param {number[]} array The packed array.
* @param {number} [startingIndex=0] The starting index of the element to be unpacked.
* @param {Matrix2} [result] The object into which to store the result.
* @returns {Matrix2} The modified result parameter or a new Matrix2 instance if one was not provided.
*/
Matrix2.unpack = function (array, startingIndex, result) {
//>>includeStart('debug', pragmas.debug);
Check.defined("array", array);
//>>includeEnd('debug');
startingIndex = startingIndex ?? 0;
if (!defined(result)) {
result = new Matrix2();
}
result[0] = array[startingIndex++];
result[1] = array[startingIndex++];
result[2] = array[startingIndex++];
result[3] = array[startingIndex++];
return result;
};
/**
* Flattens an array of Matrix2s into an array of components. The components
* are stored in column-major order.
*
* @param {Matrix2[]} array The array of matrices to pack.
* @param {number[]} [result] The array onto which to store the result. If this is a typed array, it must have array.length * 4 components, else a {@link DeveloperError} will be thrown. If it is a regular array, it will be resized to have (array.length * 4) elements.
* @returns {number[]} The packed array.
*/
Matrix2.packArray = function (array, result) {
//>>includeStart('debug', pragmas.debug);
Check.defined("array", array);
//>>includeEnd('debug');
const length = array.length;
const resultLength = length * 4;
if (!defined(result)) {
result = new Array(resultLength);
} else if (!Array.isArray(result) && result.length !== resultLength) {
//>>includeStart('debug', pragmas.debug);
throw new DeveloperError(
"If result is a typed array, it must have exactly array.length * 4 elements",
);
//>>includeEnd('debug');
} else if (result.length !== resultLength) {
result.length = resultLength;
}
for (let i = 0; i < length; ++i) {
Matrix2.pack(array[i], result, i * 4);
}
return result;
};
/**
* Unpacks an array of column-major matrix components into an array of Matrix2s.
*
* @param {number[]} array The array of components to unpack.
* @param {Matrix2[]} [result] The array onto which to store the result.
* @returns {Matrix2[]} The unpacked array.
*/
Matrix2.unpackArray = function (array, result) {
//>>includeStart('debug', pragmas.debug);
Check.defined("array", array);
Check.typeOf.number.greaterThanOrEquals("array.length", array.length, 4);
if (array.length % 4 !== 0) {
throw new DeveloperError("array length must be a multiple of 4.");
}
//>>includeEnd('debug');
const length = array.length;
if (!defined(result)) {
result = new Array(length / 4);
} else {
result.length = length / 4;
}
for (let i = 0; i < length; i += 4) {
const index = i / 4;
result[index] = Matrix2.unpack(array, i, result[index]);
}
return result;
};
/**
* Duplicates a Matrix2 instance.
*
* @param {Matrix2} matrix The matrix to duplicate.
* @param {Matrix2} [result] The object onto which to store the result.
* @returns {Matrix2} The modified result parameter or a new Matrix2 instance if one was not provided. (Returns undefined if matrix is undefined)
*/
Matrix2.clone = function (matrix, result) {
if (!defined(matrix)) {
return undefined;
}
if (!defined(result)) {
return new Matrix2(matrix[0], matrix[2], matrix[1], matrix[3]);
}
result[0] = matrix[0];
result[1] = matrix[1];
result[2] = matrix[2];
result[3] = matrix[3];
return result;
};
/**
* Creates a Matrix2 from 4 consecutive elements in an array.
*
* @function
* @param {number[]} array The array whose 4 consecutive elements correspond to the positions of the matrix. Assumes column-major order.
* @param {number} [startingIndex=0] The offset into the array of the first element, which corresponds to first column first row position in the matrix.
* @param {Matrix2} [result] The object onto which to store the result.
* @returns {Matrix2} The modified result parameter or a new Matrix2 instance if one was not provided.
*
* @example
* // Create the Matrix2:
* // [1.0, 2.0]
* // [1.0, 2.0]
*
* const v = [1.0, 1.0, 2.0, 2.0];
* const m = Cesium.Matrix2.fromArray(v);
*
* // Create same Matrix2 with using an offset into an array
* const v2 = [0.0, 0.0, 1.0, 1.0, 2.0, 2.0];
* const m2 = Cesium.Matrix2.fromArray(v2, 2);
*/
Matrix2.fromArray = Matrix2.unpack;
/**
* Creates a Matrix2 instance from a column-major order array.
*
* @param {number[]} values The column-major order array.
* @param {Matrix2} [result] The object in which the result will be stored, if undefined a new instance will be created.
* @returns {Matrix2} The modified result parameter, or a new Matrix2 instance if one was not provided.
*/
Matrix2.fromColumnMajorArray = function (values, result) {
//>>includeStart('debug', pragmas.debug);
Check.defined("values", values);
//>>includeEnd('debug');
return Matrix2.clone(values, result);
};
/**
* Creates a Matrix2 instance from a row-major order array.
* The resulting matrix will be in column-major order.
*
* @param {number[]} values The row-major order array.
* @param {Matrix2} [result] The object in which the result will be stored, if undefined a new instance will be created.
* @returns {Matrix2} The modified result parameter, or a new Matrix2 instance if one was not provided.
*/
Matrix2.fromRowMajorArray = function (values, result) {
//>>includeStart('debug', pragmas.debug);
Check.defined("values", values);
//>>includeEnd('debug');
if (!defined(result)) {
return new Matrix2(values[0], values[1], values[2], values[3]);
}
result[0] = values[0];
result[1] = values[2];
result[2] = values[1];
result[3] = values[3];
return result;
};
/**
* Computes a Matrix2 instance representing a non-uniform scale.
*
* @param {Cartesian2} scale The x and y scale factors.
* @param {Matrix2} [result] The object in which the result will be stored, if undefined a new instance will be created.
* @returns {Matrix2} The modified result parameter, or a new Matrix2 instance if one was not provided.
*
* @example
* // Creates
* // [7.0, 0.0]
* // [0.0, 8.0]
* const m = Cesium.Matrix2.fromScale(new Cesium.Cartesian2(7.0, 8.0));
*/
Matrix2.fromScale = function (scale, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("scale", scale);
//>>includeEnd('debug');
if (!defined(result)) {
return new Matrix2(scale.x, 0.0, 0.0, scale.y);
}
result[0] = scale.x;
result[1] = 0.0;
result[2] = 0.0;
result[3] = scale.y;
return result;
};
/**
* Computes a Matrix2 instance representing a uniform scale.
*
* @param {number} scale The uniform scale factor.
* @param {Matrix2} [result] The object in which the result will be stored, if undefined a new instance will be created.
* @returns {Matrix2} The modified result parameter, or a new Matrix2 instance if one was not provided.
*
* @example
* // Creates
* // [2.0, 0.0]
* // [0.0, 2.0]
* const m = Cesium.Matrix2.fromUniformScale(2.0);
*/
Matrix2.fromUniformScale = function (scale, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.number("scale", scale);
//>>includeEnd('debug');
if (!defined(result)) {
return new Matrix2(scale, 0.0, 0.0, scale);
}
result[0] = scale;
result[1] = 0.0;
result[2] = 0.0;
result[3] = scale;
return result;
};
/**
* Creates a rotation matrix.
*
* @param {number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
* @param {Matrix2} [result] The object in which the result will be stored, if undefined a new instance will be created.
* @returns {Matrix2} The modified result parameter, or a new Matrix2 instance if one was not provided.
*
* @example
* // Rotate a point 45 degrees counterclockwise.
* const p = new Cesium.Cartesian2(5, 6);
* const m = Cesium.Matrix2.fromRotation(Cesium.Math.toRadians(45.0));
* const rotated = Cesium.Matrix2.multiplyByVector(m, p, new Cesium.Cartesian2());
*/
Matrix2.fromRotation = function (angle, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.number("angle", angle);
//>>includeEnd('debug');
const cosAngle = Math.cos(angle);
const sinAngle = Math.sin(angle);
if (!defined(result)) {
return new Matrix2(cosAngle, -sinAngle, sinAngle, cosAngle);
}
result[0] = cosAngle;
result[1] = sinAngle;
result[2] = -sinAngle;
result[3] = cosAngle;
return result;
};
/**
* Creates an Array from the provided Matrix2 instance.
* The array will be in column-major order.
*
* @param {Matrix2} matrix The matrix to use..
* @param {number[]} [result] The Array onto which to store the result.
* @returns {number[]} The modified Array parameter or a new Array instance if one was not provided.
*/
Matrix2.toArray = function (matrix, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("matrix", matrix);
//>>includeEnd('debug');
if (!defined(result)) {
return [matrix[0], matrix[1], matrix[2], matrix[3]];
}
result[0] = matrix[0];
result[1] = matrix[1];
result[2] = matrix[2];
result[3] = matrix[3];
return result;
};
/**
* Computes the array index of the element at the provided row and column.
*
* @param {number} row The zero-based index of the row.
* @param {number} column The zero-based index of the column.
* @returns {number} The index of the element at the provided row and column.
*
* @exception {DeveloperError} row must be 0 or 1.
* @exception {DeveloperError} column must be 0 or 1.
*
* @example
* const myMatrix = new Cesium.Matrix2();
* const column1Row0Index = Cesium.Matrix2.getElementIndex(1, 0);
* const column1Row0 = myMatrix[column1Row0Index]
* myMatrix[column1Row0Index] = 10.0;
*/
Matrix2.getElementIndex = function (column, row) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.number.greaterThanOrEquals("row", row, 0);
Check.typeOf.number.lessThanOrEquals("row", row, 1);
Check.typeOf.number.greaterThanOrEquals("column", column, 0);
Check.typeOf.number.lessThanOrEquals("column", column, 1);
//>>includeEnd('debug');
return column * 2 + row;
};
/**
* Retrieves a copy of the matrix column at the provided index as a Cartesian2 instance.
*
* @param {Matrix2} matrix The matrix to use.
* @param {number} index The zero-based index of the column to retrieve.
* @param {Cartesian2} result The object onto which to store the result.
* @returns {Cartesian2} The modified result parameter.
*
* @exception {DeveloperError} index must be 0 or 1.
*/
Matrix2.getColumn = function (matrix, index, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("matrix", matrix);
Check.typeOf.number.greaterThanOrEquals("index", index, 0);
Check.typeOf.number.lessThanOrEquals("index", index, 1);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
const startIndex = index * 2;
const x = matrix[startIndex];
const y = matrix[startIndex + 1];
result.x = x;
result.y = y;
return result;
};
/**
* Computes a new matrix that replaces the specified column in the provided matrix with the provided Cartesian2 instance.
*
* @param {Matrix2} matrix The matrix to use.
* @param {number} index The zero-based index of the column to set.
* @param {Cartesian2} cartesian The Cartesian whose values will be assigned to the specified column.
* @param {Cartesian2} result The object onto which to store the result.
* @returns {Matrix2} The modified result parameter.
*
* @exception {DeveloperError} index must be 0 or 1.
*/
Matrix2.setColumn = function (matrix, index, cartesian, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("matrix", matrix);
Check.typeOf.number.greaterThanOrEquals("index", index, 0);
Check.typeOf.number.lessThanOrEquals("index", index, 1);
Check.typeOf.object("cartesian", cartesian);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
result = Matrix2.clone(matrix, result);
const startIndex = index * 2;
result[startIndex] = cartesian.x;
result[startIndex + 1] = cartesian.y;
return result;
};
/**
* Retrieves a copy of the matrix row at the provided index as a Cartesian2 instance.
*
* @param {Matrix2} matrix The matrix to use.
* @param {number} index The zero-based index of the row to retrieve.
* @param {Cartesian2} result The object onto which to store the result.
* @returns {Cartesian2} The modified result parameter.
*
* @exception {DeveloperError} index must be 0 or 1.
*/
Matrix2.getRow = function (matrix, index, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("matrix", matrix);
Check.typeOf.number.greaterThanOrEquals("index", index, 0);
Check.typeOf.number.lessThanOrEquals("index", index, 1);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
const x = matrix[index];
const y = matrix[index + 2];
result.x = x;
result.y = y;
return result;
};
/**
* Computes a new matrix that replaces the specified row in the provided matrix with the provided Cartesian2 instance.
*
* @param {Matrix2} matrix The matrix to use.
* @param {number} index The zero-based index of the row to set.
* @param {Cartesian2} cartesian The Cartesian whose values will be assigned to the specified row.
* @param {Matrix2} result The object onto which to store the result.
* @returns {Matrix2} The modified result parameter.
*
* @exception {DeveloperError} index must be 0 or 1.
*/
Matrix2.setRow = function (matrix, index, cartesian, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("matrix", matrix);
Check.typeOf.number.greaterThanOrEquals("index", index, 0);
Check.typeOf.number.lessThanOrEquals("index", index, 1);
Check.typeOf.object("cartesian", cartesian);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
result = Matrix2.clone(matrix, result);
result[index] = cartesian.x;
result[index + 2] = cartesian.y;
return result;
};
const scaleScratch1 = new Cartesian2();
/**
* Computes a new matrix that replaces the scale with the provided scale.
* This assumes the matrix is an affine transformation.
*
* @param {Matrix2} matrix The matrix to use.
* @param {Cartesian2} scale The scale that replaces the scale of the provided matrix.
* @param {Matrix2} result The object onto which to store the result.
* @returns {Matrix2} The modified result parameter.
*
* @see Matrix2.setUniformScale
* @see Matrix2.fromScale
* @see Matrix2.fromUniformScale
* @see Matrix2.multiplyByScale
* @see Matrix2.multiplyByUniformScale
* @see Matrix2.getScale
*/
Matrix2.setScale = function (matrix, scale, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("matrix", matrix);
Check.typeOf.object("scale", scale);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
const existingScale = Matrix2.getScale(matrix, scaleScratch1);
const scaleRatioX = scale.x / existingScale.x;
const scaleRatioY = scale.y / existingScale.y;
result[0] = matrix[0] * scaleRatioX;
result[1] = matrix[1] * scaleRatioX;
result[2] = matrix[2] * scaleRatioY;
result[3] = matrix[3] * scaleRatioY;
return result;
};
const scaleScratch2 = new Cartesian2();
/**
* Computes a new matrix that replaces the scale with the provided uniform scale.
* This assumes the matrix is an affine transformation.
*
* @param {Matrix2} matrix The matrix to use.
* @param {number} scale The uniform scale that replaces the scale of the provided matrix.
* @param {Matrix2} result The object onto which to store the result.
* @returns {Matrix2} The modified result parameter.
*
* @see Matrix2.setScale
* @see Matrix2.fromScale
* @see Matrix2.fromUniformScale
* @see Matrix2.multiplyByScale
* @see Matrix2.multiplyByUniformScale
* @see Matrix2.getScale
*/
Matrix2.setUniformScale = function (matrix, scale, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("matrix", matrix);
Check.typeOf.number("scale", scale);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
const existingScale = Matrix2.getScale(matrix, scaleScratch2);
const scaleRatioX = scale / existingScale.x;
const scaleRatioY = scale / existingScale.y;
result[0] = matrix[0] * scaleRatioX;
result[1] = matrix[1] * scaleRatioX;
result[2] = matrix[2] * scaleRatioY;
result[3] = matrix[3] * scaleRatioY;
return result;
};
const scratchColumn = new Cartesian2();
/**
* Extracts the non-uniform scale assuming the matrix is an affine transformation.
*
* @param {Matrix2} matrix The matrix.
* @param {Cartesian2} result The object onto which to store the result.
* @returns {Cartesian2} The modified result parameter.
*
* @see Matrix2.multiplyByScale
* @see Matrix2.multiplyByUniformScale
* @see Matrix2.fromScale
* @see Matrix2.fromUniformScale
* @see Matrix2.setScale
* @see Matrix2.setUniformScale
*/
Matrix2.getScale = function (matrix, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("matrix", matrix);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
result.x = Cartesian2.magnitude(
Cartesian2.fromElements(matrix[0], matrix[1], scratchColumn),
);
result.y = Cartesian2.magnitude(
Cartesian2.fromElements(matrix[2], matrix[3], scratchColumn),
);
return result;
};
const scaleScratch3 = new Cartesian2();
/**
* Computes the maximum scale assuming the matrix is an affine transformation.
* The maximum scale is the maximum length of the column vectors.
*
* @param {Matrix2} matrix The matrix.
* @returns {number} The maximum scale.
*/
Matrix2.getMaximumScale = function (matrix) {
Matrix2.getScale(matrix, scaleScratch3);
return Cartesian2.maximumComponent(scaleScratch3);
};
const scaleScratch4 = new Cartesian2();
/**
* Sets the rotation assuming the matrix is an affine transformation.
*
* @param {Matrix2} matrix The matrix.
* @param {Matrix2} rotation The rotation matrix.
* @param {Matrix2} result The object onto which to store the result.
* @returns {Matrix2} The modified result parameter.
*
* @see Matrix2.fromRotation
* @see Matrix2.getRotation
*/
Matrix2.setRotation = function (matrix, rotation, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("matrix", matrix);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
const scale = Matrix2.getScale(matrix, scaleScratch4);
result[0] = rotation[0] * scale.x;
result[1] = rotation[1] * scale.x;
result[2] = rotation[2] * scale.y;
result[3] = rotation[3] * scale.y;
return result;
};
const scaleScratch5 = new Cartesian2();
/**
* Extracts the rotation matrix assuming the matrix is an affine transformation.
*
* @param {Matrix2} matrix The matrix.
* @param {Matrix2} result The object onto which to store the result.
* @returns {Matrix2} The modified result parameter.
*
* @see Matrix2.setRotation
* @see Matrix2.fromRotation
*/
Matrix2.getRotation = function (matrix, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("matrix", matrix);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
const scale = Matrix2.getScale(matrix, scaleScratch5);
result[0] = matrix[0] / scale.x;
result[1] = matrix[1] / scale.x;
result[2] = matrix[2] / scale.y;
result[3] = matrix[3] / scale.y;
return result;
};
/**
* Computes the product of two matrices.
*
* @param {Matrix2} left The first matrix.
* @param {Matrix2} right The second matrix.
* @param {Matrix2} result The object onto which to store the result.
* @returns {Matrix2} The modified result parameter.
*/
Matrix2.multiply = function (left, right, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("left", left);
Check.typeOf.object("right", right);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
const column0Row0 = left[0] * right[0] + left[2] * right[1];
const column1Row0 = left[0] * right[2] + left[2] * right[3];
const column0Row1 = left[1] * right[0] + left[3] * right[1];
const column1Row1 = left[1] * right[2] + left[3] * right[3];
result[0] = column0Row0;
result[1] = column0Row1;
result[2] = column1Row0;
result[3] = column1Row1;
return result;
};
/**
* Computes the sum of two matrices.
*
* @param {Matrix2} left The first matrix.
* @param {Matrix2} right The second matrix.
* @param {Matrix2} result The object onto which to store the result.
* @returns {Matrix2} The modified result parameter.
*/
Matrix2.add = function (left, right, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("left", left);
Check.typeOf.object("right", right);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
result[0] = left[0] + right[0];
result[1] = left[1] + right[1];
result[2] = left[2] + right[2];
result[3] = left[3] + right[3];
return result;
};
/**
* Computes the difference of two matrices.
*
* @param {Matrix2} left The first matrix.
* @param {Matrix2} right The second matrix.
* @param {Matrix2} result The object onto which to store the result.
* @returns {Matrix2} The modified result parameter.
*/
Matrix2.subtract = function (left, right, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("left", left);
Check.typeOf.object("right", right);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
result[0] = left[0] - right[0];
result[1] = left[1] - right[1];
result[2] = left[2] - right[2];
result[3] = left[3] - right[3];
return result;
};
/**
* Computes the product of a matrix and a column vector.
*
* @param {Matrix2} matrix The matrix.
* @param {Cartesian2} cartesian The column.
* @param {Cartesian2} result The object onto which to store the result.
* @returns {Cartesian2} The modified result parameter.
*/
Matrix2.multiplyByVector = function (matrix, cartesian, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("matrix", matrix);
Check.typeOf.object("cartesian", cartesian);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
const x = matrix[0] * cartesian.x + matrix[2] * cartesian.y;
const y = matrix[1] * cartesian.x + matrix[3] * cartesian.y;
result.x = x;
result.y = y;
return result;
};
/**
* Computes the product of a matrix and a scalar.
*
* @param {Matrix2} matrix The matrix.
* @param {number} scalar The number to multiply by.
* @param {Matrix2} result The object onto which to store the result.
* @returns {Matrix2} The modified result parameter.
*/
Matrix2.multiplyByScalar = function (matrix, scalar, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("matrix", matrix);
Check.typeOf.number("scalar", scalar);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
result[0] = matrix[0] * scalar;
result[1] = matrix[1] * scalar;
result[2] = matrix[2] * scalar;
result[3] = matrix[3] * scalar;
return result;
};
/**
* Computes the product of a matrix times a (non-uniform) scale, as if the scale were a scale matrix.
*
* @param {Matrix2} matrix The matrix on the left-hand side.
* @param {Cartesian2} scale The non-uniform scale on the right-hand side.
* @param {Matrix2} result The object onto which to store the result.
* @returns {Matrix2} The modified result parameter.
*
*
* @example
* // Instead of Cesium.Matrix2.multiply(m, Cesium.Matrix2.fromScale(scale), m);
* Cesium.Matrix2.multiplyByScale(m, scale, m);
*
* @see Matrix2.multiplyByUniformScale
* @see Matrix2.fromScale
* @see Matrix2.fromUniformScale
* @see Matrix2.setScale
* @see Matrix2.setUniformScale
* @see Matrix2.getScale
*/
Matrix2.multiplyByScale = function (matrix, scale, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("matrix", matrix);
Check.typeOf.object("scale", scale);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
result[0] = matrix[0] * scale.x;
result[1] = matrix[1] * scale.x;
result[2] = matrix[2] * scale.y;
result[3] = matrix[3] * scale.y;
return result;
};
/**
* Computes the product of a matrix times a uniform scale, as if the scale were a scale matrix.
*
* @param {Matrix2} matrix The matrix on the left-hand side.
* @param {number} scale The uniform scale on the right-hand side.
* @param {Matrix2} result The object onto which to store the result.
* @returns {Matrix2} The modified result parameter.
*
* @example
* // Instead of Cesium.Matrix2.multiply(m, Cesium.Matrix2.fromUniformScale(scale), m);
* Cesium.Matrix2.multiplyByUniformScale(m, scale, m);
*
* @see Matrix2.multiplyByScale
* @see Matrix2.fromScale
* @see Matrix2.fromUniformScale
* @see Matrix2.setScale
* @see Matrix2.setUniformScale
* @see Matrix2.getScale
*/
Matrix2.multiplyByUniformScale = function (matrix, scale, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("matrix", matrix);
Check.typeOf.number("scale", scale);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
result[0] = matrix[0] * scale;
result[1] = matrix[1] * scale;
result[2] = matrix[2] * scale;
result[3] = matrix[3] * scale;
return result;
};
/**
* Creates a negated copy of the provided matrix.
*
* @param {Matrix2} matrix The matrix to negate.
* @param {Matrix2} result The object onto which to store the result.
* @returns {Matrix2} The modified result parameter.
*/
Matrix2.negate = function (matrix, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("matrix", matrix);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
result[0] = -matrix[0];
result[1] = -matrix[1];
result[2] = -matrix[2];
result[3] = -matrix[3];
return result;
};
/**
* Computes the transpose of the provided matrix.
*
* @param {Matrix2} matrix The matrix to transpose.
* @param {Matrix2} result The object onto which to store the result.
* @returns {Matrix2} The modified result parameter.
*/
Matrix2.transpose = function (matrix, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("matrix", matrix);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
const column0Row0 = matrix[0];
const column0Row1 = matrix[2];
const column1Row0 = matrix[1];
const column1Row1 = matrix[3];
result[0] = column0Row0;
result[1] = column0Row1;
result[2] = column1Row0;
result[3] = column1Row1;
return result;
};
/**
* Computes a matrix, which contains the absolute (unsigned) values of the provided matrix's elements.
*
* @param {Matrix2} matrix The matrix with signed elements.
* @param {Matrix2} result The object onto which to store the result.
* @returns {Matrix2} The modified result parameter.
*/
Matrix2.abs = function (matrix, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("matrix", matrix);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
result[0] = Math.abs(matrix[0]);
result[1] = Math.abs(matrix[1]);
result[2] = Math.abs(matrix[2]);
result[3] = Math.abs(matrix[3]);
return result;
};
/**
* Compares the provided matrices componentwise and returns
* <code>true</code> if they are equal, <code>false</code> otherwise.
*
* @param {Matrix2} [left] The first matrix.
* @param {Matrix2} [right] The second matrix.
* @returns {boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
*/
Matrix2.equals = function (left, right) {
return (
left === right ||
(defined(left) &&
defined(right) &&
left[0] === right[0] &&
left[1] === right[1] &&
left[2] === right[2] &&
left[3] === right[3])
);
};
/**
* @private
*/
Matrix2.equalsArray = function (matrix, array, offset) {
return (
matrix[0] === array[offset] &&
matrix[1] === array[offset + 1] &&
matrix[2] === array[offset + 2] &&
matrix[3] === array[offset + 3]
);
};
/**
* Compares the provided matrices componentwise and returns
* <code>true</code> if they are within the provided epsilon,
* <code>false</code> otherwise.
*
* @param {Matrix2} [left] The first matrix.
* @param {Matrix2} [right] The second matrix.
* @param {number} [epsilon=0] The epsilon to use for equality testing.
* @returns {boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
*/
Matrix2.equalsEpsilon = function (left, right, epsilon) {
epsilon = epsilon ?? 0;
return (
left === right ||
(defined(left) &&
defined(right) &&
Math.abs(left[0] - right[0]) <= epsilon &&
Math.abs(left[1] - right[1]) <= epsilon &&
Math.abs(left[2] - right[2]) <= epsilon &&
Math.abs(left[3] - right[3]) <= epsilon)
);
};
/**
* An immutable Matrix2 instance initialized to the identity matrix.
*
* @type {Matrix2}
* @constant
*/
Matrix2.IDENTITY = Object.freeze(new Matrix2(1.0, 0.0, 0.0, 1.0));
/**
* An immutable Matrix2 instance initialized to the zero matrix.
*
* @type {Matrix2}
* @constant
*/
Matrix2.ZERO = Object.freeze(new Matrix2(0.0, 0.0, 0.0, 0.0));
/**
* The index into Matrix2 for column 0, row 0.
*
* @type {number}
* @constant
*
* @example
* const matrix = new Cesium.Matrix2();
* matrix[Cesium.Matrix2.COLUMN0ROW0] = 5.0; // set column 0, row 0 to 5.0
*/
Matrix2.COLUMN0ROW0 = 0;
/**
* The index into Matrix2 for column 0, row 1.
*
* @type {number}
* @constant
*
* @example
* const matrix = new Cesium.Matrix2();
* matrix[Cesium.Matrix2.COLUMN0ROW1] = 5.0; // set column 0, row 1 to 5.0
*/
Matrix2.COLUMN0ROW1 = 1;
/**
* The index into Matrix2 for column 1, row 0.
*
* @type {number}
* @constant
*
* @example
* const matrix = new Cesium.Matrix2();
* matrix[Cesium.Matrix2.COLUMN1ROW0] = 5.0; // set column 1, row 0 to 5.0
*/
Matrix2.COLUMN1ROW0 = 2;
/**
* The index into Matrix2 for column 1, row 1.
*
* @type {number}
* @constant
*
* @example
* const matrix = new Cesium.Matrix2();
* matrix[Cesium.Matrix2.COLUMN1ROW1] = 5.0; // set column 1, row 1 to 5.0
*/
Matrix2.COLUMN1ROW1 = 3;
Object.defineProperties(Matrix2.prototype, {
/**
* Gets the number of items in the collection.
* @memberof Matrix2.prototype
*
* @type {number}
*/
length: {
get: function () {
return Matrix2.packedLength;
},
},
});
/**
* Duplicates the provided Matrix2 instance.
*
* @param {Matrix2} [result] The object onto which to store the result.
* @returns {Matrix2} The modified result parameter or a new Matrix2 instance if one was not provided.
*/
Matrix2.prototype.clone = function (result) {
return Matrix2.clone(this, result);
};
/**
* Compares this matrix to the provided matrix componentwise and returns
* <code>true</code> if they are equal, <code>false</code> otherwise.
*
* @param {Matrix2} [right] The right hand side matrix.
* @returns {boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
*/
Matrix2.prototype.equals = function (right) {
return Matrix2.equals(this, right);
};
/**
* Compares this matrix to the provided matrix componentwise and returns
* <code>true</code> if they are within the provided epsilon,
* <code>false</code> otherwise.
*
* @param {Matrix2} [right] The right hand side matrix.
* @param {number} [epsilon=0] The epsilon to use for equality testing.
* @returns {boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
*/
Matrix2.prototype.equalsEpsilon = function (right, epsilon) {
return Matrix2.equalsEpsilon(this, right, epsilon);
};
/**
* Creates a string representing this Matrix with each row being
* on a separate line and in the format '(column0, column1)'.
*
* @returns {string} A string representing the provided Matrix with each row being on a separate line and in the format '(column0, column1)'.
*/
Matrix2.prototype.toString = function () {
return `(${this[0]}, ${this[2]})\n` + `(${this[1]}, ${this[3]})`;
};
export default Matrix2;
|