Debian Codegraph @

tests/code/testcases/testcastlevectors.pas

raw
1 // -*- compile-command: "./test_single_testcase.sh TTestCastleVectors" -*-
2 {
3 Copyright 2004-2023 Michalis Kamburelis.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 ----------------------------------------------------------------------------
15 }
16
17 { Test CastleVectors. }
18 unit TestCastleVectors;
19
20 { $define VECTOR_MATH_SPEED_TESTS}
21
22 interface
23
24 uses
25 Classes, SysUtils,
26 CastleTester, CastleVectors;
27
28 type
29 TTestCastleVectors = class(TCastleTestCase)
30 published
31 procedure TestPlaneOdcCollision;
32 procedure TestCollisions;
33 procedure TestArea;
34 procedure TestPerpParallel;
35 procedure TestPlanesIntersection;
36 procedure TestOther;
37 procedure TestVectorStr;
38 procedure TestMatrixInverse;
39 procedure TestMultMatrixTranslation;
40 procedure TestMultMatricesTranslation;
41 procedure TestIndexedPolygonNormalArea;
42 procedure TestSphereRayIntersection;
43 procedure TestMatrixMultiplication;
44 procedure TestMatrixTranspose;
45 procedure TestVector3FromStr;
46 procedure TestVector4FromStr;
47 procedure TestPlaneTransform;
48 procedure TestTransformToFromCoordsMatrix;
49 procedure Test2D;
50 procedure TestApproximateScale;
51 procedure TestXYZ;
52 procedure TestPlaneMove;
53 procedure TestPlaneMoveRandom;
54 procedure TestTryInverseHarder;
55 procedure TestMaxAbsVectorCoord;
56 procedure TestMinAbsVectorCoord;
57 procedure TestPointOnLineClosestToLine;
58 procedure TestRotatePointAroundAxis;
59 procedure TestMakeVectorOrthogonal;
60 procedure TestEpsilonInModelViewToNormalMatrix;
61 procedure TestInit;
62 procedure TestRotationZeroAxis;
63 procedure TestVectorsList;
64 procedure TestToString;
65 procedure TestBasicVectorMatrix;
66 end;
67
68 function RandomVector: TVector3;
69 function RandomMatrix: TMatrix4;
70 function RandomNonProjectionMatrix: TMatrix4;
71
72 implementation
73
74 uses Math,
75 CastleUtils, CastleStringUtils, CastleTimeUtils, CastleTriangles;
76
77 const
78 {$ifdef VECTOR_MATH_SPEED_TESTS}
79 SPEED_TEST_1_CYCLES = 1000000;
80 SPEED_TEST_2_CYCLES = 10000000;
81 SPEED_TEST_3_CYCLES = 1000;
82 {$else}
83 { speed testy przeprowadzaja przy okazji normalne testy ktore
84 zawsze bedziesz chcial wykonac. Wiec jesli VECTOR_MATH_SPEED_TESTS
85 nie jest zdefiniowane to po prostu SPEED_TEST_x_CYCLES beda
86 mniejsze (ale nie zerowe) i nie bedzie Writelnow wynikow. }
87 SPEED_TEST_1_CYCLES = 1000;
88 SPEED_TEST_2_CYCLES = 1000;
89 SPEED_TEST_3_CYCLES = 1000;
90 {$endif}
91
92 procedure TTestCastleVectors.TestPlaneOdcCollision;
93 { test below caught once fpc 1.0.10 bugs in inlines }
94 var Intersection: TVector3;
95 T: Single;
96 begin
97 T := TVector3.DotProduct(Vector3(0, 0, 1), Vector3(0, 0, 6));
98 AssertSameValue(6, T);
99
100 AssertTrue(TryPlaneLineIntersection(T,
101 Vector4(0, 0, 1, 1),
102 Vector3(2, 2, -3),
103 Vector3(0, 0, 6) ));
104 AssertSameValue(1/3, T, 0.0000001);
105
106 AssertTrue(TryPlaneSegmentDirIntersection(Intersection, T,
107 Vector4(0, 0, 1, 1),
108 Vector3(2, 2, -3),
109 Vector3(0, 0, 6) ));
110 AssertVectorEquals(Vector3(2, 2, -1), Intersection);
111 AssertSameValue(1/3, T, 0.0000001);
112 end;
113
114 procedure WritelnSpeedTest(const s: string);
115 begin
116 {$ifdef VECTOR_MATH_SPEED_TESTS}
117 Writeln(s);
118 {$endif}
119 end;
120
121 procedure TTestCastleVectors.TestCollisions;
122 const
123 TriConst: TTriangle3 = (Data: (
124 (X: 2; Y: 2; Z: -1),
125 (X: 3; Y: 3; Z: -1),
126 (X: 3; Y: 2; Z: -1) ));
127 TriPlaneConst: TVector4 = (X: 0; Y: 0; Z: 1; W: 1);
128 Pos1Const: TVector3 = (X: 2; Y: 2; Z: -3);
129 Pos2Const: TVector3 = (X: 2; Y: 2; Z: 3);
130 Coll: TVector3 = (X: 2; Y: 2; Z: -1);
131 var
132 Intersection, Intersection2: TVector3;
133 begin
134 { czy dziala TryTriangleSegmentCollision na tym naszym skonstruowanym
135 przykladzie ? }
136 AssertTrue( TryTriangleSegmentCollision(Intersection, TriConst, TriPlaneConst,
137 Pos1Const, Pos2Const) and TVector3.Equals(Intersection, Coll));
138 { czy dziala tak samo gdy sam musi sobie wyliczyc place (test TTriangle3.Plane) ? }
139 AssertTrue( TryTriangleSegmentCollision(Intersection2, TriConst, TriConst.Plane,
140 Pos1Const, Pos2Const) and TVector3.Equals(Intersection2, Coll));
141 end;
142
143 procedure TTestCastleVectors.TestArea;
144 const
145 Tri: TTriangle3 = (Data: (
146 (X: 0; Y: 0; Z: 0),
147 (X: 10; Y: 0; Z: 0),
148 (X: 0; Y: 25; Z: 0) ));
149 CCWPoly: array [0..4] of TVector2 = (
150 (X: 5; Y: 4),
151 (X: 2; Y: 3),
152 (X: 4; Y: 3),
153 (X: 2; Y: 1),
154 (X: 6; Y: 2) );
155 CWPoly: array [0..4] of TVector2 = (
156 (X: 6; Y: 2),
157 (X: 2; Y: 1),
158 (X: 4; Y: 3),
159 (X: 2; Y: 3),
160 (X: 5; Y: 4) );
161 begin
162 AssertTrue(Tri.Area = 10*25/2);
163
164 AssertTrue(Polygon2dArea(CCWPoly) = 5.5);
165 AssertTrue(Polygon2dArea(CWPoly) = 5.5);
166 AssertTrue(IsPolygon2dCCW(CCWPoly) > 0);
167 AssertTrue(IsPolygon2dCCW(CWPoly) < 0);
168 end;
169
170 procedure TTestCastleVectors.TestPerpParallel;
171 var v: TVector3;
172 i: integer;
173 begin
174 for i := 1 to 10 do
175 try
176 v := RandomVector;
177 AssertTrue( VectorsPerp(AnyOrthogonalVector(v), v) );
178 { I has to comment it out -- it fails too often due to floating point
179 inaccuracy. }
180 { AssertTrue( VectorsParallel(v * (Random * 10)), v) ); }
181 AssertTrue( VectorsPerp(TVector3.Zero, v) );
182 AssertTrue( VectorsParallel(TVector3.Zero, v) );
183 except
184 Writeln('and failed : v = ',v.ToString,
185 ' anyPerp = ',AnyOrthogonalVector(v).ToString);
186 raise;
187 end;
188
189 AssertTrue( VectorsPerp(TVector3.Zero, TVector3.Zero) );
190 AssertTrue( VectorsParallel(TVector3.Zero, TVector3.Zero) );
191
192 AssertTrue( VectorsPerp(TVector3.One[0], TVector3.One[1]) );
193 AssertTrue( VectorsPerp(TVector3.One[0], TVector3.One[2]) );
194 AssertTrue( VectorsPerp(TVector3.One[1], TVector3.One[2]) );
195 AssertTrue( not VectorsPerp(TVector3.One[0], TVector3.One[0]) );
196
197 AssertTrue( not VectorsParallel(TVector3.One[0], TVector3.One[1]) );
198 AssertTrue( not VectorsParallel(TVector3.One[0], TVector3.One[2]) );
199 AssertTrue( not VectorsParallel(TVector3.One[1], TVector3.One[2]) );
200 AssertTrue( VectorsParallel(TVector3.One[0], TVector3.One[0]) );
201 end;
202
203 procedure TTestCastleVectors.TestPlanesIntersection;
204 const
205 P1: TVector4 = (X: -1.9935636520385742; Y: -0.00000009909226151; Z: 0.25691652297973633; W: -30.014257431030273);
206 P2: TVector4 = (X: -1.2131816148757935; Y: 1.90326225890658E-008; Z: -1.5900282859802246; W: 1.5900282859802246);
207 var
208 Line0, LineVector: TVector3;
209 begin
210 TwoPlanesIntersectionLine(P1, P2, Line0, LineVector);
211 { Writeln(Line0.ToRawString, ' ', LineVector.ToRawString); }
212 end;
213
214 procedure TTestCastleVectors.TestOther;
215 var
216 I1, I2, RayOrigin, RayDirection: TVector3;
217 Plane: TVector4;
218 // PlaneDir: TVector3 absolute Plane;
219 PlaneConstCoord: integer;
220 PlaneConstVal: Single;
221 b1, b2: boolean;
222 // t1, t2: Double;
223
224 function RandomVector3: TVector3;
225 begin
226 Result := Vector3(
227 Random*1000 -500.0,
228 Random*1000 -500.0,
229 Random*1000 -500.0
230 );
231 end;
232
233 const VConst: TVector3 = (X: 1.0; Y: 2.0; Z: 3.0);
234
235 var
236 i: integer;
237 V: TVector3;
238 Time0, Time1, Time2: Double;
239 StartTime: TProcessTimerResult;
240 begin
241 { ------------------------------------------------------------
242 testuj TrySimplePlaneRayIntersection przy uzyciu TryPlaneRayIntersection }
243 for i := 1 to 100000 do
244 begin
245 RayOrigin := RandomVector3;
246 RayDirection := RandomVector3;
247
248 PlaneConstCoord := Random(3);
249 PlaneConstVal := Random*1000 - 500;
250 FillChar(Plane, SizeOf(Plane), 0);
251 Plane.Data[PlaneConstCoord] := -1;
252 Plane.Data[3] := PlaneConstVal;
253
254 { czasami uczyn promien rownoleglym do [Simple]Plane (zeby zobaczyc
255 czy sobie z tym radzi) }
256 if Random(10) = 1 then
257 begin
258 RayDirection.Data[PlaneConstCoord] := 0;
259 b1 := TrySimplePlaneRayIntersection(I1, PlaneConstCoord, PlaneConstVal, RayOrigin, RayDirection);
260 b2 := TryPlaneRayIntersection(I2, Plane, RayOrigin, RayDirection);
261 Check( (not b1) and (not b2) ,'intersect with parallel plane');
262 end else
263 begin
264 { nie wykonuj testu jesli wylosowalimy niepoprawne dane }
265 if not TVector3.Equals(RayDirection, Vector3(0, 0, 0)) then
266 begin
267 b1 := TrySimplePlaneRayIntersection(I1, PlaneConstCoord, PlaneConstVal, RayOrigin, RayDirection);
268 b2 := TryPlaneRayIntersection(I2, Plane, RayOrigin, RayDirection);
269 AssertEquals(b1, b2);
270 if b1 then
271 begin
272 { if not TVector3.Equals(I1, I2) or
273 not SameValue(I1[PlaneConstCoord], PlaneConstVal) or
274 not SameValue(I2[PlaneConstCoord], PlaneConstVal) then
275 begin
276 t1:=(PlaneConstVal-RayOrigin[PlaneConstCoord]) / RayDirection[PlaneConstCoord];
277 t2 := -(plane[0]*RayOrigin[0] + plane[1]*RayOrigin[1] + plane[2]*RayOrigin[2] + plane[3])/
278 TVector3.DotProduct(PlaneDir, RayDirection);
279 Writeln('I1 = ',I1.ToString, ' I2 = ',I2.ToString, nl,
280 'PlaneConst Coord = ',PlaneConstCoord, ' Value = ',PlaneConstVal, nl,
281 'Plane = ',Plane.ToString, nl,
282 'RayOrigin = ',RayOrigin.ToString, ' RayDirection = ',RayDirection.ToString, nl,
283 t1:1:2, nl,
284 t2:1:2, nl,
285 (RayOrigin + RayDirection * t1).ToString, nl,
286 (RayOrigin + RayDirection * t2).ToString
287 );
288 end; }
289 AssertSameValue(PlaneConstVal, I1[PlaneConstCoord]);
290 AssertSameValue(PlaneConstVal, I2[PlaneConstCoord]);
291 AssertVectorEquals(I1, I2);
292 end;
293 end;
294 end;
295 end;
296
297 { testuj szybkosc TrySimplePlaneRayIntersection w porownaniu z
298 TryPlaneRayIntersection }
299 WritelnSpeedTest('SPEED TEST 1 ----------------------------------------------');
300
301 StartTime := ProcessTimer;
302 for i := 1 to SPEED_TEST_1_CYCLES do ;
303 Time0 := ProcessTimerSeconds(ProcessTimer, StartTime);
304 WritelnSpeedTest(Format('Empty loop = %f',[Time0]));
305
306 StartTime := ProcessTimer;
307 for i := 1 to SPEED_TEST_1_CYCLES do
308 TrySimplePlaneRayIntersection(I1, PlaneConstCoord, PlaneConstVal, RayOrigin, RayDirection);
309 Time1 := ProcessTimerSeconds(ProcessTimer, StartTime);
310 WritelnSpeedTest(Format('TrySimplePlaneRayIntersection = %f',[Time1]));
311
312 StartTime := ProcessTimer;
313 for i := 1 to SPEED_TEST_1_CYCLES do
314 TryPlaneRayIntersection(I1, Plane, RayOrigin, RayDirection);
315 Time2 := ProcessTimerSeconds(ProcessTimer, StartTime);
316 WritelnSpeedTest(Format('TryPlaneRayIntersection = %f',[Time2]));
317
318 {$ifdef VECTOR_MATH_SPEED_TESTS}
319 { nie uzywam tutaj WritelnSpeedTest. Jesli VECTOR_MATH_SPEED_TESTS
320 not defined to stale SPEED_TEST_x_CYCLES sa tak male ze nie moge
321 wykonac dzielenia przez Time1-Time0 bo Time1-Time0 = 0. }
322 Writeln(Format('SimplePlane is faster than Plane by %f', [(Time2-Time0)/(Time1-Time0)]));
323 {$endif}
324
325 WritelnSpeedTest('SPEED TEST 2 ----------------------------------------------');
326
327 StartTime := ProcessTimer;
328 for i := 1 to SPEED_TEST_2_CYCLES do ;
329 Time0 := ProcessTimerSeconds(ProcessTimer, StartTime);
330 WritelnSpeedTest(Format('Empty loop = %f',[Time0]));
331
332 StartTime := ProcessTimer;
333 for i := 1 to SPEED_TEST_2_CYCLES do
334 begin
335 V := VConst;
336 V := V * Pi;
337 end;
338 Time1 := ProcessTimerSeconds(ProcessTimer, StartTime);
339 WritelnSpeedTest(Format('Using assignment + VectorScaleVar = %f',[Time1]));
340
341 StartTime := ProcessTimer;
342 for i := 1 to SPEED_TEST_2_CYCLES do
343 begin
344 V := VConst * Pi;
345 end;
346 Time2 := ProcessTimerSeconds(ProcessTimer, StartTime);
347 WritelnSpeedTest(Format('Using VectorScale = %f',[Time2]));
348
349 {$ifdef VECTOR_MATH_SPEED_TESTS}
350 { nie uzywam tutaj WritelnSpeedTest. Jesli VECTOR_MATH_SPEED_TESTS
351 not defined to stale SPEED_TEST_x_CYCLES sa tak male ze nie moge
352 wykonac dzielenia przez Time1-Time0 bo Time1-Time0 = 0. }
353 Writeln(Format('Assignment+Var is faster by %f', [(Time2-Time0)/(Time1-Time0)]));
354 {$endif}
355 end;
356
357 procedure TTestCastleVectors.TestVectorStr;
358
359 procedure OneTestVectorFromStr;
360 var v, v2: TVector3;
361 s: string;
362 begin
363 v := RandomVector;
364 s := v.ToRawString;
365 v2 := Vector3FromStr(s);
366 AssertVectorEquals(v2, v, 0.001); // larger epsilon for ppc64
367 end;
368
369 procedure OneTestByDeformat;
370 var v, v2: TVector3;
371 s: string;
372 begin
373 v := RandomVector;
374 s := v.ToRawString;
375 DeFormat(s, '%.single. %.single. %.single.', [@v2.X, @v2.Y, @v2.Z]);
376 AssertVectorEquals(v2, v, 0.001); // larger epsilon for ppc64
377 end;
378
379 const
380 CYCLES = SPEED_TEST_3_CYCLES;
381 var
382 Time0, Time1, Time2: Double;
383 i: integer;
384 StartTime: TProcessTimerResult;
385 begin
386 WritelnSpeedTest('SPEED TEST VectorFromStr ------------------------------------------');
387 StartTime := ProcessTimer;
388 for i := 1 to CYCLES do ;
389 Time0 := ProcessTimerSeconds(ProcessTimer, StartTime);
390 WritelnSpeedTest(Format('Empty loop = %f',[Time0]));
391
392 StartTime := ProcessTimer;
393 for i := 1 to CYCLES do OneTestVectorFromStr;
394 Time1 := ProcessTimerSeconds(ProcessTimer, StartTime);
395 WritelnSpeedTest(Format('VectorFromStr = %f',[Time1]));
396
397 StartTime := ProcessTimer;
398 for i := 1 to CYCLES do OneTestByDeFormat;
399 Time2 := ProcessTimerSeconds(ProcessTimer, StartTime);
400 WritelnSpeedTest(Format('DeFormat = %f',[Time2]));
401
402 {$ifdef VECTOR_MATH_SPEED_TESTS}
403 { nie uzywam tutaj WritelnSpeedTest. Jesli VECTOR_MATH_SPEED_TESTS
404 not defined to stale SPEED_TEST_x_CYCLES sa tak male ze nie moge
405 wykonac dzielenia przez Time1-Time0 bo Time1-Time0 = 0. }
406 Writeln(Format('VectorFromStr is faster by %f', [(Time2-Time0)/(Time1-Time0)]));
407 {$endif}
408 end;
409
410 procedure TTestCastleVectors.TestMatrixInverse;
411 var
412 M: TMatrix4;
413 begin
414 M := ScalingMatrix(Vector3(2, 2, 2));
415
416 { Tests:
417 Writeln(M.ToString(' '));
418 Writeln(ScalingMatrix(Vector3(0.5, 0.5, 0.5)).ToString(' '));
419 Writeln(MatrixInverse(M, MatrixDeterminant(M)).ToString(' '));
420 }
421
422 AssertMatrixEquals(
423 ScalingMatrix(Vector3(0.5, 0.5, 0.5)),
424 M.Inverse(M.Determinant),
425 0.01);
426
427 M := TranslationMatrix(Vector3(2, 2, 2));
428 AssertMatrixEquals(
429 TranslationMatrix(Vector3(-2, -2, -2)),
430 M.Inverse(M.Determinant),
431 0.01);
432 end;
433
434 procedure TTestCastleVectors.TestMultMatrixTranslation;
435 var
436 M, NewM: TMatrix4;
437 I: Integer;
438 V: TVector3;
439 begin
440 for I := 1 to 100 do
441 begin
442 M := RandomMatrix;
443 V := RandomVector;
444 NewM := M * TranslationMatrix(V);
445 MultMatrixTranslation(M, V);
446 AssertMatrixEquals(M, NewM, 0.001);
447 end;
448 end;
449
450 procedure TTestCastleVectors.TestMultMatricesTranslation;
451 var
452 M, NewM, MInverse, NewMInverse: TMatrix4;
453 I: Integer;
454 V: TVector3;
455 begin
456 for I := 1 to 100 do
457 begin
458 M := RandomMatrix;
459 if not M.TryInverse(MInverse) then
460 MInverse := TMatrix4.Identity;
461
462 V := RandomVector;
463 NewM := M * TranslationMatrix(V);
464 NewMInverse := TranslationMatrix(-V) * MInverse;
465 MultMatricesTranslation(M, MInverse, V);
466 AssertMatrixEquals(M, NewM, 0.001);
467 AssertMatrixEquals(MInverse, NewMInverse, 0.001);
468 end;
469 end;
470
471 procedure TTestCastleVectors.TestIndexedPolygonNormalArea;
472 const
473 Poly: array [0..4] of TVector3 = (
474 (X: 5; Y: 4; Z: 0),
475 (X: 4; Y: 4; Z: 0),
476 (X: 2; Y: 3; Z: 0),
477 (X: 2; Y: 1; Z: 0),
478 (X: 6; Y: 2; Z: 0) );
479 CCWPolyIndex: array [0..6] of Int32 = (0, 1, 5, 2, 3, 4, 999);
480 CWPolyIndex: array [0..6] of Int32 = (666, 4, 105, 3, 2, 1, 0);
481 begin
482 AssertVectorEquals(
483 Vector3(0, 0, 1),
484 IndexedConvexPolygonNormal(@CCWPolyIndex, High(CCWPolyIndex) + 1,
485 @Poly, High(Poly) + 1, TVector3.Zero));
486
487 AssertVectorEquals(
488 Vector3(0, 0, -1),
489 IndexedConvexPolygonNormal(@CWPolyIndex, High(CWPolyIndex) + 1,
490 @Poly, High(Poly) + 1, TVector3.Zero));
491
492 AssertSameValue(8,
493 IndexedConvexPolygonArea(@CCWPolyIndex, High(CCWPolyIndex) + 1,
494 @Poly, High(Poly) + 1));
495
496 AssertSameValue(8,
497 IndexedConvexPolygonArea(@CWPolyIndex , High(CWPolyIndex) + 1,
498 @Poly, High(Poly) + 1));
499 end;
500
501 procedure TTestCastleVectors.TestSphereRayIntersection;
502 var
503 Res: boolean;
504 I: TVector3;
505 begin
506 Res := TrySphereRayIntersection(I, Vector3(3, 0, 0), 10,
507 Vector3(0, 0, 0), Vector3(1, 0, 0));
508 AssertTrue(Res);
509 AssertVectorEquals(Vector3(13, 0, 0), I);
510
511 Res := TrySphereRayIntersection(I, Vector3(3, 0, 0), 10,
512 Vector3(0, 0, 0), Vector3(-1, 0, 0));
513 AssertTrue(Res);
514 AssertVectorEquals(Vector3(-7, 0, 0), I);
515
516 Res := TrySphereRayIntersection(I, Vector3(3, 0, 0), 10,
517 Vector3(20, 0, 0), Vector3(1, 0, 0));
518 AssertFalse(Res);
519
520 Res := TrySphereRayIntersection(I, Vector3(3, 0, 0), 10,
521 Vector3(20, 0, 0), Vector3(-1, 0, 0));
522 AssertTrue(Res);
523 AssertVectorEquals(Vector3(13, 0, 0), I);
524 end;
525
526 { global utils --------------------------------------------------------------- }
527
528 function RandomVector: TVector3;
529 begin
530 Result := Vector3(
531 Random*1000,
532 Random*1000,
533 Random*1000
534 );
535 end;
536
537 function RandomMatrix: TMatrix4;
538 var
539 I, J: Integer;
540 begin
541 for I := 0 to 3 do
542 for J := 0 to 3 do
543 Result.Data[I, J] := 50 - Random * 100;
544 end;
545
546 function RandomNonProjectionMatrix: TMatrix4;
547 var
548 I, J: Integer;
549 begin
550 for I := 0 to 3 do
551 for J := 0 to 2 do
552 Result.Data[I, J] := 50 - Random * 100;
553
554 Result.Data[0, 3] := 0;
555 Result.Data[1, 3] := 0;
556 Result.Data[2, 3] := 0;
557 Result.Data[3, 3] := 1;
558 end;
559
560 procedure TTestCastleVectors.TestMatrixMultiplication;
561 var
562 M1, M2, M3, Result1, Result2: TMatrix4;
563 begin
564 M1.Columns[0] := Vector4(1, 0, 0, 0);
565 M1.Columns[1] := Vector4(0, 1, 0, 0);
566 M1.Columns[2] := Vector4(0, 0, 1, 0);
567 M1.Columns[3] := Vector4(-0.31, 1.26, -0.03, 1);
568
569 M2.Columns[0] := Vector4( 0.58, 0.75, 0.31, 0.00);
570 M2.Columns[1] := Vector4(-0.81, 0.52, 0.26, 0.00);
571 M2.Columns[2] := Vector4( 0.03, -0.40, 0.92, 0.00);
572 M2.Columns[3] := Vector4( 0.00, 0.00, 0.00, 1.00);
573
574 M3.Columns[0] := Vector4(1.00, 0.00, 0.00, 0.31);
575 M3.Columns[1] := Vector4(0.00, 1.00, 0.00, -1.26);
576 M3.Columns[2] := Vector4(0.00, 0.00, 1.00, 0.03);
577 M3.Columns[3] := Vector4(0.00, 0.00, 0.00, 1.00);
578
579 Result1 := M1 * M2;
580 Result2 := M1 * M2;
581 AssertMatrixEquals(Result1, Result2, 0.1);
582
583 Result2 := M1 * M2 * M3;
584
585 Result1 := M1 * M2;
586 Result1 := Result1 * M3;
587 AssertMatrixEquals(Result1, Result2, 0.1);
588
589 Result1 := M1 * M2 * M3;
590 AssertMatrixEquals(Result1, Result2, 0.1);
591 end;
592
593 procedure TTestCastleVectors.TestMatrixTranspose;
594 var
595 M1, M2: TMatrix3;
596 begin
597 M1.Columns[0] := Vector3(1, 2, 3);
598 M1.Columns[1] := Vector3(4, 5, 6);
599 M1.Columns[2] := Vector3(7, 8, 9);
600
601 M2.Columns[0] := Vector3(1, 4, 7);
602 M2.Columns[1] := Vector3(2, 5, 8);
603 M2.Columns[2] := Vector3(3, 6, 9);
604
605 M1 := M1.Transpose;
606 AssertTrue(TMatrix3.PerfectlyEquals(M1, M2));
607 end;
608
609 procedure TTestCastleVectors.TestVector3FromStr;
610 var
611 V: TVector3;
612 begin
613 try
614 V := Vector3FromStr('1 2 abc');
615 Fail('Above should fail with EConvertError');
616 except on EConvertError do ; end;
617
618 try
619 V := Vector3FromStr('1 2 3 4');
620 Fail('Above should fail with EConvertError');
621 except on EConvertError do ; end;
622
623 try
624 V := Vector3FromStr('1 2');
625 Fail('Above should fail with EConvertError');
626 except on EConvertError do ; end;
627
628 try
629 V := Vector3FromStr('');
630 Fail('Above should fail with EConvertError');
631 except on EConvertError do ; end;
632
633 V := Vector3FromStr(' 11 22 ' + NL + ' 33 ');
634 AssertSameValue(11, V[0]);
635 AssertSameValue(22, V[1]);
636 AssertSameValue(33, V[2]);
637 end;
638
639 procedure TTestCastleVectors.TestVector4FromStr;
640 var
641 V: TVector4;
642 begin
643 try
644 V := Vector4FromStr('1 2 3 abc');
645 Fail('Above should fail with EConvertError');
646 except on EConvertError do ; end;
647
648 try
649 V := Vector4FromStr('1 2 3 4 5');
650 Fail('Above should fail with EConvertError');
651 except on EConvertError do ; end;
652
653 try
654 V := Vector4FromStr('1 2 3');
655 Fail('Above should fail with EConvertError');
656 except on EConvertError do ; end;
657
658 try
659 V := Vector4FromStr('');
660 Fail('Above should fail with EConvertError');
661 except on EConvertError do ; end;
662
663 V := Vector4FromStr(' 11 22 ' + NL + ' 33 44');
664 AssertSameValue(11, V[0]);
665 AssertSameValue(22, V[1]);
666 AssertSameValue(33, V[2]);
667 AssertSameValue(44, V[3]);
668 end;
669
670 procedure TTestCastleVectors.TestPlaneTransform;
671
672 function PointLiesOnPlane(const Point: TVector3; const Plane: TVector4): boolean;
673 var
674 PlaneDir: TVector3 absolute Plane;
675 begin
676 // Writeln('point ', Point.ToString, ' gives ',
677 // (TVector3.DotProduct(Point, PlaneDir) + Plane[3]):1:2);
678 Result := IsZero(TVector3.DotProduct(Point, PlaneDir) + Plane[3], 0.001);
679 end;
680
681 procedure DoTest(const Plane: TVector4; const Matrix: TMatrix4;
682 const PointsYes: array of TVector3;
683 const PointsNo: array of TVector3);
684 var
685 I: Integer;
686 NewPlane: TVector4;
687 begin
688 NewPlane := PlaneTransform(Plane, Matrix);
689 // Writeln('New plane ', NewPlane.ToString);
690 for I := 0 to High(PointsYes) do
691 AssertTrue(PointLiesOnPlane(PointsYes[I], NewPlane));
692 for I := 0 to High(PointsNo) do
693 AssertTrue(not PointLiesOnPlane(PointsNo[I], NewPlane));
694 end;
695
696 begin
697 { x = 0 plane }
698 DoTest(Vector4(1, 0, 0, 0),
699 TMatrix4.Identity,
700 [ Vector3(0, 10, 10),
701 Vector3(0, -10, 10),
702 Vector3(0, 10, -10),
703 Vector3(0, -10, -10) ],
704 [ Vector3( 10, 10, 123),
705 Vector3(-10, 10, 2),
706 Vector3( 10, -10, -3),
707 Vector3(1, 0, 0) ]);
708
709 { rotate x = 0 plane to make z = 0 }
710 DoTest(Vector4(1, 0, 0, 0),
711 RotationMatrixDeg(90, 0, 1, 0),
712 [ Vector3( 10, 10, 0),
713 Vector3(-10, 10, 0),
714 Vector3( 10, -10, 0),
715 Vector3(-10, -10, 0) ],
716 [ Vector3( 10, 10, 123),
717 Vector3(-10, 10, 2),
718 Vector3( 10, -10, -3),
719 Vector3(0, 0, 1) ]);
720
721 { rotate and move x = 0 plane to make z = 10 }
722 DoTest(Vector4(1, 0, 0, 0),
723 TranslationMatrix(Single(0), 0, 10) * RotationMatrixDeg(90, 0, 1, 0),
724 [ Vector3( 10, 10, 10),
725 Vector3(-10, 10, 10),
726 Vector3( 10, -10, 10),
727 Vector3(-10, -10, 10) ],
728 [ Vector3( 10, 10, 0),
729 Vector3(-10, 10, 0),
730 Vector3( 10, -10, 0),
731 Vector3(-10, -10, 0),
732 Vector3( 10, 10, 123),
733 Vector3(-10, 10, 2),
734 Vector3( 10, -10, -3),
735 Vector3(0, 0, 1) ]);
736
737 { rotate and move and scale x = 0 plane to make z = 100 }
738 DoTest(Vector4(1, 0, 0, 0),
739 ScalingMatrix(Vector3(10, 10, 10)) *
740 TranslationMatrix(Single(0), 0, 10) *
741 RotationMatrixDeg(90, 0, 1, 0),
742 [ Vector3( 10, 10, 100),
743 Vector3(-10, 10, 100),
744 Vector3( 10, -10, 100),
745 Vector3(-10, -10, 100) ],
746 [ Vector3( 10, 10, 10),
747 Vector3(-10, 10, 10),
748 Vector3( 10, -10, 0),
749 Vector3(-10, -10, 0),
750 Vector3( 10, 10, 123),
751 Vector3(-10, 10, 2),
752 Vector3( 10, -10, -3),
753 Vector3(0, 0, 1) ]);
754 end;
755
756 procedure TTestCastleVectors.TestTransformToFromCoordsMatrix;
757 var
758 M, MInverse: TMatrix4;
759 NewOrigin, NewX, NewY, NewZ: TVector3;
760 begin
761 NewOrigin := RandomVector;
762 repeat NewX := RandomVector.Normalize until not NewX.IsZero;
763 NewY := AnyOrthogonalVector(NewX).Normalize;
764 NewZ := TVector3.CrossProduct(NewX, NewY);
765
766 M := TransformToCoordsMatrix (NewOrigin, NewX, NewY, NewZ);
767 MInverse := TransformFromCoordsMatrix(NewOrigin, NewX, NewY, NewZ);
768
769 try
770 AssertMatrixEquals(TMatrix4.Identity, M * MInverse, 0.01);
771 AssertMatrixEquals(TMatrix4.Identity, MInverse * M, 0.01);
772 except
773 Writeln('Failed for origin=', NewOrigin.ToRawString,
774 ' newX=', NewX.ToRawString);
775 raise;
776 end;
777 end;
778
779 procedure TTestCastleVectors.Test2D;
780 const
781 P1: TVector3 = (X: 1; Y: 2; Z: 3);
782 P2: TVector3 = (X: 2; Y: 5; Z: 13);
783 begin
784 AssertSameValue(Sqr(1) + Sqr(3) + Sqr(10), PointsDistanceSqr(P1, P2), 0.01);
785 AssertSameValue(Sqr(3) + Sqr(10), PointsDistance2DSqr(P1, P2, 0), 0.01);
786 AssertSameValue(Sqr(1) + Sqr(10), PointsDistance2DSqr(P1, P2, 1), 0.01);
787 AssertSameValue(Sqr(1) + Sqr(3), PointsDistance2DSqr(P1, P2, 2), 0.01);
788 try
789 PointsDistance2DSqr(P1, P2, 3);
790 Fail('Above PointsDistance2DSqr with IgnoreIndex = 3 should raise exception');
791 except end;
792 end;
793
794 procedure TTestCastleVectors.TestApproximateScale;
795 const
796 Epsilon = 0.0001;
797 begin
798 AssertSameValue(2, Approximate3DScale(2, 2, 2), Epsilon);
799 AssertSameValue(-2, Approximate3DScale(-2, -2, -2), Epsilon);
800 AssertSameValue(1, Approximate3DScale(1, 1, 1), Epsilon);
801 AssertSameValue(-1, Approximate3DScale(-1, -1, -1), Epsilon);
802 AssertSameValue(7/3, Approximate3DScale(1, 3, 3), Epsilon);
803 AssertSameValue(-7/3, Approximate3DScale(-1, -3, -3), Epsilon);
804 AssertSameValue(1, Approximate3DScale(-1, 1, 1), Epsilon);
805
806 AssertSameValue(2, Approximate2DScale(2, 2), Epsilon);
807 AssertSameValue(-2, Approximate2DScale(-2, -2), Epsilon);
808 AssertSameValue(1, Approximate2DScale(-1, 1), Epsilon);
809 end;
810
811 procedure TTestCastleVectors.TestXYZ;
812 const
813 V2Const: TVector2 = (X: 1; Y: 2);
814 V3Const: TVector3 = (X: 1; Y: 2; Z: 3);
815 V4Const: TVector4 = (X: 1; Y: 2; Z: 3; W: 4);
816 var
817 V2: TVector2;
818 V3: TVector3;
819 V4: TVector4;
820 begin
821 V2 := V2Const;
822 V3 := V3Const;
823 V4 := V4Const;
824
825 AssertEquals(1, V2.X);
826 V2.X := 33;
827 AssertEquals(33, V2.X);
828 AssertEquals(2, V2.Y);
829
830 AssertEquals(1, V3.X);
831 AssertEquals(2, V3.Y);
832 AssertEquals(3, V3.Z);
833 V3.Z := 44;
834 AssertEquals(1, V3.X);
835 AssertEquals(2, V3.Y);
836 AssertEquals(44, V3.Z);
837
838 AssertEquals(1, V4.X);
839 AssertEquals(2, V4.Y);
840 AssertEquals(3, V4.Z);
841 AssertEquals(4, V4.W);
842 end;
843
844 procedure TTestCastleVectors.TestPlaneMove;
845 var
846 Plane: TVector4;
847 begin
848 Plane := Vector4(1, 0, 0, 10); // x = -10
849 AssertVectorEquals(Vector4(1, 0, 0, 9), PlaneMove(Plane, Vector3(1, 2, 3)));
850
851 Plane := Vector4(1, 0, 0, 10); // x = -10
852 PlaneMoveVar(Plane, Vector3(1, 2, 3));
853 AssertVectorEquals(Vector4(1, 0, 0, 9), Plane);
854
855 Plane := Vector4(0, 1, 0, 10); // y = -10
856 AssertVectorEquals(Vector4(0, 1, 0, 8), PlaneMove(Plane, Vector3(1, 2, 3)));
857
858 Plane := Vector4(0, 1, 0, 10); // y = -10
859 PlaneMoveVar(Plane, Vector3(1, 2, 3));
860 AssertVectorEquals(Vector4(0, 1, 0, 8), Plane);
861
862 Plane := Vector4(0, 1, 0, 8); // y = -10
863 AssertVectorEquals(Vector4(0, 1, 0, 10), PlaneAntiMove(Plane, Vector3(1, 2, 3)));
864 end;
865
866 procedure TTestCastleVectors.TestPlaneMoveRandom;
867 var
868 I: Integer;
869 Plane: TVector4;
870 Move, PlaneDir: TVector3;
871 begin
872 for I := 1 to 100 do
873 begin
874 repeat
875 PlaneDir := RandomVector;
876 until not PlaneDir.IsZero;
877 Plane := Vector4(PlaneDir, Random * 100);
878 Move := RandomVector;
879 // "PlaneAntiMove + PlaneMove" should zero each other out
880 AssertVectorEquals(Plane, PlaneAntiMove(PlaneMove(Plane, Move), Move), 1.0);
881 end;
882 end;
883
884 procedure TTestCastleVectors.TestTryInverseHarder;
885 var
886 M, M2: TMatrix4;
887 begin
888 // used in gate_backround in castle-game demo
889
890 M[0, 0] := -0.001710;
891 M[1, 0] := -0.004698;
892 M[2, 0] := 0.000000;
893 M[3, 0] := 0.000000;
894
895 M[0, 1] := 0.004698;
896 M[1, 1] := -0.001710;
897 M[2, 1] := 0.000000;
898 M[3, 1] := 0.000000;
899
900 M[0, 2] := 0.000000;
901 M[1, 2] := 0.000000;
902 M[2, 2] := 0.005000;
903 M[3, 2] := 0.000000;
904
905 M[0, 3] := -79.753189;
906 M[1, 3] := -70.291077;
907 M[2, 3] := 0.182218;
908 M[3, 3] := 1.000000;
909
910 AssertFalse(M.TryInverse(M2));
911 AssertTrue(TryInverseHarder(M, M2));
912 end;
913
914 procedure TTestCastleVectors.TestMaxAbsVectorCoord;
915 begin
916 AssertEquals(0, MaxVectorCoord(Vector2(1, -10)));
917 AssertEquals(1, MaxVectorCoord(Vector2(1, 10)));
918
919 AssertEquals(2, MaxVectorCoord(Vector3(1, 2, 3)));
920 AssertEquals(0, MaxVectorCoord(Vector3(10, 2, 3)));
921 AssertEquals(0, MaxVectorCoord(Vector3(1, 1, 1)));
922 AssertEquals(1, MaxVectorCoord(Vector3(1, 2, 2)));
923 AssertEquals(1, MaxVectorCoord(Vector3(1, 2, -3)));
924
925 AssertEquals(3, MaxVectorCoord(Vector4(1, 2, 3, 10)));
926 AssertEquals(0, MaxVectorCoord(Vector4(-1, -2, -3, -10)));
927
928 AssertEquals(1, MaxAbsVectorCoord(Vector2(1, -10)));
929 AssertEquals(1, MaxAbsVectorCoord(Vector2(1, 10)));
930
931 AssertEquals(2, MaxAbsVectorCoord(Vector3(1, 10, -20)));
932 AssertEquals(1, MaxAbsVectorCoord(Vector3(1, -20, 10)));
933
934 AssertEquals(2, MaxAbsVectorCoord(Vector3(1, 2, 3)));
935 AssertEquals(0, MaxAbsVectorCoord(Vector3(10, 2, 3)));
936 AssertEquals(0, MaxAbsVectorCoord(Vector3(1, 1, 1)));
937 AssertEquals(1, MaxAbsVectorCoord(Vector3(1, 2, 2)));
938 AssertEquals(2, MaxAbsVectorCoord(Vector3(1, 2, -3)));
939
940 AssertEquals(3, MaxAbsVectorCoord(Vector4(1, 2, 3, 10)));
941 AssertEquals(3, MaxAbsVectorCoord(Vector4(-1, -2, -3, -10)));
942 end;
943
944 procedure TTestCastleVectors.TestMinAbsVectorCoord;
945 begin
946 AssertEquals(0, MinAbsVectorCoord(Vector2(1, -10)));
947 AssertEquals(0, MinAbsVectorCoord(Vector2(1, 10)));
948 AssertEquals(1, MinAbsVectorCoord(Vector2(-10, -1)));
949 AssertEquals(1, MinAbsVectorCoord(Vector2( 10, 1)));
950
951 AssertEquals(0, MinAbsVectorCoord(Vector3(1, 2, 3)));
952 AssertEquals(1, MinAbsVectorCoord(Vector3(33, 2, 3)));
953 AssertEquals(2, MinAbsVectorCoord(Vector3(3, 2, 1)));
954
955 AssertEquals(0, MinAbsVectorCoord(Vector4(1, 2, 3, 10)));
956 AssertEquals(0, MinAbsVectorCoord(Vector4(-1, -2, -3, -10)));
957 end;
958
959 procedure TTestCastleVectors.TestPointOnLineClosestToLine;
960 var
961 I: TVector3;
962 begin
963 // lines parallel
964 AssertFalse(PointOnLineClosestToLine(I,
965 Vector3(0, 0, 0), Vector3(1, 1, 1),
966 Vector3(10, 1, 1), Vector3(1, 1, 1)
967 ));
968
969 AssertTrue(PointOnLineClosestToLine(I,
970 Vector3(0, 0, 0), Vector3(1, 1, 1),
971 Vector3(0, 0, 0), Vector3(-1, 1, 1)
972 ));
973 AssertVectorEquals(Vector3(0, 0, 0), I);
974
975 AssertTrue(PointOnLineClosestToLine(I,
976 Vector3(1, 2, 3), Vector3(1, 1, 1),
977 Vector3(1, 2, 3), Vector3(-1, 1, 1)
978 ));
979 AssertVectorEquals(Vector3(1, 2, 3), I);
980
981 AssertTrue(PointOnLineClosestToLine(I,
982 Vector3(0, 0, 0), Vector3(1, 0, 0),
983 Vector3(110, 10, 10), Vector3(1, 1, 1)
984 ));
985 AssertVectorEquals(Vector3(100, 0, 0), I);
986
987 AssertTrue(PointOnLineClosestToLine(I,
988 Vector3(0, 0, 0), Vector3(1, 0, 0),
989 Vector3(110, 10, 10), Vector3(1, 1, 0)
990 ));
991 AssertVectorEquals(Vector3(100, 0, 0), I);
992
993 AssertTrue(PointOnLineClosestToLine(I,
994 Vector3(0.00, 0.00, 0.00),
995 Vector3(0.00, 1.00, 0.00),
996 Vector3(-6.58, 1.97, -5.73),
997 Vector3(0.74, -0.16, 0.65)
998 ));
999 AssertVectorEquals(Vector3(0, 0.5, 0), I, 0.1);
1000 end;
1001
1002 procedure TTestCastleVectors.TestRotatePointAroundAxis;
1003 begin
1004 AssertVectorEquals(
1005 RotatePointAroundAxis90(Vector3(1, 2, 3), Vector3(4, 5, 6)),
1006 RotatePointAroundAxisRad(Pi / 2, Vector3(1, 2, 3), Vector3(4, 5, 6)));
1007 end;
1008
1009 procedure TTestCastleVectors.TestMakeVectorOrthogonal;
1010 var
1011 V, NewV: TVector3;
1012 begin
1013 V := Vector3(Sqrt(2) / 2, Sqrt(2) / 2, 0);
1014 NewV := MakeVectorOrthogonal(V, Vector3(0, 1, 0));
1015 AssertVectorEquals(Vector3(1, 0, 0), NewV);
1016 end;
1017
1018 procedure TTestCastleVectors.TestEpsilonInModelViewToNormalMatrix;
1019 var
1020 Mv1, Mv2: TMatrix4;
1021 Norml1Approx: TMatrix3;
1022 begin
1023 {
1024 Mv1 and Mv2 are 2 TMatrix4 values which are close, but not precisely equal.
1025 If ModelViewToNormalMatrix would use IsZero(D), then it would yield
1026 drastically different results for them, due to one matrix resulting
1027 in determinant close enough to Single epsilon, and other not.
1028
1029 ------------------------------------------------------------------------------
1030 Exact test input, generated from testcase on https://github.com/castle-engine/castle-model-viewer/issues/35,
1031 with mesh renderer code:
1032
1033 if Arrays.Count = 629 then
1034 begin
1035 Writeln('Arrays.Count ', Arrays.Count);
1036 Writeln('ModelView ', Shape.ModelView.ToString(' '));
1037 Writeln('ModelViewRaw ', Shape.ModelView.ToRawString(' '));
1038 Writeln('NormalMatrix ', ModelViewToNormalMatrix(Shape.ModelView).ToString(' '));
1039 Writeln('NormalMatrixRaw ', ModelViewToNormalMatrix(Shape.ModelView).ToRawString(' '));
1040 end;
1041
1042 ---------------------------------------------------------------------------
1043 ModelView 0.05 0.04 0.00 0.12
1044 0.00 -0.01 -0.02 -0.03
1045 -0.02 0.09 0.00 -0.41
1046 0.00 0.00 0.00 1.00
1047
1048 ModelViewRaw 0.04658060148358345 0.035663504153490067 -0.0014015145134180784 0.12454544752836227
1049 -0.0025431409012526274 -0.0062981243245303631 -0.019934356212615967 -0.025968225672841072
1050 -0.017993897199630737 0.093211852014064789 -0.0008106923196464777 -0.40757930278778076
1051 0 0 0 1
1052
1053 NormalMatrix (ModelViewToNormalMatrix determinant 0.000100) 18.63 3.57 -3.50
1054 -1.02 -0.63 -49.84
1055 -7.20 9.32 -2.03
1056
1057 NormalMatrixRaw (ModelViewToNormalMatrix determinant 0.000100) 18.632238388061523 3.5663502216339111 -3.503786563873291
1058 -1.0172562599182129 -0.62981235980987549 -49.835891723632813
1059 -7.1975584030151367 9.3211841583251953 -2.0267307758331299
1060
1061 (ModelViewToNormalMatrix determinant 0.000100)(ModelViewToNormalMatrix determinant 1.000000)Arrays.Count 629
1062
1063 ModelView 0.05 0.04 0.00 0.12
1064 0.00 -0.01 -0.02 -0.03
1065 -0.02 0.09 0.00 -0.41
1066 0.00 0.00 0.00 1.00
1067
1068 ModelViewRaw 0.046580597758293152 0.03566349670290947 -0.0014015162596479058 0.12454545497894287
1069 -0.0025431474205106497 -0.0062981159426271915 -0.019934354349970818 -0.025968223810195923
1070 -0.017993893474340439 0.093211852014064789 -0.00081068999134004116 -0.40757903456687927
1071 0 0 0 1
1072
1073 NormalMatrix (ModelViewToNormalMatrix determinant 0.000100) 0.05 0.04 0.00
1074 0.00 -0.01 -0.02
1075 -0.02 0.09 0.00
1076 ------------------------------------------------------------------------------
1077 }
1078
1079 Mv1.Columns[0] := Vector4(
1080 0.04658060148358345,
1081 -0.0025431409012526274,
1082 -0.017993897199630737,
1083 0
1084 );
1085 Mv1.Columns[1] := Vector4(
1086 0.035663504153490067,
1087 -0.0062981243245303631,
1088 0.093211852014064789,
1089 0
1090 );
1091 Mv1.Columns[2] := Vector4(
1092 -0.0014015145134180784,
1093 -0.019934356212615967,
1094 -0.0008106923196464777,
1095 0
1096 );
1097 Mv1.Columns[3] := Vector4(
1098 0.12454544752836227,
1099 -0.025968225672841072,
1100 -0.40757930278778076,
1101 1
1102 );
1103
1104 Mv2.Columns[0] := Vector4(
1105 0.046580597758293152,
1106 -0.0025431474205106497,
1107 -0.017993893474340439,
1108 0
1109 );
1110 Mv2.Columns[1] := Vector4(
1111 0.03566349670290947,
1112 -0.0062981159426271915,
1113 0.093211852014064789,
1114 0
1115 );
1116 Mv2.Columns[2] := Vector4(
1117 -0.0014015162596479058,
1118 -0.019934354349970818,
1119 -0.00081068999134004116,
1120 0
1121 );
1122 Mv2.Columns[3] := Vector4(
1123 0.12454545497894287,
1124 -0.025968223810195923,
1125 -0.40757903456687927,
1126 1
1127 );
1128
1129 AssertTrue(TMatrix4.Equals(Mv1, Mv2));
1130 // Writeln(ModelViewToNormalMatrix(Mv1).ToString(' '));
1131 // Writeln(ModelViewToNormalMatrix(Mv2).ToString(' '));
1132
1133 Norml1Approx.Columns[0] := Vector3(
1134 18.63,
1135 -1.02,
1136 -7.20
1137 );
1138 Norml1Approx.Columns[1] := Vector3(
1139 3.57,
1140 -0.63,
1141 9.32
1142 );
1143 Norml1Approx.Columns[2] := Vector3(
1144 -3.50,
1145 -49.84,
1146 -2.03
1147 );
1148 AssertTrue(TMatrix3.Equals(ModelViewToNormalMatrix(Mv1), Norml1Approx, 0.01));
1149 AssertTrue(TMatrix3.Equals(ModelViewToNormalMatrix(Mv2), Norml1Approx, 0.01));
1150 end;
1151
1152 procedure TTestCastleVectors.TestInit;
1153 var
1154 V2: TVector2;
1155 V3: TVector3;
1156 V4: TVector4;
1157 begin
1158 V4.Init(1, 2, 3, 4);
1159 AssertVectorEquals(Vector4(1, 2, 3, 4), V4);
1160
1161 V3.Init(11, 22, 33);
1162 AssertVectorEquals(Vector3(11, 22, 33), V3);
1163
1164 V2.Init(111, 222);
1165 AssertVectorEquals(Vector2(111, 222), V2);
1166 end;
1167
1168 procedure TTestCastleVectors.TestRotationZeroAxis;
1169 var
1170 T: TTransformation;
1171 begin
1172 T.Init;
1173 T.Multiply(
1174 Vector4(0, 0, 0, Pi/2),
1175 Vector3(1, 1, 1),
1176 Vector3(0, 0, 0)
1177 );
1178 AssertMatrixEquals(T.Transform, TMatrix4.Identity);
1179 AssertMatrixEquals(T.InverseTransform, TMatrix4.Identity);
1180 AssertSameValue(1, T.Scale);
1181 AssertTrue(T.UniformScale);
1182 end;
1183
1184 procedure TTestCastleVectors.TestVectorsList;
1185
1186 { This test is similar to TTestGenericsCollections.TestVectorsList,
1187 but now using TVector2 instead of TMyVector. }
1188
1189 var
1190 List: TVector2List;
1191 R1, R2, R: TVector2;
1192 begin
1193 List := TVector2List.Create;
1194 try
1195 R1.X := 11;
1196 R1.Y := 22;
1197 List.Add(R1);
1198
1199 R2.X := 33;
1200 R2.Y := 44;
1201 List.Add(R2);
1202
1203 R2.X := 33;
1204 R2.Y := 44;
1205 List.Add(R2);
1206
1207 AssertEquals(3, List.Count);
1208 AssertEquals(11, List[0].X);
1209 AssertEquals(22, List[0].Y);
1210 AssertEquals(33, List[1].X);
1211 AssertEquals(44, List[1].Y);
1212 AssertEquals(33, List[2].X);
1213 AssertEquals(44, List[2].Y);
1214
1215 List.Delete(2);
1216
1217 AssertEquals(2, List.Count);
1218 AssertEquals(11, List[0].X);
1219 AssertEquals(22, List[0].Y);
1220 AssertEquals(33, List[1].X);
1221 AssertEquals(44, List[1].Y);
1222
1223 AssertEquals(0, List.IndexOf(R1));
1224 AssertEquals(1, List.IndexOf(R2));
1225
1226 // change R1 and R2, to make sure it doesn't matter for tests
1227 R1.X := 111111;
1228 R1.Y := 222222;
1229 R2.X := 333333;
1230 R2.Y := 444444;
1231 AssertEquals(-1, List.IndexOf(R1));
1232 AssertEquals(-1, List.IndexOf(R2));
1233
1234 R.X := 11;
1235 R.Y := 22;
1236 AssertEquals(0, List.IndexOf(R));
1237
1238 R.X := 33;
1239 R.Y := 44;
1240 AssertEquals(1, List.IndexOf(R));
1241
1242 R.X := 11;
1243 R.Y := 22;
1244 List.Remove(R);
1245 AssertEquals(1, List.Count);
1246 AssertEquals(33, List[0].X);
1247 AssertEquals(44, List[0].Y);
1248
1249 R.X := 666;
1250 R.Y := 22;
1251 List.Remove(R); // does nothing, no such item
1252 AssertEquals(1, List.Count);
1253 AssertEquals(33, List[0].X);
1254 AssertEquals(44, List[0].Y);
1255 finally FreeAndNil(List) end;
1256 end;
1257
1258 procedure TTestCastleVectors.TestToString;
1259 var
1260 V2: TVector2;
1261 V3: TVector3;
1262 V4: TVector4;
1263 M2: TMatrix2;
1264 M3: TMatrix3;
1265 M4: TMatrix4;
1266 begin
1267 V2 := Vector2(0, 123.456);
1268 AssertEquals('0 123.46', V2.ToString);
1269
1270 V3 := Vector3(0, 123.456, 78.9);
1271 AssertEquals('0 123.46 78.9', V3.ToString);
1272
1273 V4 := Vector4(0, 123.456, 78.9, 99.99);
1274 AssertEquals('0 123.46 78.9 99.99', V4.ToString);
1275
1276 M2.Rows[0] := Vector2(1, 2);
1277 M2.Rows[1] := Vector2(3, 4);
1278 AssertEquals(
1279 '1 2' + LineEnding +
1280 '3 4',
1281 M2.ToString);
1282
1283 M3.Rows[0] := Vector3(1, 2, 3);
1284 M3.Rows[1] := Vector3(4, 5, 6);
1285 M3.Rows[2] := Vector3(7, 8, 9);
1286 AssertEquals(
1287 '1 2 3' + LineEnding +
1288 '4 5 6' + LineEnding +
1289 '7 8 9',
1290 M3.ToString);
1291
1292 M4.Rows[0] := Vector4(1, 2, 3, 4);
1293 M4.Rows[1] := Vector4(5, 6, 7, 8);
1294 M4.Rows[2] := Vector4(9, 10, 11, 12);
1295 M4.Rows[3] := Vector4(13, 14, 15, 16);
1296 AssertEquals(
1297 '1 2 3 4' + LineEnding +
1298 '5 6 7 8' + LineEnding +
1299 '9 10 11 12' + LineEnding +
1300 '13 14 15 16',
1301 M4.ToString);
1302 end;
1303
1304 procedure TTestCastleVectors.TestBasicVectorMatrix;
1305 var
1306 M: TMatrix4;
1307 V: TVector3;
1308 begin
1309 // vectors
1310 M := TranslationMatrix(10, 20, 30);
1311 V := Vector3(1, 2, 3);
1312 V := M.MultPoint(V);
1313 AssertVectorEquals(Vector3(11, 22, 33), V);
1314 end;
1315
1316 initialization
1317 RegisterTest(TTestCastleVectors);
1318 end.