Debian Codegraph @

src/base/castlevectors_integer.inc

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