utcpoint.pp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 utcpoint;
  12. {$mode ObjFPC}{$H+}
  13. interface
  14. uses
  15. Classes, SysUtils, fpcunit, testregistry, types, utmathvectorbase, system.math.vectors;
  16. type
  17. { TTestPoint3D }
  18. TTestPoint3D = Class(TCMathVectorsBase)
  19. Private
  20. FP : Array[1..3] of TPoint3D;
  21. procedure ClearPoints;
  22. function GetP(aIndex: Integer): TPoint3D;
  23. procedure SetP(aIndex: Integer; aValue: TPoint3D);
  24. Protected
  25. procedure SetUp; override;
  26. procedure TearDown; override;
  27. Property P1 : TPoint3D Index 1 Read GetP Write SetP;
  28. Property P2 : TPoint3D Index 2 Read GetP Write SetP;
  29. Property P3 : TPoint3D Index 3 Read GetP Write SetP;
  30. Published
  31. procedure TestHookUp;
  32. Procedure TestZero;
  33. Procedure TestCreate;
  34. Procedure TestCreate2DPointAndZ;
  35. Procedure TestCreateVector3D;
  36. Procedure TestAdd;
  37. Procedure TestMultiplyFactor;
  38. Procedure TestMultiply;
  39. Procedure TestSubtract;
  40. Procedure TestEqual;
  41. Procedure TestDivide;
  42. Procedure TestUnEqual;
  43. Procedure TestUnaryMinus;
  44. Procedure TestAngleCosine;
  45. Procedure TestCrossProduct;
  46. Procedure TestDistance;
  47. Procedure TestDotProduct;
  48. Procedure TestEqualsTo;
  49. Procedure TestLength;
  50. Procedure TestMidPoint;
  51. Procedure TestNormalize;
  52. Procedure TestOffsetPoint;
  53. Procedure TestOffsetDelta;
  54. Procedure TestReflect;
  55. Procedure TestRotate;
  56. end;
  57. implementation
  58. { TTestPoint3D }
  59. procedure TTestPoint3D.ClearPoints;
  60. var
  61. I : integer;
  62. begin
  63. For I:=1 to 3 do
  64. begin
  65. FP[I].X:=0;
  66. FP[I].Y:=0;
  67. FP[I].Z:=0;
  68. end;
  69. end;
  70. function TTestPoint3D.GetP(aIndex: Integer): TPoint3D;
  71. begin
  72. Result.X:=FP[AIndex].X;
  73. Result.Y:=FP[AIndex].Y;
  74. Result.Z:=FP[AIndex].Z;
  75. end;
  76. procedure TTestPoint3D.SetP(aIndex: Integer; aValue: TPoint3D);
  77. begin
  78. FP[AIndex].X:=aValue.X;
  79. FP[AIndex].Y:=aValue.Y;
  80. FP[AIndex].Z:=aValue.Z;
  81. end;
  82. procedure TTestPoint3D.SetUp;
  83. begin
  84. inherited SetUp;
  85. ClearPoints;
  86. end;
  87. procedure TTestPoint3D.TearDown;
  88. begin
  89. inherited TearDown;
  90. end;
  91. procedure TTestPoint3D.TestHookUp;
  92. var
  93. I : Integer;
  94. begin
  95. For I:=1 to 3 do
  96. begin
  97. AssertEquals('Point3D '+intTostr(i)+'.X',0.0,FP[I].X);
  98. AssertEquals('Point3D '+intTostr(i)+'.Y',0.0,FP[I].Y);
  99. AssertEquals('Point3D '+intTostr(i)+'.Z',0.0,FP[I].Z);
  100. end;
  101. end;
  102. procedure TTestPoint3D.TestZero;
  103. begin
  104. P1:=TPoint3D.Zero;
  105. AssertPoint3D('Zero',0,0,0,P1);
  106. end;
  107. procedure TTestPoint3D.TestCreate;
  108. begin
  109. P1:=TPoint3D.Create(1,2,3);
  110. AssertPoint3D('Create',1,2,3,P1);
  111. end;
  112. procedure TTestPoint3D.TestCreate2DPointAndZ;
  113. begin
  114. P1:=TPoint3D.Create(PointF(1,2),3);
  115. AssertPoint3D('Create',1,2,3,P1);
  116. end;
  117. procedure TTestPoint3D.TestCreateVector3D;
  118. begin
  119. P1:=TPoint3D.Create([1,2,3,4]);
  120. AssertPoint3D('Create',1,2,3,P1);
  121. end;
  122. procedure TTestPoint3D.TestAdd;
  123. begin
  124. P1:=TPoint3D.Create(1,2,3);
  125. P2:=TPoint3D.Create(5,4,3);
  126. P3:=P2+P1;
  127. AssertPoint3D('Add 1',6,6,6,P3);
  128. P2:=TPoint3D.Create(7,8,9);
  129. P3:=P2+P1;
  130. AssertPoint3D('Add 2',8,10,12,P3);
  131. end;
  132. procedure TTestPoint3D.TestMultiplyFactor;
  133. begin
  134. P1:=TPoint3D.Create(1,2,3);
  135. P2:=P1*3;
  136. AssertPoint3D('Factor 1',3,6,9,P2);
  137. P2:=2*P1;
  138. AssertPoint3D('Factor 2',2,4,6,P2);
  139. end;
  140. procedure TTestPoint3D.TestMultiply;
  141. begin
  142. P1:=TPoint3D.Create(1,2,3);
  143. P2:=TPoint3D.Create(5,4,3);
  144. P3:=P2*P1;
  145. AssertPoint3D('Create',5,8,9,P3);
  146. end;
  147. procedure TTestPoint3D.TestSubtract;
  148. begin
  149. P1:=TPoint3D.Create(1,2,3);
  150. P2:=TPoint3D.Create(5,4,3);
  151. P3:=P2-P1;
  152. AssertPoint3D('Subtract 1',4,2,0,P3);
  153. P2:=TPoint3D.Create(7,8,9);
  154. P3:=P2-P1;
  155. AssertPoint3D('Subtract 2',6,6,6,P3);
  156. end;
  157. procedure TTestPoint3D.TestEqual;
  158. begin
  159. P1:=TPoint3D.Create(1,2,3);
  160. P2:=TPoint3D.Create(5,4,3);
  161. AssertFalse('Equal NOk',P1=P2);
  162. P2:=TPoint3D.Create(1,2,3);
  163. AssertTrue('Equal OK',P1=P2);
  164. end;
  165. procedure TTestPoint3D.TestDivide;
  166. begin
  167. P1:=TPoint3D.Create(3,6,9);
  168. P2:=P1/3;
  169. AssertPoint3D('Factor 1',1,2,3,P2);
  170. end;
  171. procedure TTestPoint3D.TestUnEqual;
  172. begin
  173. P1:=TPoint3D.Create(1,2,3);
  174. P2:=TPoint3D.Create(5,4,3);
  175. AssertTrue('unEqual Ok',P1<>P2);
  176. P2:=TPoint3D.Create(1,2,3);
  177. AssertFalse('unEqual OK',P1<>P2);
  178. end;
  179. procedure TTestPoint3D.TestUnaryMinus;
  180. begin
  181. P1:=TPoint3D.Create(1,2,3);
  182. P2:=-P1;
  183. AssertPoint3D('Unary minus',-1,-2,-3,P2);
  184. end;
  185. procedure TTestPoint3D.TestAngleCosine;
  186. begin
  187. P1:=TPoint3D.Create(1,0,0);
  188. P2:=TPoint3D.Create(0,1,0);
  189. AssertEquals('Angle',0, P1.AngleCosine(P2));
  190. P1:=TPoint3D.Create(1,0,0);
  191. P2:=TPoint3D.Create(0,0,1);
  192. AssertEquals('Angle',0, P1.AngleCosine(P2));
  193. P1:=TPoint3D.Create(0,1,0);
  194. P2:=TPoint3D.Create(0,0,1);
  195. AssertEquals('Angle',0, P1.AngleCosine(P2));
  196. P1:=TPoint3D.Create(0,1,0);
  197. P2:=TPoint3D.Create(1,1,0);
  198. AssertEquals('Angle',c45, P1.AngleCosine(P2));
  199. end;
  200. procedure TTestPoint3D.TestCrossProduct;
  201. begin
  202. P1:=TPoint3D.Create(1,1,0);
  203. P2:=TPoint3D.Create(2,2,0);
  204. P3:=P2.CrossProduct(P1);
  205. AssertPoint3D('T1',0,0,0,P3);
  206. P1:=TPoint3D.Create(1,1,0);
  207. P2:=TPoint3D.Create(2,2,0);
  208. P3:=P2.CrossProduct(P1);
  209. AssertPoint3D('T2',0,0,0,P3);
  210. end;
  211. procedure TTestPoint3D.TestDistance;
  212. begin
  213. P1:=TPoint3D.Create(1,1,0);
  214. P2:=TPoint3D.Create(1,2,0);
  215. AssertEquals('Dist 1,',1,P1.Distance(P2));
  216. P1:=TPoint3D.Create(0,1,1);
  217. P2:=TPoint3D.Create(0,2,1);
  218. AssertEquals('Dist 2,',1,P1.Distance(P2));
  219. P1:=TPoint3D.Create(0,0,0);
  220. P2:=TPoint3D.Create(1,1,0);
  221. AssertEquals('Dist 3,',c45*2,P1.Distance(P2));
  222. end;
  223. procedure TTestPoint3D.TestDotProduct;
  224. begin
  225. P1:=TPoint3D.Create(3,4,9);
  226. P2:=TPoint3D.Create(3,4,9);
  227. AssertEquals('Test 1',9+16+81,P1.DotProduct(P2));
  228. P2:=TPoint3D.Create(1,1,0);
  229. AssertEquals('Test 1',7,P1.DotProduct(P2));
  230. end;
  231. procedure TTestPoint3D.TestEqualsTo;
  232. begin
  233. P1:=TPoint3D.Create(3,4,9);
  234. P2:=TPoint3D.Create(3,4,9);
  235. AssertTrue('Test 1',P1.EqualsTo(P2));
  236. P2:=TPoint3D.Create(1,1,1);
  237. AssertFalse('Test 2',P1.EqualsTo(P2));
  238. end;
  239. procedure TTestPoint3D.TestLength;
  240. begin
  241. P1:=TPoint3D.Create(1,0,0);
  242. AssertEquals('Dist 1,',1,P1.Length);
  243. P1:=TPoint3D.Create(0,1,1);
  244. AssertEquals('Dist 2,',c45*2,P1.Length);
  245. P1:=TPoint3D.Create(1,1,1);
  246. AssertEquals('Dist 3,',1.7321,P1.Length);
  247. end;
  248. procedure TTestPoint3D.TestMidPoint;
  249. begin
  250. P1:=TPoint3D.Create(0,0,1);
  251. P2:=TPoint3D.Create(0,2,1);
  252. AssertPoint3D('Midpoint1',0,1,1,P1.MidPoint(P2));
  253. P1:=TPoint3D.Create(1,0,0);
  254. P2:=TPoint3D.Create(0,1,0);
  255. AssertPoint3D('Midpoint2',0.5,0.5,0,P1.MidPoint(P2));
  256. end;
  257. procedure TTestPoint3D.TestNormalize;
  258. begin
  259. P1:=TPoint3D.Create(1,1,1);
  260. P2:=P1.Normalize;
  261. AssertPoint3D('Normalize 1',0.5773,0.5773,0.5773,P2);
  262. P1:=TPoint3D.Create(0,0,1);
  263. P2:=P1.Normalize;
  264. AssertPoint3D('Normalize 2',0,0,1,P2);
  265. P1:=TPoint3D.Create(1,0,0);
  266. P2:=P1.Normalize;
  267. AssertPoint3D('Normalize 3',1,0,0,P2);
  268. end;
  269. procedure TTestPoint3D.TestOffsetPoint;
  270. Var
  271. P : TPoint3D; // Cannot use property
  272. begin
  273. P:=TPoint3D.Create(1,2,3);
  274. P.Offset(Point3D(4,5,6));
  275. AssertPoint3D('Off',5,7,9,P);
  276. end;
  277. procedure TTestPoint3D.TestOffsetDelta;
  278. Var
  279. P : TPoint3D; // Cannot use property
  280. begin
  281. P:=TPoint3D.Create(0,0,0);
  282. P.Offset(1,2,3);
  283. AssertPoint3D('Off',1,2,3,P);
  284. end;
  285. procedure TTestPoint3D.TestReflect;
  286. // P2 is normal of reflecting surface: X,Y plane.
  287. begin
  288. P1:=TPoint3D.Create(0,0,1);
  289. P2:=TPoint3D.Create(0,0,1);
  290. P3:=P1.Reflect(P2);
  291. AssertPoint3D('Reflect 1',0,0,-1,P3);
  292. P1:=TPoint3D.Create(0,-1,-1);
  293. P3:=P1.Reflect(P2);
  294. AssertPoint3D('Reflect 2',0,-1,1,P3);
  295. end;
  296. procedure TTestPoint3D.TestRotate;
  297. begin
  298. P1:=TPoint3D.Create(1,0,0);
  299. P2:=TPoint3D.Create(0,0,1); // Z-axis
  300. P3:=P1.Rotate(P2,Pi/2);
  301. AssertPoint3D('Rotate 1',0,1,0,P3);
  302. P2:=TPoint3D.Create(0,1,0); // Y-axis
  303. P3:=P1.Rotate(P2,Pi/2);
  304. AssertPoint3D('Rotate 2',0,0,-1,P3); // Z is pointing down!
  305. end;
  306. initialization
  307. RegisterTests([TTestPoint3D]);
  308. end.