Debian Codegraph @

src/base/castlevectors_float.inc

raw
1 {%MainUnit castlevectors.pas}
2 {
3 Copyright 2017-2023 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 { Define Single- and Double-precision vector and matrix types
18 (and their constructors) using advanced records. }
19
20 {$ifdef read_interface}
21
22 type
23 { Vector of 2 floating-point values (Single precision). }
24 TVector2 = CastleVectorsInternalSingle.TGenericVector2; PVector2 = ^TVector2;
25 { Vector of 2 floating-point values (Double precision). }
26 TVector2Double = CastleVectorsInternalDouble.TGenericVector2; PVector2Double = ^TVector2Double;
27
28 { Vector of 3 floating-point values (Single precision). }
29 TVector3 = CastleVectorsInternalSingle.TGenericVector3; PVector3 = ^TVector3;
30 { Vector of 3 floating-point values (Double precision). }
31 TVector3Double = CastleVectorsInternalDouble.TGenericVector3; PVector3Double = ^TVector3Double;
32
33 { Vector of 4 floating-point values (Single precision). }
34 TVector4 = CastleVectorsInternalSingle.TGenericVector4; PVector4 = ^TVector4;
35 { Vector of 4 floating-point values (Double precision). }
36 TVector4Double = CastleVectorsInternalDouble.TGenericVector4; PVector4Double = ^TVector4Double;
37
38 { 2x2 matrix of floating-point values (Single precision). }
39 TMatrix2 = CastleVectorsInternalSingle.TGenericMatrix2; PMatrix2 = ^TMatrix2;
40 { 2x2 matrix of floating-point values (Double precision). }
41 TMatrix2Double = CastleVectorsInternalDouble.TGenericMatrix2; PMatrix2Double = ^TMatrix2Double;
42
43 { 3x3 matrix of floating-point values (Single precision). }
44 TMatrix3 = CastleVectorsInternalSingle.TGenericMatrix3; PMatrix3 = ^TMatrix3;
45 { 3x3 matrix of floating-point values (Double precision). }
46 TMatrix3Double = CastleVectorsInternalDouble.TGenericMatrix3; PMatrix3Double = ^TMatrix3Double;
47
48 { 4x4 matrix of floating-point values (Single precision). }
49 TMatrix4 = CastleVectorsInternalSingle.TGenericMatrix4; PMatrix4 = ^TMatrix4;
50 { 4x4 matrix of floating-point values (Double precision). }
51 TMatrix4Double = CastleVectorsInternalDouble.TGenericMatrix4; PMatrix4Double = ^TMatrix4Double;
52
53 TVector2Array = packed array [0..MaxInt div SizeOf(TVector2) - 1] of TVector2;
54 PVector2Array = ^TVector2Array;
55 TVector3Array = packed array [0..MaxInt div SizeOf(TVector3) - 1] of TVector3;
56 PVector3Array = ^TVector3Array;
57 TVector4Array = packed array [0..MaxInt div SizeOf(TVector4) - 1] of TVector4;
58 PVector4Array = ^TVector4Array;
59
60 { Construct an initialized TVector2 value. }
61 function Vector2(const X, Y: Single): TVector2; overload; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif}
62 function Vector2(const V: TVector2Double): TVector2; overload; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif}
63
64 { Construct an initialized TVector2Double value. }
65 function Vector2Double(const X, Y: Double): TVector2Double; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif}
66
67 { Construct an initialized TVector3 value. }
68 function Vector3(const X, Y, Z: Single): TVector3; overload; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif}
69 function Vector3(const V: TVector2; const Z: Single): TVector3; overload; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif}
70 function Vector3(const V: TVector3Double): TVector3; overload; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif}
71
72 { Construct an initialized TVector3Double value. }
73 function Vector3Double(const X, Y, Z: Double): TVector3Double; overload; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif}
74 function Vector3Double(const V: TVector3): TVector3Double; overload; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif}
75
76 { Construct an initialized TVector4 value. }
77 function Vector4(const X, Y, Z, W: Single): TVector4; overload; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif}
78 function Vector4(const V: TVector3; const W: Single): TVector4; overload; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif}
79 function Vector4(const V: TVector2; const Z, W: Single): TVector4; overload; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif}
80 function Vector4(const V: TVector4Double): TVector4; overload; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif}
81
82 { Construct an initialized TVector4Double value. }
83 function Vector4Double(const X, Y, Z, W: Double): TVector4Double; overload; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif}
84 function Vector4Double(const V: TVector4): TVector4Double; overload; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif}
85 function Vector4Double(const V: TVector3Double; const W: Double): TVector4Double; overload; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif}
86
87 { Convert between Double-precision and Single-precision matrix. }
88 function Matrix3(const V: TMatrix3Double): TMatrix3;
89 function Matrix4(const V: TMatrix4Double): TMatrix4;
90 function Matrix3Double(const V: TMatrix3): TMatrix3Double;
91 function Matrix4Double(const V: TMatrix4): TMatrix4Double;
92
93 { Convert string to vector. Each component is simply parsed by StrToFloatDot,
94 and components must be separated by whitespace (see @link(WhiteSpaces) constant).
95 This is a reverse of
96 @link(CastleVectorsInternalSingle.TGenericVector2.ToString TVector2.ToString),
97 @link(CastleVectorsInternalSingle.TGenericVector3.ToString TVector3.ToString),
98 @link(CastleVectorsInternalSingle.TGenericVector4.ToString TVector4.ToString) methods.
99
100 @raises(EConvertError In case of problems during conversion (invalid float
101 or unexpected string end or expected but missed string end).)
102
103 @groupBegin }
104 function Vector2FromStr(const S: string): TVector2;
105 function Vector3FromStr(const S: string): TVector3;
106 function Vector4FromStr(const S: string): TVector4;
107 { @groupEnd }
108
109 { Linear interpolation between two vector values.
110 Returns @code((1-A) * V1 + A * V2).
111
112 So:
113
114 @unorderedList(
115 @itemSpacing Compact
116 @item A = 0 gives V1,
117 @item A = 1 gives V2,
118 @item values between are interpolated,
119 @item values outside are extrapolated.
120 )
121
122 @groupBegin }
123 function Lerp(const A: Single; const V1, V2: TVector2): TVector2; overload; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif}
124 function Lerp(const A: Single; const V1, V2: TVector3): TVector3; overload; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif}
125 function Lerp(const A: Single; const V1, V2: TVector4): TVector4; overload; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif}
126 function Lerp(const A: Single; const M1, M2: TMatrix2): TMatrix2; overload; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif}
127 function Lerp(const A: Single; const M1, M2: TMatrix3): TMatrix3; overload; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif}
128 function Lerp(const A: Single; const M1, M2: TMatrix4): TMatrix4; overload; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif}
129
130 function Lerp(const A: Double; const V1, V2: TVector2Double): TVector2Double; overload; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif}
131 function Lerp(const A: Double; const V1, V2: TVector3Double): TVector3Double; overload; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif}
132 function Lerp(const A: Double; const V1, V2: TVector4Double): TVector4Double; overload; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif}
133 function Lerp(const A: Double; const M1, M2: TMatrix2Double): TMatrix2Double; overload; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif}
134 function Lerp(const A: Double; const M1, M2: TMatrix3Double): TMatrix3Double; overload; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif}
135 function Lerp(const A: Double; const M1, M2: TMatrix4Double): TMatrix4Double; overload; {$ifdef INLINE_GENERIC_VECTORS}inline;{$endif}
136 { @groupEnd }
137
138 { Try to inverse single-precision matrix using double-precision, if necessary. }
139 function TryInverseHarder(const M: TMatrix4; out MInverse: TMatrix4): boolean;
140
141 {$endif read_interface}
142
143 {$ifdef read_implementation}
144
145 { Construct 2D vectors ------------------------------------------------------------ }
146
147 function Vector2(const X, Y: Single): TVector2;
148 begin
149 Result.X := X;
150 Result.Y := Y;
151 end;
152
153 function Vector2(const V: TVector2Double): TVector2;
154 begin
155 Result.X := V.X;
156 Result.Y := V.Y;
157 end;
158
159 function Vector2Double(const X, Y: Double): TVector2Double;
160 begin
161 Result.X := X;
162 Result.Y := Y;
163 end;
164
165 { Construct 3D vectors ------------------------------------------------------------ }
166
167 function Vector3(const X, Y, Z: Single): TVector3;
168 begin
169 Result.X := X;
170 Result.Y := Y;
171 Result.Z := Z;
172 end;
173
174 function Vector3(const V: TVector2; const Z: Single): TVector3;
175 begin
176 Result.X := V.X;
177 Result.Y := V.Y;
178 Result.Z := Z;
179 end;
180
181 function Vector3(const V: TVector3Double): TVector3;
182 begin
183 Result.X := V.X;
184 Result.Y := V.Y;
185 Result.Z := V.Z;
186 end;
187
188 function Vector3Double(const X, Y, Z: Double): TVector3Double;
189 begin
190 Result.X := X;
191 Result.Y := Y;
192 Result.Z := Z;
193 end;
194
195 function Vector3Double(const V: TVector3): TVector3Double;
196 begin
197 Result.X := V.X;
198 Result.Y := V.Y;
199 Result.Z := V.Z;
200 end;
201
202 { Construct 4D vectors ------------------------------------------------------------ }
203
204 function Vector4(const X, Y, Z, W: Single): TVector4;
205 begin
206 Result.X := X;
207 Result.Y := Y;
208 Result.Z := Z;
209 Result.W := W;
210 end;
211
212 function Vector4(const V: TVector3; const W: Single): TVector4;
213 begin
214 Result.X := V.X;
215 Result.Y := V.Y;
216 Result.Z := V.Z;
217 Result.W := W;
218 end;
219
220 function Vector4(const V: TVector2; const Z, W: Single): TVector4;
221 begin
222 Result.X := V.X;
223 Result.Y := V.Y;
224 Result.Z := Z;
225 Result.W := W;
226 end;
227
228 function Vector4(const V: TVector4Double): TVector4;
229 begin
230 Result.X := V.X;
231 Result.Y := V.Y;
232 Result.Z := V.Z;
233 Result.W := V.W;
234 end;
235
236 function Vector4Double(const X, Y, Z, W: Double): TVector4Double;
237 begin
238 Result.X := X;
239 Result.Y := Y;
240 Result.Z := Z;
241 Result.W := W;
242 end;
243
244 function Vector4Double(const V: TVector4): TVector4Double;
245 begin
246 Result.X := V.X;
247 Result.Y := V.Y;
248 Result.Z := V.Z;
249 Result.W := V.W;
250 end;
251
252 function Vector4Double(const V: TVector3Double; const W: Double): TVector4Double;
253 begin
254 Result.X := V.X;
255 Result.Y := V.Y;
256 Result.Z := V.Z;
257 Result.W := W;
258 end;
259
260 { Convert matrix ------------------------------------------------------------- }
261
262 function Matrix3(const V: TMatrix3Double): TMatrix3;
263 var
264 I, J: Integer;
265 begin
266 for I := 0 to 2 do
267 for J := 0 to 2 do
268 Result.Data[I, J] := V.Data[I, J];
269 end;
270
271 function Matrix4(const V: TMatrix4Double): TMatrix4;
272 var
273 I, J: Integer;
274 begin
275 for I := 0 to 3 do
276 for J := 0 to 3 do
277 Result.Data[I, J] := V.Data[I, J];
278 end;
279
280 function Matrix3Double(const V: TMatrix3): TMatrix3Double;
281 var
282 I, J: Integer;
283 begin
284 for I := 0 to 2 do
285 for J := 0 to 2 do
286 Result.Data[I, J] := V.Data[I, J];
287 end;
288
289 function Matrix4Double(const V: TMatrix4): TMatrix4Double;
290 var
291 I, J: Integer;
292 begin
293 for I := 0 to 3 do
294 for J := 0 to 3 do
295 Result.Data[I, J] := V.Data[I, J];
296 end;
297
298 { Convert from string -------------------------------------------------------- }
299
300 function Vector2FromStr(const S: string): TVector2;
301 var
302 SPosition: Integer;
303 begin
304 SPosition := 1;
305 Result.X := StrToFloatDot(NextToken(S, SPosition));
306 Result.Y := StrToFloatDot(NextToken(S, SPosition));
307 if NextToken(S, SPosition) <> '' then
308 raise EConvertError.Create('Expected end of data when reading vector from string');
309 end;
310
311 function Vector3FromStr(const S: string): TVector3;
312 var
313 SPosition: Integer;
314 begin
315 SPosition := 1;
316 Result.X := StrToFloatDot(NextToken(S, SPosition));
317 Result.Y := StrToFloatDot(NextToken(S, SPosition));
318 Result.Z := StrToFloatDot(NextToken(S, SPosition));
319 if NextToken(S, SPosition) <> '' then
320 raise EConvertError.Create('Expected end of data when reading vector from string');
321
322 { This could also be implemented by DeFormat, but this is faster
323 (which is important for e.g. reading 3D data where this function may be used
324 often) }
325 end;
326
327 function Vector4FromStr(const S: string): TVector4;
328 var
329 SPosition: Integer;
330 begin
331 SPosition := 1;
332 Result.X := StrToFloatDot(NextToken(S, SPosition));
333 Result.Y := StrToFloatDot(NextToken(S, SPosition));
334 Result.Z := StrToFloatDot(NextToken(S, SPosition));
335 Result.W := StrToFloatDot(NextToken(S, SPosition));
336 if NextToken(S, SPosition) <> '' then
337 raise EConvertError.Create('Expected end of data when reading vector from string');
338 end;
339
340 { Lerp ----------------------------------------------------------------------- }
341
342 function Lerp(const A: Single; const V1, V2: TVector2): TVector2;
343 begin
344 Result := TVector2.Lerp(A, V1, V2);
345 end;
346
347 function Lerp(const A: Single; const V1, V2: TVector3): TVector3;
348 begin
349 Result := TVector3.Lerp(A, V1, V2);
350 end;
351
352 function Lerp(const A: Single; const V1, V2: TVector4): TVector4;
353 begin
354 Result := TVector4.Lerp(A, V1, V2);
355 end;
356
357 function Lerp(const A: Single; const M1, M2: TMatrix2): TMatrix2;
358 begin
359 Result := TMatrix2.Lerp(A, M1, M2);
360 end;
361
362 function Lerp(const A: Single; const M1, M2: TMatrix3): TMatrix3;
363 begin
364 Result := TMatrix3.Lerp(A, M1, M2);
365 end;
366
367 function Lerp(const A: Single; const M1, M2: TMatrix4): TMatrix4;
368 begin
369 Result := TMatrix4.Lerp(A, M1, M2);
370 end;
371
372 function Lerp(const A: Double; const V1, V2: TVector2Double): TVector2Double;
373 begin
374 Result := TVector2Double.Lerp(A, V1, V2);
375 end;
376
377 function Lerp(const A: Double; const V1, V2: TVector3Double): TVector3Double;
378 begin
379 Result := TVector3Double.Lerp(A, V1, V2);
380 end;
381
382 function Lerp(const A: Double; const V1, V2: TVector4Double): TVector4Double;
383 begin
384 Result := TVector4Double.Lerp(A, V1, V2);
385 end;
386
387 function Lerp(const A: Double; const M1, M2: TMatrix2Double): TMatrix2Double;
388 begin
389 Result := TMatrix2Double.Lerp(A, M1, M2);
390 end;
391
392 function Lerp(const A: Double; const M1, M2: TMatrix3Double): TMatrix3Double;
393 begin
394 Result := TMatrix3Double.Lerp(A, M1, M2);
395 end;
396
397 function Lerp(const A: Double; const M1, M2: TMatrix4Double): TMatrix4Double;
398 begin
399 Result := TMatrix4Double.Lerp(A, M1, M2);
400 end;
401
402 { TryInverseHarder ----------------------------------------------------------- }
403
404 function TryInverseHarder(const M: TMatrix4; out MInverse: TMatrix4): boolean;
405 var
406 MInverseDouble: TMatrix4Double;
407 begin
408 Result := M.TryInverse(MInverse);
409 if not Result then
410 begin
411 Result := Matrix4Double(M).TryInverse(MInverseDouble);
412 if Result then
413 begin
414 // WritelnLogMultiline('TryInverseHarder', 'Failed to inverse matrix in Single-precision, but managed to inverse in Double-precision.' + NL +
415 // M.ToRawString);
416 MInverse := Matrix4(MInverseDouble);
417 end;
418 end;
419 end;
420
421 {$endif read_implementation}