| 1 |
{%MainUnit castlevectors.pas} |
| 2 |
{ |
| 3 |
Copyright 2017-2024 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 |
(*Interface and implementation of a TGenericVector* types, |
| 18 |
that can use any floating-point precision (Single, Double, Extended). |
| 19 |
|
| 20 |
---------------------------------------------------------------------------- |
| 21 |
Note that we cannot use proper generics to define a TGenericVector3<T>, |
| 22 |
because this cannot work under Delphi: |
| 23 |
|
| 24 |
function TGenericVector3 {$ifndef FPC}<T>{$endif}.Length: T; |
| 25 |
begin |
| 26 |
Result := Sqrt( |
| 27 |
(X * X) + |
| 28 |
(Y * Y) + |
| 29 |
(Z * Z) |
| 30 |
); |
| 31 |
end; |
| 32 |
|
| 33 |
Delphi wants to check the correctness when reading the generic, |
| 34 |
and it will not allow artihmetic operations on X, |
| 35 |
and it cannot narrow the type to something where artihmetic operations are OK. |
| 36 |
See |
| 37 |
https://stackoverflow.com/questions/40059579/arithmetic-operations-with-generic-types-in-delphi |
| 38 |
|
| 39 |
Under FPC it works, it seems that FPC checks Sqrt() correctness only later. |
| 40 |
But FPC doesn't allow "Sqr(X)" too, so it's possible that the lack |
| 41 |
of an early Sqrt check is just an FPC bug. |
| 42 |
|
| 43 |
---------------------------------------------------------------------------- |
| 44 |
Note: We avoid introducing in these records *any* method that changes |
| 45 |
the current record value. E.g. |
| 46 |
|
| 47 |
- X, Y, Z cannot be properties, esp. they cannot be properties with setters. |
| 48 |
They have to be simple fields. |
| 49 |
|
| 50 |
- Likewise we don't want a method like "Init" that modifies current record. |
| 51 |
|
| 52 |
The only way to change the record should be through direct field access. |
| 53 |
|
| 54 |
Why? Because it was leading to traps. |
| 55 |
|
| 56 |
S.Translation.X := S.Translation.X + 10; // when X is a setter |
| 57 |
S.Translation.Init; |
| 58 |
|
| 59 |
.. would compile, but actually be incorrect, as you (potentially -- depending |
| 60 |
on optimization) modify only the temporary record value. |
| 61 |
*) |
| 62 |
|
| 63 |
type |
| 64 |
{ Vector of 2 floating-point values. |
| 65 |
@seealso TGenericVector3 } |
| 66 |
TGenericVector2 = packed record |
| 67 |
public |
| 68 |
const |
| 69 |
Count = 2; |
| 70 |
type |
| 71 |
TIndex = 0..Count - 1; |
| 72 |
strict private |
| 73 |
function GetItems(const Index: TIndex): TGenericScalar; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 74 |
procedure SetItems(const Index: TIndex; const Value: TGenericScalar); {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 75 |
class function GetOne(const Index: TIndex): TGenericVector2; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} static; |
| 76 |
public |
| 77 |
{ Get vector components by index. |
| 78 |
This is a default property, so you can write @code(MyVector[0]) |
| 79 |
instead of @code(MyVector.Data[0]) or @code(MyVector.AsArray[0]). |
| 80 |
|
| 81 |
But note that this is not writeable (because exposing writeable properties on vectors |
| 82 |
would cause some subtle traps, see https://castle-engine.io/coding_traps ). |
| 83 |
Use @code(MyVector.Data[0] := 123.456) if you want to set by index, |
| 84 |
or @code(MyVector.X := 123.456). } |
| 85 |
property AsArray [const Index: TIndex]: TGenericScalar read GetItems; default; |
| 86 |
|
| 87 |
{ @exclude |
| 88 |
Access (get, set) vector components by index. } |
| 89 |
property InternalData [const Index: TIndex]: TGenericScalar read GetItems write SetItems; |
| 90 |
{$ifdef FPC}deprecated 'use Data or X,Y';{$endif} |
| 91 |
{ @exclude |
| 92 |
Access (get, set) vector components by index. } |
| 93 |
property Items [const Index: TIndex]: TGenericScalar read GetItems write SetItems; |
| 94 |
{$ifdef FPC}deprecated 'use Data or X,Y';{$endif} |
| 95 |
|
| 96 |
class operator {$ifdef FPC}+{$else}Add{$endif} (const A, B: TGenericVector2): TGenericVector2; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 97 |
class operator {$ifdef FPC}-{$else}Subtract{$endif} (const A, B: TGenericVector2): TGenericVector2; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 98 |
class operator {$ifdef FPC}-{$else}Negative{$endif} (const V: TGenericVector2): TGenericVector2; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 99 |
class operator {$ifdef FPC}*{$else}Multiply{$endif} (const V: TGenericVector2; const Scalar: TGenericScalar): TGenericVector2; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 100 |
class operator {$ifdef FPC}*{$else}Multiply{$endif} (const Scalar: TGenericScalar; const V: TGenericVector2): TGenericVector2; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 101 |
{ Vector * vector makes a component-wise multiplication. |
| 102 |
This is consistent with GLSL and other vector APIs. } |
| 103 |
class operator {$ifdef FPC}*{$else}Multiply{$endif} (const V1, V2: TGenericVector2): TGenericVector2; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 104 |
class operator {$ifdef FPC}/{$else}Divide{$endif} (const V: TGenericVector2; const Scalar: TGenericScalar): TGenericVector2; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 105 |
|
| 106 |
procedure Init(const AX, AY: TGenericScalar); {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 107 |
deprecated 'initialize instead like "V := Vector2(X, Y)"; modifying a temporary record value is a trap, e.g. this is not reliable: "Scene.TranslationXY.Init(X, Y)"'; |
| 108 |
|
| 109 |
function ToString: string; |
| 110 |
|
| 111 |
{ Convert to string using the most precise (not always easily readable |
| 112 |
by humans) float format. This may use the exponential |
| 113 |
(scientific) notation to represent the floating-point value, |
| 114 |
if needed. |
| 115 |
|
| 116 |
You can pass, as parameter, the format to use. By default it is '%g', |
| 117 |
"general number format" with maximum precision documented on |
| 118 |
https://www.freepascal.org/docs-html/rtl/sysutils/format.html . |
| 119 |
|
| 120 |
This is suitable for storing the value in a file, |
| 121 |
with a best precision possible. } |
| 122 |
function ToRawString(const FloatFormat: String = '%g'): string; |
| 123 |
|
| 124 |
function Normalize: TGenericVector2; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 125 |
procedure NormalizeMe; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 126 |
deprecated 'normalize instead like "V := V.Normalize"; modifying a temporary record value is a trap, e.g. this is not reliable: "Scene.TranslationXY.NormalizeMe"'; |
| 127 |
|
| 128 |
function Length: TGenericScalar; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 129 |
|
| 130 |
{ Vector length squared. |
| 131 |
This is slightly faster than @link(Length) as it avoids calculating |
| 132 |
a square root along the way. (But, please remember to not optimize |
| 133 |
your code without a need. Optimize only parts that are proven bottlenecks, |
| 134 |
otherwise don't make the code less readable for the sake of speed.) } |
| 135 |
function LengthSqr: TGenericScalar; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 136 |
|
| 137 |
{ Calculate a new vector scaled so that it has length equal to NewLength. |
| 138 |
NewLength may be negative, in which case we'll negate the vector |
| 139 |
and then adjust it's length to Abs(NewLength). } |
| 140 |
function AdjustToLength(const NewLength: TGenericScalar): TGenericVector2; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 141 |
|
| 142 |
{ Dot product of two vectors. |
| 143 |
See https://en.wikipedia.org/wiki/Dot_product . } |
| 144 |
class function DotProduct(const V1, V2: TGenericVector2): TGenericScalar; static; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 145 |
|
| 146 |
{ Absolute value on all components. } |
| 147 |
function Abs: TGenericVector2; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 148 |
|
| 149 |
{ Are all components equal to zero (within some epsilon margin). } |
| 150 |
function IsZero: boolean; overload; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 151 |
|
| 152 |
{ Are all components equal to zero (within Epsilon margin). } |
| 153 |
function IsZero(const Epsilon: TGenericScalar): boolean; overload; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 154 |
|
| 155 |
function IsPerfectlyZero: boolean; |
| 156 |
|
| 157 |
{ Compare two vectors, with epsilon to tolerate slightly different floats. } |
| 158 |
class function Equals(const V1, V2: TGenericVector2): boolean; overload; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} static; |
| 159 |
class function Equals(const V1, V2: TGenericVector2; const Epsilon: TGenericScalar): boolean; overload; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} static; |
| 160 |
|
| 161 |
{ Compare two vectors using exact comparison (like the "=" operator to compare floats). } |
| 162 |
class function PerfectlyEquals(const V1, V2: TGenericVector2): boolean; static; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 163 |
|
| 164 |
{ Linear interpolation between two vector values. |
| 165 |
@seealso TGenericVector3.Lerp } |
| 166 |
class function Lerp(const A: TGenericScalar; const V1, V2: TGenericVector2): TGenericVector2; static; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 167 |
|
| 168 |
class function Zero: TGenericVector2; static; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 169 |
class property One [const Index: TIndex]: TGenericVector2 read GetOne; |
| 170 |
|
| 171 |
case Integer of |
| 172 |
0: (X, Y: TGenericScalar); |
| 173 |
1: ({ Access (get, set) vector components by index. } Data: array [TIndex] of TGenericScalar); |
| 174 |
end; |
| 175 |
|
| 176 |
{ Vector of 3 floating-point values. |
| 177 |
|
| 178 |
This is generic type (although not using "proper" Pascal generics |
| 179 |
for implementation reasons). In has two actual uses: |
| 180 |
|
| 181 |
@orderedList( |
| 182 |
@itemSpacing Compact |
| 183 |
@item @link(TVector3), a vector of 3 Single values (floats with single precision), |
| 184 |
@item @link(TVector3Double), a vector of 3 Double values (floats with double precision). |
| 185 |
) |
| 186 |
|
| 187 |
The actual type of TGenericScalar is |
| 188 |
Single or Double for (respectively) @link(TVector3) or @link(TVector3Double). |
| 189 |
} |
| 190 |
TGenericVector3 = packed record |
| 191 |
public |
| 192 |
const |
| 193 |
Count = 3; |
| 194 |
type |
| 195 |
TIndex = 0..Count - 1; |
| 196 |
strict private |
| 197 |
function GetItems(const Index: TIndex): TGenericScalar; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 198 |
procedure SetItems(const Index: TIndex; const Value: TGenericScalar); {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 199 |
class function GetOne(const Index: TIndex): TGenericVector3; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} static; |
| 200 |
public |
| 201 |
{ Get vector components by index. |
| 202 |
This is a default property, so you can write @code(MyVector[0]) |
| 203 |
instead of @code(MyVector.Data[0]) or @code(MyVector.AsArray[0]). |
| 204 |
|
| 205 |
But note that this is not writeable (because exposing writeable properties on vectors |
| 206 |
would cause some subtle traps, see https://castle-engine.io/coding_traps ). |
| 207 |
Use @code(MyVector.Data[0] := 123.456) if you want to set by index, |
| 208 |
or @code(MyVector.X := 123.456). } |
| 209 |
property AsArray [const Index: TIndex]: TGenericScalar read GetItems; default; |
| 210 |
|
| 211 |
{ @exclude |
| 212 |
Access (get, set) vector components by index. } |
| 213 |
property InternalData [const Index: TIndex]: TGenericScalar read GetItems write SetItems; |
| 214 |
{$ifdef FPC}deprecated 'use Data or X, Y, Z';{$endif} |
| 215 |
{ @exclude |
| 216 |
Access (get, set) vector components by index. } |
| 217 |
property Items [const Index: TIndex]: TGenericScalar read GetItems write SetItems; |
| 218 |
{$ifdef FPC}deprecated 'use Data or X, Y, Z';{$endif} |
| 219 |
|
| 220 |
class operator {$ifdef FPC}+{$else}Add{$endif} (const A, B: TGenericVector3): TGenericVector3; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 221 |
class operator {$ifdef FPC}-{$else}Subtract{$endif} (const A, B: TGenericVector3): TGenericVector3; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 222 |
class operator {$ifdef FPC}-{$else}Negative{$endif} (const V: TGenericVector3): TGenericVector3; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 223 |
class operator {$ifdef FPC}*{$else}Multiply{$endif} (const V: TGenericVector3; const Scalar: TGenericScalar): TGenericVector3; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 224 |
class operator {$ifdef FPC}*{$else}Multiply{$endif} (const Scalar: TGenericScalar; const V: TGenericVector3): TGenericVector3; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 225 |
{ Vector * vector makes a component-wise multiplication. |
| 226 |
This is consistent with GLSL and other vector APIs. } |
| 227 |
class operator {$ifdef FPC}*{$else}Multiply{$endif} (const V1, V2: TGenericVector3): TGenericVector3; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 228 |
class operator {$ifdef FPC}/{$else}Divide{$endif} (const V: TGenericVector3; const Scalar: TGenericScalar): TGenericVector3; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 229 |
|
| 230 |
procedure Init(const AX, AY, AZ: TGenericScalar); {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 231 |
deprecated 'initialize instead like "V := Vector3(X, Y, Z)"; modifying a temporary record value is a trap, e.g. this is not reliable: "Scene.Translation.Init(X, Y, Z)"'; |
| 232 |
|
| 233 |
function ToString: string; |
| 234 |
|
| 235 |
{ Convert to string using the most precise (not always easily readable |
| 236 |
by humans) float format. This may use the exponential |
| 237 |
(scientific) notation to represent the floating-point value, |
| 238 |
if needed. |
| 239 |
|
| 240 |
You can pass, as parameter, the format to use. By default it is '%g', |
| 241 |
"general number format" with maximum precision documented on |
| 242 |
https://www.freepascal.org/docs-html/rtl/sysutils/format.html . |
| 243 |
|
| 244 |
This is suitable for storing the value in a file, |
| 245 |
with a best precision possible. } |
| 246 |
function ToRawString(const FloatFormat: String = '%g'): string; |
| 247 |
|
| 248 |
function Normalize: TGenericVector3; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 249 |
procedure NormalizeMe; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 250 |
deprecated 'normalize instead like "V := V.Normalize"; modifying a temporary record value is a trap, e.g. this is not reliable: "Scene.Translation.NormalizeMe"'; |
| 251 |
|
| 252 |
function Length: TGenericScalar; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 253 |
|
| 254 |
{ Vector length squared. |
| 255 |
This is slightly faster than @link(Length) as it avoids calculating |
| 256 |
a square root along the way. (But, please remember to not optimize |
| 257 |
your code without a need. Optimize only parts that are proven bottlenecks, |
| 258 |
otherwise don't make the code less readable for the sake of speed.) } |
| 259 |
function LengthSqr: TGenericScalar; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 260 |
|
| 261 |
{ Calculate a new vector scaled so that it has length equal to NewLength. |
| 262 |
NewLength may be negative, in which case we'll negate the vector |
| 263 |
and then adjust it's length to Abs(NewLength). } |
| 264 |
function AdjustToLength(const NewLength: TGenericScalar): TGenericVector3; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 265 |
|
| 266 |
{ Vector cross product. |
| 267 |
See http://en.wikipedia.org/wiki/Cross_product . |
| 268 |
|
| 269 |
Result is a vector orthogonal to both given vectors. |
| 270 |
Generally there are two such vectors, this method returns |
| 271 |
the one following right-hand rule. More precisely, V1, V2 and |
| 272 |
Product(V1, V2) are in the same relation as basic X, Y, Z |
| 273 |
axes. Reverse the order of arguments to get negated result. |
| 274 |
|
| 275 |
If you use this to calculate a normal vector of a triangle |
| 276 |
(P0, P1, P2): note that @code(TVector3.CrossProduct(P1 - P0, P1 - P2)) |
| 277 |
points out from CCW triangle side in right-handed coordinate system. |
| 278 |
|
| 279 |
When V1 and V2 are parallel (that is, when V1 = V2 multiplied by some scalar), |
| 280 |
and this includes the case when one of them is zero, |
| 281 |
then result is a zero vector. } |
| 282 |
class function CrossProduct(const V1, V2: TGenericVector3): TGenericVector3; static; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 283 |
|
| 284 |
{ Dot product of two vectors. |
| 285 |
See https://en.wikipedia.org/wiki/Dot_product . } |
| 286 |
class function DotProduct(const V1, V2: TGenericVector3): TGenericScalar; static; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 287 |
|
| 288 |
{ Absolute value on all components. } |
| 289 |
function Abs: TGenericVector3; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 290 |
|
| 291 |
{ Smallest component. } |
| 292 |
function Min: TGenericScalar; |
| 293 |
|
| 294 |
{ Largest component. } |
| 295 |
function Max: TGenericScalar; |
| 296 |
|
| 297 |
{ Average from all components. } |
| 298 |
function Average: TGenericScalar; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 299 |
|
| 300 |
{ Are all components equal to zero (within some epsilon margin). } |
| 301 |
function IsZero: boolean; overload; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 302 |
|
| 303 |
{ Are all components equal to zero (within Epsilon margin). } |
| 304 |
function IsZero(const Epsilon: TGenericScalar): boolean; overload; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 305 |
|
| 306 |
function IsPerfectlyZero: boolean; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 307 |
|
| 308 |
{ Compare two vectors, with epsilon to tolerate slightly different floats. } |
| 309 |
class function Equals(const V1, V2: TGenericVector3): boolean; overload; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} static; |
| 310 |
class function Equals(const V1, V2: TGenericVector3; const Epsilon: TGenericScalar): boolean; overload; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} static; |
| 311 |
|
| 312 |
{ Compare two vectors using exact comparison (like the "=" operator to compare floats). } |
| 313 |
class function PerfectlyEquals(const V1, V2: TGenericVector3): boolean; static; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 314 |
|
| 315 |
{ Linear interpolation between two vector values. |
| 316 |
Returns @code((1-A) * V1 + A * V2). |
| 317 |
|
| 318 |
So: |
| 319 |
|
| 320 |
@unorderedList( |
| 321 |
@itemSpacing Compact |
| 322 |
@item A = 0 gives V1, |
| 323 |
@item A = 1 gives V2, |
| 324 |
@item values between are interpolated, |
| 325 |
@item values outside are extrapolated. |
| 326 |
) } |
| 327 |
class function Lerp(const A: TGenericScalar; const V1, V2: TGenericVector3): TGenericVector3; static; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 328 |
|
| 329 |
class function Zero: TGenericVector3; static; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 330 |
class property One [const Index: TIndex]: TGenericVector3 read GetOne; |
| 331 |
|
| 332 |
case Integer of |
| 333 |
0: (X, Y, Z: TGenericScalar); |
| 334 |
1: ({ Access (get, set) vector components by index. } Data: array [TIndex] of TGenericScalar); |
| 335 |
2: (XY: TGenericVector2); |
| 336 |
end; |
| 337 |
|
| 338 |
{ Vector of 4 floating-point values. |
| 339 |
@seealso TGenericVector3 } |
| 340 |
TGenericVector4 = packed record |
| 341 |
public |
| 342 |
const |
| 343 |
Count = 4; |
| 344 |
type |
| 345 |
TIndex = 0..Count - 1; |
| 346 |
strict private |
| 347 |
function GetItems(const Index: TIndex): TGenericScalar; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 348 |
procedure SetItems(const Index: TIndex; const Value: TGenericScalar); {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 349 |
class function GetOne(const Index: TIndex): TGenericVector4; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} static; |
| 350 |
public |
| 351 |
{ Get vector components by index. |
| 352 |
This is a default property, so you can write @code(MyVector[0]) |
| 353 |
instead of @code(MyVector.Data[0]) or @code(MyVector.AsArray[0]). |
| 354 |
|
| 355 |
But note that this is not writeable (because exposing writeable properties on vectors |
| 356 |
would cause some subtle traps, see https://castle-engine.io/coding_traps ). |
| 357 |
Use @code(MyVector.Data[0] := 123.456) if you want to set by index, |
| 358 |
or @code(MyVector.X := 123.456). } |
| 359 |
property AsArray [const Index: TIndex]: TGenericScalar read GetItems; default; |
| 360 |
|
| 361 |
{ @exclude |
| 362 |
Access (get, set) vector components by index. } |
| 363 |
property InternalData [const Index: TIndex]: TGenericScalar read GetItems write SetItems; |
| 364 |
{$ifdef FPC}deprecated 'use Data or X, Y, Z, W';{$endif} |
| 365 |
{ @exclude |
| 366 |
Access (get, set) vector components by index. } |
| 367 |
property Items [const Index: TIndex]: TGenericScalar read GetItems write SetItems; |
| 368 |
{$ifdef FPC}deprecated 'use Data or X, Y, Z, W';{$endif} |
| 369 |
|
| 370 |
class operator {$ifdef FPC}+{$else}Add{$endif} (const A, B: TGenericVector4): TGenericVector4; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 371 |
class operator {$ifdef FPC}-{$else}Subtract{$endif} (const A, B: TGenericVector4): TGenericVector4; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 372 |
class operator {$ifdef FPC}-{$else}Negative{$endif} (const V: TGenericVector4): TGenericVector4; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 373 |
class operator {$ifdef FPC}*{$else}Multiply{$endif} (const V: TGenericVector4; const Scalar: TGenericScalar): TGenericVector4; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 374 |
class operator {$ifdef FPC}*{$else}Multiply{$endif} (const Scalar: TGenericScalar; const V: TGenericVector4): TGenericVector4; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 375 |
{ Vector * vector makes a component-wise multiplication. |
| 376 |
This is consistent with GLSL and other vector APIs. } |
| 377 |
class operator {$ifdef FPC}*{$else}Multiply{$endif} (const V1, V2: TGenericVector4): TGenericVector4; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 378 |
class operator {$ifdef FPC}/{$else}Divide{$endif} (const V: TGenericVector4; const Scalar: TGenericScalar): TGenericVector4; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 379 |
|
| 380 |
procedure Init(const AX, AY, AZ, AW: TGenericScalar); {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 381 |
deprecated 'initialize instead like "V := Vector4(X, Y, Z, W)"; modifying a temporary record value is a trap, e.g. this is not reliable: "Scene.Rotation.Init(X, Y, Z, W)"'; |
| 382 |
|
| 383 |
function ToString: string; |
| 384 |
|
| 385 |
{ Convert to string using the most precise (not always easily readable |
| 386 |
by humans) float format. This may use the exponential |
| 387 |
(scientific) notation to represent the floating-point value, |
| 388 |
if needed. |
| 389 |
|
| 390 |
You can pass, as parameter, the format to use. By default it is '%g', |
| 391 |
"general number format" with maximum precision documented on |
| 392 |
https://www.freepascal.org/docs-html/rtl/sysutils/format.html . |
| 393 |
|
| 394 |
This is suitable for storing the value in a file, |
| 395 |
with a best precision possible. } |
| 396 |
function ToRawString(const FloatFormat: String = '%g'): string; |
| 397 |
|
| 398 |
function Length: TGenericScalar; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 399 |
|
| 400 |
{ Vector length squared. |
| 401 |
This is slightly faster than @link(Length) as it avoids calculating |
| 402 |
a square root along the way. (But, please remember to not optimize |
| 403 |
your code without a need. Optimize only parts that are proven bottlenecks, |
| 404 |
otherwise don't make the code less readable for the sake of speed.) } |
| 405 |
function LengthSqr: TGenericScalar; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 406 |
|
| 407 |
{ Calculate a new vector scaled so that it has length equal to NewLength. |
| 408 |
NewLength may be negative, in which case we'll negate the vector |
| 409 |
and then adjust it's length to Abs(NewLength). } |
| 410 |
function AdjustToLength(const NewLength: TGenericScalar): TGenericVector4; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 411 |
|
| 412 |
{ Dot product of two vectors. |
| 413 |
See https://en.wikipedia.org/wiki/Dot_product . } |
| 414 |
class function DotProduct(const V1, V2: TGenericVector4): TGenericScalar; static; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 415 |
|
| 416 |
{ Absolute value on all components. } |
| 417 |
function Abs: TGenericVector4; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 418 |
|
| 419 |
{ Are all components equal to zero (within some epsilon margin). } |
| 420 |
function IsZero: boolean; overload; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 421 |
|
| 422 |
{ Are all components equal to zero (within Epsilon margin). } |
| 423 |
function IsZero(const Epsilon: TGenericScalar): boolean; overload; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 424 |
|
| 425 |
function IsPerfectlyZero: boolean; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 426 |
|
| 427 |
{ Compare two vectors, with epsilon to tolerate slightly different floats. } |
| 428 |
class function Equals(const V1, V2: TGenericVector4): boolean; overload; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} static; |
| 429 |
class function Equals(const V1, V2: TGenericVector4; const Epsilon: TGenericScalar): boolean; overload; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} static; |
| 430 |
|
| 431 |
{ Compare two vectors using exact comparison (like the "=" operator to compare floats). } |
| 432 |
class function PerfectlyEquals(const V1, V2: TGenericVector4): boolean; static; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 433 |
|
| 434 |
{ Convert a 4D homogeneous coordinate to 3D position. } |
| 435 |
function ToPosition: TGenericVector3; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 436 |
|
| 437 |
{ Linear interpolation between two vector values. |
| 438 |
Works analogous to @link(TGenericVector3.Lerp TVector3.Lerp) } |
| 439 |
class function Lerp(const A: TGenericScalar; const V1, V2: TGenericVector4): TGenericVector4; static; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 440 |
|
| 441 |
class function Zero: TGenericVector4; static; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 442 |
class property One [const Index: TIndex]: TGenericVector4 read GetOne; |
| 443 |
|
| 444 |
case Integer of |
| 445 |
0: (X, Y, Z, W: TGenericScalar); |
| 446 |
1: ({ Access (get, set) vector components by index. } Data: array [TIndex] of TGenericScalar); |
| 447 |
2: ({ Get first 2 components as a 2D vector. |
| 448 |
This simply rejects the remaining vector components. } |
| 449 |
XY: TGenericVector2; |
| 450 |
ZW: TGenericVector2); |
| 451 |
3: ({ Get first 3 components as a 3D vector. |
| 452 |
This simply rejects the 4th component. } |
| 453 |
XYZ: TGenericVector3); |
| 454 |
end; |
| 455 |
|
| 456 |
{ 2x2 matrix of floating-point values. |
| 457 |
@seealso TGenericMatrix3 } |
| 458 |
TGenericMatrix2 = record |
| 459 |
public |
| 460 |
type |
| 461 |
TIndex = 0..1; |
| 462 |
strict private |
| 463 |
function GetItems(const AColumn, ARow: TIndex): TGenericScalar; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 464 |
procedure SetItems(const AColumn, ARow: TIndex; const Value: TGenericScalar); {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 465 |
function GetRows(const ARow: TIndex): TGenericVector2; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 466 |
procedure SetRows(const ARow: TIndex; const Value: TGenericVector2); {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 467 |
function GetColumns(const AColumn: TIndex): TGenericVector2; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 468 |
procedure SetColumns(const AColumn: TIndex; const Value: TGenericVector2); {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 469 |
public |
| 470 |
var |
| 471 |
Data: array [TIndex, TIndex] of TGenericScalar; |
| 472 |
|
| 473 |
class operator {$ifdef FPC}+{$else}Add{$endif} (const A, B: TGenericMatrix2): TGenericMatrix2; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 474 |
class operator {$ifdef FPC}-{$else}Subtract{$endif} (const A, B: TGenericMatrix2): TGenericMatrix2; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 475 |
class operator {$ifdef FPC}-{$else}Negative{$endif} (const M: TGenericMatrix2): TGenericMatrix2; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 476 |
class operator {$ifdef FPC}*{$else}Multiply{$endif} (const V: TGenericMatrix2; const Scalar: TGenericScalar): TGenericMatrix2; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 477 |
class operator {$ifdef FPC}*{$else}Multiply{$endif} (const Scalar: TGenericScalar; const V: TGenericMatrix2): TGenericMatrix2; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 478 |
class operator {$ifdef FPC}*{$else}Multiply{$endif} (const M: TGenericMatrix2; const V: TGenericVector2): TGenericVector2; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 479 |
{ Matrix * matrix makes a normal matrix algebraic multiplication |
| 480 |
(not component-wise multiplication). |
| 481 |
Note that this is different from vectors, where vector * vector |
| 482 |
makes a component-wise multiplication. } |
| 483 |
class operator {$ifdef FPC}*{$else}Multiply{$endif} (const M1, M2: TGenericMatrix2): TGenericMatrix2; |
| 484 |
|
| 485 |
function ToString(const LineIndent: string = ''): string; |
| 486 |
|
| 487 |
{ Convert to string using the most precise (not always easily readable |
| 488 |
by humans) float format. This may use the exponential |
| 489 |
(scientific) notation to represent the floating-point value, |
| 490 |
if needed. |
| 491 |
|
| 492 |
You can pass, as parameter, the format to use. By default it is '%g', |
| 493 |
"general number format" with maximum precision documented on |
| 494 |
https://www.freepascal.org/docs-html/rtl/sysutils/format.html . |
| 495 |
|
| 496 |
This is suitable for storing the value in a file, |
| 497 |
with a best precision possible. } |
| 498 |
function ToRawString(const LineIndent: String = ''; const FloatFormat: String = '%g'): String; |
| 499 |
|
| 500 |
property Items [const AColumn, ARow: TIndex]: TGenericScalar read GetItems write SetItems; default; |
| 501 |
property Rows [const ARow: TIndex]: TGenericVector2 read GetRows write SetRows; |
| 502 |
property Columns [const AColumn: TIndex]: TGenericVector2 read GetColumns write SetColumns; |
| 503 |
function Determinant: TGenericScalar; |
| 504 |
|
| 505 |
{ Inverse the matrix. |
| 506 |
|
| 507 |
This does division by ADeterminant internally, so will raise exception |
| 508 |
from this float division if the matrix is not reversible. |
| 509 |
Check Math.IsZero(ADeterminant) first to avoid this, or use @link(TryInverse). } |
| 510 |
function Inverse(ADeterminant: TGenericScalar): TGenericMatrix2; |
| 511 |
|
| 512 |
{ Inverse the matrix, or return @false if the matrix is not invertible. } |
| 513 |
function TryInverse(out MInverse: TGenericMatrix2): boolean; |
| 514 |
|
| 515 |
function Transpose: TGenericMatrix2; |
| 516 |
|
| 517 |
{ Compare two vectors, with epsilon to tolerate slightly different floats. } |
| 518 |
class function Equals(const M1, M2: TGenericMatrix2): boolean; overload; static; |
| 519 |
class function Equals(const M1, M2: TGenericMatrix2; const Epsilon: TGenericScalar): boolean; overload; static; |
| 520 |
|
| 521 |
{ Compare two vectors using exact comparison (like the "=" operator to compare floats). } |
| 522 |
class function PerfectlyEquals(const M1, M2: TGenericMatrix2): boolean; static; |
| 523 |
|
| 524 |
{ Linear interpolation between two matrix values. |
| 525 |
@seealso TGenericVector3.Lerp } |
| 526 |
class function Lerp(const A: TGenericScalar; const M1, M2: TGenericMatrix2): TGenericMatrix2; static; |
| 527 |
|
| 528 |
class function Zero: TGenericMatrix2; static; |
| 529 |
class function Identity: TGenericMatrix2; static; |
| 530 |
end; |
| 531 |
|
| 532 |
{ 3x3 matrix of floating-point values. |
| 533 |
Column-major, just like OpenGL, which means that the first index |
| 534 |
of @link(Data) array should be treated as a column number, |
| 535 |
the 2nd index is the row number. |
| 536 |
|
| 537 |
This is generic type (although not using "proper" Pascal generics |
| 538 |
for implementation reasons). In has two actual uses: |
| 539 |
|
| 540 |
@orderedList( |
| 541 |
@itemSpacing Compact |
| 542 |
@item @link(TMatrix3), a matrix of 3 Single values (floats with single precision), |
| 543 |
@item @link(TMatrix3Double), a matrix of 3 Double values (floats with double precision). |
| 544 |
) |
| 545 |
|
| 546 |
The type TGenericScalar is, accordingly, |
| 547 |
Single or Double for @link(TMatrix3) or @link(TMatrix3Double). |
| 548 |
} |
| 549 |
TGenericMatrix3 = record |
| 550 |
public |
| 551 |
type |
| 552 |
TIndex = 0..2; |
| 553 |
strict private |
| 554 |
function GetItems(const AColumn, ARow: TIndex): TGenericScalar; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 555 |
procedure SetItems(const AColumn, ARow: TIndex; const Value: TGenericScalar); {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 556 |
function GetRows(const ARow: TIndex): TGenericVector3; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 557 |
procedure SetRows(const ARow: TIndex; const Value: TGenericVector3); {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 558 |
function GetColumns(const AColumn: TIndex): TGenericVector3; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 559 |
procedure SetColumns(const AColumn: TIndex; const Value: TGenericVector3); {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 560 |
public |
| 561 |
type |
| 562 |
TIndexArray = array[TIndex] of TGenericScalar; |
| 563 |
var |
| 564 |
//Data: array [TIndex, TIndex] of TGenericScalar; |
| 565 |
Data: array [TIndex] of TIndexArray; |
| 566 |
|
| 567 |
class operator {$ifdef FPC}+{$else}Add{$endif} (const A, B: TGenericMatrix3): TGenericMatrix3; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 568 |
class operator {$ifdef FPC}-{$else}Subtract{$endif} (const A, B: TGenericMatrix3): TGenericMatrix3; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 569 |
class operator {$ifdef FPC}-{$else}Negative{$endif} (const M: TGenericMatrix3): TGenericMatrix3; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 570 |
class operator {$ifdef FPC}*{$else}Multiply{$endif} (const V: TGenericMatrix3; const Scalar: TGenericScalar): TGenericMatrix3; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 571 |
class operator {$ifdef FPC}*{$else}Multiply{$endif} (const Scalar: TGenericScalar; const V: TGenericMatrix3): TGenericMatrix3; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 572 |
class operator {$ifdef FPC}*{$else}Multiply{$endif} (const M: TGenericMatrix3; const V: TGenericVector3): TGenericVector3; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 573 |
{ Matrix * matrix makes a normal matrix algebraic multiplication |
| 574 |
(not component-wise multiplication). |
| 575 |
Note that this is different from vectors, where vector * vector |
| 576 |
makes a component-wise multiplication. } |
| 577 |
class operator {$ifdef FPC}*{$else}Multiply{$endif} (const M1, M2: TGenericMatrix3): TGenericMatrix3; |
| 578 |
|
| 579 |
function ToString(const LineIndent: string = ''): string; |
| 580 |
|
| 581 |
{ Convert to string using the most precise (not always easily readable |
| 582 |
by humans) float format. This may use the exponential |
| 583 |
(scientific) notation to represent the floating-point value, |
| 584 |
if needed. |
| 585 |
|
| 586 |
You can pass, as parameter, the format to use. By default it is '%g', |
| 587 |
"general number format" with maximum precision documented on |
| 588 |
https://www.freepascal.org/docs-html/rtl/sysutils/format.html . |
| 589 |
|
| 590 |
This is suitable for storing the value in a file, |
| 591 |
with a best precision possible. } |
| 592 |
function ToRawString(const LineIndent: String = ''; const FloatFormat: String = '%g'): String; |
| 593 |
|
| 594 |
property Items [const AColumn, ARow: TIndex]: TGenericScalar read GetItems write SetItems; default; |
| 595 |
property Rows [const ARow: TIndex]: TGenericVector3 read GetRows write SetRows; |
| 596 |
property Columns [const AColumn: TIndex]: TGenericVector3 read GetColumns write SetColumns; |
| 597 |
function Determinant: TGenericScalar; |
| 598 |
|
| 599 |
{ Inverse the matrix. |
| 600 |
|
| 601 |
This does division by ADeterminant internally, so will raise exception |
| 602 |
from this float division if the matrix is not reversible. |
| 603 |
Check Math.IsZero(ADeterminant) first to avoid this, or use @link(TryInverse). } |
| 604 |
function Inverse(ADeterminant: TGenericScalar): TGenericMatrix3; |
| 605 |
|
| 606 |
{ Inverse the matrix, or return @false if the matrix is not invertible. } |
| 607 |
function TryInverse(out MInverse: TGenericMatrix3): boolean; |
| 608 |
|
| 609 |
function Transpose: TGenericMatrix3; |
| 610 |
|
| 611 |
{ Compare two vectors, with epsilon to tolerate slightly different floats. } |
| 612 |
class function Equals(const M1, M2: TGenericMatrix3): boolean; overload; static; |
| 613 |
class function Equals(const M1, M2: TGenericMatrix3; const Epsilon: TGenericScalar): boolean; overload; static; |
| 614 |
|
| 615 |
{ Compare two vectors using exact comparison (like the "=" operator to compare floats). } |
| 616 |
class function PerfectlyEquals(const M1, M2: TGenericMatrix3): boolean; static; |
| 617 |
|
| 618 |
{ Linear interpolation between two matrix values. |
| 619 |
@seealso TGenericVector3.Lerp } |
| 620 |
class function Lerp(const A: TGenericScalar; const M1, M2: TGenericMatrix3): TGenericMatrix3; static; |
| 621 |
|
| 622 |
class function Zero: TGenericMatrix3; static; |
| 623 |
class function Identity: TGenericMatrix3; static; |
| 624 |
end; |
| 625 |
|
| 626 |
{ 4x4 matrix of floating-point values. |
| 627 |
@seealso TGenericMatrix3 } |
| 628 |
TGenericMatrix4 = record |
| 629 |
public |
| 630 |
type |
| 631 |
TIndex = 0..3; |
| 632 |
strict private |
| 633 |
function GetItems(const AColumn, ARow: TIndex): TGenericScalar; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 634 |
procedure SetItems(const AColumn, ARow: TIndex; const Value: TGenericScalar); {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 635 |
function GetRows(const ARow: TIndex): TGenericVector4; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 636 |
procedure SetRows(const ARow: TIndex; const Value: TGenericVector4); {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 637 |
function GetColumns(const AColumn: TIndex): TGenericVector4; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 638 |
procedure SetColumns(const AColumn: TIndex; const Value: TGenericVector4); {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 639 |
procedure RaisePositionTransformResultInvalid; |
| 640 |
{ Raise ETransformedResultInvalid. |
| 641 |
Since TGenericMatrix4.MultDirection is time-critical (it is used in TFrustum.Transform |
| 642 |
which can be a bottleneck, testcase: The Unholy Society), |
| 643 |
keep it fast. } |
| 644 |
procedure RaiseDirectionTransformedResultInvalid(const Divisor: TGenericScalar); |
| 645 |
public |
| 646 |
type |
| 647 |
TIndexArray = array[TIndex] of TGenericScalar; |
| 648 |
var |
| 649 |
//Data: array [TIndex, TIndex] of TGenericScalar; |
| 650 |
Data: array [TIndex] of TIndexArray; |
| 651 |
|
| 652 |
class operator {$ifdef FPC}+{$else}Add{$endif} (const A, B: TGenericMatrix4): TGenericMatrix4; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 653 |
class operator {$ifdef FPC}-{$else}Subtract{$endif} (const A, B: TGenericMatrix4): TGenericMatrix4; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 654 |
class operator {$ifdef FPC}-{$else}Negative{$endif} (const M: TGenericMatrix4): TGenericMatrix4; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 655 |
class operator {$ifdef FPC}*{$else}Multiply{$endif} (const V: TGenericMatrix4; const Scalar: TGenericScalar): TGenericMatrix4; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 656 |
class operator {$ifdef FPC}*{$else}Multiply{$endif} (const Scalar: TGenericScalar; const V: TGenericMatrix4): TGenericMatrix4; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 657 |
class operator {$ifdef FPC}*{$else}Multiply{$endif} (const M: TGenericMatrix4; const V: TGenericVector4): TGenericVector4; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 658 |
{ Matrix * matrix makes a normal matrix algebraic multiplication |
| 659 |
(not component-wise multiplication). |
| 660 |
Note that this is different from vectors, where vector * vector |
| 661 |
makes a component-wise multiplication. } |
| 662 |
class operator {$ifdef FPC}*{$else}Multiply{$endif} (const M1, M2: TGenericMatrix4): TGenericMatrix4; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 663 |
|
| 664 |
{ Calculates "M.Transpose * V" in a faster way |
| 665 |
(without spending *any* time on calculating transposition). } |
| 666 |
function TransposeMultiply(const V: TGenericVector4): TGenericVector4; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif} |
| 667 |
|
| 668 |
function ToString(const LineIndent: string = ''): string; |
| 669 |
|
| 670 |
{ Convert to string using the most precise (not always easily readable |
| 671 |
by humans) float format. This may use the exponential |
| 672 |
(scientific) notation to represent the floating-point value, |
| 673 |
if needed. |
| 674 |
|
| 675 |
You can pass, as parameter, the format to use. By default it is '%g', |
| 676 |
"general number format" with maximum precision documented on |
| 677 |
https://www.freepascal.org/docs-html/rtl/sysutils/format.html . |
| 678 |
|
| 679 |
This is suitable for storing the value in a file, |
| 680 |
with a best precision possible. } |
| 681 |
function ToRawString(const LineIndent: String = ''; const FloatFormat: String = '%g'): String; |
| 682 |
|
| 683 |
property Items [const AColumn, ARow: TIndex]: TGenericScalar read GetItems write SetItems; default; |
| 684 |
property Rows [const ARow: TIndex]: TGenericVector4 read GetRows write SetRows; |
| 685 |
property Columns [const AColumn: TIndex]: TGenericVector4 read GetColumns write SetColumns; |
| 686 |
function Determinant: TGenericScalar; |
| 687 |
|
| 688 |
{ Inverse the matrix. |
| 689 |
|
| 690 |
This does division by ADeterminant internally, so will raise exception |
| 691 |
from this float division if the matrix is not reversible. |
| 692 |
Check Math.IsZero(ADeterminant) first to avoid this, or use @link(TryInverse). } |
| 693 |
function Inverse(ADeterminant: TGenericScalar): TGenericMatrix4; |
| 694 |
|
| 695 |
{ Inverse the matrix, or return @false if the matrix is not invertible. } |
| 696 |
function TryInverse(out MInverse: TGenericMatrix4): boolean; |
| 697 |
|
| 698 |
function Transpose: TGenericMatrix4; |
| 699 |
|
| 700 |
{ Transform a 3D or 2D point with 4x4 matrix. |
| 701 |
|
| 702 |
This works by temporarily converting point to 4-component vector |
| 703 |
(4th component is 1). After multiplying matrix * vector we divide |
| 704 |
by 4th component. So this works Ok for all matrices, |
| 705 |
even with last row different than identity (0, 0, 0, 1). |
| 706 |
E.g. this works for projection matrices too. |
| 707 |
|
| 708 |
@raises(ETransformedResultInvalid This is raised when matrix |
| 709 |
will transform point to a direction (vector with 4th component |
| 710 |
equal zero). In this case we just cannot interpret the result as a point.) |
| 711 |
|
| 712 |
@groupBegin } |
| 713 |
function MultPoint(const Pt: TGenericVector3): TGenericVector3; overload; |
| 714 |
function MultPoint(const Pt: TGenericVector2): TGenericVector2; overload; |
| 715 |
{ @groupEnd } |
| 716 |
|
| 717 |
{ Transform a 3D or 2D direction with 4x4 matrix. |
| 718 |
|
| 719 |
This works by temporarily converting direction to 4-component vector |
| 720 |
(4th component is 0). After multiplying matrix * vector we check |
| 721 |
is the 4th component still 0 (eventually raising ETransformedResultInvalid). |
| 722 |
|
| 723 |
@raises(ETransformedResultInvalid This is raised when matrix |
| 724 |
will transform direction to a point (vector with 4th component |
| 725 |
nonzero). In this case we just cannot interpret the result as a direction.) |
| 726 |
|
| 727 |
@groupBegin } |
| 728 |
function MultDirection(const Dir: TGenericVector3): TGenericVector3; overload; |
| 729 |
function MultDirection(const Dir: TGenericVector2): TGenericVector2; overload; |
| 730 |
{ @groupEnd } |
| 731 |
|
| 732 |
{ Compare two vectors, with epsilon to tolerate slightly different floats. } |
| 733 |
class function Equals(const M1, M2: TGenericMatrix4): boolean; overload; static; |
| 734 |
class function Equals(const M1, M2: TGenericMatrix4; const Epsilon: TGenericScalar): boolean; overload; static; |
| 735 |
|
| 736 |
{ Compare two vectors using exact comparison (like the "=" operator to compare floats). } |
| 737 |
class function PerfectlyEquals(const M1, M2: TGenericMatrix4): boolean; static; |
| 738 |
|
| 739 |
{ Linear interpolation between two matrix values. |
| 740 |
@seealso TGenericVector3.Lerp } |
| 741 |
class function Lerp(const A: TGenericScalar; const M1, M2: TGenericMatrix4): TGenericMatrix4; static; |
| 742 |
|
| 743 |
class function Zero: TGenericMatrix4; static; |
| 744 |
class function Identity: TGenericMatrix4; static; |
| 745 |
end; |
| 746 |
|
| 747 |
implementation |
| 748 |
|
| 749 |
uses Math, |
| 750 |
CastleUtils; |
| 751 |
|
| 752 |
function FormatScalar(const Value: TGenericScalar; const Fmt: String): String; |
| 753 |
begin |
| 754 |
Result := FormatDot(Fmt, [Value]); |
| 755 |
end; |
| 756 |
|
| 757 |
{ TGenericVector2 ------------------------------------------------------------ } |
| 758 |
|
| 759 |
class operator TGenericVector2. {$ifdef FPC}+{$else}Add{$endif} (const A, B: TGenericVector2): TGenericVector2; |
| 760 |
begin |
| 761 |
Result.X := A.X + B.X; |
| 762 |
Result.Y := A.Y + B.Y; |
| 763 |
end; |
| 764 |
|
| 765 |
class operator TGenericVector2. {$ifdef FPC}-{$else}Subtract{$endif} (const A, B: TGenericVector2): TGenericVector2; |
| 766 |
begin |
| 767 |
Result.X := A.X - B.X; |
| 768 |
Result.Y := A.Y - B.Y; |
| 769 |
end; |
| 770 |
|
| 771 |
class operator TGenericVector2. {$ifdef FPC}-{$else}Negative{$endif} (const V: TGenericVector2): TGenericVector2; |
| 772 |
begin |
| 773 |
Result.X := - V.X; |
| 774 |
Result.Y := - V.Y; |
| 775 |
end; |
| 776 |
|
| 777 |
class operator TGenericVector2. {$ifdef FPC}*{$else}Multiply{$endif} (const V: TGenericVector2; const Scalar: TGenericScalar): TGenericVector2; |
| 778 |
begin |
| 779 |
Result.X := V.X * Scalar; |
| 780 |
Result.Y := V.Y * Scalar; |
| 781 |
end; |
| 782 |
|
| 783 |
class operator TGenericVector2. {$ifdef FPC}*{$else}Multiply{$endif} (const Scalar: TGenericScalar; const V: TGenericVector2): TGenericVector2; |
| 784 |
begin |
| 785 |
Result.X := V.X * Scalar; |
| 786 |
Result.Y := V.Y * Scalar; |
| 787 |
end; |
| 788 |
|
| 789 |
class operator TGenericVector2. {$ifdef FPC}*{$else}Multiply{$endif} (const V1, V2: TGenericVector2): TGenericVector2; |
| 790 |
begin |
| 791 |
Result.X := V1.X * V2.X; |
| 792 |
Result.Y := V1.Y * V2.Y; |
| 793 |
end; |
| 794 |
|
| 795 |
class operator TGenericVector2. {$ifdef FPC}/{$else}Divide{$endif} (const V: TGenericVector2; const Scalar: TGenericScalar): TGenericVector2; |
| 796 |
var |
| 797 |
ScalarInv: TGenericScalar; |
| 798 |
begin |
| 799 |
ScalarInv := 1 / Scalar; // multiplication is faster than division, so invert once |
| 800 |
Result.X := V.X * ScalarInv; |
| 801 |
Result.Y := V.Y * ScalarInv; |
| 802 |
end; |
| 803 |
|
| 804 |
procedure TGenericVector2.Init(const AX, AY: TGenericScalar); |
| 805 |
begin |
| 806 |
X := AX; |
| 807 |
Y := AY; |
| 808 |
end; |
| 809 |
|
| 810 |
function TGenericVector2.ToString: string; |
| 811 |
begin |
| 812 |
Result := |
| 813 |
FloatToStrDisplay(X) + ' ' + |
| 814 |
FloatToStrDisplay(Y); |
| 815 |
end; |
| 816 |
|
| 817 |
function TGenericVector2.ToRawString(const FloatFormat: String): string; |
| 818 |
begin |
| 819 |
Result := |
| 820 |
FormatScalar(X, FloatFormat) + ' ' + |
| 821 |
FormatScalar(Y, FloatFormat); |
| 822 |
end; |
| 823 |
|
| 824 |
function TGenericVector2.GetItems(const Index: TIndex): TGenericScalar; |
| 825 |
begin |
| 826 |
case Index of |
| 827 |
0: Result := X; |
| 828 |
1: Result := Y; |
| 829 |
{ We use here numbers, not strings, for internal errors. |
| 830 |
Reason: avoid implicit try..finally clause, |
| 831 |
this method must be fast. } |
| 832 |
{$ifndef FPC} else raise EInternalError.Create(202202111); {$endif} |
| 833 |
end; |
| 834 |
end; |
| 835 |
|
| 836 |
procedure TGenericVector2.SetItems(const Index: TIndex; const Value: TGenericScalar); |
| 837 |
begin |
| 838 |
case Index of |
| 839 |
0: X := Value; |
| 840 |
1: Y := Value; |
| 841 |
{$ifndef FPC} else raise EInternalError.Create(202202112); {$endif} |
| 842 |
end; |
| 843 |
end; |
| 844 |
|
| 845 |
function TGenericVector2.Normalize: TGenericVector2; |
| 846 |
var |
| 847 |
L: TGenericScalar; |
| 848 |
begin |
| 849 |
L := Length; |
| 850 |
if L = 0 then |
| 851 |
Result := Self |
| 852 |
else |
| 853 |
begin |
| 854 |
L := 1 / L; |
| 855 |
Result.X := X * L; |
| 856 |
Result.Y := Y * L; |
| 857 |
end; |
| 858 |
end; |
| 859 |
|
| 860 |
procedure TGenericVector2.NormalizeMe; |
| 861 |
var |
| 862 |
L: TGenericScalar; |
| 863 |
begin |
| 864 |
L := Length; |
| 865 |
if L <> 0 then |
| 866 |
begin |
| 867 |
L := 1 / L; |
| 868 |
X := X * L; |
| 869 |
Y := Y * L; |
| 870 |
end; |
| 871 |
end; |
| 872 |
|
| 873 |
function TGenericVector2.Length: TGenericScalar; |
| 874 |
begin |
| 875 |
Result := Sqrt( |
| 876 |
(X * X) + |
| 877 |
(Y * Y) |
| 878 |
); |
| 879 |
end; |
| 880 |
|
| 881 |
function TGenericVector2.LengthSqr: TGenericScalar; |
| 882 |
begin |
| 883 |
Result := |
| 884 |
(X * X) + |
| 885 |
(Y * Y); |
| 886 |
end; |
| 887 |
|
| 888 |
function TGenericVector2.AdjustToLength(const NewLength: TGenericScalar): TGenericVector2; |
| 889 |
begin |
| 890 |
Result := Self * (NewLength / Length); |
| 891 |
end; |
| 892 |
|
| 893 |
class function TGenericVector2.DotProduct(const V1, V2: TGenericVector2): TGenericScalar; |
| 894 |
begin |
| 895 |
Result := |
| 896 |
V1.X * V2.X + |
| 897 |
V1.Y * V2.Y; |
| 898 |
end; |
| 899 |
|
| 900 |
function TGenericVector2.Abs: TGenericVector2; |
| 901 |
begin |
| 902 |
Result.X := System.Abs(X); |
| 903 |
Result.Y := System.Abs(Y); |
| 904 |
end; |
| 905 |
|
| 906 |
function TGenericVector2.IsZero: boolean; |
| 907 |
begin |
| 908 |
Result := |
| 909 |
Math.IsZero(X) and |
| 910 |
Math.IsZero(Y); |
| 911 |
end; |
| 912 |
|
| 913 |
function TGenericVector2.IsZero(const Epsilon: TGenericScalar): boolean; |
| 914 |
begin |
| 915 |
Result := (System.Abs(X) < Epsilon) and |
| 916 |
(System.Abs(Y) < Epsilon); |
| 917 |
end; |
| 918 |
|
| 919 |
function TGenericVector2.IsPerfectlyZero: boolean; |
| 920 |
begin |
| 921 |
Result := |
| 922 |
(X = 0) and |
| 923 |
(Y = 0); |
| 924 |
end; |
| 925 |
|
| 926 |
class function TGenericVector2.Equals(const V1, V2: TGenericVector2): boolean; |
| 927 |
begin |
| 928 |
Result := |
| 929 |
SameValue(V1.X, V2.X) and |
| 930 |
SameValue(V1.Y, V2.Y); |
| 931 |
end; |
| 932 |
|
| 933 |
class function TGenericVector2.Equals(const V1, V2: TGenericVector2; const Epsilon: TGenericScalar): boolean; |
| 934 |
begin |
| 935 |
if Epsilon = 0 then |
| 936 |
Result := (V1.X = V2.X) and |
| 937 |
(V1.Y = V2.Y) else |
| 938 |
Result := (System.Abs(V1.X-V2.X) < Epsilon) and |
| 939 |
(System.Abs(V1.Y-V2.Y) < Epsilon); |
| 940 |
end; |
| 941 |
|
| 942 |
class function TGenericVector2.PerfectlyEquals(const V1, V2: TGenericVector2): boolean; |
| 943 |
begin |
| 944 |
Result := (V1.X = V2.X) and |
| 945 |
(V1.Y = V2.Y); |
| 946 |
end; |
| 947 |
|
| 948 |
class function TGenericVector2.Lerp(const A: TGenericScalar; const V1, V2: TGenericVector2): TGenericVector2; |
| 949 |
begin |
| 950 |
Result.X := V1.X + A * (V2.X - V1.X); |
| 951 |
Result.Y := V1.Y + A * (V2.Y - V1.Y); |
| 952 |
end; |
| 953 |
|
| 954 |
class function TGenericVector2.Zero: TGenericVector2; |
| 955 |
begin |
| 956 |
FillChar(Result, SizeOf(Result), 0); |
| 957 |
end; |
| 958 |
|
| 959 |
class function TGenericVector2.GetOne(const Index: TIndex): TGenericVector2; |
| 960 |
begin |
| 961 |
FillChar(Result, SizeOf(Result), 0); |
| 962 |
Result.Data[Index] := 1.0; |
| 963 |
end; |
| 964 |
|
| 965 |
{ TGenericVector3 ------------------------------------------------------------ } |
| 966 |
|
| 967 |
class operator TGenericVector3. {$ifdef FPC}+{$else}Add{$endif} (const A, B: TGenericVector3): TGenericVector3; |
| 968 |
begin |
| 969 |
Result.X := A.X + B.X; |
| 970 |
Result.Y := A.Y + B.Y; |
| 971 |
Result.Z := A.Z + B.Z; |
| 972 |
end; |
| 973 |
|
| 974 |
class operator TGenericVector3. {$ifdef FPC}-{$else}Subtract{$endif} (const A, B: TGenericVector3): TGenericVector3; |
| 975 |
begin |
| 976 |
Result.X := A.X - B.X; |
| 977 |
Result.Y := A.Y - B.Y; |
| 978 |
Result.Z := A.Z - B.Z; |
| 979 |
end; |
| 980 |
|
| 981 |
class operator TGenericVector3. {$ifdef FPC}-{$else}Negative{$endif} (const V: TGenericVector3): TGenericVector3; |
| 982 |
begin |
| 983 |
Result.X := - V.X; |
| 984 |
Result.Y := - V.Y; |
| 985 |
Result.Z := - V.Z; |
| 986 |
end; |
| 987 |
|
| 988 |
class operator TGenericVector3. {$ifdef FPC}*{$else}Multiply{$endif} (const V: TGenericVector3; const Scalar: TGenericScalar): TGenericVector3; |
| 989 |
begin |
| 990 |
Result.X := V.X * Scalar; |
| 991 |
Result.Y := V.Y * Scalar; |
| 992 |
Result.Z := V.Z * Scalar; |
| 993 |
end; |
| 994 |
|
| 995 |
class operator TGenericVector3. {$ifdef FPC}*{$else}Multiply{$endif} (const Scalar: TGenericScalar; const V: TGenericVector3): TGenericVector3; |
| 996 |
begin |
| 997 |
Result.X := V.X * Scalar; |
| 998 |
Result.Y := V.Y * Scalar; |
| 999 |
Result.Z := V.Z * Scalar; |
| 1000 |
end; |
| 1001 |
|
| 1002 |
class operator TGenericVector3. {$ifdef FPC}*{$else}Multiply{$endif} (const V1, V2: TGenericVector3): TGenericVector3; |
| 1003 |
begin |
| 1004 |
Result.X := V1.X * V2.X; |
| 1005 |
Result.Y := V1.Y * V2.Y; |
| 1006 |
Result.Z := V1.Z * V2.Z; |
| 1007 |
end; |
| 1008 |
|
| 1009 |
class operator TGenericVector3. {$ifdef FPC}/{$else}Divide{$endif} (const V: TGenericVector3; const Scalar: TGenericScalar): TGenericVector3; |
| 1010 |
var |
| 1011 |
ScalarInv: TGenericScalar; |
| 1012 |
begin |
| 1013 |
ScalarInv := 1 / Scalar; // multiplication is faster than division, so invert once |
| 1014 |
Result.X := V.X * ScalarInv; |
| 1015 |
Result.Y := V.Y * ScalarInv; |
| 1016 |
Result.Z := V.Z * ScalarInv; |
| 1017 |
end; |
| 1018 |
|
| 1019 |
procedure TGenericVector3.Init(const AX, AY, AZ: TGenericScalar); |
| 1020 |
begin |
| 1021 |
X := AX; |
| 1022 |
Y := AY; |
| 1023 |
Z := AZ; |
| 1024 |
end; |
| 1025 |
|
| 1026 |
function TGenericVector3.ToString: string; |
| 1027 |
begin |
| 1028 |
Result := |
| 1029 |
FloatToStrDisplay(X) + ' ' + |
| 1030 |
FloatToStrDisplay(Y) + ' ' + |
| 1031 |
FloatToStrDisplay(Z); |
| 1032 |
end; |
| 1033 |
|
| 1034 |
function TGenericVector3.ToRawString(const FloatFormat: String): string; |
| 1035 |
begin |
| 1036 |
Result := |
| 1037 |
FormatScalar(X, FloatFormat) + ' ' + |
| 1038 |
FormatScalar(Y, FloatFormat) + ' ' + |
| 1039 |
FormatScalar(Z, FloatFormat); |
| 1040 |
end; |
| 1041 |
|
| 1042 |
function TGenericVector3.GetItems(const Index: TIndex): TGenericScalar; |
| 1043 |
begin |
| 1044 |
case Index of |
| 1045 |
0: Result := X; |
| 1046 |
1: Result := Y; |
| 1047 |
2: Result := Z; |
| 1048 |
{$ifndef FPC} else raise EInternalError.Create(202202113); {$endif} |
| 1049 |
end; |
| 1050 |
end; |
| 1051 |
|
| 1052 |
procedure TGenericVector3.SetItems(const Index: TIndex; const Value: TGenericScalar); |
| 1053 |
begin |
| 1054 |
case Index of |
| 1055 |
0: X := Value; |
| 1056 |
1: Y := Value; |
| 1057 |
2: Z := Value; |
| 1058 |
{$ifndef FPC} else raise EInternalError.Create(202202114); {$endif} |
| 1059 |
end; |
| 1060 |
end; |
| 1061 |
|
| 1062 |
function TGenericVector3.Normalize: TGenericVector3; |
| 1063 |
var |
| 1064 |
L: TGenericScalar; |
| 1065 |
begin |
| 1066 |
L := Length; |
| 1067 |
if L = 0 then |
| 1068 |
Result := Self |
| 1069 |
else |
| 1070 |
begin |
| 1071 |
L := 1 / L; |
| 1072 |
Result.X := X * L; |
| 1073 |
Result.Y := Y * L; |
| 1074 |
Result.Z := Z * L; |
| 1075 |
end; |
| 1076 |
end; |
| 1077 |
|
| 1078 |
procedure TGenericVector3.NormalizeMe; |
| 1079 |
var |
| 1080 |
L: TGenericScalar; |
| 1081 |
begin |
| 1082 |
L := Length; |
| 1083 |
if L <> 0 then |
| 1084 |
begin |
| 1085 |
L := 1 / L; |
| 1086 |
X := X * L; |
| 1087 |
Y := Y * L; |
| 1088 |
Z := Z * L; |
| 1089 |
end; |
| 1090 |
end; |
| 1091 |
|
| 1092 |
function TGenericVector3.Length: TGenericScalar; |
| 1093 |
begin |
| 1094 |
Result := Sqrt( |
| 1095 |
(X * X) + |
| 1096 |
(Y * Y) + |
| 1097 |
(Z * Z) |
| 1098 |
); |
| 1099 |
end; |
| 1100 |
|
| 1101 |
function TGenericVector3.LengthSqr: TGenericScalar; |
| 1102 |
begin |
| 1103 |
Result := |
| 1104 |
(X * X) + |
| 1105 |
(Y * Y) + |
| 1106 |
(Z * Z); |
| 1107 |
end; |
| 1108 |
|
| 1109 |
function TGenericVector3.AdjustToLength(const NewLength: TGenericScalar): TGenericVector3; |
| 1110 |
begin |
| 1111 |
Result := Self * (NewLength / Length); |
| 1112 |
end; |
| 1113 |
|
| 1114 |
class function TGenericVector3.CrossProduct(const V1, V2: TGenericVector3): TGenericVector3; |
| 1115 |
begin |
| 1116 |
Result.X := V1.Y * V2.Z - V1.Z * V2.Y; |
| 1117 |
Result.Y := V1.Z * V2.X - V1.X * V2.Z; |
| 1118 |
Result.Z := V1.X * V2.Y - V1.Y * V2.X; |
| 1119 |
end; |
| 1120 |
|
| 1121 |
class function TGenericVector3.DotProduct(const V1, V2: TGenericVector3): TGenericScalar; |
| 1122 |
begin |
| 1123 |
Result := |
| 1124 |
V1.X * V2.X + |
| 1125 |
V1.Y * V2.Y + |
| 1126 |
V1.Z * V2.Z; |
| 1127 |
end; |
| 1128 |
|
| 1129 |
function TGenericVector3.Abs: TGenericVector3; |
| 1130 |
begin |
| 1131 |
Result.X := System.Abs(X); |
| 1132 |
Result.Y := System.Abs(Y); |
| 1133 |
Result.Z := System.Abs(Z); |
| 1134 |
end; |
| 1135 |
|
| 1136 |
function TGenericVector3.Min: TGenericScalar; |
| 1137 |
begin |
| 1138 |
Result := MinValue([X, Y, Z]); |
| 1139 |
end; |
| 1140 |
|
| 1141 |
function TGenericVector3.Max: TGenericScalar; |
| 1142 |
begin |
| 1143 |
Result := MaxValue([X, Y, Z]); |
| 1144 |
end; |
| 1145 |
|
| 1146 |
function TGenericVector3.Average: TGenericScalar; |
| 1147 |
begin |
| 1148 |
Result := (X + Y + Z) / 3; |
| 1149 |
end; |
| 1150 |
|
| 1151 |
function TGenericVector3.IsZero: boolean; |
| 1152 |
begin |
| 1153 |
Result := |
| 1154 |
Math.IsZero(X) and |
| 1155 |
Math.IsZero(Y) and |
| 1156 |
Math.IsZero(Z); |
| 1157 |
end; |
| 1158 |
|
| 1159 |
function TGenericVector3.IsZero(const Epsilon: TGenericScalar): boolean; |
| 1160 |
begin |
| 1161 |
Result := (System.Abs(X) < Epsilon) and |
| 1162 |
(System.Abs(Y) < Epsilon) and |
| 1163 |
(System.Abs(Z) < Epsilon); |
| 1164 |
end; |
| 1165 |
|
| 1166 |
function TGenericVector3.IsPerfectlyZero: boolean; |
| 1167 |
begin |
| 1168 |
Result := |
| 1169 |
(X = 0) and |
| 1170 |
(Y = 0) and |
| 1171 |
(Z = 0); |
| 1172 |
end; |
| 1173 |
|
| 1174 |
class function TGenericVector3.Equals(const V1, V2: TGenericVector3): boolean; |
| 1175 |
begin |
| 1176 |
Result := |
| 1177 |
SameValue(V1.X, V2.X) and |
| 1178 |
SameValue(V1.Y, V2.Y) and |
| 1179 |
SameValue(V1.Z, V2.Z); |
| 1180 |
end; |
| 1181 |
|
| 1182 |
class function TGenericVector3.Equals(const V1, V2: TGenericVector3; const Epsilon: TGenericScalar): boolean; |
| 1183 |
begin |
| 1184 |
if Epsilon = 0 then |
| 1185 |
Result := (V1.X = V2.X) and |
| 1186 |
(V1.Y = V2.Y) and |
| 1187 |
(V1.Z = V2.Z) else |
| 1188 |
Result := (System.Abs(V1.X-V2.X) < Epsilon) and |
| 1189 |
(System.Abs(V1.Y-V2.Y) < Epsilon) and |
| 1190 |
(System.Abs(V1.Z-V2.Z) < Epsilon); |
| 1191 |
end; |
| 1192 |
|
| 1193 |
class function TGenericVector3.PerfectlyEquals(const V1, V2: TGenericVector3): boolean; |
| 1194 |
begin |
| 1195 |
Result := (V1.X = V2.X) and |
| 1196 |
(V1.Y = V2.Y) and |
| 1197 |
(V1.Z = V2.Z); |
| 1198 |
end; |
| 1199 |
|
| 1200 |
class function TGenericVector3.Lerp(const A: TGenericScalar; const V1, V2: TGenericVector3): TGenericVector3; |
| 1201 |
begin |
| 1202 |
Result.X := V1.X + A * (V2.X - V1.X); |
| 1203 |
Result.Y := V1.Y + A * (V2.Y - V1.Y); |
| 1204 |
Result.Z := V1.Z + A * (V2.Z - V1.Z); |
| 1205 |
end; |
| 1206 |
|
| 1207 |
class function TGenericVector3.Zero: TGenericVector3; |
| 1208 |
begin |
| 1209 |
FillChar(Result, SizeOf(Result), 0); |
| 1210 |
end; |
| 1211 |
|
| 1212 |
class function TGenericVector3.GetOne(const Index: TIndex): TGenericVector3; |
| 1213 |
begin |
| 1214 |
FillChar(Result, SizeOf(Result), 0); |
| 1215 |
Result.Data[Index] := 1.0; |
| 1216 |
end; |
| 1217 |
|
| 1218 |
{ TGenericVector4 ------------------------------------------------------------ } |
| 1219 |
|
| 1220 |
class operator TGenericVector4. {$ifdef FPC}+{$else}Add{$endif} (const A, B: TGenericVector4): TGenericVector4; |
| 1221 |
begin |
| 1222 |
Result.X := A.X + B.X; |
| 1223 |
Result.Y := A.Y + B.Y; |
| 1224 |
Result.Z := A.Z + B.Z; |
| 1225 |
Result.W := A.W + B.W; |
| 1226 |
end; |
| 1227 |
|
| 1228 |
class operator TGenericVector4. {$ifdef FPC}-{$else}Subtract{$endif} (const A, B: TGenericVector4): TGenericVector4; |
| 1229 |
begin |
| 1230 |
Result.X := A.X - B.X; |
| 1231 |
Result.Y := A.Y - B.Y; |
| 1232 |
Result.Z := A.Z - B.Z; |
| 1233 |
Result.W := A.W - B.W; |
| 1234 |
end; |
| 1235 |
|
| 1236 |
class operator TGenericVector4. {$ifdef FPC}-{$else}Negative{$endif} (const V: TGenericVector4): TGenericVector4; |
| 1237 |
begin |
| 1238 |
Result.X := - V.X; |
| 1239 |
Result.Y := - V.Y; |
| 1240 |
Result.Z := - V.Z; |
| 1241 |
Result.W := - V.W; |
| 1242 |
end; |
| 1243 |
|
| 1244 |
class operator TGenericVector4. {$ifdef FPC}*{$else}Multiply{$endif} (const V: TGenericVector4; const Scalar: TGenericScalar): TGenericVector4; |
| 1245 |
begin |
| 1246 |
Result.X := V.X * Scalar; |
| 1247 |
Result.Y := V.Y * Scalar; |
| 1248 |
Result.Z := V.Z * Scalar; |
| 1249 |
Result.W := V.W * Scalar; |
| 1250 |
end; |
| 1251 |
|
| 1252 |
class operator TGenericVector4. {$ifdef FPC}*{$else}Multiply{$endif} (const Scalar: TGenericScalar; const V: TGenericVector4): TGenericVector4; |
| 1253 |
begin |
| 1254 |
Result.X := V.X * Scalar; |
| 1255 |
Result.Y := V.Y * Scalar; |
| 1256 |
Result.Z := V.Z * Scalar; |
| 1257 |
Result.W := V.W * Scalar; |
| 1258 |
end; |
| 1259 |
|
| 1260 |
class operator TGenericVector4. {$ifdef FPC}*{$else}Multiply{$endif} (const V1, V2: TGenericVector4): TGenericVector4; |
| 1261 |
begin |
| 1262 |
Result.X := V1.X * V2.X; |
| 1263 |
Result.Y := V1.Y * V2.Y; |
| 1264 |
Result.Z := V1.Z * V2.Z; |
| 1265 |
Result.W := V1.W * V2.W; |
| 1266 |
end; |
| 1267 |
|
| 1268 |
class operator TGenericVector4. {$ifdef FPC}/{$else}Divide{$endif} (const V: TGenericVector4; const Scalar: TGenericScalar): TGenericVector4; |
| 1269 |
var |
| 1270 |
ScalarInv: TGenericScalar; |
| 1271 |
begin |
| 1272 |
ScalarInv := 1 / Scalar; // multiplication is faster than division, so invert once |
| 1273 |
Result.X := V.X * ScalarInv; |
| 1274 |
Result.Y := V.Y * ScalarInv; |
| 1275 |
Result.Z := V.Z * ScalarInv; |
| 1276 |
Result.W := V.W * ScalarInv; |
| 1277 |
end; |
| 1278 |
|
| 1279 |
procedure TGenericVector4.Init(const AX, AY, AZ, AW: TGenericScalar); |
| 1280 |
begin |
| 1281 |
X := AX; |
| 1282 |
Y := AY; |
| 1283 |
Z := AZ; |
| 1284 |
W := AW; |
| 1285 |
end; |
| 1286 |
|
| 1287 |
function TGenericVector4.ToString: string; |
| 1288 |
begin |
| 1289 |
Result := |
| 1290 |
FloatToStrDisplay(X) + ' ' + |
| 1291 |
FloatToStrDisplay(Y) + ' ' + |
| 1292 |
FloatToStrDisplay(Z) + ' ' + |
| 1293 |
FloatToStrDisplay(W); |
| 1294 |
end; |
| 1295 |
|
| 1296 |
function TGenericVector4.ToRawString(const FloatFormat: String): string; |
| 1297 |
begin |
| 1298 |
Result := |
| 1299 |
FormatScalar(X, FloatFormat) + ' ' + |
| 1300 |
FormatScalar(Y, FloatFormat) + ' ' + |
| 1301 |
FormatScalar(Z, FloatFormat) + ' ' + |
| 1302 |
FormatScalar(W, FloatFormat); |
| 1303 |
end; |
| 1304 |
|
| 1305 |
function TGenericVector4.GetItems(const Index: TIndex): TGenericScalar; |
| 1306 |
begin |
| 1307 |
case Index of |
| 1308 |
0: Result := X; |
| 1309 |
1: Result := Y; |
| 1310 |
2: Result := Z; |
| 1311 |
3: Result := W; |
| 1312 |
{$ifndef FPC} else raise EInternalError.Create(202202115); {$endif} |
| 1313 |
end; |
| 1314 |
end; |
| 1315 |
|
| 1316 |
procedure TGenericVector4.SetItems(const Index: TIndex; const Value: TGenericScalar); |
| 1317 |
begin |
| 1318 |
case Index of |
| 1319 |
0: X := Value; |
| 1320 |
1: Y := Value; |
| 1321 |
2: Z := Value; |
| 1322 |
3: W := Value; |
| 1323 |
{$ifndef FPC} else raise EInternalError.Create(202202116); {$endif} |
| 1324 |
end; |
| 1325 |
end; |
| 1326 |
|
| 1327 |
function TGenericVector4.Length: TGenericScalar; |
| 1328 |
begin |
| 1329 |
Result := Sqrt( |
| 1330 |
(X * X) + |
| 1331 |
(Y * Y) + |
| 1332 |
(Z * Z) + |
| 1333 |
(W * W) |
| 1334 |
); |
| 1335 |
end; |
| 1336 |
|
| 1337 |
function TGenericVector4.LengthSqr: TGenericScalar; |
| 1338 |
begin |
| 1339 |
Result := |
| 1340 |
(X * X) + |
| 1341 |
(Y * Y) + |
| 1342 |
(Z * Z) + |
| 1343 |
(W * W); |
| 1344 |
end; |
| 1345 |
|
| 1346 |
function TGenericVector4.AdjustToLength(const NewLength: TGenericScalar): TGenericVector4; |
| 1347 |
begin |
| 1348 |
Result := Self * (NewLength / Length); |
| 1349 |
end; |
| 1350 |
|
| 1351 |
class function TGenericVector4.DotProduct(const V1, V2: TGenericVector4): TGenericScalar; |
| 1352 |
begin |
| 1353 |
Result := |
| 1354 |
V1.X * V2.X + |
| 1355 |
V1.Y * V2.Y + |
| 1356 |
V1.Z * V2.Z + |
| 1357 |
V1.W * V2.W; |
| 1358 |
end; |
| 1359 |
|
| 1360 |
function TGenericVector4.Abs: TGenericVector4; |
| 1361 |
begin |
| 1362 |
Result.X := System.Abs(X); |
| 1363 |
Result.Y := System.Abs(Y); |
| 1364 |
Result.Z := System.Abs(Z); |
| 1365 |
Result.W := System.Abs(W); |
| 1366 |
end; |
| 1367 |
|
| 1368 |
function TGenericVector4.IsZero: boolean; |
| 1369 |
begin |
| 1370 |
Result := |
| 1371 |
Math.IsZero(X) and |
| 1372 |
Math.IsZero(Y) and |
| 1373 |
Math.IsZero(Z) and |
| 1374 |
Math.IsZero(W); |
| 1375 |
end; |
| 1376 |
|
| 1377 |
function TGenericVector4.IsZero(const Epsilon: TGenericScalar): boolean; |
| 1378 |
begin |
| 1379 |
Result := (System.Abs(X) < Epsilon) and |
| 1380 |
(System.Abs(Y) < Epsilon) and |
| 1381 |
(System.Abs(Z) < Epsilon) and |
| 1382 |
(System.Abs(W) < Epsilon); |
| 1383 |
end; |
| 1384 |
|
| 1385 |
function TGenericVector4.IsPerfectlyZero: boolean; |
| 1386 |
begin |
| 1387 |
Result := |
| 1388 |
(X = 0) and |
| 1389 |
(Y = 0) and |
| 1390 |
(Z = 0) and |
| 1391 |
(W = 0); |
| 1392 |
end; |
| 1393 |
|
| 1394 |
class function TGenericVector4.Equals(const V1, V2: TGenericVector4): boolean; |
| 1395 |
begin |
| 1396 |
Result := |
| 1397 |
SameValue(V1.X, V2.X) and |
| 1398 |
SameValue(V1.Y, V2.Y) and |
| 1399 |
SameValue(V1.Z, V2.Z) and |
| 1400 |
SameValue(V1.W, V2.W); |
| 1401 |
end; |
| 1402 |
|
| 1403 |
class function TGenericVector4.Equals(const V1, V2: TGenericVector4; const Epsilon: TGenericScalar): boolean; |
| 1404 |
begin |
| 1405 |
if Epsilon = 0 then |
| 1406 |
Result := (V1.X = V2.X) and |
| 1407 |
(V1.Y = V2.Y) and |
| 1408 |
(V1.Z = V2.Z) and |
| 1409 |
(V1.W = V2.W) else |
| 1410 |
Result := (System.Abs(V1.X-V2.X) < Epsilon) and |
| 1411 |
(System.Abs(V1.Y-V2.Y) < Epsilon) and |
| 1412 |
(System.Abs(V1.Z-V2.Z) < Epsilon) and |
| 1413 |
(System.Abs(V1.W-V2.W) < Epsilon); |
| 1414 |
end; |
| 1415 |
|
| 1416 |
class function TGenericVector4.PerfectlyEquals(const V1, V2: TGenericVector4): boolean; |
| 1417 |
begin |
| 1418 |
Result := (V1.X = V2.X) and |
| 1419 |
(V1.Y = V2.Y) and |
| 1420 |
(V1.Z = V2.Z) and |
| 1421 |
(V1.W = V2.W); |
| 1422 |
end; |
| 1423 |
|
| 1424 |
function TGenericVector4.ToPosition: TGenericVector3; |
| 1425 |
var |
| 1426 |
Inv: TGenericScalar; |
| 1427 |
begin |
| 1428 |
Inv := 1 / W; |
| 1429 |
Result.X := X * Inv; |
| 1430 |
Result.Y := Y * Inv; |
| 1431 |
Result.Z := Z * Inv; |
| 1432 |
end; |
| 1433 |
|
| 1434 |
class function TGenericVector4.Lerp(const A: TGenericScalar; const V1, V2: TGenericVector4): TGenericVector4; |
| 1435 |
begin |
| 1436 |
Result.X := V1.X + A * (V2.X - V1.X); |
| 1437 |
Result.Y := V1.Y + A * (V2.Y - V1.Y); |
| 1438 |
Result.Z := V1.Z + A * (V2.Z - V1.Z); |
| 1439 |
Result.W := V1.W + A * (V2.W - V1.W); |
| 1440 |
end; |
| 1441 |
|
| 1442 |
class function TGenericVector4.Zero: TGenericVector4; |
| 1443 |
begin |
| 1444 |
FillChar(Result, SizeOf(Result), 0); |
| 1445 |
end; |
| 1446 |
|
| 1447 |
class function TGenericVector4.GetOne(const Index: TIndex): TGenericVector4; |
| 1448 |
begin |
| 1449 |
FillChar(Result, SizeOf(Result), 0); |
| 1450 |
Result.Data[Index] := 1.0; |
| 1451 |
end; |
| 1452 |
|
| 1453 |
{ TGenericMatrix2 ------------------------------------------------------------ } |
| 1454 |
|
| 1455 |
class operator TGenericMatrix2. {$ifdef FPC}+{$else}Add{$endif} (const A, B: TGenericMatrix2): TGenericMatrix2; |
| 1456 |
begin |
| 1457 |
Result.Data[0, 0] := A.Data[0, 0] + B.Data[0, 0]; |
| 1458 |
Result.Data[0, 1] := A.Data[0, 1] + B.Data[0, 1]; |
| 1459 |
|
| 1460 |
Result.Data[1, 0] := A.Data[1, 0] + B.Data[1, 0]; |
| 1461 |
Result.Data[1, 1] := A.Data[1, 1] + B.Data[1, 1]; |
| 1462 |
end; |
| 1463 |
|
| 1464 |
class operator TGenericMatrix2. {$ifdef FPC}-{$else}Subtract{$endif} (const A, B: TGenericMatrix2): TGenericMatrix2; |
| 1465 |
begin |
| 1466 |
Result.Data[0, 0] := A.Data[0, 0] - B.Data[0, 0]; |
| 1467 |
Result.Data[0, 1] := A.Data[0, 1] - B.Data[0, 1]; |
| 1468 |
|
| 1469 |
Result.Data[1, 0] := A.Data[1, 0] - B.Data[1, 0]; |
| 1470 |
Result.Data[1, 1] := A.Data[1, 1] - B.Data[1, 1]; |
| 1471 |
end; |
| 1472 |
|
| 1473 |
class operator TGenericMatrix2. {$ifdef FPC}-{$else}Negative{$endif} (const M: TGenericMatrix2): TGenericMatrix2; |
| 1474 |
begin |
| 1475 |
Result.Data[0, 0] := - M.Data[0, 0]; |
| 1476 |
Result.Data[0, 1] := - M.Data[0, 1]; |
| 1477 |
|
| 1478 |
Result.Data[1, 0] := - M.Data[1, 0]; |
| 1479 |
Result.Data[1, 1] := - M.Data[1, 1]; |
| 1480 |
end; |
| 1481 |
|
| 1482 |
class operator TGenericMatrix2.{$ifdef FPC}*{$else}Multiply{$endif} (const V: TGenericMatrix2; const Scalar: TGenericScalar): TGenericMatrix2; |
| 1483 |
begin |
| 1484 |
Result.Data[0, 0] := V.Data[0, 0] * Scalar; |
| 1485 |
Result.Data[0, 1] := V.Data[0, 1] * Scalar; |
| 1486 |
|
| 1487 |
Result.Data[1, 0] := V.Data[1, 0] * Scalar; |
| 1488 |
Result.Data[1, 1] := V.Data[1, 1] * Scalar; |
| 1489 |
end; |
| 1490 |
|
| 1491 |
class operator TGenericMatrix2.{$ifdef FPC}*{$else}Multiply{$endif} (const Scalar: TGenericScalar; const V: TGenericMatrix2): TGenericMatrix2; |
| 1492 |
begin |
| 1493 |
Result.Data[0, 0] := V.Data[0, 0] * Scalar; |
| 1494 |
Result.Data[0, 1] := V.Data[0, 1] * Scalar; |
| 1495 |
|
| 1496 |
Result.Data[1, 0] := V.Data[1, 0] * Scalar; |
| 1497 |
Result.Data[1, 1] := V.Data[1, 1] * Scalar; |
| 1498 |
end; |
| 1499 |
|
| 1500 |
class operator TGenericMatrix2.{$ifdef FPC}*{$else}Multiply{$endif} (const M: TGenericMatrix2; const V: TGenericVector2): TGenericVector2; |
| 1501 |
begin |
| 1502 |
Result.X := M.Data[0, 0] * V.X + M.Data[1, 0] * V.Y; |
| 1503 |
Result.Y := M.Data[0, 1] * V.X + M.Data[1, 1] * V.Y; |
| 1504 |
end; |
| 1505 |
|
| 1506 |
class operator TGenericMatrix2.{$ifdef FPC}*{$else}Multiply{$endif} (const M1, M2: TGenericMatrix2): TGenericMatrix2; |
| 1507 |
begin |
| 1508 |
Result.Data[0, 0] := M1.Data[0, 0] * M2.Data[0, 0] + M1.Data[1, 0] * M2.Data[0, 1]; |
| 1509 |
Result.Data[1, 0] := M1.Data[0, 0] * M2.Data[1, 0] + M1.Data[1, 0] * M2.Data[1, 1]; |
| 1510 |
Result.Data[0, 1] := M1.Data[0, 1] * M2.Data[0, 0] + M1.Data[1, 1] * M2.Data[0, 1]; |
| 1511 |
Result.Data[1, 1] := M1.Data[0, 1] * M2.Data[1, 0] + M1.Data[1, 1] * M2.Data[1, 1]; |
| 1512 |
end; |
| 1513 |
|
| 1514 |
function TGenericMatrix2.ToString(const LineIndent: string): string; |
| 1515 |
begin |
| 1516 |
Result := FormatDot('%s%s %s' + NL + |
| 1517 |
'%s%s %s', |
| 1518 |
[LineIndent, FloatToStrDisplay(Data[0, 0]), FloatToStrDisplay(Data[1, 0]), |
| 1519 |
LineIndent, FloatToStrDisplay(Data[0, 1]), FloatToStrDisplay(Data[1, 1]) ]); |
| 1520 |
end; |
| 1521 |
|
| 1522 |
function TGenericMatrix2.ToRawString(const LineIndent, FloatFormat: String): String; |
| 1523 |
begin |
| 1524 |
Result := |
| 1525 |
LineIndent + FormatScalar(Data[0, 0], FloatFormat) + ' ' + FormatScalar(Data[1, 0], FloatFormat) + NL + |
| 1526 |
LineIndent + FormatScalar(Data[0, 1], FloatFormat) + ' ' + FormatScalar(Data[1, 1], FloatFormat); |
| 1527 |
end; |
| 1528 |
|
| 1529 |
function TGenericMatrix2.GetItems(const AColumn, ARow: TIndex): TGenericScalar; |
| 1530 |
begin |
| 1531 |
Result := Data[AColumn, ARow]; |
| 1532 |
end; |
| 1533 |
|
| 1534 |
procedure TGenericMatrix2.SetItems(const AColumn, ARow: TIndex; const Value: TGenericScalar); |
| 1535 |
begin |
| 1536 |
Data[AColumn, ARow] := Value; |
| 1537 |
end; |
| 1538 |
|
| 1539 |
function TGenericMatrix2.GetRows(const ARow: TIndex): TGenericVector2; |
| 1540 |
begin |
| 1541 |
Result.X := Data[0, ARow]; |
| 1542 |
Result.Y := Data[1, ARow]; |
| 1543 |
end; |
| 1544 |
|
| 1545 |
procedure TGenericMatrix2.SetRows(const ARow: TIndex; const Value: TGenericVector2); |
| 1546 |
begin |
| 1547 |
Data[0, ARow] := Value.X; |
| 1548 |
Data[1, ARow] := Value.Y; |
| 1549 |
end; |
| 1550 |
|
| 1551 |
function TGenericMatrix2.GetColumns(const AColumn: TIndex): TGenericVector2; |
| 1552 |
begin |
| 1553 |
Result := TGenericVector2(Data[AColumn]); // TODO: should not require a typecast |
| 1554 |
end; |
| 1555 |
|
| 1556 |
procedure TGenericMatrix2.SetColumns(const AColumn: TIndex; const Value: TGenericVector2); |
| 1557 |
begin |
| 1558 |
TGenericVector2(Data[AColumn]) := Value; // TODO: should not require a typecast |
| 1559 |
end; |
| 1560 |
|
| 1561 |
function MatrixDet2x2(const a1, a2, b1, b2: TGenericScalar): TGenericScalar; |
| 1562 |
{ Matrix is |
| 1563 |
[ a1 a2 ] |
| 1564 |
[ b1 b2 ] |
| 1565 |
} |
| 1566 |
begin |
| 1567 |
Result := a1 * b2 - a2 * b1; |
| 1568 |
end; |
| 1569 |
|
| 1570 |
function TGenericMatrix2.Determinant: TGenericScalar; |
| 1571 |
begin |
| 1572 |
Result := MatrixDet2x2( |
| 1573 |
Data[0, 0], Data[1, 0], |
| 1574 |
Data[0, 1], Data[1, 1] |
| 1575 |
); |
| 1576 |
end; |
| 1577 |
|
| 1578 |
function TGenericMatrix2.Inverse(ADeterminant: TGenericScalar): TGenericMatrix2; |
| 1579 |
begin |
| 1580 |
{ Code adapted from FPC Matrix unit (same license as Castle Game Engine). |
| 1581 |
|
| 1582 |
Note that the code below is taken from FPC Matrix unit which has transposed |
| 1583 |
matrices. So we should transpose the input and output, in general. |
| 1584 |
But in this case, it's not needed, as the transpose of the inverse |
| 1585 |
is the inverse of the transpose. |
| 1586 |
Which means that |
| 1587 |
|
| 1588 |
Result = transpose(inverse(transpose(m)) |
| 1589 |
= transpose(transpose(inverse(m))) |
| 1590 |
= just inverse(m)) |
| 1591 |
} |
| 1592 |
|
| 1593 |
ADeterminant := 1 / ADeterminant; |
| 1594 |
Result.Data[0, 0] := Data[1, 1] * ADeterminant; |
| 1595 |
Result.Data[0, 1] := -Data[0, 1] * ADeterminant; |
| 1596 |
Result.Data[1, 0] := -Data[1, 0] * ADeterminant; |
| 1597 |
Result.Data[1, 1] := Data[0, 0] * ADeterminant; |
| 1598 |
end; |
| 1599 |
|
| 1600 |
function TGenericMatrix2.TryInverse(out MInverse: TGenericMatrix2): boolean; |
| 1601 |
var |
| 1602 |
D: TGenericScalar; |
| 1603 |
begin |
| 1604 |
D := Determinant; |
| 1605 |
Result := not Math.IsZero(D); |
| 1606 |
if Result then |
| 1607 |
MInverse := Inverse(D); |
| 1608 |
end; |
| 1609 |
|
| 1610 |
function TGenericMatrix2.Transpose: TGenericMatrix2; |
| 1611 |
begin |
| 1612 |
Result.Data[0, 0] := Data[0, 0]; |
| 1613 |
Result.Data[0, 1] := Data[1, 0]; |
| 1614 |
|
| 1615 |
Result.Data[1, 0] := Data[0, 1]; |
| 1616 |
Result.Data[1, 1] := Data[1, 1]; |
| 1617 |
end; |
| 1618 |
|
| 1619 |
class function TGenericMatrix2.Equals(const M1, M2: TGenericMatrix2): boolean; |
| 1620 |
begin |
| 1621 |
Result := |
| 1622 |
SameValue(M1.Data[0, 0], M2.Data[0, 0]) and |
| 1623 |
SameValue(M1.Data[0, 1], M2.Data[0, 1]) and |
| 1624 |
|
| 1625 |
SameValue(M1.Data[1, 0], M2.Data[1, 0]) and |
| 1626 |
SameValue(M1.Data[1, 1], M2.Data[1, 1]); |
| 1627 |
end; |
| 1628 |
|
| 1629 |
class function TGenericMatrix2.Equals(const M1, M2: TGenericMatrix2; const Epsilon: TGenericScalar): boolean; |
| 1630 |
begin |
| 1631 |
if Epsilon = 0 then |
| 1632 |
Result := CompareMem(@M1, @M2, SizeOf(M1)) |
| 1633 |
else |
| 1634 |
Result := |
| 1635 |
(System.Abs(M1.Data[0, 0] - M2.Data[0, 0]) < Epsilon) and |
| 1636 |
(System.Abs(M1.Data[0, 1] - M2.Data[0, 1]) < Epsilon) and |
| 1637 |
|
| 1638 |
(System.Abs(M1.Data[1, 0] - M2.Data[1, 0]) < Epsilon) and |
| 1639 |
(System.Abs(M1.Data[1, 1] - M2.Data[1, 1]) < Epsilon); |
| 1640 |
end; |
| 1641 |
|
| 1642 |
class function TGenericMatrix2.PerfectlyEquals(const M1, M2: TGenericMatrix2): boolean; |
| 1643 |
begin |
| 1644 |
Result := CompareMem(@M1, @M2, SizeOf(M1)); |
| 1645 |
end; |
| 1646 |
|
| 1647 |
class function TGenericMatrix2.Lerp(const A: TGenericScalar; const M1, M2: TGenericMatrix2): TGenericMatrix2; |
| 1648 |
begin |
| 1649 |
Result.Data[0, 0] := M1.Data[0, 0] + A * (M2.Data[0, 0] - M1.Data[0, 0]); |
| 1650 |
Result.Data[0, 1] := M1.Data[0, 1] + A * (M2.Data[0, 1] - M1.Data[0, 1]); |
| 1651 |
|
| 1652 |
Result.Data[1, 0] := M1.Data[1, 0] + A * (M2.Data[1, 0] - M1.Data[1, 0]); |
| 1653 |
Result.Data[1, 1] := M1.Data[1, 1] + A * (M2.Data[1, 1] - M1.Data[1, 1]); |
| 1654 |
end; |
| 1655 |
|
| 1656 |
class function TGenericMatrix2.Zero: TGenericMatrix2; |
| 1657 |
begin |
| 1658 |
FillChar(Result, SizeOf(Result), 0); |
| 1659 |
end; |
| 1660 |
|
| 1661 |
class function TGenericMatrix2.Identity: TGenericMatrix2; |
| 1662 |
begin |
| 1663 |
FillChar(Result, SizeOf(Result), 0); |
| 1664 |
Result[0, 0] := 1; |
| 1665 |
Result[1, 1] := 1; |
| 1666 |
end; |
| 1667 |
|
| 1668 |
{ TGenericMatrix3 ------------------------------------------------------------ } |
| 1669 |
|
| 1670 |
class operator TGenericMatrix3. {$ifdef FPC}+{$else}Add{$endif} (const A, B: TGenericMatrix3): TGenericMatrix3; |
| 1671 |
begin |
| 1672 |
Result.Data[0, 0] := A.Data[0, 0] + B.Data[0, 0]; |
| 1673 |
Result.Data[0, 1] := A.Data[0, 1] + B.Data[0, 1]; |
| 1674 |
Result.Data[0, 2] := A.Data[0, 2] + B.Data[0, 2]; |
| 1675 |
|
| 1676 |
Result.Data[1, 0] := A.Data[1, 0] + B.Data[1, 0]; |
| 1677 |
Result.Data[1, 1] := A.Data[1, 1] + B.Data[1, 1]; |
| 1678 |
Result.Data[1, 2] := A.Data[1, 2] + B.Data[1, 2]; |
| 1679 |
|
| 1680 |
Result.Data[2, 0] := A.Data[2, 0] + B.Data[2, 0]; |
| 1681 |
Result.Data[2, 1] := A.Data[2, 1] + B.Data[2, 1]; |
| 1682 |
Result.Data[2, 2] := A.Data[2, 2] + B.Data[2, 2]; |
| 1683 |
end; |
| 1684 |
|
| 1685 |
class operator TGenericMatrix3. {$ifdef FPC}-{$else}Subtract{$endif} (const A, B: TGenericMatrix3): TGenericMatrix3; |
| 1686 |
begin |
| 1687 |
Result.Data[0, 0] := A.Data[0, 0] - B.Data[0, 0]; |
| 1688 |
Result.Data[0, 1] := A.Data[0, 1] - B.Data[0, 1]; |
| 1689 |
Result.Data[0, 2] := A.Data[0, 2] - B.Data[0, 2]; |
| 1690 |
|
| 1691 |
Result.Data[1, 0] := A.Data[1, 0] - B.Data[1, 0]; |
| 1692 |
Result.Data[1, 1] := A.Data[1, 1] - B.Data[1, 1]; |
| 1693 |
Result.Data[1, 2] := A.Data[1, 2] - B.Data[1, 2]; |
| 1694 |
|
| 1695 |
Result.Data[2, 0] := A.Data[2, 0] - B.Data[2, 0]; |
| 1696 |
Result.Data[2, 1] := A.Data[2, 1] - B.Data[2, 1]; |
| 1697 |
Result.Data[2, 2] := A.Data[2, 2] - B.Data[2, 2]; |
| 1698 |
end; |
| 1699 |
|
| 1700 |
class operator TGenericMatrix3. {$ifdef FPC}-{$else}Negative{$endif} (const M: TGenericMatrix3): TGenericMatrix3; |
| 1701 |
begin |
| 1702 |
Result.Data[0, 0] := - M.Data[0, 0]; |
| 1703 |
Result.Data[0, 1] := - M.Data[0, 1]; |
| 1704 |
Result.Data[0, 2] := - M.Data[0, 2]; |
| 1705 |
|
| 1706 |
Result.Data[1, 0] := - M.Data[1, 0]; |
| 1707 |
Result.Data[1, 1] := - M.Data[1, 1]; |
| 1708 |
Result.Data[1, 2] := - M.Data[1, 2]; |
| 1709 |
|
| 1710 |
Result.Data[2, 0] := - M.Data[2, 0]; |
| 1711 |
Result.Data[2, 1] := - M.Data[2, 1]; |
| 1712 |
Result.Data[2, 2] := - M.Data[2, 2]; |
| 1713 |
end; |
| 1714 |
|
| 1715 |
class operator TGenericMatrix3.{$ifdef FPC}*{$else}Multiply{$endif} (const V: TGenericMatrix3; const Scalar: TGenericScalar): TGenericMatrix3; |
| 1716 |
begin |
| 1717 |
Result.Data[0, 0] := V.Data[0, 0] * Scalar; |
| 1718 |
Result.Data[0, 1] := V.Data[0, 1] * Scalar; |
| 1719 |
Result.Data[0, 2] := V.Data[0, 2] * Scalar; |
| 1720 |
|
| 1721 |
Result.Data[1, 0] := V.Data[1, 0] * Scalar; |
| 1722 |
Result.Data[1, 1] := V.Data[1, 1] * Scalar; |
| 1723 |
Result.Data[1, 2] := V.Data[1, 2] * Scalar; |
| 1724 |
|
| 1725 |
Result.Data[2, 0] := V.Data[2, 0] * Scalar; |
| 1726 |
Result.Data[2, 1] := V.Data[2, 1] * Scalar; |
| 1727 |
Result.Data[2, 2] := V.Data[2, 2] * Scalar; |
| 1728 |
end; |
| 1729 |
|
| 1730 |
class operator TGenericMatrix3.{$ifdef FPC}*{$else}Multiply{$endif} (const Scalar: TGenericScalar; const V: TGenericMatrix3): TGenericMatrix3; |
| 1731 |
begin |
| 1732 |
Result.Data[0, 0] := V.Data[0, 0] * Scalar; |
| 1733 |
Result.Data[0, 1] := V.Data[0, 1] * Scalar; |
| 1734 |
Result.Data[0, 2] := V.Data[0, 2] * Scalar; |
| 1735 |
|
| 1736 |
Result.Data[1, 0] := V.Data[1, 0] * Scalar; |
| 1737 |
Result.Data[1, 1] := V.Data[1, 1] * Scalar; |
| 1738 |
Result.Data[1, 2] := V.Data[1, 2] * Scalar; |
| 1739 |
|
| 1740 |
Result.Data[2, 0] := V.Data[2, 0] * Scalar; |
| 1741 |
Result.Data[2, 1] := V.Data[2, 1] * Scalar; |
| 1742 |
Result.Data[2, 2] := V.Data[2, 2] * Scalar; |
| 1743 |
end; |
| 1744 |
|
| 1745 |
class operator TGenericMatrix3.{$ifdef FPC}*{$else}Multiply{$endif} (const M: TGenericMatrix3; const V: TGenericVector3): TGenericVector3; |
| 1746 |
begin |
| 1747 |
Result.X := M.Data[0, 0] * V.X + M.Data[1, 0] * V.Y + M.Data[2, 0] * V.Z; |
| 1748 |
Result.Y := M.Data[0, 1] * V.X + M.Data[1, 1] * V.Y + M.Data[2, 1] * V.Z; |
| 1749 |
Result.Z := M.Data[0, 2] * V.X + M.Data[1, 2] * V.Y + M.Data[2, 2] * V.Z; |
| 1750 |
end; |
| 1751 |
|
| 1752 |
class operator TGenericMatrix3.{$ifdef FPC}*{$else}Multiply{$endif} (const M1, M2: TGenericMatrix3): TGenericMatrix3; |
| 1753 |
begin |
| 1754 |
Result.Data[0, 0] := M1.Data[0, 0] * M2.Data[0, 0] + M1.Data[1, 0] * M2.Data[0, 1] + M1.Data[2, 0] * M2.Data[0, 2]; |
| 1755 |
Result.Data[1, 0] := M1.Data[0, 0] * M2.Data[1, 0] + M1.Data[1, 0] * M2.Data[1, 1] + M1.Data[2, 0] * M2.Data[1, 2]; |
| 1756 |
Result.Data[2, 0] := M1.Data[0, 0] * M2.Data[2, 0] + M1.Data[1, 0] * M2.Data[2, 1] + M1.Data[2, 0] * M2.Data[2, 2]; |
| 1757 |
Result.Data[0, 1] := M1.Data[0, 1] * M2.Data[0, 0] + M1.Data[1, 1] * M2.Data[0, 1] + M1.Data[2, 1] * M2.Data[0, 2]; |
| 1758 |
Result.Data[1, 1] := M1.Data[0, 1] * M2.Data[1, 0] + M1.Data[1, 1] * M2.Data[1, 1] + M1.Data[2, 1] * M2.Data[1, 2]; |
| 1759 |
Result.Data[2, 1] := M1.Data[0, 1] * M2.Data[2, 0] + M1.Data[1, 1] * M2.Data[2, 1] + M1.Data[2, 1] * M2.Data[2, 2]; |
| 1760 |
Result.Data[0, 2] := M1.Data[0, 2] * M2.Data[0, 0] + M1.Data[1, 2] * M2.Data[0, 1] + M1.Data[2, 2] * M2.Data[0, 2]; |
| 1761 |
Result.Data[1, 2] := M1.Data[0, 2] * M2.Data[1, 0] + M1.Data[1, 2] * M2.Data[1, 1] + M1.Data[2, 2] * M2.Data[1, 2]; |
| 1762 |
Result.Data[2, 2] := M1.Data[0, 2] * M2.Data[2, 0] + M1.Data[1, 2] * M2.Data[2, 1] + M1.Data[2, 2] * M2.Data[2, 2]; |
| 1763 |
end; |
| 1764 |
|
| 1765 |
function TGenericMatrix3.ToString(const LineIndent: string): string; |
| 1766 |
begin |
| 1767 |
Result := FormatDot('%s%s %s %s' + NL + |
| 1768 |
'%s%s %s %s' + NL + |
| 1769 |
'%s%s %s %s', |
| 1770 |
[LineIndent, FloatToStrDisplay(Data[0, 0]), FloatToStrDisplay(Data[1, 0]), FloatToStrDisplay(Data[2, 0]), |
| 1771 |
LineIndent, FloatToStrDisplay(Data[0, 1]), FloatToStrDisplay(Data[1, 1]), FloatToStrDisplay(Data[2, 1]), |
| 1772 |
LineIndent, FloatToStrDisplay(Data[0, 2]), FloatToStrDisplay(Data[1, 2]), FloatToStrDisplay(Data[2, 2]) ]); |
| 1773 |
end; |
| 1774 |
|
| 1775 |
function TGenericMatrix3.ToRawString(const LineIndent, FloatFormat: String): String; |
| 1776 |
begin |
| 1777 |
Result := |
| 1778 |
LineIndent + FormatScalar(Data[0, 0], FloatFormat) + ' ' + FormatScalar(Data[1, 0], FloatFormat) + ' ' + FormatScalar(Data[2, 0], FloatFormat) + NL + |
| 1779 |
LineIndent + FormatScalar(Data[0, 1], FloatFormat) + ' ' + FormatScalar(Data[1, 1], FloatFormat) + ' ' + FormatScalar(Data[2, 1], FloatFormat) + NL + |
| 1780 |
LineIndent + FormatScalar(Data[0, 2], FloatFormat) + ' ' + FormatScalar(Data[1, 2], FloatFormat) + ' ' + FormatScalar(Data[2, 2], FloatFormat); |
| 1781 |
end; |
| 1782 |
|
| 1783 |
function TGenericMatrix3.GetItems(const AColumn, ARow: TIndex): TGenericScalar; |
| 1784 |
begin |
| 1785 |
Result := Data[AColumn, ARow]; |
| 1786 |
end; |
| 1787 |
|
| 1788 |
procedure TGenericMatrix3.SetItems(const AColumn, ARow: TIndex; const Value: TGenericScalar); |
| 1789 |
begin |
| 1790 |
Data[AColumn, ARow] := Value; |
| 1791 |
end; |
| 1792 |
|
| 1793 |
function TGenericMatrix3.GetRows(const ARow: TIndex): TGenericVector3; |
| 1794 |
begin |
| 1795 |
Result.X := Data[0, ARow]; |
| 1796 |
Result.Y := Data[1, ARow]; |
| 1797 |
Result.Z := Data[2, ARow]; |
| 1798 |
end; |
| 1799 |
|
| 1800 |
procedure TGenericMatrix3.SetRows(const ARow: TIndex; const Value: TGenericVector3); |
| 1801 |
begin |
| 1802 |
Data[0, ARow] := Value.X; |
| 1803 |
Data[1, ARow] := Value.Y; |
| 1804 |
Data[2, ARow] := Value.Z; |
| 1805 |
end; |
| 1806 |
|
| 1807 |
function TGenericMatrix3.GetColumns(const AColumn: TIndex): TGenericVector3; |
| 1808 |
begin |
| 1809 |
Result := TGenericVector3(Data[AColumn]); // TODO: should not require a typecast |
| 1810 |
end; |
| 1811 |
|
| 1812 |
procedure TGenericMatrix3.SetColumns(const AColumn: TIndex; const Value: TGenericVector3); |
| 1813 |
begin |
| 1814 |
TGenericVector3(Data[AColumn]) := Value; // TODO: should not require a typecast |
| 1815 |
end; |
| 1816 |
|
| 1817 |
function MatrixDet3x3(const a1, a2, a3, b1, b2, b3, c1, c2, c3: TGenericScalar): TGenericScalar; |
| 1818 |
begin |
| 1819 |
Result := a1 * MatrixDet2x2 (b2, b3, c2, c3) |
| 1820 |
- b1 * MatrixDet2x2 (a2, a3, c2, c3) |
| 1821 |
+ c1 * MatrixDet2x2 (a2, a3, b2, b3); |
| 1822 |
end; |
| 1823 |
|
| 1824 |
function TGenericMatrix3.Determinant: TGenericScalar; |
| 1825 |
begin |
| 1826 |
Result := MatrixDet3x3( |
| 1827 |
Data[0, 0], Data[1, 0], Data[2, 0], |
| 1828 |
Data[0, 1], Data[1, 1], Data[2, 1], |
| 1829 |
Data[0, 2], Data[1, 2], Data[2, 2] |
| 1830 |
); |
| 1831 |
end; |
| 1832 |
|
| 1833 |
function TGenericMatrix3.Inverse(ADeterminant: TGenericScalar): TGenericMatrix3; |
| 1834 |
begin |
| 1835 |
{ Code adapted from FPC Matrix unit (same license as Castle Game Engine). |
| 1836 |
This calculates the inverse of a transpose, but it doesn't matter, |
| 1837 |
see MatrixInverse on TMatrix2 comments. } |
| 1838 |
|
| 1839 |
ADeterminant := 1 / ADeterminant; |
| 1840 |
Result.Data[0,0] := (Data[1,1] * Data[2,2]-Data[2,1] * Data[1,2]) * ADeterminant; |
| 1841 |
Result.Data[0,1] := -(Data[0,1] * Data[2,2]-Data[2,1] * Data[0,2]) * ADeterminant; |
| 1842 |
Result.Data[0,2] := (Data[0,1] * Data[1,2]-Data[1,1] * Data[0,2]) * ADeterminant; |
| 1843 |
Result.Data[1,0] := -(Data[1,0] * Data[2,2]-Data[2,0] * Data[1,2]) * ADeterminant; |
| 1844 |
Result.Data[1,1] := (Data[0,0] * Data[2,2]-Data[2,0] * Data[0,2]) * ADeterminant; |
| 1845 |
Result.Data[1,2] := -(Data[0,0] * Data[1,2]-Data[1,0] * Data[0,2]) * ADeterminant; |
| 1846 |
Result.Data[2,0] := (Data[1,0] * Data[2,1]-Data[2,0] * Data[1,1]) * ADeterminant; |
| 1847 |
Result.Data[2,1] := -(Data[0,0] * Data[2,1]-Data[2,0] * Data[0,1]) * ADeterminant; |
| 1848 |
Result.Data[2,2] := (Data[0,0] * Data[1,1]-Data[1,0] * Data[0,1]) * ADeterminant; |
| 1849 |
end; |
| 1850 |
|
| 1851 |
function TGenericMatrix3.TryInverse(out MInverse: TGenericMatrix3): boolean; |
| 1852 |
var |
| 1853 |
D: TGenericScalar; |
| 1854 |
begin |
| 1855 |
D := Determinant; |
| 1856 |
Result := not Math.IsZero(D); |
| 1857 |
if Result then |
| 1858 |
MInverse := Inverse(D); |
| 1859 |
end; |
| 1860 |
|
| 1861 |
function TGenericMatrix3.Transpose: TGenericMatrix3; |
| 1862 |
begin |
| 1863 |
Result.Data[0, 0] := Data[0, 0]; |
| 1864 |
Result.Data[0, 1] := Data[1, 0]; |
| 1865 |
Result.Data[0, 2] := Data[2, 0]; |
| 1866 |
|
| 1867 |
Result.Data[1, 0] := Data[0, 1]; |
| 1868 |
Result.Data[1, 1] := Data[1, 1]; |
| 1869 |
Result.Data[1, 2] := Data[2, 1]; |
| 1870 |
|
| 1871 |
Result.Data[2, 0] := Data[0, 2]; |
| 1872 |
Result.Data[2, 1] := Data[1, 2]; |
| 1873 |
Result.Data[2, 2] := Data[2, 2]; |
| 1874 |
end; |
| 1875 |
|
| 1876 |
class function TGenericMatrix3.Equals(const M1, M2: TGenericMatrix3): boolean; |
| 1877 |
begin |
| 1878 |
Result := |
| 1879 |
SameValue(M1.Data[0, 0], M2.Data[0, 0]) and |
| 1880 |
SameValue(M1.Data[0, 1], M2.Data[0, 1]) and |
| 1881 |
SameValue(M1.Data[0, 2], M2.Data[0, 2]) and |
| 1882 |
|
| 1883 |
SameValue(M1.Data[1, 0], M2.Data[1, 0]) and |
| 1884 |
SameValue(M1.Data[1, 1], M2.Data[1, 1]) and |
| 1885 |
SameValue(M1.Data[1, 2], M2.Data[1, 2]) and |
| 1886 |
|
| 1887 |
SameValue(M1.Data[2, 0], M2.Data[2, 0]) and |
| 1888 |
SameValue(M1.Data[2, 1], M2.Data[2, 1]) and |
| 1889 |
SameValue(M1.Data[2, 2], M2.Data[2, 2]); |
| 1890 |
end; |
| 1891 |
|
| 1892 |
class function TGenericMatrix3.Equals(const M1, M2: TGenericMatrix3; const Epsilon: TGenericScalar): boolean; |
| 1893 |
begin |
| 1894 |
if Epsilon = 0 then |
| 1895 |
Result := CompareMem(@M1, @M2, SizeOf(M1)) |
| 1896 |
else |
| 1897 |
Result := |
| 1898 |
(System.Abs(M1.Data[0, 0] - M2.Data[0, 0]) < Epsilon) and |
| 1899 |
(System.Abs(M1.Data[0, 1] - M2.Data[0, 1]) < Epsilon) and |
| 1900 |
(System.Abs(M1.Data[0, 2] - M2.Data[0, 2]) < Epsilon) and |
| 1901 |
|
| 1902 |
(System.Abs(M1.Data[1, 0] - M2.Data[1, 0]) < Epsilon) and |
| 1903 |
(System.Abs(M1.Data[1, 1] - M2.Data[1, 1]) < Epsilon) and |
| 1904 |
(System.Abs(M1.Data[1, 2] - M2.Data[1, 2]) < Epsilon) and |
| 1905 |
|
| 1906 |
(System.Abs(M1.Data[2, 0] - M2.Data[2, 0]) < Epsilon) and |
| 1907 |
(System.Abs(M1.Data[2, 1] - M2.Data[2, 1]) < Epsilon) and |
| 1908 |
(System.Abs(M1.Data[2, 2] - M2.Data[2, 2]) < Epsilon); |
| 1909 |
end; |
| 1910 |
|
| 1911 |
class function TGenericMatrix3.PerfectlyEquals(const M1, M2: TGenericMatrix3): boolean; |
| 1912 |
begin |
| 1913 |
Result := CompareMem(@M1, @M2, SizeOf(M1)); |
| 1914 |
end; |
| 1915 |
|
| 1916 |
class function TGenericMatrix3.Lerp(const A: TGenericScalar; const M1, M2: TGenericMatrix3): TGenericMatrix3; |
| 1917 |
begin |
| 1918 |
Result.Data[0, 0] := M1.Data[0, 0] + A * (M2.Data[0, 0] - M1.Data[0, 0]); |
| 1919 |
Result.Data[0, 1] := M1.Data[0, 1] + A * (M2.Data[0, 1] - M1.Data[0, 1]); |
| 1920 |
Result.Data[0, 2] := M1.Data[0, 2] + A * (M2.Data[0, 2] - M1.Data[0, 2]); |
| 1921 |
|
| 1922 |
Result.Data[1, 0] := M1.Data[1, 0] + A * (M2.Data[1, 0] - M1.Data[1, 0]); |
| 1923 |
Result.Data[1, 1] := M1.Data[1, 1] + A * (M2.Data[1, 1] - M1.Data[1, 1]); |
| 1924 |
Result.Data[1, 2] := M1.Data[1, 2] + A * (M2.Data[1, 2] - M1.Data[1, 2]); |
| 1925 |
|
| 1926 |
Result.Data[2, 0] := M1.Data[2, 0] + A * (M2.Data[2, 0] - M1.Data[2, 0]); |
| 1927 |
Result.Data[2, 1] := M1.Data[2, 1] + A * (M2.Data[2, 1] - M1.Data[2, 1]); |
| 1928 |
Result.Data[2, 2] := M1.Data[2, 2] + A * (M2.Data[2, 2] - M1.Data[2, 2]); |
| 1929 |
end; |
| 1930 |
|
| 1931 |
class function TGenericMatrix3.Zero: TGenericMatrix3; |
| 1932 |
begin |
| 1933 |
FillChar(Result, SizeOf(Result), 0); |
| 1934 |
end; |
| 1935 |
|
| 1936 |
class function TGenericMatrix3.Identity: TGenericMatrix3; |
| 1937 |
begin |
| 1938 |
FillChar(Result, SizeOf(Result), 0); |
| 1939 |
Result[0, 0] := 1; |
| 1940 |
Result[1, 1] := 1; |
| 1941 |
Result[2, 2] := 1; |
| 1942 |
end; |
| 1943 |
|
| 1944 |
{ TGenericMatrix4 ------------------------------------------------------------ } |
| 1945 |
|
| 1946 |
class operator TGenericMatrix4. {$ifdef FPC}+{$else}Add{$endif} (const A, B: TGenericMatrix4): TGenericMatrix4; |
| 1947 |
begin |
| 1948 |
Result.Data[0, 0] := A.Data[0, 0] + B.Data[0, 0]; |
| 1949 |
Result.Data[0, 1] := A.Data[0, 1] + B.Data[0, 1]; |
| 1950 |
Result.Data[0, 2] := A.Data[0, 2] + B.Data[0, 2]; |
| 1951 |
Result.Data[0, 3] := A.Data[0, 3] + B.Data[0, 3]; |
| 1952 |
|
| 1953 |
Result.Data[1, 0] := A.Data[1, 0] + B.Data[1, 0]; |
| 1954 |
Result.Data[1, 1] := A.Data[1, 1] + B.Data[1, 1]; |
| 1955 |
Result.Data[1, 2] := A.Data[1, 2] + B.Data[1, 2]; |
| 1956 |
Result.Data[1, 3] := A.Data[1, 3] + B.Data[1, 3]; |
| 1957 |
|
| 1958 |
Result.Data[2, 0] := A.Data[2, 0] + B.Data[2, 0]; |
| 1959 |
Result.Data[2, 1] := A.Data[2, 1] + B.Data[2, 1]; |
| 1960 |
Result.Data[2, 2] := A.Data[2, 2] + B.Data[2, 2]; |
| 1961 |
Result.Data[2, 3] := A.Data[2, 3] + B.Data[2, 3]; |
| 1962 |
|
| 1963 |
Result.Data[3, 0] := A.Data[3, 0] + B.Data[3, 0]; |
| 1964 |
Result.Data[3, 1] := A.Data[3, 1] + B.Data[3, 1]; |
| 1965 |
Result.Data[3, 2] := A.Data[3, 2] + B.Data[3, 2]; |
| 1966 |
Result.Data[3, 3] := A.Data[3, 3] + B.Data[3, 3]; |
| 1967 |
end; |
| 1968 |
|
| 1969 |
class operator TGenericMatrix4. {$ifdef FPC}-{$else}Subtract{$endif} (const A, B: TGenericMatrix4): TGenericMatrix4; |
| 1970 |
begin |
| 1971 |
Result.Data[0, 0] := A.Data[0, 0] - B.Data[0, 0]; |
| 1972 |
Result.Data[0, 1] := A.Data[0, 1] - B.Data[0, 1]; |
| 1973 |
Result.Data[0, 2] := A.Data[0, 2] - B.Data[0, 2]; |
| 1974 |
Result.Data[0, 3] := A.Data[0, 3] - B.Data[0, 3]; |
| 1975 |
|
| 1976 |
Result.Data[1, 0] := A.Data[1, 0] - B.Data[1, 0]; |
| 1977 |
Result.Data[1, 1] := A.Data[1, 1] - B.Data[1, 1]; |
| 1978 |
Result.Data[1, 2] := A.Data[1, 2] - B.Data[1, 2]; |
| 1979 |
Result.Data[1, 3] := A.Data[1, 3] - B.Data[1, 3]; |
| 1980 |
|
| 1981 |
Result.Data[2, 0] := A.Data[2, 0] - B.Data[2, 0]; |
| 1982 |
Result.Data[2, 1] := A.Data[2, 1] - B.Data[2, 1]; |
| 1983 |
Result.Data[2, 2] := A.Data[2, 2] - B.Data[2, 2]; |
| 1984 |
Result.Data[2, 3] := A.Data[2, 3] - B.Data[2, 3]; |
| 1985 |
|
| 1986 |
Result.Data[3, 0] := A.Data[3, 0] - B.Data[3, 0]; |
| 1987 |
Result.Data[3, 1] := A.Data[3, 1] - B.Data[3, 1]; |
| 1988 |
Result.Data[3, 2] := A.Data[3, 2] - B.Data[3, 2]; |
| 1989 |
Result.Data[3, 3] := A.Data[3, 3] - B.Data[3, 3]; |
| 1990 |
end; |
| 1991 |
|
| 1992 |
class operator TGenericMatrix4. {$ifdef FPC}-{$else}Negative{$endif} (const M: TGenericMatrix4): TGenericMatrix4; |
| 1993 |
begin |
| 1994 |
Result.Data[0, 0] := - M.Data[0, 0]; |
| 1995 |
Result.Data[0, 1] := - M.Data[0, 1]; |
| 1996 |
Result.Data[0, 2] := - M.Data[0, 2]; |
| 1997 |
Result.Data[0, 3] := - M.Data[0, 3]; |
| 1998 |
|
| 1999 |
Result.Data[1, 0] := - M.Data[1, 0]; |
| 2000 |
Result.Data[1, 1] := - M.Data[1, 1]; |
| 2001 |
Result.Data[1, 2] := - M.Data[1, 2]; |
| 2002 |
Result.Data[1, 3] := - M.Data[1, 3]; |
| 2003 |
|
| 2004 |
Result.Data[2, 0] := - M.Data[2, 0]; |
| 2005 |
Result.Data[2, 1] := - M.Data[2, 1]; |
| 2006 |
Result.Data[2, 2] := - M.Data[2, 2]; |
| 2007 |
Result.Data[2, 3] := - M.Data[2, 3]; |
| 2008 |
|
| 2009 |
Result.Data[3, 0] := - M.Data[3, 0]; |
| 2010 |
Result.Data[3, 1] := - M.Data[3, 1]; |
| 2011 |
Result.Data[3, 2] := - M.Data[3, 2]; |
| 2012 |
Result.Data[3, 3] := - M.Data[3, 3]; |
| 2013 |
end; |
| 2014 |
|
| 2015 |
class operator TGenericMatrix4.{$ifdef FPC}*{$else}Multiply{$endif} (const V: TGenericMatrix4; const Scalar: TGenericScalar): TGenericMatrix4; |
| 2016 |
begin |
| 2017 |
Result.Data[0, 0] := V.Data[0, 0] * Scalar; |
| 2018 |
Result.Data[0, 1] := V.Data[0, 1] * Scalar; |
| 2019 |
Result.Data[0, 2] := V.Data[0, 2] * Scalar; |
| 2020 |
Result.Data[0, 3] := V.Data[0, 3] * Scalar; |
| 2021 |
|
| 2022 |
Result.Data[1, 0] := V.Data[1, 0] * Scalar; |
| 2023 |
Result.Data[1, 1] := V.Data[1, 1] * Scalar; |
| 2024 |
Result.Data[1, 2] := V.Data[1, 2] * Scalar; |
| 2025 |
Result.Data[1, 3] := V.Data[1, 3] * Scalar; |
| 2026 |
|
| 2027 |
Result.Data[2, 0] := V.Data[2, 0] * Scalar; |
| 2028 |
Result.Data[2, 1] := V.Data[2, 1] * Scalar; |
| 2029 |
Result.Data[2, 2] := V.Data[2, 2] * Scalar; |
| 2030 |
Result.Data[2, 3] := V.Data[2, 3] * Scalar; |
| 2031 |
|
| 2032 |
Result.Data[3, 0] := V.Data[3, 0] * Scalar; |
| 2033 |
Result.Data[3, 1] := V.Data[3, 1] * Scalar; |
| 2034 |
Result.Data[3, 2] := V.Data[3, 2] * Scalar; |
| 2035 |
Result.Data[3, 3] := V.Data[3, 3] * Scalar; |
| 2036 |
end; |
| 2037 |
|
| 2038 |
class operator TGenericMatrix4.{$ifdef FPC}*{$else}Multiply{$endif} (const Scalar: TGenericScalar; const V: TGenericMatrix4): TGenericMatrix4; |
| 2039 |
begin |
| 2040 |
Result.Data[0, 0] := V.Data[0, 0] * Scalar; |
| 2041 |
Result.Data[0, 1] := V.Data[0, 1] * Scalar; |
| 2042 |
Result.Data[0, 2] := V.Data[0, 2] * Scalar; |
| 2043 |
Result.Data[0, 3] := V.Data[0, 3] * Scalar; |
| 2044 |
|
| 2045 |
Result.Data[1, 0] := V.Data[1, 0] * Scalar; |
| 2046 |
Result.Data[1, 1] := V.Data[1, 1] * Scalar; |
| 2047 |
Result.Data[1, 2] := V.Data[1, 2] * Scalar; |
| 2048 |
Result.Data[1, 3] := V.Data[1, 3] * Scalar; |
| 2049 |
|
| 2050 |
Result.Data[2, 0] := V.Data[2, 0] * Scalar; |
| 2051 |
Result.Data[2, 1] := V.Data[2, 1] * Scalar; |
| 2052 |
Result.Data[2, 2] := V.Data[2, 2] * Scalar; |
| 2053 |
Result.Data[2, 3] := V.Data[2, 3] * Scalar; |
| 2054 |
|
| 2055 |
Result.Data[3, 0] := V.Data[3, 0] * Scalar; |
| 2056 |
Result.Data[3, 1] := V.Data[3, 1] * Scalar; |
| 2057 |
Result.Data[3, 2] := V.Data[3, 2] * Scalar; |
| 2058 |
Result.Data[3, 3] := V.Data[3, 3] * Scalar; |
| 2059 |
end; |
| 2060 |
|
| 2061 |
function TGenericMatrix4.TransposeMultiply(const V: TGenericVector4): TGenericVector4; |
| 2062 |
begin |
| 2063 |
Result.X := Data[0, 0] * V.X + Data[0, 1] * V.Y + Data[0, 2] * V.Z + Data[0, 3] * V.W; |
| 2064 |
Result.Y := Data[1, 0] * V.X + Data[1, 1] * V.Y + Data[1, 2] * V.Z + Data[1, 3] * V.W; |
| 2065 |
Result.Z := Data[2, 0] * V.X + Data[2, 1] * V.Y + Data[2, 2] * V.Z + Data[2, 3] * V.W; |
| 2066 |
Result.W := Data[3, 0] * V.X + Data[3, 1] * V.Y + Data[3, 2] * V.Z + Data[3, 3] * V.W; |
| 2067 |
end; |
| 2068 |
|
| 2069 |
class operator TGenericMatrix4.{$ifdef FPC}*{$else}Multiply{$endif} (const M: TGenericMatrix4; const V: TGenericVector4): TGenericVector4; |
| 2070 |
{var |
| 2071 |
I, J: Integer;} |
| 2072 |
begin |
| 2073 |
{ We cannot have this uncommented (because we cannot use blindly Writeln, |
| 2074 |
and depending on CastleLog for WritelnWarning makes FPC internal errors). |
| 2075 |
if @V = @Result then |
| 2076 |
Writeln('TGenericMatrix4.Multiply(vector) may be invalid: Argument and Result have the same address'); } |
| 2077 |
|
| 2078 |
{ |
| 2079 |
for I := 0 to 3 do |
| 2080 |
begin |
| 2081 |
Result.Data[I] := 0; |
| 2082 |
for J := 0 to 3 do |
| 2083 |
Result.Data[I] := Result.Data[I] + M.Data[J, I] * V.Data[J]; |
| 2084 |
end; |
| 2085 |
|
| 2086 |
Code expanded for the sake of speed:} |
| 2087 |
|
| 2088 |
Result.X := M.Data[0, 0] * V.X + M.Data[1, 0] * V.Y + M.Data[2, 0] * V.Z + M.Data[3, 0] * V.W; |
| 2089 |
Result.Y := M.Data[0, 1] * V.X + M.Data[1, 1] * V.Y + M.Data[2, 1] * V.Z + M.Data[3, 1] * V.W; |
| 2090 |
Result.Z := M.Data[0, 2] * V.X + M.Data[1, 2] * V.Y + M.Data[2, 2] * V.Z + M.Data[3, 2] * V.W; |
| 2091 |
Result.W := M.Data[0, 3] * V.X + M.Data[1, 3] * V.Y + M.Data[2, 3] * V.Z + M.Data[3, 3] * V.W; |
| 2092 |
end; |
| 2093 |
|
| 2094 |
class operator TGenericMatrix4.{$ifdef FPC}*{$else}Multiply{$endif} (const M1, M2: TGenericMatrix4): TGenericMatrix4; |
| 2095 |
{var |
| 2096 |
I, J, K: Integer;} |
| 2097 |
begin |
| 2098 |
(* |
| 2099 |
FillChar(Result, SizeOf(Result), 0); |
| 2100 |
for I := 0 to 3 do { i = rows, j = columns } |
| 2101 |
for J := 0 to 3 do |
| 2102 |
for K := 0 to 3 do |
| 2103 |
Result.Data[J, I] := Result.Data[J, I] + M1.Data[K, I] * M2.Data[J, K]; |
| 2104 |
*) |
| 2105 |
|
| 2106 |
{ This is code above expanded for speed sake |
| 2107 |
(code generated by genMultMatrix) } |
| 2108 |
Result.Data[0, 0] := M1.Data[0, 0] * M2.Data[0, 0] + M1.Data[1, 0] * M2.Data[0, 1] + M1.Data[2, 0] * M2.Data[0, 2] + M1.Data[3, 0] * M2.Data[0, 3]; |
| 2109 |
Result.Data[1, 0] := M1.Data[0, 0] * M2.Data[1, 0] + M1.Data[1, 0] * M2.Data[1, 1] + M1.Data[2, 0] * M2.Data[1, 2] + M1.Data[3, 0] * M2.Data[1, 3]; |
| 2110 |
Result.Data[2, 0] := M1.Data[0, 0] * M2.Data[2, 0] + M1.Data[1, 0] * M2.Data[2, 1] + M1.Data[2, 0] * M2.Data[2, 2] + M1.Data[3, 0] * M2.Data[2, 3]; |
| 2111 |
Result.Data[3, 0] := M1.Data[0, 0] * M2.Data[3, 0] + M1.Data[1, 0] * M2.Data[3, 1] + M1.Data[2, 0] * M2.Data[3, 2] + M1.Data[3, 0] * M2.Data[3, 3]; |
| 2112 |
Result.Data[0, 1] := M1.Data[0, 1] * M2.Data[0, 0] + M1.Data[1, 1] * M2.Data[0, 1] + M1.Data[2, 1] * M2.Data[0, 2] + M1.Data[3, 1] * M2.Data[0, 3]; |
| 2113 |
Result.Data[1, 1] := M1.Data[0, 1] * M2.Data[1, 0] + M1.Data[1, 1] * M2.Data[1, 1] + M1.Data[2, 1] * M2.Data[1, 2] + M1.Data[3, 1] * M2.Data[1, 3]; |
| 2114 |
Result.Data[2, 1] := M1.Data[0, 1] * M2.Data[2, 0] + M1.Data[1, 1] * M2.Data[2, 1] + M1.Data[2, 1] * M2.Data[2, 2] + M1.Data[3, 1] * M2.Data[2, 3]; |
| 2115 |
Result.Data[3, 1] := M1.Data[0, 1] * M2.Data[3, 0] + M1.Data[1, 1] * M2.Data[3, 1] + M1.Data[2, 1] * M2.Data[3, 2] + M1.Data[3, 1] * M2.Data[3, 3]; |
| 2116 |
Result.Data[0, 2] := M1.Data[0, 2] * M2.Data[0, 0] + M1.Data[1, 2] * M2.Data[0, 1] + M1.Data[2, 2] * M2.Data[0, 2] + M1.Data[3, 2] * M2.Data[0, 3]; |
| 2117 |
Result.Data[1, 2] := M1.Data[0, 2] * M2.Data[1, 0] + M1.Data[1, 2] * M2.Data[1, 1] + M1.Data[2, 2] * M2.Data[1, 2] + M1.Data[3, 2] * M2.Data[1, 3]; |
| 2118 |
Result.Data[2, 2] := M1.Data[0, 2] * M2.Data[2, 0] + M1.Data[1, 2] * M2.Data[2, 1] + M1.Data[2, 2] * M2.Data[2, 2] + M1.Data[3, 2] * M2.Data[2, 3]; |
| 2119 |
Result.Data[3, 2] := M1.Data[0, 2] * M2.Data[3, 0] + M1.Data[1, 2] * M2.Data[3, 1] + M1.Data[2, 2] * M2.Data[3, 2] + M1.Data[3, 2] * M2.Data[3, 3]; |
| 2120 |
Result.Data[0, 3] := M1.Data[0, 3] * M2.Data[0, 0] + M1.Data[1, 3] * M2.Data[0, 1] + M1.Data[2, 3] * M2.Data[0, 2] + M1.Data[3, 3] * M2.Data[0, 3]; |
| 2121 |
Result.Data[1, 3] := M1.Data[0, 3] * M2.Data[1, 0] + M1.Data[1, 3] * M2.Data[1, 1] + M1.Data[2, 3] * M2.Data[1, 2] + M1.Data[3, 3] * M2.Data[1, 3]; |
| 2122 |
Result.Data[2, 3] := M1.Data[0, 3] * M2.Data[2, 0] + M1.Data[1, 3] * M2.Data[2, 1] + M1.Data[2, 3] * M2.Data[2, 2] + M1.Data[3, 3] * M2.Data[2, 3]; |
| 2123 |
Result.Data[3, 3] := M1.Data[0, 3] * M2.Data[3, 0] + M1.Data[1, 3] * M2.Data[3, 1] + M1.Data[2, 3] * M2.Data[3, 2] + M1.Data[3, 3] * M2.Data[3, 3]; |
| 2124 |
end; |
| 2125 |
|
| 2126 |
function TGenericMatrix4.ToString(const LineIndent: string): string; |
| 2127 |
begin |
| 2128 |
Result := FormatDot('%s%s %s %s %s' + NL + |
| 2129 |
'%s%s %s %s %s' + NL + |
| 2130 |
'%s%s %s %s %s' + NL + |
| 2131 |
'%s%s %s %s %s', |
| 2132 |
[LineIndent, FloatToStrDisplay(Data[0, 0]), FloatToStrDisplay(Data[1, 0]), FloatToStrDisplay(Data[2, 0]), FloatToStrDisplay(Data[3, 0]), |
| 2133 |
LineIndent, FloatToStrDisplay(Data[0, 1]), FloatToStrDisplay(Data[1, 1]), FloatToStrDisplay(Data[2, 1]), FloatToStrDisplay(Data[3, 1]), |
| 2134 |
LineIndent, FloatToStrDisplay(Data[0, 2]), FloatToStrDisplay(Data[1, 2]), FloatToStrDisplay(Data[2, 2]), FloatToStrDisplay(Data[3, 2]), |
| 2135 |
LineIndent, FloatToStrDisplay(Data[0, 3]), FloatToStrDisplay(Data[1, 3]), FloatToStrDisplay(Data[2, 3]), FloatToStrDisplay(Data[3, 3]) ]); |
| 2136 |
end; |
| 2137 |
|
| 2138 |
function TGenericMatrix4.ToRawString(const LineIndent, FloatFormat: String): String; |
| 2139 |
begin |
| 2140 |
Result := |
| 2141 |
LineIndent + FormatScalar(Data[0, 0], FloatFormat) + ' ' + FormatScalar(Data[1, 0], FloatFormat) + ' ' + FormatScalar(Data[2, 0], FloatFormat) + ' ' + FormatScalar(Data[3, 0], FloatFormat) + NL + |
| 2142 |
LineIndent + FormatScalar(Data[0, 1], FloatFormat) + ' ' + FormatScalar(Data[1, 1], FloatFormat) + ' ' + FormatScalar(Data[2, 1], FloatFormat) + ' ' + FormatScalar(Data[3, 1], FloatFormat) + NL + |
| 2143 |
LineIndent + FormatScalar(Data[0, 2], FloatFormat) + ' ' + FormatScalar(Data[1, 2], FloatFormat) + ' ' + FormatScalar(Data[2, 2], FloatFormat) + ' ' + FormatScalar(Data[3, 2], FloatFormat) + NL + |
| 2144 |
LineIndent + FormatScalar(Data[0, 3], FloatFormat) + ' ' + FormatScalar(Data[1, 3], FloatFormat) + ' ' + FormatScalar(Data[2, 3], FloatFormat) + ' ' + FormatScalar(Data[3, 3], FloatFormat); |
| 2145 |
end; |
| 2146 |
|
| 2147 |
function TGenericMatrix4.GetItems(const AColumn, ARow: TIndex): TGenericScalar; |
| 2148 |
begin |
| 2149 |
Result := Data[AColumn, ARow]; |
| 2150 |
end; |
| 2151 |
|
| 2152 |
procedure TGenericMatrix4.SetItems(const AColumn, ARow: TIndex; const Value: TGenericScalar); |
| 2153 |
begin |
| 2154 |
Data[AColumn, ARow] := Value; |
| 2155 |
end; |
| 2156 |
|
| 2157 |
function TGenericMatrix4.GetRows(const ARow: TIndex): TGenericVector4; |
| 2158 |
begin |
| 2159 |
Result.X := Data[0, ARow]; |
| 2160 |
Result.Y := Data[1, ARow]; |
| 2161 |
Result.Z := Data[2, ARow]; |
| 2162 |
Result.W := Data[3, ARow]; |
| 2163 |
end; |
| 2164 |
|
| 2165 |
procedure TGenericMatrix4.SetRows(const ARow: TIndex; const Value: TGenericVector4); |
| 2166 |
begin |
| 2167 |
Data[0, ARow] := Value.X; |
| 2168 |
Data[1, ARow] := Value.Y; |
| 2169 |
Data[2, ARow] := Value.Z; |
| 2170 |
Data[3, ARow] := Value.W; |
| 2171 |
end; |
| 2172 |
|
| 2173 |
function TGenericMatrix4.GetColumns(const AColumn: TIndex): TGenericVector4; |
| 2174 |
begin |
| 2175 |
Result := TGenericVector4(Data[AColumn]); // TODO: should not require a typecast |
| 2176 |
end; |
| 2177 |
|
| 2178 |
procedure TGenericMatrix4.SetColumns(const AColumn: TIndex; const Value: TGenericVector4); |
| 2179 |
begin |
| 2180 |
TGenericVector4(Data[AColumn]) := Value; // TODO: should not require a typecast |
| 2181 |
end; |
| 2182 |
|
| 2183 |
function TGenericMatrix4.Determinant: TGenericScalar; |
| 2184 |
var |
| 2185 |
a1, a2, a3, a4, b1, b2, b3, b4, c1, c2, c3, c4, d1, d2, d3, d4: TGenericScalar; |
| 2186 |
begin |
| 2187 |
a1 := Data[0, 0]; b1 := Data[0, 1]; |
| 2188 |
c1 := Data[0, 2]; d1 := Data[0, 3]; |
| 2189 |
|
| 2190 |
a2 := Data[1, 0]; b2 := Data[1, 1]; |
| 2191 |
c2 := Data[1, 2]; d2 := Data[1, 3]; |
| 2192 |
|
| 2193 |
a3 := Data[2, 0]; b3 := Data[2, 1]; |
| 2194 |
c3 := Data[2, 2]; d3 := Data[2, 3]; |
| 2195 |
|
| 2196 |
a4 := Data[3, 0]; b4 := Data[3, 1]; |
| 2197 |
c4 := Data[3, 2]; d4 := Data[3, 3]; |
| 2198 |
|
| 2199 |
Result := a1 * MatrixDet3x3 (b2, b3, b4, c2, c3, c4, d2, d3, d4) - |
| 2200 |
b1 * MatrixDet3x3 (a2, a3, a4, c2, c3, c4, d2, d3, d4) + |
| 2201 |
c1 * MatrixDet3x3 (a2, a3, a4, b2, b3, b4, d2, d3, d4) - |
| 2202 |
d1 * MatrixDet3x3 (a2, a3, a4, b2, b3, b4, c2, c3, c4); |
| 2203 |
end; |
| 2204 |
|
| 2205 |
function TGenericMatrix4.Inverse(ADeterminant: TGenericScalar): TGenericMatrix4; |
| 2206 |
begin |
| 2207 |
{ Code adapted from FPC Matrix unit (same license as Castle Game Engine). |
| 2208 |
This calculates the inverse of a transpose, but it doesn't matter, |
| 2209 |
see TGenericMatrix3.Inverse comments. } |
| 2210 |
|
| 2211 |
ADeterminant := 1 / ADeterminant; |
| 2212 |
Result.Data[0,0] := ADeterminant * (Data[1,1] * (Data[2,2] * Data[3,3] - Data[2,3] * Data[3,2])+ |
| 2213 |
Data[1,2] * (Data[2,3] * Data[3,1] - Data[2,1] * Data[3,3])+ |
| 2214 |
Data[1,3] * (Data[2,1] * Data[3,2] - Data[2,2] * Data[3,1])); |
| 2215 |
Result.Data[0,1] := ADeterminant * (Data[2,1] * (Data[0,2] * Data[3,3] - Data[0,3] * Data[3,2])+ |
| 2216 |
Data[2,2] * (Data[0,3] * Data[3,1] - Data[0,1] * Data[3,3])+ |
| 2217 |
Data[2,3] * (Data[0,1] * Data[3,2] - Data[0,2] * Data[3,1])); |
| 2218 |
Result.Data[0,2] := ADeterminant * (Data[3,1] * (Data[0,2] * Data[1,3] - Data[0,3] * Data[1,2])+ |
| 2219 |
Data[3,2] * (Data[0,3] * Data[1,1] - Data[0,1] * Data[1,3])+ |
| 2220 |
Data[3,3] * (Data[0,1] * Data[1,2] - Data[0,2] * Data[1,1])); |
| 2221 |
Result.Data[0,3] := ADeterminant * (Data[0,1] * (Data[1,3] * Data[2,2] - Data[1,2] * Data[2,3])+ |
| 2222 |
Data[0,2] * (Data[1,1] * Data[2,3] - Data[1,3] * Data[2,1])+ |
| 2223 |
Data[0,3] * (Data[1,2] * Data[2,1] - Data[1,1] * Data[2,2])); |
| 2224 |
Result.Data[1,0] := ADeterminant * (Data[1,2] * (Data[2,0] * Data[3,3] - Data[2,3] * Data[3,0])+ |
| 2225 |
Data[1,3] * (Data[2,2] * Data[3,0] - Data[2,0] * Data[3,2])+ |
| 2226 |
Data[1,0] * (Data[2,3] * Data[3,2] - Data[2,2] * Data[3,3])); |
| 2227 |
Result.Data[1,1] := ADeterminant * (Data[2,2] * (Data[0,0] * Data[3,3] - Data[0,3] * Data[3,0])+ |
| 2228 |
Data[2,3] * (Data[0,2] * Data[3,0] - Data[0,0] * Data[3,2])+ |
| 2229 |
Data[2,0] * (Data[0,3] * Data[3,2] - Data[0,2] * Data[3,3])); |
| 2230 |
Result.Data[1,2] := ADeterminant * (Data[3,2] * (Data[0,0] * Data[1,3] - Data[0,3] * Data[1,0])+ |
| 2231 |
Data[3,3] * (Data[0,2] * Data[1,0] - Data[0,0] * Data[1,2])+ |
| 2232 |
Data[3,0] * (Data[0,3] * Data[1,2] - Data[0,2] * Data[1,3])); |
| 2233 |
Result.Data[1,3] := ADeterminant * (Data[0,2] * (Data[1,3] * Data[2,0] - Data[1,0] * Data[2,3])+ |
| 2234 |
Data[0,3] * (Data[1,0] * Data[2,2] - Data[1,2] * Data[2,0])+ |
| 2235 |
Data[0,0] * (Data[1,2] * Data[2,3] - Data[1,3] * Data[2,2])); |
| 2236 |
Result.Data[2,0] := ADeterminant * (Data[1,3] * (Data[2,0] * Data[3,1] - Data[2,1] * Data[3,0])+ |
| 2237 |
Data[1,0] * (Data[2,1] * Data[3,3] - Data[2,3] * Data[3,1])+ |
| 2238 |
Data[1,1] * (Data[2,3] * Data[3,0] - Data[2,0] * Data[3,3])); |
| 2239 |
Result.Data[2,1] := ADeterminant * (Data[2,3] * (Data[0,0] * Data[3,1] - Data[0,1] * Data[3,0])+ |
| 2240 |
Data[2,0] * (Data[0,1] * Data[3,3] - Data[0,3] * Data[3,1])+ |
| 2241 |
Data[2,1] * (Data[0,3] * Data[3,0] - Data[0,0] * Data[3,3])); |
| 2242 |
Result.Data[2,2] := ADeterminant * (Data[3,3] * (Data[0,0] * Data[1,1] - Data[0,1] * Data[1,0])+ |
| 2243 |
Data[3,0] * (Data[0,1] * Data[1,3] - Data[0,3] * Data[1,1])+ |
| 2244 |
Data[3,1] * (Data[0,3] * Data[1,0] - Data[0,0] * Data[1,3])); |
| 2245 |
Result.Data[2,3] := ADeterminant * (Data[0,3] * (Data[1,1] * Data[2,0] - Data[1,0] * Data[2,1])+ |
| 2246 |
Data[0,0] * (Data[1,3] * Data[2,1] - Data[1,1] * Data[2,3])+ |
| 2247 |
Data[0,1] * (Data[1,0] * Data[2,3] - Data[1,3] * Data[2,0])); |
| 2248 |
Result.Data[3,0] := ADeterminant * (Data[1,0] * (Data[2,2] * Data[3,1] - Data[2,1] * Data[3,2])+ |
| 2249 |
Data[1,1] * (Data[2,0] * Data[3,2] - Data[2,2] * Data[3,0])+ |
| 2250 |
Data[1,2] * (Data[2,1] * Data[3,0] - Data[2,0] * Data[3,1])); |
| 2251 |
Result.Data[3,1] := ADeterminant * (Data[2,0] * (Data[0,2] * Data[3,1] - Data[0,1] * Data[3,2])+ |
| 2252 |
Data[2,1] * (Data[0,0] * Data[3,2] - Data[0,2] * Data[3,0])+ |
| 2253 |
Data[2,2] * (Data[0,1] * Data[3,0] - Data[0,0] * Data[3,1])); |
| 2254 |
Result.Data[3,2] := ADeterminant * (Data[3,0] * (Data[0,2] * Data[1,1] - Data[0,1] * Data[1,2])+ |
| 2255 |
Data[3,1] * (Data[0,0] * Data[1,2] - Data[0,2] * Data[1,0])+ |
| 2256 |
Data[3,2] * (Data[0,1] * Data[1,0] - Data[0,0] * Data[1,1])); |
| 2257 |
Result.Data[3,3] := ADeterminant * (Data[0,0] * (Data[1,1] * Data[2,2] - Data[1,2] * Data[2,1])+ |
| 2258 |
Data[0,1] * (Data[1,2] * Data[2,0] - Data[1,0] * Data[2,2])+ |
| 2259 |
Data[0,2] * (Data[1,0] * Data[2,1] - Data[1,1] * Data[2,0])); |
| 2260 |
end; |
| 2261 |
|
| 2262 |
function TGenericMatrix4.TryInverse(out MInverse: TGenericMatrix4): boolean; |
| 2263 |
var |
| 2264 |
D: TGenericScalar; |
| 2265 |
begin |
| 2266 |
D := Determinant; |
| 2267 |
Result := not Math.IsZero(D); |
| 2268 |
if Result then |
| 2269 |
MInverse := Inverse(D); |
| 2270 |
end; |
| 2271 |
|
| 2272 |
function TGenericMatrix4.Transpose: TGenericMatrix4; |
| 2273 |
begin |
| 2274 |
Result.Data[0, 0] := Data[0, 0]; |
| 2275 |
Result.Data[0, 1] := Data[1, 0]; |
| 2276 |
Result.Data[0, 2] := Data[2, 0]; |
| 2277 |
Result.Data[0, 3] := Data[3, 0]; |
| 2278 |
|
| 2279 |
Result.Data[1, 0] := Data[0, 1]; |
| 2280 |
Result.Data[1, 1] := Data[1, 1]; |
| 2281 |
Result.Data[1, 2] := Data[2, 1]; |
| 2282 |
Result.Data[1, 3] := Data[3, 1]; |
| 2283 |
|
| 2284 |
Result.Data[2, 0] := Data[0, 2]; |
| 2285 |
Result.Data[2, 1] := Data[1, 2]; |
| 2286 |
Result.Data[2, 2] := Data[2, 2]; |
| 2287 |
Result.Data[2, 3] := Data[3, 2]; |
| 2288 |
|
| 2289 |
Result.Data[3, 0] := Data[0, 3]; |
| 2290 |
Result.Data[3, 1] := Data[1, 3]; |
| 2291 |
Result.Data[3, 2] := Data[2, 3]; |
| 2292 |
Result.Data[3, 3] := Data[3, 3]; |
| 2293 |
end; |
| 2294 |
|
| 2295 |
procedure TGenericMatrix4.RaisePositionTransformResultInvalid; |
| 2296 |
begin |
| 2297 |
raise ETransformedResultInvalid.Create('3D point transformed by 4x4 matrix to a direction'); |
| 2298 |
end; |
| 2299 |
|
| 2300 |
function TGenericMatrix4.MultPoint(const Pt: TGenericVector3): TGenericVector3; |
| 2301 |
var |
| 2302 |
Divisor: TGenericScalar; |
| 2303 |
begin |
| 2304 |
{ Simple implementation: |
| 2305 |
Result := (M * Vector4(Pt, 1)).ToPosition; } |
| 2306 |
|
| 2307 |
{ We cannot have this uncommented (because we cannot use blindly Writeln, |
| 2308 |
and depending on CastleLog for WritelnWarning makes FPC internal errors). |
| 2309 |
if @Pt = @Result then |
| 2310 |
Writeln('TGenericMatrix4.MultPoint may be invalid: Argument and Result have the same address'); } |
| 2311 |
|
| 2312 |
Result.X := Data[0, 0] * Pt.X + Data[1, 0] * Pt.Y + Data[2, 0] * Pt.Z + Data[3, 0]; |
| 2313 |
Result.Y := Data[0, 1] * Pt.X + Data[1, 1] * Pt.Y + Data[2, 1] * Pt.Z + Data[3, 1]; |
| 2314 |
Result.Z := Data[0, 2] * Pt.X + Data[1, 2] * Pt.Y + Data[2, 2] * Pt.Z + Data[3, 2]; |
| 2315 |
|
| 2316 |
{ It looks strange, but the check below usually pays off. |
| 2317 |
|
| 2318 |
Tests: 17563680 calls of this proc within Creatures.PrepareRender |
| 2319 |
inside "The Castle", gprof says that time without this check |
| 2320 |
is 12.01 secs and with this checks it's 8.25. |
| 2321 |
|
| 2322 |
Why ? Because in 99% of situations, the conditions "(Data[0, 3] = 0) and ..." |
| 2323 |
is true. Because that's how all usual matrices in 3D graphics |
| 2324 |
(translation, rotation, scaling) look like. |
| 2325 |
So usually I pay 4 comparisons (exact comparisons, not things like |
| 2326 |
FloatsEqual) and I avoid 3 multiplications, 4 additions and |
| 2327 |
3 divisions. } |
| 2328 |
|
| 2329 |
if not ( |
| 2330 |
(Data[0, 3] = 0) and |
| 2331 |
(Data[1, 3] = 0) and |
| 2332 |
(Data[2, 3] = 0) and |
| 2333 |
(Data[3, 3] = 1)) then |
| 2334 |
begin |
| 2335 |
Divisor := |
| 2336 |
Data[0, 3] * Pt.X + |
| 2337 |
Data[1, 3] * Pt.Y + |
| 2338 |
Data[2, 3] * Pt.Z + |
| 2339 |
Data[3, 3]; |
| 2340 |
if Math.IsZero(Divisor) then |
| 2341 |
RaisePositionTransformResultInvalid; |
| 2342 |
|
| 2343 |
Divisor := 1 / Divisor; |
| 2344 |
Result.X := Result.X * Divisor; |
| 2345 |
Result.Y := Result.Y * Divisor; |
| 2346 |
Result.Z := Result.Z * Divisor; |
| 2347 |
end; |
| 2348 |
end; |
| 2349 |
|
| 2350 |
function TGenericMatrix4.MultPoint(const Pt: TGenericVector2): TGenericVector2; |
| 2351 |
var |
| 2352 |
V3: TGenericVector3; |
| 2353 |
Res3D: TGenericVector3; |
| 2354 |
begin |
| 2355 |
V3.X := Pt.X; |
| 2356 |
V3.Y := Pt.Y; |
| 2357 |
V3.Z := 0; |
| 2358 |
|
| 2359 |
Res3D := MultPoint(V3); |
| 2360 |
|
| 2361 |
Result.X := Res3D.X; |
| 2362 |
Result.Y := Res3D.Y; |
| 2363 |
end; |
| 2364 |
|
| 2365 |
procedure TGenericMatrix4.RaiseDirectionTransformedResultInvalid(const Divisor: TGenericScalar); |
| 2366 |
begin |
| 2367 |
raise ETransformedResultInvalid.Create(Format( |
| 2368 |
'3D direction transformed by 4x4 matrix to a point, with divisor = %f (%g), with matrix:', |
| 2369 |
[Divisor, Divisor]) + NL + ToString); |
| 2370 |
end; |
| 2371 |
|
| 2372 |
function TGenericMatrix4.MultDirection(const Dir: TGenericVector3): TGenericVector3; |
| 2373 |
var |
| 2374 |
Divisor: TGenericScalar; |
| 2375 |
begin |
| 2376 |
{ We cannot have this uncommented (because we cannot use blindly Writeln, |
| 2377 |
and depending on CastleLog for WritelnWarning makes FPC internal errors). |
| 2378 |
if @Dir = @Result then |
| 2379 |
Writeln('TGenericMatrix4.MultDirection may be invalid: Argument and Result have the same address'); } |
| 2380 |
|
| 2381 |
Result.X := Data[0, 0] * Dir.X + Data[1, 0] * Dir.Y + Data[2, 0] * Dir.Z; |
| 2382 |
Result.Y := Data[0, 1] * Dir.X + Data[1, 1] * Dir.Y + Data[2, 1] * Dir.Z; |
| 2383 |
Result.Z := Data[0, 2] * Dir.X + Data[1, 2] * Dir.Y + Data[2, 2] * Dir.Z; |
| 2384 |
|
| 2385 |
if not ( |
| 2386 |
(Data[0, 3] = 0) and |
| 2387 |
(Data[1, 3] = 0) and |
| 2388 |
(Data[2, 3] = 0) ) then |
| 2389 |
begin |
| 2390 |
Divisor := Data[0, 3] * Dir.X + Data[1, 3] * Dir.Y + Data[2, 3] * Dir.Z; |
| 2391 |
if not Math.IsZero(Divisor) then |
| 2392 |
RaiseDirectionTransformedResultInvalid(Divisor); |
| 2393 |
end; |
| 2394 |
end; |
| 2395 |
|
| 2396 |
function TGenericMatrix4.MultDirection(const Dir: TGenericVector2): TGenericVector2; |
| 2397 |
var |
| 2398 |
V3: TGenericVector3; |
| 2399 |
Res3D: TGenericVector3; |
| 2400 |
begin |
| 2401 |
V3.X := Dir.X; |
| 2402 |
V3.Y := Dir.Y; |
| 2403 |
V3.Z := 0; |
| 2404 |
|
| 2405 |
Res3D := MultDirection(V3); |
| 2406 |
|
| 2407 |
Result.X := Res3D.X; |
| 2408 |
Result.Y := Res3D.Y; |
| 2409 |
end; |
| 2410 |
|
| 2411 |
class function TGenericMatrix4.Equals(const M1, M2: TGenericMatrix4): boolean; |
| 2412 |
begin |
| 2413 |
Result := |
| 2414 |
SameValue(M1.Data[0, 0], M2.Data[0, 0]) and |
| 2415 |
SameValue(M1.Data[0, 1], M2.Data[0, 1]) and |
| 2416 |
SameValue(M1.Data[0, 2], M2.Data[0, 2]) and |
| 2417 |
SameValue(M1.Data[0, 3], M2.Data[0, 3]) and |
| 2418 |
|
| 2419 |
SameValue(M1.Data[1, 0], M2.Data[1, 0]) and |
| 2420 |
SameValue(M1.Data[1, 1], M2.Data[1, 1]) and |
| 2421 |
SameValue(M1.Data[1, 2], M2.Data[1, 2]) and |
| 2422 |
SameValue(M1.Data[1, 3], M2.Data[1, 3]) and |
| 2423 |
|
| 2424 |
SameValue(M1.Data[2, 0], M2.Data[2, 0]) and |
| 2425 |
SameValue(M1.Data[2, 1], M2.Data[2, 1]) and |
| 2426 |
SameValue(M1.Data[2, 2], M2.Data[2, 2]) and |
| 2427 |
SameValue(M1.Data[2, 3], M2.Data[2, 3]) and |
| 2428 |
|
| 2429 |
SameValue(M1.Data[3, 0], M2.Data[3, 0]) and |
| 2430 |
SameValue(M1.Data[3, 1], M2.Data[3, 1]) and |
| 2431 |
SameValue(M1.Data[3, 2], M2.Data[3, 2]) and |
| 2432 |
SameValue(M1.Data[3, 3], M2.Data[3, 3]); |
| 2433 |
end; |
| 2434 |
|
| 2435 |
class function TGenericMatrix4.Equals(const M1, M2: TGenericMatrix4; const Epsilon: TGenericScalar): boolean; |
| 2436 |
begin |
| 2437 |
if Epsilon = 0 then |
| 2438 |
Result := CompareMem(@M1, @M2, SizeOf(M1)) |
| 2439 |
else |
| 2440 |
Result := |
| 2441 |
(System.Abs(M1.Data[0, 0] - M2.Data[0, 0]) < Epsilon) and |
| 2442 |
(System.Abs(M1.Data[0, 1] - M2.Data[0, 1]) < Epsilon) and |
| 2443 |
(System.Abs(M1.Data[0, 2] - M2.Data[0, 2]) < Epsilon) and |
| 2444 |
(System.Abs(M1.Data[0, 3] - M2.Data[0, 3]) < Epsilon) and |
| 2445 |
|
| 2446 |
(System.Abs(M1.Data[1, 0] - M2.Data[1, 0]) < Epsilon) and |
| 2447 |
(System.Abs(M1.Data[1, 1] - M2.Data[1, 1]) < Epsilon) and |
| 2448 |
(System.Abs(M1.Data[1, 2] - M2.Data[1, 2]) < Epsilon) and |
| 2449 |
(System.Abs(M1.Data[1, 3] - M2.Data[1, 3]) < Epsilon) and |
| 2450 |
|
| 2451 |
(System.Abs(M1.Data[2, 0] - M2.Data[2, 0]) < Epsilon) and |
| 2452 |
(System.Abs(M1.Data[2, 1] - M2.Data[2, 1]) < Epsilon) and |
| 2453 |
(System.Abs(M1.Data[2, 2] - M2.Data[2, 2]) < Epsilon) and |
| 2454 |
(System.Abs(M1.Data[2, 3] - M2.Data[2, 3]) < Epsilon) and |
| 2455 |
|
| 2456 |
(System.Abs(M1.Data[3, 0] - M2.Data[3, 0]) < Epsilon) and |
| 2457 |
(System.Abs(M1.Data[3, 1] - M2.Data[3, 1]) < Epsilon) and |
| 2458 |
(System.Abs(M1.Data[3, 2] - M2.Data[3, 2]) < Epsilon) and |
| 2459 |
(System.Abs(M1.Data[3, 3] - M2.Data[3, 3]) < Epsilon); |
| 2460 |
end; |
| 2461 |
|
| 2462 |
class function TGenericMatrix4.PerfectlyEquals(const M1, M2: TGenericMatrix4): boolean; |
| 2463 |
begin |
| 2464 |
Result := CompareMem(@M1, @M2, SizeOf(M1)); |
| 2465 |
end; |
| 2466 |
|
| 2467 |
class function TGenericMatrix4.Lerp(const A: TGenericScalar; const M1, M2: TGenericMatrix4): TGenericMatrix4; |
| 2468 |
begin |
| 2469 |
Result.Data[0, 0] := M1.Data[0, 0] + A * (M2.Data[0, 0] - M1.Data[0, 0]); |
| 2470 |
Result.Data[0, 1] := M1.Data[0, 1] + A * (M2.Data[0, 1] - M1.Data[0, 1]); |
| 2471 |
Result.Data[0, 2] := M1.Data[0, 2] + A * (M2.Data[0, 2] - M1.Data[0, 2]); |
| 2472 |
Result.Data[0, 3] := M1.Data[0, 3] + A * (M2.Data[0, 3] - M1.Data[0, 3]); |
| 2473 |
|
| 2474 |
Result.Data[1, 0] := M1.Data[1, 0] + A * (M2.Data[1, 0] - M1.Data[1, 0]); |
| 2475 |
Result.Data[1, 1] := M1.Data[1, 1] + A * (M2.Data[1, 1] - M1.Data[1, 1]); |
| 2476 |
Result.Data[1, 2] := M1.Data[1, 2] + A * (M2.Data[1, 2] - M1.Data[1, 2]); |
| 2477 |
Result.Data[1, 3] := M1.Data[1, 3] + A * (M2.Data[1, 3] - M1.Data[1, 3]); |
| 2478 |
|
| 2479 |
Result.Data[2, 0] := M1.Data[2, 0] + A * (M2.Data[2, 0] - M1.Data[2, 0]); |
| 2480 |
Result.Data[2, 1] := M1.Data[2, 1] + A * (M2.Data[2, 1] - M1.Data[2, 1]); |
| 2481 |
Result.Data[2, 2] := M1.Data[2, 2] + A * (M2.Data[2, 2] - M1.Data[2, 2]); |
| 2482 |
Result.Data[2, 3] := M1.Data[2, 3] + A * (M2.Data[2, 3] - M1.Data[2, 3]); |
| 2483 |
|
| 2484 |
Result.Data[3, 0] := M1.Data[3, 0] + A * (M2.Data[3, 0] - M1.Data[3, 0]); |
| 2485 |
Result.Data[3, 1] := M1.Data[3, 1] + A * (M2.Data[3, 1] - M1.Data[3, 1]); |
| 2486 |
Result.Data[3, 2] := M1.Data[3, 2] + A * (M2.Data[3, 2] - M1.Data[3, 2]); |
| 2487 |
Result.Data[3, 3] := M1.Data[3, 3] + A * (M2.Data[3, 3] - M1.Data[3, 3]); |
| 2488 |
end; |
| 2489 |
|
| 2490 |
class function TGenericMatrix4.Zero: TGenericMatrix4; |
| 2491 |
begin |
| 2492 |
FillChar(Result, SizeOf(Result), 0); |
| 2493 |
end; |
| 2494 |
|
| 2495 |
class function TGenericMatrix4.Identity: TGenericMatrix4; |
| 2496 |
begin |
| 2497 |
FillChar(Result, SizeOf(Result), 0); |
| 2498 |
Result[0, 0] := 1; |
| 2499 |
Result[1, 1] := 1; |
| 2500 |
Result[2, 2] := 1; |
| 2501 |
Result[3, 3] := 1; |
| 2502 |
end; |
| 2503 |
|
| 2504 |
end. |