Skip to content

Commit

Permalink
Use epsilon from castle-anim-frames to compare fields
Browse files Browse the repository at this point in the history
  • Loading branch information
michaliskambi committed Aug 12, 2017
1 parent 5d59e60 commit fab51cb
Show file tree
Hide file tree
Showing 12 changed files with 307 additions and 248 deletions.
26 changes: 15 additions & 11 deletions src/base/castlestringutils.pas
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ TCastleStringList = class(TStringList)
The comparison is case-sensitive, or not, depending on the value
of CaseSensitive property of this list. }
function Equals(SecondValue: TObject): boolean; override; overload;
function Equals(SecondValue: TStrings; const Epsilon: Single): boolean; overload;

function Equals(const A: array of string): boolean; overload;

Expand Down Expand Up @@ -1031,21 +1032,24 @@ procedure TCastleStringList.Reverse;
end;

function TCastleStringList.Equals(SecondValue: TObject): boolean;
begin
Result :=
(SecondValue is TStrings) and
Equals(TStrings(SecondValue), 0);
end;

function TCastleStringList.Equals(SecondValue: TStrings; const Epsilon: Single): boolean; overload;
var
I: Integer;
begin
Result := SecondValue is TStrings;
Result := Count = SecondValue.Count;
if Result then
begin
Result := Count = TStrings(SecondValue).Count;
if Result then
for I := 0 to Count - 1 do
if DoCompareText(Strings[I], TStrings(SecondValue)[I]) <> 0 then
begin
Result := false;
Exit;
end;
end;
for I := 0 to Count - 1 do
if DoCompareText(Strings[I], SecondValue[I]) <> 0 then
begin
Result := false;
Exit;
end;
end;

function TCastleStringList.Equals(const A: array of string): boolean;
Expand Down
14 changes: 12 additions & 2 deletions src/base/castleutils_math.inc
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,21 @@
----------------------------------------------------------------------------
}

{ Very basic operations on numbers.
Also things related to setting 8087 Control Word. }
{ Basic operations on numbers. }

{$ifdef read_interface}

const
{ Epsilon used by default when compating Single (Single-precision float values).
Compatible with Math unit value, used by standard routines like Math.SameValue
and Math.IsZero. }
SingleEpsilon = 1E-4;

{ Epsilon used by default when compating Double (Double-precision float values).
Compatible with Math unit value, used by standard routines like Math.SameValue
and Math.IsZero. }
DoubleEpsilon = 1E-12;

{ Swap variables values.
@groupBegin }
procedure SwapValues(var a, b: Byte ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
Expand Down
112 changes: 64 additions & 48 deletions src/base/castleutils_primitive_lists.inc
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ type
{ Does the SecondValue have equal length and content.
The values are compared with an Epsilon tolerance, as usual for floating-point
values. }
function Equals(SecondValue: TObject): boolean; override;
function Equals(SecondValue: TObject): boolean; override; overload;
function Equals(SecondValue: TSingleList; const Epsilon: Single): boolean; overload;

{ Does the SecondValue have equal length and content.
The values are compared perfectly, without any tolerance for difference. }
Expand All @@ -91,7 +92,8 @@ type
{ Does the SecondValue have equal length and content.
The values are compared with an Epsilon tolerance, as usual for floating-point
values. }
function Equals(SecondValue: TObject): boolean; override;
function Equals(SecondValue: TObject): boolean; override; overload;
function Equals(SecondValue: TDoubleList; const Epsilon: Double): boolean; overload;

{ Does the SecondValue have equal length and content.
The values are compared perfectly, without any tolerance for difference. }
Expand All @@ -117,7 +119,8 @@ type
function ToLongInt: TLongIntList;

{ Does the SecondValue have equal length and content. }
function Equals(SecondValue: TObject): boolean; override;
function Equals(SecondValue: TObject): boolean; override; overload;
function Equals(SecondValue: TBooleanList; const Epsilon: Single): boolean; overload;

{ Does the SecondValue have equal length and content.
Expand All @@ -140,7 +143,8 @@ type
procedure AddDuplicate(const Item: LongInt; const DuplicateCount: Cardinal);

{ Does the SecondValue have equal length and content. }
function Equals(SecondValue: TObject): boolean; override;
function Equals(SecondValue: TObject): boolean; override; overload;
function Equals(SecondValue: TLongIntList; const Epsilon: Single): boolean; overload;

{ Does the SecondValue have equal length and content.
Expand Down Expand Up @@ -290,21 +294,24 @@ begin
end;

function TSingleList.Equals(SecondValue: TObject): boolean;
begin
Result :=
(SecondValue is TSingleList) and
Equals(TSingleList(SecondValue), SingleEpsilon);
end;

function TSingleList.Equals(SecondValue: TSingleList; const Epsilon: Single): boolean; overload;
var
I: Integer;
begin
Result := SecondValue is TSingleList;
Result := Count = SecondValue.Count;
if Result then
begin
Result := Count = TSingleList(SecondValue).Count;
if Result then
for I := 0 to Count - 1 do
if not SameValue(TSingleList(SecondValue).List^[I], List^[I]) then
begin
Result := false;
Exit;
end;
end;
for I := 0 to Count - 1 do
if not SameValue(SecondValue.List^[I], List^[I], Epsilon) then
begin
Result := false;
Exit;
end;
end;

function TSingleList.PerfectlyEquals(SecondValue: TObject): boolean;
Expand Down Expand Up @@ -364,21 +371,24 @@ begin
end;

function TDoubleList.Equals(SecondValue: TObject): boolean;
begin
Result :=
(SecondValue is TDoubleList) and
Equals(TDoubleList(SecondValue), DoubleEpsilon);
end;

function TDoubleList.Equals(SecondValue: TDoubleList; const Epsilon: Double): boolean; overload;
var
I: Integer;
begin
Result := SecondValue is TDoubleList;
Result := Count = SecondValue.Count;
if Result then
begin
Result := Count = TDoubleList(SecondValue).Count;
if Result then
for I := 0 to Count - 1 do
if not SameValue(TDoubleList(SecondValue).List^[I], List^[I]) then
begin
Result := false;
Exit;
end;
end;
for I := 0 to Count - 1 do
if not SameValue(SecondValue.List^[I], List^[I], Epsilon) then
begin
Result := false;
Exit;
end;
end;

function TDoubleList.PerfectlyEquals(SecondValue: TObject): boolean;
Expand Down Expand Up @@ -440,21 +450,24 @@ begin
end;

function TBooleanList.Equals(SecondValue: TObject): boolean;
begin
Result :=
(SecondValue is TBooleanList) and
Equals(TBooleanList(SecondValue), 0);
end;

function TBooleanList.Equals(SecondValue: TBooleanList; const Epsilon: Single): boolean; overload;
var
I: Integer;
begin
Result := SecondValue is TBooleanList;
Result := Count = SecondValue.Count;
if Result then
begin
Result := Count = TBooleanList(SecondValue).Count;
if Result then
for I := 0 to Count - 1 do
if TBooleanList(SecondValue).List^[I] <> List^[I] then
begin
Result := false;
Exit;
end;
end;
for I := 0 to Count - 1 do
if SecondValue.List^[I] <> List^[I] then
begin
Result := false;
Exit;
end;
end;

function TBooleanList.PerfectlyEquals(SecondValue: TObject): boolean;
Expand Down Expand Up @@ -508,21 +521,24 @@ begin
end;

function TLongIntList.Equals(SecondValue: TObject): boolean;
begin
Result :=
(SecondValue is TLongIntList) and
Equals(TLongIntList(SecondValue), 0);
end;

function TLongIntList.Equals(SecondValue: TLongIntList; const Epsilon: Single): boolean; overload;
var
I: Integer;
begin
Result := SecondValue is TLongIntList;
Result := Count = SecondValue.Count;
if Result then
begin
Result := Count = TLongIntList(SecondValue).Count;
if Result then
for I := 0 to Count - 1 do
if TLongIntList(SecondValue).List^[I] <> List^[I] then
begin
Result := false;
Exit;
end;
end;
for I := 0 to Count - 1 do
if SecondValue.List^[I] <> List^[I] then
begin
Result := false;
Exit;
end;
end;

function TLongIntList.PerfectlyEquals(SecondValue: TObject): boolean;
Expand Down
Loading

0 comments on commit fab51cb

Please sign in to comment.