1+ "use strict" ;
2+
3+ Object . defineProperty ( exports , "__esModule" , {
4+ value : true
5+ } ) ;
6+ exports [ "default" ] = void 0 ;
7+
8+ function _classCallCheck ( instance , Constructor ) { if ( ! ( instance instanceof Constructor ) ) { throw new TypeError ( "Cannot call a class as a function" ) ; } }
9+
10+ function _defineProperties ( target , props ) { for ( var i = 0 ; i < props . length ; i ++ ) { var descriptor = props [ i ] ; descriptor . enumerable = descriptor . enumerable || false ; descriptor . configurable = true ; if ( "value" in descriptor ) descriptor . writable = true ; Object . defineProperty ( target , descriptor . key , descriptor ) ; } }
11+
12+ function _createClass ( Constructor , protoProps , staticProps ) { if ( protoProps ) _defineProperties ( Constructor . prototype , protoProps ) ; if ( staticProps ) _defineProperties ( Constructor , staticProps ) ; return Constructor ; }
13+
14+ var identity = [ 1 , 0 , 0 , 1 ] ;
15+
16+ var identToIndex = function identToIndex ( v ) {
17+ return [ 'a11' , 'a12' , 'a21' , 'a22' ] . indexOf ( v ) ;
18+ } ;
19+
20+ var orDefault = function orDefault ( v , ident ) {
21+ return isNaN ( v ) ? identity [ identToIndex ( ident ) ] : Number ( v ) ;
22+ } ;
23+
24+ var Mat2 =
25+ /*#__PURE__*/
26+ function ( ) {
27+ function Mat2 ( a11 , a12 , a21 , a22 ) {
28+ _classCallCheck ( this , Mat2 ) ;
29+
30+ this . reset ( a11 , a12 , a21 , a22 ) ;
31+ }
32+
33+ _createClass ( Mat2 , [ {
34+ key : "reset" ,
35+ value : function reset ( a11 , a12 , a21 , a22 ) {
36+ if ( a11 instanceof Array && a11 . length >= 4 ) {
37+ this . a11 = orDefault ( a11 [ 0 ] , 'a11' ) ;
38+ this . a12 = orDefault ( a11 [ 1 ] , 'a12' ) ;
39+ this . a21 = orDefault ( a11 [ 2 ] , 'a21' ) ;
40+ this . a22 = orDefault ( a11 [ 3 ] , 'a22' ) ;
41+ } else {
42+ this . a11 = orDefault ( a11 , 'a11' ) ;
43+ this . a12 = orDefault ( a12 , 'a12' ) ;
44+ this . a21 = orDefault ( a21 , 'a21' ) ;
45+ this . a22 = orDefault ( a22 , 'a22' ) ;
46+ }
47+ }
48+ } , {
49+ key : "resetToMat2" ,
50+ value : function resetToMat2 ( m ) {
51+ this . a11 = m . a11 ;
52+ this . a12 = m . a12 ;
53+ this . a21 = m . a21 ;
54+ this . a22 = m . a22 ;
55+ }
56+ } , {
57+ key : "clone" ,
58+ value : function clone ( ) {
59+ return new Mat2 ( this . a11 , this . a12 , this . a21 , this . a22 ) ;
60+ }
61+ } , {
62+ key : "transpose" ,
63+ value : function transpose ( ) {
64+ var a12 = this . a12 ;
65+ this . a12 = this . a21 ;
66+ this . a21 = a12 ;
67+ return this ;
68+ }
69+ } , {
70+ key : "transposeNew" ,
71+ value : function transposeNew ( ) {
72+ return this . clone ( ) . transpose ( m ) ;
73+ }
74+ } , {
75+ key : "add" ,
76+ value : function add ( m ) {
77+ if ( m instanceof Mat2 ) {
78+ this . a11 += m . a11 ;
79+ this . a12 += m . a12 ;
80+ this . a21 += m . a21 ;
81+ this . a22 += m . a22 ;
82+ }
83+
84+ return this ;
85+ }
86+ } , {
87+ key : "addNew" ,
88+ value : function addNew ( m ) {
89+ return this . clone ( ) . add ( m ) ;
90+ } // @TODO : We might want to generalise this and allow any sort of matrix on these operations
91+
92+ } , {
93+ key : "subtract" ,
94+ value : function subtract ( m ) {
95+ if ( m instanceof Mat2 ) {
96+ this . a11 -= m . a11 ;
97+ this . a12 -= m . a12 ;
98+ this . a21 -= m . a21 ;
99+ this . a22 -= m . a22 ;
100+ }
101+
102+ return this ;
103+ }
104+ } , {
105+ key : "subtractNew" ,
106+ value : function subtractNew ( m ) {
107+ return this . clone ( ) . subtract ( m ) ;
108+ }
109+ } , {
110+ key : "multiply" ,
111+ value : function multiply ( m ) {
112+ if ( m instanceof Mat2 ) {
113+ var o = this . clone ( ) ;
114+ this . a11 = o . a11 * m . a11 + o . a21 * m . a12 ;
115+ this . a12 = o . a12 * m . a11 + o . a22 * m . a12 ;
116+ this . a21 = o . a11 * m . a21 + o . a21 * m . a22 ;
117+ this . a22 = o . a12 * m . a21 + o . a22 * m . a22 ;
118+ }
119+
120+ return this ;
121+ }
122+ } , {
123+ key : "multiplyNew" ,
124+ value : function multiplyNew ( m ) {
125+ return this . clone ( ) . multiply ( m ) ;
126+ }
127+ } , {
128+ key : "multiplyScalar" ,
129+ value : function multiplyScalar ( s ) {
130+ this . a11 *= s ;
131+ this . a12 *= s ;
132+ this . a21 *= s ;
133+ this . a22 *= s ;
134+ return this ;
135+ }
136+ } , {
137+ key : "multiplyScalarNew" ,
138+ value : function multiplyScalarNew ( s ) {
139+ return this . clone ( ) . multiplyScalar ( s ) ;
140+ }
141+ } , {
142+ key : "scale" ,
143+ value : function scale ( s ) {
144+ return this . multiplyScalar ( s ) ;
145+ }
146+ } , {
147+ key : "scaleNew" ,
148+ value : function scaleNew ( s ) {
149+ return this . multiplyScalarNew ( s ) ;
150+ }
151+ } , {
152+ key : "scaleByVec2" ,
153+ value : function scaleByVec2 ( v ) {
154+ if ( v . array ) v = v . array ; // This just transforms a provided vector into to an array.
155+
156+ if ( v instanceof Array ) {
157+ this . a11 *= v [ 0 ] ;
158+ this . a12 *= v [ 0 ] ;
159+ this . a21 *= v [ 1 ] ;
160+ this . a22 *= v [ 1 ] ;
161+ }
162+
163+ return this ;
164+ }
165+ } , {
166+ key : "scaleByVec2New" ,
167+ value : function scaleByVec2New ( v ) {
168+ return this . clone ( ) . scaleByVec2 ( v ) ;
169+ }
170+ } , {
171+ key : "rotate" ,
172+ value : function rotate ( r ) {
173+ var o = this . clone ( ) ;
174+ var s = Math . sin ( r ) ;
175+ var c = Math . cos ( r ) ;
176+ this . a11 = o . a11 * c + o . a21 * s ;
177+ this . a12 = o . a12 * c + o . a22 * s ;
178+ this . a21 = o . a11 * - s + o . a21 * c ;
179+ this . a22 = o . a12 * - s + o . a22 * c ;
180+ return this ;
181+ }
182+ } , {
183+ key : "rotateNew" ,
184+ value : function rotateNew ( r ) {
185+ return this . clone ( ) . rotate ( r ) ;
186+ }
187+ } , {
188+ key : "invert" ,
189+ value : function invert ( ) {
190+ var c = this . clone ( ) ;
191+ var det = this . determinant ; // If we don't have a determinant this function should fail silently and just return the unmodified array
192+
193+ if ( det ) {
194+ det = 1. / det ;
195+ this . a11 = c . a22 * det ;
196+ this . a12 = - c . a12 * det ;
197+ this . a21 = - c . a21 * det ;
198+ this . a22 = c . a11 * det ;
199+ }
200+
201+ return this ;
202+ }
203+ } , {
204+ key : "invertNew" ,
205+ value : function invertNew ( ) {
206+ return this . clone ( ) . invert ( ) ;
207+ }
208+ /**
209+ * Calculates the adjugate of a mat2
210+ *
211+ * @param {mat2 } out the receiving matrix
212+ * @param {mat2 } a the source matrix
213+ * @returns {mat2 } out
214+ */
215+
216+ } , {
217+ key : "adjoint" ,
218+ value : function adjoint ( ) {
219+ var a11 = this . a11 ;
220+ this . a11 = this . a22 ;
221+ this . a12 = - this . a12 ;
222+ this . a21 = - this . a21 ;
223+ this . a22 = a11 ;
224+ return this ;
225+ }
226+ } , {
227+ key : "adjointNew" ,
228+ value : function adjointNew ( ) {
229+ this . clone ( ) . adjoint ( ) ;
230+ }
231+ } , {
232+ key : "toString" ,
233+ value : function toString ( ) {
234+ return "\n " . concat ( this . a11 , ", " ) . concat ( this . a12 , ",\n " ) . concat ( this . a21 , ", " ) . concat ( this . a22 , "\n " ) ;
235+ }
236+ /**
237+ * Getters and setters
238+ */
239+
240+ /**
241+ * (getter/setter) The a11 value of the matrix.
242+ *
243+ * @type {number }
244+ * @default 0
245+ */
246+
247+ } , {
248+ key : "a11" ,
249+ set : function set ( v ) {
250+ if ( typeof v == 'number' ) {
251+ this . _a11 = v ;
252+ } else {
253+ throw new TypeError ( 'a11 should be a number' ) ;
254+ }
255+ } ,
256+ get : function get ( ) {
257+ return this . _a11 || 0 ;
258+ }
259+ /**
260+ * (getter/setter) The a12 value of the matrix.
261+ *
262+ * @type {number }
263+ * @default 0
264+ */
265+
266+ } , {
267+ key : "a12" ,
268+ set : function set ( v ) {
269+ if ( typeof v == 'number' ) {
270+ this . _a12 = v ;
271+ } else {
272+ throw new TypeError ( 'a12 should be a number' ) ;
273+ }
274+ } ,
275+ get : function get ( ) {
276+ return this . _a12 || 0 ;
277+ }
278+ /**
279+ * (getter/setter) The a21 value of the matrix.
280+ *
281+ * @type {number }
282+ * @default 0
283+ */
284+
285+ } , {
286+ key : "a21" ,
287+ set : function set ( v ) {
288+ if ( typeof v == 'number' ) {
289+ this . _a21 = v ;
290+ } else {
291+ throw new TypeError ( 'a21 should be a number' ) ;
292+ }
293+ } ,
294+ get : function get ( ) {
295+ return this . _a21 || 0 ;
296+ }
297+ /**
298+ * (getter/setter) The a22 value of the matrix.
299+ *
300+ * @type {number }
301+ * @default 0
302+ */
303+
304+ } , {
305+ key : "a22" ,
306+ set : function set ( v ) {
307+ if ( typeof v == 'number' ) {
308+ this . _a22 = v ;
309+ } else {
310+ throw new TypeError ( 'a22 should be a number' ) ;
311+ }
312+ } ,
313+ get : function get ( ) {
314+ return this . _a22 || 0 ;
315+ }
316+ } , {
317+ key : "determinant" ,
318+ get : function get ( ) {
319+ return this . a11 * this . a21 - this . a21 * this . a12 ;
320+ }
321+ /**
322+ * (getter) Returns the basic array representation of this matrix.
323+ * @readonly
324+ *
325+ * @type {array }
326+ */
327+
328+ } , {
329+ key : "array" ,
330+ get : function get ( ) {
331+ return [ this . a11 , this . a12 , this . a21 , this . a22 ] ;
332+ }
333+ } ] , [ {
334+ key : "fromAngle" ,
335+ value : function fromAngle ( r ) {
336+ var s = Math . sin ( rad ) ;
337+ var c = Math . cos ( rad ) ;
338+ return new Mat2 ( c , - s , s , c ) ;
339+ }
340+ } , {
341+ key : "fromScalingVec2" ,
342+ value : function fromScalingVec2 ( v ) {
343+ if ( v . array ) v = v . array ; // This just transforms a provided vector into to an array.
344+
345+ if ( v instanceof Array ) {
346+ return new Mat2 ( v [ 0 ] , 0 , 0 , v [ 1 ] ) ;
347+ }
348+ }
349+ } ] ) ;
350+
351+ return Mat2 ;
352+ } ( ) ;
353+
354+ var _default = Mat2 ;
355+ exports [ "default" ] = _default ;
0 commit comments