Matrix3.js 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839
  1. import Cartesian3 from "./Cartesian3.js";
  2. import Check from "./Check.js";
  3. import defaultValue from "./defaultValue.js";
  4. import defined from "./defined.js";
  5. import DeveloperError from "./DeveloperError.js";
  6. import CesiumMath from "./Math.js";
  7. /**
  8. * A 3x3 matrix, indexable as a column-major order array.
  9. * Constructor parameters are in row-major order for code readability.
  10. * @alias Matrix3
  11. * @constructor
  12. * @implements {ArrayLike<number>}
  13. *
  14. * @param {number} [column0Row0=0.0] The value for column 0, row 0.
  15. * @param {number} [column1Row0=0.0] The value for column 1, row 0.
  16. * @param {number} [column2Row0=0.0] The value for column 2, row 0.
  17. * @param {number} [column0Row1=0.0] The value for column 0, row 1.
  18. * @param {number} [column1Row1=0.0] The value for column 1, row 1.
  19. * @param {number} [column2Row1=0.0] The value for column 2, row 1.
  20. * @param {number} [column0Row2=0.0] The value for column 0, row 2.
  21. * @param {number} [column1Row2=0.0] The value for column 1, row 2.
  22. * @param {number} [column2Row2=0.0] The value for column 2, row 2.
  23. *
  24. * @see Matrix3.fromArray
  25. * @see Matrix3.fromColumnMajorArray
  26. * @see Matrix3.fromRowMajorArray
  27. * @see Matrix3.fromQuaternion
  28. * @see Matrix3.fromHeadingPitchRoll
  29. * @see Matrix3.fromScale
  30. * @see Matrix3.fromUniformScale
  31. * @see Matrix3.fromCrossProduct
  32. * @see Matrix3.fromRotationX
  33. * @see Matrix3.fromRotationY
  34. * @see Matrix3.fromRotationZ
  35. * @see Matrix2
  36. * @see Matrix4
  37. */
  38. function Matrix3(
  39. column0Row0,
  40. column1Row0,
  41. column2Row0,
  42. column0Row1,
  43. column1Row1,
  44. column2Row1,
  45. column0Row2,
  46. column1Row2,
  47. column2Row2
  48. ) {
  49. this[0] = defaultValue(column0Row0, 0.0);
  50. this[1] = defaultValue(column0Row1, 0.0);
  51. this[2] = defaultValue(column0Row2, 0.0);
  52. this[3] = defaultValue(column1Row0, 0.0);
  53. this[4] = defaultValue(column1Row1, 0.0);
  54. this[5] = defaultValue(column1Row2, 0.0);
  55. this[6] = defaultValue(column2Row0, 0.0);
  56. this[7] = defaultValue(column2Row1, 0.0);
  57. this[8] = defaultValue(column2Row2, 0.0);
  58. }
  59. /**
  60. * The number of elements used to pack the object into an array.
  61. * @type {number}
  62. */
  63. Matrix3.packedLength = 9;
  64. /**
  65. * Stores the provided instance into the provided array.
  66. *
  67. * @param {Matrix3} value The value to pack.
  68. * @param {number[]} array The array to pack into.
  69. * @param {number} [startingIndex=0] The index into the array at which to start packing the elements.
  70. *
  71. * @returns {number[]} The array that was packed into
  72. */
  73. Matrix3.pack = function (value, array, startingIndex) {
  74. //>>includeStart('debug', pragmas.debug);
  75. Check.typeOf.object("value", value);
  76. Check.defined("array", array);
  77. //>>includeEnd('debug');
  78. startingIndex = defaultValue(startingIndex, 0);
  79. array[startingIndex++] = value[0];
  80. array[startingIndex++] = value[1];
  81. array[startingIndex++] = value[2];
  82. array[startingIndex++] = value[3];
  83. array[startingIndex++] = value[4];
  84. array[startingIndex++] = value[5];
  85. array[startingIndex++] = value[6];
  86. array[startingIndex++] = value[7];
  87. array[startingIndex++] = value[8];
  88. return array;
  89. };
  90. /**
  91. * Retrieves an instance from a packed array.
  92. *
  93. * @param {number[]} array The packed array.
  94. * @param {number} [startingIndex=0] The starting index of the element to be unpacked.
  95. * @param {Matrix3} [result] The object into which to store the result.
  96. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided.
  97. */
  98. Matrix3.unpack = function (array, startingIndex, result) {
  99. //>>includeStart('debug', pragmas.debug);
  100. Check.defined("array", array);
  101. //>>includeEnd('debug');
  102. startingIndex = defaultValue(startingIndex, 0);
  103. if (!defined(result)) {
  104. result = new Matrix3();
  105. }
  106. result[0] = array[startingIndex++];
  107. result[1] = array[startingIndex++];
  108. result[2] = array[startingIndex++];
  109. result[3] = array[startingIndex++];
  110. result[4] = array[startingIndex++];
  111. result[5] = array[startingIndex++];
  112. result[6] = array[startingIndex++];
  113. result[7] = array[startingIndex++];
  114. result[8] = array[startingIndex++];
  115. return result;
  116. };
  117. /**
  118. * Flattens an array of Matrix3s into an array of components. The components
  119. * are stored in column-major order.
  120. *
  121. * @param {Matrix3[]} array The array of matrices to pack.
  122. * @param {number[]} [result] The array onto which to store the result. If this is a typed array, it must have array.length * 9 components, else a {@link DeveloperError} will be thrown. If it is a regular array, it will be resized to have (array.length * 9) elements.
  123. * @returns {number[]} The packed array.
  124. */
  125. Matrix3.packArray = function (array, result) {
  126. //>>includeStart('debug', pragmas.debug);
  127. Check.defined("array", array);
  128. //>>includeEnd('debug');
  129. const length = array.length;
  130. const resultLength = length * 9;
  131. if (!defined(result)) {
  132. result = new Array(resultLength);
  133. } else if (!Array.isArray(result) && result.length !== resultLength) {
  134. //>>includeStart('debug', pragmas.debug);
  135. throw new DeveloperError(
  136. "If result is a typed array, it must have exactly array.length * 9 elements"
  137. );
  138. //>>includeEnd('debug');
  139. } else if (result.length !== resultLength) {
  140. result.length = resultLength;
  141. }
  142. for (let i = 0; i < length; ++i) {
  143. Matrix3.pack(array[i], result, i * 9);
  144. }
  145. return result;
  146. };
  147. /**
  148. * Unpacks an array of column-major matrix components into an array of Matrix3s.
  149. *
  150. * @param {number[]} array The array of components to unpack.
  151. * @param {Matrix3[]} [result] The array onto which to store the result.
  152. * @returns {Matrix3[]} The unpacked array.
  153. */
  154. Matrix3.unpackArray = function (array, result) {
  155. //>>includeStart('debug', pragmas.debug);
  156. Check.defined("array", array);
  157. Check.typeOf.number.greaterThanOrEquals("array.length", array.length, 9);
  158. if (array.length % 9 !== 0) {
  159. throw new DeveloperError("array length must be a multiple of 9.");
  160. }
  161. //>>includeEnd('debug');
  162. const length = array.length;
  163. if (!defined(result)) {
  164. result = new Array(length / 9);
  165. } else {
  166. result.length = length / 9;
  167. }
  168. for (let i = 0; i < length; i += 9) {
  169. const index = i / 9;
  170. result[index] = Matrix3.unpack(array, i, result[index]);
  171. }
  172. return result;
  173. };
  174. /**
  175. * Duplicates a Matrix3 instance.
  176. *
  177. * @param {Matrix3} matrix The matrix to duplicate.
  178. * @param {Matrix3} [result] The object onto which to store the result.
  179. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided. (Returns undefined if matrix is undefined)
  180. */
  181. Matrix3.clone = function (matrix, result) {
  182. if (!defined(matrix)) {
  183. return undefined;
  184. }
  185. if (!defined(result)) {
  186. return new Matrix3(
  187. matrix[0],
  188. matrix[3],
  189. matrix[6],
  190. matrix[1],
  191. matrix[4],
  192. matrix[7],
  193. matrix[2],
  194. matrix[5],
  195. matrix[8]
  196. );
  197. }
  198. result[0] = matrix[0];
  199. result[1] = matrix[1];
  200. result[2] = matrix[2];
  201. result[3] = matrix[3];
  202. result[4] = matrix[4];
  203. result[5] = matrix[5];
  204. result[6] = matrix[6];
  205. result[7] = matrix[7];
  206. result[8] = matrix[8];
  207. return result;
  208. };
  209. /**
  210. * Creates a Matrix3 from 9 consecutive elements in an array.
  211. *
  212. * @function
  213. * @param {number[]} array The array whose 9 consecutive elements correspond to the positions of the matrix. Assumes column-major order.
  214. * @param {number} [startingIndex=0] The offset into the array of the first element, which corresponds to first column first row position in the matrix.
  215. * @param {Matrix3} [result] The object onto which to store the result.
  216. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided.
  217. *
  218. * @example
  219. * // Create the Matrix3:
  220. * // [1.0, 2.0, 3.0]
  221. * // [1.0, 2.0, 3.0]
  222. * // [1.0, 2.0, 3.0]
  223. *
  224. * const v = [1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0];
  225. * const m = Cesium.Matrix3.fromArray(v);
  226. *
  227. * // Create same Matrix3 with using an offset into an array
  228. * const v2 = [0.0, 0.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0];
  229. * const m2 = Cesium.Matrix3.fromArray(v2, 2);
  230. */
  231. Matrix3.fromArray = Matrix3.unpack;
  232. /**
  233. * Creates a Matrix3 instance from a column-major order array.
  234. *
  235. * @param {number[]} values The column-major order array.
  236. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  237. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  238. */
  239. Matrix3.fromColumnMajorArray = function (values, result) {
  240. //>>includeStart('debug', pragmas.debug);
  241. Check.defined("values", values);
  242. //>>includeEnd('debug');
  243. return Matrix3.clone(values, result);
  244. };
  245. /**
  246. * Creates a Matrix3 instance from a row-major order array.
  247. * The resulting matrix will be in column-major order.
  248. *
  249. * @param {number[]} values The row-major order array.
  250. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  251. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  252. */
  253. Matrix3.fromRowMajorArray = function (values, result) {
  254. //>>includeStart('debug', pragmas.debug);
  255. Check.defined("values", values);
  256. //>>includeEnd('debug');
  257. if (!defined(result)) {
  258. return new Matrix3(
  259. values[0],
  260. values[1],
  261. values[2],
  262. values[3],
  263. values[4],
  264. values[5],
  265. values[6],
  266. values[7],
  267. values[8]
  268. );
  269. }
  270. result[0] = values[0];
  271. result[1] = values[3];
  272. result[2] = values[6];
  273. result[3] = values[1];
  274. result[4] = values[4];
  275. result[5] = values[7];
  276. result[6] = values[2];
  277. result[7] = values[5];
  278. result[8] = values[8];
  279. return result;
  280. };
  281. /**
  282. * Computes a 3x3 rotation matrix from the provided quaternion.
  283. *
  284. * @param {Quaternion} quaternion the quaternion to use.
  285. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  286. * @returns {Matrix3} The 3x3 rotation matrix from this quaternion.
  287. */
  288. Matrix3.fromQuaternion = function (quaternion, result) {
  289. //>>includeStart('debug', pragmas.debug);
  290. Check.typeOf.object("quaternion", quaternion);
  291. //>>includeEnd('debug');
  292. const x2 = quaternion.x * quaternion.x;
  293. const xy = quaternion.x * quaternion.y;
  294. const xz = quaternion.x * quaternion.z;
  295. const xw = quaternion.x * quaternion.w;
  296. const y2 = quaternion.y * quaternion.y;
  297. const yz = quaternion.y * quaternion.z;
  298. const yw = quaternion.y * quaternion.w;
  299. const z2 = quaternion.z * quaternion.z;
  300. const zw = quaternion.z * quaternion.w;
  301. const w2 = quaternion.w * quaternion.w;
  302. const m00 = x2 - y2 - z2 + w2;
  303. const m01 = 2.0 * (xy - zw);
  304. const m02 = 2.0 * (xz + yw);
  305. const m10 = 2.0 * (xy + zw);
  306. const m11 = -x2 + y2 - z2 + w2;
  307. const m12 = 2.0 * (yz - xw);
  308. const m20 = 2.0 * (xz - yw);
  309. const m21 = 2.0 * (yz + xw);
  310. const m22 = -x2 - y2 + z2 + w2;
  311. if (!defined(result)) {
  312. return new Matrix3(m00, m01, m02, m10, m11, m12, m20, m21, m22);
  313. }
  314. result[0] = m00;
  315. result[1] = m10;
  316. result[2] = m20;
  317. result[3] = m01;
  318. result[4] = m11;
  319. result[5] = m21;
  320. result[6] = m02;
  321. result[7] = m12;
  322. result[8] = m22;
  323. return result;
  324. };
  325. /**
  326. * Computes a 3x3 rotation matrix from the provided headingPitchRoll. (see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles )
  327. *
  328. * @param {HeadingPitchRoll} headingPitchRoll the headingPitchRoll to use.
  329. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  330. * @returns {Matrix3} The 3x3 rotation matrix from this headingPitchRoll.
  331. */
  332. Matrix3.fromHeadingPitchRoll = function (headingPitchRoll, result) {
  333. //>>includeStart('debug', pragmas.debug);
  334. Check.typeOf.object("headingPitchRoll", headingPitchRoll);
  335. //>>includeEnd('debug');
  336. const cosTheta = Math.cos(-headingPitchRoll.pitch);
  337. const cosPsi = Math.cos(-headingPitchRoll.heading);
  338. const cosPhi = Math.cos(headingPitchRoll.roll);
  339. const sinTheta = Math.sin(-headingPitchRoll.pitch);
  340. const sinPsi = Math.sin(-headingPitchRoll.heading);
  341. const sinPhi = Math.sin(headingPitchRoll.roll);
  342. const m00 = cosTheta * cosPsi;
  343. const m01 = -cosPhi * sinPsi + sinPhi * sinTheta * cosPsi;
  344. const m02 = sinPhi * sinPsi + cosPhi * sinTheta * cosPsi;
  345. const m10 = cosTheta * sinPsi;
  346. const m11 = cosPhi * cosPsi + sinPhi * sinTheta * sinPsi;
  347. const m12 = -sinPhi * cosPsi + cosPhi * sinTheta * sinPsi;
  348. const m20 = -sinTheta;
  349. const m21 = sinPhi * cosTheta;
  350. const m22 = cosPhi * cosTheta;
  351. if (!defined(result)) {
  352. return new Matrix3(m00, m01, m02, m10, m11, m12, m20, m21, m22);
  353. }
  354. result[0] = m00;
  355. result[1] = m10;
  356. result[2] = m20;
  357. result[3] = m01;
  358. result[4] = m11;
  359. result[5] = m21;
  360. result[6] = m02;
  361. result[7] = m12;
  362. result[8] = m22;
  363. return result;
  364. };
  365. /**
  366. * Computes a Matrix3 instance representing a non-uniform scale.
  367. *
  368. * @param {Cartesian3} scale The x, y, and z scale factors.
  369. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  370. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  371. *
  372. * @example
  373. * // Creates
  374. * // [7.0, 0.0, 0.0]
  375. * // [0.0, 8.0, 0.0]
  376. * // [0.0, 0.0, 9.0]
  377. * const m = Cesium.Matrix3.fromScale(new Cesium.Cartesian3(7.0, 8.0, 9.0));
  378. */
  379. Matrix3.fromScale = function (scale, result) {
  380. //>>includeStart('debug', pragmas.debug);
  381. Check.typeOf.object("scale", scale);
  382. //>>includeEnd('debug');
  383. if (!defined(result)) {
  384. return new Matrix3(scale.x, 0.0, 0.0, 0.0, scale.y, 0.0, 0.0, 0.0, scale.z);
  385. }
  386. result[0] = scale.x;
  387. result[1] = 0.0;
  388. result[2] = 0.0;
  389. result[3] = 0.0;
  390. result[4] = scale.y;
  391. result[5] = 0.0;
  392. result[6] = 0.0;
  393. result[7] = 0.0;
  394. result[8] = scale.z;
  395. return result;
  396. };
  397. /**
  398. * Computes a Matrix3 instance representing a uniform scale.
  399. *
  400. * @param {number} scale The uniform scale factor.
  401. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  402. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  403. *
  404. * @example
  405. * // Creates
  406. * // [2.0, 0.0, 0.0]
  407. * // [0.0, 2.0, 0.0]
  408. * // [0.0, 0.0, 2.0]
  409. * const m = Cesium.Matrix3.fromUniformScale(2.0);
  410. */
  411. Matrix3.fromUniformScale = function (scale, result) {
  412. //>>includeStart('debug', pragmas.debug);
  413. Check.typeOf.number("scale", scale);
  414. //>>includeEnd('debug');
  415. if (!defined(result)) {
  416. return new Matrix3(scale, 0.0, 0.0, 0.0, scale, 0.0, 0.0, 0.0, scale);
  417. }
  418. result[0] = scale;
  419. result[1] = 0.0;
  420. result[2] = 0.0;
  421. result[3] = 0.0;
  422. result[4] = scale;
  423. result[5] = 0.0;
  424. result[6] = 0.0;
  425. result[7] = 0.0;
  426. result[8] = scale;
  427. return result;
  428. };
  429. /**
  430. * Computes a Matrix3 instance representing the cross product equivalent matrix of a Cartesian3 vector.
  431. *
  432. * @param {Cartesian3} vector the vector on the left hand side of the cross product operation.
  433. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  434. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  435. *
  436. * @example
  437. * // Creates
  438. * // [0.0, -9.0, 8.0]
  439. * // [9.0, 0.0, -7.0]
  440. * // [-8.0, 7.0, 0.0]
  441. * const m = Cesium.Matrix3.fromCrossProduct(new Cesium.Cartesian3(7.0, 8.0, 9.0));
  442. */
  443. Matrix3.fromCrossProduct = function (vector, result) {
  444. //>>includeStart('debug', pragmas.debug);
  445. Check.typeOf.object("vector", vector);
  446. //>>includeEnd('debug');
  447. if (!defined(result)) {
  448. return new Matrix3(
  449. 0.0,
  450. -vector.z,
  451. vector.y,
  452. vector.z,
  453. 0.0,
  454. -vector.x,
  455. -vector.y,
  456. vector.x,
  457. 0.0
  458. );
  459. }
  460. result[0] = 0.0;
  461. result[1] = vector.z;
  462. result[2] = -vector.y;
  463. result[3] = -vector.z;
  464. result[4] = 0.0;
  465. result[5] = vector.x;
  466. result[6] = vector.y;
  467. result[7] = -vector.x;
  468. result[8] = 0.0;
  469. return result;
  470. };
  471. /**
  472. * Creates a rotation matrix around the x-axis.
  473. *
  474. * @param {number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
  475. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  476. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  477. *
  478. * @example
  479. * // Rotate a point 45 degrees counterclockwise around the x-axis.
  480. * const p = new Cesium.Cartesian3(5, 6, 7);
  481. * const m = Cesium.Matrix3.fromRotationX(Cesium.Math.toRadians(45.0));
  482. * const rotated = Cesium.Matrix3.multiplyByVector(m, p, new Cesium.Cartesian3());
  483. */
  484. Matrix3.fromRotationX = function (angle, result) {
  485. //>>includeStart('debug', pragmas.debug);
  486. Check.typeOf.number("angle", angle);
  487. //>>includeEnd('debug');
  488. const cosAngle = Math.cos(angle);
  489. const sinAngle = Math.sin(angle);
  490. if (!defined(result)) {
  491. return new Matrix3(
  492. 1.0,
  493. 0.0,
  494. 0.0,
  495. 0.0,
  496. cosAngle,
  497. -sinAngle,
  498. 0.0,
  499. sinAngle,
  500. cosAngle
  501. );
  502. }
  503. result[0] = 1.0;
  504. result[1] = 0.0;
  505. result[2] = 0.0;
  506. result[3] = 0.0;
  507. result[4] = cosAngle;
  508. result[5] = sinAngle;
  509. result[6] = 0.0;
  510. result[7] = -sinAngle;
  511. result[8] = cosAngle;
  512. return result;
  513. };
  514. /**
  515. * Creates a rotation matrix around the y-axis.
  516. *
  517. * @param {number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
  518. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  519. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  520. *
  521. * @example
  522. * // Rotate a point 45 degrees counterclockwise around the y-axis.
  523. * const p = new Cesium.Cartesian3(5, 6, 7);
  524. * const m = Cesium.Matrix3.fromRotationY(Cesium.Math.toRadians(45.0));
  525. * const rotated = Cesium.Matrix3.multiplyByVector(m, p, new Cesium.Cartesian3());
  526. */
  527. Matrix3.fromRotationY = function (angle, result) {
  528. //>>includeStart('debug', pragmas.debug);
  529. Check.typeOf.number("angle", angle);
  530. //>>includeEnd('debug');
  531. const cosAngle = Math.cos(angle);
  532. const sinAngle = Math.sin(angle);
  533. if (!defined(result)) {
  534. return new Matrix3(
  535. cosAngle,
  536. 0.0,
  537. sinAngle,
  538. 0.0,
  539. 1.0,
  540. 0.0,
  541. -sinAngle,
  542. 0.0,
  543. cosAngle
  544. );
  545. }
  546. result[0] = cosAngle;
  547. result[1] = 0.0;
  548. result[2] = -sinAngle;
  549. result[3] = 0.0;
  550. result[4] = 1.0;
  551. result[5] = 0.0;
  552. result[6] = sinAngle;
  553. result[7] = 0.0;
  554. result[8] = cosAngle;
  555. return result;
  556. };
  557. /**
  558. * Creates a rotation matrix around the z-axis.
  559. *
  560. * @param {number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
  561. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  562. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  563. *
  564. * @example
  565. * // Rotate a point 45 degrees counterclockwise around the z-axis.
  566. * const p = new Cesium.Cartesian3(5, 6, 7);
  567. * const m = Cesium.Matrix3.fromRotationZ(Cesium.Math.toRadians(45.0));
  568. * const rotated = Cesium.Matrix3.multiplyByVector(m, p, new Cesium.Cartesian3());
  569. */
  570. Matrix3.fromRotationZ = function (angle, result) {
  571. //>>includeStart('debug', pragmas.debug);
  572. Check.typeOf.number("angle", angle);
  573. //>>includeEnd('debug');
  574. const cosAngle = Math.cos(angle);
  575. const sinAngle = Math.sin(angle);
  576. if (!defined(result)) {
  577. return new Matrix3(
  578. cosAngle,
  579. -sinAngle,
  580. 0.0,
  581. sinAngle,
  582. cosAngle,
  583. 0.0,
  584. 0.0,
  585. 0.0,
  586. 1.0
  587. );
  588. }
  589. result[0] = cosAngle;
  590. result[1] = sinAngle;
  591. result[2] = 0.0;
  592. result[3] = -sinAngle;
  593. result[4] = cosAngle;
  594. result[5] = 0.0;
  595. result[6] = 0.0;
  596. result[7] = 0.0;
  597. result[8] = 1.0;
  598. return result;
  599. };
  600. /**
  601. * Creates an Array from the provided Matrix3 instance.
  602. * The array will be in column-major order.
  603. *
  604. * @param {Matrix3} matrix The matrix to use..
  605. * @param {number[]} [result] The Array onto which to store the result.
  606. * @returns {number[]} The modified Array parameter or a new Array instance if one was not provided.
  607. */
  608. Matrix3.toArray = function (matrix, result) {
  609. //>>includeStart('debug', pragmas.debug);
  610. Check.typeOf.object("matrix", matrix);
  611. //>>includeEnd('debug');
  612. if (!defined(result)) {
  613. return [
  614. matrix[0],
  615. matrix[1],
  616. matrix[2],
  617. matrix[3],
  618. matrix[4],
  619. matrix[5],
  620. matrix[6],
  621. matrix[7],
  622. matrix[8],
  623. ];
  624. }
  625. result[0] = matrix[0];
  626. result[1] = matrix[1];
  627. result[2] = matrix[2];
  628. result[3] = matrix[3];
  629. result[4] = matrix[4];
  630. result[5] = matrix[5];
  631. result[6] = matrix[6];
  632. result[7] = matrix[7];
  633. result[8] = matrix[8];
  634. return result;
  635. };
  636. /**
  637. * Computes the array index of the element at the provided row and column.
  638. *
  639. * @param {number} column The zero-based index of the column.
  640. * @param {number} row The zero-based index of the row.
  641. * @returns {number} The index of the element at the provided row and column.
  642. *
  643. * @exception {DeveloperError} row must be 0, 1, or 2.
  644. * @exception {DeveloperError} column must be 0, 1, or 2.
  645. *
  646. * @example
  647. * const myMatrix = new Cesium.Matrix3();
  648. * const column1Row0Index = Cesium.Matrix3.getElementIndex(1, 0);
  649. * const column1Row0 = myMatrix[column1Row0Index]
  650. * myMatrix[column1Row0Index] = 10.0;
  651. */
  652. Matrix3.getElementIndex = function (column, row) {
  653. //>>includeStart('debug', pragmas.debug);
  654. Check.typeOf.number.greaterThanOrEquals("row", row, 0);
  655. Check.typeOf.number.lessThanOrEquals("row", row, 2);
  656. Check.typeOf.number.greaterThanOrEquals("column", column, 0);
  657. Check.typeOf.number.lessThanOrEquals("column", column, 2);
  658. //>>includeEnd('debug');
  659. return column * 3 + row;
  660. };
  661. /**
  662. * Retrieves a copy of the matrix column at the provided index as a Cartesian3 instance.
  663. *
  664. * @param {Matrix3} matrix The matrix to use.
  665. * @param {number} index The zero-based index of the column to retrieve.
  666. * @param {Cartesian3} result The object onto which to store the result.
  667. * @returns {Cartesian3} The modified result parameter.
  668. *
  669. * @exception {DeveloperError} index must be 0, 1, or 2.
  670. */
  671. Matrix3.getColumn = function (matrix, index, result) {
  672. //>>includeStart('debug', pragmas.debug);
  673. Check.typeOf.object("matrix", matrix);
  674. Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  675. Check.typeOf.number.lessThanOrEquals("index", index, 2);
  676. Check.typeOf.object("result", result);
  677. //>>includeEnd('debug');
  678. const startIndex = index * 3;
  679. const x = matrix[startIndex];
  680. const y = matrix[startIndex + 1];
  681. const z = matrix[startIndex + 2];
  682. result.x = x;
  683. result.y = y;
  684. result.z = z;
  685. return result;
  686. };
  687. /**
  688. * Computes a new matrix that replaces the specified column in the provided matrix with the provided Cartesian3 instance.
  689. *
  690. * @param {Matrix3} matrix The matrix to use.
  691. * @param {number} index The zero-based index of the column to set.
  692. * @param {Cartesian3} cartesian The Cartesian whose values will be assigned to the specified column.
  693. * @param {Matrix3} result The object onto which to store the result.
  694. * @returns {Matrix3} The modified result parameter.
  695. *
  696. * @exception {DeveloperError} index must be 0, 1, or 2.
  697. */
  698. Matrix3.setColumn = function (matrix, index, cartesian, result) {
  699. //>>includeStart('debug', pragmas.debug);
  700. Check.typeOf.object("matrix", matrix);
  701. Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  702. Check.typeOf.number.lessThanOrEquals("index", index, 2);
  703. Check.typeOf.object("cartesian", cartesian);
  704. Check.typeOf.object("result", result);
  705. //>>includeEnd('debug');
  706. result = Matrix3.clone(matrix, result);
  707. const startIndex = index * 3;
  708. result[startIndex] = cartesian.x;
  709. result[startIndex + 1] = cartesian.y;
  710. result[startIndex + 2] = cartesian.z;
  711. return result;
  712. };
  713. /**
  714. * Retrieves a copy of the matrix row at the provided index as a Cartesian3 instance.
  715. *
  716. * @param {Matrix3} matrix The matrix to use.
  717. * @param {number} index The zero-based index of the row to retrieve.
  718. * @param {Cartesian3} result The object onto which to store the result.
  719. * @returns {Cartesian3} The modified result parameter.
  720. *
  721. * @exception {DeveloperError} index must be 0, 1, or 2.
  722. */
  723. Matrix3.getRow = function (matrix, index, result) {
  724. //>>includeStart('debug', pragmas.debug);
  725. Check.typeOf.object("matrix", matrix);
  726. Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  727. Check.typeOf.number.lessThanOrEquals("index", index, 2);
  728. Check.typeOf.object("result", result);
  729. //>>includeEnd('debug');
  730. const x = matrix[index];
  731. const y = matrix[index + 3];
  732. const z = matrix[index + 6];
  733. result.x = x;
  734. result.y = y;
  735. result.z = z;
  736. return result;
  737. };
  738. /**
  739. * Computes a new matrix that replaces the specified row in the provided matrix with the provided Cartesian3 instance.
  740. *
  741. * @param {Matrix3} matrix The matrix to use.
  742. * @param {number} index The zero-based index of the row to set.
  743. * @param {Cartesian3} cartesian The Cartesian whose values will be assigned to the specified row.
  744. * @param {Matrix3} result The object onto which to store the result.
  745. * @returns {Matrix3} The modified result parameter.
  746. *
  747. * @exception {DeveloperError} index must be 0, 1, or 2.
  748. */
  749. Matrix3.setRow = function (matrix, index, cartesian, result) {
  750. //>>includeStart('debug', pragmas.debug);
  751. Check.typeOf.object("matrix", matrix);
  752. Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  753. Check.typeOf.number.lessThanOrEquals("index", index, 2);
  754. Check.typeOf.object("cartesian", cartesian);
  755. Check.typeOf.object("result", result);
  756. //>>includeEnd('debug');
  757. result = Matrix3.clone(matrix, result);
  758. result[index] = cartesian.x;
  759. result[index + 3] = cartesian.y;
  760. result[index + 6] = cartesian.z;
  761. return result;
  762. };
  763. const scaleScratch1 = new Cartesian3();
  764. /**
  765. * Computes a new matrix that replaces the scale with the provided scale.
  766. * This assumes the matrix is an affine transformation.
  767. *
  768. * @param {Matrix3} matrix The matrix to use.
  769. * @param {Cartesian3} scale The scale that replaces the scale of the provided matrix.
  770. * @param {Matrix3} result The object onto which to store the result.
  771. * @returns {Matrix3} The modified result parameter.
  772. *
  773. * @see Matrix3.setUniformScale
  774. * @see Matrix3.fromScale
  775. * @see Matrix3.fromUniformScale
  776. * @see Matrix3.multiplyByScale
  777. * @see Matrix3.multiplyByUniformScale
  778. * @see Matrix3.getScale
  779. */
  780. Matrix3.setScale = function (matrix, scale, result) {
  781. //>>includeStart('debug', pragmas.debug);
  782. Check.typeOf.object("matrix", matrix);
  783. Check.typeOf.object("scale", scale);
  784. Check.typeOf.object("result", result);
  785. //>>includeEnd('debug');
  786. const existingScale = Matrix3.getScale(matrix, scaleScratch1);
  787. const scaleRatioX = scale.x / existingScale.x;
  788. const scaleRatioY = scale.y / existingScale.y;
  789. const scaleRatioZ = scale.z / existingScale.z;
  790. result[0] = matrix[0] * scaleRatioX;
  791. result[1] = matrix[1] * scaleRatioX;
  792. result[2] = matrix[2] * scaleRatioX;
  793. result[3] = matrix[3] * scaleRatioY;
  794. result[4] = matrix[4] * scaleRatioY;
  795. result[5] = matrix[5] * scaleRatioY;
  796. result[6] = matrix[6] * scaleRatioZ;
  797. result[7] = matrix[7] * scaleRatioZ;
  798. result[8] = matrix[8] * scaleRatioZ;
  799. return result;
  800. };
  801. const scaleScratch2 = new Cartesian3();
  802. /**
  803. * Computes a new matrix that replaces the scale with the provided uniform scale.
  804. * This assumes the matrix is an affine transformation.
  805. *
  806. * @param {Matrix3} matrix The matrix to use.
  807. * @param {number} scale The uniform scale that replaces the scale of the provided matrix.
  808. * @param {Matrix3} result The object onto which to store the result.
  809. * @returns {Matrix3} The modified result parameter.
  810. *
  811. * @see Matrix3.setScale
  812. * @see Matrix3.fromScale
  813. * @see Matrix3.fromUniformScale
  814. * @see Matrix3.multiplyByScale
  815. * @see Matrix3.multiplyByUniformScale
  816. * @see Matrix3.getScale
  817. */
  818. Matrix3.setUniformScale = function (matrix, scale, result) {
  819. //>>includeStart('debug', pragmas.debug);
  820. Check.typeOf.object("matrix", matrix);
  821. Check.typeOf.number("scale", scale);
  822. Check.typeOf.object("result", result);
  823. //>>includeEnd('debug');
  824. const existingScale = Matrix3.getScale(matrix, scaleScratch2);
  825. const scaleRatioX = scale / existingScale.x;
  826. const scaleRatioY = scale / existingScale.y;
  827. const scaleRatioZ = scale / existingScale.z;
  828. result[0] = matrix[0] * scaleRatioX;
  829. result[1] = matrix[1] * scaleRatioX;
  830. result[2] = matrix[2] * scaleRatioX;
  831. result[3] = matrix[3] * scaleRatioY;
  832. result[4] = matrix[4] * scaleRatioY;
  833. result[5] = matrix[5] * scaleRatioY;
  834. result[6] = matrix[6] * scaleRatioZ;
  835. result[7] = matrix[7] * scaleRatioZ;
  836. result[8] = matrix[8] * scaleRatioZ;
  837. return result;
  838. };
  839. const scratchColumn = new Cartesian3();
  840. /**
  841. * Extracts the non-uniform scale assuming the matrix is an affine transformation.
  842. *
  843. * @param {Matrix3} matrix The matrix.
  844. * @param {Cartesian3} result The object onto which to store the result.
  845. * @returns {Cartesian3} The modified result parameter.
  846. *
  847. * @see Matrix3.multiplyByScale
  848. * @see Matrix3.multiplyByUniformScale
  849. * @see Matrix3.fromScale
  850. * @see Matrix3.fromUniformScale
  851. * @see Matrix3.setScale
  852. * @see Matrix3.setUniformScale
  853. */
  854. Matrix3.getScale = function (matrix, result) {
  855. //>>includeStart('debug', pragmas.debug);
  856. Check.typeOf.object("matrix", matrix);
  857. Check.typeOf.object("result", result);
  858. //>>includeEnd('debug');
  859. result.x = Cartesian3.magnitude(
  860. Cartesian3.fromElements(matrix[0], matrix[1], matrix[2], scratchColumn)
  861. );
  862. result.y = Cartesian3.magnitude(
  863. Cartesian3.fromElements(matrix[3], matrix[4], matrix[5], scratchColumn)
  864. );
  865. result.z = Cartesian3.magnitude(
  866. Cartesian3.fromElements(matrix[6], matrix[7], matrix[8], scratchColumn)
  867. );
  868. return result;
  869. };
  870. const scaleScratch3 = new Cartesian3();
  871. /**
  872. * Computes the maximum scale assuming the matrix is an affine transformation.
  873. * The maximum scale is the maximum length of the column vectors.
  874. *
  875. * @param {Matrix3} matrix The matrix.
  876. * @returns {number} The maximum scale.
  877. */
  878. Matrix3.getMaximumScale = function (matrix) {
  879. Matrix3.getScale(matrix, scaleScratch3);
  880. return Cartesian3.maximumComponent(scaleScratch3);
  881. };
  882. const scaleScratch4 = new Cartesian3();
  883. /**
  884. * Sets the rotation assuming the matrix is an affine transformation.
  885. *
  886. * @param {Matrix3} matrix The matrix.
  887. * @param {Matrix3} rotation The rotation matrix.
  888. * @param {Matrix3} result The object onto which to store the result.
  889. * @returns {Matrix3} The modified result parameter.
  890. *
  891. * @see Matrix3.getRotation
  892. */
  893. Matrix3.setRotation = function (matrix, rotation, result) {
  894. //>>includeStart('debug', pragmas.debug);
  895. Check.typeOf.object("matrix", matrix);
  896. Check.typeOf.object("result", result);
  897. //>>includeEnd('debug');
  898. const scale = Matrix3.getScale(matrix, scaleScratch4);
  899. result[0] = rotation[0] * scale.x;
  900. result[1] = rotation[1] * scale.x;
  901. result[2] = rotation[2] * scale.x;
  902. result[3] = rotation[3] * scale.y;
  903. result[4] = rotation[4] * scale.y;
  904. result[5] = rotation[5] * scale.y;
  905. result[6] = rotation[6] * scale.z;
  906. result[7] = rotation[7] * scale.z;
  907. result[8] = rotation[8] * scale.z;
  908. return result;
  909. };
  910. const scaleScratch5 = new Cartesian3();
  911. /**
  912. * Extracts the rotation matrix assuming the matrix is an affine transformation.
  913. *
  914. * @param {Matrix3} matrix The matrix.
  915. * @param {Matrix3} result The object onto which to store the result.
  916. * @returns {Matrix3} The modified result parameter.
  917. *
  918. * @see Matrix3.setRotation
  919. */
  920. Matrix3.getRotation = function (matrix, result) {
  921. //>>includeStart('debug', pragmas.debug);
  922. Check.typeOf.object("matrix", matrix);
  923. Check.typeOf.object("result", result);
  924. //>>includeEnd('debug');
  925. const scale = Matrix3.getScale(matrix, scaleScratch5);
  926. result[0] = matrix[0] / scale.x;
  927. result[1] = matrix[1] / scale.x;
  928. result[2] = matrix[2] / scale.x;
  929. result[3] = matrix[3] / scale.y;
  930. result[4] = matrix[4] / scale.y;
  931. result[5] = matrix[5] / scale.y;
  932. result[6] = matrix[6] / scale.z;
  933. result[7] = matrix[7] / scale.z;
  934. result[8] = matrix[8] / scale.z;
  935. return result;
  936. };
  937. /**
  938. * Computes the product of two matrices.
  939. *
  940. * @param {Matrix3} left The first matrix.
  941. * @param {Matrix3} right The second matrix.
  942. * @param {Matrix3} result The object onto which to store the result.
  943. * @returns {Matrix3} The modified result parameter.
  944. */
  945. Matrix3.multiply = function (left, right, result) {
  946. //>>includeStart('debug', pragmas.debug);
  947. Check.typeOf.object("left", left);
  948. Check.typeOf.object("right", right);
  949. Check.typeOf.object("result", result);
  950. //>>includeEnd('debug');
  951. const column0Row0 =
  952. left[0] * right[0] + left[3] * right[1] + left[6] * right[2];
  953. const column0Row1 =
  954. left[1] * right[0] + left[4] * right[1] + left[7] * right[2];
  955. const column0Row2 =
  956. left[2] * right[0] + left[5] * right[1] + left[8] * right[2];
  957. const column1Row0 =
  958. left[0] * right[3] + left[3] * right[4] + left[6] * right[5];
  959. const column1Row1 =
  960. left[1] * right[3] + left[4] * right[4] + left[7] * right[5];
  961. const column1Row2 =
  962. left[2] * right[3] + left[5] * right[4] + left[8] * right[5];
  963. const column2Row0 =
  964. left[0] * right[6] + left[3] * right[7] + left[6] * right[8];
  965. const column2Row1 =
  966. left[1] * right[6] + left[4] * right[7] + left[7] * right[8];
  967. const column2Row2 =
  968. left[2] * right[6] + left[5] * right[7] + left[8] * right[8];
  969. result[0] = column0Row0;
  970. result[1] = column0Row1;
  971. result[2] = column0Row2;
  972. result[3] = column1Row0;
  973. result[4] = column1Row1;
  974. result[5] = column1Row2;
  975. result[6] = column2Row0;
  976. result[7] = column2Row1;
  977. result[8] = column2Row2;
  978. return result;
  979. };
  980. /**
  981. * Computes the sum of two matrices.
  982. *
  983. * @param {Matrix3} left The first matrix.
  984. * @param {Matrix3} right The second matrix.
  985. * @param {Matrix3} result The object onto which to store the result.
  986. * @returns {Matrix3} The modified result parameter.
  987. */
  988. Matrix3.add = function (left, right, result) {
  989. //>>includeStart('debug', pragmas.debug);
  990. Check.typeOf.object("left", left);
  991. Check.typeOf.object("right", right);
  992. Check.typeOf.object("result", result);
  993. //>>includeEnd('debug');
  994. result[0] = left[0] + right[0];
  995. result[1] = left[1] + right[1];
  996. result[2] = left[2] + right[2];
  997. result[3] = left[3] + right[3];
  998. result[4] = left[4] + right[4];
  999. result[5] = left[5] + right[5];
  1000. result[6] = left[6] + right[6];
  1001. result[7] = left[7] + right[7];
  1002. result[8] = left[8] + right[8];
  1003. return result;
  1004. };
  1005. /**
  1006. * Computes the difference of two matrices.
  1007. *
  1008. * @param {Matrix3} left The first matrix.
  1009. * @param {Matrix3} right The second matrix.
  1010. * @param {Matrix3} result The object onto which to store the result.
  1011. * @returns {Matrix3} The modified result parameter.
  1012. */
  1013. Matrix3.subtract = function (left, right, result) {
  1014. //>>includeStart('debug', pragmas.debug);
  1015. Check.typeOf.object("left", left);
  1016. Check.typeOf.object("right", right);
  1017. Check.typeOf.object("result", result);
  1018. //>>includeEnd('debug');
  1019. result[0] = left[0] - right[0];
  1020. result[1] = left[1] - right[1];
  1021. result[2] = left[2] - right[2];
  1022. result[3] = left[3] - right[3];
  1023. result[4] = left[4] - right[4];
  1024. result[5] = left[5] - right[5];
  1025. result[6] = left[6] - right[6];
  1026. result[7] = left[7] - right[7];
  1027. result[8] = left[8] - right[8];
  1028. return result;
  1029. };
  1030. /**
  1031. * Computes the product of a matrix and a column vector.
  1032. *
  1033. * @param {Matrix3} matrix The matrix.
  1034. * @param {Cartesian3} cartesian The column.
  1035. * @param {Cartesian3} result The object onto which to store the result.
  1036. * @returns {Cartesian3} The modified result parameter.
  1037. */
  1038. Matrix3.multiplyByVector = function (matrix, cartesian, result) {
  1039. //>>includeStart('debug', pragmas.debug);
  1040. Check.typeOf.object("matrix", matrix);
  1041. Check.typeOf.object("cartesian", cartesian);
  1042. Check.typeOf.object("result", result);
  1043. //>>includeEnd('debug');
  1044. const vX = cartesian.x;
  1045. const vY = cartesian.y;
  1046. const vZ = cartesian.z;
  1047. const x = matrix[0] * vX + matrix[3] * vY + matrix[6] * vZ;
  1048. const y = matrix[1] * vX + matrix[4] * vY + matrix[7] * vZ;
  1049. const z = matrix[2] * vX + matrix[5] * vY + matrix[8] * vZ;
  1050. result.x = x;
  1051. result.y = y;
  1052. result.z = z;
  1053. return result;
  1054. };
  1055. /**
  1056. * Computes the product of a matrix and a scalar.
  1057. *
  1058. * @param {Matrix3} matrix The matrix.
  1059. * @param {number} scalar The number to multiply by.
  1060. * @param {Matrix3} result The object onto which to store the result.
  1061. * @returns {Matrix3} The modified result parameter.
  1062. */
  1063. Matrix3.multiplyByScalar = function (matrix, scalar, result) {
  1064. //>>includeStart('debug', pragmas.debug);
  1065. Check.typeOf.object("matrix", matrix);
  1066. Check.typeOf.number("scalar", scalar);
  1067. Check.typeOf.object("result", result);
  1068. //>>includeEnd('debug');
  1069. result[0] = matrix[0] * scalar;
  1070. result[1] = matrix[1] * scalar;
  1071. result[2] = matrix[2] * scalar;
  1072. result[3] = matrix[3] * scalar;
  1073. result[4] = matrix[4] * scalar;
  1074. result[5] = matrix[5] * scalar;
  1075. result[6] = matrix[6] * scalar;
  1076. result[7] = matrix[7] * scalar;
  1077. result[8] = matrix[8] * scalar;
  1078. return result;
  1079. };
  1080. /**
  1081. * Computes the product of a matrix times a (non-uniform) scale, as if the scale were a scale matrix.
  1082. *
  1083. * @param {Matrix3} matrix The matrix on the left-hand side.
  1084. * @param {Cartesian3} scale The non-uniform scale on the right-hand side.
  1085. * @param {Matrix3} result The object onto which to store the result.
  1086. * @returns {Matrix3} The modified result parameter.
  1087. *
  1088. *
  1089. * @example
  1090. * // Instead of Cesium.Matrix3.multiply(m, Cesium.Matrix3.fromScale(scale), m);
  1091. * Cesium.Matrix3.multiplyByScale(m, scale, m);
  1092. *
  1093. * @see Matrix3.multiplyByUniformScale
  1094. * @see Matrix3.fromScale
  1095. * @see Matrix3.fromUniformScale
  1096. * @see Matrix3.setScale
  1097. * @see Matrix3.setUniformScale
  1098. * @see Matrix3.getScale
  1099. */
  1100. Matrix3.multiplyByScale = function (matrix, scale, result) {
  1101. //>>includeStart('debug', pragmas.debug);
  1102. Check.typeOf.object("matrix", matrix);
  1103. Check.typeOf.object("scale", scale);
  1104. Check.typeOf.object("result", result);
  1105. //>>includeEnd('debug');
  1106. result[0] = matrix[0] * scale.x;
  1107. result[1] = matrix[1] * scale.x;
  1108. result[2] = matrix[2] * scale.x;
  1109. result[3] = matrix[3] * scale.y;
  1110. result[4] = matrix[4] * scale.y;
  1111. result[5] = matrix[5] * scale.y;
  1112. result[6] = matrix[6] * scale.z;
  1113. result[7] = matrix[7] * scale.z;
  1114. result[8] = matrix[8] * scale.z;
  1115. return result;
  1116. };
  1117. /**
  1118. * Computes the product of a matrix times a uniform scale, as if the scale were a scale matrix.
  1119. *
  1120. * @param {Matrix3} matrix The matrix on the left-hand side.
  1121. * @param {number} scale The uniform scale on the right-hand side.
  1122. * @param {Matrix3} result The object onto which to store the result.
  1123. * @returns {Matrix3} The modified result parameter.
  1124. *
  1125. * @example
  1126. * // Instead of Cesium.Matrix3.multiply(m, Cesium.Matrix3.fromUniformScale(scale), m);
  1127. * Cesium.Matrix3.multiplyByUniformScale(m, scale, m);
  1128. *
  1129. * @see Matrix3.multiplyByScale
  1130. * @see Matrix3.fromScale
  1131. * @see Matrix3.fromUniformScale
  1132. * @see Matrix3.setScale
  1133. * @see Matrix3.setUniformScale
  1134. * @see Matrix3.getScale
  1135. */
  1136. Matrix3.multiplyByUniformScale = function (matrix, scale, result) {
  1137. //>>includeStart('debug', pragmas.debug);
  1138. Check.typeOf.object("matrix", matrix);
  1139. Check.typeOf.number("scale", scale);
  1140. Check.typeOf.object("result", result);
  1141. //>>includeEnd('debug');
  1142. result[0] = matrix[0] * scale;
  1143. result[1] = matrix[1] * scale;
  1144. result[2] = matrix[2] * scale;
  1145. result[3] = matrix[3] * scale;
  1146. result[4] = matrix[4] * scale;
  1147. result[5] = matrix[5] * scale;
  1148. result[6] = matrix[6] * scale;
  1149. result[7] = matrix[7] * scale;
  1150. result[8] = matrix[8] * scale;
  1151. return result;
  1152. };
  1153. /**
  1154. * Creates a negated copy of the provided matrix.
  1155. *
  1156. * @param {Matrix3} matrix The matrix to negate.
  1157. * @param {Matrix3} result The object onto which to store the result.
  1158. * @returns {Matrix3} The modified result parameter.
  1159. */
  1160. Matrix3.negate = function (matrix, result) {
  1161. //>>includeStart('debug', pragmas.debug);
  1162. Check.typeOf.object("matrix", matrix);
  1163. Check.typeOf.object("result", result);
  1164. //>>includeEnd('debug');
  1165. result[0] = -matrix[0];
  1166. result[1] = -matrix[1];
  1167. result[2] = -matrix[2];
  1168. result[3] = -matrix[3];
  1169. result[4] = -matrix[4];
  1170. result[5] = -matrix[5];
  1171. result[6] = -matrix[6];
  1172. result[7] = -matrix[7];
  1173. result[8] = -matrix[8];
  1174. return result;
  1175. };
  1176. /**
  1177. * Computes the transpose of the provided matrix.
  1178. *
  1179. * @param {Matrix3} matrix The matrix to transpose.
  1180. * @param {Matrix3} result The object onto which to store the result.
  1181. * @returns {Matrix3} The modified result parameter.
  1182. */
  1183. Matrix3.transpose = function (matrix, result) {
  1184. //>>includeStart('debug', pragmas.debug);
  1185. Check.typeOf.object("matrix", matrix);
  1186. Check.typeOf.object("result", result);
  1187. //>>includeEnd('debug');
  1188. const column0Row0 = matrix[0];
  1189. const column0Row1 = matrix[3];
  1190. const column0Row2 = matrix[6];
  1191. const column1Row0 = matrix[1];
  1192. const column1Row1 = matrix[4];
  1193. const column1Row2 = matrix[7];
  1194. const column2Row0 = matrix[2];
  1195. const column2Row1 = matrix[5];
  1196. const column2Row2 = matrix[8];
  1197. result[0] = column0Row0;
  1198. result[1] = column0Row1;
  1199. result[2] = column0Row2;
  1200. result[3] = column1Row0;
  1201. result[4] = column1Row1;
  1202. result[5] = column1Row2;
  1203. result[6] = column2Row0;
  1204. result[7] = column2Row1;
  1205. result[8] = column2Row2;
  1206. return result;
  1207. };
  1208. function computeFrobeniusNorm(matrix) {
  1209. let norm = 0.0;
  1210. for (let i = 0; i < 9; ++i) {
  1211. const temp = matrix[i];
  1212. norm += temp * temp;
  1213. }
  1214. return Math.sqrt(norm);
  1215. }
  1216. const rowVal = [1, 0, 0];
  1217. const colVal = [2, 2, 1];
  1218. function offDiagonalFrobeniusNorm(matrix) {
  1219. // Computes the "off-diagonal" Frobenius norm.
  1220. // Assumes matrix is symmetric.
  1221. let norm = 0.0;
  1222. for (let i = 0; i < 3; ++i) {
  1223. const temp = matrix[Matrix3.getElementIndex(colVal[i], rowVal[i])];
  1224. norm += 2.0 * temp * temp;
  1225. }
  1226. return Math.sqrt(norm);
  1227. }
  1228. function shurDecomposition(matrix, result) {
  1229. // This routine was created based upon Matrix Computations, 3rd ed., by Golub and Van Loan,
  1230. // section 8.4.2 The 2by2 Symmetric Schur Decomposition.
  1231. //
  1232. // The routine takes a matrix, which is assumed to be symmetric, and
  1233. // finds the largest off-diagonal term, and then creates
  1234. // a matrix (result) which can be used to help reduce it
  1235. const tolerance = CesiumMath.EPSILON15;
  1236. let maxDiagonal = 0.0;
  1237. let rotAxis = 1;
  1238. // find pivot (rotAxis) based on max diagonal of matrix
  1239. for (let i = 0; i < 3; ++i) {
  1240. const temp = Math.abs(
  1241. matrix[Matrix3.getElementIndex(colVal[i], rowVal[i])]
  1242. );
  1243. if (temp > maxDiagonal) {
  1244. rotAxis = i;
  1245. maxDiagonal = temp;
  1246. }
  1247. }
  1248. let c = 1.0;
  1249. let s = 0.0;
  1250. const p = rowVal[rotAxis];
  1251. const q = colVal[rotAxis];
  1252. if (Math.abs(matrix[Matrix3.getElementIndex(q, p)]) > tolerance) {
  1253. const qq = matrix[Matrix3.getElementIndex(q, q)];
  1254. const pp = matrix[Matrix3.getElementIndex(p, p)];
  1255. const qp = matrix[Matrix3.getElementIndex(q, p)];
  1256. const tau = (qq - pp) / 2.0 / qp;
  1257. let t;
  1258. if (tau < 0.0) {
  1259. t = -1.0 / (-tau + Math.sqrt(1.0 + tau * tau));
  1260. } else {
  1261. t = 1.0 / (tau + Math.sqrt(1.0 + tau * tau));
  1262. }
  1263. c = 1.0 / Math.sqrt(1.0 + t * t);
  1264. s = t * c;
  1265. }
  1266. result = Matrix3.clone(Matrix3.IDENTITY, result);
  1267. result[Matrix3.getElementIndex(p, p)] = result[
  1268. Matrix3.getElementIndex(q, q)
  1269. ] = c;
  1270. result[Matrix3.getElementIndex(q, p)] = s;
  1271. result[Matrix3.getElementIndex(p, q)] = -s;
  1272. return result;
  1273. }
  1274. const jMatrix = new Matrix3();
  1275. const jMatrixTranspose = new Matrix3();
  1276. /**
  1277. * Computes the eigenvectors and eigenvalues of a symmetric matrix.
  1278. * <p>
  1279. * Returns a diagonal matrix and unitary matrix such that:
  1280. * <code>matrix = unitary matrix * diagonal matrix * transpose(unitary matrix)</code>
  1281. * </p>
  1282. * <p>
  1283. * The values along the diagonal of the diagonal matrix are the eigenvalues. The columns
  1284. * of the unitary matrix are the corresponding eigenvectors.
  1285. * </p>
  1286. *
  1287. * @param {Matrix3} matrix The matrix to decompose into diagonal and unitary matrix. Expected to be symmetric.
  1288. * @param {object} [result] An object with unitary and diagonal properties which are matrices onto which to store the result.
  1289. * @returns {object} An object with unitary and diagonal properties which are the unitary and diagonal matrices, respectively.
  1290. *
  1291. * @example
  1292. * const a = //... symetric matrix
  1293. * const result = {
  1294. * unitary : new Cesium.Matrix3(),
  1295. * diagonal : new Cesium.Matrix3()
  1296. * };
  1297. * Cesium.Matrix3.computeEigenDecomposition(a, result);
  1298. *
  1299. * const unitaryTranspose = Cesium.Matrix3.transpose(result.unitary, new Cesium.Matrix3());
  1300. * const b = Cesium.Matrix3.multiply(result.unitary, result.diagonal, new Cesium.Matrix3());
  1301. * Cesium.Matrix3.multiply(b, unitaryTranspose, b); // b is now equal to a
  1302. *
  1303. * const lambda = Cesium.Matrix3.getColumn(result.diagonal, 0, new Cesium.Cartesian3()).x; // first eigenvalue
  1304. * const v = Cesium.Matrix3.getColumn(result.unitary, 0, new Cesium.Cartesian3()); // first eigenvector
  1305. * const c = Cesium.Cartesian3.multiplyByScalar(v, lambda, new Cesium.Cartesian3()); // equal to Cesium.Matrix3.multiplyByVector(a, v)
  1306. */
  1307. Matrix3.computeEigenDecomposition = function (matrix, result) {
  1308. //>>includeStart('debug', pragmas.debug);
  1309. Check.typeOf.object("matrix", matrix);
  1310. //>>includeEnd('debug');
  1311. // This routine was created based upon Matrix Computations, 3rd ed., by Golub and Van Loan,
  1312. // section 8.4.3 The Classical Jacobi Algorithm
  1313. const tolerance = CesiumMath.EPSILON20;
  1314. const maxSweeps = 10;
  1315. let count = 0;
  1316. let sweep = 0;
  1317. if (!defined(result)) {
  1318. result = {};
  1319. }
  1320. const unitaryMatrix = (result.unitary = Matrix3.clone(
  1321. Matrix3.IDENTITY,
  1322. result.unitary
  1323. ));
  1324. const diagMatrix = (result.diagonal = Matrix3.clone(matrix, result.diagonal));
  1325. const epsilon = tolerance * computeFrobeniusNorm(diagMatrix);
  1326. while (sweep < maxSweeps && offDiagonalFrobeniusNorm(diagMatrix) > epsilon) {
  1327. shurDecomposition(diagMatrix, jMatrix);
  1328. Matrix3.transpose(jMatrix, jMatrixTranspose);
  1329. Matrix3.multiply(diagMatrix, jMatrix, diagMatrix);
  1330. Matrix3.multiply(jMatrixTranspose, diagMatrix, diagMatrix);
  1331. Matrix3.multiply(unitaryMatrix, jMatrix, unitaryMatrix);
  1332. if (++count > 2) {
  1333. ++sweep;
  1334. count = 0;
  1335. }
  1336. }
  1337. return result;
  1338. };
  1339. /**
  1340. * Computes a matrix, which contains the absolute (unsigned) values of the provided matrix's elements.
  1341. *
  1342. * @param {Matrix3} matrix The matrix with signed elements.
  1343. * @param {Matrix3} result The object onto which to store the result.
  1344. * @returns {Matrix3} The modified result parameter.
  1345. */
  1346. Matrix3.abs = function (matrix, result) {
  1347. //>>includeStart('debug', pragmas.debug);
  1348. Check.typeOf.object("matrix", matrix);
  1349. Check.typeOf.object("result", result);
  1350. //>>includeEnd('debug');
  1351. result[0] = Math.abs(matrix[0]);
  1352. result[1] = Math.abs(matrix[1]);
  1353. result[2] = Math.abs(matrix[2]);
  1354. result[3] = Math.abs(matrix[3]);
  1355. result[4] = Math.abs(matrix[4]);
  1356. result[5] = Math.abs(matrix[5]);
  1357. result[6] = Math.abs(matrix[6]);
  1358. result[7] = Math.abs(matrix[7]);
  1359. result[8] = Math.abs(matrix[8]);
  1360. return result;
  1361. };
  1362. /**
  1363. * Computes the determinant of the provided matrix.
  1364. *
  1365. * @param {Matrix3} matrix The matrix to use.
  1366. * @returns {number} The value of the determinant of the matrix.
  1367. */
  1368. Matrix3.determinant = function (matrix) {
  1369. //>>includeStart('debug', pragmas.debug);
  1370. Check.typeOf.object("matrix", matrix);
  1371. //>>includeEnd('debug');
  1372. const m11 = matrix[0];
  1373. const m21 = matrix[3];
  1374. const m31 = matrix[6];
  1375. const m12 = matrix[1];
  1376. const m22 = matrix[4];
  1377. const m32 = matrix[7];
  1378. const m13 = matrix[2];
  1379. const m23 = matrix[5];
  1380. const m33 = matrix[8];
  1381. return (
  1382. m11 * (m22 * m33 - m23 * m32) +
  1383. m12 * (m23 * m31 - m21 * m33) +
  1384. m13 * (m21 * m32 - m22 * m31)
  1385. );
  1386. };
  1387. /**
  1388. * Computes the inverse of the provided matrix.
  1389. *
  1390. * @param {Matrix3} matrix The matrix to invert.
  1391. * @param {Matrix3} result The object onto which to store the result.
  1392. * @returns {Matrix3} The modified result parameter.
  1393. *
  1394. * @exception {DeveloperError} matrix is not invertible.
  1395. */
  1396. Matrix3.inverse = function (matrix, result) {
  1397. //>>includeStart('debug', pragmas.debug);
  1398. Check.typeOf.object("matrix", matrix);
  1399. Check.typeOf.object("result", result);
  1400. //>>includeEnd('debug');
  1401. const m11 = matrix[0];
  1402. const m21 = matrix[1];
  1403. const m31 = matrix[2];
  1404. const m12 = matrix[3];
  1405. const m22 = matrix[4];
  1406. const m32 = matrix[5];
  1407. const m13 = matrix[6];
  1408. const m23 = matrix[7];
  1409. const m33 = matrix[8];
  1410. const determinant = Matrix3.determinant(matrix);
  1411. //>>includeStart('debug', pragmas.debug);
  1412. if (Math.abs(determinant) <= CesiumMath.EPSILON15) {
  1413. throw new DeveloperError("matrix is not invertible");
  1414. }
  1415. //>>includeEnd('debug');
  1416. result[0] = m22 * m33 - m23 * m32;
  1417. result[1] = m23 * m31 - m21 * m33;
  1418. result[2] = m21 * m32 - m22 * m31;
  1419. result[3] = m13 * m32 - m12 * m33;
  1420. result[4] = m11 * m33 - m13 * m31;
  1421. result[5] = m12 * m31 - m11 * m32;
  1422. result[6] = m12 * m23 - m13 * m22;
  1423. result[7] = m13 * m21 - m11 * m23;
  1424. result[8] = m11 * m22 - m12 * m21;
  1425. const scale = 1.0 / determinant;
  1426. return Matrix3.multiplyByScalar(result, scale, result);
  1427. };
  1428. const scratchTransposeMatrix = new Matrix3();
  1429. /**
  1430. * Computes the inverse transpose of a matrix.
  1431. *
  1432. * @param {Matrix3} matrix The matrix to transpose and invert.
  1433. * @param {Matrix3} result The object onto which to store the result.
  1434. * @returns {Matrix3} The modified result parameter.
  1435. */
  1436. Matrix3.inverseTranspose = function (matrix, result) {
  1437. //>>includeStart('debug', pragmas.debug);
  1438. Check.typeOf.object("matrix", matrix);
  1439. Check.typeOf.object("result", result);
  1440. //>>includeEnd('debug');
  1441. return Matrix3.inverse(
  1442. Matrix3.transpose(matrix, scratchTransposeMatrix),
  1443. result
  1444. );
  1445. };
  1446. /**
  1447. * Compares the provided matrices componentwise and returns
  1448. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1449. *
  1450. * @param {Matrix3} [left] The first matrix.
  1451. * @param {Matrix3} [right] The second matrix.
  1452. * @returns {boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  1453. */
  1454. Matrix3.equals = function (left, right) {
  1455. return (
  1456. left === right ||
  1457. (defined(left) &&
  1458. defined(right) &&
  1459. left[0] === right[0] &&
  1460. left[1] === right[1] &&
  1461. left[2] === right[2] &&
  1462. left[3] === right[3] &&
  1463. left[4] === right[4] &&
  1464. left[5] === right[5] &&
  1465. left[6] === right[6] &&
  1466. left[7] === right[7] &&
  1467. left[8] === right[8])
  1468. );
  1469. };
  1470. /**
  1471. * Compares the provided matrices componentwise and returns
  1472. * <code>true</code> if they are within the provided epsilon,
  1473. * <code>false</code> otherwise.
  1474. *
  1475. * @param {Matrix3} [left] The first matrix.
  1476. * @param {Matrix3} [right] The second matrix.
  1477. * @param {number} [epsilon=0] The epsilon to use for equality testing.
  1478. * @returns {boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  1479. */
  1480. Matrix3.equalsEpsilon = function (left, right, epsilon) {
  1481. epsilon = defaultValue(epsilon, 0);
  1482. return (
  1483. left === right ||
  1484. (defined(left) &&
  1485. defined(right) &&
  1486. Math.abs(left[0] - right[0]) <= epsilon &&
  1487. Math.abs(left[1] - right[1]) <= epsilon &&
  1488. Math.abs(left[2] - right[2]) <= epsilon &&
  1489. Math.abs(left[3] - right[3]) <= epsilon &&
  1490. Math.abs(left[4] - right[4]) <= epsilon &&
  1491. Math.abs(left[5] - right[5]) <= epsilon &&
  1492. Math.abs(left[6] - right[6]) <= epsilon &&
  1493. Math.abs(left[7] - right[7]) <= epsilon &&
  1494. Math.abs(left[8] - right[8]) <= epsilon)
  1495. );
  1496. };
  1497. /**
  1498. * An immutable Matrix3 instance initialized to the identity matrix.
  1499. *
  1500. * @type {Matrix3}
  1501. * @constant
  1502. */
  1503. Matrix3.IDENTITY = Object.freeze(
  1504. new Matrix3(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0)
  1505. );
  1506. /**
  1507. * An immutable Matrix3 instance initialized to the zero matrix.
  1508. *
  1509. * @type {Matrix3}
  1510. * @constant
  1511. */
  1512. Matrix3.ZERO = Object.freeze(
  1513. new Matrix3(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
  1514. );
  1515. /**
  1516. * The index into Matrix3 for column 0, row 0.
  1517. *
  1518. * @type {number}
  1519. * @constant
  1520. */
  1521. Matrix3.COLUMN0ROW0 = 0;
  1522. /**
  1523. * The index into Matrix3 for column 0, row 1.
  1524. *
  1525. * @type {number}
  1526. * @constant
  1527. */
  1528. Matrix3.COLUMN0ROW1 = 1;
  1529. /**
  1530. * The index into Matrix3 for column 0, row 2.
  1531. *
  1532. * @type {number}
  1533. * @constant
  1534. */
  1535. Matrix3.COLUMN0ROW2 = 2;
  1536. /**
  1537. * The index into Matrix3 for column 1, row 0.
  1538. *
  1539. * @type {number}
  1540. * @constant
  1541. */
  1542. Matrix3.COLUMN1ROW0 = 3;
  1543. /**
  1544. * The index into Matrix3 for column 1, row 1.
  1545. *
  1546. * @type {number}
  1547. * @constant
  1548. */
  1549. Matrix3.COLUMN1ROW1 = 4;
  1550. /**
  1551. * The index into Matrix3 for column 1, row 2.
  1552. *
  1553. * @type {number}
  1554. * @constant
  1555. */
  1556. Matrix3.COLUMN1ROW2 = 5;
  1557. /**
  1558. * The index into Matrix3 for column 2, row 0.
  1559. *
  1560. * @type {number}
  1561. * @constant
  1562. */
  1563. Matrix3.COLUMN2ROW0 = 6;
  1564. /**
  1565. * The index into Matrix3 for column 2, row 1.
  1566. *
  1567. * @type {number}
  1568. * @constant
  1569. */
  1570. Matrix3.COLUMN2ROW1 = 7;
  1571. /**
  1572. * The index into Matrix3 for column 2, row 2.
  1573. *
  1574. * @type {number}
  1575. * @constant
  1576. */
  1577. Matrix3.COLUMN2ROW2 = 8;
  1578. Object.defineProperties(Matrix3.prototype, {
  1579. /**
  1580. * Gets the number of items in the collection.
  1581. * @memberof Matrix3.prototype
  1582. *
  1583. * @type {number}
  1584. */
  1585. length: {
  1586. get: function () {
  1587. return Matrix3.packedLength;
  1588. },
  1589. },
  1590. });
  1591. /**
  1592. * Duplicates the provided Matrix3 instance.
  1593. *
  1594. * @param {Matrix3} [result] The object onto which to store the result.
  1595. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided.
  1596. */
  1597. Matrix3.prototype.clone = function (result) {
  1598. return Matrix3.clone(this, result);
  1599. };
  1600. /**
  1601. * Compares this matrix to the provided matrix componentwise and returns
  1602. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1603. *
  1604. * @param {Matrix3} [right] The right hand side matrix.
  1605. * @returns {boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  1606. */
  1607. Matrix3.prototype.equals = function (right) {
  1608. return Matrix3.equals(this, right);
  1609. };
  1610. /**
  1611. * @private
  1612. */
  1613. Matrix3.equalsArray = function (matrix, array, offset) {
  1614. return (
  1615. matrix[0] === array[offset] &&
  1616. matrix[1] === array[offset + 1] &&
  1617. matrix[2] === array[offset + 2] &&
  1618. matrix[3] === array[offset + 3] &&
  1619. matrix[4] === array[offset + 4] &&
  1620. matrix[5] === array[offset + 5] &&
  1621. matrix[6] === array[offset + 6] &&
  1622. matrix[7] === array[offset + 7] &&
  1623. matrix[8] === array[offset + 8]
  1624. );
  1625. };
  1626. /**
  1627. * Compares this matrix to the provided matrix componentwise and returns
  1628. * <code>true</code> if they are within the provided epsilon,
  1629. * <code>false</code> otherwise.
  1630. *
  1631. * @param {Matrix3} [right] The right hand side matrix.
  1632. * @param {number} [epsilon=0] The epsilon to use for equality testing.
  1633. * @returns {boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  1634. */
  1635. Matrix3.prototype.equalsEpsilon = function (right, epsilon) {
  1636. return Matrix3.equalsEpsilon(this, right, epsilon);
  1637. };
  1638. /**
  1639. * Creates a string representing this Matrix with each row being
  1640. * on a separate line and in the format '(column0, column1, column2)'.
  1641. *
  1642. * @returns {string} A string representing the provided Matrix with each row being on a separate line and in the format '(column0, column1, column2)'.
  1643. */
  1644. Matrix3.prototype.toString = function () {
  1645. return (
  1646. `(${this[0]}, ${this[3]}, ${this[6]})\n` +
  1647. `(${this[1]}, ${this[4]}, ${this[7]})\n` +
  1648. `(${this[2]}, ${this[5]}, ${this[8]})`
  1649. );
  1650. };
  1651. export default Matrix3;