matrix.bmx 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962
  1. ' Copyright (c) 2019-2020 Bruce A Henderson
  2. '
  3. ' This software is provided 'as-is', without any express or implied
  4. ' warranty. In no event will the authors be held liable for any damages
  5. ' arising from the use of this software.
  6. '
  7. ' Permission is granted to anyone to use this software for any purpose,
  8. ' including commercial applications, and to alter it and redistribute it
  9. ' freely, subject to the following restrictions:
  10. '
  11. ' 1. The origin of this software must not be misrepresented; you must not
  12. ' claim that you wrote the original software. If you use this software
  13. ' in a product, an acknowledgment in the product documentation would be
  14. ' appreciated but is not required.
  15. '
  16. ' 2. Altered source versions must be plainly marked as such, and must not be
  17. ' misrepresented as being the original software.
  18. '
  19. ' 3. This notice may not be removed or altered from any source
  20. ' distribution.
  21. '
  22. SuperStrict
  23. Rem
  24. bbdoc: Math/Matrix
  25. End Rem
  26. Module BRL.Matrix
  27. ModuleInfo "Version: 1.00"
  28. ModuleInfo "Author: Bruce A Henderson"
  29. ModuleInfo "License: zlib"
  30. ModuleInfo "Copyright: 2019-2020 Bruce A Henderson"
  31. ModuleInfo "History: 1.00"
  32. ModuleInfo "History: Initial Release"
  33. Import BRL.Math
  34. Import BRL.Vector
  35. Import BRL.StringBuilder
  36. Rem
  37. bbdoc: A 2x2 Matrix
  38. End Rem
  39. Struct SMat2D
  40. Field ReadOnly a:Double
  41. Field ReadOnly b:Double
  42. Field ReadOnly c:Double
  43. Field ReadOnly d:Double
  44. Rem
  45. bbdoc: Creates a new #SMat2D from the supplied arguments.
  46. End Rem
  47. Method New(a:Double, b:Double, c:Double, d:Double)
  48. Self.a = a
  49. Self.b = b
  50. Self.c = c
  51. Self.d = d
  52. End Method
  53. Rem
  54. bbdoc: Applies the matrix to the vector @v, returning a new vector.
  55. End Rem
  56. Method Apply:SVec2D(v:SVec2D)
  57. Return New SVec2D(a * v.x + c * v.y, b * v.x + d * v.y)
  58. End Method
  59. Rem
  60. bbdoc: Returns the identity matrix.
  61. End Rem
  62. Function Identity:SMat2D()
  63. Return New SMat2D(1, 0, 0, 1)
  64. End Function
  65. Rem
  66. bbdoc: Adds @z to the matrix, returning a new matrix.
  67. End Rem
  68. Method Operator+:SMat2D(z:SMat2D)
  69. Return New SMat2D(a + z.a, b + z.b, c + z.c, d + z.d)
  70. End Method
  71. Rem
  72. bbdoc: Subtracts @z from the matrix, returning a new matrix.
  73. End Rem
  74. Method Operator-:SMat2D(z:SMat2D)
  75. Return New SMat2D(a - z.a, b - z.b, c - z.c, d - z.d)
  76. End Method
  77. Rem
  78. bbdoc: Multiplies the matrix by @z, the dot product, returning a new matrix.
  79. End Rem
  80. Method Operator*:SMat2D(z:SMat2D)
  81. Return New SMat2D(a * z.a + c * z.b, b * z.a + d * z.b, a * z.c + c * z.d, b * z.c + d * z.d)
  82. End Method
  83. Rem
  84. bbdoc: Returns the transposition of the cofactor matrix.
  85. End Rem
  86. Method Adjoint:SMat2D()
  87. Return New SMat2D(d, -b, -c, a)
  88. End Method
  89. Rem
  90. bbdoc: Multiplies the matrix by @z by its components, or element-wise matrix multiplication, return a new matrix.
  91. End Rem
  92. Method CompMul:SMat2D(z:SMat2D)
  93. Return New SMat2D(a * z.a, b * z.b, c * z.c, d * z.d)
  94. End Method
  95. Rem
  96. bbdoc: Returns the determinant of the matrix.
  97. End Rem
  98. Method Determinant:Double()
  99. Return a * d - c * b
  100. End Method
  101. Rem
  102. bbdoc: Returns the inverse of the matrix.
  103. End Rem
  104. Method Invert:SMat2D()
  105. Local det:Double = a * d - c * b
  106. If det = 0 Then
  107. Return New SMat2D(0, 0, 0, 0)
  108. End If
  109. det = 1 / det
  110. Return New SMat2D(d * det, -b * det, -c * det, a * det)
  111. End Method
  112. Rem
  113. bbdoc: Rotates the matrix by @angle degrees, returning the rotated matrix.
  114. End Rem
  115. Method Rotate:SMat2D(angle:Double)
  116. Local sa:Double = Sin(angle)
  117. Local ca:Double = Cos(angle)
  118. Return New SMat2D(a * ca + c * sa, b * ca + d * sa, a * -sa + c * ca, b * -sa + d * ca)
  119. End Method
  120. Rem
  121. bbdoc: Creates a rotated matrix of @angle degrees.
  122. End Rem
  123. Function Rotation:SMat2D(angle:Double)
  124. Local sa:Double = Sin(angle)
  125. Local ca:Double = Cos(angle)
  126. Return New SMat2D(ca, sa, -sa, ca)
  127. End Function
  128. Rem
  129. bbdoc: Returns the scale of this matrix.
  130. End Rem
  131. Method Scale:SMat2D(s:SVec2D)
  132. Return New SMat2D(a * s.x, b * s.x, c * s.y, d * s.y)
  133. End Method
  134. Rem
  135. bbdoc: Creates a scaled matrix of the scale @s.
  136. End Rem
  137. Function Scaling:SMat2D(s:SVec2D)
  138. Return New SMat2D(s.x, 0, 0, s.y)
  139. End Function
  140. Rem
  141. bbdoc: Returns the transpose of this matrix.
  142. End Rem
  143. Method Transpose:SMat2D()
  144. Return New SMat2D(a, c, b, d)
  145. End Method
  146. Rem
  147. bbdoc: Returns a #String representation of the matrix.
  148. End Rem
  149. Method ToString:String() Override
  150. Local sb:TStringBuilder = New TStringBuilder
  151. sb.Append(a).Append(", ").Append(c).Append(",~n")
  152. sb.Append(b).Append(", ").Append(d)
  153. Return sb.ToString()
  154. End Method
  155. End Struct
  156. Rem
  157. bbdoc: A 3x3 matrix.
  158. End Rem
  159. Struct SMat3D
  160. Field ReadOnly a:Double
  161. Field ReadOnly b:Double
  162. Field ReadOnly c:Double
  163. Field ReadOnly d:Double
  164. Field ReadOnly e:Double
  165. Field ReadOnly f:Double
  166. Field ReadOnly g:Double
  167. Field ReadOnly h:Double
  168. Field ReadOnly i:Double
  169. Rem
  170. bbdoc: Creates a new #SMat3D from the supplied arguments.
  171. End Rem
  172. Method New(a:Double, b:Double, c:Double, d:Double, e:Double, f:Double, g:Double, h:Double, i:Double)
  173. Self.a = a
  174. Self.b = b
  175. Self.c = c
  176. Self.d = d
  177. Self.e = e
  178. Self.f = f
  179. Self.g = g
  180. Self.h = h
  181. Self.i = i
  182. End Method
  183. Rem
  184. bbdoc: Applies the matrix to the vector @v, returning a new vector.
  185. End Rem
  186. Method Apply:SVec2D(v:SVec2D)
  187. Return New SVec2D(a * v.x + d * v.y + g, b * v.x + e * v.y + h)
  188. End Method
  189. Rem
  190. bbdoc: Applies the matrix to the vector @v, returning a new vector.
  191. End Rem
  192. Method Apply:SVec3D(v:SVec3D)
  193. Return New SVec3D(v.x * a + v.y * d + v.z * g, v.x * b + v.y * e + v.z * h, v.x * c + v.y * f + v.z * i)
  194. End Method
  195. Rem
  196. bbdoc: Applies the matrix to the vector @v, returning a new vector.
  197. End Rem
  198. Method Apply:SVec4D(v:SVec4D)
  199. Return New SVec4D(v.x * a + v.y * d + v.z * g, v.x * b + v.y * e + v.z * h, v.x * c + v.y * f + v.z * i, 0)
  200. End Method
  201. Rem
  202. bbdoc: Return the 3x3 identity matrix.
  203. End Rem
  204. Function Identity:SMat3D()
  205. Return New SMat3D(1, 0, 0, 0, 1, 0, 0, 0, 1)
  206. End Function
  207. Rem
  208. bbdoc: Adds @z to the matrix, returning a new matrix.
  209. End Rem
  210. Method Operator+:SMat3D(z:SMat3D Var)
  211. Return New SMat3D(a + z.a, b + z.b, c + z.c, d + z.d, e + z.e, f + z.f, g + z.g, h + z.h, i + z.i)
  212. End Method
  213. Rem
  214. bbdoc: Subtracts @z from the matrix, returning a new matrix.
  215. End Rem
  216. Method Operator-:SMat3D(z:SMat3D Var)
  217. Return New SMat3D(a - z.a, b - z.b, c - z.c, d - z.d, e - z.e, f - z.f, g - z.g, h - z.h, i - z.i)
  218. End Method
  219. Rem
  220. bbdoc: Multiplies the matrix by @z, the dot product, returning a new matrix.
  221. End Rem
  222. Method Operator*:SMat3D(z:SMat3D Var)
  223. Local a00:Double = a
  224. Local a01:Double = b
  225. Local a02:Double = c
  226. Local a10:Double = d
  227. Local a11:Double = e
  228. Local a12:Double = f
  229. Local a20:Double = g
  230. Local a21:Double = h
  231. Local a22:Double = i
  232. Local b00:Double = z.a
  233. Local b01:Double = z.b
  234. Local b02:Double = z.c
  235. Local b10:Double = z.d
  236. Local b11:Double = z.e
  237. Local b12:Double = z.f
  238. Local b20:Double = z.g
  239. Local b21:Double = z.h
  240. Local b22:Double = z.i
  241. Return New SMat3D(b00 * a00 + b01 * a10 + b02 * a20, ..
  242. b00 * a01 + b01 * a11 + b02 * a21, ..
  243. b00 * a02 + b01 * a12 + b02 * a22, ..
  244. b10 * a00 + b11 * a10 + b12 * a20, ..
  245. b10 * a01 + b11 * a11 + b12 * a21, ..
  246. b10 * a02 + b11 * a12 + b12 * a22, ..
  247. b20 * a00 + b21 * a10 + b22 * a20, ..
  248. b20 * a01 + b21 * a11 + b22 * a21, ..
  249. b20 * a02 + b21 * a12 + b22 * a22)
  250. End Method
  251. Rem
  252. bbdoc: Returns the transposition of the cofactor matrix.
  253. End Rem
  254. Method Adjoint:SMat3D()
  255. Return New SMat3D(e * i - f * h, ..
  256. c * h - b * i, ..
  257. b * f - c * e, ..
  258. f * g - d * i, ..
  259. a * i - c * g, ..
  260. c * d - a * f, ..
  261. d * h - e * g, ..
  262. b * g - a * h, ..
  263. a * e - b * d)
  264. End Method
  265. Rem
  266. bbdoc: Multiplies the matrix by @z by its components, or element-wise matrix multiplication, return a new matrix.
  267. End Rem
  268. Method CompMul:SMat3D(z:SMat3D Var)
  269. Return New SMat3D(a * z.a, b * z.b, c * z.c, d * z.d, e * z.e, f * z.f, g * z.g, h * z.h, i * z.i)
  270. End Method
  271. Rem
  272. bbdoc: Returns the determinant of the matrix.
  273. End Rem
  274. Method Determinant:Double()
  275. Local a00:Double = a
  276. Local a01:Double = b
  277. Local a02:Double = c
  278. Local a10:Double = d
  279. Local a11:Double = e
  280. Local a12:Double = f
  281. Local a20:Double = g
  282. Local a21:Double = h
  283. Local a22:Double = i
  284. Return a00 * ( a22 * a11 - a12 * a21) + a01 * (-a22 * a10 + a12 * a20) + a02 * ( a21 * a10 - a11 * a20)
  285. End Method
  286. Rem
  287. bbdoc: Returns the inverse of the matrix.
  288. End Rem
  289. Method Invert:SMat3D()
  290. Local a00:Double = a
  291. Local a01:Double = b
  292. Local a02:Double = c
  293. Local a10:Double = d
  294. Local a11:Double = e
  295. Local a12:Double = f
  296. Local a20:Double = g
  297. Local a21:Double = h
  298. Local a22:Double = i
  299. Local b01:Double = a22 * a11 - a12 * a21
  300. Local b11:Double = -a22 * a10 + a12 * a20
  301. Local b21:Double = a21 * a10 - a11 * a20
  302. Local det:Double = a00 * b01 + a01 * b11 + a02 * b21
  303. If det = 0 Then
  304. Return New SMat3D(0, 0, 0, 0, 0, 0, 0, 0, 0)
  305. End If
  306. det = 1 / det
  307. Return New SMat3D(b01 * det, ..
  308. (-a22 * a01 + a02 * a21) * det, ..
  309. ( a12 * a01 - a02 * a11) * det,
  310. b11 * det, ..
  311. ( a22 * a00 - a02 * a20) * det, ..
  312. (-a12 * a00 + a02 * a10) * det, ..
  313. b21 * det, ..
  314. (-a21 * a00 + a01 * a20) * det, ..
  315. ( a11 * a00 - a01 * a10) * det)
  316. End Method
  317. Rem
  318. bbdoc: Rotates the matrix on a 2D rotation in the XY plane by @angle degrees, returning a new matrix.
  319. End Rem
  320. Method Rotate:SMat3D(angle:Double)
  321. Local sa:Double = Sin(angle)
  322. Local ca:Double = Cos(angle)
  323. Return New SMat3D(ca * a + sa * d, ..
  324. ca * b + sa * e, ..
  325. ca * c + sa * f, ..
  326. ca * d - sa * a, ..
  327. ca * e - sa * b, ..
  328. ca * f - sa * c, ..
  329. g, h, i)
  330. End Method
  331. Rem
  332. bbdoc: Rotates the matrix around the Z axis by @angle degrees, returning a new matrix.
  333. End Rem
  334. Method RotateZ:SMat3D(angle:Double)
  335. Local ca:Double = Cos(angle)
  336. Local sa:Double = Sin(angle)
  337. Return New SMat3D( ..
  338. a * ca - c * sa, ..
  339. b * ca - d * sa, ..
  340. 0, ..
  341. a * sa + c * ca, ..
  342. b * sa + d * ca, ..
  343. 0, ..
  344. g, h, i)
  345. End Method
  346. Rem
  347. bbdoc: Returns a rotation matrix of @angle degrees.
  348. End Rem
  349. Function Rotation:SMat3D(angle:Double)
  350. Local sa:Double = Sin(angle)
  351. Local ca:Double = Cos(angle)
  352. Return New SMat3D(ca, sa, 0, -sa, ca, 0, 0, 0, 1)
  353. End Function
  354. Rem
  355. bbdoc: Scales the matrix by @s, returning a new matrix.
  356. End Rem
  357. Method Scale:SMat3D(s:SVec2D)
  358. Local bx:Double = s.x
  359. Local by:Double = s.y
  360. Return New SMat3D(a * bx, b * bx, c * bx, d * by, e * by, f * by, g, h, i)
  361. End Method
  362. Rem
  363. bbdoc: Returns a scaling matrix of @s.
  364. End Rem
  365. Function Scaling:SMat3D(s:SVec2D)
  366. Return New SMat3D(s.x, 0, 0, 0, s.y, 0, 0, 0, 1)
  367. End Function
  368. Rem
  369. bbdoc: Returns a translation with the specified @x, @y, and @z displacements.
  370. End Rem
  371. Method Translate:SMat3D(x:Double, y:Double, z:Double)
  372. Return New SMat3D( ..
  373. a, b, c, ..
  374. d, e, f, ..
  375. g + a * x + b * y + c * z, ..
  376. h + d * x + e * y + f * z, ..
  377. i + g * x + h * y + i * z)
  378. End Method
  379. Rem
  380. bbdoc: Returns a translation with displacement vector @s.
  381. End Rem
  382. Method Translate:SMat3D(t:SVec3D)
  383. Return New SMat3D( ..
  384. a, b, c, ..
  385. d, e, f, ..
  386. g + a * t.x + b * t.y + c * t.z, ..
  387. h + d * t.x + e * t.y + f * t.z, ..
  388. i + g * t.x + h * t.y + i * t.z)
  389. End Method
  390. Rem
  391. bbdoc: Returns a transposition of the matrix.
  392. End Rem
  393. Method Transpose:SMat3D()
  394. Return New SMat3D(a, d, g, b, e, h, c, f, i)
  395. End Method
  396. Rem
  397. bbdoc: Returns a #String representation of the matrix.
  398. End Rem
  399. Method ToString:String() Override
  400. Local sb:TStringBuilder = New TStringBuilder
  401. sb.Append(a).Append(", ").Append(d).Append(", ").Append(g).Append(",~n")
  402. sb.Append(b).Append(", ").Append(e).Append(", ").Append(h).Append(",~n")
  403. sb.Append(c).Append(", ").Append(f).Append(", ").Append(i)
  404. Return sb.ToString()
  405. End Method
  406. End Struct
  407. Rem
  408. bbdoc: A standard 4x4 transformation matrix.
  409. End Rem
  410. Struct SMat4D
  411. Field ReadOnly a:Double
  412. Field ReadOnly b:Double
  413. Field ReadOnly c:Double
  414. Field ReadOnly d:Double
  415. Field ReadOnly e:Double
  416. Field ReadOnly f:Double
  417. Field ReadOnly g:Double
  418. Field ReadOnly h:Double
  419. Field ReadOnly i:Double
  420. Field ReadOnly j:Double
  421. Field ReadOnly k:Double
  422. Field ReadOnly l:Double
  423. Field ReadOnly m:Double
  424. Field ReadOnly n:Double
  425. Field ReadOnly o:Double
  426. Field ReadOnly p:Double
  427. Rem
  428. bbdoc: Creates a new #SMat4D from the supplied arguments.
  429. End Rem
  430. Method New(a:Double, b:Double, c:Double, d:Double, e:Double, f:Double, g:Double, h:Double, i:Double, j:Double, k:Double, l:Double, m:Double, n:Double, o:Double, p:Double)
  431. Self.a = a
  432. Self.b = b
  433. Self.c = c
  434. Self.d = d
  435. Self.e = e
  436. Self.f = f
  437. Self.g = g
  438. Self.h = h
  439. Self.i = i
  440. Self.j = j
  441. Self.k = k
  442. Self.l = l
  443. Self.m = m
  444. Self.n = n
  445. Self.o = o
  446. Self.p = p
  447. End Method
  448. Rem
  449. bbdoc: Applies the matrix to the vector @v, returning a new vector.
  450. End Rem
  451. Method Apply:SVec2D(v:SVec2D)
  452. Return New SVec2D(a * v.x + e * v.y + m, b * v.x + f * v.y + n)
  453. End Method
  454. Rem
  455. bbdoc: Applies the 4x4 matrix @b to the vector, returning a new vector.
  456. End Rem
  457. Method Apply:SVec3D(v:SVec3D)
  458. Local w:Double = d * v.x + h * v.y + l * v.z + p
  459. If w = 0 Then
  460. w = 1
  461. Else
  462. w = 1 / w
  463. End If
  464. Return New SVec3D((a * v.x + e * v.y + i * v.z + m) * w, ..
  465. (b * v.x + f * v.y + j * v.z + n) * w, ..
  466. (c * v.x + g * v.y + k * v.z + o) * w)
  467. End Method
  468. Rem
  469. bbdoc: Applies the 4x4 matrix @b to the vector, returning a new vector.
  470. End Rem
  471. Method Apply:SVec4D(v:SVec4D)
  472. Return New SVec4D(a * v.x + e * v.y + i * v.z + m * v.w, ..
  473. b * v.x + f * v.y + j * v.z + n * v.w, ..
  474. c * v.x + g * v.y + k * v.z + o * v.w, ..
  475. d * v.x + h * v.y + l * v.z + p * v.w)
  476. End Method
  477. Rem
  478. bbdoc: Returns the identity matrix.
  479. End Rem
  480. Function Identity:SMat4D()
  481. Return New SMat4D(1, 0, 0, 0, ..
  482. 0, 1, 0, 0, ..
  483. 0, 0, 1, 0, ..
  484. 0, 0, 0, 1)
  485. End Function
  486. Rem
  487. bbdoc: Adds @z to the matrix, returning a new matrix.
  488. End Rem
  489. Method Operator+:SMat4D(z:SMat4D Var)
  490. Return New SMat4D(a + z.a, b + z.b, c + z.c, d + z.d, ..
  491. e + z.e, f + z.f, g + z.g, h + z.h, ..
  492. i + z.i, j + z.j, k + z.k, l + z.l, ..
  493. m + z.m, n + z.n, o + z.o, p + z.p)
  494. End Method
  495. Rem
  496. bbdoc: Subtracts @z from the matrix, returning a new matrix.
  497. End Rem
  498. Method Operator-:SMat4D(z:SMat4D Var)
  499. Return New SMat4D(a - z.a, b - z.b, c - z.c, d - z.d, ..
  500. e - z.e, f - z.f, g - z.g, h - z.h, ..
  501. i - z.i, j - z.j, k - z.k, l - z.l, ..
  502. m - z.m, n - z.n, o - z.o, p - z.p)
  503. End Method
  504. Rem
  505. bbdoc: Multiplies the matrix by @z, the dot product, returning a new matrix.
  506. End Rem
  507. Method Operator*:SMat4D(z:SMat4D Var)
  508. Local a00:Double = a
  509. Local a01:Double = b
  510. Local a02:Double = c
  511. Local a03:Double = d
  512. Local a10:Double = e
  513. Local a11:Double = f
  514. Local a12:Double = g
  515. Local a13:Double = h
  516. Local a20:Double = i
  517. Local a21:Double = j
  518. Local a22:Double = k
  519. Local a23:Double = l
  520. Local a30:Double = m
  521. Local a31:Double = n
  522. Local a32:Double = o
  523. Local a33:Double = p
  524. Local b00:Double = z.a
  525. Local b01:Double = z.b
  526. Local b02:Double = z.c
  527. Local b03:Double = z.d
  528. Local b10:Double = z.e
  529. Local b11:Double = z.f
  530. Local b12:Double = z.g
  531. Local b13:Double = z.h
  532. Local b20:Double = z.i
  533. Local b21:Double = z.j
  534. Local b22:Double = z.k
  535. Local b23:Double = z.l
  536. Local b30:Double = z.m
  537. Local b31:Double = z.n
  538. Local b32:Double = z.o
  539. Local b33:Double = z.p
  540. Return New SMat4D(b00 * a00 + b01 * a10 + b02 * a20 + b03 * a30, ..
  541. b00 * a01 + b01 * a11 + b02 * a21 + b03 * a31, ..
  542. b00 * a02 + b01 * a12 + b02 * a22 + b03 * a32, ..
  543. b00 * a03 + b01 * a13 + b02 * a23 + b03 * a33, ..
  544. b10 * a00 + b11 * a10 + b12 * a20 + b13 * a30, ..
  545. b10 * a01 + b11 * a11 + b12 * a21 + b13 * a31, ..
  546. b10 * a02 + b11 * a12 + b12 * a22 + b13 * a32, ..
  547. b10 * a03 + b11 * a13 + b12 * a23 + b13 * a33, ..
  548. b20 * a00 + b21 * a10 + b22 * a20 + b23 * a30, ..
  549. b20 * a01 + b21 * a11 + b22 * a21 + b23 * a31, ..
  550. b20 * a02 + b21 * a12 + b22 * a22 + b23 * a32, ..
  551. b20 * a03 + b21 * a13 + b22 * a23 + b23 * a33, ..
  552. b30 * a00 + b31 * a10 + b32 * a20 + b33 * a30, ..
  553. b30 * a01 + b31 * a11 + b32 * a21 + b33 * a31, ..
  554. b30 * a02 + b31 * a12 + b32 * a22 + b33 * a32, ..
  555. b30 * a03 + b31 * a13 + b32 * a23 + b33 * a33)
  556. End Method
  557. Rem
  558. bbdoc: Returns the transposition of the cofactor matrix.
  559. End Rem
  560. Method Adjoint:SMat4D()
  561. Local a00:Double = a
  562. Local a01:Double = b
  563. Local a02:Double = c
  564. Local a03:Double = d
  565. Local a10:Double = e
  566. Local a11:Double = f
  567. Local a12:Double = g
  568. Local a13:Double = h
  569. Local a20:Double = i
  570. Local a21:Double = j
  571. Local a22:Double = k
  572. Local a23:Double = l
  573. Local a30:Double = m
  574. Local a31:Double = n
  575. Local a32:Double = o
  576. Local a33:Double = p
  577. Return New SMat4D(a11 * (a22 * a33 - a23 * a32) - a21 * (a12 * a33 - a13 * a32) + a31 * (a12 * a23 - a13 * a22), ..
  578. -(a01 * (a22 * a33 - a23 * a32) - a21 * (a02 * a33 - a03 * a32) + a31 * (a02 * a23 - a03 * a22)), ..
  579. a01 * (a12 * a33 - a13 * a32) - a11 * (a02 * a33 - a03 * a32) + a31 * (a02 * a13 - a03 * a12), ..
  580. -(a01 * (a12 * a23 - a13 * a22) - a11 * (a02 * a23 - a03 * a22) + a21 * (a02 * a13 - a03 * a12)), ..
  581. -(a10 * (a22 * a33 - a23 * a32) - a20 * (a12 * a33 - a13 * a32) + a30 * (a12 * a23 - a13 * a22)), ..
  582. a00 * (a22 * a33 - a23 * a32) - a20 * (a02 * a33 - a03 * a32) + a30 * (a02 * a23 - a03 * a22), ..
  583. -(a00 * (a12 * a33 - a13 * a32) - a10 * (a02 * a33 - a03 * a32) + a30 * (a02 * a13 - a03 * a12)), ..
  584. a00 * (a12 * a23 - a13 * a22) - a10 * (a02 * a23 - a03 * a22) + a20 * (a02 * a13 - a03 * a12), ..
  585. a10 * (a21 * a33 - a23 * a31) - a20 * (a11 * a33 - a13 * a31) + a30 * (a11 * a23 - a13 * a21), ..
  586. -(a00 * (a21 * a33 - a23 * a31) - a20 * (a01 * a33 - a03 * a31) + a30 * (a01 * a23 - a03 * a21)), ..
  587. a00 * (a11 * a33 - a13 * a31) - a10 * (a01 * a33 - a03 * a31) + a30 * (a01 * a13 - a03 * a11), ..
  588. -(a00 * (a11 * a23 - a13 * a21) - a10 * (a01 * a23 - a03 * a21) + a20 * (a01 * a13 - a03 * a11)), ..
  589. -(a10 * (a21 * a32 - a22 * a31) - a20 * (a11 * a32 - a12 * a31) + a30 * (a11 * a22 - a12 * a21)), ..
  590. a00 * (a21 * a32 - a22 * a31) - a20 * (a01 * a32 - a02 * a31) + a30 * (a01 * a22 - a02 * a21), ..
  591. -(a00 * (a11 * a32 - a12 * a31) - a10 * (a01 * a32 - a02 * a31) + a30 * (a01 * a12 - a02 * a11)), ..
  592. a00 * (a11 * a22 - a12 * a21) - a10 * (a01 * a22 - a02 * a21) + a20 * (a01 * a12 - a02 * a11))
  593. End Method
  594. Rem
  595. bbdoc: Multiplies the matrix by @z by its components, or element-wise matrix multiplication, returning a new matrix.
  596. End Rem
  597. Method CompMul:SMat4D(z:SMat4D Var)
  598. Return New SMat4D(a * z.a, b * z.b, c * z.c, d * z.d, ..
  599. e * z.e, f * z.f, g * z.g, h * z.h, ..
  600. i * z.i, j * z.j, k * z.k, l * z.l, ..
  601. m * z.m, n * z.n, o * z.o, p * z.p)
  602. End Method
  603. Rem
  604. bbdoc: Returns the determinant of the matrix.
  605. End Rem
  606. Method Determinant:Double()
  607. Local a00:Double = a
  608. Local a01:Double = b
  609. Local a02:Double = c
  610. Local a03:Double = d
  611. Local a10:Double = e
  612. Local a11:Double = f
  613. Local a12:Double = g
  614. Local a13:Double = h
  615. Local a20:Double = i
  616. Local a21:Double = j
  617. Local a22:Double = k
  618. Local a23:Double = l
  619. Local a30:Double = m
  620. Local a31:Double = n
  621. Local a32:Double = o
  622. Local a33:Double = p
  623. Local b00:Double = a00 * a11 - a01 * a10
  624. Local b01:Double = a00 * a12 - a02 * a10
  625. Local b02:Double = a00 * a13 - a03 * a10
  626. Local b03:Double = a01 * a12 - a02 * a11
  627. Local b04:Double = a01 * a13 - a03 * a11
  628. Local b05:Double = a02 * a13 - a03 * a12
  629. Local b06:Double = a20 * a31 - a21 * a30
  630. Local b07:Double = a20 * a32 - a22 * a30
  631. Local b08:Double = a20 * a33 - a23 * a30
  632. Local b09:Double = a21 * a32 - a22 * a31
  633. Local b10:Double = a21 * a33 - a23 * a31
  634. Local b11:Double = a22 * a33 - a23 * a32
  635. Return b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06
  636. End Method
  637. Rem
  638. bbdoc: Returns a projection matrix with a viewing frustum defined by the plane coordinates passed in.
  639. End Rem
  640. Function Frustum:SMat4D(l:Double, r:Double, b:Double, t:Double, n:Double, f:Double)
  641. Local rl:Double = 1.0 / (r - l)
  642. Local tb:Double = 1.0 / (t - b)
  643. Local nf:Double = 1.0 / (n - f)
  644. Return New SMat4D((2.0 * n) * rl, 0, 0, 0, ..
  645. 0, (2.0 * n) * tb, 0, 0, ..
  646. (r + l) * rl, (t + b) * tb, (f + n) * nf, -1, ..
  647. 0, 0, (2.0 * n * f) * nf, 0)
  648. End Function
  649. Rem
  650. bbdoc: The inverse of this matrix.
  651. about: An inverted matrix is such that if multiplied by the original would result in identity matrix.
  652. If some matrix transforms vectors in a particular way, then the inverse matrix can transform them back.
  653. End Rem
  654. Method Invert:SMat4D()
  655. Local a00:Double = a
  656. Local a01:Double = b
  657. Local a02:Double = c
  658. Local a03:Double = d
  659. Local a10:Double = e
  660. Local a11:Double = f
  661. Local a12:Double = g
  662. Local a13:Double = h
  663. Local a20:Double = i
  664. Local a21:Double = j
  665. Local a22:Double = k
  666. Local a23:Double = l
  667. Local a30:Double = m
  668. Local a31:Double = n
  669. Local a32:Double = o
  670. Local a33:Double = p
  671. Local b00:Double = a00 * a11 - a01 * a10
  672. Local b01:Double = a00 * a12 - a02 * a10
  673. Local b02:Double = a00 * a13 - a03 * a10
  674. Local b03:Double = a01 * a12 - a02 * a11
  675. Local b04:Double = a01 * a13 - a03 * a11
  676. Local b05:Double = a02 * a13 - a03 * a12
  677. Local b06:Double = a20 * a31 - a21 * a30
  678. Local b07:Double = a20 * a32 - a22 * a30
  679. Local b08:Double = a20 * a33 - a23 * a30
  680. Local b09:Double = a21 * a32 - a22 * a31
  681. Local b10:Double = a21 * a33 - a23 * a31
  682. Local b11:Double = a22 * a33 - a23 * a32
  683. Local det:Double = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06
  684. If det = 0 Then
  685. Return New SMat4D(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
  686. End If
  687. det = 1 / det
  688. Return New SMat4D((a11 * b11 - a12 * b10 + a13 * b09) * det, ..
  689. (a02 * b10 - a01 * b11 - a03 * b09) * det, ..
  690. (a31 * b05 - a32 * b04 + a33 * b03) * det, ..
  691. (a22 * b04 - a21 * b05 - a23 * b03) * det, ..
  692. (a12 * b08 - a10 * b11 - a13 * b07) * det, ..
  693. (a00 * b11 - a02 * b08 + a03 * b07) * det, ..
  694. (a32 * b02 - a30 * b05 - a33 * b01) * det, ..
  695. (a20 * b05 - a22 * b02 + a23 * b01) * det, ..
  696. (a10 * b10 - a11 * b08 + a13 * b06) * det, ..
  697. (a01 * b08 - a00 * b10 - a03 * b06) * det, ..
  698. (a30 * b04 - a31 * b02 + a33 * b00) * det, ..
  699. (a21 * b02 - a20 * b04 - a23 * b00) * det, ..
  700. (a11 * b07 - a10 * b09 - a12 * b06) * det, ..
  701. (a00 * b09 - a01 * b07 + a02 * b06) * det, ..
  702. (a31 * b01 - a30 * b03 - a32 * b00) * det, ..
  703. (a20 * b03 - a21 * b01 + a22 * b00) * det)
  704. End Method
  705. Rem
  706. bbdoc: Computes a transformation matrix that corresponds to a camera viewing the @eye from the @pos.
  707. about: The right-hand vector is perpendicular to the up vector.
  708. End Rem
  709. Function LookAt:SMat4D(eye:SVec3D, pos:SVec3D, upDir:SVec3D)
  710. Local forward:SVec3D = (eye - pos).Normal()
  711. Local lft:SVec3D = upDir.Cross(forward).Normal()
  712. Local up:SVec3D = forward.Cross(lft)
  713. Local mat:SMat4D = SMat4D.Identity()
  714. Local a00:Double = lft.x
  715. Local a01:Double = up.x
  716. Local a02:Double = forward.x
  717. Local a03:Double = mat.d
  718. Local a10:Double = lft.y
  719. Local a11:Double = up.y
  720. Local a12:Double = forward.y
  721. Local a13:Double = mat.h
  722. Local a20:Double = lft.z
  723. Local a21:Double = up.z
  724. Local a22:Double = forward.z
  725. Local a23:Double = mat.l
  726. Local a30:Double = -lft.x * eye.x - lft.y * eye.y - lft.z * eye.z
  727. Local a31:Double = -up.x * eye.x - up.y * eye.y - up.z * eye.z
  728. Local a32:Double = -forward.x * eye.x - forward.y * eye.y - forward.z * eye.z
  729. Local a33:Double = mat.p
  730. Return New SMat4D(a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23, a30, a31, a32, a33)
  731. End Function
  732. Rem
  733. bbdoc: Creates an orthogonal projection matrix.
  734. about: The returned matrix, when used as a Camera's projection matrix, creates a view showing the area between @width and @height, with @zNear and @zFar as the near and far depth clipping planes.
  735. End Rem
  736. Function Orthogonal:SMat4D(width:Double, height:Double, zNear:Double, zFar:Double)
  737. Local nf:Double = 1.0 / (zNear - zFar)
  738. Return New SMat4D(2.0 / width, 0, 0, 0, ..
  739. 0, 2.0 / height, 0, 0, ..
  740. 0, 0, 2.0 * nf, 0, ..
  741. 0, 0, (zNear + zFar) * nf, 1)
  742. End Function
  743. Rem
  744. bbdoc: Creates a perspective projection matrix.
  745. End Rem
  746. Function Perspective:SMat4D(fov:Double, w:Double, h:Double, n:Double, f:Double)
  747. Local tf:Double = Tan(fov / 2)
  748. Return New SMat4D(1 / ((w / h) * tf), 0, 0, 0, ..
  749. 0, 1 / tf, 0, 0, ..
  750. 0, 0, - (f + n) / (f - n), -1, ..
  751. 0, 0, - (2 * f * n) / (f - n), 0)
  752. End Function
  753. Rem
  754. bbdoc: Creates a rotation matrix, rotated @angle degrees around the point @axis.
  755. End Rem
  756. Method Rotate:SMat4D(axis:SVec3D, angle:Double)
  757. Local c:Double = Cos(angle)
  758. Local ic:Double = 1 - c
  759. Local s:Double = Sin(angle)
  760. Local norm:SVec3D = axis.Normal()
  761. Local x:Double = ic * norm.x
  762. Local y:Double = ic * norm.y
  763. Local z:Double = ic * norm.z
  764. Local mat:SMat4D = New SMat4D(c + x * norm.x, x * norm.y + s * norm.z, x * norm.z - s * norm.y, 0, ..
  765. y * norm.x - s * norm.z, c + y * norm.y, y * norm.z + s * norm.x, 0, ..
  766. z * norm.x + s * norm.y, z * norm.y - s * norm.x, c + z * norm.z, 0, ..
  767. 0, 0, 0, 1)
  768. Return Self * mat
  769. End Method
  770. Rem
  771. bbdoc: Returns a rotation matrix on the given @axis and @angle degrees.
  772. End Rem
  773. Function Rotation:SMat4D(axis:SVec3D, angle:Double)
  774. Local x:Double = axis.x
  775. Local y:Double = axis.y
  776. Local z:Double = axis.z
  777. Local sa:Double = Sin(angle)
  778. Local ca:Double = Cos(angle)
  779. Local t:Double = 1 - ca
  780. Return New SMat4D(x * x * t + ca, ..
  781. y * x * t + z * sa, ..
  782. z * x * t - y * sa, ..
  783. 0, ..
  784. x * y * t - z * sa, ..
  785. y * y * t + ca, ..
  786. z * y * t + x * sa, ..
  787. 0, ..
  788. x * z * t + y * sa, ..
  789. y * z * t - x * sa, ..
  790. z * z * t + ca, ..
  791. 0, 0, 0, 0, 1)
  792. End Function
  793. Rem
  794. bbdoc: Scales the matrix, return the new scaled matrix.
  795. End Rem
  796. Method Scale:SMat4D(s:SVec3D)
  797. Local bx:Double = s.x
  798. Local by:Double = s.y
  799. Local bz:Double = s.z
  800. Return New SMat4D(a * bx, b * bx, c * bx, d * bx, ..
  801. e * by, f * by, g * by, h * by, ..
  802. i * bz, j * bz, k * bz, l * bz, ..
  803. m, n, o, p)
  804. End Method
  805. Rem
  806. bbdoc: Creates a scaling matrix.
  807. End Rem
  808. Function Scaling:SMat4D(s:SVec3D)
  809. Return New SMat4D(s.x, 0, 0, 0, 0, s.y, 0, 0, 0, 0, s.z, 0, 0, 0, 0, 1)
  810. End Function
  811. Rem
  812. bbdoc: Returns the transpose of this matrix.
  813. about: The transposed matrix is the one that has the columns exchanged with its rows.
  814. End Rem
  815. Method Transpose:SMat4D()
  816. Return New SMat4D(a, e, i, m, b, f, j, n, c, g, k, o, d, h, l, p)
  817. End Method
  818. Rem
  819. bbdoc: Translates the matrix to @s.
  820. End Rem
  821. Method Translate:SMat4D(s:SVec3D)
  822. Local bx:Double = s.x
  823. Local by:Double = s.y
  824. Local bz:Double = s.z
  825. Return New SMat4D(a, b, c, d, e, f, g, h, i, j, k, l, ..
  826. a * bx + e * by + i * bz + m, ..
  827. b * bx + f * by + j * bz + n, ..
  828. c * bx + g * by + k * bz + o, ..
  829. d * bx + h * by + l * bz + p)
  830. End Method
  831. Rem
  832. bbdoc: Creates a translation matrix.
  833. End Rem
  834. Function Translation:SMat4D(s:SVec3D)
  835. Return New SMat4D(1, 0, 0, 0, ..
  836. 0, 1, 0, 0, ..
  837. 0, 0, 1, 0, ..
  838. s.x, s.y, s.z, 1)
  839. End Function
  840. Rem
  841. bbdoc: Returns a #String representation of the matrix.
  842. End Rem
  843. Method ToString:String() Override
  844. Local sb:TStringBuilder = New TStringBuilder
  845. sb.Append(a).Append(", ").Append(e).Append(", ").Append(i).Append(", ").Append(m).Append(",~n")
  846. sb.Append(b).Append(", ").Append(f).Append(", ").Append(j).Append(", ").Append(n).Append(",~n")
  847. sb.Append(c).Append(", ").Append(g).Append(", ").Append(k).Append(", ").Append(o).Append(",~n")
  848. sb.Append(d).Append(", ").Append(h).Append(", ").Append(l).Append(", ").Append(p)
  849. Return sb.ToString()
  850. End Method
  851. End Struct
  852. Rem
  853. bbdoc: A #Float backed 2x2 Matrix.
  854. End Rem
  855. Struct SMat2F
  856. Field ReadOnly a:Float
  857. Field ReadOnly b:Float
  858. Field ReadOnly c:Float
  859. Field ReadOnly d:Float
  860. Rem
  861. bbdoc: Creates a new #SMat2F from the supplied arguments.
  862. End Rem
  863. Method New(a:Float, b:Float, c:Float, d:Float)
  864. Self.a = a
  865. Self.b = b
  866. Self.c = c
  867. Self.d = d
  868. End Method
  869. Rem
  870. bbdoc: Applies the matrix to the vector @v, returning a new vector.
  871. End Rem
  872. Method Apply:SVec2F(v:SVec2F)
  873. Return New SVec2F(a * v.x + c * v.y, b * v.x + d * v.y)
  874. End Method
  875. Rem
  876. bbdoc: Returns the identity matrix.
  877. End Rem
  878. Function Identity:SMat2F()
  879. Return New SMat2F(1, 0, 0, 1)
  880. End Function
  881. Rem
  882. bbdoc: Adds @z to the matrix, returning a new matrix.
  883. End Rem
  884. Method Operator+:SMat2F(z:SMat2F)
  885. Return New SMat2F(a + z.a, b + z.b, c + z.c, d + z.d)
  886. End Method
  887. Rem
  888. bbdoc: Subtracts @z from the matrix, returning a new matrix.
  889. End Rem
  890. Method Operator-:SMat2F(z:SMat2F)
  891. Return New SMat2F(a - z.a, b - z.b, c - z.c, d - z.d)
  892. End Method
  893. Rem
  894. bbdoc: Multiplies the matrix by @z, the dot product, returning a new matrix.
  895. End Rem
  896. Method Operator*:SMat2F(z:SMat2F)
  897. Return New SMat2F(a * z.a + c * z.b, b * z.a + d * z.b, a * z.c + c * z.d, b * z.c + d * z.d)
  898. End Method
  899. Rem
  900. bbdoc: Returns the transposition of the cofactor matrix.
  901. End Rem
  902. Method Adjoint:SMat2F()
  903. Return New SMat2F(d, -b, -c, a)
  904. End Method
  905. Rem
  906. bbdoc: Multiplies the matrix by @z by its components, return a new matrix.
  907. End Rem
  908. Method CompMul:SMat2F(z:SMat2F)
  909. Return New SMat2F(a * z.a, b * z.b, c * z.c, d * z.d)
  910. End Method
  911. Rem
  912. bbdoc: Returns the determinant of the matrix.
  913. End Rem
  914. Method Determinant:Float()
  915. Return a * d - c * b
  916. End Method
  917. Rem
  918. bbdoc: Returns the inverse of the matrix.
  919. End Rem
  920. Method Invert:SMat2F()
  921. Local det:Float = a * d - c * b
  922. If det = 0 Then
  923. Return New SMat2F(0, 0, 0, 0)
  924. End If
  925. det = 1 / det
  926. Return New SMat2F(d * det, -b * det, -c * det, a * det)
  927. End Method
  928. Rem
  929. bbdoc: Rotates the matrix by @angle degrees, returning the rotated matrix.
  930. End Rem
  931. Method Rotate:SMat2F(angle:Double)
  932. Local sa:Double = Sin(angle)
  933. Local ca:Double = Cos(angle)
  934. Return New SMat2F(Float(a * ca + c * sa), Float(b * ca + d * sa), Float(a * -sa + c * ca), Float(b * -sa + d * ca))
  935. End Method
  936. Rem
  937. bbdoc: Creates a rotated matrix of @angle degrees.
  938. End Rem
  939. Function Rotation:SMat2F(angle:Double)
  940. Local sa:Double = Sin(angle)
  941. Local ca:Double = Cos(angle)
  942. Return New SMat2F(Float(ca), Float(sa), Float(-sa), Float(ca))
  943. End Function
  944. Rem
  945. bbdoc: Returns the scale of this matrix.
  946. End Rem
  947. Method Scale:SMat2F(s:SVec2F)
  948. Return New SMat2F(a * s.x, b * s.x, c * s.y, d * s.y)
  949. End Method
  950. Rem
  951. bbdoc: Returns the scale of this matrix.
  952. End Rem
  953. Method Scale:SMat2F(s:SVec2D)
  954. Return New SMat2F(Float(a * s.x), Float(b * s.x), Float(c * s.y), Float(d * s.y))
  955. End Method
  956. Rem
  957. bbdoc: Creates a scaled matrix of the scale @s.
  958. End Rem
  959. Function Scaling:SMat2F(s:SVec2F)
  960. Return New SMat2F(s.x, 0, 0, s.y)
  961. End Function
  962. Rem
  963. bbdoc: Creates a scaled matrix of the scale @s.
  964. End Rem
  965. Function Scaling:SMat2F(s:SVec2D)
  966. Return New SMat2F(Float(s.x), 0, 0, Float(s.y))
  967. End Function
  968. Rem
  969. bbdoc: Returns the transpose of this matrix.
  970. End Rem
  971. Method Transpose:SMat2F()
  972. Return New SMat2F(a, c, b, d)
  973. End Method
  974. Rem
  975. bbdoc: Returns a #String representation of the matrix.
  976. End Rem
  977. Method ToString:String() Override
  978. Local sb:TStringBuilder = New TStringBuilder
  979. sb.Append(a).Append(", ").Append(c).Append(",~n")
  980. sb.Append(b).Append(", ").Append(d)
  981. Return sb.ToString()
  982. End Method
  983. End Struct
  984. Rem
  985. bbdoc: A #Float backed 3x3 matrix.
  986. End Rem
  987. Struct SMat3F
  988. Field ReadOnly a:Float
  989. Field ReadOnly b:Float
  990. Field ReadOnly c:Float
  991. Field ReadOnly d:Float
  992. Field ReadOnly e:Float
  993. Field ReadOnly f:Float
  994. Field ReadOnly g:Float
  995. Field ReadOnly h:Float
  996. Field ReadOnly i:Float
  997. Rem
  998. bbdoc: Creates a new #SMat3F from the supplied arguments.
  999. End Rem
  1000. Method New(a:Float, b:Float, c:Float, d:Float, e:Float, f:Float, g:Float, h:Float, i:Float)
  1001. Self.a = a
  1002. Self.b = b
  1003. Self.c = c
  1004. Self.d = d
  1005. Self.e = e
  1006. Self.f = f
  1007. Self.g = g
  1008. Self.h = h
  1009. Self.i = i
  1010. End Method
  1011. Rem
  1012. bbdoc: Applies the matrix to the vector @v, returning a new vector.
  1013. End Rem
  1014. Method Apply:SVec2F(v:SVec2F)
  1015. Return New SVec2F(a * v.x + d * v.y + g, b * v.x + e * v.y + h)
  1016. End Method
  1017. Rem
  1018. bbdoc: Applies the matrix to the vector @v, returning a new vector.
  1019. End Rem
  1020. Method Apply:SVec3F(v:SVec3F)
  1021. Return New SVec3F(v.x * a + v.y * d + v.z * g, v.x * b + v.y * e + v.z * h, v.x * c + v.y * f + v.z * i)
  1022. End Method
  1023. Rem
  1024. bbdoc: Applies the matrix to the vector @v, returning a new vector.
  1025. End Rem
  1026. Method Apply:SVec4F(v:SVec4F)
  1027. Return New SVec4F(v.x * a + v.y * d + v.z * g, v.x * b + v.y * e + v.z * h, v.x * c + v.y * f + v.z * i, 0)
  1028. End Method
  1029. Rem
  1030. bbdoc: Return the 3x3 identity matrix.
  1031. End Rem
  1032. Function Identity:SMat3F()
  1033. Return New SMat3F(1, 0, 0, 0, 1, 0, 0, 0, 1)
  1034. End Function
  1035. Rem
  1036. bbdoc: Adds @z to the matrix, returning a new matrix.
  1037. End Rem
  1038. Method Operator+:SMat3F(z:SMat3F Var)
  1039. Return New SMat3F(a + z.a, b + z.b, c + z.c, d + z.d, e + z.e, f + z.f, g + z.g, h + z.h, i + z.i)
  1040. End Method
  1041. Rem
  1042. bbdoc: Subtracts @z from the matrix, returning a new matrix.
  1043. End Rem
  1044. Method Operator-:SMat3F(z:SMat3F Var)
  1045. Return New SMat3F(a - z.a, b - z.b, c - z.c, d - z.d, e - z.e, f - z.f, g - z.g, h - z.h, i - z.i)
  1046. End Method
  1047. Rem
  1048. bbdoc: Multiplies the matrix by @z, the dot product, returning a new matrix.
  1049. End Rem
  1050. Method Operator*:SMat3F(z:SMat3F Var)
  1051. Local a00:Float = a
  1052. Local a01:Float = b
  1053. Local a02:Float = c
  1054. Local a10:Float = d
  1055. Local a11:Float = e
  1056. Local a12:Float = f
  1057. Local a20:Float = g
  1058. Local a21:Float = h
  1059. Local a22:Float = i
  1060. Local b00:Float = z.a
  1061. Local b01:Float = z.b
  1062. Local b02:Float = z.c
  1063. Local b10:Float = z.d
  1064. Local b11:Float = z.e
  1065. Local b12:Float = z.f
  1066. Local b20:Float = z.g
  1067. Local b21:Float = z.h
  1068. Local b22:Float = z.i
  1069. Return New SMat3F(b00 * a00 + b01 * a10 + b02 * a20, ..
  1070. b00 * a01 + b01 * a11 + b02 * a21, ..
  1071. b00 * a02 + b01 * a12 + b02 * a22, ..
  1072. b10 * a00 + b11 * a10 + b12 * a20, ..
  1073. b10 * a01 + b11 * a11 + b12 * a21, ..
  1074. b10 * a02 + b11 * a12 + b12 * a22, ..
  1075. b20 * a00 + b21 * a10 + b22 * a20, ..
  1076. b20 * a01 + b21 * a11 + b22 * a21, ..
  1077. b20 * a02 + b21 * a12 + b22 * a22)
  1078. End Method
  1079. Rem
  1080. bbdoc: Returns the transposition of the cofactor matrix.
  1081. End Rem
  1082. Method Adjoint:SMat3F()
  1083. Return New SMat3F(e * i - f * h, ..
  1084. c * h - b * i, ..
  1085. b * f - c * e, ..
  1086. f * g - d * i, ..
  1087. a * i - c * g, ..
  1088. c * d - a * f, ..
  1089. d * h - e * g, ..
  1090. b * g - a * h, ..
  1091. a * e - b * d)
  1092. End Method
  1093. Rem
  1094. bbdoc: Multiplies the matrix by @z by its components, return a new matrix.
  1095. End Rem
  1096. Method CompMul:SMat3F(z:SMat3F Var)
  1097. Return New SMat3F(a * z.a, b * z.b, c * z.c, d * z.d, e * z.e, f * z.f, g * z.g, h * z.h, i * z.i)
  1098. End Method
  1099. Rem
  1100. bbdoc: Returns the determinant of the matrix.
  1101. End Rem
  1102. Method Determinant:Float()
  1103. Local a00:Float = a
  1104. Local a01:Float = b
  1105. Local a02:Float = c
  1106. Local a10:Float = d
  1107. Local a11:Float = e
  1108. Local a12:Float = f
  1109. Local a20:Float = g
  1110. Local a21:Float = h
  1111. Local a22:Float = i
  1112. Return a00 * ( a22 * a11 - a12 * a21) + a01 * (-a22 * a10 + a12 * a20) + a02 * ( a21 * a10 - a11 * a20)
  1113. End Method
  1114. Rem
  1115. bbdoc: Returns the inverse of the matrix.
  1116. End Rem
  1117. Method Invert:SMat3F()
  1118. Local a00:Float = a
  1119. Local a01:Float = b
  1120. Local a02:Float = c
  1121. Local a10:Float = d
  1122. Local a11:Float = e
  1123. Local a12:Float = f
  1124. Local a20:Float = g
  1125. Local a21:Float = h
  1126. Local a22:Float = i
  1127. Local b01:Float = a22 * a11 - a12 * a21
  1128. Local b11:Float = -a22 * a10 + a12 * a20
  1129. Local b21:Float = a21 * a10 - a11 * a20
  1130. Local det:Float = a00 * b01 + a01 * b11 + a02 * b21
  1131. If det = 0 Then
  1132. Return New SMat3F(0, 0, 0, 0, 0, 0, 0, 0, 0)
  1133. End If
  1134. det = 1 / det
  1135. Return New SMat3F(b01 * det, ..
  1136. (-a22 * a01 + a02 * a21) * det, ..
  1137. ( a12 * a01 - a02 * a11) * det,
  1138. b11 * det, ..
  1139. ( a22 * a00 - a02 * a20) * det, ..
  1140. (-a12 * a00 + a02 * a10) * det, ..
  1141. b21 * det, ..
  1142. (-a21 * a00 + a01 * a20) * det, ..
  1143. ( a11 * a00 - a01 * a10) * det)
  1144. End Method
  1145. Rem
  1146. bbdoc: Rotates the matrix by @angle degrees, returning a new matrix.
  1147. End Rem
  1148. Method Rotate:SMat3F(angle:Double)
  1149. Local sa:Double = Sin(angle)
  1150. Local ca:Double = Cos(angle)
  1151. Return New SMat3F(Float(ca * a + sa * d), ..
  1152. Float(ca * b + sa * e), ..
  1153. Float(ca * c + sa * f), ..
  1154. Float(ca * d - sa * a), ..
  1155. Float(ca * e - sa * b), ..
  1156. Float(ca * f - sa * c), ..
  1157. g, h, i)
  1158. End Method
  1159. Rem
  1160. bbdoc: Retrns a rotation matrix of @angle degrees.
  1161. End Rem
  1162. Function Rotation:SMat3F(angle:Double)
  1163. Local sa:Double = Sin(angle)
  1164. Local ca:Double = Cos(angle)
  1165. Return New SMat3F(Float(ca), Float(sa), 0, Float(-sa), Float(ca), 0, 0, 0, 1)
  1166. End Function
  1167. Rem
  1168. bbdoc: Scales the matrix by @s, returning a new matrix.
  1169. End Rem
  1170. Method Scale:SMat3F(s:SVec2F)
  1171. Local bx:Float = s.x
  1172. Local by:Float = s.y
  1173. Return New SMat3F(a * bx, b * bx, c * bx, d * by, e * by, f * by, g, h, i)
  1174. End Method
  1175. Rem
  1176. bbdoc: Scales the matrix by @s, returning a new matrix.
  1177. End Rem
  1178. Method Scale:SMat3F(s:SVec2D)
  1179. Local bx:Float = s.x
  1180. Local by:Float = s.y
  1181. Return New SMat3F(Float(a * bx), Float(b * bx), Float(c * bx), Float(d * by), Float(e * by), Float(f * by), g, h, i)
  1182. End Method
  1183. Rem
  1184. bbdoc: Returns a scaling matrix of @s.
  1185. End Rem
  1186. Function Scaling:SMat3F(s:SVec2F)
  1187. Return New SMat3F(s.x, 0, 0, 0, s.y, 0, 0, 0, 1)
  1188. End Function
  1189. Rem
  1190. bbdoc: Returns a scaling matrix of @s.
  1191. End Rem
  1192. Function Scaling:SMat3F(s:SVec2D)
  1193. Return New SMat3F(Float(s.x), 0, 0, 0, Float(s.y), 0, 0, 0, 1)
  1194. End Function
  1195. Rem
  1196. bbdoc: Returns a translation with the specified @x, @y, and @z displacements.
  1197. End Rem
  1198. Method Translate:SMat3F(x:Float, y:Float, z:Float)
  1199. Return New SMat3F( ..
  1200. a, b, c, ..
  1201. d, e, f, ..
  1202. g + a * x + b * y + c * z, ..
  1203. h + d * x + e * y + f * z, ..
  1204. i + g * x + h * y + i * z)
  1205. End Method
  1206. Rem
  1207. bbdoc: Returns a translation with displacement vector @s.
  1208. End Rem
  1209. Method Translate:SMat3F(t:SVec3F)
  1210. Return New SMat3F( ..
  1211. a, b, c, ..
  1212. d, e, f, ..
  1213. g + a * t.x + b * t.y + c * t.z, ..
  1214. h + d * t.x + e * t.y + f * t.z, ..
  1215. i + g * t.x + h * t.y + i * t.z)
  1216. End Method
  1217. Rem
  1218. bbdoc: Returns a transposition of the matrix.
  1219. End Rem
  1220. Method Transpose:SMat3F()
  1221. Return New SMat3F(a, d, g, b, e, h, c, f, i)
  1222. End Method
  1223. Rem
  1224. bbdoc: Returns a #String representation of the matrix.
  1225. End Rem
  1226. Method ToString:String() Override
  1227. Local sb:TStringBuilder = New TStringBuilder
  1228. sb.Append(a).Append(", ").Append(d).Append(", ").Append(g).Append(",~n")
  1229. sb.Append(b).Append(", ").Append(e).Append(", ").Append(h).Append(",~n")
  1230. sb.Append(c).Append(", ").Append(f).Append(", ").Append(i)
  1231. Return sb.ToString()
  1232. End Method
  1233. End Struct
  1234. Rem
  1235. bbdoc: A standard #Float backed 4x4 transformation matrix.
  1236. End Rem
  1237. Struct SMat4F
  1238. Field ReadOnly a:Float
  1239. Field ReadOnly b:Float
  1240. Field ReadOnly c:Float
  1241. Field ReadOnly d:Float
  1242. Field ReadOnly e:Float
  1243. Field ReadOnly f:Float
  1244. Field ReadOnly g:Float
  1245. Field ReadOnly h:Float
  1246. Field ReadOnly i:Float
  1247. Field ReadOnly j:Float
  1248. Field ReadOnly k:Float
  1249. Field ReadOnly l:Float
  1250. Field ReadOnly m:Float
  1251. Field ReadOnly n:Float
  1252. Field ReadOnly o:Float
  1253. Field ReadOnly p:Float
  1254. Rem
  1255. bbdoc: Creates a new #SMat4F from the supplied arguments.
  1256. End Rem
  1257. Method New(a:Float, b:Float, c:Float, d:Float, e:Float, f:Float, g:Float, h:Float, i:Float, j:Float, k:Float, l:Float, m:Float, n:Float, o:Float, p:Float)
  1258. Self.a = a
  1259. Self.b = b
  1260. Self.c = c
  1261. Self.d = d
  1262. Self.e = e
  1263. Self.f = f
  1264. Self.g = g
  1265. Self.h = h
  1266. Self.i = i
  1267. Self.j = j
  1268. Self.k = k
  1269. Self.l = l
  1270. Self.m = m
  1271. Self.n = n
  1272. Self.o = o
  1273. Self.p = p
  1274. End Method
  1275. Rem
  1276. bbdoc: Applies the matrix to the vector @v, returning a new vector.
  1277. End Rem
  1278. Method Apply:SVec2F(v:SVec2F)
  1279. Return New SVec2F(a * v.x + e * v.y + m, b * v.x + f * v.y + n)
  1280. End Method
  1281. Rem
  1282. bbdoc: Applies the 4x4 matrix @b to the vector, returning a new vector.
  1283. End Rem
  1284. Method Apply:SVec3F(v:SVec3F)
  1285. Local w:Float = d * v.x + h * v.y + l * v.z + p
  1286. If w = 0 Then
  1287. w = 1
  1288. Else
  1289. w = 1 / w
  1290. End If
  1291. Return New SVec3F((a * v.x + e * v.y + i * v.z + m) * w, ..
  1292. (b * v.x + f * v.y + j * v.z + n) * w, ..
  1293. (c * v.x + g * v.y + k * v.z + o) * w)
  1294. End Method
  1295. Rem
  1296. bbdoc: Applies the 4x4 matrix @b to the vector, returning a new vector.
  1297. End Rem
  1298. Method Apply:SVec4F(v:SVec4F)
  1299. Return New SVec4F(a * v.x + e * v.y + i * v.z + m * v.w, ..
  1300. b * v.x + f * v.y + j * v.z + n * v.w, ..
  1301. c * v.x + g * v.y + k * v.z + o * v.w, ..
  1302. d * v.x + h * v.y + l * v.z + p * v.w)
  1303. End Method
  1304. Rem
  1305. bbdoc: Returns the identity matrix.
  1306. End Rem
  1307. Function Identity:SMat4F()
  1308. Return New SMat4F(1, 0, 0, 0, ..
  1309. 0, 1, 0, 0, ..
  1310. 0, 0, 1, 0, ..
  1311. 0, 0, 0, 1)
  1312. End Function
  1313. Rem
  1314. bbdoc: Adds @z to the matrix, returning a new matrix.
  1315. End Rem
  1316. Method Operator+:SMat4F(z:SMat4F Var)
  1317. Return New SMat4F(a + z.a, b + z.b, c + z.c, d + z.d, ..
  1318. e + z.e, f + z.f, g + z.g, h + z.h, ..
  1319. i + z.i, j + z.j, k + z.k, l + z.l, ..
  1320. m + z.m, n + z.n, o + z.o, p + z.p)
  1321. End Method
  1322. Rem
  1323. bbdoc: Subtracts @z from the matrix, returning a new matrix.
  1324. End Rem
  1325. Method Operator-:SMat4F(z:SMat4F Var)
  1326. Return New SMat4F(a - z.a, b - z.b, c - z.c, d - z.d, ..
  1327. e - z.e, f - z.f, g - z.g, h - z.h, ..
  1328. i - z.i, j - z.j, k - z.k, l - z.l, ..
  1329. m - z.m, n - z.n, o - z.o, p - z.p)
  1330. End Method
  1331. Rem
  1332. bbdoc: Multiplies the matrix by @z, the dot product, returning a new matrix.
  1333. End Rem
  1334. Method Operator*:SMat4F(z:SMat4F Var)
  1335. Local a00:Float = a
  1336. Local a01:Float = b
  1337. Local a02:Float = c
  1338. Local a03:Float = d
  1339. Local a10:Float = e
  1340. Local a11:Float = f
  1341. Local a12:Float = g
  1342. Local a13:Float = h
  1343. Local a20:Float = i
  1344. Local a21:Float = j
  1345. Local a22:Float = k
  1346. Local a23:Float = l
  1347. Local a30:Float = m
  1348. Local a31:Float = n
  1349. Local a32:Float = o
  1350. Local a33:Float = p
  1351. Local b00:Float = z.a
  1352. Local b01:Float = z.b
  1353. Local b02:Float = z.c
  1354. Local b03:Float = z.d
  1355. Local b10:Float = z.e
  1356. Local b11:Float = z.f
  1357. Local b12:Float = z.g
  1358. Local b13:Float = z.h
  1359. Local b20:Float = z.i
  1360. Local b21:Float = z.j
  1361. Local b22:Float = z.k
  1362. Local b23:Float = z.l
  1363. Local b30:Float = z.m
  1364. Local b31:Float = z.n
  1365. Local b32:Float = z.o
  1366. Local b33:Float = z.p
  1367. Return New SMat4F(b00 * a00 + b01 * a10 + b02 * a20 + b03 * a30, ..
  1368. b00 * a01 + b01 * a11 + b02 * a21 + b03 * a31, ..
  1369. b00 * a02 + b01 * a12 + b02 * a22 + b03 * a32, ..
  1370. b00 * a03 + b01 * a13 + b02 * a23 + b03 * a33, ..
  1371. b10 * a00 + b11 * a10 + b12 * a20 + b13 * a30, ..
  1372. b10 * a01 + b11 * a11 + b12 * a21 + b13 * a31, ..
  1373. b10 * a02 + b11 * a12 + b12 * a22 + b13 * a32, ..
  1374. b10 * a03 + b11 * a13 + b12 * a23 + b13 * a33, ..
  1375. b20 * a00 + b21 * a10 + b22 * a20 + b23 * a30, ..
  1376. b20 * a01 + b21 * a11 + b22 * a21 + b23 * a31, ..
  1377. b20 * a02 + b21 * a12 + b22 * a22 + b23 * a32, ..
  1378. b20 * a03 + b21 * a13 + b22 * a23 + b23 * a33, ..
  1379. b30 * a00 + b31 * a10 + b32 * a20 + b33 * a30, ..
  1380. b30 * a01 + b31 * a11 + b32 * a21 + b33 * a31, ..
  1381. b30 * a02 + b31 * a12 + b32 * a22 + b33 * a32, ..
  1382. b30 * a03 + b31 * a13 + b32 * a23 + b33 * a33)
  1383. End Method
  1384. Rem
  1385. bbdoc: Returns the transposition of the cofactor matrix.
  1386. End Rem
  1387. Method Adjoint:SMat4F()
  1388. Local a00:Float = a
  1389. Local a01:Float = b
  1390. Local a02:Float = c
  1391. Local a03:Float = d
  1392. Local a10:Float = e
  1393. Local a11:Float = f
  1394. Local a12:Float = g
  1395. Local a13:Float = h
  1396. Local a20:Float = i
  1397. Local a21:Float = j
  1398. Local a22:Float = k
  1399. Local a23:Float = l
  1400. Local a30:Float = m
  1401. Local a31:Float = n
  1402. Local a32:Float = o
  1403. Local a33:Float = p
  1404. Return New SMat4F(a11 * (a22 * a33 - a23 * a32) - a21 * (a12 * a33 - a13 * a32) + a31 * (a12 * a23 - a13 * a22), ..
  1405. -(a01 * (a22 * a33 - a23 * a32) - a21 * (a02 * a33 - a03 * a32) + a31 * (a02 * a23 - a03 * a22)), ..
  1406. a01 * (a12 * a33 - a13 * a32) - a11 * (a02 * a33 - a03 * a32) + a31 * (a02 * a13 - a03 * a12), ..
  1407. -(a01 * (a12 * a23 - a13 * a22) - a11 * (a02 * a23 - a03 * a22) + a21 * (a02 * a13 - a03 * a12)), ..
  1408. -(a10 * (a22 * a33 - a23 * a32) - a20 * (a12 * a33 - a13 * a32) + a30 * (a12 * a23 - a13 * a22)), ..
  1409. a00 * (a22 * a33 - a23 * a32) - a20 * (a02 * a33 - a03 * a32) + a30 * (a02 * a23 - a03 * a22), ..
  1410. -(a00 * (a12 * a33 - a13 * a32) - a10 * (a02 * a33 - a03 * a32) + a30 * (a02 * a13 - a03 * a12)), ..
  1411. a00 * (a12 * a23 - a13 * a22) - a10 * (a02 * a23 - a03 * a22) + a20 * (a02 * a13 - a03 * a12), ..
  1412. a10 * (a21 * a33 - a23 * a31) - a20 * (a11 * a33 - a13 * a31) + a30 * (a11 * a23 - a13 * a21), ..
  1413. -(a00 * (a21 * a33 - a23 * a31) - a20 * (a01 * a33 - a03 * a31) + a30 * (a01 * a23 - a03 * a21)), ..
  1414. a00 * (a11 * a33 - a13 * a31) - a10 * (a01 * a33 - a03 * a31) + a30 * (a01 * a13 - a03 * a11), ..
  1415. -(a00 * (a11 * a23 - a13 * a21) - a10 * (a01 * a23 - a03 * a21) + a20 * (a01 * a13 - a03 * a11)), ..
  1416. -(a10 * (a21 * a32 - a22 * a31) - a20 * (a11 * a32 - a12 * a31) + a30 * (a11 * a22 - a12 * a21)), ..
  1417. a00 * (a21 * a32 - a22 * a31) - a20 * (a01 * a32 - a02 * a31) + a30 * (a01 * a22 - a02 * a21), ..
  1418. -(a00 * (a11 * a32 - a12 * a31) - a10 * (a01 * a32 - a02 * a31) + a30 * (a01 * a12 - a02 * a11)), ..
  1419. a00 * (a11 * a22 - a12 * a21) - a10 * (a01 * a22 - a02 * a21) + a20 * (a01 * a12 - a02 * a11))
  1420. End Method
  1421. Rem
  1422. bbdoc: Multiplies the matrix by @z by its components, returning a new matrix.
  1423. End Rem
  1424. Method CompMul:SMat4F(z:SMat4F Var)
  1425. Return New SMat4F(a * z.a, b * z.b, c * z.c, d * z.d, ..
  1426. e * z.e, f * z.f, g * z.g, h * z.h, ..
  1427. i * z.i, j * z.j, k * z.k, l * z.l, ..
  1428. m * z.m, n * z.n, o * z.o, p * z.p)
  1429. End Method
  1430. Rem
  1431. bbdoc: Returns the determinant of the matrix.
  1432. End Rem
  1433. Method Determinant:Float()
  1434. Local a00:Float = a
  1435. Local a01:Float = b
  1436. Local a02:Float = c
  1437. Local a03:Float = d
  1438. Local a10:Float = e
  1439. Local a11:Float = f
  1440. Local a12:Float = g
  1441. Local a13:Float = h
  1442. Local a20:Float = i
  1443. Local a21:Float = j
  1444. Local a22:Float = k
  1445. Local a23:Float = l
  1446. Local a30:Float = m
  1447. Local a31:Float = n
  1448. Local a32:Float = o
  1449. Local a33:Float = p
  1450. Local b00:Float = a00 * a11 - a01 * a10
  1451. Local b01:Float = a00 * a12 - a02 * a10
  1452. Local b02:Float = a00 * a13 - a03 * a10
  1453. Local b03:Float = a01 * a12 - a02 * a11
  1454. Local b04:Float = a01 * a13 - a03 * a11
  1455. Local b05:Float = a02 * a13 - a03 * a12
  1456. Local b06:Float = a20 * a31 - a21 * a30
  1457. Local b07:Float = a20 * a32 - a22 * a30
  1458. Local b08:Float = a20 * a33 - a23 * a30
  1459. Local b09:Float = a21 * a32 - a22 * a31
  1460. Local b10:Float = a21 * a33 - a23 * a31
  1461. Local b11:Float = a22 * a33 - a23 * a32
  1462. Return b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06
  1463. End Method
  1464. Rem
  1465. bbdoc: Returns a projection matrix with a viewing frustum defined by the plane coordinates passed in.
  1466. End Rem
  1467. Function Frustum:SMat4F(l:Float, r:Float, b:Float, t:Float, n:Float, f:Float)
  1468. Local rl:Float = 1.0 / (r - l)
  1469. Local tb:Float = 1.0 / (t - b)
  1470. Local nf:Float = 1.0 / (n - f)
  1471. Return New SMat4F((2.0 * n) * rl, 0, 0, 0, ..
  1472. 0, (2.0 * n) * tb, 0, 0, ..
  1473. (r + l) * rl, (t + b) * tb, (f + n) * nf, -1, ..
  1474. 0, 0, (2.0 * n * f) * nf, 0)
  1475. End Function
  1476. Rem
  1477. bbdoc: The inverse of this matrix.
  1478. about: An inverted matrix is such that if multiplied by the original would result in identity matrix.
  1479. If some matrix transforms vectors in a particular way, then the inverse matrix can transform them back.
  1480. End Rem
  1481. Method Invert:SMat4F()
  1482. Local a00:Float = a
  1483. Local a01:Float = b
  1484. Local a02:Float = c
  1485. Local a03:Float = d
  1486. Local a10:Float = e
  1487. Local a11:Float = f
  1488. Local a12:Float = g
  1489. Local a13:Float = h
  1490. Local a20:Float = i
  1491. Local a21:Float = j
  1492. Local a22:Float = k
  1493. Local a23:Float = l
  1494. Local a30:Float = m
  1495. Local a31:Float = n
  1496. Local a32:Float = o
  1497. Local a33:Float = p
  1498. Local b00:Float = a00 * a11 - a01 * a10
  1499. Local b01:Float = a00 * a12 - a02 * a10
  1500. Local b02:Float = a00 * a13 - a03 * a10
  1501. Local b03:Float = a01 * a12 - a02 * a11
  1502. Local b04:Float = a01 * a13 - a03 * a11
  1503. Local b05:Float = a02 * a13 - a03 * a12
  1504. Local b06:Float = a20 * a31 - a21 * a30
  1505. Local b07:Float = a20 * a32 - a22 * a30
  1506. Local b08:Float = a20 * a33 - a23 * a30
  1507. Local b09:Float = a21 * a32 - a22 * a31
  1508. Local b10:Float = a21 * a33 - a23 * a31
  1509. Local b11:Float = a22 * a33 - a23 * a32
  1510. Local det:Float = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06
  1511. If det = 0 Then
  1512. Return New SMat4F(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
  1513. End If
  1514. det = 1 / det
  1515. Return New SMat4F((a11 * b11 - a12 * b10 + a13 * b09) * det, ..
  1516. (a02 * b10 - a01 * b11 - a03 * b09) * det, ..
  1517. (a31 * b05 - a32 * b04 + a33 * b03) * det, ..
  1518. (a22 * b04 - a21 * b05 - a23 * b03) * det, ..
  1519. (a12 * b08 - a10 * b11 - a13 * b07) * det, ..
  1520. (a00 * b11 - a02 * b08 + a03 * b07) * det, ..
  1521. (a32 * b02 - a30 * b05 - a33 * b01) * det, ..
  1522. (a20 * b05 - a22 * b02 + a23 * b01) * det, ..
  1523. (a10 * b10 - a11 * b08 + a13 * b06) * det, ..
  1524. (a01 * b08 - a00 * b10 - a03 * b06) * det, ..
  1525. (a30 * b04 - a31 * b02 + a33 * b00) * det, ..
  1526. (a21 * b02 - a20 * b04 - a23 * b00) * det, ..
  1527. (a11 * b07 - a10 * b09 - a12 * b06) * det, ..
  1528. (a00 * b09 - a01 * b07 + a02 * b06) * det, ..
  1529. (a31 * b01 - a30 * b03 - a32 * b00) * det, ..
  1530. (a20 * b03 - a21 * b01 + a22 * b00) * det)
  1531. End Method
  1532. Rem
  1533. bbdoc: Computes a transformation matrix that corresponds to a camera viewing the @eye from the @pos.
  1534. about: The right-hand vector is perpendicular to the up vector.
  1535. End Rem
  1536. Function LookAt:SMat4F(eye:SVec3F, pos:SVec3F, upDir:SVec3F)
  1537. Local forward:SVec3F = (eye - pos).Normal()
  1538. Local lft:SVec3F = upDir.Cross(forward).Normal()
  1539. Local up:SVec3F = forward.Cross(lft)
  1540. Local mat:SMat4F = SMat4F.Identity()
  1541. Local a00:Float = lft.x
  1542. Local a01:Float = up.x
  1543. Local a02:Float = forward.x
  1544. Local a03:Float = mat.d
  1545. Local a10:Float = lft.y
  1546. Local a11:Float = up.y
  1547. Local a12:Float = forward.y
  1548. Local a13:Float = mat.h
  1549. Local a20:Float = lft.z
  1550. Local a21:Float = up.z
  1551. Local a22:Float = forward.z
  1552. Local a23:Float = mat.l
  1553. Local a30:Float = -lft.x * eye.x - lft.y * eye.y - lft.z * eye.z
  1554. Local a31:Float = -up.x * eye.x - up.y * eye.y - up.z * eye.z
  1555. Local a32:Float = -forward.x * eye.x - forward.y * eye.y - forward.z * eye.z
  1556. Local a33:Float = mat.p
  1557. Return New SMat4F(a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23, a30, a31, a32, a33)
  1558. End Function
  1559. Rem
  1560. bbdoc: Creates an orthogonal projection matrix.
  1561. about: The returned matrix, when used as a Camera's projection matrix, creates a view showing the area between @width and @height, with @zNear and @zFar as the near and far depth clipping planes.
  1562. End Rem
  1563. Function Orthogonal:SMat4F(width:Float, height:Float, zNear:Float, zFar:Float)
  1564. Local nf:Float = 1.0 / (zNear - zFar)
  1565. Return New SMat4F(2.0 / width, 0, 0, 0, ..
  1566. 0, 2.0 / height, 0, 0, ..
  1567. 0, 0, 2.0 * nf, 0, ..
  1568. 0, 0, (zNear + zFar) * nf, 1)
  1569. End Function
  1570. Rem
  1571. bbdoc: Creates a perspective projection matrix.
  1572. End Rem
  1573. Function Perspective:SMat4F(fov:Float, w:Float, h:Float, n:Float, f:Float)
  1574. Local tf:Float = Tan(fov / 2)
  1575. Return New SMat4F(1 / ((w / h) * tf), 0, 0, 0, ..
  1576. 0, 1 / tf, 0, 0, ..
  1577. 0, 0, - (f + n) / (f - n), -1, ..
  1578. 0, 0, - (2 * f * n) / (f - n), 0)
  1579. End Function
  1580. Rem
  1581. bbdoc: Creates a rotation matrix, rotated @angle degrees around the point @axis.
  1582. End Rem
  1583. Method Rotate:SMat4F(axis:SVec3F, angle:Double)
  1584. Local c:Float = Cos(angle)
  1585. Local ic:Float = 1 - c
  1586. Local s:Float = Sin(angle)
  1587. Local norm:SVec3F = axis.Normal()
  1588. Local x:Float = ic * norm.x
  1589. Local y:Float = ic * norm.y
  1590. Local z:Float = ic * norm.z
  1591. Local mat:SMat4F = New SMat4F(c + x * norm.x, x * norm.y + s * norm.z, x * norm.z - s * norm.y, 0, ..
  1592. y * norm.x - s * norm.z, c + y * norm.y, y * norm.z + s * norm.x, 0, ..
  1593. z * norm.x + s * norm.y, z * norm.y - s * norm.x, c + z * norm.z, 0, ..
  1594. 0, 0, 0, 1)
  1595. Return Self * mat
  1596. End Method
  1597. Rem
  1598. bbdoc: Returns a rotation matrix on the given @axis and @angle degrees.
  1599. End Rem
  1600. Function Rotation:SMat4F(axis:SVec3F, angle:Double)
  1601. Local x:Float = axis.x
  1602. Local y:Float = axis.y
  1603. Local z:Float = axis.z
  1604. Local sa:Double = Sin(angle)
  1605. Local ca:Double = Cos(angle)
  1606. Local t:Float = 1 - ca
  1607. Return New SMat4F(Float(x * x * t + ca), ..
  1608. Float(y * x * t + z * sa), ..
  1609. Float(z * x * t - y * sa), ..
  1610. 0, ..
  1611. Float(x * y * t - z * sa), ..
  1612. Float(y * y * t + ca), ..
  1613. Float(z * y * t + x * sa), ..
  1614. 0, ..
  1615. Float(x * z * t + y * sa), ..
  1616. Float(y * z * t - x * sa), ..
  1617. Float(z * z * t + ca), ..
  1618. 0, 0, 0, 0, 1)
  1619. End Function
  1620. Rem
  1621. bbdoc: Scales the matrix, return the new scaled matrix.
  1622. End Rem
  1623. Method Scale:SMat4F(s:SVec3F)
  1624. Local bx:Float = s.x
  1625. Local by:Float = s.y
  1626. Local bz:Float = s.z
  1627. Return New SMat4F(a * bx, b * bx, c * bx, d * bx, ..
  1628. e * by, f * by, g * by, h * by, ..
  1629. i * bz, j * bz, k * bz, l * bz, ..
  1630. m, n, o, p)
  1631. End Method
  1632. Rem
  1633. bbdoc: Scales the matrix, return the new scaled matrix.
  1634. End Rem
  1635. Method Scale:SMat4F(s:SVec3D)
  1636. Local bx:Double = s.x
  1637. Local by:Double = s.y
  1638. Local bz:Double = s.z
  1639. Return New SMat4F(Float(a * bx), Float(b * bx), Float(c * bx), Float(d * bx), ..
  1640. Float(e * by), Float(f * by), Float(g * by), Float(h * by), ..
  1641. Float(i * bz), Float(j * bz), Float(k * bz), Float(l * bz), ..
  1642. m, n, o, p)
  1643. End Method
  1644. Rem
  1645. bbdoc: Creates a scaling matrix.
  1646. End Rem
  1647. Function Scaling:SMat4F(s:SVec3F)
  1648. Return New SMat4F(s.x, 0, 0, 0, 0, s.y, 0, 0, 0, 0, s.z, 0, 0, 0, 0, 1)
  1649. End Function
  1650. Rem
  1651. bbdoc: Creates a Scaling matrix.
  1652. End Rem
  1653. Function Scaling:SMat4F(s:SVec3D)
  1654. Return New SMat4F(Float(s.x), 0, 0, 0, 0, Float(s.y), 0, 0, 0, 0, Float(s.z), 0, 0, 0, 0, 1)
  1655. End Function
  1656. Rem
  1657. bbdoc: Returns the transpose of this matrix.
  1658. about: The transposed matrix is the one that has the columns exchanged with its rows.
  1659. End Rem
  1660. Method Transpose:SMat4F()
  1661. Return New SMat4F(a, e, i, m, b, f, j, n, c, g, k, o, d, h, l, p)
  1662. End Method
  1663. Rem
  1664. bbdoc: Translates the matrix to @s.
  1665. End Rem
  1666. Method Translate:SMat4F(s:SVec3F)
  1667. Local bx:Float = s.x
  1668. Local by:Float = s.y
  1669. Local bz:Float = s.z
  1670. Return New SMat4F(a, b, c, d, e, f, g, h, i, j, k, l, ..
  1671. a * bx + e * by + i * bz + m, ..
  1672. b * bx + f * by + j * bz + n, ..
  1673. c * bx + g * by + k * bz + o, ..
  1674. d * bx + h * by + l * bz + p)
  1675. End Method
  1676. Rem
  1677. bbdoc: Translates the matrix To @s.
  1678. End Rem
  1679. Method Translate:SMat4F(s:SVec3D)
  1680. Local bx:Float = s.x
  1681. Local by:Float = s.y
  1682. Local bz:Float = s.z
  1683. Return New SMat4F(a, b, c, d, e, f, g, h, i, j, k, l, ..
  1684. a * bx + e * by + i * bz + m, ..
  1685. b * bx + f * by + j * bz + n, ..
  1686. c * bx + g * by + k * bz + o, ..
  1687. d * bx + h * by + l * bz + p)
  1688. End Method
  1689. Rem
  1690. bbdoc: Creates a translation matrix.
  1691. End Rem
  1692. Function Translation:SMat4F(s:SVec3F)
  1693. Return New SMat4F(1, 0, 0, 0, ..
  1694. 0, 1, 0, 0, ..
  1695. 0, 0, 1, 0, ..
  1696. s.x, s.y, s.z, 1)
  1697. End Function
  1698. Rem
  1699. bbdoc: Creates a translation matrix.
  1700. End Rem
  1701. Function Translation:SMat4F(s:SVec3D)
  1702. Return New SMat4F(1, 0, 0, 0, ..
  1703. 0, 1, 0, 0, ..
  1704. 0, 0, 1, 0, ..
  1705. Float(s.x), Float(s.y), Float(s.z), 1)
  1706. End Function
  1707. Rem
  1708. bbdoc: Returns a #String representation of the matrix.
  1709. End Rem
  1710. Method ToString:String() Override
  1711. Local sb:TStringBuilder = New TStringBuilder
  1712. sb.Append(a).Append(", ").Append(e).Append(", ").Append(i).Append(", ").Append(m).Append(",~n")
  1713. sb.Append(b).Append(", ").Append(f).Append(", ").Append(j).Append(", ").Append(n).Append(",~n")
  1714. sb.Append(c).Append(", ").Append(g).Append(", ").Append(k).Append(", ").Append(o).Append(",~n")
  1715. sb.Append(d).Append(", ").Append(h).Append(", ").Append(l).Append(", ").Append(p)
  1716. Return sb.ToString()
  1717. End Method
  1718. End Struct
  1719. Rem
  1720. bbdoc: An #Int backed 2x2 Matrix.
  1721. End Rem
  1722. Struct SMat2I
  1723. Field ReadOnly a:Int
  1724. Field ReadOnly b:Int
  1725. Field ReadOnly c:Int
  1726. Field ReadOnly d:Int
  1727. Rem
  1728. bbdoc: Creates a new #SMat2I from the supplied arguments.
  1729. End Rem
  1730. Method New(a:Int, b:Int, c:Int, d:Int)
  1731. Self.a = a
  1732. Self.b = b
  1733. Self.c = c
  1734. Self.d = d
  1735. End Method
  1736. Rem
  1737. bbdoc: Applies the matrix to the vector @v, returning a new vector.
  1738. End Rem
  1739. Method Apply:SVec2I(v:SVec2I)
  1740. Return New SVec2I(a * v.x + c * v.y, b * v.x + d * v.y)
  1741. End Method
  1742. Rem
  1743. bbdoc: Returns the identity matrix.
  1744. End Rem
  1745. Function Identity:SMat2I()
  1746. Return New SMat2I(1, 0, 0, 1)
  1747. End Function
  1748. Rem
  1749. bbdoc: Adds @z to the matrix, returning a new matrix.
  1750. End Rem
  1751. Method Operator+:SMat2I(z:SMat2I)
  1752. Return New SMat2I(a + z.a, b + z.b, c + z.c, d + z.d)
  1753. End Method
  1754. Rem
  1755. bbdoc: Subtracts @z from the matrix, returning a new matrix.
  1756. End Rem
  1757. Method Operator-:SMat2I(z:SMat2I)
  1758. Return New SMat2I(a - z.a, b - z.b, c - z.c, d - z.d)
  1759. End Method
  1760. Rem
  1761. bbdoc: Multiplies the matrix by @z, the dot product, returning a new matrix.
  1762. End Rem
  1763. Method Operator*:SMat2I(z:SMat2I)
  1764. Return New SMat2I(a * z.a + c * z.b, b * z.a + d * z.b, a * z.c + c * z.d, b * z.c + d * z.d)
  1765. End Method
  1766. Rem
  1767. bbdoc: Returns the transposition of the cofactor matrix.
  1768. End Rem
  1769. Method Adjoint:SMat2I()
  1770. Return New SMat2I(d, -b, -c, a)
  1771. End Method
  1772. Rem
  1773. bbdoc: Multiplies the matrix by @z by its components, return a new matrix.
  1774. End Rem
  1775. Method CompMul:SMat2I(z:SMat2I)
  1776. Return New SMat2I(a * z.a, b * z.b, c * z.c, d * z.d)
  1777. End Method
  1778. Rem
  1779. bbdoc: Returns the determinant of the matrix.
  1780. End Rem
  1781. Method Determinant:Int()
  1782. Return a * d - c * b
  1783. End Method
  1784. Rem
  1785. bbdoc: Returns the inverse of the matrix.
  1786. End Rem
  1787. Method Invert:SMat2I()
  1788. Local det:Double = a * d - c * b
  1789. If det = 0 Then
  1790. Return New SMat2I(0, 0, 0, 0)
  1791. End If
  1792. det = 1 / det
  1793. Return New SMat2I(Int(d * det), Int(-b * det), Int(-c * det), Int(a * det))
  1794. End Method
  1795. Rem
  1796. bbdoc: Rotates the matrix by @angle degrees, returning the rotated matrix.
  1797. End Rem
  1798. Method Rotate:SMat2I(angle:Double)
  1799. Local sa:Double = Sin(angle)
  1800. Local ca:Double = Cos(angle)
  1801. Return New SMat2I(Int(a * ca + c * sa), Int(b * ca + d * sa), Int(a * -sa + c * ca), Int(b * -sa + d * ca))
  1802. End Method
  1803. Rem
  1804. bbdoc: Creates a rotated matrix of @angle degrees.
  1805. End Rem
  1806. Function Rotation:SMat2I(angle:Double)
  1807. Local sa:Double = Sin(angle)
  1808. Local ca:Double = Cos(angle)
  1809. Return New SMat2I(Int(ca), Int(sa), Int(-sa), Int(ca))
  1810. End Function
  1811. Rem
  1812. bbdoc: Returns the scale of this matrix.
  1813. End Rem
  1814. Method Scale:SMat2I(s:SVec2I)
  1815. Return New SMat2I(a * s.x, b * s.x, c * s.y, d * s.y)
  1816. End Method
  1817. Rem
  1818. bbdoc: Returns the scale of this matrix.
  1819. End Rem
  1820. Method Scale:SMat2I(s:SVec2D)
  1821. Return New SMat2I(Int(a * s.x), Int(b * s.x), Int(c * s.y), Int(d * s.y))
  1822. End Method
  1823. Rem
  1824. bbdoc: Returns the scale of this matrix.
  1825. End Rem
  1826. Method Scale:SMat2I(s:SVec2F)
  1827. Return New SMat2I(Int(a * s.x), Int(b * s.x), Int(c * s.y), Int(d * s.y))
  1828. End Method
  1829. Rem
  1830. bbdoc: Creates a scaled matrix of the scale @s.
  1831. End Rem
  1832. Function Scaling:SMat2I(s:SVec2I)
  1833. Return New SMat2I(s.x, 0, 0, s.y)
  1834. End Function
  1835. Rem
  1836. bbdoc: Returns the transpose of this matrix.
  1837. End Rem
  1838. Method Transpose:SMat2I()
  1839. Return New SMat2I(a, c, b, d)
  1840. End Method
  1841. Rem
  1842. bbdoc: Returns a #String representation of the matrix.
  1843. End Rem
  1844. Method ToString:String() Override
  1845. Local sb:TStringBuilder = New TStringBuilder
  1846. sb.Append(a).Append(", ").Append(c).Append(",~n")
  1847. sb.Append(b).Append(", ").Append(d)
  1848. Return sb.ToString()
  1849. End Method
  1850. End Struct
  1851. Rem
  1852. bbdoc: An #Int backed 3x3 matrix.
  1853. End Rem
  1854. Struct SMat3I
  1855. Field ReadOnly a:Int
  1856. Field ReadOnly b:Int
  1857. Field ReadOnly c:Int
  1858. Field ReadOnly d:Int
  1859. Field ReadOnly e:Int
  1860. Field ReadOnly f:Int
  1861. Field ReadOnly g:Int
  1862. Field ReadOnly h:Int
  1863. Field ReadOnly i:Int
  1864. Rem
  1865. bbdoc: Creates a new #SMat3I from the supplied arguments.
  1866. End Rem
  1867. Method New(a:Int, b:Int, c:Int, d:Int, e:Int, f:Int, g:Int, h:Int, i:Int)
  1868. Self.a = a
  1869. Self.b = b
  1870. Self.c = c
  1871. Self.d = d
  1872. Self.e = e
  1873. Self.f = f
  1874. Self.g = g
  1875. Self.h = h
  1876. Self.i = i
  1877. End Method
  1878. Rem
  1879. bbdoc: Applies the matrix to the vector @v, returning a new vector.
  1880. End Rem
  1881. Method Apply:SVec2I(v:SVec2I)
  1882. Return New SVec2I(a * v.x + d * v.y + g, b * v.x + e * v.y + h)
  1883. End Method
  1884. Rem
  1885. bbdoc: Applies the matrix to the vector @v, returning a new vector.
  1886. End Rem
  1887. Method Apply:SVec3I(v:SVec3I)
  1888. Return New SVec3I(v.x * a + v.y * d + v.z * g, v.x * b + v.y * e + v.z * h, v.x * c + v.y * f + v.z * i)
  1889. End Method
  1890. Rem
  1891. bbdoc: Applies the matrix to the vector @v, returning a new vector.
  1892. End Rem
  1893. Method Apply:SVec4I(v:SVec4I)
  1894. Return New SVec4I(v.x * a + v.y * d + v.z * g, v.x * b + v.y * e + v.z * h, v.x * c + v.y * f + v.z * i, 0)
  1895. End Method
  1896. Rem
  1897. bbdoc: Return the 3x3 identity matrix.
  1898. End Rem
  1899. Function Identity:SMat3I()
  1900. Return New SMat3I(1, 0, 0, 0, 1, 0, 0, 0, 1)
  1901. End Function
  1902. Rem
  1903. bbdoc: Adds @z to the matrix, returning a new matrix.
  1904. End Rem
  1905. Method Operator+:SMat3I(z:SMat3I Var)
  1906. Return New SMat3I(a + z.a, b + z.b, c + z.c, d + z.d, e + z.e, f + z.f, g + z.g, h + z.h, i + z.i)
  1907. End Method
  1908. Rem
  1909. bbdoc: Subtracts @z from the matrix, returning a new matrix.
  1910. End Rem
  1911. Method Operator-:SMat3I(z:SMat3I Var)
  1912. Return New SMat3I(a - z.a, b - z.b, c - z.c, d - z.d, e - z.e, f - z.f, g - z.g, h - z.h, i - z.i)
  1913. End Method
  1914. Rem
  1915. bbdoc: Multiplies the matrix by @z, the dot product, returning a new matrix.
  1916. End Rem
  1917. Method Operator*:SMat3I(z:SMat3I Var)
  1918. Local a00:Int = a
  1919. Local a01:Int = b
  1920. Local a02:Int = c
  1921. Local a10:Int = d
  1922. Local a11:Int = e
  1923. Local a12:Int = f
  1924. Local a20:Int = g
  1925. Local a21:Int = h
  1926. Local a22:Int = i
  1927. Local b00:Int = z.a
  1928. Local b01:Int = z.b
  1929. Local b02:Int = z.c
  1930. Local b10:Int = z.d
  1931. Local b11:Int = z.e
  1932. Local b12:Int = z.f
  1933. Local b20:Int = z.g
  1934. Local b21:Int = z.h
  1935. Local b22:Int = z.i
  1936. Return New SMat3I(b00 * a00 + b01 * a10 + b02 * a20, ..
  1937. b00 * a01 + b01 * a11 + b02 * a21, ..
  1938. b00 * a02 + b01 * a12 + b02 * a22, ..
  1939. b10 * a00 + b11 * a10 + b12 * a20, ..
  1940. b10 * a01 + b11 * a11 + b12 * a21, ..
  1941. b10 * a02 + b11 * a12 + b12 * a22, ..
  1942. b20 * a00 + b21 * a10 + b22 * a20, ..
  1943. b20 * a01 + b21 * a11 + b22 * a21, ..
  1944. b20 * a02 + b21 * a12 + b22 * a22)
  1945. End Method
  1946. Rem
  1947. bbdoc: Returns the transposition of the cofactor matrix.
  1948. End Rem
  1949. Method Adjoint:SMat3I()
  1950. Return New SMat3I(e * i - f * h, ..
  1951. c * h - b * i, ..
  1952. b * f - c * e, ..
  1953. f * g - d * i, ..
  1954. a * i - c * g, ..
  1955. c * d - a * f, ..
  1956. d * h - e * g, ..
  1957. b * g - a * h, ..
  1958. a * e - b * d)
  1959. End Method
  1960. Rem
  1961. bbdoc: Multiplies the matrix by @z by its components, return a new matrix.
  1962. End Rem
  1963. Method CompMul:SMat3I(z:SMat3I Var)
  1964. Return New SMat3I(a * z.a, b * z.b, c * z.c, d * z.d, e * z.e, f * z.f, g * z.g, h * z.h, i * z.i)
  1965. End Method
  1966. Rem
  1967. bbdoc: Returns the determinant of the matrix.
  1968. End Rem
  1969. Method Determinant:Int()
  1970. Local a00:Int = a
  1971. Local a01:Int = b
  1972. Local a02:Int = c
  1973. Local a10:Int = d
  1974. Local a11:Int = e
  1975. Local a12:Int = f
  1976. Local a20:Int = g
  1977. Local a21:Int = h
  1978. Local a22:Int = i
  1979. Return a00 * ( a22 * a11 - a12 * a21) + a01 * (-a22 * a10 + a12 * a20) + a02 * ( a21 * a10 - a11 * a20)
  1980. End Method
  1981. Rem
  1982. bbdoc: Returns the inverse of the matrix.
  1983. End Rem
  1984. Method Invert:SMat3I()
  1985. Local a00:Int = a
  1986. Local a01:Int = b
  1987. Local a02:Int = c
  1988. Local a10:Int = d
  1989. Local a11:Int = e
  1990. Local a12:Int = f
  1991. Local a20:Int = g
  1992. Local a21:Int = h
  1993. Local a22:Int = i
  1994. Local b01:Int = a22 * a11 - a12 * a21
  1995. Local b11:Int = -a22 * a10 + a12 * a20
  1996. Local b21:Int = a21 * a10 - a11 * a20
  1997. Local det:Double = a00 * b01 + a01 * b11 + a02 * b21
  1998. If det = 0 Then
  1999. Return New SMat3I(0, 0, 0, 0, 0, 0, 0, 0, 0)
  2000. End If
  2001. det = 1 / det
  2002. Return New SMat3I(Int(b01 * det), ..
  2003. Int((-a22 * a01 + a02 * a21) * det), ..
  2004. Int(( a12 * a01 - a02 * a11) * det),
  2005. Int(b11 * det), ..
  2006. Int(( a22 * a00 - a02 * a20) * det), ..
  2007. Int((-a12 * a00 + a02 * a10) * det), ..
  2008. Int(b21 * det), ..
  2009. Int((-a21 * a00 + a01 * a20) * det), ..
  2010. Int(( a11 * a00 - a01 * a10) * det))
  2011. End Method
  2012. Rem
  2013. bbdoc: Rotates the matrix by @angle degrees, returning a new matrix.
  2014. End Rem
  2015. Method Rotate:SMat3I(angle:Double)
  2016. Local sa:Double = Sin(angle)
  2017. Local ca:Double = Cos(angle)
  2018. Return New SMat3I(Int(ca * a + sa * d), ..
  2019. Int(ca * b + sa * e), ..
  2020. Int(ca * c + sa * f), ..
  2021. Int(ca * d - sa * a), ..
  2022. Int(ca * e - sa * b), ..
  2023. Int(ca * f - sa * c), ..
  2024. g, h, i)
  2025. End Method
  2026. Rem
  2027. bbdoc: Retrns a rotation matrix of @angle degrees.
  2028. End Rem
  2029. Function Rotation:SMat3I(angle:Double)
  2030. Local sa:Double = Sin(angle)
  2031. Local ca:Double = Cos(angle)
  2032. Return New SMat3I(Int(ca), Int(sa), 0, Int(-sa), Int(ca), 0, 0, 0, 1)
  2033. End Function
  2034. Rem
  2035. bbdoc: Scales the matrix by @s, returning a new matrix.
  2036. End Rem
  2037. Method Scale:SMat3I(s:SVec2I)
  2038. Local bx:Int = s.x
  2039. Local by:Int = s.y
  2040. Return New SMat3I(a * bx, b * bx, c * bx, d * by, e * by, f * by, g, h, i)
  2041. End Method
  2042. Rem
  2043. bbdoc: Scales the matrix by @s, returning a new matrix.
  2044. End Rem
  2045. Method Scale:SMat3I(s:SVec2D)
  2046. Local bx:Int = s.x
  2047. Local by:Int = s.y
  2048. Return New SMat3I(Int(a * bx), Int(b * bx), Int(c * bx), Int(d * by), Int(e * by), Int(f * by), g, h, i)
  2049. End Method
  2050. Rem
  2051. bbdoc: Scales the matrix by @s, returning a new matrix.
  2052. End Rem
  2053. Method Scale:SMat3I(s:SVec2F)
  2054. Local bx:Int = s.x
  2055. Local by:Int = s.y
  2056. Return New SMat3I(Int(a * bx), Int(b * bx), Int(c * bx), Int(d * by), Int(e * by), Int(f * by), g, h, i)
  2057. End Method
  2058. Rem
  2059. bbdoc: Returns a scaling matrix of @s.
  2060. End Rem
  2061. Function Scaling:SMat3I(s:SVec2I)
  2062. Return New SMat3I(s.x, 0, 0, 0, s.y, 0, 0, 0, 1)
  2063. End Function
  2064. Rem
  2065. bbdoc: Returns a scaling matrix of @s.
  2066. End Rem
  2067. Function Scaling:SMat3I(s:SVec2D)
  2068. Return New SMat3I(Int(s.x), 0, 0, 0, Int(s.y), 0, 0, 0, 1)
  2069. End Function
  2070. Rem
  2071. bbdoc: Returns a scaling matrix of @s.
  2072. End Rem
  2073. Function Scaling:SMat3I(s:SVec2F)
  2074. Return New SMat3I(Int(s.x), 0, 0, 0, Int(s.y), 0, 0, 0, 1)
  2075. End Function
  2076. Rem
  2077. bbdoc: Returns a translation with the specified @x, @y, and @z displacements.
  2078. End Rem
  2079. Method Translate:SMat3I(x:Int, y:Int, z:Int)
  2080. Return New SMat3I( ..
  2081. a, b, c, ..
  2082. d, e, f, ..
  2083. g + a * x + b * y + c * z, ..
  2084. h + d * x + e * y + f * z, ..
  2085. i + g * x + h * y + i * z)
  2086. End Method
  2087. Rem
  2088. bbdoc: Returns a translation with displacement vector @s.
  2089. End Rem
  2090. Method Translate:SMat3I(t:SVec3I)
  2091. Return New SMat3I( ..
  2092. a, b, c, ..
  2093. d, e, f, ..
  2094. g + a * t.x + b * t.y + c * t.z, ..
  2095. h + d * t.x + e * t.y + f * t.z, ..
  2096. i + g * t.x + h * t.y + i * t.z)
  2097. End Method
  2098. Rem
  2099. bbdoc: Returns a transposition of the matrix.
  2100. End Rem
  2101. Method Transpose:SMat3I()
  2102. Return New SMat3I(a, d, g, b, e, h, c, f, i)
  2103. End Method
  2104. Rem
  2105. bbdoc: Returns a #String representation of the matrix.
  2106. End Rem
  2107. Method ToString:String() Override
  2108. Local sb:TStringBuilder = New TStringBuilder
  2109. sb.Append(a).Append(", ").Append(d).Append(", ").Append(g).Append(",~n")
  2110. sb.Append(b).Append(", ").Append(e).Append(", ").Append(h).Append(",~n")
  2111. sb.Append(c).Append(", ").Append(f).Append(", ").Append(i)
  2112. Return sb.ToString()
  2113. End Method
  2114. End Struct
  2115. Rem
  2116. bbdoc: A standard #Int backed 4x4 transformation matrix.
  2117. End Rem
  2118. Struct SMat4I
  2119. Field ReadOnly a:Int
  2120. Field ReadOnly b:Int
  2121. Field ReadOnly c:Int
  2122. Field ReadOnly d:Int
  2123. Field ReadOnly e:Int
  2124. Field ReadOnly f:Int
  2125. Field ReadOnly g:Int
  2126. Field ReadOnly h:Int
  2127. Field ReadOnly i:Int
  2128. Field ReadOnly j:Int
  2129. Field ReadOnly k:Int
  2130. Field ReadOnly l:Int
  2131. Field ReadOnly m:Int
  2132. Field ReadOnly n:Int
  2133. Field ReadOnly o:Int
  2134. Field ReadOnly p:Int
  2135. Rem
  2136. bbdoc: Creates a new #SMat4I from the supplied arguments.
  2137. End Rem
  2138. Method New(a:Int, b:Int, c:Int, d:Int, e:Int, f:Int, g:Int, h:Int, i:Int, j:Int, k:Int, l:Int, m:Int, n:Int, o:Int, p:Int)
  2139. Self.a = a
  2140. Self.b = b
  2141. Self.c = c
  2142. Self.d = d
  2143. Self.e = e
  2144. Self.f = f
  2145. Self.g = g
  2146. Self.h = h
  2147. Self.i = i
  2148. Self.j = j
  2149. Self.k = k
  2150. Self.l = l
  2151. Self.m = m
  2152. Self.n = n
  2153. Self.o = o
  2154. Self.p = p
  2155. End Method
  2156. Rem
  2157. bbdoc: Applies the matrix to the vector @v, returning a new vector.
  2158. End Rem
  2159. Method Apply:SVec2I(v:SVec2I)
  2160. Return New SVec2I(a * v.x + e * v.y + m, b * v.x + f * v.y + n)
  2161. End Method
  2162. Rem
  2163. bbdoc: Applies the 4x4 matrix @b to the vector, returning a new vector.
  2164. End Rem
  2165. Method Apply:SVec3I(v:SVec3I)
  2166. Local w:Double = d * v.x + h * v.y + l * v.z + p
  2167. If w = 0 Then
  2168. w = 1
  2169. Else
  2170. w = 1 / w
  2171. End If
  2172. Return New SVec3I(Int((a * v.x + e * v.y + i * v.z + m) * w), ..
  2173. Int((b * v.x + f * v.y + j * v.z + n) * w), ..
  2174. Int((c * v.x + g * v.y + k * v.z + o) * w))
  2175. End Method
  2176. Rem
  2177. bbdoc: Applies the 4x4 matrix @b to the vector, returning a new vector.
  2178. End Rem
  2179. Method Apply:SVec4I(v:SVec4I)
  2180. Return New SVec4I(a * v.x + e * v.y + i * v.z + m * v.w, ..
  2181. b * v.x + f * v.y + j * v.z + n * v.w, ..
  2182. c * v.x + g * v.y + k * v.z + o * v.w, ..
  2183. d * v.x + h * v.y + l * v.z + p * v.w)
  2184. End Method
  2185. Rem
  2186. bbdoc: Returns the identity matrix.
  2187. End Rem
  2188. Function Identity:SMat4I()
  2189. Return New SMat4I(1, 0, 0, 0, ..
  2190. 0, 1, 0, 0, ..
  2191. 0, 0, 1, 0, ..
  2192. 0, 0, 0, 1)
  2193. End Function
  2194. Rem
  2195. bbdoc: Adds @z to the matrix, returning a new matrix.
  2196. End Rem
  2197. Method Operator+:SMat4I(z:SMat4I Var)
  2198. Return New SMat4I(a + z.a, b + z.b, c + z.c, d + z.d, ..
  2199. e + z.e, f + z.f, g + z.g, h + z.h, ..
  2200. i + z.i, j + z.j, k + z.k, l + z.l, ..
  2201. m + z.m, n + z.n, o + z.o, p + z.p)
  2202. End Method
  2203. Rem
  2204. bbdoc: Subtracts @z from the matrix, returning a new matrix.
  2205. End Rem
  2206. Method Operator-:SMat4I(z:SMat4I Var)
  2207. Return New SMat4I(a - z.a, b - z.b, c - z.c, d - z.d, ..
  2208. e - z.e, f - z.f, g - z.g, h - z.h, ..
  2209. i - z.i, j - z.j, k - z.k, l - z.l, ..
  2210. m - z.m, n - z.n, o - z.o, p - z.p)
  2211. End Method
  2212. Rem
  2213. bbdoc: Multiplies the matrix by @z, the dot product, returning a new matrix.
  2214. End Rem
  2215. Method Operator*:SMat4I(z:SMat4I Var)
  2216. Local a00:Int = a
  2217. Local a01:Int = b
  2218. Local a02:Int = c
  2219. Local a03:Int = d
  2220. Local a10:Int = e
  2221. Local a11:Int = f
  2222. Local a12:Int = g
  2223. Local a13:Int = h
  2224. Local a20:Int = i
  2225. Local a21:Int = j
  2226. Local a22:Int = k
  2227. Local a23:Int = l
  2228. Local a30:Int = m
  2229. Local a31:Int = n
  2230. Local a32:Int = o
  2231. Local a33:Int = p
  2232. Local b00:Int = z.a
  2233. Local b01:Int = z.b
  2234. Local b02:Int = z.c
  2235. Local b03:Int = z.d
  2236. Local b10:Int = z.e
  2237. Local b11:Int = z.f
  2238. Local b12:Int = z.g
  2239. Local b13:Int = z.h
  2240. Local b20:Int = z.i
  2241. Local b21:Int = z.j
  2242. Local b22:Int = z.k
  2243. Local b23:Int = z.l
  2244. Local b30:Int = z.m
  2245. Local b31:Int = z.n
  2246. Local b32:Int = z.o
  2247. Local b33:Int = z.p
  2248. Return New SMat4I(b00 * a00 + b01 * a10 + b02 * a20 + b03 * a30, ..
  2249. b00 * a01 + b01 * a11 + b02 * a21 + b03 * a31, ..
  2250. b00 * a02 + b01 * a12 + b02 * a22 + b03 * a32, ..
  2251. b00 * a03 + b01 * a13 + b02 * a23 + b03 * a33, ..
  2252. b10 * a00 + b11 * a10 + b12 * a20 + b13 * a30, ..
  2253. b10 * a01 + b11 * a11 + b12 * a21 + b13 * a31, ..
  2254. b10 * a02 + b11 * a12 + b12 * a22 + b13 * a32, ..
  2255. b10 * a03 + b11 * a13 + b12 * a23 + b13 * a33, ..
  2256. b20 * a00 + b21 * a10 + b22 * a20 + b23 * a30, ..
  2257. b20 * a01 + b21 * a11 + b22 * a21 + b23 * a31, ..
  2258. b20 * a02 + b21 * a12 + b22 * a22 + b23 * a32, ..
  2259. b20 * a03 + b21 * a13 + b22 * a23 + b23 * a33, ..
  2260. b30 * a00 + b31 * a10 + b32 * a20 + b33 * a30, ..
  2261. b30 * a01 + b31 * a11 + b32 * a21 + b33 * a31, ..
  2262. b30 * a02 + b31 * a12 + b32 * a22 + b33 * a32, ..
  2263. b30 * a03 + b31 * a13 + b32 * a23 + b33 * a33)
  2264. End Method
  2265. Rem
  2266. bbdoc: Returns the transposition of the cofactor matrix.
  2267. End Rem
  2268. Method Adjoint:SMat4I()
  2269. Local a00:Int = a
  2270. Local a01:Int = b
  2271. Local a02:Int = c
  2272. Local a03:Int = d
  2273. Local a10:Int = e
  2274. Local a11:Int = f
  2275. Local a12:Int = g
  2276. Local a13:Int = h
  2277. Local a20:Int = i
  2278. Local a21:Int = j
  2279. Local a22:Int = k
  2280. Local a23:Int = l
  2281. Local a30:Int = m
  2282. Local a31:Int = n
  2283. Local a32:Int = o
  2284. Local a33:Int = p
  2285. Return New SMat4I(a11 * (a22 * a33 - a23 * a32) - a21 * (a12 * a33 - a13 * a32) + a31 * (a12 * a23 - a13 * a22), ..
  2286. -(a01 * (a22 * a33 - a23 * a32) - a21 * (a02 * a33 - a03 * a32) + a31 * (a02 * a23 - a03 * a22)), ..
  2287. a01 * (a12 * a33 - a13 * a32) - a11 * (a02 * a33 - a03 * a32) + a31 * (a02 * a13 - a03 * a12), ..
  2288. -(a01 * (a12 * a23 - a13 * a22) - a11 * (a02 * a23 - a03 * a22) + a21 * (a02 * a13 - a03 * a12)), ..
  2289. -(a10 * (a22 * a33 - a23 * a32) - a20 * (a12 * a33 - a13 * a32) + a30 * (a12 * a23 - a13 * a22)), ..
  2290. a00 * (a22 * a33 - a23 * a32) - a20 * (a02 * a33 - a03 * a32) + a30 * (a02 * a23 - a03 * a22), ..
  2291. -(a00 * (a12 * a33 - a13 * a32) - a10 * (a02 * a33 - a03 * a32) + a30 * (a02 * a13 - a03 * a12)), ..
  2292. a00 * (a12 * a23 - a13 * a22) - a10 * (a02 * a23 - a03 * a22) + a20 * (a02 * a13 - a03 * a12), ..
  2293. a10 * (a21 * a33 - a23 * a31) - a20 * (a11 * a33 - a13 * a31) + a30 * (a11 * a23 - a13 * a21), ..
  2294. -(a00 * (a21 * a33 - a23 * a31) - a20 * (a01 * a33 - a03 * a31) + a30 * (a01 * a23 - a03 * a21)), ..
  2295. a00 * (a11 * a33 - a13 * a31) - a10 * (a01 * a33 - a03 * a31) + a30 * (a01 * a13 - a03 * a11), ..
  2296. -(a00 * (a11 * a23 - a13 * a21) - a10 * (a01 * a23 - a03 * a21) + a20 * (a01 * a13 - a03 * a11)), ..
  2297. -(a10 * (a21 * a32 - a22 * a31) - a20 * (a11 * a32 - a12 * a31) + a30 * (a11 * a22 - a12 * a21)), ..
  2298. a00 * (a21 * a32 - a22 * a31) - a20 * (a01 * a32 - a02 * a31) + a30 * (a01 * a22 - a02 * a21), ..
  2299. -(a00 * (a11 * a32 - a12 * a31) - a10 * (a01 * a32 - a02 * a31) + a30 * (a01 * a12 - a02 * a11)), ..
  2300. a00 * (a11 * a22 - a12 * a21) - a10 * (a01 * a22 - a02 * a21) + a20 * (a01 * a12 - a02 * a11))
  2301. End Method
  2302. Rem
  2303. bbdoc: Multiplies the matrix by @z by its components, returning a new matrix.
  2304. End Rem
  2305. Method CompMul:SMat4I(z:SMat4I Var)
  2306. Return New SMat4I(a * z.a, b * z.b, c * z.c, d * z.d, ..
  2307. e * z.e, f * z.f, g * z.g, h * z.h, ..
  2308. i * z.i, j * z.j, k * z.k, l * z.l, ..
  2309. m * z.m, n * z.n, o * z.o, p * z.p)
  2310. End Method
  2311. Rem
  2312. bbdoc: Returns the determinant of the matrix.
  2313. End Rem
  2314. Method Determinant:Int()
  2315. Local a00:Int = a
  2316. Local a01:Int = b
  2317. Local a02:Int = c
  2318. Local a03:Int = d
  2319. Local a10:Int = e
  2320. Local a11:Int = f
  2321. Local a12:Int = g
  2322. Local a13:Int = h
  2323. Local a20:Int = i
  2324. Local a21:Int = j
  2325. Local a22:Int = k
  2326. Local a23:Int = l
  2327. Local a30:Int = m
  2328. Local a31:Int = n
  2329. Local a32:Int = o
  2330. Local a33:Int = p
  2331. Local b00:Int = a00 * a11 - a01 * a10
  2332. Local b01:Int = a00 * a12 - a02 * a10
  2333. Local b02:Int = a00 * a13 - a03 * a10
  2334. Local b03:Int = a01 * a12 - a02 * a11
  2335. Local b04:Int = a01 * a13 - a03 * a11
  2336. Local b05:Int = a02 * a13 - a03 * a12
  2337. Local b06:Int = a20 * a31 - a21 * a30
  2338. Local b07:Int = a20 * a32 - a22 * a30
  2339. Local b08:Int = a20 * a33 - a23 * a30
  2340. Local b09:Int = a21 * a32 - a22 * a31
  2341. Local b10:Int = a21 * a33 - a23 * a31
  2342. Local b11:Int = a22 * a33 - a23 * a32
  2343. Return b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06
  2344. End Method
  2345. Rem
  2346. bbdoc: Returns a projection matrix with a viewing frustum defined by the plane coordinates passed in.
  2347. End Rem
  2348. Function Frustum:SMat4I(l:Double, r:Double, b:Double, t:Double, n:Double, f:Double)
  2349. Local rl:Double = 1.0 / (r - l)
  2350. Local tb:Double = 1.0 / (t - b)
  2351. Local nf:Double = 1.0 / (n - f)
  2352. Return New SMat4I(Int((2.0 * n) * rl), 0, 0, 0, ..
  2353. 0, Int((2.0 * n) * tb), 0, 0, ..
  2354. Int((r + l) * rl), Int((t + b) * tb), Int((f + n) * nf), -1, ..
  2355. 0, 0, Int((2.0 * n * f) * nf), 0)
  2356. End Function
  2357. Rem
  2358. bbdoc: The inverse of this matrix.
  2359. about: An inverted matrix is such that if multiplied by the original would result in identity matrix.
  2360. If some matrix transforms vectors in a particular way, then the inverse matrix can transform them back.
  2361. End Rem
  2362. Method Invert:SMat4I()
  2363. Local a00:Int = a
  2364. Local a01:Int = b
  2365. Local a02:Int = c
  2366. Local a03:Int = d
  2367. Local a10:Int = e
  2368. Local a11:Int = f
  2369. Local a12:Int = g
  2370. Local a13:Int = h
  2371. Local a20:Int = i
  2372. Local a21:Int = j
  2373. Local a22:Int = k
  2374. Local a23:Int = l
  2375. Local a30:Int = m
  2376. Local a31:Int = n
  2377. Local a32:Int = o
  2378. Local a33:Int = p
  2379. Local b00:Int = a00 * a11 - a01 * a10
  2380. Local b01:Int = a00 * a12 - a02 * a10
  2381. Local b02:Int = a00 * a13 - a03 * a10
  2382. Local b03:Int = a01 * a12 - a02 * a11
  2383. Local b04:Int = a01 * a13 - a03 * a11
  2384. Local b05:Int = a02 * a13 - a03 * a12
  2385. Local b06:Int = a20 * a31 - a21 * a30
  2386. Local b07:Int = a20 * a32 - a22 * a30
  2387. Local b08:Int = a20 * a33 - a23 * a30
  2388. Local b09:Int = a21 * a32 - a22 * a31
  2389. Local b10:Int = a21 * a33 - a23 * a31
  2390. Local b11:Int = a22 * a33 - a23 * a32
  2391. Local det:Int = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06
  2392. If det = 0 Then
  2393. Return New SMat4I(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
  2394. End If
  2395. det = 1 / det
  2396. Return New SMat4I((a11 * b11 - a12 * b10 + a13 * b09) * det, ..
  2397. (a02 * b10 - a01 * b11 - a03 * b09) * det, ..
  2398. (a31 * b05 - a32 * b04 + a33 * b03) * det, ..
  2399. (a22 * b04 - a21 * b05 - a23 * b03) * det, ..
  2400. (a12 * b08 - a10 * b11 - a13 * b07) * det, ..
  2401. (a00 * b11 - a02 * b08 + a03 * b07) * det, ..
  2402. (a32 * b02 - a30 * b05 - a33 * b01) * det, ..
  2403. (a20 * b05 - a22 * b02 + a23 * b01) * det, ..
  2404. (a10 * b10 - a11 * b08 + a13 * b06) * det, ..
  2405. (a01 * b08 - a00 * b10 - a03 * b06) * det, ..
  2406. (a30 * b04 - a31 * b02 + a33 * b00) * det, ..
  2407. (a21 * b02 - a20 * b04 - a23 * b00) * det, ..
  2408. (a11 * b07 - a10 * b09 - a12 * b06) * det, ..
  2409. (a00 * b09 - a01 * b07 + a02 * b06) * det, ..
  2410. (a31 * b01 - a30 * b03 - a32 * b00) * det, ..
  2411. (a20 * b03 - a21 * b01 + a22 * b00) * det)
  2412. End Method
  2413. Rem
  2414. bbdoc: Computes a transformation matrix that corresponds to a camera viewing the @eye from the @pos.
  2415. about: The right-hand vector is perpendicular to the up vector.
  2416. End Rem
  2417. Function LookAt:SMat4I(eye:SVec3I, pos:SVec3I, up:SVec3I)
  2418. Local ex:Int = eye.x
  2419. Local ey:Int = eye.y
  2420. Local ez:Int = eye.z
  2421. Local px:Int = pos.x
  2422. Local py:Int = pos.y
  2423. Local pz:Int = pos.z
  2424. Local ux:Int = up.x
  2425. Local uy:Int = up.y
  2426. Local uz:Int = up.z
  2427. Local z0:Int = ex - px
  2428. Local z1:Int = ey - py
  2429. Local z2:Int = ez - pz
  2430. If z0 = 0 Or z1 = 0 Or z2 = 0 Then
  2431. Return Identity()
  2432. End If
  2433. Local length:Int = Sqr(z0 * z0 + z1 * z1 + z2 * z2)
  2434. z0 :* length
  2435. z1 :* length
  2436. z2 :* length
  2437. Local x0:Int = uy * z2 - uz * z1
  2438. Local x1:Int = uz * z0 - ux * z2
  2439. Local x2:Int = ux * z1 - uy * z0
  2440. length = Sqr(x0 * x0 + x1 * x1 + x2 * x2)
  2441. If length = 0 Then
  2442. x0 = 0
  2443. x1 = 0
  2444. x2 = 0
  2445. Else
  2446. length = 1 / length
  2447. x0 :* length
  2448. x1 :* length
  2449. x2 :* length
  2450. End If
  2451. Local y0:Int = z1 * x2 - z2 * x1
  2452. Local y1:Int = z2 * x0 - z0 * x2
  2453. Local y2:Int = z0 * x1 - z1 * x0
  2454. length = Sqr(y0 * y0 + y1 * y1 + y2 * y2)
  2455. If length = 0 Then
  2456. y0 = 0
  2457. y1 = 0
  2458. y2 = 0
  2459. Else
  2460. length = 1 / length
  2461. y0 :* length
  2462. y1 :* length
  2463. y2 :* length
  2464. End If
  2465. Return New SMat4I(x0, y0, z0, 0, x1, y1, z1, 0, x2, y2, z2, 0, ..
  2466. -(x0 * ex + x1 * ey + x2 * ez), -(y0 * ex + y1 * ey + y2 * ez), -(z0 * ex + z1 * ey + z2 * ez), 1)
  2467. End Function
  2468. Rem
  2469. bbdoc: Creates an orthogonal projection matrix.
  2470. about: The returned matrix, when used as a Camera's projection matrix, creates a view showing the area between @width and @height, with @zNear and @zFar as the near and far depth clipping planes.
  2471. End Rem
  2472. Function Orthogonal:SMat4I(width:Double, height:Double, zNear:Double, zFar:Double)
  2473. Local nf:Double = 1.0 / (zNear - zFar)
  2474. Return New SMat4I(Int(2.0 / width), 0, 0, 0, ..
  2475. 0, Int(2.0 / height), 0, 0, ..
  2476. 0, 0, Int(2.0 * nf), 0, ..
  2477. 0, 0, Int((zNear + zFar) * nf), 1)
  2478. End Function
  2479. Rem
  2480. bbdoc: Creates a Perspective projection matrix.
  2481. End Rem
  2482. Function Perspective:SMat4I(fov:Double, w:Double, h:Double, n:Double, f:Double)
  2483. Local ft:Double = 1.0 / Tan(fov * 0.5)
  2484. Local nf:Double = 1.0 / (n - f)
  2485. Return New SMat4I(Int(ft), 0, 0, 0, ..
  2486. 0, Int(ft * w / h), 0, 0, ..
  2487. 0, 0, Int((f + n) * nf), -1, ..
  2488. 0, 0, Int((2.0 * f * n) * nf), 0)
  2489. End Function
  2490. Rem
  2491. bbdoc: Creates a rotation matrix, rotated @angle degrees around the point @axis.
  2492. End Rem
  2493. Method Rotate:SMat4I(axis:SVec3I, angle:Double)
  2494. Local x:Int = axis.x
  2495. Local y:Int = axis.y
  2496. Local z:Int = axis.z
  2497. Local a00:Int = a
  2498. Local a01:Int = b
  2499. Local a02:Int = c
  2500. Local a03:Int = d
  2501. Local a10:Int = e
  2502. Local a11:Int = f
  2503. Local a12:Int = g
  2504. Local a13:Int = h
  2505. Local a20:Int = i
  2506. Local a21:Int = j
  2507. Local a22:Int = k
  2508. Local a23:Int = l
  2509. Local sa:Double = Sin(angle)
  2510. Local ca:Double = Cos(angle)
  2511. Local t:Double = 1 - ca
  2512. Local b00:Double = x * x * t + ca
  2513. Local b01:Double = y * x * t + z * sa
  2514. Local b02:Double = z * x * t - y * sa
  2515. Local b10:Double = x * y * t - z * sa
  2516. Local b11:Double = y * y * t + ca
  2517. Local b12:Double = z * y * t + x * sa
  2518. Local b20:Double = x * z * t + y * sa
  2519. Local b21:Double = y * z * t - x * sa
  2520. Local b22:Double = z * z * t + ca
  2521. Return New SMat4I(Int(a00 * b00 + a10 * b01 + a20 * b02), ..
  2522. Int(a01 * b00 + a11 * b01 + a21 * b02), ..
  2523. Int(a02 * b00 + a12 * b01 + a22 * b02), ..
  2524. Int(a03 * b00 + a13 * b01 + a23 * b02), ..
  2525. Int(a00 * b10 + a10 * b11 + a20 * b12), ..
  2526. Int(a01 * b10 + a11 * b11 + a21 * b12), ..
  2527. Int(a02 * b10 + a12 * b11 + a22 * b12), ..
  2528. Int(a03 * b10 + a13 * b11 + a23 * b12), ..
  2529. Int(a00 * b20 + a10 * b21 + a20 * b22), ..
  2530. Int(a01 * b20 + a11 * b21 + a21 * b22), ..
  2531. Int(a02 * b20 + a12 * b21 + a22 * b22), ..
  2532. Int(a03 * b20 + a13 * b21 + a23 * b22), ..
  2533. m, n, o, p)
  2534. End Method
  2535. Rem
  2536. bbdoc: Returns a rotation matrix on the given @axis and @angle degrees.
  2537. End Rem
  2538. Function Rotation:SMat4I(axis:SVec3I, angle:Double)
  2539. Local x:Int = axis.x
  2540. Local y:Int = axis.y
  2541. Local z:Int = axis.z
  2542. Local sa:Double = Sin(angle)
  2543. Local ca:Double = Cos(angle)
  2544. Local t:Double = 1 - ca
  2545. Return New SMat4I(Int(x * x * t + ca), ..
  2546. Int(y * x * t + z * sa), ..
  2547. Int(z * x * t - y * sa), ..
  2548. 0, ..
  2549. Int(x * y * t - z * sa), ..
  2550. Int(y * y * t + ca), ..
  2551. Int(z * y * t + x * sa), ..
  2552. 0, ..
  2553. Int(x * z * t + y * sa), ..
  2554. Int(y * z * t - x * sa), ..
  2555. Int(z * z * t + ca), ..
  2556. 0, 0, 0, 0, 1)
  2557. End Function
  2558. Rem
  2559. bbdoc: Scales the matrix, return the new scaled matrix.
  2560. End Rem
  2561. Method Scale:SMat4I(s:SVec3I)
  2562. Local bx:Int = s.x
  2563. Local by:Int = s.y
  2564. Local bz:Int = s.z
  2565. Return New SMat4I(a * bx, b * bx, c * bx, d * bx, ..
  2566. e * by, f * by, g * by, h * by, ..
  2567. i * bz, j * bz, k * bz, l * bz, ..
  2568. m, n, o, p)
  2569. End Method
  2570. Rem
  2571. bbdoc: Scales the matrix, return the new scaled matrix.
  2572. End Rem
  2573. Method Scale:SMat4I(s:SVec3D)
  2574. Local bx:Double = s.x
  2575. Local by:Double = s.y
  2576. Local bz:Double = s.z
  2577. Return New SMat4I(Int(a * bx), Int(b * bx), Int(c * bx), Int(d * bx), ..
  2578. Int(e * by), Int(f * by), Int(g * by), Int(h * by), ..
  2579. Int(i * bz), Int(j * bz), Int(k * bz), Int(l * bz), ..
  2580. m, n, o, p)
  2581. End Method
  2582. Rem
  2583. bbdoc: Scales the matrix, return the new scaled matrix.
  2584. End Rem
  2585. Method Scale:SMat4I(s:SVec3F)
  2586. Local bx:Float = s.x
  2587. Local by:Float = s.y
  2588. Local bz:Float = s.z
  2589. Return New SMat4I(Int(a * bx), Int(b * bx), Int(c * bx), Int(d * bx), ..
  2590. Int(e * by), Int(f * by), Int(g * by), Int(h * by), ..
  2591. Int(i * bz), Int(j * bz), Int(k * bz), Int(l * bz), ..
  2592. m, n, o, p)
  2593. End Method
  2594. Rem
  2595. bbdoc: Creates a scaling matrix.
  2596. End Rem
  2597. Function Scaling:SMat4I(s:SVec3I)
  2598. Return New SMat4I(s.x, 0, 0, 0, 0, s.y, 0, 0, 0, 0, s.z, 0, 0, 0, 0, 1)
  2599. End Function
  2600. Rem
  2601. bbdoc: Creates a scaling matrix.
  2602. End Rem
  2603. Function Scaling:SMat4I(s:SVec3D)
  2604. Return New SMat4I(Int(s.x), 0, 0, 0, 0, Int(s.y), 0, 0, 0, 0, Int(s.z), 0, 0, 0, 0, 1)
  2605. End Function
  2606. Rem
  2607. bbdoc: Creates a scaling matrix.
  2608. End Rem
  2609. Function Scaling:SMat4I(s:SVec3F)
  2610. Return New SMat4I(Int(s.x), 0, 0, 0, 0, Int(s.y), 0, 0, 0, 0, Int(s.z), 0, 0, 0, 0, 1)
  2611. End Function
  2612. Rem
  2613. bbdoc: Returns the transpose of this matrix.
  2614. about: The transposed matrix is the one that has the columns exchanged with its rows.
  2615. End Rem
  2616. Method Transpose:SMat4I()
  2617. Return New SMat4I(a, e, i, m, b, f, j, n, c, g, k, o, d, h, l, p)
  2618. End Method
  2619. Rem
  2620. bbdoc: Translates the matrix to @s.
  2621. End Rem
  2622. Method Translate:SMat4I(s:SVec3I)
  2623. Local bx:Int = s.x
  2624. Local by:Int = s.y
  2625. Local bz:Int = s.z
  2626. Return New SMat4I(a, b, c, d, e, f, g, h, i, j, k, l, ..
  2627. a * bx + e * by + i * bz + m, ..
  2628. b * bx + f * by + j * bz + n, ..
  2629. c * bx + g * by + k * bz + o, ..
  2630. d * bx + h * by + l * bz + p)
  2631. End Method
  2632. Rem
  2633. bbdoc: Translates the matrix to @s.
  2634. End Rem
  2635. Method Translate:SMat4I(s:SVec3D)
  2636. Local bx:Double = s.x
  2637. Local by:Double = s.y
  2638. Local bz:Double = s.z
  2639. Return New SMat4I(a, b, c, d, e, f, g, h, i, j, k, l, ..
  2640. Int(a * bx + e * by + i * bz + m), ..
  2641. Int(b * bx + f * by + j * bz + n), ..
  2642. Int(c * bx + g * by + k * bz + o), ..
  2643. Int(d * bx + h * by + l * bz + p))
  2644. End Method
  2645. Rem
  2646. bbdoc: Translates the matrix To @s.
  2647. End Rem
  2648. Method Translate:SMat4I(s:SVec3F)
  2649. Local bx:Float = s.x
  2650. Local by:Float = s.y
  2651. Local bz:Float = s.z
  2652. Return New SMat4I(a, b, c, d, e, f, g, h, i, j, k, l, ..
  2653. Int(a * bx + e * by + i * bz + m), ..
  2654. Int(b * bx + f * by + j * bz + n), ..
  2655. Int(c * bx + g * by + k * bz + o), ..
  2656. Int(d * bx + h * by + l * bz + p))
  2657. End Method
  2658. Rem
  2659. bbdoc: Creates a translation matrix.
  2660. End Rem
  2661. Function Translation:SMat4I(s:SVec3I)
  2662. Return New SMat4I(1, 0, 0, 0, ..
  2663. 0, 1, 0, 0, ..
  2664. 0, 0, 1, 0, ..
  2665. s.x, s.y, s.z, 1)
  2666. End Function
  2667. Rem
  2668. bbdoc: Creates a translation matrix.
  2669. End Rem
  2670. Function Translation:SMat4I(s:SVec3D)
  2671. Return New SMat4I(1, 0, 0, 0, ..
  2672. 0, 1, 0, 0, ..
  2673. 0, 0, 1, 0, ..
  2674. Int(s.x), Int(s.y), Int(s.z), 1)
  2675. End Function
  2676. Rem
  2677. bbdoc: Creates a translation matrix.
  2678. End Rem
  2679. Function Translation:SMat4I(s:SVec3F)
  2680. Return New SMat4I(1, 0, 0, 0, ..
  2681. 0, 1, 0, 0, ..
  2682. 0, 0, 1, 0, ..
  2683. Int(s.x), Int(s.y), Int(s.z), 1)
  2684. End Function
  2685. Rem
  2686. bbdoc: Returns a #String representation of the matrix.
  2687. End Rem
  2688. Method ToString:String() Override
  2689. Local sb:TStringBuilder = New TStringBuilder
  2690. sb.Append(a).Append(", ").Append(e).Append(", ").Append(i).Append(", ").Append(m).Append(",~n")
  2691. sb.Append(b).Append(", ").Append(f).Append(", ").Append(j).Append(", ").Append(n).Append(",~n")
  2692. sb.Append(c).Append(", ").Append(g).Append(", ").Append(k).Append(", ").Append(o).Append(",~n")
  2693. sb.Append(d).Append(", ").Append(h).Append(", ").Append(l).Append(", ").Append(p)
  2694. Return sb.ToString()
  2695. End Method
  2696. End Struct