Debian Codegraph @

src/base/castlevectors_cardinal.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 Cardinal components. }
18
19 {$ifdef read_interface}
20
21 type
22 { Vector of 2 Cardinal values.
23 @seealso TVector3Cardinal }
24 TVector2Cardinal = record
25 public
26 type
27 TIndex = 0..1;
28 strict private
29 function GetItems(const Index: TIndex): Cardinal; inline;
30 procedure SetItems(const Index: TIndex; const Value: Cardinal); 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]: Cardinal read GetItems; default;
41
42 { @exclude
43 Access (get, set) vector components by index. }
44 property InternalData [const Index: TIndex]: Cardinal read GetItems write SetItems;
45 {$ifdef FPC}deprecated 'use Data or X, Y';{$endif}
46 { @exclude
47 Access (get, set) vector components by index. }
48 property Items [const Index: TIndex]: Cardinal read GetItems write SetItems;
49 {$ifdef FPC}deprecated 'use Data or X, Y';{$endif}
50
51 class operator {$ifdef FPC}+{$else}Add{$endif} (const A, B: TVector2Cardinal): TVector2Cardinal; inline;
52 class operator {$ifdef FPC}-{$else}Subtract{$endif} (const A, B: TVector2Cardinal): TVector2Cardinal; inline;
53 class operator {$ifdef FPC}-{$else}Negative{$endif} (const V: TVector2Cardinal): TVector2Cardinal; inline;
54
55 function ToString: string;
56
57 function IsZero: boolean; inline;
58 class function Equals(const V1, V2: TVector2Cardinal): boolean; static; inline;
59
60 class function Zero: TVector2Cardinal; static; inline;
61
62 case Integer of
63 0: (X, Y: Cardinal);
64 1: (Data: array [TIndex] of Cardinal);
65 end;
66
67 { Vector of 3 Cardinal values. }
68 TVector3Cardinal = record
69 public
70 type
71 TIndex = 0..2;
72 strict private
73 function GetItems(const Index: TIndex): Cardinal; inline;
74 procedure SetItems(const Index: TIndex; const Value: Cardinal); 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]: Cardinal read GetItems; default;
85
86 { @exclude
87 Access (get, set) vector components by index. }
88 property InternalData [const Index: TIndex]: Cardinal 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]: Cardinal 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: TVector3Cardinal): TVector3Cardinal; inline;
96 class operator {$ifdef FPC}-{$else}Subtract{$endif} (const A, B: TVector3Cardinal): TVector3Cardinal; inline;
97 class operator {$ifdef FPC}-{$else}Negative{$endif} (const V: TVector3Cardinal): TVector3Cardinal; inline;
98
99 function ToString: string;
100
101 function IsZero: boolean; inline;
102 class function Equals(const V1, V2: TVector3Cardinal): boolean; static; inline;
103
104 class function Zero: TVector3Cardinal; static; inline;
105
106 case Integer of
107 0: (X, Y, Z: Cardinal);
108 1: (Data: array [TIndex] of Cardinal);
109 2: (XY: TVector2Cardinal);
110 end;
111
112 { Vector of 4 Cardinal values. }
113 TVector4Cardinal = record
114 public
115 type
116 TIndex = 0..3;
117 strict private
118 function GetItems(const Index: TIndex): Cardinal; inline;
119 procedure SetItems(const Index: TIndex; const Value: Cardinal); 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]: Cardinal read GetItems; default;
130
131 { @exclude
132 Access (get, set) vector components by index. }
133 property InternalData [const Index: TIndex]: Cardinal read GetItems write SetItems;
134 { @exclude
135 Access (get, set) vector components by index. }
136 property Items [const Index: TIndex]: Cardinal read GetItems write SetItems;
137 {$ifdef FPC}deprecated 'use instead X, Y, Z, W fields';{$endif}
138
139 class operator {$ifdef FPC}+{$else}Add{$endif} (const A, B: TVector4Cardinal): TVector4Cardinal; inline;
140 class operator {$ifdef FPC}-{$else}Subtract{$endif} (const A, B: TVector4Cardinal): TVector4Cardinal; inline;
141 class operator {$ifdef FPC}-{$else}Negative{$endif} (const V: TVector4Cardinal): TVector4Cardinal; inline;
142
143 function ToString: string;
144
145 function IsZero: boolean; inline;
146 class function Equals(const V1, V2: TVector4Cardinal): boolean; static; inline;
147
148 class function Zero: TVector4Cardinal; static; inline;
149
150 case Integer of
151 0: (X, Y, Z, W: Cardinal);
152 1: (Data: array [TIndex] of Cardinal);
153 2: (XY: TVector2Cardinal; ZW: Cardinal);
154 3: (XYZ: TVector3Cardinal);
155 end;
156
157 PVector2Cardinal = ^TVector2Cardinal;
158 PVector3Cardinal = ^TVector3Cardinal;
159 PVector4Cardinal = ^TVector4Cardinal;
160
161 TVector2CardinalArray = packed array [0..MaxInt div SizeOf(TVector2Cardinal)-1] of TVector2Cardinal;
162 PVector2CardinalArray = ^TVector2CardinalArray;
163 TVector3CardinalArray = packed array [0..MaxInt div SizeOf(TVector3Cardinal)-1] of TVector3Cardinal;
164 PVector3CardinalArray = ^TVector3CardinalArray;
165 TVector4CardinalArray = packed array [0..MaxInt div SizeOf(TVector4Cardinal)-1] of TVector4Cardinal;
166 PVector4CardinalArray = ^TVector4CardinalArray;
167
168 function Vector2Cardinal(const X, Y: Cardinal): TVector2Cardinal; inline;
169 function Vector3Cardinal(const X, Y, Z: Cardinal): TVector3Cardinal; inline;
170 function Vector4Cardinal(const X, Y, Z, W: Cardinal): TVector4Cardinal; inline;
171
172 {$endif read_interface}
173
174 {$ifdef read_implementation}
175
176 { TVector2Cardinal ------------------------------------------------------------ }
177
178 class operator TVector2Cardinal. {$ifdef FPC}+{$else}Add{$endif} (const A, B: TVector2Cardinal): TVector2Cardinal;
179 begin
180 Result.X := A.X + B.X;
181 Result.Y := A.Y + B.Y;
182 end;
183
184 class operator TVector2Cardinal. {$ifdef FPC}-{$else}Subtract{$endif} (const A, B: TVector2Cardinal): TVector2Cardinal;
185 begin
186 Result.X := A.X - B.X;
187 Result.Y := A.Y - B.Y;
188 end;
189
190 class operator TVector2Cardinal. {$ifdef FPC}-{$else}Negative{$endif} (const V: TVector2Cardinal): TVector2Cardinal;
191 begin
192 Result.X := - V.X;
193 Result.Y := - V.Y;
194 end;
195
196 function TVector2Cardinal.ToString: string;
197 begin
198 Result := Format('%d %d', [X, Y]);
199 end;
200
201 function TVector2Cardinal.GetItems(const Index: TIndex): Cardinal;
202 begin
203 case Index of
204 0: Result := X;
205 1: Result := Y;
206 {$ifndef FPC} else raise EInternalError.Create(2022021119); {$endif}
207 end;
208 end;
209
210 procedure TVector2Cardinal.SetItems(const Index: TIndex; const Value: Cardinal);
211 begin
212 case Index of
213 0: X := Value;
214 1: Y := Value;
215 {$ifndef FPC} else raise EInternalError.Create(2022021120); {$endif}
216 end;
217 end;
218
219 function TVector2Cardinal.IsZero: boolean;
220 begin
221 Result :=
222 (X = 0) and
223 (Y = 0);
224 end;
225
226 class function TVector2Cardinal.Equals(const V1, V2: TVector2Cardinal): boolean;
227 begin
228 Result := (V1.X = V2.X) and
229 (V1.Y = V2.Y);
230 end;
231
232 class function TVector2Cardinal.Zero: TVector2Cardinal;
233 begin
234 FillChar(Result, SizeOf(Result), 0);
235 end;
236
237 { TVector3Cardinal ------------------------------------------------------------ }
238
239 class operator TVector3Cardinal. {$ifdef FPC}+{$else}Add{$endif} (const A, B: TVector3Cardinal): TVector3Cardinal;
240 begin
241 Result.X := A.X + B.X;
242 Result.Y := A.Y + B.Y;
243 Result.Z := A.Z + B.Z;
244 end;
245
246 class operator TVector3Cardinal. {$ifdef FPC}-{$else}Subtract{$endif} (const A, B: TVector3Cardinal): TVector3Cardinal;
247 begin
248 Result.X := A.X - B.X;
249 Result.Y := A.Y - B.Y;
250 Result.Z := A.Z - B.Z;
251 end;
252
253 class operator TVector3Cardinal. {$ifdef FPC}-{$else}Negative{$endif} (const V: TVector3Cardinal): TVector3Cardinal;
254 begin
255 Result.X := - V.X;
256 Result.Y := - V.Y;
257 Result.Z := - V.Z;
258 end;
259
260 function TVector3Cardinal.ToString: string;
261 begin
262 Result := Format('%d %d %d', [X, Y, Z]);
263 end;
264
265 function TVector3Cardinal.GetItems(const Index: TIndex): Cardinal;
266 begin
267 case Index of
268 0: Result := X;
269 1: Result := Y;
270 2: Result := Z;
271 {$ifndef FPC} else raise EInternalError.Create(2022021121); {$endif}
272 end;
273 end;
274
275 procedure TVector3Cardinal.SetItems(const Index: TIndex; const Value: Cardinal);
276 begin
277 case Index of
278 0: X := Value;
279 1: Y := Value;
280 2: Z := Value;
281 {$ifndef FPC} else raise EInternalError.Create(2022021122); {$endif}
282 end;
283 end;
284
285 function TVector3Cardinal.IsZero: boolean;
286 begin
287 Result :=
288 (X = 0) and
289 (Y = 0) and
290 (Z = 0);
291 end;
292
293 class function TVector3Cardinal.Equals(const V1, V2: TVector3Cardinal): boolean;
294 begin
295 Result := (V1.X = V2.X) and
296 (V1.Y = V2.Y) and
297 (V1.Z = V2.Z);
298 end;
299
300 class function TVector3Cardinal.Zero: TVector3Cardinal;
301 begin
302 FillChar(Result, SizeOf(Result), 0);
303 end;
304
305 { TVector4Cardinal ------------------------------------------------------------ }
306
307 class operator TVector4Cardinal. {$ifdef FPC}+{$else}Add{$endif} (const A, B: TVector4Cardinal): TVector4Cardinal;
308 begin
309 Result.X := A.X + B.X;
310 Result.Y := A.Y + B.Y;
311 Result.Z := A.Z + B.Z;
312 Result.W := A.W + B.W;
313 end;
314
315 class operator TVector4Cardinal. {$ifdef FPC}-{$else}Subtract{$endif} (const A, B: TVector4Cardinal): TVector4Cardinal;
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 TVector4Cardinal. {$ifdef FPC}-{$else}Negative{$endif} (const V: TVector4Cardinal): TVector4Cardinal;
324 begin
325 Result.X := - V.X;
326 Result.Y := - V.Y;
327 Result.Z := - V.Z;
328 Result.W := - V.W;
329 end;
330
331 function TVector4Cardinal.ToString: string;
332 begin
333 Result := Format('%d %d %d %d', [X, Y, Z, W]);
334 end;
335
336 function TVector4Cardinal.GetItems(const Index: TIndex): Cardinal;
337 begin
338 case Index of
339 0: Result := X;
340 1: Result := Y;
341 2: Result := Z;
342 3: Result := W;
343 {$ifndef FPC} else raise EInternalError.Create(2022021123); {$endif}
344 end;
345 end;
346
347 procedure TVector4Cardinal.SetItems(const Index: TIndex; const Value: Cardinal);
348 begin
349 case Index of
350 0: X := Value;
351 1: Y := Value;
352 2: Z := Value;
353 3: W := Value;
354 {$ifndef FPC} else raise EInternalError.Create(2022021124); {$endif}
355 end;
356 end;
357
358 function TVector4Cardinal.IsZero: boolean;
359 begin
360 Result :=
361 (X = 0) and
362 (Y = 0) and
363 (Z = 0) and
364 (W = 0);
365 end;
366
367 class function TVector4Cardinal.Equals(const V1, V2: TVector4Cardinal): boolean;
368 begin
369 Result := (V1.X = V2.X) and
370 (V1.Y = V2.Y) and
371 (V1.Z = V2.Z) and
372 (V1.W = V2.W);
373 end;
374
375 class function TVector4Cardinal.Zero: TVector4Cardinal;
376 begin
377 FillChar(Result, SizeOf(Result), 0);
378 end;
379
380 { global routines ------------------------------------------------------------ }
381
382 function Vector2Cardinal(const X, Y: Cardinal): TVector2Cardinal;
383 begin
384 Result.X := X;
385 Result.Y := Y;
386 end;
387
388 function Vector3Cardinal(const X, Y, Z: Cardinal): TVector3Cardinal;
389 begin
390 Result.X := X;
391 Result.Y := Y;
392 Result.Z := Z;
393 end;
394
395 function Vector4Cardinal(const X, Y, Z, W: Cardinal): TVector4Cardinal;
396 begin
397 Result.X := X;
398 Result.Y := Y;
399 Result.Z := Z;
400 Result.W := W;
401 end;
402
403 (*
404 Not used now, see TVector*Integer comments for Lerp.
405
406 function Lerp(const A: Single; const V1, V2: TVector2Cardinal): TVector2Cardinal; overload;
407 begin
408 Result.X := Clamped(Round(V1.X + A * (V2.X - V1.X)), 0, High(Cardinal));
409 Result.Y := Clamped(Round(V1.Y + A * (V2.Y - V1.Y)), 0, High(Cardinal));
410 end;
411
412 function Lerp(const A: Single; const V1, V2: TVector3Cardinal): TVector3Cardinal; overload;
413 begin
414 Result.X := Clamped(Round(V1.X + A * (V2.X - V1.X)), 0, High(Cardinal));
415 Result.Y := Clamped(Round(V1.Y + A * (V2.Y - V1.Y)), 0, High(Cardinal));
416 Result.Z := Clamped(Round(V1.Z + A * (V2.Z - V1.Z)), 0, High(Cardinal));
417 end;
418
419 function Lerp(const A: Single; const V1, V2: TVector4Cardinal): TVector4Cardinal; overload;
420 begin
421 Result.X := Clamped(Round(V1.X + A * (V2.X - V1.X)), 0, High(Cardinal));
422 Result.Y := Clamped(Round(V1.Y + A * (V2.Y - V1.Y)), 0, High(Cardinal));
423 Result.Z := Clamped(Round(V1.Z + A * (V2.Z - V1.Z)), 0, High(Cardinal));
424 Result.W := Clamped(Round(V1.W + A * (V2.W - V1.W)), 0, High(Cardinal));
425 end;
426 *)
427
428 {$endif read_implementation}