| 1 |
{%MainUnit castlevectors.pas} |
| 2 |
{ |
| 3 |
Copyright 2017-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 |
(* Vectors based on the Byte type. *) |
| 18 |
|
| 19 |
{$ifdef read_interface} |
| 20 |
|
| 21 |
type |
| 22 |
{ Vector of 2 Byte values. |
| 23 |
@seealso TVector3Byte } |
| 24 |
TVector2Byte = packed record |
| 25 |
public |
| 26 |
type |
| 27 |
TIndex = 0..1; |
| 28 |
strict private |
| 29 |
function GetItems(const Index: TIndex): Byte; inline; |
| 30 |
procedure SetItems(const Index: TIndex; const Value: Byte); inline; |
| 31 |
public |
| 32 |
{ Get vector components by index. |
| 33 |
This is a default property, so you can write @code(MyVector[0]) |
| 34 |
instead of @code(MyVector.Data[0]) or @code(MyVector.AsArray[0]). |
| 35 |
|
| 36 |
But note that this is not writeable (because exposing writeable properties on vectors |
| 37 |
would cause some subtle traps, see https://castle-engine.io/coding_traps ). |
| 38 |
Use @code(MyVector.Data[0] := 123) if you want to set by index, |
| 39 |
or @code(MyVector.X := 123). } |
| 40 |
property AsArray [const Index: TIndex]: Byte read GetItems; default; |
| 41 |
|
| 42 |
{ @exclude |
| 43 |
Access (get, set) vector components by index. } |
| 44 |
property Items [const Index: TIndex]: Byte read GetItems write SetItems; |
| 45 |
{$ifdef FPC}deprecated 'use Data or X, Y';{$endif} |
| 46 |
|
| 47 |
class operator {$ifdef FPC}+{$else}Add{$endif} (const A, B: TVector2Byte): TVector2Byte; inline; |
| 48 |
class operator {$ifdef FPC}-{$else}Subtract{$endif} (const A, B: TVector2Byte): TVector2Byte; inline; |
| 49 |
class operator {$ifdef FPC}-{$else}Negative{$endif} (const V: TVector2Byte): TVector2Byte; inline; |
| 50 |
|
| 51 |
function ToString: string; |
| 52 |
|
| 53 |
function IsZero: boolean; inline; |
| 54 |
class function Equals(const V1, V2: TVector2Byte): boolean; static; inline; |
| 55 |
|
| 56 |
class function Zero: TVector2Byte; static; inline; |
| 57 |
|
| 58 |
case Integer of |
| 59 |
0: (X, Y: Byte); |
| 60 |
1: (Data: array [TIndex] of Byte); |
| 61 |
end; |
| 62 |
|
| 63 |
{ Vector of 3 Byte values. } |
| 64 |
TVector3Byte = packed record |
| 65 |
public |
| 66 |
type |
| 67 |
TIndex = 0..2; |
| 68 |
strict private |
| 69 |
function GetItems(const Index: TIndex): Byte; inline; |
| 70 |
procedure SetItems(const Index: TIndex; const Value: Byte); inline; |
| 71 |
public |
| 72 |
{ Get vector components by index. |
| 73 |
This is a default property, so you can write @code(MyVector[0]) |
| 74 |
instead of @code(MyVector.Data[0]) or @code(MyVector.AsArray[0]). |
| 75 |
|
| 76 |
But note that this is not writeable (because exposing writeable properties on vectors |
| 77 |
would cause some subtle traps, see https://castle-engine.io/coding_traps ). |
| 78 |
Use @code(MyVector.Data[0] := 123) if you want to set by index, |
| 79 |
or @code(MyVector.X := 123). } |
| 80 |
property AsArray [const Index: TIndex]: Byte read GetItems; default; |
| 81 |
|
| 82 |
{ @exclude |
| 83 |
Access (get, set) vector components by index. } |
| 84 |
property Items [const Index: TIndex]: Byte read GetItems write SetItems; |
| 85 |
{$ifdef FPC}deprecated 'use Data or X, Y, Z';{$endif} |
| 86 |
|
| 87 |
class operator {$ifdef FPC}+{$else}Add{$endif} (const A, B: TVector3Byte): TVector3Byte; inline; |
| 88 |
class operator {$ifdef FPC}-{$else}Subtract{$endif} (const A, B: TVector3Byte): TVector3Byte; inline; |
| 89 |
class operator {$ifdef FPC}-{$else}Negative{$endif} (const V: TVector3Byte): TVector3Byte; inline; |
| 90 |
|
| 91 |
function ToString: string; |
| 92 |
|
| 93 |
function IsZero: boolean; inline; |
| 94 |
class function Equals(const V1, V2: TVector3Byte): boolean; static; inline; |
| 95 |
|
| 96 |
class function Zero: TVector3Byte; static; inline; |
| 97 |
|
| 98 |
case Integer of |
| 99 |
0: (X, Y, Z: Byte); |
| 100 |
1: (Data: array [TIndex] of Byte); |
| 101 |
2: (XY: TVector2Byte); |
| 102 |
end; |
| 103 |
|
| 104 |
{ Vector of 4 Byte values. } |
| 105 |
TVector4Byte = packed record |
| 106 |
public |
| 107 |
type |
| 108 |
TIndex = 0..3; |
| 109 |
strict private |
| 110 |
function GetItems(const Index: TIndex): Byte; inline; |
| 111 |
procedure SetItems(const Index: TIndex; const Value: Byte); inline; |
| 112 |
public |
| 113 |
{ Get vector components by index. |
| 114 |
This is a default property, so you can write @code(MyVector[0]) |
| 115 |
instead of @code(MyVector.Data[0]) or @code(MyVector.AsArray[0]). |
| 116 |
|
| 117 |
But note that this is not writeable (because exposing writeable properties on vectors |
| 118 |
would cause some subtle traps, see https://castle-engine.io/coding_traps ). |
| 119 |
Use @code(MyVector.Data[0] := 123) if you want to set by index, |
| 120 |
or @code(MyVector.X := 123). } |
| 121 |
property AsArray [const Index: TIndex]: Byte read GetItems; default; |
| 122 |
|
| 123 |
{ @exclude |
| 124 |
Access (get, set) vector components by index. } |
| 125 |
property Items [const Index: TIndex]: Byte read GetItems write SetItems; |
| 126 |
{$ifdef FPC}deprecated 'use Data or X, Y, Z, W';{$endif} |
| 127 |
|
| 128 |
class operator {$ifdef FPC}+{$else}Add{$endif} (const A, B: TVector4Byte): TVector4Byte; inline; |
| 129 |
class operator {$ifdef FPC}-{$else}Subtract{$endif} (const A, B: TVector4Byte): TVector4Byte; inline; |
| 130 |
class operator {$ifdef FPC}-{$else}Negative{$endif} (const V: TVector4Byte): TVector4Byte; inline; |
| 131 |
|
| 132 |
function ToString: string; |
| 133 |
|
| 134 |
function IsZero: boolean; inline; |
| 135 |
class function Equals(const V1, V2: TVector4Byte): boolean; static; inline; |
| 136 |
|
| 137 |
class function Zero: TVector4Byte; static; |
| 138 |
|
| 139 |
case Integer of |
| 140 |
0: (X, Y, Z, W: Byte); |
| 141 |
1: (Data: array [TIndex] of Byte); |
| 142 |
2: (XY: TVector2Byte; ZW: TVector2Byte); |
| 143 |
3: (XYZ: TVector3Byte); |
| 144 |
end; |
| 145 |
|
| 146 |
PVector2Byte = ^TVector2Byte; |
| 147 |
PVector3Byte = ^TVector3Byte; |
| 148 |
PVector4Byte = ^TVector4Byte; |
| 149 |
|
| 150 |
TVector2ByteArray = packed array [0..MaxInt div SizeOf(TVector2Byte)-1] of TVector2Byte; |
| 151 |
PVector2ByteArray = ^TVector2ByteArray; |
| 152 |
TVector3ByteArray = packed array [0..MaxInt div SizeOf(TVector3Byte)-1] of TVector3Byte; |
| 153 |
PVector3ByteArray = ^TVector3ByteArray; |
| 154 |
TVector4ByteArray = packed array [0..MaxInt div SizeOf(TVector4Byte)-1] of TVector4Byte; |
| 155 |
PVector4ByteArray = ^TVector4ByteArray; |
| 156 |
|
| 157 |
function Vector2Byte(const X, Y: Byte): TVector2Byte; overload; inline; |
| 158 |
function Vector3Byte(const X, Y, Z: Byte): TVector3Byte; overload; inline; |
| 159 |
function Vector4Byte(const X, Y, Z, W: Byte): TVector4Byte; overload; inline; |
| 160 |
|
| 161 |
{ Convert float vectors into byte vectors. |
| 162 |
Each float component is converted such that float 0.0 (or less) |
| 163 |
results in byte 0, float 1.0 (or more) results in byte 255. |
| 164 |
Values between 0.0 and 1.0 are appropriately (linearly) converted |
| 165 |
into the byte range. |
| 166 |
@groupBegin } |
| 167 |
function Vector2Byte(const V: TVector2): TVector2Byte; overload; |
| 168 |
function Vector3Byte(const V: TVector3): TVector3Byte; overload; |
| 169 |
function Vector4Byte(const V: TVector4): TVector4Byte; overload; |
| 170 |
{ @groupEnd } |
| 171 |
|
| 172 |
function Vector2(const V: TVector2Byte): TVector2; overload; |
| 173 |
function Vector3(const V: TVector3Byte): TVector3; overload; |
| 174 |
function Vector4(const V: TVector4Byte): TVector4; overload; |
| 175 |
|
| 176 |
function Lerp(const A: Single; const V1, V2: TVector2Byte): TVector2Byte; overload; inline; |
| 177 |
function Lerp(const A: Single; const V1, V2: TVector3Byte): TVector3Byte; overload; inline; |
| 178 |
function Lerp(const A: Single; const V1, V2: TVector4Byte): TVector4Byte; overload; inline; |
| 179 |
|
| 180 |
{$endif read_interface} |
| 181 |
|
| 182 |
{$ifdef read_implementation} |
| 183 |
|
| 184 |
{ TVector2Byte ------------------------------------------------------------ } |
| 185 |
|
| 186 |
class operator TVector2Byte. {$ifdef FPC}+{$else}Add{$endif} (const A, B: TVector2Byte): TVector2Byte; |
| 187 |
begin |
| 188 |
Result.X := A.X + B.X; |
| 189 |
Result.Y := A.Y + B.Y; |
| 190 |
end; |
| 191 |
|
| 192 |
class operator TVector2Byte. {$ifdef FPC}-{$else}Subtract{$endif} (const A, B: TVector2Byte): TVector2Byte; |
| 193 |
begin |
| 194 |
Result.X := A.X - B.X; |
| 195 |
Result.Y := A.Y - B.Y; |
| 196 |
end; |
| 197 |
|
| 198 |
class operator TVector2Byte. {$ifdef FPC}-{$else}Negative{$endif} (const V: TVector2Byte): TVector2Byte; |
| 199 |
begin |
| 200 |
Result.X := - V.X; |
| 201 |
Result.Y := - V.Y; |
| 202 |
end; |
| 203 |
|
| 204 |
function TVector2Byte.ToString: string; |
| 205 |
begin |
| 206 |
Result := Format('%d %d', [X, Y]); |
| 207 |
end; |
| 208 |
|
| 209 |
function TVector2Byte.GetItems(const Index: TIndex): Byte; |
| 210 |
begin |
| 211 |
case Index of |
| 212 |
0: Result := X; |
| 213 |
1: Result := Y; |
| 214 |
{$ifndef FPC} else raise EInternalError.Create(202202117); {$endif} |
| 215 |
end; |
| 216 |
end; |
| 217 |
|
| 218 |
procedure TVector2Byte.SetItems(const Index: TIndex; const Value: Byte); |
| 219 |
begin |
| 220 |
case Index of |
| 221 |
0: X := Value; |
| 222 |
1: Y := Value; |
| 223 |
{$ifndef FPC} else raise EInternalError.Create(202202118); {$endif} |
| 224 |
end; |
| 225 |
end; |
| 226 |
|
| 227 |
function TVector2Byte.IsZero: boolean; |
| 228 |
begin |
| 229 |
Result := |
| 230 |
(X = 0) and |
| 231 |
(Y = 0); |
| 232 |
end; |
| 233 |
|
| 234 |
class function TVector2Byte.Equals(const V1, V2: TVector2Byte): boolean; |
| 235 |
begin |
| 236 |
Result := (V1.X = V2.X) and |
| 237 |
(V1.Y = V2.Y); |
| 238 |
end; |
| 239 |
|
| 240 |
class function TVector2Byte.Zero: TVector2Byte; |
| 241 |
begin |
| 242 |
FillChar(Result, SizeOf(Result), 0); |
| 243 |
end; |
| 244 |
|
| 245 |
{ TVector3Byte ------------------------------------------------------------ } |
| 246 |
|
| 247 |
class operator TVector3Byte. {$ifdef FPC}+{$else}Add{$endif} (const A, B: TVector3Byte): TVector3Byte; |
| 248 |
begin |
| 249 |
Result.X := A.X + B.X; |
| 250 |
Result.Y := A.Y + B.Y; |
| 251 |
Result.Z := A.Z + B.Z; |
| 252 |
end; |
| 253 |
|
| 254 |
class operator TVector3Byte. {$ifdef FPC}-{$else}Subtract{$endif} (const A, B: TVector3Byte): TVector3Byte; |
| 255 |
begin |
| 256 |
Result.X := A.X - B.X; |
| 257 |
Result.Y := A.Y - B.Y; |
| 258 |
Result.Z := A.Z - B.Z; |
| 259 |
end; |
| 260 |
|
| 261 |
class operator TVector3Byte. {$ifdef FPC}-{$else}Negative{$endif} (const V: TVector3Byte): TVector3Byte; |
| 262 |
begin |
| 263 |
Result.X := - V.X; |
| 264 |
Result.Y := - V.Y; |
| 265 |
Result.Z := - V.Z; |
| 266 |
end; |
| 267 |
|
| 268 |
function TVector3Byte.ToString: string; |
| 269 |
begin |
| 270 |
Result := Format('%d %d %d', [X, Y, Z]); |
| 271 |
end; |
| 272 |
|
| 273 |
function TVector3Byte.GetItems(const Index: TIndex): Byte; |
| 274 |
begin |
| 275 |
case Index of |
| 276 |
0: Result := X; |
| 277 |
1: Result := Y; |
| 278 |
2: Result := Z; |
| 279 |
{$ifndef FPC} else raise EInternalError.Create(202202119); {$endif} |
| 280 |
end; |
| 281 |
end; |
| 282 |
|
| 283 |
procedure TVector3Byte.SetItems(const Index: TIndex; const Value: Byte); |
| 284 |
begin |
| 285 |
case Index of |
| 286 |
0: X := Value; |
| 287 |
1: Y := Value; |
| 288 |
2: Z := Value; |
| 289 |
{$ifndef FPC} else raise EInternalError.Create(2022021110); {$endif} |
| 290 |
end; |
| 291 |
end; |
| 292 |
|
| 293 |
function TVector3Byte.IsZero: boolean; |
| 294 |
begin |
| 295 |
Result := |
| 296 |
(X = 0) and |
| 297 |
(Y = 0) and |
| 298 |
(Z = 0); |
| 299 |
end; |
| 300 |
|
| 301 |
class function TVector3Byte.Equals(const V1, V2: TVector3Byte): boolean; |
| 302 |
begin |
| 303 |
Result := (V1.X = V2.X) and |
| 304 |
(V1.Y = V2.Y) and |
| 305 |
(V1.Z = V2.Z); |
| 306 |
end; |
| 307 |
|
| 308 |
class function TVector3Byte.Zero: TVector3Byte; |
| 309 |
begin |
| 310 |
FillChar(Result, SizeOf(Result), 0); |
| 311 |
end; |
| 312 |
|
| 313 |
{ TVector4Byte ------------------------------------------------------------ } |
| 314 |
|
| 315 |
class operator TVector4Byte. {$ifdef FPC}+{$else}Add{$endif} (const A, B: TVector4Byte): TVector4Byte; |
| 316 |
begin |
| 317 |
Result.X := A.X + B.X; |
| 318 |
Result.Y := A.Y + B.Y; |
| 319 |
Result.Z := A.Z + B.Z; |
| 320 |
Result.W := A.W + B.W; |
| 321 |
end; |
| 322 |
|
| 323 |
class operator TVector4Byte. {$ifdef FPC}-{$else}Subtract{$endif} (const A, B: TVector4Byte): TVector4Byte; |
| 324 |
begin |
| 325 |
Result.X := A.X - B.X; |
| 326 |
Result.Y := A.Y - B.Y; |
| 327 |
Result.Z := A.Z - B.Z; |
| 328 |
Result.W := A.W - B.W; |
| 329 |
end; |
| 330 |
|
| 331 |
class operator TVector4Byte. {$ifdef FPC}-{$else}Negative{$endif} (const V: TVector4Byte): TVector4Byte; |
| 332 |
begin |
| 333 |
Result.X := - V.X; |
| 334 |
Result.Y := - V.Y; |
| 335 |
Result.Z := - V.Z; |
| 336 |
Result.W := - V.W; |
| 337 |
end; |
| 338 |
|
| 339 |
function TVector4Byte.ToString: string; |
| 340 |
begin |
| 341 |
Result := Format('%d %d %d %d', [X, Y, Z, W]); |
| 342 |
end; |
| 343 |
|
| 344 |
function TVector4Byte.GetItems(const Index: TIndex): Byte; |
| 345 |
begin |
| 346 |
case Index of |
| 347 |
0: Result := X; |
| 348 |
1: Result := Y; |
| 349 |
2: Result := Z; |
| 350 |
3: Result := W; |
| 351 |
{$ifndef FPC} else raise EInternalError.Create(2022021111); {$endif} |
| 352 |
end; |
| 353 |
end; |
| 354 |
|
| 355 |
procedure TVector4Byte.SetItems(const Index: TIndex; const Value: Byte); |
| 356 |
begin |
| 357 |
case Index of |
| 358 |
0: X := Value; |
| 359 |
1: Y := Value; |
| 360 |
2: Z := Value; |
| 361 |
3: W := Value; |
| 362 |
{$ifndef FPC} else raise EInternalError.Create(2022021112); {$endif} |
| 363 |
end; |
| 364 |
end; |
| 365 |
|
| 366 |
function TVector4Byte.IsZero: boolean; |
| 367 |
begin |
| 368 |
Result := |
| 369 |
(X = 0) and |
| 370 |
(Y = 0) and |
| 371 |
(Z = 0) and |
| 372 |
(W = 0); |
| 373 |
end; |
| 374 |
|
| 375 |
class function TVector4Byte.Equals(const V1, V2: TVector4Byte): boolean; |
| 376 |
begin |
| 377 |
Result := (V1.X = V2.X) and |
| 378 |
(V1.Y = V2.Y) and |
| 379 |
(V1.Z = V2.Z) and |
| 380 |
(V1.W = V2.W); |
| 381 |
end; |
| 382 |
|
| 383 |
class function TVector4Byte.Zero: TVector4Byte; |
| 384 |
begin |
| 385 |
FillChar(Result, SizeOf(Result), 0); |
| 386 |
end; |
| 387 |
|
| 388 |
{ global routines ------------------------------------------------------------ } |
| 389 |
|
| 390 |
function Vector2Byte(const X, Y: Byte): TVector2Byte; |
| 391 |
begin |
| 392 |
Result.X := X; |
| 393 |
Result.Y := Y; |
| 394 |
end; |
| 395 |
|
| 396 |
function Vector3Byte(const X, Y, Z: Byte): TVector3Byte; |
| 397 |
begin |
| 398 |
Result.X := X; |
| 399 |
Result.Y := Y; |
| 400 |
Result.Z := Z; |
| 401 |
end; |
| 402 |
|
| 403 |
function Vector4Byte(const X, Y, Z, W: Byte): TVector4Byte; |
| 404 |
begin |
| 405 |
Result.X := X; |
| 406 |
Result.Y := Y; |
| 407 |
Result.Z := Z; |
| 408 |
Result.W := W; |
| 409 |
end; |
| 410 |
|
| 411 |
function Vector2Byte(const V: TVector2): TVector2Byte; |
| 412 |
begin |
| 413 |
Result.X := Clamped(Round(V.X * 255), Low(Byte), High(Byte)); |
| 414 |
Result.Y := Clamped(Round(V.Y * 255), Low(Byte), High(Byte)); |
| 415 |
end; |
| 416 |
|
| 417 |
function Vector3Byte(const V: TVector3): TVector3Byte; |
| 418 |
begin |
| 419 |
Result.X := Clamped(Round(V.X * 255), Low(Byte), High(Byte)); |
| 420 |
Result.Y := Clamped(Round(V.Y * 255), Low(Byte), High(Byte)); |
| 421 |
Result.Z := Clamped(Round(V.Z * 255), Low(Byte), High(Byte)); |
| 422 |
end; |
| 423 |
|
| 424 |
function Vector4Byte(const V: TVector4): TVector4Byte; |
| 425 |
begin |
| 426 |
Result.X := Clamped(Round(V.X * 255), Low(Byte), High(Byte)); |
| 427 |
Result.Y := Clamped(Round(V.Y * 255), Low(Byte), High(Byte)); |
| 428 |
Result.Z := Clamped(Round(V.Z * 255), Low(Byte), High(Byte)); |
| 429 |
Result.W := Clamped(Round(V.W * 255), Low(Byte), High(Byte)); |
| 430 |
end; |
| 431 |
|
| 432 |
function Vector2(const V: TVector2Byte): TVector2; |
| 433 |
begin |
| 434 |
Result.X := V.X / 255; |
| 435 |
Result.Y := V.Y / 255; |
| 436 |
end; |
| 437 |
|
| 438 |
function Vector3(const V: TVector3Byte): TVector3; |
| 439 |
begin |
| 440 |
Result.X := V.X / 255; |
| 441 |
Result.Y := V.Y / 255; |
| 442 |
Result.Z := V.Z / 255; |
| 443 |
end; |
| 444 |
|
| 445 |
function Vector4(const V: TVector4Byte): TVector4; |
| 446 |
begin |
| 447 |
Result.X := V.X / 255; |
| 448 |
Result.Y := V.Y / 255; |
| 449 |
Result.Z := V.Z / 255; |
| 450 |
Result.W := V.W / 255; |
| 451 |
end; |
| 452 |
|
| 453 |
function Lerp(const A: Single; const V1, V2: TVector2Byte): TVector2Byte; |
| 454 |
begin |
| 455 |
Result.X := Clamped(Round(V1.X + A * (V2.X - V1.X)), 0, High(Byte)); |
| 456 |
Result.Y := Clamped(Round(V1.Y + A * (V2.Y - V1.Y)), 0, High(Byte)); |
| 457 |
end; |
| 458 |
|
| 459 |
function Lerp(const A: Single; const V1, V2: TVector3Byte): TVector3Byte; |
| 460 |
begin |
| 461 |
Result.X := Clamped(Round(V1.X + A * (V2.X - V1.X)), 0, High(Byte)); |
| 462 |
Result.Y := Clamped(Round(V1.Y + A * (V2.Y - V1.Y)), 0, High(Byte)); |
| 463 |
Result.Z := Clamped(Round(V1.Z + A * (V2.Z - V1.Z)), 0, High(Byte)); |
| 464 |
end; |
| 465 |
|
| 466 |
function Lerp(const A: Single; const V1, V2: TVector4Byte): TVector4Byte; |
| 467 |
begin |
| 468 |
Result.X := Clamped(Round(V1.X + A * (V2.X - V1.X)), 0, High(Byte)); |
| 469 |
Result.Y := Clamped(Round(V1.Y + A * (V2.Y - V1.Y)), 0, High(Byte)); |
| 470 |
Result.Z := Clamped(Round(V1.Z + A * (V2.Z - V1.Z)), 0, High(Byte)); |
| 471 |
Result.W := Clamped(Round(V1.W + A * (V2.W - V1.W)), 0, High(Byte)); |
| 472 |
end; |
| 473 |
|
| 474 |
{$endif read_implementation} |