| 1 |
{%MainUnit castlevectors.pas} |
| 2 |
{ |
| 3 |
Copyright 2003-2022 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 |
{ CastleVectors lists of vectors and matrices using Double-precision types. } |
| 18 |
|
| 19 |
{$ifdef read_interface} |
| 20 |
|
| 21 |
type |
| 22 |
{ List of @link(TVector2Double). } |
| 23 |
TVector2DoubleList = class({$ifdef FPC}specialize{$endif} TStructList<TVector2Double>) |
| 24 |
public |
| 25 |
function ToVector2: TVector2List; |
| 26 |
|
| 27 |
{ Assign linear interpolation between two other vector arrays. |
| 28 |
@raises EListsDifferentCount If V1 and V2 have different count. } |
| 29 |
procedure AssignLerp(const Fraction: Double; const V1, V2: TVector2DoubleList); |
| 30 |
|
| 31 |
{ Does the SecondValue have equal length and content. |
| 32 |
The values are compared with an Epsilon tolerance, as usual for floating-point |
| 33 |
values. } |
| 34 |
function Equals(SecondValue: TObject): boolean; override; |
| 35 |
|
| 36 |
{ Does the SecondValue have equal length and content. |
| 37 |
The values are compared perfectly, without any tolerance for difference. } |
| 38 |
function PerfectlyEquals(SecondValue: TObject): boolean; |
| 39 |
end; |
| 40 |
|
| 41 |
{ List of @link(TVector3Double). } |
| 42 |
TVector3DoubleList = class({$ifdef FPC}specialize{$endif} TStructList<TVector3Double>) |
| 43 |
public |
| 44 |
function ToVector3: TVector3List; |
| 45 |
|
| 46 |
{ Assign linear interpolation between two other vector arrays. |
| 47 |
@raises EListsDifferentCount If V1 and V2 have different count. } |
| 48 |
procedure AssignLerp(const Fraction: Double; const V1, V2: TVector3DoubleList); |
| 49 |
|
| 50 |
{ Does the SecondValue have equal length and content. |
| 51 |
The values are compared with an Epsilon tolerance, as usual for floating-point |
| 52 |
values. } |
| 53 |
function Equals(SecondValue: TObject): boolean; override; |
| 54 |
|
| 55 |
{ Does the SecondValue have equal length and content. |
| 56 |
The values are compared perfectly, without any tolerance for difference. } |
| 57 |
function PerfectlyEquals(SecondValue: TObject): boolean; |
| 58 |
end; |
| 59 |
|
| 60 |
{ List of @link(TVector4Double). } |
| 61 |
TVector4DoubleList = class({$ifdef FPC}specialize{$endif} TStructList<TVector4Double>) |
| 62 |
public |
| 63 |
function ToVector4: TVector4List; |
| 64 |
|
| 65 |
{ Assign linear interpolation between two other vector arrays. |
| 66 |
@raises EListsDifferentCount If V1 and V2 have different count. } |
| 67 |
procedure AssignLerp(const Fraction: Double; const V1, V2: TVector4DoubleList); |
| 68 |
|
| 69 |
{ Does the SecondValue have equal length and content. |
| 70 |
The values are compared with an Epsilon tolerance, as usual for floating-point |
| 71 |
values. } |
| 72 |
function Equals(SecondValue: TObject): boolean; override; |
| 73 |
|
| 74 |
{ Does the SecondValue have equal length and content. |
| 75 |
The values are compared perfectly, without any tolerance for difference. } |
| 76 |
function PerfectlyEquals(SecondValue: TObject): boolean; |
| 77 |
end; |
| 78 |
|
| 79 |
{ List of @link(TMatrix3Double). } |
| 80 |
TMatrix3DoubleList = class({$ifdef FPC}specialize{$endif} TStructList<TMatrix3Double>) |
| 81 |
public |
| 82 |
function ToMatrix3: TMatrix3List; |
| 83 |
|
| 84 |
{ Assign linear interpolation between two other vector arrays. |
| 85 |
@raises EListsDifferentCount If V1 and V2 have different count. } |
| 86 |
procedure AssignLerp(const Fraction: Double; const V1, V2: TMatrix3DoubleList); |
| 87 |
|
| 88 |
{ Does the SecondValue have equal length and content. |
| 89 |
The values are compared with an Epsilon tolerance, as usual for floating-point |
| 90 |
values. } |
| 91 |
function Equals(SecondValue: TObject): boolean; override; |
| 92 |
|
| 93 |
{ Does the SecondValue have equal length and content. |
| 94 |
The values are compared perfectly, without any tolerance for difference. } |
| 95 |
function PerfectlyEquals(SecondValue: TObject): boolean; |
| 96 |
end; |
| 97 |
|
| 98 |
{ List of @link(TMatrix4Double). } |
| 99 |
TMatrix4DoubleList = class({$ifdef FPC}specialize{$endif} TStructList<TMatrix4Double>) |
| 100 |
public |
| 101 |
function ToMatrix4: TMatrix4List; |
| 102 |
|
| 103 |
{ Assign linear interpolation between two other vector arrays. |
| 104 |
@raises EListsDifferentCount If V1 and V2 have different count. } |
| 105 |
procedure AssignLerp(const Fraction: Double; const V1, V2: TMatrix4DoubleList); |
| 106 |
|
| 107 |
{ Does the SecondValue have equal length and content. |
| 108 |
The values are compared with an Epsilon tolerance, as usual for floating-point |
| 109 |
values. } |
| 110 |
function Equals(SecondValue: TObject): boolean; override; |
| 111 |
|
| 112 |
{ Does the SecondValue have equal length and content. |
| 113 |
The values are compared perfectly, without any tolerance for difference. } |
| 114 |
function PerfectlyEquals(SecondValue: TObject): boolean; |
| 115 |
end; |
| 116 |
|
| 117 |
{$endif read_interface} |
| 118 |
|
| 119 |
{$ifdef read_implementation} |
| 120 |
|
| 121 |
{ TVector2DoubleList ----------------------------------------------------- } |
| 122 |
|
| 123 |
function TVector2DoubleList.ToVector2: TVector2List; |
| 124 |
var |
| 125 |
I: Integer; |
| 126 |
Source: PDouble; |
| 127 |
Dest: PSingle; |
| 128 |
begin |
| 129 |
Result := TVector2List.Create; |
| 130 |
Result.Count := Count; |
| 131 |
Source := PDouble(L); |
| 132 |
Dest := PSingle(Result.L); |
| 133 |
for I := 0 to Count * 2 - 1 do |
| 134 |
begin |
| 135 |
Dest^ := Source^; |
| 136 |
Inc(Source); |
| 137 |
Inc(Dest); |
| 138 |
end; |
| 139 |
end; |
| 140 |
|
| 141 |
procedure TVector2DoubleList.AssignLerp(const Fraction: Double; const V1, V2: TVector2DoubleList); |
| 142 |
var |
| 143 |
I: Integer; |
| 144 |
begin |
| 145 |
if V1.Count <> V2.Count then |
| 146 |
raise EListsDifferentCount.CreateFmt('Different lists length: %d and %d, cannot do AssignLerp on the lists', |
| 147 |
[V1.Count, V2.Count]); |
| 148 |
|
| 149 |
Count := V1.Count; |
| 150 |
for I := 0 to Count - 1 do |
| 151 |
L[I] := Lerp(Fraction, V1.L[I], V2.L[I]); |
| 152 |
end; |
| 153 |
|
| 154 |
function TVector2DoubleList.Equals(SecondValue: TObject): boolean; |
| 155 |
var |
| 156 |
I: Integer; |
| 157 |
begin |
| 158 |
Result := SecondValue is TVector2DoubleList; |
| 159 |
if Result then |
| 160 |
begin |
| 161 |
Result := Count = TVector2DoubleList(SecondValue).Count; |
| 162 |
if Result then |
| 163 |
for I := 0 to Count - 1 do |
| 164 |
if not TVector2Double.Equals(TVector2DoubleList(SecondValue).L[I], L[I]) then |
| 165 |
begin |
| 166 |
Result := false; |
| 167 |
Exit; |
| 168 |
end; |
| 169 |
end; |
| 170 |
end; |
| 171 |
|
| 172 |
function TVector2DoubleList.PerfectlyEquals(SecondValue: TObject): boolean; |
| 173 |
var |
| 174 |
I: Integer; |
| 175 |
begin |
| 176 |
Result := SecondValue is TVector2DoubleList; |
| 177 |
if Result then |
| 178 |
begin |
| 179 |
Result := Count = TVector2DoubleList(SecondValue).Count; |
| 180 |
if Result then |
| 181 |
for I := 0 to Count - 1 do |
| 182 |
if not TVector2Double.PerfectlyEquals(TVector2DoubleList(SecondValue).L[I], L[I]) then |
| 183 |
begin |
| 184 |
Result := false; |
| 185 |
Exit; |
| 186 |
end; |
| 187 |
end; |
| 188 |
end; |
| 189 |
|
| 190 |
{ TVector3DoubleList ----------------------------------------------------- } |
| 191 |
|
| 192 |
function TVector3DoubleList.ToVector3: TVector3List; |
| 193 |
var |
| 194 |
I: Integer; |
| 195 |
Source: PDouble; |
| 196 |
Dest: PSingle; |
| 197 |
begin |
| 198 |
Result := TVector3List.Create; |
| 199 |
Result.Count := Count; |
| 200 |
Source := PDouble(L); |
| 201 |
Dest := PSingle(Result.L); |
| 202 |
for I := 0 to Count * 3 - 1 do |
| 203 |
begin |
| 204 |
Dest^ := Source^; |
| 205 |
Inc(Source); |
| 206 |
Inc(Dest); |
| 207 |
end; |
| 208 |
end; |
| 209 |
|
| 210 |
procedure TVector3DoubleList.AssignLerp(const Fraction: Double; const V1, V2: TVector3DoubleList); |
| 211 |
var |
| 212 |
I: Integer; |
| 213 |
begin |
| 214 |
if V1.Count <> V2.Count then |
| 215 |
raise EListsDifferentCount.CreateFmt('Different lists length: %d and %d, cannot do AssignLerp on the lists', |
| 216 |
[V1.Count, V2.Count]); |
| 217 |
|
| 218 |
Count := V1.Count; |
| 219 |
for I := 0 to Count - 1 do |
| 220 |
L[I] := Lerp(Fraction, V1.L[I], V2.L[I]); |
| 221 |
end; |
| 222 |
|
| 223 |
function TVector3DoubleList.Equals(SecondValue: TObject): boolean; |
| 224 |
var |
| 225 |
I: Integer; |
| 226 |
begin |
| 227 |
Result := SecondValue is TVector3DoubleList; |
| 228 |
if Result then |
| 229 |
begin |
| 230 |
Result := Count = TVector3DoubleList(SecondValue).Count; |
| 231 |
if Result then |
| 232 |
for I := 0 to Count - 1 do |
| 233 |
if not TVector3Double.Equals(TVector3DoubleList(SecondValue).L[I], L[I]) then |
| 234 |
begin |
| 235 |
Result := false; |
| 236 |
Exit; |
| 237 |
end; |
| 238 |
end; |
| 239 |
end; |
| 240 |
|
| 241 |
function TVector3DoubleList.PerfectlyEquals(SecondValue: TObject): boolean; |
| 242 |
var |
| 243 |
I: Integer; |
| 244 |
begin |
| 245 |
Result := SecondValue is TVector3DoubleList; |
| 246 |
if Result then |
| 247 |
begin |
| 248 |
Result := Count = TVector3DoubleList(SecondValue).Count; |
| 249 |
if Result then |
| 250 |
for I := 0 to Count - 1 do |
| 251 |
if not TVector3Double.PerfectlyEquals(TVector3DoubleList(SecondValue).L[I], L[I]) then |
| 252 |
begin |
| 253 |
Result := false; |
| 254 |
Exit; |
| 255 |
end; |
| 256 |
end; |
| 257 |
end; |
| 258 |
|
| 259 |
{ TVector4DoubleList ----------------------------------------------------- } |
| 260 |
|
| 261 |
function TVector4DoubleList.ToVector4: TVector4List; |
| 262 |
var |
| 263 |
I: Integer; |
| 264 |
Source: PDouble; |
| 265 |
Dest: PSingle; |
| 266 |
begin |
| 267 |
Result := TVector4List.Create; |
| 268 |
Result.Count := Count; |
| 269 |
Source := PDouble(L); |
| 270 |
Dest := PSingle(Result.L); |
| 271 |
for I := 0 to Count * 4 - 1 do |
| 272 |
begin |
| 273 |
Dest^ := Source^; |
| 274 |
Inc(Source); |
| 275 |
Inc(Dest); |
| 276 |
end; |
| 277 |
end; |
| 278 |
|
| 279 |
procedure TVector4DoubleList.AssignLerp(const Fraction: Double; const V1, V2: TVector4DoubleList); |
| 280 |
var |
| 281 |
I: Integer; |
| 282 |
begin |
| 283 |
if V1.Count <> V2.Count then |
| 284 |
raise EListsDifferentCount.CreateFmt('Different lists length: %d and %d, cannot do AssignLerp on the lists', |
| 285 |
[V1.Count, V2.Count]); |
| 286 |
|
| 287 |
Count := V1.Count; |
| 288 |
for I := 0 to Count - 1 do |
| 289 |
L[I] := Lerp(Fraction, V1.L[I], V2.L[I]); |
| 290 |
end; |
| 291 |
|
| 292 |
function TVector4DoubleList.Equals(SecondValue: TObject): boolean; |
| 293 |
var |
| 294 |
I: Integer; |
| 295 |
begin |
| 296 |
Result := SecondValue is TVector4DoubleList; |
| 297 |
if Result then |
| 298 |
begin |
| 299 |
Result := Count = TVector4DoubleList(SecondValue).Count; |
| 300 |
if Result then |
| 301 |
for I := 0 to Count - 1 do |
| 302 |
if not TVector4Double.Equals(TVector4DoubleList(SecondValue).L[I], L[I]) then |
| 303 |
begin |
| 304 |
Result := false; |
| 305 |
Exit; |
| 306 |
end; |
| 307 |
end; |
| 308 |
end; |
| 309 |
|
| 310 |
function TVector4DoubleList.PerfectlyEquals(SecondValue: TObject): boolean; |
| 311 |
var |
| 312 |
I: Integer; |
| 313 |
begin |
| 314 |
Result := SecondValue is TVector4DoubleList; |
| 315 |
if Result then |
| 316 |
begin |
| 317 |
Result := Count = TVector4DoubleList(SecondValue).Count; |
| 318 |
if Result then |
| 319 |
for I := 0 to Count - 1 do |
| 320 |
if not TVector4Double.PerfectlyEquals(TVector4DoubleList(SecondValue).L[I], L[I]) then |
| 321 |
begin |
| 322 |
Result := false; |
| 323 |
Exit; |
| 324 |
end; |
| 325 |
end; |
| 326 |
end; |
| 327 |
|
| 328 |
{ TMatrix3DoubleList ----------------------------------------------------- } |
| 329 |
|
| 330 |
function TMatrix3DoubleList.ToMatrix3: TMatrix3List; |
| 331 |
var |
| 332 |
I: Integer; |
| 333 |
Source: PDouble; |
| 334 |
Dest: PSingle; |
| 335 |
begin |
| 336 |
Result := TMatrix3List.Create; |
| 337 |
Result.Count := Count; |
| 338 |
Source := PDouble(L); |
| 339 |
Dest := PSingle(Result.L); |
| 340 |
for I := 0 to Count * 3 * 3 - 1 do |
| 341 |
begin |
| 342 |
Dest^ := Source^; |
| 343 |
Inc(Source); |
| 344 |
Inc(Dest); |
| 345 |
end; |
| 346 |
end; |
| 347 |
|
| 348 |
procedure TMatrix3DoubleList.AssignLerp(const Fraction: Double; const V1, V2: TMatrix3DoubleList); |
| 349 |
var |
| 350 |
I: Integer; |
| 351 |
begin |
| 352 |
if V1.Count <> V2.Count then |
| 353 |
raise EListsDifferentCount.CreateFmt('Different lists length: %d and %d, cannot do AssignLerp on the lists', |
| 354 |
[V1.Count, V2.Count]); |
| 355 |
|
| 356 |
Count := V1.Count; |
| 357 |
for I := 0 to Count - 1 do |
| 358 |
L[I] := Lerp(Fraction, V1.L[I], V2.L[I]); |
| 359 |
end; |
| 360 |
|
| 361 |
function TMatrix3DoubleList.Equals(SecondValue: TObject): boolean; |
| 362 |
var |
| 363 |
I: Integer; |
| 364 |
begin |
| 365 |
Result := SecondValue is TMatrix3DoubleList; |
| 366 |
if Result then |
| 367 |
begin |
| 368 |
Result := Count = TMatrix3DoubleList(SecondValue).Count; |
| 369 |
if Result then |
| 370 |
for I := 0 to Count - 1 do |
| 371 |
if not TMatrix3Double.Equals(TMatrix3DoubleList(SecondValue).L[I], L[I]) then |
| 372 |
begin |
| 373 |
Result := false; |
| 374 |
Exit; |
| 375 |
end; |
| 376 |
end; |
| 377 |
end; |
| 378 |
|
| 379 |
function TMatrix3DoubleList.PerfectlyEquals(SecondValue: TObject): boolean; |
| 380 |
var |
| 381 |
I: Integer; |
| 382 |
begin |
| 383 |
Result := SecondValue is TMatrix3DoubleList; |
| 384 |
if Result then |
| 385 |
begin |
| 386 |
Result := Count = TMatrix3DoubleList(SecondValue).Count; |
| 387 |
if Result then |
| 388 |
for I := 0 to Count - 1 do |
| 389 |
if not TMatrix3Double.PerfectlyEquals(TMatrix3DoubleList(SecondValue).L[I], L[I]) then |
| 390 |
begin |
| 391 |
Result := false; |
| 392 |
Exit; |
| 393 |
end; |
| 394 |
end; |
| 395 |
end; |
| 396 |
|
| 397 |
{ TMatrix4DoubleList ----------------------------------------------------- } |
| 398 |
|
| 399 |
function TMatrix4DoubleList.ToMatrix4: TMatrix4List; |
| 400 |
var |
| 401 |
I: Integer; |
| 402 |
Source: PDouble; |
| 403 |
Dest: PSingle; |
| 404 |
begin |
| 405 |
Result := TMatrix4List.Create; |
| 406 |
Result.Count := Count; |
| 407 |
Source := PDouble(L); |
| 408 |
Dest := PSingle(Result.L); |
| 409 |
for I := 0 to Count * 4 * 4 - 1 do |
| 410 |
begin |
| 411 |
Dest^ := Source^; |
| 412 |
Inc(Source); |
| 413 |
Inc(Dest); |
| 414 |
end; |
| 415 |
end; |
| 416 |
|
| 417 |
procedure TMatrix4DoubleList.AssignLerp(const Fraction: Double; const V1, V2: TMatrix4DoubleList); |
| 418 |
var |
| 419 |
I: Integer; |
| 420 |
begin |
| 421 |
if V1.Count <> V2.Count then |
| 422 |
raise EListsDifferentCount.CreateFmt('Different lists length: %d and %d, cannot do AssignLerp on the lists', |
| 423 |
[V1.Count, V2.Count]); |
| 424 |
|
| 425 |
Count := V1.Count; |
| 426 |
for I := 0 to Count - 1 do |
| 427 |
L[I] := Lerp(Fraction, V1.L[I], V2.L[I]); |
| 428 |
end; |
| 429 |
|
| 430 |
function TMatrix4DoubleList.Equals(SecondValue: TObject): boolean; |
| 431 |
var |
| 432 |
I: Integer; |
| 433 |
begin |
| 434 |
Result := SecondValue is TMatrix4DoubleList; |
| 435 |
if Result then |
| 436 |
begin |
| 437 |
Result := Count = TMatrix4DoubleList(SecondValue).Count; |
| 438 |
if Result then |
| 439 |
for I := 0 to Count - 1 do |
| 440 |
if not TMatrix4Double.Equals(TMatrix4DoubleList(SecondValue).L[I], L[I]) then |
| 441 |
begin |
| 442 |
Result := false; |
| 443 |
Exit; |
| 444 |
end; |
| 445 |
end; |
| 446 |
end; |
| 447 |
|
| 448 |
function TMatrix4DoubleList.PerfectlyEquals(SecondValue: TObject): boolean; |
| 449 |
var |
| 450 |
I: Integer; |
| 451 |
begin |
| 452 |
Result := SecondValue is TMatrix4DoubleList; |
| 453 |
if Result then |
| 454 |
begin |
| 455 |
Result := Count = TMatrix4DoubleList(SecondValue).Count; |
| 456 |
if Result then |
| 457 |
for I := 0 to Count - 1 do |
| 458 |
if not TMatrix4Double.PerfectlyEquals(TMatrix4DoubleList(SecondValue).L[I], L[I]) then |
| 459 |
begin |
| 460 |
Result := false; |
| 461 |
Exit; |
| 462 |
end; |
| 463 |
end; |
| 464 |
end; |
| 465 |
|
| 466 |
{$endif read_implementation} |