123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- {
- This file is part of the Free Pascal run time library.
- Copyright (c) 2023 by Michael Van Canneyt
- member of the Free Pascal development team
- See the file COPYING.FPC, included in this distribution,
- for details about the copyright.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- **********************************************************************}
- unit utcpoint;
- {$mode ObjFPC}{$H+}
- interface
- uses
- Classes, SysUtils, fpcunit, testregistry, types, utmathvectorbase, system.math.vectors;
- type
- { TTestPoint3D }
- TTestPoint3D = Class(TCMathVectorsBase)
- Private
- FP : Array[1..3] of TPoint3D;
- procedure ClearPoints;
- function GetP(aIndex: Integer): TPoint3D;
- procedure SetP(aIndex: Integer; aValue: TPoint3D);
- Protected
- procedure SetUp; override;
- procedure TearDown; override;
- Property P1 : TPoint3D Index 1 Read GetP Write SetP;
- Property P2 : TPoint3D Index 2 Read GetP Write SetP;
- Property P3 : TPoint3D Index 3 Read GetP Write SetP;
- Published
- procedure TestHookUp;
- Procedure TestZero;
- Procedure TestCreate;
- Procedure TestCreate2DPointAndZ;
- Procedure TestCreateVector3D;
- Procedure TestAdd;
- Procedure TestMultiplyFactor;
- Procedure TestMultiply;
- Procedure TestSubtract;
- Procedure TestEqual;
- Procedure TestDivide;
- Procedure TestUnEqual;
- Procedure TestUnaryMinus;
- Procedure TestAngleCosine;
- Procedure TestCrossProduct;
- Procedure TestDistance;
- Procedure TestDotProduct;
- Procedure TestEqualsTo;
- Procedure TestLength;
- Procedure TestMidPoint;
- Procedure TestNormalize;
- Procedure TestOffsetPoint;
- Procedure TestOffsetDelta;
- Procedure TestReflect;
- Procedure TestRotate;
- end;
- implementation
- { TTestPoint3D }
- procedure TTestPoint3D.ClearPoints;
- var
- I : integer;
- begin
- For I:=1 to 3 do
- begin
- FP[I].X:=0;
- FP[I].Y:=0;
- FP[I].Z:=0;
- end;
- end;
- function TTestPoint3D.GetP(aIndex: Integer): TPoint3D;
- begin
- Result.X:=FP[AIndex].X;
- Result.Y:=FP[AIndex].Y;
- Result.Z:=FP[AIndex].Z;
- end;
- procedure TTestPoint3D.SetP(aIndex: Integer; aValue: TPoint3D);
- begin
- FP[AIndex].X:=aValue.X;
- FP[AIndex].Y:=aValue.Y;
- FP[AIndex].Z:=aValue.Z;
- end;
- procedure TTestPoint3D.SetUp;
- begin
- inherited SetUp;
- ClearPoints;
- end;
- procedure TTestPoint3D.TearDown;
- begin
- inherited TearDown;
- end;
- procedure TTestPoint3D.TestHookUp;
- var
- I : Integer;
- begin
- For I:=1 to 3 do
- begin
- AssertEquals('Point3D '+intTostr(i)+'.X',0.0,FP[I].X);
- AssertEquals('Point3D '+intTostr(i)+'.Y',0.0,FP[I].Y);
- AssertEquals('Point3D '+intTostr(i)+'.Z',0.0,FP[I].Z);
- end;
- end;
- procedure TTestPoint3D.TestZero;
- begin
- P1:=TPoint3D.Zero;
- AssertPoint3D('Zero',0,0,0,P1);
- end;
- procedure TTestPoint3D.TestCreate;
- begin
- P1:=TPoint3D.Create(1,2,3);
- AssertPoint3D('Create',1,2,3,P1);
- end;
- procedure TTestPoint3D.TestCreate2DPointAndZ;
- begin
- P1:=TPoint3D.Create(PointF(1,2),3);
- AssertPoint3D('Create',1,2,3,P1);
- end;
- procedure TTestPoint3D.TestCreateVector3D;
- begin
- P1:=TPoint3D.Create([1,2,3,4]);
- AssertPoint3D('Create',1,2,3,P1);
- end;
- procedure TTestPoint3D.TestAdd;
- begin
- P1:=TPoint3D.Create(1,2,3);
- P2:=TPoint3D.Create(5,4,3);
- P3:=P2+P1;
- AssertPoint3D('Add 1',6,6,6,P3);
- P2:=TPoint3D.Create(7,8,9);
- P3:=P2+P1;
- AssertPoint3D('Add 2',8,10,12,P3);
- end;
- procedure TTestPoint3D.TestMultiplyFactor;
- begin
- P1:=TPoint3D.Create(1,2,3);
- P2:=P1*3;
- AssertPoint3D('Factor 1',3,6,9,P2);
- P2:=2*P1;
- AssertPoint3D('Factor 2',2,4,6,P2);
- end;
- procedure TTestPoint3D.TestMultiply;
- begin
- P1:=TPoint3D.Create(1,2,3);
- P2:=TPoint3D.Create(5,4,3);
- P3:=P2*P1;
- AssertPoint3D('Create',5,8,9,P3);
- end;
- procedure TTestPoint3D.TestSubtract;
- begin
- P1:=TPoint3D.Create(1,2,3);
- P2:=TPoint3D.Create(5,4,3);
- P3:=P2-P1;
- AssertPoint3D('Subtract 1',4,2,0,P3);
- P2:=TPoint3D.Create(7,8,9);
- P3:=P2-P1;
- AssertPoint3D('Subtract 2',6,6,6,P3);
- end;
- procedure TTestPoint3D.TestEqual;
- begin
- P1:=TPoint3D.Create(1,2,3);
- P2:=TPoint3D.Create(5,4,3);
- AssertFalse('Equal NOk',P1=P2);
- P2:=TPoint3D.Create(1,2,3);
- AssertTrue('Equal OK',P1=P2);
- end;
- procedure TTestPoint3D.TestDivide;
- begin
- P1:=TPoint3D.Create(3,6,9);
- P2:=P1/3;
- AssertPoint3D('Factor 1',1,2,3,P2);
- end;
- procedure TTestPoint3D.TestUnEqual;
- begin
- P1:=TPoint3D.Create(1,2,3);
- P2:=TPoint3D.Create(5,4,3);
- AssertTrue('unEqual Ok',P1<>P2);
- P2:=TPoint3D.Create(1,2,3);
- AssertFalse('unEqual OK',P1<>P2);
- end;
- procedure TTestPoint3D.TestUnaryMinus;
- begin
- P1:=TPoint3D.Create(1,2,3);
- P2:=-P1;
- AssertPoint3D('Unary minus',-1,-2,-3,P2);
- end;
- procedure TTestPoint3D.TestAngleCosine;
- begin
- P1:=TPoint3D.Create(1,0,0);
- P2:=TPoint3D.Create(0,1,0);
- AssertEquals('Angle',0, P1.AngleCosine(P2));
- P1:=TPoint3D.Create(1,0,0);
- P2:=TPoint3D.Create(0,0,1);
- AssertEquals('Angle',0, P1.AngleCosine(P2));
- P1:=TPoint3D.Create(0,1,0);
- P2:=TPoint3D.Create(0,0,1);
- AssertEquals('Angle',0, P1.AngleCosine(P2));
- P1:=TPoint3D.Create(0,1,0);
- P2:=TPoint3D.Create(1,1,0);
- AssertEquals('Angle',c45, P1.AngleCosine(P2));
- end;
- procedure TTestPoint3D.TestCrossProduct;
- begin
- P1:=TPoint3D.Create(1,1,0);
- P2:=TPoint3D.Create(2,2,0);
- P3:=P2.CrossProduct(P1);
- AssertPoint3D('T1',0,0,0,P3);
- P1:=TPoint3D.Create(1,1,0);
- P2:=TPoint3D.Create(2,2,0);
- P3:=P2.CrossProduct(P1);
- AssertPoint3D('T2',0,0,0,P3);
- end;
- procedure TTestPoint3D.TestDistance;
- begin
- P1:=TPoint3D.Create(1,1,0);
- P2:=TPoint3D.Create(1,2,0);
- AssertEquals('Dist 1,',1,P1.Distance(P2));
- P1:=TPoint3D.Create(0,1,1);
- P2:=TPoint3D.Create(0,2,1);
- AssertEquals('Dist 2,',1,P1.Distance(P2));
- P1:=TPoint3D.Create(0,0,0);
- P2:=TPoint3D.Create(1,1,0);
- AssertEquals('Dist 3,',c45*2,P1.Distance(P2));
- end;
- procedure TTestPoint3D.TestDotProduct;
- begin
- P1:=TPoint3D.Create(3,4,9);
- P2:=TPoint3D.Create(3,4,9);
- AssertEquals('Test 1',9+16+81,P1.DotProduct(P2));
- P2:=TPoint3D.Create(1,1,0);
- AssertEquals('Test 1',7,P1.DotProduct(P2));
- end;
- procedure TTestPoint3D.TestEqualsTo;
- begin
- P1:=TPoint3D.Create(3,4,9);
- P2:=TPoint3D.Create(3,4,9);
- AssertTrue('Test 1',P1.EqualsTo(P2));
- P2:=TPoint3D.Create(1,1,1);
- AssertFalse('Test 2',P1.EqualsTo(P2));
- end;
- procedure TTestPoint3D.TestLength;
- begin
- P1:=TPoint3D.Create(1,0,0);
- AssertEquals('Dist 1,',1,P1.Length);
- P1:=TPoint3D.Create(0,1,1);
- AssertEquals('Dist 2,',c45*2,P1.Length);
- P1:=TPoint3D.Create(1,1,1);
- AssertEquals('Dist 3,',1.7321,P1.Length);
- end;
- procedure TTestPoint3D.TestMidPoint;
- begin
- P1:=TPoint3D.Create(0,0,1);
- P2:=TPoint3D.Create(0,2,1);
- AssertPoint3D('Midpoint1',0,1,1,P1.MidPoint(P2));
- P1:=TPoint3D.Create(1,0,0);
- P2:=TPoint3D.Create(0,1,0);
- AssertPoint3D('Midpoint2',0.5,0.5,0,P1.MidPoint(P2));
- end;
- procedure TTestPoint3D.TestNormalize;
- begin
- P1:=TPoint3D.Create(1,1,1);
- P2:=P1.Normalize;
- AssertPoint3D('Normalize 1',0.5773,0.5773,0.5773,P2);
- P1:=TPoint3D.Create(0,0,1);
- P2:=P1.Normalize;
- AssertPoint3D('Normalize 2',0,0,1,P2);
- P1:=TPoint3D.Create(1,0,0);
- P2:=P1.Normalize;
- AssertPoint3D('Normalize 3',1,0,0,P2);
- end;
- procedure TTestPoint3D.TestOffsetPoint;
- Var
- P : TPoint3D; // Cannot use property
- begin
- P:=TPoint3D.Create(1,2,3);
- P.Offset(Point3D(4,5,6));
- AssertPoint3D('Off',5,7,9,P);
- end;
- procedure TTestPoint3D.TestOffsetDelta;
- Var
- P : TPoint3D; // Cannot use property
- begin
- P:=TPoint3D.Create(0,0,0);
- P.Offset(1,2,3);
- AssertPoint3D('Off',1,2,3,P);
- end;
- procedure TTestPoint3D.TestReflect;
- // P2 is normal of reflecting surface: X,Y plane.
- begin
- P1:=TPoint3D.Create(0,0,1);
- P2:=TPoint3D.Create(0,0,1);
- P3:=P1.Reflect(P2);
- AssertPoint3D('Reflect 1',0,0,-1,P3);
- P1:=TPoint3D.Create(0,-1,-1);
- P3:=P1.Reflect(P2);
- AssertPoint3D('Reflect 2',0,-1,1,P3);
- end;
- procedure TTestPoint3D.TestRotate;
- begin
- P1:=TPoint3D.Create(1,0,0);
- P2:=TPoint3D.Create(0,0,1); // Z-axis
- P3:=P1.Rotate(P2,Pi/2);
- AssertPoint3D('Rotate 1',0,1,0,P3);
- P2:=TPoint3D.Create(0,1,0); // Y-axis
- P3:=P1.Rotate(P2,Pi/2);
- AssertPoint3D('Rotate 2',0,0,-1,P3); // Z is pointing down!
- end;
- initialization
- RegisterTests([TTestPoint3D]);
- end.
|