utcmatrix3d.pp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2023 by Michael Van Canneyt
  4. member of the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit utcmatrix3d;
  12. {$mode ObjFPC}{$H+}
  13. interface
  14. uses
  15. Classes, SysUtils, fpcunit, testregistry, utmathvectorbase, types, system.math.vectors;
  16. Type
  17. { TTestMatrix3D }
  18. TTestMatrix3D = class(TCMathVectorsBase)
  19. Private
  20. FM : Array[1..3] of TMatrix3D;
  21. procedure ClearMatrices;
  22. function GetM(AIndex: Integer): TMatrix3D;
  23. procedure SetM(AIndex: Integer; AValue: TMatrix3D);
  24. Protected
  25. procedure SetUp; override;
  26. procedure TearDown; override;
  27. Property M1 : TMatrix3D Index 1 Read GetM Write SetM;
  28. Property M2 : TMatrix3D Index 2 Read GetM Write SetM;
  29. Property M3 : TMatrix3D Index 3 Read GetM Write SetM;
  30. Published
  31. procedure TestHookUp;
  32. constructor TestCreate;
  33. constructor TestCreateArray;
  34. Procedure TestZero;
  35. Procedure TestCreateLookAtDirLH;
  36. Procedure TestCreateLookAtDirRH;
  37. Procedure TestCreateLookAtLH;
  38. Procedure TestCreateLookAtRH;
  39. Procedure TestCreateOrthoLH;
  40. Procedure TestCreateOrthoOffCenterLH;
  41. Procedure TestCreateOrthoOffCenterRH;
  42. Procedure TestCreateOrthoRH;
  43. Procedure TestCreatePerspectiveFovLH;
  44. Procedure TestCreatePerspectiveFovRH;
  45. Procedure TestCreateRotation;
  46. Procedure TestCreateRotationX;
  47. Procedure TestCreateRotationY;
  48. Procedure TestCreateRotationZ;
  49. Procedure TestCreateRotationHeadingPitchBank;
  50. Procedure TestCreateRotationYawPitchRoll;
  51. Procedure TestCreateScaling;
  52. Procedure TestCreateTranslation;
  53. Procedure TestMultiplyPoint;
  54. Procedure TestMultiplyMatrix;
  55. Procedure TestMultiplyVector3D;
  56. Procedure TestMultiplyFactor;
  57. Procedure TestMultiplyFactor2;
  58. Procedure TestDiv;
  59. Procedure TestAdjoint;
  60. Procedure TestDeterminant;
  61. Procedure TestEyePosition;
  62. Procedure TestInverse;
  63. Procedure TestScale;
  64. Procedure TestToMatrix;
  65. Procedure TestTranspose;
  66. end;
  67. implementation
  68. procedure TTestMatrix3D.ClearMatrices;
  69. var
  70. I : integer;
  71. begin
  72. For I:=1 to 3 do
  73. FM[I]:=Default(TMatrix3D);
  74. end;
  75. Function TTestMatrix3D.GetM(AIndex: Integer): TMatrix3D;
  76. begin
  77. Result:=FM[aIndex];
  78. end;
  79. procedure TTestMatrix3D.SetM(AIndex: Integer; AValue: TMatrix3D);
  80. begin
  81. FM[aIndex]:=aValue;
  82. end;
  83. procedure TTestMatrix3D.SetUp;
  84. begin
  85. inherited SetUp;
  86. ClearMatrices;
  87. end;
  88. procedure TTestMatrix3D.TearDown;
  89. begin
  90. inherited TearDown;
  91. ClearMatrices;
  92. end;
  93. procedure TTestMatrix3D.TestHookUp;
  94. var
  95. I,C,R : Integer;
  96. begin
  97. For I:=1 to 3 do
  98. For R:=0 to 3 do
  99. For C:=0 to 3 do
  100. AssertEquals(Format('M%d[%d,%d]',[I,C,R]),0.0,FM[I].M[R].V[C]);
  101. end;
  102. constructor TTestMatrix3D.TestCreate;
  103. begin
  104. M1:=TMatrix3D.Create(1,2,3,4,
  105. 5,6,7,8,
  106. 9,10,11,12,
  107. 13,14,15,16);
  108. // actual test
  109. AssertMatrix3D('Create',1,2,3,4,
  110. 5,6,7,8,
  111. 9,10,11,12,
  112. 13,14,15,16,M1);
  113. // Test assert using array
  114. AssertMatrix3D('Create',[1,2,3,4,
  115. 5,6,7,8,
  116. 9,10,11,12,
  117. 13,14,15,16],M1);
  118. // Test assert using second matrix
  119. M2:=M1;
  120. AssertMatrix3D('Create',M2,M1);
  121. end;
  122. constructor TTestMatrix3D.TestCreateArray;
  123. begin
  124. M1:=TMatrix3D.Create([ 1, 2, 3, 4,
  125. 5, 6, 7, 8,
  126. 9, 10, 11, 12,
  127. 13, 14, 15, 16]);
  128. AssertMatrix3D('Create',1, 5, 9, 13,
  129. 2, 6, 10, 14,
  130. 3, 7, 11, 15,
  131. 4, 8, 12, 16, M1);
  132. end;
  133. procedure TTestMatrix3D.TestZero;
  134. begin
  135. M1:=TMatrix3D.Zero;
  136. AssertMatrix3D('Create',0,0,0,0,
  137. 0,0,0,0,
  138. 0,0,0,0,
  139. 0,0,0,0, M1);
  140. end;
  141. procedure TTestMatrix3D.TestCreateLookAtDirLH;
  142. begin
  143. M1:=TMatrix3D.CreateLookAtDirLH(Point3D(0,0,0),Point3D(0,0,1),Point3D(0,0,1));
  144. // test 46
  145. AssertMatrix3D('Create 1',[ 0.0000, 0.0000, 0.0000, 0.0000,
  146. 0.0000, 0.0000, 0.0000, 0.0000,
  147. 0.0000, 0.0000,-1.0000, 0.0000,
  148. 0.0000, 0.0000, 0.0000, 1.0000],M1);
  149. // test 47
  150. M1:=TMatrix3D.CreateLookAtDirLH(Point3D(0,0,0),Point3D(0,1,0),Point3D(0,0,1));
  151. AssertMatrix3D('Create 2',[ 1.0000, 0.0000, 0.0000, 0.0000,
  152. 0.0000, 0.0000,-1.0000, 0.0000,
  153. 0.0000, 1.0000, 0.0000, 0.0000,
  154. 0.0000, 0.0000, 0.0000, 1.0000],M1);
  155. // test 48
  156. M1:=TMatrix3D.CreateLookAtDirLH(Point3D(0,0,0),Point3D(1,0,0),Point3D(0,0,1));
  157. AssertMatrix3D('Create 3',[ 0.0000, 0.0000,-1.0000, 0.0000,
  158. -1.0000, 0.0000, 0.0000, 0.0000,
  159. 0.0000, 1.0000, 0.0000, 0.0000,
  160. 0.0000, 0.0000, 0.0000, 1.0000],M1);
  161. end;
  162. procedure TTestMatrix3D.TestCreateLookAtDirRH;
  163. begin
  164. // test 49
  165. M1:=TMatrix3D.CreateLookAtDirRH(Point3D(0,0,0),Point3D(0,0,1),Point3D(0,0,1));
  166. AssertMatrix3D('Create 1',[ 0.0000, 0.0000, 0.0000, 0.0000,
  167. 0.0000, 0.0000, 0.0000, 0.0000,
  168. 0.0000, 0.0000, 1.0000, 0.0000,
  169. 0.0000, 0.0000, 0.0000, 1.0000],M1);
  170. // test 50
  171. M1:=TMatrix3D.CreateLookAtDirRH(Point3D(0,0,0),Point3D(0,1,0),Point3D(0,0,1));
  172. AssertMatrix3D('Create 2',[ -1.0000, 0.0000, 0.0000, 0.0000,
  173. 0.0000, 0.0000, 1.0000, 0.0000,
  174. 0.0000, 1.0000, 0.0000, 0.0000,
  175. 0.0000, 0.0000, 0.0000, 1.0000],M1);
  176. // test 51
  177. M1:=TMatrix3D.CreateLookAtDirRH(Point3D(0,0,0),Point3D(1,0,0),Point3D(0,0,1));
  178. AssertMatrix3D('Create 3',[ 0.0000, 0.0000, 1.0000, 0.0000,
  179. 1.0000, 0.0000, 0.0000, 0.0000,
  180. 0.0000, 1.0000, 0.0000, 0.0000,
  181. 0.0000, 0.0000, 0.0000, 1.0000],M1);
  182. end;
  183. procedure TTestMatrix3D.TestCreateLookAtLH;
  184. begin
  185. M1:=TMatrix3D.CreateLookAtLH(Point3D(0,0,0),Point3D(0,0,1),Point3D(0,0,1));
  186. // test 52
  187. AssertMatrix3D('Create 1',[ 0.0000, 0.0000, 0.0000, 0.0000,
  188. 0.0000, 0.0000, 0.0000, 0.0000,
  189. 0.0000, 0.0000, 1.0000, 0.0000,
  190. 0.0000, 0.0000, 0.0000, 1.0000],M1);
  191. // test 53
  192. M1:=TMatrix3D.CreateLookAtLH(Point3D(0,0,0),Point3D(0,1,0),Point3D(0,0,1));
  193. AssertMatrix3D('Create 2',[ -1.0000, 0.0000, 0.0000, 0.0000,
  194. 0.0000, 0.0000, 1.0000, 0.0000,
  195. 0.0000, 1.0000, 0.0000, 0.0000,
  196. 0.0000, 0.0000, 0.0000, 1.0000],M1);
  197. // test 54
  198. M1:=TMatrix3D.CreateLookAtLH(Point3D(0,0,0),Point3D(1,0,0),Point3D(0,0,1));
  199. AssertMatrix3D('Create 3',[ 0.0000, 0.0000, 1.0000, 0.0000,
  200. 1.0000, 0.0000, 0.0000, 0.0000,
  201. 0.0000, 1.0000, 0.0000, 0.0000,
  202. 0.0000, 0.0000, 0.0000, 1.0000],M1);
  203. end;
  204. procedure TTestMatrix3D.TestCreateLookAtRH;
  205. begin
  206. M1:=TMatrix3D.CreateLookAtRH(Point3D(0,0,0),Point3D(0,0,1),Point3D(0,0,1));
  207. // test 55
  208. AssertMatrix3D('Create 1',[ 0.0000, 0.0000, 0.0000, 0.0000,
  209. 0.0000, 0.0000, 0.0000, 0.0000,
  210. 0.0000, 0.0000,-1.0000, 0.0000,
  211. 0.0000, 0.0000, 0.0000, 1.0000],M1);
  212. // test 56
  213. M1:=TMatrix3D.CreateLookAtRH(Point3D(0,0,0),Point3D(0,1,0),Point3D(0,0,1));
  214. AssertMatrix3D('Create 2',[ 1.0000, 0.0000, 0.0000, 0.0000,
  215. 0.0000, 0.0000,-1.0000, 0.0000,
  216. 0.0000, 1.0000, 0.0000, 0.0000,
  217. 0.0000, 0.0000, 0.0000, 1.0000],M1);
  218. // test 57
  219. M1:=TMatrix3D.CreateLookAtRH(Point3D(0,0,0),Point3D(1,0,0),Point3D(0,0,1));
  220. AssertMatrix3D('Create 3',[ 0.0000, 0.0000,-1.0000, 0.0000,
  221. -1.0000, 0.0000, 0.0000, 0.0000,
  222. 0.0000, 1.0000, 0.0000, 0.0000,
  223. 0.0000, 0.0000, 0.0000, 1.0000],M1);
  224. end;
  225. procedure TTestMatrix3D.TestCreateOrthoLH;
  226. begin
  227. // test 58
  228. M1:=TMatrix3D.CreateOrthoLH(1,1,0,1);
  229. AssertMatrix3D('Create 1',[ 2.0000, 0.0000, 0.0000, 0.0000,
  230. 0.0000, 2.0000, 0.0000, 0.0000,
  231. 0.0000, 0.0000, 1.0000, 0.0000,
  232. 0.0000, 0.0000, 0.0000, 1.0000],M1);
  233. // test 59
  234. M1:=TMatrix3D.CreateOrthoLH(1,1,0,2);
  235. AssertMatrix3D('Create 2',[2.0000, 0.0000, 0.0000, 0.0000,
  236. 0.0000, 2.0000, 0.0000, 0.0000,
  237. 0.0000, 0.0000, 0.5000, 0.0000,
  238. 0.0000, 0.0000, 0.0000, 1.0000],M1);
  239. end;
  240. procedure TTestMatrix3D.TestCreateOrthoRH;
  241. begin
  242. // test 60
  243. M1:=TMatrix3D.CreateOrthoRH(1,1,0,1);
  244. AssertMatrix3D('Create 1',[ 2.0000, 0.0000, 0.0000, 0.0000,
  245. 0.0000, 2.0000, 0.0000, 0.0000,
  246. 0.0000, 0.0000,-1.0000, 0.0000,
  247. 0.0000, 0.0000, 0.0000, 1.0000],M1);
  248. // test 61
  249. M1:=TMatrix3D.CreateOrthoRH(1,1,0,2);
  250. AssertMatrix3D('Create 2',[ 2.0000, 0.0000, 0.0000, 0.0000,
  251. 0.0000, 2.0000, 0.0000, 0.0000,
  252. 0.0000, 0.0000,-0.5000, 0.0000,
  253. 0.0000, 0.0000, 0.0000, 1.0000],M1);
  254. end;
  255. procedure TTestMatrix3D.TestCreateOrthoOffCenterLH;
  256. begin
  257. // test 62
  258. M1:=TMatrix3D.CreateOrthoOffCenterLH(0,0,1,1,0,1);
  259. AssertMatrix3D('Create 1',[2.0000, 0.0000, 0.0000, 0.0000,
  260. 0.0000,-2.0000, 0.0000, 0.0000,
  261. 0.0000, 0.0000, 1.0000, 0.0000,
  262. -1.0000, 1.0000, 0.0000, 1.0000],M1);
  263. // test 63
  264. M1:=TMatrix3D.CreateOrthoOffCenterLH(0,0,1,1,0,2);
  265. AssertMatrix3D('Create 2',[2.0000, 0.0000, 0.0000, 0.0000,
  266. 0.0000,-2.0000, 0.0000, 0.0000,
  267. 0.0000, 0.0000, 0.5000, 0.0000,
  268. -1.0000, 1.0000, 0.0000, 1.0000],M1);
  269. end;
  270. procedure TTestMatrix3D.TestCreateOrthoOffCenterRH;
  271. begin
  272. // test 64
  273. M1:=TMatrix3D.CreateOrthoOffCenterRH(0,0,1,1,0,1);
  274. AssertMatrix3D('Create 1',[2.0000, 0.0000, 0.0000, 0.0000,
  275. 0.0000,-2.0000, 0.0000, 0.0000,
  276. 0.0000, 0.0000,-1.0000, 0.0000,
  277. -1.0000, 1.0000, 0.0000, 1.0000],M1);
  278. // test 65
  279. M1:=TMatrix3D.CreateOrthoOffCenterRH(0,0,1,1,0,2);
  280. AssertMatrix3D('Create 2',[ 2.0000, 0.0000, 0.0000, 0.0000,
  281. 0.0000,-2.0000, 0.0000, 0.0000,
  282. 0.0000, 0.0000,-0.5000, 0.0000,
  283. -1.0000, 1.0000, 0.0000, 1.0000],M1);
  284. end;
  285. procedure TTestMatrix3D.TestCreatePerspectiveFovLH;
  286. begin
  287. // Test 66
  288. M1:=TMatrix3D.CreatePerspectiveFovLH(pi/2,1,0,1);
  289. AssertMatrix3D('Create 1',[ 1.0000, 0.0000, 0.0000, 0.0000,
  290. 0.0000, 1.0000, 0.0000, 0.0000,
  291. 0.0000, 0.0000, 1.0000, 1.0000,
  292. 0.0000, 0.0000, 0.0000, 0.0000],M1);
  293. // Test 67
  294. M1:=TMatrix3D.CreatePerspectiveFovLH(pi/2,1,0,1,True);
  295. AssertMatrix3D('Create 2',[ 1.0000, 0.0000, 0.0000, 0.0000,
  296. 0.0000, 1.0000, 0.0000, 0.0000,
  297. 0.0000, 0.0000, 1.0000, 1.0000,
  298. 0.0000, 0.0000,-0.0000, 0.0000], M1);
  299. end;
  300. procedure TTestMatrix3D.TestCreatePerspectiveFovRH;
  301. begin
  302. // Test 68
  303. M1:=TMatrix3D.CreatePerspectiveFovRH(pi/2,1,0,1);
  304. AssertMatrix3D('Create 1',[ 1.0000, 0.0000, 0.0000, 0.0000,
  305. 0.0000, 1.0000, 0.0000, 0.0000,
  306. 0.0000, 0.0000,-1.0000,-1.0000,
  307. 0.0000, 0.0000, 0.0000, 0.0000],M1);
  308. // Test 69
  309. M1:=TMatrix3D.CreatePerspectiveFovRH(pi/2,1,0,1,True);
  310. AssertMatrix3D('Create 2',[ 1.0000, 0.0000, 0.0000, 0.0000,
  311. 0.0000, 1.0000, 0.0000, 0.0000,
  312. 0.0000, 0.0000,-1.0000,-1.0000,
  313. 0.0000, 0.0000, 0.0000, 0.0000 ],M1);
  314. end;
  315. procedure TTestMatrix3D.TestCreateRotation;
  316. begin
  317. // test 32
  318. M1:=TMatrix3D.CreateRotation(Point3D(1,0,0),Pi/2);
  319. AssertMatrix3D('Create 1',[ 1.0000, 0.0000, 0.0000, 0.0000,
  320. 0.0000, 0.0000, 1.0000, 0.0000,
  321. 0.0000,-1.0000, 0.0000, 0.0000,
  322. 0.0000, 0.0000, 0.0000, 1.0000],M1);
  323. // test 33
  324. M1:=TMatrix3D.CreateRotation(Point3D(0,1,0),Pi/2);
  325. AssertMatrix3D('Create 2',[ 0.0000, 0.0000,-1.0000, 0.0000,
  326. 0.0000, 1.0000, 0.0000, 0.0000,
  327. 1.0000, 0.0000, 0.0000, 0.0000,
  328. 0.0000, 0.0000, 0.0000, 1.0000],M1);
  329. // test 34
  330. M1:=TMatrix3D.CreateRotation(Point3D(0,0,1),pi/2);
  331. AssertMatrix3D('Create 3',[ -0.0000, 1.0000, 0.0000, 0.0000,
  332. -1.0000,-0.0000, 0.0000, 0.0000,
  333. 0.0000, 0.0000, 1.0000, 0.0000,
  334. 0.0000, 0.0000, 0.0000, 1.0000],M1);
  335. end;
  336. procedure TTestMatrix3D.TestCreateRotationX;
  337. begin
  338. // test 40
  339. M1:=TMatrix3D.CreateRotationX(Pi/2);
  340. AssertMatrix3D('Create 1',[ 1.0000, 0.0000, 0.0000, 0.0000,
  341. 0.0000, 0.0000, 1.0000, 0.0000,
  342. 0.0000,-1.0000, 0.0000, 0.0000,
  343. 0.0000, 0.0000, 0.0000, 1.0000],M1);
  344. end;
  345. procedure TTestMatrix3D.TestCreateRotationY;
  346. begin
  347. // test 41
  348. M1:=TMatrix3D.CreateRotationY(Pi/2);
  349. AssertMatrix3D('Create 1',[ 0.0000, 0.0000,-1.0000, 0.0000,
  350. 0.0000, 1.0000, 0.0000, 0.0000,
  351. 1.0000, 0.0000, 0.0000, 0.0000,
  352. 0.0000, 0.0000, 0.0000, 1.0000],M1);
  353. end;
  354. procedure TTestMatrix3D.TestCreateRotationZ;
  355. begin
  356. // test 42
  357. M1:=TMatrix3D.CreateRotationZ(Pi/2);
  358. AssertMatrix3D('Create 1',[ -0.0000, 1.0000, 0.0000, 0.0000,
  359. -1.0000,-0.0000, 0.0000, 0.0000,
  360. 0.0000, 0.0000, 1.0000, 0.0000,
  361. 0.0000, 0.0000, 0.0000, 1.0000],M1);
  362. end;
  363. procedure TTestMatrix3D.TestCreateRotationHeadingPitchBank;
  364. begin
  365. // Test 37
  366. M1:=TMatrix3D.CreateRotationHeadingPitchBank(Pi/2,0,0);
  367. AssertMatrix3D('Create 1',[ 0.0000, 0.0000, 1.0000, 0.0000,
  368. 0.0000, 1.0000, 0.0000, 0.0000,
  369. -1.0000, 0.0000, 0.0000, 0.0000,
  370. 0.0000, 0.0000, 0.0000, 1.0000],M1);
  371. // Test 38
  372. M1:=TMatrix3D.CreateRotationHeadingPitchBank(0,Pi/2,0);
  373. AssertMatrix3D('Create 2',[ 1.0000, 0.0000, 0.0000, 0.0000,
  374. 0.0000, 0.0000,-1.0000, 0.0000,
  375. 0.0000, 1.0000, 0.0000, 0.0000,
  376. 0.0000, 0.0000, 0.0000, 1.0000],M1);
  377. // Test 39
  378. M1:=TMatrix3D.CreateRotationHeadingPitchBank(0,0,Pi/2);
  379. AssertMatrix3D('Create 3',[ 0.0000,-1.0000, 0.0000, 0.0000,
  380. 1.0000, 0.0000, 0.0000, 0.0000,
  381. 0.0000, 0.0000, 1.0000, 0.0000,
  382. 0.0000, 0.0000, 0.0000, 1.0000 ],M1);
  383. end;
  384. procedure TTestMatrix3D.TestCreateRotationYawPitchRoll;
  385. begin
  386. // test 43
  387. M1:=TMatrix3D.CreateRotationYawPitchRoll(Pi/2,0,0);
  388. AssertMatrix3D('Create 1',[ 0.0000,-1.0000, 0.0000, 0.0000,
  389. 1.0000, 0.0000, 0.0000, 0.0000,
  390. 0.0000, 0.0000, 1.0000, 0.0000,
  391. 0.0000, 0.0000, 0.0000, 1.0000 ],M1);
  392. // test 44
  393. M1:=TMatrix3D.CreateRotationYawPitchRoll(0,Pi/2,0);
  394. AssertMatrix3D('Create 2',[ 1.0000, 0.0000, 0.0000, 0.0000,
  395. 0.0000, 0.0000, 1.0000, 0.0000,
  396. 0.0000,-1.0000, 0.0000, 0.0000,
  397. 0.0000, 0.0000, 0.0000, 1.0000],M1);
  398. // test 45
  399. M1:=TMatrix3D.CreateRotationYawPitchRoll(0,0,Pi/2);
  400. AssertMatrix3D('Create 3',[ 0.0000, 0.0000,-1.0000, 0.0000,
  401. 0.0000, 1.0000, 0.0000, 0.0000,
  402. 1.0000, 0.0000, 0.0000, 0.0000,
  403. 0.0000, 0.0000, 0.0000, 1.0000],M1);
  404. end;
  405. procedure TTestMatrix3D.TestCreateScaling;
  406. begin
  407. // test 71
  408. M1:=TMatrix3D.CreateScaling(Point3D(1,2,3));
  409. AssertMatrix3D('Create 1',[ 1, 0, 0, 0,
  410. 0, 2, 0, 0,
  411. 0, 0, 3, 0,
  412. 0, 0, 0, 1],M1);
  413. end;
  414. procedure TTestMatrix3D.TestCreateTranslation;
  415. begin
  416. // test 72
  417. M1:=TMatrix3D.CreateTranslation(Point3D(1,2,3));
  418. AssertMatrix3D('Create 1',[ 1.0000, 0.0000, 0.0000, 0.0000,
  419. 0.0000, 1.0000, 0.0000, 0.0000,
  420. 0.0000, 0.0000, 1.0000, 0.0000,
  421. 1.0000, 2.0000, 3.0000, 1.0000 ],M1);
  422. end;
  423. procedure TTestMatrix3D.TestMultiplyPoint;
  424. var
  425. P1,P2 : TPoint3D;
  426. begin
  427. P1:=Point3D(1,0,0);
  428. M1:=TMatrix3D.CreateRotationZ(pi/2);
  429. P2:=P1*M1;
  430. AssertPoint3D('Multiply 1',0,1,0,P2);
  431. M1:=TMatrix3D.CreateRotationY(pi/2);
  432. P2:=P1*M1;
  433. AssertPoint3D('Multiply 2',0,0,-1,P2);
  434. P1:=Point3D(0,1,0);
  435. M1:=TMatrix3D.CreateRotationX(pi/2);
  436. P2:=P1*M1;
  437. AssertPoint3D('Multiply 3',0,0,1,P2);
  438. end;
  439. procedure TTestMatrix3D.TestMultiplyMatrix;
  440. begin
  441. // Test 73
  442. M1:=TMatrix3D.Create( [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]);
  443. M2:=TMatrix3D.Create([16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1]);
  444. M3:=M1*M2;
  445. AssertMatrix3D('Multiplied',[ 386.0000,274.0000,162.0000,50.0000,
  446. 444.0000,316.0000,188.0000,60.0000,
  447. 502.0000,358.0000,214.0000,70.0000,
  448. 560.0000,400.0000,240.0000,80.0000],m3);
  449. end;
  450. procedure TTestMatrix3D.TestMultiplyVector3D;
  451. var
  452. P1,P2 : TVector3D;
  453. begin
  454. P1:=Vector3D(1,0,0);
  455. M1:=TMatrix3D.CreateRotationZ(pi/2);
  456. P2:=P1*M1;
  457. AssertVector3D('Multiply 1',0,1,0,1,P2);
  458. M1:=TMatrix3D.CreateRotationY(pi/2);
  459. P2:=P1*M1;
  460. AssertVector3D('Multiply 2',0,0,-1,1,P2);
  461. P1:=Point3D(0,1,0);
  462. M1:=TMatrix3D.CreateRotationX(pi/2);
  463. P2:=P1*M1;
  464. AssertVector3D('Multiply 3',0,0,1,1,P2);
  465. end;
  466. procedure TTestMatrix3D.TestMultiplyFactor;
  467. begin
  468. M1:=TMatrix3D.Create( 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);
  469. M2:=2*M1;
  470. AssertMatrix3D('Multiplied',[ 2, 4, 6, 8,
  471. 10, 12, 14, 16,
  472. 18, 20, 22, 24,
  473. 26, 28, 30, 32],M2);
  474. end;
  475. procedure TTestMatrix3D.TestMultiplyFactor2;
  476. begin
  477. M1:=TMatrix3D.Create( 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);
  478. M3:=M1*2;
  479. AssertMatrix3D('Multiplied',[ 2, 4, 6, 8,
  480. 10, 12, 14, 16,
  481. 18, 20, 22, 24,
  482. 26, 28, 30, 32],M3);
  483. end;
  484. procedure TTestMatrix3D.TestDiv;
  485. begin
  486. M1:=TMatrix3D.Create( 2, 4, 6, 8,
  487. 10, 12, 14, 16,
  488. 18, 20, 22, 24,
  489. 26, 28, 30, 32);
  490. M3:=M1/2;
  491. AssertMatrix3D('Divided',[ 1 , 2, 3, 4,
  492. 5 , 6, 7, 8,
  493. 9 , 10, 11, 12,
  494. 13, 14, 15, 16 ],M3);
  495. end;
  496. procedure TTestMatrix3D.TestAdjoint;
  497. begin
  498. // Test 74
  499. M1:=TMatrix3D.CreateRotationZ(Pi/3);
  500. M2:=M1.Adjoint;
  501. AssertMatrix3D('Adjoint',[0.5000,-0.8660, 0.0000, 0.0000,
  502. 0.8660, 0.5000, 0.0000, 0.0000,
  503. 0.0000, 0.0000, 1.0000, 0.0000,
  504. 0.0000, 0.0000, 0.0000, 1.0000],M2);
  505. end;
  506. procedure TTestMatrix3D.TestDeterminant;
  507. begin
  508. // Test 75
  509. M1:=TMatrix3D.CreateRotationZ(Pi/3);
  510. AssertEquals('Determinant',1.0,M1.Determinant);
  511. // Test 76
  512. M1:=TMatrix3D.Create(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);
  513. AssertEquals('Determinant',0,M1.Determinant);
  514. M1:=TMatrix3D.Create(1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1);
  515. AssertEquals('Determinant',1,M1.Determinant);
  516. end;
  517. procedure TTestMatrix3D.TestEyePosition;
  518. begin
  519. // Test 77
  520. M1:=TMatrix3D.CreateRotationZ(Pi/3);
  521. AssertPoint3D('EyePosition 1',0,0,0,M1.EyePosition);
  522. // Test 78
  523. M1:=TMatrix3D.CreateRotationZ(Pi/3)*TMatrix3D.CreateTranslation(Point3D(1,0,0));
  524. AssertPoint3D('EyePosition 2',-0.50, 0.8660, 0,M1.EyePosition);
  525. end;
  526. procedure TTestMatrix3D.TestInverse;
  527. begin
  528. // Test 77
  529. M1:=TMatrix3D.CreateRotationZ(Pi/3);
  530. M2:=TMatrix3D.CreateRotationZ(-Pi/3);
  531. AssertMatrix3D('Inverse 1',M2,M1.Inverse);
  532. AssertMatrix3D('Inverse 2',TMatrix3D.Identity,M1*M1.Inverse);
  533. end;
  534. procedure TTestMatrix3D.TestScale;
  535. begin
  536. M1:=TMatrix3D.Create( 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);
  537. M3:=M1.Scale(2);
  538. AssertMatrix3D('Multiplied',[ 2, 4, 6, 8,
  539. 10, 12, 14, 16,
  540. 18, 20, 22, 24,
  541. 26, 28, 30, 32],M3);
  542. end;
  543. procedure TTestMatrix3D.TestToMatrix;
  544. Var
  545. M : TMatrix;
  546. begin
  547. M1:=TMatrix3D.CreateRotationZ(Pi/3);
  548. M:=M1.ToMatrix;
  549. AssertMatrix('To Matrix',TMatrix.CreateRotation(Pi/3),M);
  550. end;
  551. procedure TTestMatrix3D.TestTranspose;
  552. begin
  553. M1:=TMatrix3D.Create( 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);
  554. M2:=TMatrix3D.Create( [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]);
  555. M3:=M1.Transpose;
  556. AssertMatrix3D('Transposed',M2,M3);
  557. end;
  558. initialization
  559. RegisterTest(TTestMatrix3D);
  560. end.