123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502 |
- // MIT License - Copyright (C) The Mono.Xna Team
- // This file is subject to the terms and conditions defined in
- // file 'LICENSE.txt', which is part of this source code package.
- using System;
- using System.Diagnostics;
- using System.Runtime.Serialization;
- namespace CommonLang.Geometry
- {
- /// <summary>
- /// Represents the right-handed 4x4 floating point matrix, which can store translation, scale and rotation information.
- /// </summary>
-
-
- public struct Matrix : IEquatable<Matrix>
- {
- #region Public Constructors
- /// <summary>
- /// Constructs a matrix.
- /// </summary>
- /// <param name="m11">A first row and first column value.</param>
- /// <param name="m12">A first row and second column value.</param>
- /// <param name="m13">A first row and third column value.</param>
- /// <param name="m14">A first row and fourth column value.</param>
- /// <param name="m21">A second row and first column value.</param>
- /// <param name="m22">A second row and second column value.</param>
- /// <param name="m23">A second row and third column value.</param>
- /// <param name="m24">A second row and fourth column value.</param>
- /// <param name="m31">A third row and first column value.</param>
- /// <param name="m32">A third row and second column value.</param>
- /// <param name="m33">A third row and third column value.</param>
- /// <param name="m34">A third row and fourth column value.</param>
- /// <param name="m41">A fourth row and first column value.</param>
- /// <param name="m42">A fourth row and second column value.</param>
- /// <param name="m43">A fourth row and third column value.</param>
- /// <param name="m44">A fourth row and fourth column value.</param>
- public Matrix(float m11, float m12, float m13, float m14, float m21, float m22, float m23, float m24, float m31,
- float m32, float m33, float m34, float m41, float m42, float m43, float m44)
- {
- this.M11 = m11;
- this.M12 = m12;
- this.M13 = m13;
- this.M14 = m14;
- this.M21 = m21;
- this.M22 = m22;
- this.M23 = m23;
- this.M24 = m24;
- this.M31 = m31;
- this.M32 = m32;
- this.M33 = m33;
- this.M34 = m34;
- this.M41 = m41;
- this.M42 = m42;
- this.M43 = m43;
- this.M44 = m44;
- }
- /// <summary>
- /// Constructs a matrix.
- /// </summary>
- /// <param name="row1">A first row of the created matrix.</param>
- /// <param name="row2">A second row of the created matrix.</param>
- /// <param name="row3">A third row of the created matrix.</param>
- /// <param name="row4">A fourth row of the created matrix.</param>
- public Matrix(Vector4 row1, Vector4 row2, Vector4 row3, Vector4 row4)
- {
- this.M11 = row1.X;
- this.M12 = row1.Y;
- this.M13 = row1.Z;
- this.M14 = row1.W;
- this.M21 = row2.X;
- this.M22 = row2.Y;
- this.M23 = row2.Z;
- this.M24 = row2.W;
- this.M31 = row3.X;
- this.M32 = row3.Y;
- this.M33 = row3.Z;
- this.M34 = row3.W;
- this.M41 = row4.X;
- this.M42 = row4.Y;
- this.M43 = row4.Z;
- this.M44 = row4.W;
- }
- #endregion
- #region Public Fields
- /// <summary>
- /// A first row and first column value.
- /// </summary>
-
- public float M11;
- /// <summary>
- /// A first row and second column value.
- /// </summary>
-
- public float M12;
- /// <summary>
- /// A first row and third column value.
- /// </summary>
-
- public float M13;
- /// <summary>
- /// A first row and fourth column value.
- /// </summary>
-
- public float M14;
- /// <summary>
- /// A second row and first column value.
- /// </summary>
-
- public float M21;
- /// <summary>
- /// A second row and second column value.
- /// </summary>
-
- public float M22;
- /// <summary>
- /// A second row and third column value.
- /// </summary>
-
- public float M23;
- /// <summary>
- /// A second row and fourth column value.
- /// </summary>
-
- public float M24;
- /// <summary>
- /// A third row and first column value.
- /// </summary>
-
- public float M31;
- /// <summary>
- /// A third row and second column value.
- /// </summary>
-
- public float M32;
- /// <summary>
- /// A third row and third column value.
- /// </summary>
-
- public float M33;
- /// <summary>
- /// A third row and fourth column value.
- /// </summary>
-
- public float M34;
- /// <summary>
- /// A fourth row and first column value.
- /// </summary>
-
- public float M41;
- /// <summary>
- /// A fourth row and second column value.
- /// </summary>
-
- public float M42;
- /// <summary>
- /// A fourth row and third column value.
- /// </summary>
-
- public float M43;
- /// <summary>
- /// A fourth row and fourth column value.
- /// </summary>
-
- public float M44;
- #endregion
- #region Indexers
- public float this[int index]
- {
- get
- {
- switch (index)
- {
- case 0: return M11;
- case 1: return M12;
- case 2: return M13;
- case 3: return M14;
- case 4: return M21;
- case 5: return M22;
- case 6: return M23;
- case 7: return M24;
- case 8: return M31;
- case 9: return M32;
- case 10: return M33;
- case 11: return M34;
- case 12: return M41;
- case 13: return M42;
- case 14: return M43;
- case 15: return M44;
- }
- throw new ArgumentOutOfRangeException();
- }
- set
- {
- switch (index)
- {
- case 0: M11 = value; break;
- case 1: M12 = value; break;
- case 2: M13 = value; break;
- case 3: M14 = value; break;
- case 4: M21 = value; break;
- case 5: M22 = value; break;
- case 6: M23 = value; break;
- case 7: M24 = value; break;
- case 8: M31 = value; break;
- case 9: M32 = value; break;
- case 10: M33 = value; break;
- case 11: M34 = value; break;
- case 12: M41 = value; break;
- case 13: M42 = value; break;
- case 14: M43 = value; break;
- case 15: M44 = value; break;
- default: throw new ArgumentOutOfRangeException();
- }
- }
- }
- public float this[int row, int column]
- {
- get
- {
- return this[(row * 4) + column];
- }
- set
- {
- this[(row * 4) + column] = value;
- }
- }
- #endregion
- #region Private Members
- private static Matrix identity = new Matrix(1f, 0f, 0f, 0f,
- 0f, 1f, 0f, 0f,
- 0f, 0f, 1f, 0f,
- 0f, 0f, 0f, 1f);
- #endregion
- #region Public Properties
- /// <summary>
- /// The backward vector formed from the third row M31, M32, M33 elements.
- /// </summary>
- public Vector3 Backward
- {
- get
- {
- return new Vector3(this.M31, this.M32, this.M33);
- }
- set
- {
- this.M31 = value.X;
- this.M32 = value.Y;
- this.M33 = value.Z;
- }
- }
- /// <summary>
- /// The down vector formed from the second row -M21, -M22, -M23 elements.
- /// </summary>
- public Vector3 Down
- {
- get
- {
- return new Vector3(-this.M21, -this.M22, -this.M23);
- }
- set
- {
- this.M21 = -value.X;
- this.M22 = -value.Y;
- this.M23 = -value.Z;
- }
- }
- /// <summary>
- /// The forward vector formed from the third row -M31, -M32, -M33 elements.
- /// </summary>
- public Vector3 Forward
- {
- get
- {
- return new Vector3(-this.M31, -this.M32, -this.M33);
- }
- set
- {
- this.M31 = -value.X;
- this.M32 = -value.Y;
- this.M33 = -value.Z;
- }
- }
- /// <summary>
- /// Returns the identity matrix.
- /// </summary>
- public static Matrix Identity
- {
- get { return identity; }
- }
- /// <summary>
- /// The left vector formed from the first row -M11, -M12, -M13 elements.
- /// </summary>
- public Vector3 Left
- {
- get
- {
- return new Vector3(-this.M11, -this.M12, -this.M13);
- }
- set
- {
- this.M11 = -value.X;
- this.M12 = -value.Y;
- this.M13 = -value.Z;
- }
- }
- /// <summary>
- /// The right vector formed from the first row M11, M12, M13 elements.
- /// </summary>
- public Vector3 Right
- {
- get
- {
- return new Vector3(this.M11, this.M12, this.M13);
- }
- set
- {
- this.M11 = value.X;
- this.M12 = value.Y;
- this.M13 = value.Z;
- }
- }
- /// <summary>
- /// Rotation stored in this matrix.
- /// </summary>
- public Quaternion Rotation
- {
- get
- {
- return Quaternion.CreateFromRotationMatrix(this);
- }
- }
- /// <summary>
- /// Position stored in this matrix.
- /// </summary>
- public Vector3 Translation
- {
- get
- {
- return new Vector3(this.M41, this.M42, this.M43);
- }
- set
- {
- this.M41 = value.X;
- this.M42 = value.Y;
- this.M43 = value.Z;
- }
- }
- /// <summary>
- /// Scale stored in this matrix.
- /// </summary>
- public Vector3 Scale
- {
- get
- {
- return new Vector3(this.M11, this.M22, this.M33);
- }
- set
- {
- this.M11 = value.X;
- this.M22 = value.Y;
- this.M33 = value.Z;
- }
- }
- /// <summary>
- /// The upper vector formed from the second row M21, M22, M23 elements.
- /// </summary>
- public Vector3 Up
- {
- get
- {
- return new Vector3(this.M21, this.M22, this.M23);
- }
- set
- {
- this.M21 = value.X;
- this.M22 = value.Y;
- this.M23 = value.Z;
- }
- }
- #endregion
- #region Public Methods
- /// <summary>
- /// Creates a new <see cref="Matrix"/> which contains sum of two matrixes.
- /// </summary>
- /// <param name="matrix1">The first matrix to add.</param>
- /// <param name="matrix2">The second matrix to add.</param>
- /// <returns>The result of the matrix addition.</returns>
- public static Matrix Add(Matrix matrix1, Matrix matrix2)
- {
- matrix1.M11 += matrix2.M11;
- matrix1.M12 += matrix2.M12;
- matrix1.M13 += matrix2.M13;
- matrix1.M14 += matrix2.M14;
- matrix1.M21 += matrix2.M21;
- matrix1.M22 += matrix2.M22;
- matrix1.M23 += matrix2.M23;
- matrix1.M24 += matrix2.M24;
- matrix1.M31 += matrix2.M31;
- matrix1.M32 += matrix2.M32;
- matrix1.M33 += matrix2.M33;
- matrix1.M34 += matrix2.M34;
- matrix1.M41 += matrix2.M41;
- matrix1.M42 += matrix2.M42;
- matrix1.M43 += matrix2.M43;
- matrix1.M44 += matrix2.M44;
- return matrix1;
- }
- /// <summary>
- /// Creates a new <see cref="Matrix"/> which contains sum of two matrixes.
- /// </summary>
- /// <param name="matrix1">The first matrix to add.</param>
- /// <param name="matrix2">The second matrix to add.</param>
- /// <param name="result">The result of the matrix addition as an output parameter.</param>
- public static void Add(ref Matrix matrix1, ref Matrix matrix2, out Matrix result)
- {
- result.M11 = matrix1.M11 + matrix2.M11;
- result.M12 = matrix1.M12 + matrix2.M12;
- result.M13 = matrix1.M13 + matrix2.M13;
- result.M14 = matrix1.M14 + matrix2.M14;
- result.M21 = matrix1.M21 + matrix2.M21;
- result.M22 = matrix1.M22 + matrix2.M22;
- result.M23 = matrix1.M23 + matrix2.M23;
- result.M24 = matrix1.M24 + matrix2.M24;
- result.M31 = matrix1.M31 + matrix2.M31;
- result.M32 = matrix1.M32 + matrix2.M32;
- result.M33 = matrix1.M33 + matrix2.M33;
- result.M34 = matrix1.M34 + matrix2.M34;
- result.M41 = matrix1.M41 + matrix2.M41;
- result.M42 = matrix1.M42 + matrix2.M42;
- result.M43 = matrix1.M43 + matrix2.M43;
- result.M44 = matrix1.M44 + matrix2.M44;
- }
- /// <summary>
- /// Creates a new <see cref="Matrix"/> for spherical billboarding that rotates around specified object position.
- /// </summary>
- /// <param name="objectPosition">Position of billboard object. It will rotate around that vector.</param>
- /// <param name="cameraPosition">The camera position.</param>
- /// <param name="cameraUpVector">The camera up vector.</param>
- /// <param name="cameraForwardVector">Optional camera forward vector.</param>
- /// <returns>The <see cref="Matrix"/> for spherical billboarding.</returns>
- public static Matrix CreateBillboard(Vector3 objectPosition, Vector3 cameraPosition,
- Vector3 cameraUpVector, Nullable<Vector3> cameraForwardVector)
- {
- Matrix result;
- // Delegate to the other overload of the function to do the work
- CreateBillboard(ref objectPosition, ref cameraPosition, ref cameraUpVector, cameraForwardVector, out result);
- return result;
- }
- /// <summary>
- /// Creates a new <see cref="Matrix"/> for spherical billboarding that rotates around specified object position.
- /// </summary>
- /// <param name="objectPosition">Position of billboard object. It will rotate around that vector.</param>
- /// <param name="cameraPosition">The camera position.</param>
- /// <param name="cameraUpVector">The camera up vector.</param>
- /// <param name="cameraForwardVector">Optional camera forward vector.</param>
- /// <param name="result">The <see cref="Matrix"/> for spherical billboarding as an output parameter.</param>
- public static void CreateBillboard(ref Vector3 objectPosition, ref Vector3 cameraPosition,
- ref Vector3 cameraUpVector, Vector3? cameraForwardVector, out Matrix result)
- {
- Vector3 vector;
- Vector3 vector2;
- Vector3 vector3;
- vector.X = objectPosition.X - cameraPosition.X;
- vector.Y = objectPosition.Y - cameraPosition.Y;
- vector.Z = objectPosition.Z - cameraPosition.Z;
- float num = vector.LengthSquared();
- if (num < 0.0001f)
- {
- vector = cameraForwardVector.HasValue ? -cameraForwardVector.Value : Vector3.Forward;
- }
- else
- {
- Vector3.Multiply(ref vector, (float)(1f / ((float)Math.Sqrt((double)num))), out vector);
- }
- Vector3.Cross(ref cameraUpVector, ref vector, out vector3);
- vector3.Normalize();
- Vector3.Cross(ref vector, ref vector3, out vector2);
- result.M11 = vector3.X;
- result.M12 = vector3.Y;
- result.M13 = vector3.Z;
- result.M14 = 0;
- result.M21 = vector2.X;
- result.M22 = vector2.Y;
- result.M23 = vector2.Z;
- result.M24 = 0;
- result.M31 = vector.X;
- result.M32 = vector.Y;
- result.M33 = vector.Z;
- result.M34 = 0;
- result.M41 = objectPosition.X;
- result.M42 = objectPosition.Y;
- result.M43 = objectPosition.Z;
- result.M44 = 1;
- }
- /// <summary>
- /// Creates a new <see cref="Matrix"/> for cylindrical billboarding that rotates around specified axis.
- /// </summary>
- /// <param name="objectPosition">Object position the billboard will rotate around.</param>
- /// <param name="cameraPosition">Camera position.</param>
- /// <param name="rotateAxis">Axis of billboard for rotation.</param>
- /// <param name="cameraForwardVector">Optional camera forward vector.</param>
- /// <param name="objectForwardVector">Optional object forward vector.</param>
- /// <returns>The <see cref="Matrix"/> for cylindrical billboarding.</returns>
- public static Matrix CreateConstrainedBillboard(Vector3 objectPosition, Vector3 cameraPosition,
- Vector3 rotateAxis, Nullable<Vector3> cameraForwardVector, Nullable<Vector3> objectForwardVector)
- {
- Matrix result;
- CreateConstrainedBillboard(ref objectPosition, ref cameraPosition, ref rotateAxis,
- cameraForwardVector, objectForwardVector, out result);
- return result;
- }
- /// <summary>
- /// Creates a new <see cref="Matrix"/> for cylindrical billboarding that rotates around specified axis.
- /// </summary>
- /// <param name="objectPosition">Object position the billboard will rotate around.</param>
- /// <param name="cameraPosition">Camera position.</param>
- /// <param name="rotateAxis">Axis of billboard for rotation.</param>
- /// <param name="cameraForwardVector">Optional camera forward vector.</param>
- /// <param name="objectForwardVector">Optional object forward vector.</param>
- /// <param name="result">The <see cref="Matrix"/> for cylindrical billboarding as an output parameter.</param>
- public static void CreateConstrainedBillboard(ref Vector3 objectPosition, ref Vector3 cameraPosition,
- ref Vector3 rotateAxis, Vector3? cameraForwardVector, Vector3? objectForwardVector, out Matrix result)
- {
- float num;
- Vector3 vector;
- Vector3 vector2;
- Vector3 vector3;
- vector2.X = objectPosition.X - cameraPosition.X;
- vector2.Y = objectPosition.Y - cameraPosition.Y;
- vector2.Z = objectPosition.Z - cameraPosition.Z;
- float num2 = vector2.LengthSquared();
- if (num2 < 0.0001f)
- {
- vector2 = cameraForwardVector.HasValue ? -cameraForwardVector.Value : Vector3.Forward;
- }
- else
- {
- Vector3.Multiply(ref vector2, (float) (1f / ((float) Math.Sqrt((double) num2))), out vector2);
- }
- Vector3 vector4 = rotateAxis;
- Vector3.Dot(ref rotateAxis, ref vector2, out num);
- if (Math.Abs(num) > 0.9982547f)
- {
- if (objectForwardVector.HasValue)
- {
- vector = objectForwardVector.Value;
- Vector3.Dot(ref rotateAxis, ref vector, out num);
- if (Math.Abs(num) > 0.9982547f)
- {
- num = ((rotateAxis.X * Vector3.Forward.X) + (rotateAxis.Y * Vector3.Forward.Y)) + (rotateAxis.Z * Vector3.Forward.Z);
- vector = (Math.Abs(num) > 0.9982547f) ? Vector3.Right : Vector3.Forward;
- }
- }
- else
- {
- num = ((rotateAxis.X * Vector3.Forward.X) + (rotateAxis.Y * Vector3.Forward.Y)) + (rotateAxis.Z * Vector3.Forward.Z);
- vector = (Math.Abs(num) > 0.9982547f) ? Vector3.Right : Vector3.Forward;
- }
- Vector3.Cross(ref rotateAxis, ref vector, out vector3);
- vector3.Normalize();
- Vector3.Cross(ref vector3, ref rotateAxis, out vector);
- vector.Normalize();
- }
- else
- {
- Vector3.Cross(ref rotateAxis, ref vector2, out vector3);
- vector3.Normalize();
- Vector3.Cross(ref vector3, ref vector4, out vector);
- vector.Normalize();
- }
- result.M11 = vector3.X;
- result.M12 = vector3.Y;
- result.M13 = vector3.Z;
- result.M14 = 0;
- result.M21 = vector4.X;
- result.M22 = vector4.Y;
- result.M23 = vector4.Z;
- result.M24 = 0;
- result.M31 = vector.X;
- result.M32 = vector.Y;
- result.M33 = vector.Z;
- result.M34 = 0;
- result.M41 = objectPosition.X;
- result.M42 = objectPosition.Y;
- result.M43 = objectPosition.Z;
- result.M44 = 1;
- }
- /// <summary>
- /// Creates a new <see cref="Matrix"/> which contains the rotation moment around specified axis.
- /// </summary>
- /// <param name="axis">The axis of rotation.</param>
- /// <param name="angle">The angle of rotation in radians.</param>
- /// <returns>The rotation <see cref="Matrix"/>.</returns>
- public static Matrix CreateFromAxisAngle(Vector3 axis, float angle)
- {
- Matrix result;
- CreateFromAxisAngle(ref axis, angle, out result);
- return result;
- }
- /// <summary>
- /// Creates a new <see cref="Matrix"/> which contains the rotation moment around specified axis.
- /// </summary>
- /// <param name="axis">The axis of rotation.</param>
- /// <param name="angle">The angle of rotation in radians.</param>
- /// <param name="result">The rotation <see cref="Matrix"/> as an output parameter.</param>
- public static void CreateFromAxisAngle(ref Vector3 axis, float angle, out Matrix result)
- {
- float x = axis.X;
- float y = axis.Y;
- float z = axis.Z;
- float num2 = (float) Math.Sin((double) angle);
- float num = (float) Math.Cos((double) angle);
- float num11 = x * x;
- float num10 = y * y;
- float num9 = z * z;
- float num8 = x * y;
- float num7 = x * z;
- float num6 = y * z;
- result.M11 = num11 + (num * (1f - num11));
- result.M12 = (num8 - (num * num8)) + (num2 * z);
- result.M13 = (num7 - (num * num7)) - (num2 * y);
- result.M14 = 0;
- result.M21 = (num8 - (num * num8)) - (num2 * z);
- result.M22 = num10 + (num * (1f - num10));
- result.M23 = (num6 - (num * num6)) + (num2 * x);
- result.M24 = 0;
- result.M31 = (num7 - (num * num7)) + (num2 * y);
- result.M32 = (num6 - (num * num6)) - (num2 * x);
- result.M33 = num9 + (num * (1f - num9));
- result.M34 = 0;
- result.M41 = 0;
- result.M42 = 0;
- result.M43 = 0;
- result.M44 = 1;
- }
- /// <summary>
- /// Creates a new rotation <see cref="Matrix"/> from a <see cref="Quaternion"/>.
- /// </summary>
- /// <param name="quaternion"><see cref="Quaternion"/> of rotation moment.</param>
- /// <returns>The rotation <see cref="Matrix"/>.</returns>
- public static Matrix CreateFromQuaternion(Quaternion quaternion)
- {
- Matrix result;
- CreateFromQuaternion(ref quaternion, out result);
- return result;
- }
- /// <summary>
- /// Creates a new rotation <see cref="Matrix"/> from a <see cref="Quaternion"/>.
- /// </summary>
- /// <param name="quaternion"><see cref="Quaternion"/> of rotation moment.</param>
- /// <param name="result">The rotation <see cref="Matrix"/> as an output parameter.</param>
- public static void CreateFromQuaternion(ref Quaternion quaternion, out Matrix result)
- {
- float num9 = quaternion.X * quaternion.X;
- float num8 = quaternion.Y * quaternion.Y;
- float num7 = quaternion.Z * quaternion.Z;
- float num6 = quaternion.X * quaternion.Y;
- float num5 = quaternion.Z * quaternion.W;
- float num4 = quaternion.Z * quaternion.X;
- float num3 = quaternion.Y * quaternion.W;
- float num2 = quaternion.Y * quaternion.Z;
- float num = quaternion.X * quaternion.W;
- result.M11 = 1f - (2f * (num8 + num7));
- result.M12 = 2f * (num6 + num5);
- result.M13 = 2f * (num4 - num3);
- result.M14 = 0f;
- result.M21 = 2f * (num6 - num5);
- result.M22 = 1f - (2f * (num7 + num9));
- result.M23 = 2f * (num2 + num);
- result.M24 = 0f;
- result.M31 = 2f * (num4 + num3);
- result.M32 = 2f * (num2 - num);
- result.M33 = 1f - (2f * (num8 + num9));
- result.M34 = 0f;
- result.M41 = 0f;
- result.M42 = 0f;
- result.M43 = 0f;
- result.M44 = 1f;
- }
- /// <summary>
- /// Creates a new rotation <see cref="Matrix"/> from the specified yaw, pitch and roll values.
- /// </summary>
- /// <param name="yaw">The yaw rotation value in radians.</param>
- /// <param name="pitch">The pitch rotation value in radians.</param>
- /// <param name="roll">The roll rotation value in radians.</param>
- /// <returns>The rotation <see cref="Matrix"/>.</returns>
- /// <remarks>For more information about yaw, pitch and roll visit http://en.wikipedia.org/wiki/Euler_angles.
- /// </remarks>
- public static Matrix CreateFromYawPitchRoll(float yaw, float pitch, float roll)
- {
- Matrix matrix;
- CreateFromYawPitchRoll(yaw, pitch, roll, out matrix);
- return matrix;
- }
- /// <summary>
- /// Creates a new rotation <see cref="Matrix"/> from the specified yaw, pitch and roll values.
- /// </summary>
- /// <param name="yaw">The yaw rotation value in radians.</param>
- /// <param name="pitch">The pitch rotation value in radians.</param>
- /// <param name="roll">The roll rotation value in radians.</param>
- /// <param name="result">The rotation <see cref="Matrix"/> as an output parameter.</param>
- /// <remarks>For more information about yaw, pitch and roll visit http://en.wikipedia.org/wiki/Euler_angles.
- /// </remarks>
- public static void CreateFromYawPitchRoll(float yaw, float pitch, float roll, out Matrix result)
- {
- Quaternion quaternion;
- Quaternion.CreateFromYawPitchRoll(yaw, pitch, roll, out quaternion);
- CreateFromQuaternion(ref quaternion, out result);
- }
- /// <summary>
- /// Creates a new viewing <see cref="Matrix"/>.
- /// </summary>
- /// <param name="cameraPosition">Position of the camera.</param>
- /// <param name="cameraTarget">Lookup vector of the camera.</param>
- /// <param name="cameraUpVector">The direction of the upper edge of the camera.</param>
- /// <returns>The viewing <see cref="Matrix"/>.</returns>
- public static Matrix CreateLookAt(Vector3 cameraPosition, Vector3 cameraTarget, Vector3 cameraUpVector)
- {
- Matrix matrix;
- CreateLookAt(ref cameraPosition, ref cameraTarget, ref cameraUpVector, out matrix);
- return matrix;
- }
- /// <summary>
- /// Creates a new viewing <see cref="Matrix"/>.
- /// </summary>
- /// <param name="cameraPosition">Position of the camera.</param>
- /// <param name="cameraTarget">Lookup vector of the camera.</param>
- /// <param name="cameraUpVector">The direction of the upper edge of the camera.</param>
- /// <param name="result">The viewing <see cref="Matrix"/> as an output parameter.</param>
- public static void CreateLookAt(ref Vector3 cameraPosition, ref Vector3 cameraTarget, ref Vector3 cameraUpVector, out Matrix result)
- {
- var vector = Vector3.Normalize(cameraPosition - cameraTarget);
- var vector2 = Vector3.Normalize(Vector3.Cross(cameraUpVector, vector));
- var vector3 = Vector3.Cross(vector, vector2);
- result.M11 = vector2.X;
- result.M12 = vector3.X;
- result.M13 = vector.X;
- result.M14 = 0f;
- result.M21 = vector2.Y;
- result.M22 = vector3.Y;
- result.M23 = vector.Y;
- result.M24 = 0f;
- result.M31 = vector2.Z;
- result.M32 = vector3.Z;
- result.M33 = vector.Z;
- result.M34 = 0f;
- result.M41 = -Vector3.Dot(vector2, cameraPosition);
- result.M42 = -Vector3.Dot(vector3, cameraPosition);
- result.M43 = -Vector3.Dot(vector, cameraPosition);
- result.M44 = 1f;
- }
- /// <summary>
- /// Creates a new projection <see cref="Matrix"/> for orthographic view.
- /// </summary>
- /// <param name="width">Width of the viewing volume.</param>
- /// <param name="height">Height of the viewing volume.</param>
- /// <param name="zNearPlane">Depth of the near plane.</param>
- /// <param name="zFarPlane">Depth of the far plane.</param>
- /// <returns>The new projection <see cref="Matrix"/> for orthographic view.</returns>
- public static Matrix CreateOrthographic(float width, float height, float zNearPlane, float zFarPlane)
- {
- Matrix matrix;
- CreateOrthographic(width, height, zNearPlane, zFarPlane, out matrix);
- return matrix;
- }
- /// <summary>
- /// Creates a new projection <see cref="Matrix"/> for orthographic view.
- /// </summary>
- /// <param name="width">Width of the viewing volume.</param>
- /// <param name="height">Height of the viewing volume.</param>
- /// <param name="zNearPlane">Depth of the near plane.</param>
- /// <param name="zFarPlane">Depth of the far plane.</param>
- /// <param name="result">The new projection <see cref="Matrix"/> for orthographic view as an output parameter.</param>
- public static void CreateOrthographic(float width, float height, float zNearPlane, float zFarPlane, out Matrix result)
- {
- result.M11 = 2f / width;
- result.M12 = result.M13 = result.M14 = 0f;
- result.M22 = 2f / height;
- result.M21 = result.M23 = result.M24 = 0f;
- result.M33 = 1f / (zNearPlane - zFarPlane);
- result.M31 = result.M32 = result.M34 = 0f;
- result.M41 = result.M42 = 0f;
- result.M43 = zNearPlane / (zNearPlane - zFarPlane);
- result.M44 = 1f;
- }
- /// <summary>
- /// Creates a new projection <see cref="Matrix"/> for customized orthographic view.
- /// </summary>
- /// <param name="left">Lower x-value at the near plane.</param>
- /// <param name="right">Upper x-value at the near plane.</param>
- /// <param name="bottom">Lower y-coordinate at the near plane.</param>
- /// <param name="top">Upper y-value at the near plane.</param>
- /// <param name="zNearPlane">Depth of the near plane.</param>
- /// <param name="zFarPlane">Depth of the far plane.</param>
- /// <returns>The new projection <see cref="Matrix"/> for customized orthographic view.</returns>
- public static Matrix CreateOrthographicOffCenter(float left, float right, float bottom, float top, float zNearPlane, float zFarPlane)
- {
- Matrix matrix;
- CreateOrthographicOffCenter(left, right, bottom, top, zNearPlane, zFarPlane, out matrix);
- return matrix;
- }
- /// <summary>
- /// Creates a new projection <see cref="Matrix"/> for customized orthographic view.
- /// </summary>
- /// <param name="viewingVolume">The viewing volume.</param>
- /// <param name="zNearPlane">Depth of the near plane.</param>
- /// <param name="zFarPlane">Depth of the far plane.</param>
- /// <returns>The new projection <see cref="Matrix"/> for customized orthographic view.</returns>
- public static Matrix CreateOrthographicOffCenter(Rectangle viewingVolume, float zNearPlane, float zFarPlane)
- {
- Matrix matrix;
- CreateOrthographicOffCenter(viewingVolume.Left, viewingVolume.Right, viewingVolume.Bottom, viewingVolume.Top, zNearPlane, zFarPlane, out matrix);
- return matrix;
- }
- /// <summary>
- /// Creates a new projection <see cref="Matrix"/> for customized orthographic view.
- /// </summary>
- /// <param name="left">Lower x-value at the near plane.</param>
- /// <param name="right">Upper x-value at the near plane.</param>
- /// <param name="bottom">Lower y-coordinate at the near plane.</param>
- /// <param name="top">Upper y-value at the near plane.</param>
- /// <param name="zNearPlane">Depth of the near plane.</param>
- /// <param name="zFarPlane">Depth of the far plane.</param>
- /// <param name="result">The new projection <see cref="Matrix"/> for customized orthographic view as an output parameter.</param>
- public static void CreateOrthographicOffCenter(float left, float right, float bottom, float top, float zNearPlane, float zFarPlane, out Matrix result)
- {
- result.M11 = (float)(2.0 / ((double)right - (double)left));
- result.M12 = 0.0f;
- result.M13 = 0.0f;
- result.M14 = 0.0f;
- result.M21 = 0.0f;
- result.M22 = (float)(2.0 / ((double)top - (double)bottom));
- result.M23 = 0.0f;
- result.M24 = 0.0f;
- result.M31 = 0.0f;
- result.M32 = 0.0f;
- result.M33 = (float)(1.0 / ((double)zNearPlane - (double)zFarPlane));
- result.M34 = 0.0f;
- result.M41 = (float)(((double)left + (double)right) / ((double)left - (double)right));
- result.M42 = (float)(((double)top + (double)bottom) / ((double)bottom - (double)top));
- result.M43 = (float)((double)zNearPlane / ((double)zNearPlane - (double)zFarPlane));
- result.M44 = 1.0f;
- }
- /// <summary>
- /// Creates a new projection <see cref="Matrix"/> for perspective view.
- /// </summary>
- /// <param name="width">Width of the viewing volume.</param>
- /// <param name="height">Height of the viewing volume.</param>
- /// <param name="nearPlaneDistance">Distance to the near plane.</param>
- /// <param name="farPlaneDistance">Distance to the far plane.</param>
- /// <returns>The new projection <see cref="Matrix"/> for perspective view.</returns>
- public static Matrix CreatePerspective(float width, float height, float nearPlaneDistance, float farPlaneDistance)
- {
- Matrix matrix;
- CreatePerspective(width, height, nearPlaneDistance, farPlaneDistance, out matrix);
- return matrix;
- }
- /// <summary>
- /// Creates a new projection <see cref="Matrix"/> for perspective view.
- /// </summary>
- /// <param name="width">Width of the viewing volume.</param>
- /// <param name="height">Height of the viewing volume.</param>
- /// <param name="nearPlaneDistance">Distance to the near plane.</param>
- /// <param name="farPlaneDistance">Distance to the far plane.</param>
- /// <param name="result">The new projection <see cref="Matrix"/> for perspective view as an output parameter.</param>
- public static void CreatePerspective(float width, float height, float nearPlaneDistance, float farPlaneDistance, out Matrix result)
- {
- if (nearPlaneDistance <= 0f)
- {
- throw new ArgumentException("nearPlaneDistance <= 0");
- }
- if (farPlaneDistance <= 0f)
- {
- throw new ArgumentException("farPlaneDistance <= 0");
- }
- if (nearPlaneDistance >= farPlaneDistance)
- {
- throw new ArgumentException("nearPlaneDistance >= farPlaneDistance");
- }
- result.M11 = (2f * nearPlaneDistance) / width;
- result.M12 = result.M13 = result.M14 = 0f;
- result.M22 = (2f * nearPlaneDistance) / height;
- result.M21 = result.M23 = result.M24 = 0f;
- result.M33 = farPlaneDistance / (nearPlaneDistance - farPlaneDistance);
- result.M31 = result.M32 = 0f;
- result.M34 = -1f;
- result.M41 = result.M42 = result.M44 = 0f;
- result.M43 = (nearPlaneDistance * farPlaneDistance) / (nearPlaneDistance - farPlaneDistance);
- }
- /// <summary>
- /// Creates a new projection <see cref="Matrix"/> for perspective view with field of view.
- /// </summary>
- /// <param name="fieldOfView">Field of view in the y direction in radians.</param>
- /// <param name="aspectRatio">Width divided by height of the viewing volume.</param>
- /// <param name="nearPlaneDistance">Distance to the near plane.</param>
- /// <param name="farPlaneDistance">Distance to the far plane.</param>
- /// <returns>The new projection <see cref="Matrix"/> for perspective view with FOV.</returns>
- public static Matrix CreatePerspectiveFieldOfView(float fieldOfView, float aspectRatio, float nearPlaneDistance, float farPlaneDistance)
- {
- Matrix result;
- CreatePerspectiveFieldOfView(fieldOfView, aspectRatio, nearPlaneDistance, farPlaneDistance, out result);
- return result;
- }
- /// <summary>
- /// Creates a new projection <see cref="Matrix"/> for perspective view with field of view.
- /// </summary>
- /// <param name="fieldOfView">Field of view in the y direction in radians.</param>
- /// <param name="aspectRatio">Width divided by height of the viewing volume.</param>
- /// <param name="nearPlaneDistance">Distance of the near plane.</param>
- /// <param name="farPlaneDistance">Distance of the far plane.</param>
- /// <param name="result">The new projection <see cref="Matrix"/> for perspective view with FOV as an output parameter.</param>
- public static void CreatePerspectiveFieldOfView(float fieldOfView, float aspectRatio, float nearPlaneDistance, float farPlaneDistance, out Matrix result)
- {
- if ((fieldOfView <= 0f) || (fieldOfView >= 3.141593f))
- {
- throw new ArgumentException("fieldOfView <= 0 or >= PI");
- }
- if (nearPlaneDistance <= 0f)
- {
- throw new ArgumentException("nearPlaneDistance <= 0");
- }
- if (farPlaneDistance <= 0f)
- {
- throw new ArgumentException("farPlaneDistance <= 0");
- }
- if (nearPlaneDistance >= farPlaneDistance)
- {
- throw new ArgumentException("nearPlaneDistance >= farPlaneDistance");
- }
- float num = 1f / ((float) Math.Tan((double) (fieldOfView * 0.5f)));
- float num9 = num / aspectRatio;
- result.M11 = num9;
- result.M12 = result.M13 = result.M14 = 0;
- result.M22 = num;
- result.M21 = result.M23 = result.M24 = 0;
- result.M31 = result.M32 = 0f;
- result.M33 = farPlaneDistance / (nearPlaneDistance - farPlaneDistance);
- result.M34 = -1;
- result.M41 = result.M42 = result.M44 = 0;
- result.M43 = (nearPlaneDistance * farPlaneDistance) / (nearPlaneDistance - farPlaneDistance);
- }
- /// <summary>
- /// Creates a new projection <see cref="Matrix"/> for customized perspective view.
- /// </summary>
- /// <param name="left">Lower x-value at the near plane.</param>
- /// <param name="right">Upper x-value at the near plane.</param>
- /// <param name="bottom">Lower y-coordinate at the near plane.</param>
- /// <param name="top">Upper y-value at the near plane.</param>
- /// <param name="nearPlaneDistance">Distance to the near plane.</param>
- /// <param name="farPlaneDistance">Distance to the far plane.</param>
- /// <returns>The new <see cref="Matrix"/> for customized perspective view.</returns>
- public static Matrix CreatePerspectiveOffCenter(float left, float right, float bottom, float top, float nearPlaneDistance, float farPlaneDistance)
- {
- Matrix result;
- CreatePerspectiveOffCenter(left, right, bottom, top, nearPlaneDistance, farPlaneDistance, out result);
- return result;
- }
- /// <summary>
- /// Creates a new projection <see cref="Matrix"/> for customized perspective view.
- /// </summary>
- /// <param name="viewingVolume">The viewing volume.</param>
- /// <param name="nearPlaneDistance">Distance to the near plane.</param>
- /// <param name="farPlaneDistance">Distance to the far plane.</param>
- /// <returns>The new <see cref="Matrix"/> for customized perspective view.</returns>
- public static Matrix CreatePerspectiveOffCenter(Rectangle viewingVolume, float nearPlaneDistance, float farPlaneDistance)
- {
- Matrix result;
- CreatePerspectiveOffCenter(viewingVolume.Left, viewingVolume.Right, viewingVolume.Bottom, viewingVolume.Top, nearPlaneDistance, farPlaneDistance, out result);
- return result;
- }
- /// <summary>
- /// Creates a new projection <see cref="Matrix"/> for customized perspective view.
- /// </summary>
- /// <param name="left">Lower x-value at the near plane.</param>
- /// <param name="right">Upper x-value at the near plane.</param>
- /// <param name="bottom">Lower y-coordinate at the near plane.</param>
- /// <param name="top">Upper y-value at the near plane.</param>
- /// <param name="nearPlaneDistance">Distance to the near plane.</param>
- /// <param name="farPlaneDistance">Distance to the far plane.</param>
- /// <param name="result">The new <see cref="Matrix"/> for customized perspective view as an output parameter.</param>
- public static void CreatePerspectiveOffCenter(float left, float right, float bottom, float top, float nearPlaneDistance, float farPlaneDistance, out Matrix result)
- {
- if (nearPlaneDistance <= 0f)
- {
- throw new ArgumentException("nearPlaneDistance <= 0");
- }
- if (farPlaneDistance <= 0f)
- {
- throw new ArgumentException("farPlaneDistance <= 0");
- }
- if (nearPlaneDistance >= farPlaneDistance)
- {
- throw new ArgumentException("nearPlaneDistance >= farPlaneDistance");
- }
- result.M11 = (2f * nearPlaneDistance) / (right - left);
- result.M12 = result.M13 = result.M14 = 0;
- result.M22 = (2f * nearPlaneDistance) / (top - bottom);
- result.M21 = result.M23 = result.M24 = 0;
- result.M31 = (left + right) / (right - left);
- result.M32 = (top + bottom) / (top - bottom);
- result.M33 = farPlaneDistance / (nearPlaneDistance - farPlaneDistance);
- result.M34 = -1;
- result.M43 = (nearPlaneDistance * farPlaneDistance) / (nearPlaneDistance - farPlaneDistance);
- result.M41 = result.M42 = result.M44 = 0;
- }
- /// <summary>
- /// Creates a new rotation <see cref="Matrix"/> around X axis.
- /// </summary>
- /// <param name="radians">Angle in radians.</param>
- /// <returns>The rotation <see cref="Matrix"/> around X axis.</returns>
- public static Matrix CreateRotationX(float radians)
- {
- Matrix result;
- CreateRotationX(radians, out result);
- return result;
- }
- /// <summary>
- /// Creates a new rotation <see cref="Matrix"/> around X axis.
- /// </summary>
- /// <param name="radians">Angle in radians.</param>
- /// <param name="result">The rotation <see cref="Matrix"/> around X axis as an output parameter.</param>
- public static void CreateRotationX(float radians, out Matrix result)
- {
- result = Matrix.Identity;
- var val1 = (float)Math.Cos(radians);
- var val2 = (float)Math.Sin(radians);
-
- result.M22 = val1;
- result.M23 = val2;
- result.M32 = -val2;
- result.M33 = val1;
- }
- /// <summary>
- /// Creates a new rotation <see cref="Matrix"/> around Y axis.
- /// </summary>
- /// <param name="radians">Angle in radians.</param>
- /// <returns>The rotation <see cref="Matrix"/> around Y axis.</returns>
- public static Matrix CreateRotationY(float radians)
- {
- Matrix result;
- CreateRotationY(radians, out result);
- return result;
- }
- /// <summary>
- /// Creates a new rotation <see cref="Matrix"/> around Y axis.
- /// </summary>
- /// <param name="radians">Angle in radians.</param>
- /// <param name="result">The rotation <see cref="Matrix"/> around Y axis as an output parameter.</param>
- public static void CreateRotationY(float radians, out Matrix result)
- {
- result = Matrix.Identity;
- var val1 = (float)Math.Cos(radians);
- var val2 = (float)Math.Sin(radians);
-
- result.M11 = val1;
- result.M13 = -val2;
- result.M31 = val2;
- result.M33 = val1;
- }
- /// <summary>
- /// Creates a new rotation <see cref="Matrix"/> around Z axis.
- /// </summary>
- /// <param name="radians">Angle in radians.</param>
- /// <returns>The rotation <see cref="Matrix"/> around Z axis.</returns>
- public static Matrix CreateRotationZ(float radians)
- {
- Matrix result;
- CreateRotationZ(radians, out result);
- return result;
- }
- /// <summary>
- /// Creates a new rotation <see cref="Matrix"/> around Z axis.
- /// </summary>
- /// <param name="radians">Angle in radians.</param>
- /// <param name="result">The rotation <see cref="Matrix"/> around Z axis as an output parameter.</param>
- public static void CreateRotationZ(float radians, out Matrix result)
- {
- result = Matrix.Identity;
- var val1 = (float)Math.Cos(radians);
- var val2 = (float)Math.Sin(radians);
-
- result.M11 = val1;
- result.M12 = val2;
- result.M21 = -val2;
- result.M22 = val1;
- }
- /// <summary>
- /// Creates a new scaling <see cref="Matrix"/>.
- /// </summary>
- /// <param name="scale">Scale value for all three axises.</param>
- /// <returns>The scaling <see cref="Matrix"/>.</returns>
- public static Matrix CreateScale(float scale)
- {
- Matrix result;
- CreateScale(scale, scale, scale, out result);
- return result;
- }
- /// <summary>
- /// Creates a new scaling <see cref="Matrix"/>.
- /// </summary>
- /// <param name="scale">Scale value for all three axises.</param>
- /// <param name="result">The scaling <see cref="Matrix"/> as an output parameter.</param>
- public static void CreateScale(float scale, out Matrix result)
- {
- CreateScale(scale, scale, scale, out result);
- }
- /// <summary>
- /// Creates a new scaling <see cref="Matrix"/>.
- /// </summary>
- /// <param name="xScale">Scale value for X axis.</param>
- /// <param name="yScale">Scale value for Y axis.</param>
- /// <param name="zScale">Scale value for Z axis.</param>
- /// <returns>The scaling <see cref="Matrix"/>.</returns>
- public static Matrix CreateScale(float xScale, float yScale, float zScale)
- {
- Matrix result;
- CreateScale(xScale, yScale, zScale, out result);
- return result;
- }
- /// <summary>
- /// Creates a new scaling <see cref="Matrix"/>.
- /// </summary>
- /// <param name="xScale">Scale value for X axis.</param>
- /// <param name="yScale">Scale value for Y axis.</param>
- /// <param name="zScale">Scale value for Z axis.</param>
- /// <param name="result">The scaling <see cref="Matrix"/> as an output parameter.</param>
- public static void CreateScale(float xScale, float yScale, float zScale, out Matrix result)
- {
- result.M11 = xScale;
- result.M12 = 0;
- result.M13 = 0;
- result.M14 = 0;
- result.M21 = 0;
- result.M22 = yScale;
- result.M23 = 0;
- result.M24 = 0;
- result.M31 = 0;
- result.M32 = 0;
- result.M33 = zScale;
- result.M34 = 0;
- result.M41 = 0;
- result.M42 = 0;
- result.M43 = 0;
- result.M44 = 1;
- }
- /// <summary>
- /// Creates a new scaling <see cref="Matrix"/>.
- /// </summary>
- /// <param name="scales"><see cref="Vector3"/> representing x,y and z scale values.</param>
- /// <returns>The scaling <see cref="Matrix"/>.</returns>
- public static Matrix CreateScale(Vector3 scales)
- {
- Matrix result;
- CreateScale(ref scales, out result);
- return result;
- }
- /// <summary>
- /// Creates a new scaling <see cref="Matrix"/>.
- /// </summary>
- /// <param name="scales"><see cref="Vector3"/> representing x,y and z scale values.</param>
- /// <param name="result">The scaling <see cref="Matrix"/> as an output parameter.</param>
- public static void CreateScale(ref Vector3 scales, out Matrix result)
- {
- result.M11 = scales.X;
- result.M12 = 0;
- result.M13 = 0;
- result.M14 = 0;
- result.M21 = 0;
- result.M22 = scales.Y;
- result.M23 = 0;
- result.M24 = 0;
- result.M31 = 0;
- result.M32 = 0;
- result.M33 = scales.Z;
- result.M34 = 0;
- result.M41 = 0;
- result.M42 = 0;
- result.M43 = 0;
- result.M44 = 1;
- }
- /// <summary>
- /// Creates a new <see cref="Matrix"/> that flattens geometry into a specified <see cref="Plane"/> as if casting a shadow from a specified light source.
- /// </summary>
- /// <param name="lightDirection">A vector specifying the direction from which the light that will cast the shadow is coming.</param>
- /// <param name="plane">The plane onto which the new matrix should flatten geometry so as to cast a shadow.</param>
- /// <returns>A <see cref="Matrix"/> that can be used to flatten geometry onto the specified plane from the specified direction. </returns>
- public static Matrix CreateShadow(Vector3 lightDirection, Plane plane)
- {
- Matrix result;
- CreateShadow(ref lightDirection, ref plane, out result);
- return result;
- }
- /// <summary>
- /// Creates a new <see cref="Matrix"/> that flattens geometry into a specified <see cref="Plane"/> as if casting a shadow from a specified light source.
- /// </summary>
- /// <param name="lightDirection">A vector specifying the direction from which the light that will cast the shadow is coming.</param>
- /// <param name="plane">The plane onto which the new matrix should flatten geometry so as to cast a shadow.</param>
- /// <param name="result">A <see cref="Matrix"/> that can be used to flatten geometry onto the specified plane from the specified direction as an output parameter.</param>
- public static void CreateShadow(ref Vector3 lightDirection, ref Plane plane, out Matrix result)
- {
- float dot = (plane.Normal.X * lightDirection.X) + (plane.Normal.Y * lightDirection.Y) + (plane.Normal.Z * lightDirection.Z);
- float x = -plane.Normal.X;
- float y = -plane.Normal.Y;
- float z = -plane.Normal.Z;
- float d = -plane.D;
- result.M11 = (x * lightDirection.X) + dot;
- result.M12 = x * lightDirection.Y;
- result.M13 = x * lightDirection.Z;
- result.M14 = 0;
- result.M21 = y * lightDirection.X;
- result.M22 = (y * lightDirection.Y) + dot;
- result.M23 = y * lightDirection.Z;
- result.M24 = 0;
- result.M31 = z * lightDirection.X;
- result.M32 = z * lightDirection.Y;
- result.M33 = (z * lightDirection.Z) + dot;
- result.M34 = 0;
- result.M41 = d * lightDirection.X;
- result.M42 = d * lightDirection.Y;
- result.M43 = d * lightDirection.Z;
- result.M44 = dot;
- }
-
- /// <summary>
- /// Creates a new translation <see cref="Matrix"/>.
- /// </summary>
- /// <param name="xPosition">X coordinate of translation.</param>
- /// <param name="yPosition">Y coordinate of translation.</param>
- /// <param name="zPosition">Z coordinate of translation.</param>
- /// <returns>The translation <see cref="Matrix"/>.</returns>
- public static Matrix CreateTranslation(float xPosition, float yPosition, float zPosition)
- {
- Matrix result;
- CreateTranslation(xPosition, yPosition, zPosition, out result);
- return result;
- }
- /// <summary>
- /// Creates a new translation <see cref="Matrix"/>.
- /// </summary>
- /// <param name="position">X,Y and Z coordinates of translation.</param>
- /// <param name="result">The translation <see cref="Matrix"/> as an output parameter.</param>
- public static void CreateTranslation(ref Vector3 position, out Matrix result)
- {
- result.M11 = 1;
- result.M12 = 0;
- result.M13 = 0;
- result.M14 = 0;
- result.M21 = 0;
- result.M22 = 1;
- result.M23 = 0;
- result.M24 = 0;
- result.M31 = 0;
- result.M32 = 0;
- result.M33 = 1;
- result.M34 = 0;
- result.M41 = position.X;
- result.M42 = position.Y;
- result.M43 = position.Z;
- result.M44 = 1;
- }
- /// <summary>
- /// Creates a new translation <see cref="Matrix"/>.
- /// </summary>
- /// <param name="position">X,Y and Z coordinates of translation.</param>
- /// <returns>The translation <see cref="Matrix"/>.</returns>
- public static Matrix CreateTranslation(Vector3 position)
- {
- Matrix result;
- CreateTranslation(ref position, out result);
- return result;
- }
- /// <summary>
- /// Creates a new translation <see cref="Matrix"/>.
- /// </summary>
- /// <param name="xPosition">X coordinate of translation.</param>
- /// <param name="yPosition">Y coordinate of translation.</param>
- /// <param name="zPosition">Z coordinate of translation.</param>
- /// <param name="result">The translation <see cref="Matrix"/> as an output parameter.</param>
- public static void CreateTranslation(float xPosition, float yPosition, float zPosition, out Matrix result)
- {
- result.M11 = 1;
- result.M12 = 0;
- result.M13 = 0;
- result.M14 = 0;
- result.M21 = 0;
- result.M22 = 1;
- result.M23 = 0;
- result.M24 = 0;
- result.M31 = 0;
- result.M32 = 0;
- result.M33 = 1;
- result.M34 = 0;
- result.M41 = xPosition;
- result.M42 = yPosition;
- result.M43 = zPosition;
- result.M44 = 1;
- }
-
- /// <summary>
- /// Creates a new reflection <see cref="Matrix"/>.
- /// </summary>
- /// <param name="value">The plane that used for reflection calculation.</param>
- /// <returns>The reflection <see cref="Matrix"/>.</returns>
- public static Matrix CreateReflection(Plane value)
- {
- Matrix result;
- CreateReflection(ref value, out result);
- return result;
- }
- /// <summary>
- /// Creates a new reflection <see cref="Matrix"/>.
- /// </summary>
- /// <param name="value">The plane that used for reflection calculation.</param>
- /// <param name="result">The reflection <see cref="Matrix"/> as an output parameter.</param>
- public static void CreateReflection(ref Plane value, out Matrix result)
- {
- Plane plane;
- Plane.Normalize(ref value, out plane);
- value.Normalize();
- float x = plane.Normal.X;
- float y = plane.Normal.Y;
- float z = plane.Normal.Z;
- float num3 = -2f * x;
- float num2 = -2f * y;
- float num = -2f * z;
- result.M11 = (num3 * x) + 1f;
- result.M12 = num2 * x;
- result.M13 = num * x;
- result.M14 = 0;
- result.M21 = num3 * y;
- result.M22 = (num2 * y) + 1;
- result.M23 = num * y;
- result.M24 = 0;
- result.M31 = num3 * z;
- result.M32 = num2 * z;
- result.M33 = (num * z) + 1;
- result.M34 = 0;
- result.M41 = num3 * plane.D;
- result.M42 = num2 * plane.D;
- result.M43 = num * plane.D;
- result.M44 = 1;
- }
- /// <summary>
- /// Creates a new world <see cref="Matrix"/>.
- /// </summary>
- /// <param name="position">The position vector.</param>
- /// <param name="forward">The forward direction vector.</param>
- /// <param name="up">The upward direction vector. Usually <see cref="Vector3.Up"/>.</param>
- /// <returns>The world <see cref="Matrix"/>.</returns>
- public static Matrix CreateWorld(Vector3 position, Vector3 forward, Vector3 up)
- {
- Matrix ret;
- CreateWorld(ref position, ref forward, ref up, out ret);
- return ret;
- }
- /// <summary>
- /// Creates a new world <see cref="Matrix"/>.
- /// </summary>
- /// <param name="position">The position vector.</param>
- /// <param name="forward">The forward direction vector.</param>
- /// <param name="up">The upward direction vector. Usually <see cref="Vector3.Up"/>.</param>
- /// <param name="result">The world <see cref="Matrix"/> as an output parameter.</param>
- public static void CreateWorld(ref Vector3 position, ref Vector3 forward, ref Vector3 up, out Matrix result)
- {
- Vector3 x, y, z;
- Vector3.Normalize(ref forward, out z);
- Vector3.Cross(ref forward, ref up, out x);
- Vector3.Cross(ref x, ref forward, out y);
- x.Normalize();
- y.Normalize();
-
- result = new Matrix();
- result.Right = x;
- result.Up = y;
- result.Forward = z;
- result.Translation = position;
- result.M44 = 1f;
- }
- /// <summary>
- /// Decomposes this matrix to translation, rotation and scale elements. Returns <c>true</c> if matrix can be decomposed; <c>false</c> otherwise.
- /// </summary>
- /// <param name="scale">Scale vector as an output parameter.</param>
- /// <param name="rotation">Rotation quaternion as an output parameter.</param>
- /// <param name="translation">Translation vector as an output parameter.</param>
- /// <returns><c>true</c> if matrix can be decomposed; <c>false</c> otherwise.</returns>
- public bool Decompose(out Vector3 scale, out Quaternion rotation, out Vector3 translation)
- {
- translation.X = this.M41;
- translation.Y = this.M42;
- translation.Z = this.M43;
- float xs = (Math.Sign(M11 * M12 * M13 * M14) < 0) ? -1 : 1;
- float ys = (Math.Sign(M21 * M22 * M23 * M24) < 0) ? -1 : 1;
- float zs = (Math.Sign(M31 * M32 * M33 * M34) < 0) ? -1 : 1;
- scale.X = xs * (float)Math.Sqrt(this.M11 * this.M11 + this.M12 * this.M12 + this.M13 * this.M13);
- scale.Y = ys * (float)Math.Sqrt(this.M21 * this.M21 + this.M22 * this.M22 + this.M23 * this.M23);
- scale.Z = zs * (float)Math.Sqrt(this.M31 * this.M31 + this.M32 * this.M32 + this.M33 * this.M33);
- if (scale.X == 0.0 || scale.Y == 0.0 || scale.Z == 0.0)
- {
- rotation = Quaternion.Identity;
- return false;
- }
- Matrix m1 = new Matrix(this.M11 / scale.X, M12 / scale.X, M13 / scale.X, 0,
- this.M21 / scale.Y, M22 / scale.Y, M23 / scale.Y, 0,
- this.M31 / scale.Z, M32 / scale.Z, M33 / scale.Z, 0,
- 0, 0, 0, 1);
- rotation = Quaternion.CreateFromRotationMatrix(m1);
- return true;
- }
- /// <summary>
- /// Returns a determinant of this <see cref="Matrix"/>.
- /// </summary>
- /// <returns>Determinant of this <see cref="Matrix"/></returns>
- /// <remarks>See more about determinant here - http://en.wikipedia.org/wiki/Determinant.
- /// </remarks>
- public float Determinant()
- {
- float num22 = this.M11;
- float num21 = this.M12;
- float num20 = this.M13;
- float num19 = this.M14;
- float num12 = this.M21;
- float num11 = this.M22;
- float num10 = this.M23;
- float num9 = this.M24;
- float num8 = this.M31;
- float num7 = this.M32;
- float num6 = this.M33;
- float num5 = this.M34;
- float num4 = this.M41;
- float num3 = this.M42;
- float num2 = this.M43;
- float num = this.M44;
- float num18 = (num6 * num) - (num5 * num2);
- float num17 = (num7 * num) - (num5 * num3);
- float num16 = (num7 * num2) - (num6 * num3);
- float num15 = (num8 * num) - (num5 * num4);
- float num14 = (num8 * num2) - (num6 * num4);
- float num13 = (num8 * num3) - (num7 * num4);
- return ((((num22 * (((num11 * num18) - (num10 * num17)) + (num9 * num16))) - (num21 * (((num12 * num18) - (num10 * num15)) + (num9 * num14)))) + (num20 * (((num12 * num17) - (num11 * num15)) + (num9 * num13)))) - (num19 * (((num12 * num16) - (num11 * num14)) + (num10 * num13))));
- }
- /// <summary>
- /// Divides the elements of a <see cref="Matrix"/> by the elements of another matrix.
- /// </summary>
- /// <param name="matrix1">Source <see cref="Matrix"/>.</param>
- /// <param name="matrix2">Divisor <see cref="Matrix"/>.</param>
- /// <returns>The result of dividing the matrix.</returns>
- public static Matrix Divide(Matrix matrix1, Matrix matrix2)
- {
- matrix1.M11 = matrix1.M11 / matrix2.M11;
- matrix1.M12 = matrix1.M12 / matrix2.M12;
- matrix1.M13 = matrix1.M13 / matrix2.M13;
- matrix1.M14 = matrix1.M14 / matrix2.M14;
- matrix1.M21 = matrix1.M21 / matrix2.M21;
- matrix1.M22 = matrix1.M22 / matrix2.M22;
- matrix1.M23 = matrix1.M23 / matrix2.M23;
- matrix1.M24 = matrix1.M24 / matrix2.M24;
- matrix1.M31 = matrix1.M31 / matrix2.M31;
- matrix1.M32 = matrix1.M32 / matrix2.M32;
- matrix1.M33 = matrix1.M33 / matrix2.M33;
- matrix1.M34 = matrix1.M34 / matrix2.M34;
- matrix1.M41 = matrix1.M41 / matrix2.M41;
- matrix1.M42 = matrix1.M42 / matrix2.M42;
- matrix1.M43 = matrix1.M43 / matrix2.M43;
- matrix1.M44 = matrix1.M44 / matrix2.M44;
- return matrix1;
- }
- /// <summary>
- /// Divides the elements of a <see cref="Matrix"/> by the elements of another matrix.
- /// </summary>
- /// <param name="matrix1">Source <see cref="Matrix"/>.</param>
- /// <param name="matrix2">Divisor <see cref="Matrix"/>.</param>
- /// <param name="result">The result of dividing the matrix as an output parameter.</param>
- public static void Divide(ref Matrix matrix1, ref Matrix matrix2, out Matrix result)
- {
- result.M11 = matrix1.M11 / matrix2.M11;
- result.M12 = matrix1.M12 / matrix2.M12;
- result.M13 = matrix1.M13 / matrix2.M13;
- result.M14 = matrix1.M14 / matrix2.M14;
- result.M21 = matrix1.M21 / matrix2.M21;
- result.M22 = matrix1.M22 / matrix2.M22;
- result.M23 = matrix1.M23 / matrix2.M23;
- result.M24 = matrix1.M24 / matrix2.M24;
- result.M31 = matrix1.M31 / matrix2.M31;
- result.M32 = matrix1.M32 / matrix2.M32;
- result.M33 = matrix1.M33 / matrix2.M33;
- result.M34 = matrix1.M34 / matrix2.M34;
- result.M41 = matrix1.M41 / matrix2.M41;
- result.M42 = matrix1.M42 / matrix2.M42;
- result.M43 = matrix1.M43 / matrix2.M43;
- result.M44 = matrix1.M44 / matrix2.M44;
- }
- /// <summary>
- /// Divides the elements of a <see cref="Matrix"/> by a scalar.
- /// </summary>
- /// <param name="matrix1">Source <see cref="Matrix"/>.</param>
- /// <param name="divider">Divisor scalar.</param>
- /// <returns>The result of dividing a matrix by a scalar.</returns>
- public static Matrix Divide(Matrix matrix1, float divider)
- {
- float num = 1f / divider;
- matrix1.M11 = matrix1.M11 * num;
- matrix1.M12 = matrix1.M12 * num;
- matrix1.M13 = matrix1.M13 * num;
- matrix1.M14 = matrix1.M14 * num;
- matrix1.M21 = matrix1.M21 * num;
- matrix1.M22 = matrix1.M22 * num;
- matrix1.M23 = matrix1.M23 * num;
- matrix1.M24 = matrix1.M24 * num;
- matrix1.M31 = matrix1.M31 * num;
- matrix1.M32 = matrix1.M32 * num;
- matrix1.M33 = matrix1.M33 * num;
- matrix1.M34 = matrix1.M34 * num;
- matrix1.M41 = matrix1.M41 * num;
- matrix1.M42 = matrix1.M42 * num;
- matrix1.M43 = matrix1.M43 * num;
- matrix1.M44 = matrix1.M44 * num;
- return matrix1;
- }
- /// <summary>
- /// Divides the elements of a <see cref="Matrix"/> by a scalar.
- /// </summary>
- /// <param name="matrix1">Source <see cref="Matrix"/>.</param>
- /// <param name="divider">Divisor scalar.</param>
- /// <param name="result">The result of dividing a matrix by a scalar as an output parameter.</param>
- public static void Divide(ref Matrix matrix1, float divider, out Matrix result)
- {
- float num = 1f / divider;
- result.M11 = matrix1.M11 * num;
- result.M12 = matrix1.M12 * num;
- result.M13 = matrix1.M13 * num;
- result.M14 = matrix1.M14 * num;
- result.M21 = matrix1.M21 * num;
- result.M22 = matrix1.M22 * num;
- result.M23 = matrix1.M23 * num;
- result.M24 = matrix1.M24 * num;
- result.M31 = matrix1.M31 * num;
- result.M32 = matrix1.M32 * num;
- result.M33 = matrix1.M33 * num;
- result.M34 = matrix1.M34 * num;
- result.M41 = matrix1.M41 * num;
- result.M42 = matrix1.M42 * num;
- result.M43 = matrix1.M43 * num;
- result.M44 = matrix1.M44 * num;
- }
- /// <summary>
- /// Compares whether current instance is equal to specified <see cref="Matrix"/> without any tolerance.
- /// </summary>
- /// <param name="other">The <see cref="Matrix"/> to compare.</param>
- /// <returns><c>true</c> if the instances are equal; <c>false</c> otherwise.</returns>
- public bool Equals(Matrix other)
- {
- return ((((((this.M11 == other.M11) && (this.M22 == other.M22)) && ((this.M33 == other.M33) && (this.M44 == other.M44))) && (((this.M12 == other.M12) && (this.M13 == other.M13)) && ((this.M14 == other.M14) && (this.M21 == other.M21)))) && ((((this.M23 == other.M23) && (this.M24 == other.M24)) && ((this.M31 == other.M31) && (this.M32 == other.M32))) && (((this.M34 == other.M34) && (this.M41 == other.M41)) && (this.M42 == other.M42)))) && (this.M43 == other.M43));
- }
- /// <summary>
- /// Compares whether current instance is equal to specified <see cref="Object"/> without any tolerance.
- /// </summary>
- /// <param name="obj">The <see cref="Object"/> to compare.</param>
- /// <returns><c>true</c> if the instances are equal; <c>false</c> otherwise.</returns>
- public override bool Equals(object obj)
- {
- bool flag = false;
- if (obj is Matrix)
- {
- flag = this.Equals((Matrix) obj);
- }
- return flag;
- }
- /// <summary>
- /// Gets the hash code of this <see cref="Matrix"/>.
- /// </summary>
- /// <returns>Hash code of this <see cref="Matrix"/>.</returns>
- public override int GetHashCode()
- {
- return (((((((((((((((this.M11.GetHashCode() + this.M12.GetHashCode()) + this.M13.GetHashCode()) + this.M14.GetHashCode()) + this.M21.GetHashCode()) + this.M22.GetHashCode()) + this.M23.GetHashCode()) + this.M24.GetHashCode()) + this.M31.GetHashCode()) + this.M32.GetHashCode()) + this.M33.GetHashCode()) + this.M34.GetHashCode()) + this.M41.GetHashCode()) + this.M42.GetHashCode()) + this.M43.GetHashCode()) + this.M44.GetHashCode());
- }
- /// <summary>
- /// Creates a new <see cref="Matrix"/> which contains inversion of the specified matrix.
- /// </summary>
- /// <param name="matrix">Source <see cref="Matrix"/>.</param>
- /// <returns>The inverted matrix.</returns>
- public static Matrix Invert(Matrix matrix)
- {
- Invert(ref matrix, out matrix);
- return matrix;
- }
- /// <summary>
- /// Creates a new <see cref="Matrix"/> which contains inversion of the specified matrix.
- /// </summary>
- /// <param name="matrix">Source <see cref="Matrix"/>.</param>
- /// <param name="result">The inverted matrix as output parameter.</param>
- public static void Invert(ref Matrix matrix, out Matrix result)
- {
- float num1 = matrix.M11;
- float num2 = matrix.M12;
- float num3 = matrix.M13;
- float num4 = matrix.M14;
- float num5 = matrix.M21;
- float num6 = matrix.M22;
- float num7 = matrix.M23;
- float num8 = matrix.M24;
- float num9 = matrix.M31;
- float num10 = matrix.M32;
- float num11 = matrix.M33;
- float num12 = matrix.M34;
- float num13 = matrix.M41;
- float num14 = matrix.M42;
- float num15 = matrix.M43;
- float num16 = matrix.M44;
- float num17 = (float) ((double) num11 * (double) num16 - (double) num12 * (double) num15);
- float num18 = (float) ((double) num10 * (double) num16 - (double) num12 * (double) num14);
- float num19 = (float) ((double) num10 * (double) num15 - (double) num11 * (double) num14);
- float num20 = (float) ((double) num9 * (double) num16 - (double) num12 * (double) num13);
- float num21 = (float) ((double) num9 * (double) num15 - (double) num11 * (double) num13);
- float num22 = (float) ((double) num9 * (double) num14 - (double) num10 * (double) num13);
- float num23 = (float) ((double) num6 * (double) num17 - (double) num7 * (double) num18 + (double) num8 * (double) num19);
- float num24 = (float) -((double) num5 * (double) num17 - (double) num7 * (double) num20 + (double) num8 * (double) num21);
- float num25 = (float) ((double) num5 * (double) num18 - (double) num6 * (double) num20 + (double) num8 * (double) num22);
- float num26 = (float) -((double) num5 * (double) num19 - (double) num6 * (double) num21 + (double) num7 * (double) num22);
- float num27 = (float) (1.0 / ((double) num1 * (double) num23 + (double) num2 * (double) num24 + (double) num3 * (double) num25 + (double) num4 * (double) num26));
-
- result.M11 = num23 * num27;
- result.M21 = num24 * num27;
- result.M31 = num25 * num27;
- result.M41 = num26 * num27;
- result.M12 = (float) -((double) num2 * (double) num17 - (double) num3 * (double) num18 + (double) num4 * (double) num19) * num27;
- result.M22 = (float) ((double) num1 * (double) num17 - (double) num3 * (double) num20 + (double) num4 * (double) num21) * num27;
- result.M32 = (float) -((double) num1 * (double) num18 - (double) num2 * (double) num20 + (double) num4 * (double) num22) * num27;
- result.M42 = (float) ((double) num1 * (double) num19 - (double) num2 * (double) num21 + (double) num3 * (double) num22) * num27;
- float num28 = (float) ((double) num7 * (double) num16 - (double) num8 * (double) num15);
- float num29 = (float) ((double) num6 * (double) num16 - (double) num8 * (double) num14);
- float num30 = (float) ((double) num6 * (double) num15 - (double) num7 * (double) num14);
- float num31 = (float) ((double) num5 * (double) num16 - (double) num8 * (double) num13);
- float num32 = (float) ((double) num5 * (double) num15 - (double) num7 * (double) num13);
- float num33 = (float) ((double) num5 * (double) num14 - (double) num6 * (double) num13);
- result.M13 = (float) ((double) num2 * (double) num28 - (double) num3 * (double) num29 + (double) num4 * (double) num30) * num27;
- result.M23 = (float) -((double) num1 * (double) num28 - (double) num3 * (double) num31 + (double) num4 * (double) num32) * num27;
- result.M33 = (float) ((double) num1 * (double) num29 - (double) num2 * (double) num31 + (double) num4 * (double) num33) * num27;
- result.M43 = (float) -((double) num1 * (double) num30 - (double) num2 * (double) num32 + (double) num3 * (double) num33) * num27;
- float num34 = (float) ((double) num7 * (double) num12 - (double) num8 * (double) num11);
- float num35 = (float) ((double) num6 * (double) num12 - (double) num8 * (double) num10);
- float num36 = (float) ((double) num6 * (double) num11 - (double) num7 * (double) num10);
- float num37 = (float) ((double) num5 * (double) num12 - (double) num8 * (double) num9);
- float num38 = (float) ((double) num5 * (double) num11 - (double) num7 * (double) num9);
- float num39 = (float) ((double) num5 * (double) num10 - (double) num6 * (double) num9);
- result.M14 = (float) -((double) num2 * (double) num34 - (double) num3 * (double) num35 + (double) num4 * (double) num36) * num27;
- result.M24 = (float) ((double) num1 * (double) num34 - (double) num3 * (double) num37 + (double) num4 * (double) num38) * num27;
- result.M34 = (float) -((double) num1 * (double) num35 - (double) num2 * (double) num37 + (double) num4 * (double) num39) * num27;
- result.M44 = (float) ((double) num1 * (double) num36 - (double) num2 * (double) num38 + (double) num3 * (double) num39) * num27;
-
-
- /*
-
-
- ///
- // Use Laplace expansion theorem to calculate the inverse of a 4x4 matrix
- //
- // 1. Calculate the 2x2 determinants needed the 4x4 determinant based on the 2x2 determinants
- // 3. Create the adjugate matrix, which satisfies: A * adj(A) = det(A) * I
- // 4. Divide adjugate matrix with the determinant to find the inverse
-
- float det1, det2, det3, det4, det5, det6, det7, det8, det9, det10, det11, det12;
- float detMatrix;
- FindDeterminants(ref matrix, out detMatrix, out det1, out det2, out det3, out det4, out det5, out det6,
- out det7, out det8, out det9, out det10, out det11, out det12);
-
- float invDetMatrix = 1f / detMatrix;
-
- Matrix ret; // Allow for matrix and result to point to the same structure
-
- ret.M11 = (matrix.M22*det12 - matrix.M23*det11 + matrix.M24*det10) * invDetMatrix;
- ret.M12 = (-matrix.M12*det12 + matrix.M13*det11 - matrix.M14*det10) * invDetMatrix;
- ret.M13 = (matrix.M42*det6 - matrix.M43*det5 + matrix.M44*det4) * invDetMatrix;
- ret.M14 = (-matrix.M32*det6 + matrix.M33*det5 - matrix.M34*det4) * invDetMatrix;
- ret.M21 = (-matrix.M21*det12 + matrix.M23*det9 - matrix.M24*det8) * invDetMatrix;
- ret.M22 = (matrix.M11*det12 - matrix.M13*det9 + matrix.M14*det8) * invDetMatrix;
- ret.M23 = (-matrix.M41*det6 + matrix.M43*det3 - matrix.M44*det2) * invDetMatrix;
- ret.M24 = (matrix.M31*det6 - matrix.M33*det3 + matrix.M34*det2) * invDetMatrix;
- ret.M31 = (matrix.M21*det11 - matrix.M22*det9 + matrix.M24*det7) * invDetMatrix;
- ret.M32 = (-matrix.M11*det11 + matrix.M12*det9 - matrix.M14*det7) * invDetMatrix;
- ret.M33 = (matrix.M41*det5 - matrix.M42*det3 + matrix.M44*det1) * invDetMatrix;
- ret.M34 = (-matrix.M31*det5 + matrix.M32*det3 - matrix.M34*det1) * invDetMatrix;
- ret.M41 = (-matrix.M21*det10 + matrix.M22*det8 - matrix.M23*det7) * invDetMatrix;
- ret.M42 = (matrix.M11*det10 - matrix.M12*det8 + matrix.M13*det7) * invDetMatrix;
- ret.M43 = (-matrix.M41*det4 + matrix.M42*det2 - matrix.M43*det1) * invDetMatrix;
- ret.M44 = (matrix.M31*det4 - matrix.M32*det2 + matrix.M33*det1) * invDetMatrix;
-
- result = ret;
- */
- }
- /// <summary>
- /// Creates a new <see cref="Matrix"/> that contains linear interpolation of the values in specified matrixes.
- /// </summary>
- /// <param name="matrix1">The first <see cref="Matrix"/>.</param>
- /// <param name="matrix2">The second <see cref="Vector2"/>.</param>
- /// <param name="amount">Weighting value(between 0.0 and 1.0).</param>
- /// <returns>>The result of linear interpolation of the specified matrixes.</returns>
- public static Matrix Lerp(Matrix matrix1, Matrix matrix2, float amount)
- {
- matrix1.M11 = matrix1.M11 + ((matrix2.M11 - matrix1.M11) * amount);
- matrix1.M12 = matrix1.M12 + ((matrix2.M12 - matrix1.M12) * amount);
- matrix1.M13 = matrix1.M13 + ((matrix2.M13 - matrix1.M13) * amount);
- matrix1.M14 = matrix1.M14 + ((matrix2.M14 - matrix1.M14) * amount);
- matrix1.M21 = matrix1.M21 + ((matrix2.M21 - matrix1.M21) * amount);
- matrix1.M22 = matrix1.M22 + ((matrix2.M22 - matrix1.M22) * amount);
- matrix1.M23 = matrix1.M23 + ((matrix2.M23 - matrix1.M23) * amount);
- matrix1.M24 = matrix1.M24 + ((matrix2.M24 - matrix1.M24) * amount);
- matrix1.M31 = matrix1.M31 + ((matrix2.M31 - matrix1.M31) * amount);
- matrix1.M32 = matrix1.M32 + ((matrix2.M32 - matrix1.M32) * amount);
- matrix1.M33 = matrix1.M33 + ((matrix2.M33 - matrix1.M33) * amount);
- matrix1.M34 = matrix1.M34 + ((matrix2.M34 - matrix1.M34) * amount);
- matrix1.M41 = matrix1.M41 + ((matrix2.M41 - matrix1.M41) * amount);
- matrix1.M42 = matrix1.M42 + ((matrix2.M42 - matrix1.M42) * amount);
- matrix1.M43 = matrix1.M43 + ((matrix2.M43 - matrix1.M43) * amount);
- matrix1.M44 = matrix1.M44 + ((matrix2.M44 - matrix1.M44) * amount);
- return matrix1;
- }
- /// <summary>
- /// Creates a new <see cref="Matrix"/> that contains linear interpolation of the values in specified matrixes.
- /// </summary>
- /// <param name="matrix1">The first <see cref="Matrix"/>.</param>
- /// <param name="matrix2">The second <see cref="Vector2"/>.</param>
- /// <param name="amount">Weighting value(between 0.0 and 1.0).</param>
- /// <param name="result">The result of linear interpolation of the specified matrixes as an output parameter.</param>
- public static void Lerp(ref Matrix matrix1, ref Matrix matrix2, float amount, out Matrix result)
- {
- result.M11 = matrix1.M11 + ((matrix2.M11 - matrix1.M11) * amount);
- result.M12 = matrix1.M12 + ((matrix2.M12 - matrix1.M12) * amount);
- result.M13 = matrix1.M13 + ((matrix2.M13 - matrix1.M13) * amount);
- result.M14 = matrix1.M14 + ((matrix2.M14 - matrix1.M14) * amount);
- result.M21 = matrix1.M21 + ((matrix2.M21 - matrix1.M21) * amount);
- result.M22 = matrix1.M22 + ((matrix2.M22 - matrix1.M22) * amount);
- result.M23 = matrix1.M23 + ((matrix2.M23 - matrix1.M23) * amount);
- result.M24 = matrix1.M24 + ((matrix2.M24 - matrix1.M24) * amount);
- result.M31 = matrix1.M31 + ((matrix2.M31 - matrix1.M31) * amount);
- result.M32 = matrix1.M32 + ((matrix2.M32 - matrix1.M32) * amount);
- result.M33 = matrix1.M33 + ((matrix2.M33 - matrix1.M33) * amount);
- result.M34 = matrix1.M34 + ((matrix2.M34 - matrix1.M34) * amount);
- result.M41 = matrix1.M41 + ((matrix2.M41 - matrix1.M41) * amount);
- result.M42 = matrix1.M42 + ((matrix2.M42 - matrix1.M42) * amount);
- result.M43 = matrix1.M43 + ((matrix2.M43 - matrix1.M43) * amount);
- result.M44 = matrix1.M44 + ((matrix2.M44 - matrix1.M44) * amount);
- }
- /// <summary>
- /// Creates a new <see cref="Matrix"/> that contains a multiplication of two matrix.
- /// </summary>
- /// <param name="matrix1">Source <see cref="Matrix"/>.</param>
- /// <param name="matrix2">Source <see cref="Matrix"/>.</param>
- /// <returns>Result of the matrix multiplication.</returns>
- public static Matrix Multiply(Matrix matrix1, Matrix matrix2)
- {
- var m11 = (((matrix1.M11 * matrix2.M11) + (matrix1.M12 * matrix2.M21)) + (matrix1.M13 * matrix2.M31)) + (matrix1.M14 * matrix2.M41);
- var m12 = (((matrix1.M11 * matrix2.M12) + (matrix1.M12 * matrix2.M22)) + (matrix1.M13 * matrix2.M32)) + (matrix1.M14 * matrix2.M42);
- var m13 = (((matrix1.M11 * matrix2.M13) + (matrix1.M12 * matrix2.M23)) + (matrix1.M13 * matrix2.M33)) + (matrix1.M14 * matrix2.M43);
- var m14 = (((matrix1.M11 * matrix2.M14) + (matrix1.M12 * matrix2.M24)) + (matrix1.M13 * matrix2.M34)) + (matrix1.M14 * matrix2.M44);
- var m21 = (((matrix1.M21 * matrix2.M11) + (matrix1.M22 * matrix2.M21)) + (matrix1.M23 * matrix2.M31)) + (matrix1.M24 * matrix2.M41);
- var m22 = (((matrix1.M21 * matrix2.M12) + (matrix1.M22 * matrix2.M22)) + (matrix1.M23 * matrix2.M32)) + (matrix1.M24 * matrix2.M42);
- var m23 = (((matrix1.M21 * matrix2.M13) + (matrix1.M22 * matrix2.M23)) + (matrix1.M23 * matrix2.M33)) + (matrix1.M24 * matrix2.M43);
- var m24 = (((matrix1.M21 * matrix2.M14) + (matrix1.M22 * matrix2.M24)) + (matrix1.M23 * matrix2.M34)) + (matrix1.M24 * matrix2.M44);
- var m31 = (((matrix1.M31 * matrix2.M11) + (matrix1.M32 * matrix2.M21)) + (matrix1.M33 * matrix2.M31)) + (matrix1.M34 * matrix2.M41);
- var m32 = (((matrix1.M31 * matrix2.M12) + (matrix1.M32 * matrix2.M22)) + (matrix1.M33 * matrix2.M32)) + (matrix1.M34 * matrix2.M42);
- var m33 = (((matrix1.M31 * matrix2.M13) + (matrix1.M32 * matrix2.M23)) + (matrix1.M33 * matrix2.M33)) + (matrix1.M34 * matrix2.M43);
- var m34 = (((matrix1.M31 * matrix2.M14) + (matrix1.M32 * matrix2.M24)) + (matrix1.M33 * matrix2.M34)) + (matrix1.M34 * matrix2.M44);
- var m41 = (((matrix1.M41 * matrix2.M11) + (matrix1.M42 * matrix2.M21)) + (matrix1.M43 * matrix2.M31)) + (matrix1.M44 * matrix2.M41);
- var m42 = (((matrix1.M41 * matrix2.M12) + (matrix1.M42 * matrix2.M22)) + (matrix1.M43 * matrix2.M32)) + (matrix1.M44 * matrix2.M42);
- var m43 = (((matrix1.M41 * matrix2.M13) + (matrix1.M42 * matrix2.M23)) + (matrix1.M43 * matrix2.M33)) + (matrix1.M44 * matrix2.M43);
- var m44 = (((matrix1.M41 * matrix2.M14) + (matrix1.M42 * matrix2.M24)) + (matrix1.M43 * matrix2.M34)) + (matrix1.M44 * matrix2.M44);
- matrix1.M11 = m11;
- matrix1.M12 = m12;
- matrix1.M13 = m13;
- matrix1.M14 = m14;
- matrix1.M21 = m21;
- matrix1.M22 = m22;
- matrix1.M23 = m23;
- matrix1.M24 = m24;
- matrix1.M31 = m31;
- matrix1.M32 = m32;
- matrix1.M33 = m33;
- matrix1.M34 = m34;
- matrix1.M41 = m41;
- matrix1.M42 = m42;
- matrix1.M43 = m43;
- matrix1.M44 = m44;
- return matrix1;
- }
- /// <summary>
- /// Creates a new <see cref="Matrix"/> that contains a multiplication of two matrix.
- /// </summary>
- /// <param name="matrix1">Source <see cref="Matrix"/>.</param>
- /// <param name="matrix2">Source <see cref="Matrix"/>.</param>
- /// <param name="result">Result of the matrix multiplication as an output parameter.</param>
- public static void Multiply(ref Matrix matrix1, ref Matrix matrix2, out Matrix result)
- {
- var m11 = (((matrix1.M11 * matrix2.M11) + (matrix1.M12 * matrix2.M21)) + (matrix1.M13 * matrix2.M31)) + (matrix1.M14 * matrix2.M41);
- var m12 = (((matrix1.M11 * matrix2.M12) + (matrix1.M12 * matrix2.M22)) + (matrix1.M13 * matrix2.M32)) + (matrix1.M14 * matrix2.M42);
- var m13 = (((matrix1.M11 * matrix2.M13) + (matrix1.M12 * matrix2.M23)) + (matrix1.M13 * matrix2.M33)) + (matrix1.M14 * matrix2.M43);
- var m14 = (((matrix1.M11 * matrix2.M14) + (matrix1.M12 * matrix2.M24)) + (matrix1.M13 * matrix2.M34)) + (matrix1.M14 * matrix2.M44);
- var m21 = (((matrix1.M21 * matrix2.M11) + (matrix1.M22 * matrix2.M21)) + (matrix1.M23 * matrix2.M31)) + (matrix1.M24 * matrix2.M41);
- var m22 = (((matrix1.M21 * matrix2.M12) + (matrix1.M22 * matrix2.M22)) + (matrix1.M23 * matrix2.M32)) + (matrix1.M24 * matrix2.M42);
- var m23 = (((matrix1.M21 * matrix2.M13) + (matrix1.M22 * matrix2.M23)) + (matrix1.M23 * matrix2.M33)) + (matrix1.M24 * matrix2.M43);
- var m24 = (((matrix1.M21 * matrix2.M14) + (matrix1.M22 * matrix2.M24)) + (matrix1.M23 * matrix2.M34)) + (matrix1.M24 * matrix2.M44);
- var m31 = (((matrix1.M31 * matrix2.M11) + (matrix1.M32 * matrix2.M21)) + (matrix1.M33 * matrix2.M31)) + (matrix1.M34 * matrix2.M41);
- var m32 = (((matrix1.M31 * matrix2.M12) + (matrix1.M32 * matrix2.M22)) + (matrix1.M33 * matrix2.M32)) + (matrix1.M34 * matrix2.M42);
- var m33 = (((matrix1.M31 * matrix2.M13) + (matrix1.M32 * matrix2.M23)) + (matrix1.M33 * matrix2.M33)) + (matrix1.M34 * matrix2.M43);
- var m34 = (((matrix1.M31 * matrix2.M14) + (matrix1.M32 * matrix2.M24)) + (matrix1.M33 * matrix2.M34)) + (matrix1.M34 * matrix2.M44);
- var m41 = (((matrix1.M41 * matrix2.M11) + (matrix1.M42 * matrix2.M21)) + (matrix1.M43 * matrix2.M31)) + (matrix1.M44 * matrix2.M41);
- var m42 = (((matrix1.M41 * matrix2.M12) + (matrix1.M42 * matrix2.M22)) + (matrix1.M43 * matrix2.M32)) + (matrix1.M44 * matrix2.M42);
- var m43 = (((matrix1.M41 * matrix2.M13) + (matrix1.M42 * matrix2.M23)) + (matrix1.M43 * matrix2.M33)) + (matrix1.M44 * matrix2.M43);
- var m44 = (((matrix1.M41 * matrix2.M14) + (matrix1.M42 * matrix2.M24)) + (matrix1.M43 * matrix2.M34)) + (matrix1.M44 * matrix2.M44);
- result.M11 = m11;
- result.M12 = m12;
- result.M13 = m13;
- result.M14 = m14;
- result.M21 = m21;
- result.M22 = m22;
- result.M23 = m23;
- result.M24 = m24;
- result.M31 = m31;
- result.M32 = m32;
- result.M33 = m33;
- result.M34 = m34;
- result.M41 = m41;
- result.M42 = m42;
- result.M43 = m43;
- result.M44 = m44;
- }
- /// <summary>
- /// Creates a new <see cref="Matrix"/> that contains a multiplication of <see cref="Matrix"/> and a scalar.
- /// </summary>
- /// <param name="matrix1">Source <see cref="Matrix"/>.</param>
- /// <param name="scaleFactor">Scalar value.</param>
- /// <returns>Result of the matrix multiplication with a scalar.</returns>
- public static Matrix Multiply(Matrix matrix1, float scaleFactor)
- {
- matrix1.M11 *= scaleFactor;
- matrix1.M12 *= scaleFactor;
- matrix1.M13 *= scaleFactor;
- matrix1.M14 *= scaleFactor;
- matrix1.M21 *= scaleFactor;
- matrix1.M22 *= scaleFactor;
- matrix1.M23 *= scaleFactor;
- matrix1.M24 *= scaleFactor;
- matrix1.M31 *= scaleFactor;
- matrix1.M32 *= scaleFactor;
- matrix1.M33 *= scaleFactor;
- matrix1.M34 *= scaleFactor;
- matrix1.M41 *= scaleFactor;
- matrix1.M42 *= scaleFactor;
- matrix1.M43 *= scaleFactor;
- matrix1.M44 *= scaleFactor;
- return matrix1;
- }
- /// <summary>
- /// Creates a new <see cref="Matrix"/> that contains a multiplication of <see cref="Matrix"/> and a scalar.
- /// </summary>
- /// <param name="matrix1">Source <see cref="Matrix"/>.</param>
- /// <param name="scaleFactor">Scalar value.</param>
- /// <param name="result">Result of the matrix multiplication with a scalar as an output parameter.</param>
- public static void Multiply(ref Matrix matrix1, float scaleFactor, out Matrix result)
- {
- result.M11 = matrix1.M11 * scaleFactor;
- result.M12 = matrix1.M12 * scaleFactor;
- result.M13 = matrix1.M13 * scaleFactor;
- result.M14 = matrix1.M14 * scaleFactor;
- result.M21 = matrix1.M21 * scaleFactor;
- result.M22 = matrix1.M22 * scaleFactor;
- result.M23 = matrix1.M23 * scaleFactor;
- result.M24 = matrix1.M24 * scaleFactor;
- result.M31 = matrix1.M31 * scaleFactor;
- result.M32 = matrix1.M32 * scaleFactor;
- result.M33 = matrix1.M33 * scaleFactor;
- result.M34 = matrix1.M34 * scaleFactor;
- result.M41 = matrix1.M41 * scaleFactor;
- result.M42 = matrix1.M42 * scaleFactor;
- result.M43 = matrix1.M43 * scaleFactor;
- result.M44 = matrix1.M44 * scaleFactor;
- }
- /// <summary>
- /// Copy the values of specified <see cref="Matrix"/> to the float array.
- /// </summary>
- /// <param name="matrix">The source <see cref="Matrix"/>.</param>
- /// <returns>The array which matrix values will be stored.</returns>
- /// <remarks>
- /// Required for OpenGL 2.0 projection matrix stuff.
- /// </remarks>
- public static float[] ToFloatArray(Matrix matrix)
- {
- float[] matarray = {
- matrix.M11, matrix.M12, matrix.M13, matrix.M14,
- matrix.M21, matrix.M22, matrix.M23, matrix.M24,
- matrix.M31, matrix.M32, matrix.M33, matrix.M34,
- matrix.M41, matrix.M42, matrix.M43, matrix.M44
- };
- return matarray;
- }
- /// <summary>
- /// Returns a matrix with the all values negated.
- /// </summary>
- /// <param name="matrix">Source <see cref="Matrix"/>.</param>
- /// <returns>Result of the matrix negation.</returns>
- public static Matrix Negate(Matrix matrix)
- {
- matrix.M11 = -matrix.M11;
- matrix.M12 = -matrix.M12;
- matrix.M13 = -matrix.M13;
- matrix.M14 = -matrix.M14;
- matrix.M21 = -matrix.M21;
- matrix.M22 = -matrix.M22;
- matrix.M23 = -matrix.M23;
- matrix.M24 = -matrix.M24;
- matrix.M31 = -matrix.M31;
- matrix.M32 = -matrix.M32;
- matrix.M33 = -matrix.M33;
- matrix.M34 = -matrix.M34;
- matrix.M41 = -matrix.M41;
- matrix.M42 = -matrix.M42;
- matrix.M43 = -matrix.M43;
- matrix.M44 = -matrix.M44;
- return matrix;
- }
- /// <summary>
- /// Returns a matrix with the all values negated.
- /// </summary>
- /// <param name="matrix">Source <see cref="Matrix"/>.</param>
- /// <param name="result">Result of the matrix negation as an output parameter.</param>
- public static void Negate(ref Matrix matrix, out Matrix result)
- {
- result.M11 = -matrix.M11;
- result.M12 = -matrix.M12;
- result.M13 = -matrix.M13;
- result.M14 = -matrix.M14;
- result.M21 = -matrix.M21;
- result.M22 = -matrix.M22;
- result.M23 = -matrix.M23;
- result.M24 = -matrix.M24;
- result.M31 = -matrix.M31;
- result.M32 = -matrix.M32;
- result.M33 = -matrix.M33;
- result.M34 = -matrix.M34;
- result.M41 = -matrix.M41;
- result.M42 = -matrix.M42;
- result.M43 = -matrix.M43;
- result.M44 = -matrix.M44;
- }
- /// <summary>
- /// Adds two matrixes.
- /// </summary>
- /// <param name="matrix1">Source <see cref="Matrix"/> on the left of the add sign.</param>
- /// <param name="matrix2">Source <see cref="Matrix"/> on the right of the add sign.</param>
- /// <returns>Sum of the matrixes.</returns>
- public static Matrix operator +(Matrix matrix1, Matrix matrix2)
- {
- matrix1.M11 = matrix1.M11 + matrix2.M11;
- matrix1.M12 = matrix1.M12 + matrix2.M12;
- matrix1.M13 = matrix1.M13 + matrix2.M13;
- matrix1.M14 = matrix1.M14 + matrix2.M14;
- matrix1.M21 = matrix1.M21 + matrix2.M21;
- matrix1.M22 = matrix1.M22 + matrix2.M22;
- matrix1.M23 = matrix1.M23 + matrix2.M23;
- matrix1.M24 = matrix1.M24 + matrix2.M24;
- matrix1.M31 = matrix1.M31 + matrix2.M31;
- matrix1.M32 = matrix1.M32 + matrix2.M32;
- matrix1.M33 = matrix1.M33 + matrix2.M33;
- matrix1.M34 = matrix1.M34 + matrix2.M34;
- matrix1.M41 = matrix1.M41 + matrix2.M41;
- matrix1.M42 = matrix1.M42 + matrix2.M42;
- matrix1.M43 = matrix1.M43 + matrix2.M43;
- matrix1.M44 = matrix1.M44 + matrix2.M44;
- return matrix1;
- }
- /// <summary>
- /// Divides the elements of a <see cref="Matrix"/> by the elements of another <see cref="Matrix"/>.
- /// </summary>
- /// <param name="matrix1">Source <see cref="Matrix"/> on the left of the div sign.</param>
- /// <param name="matrix2">Divisor <see cref="Matrix"/> on the right of the div sign.</param>
- /// <returns>The result of dividing the matrixes.</returns>
- public static Matrix operator /(Matrix matrix1, Matrix matrix2)
- {
- matrix1.M11 = matrix1.M11 / matrix2.M11;
- matrix1.M12 = matrix1.M12 / matrix2.M12;
- matrix1.M13 = matrix1.M13 / matrix2.M13;
- matrix1.M14 = matrix1.M14 / matrix2.M14;
- matrix1.M21 = matrix1.M21 / matrix2.M21;
- matrix1.M22 = matrix1.M22 / matrix2.M22;
- matrix1.M23 = matrix1.M23 / matrix2.M23;
- matrix1.M24 = matrix1.M24 / matrix2.M24;
- matrix1.M31 = matrix1.M31 / matrix2.M31;
- matrix1.M32 = matrix1.M32 / matrix2.M32;
- matrix1.M33 = matrix1.M33 / matrix2.M33;
- matrix1.M34 = matrix1.M34 / matrix2.M34;
- matrix1.M41 = matrix1.M41 / matrix2.M41;
- matrix1.M42 = matrix1.M42 / matrix2.M42;
- matrix1.M43 = matrix1.M43 / matrix2.M43;
- matrix1.M44 = matrix1.M44 / matrix2.M44;
- return matrix1;
- }
- /// <summary>
- /// Divides the elements of a <see cref="Matrix"/> by a scalar.
- /// </summary>
- /// <param name="matrix">Source <see cref="Matrix"/> on the left of the div sign.</param>
- /// <param name="divider">Divisor scalar on the right of the div sign.</param>
- /// <returns>The result of dividing a matrix by a scalar.</returns>
- public static Matrix operator /(Matrix matrix, float divider)
- {
- float num = 1f / divider;
- matrix.M11 = matrix.M11 * num;
- matrix.M12 = matrix.M12 * num;
- matrix.M13 = matrix.M13 * num;
- matrix.M14 = matrix.M14 * num;
- matrix.M21 = matrix.M21 * num;
- matrix.M22 = matrix.M22 * num;
- matrix.M23 = matrix.M23 * num;
- matrix.M24 = matrix.M24 * num;
- matrix.M31 = matrix.M31 * num;
- matrix.M32 = matrix.M32 * num;
- matrix.M33 = matrix.M33 * num;
- matrix.M34 = matrix.M34 * num;
- matrix.M41 = matrix.M41 * num;
- matrix.M42 = matrix.M42 * num;
- matrix.M43 = matrix.M43 * num;
- matrix.M44 = matrix.M44 * num;
- return matrix;
- }
- /// <summary>
- /// Compares whether two <see cref="Matrix"/> instances are equal without any tolerance.
- /// </summary>
- /// <param name="matrix1">Source <see cref="Matrix"/> on the left of the equal sign.</param>
- /// <param name="matrix2">Source <see cref="Matrix"/> on the right of the equal sign.</param>
- /// <returns><c>true</c> if the instances are equal; <c>false</c> otherwise.</returns>
- public static bool operator ==(Matrix matrix1, Matrix matrix2)
- {
- return (
- matrix1.M11 == matrix2.M11 &&
- matrix1.M12 == matrix2.M12 &&
- matrix1.M13 == matrix2.M13 &&
- matrix1.M14 == matrix2.M14 &&
- matrix1.M21 == matrix2.M21 &&
- matrix1.M22 == matrix2.M22 &&
- matrix1.M23 == matrix2.M23 &&
- matrix1.M24 == matrix2.M24 &&
- matrix1.M31 == matrix2.M31 &&
- matrix1.M32 == matrix2.M32 &&
- matrix1.M33 == matrix2.M33 &&
- matrix1.M34 == matrix2.M34 &&
- matrix1.M41 == matrix2.M41 &&
- matrix1.M42 == matrix2.M42 &&
- matrix1.M43 == matrix2.M43 &&
- matrix1.M44 == matrix2.M44
- );
- }
- /// <summary>
- /// Compares whether two <see cref="Matrix"/> instances are not equal without any tolerance.
- /// </summary>
- /// <param name="matrix1">Source <see cref="Matrix"/> on the left of the not equal sign.</param>
- /// <param name="matrix2">Source <see cref="Matrix"/> on the right of the not equal sign.</param>
- /// <returns><c>true</c> if the instances are not equal; <c>false</c> otherwise.</returns>
- public static bool operator !=(Matrix matrix1, Matrix matrix2)
- {
- return (
- matrix1.M11 != matrix2.M11 ||
- matrix1.M12 != matrix2.M12 ||
- matrix1.M13 != matrix2.M13 ||
- matrix1.M14 != matrix2.M14 ||
- matrix1.M21 != matrix2.M21 ||
- matrix1.M22 != matrix2.M22 ||
- matrix1.M23 != matrix2.M23 ||
- matrix1.M24 != matrix2.M24 ||
- matrix1.M31 != matrix2.M31 ||
- matrix1.M32 != matrix2.M32 ||
- matrix1.M33 != matrix2.M33 ||
- matrix1.M34 != matrix2.M34 ||
- matrix1.M41 != matrix2.M41 ||
- matrix1.M42 != matrix2.M42 ||
- matrix1.M43 != matrix2.M43 ||
- matrix1.M44 != matrix2.M44
- );
- }
- /// <summary>
- /// Multiplies two matrixes.
- /// </summary>
- /// <param name="matrix1">Source <see cref="Matrix"/> on the left of the mul sign.</param>
- /// <param name="matrix2">Source <see cref="Matrix"/> on the right of the mul sign.</param>
- /// <returns>Result of the matrix multiplication.</returns>
- /// <remarks>
- /// Using matrix multiplication algorithm - see http://en.wikipedia.org/wiki/Matrix_multiplication.
- /// </remarks>
- public static Matrix operator *(Matrix matrix1, Matrix matrix2)
- {
- var m11 = (((matrix1.M11 * matrix2.M11) + (matrix1.M12 * matrix2.M21)) + (matrix1.M13 * matrix2.M31)) + (matrix1.M14 * matrix2.M41);
- var m12 = (((matrix1.M11 * matrix2.M12) + (matrix1.M12 * matrix2.M22)) + (matrix1.M13 * matrix2.M32)) + (matrix1.M14 * matrix2.M42);
- var m13 = (((matrix1.M11 * matrix2.M13) + (matrix1.M12 * matrix2.M23)) + (matrix1.M13 * matrix2.M33)) + (matrix1.M14 * matrix2.M43);
- var m14 = (((matrix1.M11 * matrix2.M14) + (matrix1.M12 * matrix2.M24)) + (matrix1.M13 * matrix2.M34)) + (matrix1.M14 * matrix2.M44);
- var m21 = (((matrix1.M21 * matrix2.M11) + (matrix1.M22 * matrix2.M21)) + (matrix1.M23 * matrix2.M31)) + (matrix1.M24 * matrix2.M41);
- var m22 = (((matrix1.M21 * matrix2.M12) + (matrix1.M22 * matrix2.M22)) + (matrix1.M23 * matrix2.M32)) + (matrix1.M24 * matrix2.M42);
- var m23 = (((matrix1.M21 * matrix2.M13) + (matrix1.M22 * matrix2.M23)) + (matrix1.M23 * matrix2.M33)) + (matrix1.M24 * matrix2.M43);
- var m24 = (((matrix1.M21 * matrix2.M14) + (matrix1.M22 * matrix2.M24)) + (matrix1.M23 * matrix2.M34)) + (matrix1.M24 * matrix2.M44);
- var m31 = (((matrix1.M31 * matrix2.M11) + (matrix1.M32 * matrix2.M21)) + (matrix1.M33 * matrix2.M31)) + (matrix1.M34 * matrix2.M41);
- var m32 = (((matrix1.M31 * matrix2.M12) + (matrix1.M32 * matrix2.M22)) + (matrix1.M33 * matrix2.M32)) + (matrix1.M34 * matrix2.M42);
- var m33 = (((matrix1.M31 * matrix2.M13) + (matrix1.M32 * matrix2.M23)) + (matrix1.M33 * matrix2.M33)) + (matrix1.M34 * matrix2.M43);
- var m34 = (((matrix1.M31 * matrix2.M14) + (matrix1.M32 * matrix2.M24)) + (matrix1.M33 * matrix2.M34)) + (matrix1.M34 * matrix2.M44);
- var m41 = (((matrix1.M41 * matrix2.M11) + (matrix1.M42 * matrix2.M21)) + (matrix1.M43 * matrix2.M31)) + (matrix1.M44 * matrix2.M41);
- var m42 = (((matrix1.M41 * matrix2.M12) + (matrix1.M42 * matrix2.M22)) + (matrix1.M43 * matrix2.M32)) + (matrix1.M44 * matrix2.M42);
- var m43 = (((matrix1.M41 * matrix2.M13) + (matrix1.M42 * matrix2.M23)) + (matrix1.M43 * matrix2.M33)) + (matrix1.M44 * matrix2.M43);
- var m44 = (((matrix1.M41 * matrix2.M14) + (matrix1.M42 * matrix2.M24)) + (matrix1.M43 * matrix2.M34)) + (matrix1.M44 * matrix2.M44);
- matrix1.M11 = m11;
- matrix1.M12 = m12;
- matrix1.M13 = m13;
- matrix1.M14 = m14;
- matrix1.M21 = m21;
- matrix1.M22 = m22;
- matrix1.M23 = m23;
- matrix1.M24 = m24;
- matrix1.M31 = m31;
- matrix1.M32 = m32;
- matrix1.M33 = m33;
- matrix1.M34 = m34;
- matrix1.M41 = m41;
- matrix1.M42 = m42;
- matrix1.M43 = m43;
- matrix1.M44 = m44;
- return matrix1;
- }
- /// <summary>
- /// Multiplies the elements of matrix by a scalar.
- /// </summary>
- /// <param name="matrix">Source <see cref="Matrix"/> on the left of the mul sign.</param>
- /// <param name="scaleFactor">Scalar value on the right of the mul sign.</param>
- /// <returns>Result of the matrix multiplication with a scalar.</returns>
- public static Matrix operator *(Matrix matrix, float scaleFactor)
- {
- matrix.M11 = matrix.M11 * scaleFactor;
- matrix.M12 = matrix.M12 * scaleFactor;
- matrix.M13 = matrix.M13 * scaleFactor;
- matrix.M14 = matrix.M14 * scaleFactor;
- matrix.M21 = matrix.M21 * scaleFactor;
- matrix.M22 = matrix.M22 * scaleFactor;
- matrix.M23 = matrix.M23 * scaleFactor;
- matrix.M24 = matrix.M24 * scaleFactor;
- matrix.M31 = matrix.M31 * scaleFactor;
- matrix.M32 = matrix.M32 * scaleFactor;
- matrix.M33 = matrix.M33 * scaleFactor;
- matrix.M34 = matrix.M34 * scaleFactor;
- matrix.M41 = matrix.M41 * scaleFactor;
- matrix.M42 = matrix.M42 * scaleFactor;
- matrix.M43 = matrix.M43 * scaleFactor;
- matrix.M44 = matrix.M44 * scaleFactor;
- return matrix;
- }
- /// <summary>
- /// Subtracts the values of one <see cref="Matrix"/> from another <see cref="Matrix"/>.
- /// </summary>
- /// <param name="matrix1">Source <see cref="Matrix"/> on the left of the sub sign.</param>
- /// <param name="matrix2">Source <see cref="Matrix"/> on the right of the sub sign.</param>
- /// <returns>Result of the matrix subtraction.</returns>
- public static Matrix operator -(Matrix matrix1, Matrix matrix2)
- {
- matrix1.M11 = matrix1.M11 - matrix2.M11;
- matrix1.M12 = matrix1.M12 - matrix2.M12;
- matrix1.M13 = matrix1.M13 - matrix2.M13;
- matrix1.M14 = matrix1.M14 - matrix2.M14;
- matrix1.M21 = matrix1.M21 - matrix2.M21;
- matrix1.M22 = matrix1.M22 - matrix2.M22;
- matrix1.M23 = matrix1.M23 - matrix2.M23;
- matrix1.M24 = matrix1.M24 - matrix2.M24;
- matrix1.M31 = matrix1.M31 - matrix2.M31;
- matrix1.M32 = matrix1.M32 - matrix2.M32;
- matrix1.M33 = matrix1.M33 - matrix2.M33;
- matrix1.M34 = matrix1.M34 - matrix2.M34;
- matrix1.M41 = matrix1.M41 - matrix2.M41;
- matrix1.M42 = matrix1.M42 - matrix2.M42;
- matrix1.M43 = matrix1.M43 - matrix2.M43;
- matrix1.M44 = matrix1.M44 - matrix2.M44;
- return matrix1;
- }
- /// <summary>
- /// Inverts values in the specified <see cref="Matrix"/>.
- /// </summary>
- /// <param name="matrix">Source <see cref="Matrix"/> on the right of the sub sign.</param>
- /// <returns>Result of the inversion.</returns>
- public static Matrix operator -(Matrix matrix)
- {
- matrix.M11 = -matrix.M11;
- matrix.M12 = -matrix.M12;
- matrix.M13 = -matrix.M13;
- matrix.M14 = -matrix.M14;
- matrix.M21 = -matrix.M21;
- matrix.M22 = -matrix.M22;
- matrix.M23 = -matrix.M23;
- matrix.M24 = -matrix.M24;
- matrix.M31 = -matrix.M31;
- matrix.M32 = -matrix.M32;
- matrix.M33 = -matrix.M33;
- matrix.M34 = -matrix.M34;
- matrix.M41 = -matrix.M41;
- matrix.M42 = -matrix.M42;
- matrix.M43 = -matrix.M43;
- matrix.M44 = -matrix.M44;
- return matrix;
- }
- /// <summary>
- /// Creates a new <see cref="Matrix"/> that contains subtraction of one matrix from another.
- /// </summary>
- /// <param name="matrix1">The first <see cref="Matrix"/>.</param>
- /// <param name="matrix2">The second <see cref="Matrix"/>.</param>
- /// <returns>The result of the matrix subtraction.</returns>
- public static Matrix Subtract(Matrix matrix1, Matrix matrix2)
- {
- matrix1.M11 = matrix1.M11 - matrix2.M11;
- matrix1.M12 = matrix1.M12 - matrix2.M12;
- matrix1.M13 = matrix1.M13 - matrix2.M13;
- matrix1.M14 = matrix1.M14 - matrix2.M14;
- matrix1.M21 = matrix1.M21 - matrix2.M21;
- matrix1.M22 = matrix1.M22 - matrix2.M22;
- matrix1.M23 = matrix1.M23 - matrix2.M23;
- matrix1.M24 = matrix1.M24 - matrix2.M24;
- matrix1.M31 = matrix1.M31 - matrix2.M31;
- matrix1.M32 = matrix1.M32 - matrix2.M32;
- matrix1.M33 = matrix1.M33 - matrix2.M33;
- matrix1.M34 = matrix1.M34 - matrix2.M34;
- matrix1.M41 = matrix1.M41 - matrix2.M41;
- matrix1.M42 = matrix1.M42 - matrix2.M42;
- matrix1.M43 = matrix1.M43 - matrix2.M43;
- matrix1.M44 = matrix1.M44 - matrix2.M44;
- return matrix1;
- }
- /// <summary>
- /// Creates a new <see cref="Matrix"/> that contains subtraction of one matrix from another.
- /// </summary>
- /// <param name="matrix1">The first <see cref="Matrix"/>.</param>
- /// <param name="matrix2">The second <see cref="Matrix"/>.</param>
- /// <param name="result">The result of the matrix subtraction as an output parameter.</param>
- public static void Subtract(ref Matrix matrix1, ref Matrix matrix2, out Matrix result)
- {
- result.M11 = matrix1.M11 - matrix2.M11;
- result.M12 = matrix1.M12 - matrix2.M12;
- result.M13 = matrix1.M13 - matrix2.M13;
- result.M14 = matrix1.M14 - matrix2.M14;
- result.M21 = matrix1.M21 - matrix2.M21;
- result.M22 = matrix1.M22 - matrix2.M22;
- result.M23 = matrix1.M23 - matrix2.M23;
- result.M24 = matrix1.M24 - matrix2.M24;
- result.M31 = matrix1.M31 - matrix2.M31;
- result.M32 = matrix1.M32 - matrix2.M32;
- result.M33 = matrix1.M33 - matrix2.M33;
- result.M34 = matrix1.M34 - matrix2.M34;
- result.M41 = matrix1.M41 - matrix2.M41;
- result.M42 = matrix1.M42 - matrix2.M42;
- result.M43 = matrix1.M43 - matrix2.M43;
- result.M44 = matrix1.M44 - matrix2.M44;
- }
- internal string DebugDisplayString
- {
- get
- {
- if (this == Identity)
- {
- return "Identity";
- }
- return string.Concat(
- "( ", this.M11.ToString(), " ", this.M12.ToString(), " ", this.M13.ToString(), " ", this.M14.ToString(), " ) \r\n",
- "( ", this.M21.ToString(), " ", this.M22.ToString(), " ", this.M23.ToString(), " ", this.M24.ToString(), " ) \r\n",
- "( ", this.M31.ToString(), " ", this.M32.ToString(), " ", this.M33.ToString(), " ", this.M34.ToString(), " ) \r\n",
- "( ", this.M41.ToString(), " ", this.M42.ToString(), " ", this.M43.ToString(), " ", this.M44.ToString(), " )");
- }
- }
- /// <summary>
- /// Returns a <see cref="String"/> representation of this <see cref="Matrix"/> in the format:
- /// {M11:[<see cref="M11"/>] M12:[<see cref="M12"/>] M13:[<see cref="M13"/>] M14:[<see cref="M14"/>]}
- /// {M21:[<see cref="M21"/>] M12:[<see cref="M22"/>] M13:[<see cref="M23"/>] M14:[<see cref="M24"/>]}
- /// {M31:[<see cref="M31"/>] M32:[<see cref="M32"/>] M33:[<see cref="M33"/>] M34:[<see cref="M34"/>]}
- /// {M41:[<see cref="M41"/>] M42:[<see cref="M42"/>] M43:[<see cref="M43"/>] M44:[<see cref="M44"/>]}
- /// </summary>
- /// <returns>A <see cref="String"/> representation of this <see cref="Matrix"/>.</returns>
- public override string ToString()
- {
- return "{M11:" + M11 + " M12:" + M12 + " M13:" + M13 + " M14:" + M14 + "}"
- + " {M21:" + M21 + " M22:" + M22 + " M23:" + M23 + " M24:" + M24 + "}"
- + " {M31:" + M31 + " M32:" + M32 + " M33:" + M33 + " M34:" + M34 + "}"
- + " {M41:" + M41 + " M42:" + M42 + " M43:" + M43 + " M44:" + M44 + "}";
- }
- /// <summary>
- /// Swap the matrix rows and columns.
- /// </summary>
- /// <param name="matrix">The matrix for transposing operation.</param>
- /// <returns>The new <see cref="Matrix"/> which contains the transposing result.</returns>
- public static Matrix Transpose(Matrix matrix)
- {
- Matrix ret;
- Transpose(ref matrix, out ret);
- return ret;
- }
- /// <summary>
- /// Swap the matrix rows and columns.
- /// </summary>
- /// <param name="matrix">The matrix for transposing operation.</param>
- /// <param name="result">The new <see cref="Matrix"/> which contains the transposing result as an output parameter.</param>
- public static void Transpose(ref Matrix matrix, out Matrix result)
- {
- Matrix ret;
-
- ret.M11 = matrix.M11;
- ret.M12 = matrix.M21;
- ret.M13 = matrix.M31;
- ret.M14 = matrix.M41;
- ret.M21 = matrix.M12;
- ret.M22 = matrix.M22;
- ret.M23 = matrix.M32;
- ret.M24 = matrix.M42;
- ret.M31 = matrix.M13;
- ret.M32 = matrix.M23;
- ret.M33 = matrix.M33;
- ret.M34 = matrix.M43;
- ret.M41 = matrix.M14;
- ret.M42 = matrix.M24;
- ret.M43 = matrix.M34;
- ret.M44 = matrix.M44;
-
- result = ret;
- }
- #endregion
-
- #region Private Static Methods
-
- /// <summary>
- /// Helper method for using the Laplace expansion theorem using two rows expansions to calculate major and
- /// minor determinants of a 4x4 matrix. This method is used for inverting a matrix.
- /// </summary>
- private static void FindDeterminants(ref Matrix matrix, out float major,
- out float minor1, out float minor2, out float minor3, out float minor4, out float minor5, out float minor6,
- out float minor7, out float minor8, out float minor9, out float minor10, out float minor11, out float minor12)
- {
- double det1 = (double)matrix.M11 * (double)matrix.M22 - (double)matrix.M12 * (double)matrix.M21;
- double det2 = (double)matrix.M11 * (double)matrix.M23 - (double)matrix.M13 * (double)matrix.M21;
- double det3 = (double)matrix.M11 * (double)matrix.M24 - (double)matrix.M14 * (double)matrix.M21;
- double det4 = (double)matrix.M12 * (double)matrix.M23 - (double)matrix.M13 * (double)matrix.M22;
- double det5 = (double)matrix.M12 * (double)matrix.M24 - (double)matrix.M14 * (double)matrix.M22;
- double det6 = (double)matrix.M13 * (double)matrix.M24 - (double)matrix.M14 * (double)matrix.M23;
- double det7 = (double)matrix.M31 * (double)matrix.M42 - (double)matrix.M32 * (double)matrix.M41;
- double det8 = (double)matrix.M31 * (double)matrix.M43 - (double)matrix.M33 * (double)matrix.M41;
- double det9 = (double)matrix.M31 * (double)matrix.M44 - (double)matrix.M34 * (double)matrix.M41;
- double det10 = (double)matrix.M32 * (double)matrix.M43 - (double)matrix.M33 * (double)matrix.M42;
- double det11 = (double)matrix.M32 * (double)matrix.M44 - (double)matrix.M34 * (double)matrix.M42;
- double det12 = (double)matrix.M33 * (double)matrix.M44 - (double)matrix.M34 * (double)matrix.M43;
-
- major = (float)(det1*det12 - det2*det11 + det3*det10 + det4*det9 - det5*det8 + det6*det7);
- minor1 = (float)det1;
- minor2 = (float)det2;
- minor3 = (float)det3;
- minor4 = (float)det4;
- minor5 = (float)det5;
- minor6 = (float)det6;
- minor7 = (float)det7;
- minor8 = (float)det8;
- minor9 = (float)det9;
- minor10 = (float)det10;
- minor11 = (float)det11;
- minor12 = (float)det12;
- }
-
- #endregion
- }
- }
|