Matrix3.js 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838
  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. * @returns {Matrix3} The modified result parameter.
  889. *
  890. * @see Matrix3.getRotation
  891. */
  892. Matrix3.setRotation = function (matrix, rotation, result) {
  893. //>>includeStart('debug', pragmas.debug);
  894. Check.typeOf.object("matrix", matrix);
  895. Check.typeOf.object("result", result);
  896. //>>includeEnd('debug');
  897. const scale = Matrix3.getScale(matrix, scaleScratch4);
  898. result[0] = rotation[0] * scale.x;
  899. result[1] = rotation[1] * scale.x;
  900. result[2] = rotation[2] * scale.x;
  901. result[3] = rotation[3] * scale.y;
  902. result[4] = rotation[4] * scale.y;
  903. result[5] = rotation[5] * scale.y;
  904. result[6] = rotation[6] * scale.z;
  905. result[7] = rotation[7] * scale.z;
  906. result[8] = rotation[8] * scale.z;
  907. return result;
  908. };
  909. const scaleScratch5 = new Cartesian3();
  910. /**
  911. * Extracts the rotation matrix assuming the matrix is an affine transformation.
  912. *
  913. * @param {Matrix3} matrix The matrix.
  914. * @param {Matrix3} result The object onto which to store the result.
  915. * @returns {Matrix3} The modified result parameter.
  916. *
  917. * @see Matrix3.setRotation
  918. */
  919. Matrix3.getRotation = function (matrix, result) {
  920. //>>includeStart('debug', pragmas.debug);
  921. Check.typeOf.object("matrix", matrix);
  922. Check.typeOf.object("result", result);
  923. //>>includeEnd('debug');
  924. const scale = Matrix3.getScale(matrix, scaleScratch5);
  925. result[0] = matrix[0] / scale.x;
  926. result[1] = matrix[1] / scale.x;
  927. result[2] = matrix[2] / scale.x;
  928. result[3] = matrix[3] / scale.y;
  929. result[4] = matrix[4] / scale.y;
  930. result[5] = matrix[5] / scale.y;
  931. result[6] = matrix[6] / scale.z;
  932. result[7] = matrix[7] / scale.z;
  933. result[8] = matrix[8] / scale.z;
  934. return result;
  935. };
  936. /**
  937. * Computes the product of two matrices.
  938. *
  939. * @param {Matrix3} left The first matrix.
  940. * @param {Matrix3} right The second matrix.
  941. * @param {Matrix3} result The object onto which to store the result.
  942. * @returns {Matrix3} The modified result parameter.
  943. */
  944. Matrix3.multiply = function (left, right, result) {
  945. //>>includeStart('debug', pragmas.debug);
  946. Check.typeOf.object("left", left);
  947. Check.typeOf.object("right", right);
  948. Check.typeOf.object("result", result);
  949. //>>includeEnd('debug');
  950. const column0Row0 =
  951. left[0] * right[0] + left[3] * right[1] + left[6] * right[2];
  952. const column0Row1 =
  953. left[1] * right[0] + left[4] * right[1] + left[7] * right[2];
  954. const column0Row2 =
  955. left[2] * right[0] + left[5] * right[1] + left[8] * right[2];
  956. const column1Row0 =
  957. left[0] * right[3] + left[3] * right[4] + left[6] * right[5];
  958. const column1Row1 =
  959. left[1] * right[3] + left[4] * right[4] + left[7] * right[5];
  960. const column1Row2 =
  961. left[2] * right[3] + left[5] * right[4] + left[8] * right[5];
  962. const column2Row0 =
  963. left[0] * right[6] + left[3] * right[7] + left[6] * right[8];
  964. const column2Row1 =
  965. left[1] * right[6] + left[4] * right[7] + left[7] * right[8];
  966. const column2Row2 =
  967. left[2] * right[6] + left[5] * right[7] + left[8] * right[8];
  968. result[0] = column0Row0;
  969. result[1] = column0Row1;
  970. result[2] = column0Row2;
  971. result[3] = column1Row0;
  972. result[4] = column1Row1;
  973. result[5] = column1Row2;
  974. result[6] = column2Row0;
  975. result[7] = column2Row1;
  976. result[8] = column2Row2;
  977. return result;
  978. };
  979. /**
  980. * Computes the sum of two matrices.
  981. *
  982. * @param {Matrix3} left The first matrix.
  983. * @param {Matrix3} right The second matrix.
  984. * @param {Matrix3} result The object onto which to store the result.
  985. * @returns {Matrix3} The modified result parameter.
  986. */
  987. Matrix3.add = function (left, right, result) {
  988. //>>includeStart('debug', pragmas.debug);
  989. Check.typeOf.object("left", left);
  990. Check.typeOf.object("right", right);
  991. Check.typeOf.object("result", result);
  992. //>>includeEnd('debug');
  993. result[0] = left[0] + right[0];
  994. result[1] = left[1] + right[1];
  995. result[2] = left[2] + right[2];
  996. result[3] = left[3] + right[3];
  997. result[4] = left[4] + right[4];
  998. result[5] = left[5] + right[5];
  999. result[6] = left[6] + right[6];
  1000. result[7] = left[7] + right[7];
  1001. result[8] = left[8] + right[8];
  1002. return result;
  1003. };
  1004. /**
  1005. * Computes the difference of two matrices.
  1006. *
  1007. * @param {Matrix3} left The first matrix.
  1008. * @param {Matrix3} right The second matrix.
  1009. * @param {Matrix3} result The object onto which to store the result.
  1010. * @returns {Matrix3} The modified result parameter.
  1011. */
  1012. Matrix3.subtract = function (left, right, result) {
  1013. //>>includeStart('debug', pragmas.debug);
  1014. Check.typeOf.object("left", left);
  1015. Check.typeOf.object("right", right);
  1016. Check.typeOf.object("result", result);
  1017. //>>includeEnd('debug');
  1018. result[0] = left[0] - right[0];
  1019. result[1] = left[1] - right[1];
  1020. result[2] = left[2] - right[2];
  1021. result[3] = left[3] - right[3];
  1022. result[4] = left[4] - right[4];
  1023. result[5] = left[5] - right[5];
  1024. result[6] = left[6] - right[6];
  1025. result[7] = left[7] - right[7];
  1026. result[8] = left[8] - right[8];
  1027. return result;
  1028. };
  1029. /**
  1030. * Computes the product of a matrix and a column vector.
  1031. *
  1032. * @param {Matrix3} matrix The matrix.
  1033. * @param {Cartesian3} cartesian The column.
  1034. * @param {Cartesian3} result The object onto which to store the result.
  1035. * @returns {Cartesian3} The modified result parameter.
  1036. */
  1037. Matrix3.multiplyByVector = function (matrix, cartesian, result) {
  1038. //>>includeStart('debug', pragmas.debug);
  1039. Check.typeOf.object("matrix", matrix);
  1040. Check.typeOf.object("cartesian", cartesian);
  1041. Check.typeOf.object("result", result);
  1042. //>>includeEnd('debug');
  1043. const vX = cartesian.x;
  1044. const vY = cartesian.y;
  1045. const vZ = cartesian.z;
  1046. const x = matrix[0] * vX + matrix[3] * vY + matrix[6] * vZ;
  1047. const y = matrix[1] * vX + matrix[4] * vY + matrix[7] * vZ;
  1048. const z = matrix[2] * vX + matrix[5] * vY + matrix[8] * vZ;
  1049. result.x = x;
  1050. result.y = y;
  1051. result.z = z;
  1052. return result;
  1053. };
  1054. /**
  1055. * Computes the product of a matrix and a scalar.
  1056. *
  1057. * @param {Matrix3} matrix The matrix.
  1058. * @param {Number} scalar The number to multiply by.
  1059. * @param {Matrix3} result The object onto which to store the result.
  1060. * @returns {Matrix3} The modified result parameter.
  1061. */
  1062. Matrix3.multiplyByScalar = function (matrix, scalar, result) {
  1063. //>>includeStart('debug', pragmas.debug);
  1064. Check.typeOf.object("matrix", matrix);
  1065. Check.typeOf.number("scalar", scalar);
  1066. Check.typeOf.object("result", result);
  1067. //>>includeEnd('debug');
  1068. result[0] = matrix[0] * scalar;
  1069. result[1] = matrix[1] * scalar;
  1070. result[2] = matrix[2] * scalar;
  1071. result[3] = matrix[3] * scalar;
  1072. result[4] = matrix[4] * scalar;
  1073. result[5] = matrix[5] * scalar;
  1074. result[6] = matrix[6] * scalar;
  1075. result[7] = matrix[7] * scalar;
  1076. result[8] = matrix[8] * scalar;
  1077. return result;
  1078. };
  1079. /**
  1080. * Computes the product of a matrix times a (non-uniform) scale, as if the scale were a scale matrix.
  1081. *
  1082. * @param {Matrix3} matrix The matrix on the left-hand side.
  1083. * @param {Number} scale The non-uniform scale on the right-hand side.
  1084. * @param {Matrix3} result The object onto which to store the result.
  1085. * @returns {Matrix3} The modified result parameter.
  1086. *
  1087. *
  1088. * @example
  1089. * // Instead of Cesium.Matrix3.multiply(m, Cesium.Matrix3.fromScale(scale), m);
  1090. * Cesium.Matrix3.multiplyByScale(m, scale, m);
  1091. *
  1092. * @see Matrix3.multiplyByUniformScale
  1093. * @see Matrix3.fromScale
  1094. * @see Matrix3.fromUniformScale
  1095. * @see Matrix3.setScale
  1096. * @see Matrix3.setUniformScale
  1097. * @see Matrix3.getScale
  1098. */
  1099. Matrix3.multiplyByScale = function (matrix, scale, result) {
  1100. //>>includeStart('debug', pragmas.debug);
  1101. Check.typeOf.object("matrix", matrix);
  1102. Check.typeOf.object("scale", scale);
  1103. Check.typeOf.object("result", result);
  1104. //>>includeEnd('debug');
  1105. result[0] = matrix[0] * scale.x;
  1106. result[1] = matrix[1] * scale.x;
  1107. result[2] = matrix[2] * scale.x;
  1108. result[3] = matrix[3] * scale.y;
  1109. result[4] = matrix[4] * scale.y;
  1110. result[5] = matrix[5] * scale.y;
  1111. result[6] = matrix[6] * scale.z;
  1112. result[7] = matrix[7] * scale.z;
  1113. result[8] = matrix[8] * scale.z;
  1114. return result;
  1115. };
  1116. /**
  1117. * Computes the product of a matrix times a uniform scale, as if the scale were a scale matrix.
  1118. *
  1119. * @param {Matrix3} matrix The matrix on the left-hand side.
  1120. * @param {Number} scale The uniform scale on the right-hand side.
  1121. * @param {Matrix3} result The object onto which to store the result.
  1122. * @returns {Matrix3} The modified result parameter.
  1123. *
  1124. * @example
  1125. * // Instead of Cesium.Matrix3.multiply(m, Cesium.Matrix3.fromUniformScale(scale), m);
  1126. * Cesium.Matrix3.multiplyByUniformScale(m, scale, m);
  1127. *
  1128. * @see Matrix3.multiplyByScale
  1129. * @see Matrix3.fromScale
  1130. * @see Matrix3.fromUniformScale
  1131. * @see Matrix3.setScale
  1132. * @see Matrix3.setUniformScale
  1133. * @see Matrix3.getScale
  1134. */
  1135. Matrix3.multiplyByUniformScale = function (matrix, scale, result) {
  1136. //>>includeStart('debug', pragmas.debug);
  1137. Check.typeOf.object("matrix", matrix);
  1138. Check.typeOf.number("scale", scale);
  1139. Check.typeOf.object("result", result);
  1140. //>>includeEnd('debug');
  1141. result[0] = matrix[0] * scale;
  1142. result[1] = matrix[1] * scale;
  1143. result[2] = matrix[2] * scale;
  1144. result[3] = matrix[3] * scale;
  1145. result[4] = matrix[4] * scale;
  1146. result[5] = matrix[5] * scale;
  1147. result[6] = matrix[6] * scale;
  1148. result[7] = matrix[7] * scale;
  1149. result[8] = matrix[8] * scale;
  1150. return result;
  1151. };
  1152. /**
  1153. * Creates a negated copy of the provided matrix.
  1154. *
  1155. * @param {Matrix3} matrix The matrix to negate.
  1156. * @param {Matrix3} result The object onto which to store the result.
  1157. * @returns {Matrix3} The modified result parameter.
  1158. */
  1159. Matrix3.negate = function (matrix, result) {
  1160. //>>includeStart('debug', pragmas.debug);
  1161. Check.typeOf.object("matrix", matrix);
  1162. Check.typeOf.object("result", result);
  1163. //>>includeEnd('debug');
  1164. result[0] = -matrix[0];
  1165. result[1] = -matrix[1];
  1166. result[2] = -matrix[2];
  1167. result[3] = -matrix[3];
  1168. result[4] = -matrix[4];
  1169. result[5] = -matrix[5];
  1170. result[6] = -matrix[6];
  1171. result[7] = -matrix[7];
  1172. result[8] = -matrix[8];
  1173. return result;
  1174. };
  1175. /**
  1176. * Computes the transpose of the provided matrix.
  1177. *
  1178. * @param {Matrix3} matrix The matrix to transpose.
  1179. * @param {Matrix3} result The object onto which to store the result.
  1180. * @returns {Matrix3} The modified result parameter.
  1181. */
  1182. Matrix3.transpose = function (matrix, result) {
  1183. //>>includeStart('debug', pragmas.debug);
  1184. Check.typeOf.object("matrix", matrix);
  1185. Check.typeOf.object("result", result);
  1186. //>>includeEnd('debug');
  1187. const column0Row0 = matrix[0];
  1188. const column0Row1 = matrix[3];
  1189. const column0Row2 = matrix[6];
  1190. const column1Row0 = matrix[1];
  1191. const column1Row1 = matrix[4];
  1192. const column1Row2 = matrix[7];
  1193. const column2Row0 = matrix[2];
  1194. const column2Row1 = matrix[5];
  1195. const column2Row2 = matrix[8];
  1196. result[0] = column0Row0;
  1197. result[1] = column0Row1;
  1198. result[2] = column0Row2;
  1199. result[3] = column1Row0;
  1200. result[4] = column1Row1;
  1201. result[5] = column1Row2;
  1202. result[6] = column2Row0;
  1203. result[7] = column2Row1;
  1204. result[8] = column2Row2;
  1205. return result;
  1206. };
  1207. function computeFrobeniusNorm(matrix) {
  1208. let norm = 0.0;
  1209. for (let i = 0; i < 9; ++i) {
  1210. const temp = matrix[i];
  1211. norm += temp * temp;
  1212. }
  1213. return Math.sqrt(norm);
  1214. }
  1215. const rowVal = [1, 0, 0];
  1216. const colVal = [2, 2, 1];
  1217. function offDiagonalFrobeniusNorm(matrix) {
  1218. // Computes the "off-diagonal" Frobenius norm.
  1219. // Assumes matrix is symmetric.
  1220. let norm = 0.0;
  1221. for (let i = 0; i < 3; ++i) {
  1222. const temp = matrix[Matrix3.getElementIndex(colVal[i], rowVal[i])];
  1223. norm += 2.0 * temp * temp;
  1224. }
  1225. return Math.sqrt(norm);
  1226. }
  1227. function shurDecomposition(matrix, result) {
  1228. // This routine was created based upon Matrix Computations, 3rd ed., by Golub and Van Loan,
  1229. // section 8.4.2 The 2by2 Symmetric Schur Decomposition.
  1230. //
  1231. // The routine takes a matrix, which is assumed to be symmetric, and
  1232. // finds the largest off-diagonal term, and then creates
  1233. // a matrix (result) which can be used to help reduce it
  1234. const tolerance = CesiumMath.EPSILON15;
  1235. let maxDiagonal = 0.0;
  1236. let rotAxis = 1;
  1237. // find pivot (rotAxis) based on max diagonal of matrix
  1238. for (let i = 0; i < 3; ++i) {
  1239. const temp = Math.abs(
  1240. matrix[Matrix3.getElementIndex(colVal[i], rowVal[i])]
  1241. );
  1242. if (temp > maxDiagonal) {
  1243. rotAxis = i;
  1244. maxDiagonal = temp;
  1245. }
  1246. }
  1247. let c = 1.0;
  1248. let s = 0.0;
  1249. const p = rowVal[rotAxis];
  1250. const q = colVal[rotAxis];
  1251. if (Math.abs(matrix[Matrix3.getElementIndex(q, p)]) > tolerance) {
  1252. const qq = matrix[Matrix3.getElementIndex(q, q)];
  1253. const pp = matrix[Matrix3.getElementIndex(p, p)];
  1254. const qp = matrix[Matrix3.getElementIndex(q, p)];
  1255. const tau = (qq - pp) / 2.0 / qp;
  1256. let t;
  1257. if (tau < 0.0) {
  1258. t = -1.0 / (-tau + Math.sqrt(1.0 + tau * tau));
  1259. } else {
  1260. t = 1.0 / (tau + Math.sqrt(1.0 + tau * tau));
  1261. }
  1262. c = 1.0 / Math.sqrt(1.0 + t * t);
  1263. s = t * c;
  1264. }
  1265. result = Matrix3.clone(Matrix3.IDENTITY, result);
  1266. result[Matrix3.getElementIndex(p, p)] = result[
  1267. Matrix3.getElementIndex(q, q)
  1268. ] = c;
  1269. result[Matrix3.getElementIndex(q, p)] = s;
  1270. result[Matrix3.getElementIndex(p, q)] = -s;
  1271. return result;
  1272. }
  1273. const jMatrix = new Matrix3();
  1274. const jMatrixTranspose = new Matrix3();
  1275. /**
  1276. * Computes the eigenvectors and eigenvalues of a symmetric matrix.
  1277. * <p>
  1278. * Returns a diagonal matrix and unitary matrix such that:
  1279. * <code>matrix = unitary matrix * diagonal matrix * transpose(unitary matrix)</code>
  1280. * </p>
  1281. * <p>
  1282. * The values along the diagonal of the diagonal matrix are the eigenvalues. The columns
  1283. * of the unitary matrix are the corresponding eigenvectors.
  1284. * </p>
  1285. *
  1286. * @param {Matrix3} matrix The matrix to decompose into diagonal and unitary matrix. Expected to be symmetric.
  1287. * @param {Object} [result] An object with unitary and diagonal properties which are matrices onto which to store the result.
  1288. * @returns {Object} An object with unitary and diagonal properties which are the unitary and diagonal matrices, respectively.
  1289. *
  1290. * @example
  1291. * const a = //... symetric matrix
  1292. * const result = {
  1293. * unitary : new Cesium.Matrix3(),
  1294. * diagonal : new Cesium.Matrix3()
  1295. * };
  1296. * Cesium.Matrix3.computeEigenDecomposition(a, result);
  1297. *
  1298. * const unitaryTranspose = Cesium.Matrix3.transpose(result.unitary, new Cesium.Matrix3());
  1299. * const b = Cesium.Matrix3.multiply(result.unitary, result.diagonal, new Cesium.Matrix3());
  1300. * Cesium.Matrix3.multiply(b, unitaryTranspose, b); // b is now equal to a
  1301. *
  1302. * const lambda = Cesium.Matrix3.getColumn(result.diagonal, 0, new Cesium.Cartesian3()).x; // first eigenvalue
  1303. * const v = Cesium.Matrix3.getColumn(result.unitary, 0, new Cesium.Cartesian3()); // first eigenvector
  1304. * const c = Cesium.Cartesian3.multiplyByScalar(v, lambda, new Cesium.Cartesian3()); // equal to Cesium.Matrix3.multiplyByVector(a, v)
  1305. */
  1306. Matrix3.computeEigenDecomposition = function (matrix, result) {
  1307. //>>includeStart('debug', pragmas.debug);
  1308. Check.typeOf.object("matrix", matrix);
  1309. //>>includeEnd('debug');
  1310. // This routine was created based upon Matrix Computations, 3rd ed., by Golub and Van Loan,
  1311. // section 8.4.3 The Classical Jacobi Algorithm
  1312. const tolerance = CesiumMath.EPSILON20;
  1313. const maxSweeps = 10;
  1314. let count = 0;
  1315. let sweep = 0;
  1316. if (!defined(result)) {
  1317. result = {};
  1318. }
  1319. const unitaryMatrix = (result.unitary = Matrix3.clone(
  1320. Matrix3.IDENTITY,
  1321. result.unitary
  1322. ));
  1323. const diagMatrix = (result.diagonal = Matrix3.clone(matrix, result.diagonal));
  1324. const epsilon = tolerance * computeFrobeniusNorm(diagMatrix);
  1325. while (sweep < maxSweeps && offDiagonalFrobeniusNorm(diagMatrix) > epsilon) {
  1326. shurDecomposition(diagMatrix, jMatrix);
  1327. Matrix3.transpose(jMatrix, jMatrixTranspose);
  1328. Matrix3.multiply(diagMatrix, jMatrix, diagMatrix);
  1329. Matrix3.multiply(jMatrixTranspose, diagMatrix, diagMatrix);
  1330. Matrix3.multiply(unitaryMatrix, jMatrix, unitaryMatrix);
  1331. if (++count > 2) {
  1332. ++sweep;
  1333. count = 0;
  1334. }
  1335. }
  1336. return result;
  1337. };
  1338. /**
  1339. * Computes a matrix, which contains the absolute (unsigned) values of the provided matrix's elements.
  1340. *
  1341. * @param {Matrix3} matrix The matrix with signed elements.
  1342. * @param {Matrix3} result The object onto which to store the result.
  1343. * @returns {Matrix3} The modified result parameter.
  1344. */
  1345. Matrix3.abs = function (matrix, result) {
  1346. //>>includeStart('debug', pragmas.debug);
  1347. Check.typeOf.object("matrix", matrix);
  1348. Check.typeOf.object("result", result);
  1349. //>>includeEnd('debug');
  1350. result[0] = Math.abs(matrix[0]);
  1351. result[1] = Math.abs(matrix[1]);
  1352. result[2] = Math.abs(matrix[2]);
  1353. result[3] = Math.abs(matrix[3]);
  1354. result[4] = Math.abs(matrix[4]);
  1355. result[5] = Math.abs(matrix[5]);
  1356. result[6] = Math.abs(matrix[6]);
  1357. result[7] = Math.abs(matrix[7]);
  1358. result[8] = Math.abs(matrix[8]);
  1359. return result;
  1360. };
  1361. /**
  1362. * Computes the determinant of the provided matrix.
  1363. *
  1364. * @param {Matrix3} matrix The matrix to use.
  1365. * @returns {Number} The value of the determinant of the matrix.
  1366. */
  1367. Matrix3.determinant = function (matrix) {
  1368. //>>includeStart('debug', pragmas.debug);
  1369. Check.typeOf.object("matrix", matrix);
  1370. //>>includeEnd('debug');
  1371. const m11 = matrix[0];
  1372. const m21 = matrix[3];
  1373. const m31 = matrix[6];
  1374. const m12 = matrix[1];
  1375. const m22 = matrix[4];
  1376. const m32 = matrix[7];
  1377. const m13 = matrix[2];
  1378. const m23 = matrix[5];
  1379. const m33 = matrix[8];
  1380. return (
  1381. m11 * (m22 * m33 - m23 * m32) +
  1382. m12 * (m23 * m31 - m21 * m33) +
  1383. m13 * (m21 * m32 - m22 * m31)
  1384. );
  1385. };
  1386. /**
  1387. * Computes the inverse of the provided matrix.
  1388. *
  1389. * @param {Matrix3} matrix The matrix to invert.
  1390. * @param {Matrix3} result The object onto which to store the result.
  1391. * @returns {Matrix3} The modified result parameter.
  1392. *
  1393. * @exception {DeveloperError} matrix is not invertible.
  1394. */
  1395. Matrix3.inverse = function (matrix, result) {
  1396. //>>includeStart('debug', pragmas.debug);
  1397. Check.typeOf.object("matrix", matrix);
  1398. Check.typeOf.object("result", result);
  1399. //>>includeEnd('debug');
  1400. const m11 = matrix[0];
  1401. const m21 = matrix[1];
  1402. const m31 = matrix[2];
  1403. const m12 = matrix[3];
  1404. const m22 = matrix[4];
  1405. const m32 = matrix[5];
  1406. const m13 = matrix[6];
  1407. const m23 = matrix[7];
  1408. const m33 = matrix[8];
  1409. const determinant = Matrix3.determinant(matrix);
  1410. //>>includeStart('debug', pragmas.debug);
  1411. if (Math.abs(determinant) <= CesiumMath.EPSILON15) {
  1412. throw new DeveloperError("matrix is not invertible");
  1413. }
  1414. //>>includeEnd('debug');
  1415. result[0] = m22 * m33 - m23 * m32;
  1416. result[1] = m23 * m31 - m21 * m33;
  1417. result[2] = m21 * m32 - m22 * m31;
  1418. result[3] = m13 * m32 - m12 * m33;
  1419. result[4] = m11 * m33 - m13 * m31;
  1420. result[5] = m12 * m31 - m11 * m32;
  1421. result[6] = m12 * m23 - m13 * m22;
  1422. result[7] = m13 * m21 - m11 * m23;
  1423. result[8] = m11 * m22 - m12 * m21;
  1424. const scale = 1.0 / determinant;
  1425. return Matrix3.multiplyByScalar(result, scale, result);
  1426. };
  1427. const scratchTransposeMatrix = new Matrix3();
  1428. /**
  1429. * Computes the inverse transpose of a matrix.
  1430. *
  1431. * @param {Matrix3} matrix The matrix to transpose and invert.
  1432. * @param {Matrix3} result The object onto which to store the result.
  1433. * @returns {Matrix3} The modified result parameter.
  1434. */
  1435. Matrix3.inverseTranspose = function (matrix, result) {
  1436. //>>includeStart('debug', pragmas.debug);
  1437. Check.typeOf.object("matrix", matrix);
  1438. Check.typeOf.object("result", result);
  1439. //>>includeEnd('debug');
  1440. return Matrix3.inverse(
  1441. Matrix3.transpose(matrix, scratchTransposeMatrix),
  1442. result
  1443. );
  1444. };
  1445. /**
  1446. * Compares the provided matrices componentwise and returns
  1447. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1448. *
  1449. * @param {Matrix3} [left] The first matrix.
  1450. * @param {Matrix3} [right] The second matrix.
  1451. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  1452. */
  1453. Matrix3.equals = function (left, right) {
  1454. return (
  1455. left === right ||
  1456. (defined(left) &&
  1457. defined(right) &&
  1458. left[0] === right[0] &&
  1459. left[1] === right[1] &&
  1460. left[2] === right[2] &&
  1461. left[3] === right[3] &&
  1462. left[4] === right[4] &&
  1463. left[5] === right[5] &&
  1464. left[6] === right[6] &&
  1465. left[7] === right[7] &&
  1466. left[8] === right[8])
  1467. );
  1468. };
  1469. /**
  1470. * Compares the provided matrices componentwise and returns
  1471. * <code>true</code> if they are within the provided epsilon,
  1472. * <code>false</code> otherwise.
  1473. *
  1474. * @param {Matrix3} [left] The first matrix.
  1475. * @param {Matrix3} [right] The second matrix.
  1476. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  1477. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  1478. */
  1479. Matrix3.equalsEpsilon = function (left, right, epsilon) {
  1480. epsilon = defaultValue(epsilon, 0);
  1481. return (
  1482. left === right ||
  1483. (defined(left) &&
  1484. defined(right) &&
  1485. Math.abs(left[0] - right[0]) <= epsilon &&
  1486. Math.abs(left[1] - right[1]) <= epsilon &&
  1487. Math.abs(left[2] - right[2]) <= epsilon &&
  1488. Math.abs(left[3] - right[3]) <= epsilon &&
  1489. Math.abs(left[4] - right[4]) <= epsilon &&
  1490. Math.abs(left[5] - right[5]) <= epsilon &&
  1491. Math.abs(left[6] - right[6]) <= epsilon &&
  1492. Math.abs(left[7] - right[7]) <= epsilon &&
  1493. Math.abs(left[8] - right[8]) <= epsilon)
  1494. );
  1495. };
  1496. /**
  1497. * An immutable Matrix3 instance initialized to the identity matrix.
  1498. *
  1499. * @type {Matrix3}
  1500. * @constant
  1501. */
  1502. Matrix3.IDENTITY = Object.freeze(
  1503. new Matrix3(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0)
  1504. );
  1505. /**
  1506. * An immutable Matrix3 instance initialized to the zero matrix.
  1507. *
  1508. * @type {Matrix3}
  1509. * @constant
  1510. */
  1511. Matrix3.ZERO = Object.freeze(
  1512. new Matrix3(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
  1513. );
  1514. /**
  1515. * The index into Matrix3 for column 0, row 0.
  1516. *
  1517. * @type {Number}
  1518. * @constant
  1519. */
  1520. Matrix3.COLUMN0ROW0 = 0;
  1521. /**
  1522. * The index into Matrix3 for column 0, row 1.
  1523. *
  1524. * @type {Number}
  1525. * @constant
  1526. */
  1527. Matrix3.COLUMN0ROW1 = 1;
  1528. /**
  1529. * The index into Matrix3 for column 0, row 2.
  1530. *
  1531. * @type {Number}
  1532. * @constant
  1533. */
  1534. Matrix3.COLUMN0ROW2 = 2;
  1535. /**
  1536. * The index into Matrix3 for column 1, row 0.
  1537. *
  1538. * @type {Number}
  1539. * @constant
  1540. */
  1541. Matrix3.COLUMN1ROW0 = 3;
  1542. /**
  1543. * The index into Matrix3 for column 1, row 1.
  1544. *
  1545. * @type {Number}
  1546. * @constant
  1547. */
  1548. Matrix3.COLUMN1ROW1 = 4;
  1549. /**
  1550. * The index into Matrix3 for column 1, row 2.
  1551. *
  1552. * @type {Number}
  1553. * @constant
  1554. */
  1555. Matrix3.COLUMN1ROW2 = 5;
  1556. /**
  1557. * The index into Matrix3 for column 2, row 0.
  1558. *
  1559. * @type {Number}
  1560. * @constant
  1561. */
  1562. Matrix3.COLUMN2ROW0 = 6;
  1563. /**
  1564. * The index into Matrix3 for column 2, row 1.
  1565. *
  1566. * @type {Number}
  1567. * @constant
  1568. */
  1569. Matrix3.COLUMN2ROW1 = 7;
  1570. /**
  1571. * The index into Matrix3 for column 2, row 2.
  1572. *
  1573. * @type {Number}
  1574. * @constant
  1575. */
  1576. Matrix3.COLUMN2ROW2 = 8;
  1577. Object.defineProperties(Matrix3.prototype, {
  1578. /**
  1579. * Gets the number of items in the collection.
  1580. * @memberof Matrix3.prototype
  1581. *
  1582. * @type {Number}
  1583. */
  1584. length: {
  1585. get: function () {
  1586. return Matrix3.packedLength;
  1587. },
  1588. },
  1589. });
  1590. /**
  1591. * Duplicates the provided Matrix3 instance.
  1592. *
  1593. * @param {Matrix3} [result] The object onto which to store the result.
  1594. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided.
  1595. */
  1596. Matrix3.prototype.clone = function (result) {
  1597. return Matrix3.clone(this, result);
  1598. };
  1599. /**
  1600. * Compares this matrix to the provided matrix componentwise and returns
  1601. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1602. *
  1603. * @param {Matrix3} [right] The right hand side matrix.
  1604. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  1605. */
  1606. Matrix3.prototype.equals = function (right) {
  1607. return Matrix3.equals(this, right);
  1608. };
  1609. /**
  1610. * @private
  1611. */
  1612. Matrix3.equalsArray = function (matrix, array, offset) {
  1613. return (
  1614. matrix[0] === array[offset] &&
  1615. matrix[1] === array[offset + 1] &&
  1616. matrix[2] === array[offset + 2] &&
  1617. matrix[3] === array[offset + 3] &&
  1618. matrix[4] === array[offset + 4] &&
  1619. matrix[5] === array[offset + 5] &&
  1620. matrix[6] === array[offset + 6] &&
  1621. matrix[7] === array[offset + 7] &&
  1622. matrix[8] === array[offset + 8]
  1623. );
  1624. };
  1625. /**
  1626. * Compares this matrix to the provided matrix componentwise and returns
  1627. * <code>true</code> if they are within the provided epsilon,
  1628. * <code>false</code> otherwise.
  1629. *
  1630. * @param {Matrix3} [right] The right hand side matrix.
  1631. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  1632. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  1633. */
  1634. Matrix3.prototype.equalsEpsilon = function (right, epsilon) {
  1635. return Matrix3.equalsEpsilon(this, right, epsilon);
  1636. };
  1637. /**
  1638. * Creates a string representing this Matrix with each row being
  1639. * on a separate line and in the format '(column0, column1, column2)'.
  1640. *
  1641. * @returns {String} A string representing the provided Matrix with each row being on a separate line and in the format '(column0, column1, column2)'.
  1642. */
  1643. Matrix3.prototype.toString = function () {
  1644. return (
  1645. `(${this[0]}, ${this[3]}, ${this[6]})\n` +
  1646. `(${this[1]}, ${this[4]}, ${this[7]})\n` +
  1647. `(${this[2]}, ${this[5]}, ${this[8]})`
  1648. );
  1649. };
  1650. export default Matrix3;