Debian Codegraph @

src/base/castleutils_math.inc

raw
1 {%MainUnit castleutils.pas}
2 {
3 Copyright 2001-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 { Basic operations on numbers. }
18
19 {$ifdef read_interface}
20
21 { Swap variables values.
22 @groupBegin }
23 procedure SwapValues(var a, b: Byte ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
24 procedure SwapValues(var a, b: Int64 ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
25 procedure SwapValues(var a, b: QWord ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
26 procedure SwapValues(var a, b: Int32 ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
27 procedure SwapValues(var a, b: UInt32 ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
28 procedure SwapValues(var a, b: Single ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
29 procedure SwapValues(var a, b: Double ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
30 {$ifdef EXTENDED_OVERLOADS}
31 procedure SwapValues(var a, b: Extended); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
32 {$endif}
33 procedure SwapValues(var a, b: char ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
34 procedure SwapValues(var a, b: Pointer ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
35 { @groupEnd }
36
37 { Make sure the Smaller value is <= than the Larger value,
38 by eventually swapping them.
39 @groupBegin }
40 procedure OrderUp(var Smaller, Larger: Int64 ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
41 procedure OrderUp(var Smaller, Larger: Int32 ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
42 procedure OrderUp(var Smaller, Larger: UInt32 ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
43 procedure OrderUp(var Smaller, Larger: Single ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
44 procedure OrderUp(var Smaller, Larger: Double ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
45 {$ifdef EXTENDED_OVERLOADS}
46 procedure OrderUp(var Smaller, Larger: Extended); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
47 {$endif}
48 { @groupEnd }
49
50 { Assign the smaller value from X, Y to Smaller variable, the other one
51 to Larger variable.
52 @groupBegin }
53 procedure OrderUp(x, y: Int32 ; var Smaller, Larger: Int32 ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
54 procedure OrderUp(x, y: UInt32; var Smaller, Larger: UInt32 ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
55 procedure OrderUp(x, y: Single; var Smaller, Larger: Single ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
56 procedure OrderUp(x, y: Double; var Smaller, Larger: Double ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
57 {$ifdef EXTENDED_OVERLOADS}
58 procedure OrderUp(x, y: Extended; var Smaller, Larger: Extended); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
59 {$endif}
60 { @groupEnd }
61
62 { Update value of A to be a minimum of A, B.
63 Works like @code(A := Min(A, B)), but is marginally faster,
64 since you don't have to do anything when A is already smaller.
65 @groupBegin }
66 procedure MinVar(var a: Word ; const b: Word ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
67 procedure MinVar(var a: Int64 ; const b: Int64 ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
68 procedure MinVar(var a: Int32 ; const b: Int32 ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
69 procedure MinVar(var a: UInt32 ; const b: UInt32 ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
70 procedure MinVar(var a: Single ; const b: Single ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
71 procedure MinVar(var a: Double ; const b: Double ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
72 {$ifdef EXTENDED_OVERLOADS}
73 procedure MinVar(var a: Extended; const b: Extended); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
74 {$endif}
75 { @groupEnd }
76
77 { Update value of A to be a maximum of A, B.
78 Works like @code(A := Max(A, B)), but is marginally faster,
79 since you don't have to do anything when A is already larger.
80 @groupBegin }
81 procedure MaxVar(var a: Word ; const b: Word ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
82 procedure MaxVar(var a: Int64 ; const b: Int64 ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
83 procedure MaxVar(var a: Int32 ; const b: Int32 ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
84 procedure MaxVar(var a: UInt32 ; const b: UInt32 ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
85 procedure MaxVar(var a: Single ; const b: Single ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
86 procedure MaxVar(var a: Double ; const b: Double ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
87 {$ifdef EXTENDED_OVERLOADS}
88 procedure MaxVar(var a: Extended; const b: Extended); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
89 {$endif}
90 { @groupEnd }
91
92 type
93 { Index of axis in 3D. }
94 T3DAxis = 0..2;
95
96 { Index (0, 1 or 2) of maximum / minimum of 3 numbers.
97 @groupBegin }
98 function IndexMax(const a0, a1, a2: Double): T3DAxis; overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
99 function IndexMin(const a0, a1, a2: Double): T3DAxis; overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
100 { @groupEnd }
101
102 function Between(const a, vBegin, vEnd: Int64 ): boolean; overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
103 function Between(const a, vBegin, vEnd: Int32 ): boolean; overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
104 function Between(const a, vBegin, vEnd: UInt32 ): boolean; overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
105 function Between(const a, vBegin, vEnd: Float ): boolean; overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
106 function Between(const a, vBegin, vEnd: Char ): boolean; overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
107
108 function RoundClamp255(const A: Single): Byte; {$ifdef SUPPORTS_INLINE} inline; {$endif}
109
110 function Clamped(const a, vBegin, vEnd: Int64 ): Int64 ; overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
111 function Clamped(const a, vBegin, vEnd: QWord ): QWord ; overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
112 function Clamped(const a, vBegin, vEnd: Int32 ): Int32 ; overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
113 function Clamped(const a, vBegin, vEnd: UInt32 ): UInt32 ; overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
114 function Clamped(const a, vBegin, vEnd: Single ): Single ; overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
115 function Clamped(const a, vBegin, vEnd: Double ): Double ; overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
116 {$ifdef EXTENDED_OVERLOADS}
117 function Clamped(const a, vBegin, vEnd: Extended): Extended; overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
118 {$endif}
119
120 procedure ClampVar(var a: Int64 ; const vBegin, vEnd: Int64 ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
121 procedure ClampVar(var a: QWord ; const vBegin, vEnd: QWord ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
122 procedure ClampVar(var a: Int32 ; const vBegin, vEnd: Int32 ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
123 procedure ClampVar(var a: UInt32 ; const vBegin, vEnd: UInt32 ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
124 procedure ClampVar(var a: Single ; const vBegin, vEnd: Single ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
125 procedure ClampVar(var a: Double ; const vBegin, vEnd: Double ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
126 {$ifdef EXTENDED_OVERLOADS}
127 procedure ClampVar(var a: Extended; const vBegin, vEnd: Extended); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
128 {$endif}
129
130 procedure Clamp(var a: Int64 ; const vBegin, vEnd: Int64 ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif} deprecated 'Use ClampVar';
131 procedure Clamp(var a: Int32 ; const vBegin, vEnd: Int32 ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif} deprecated 'Use ClampVar';
132 procedure Clamp(var a: UInt32 ; const vBegin, vEnd: UInt32 ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif} deprecated 'Use ClampVar';
133 procedure Clamp(var a: Single ; const vBegin, vEnd: Single ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif} deprecated 'Use ClampVar';
134 procedure Clamp(var a: Double ; const vBegin, vEnd: Double ); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif} deprecated 'Use ClampVar';
135 {$ifdef EXTENDED_OVERLOADS}
136 procedure Clamp(var a: Extended; const vBegin, vEnd: Extended); overload; {$ifdef SUPPORTS_INLINE} inline; {$endif} deprecated 'Use ClampVar';
137 {$endif}
138
139 { Remaining 3D coordinates (0, 1 or 2) except the given coordinate, in the ascending order.
140
141 E.g. Coord = 0, then it sets First = 1 and Second = 2.
142
143 The coordinates are in ascending order,
144 so e.g. for Coord = 1 we return First = 0, Second = 2. }
145 procedure RestOf3DCoords(const Coord: T3DAxis; out First, Second: T3DAxis);
146
147 { Remaining 3D coordinates (0, 1 or 2) except the given coordinate, in order to cycle.
148
149 E.g. Coord = 0, then it sets First = 1 and Second = 2.
150
151 The coordinates are in order to cycle, so First = (Coord + 1) mod 3,
152 Second = (Coord + 2) mod 3.
153 E.g. for Coord = 1 we return First = 2, Second = 0. }
154 procedure RestOf3DCoordsCycle(const Coord: T3DAxis; out First, Second: T3DAxis);
155
156 { Increase Value by Change, nicely wrapping in [0..MaxValue], accepting
157 also negative Change. The final value is always in the [0..MaxValue] range,
158 even when initial Value was outside. }
159 function ChangeIntCycle(Value, Change, MaxValue: Integer): Integer;
160
161 { Linear interpolation between two values. Returns @code((1-A)*L + A*H).
162 @groupBegin }
163 function Lerp(const a: Single; const l, h: Int32 ): Single; overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
164 function Lerp(const a: Single; const l, h: UInt32 ): Single; overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
165 function Lerp(const a, l, h: Single): Single; overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
166 function Lerp(const a: Double; const l, h: Int32 ): Double; overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
167 function Lerp(const a: Double; const l, h: UInt32 ): Double; overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
168 function Lerp(const a, l, h: Double): Double; overload; {$ifdef SUPPORTS_INLINE} inline; {$endif}
169 { @groupEnd }
170
171 { Linear interpolation between 2 angle values, in radians.
172 Goes from one value to the other through a shortest path.
173 Values are interpreted "modulo 2 * Pi",
174 i.e. there's no difference between Angle1 = 0.1,
175 Angle1 = 2 * Pi + 0.1, Angle1 = -2 * Pi + 0.1 etc. }
176 function AngleLerp(T, Angle1, Angle2: Single): Single;
177
178 const
179 ENatural = 2.71828182845905;
180 Sqrt2 = 1.4142135623730950488016887242097;
181 Sqrt3 = 1.7320508075688773;
182 { Pi / 2. }
183 HalfPi = 1.57079632679489661923;
184 { Pi * 2. }
185 Pi2 = 6.28318530717958647703;
186
187 { Smallest multiple of Multiplicator that is still >= Value. }
188 function RoundUpToMultiply(value, multiplicator: Integer): Integer;
189
190 { Largest power of 2 still <= Value.
191 When Value = 0 returns 0. }
192 function BiggestPowerOf2(Value: Cardinal): Cardinal;
193
194 { Exponent of the largest power of 2 that it's still <= Value.
195 Like BiggestPowerOf2, except that this returns which power of 2 it is,
196 while BiggestPowerOf2 just returns exactly this power.
197 Biggest2Exponent(0) = -1. }
198 function Biggest2Exponent(Value: Cardinal): Integer;
199
200 { Smallest exponent such that 2^this exponent is >= Value.
201 Smallest2Exponent(0) = -1 (this is different situation than
202 Smallest2Exponent(1), when we return 0. }
203 function Smallest2Exponent(Value: Cardinal): Integer;
204
205 { Smallest power of 2 that is >= Value.
206 Like Smallest2Exponent, except here we return 2^Smallest2Exponent(Value).
207 Returns 0 when Value = 0. }
208 function Smallest2Power(Value: Cardinal): Cardinal;
209
210 { Check is Value an exact power of 2. }
211 function IsPowerOf2(Value: Cardinal): boolean;
212
213 function DivRoundUp(const Value, Divider: Cardinal): Cardinal; overload;
214 function DivRoundUp(const Value, Divider: Integer): Integer; overload;
215
216 { Linearly map value from a one range to another.
217
218 Consider how SourceVal is placed in the [SourceBegin .. SourceEnd]
219 (it may be outside of this range, it all still works OK).
220 It has some distance to range start (SourceBegin), and some distance
221 to range end (SourceEnd).
222
223 We want to find another number, that is identically placed in another range
224 [DestBegin .. DestEnd].
225
226 @orderedList(
227 @item(Such that @italic(Distance(SourceVal,
228 SourceBegin) / Distance(SourceVal, SourceEnd) =
229 Distance(Result, DestBegin) / Distance(Result, DestEnd)).)
230
231 @item(And such that it's on the same side of the range.
232 So if SourceVal is beyond SourceEnd (that is,
233 @code(SourceBegin < SourceEnd < SourceVal) or
234 @code(SourceBegin > SourceEnd > SourceVal)) then the result
235 must be similarly beyond DestEnd.
236
237 As you can see, this works regardless if one, or both,
238 of the ranges increase (range end is larger than range begin).)
239 )
240
241 MapRangeClamped additionally at the end clamps the result to be within
242 [DestBegin, DestEnd] range (or [DestEnd, DestBegin] range,
243 in case DestEnd < DestBegin).
244
245 @groupBegin }
246 function MapRange(const SourceVal, SourceBegin, SourceEnd, DestBegin, DestEnd: Integer): Float; overload;
247 function MapRange(const SourceVal, SourceBegin, SourceEnd, DestBegin, DestEnd: Int64 ): Float; overload;
248 function MapRange(const SourceVal, SourceBegin, SourceEnd, DestBegin, DestEnd: QWord ): Float; overload;
249 function MapRange(const SourceVal, SourceBegin, SourceEnd, DestBegin, DestEnd: Float ): Float; overload;
250 function MapRangeClamped(const SourceVal, SourceBegin, SourceEnd, DestBegin, DestEnd: Integer): Float; overload;
251 function MapRangeClamped(const SourceVal, SourceBegin, SourceEnd, DestBegin, DestEnd: Int64 ): Float; overload;
252 function MapRangeClamped(const SourceVal, SourceBegin, SourceEnd, DestBegin, DestEnd: QWord ): Float; overload;
253 function MapRangeClamped(const SourceVal, SourceBegin, SourceEnd, DestBegin, DestEnd: Float ): Float; overload;
254 { @groupEnd }
255
256 function MapRangeTo01(const SourceVal, SourceBegin, SourceEnd: Single): Single;
257
258 { Random Float value in the given range,
259 result is between RangeBegin and RangeEnd.
260 Always pass arguments such that RangeBegin < RangeEnd. }
261 function RandomFloatRange(const RangeBegin, RangeEnd: Single): Single; overload;
262 function RandomFloatRange(const RangeBegin, RangeEnd: Float): Float; overload;
263
264 { Random integer value in the given range,
265 result is always >= RangeBegin and < RangeEnd.
266 So @italic(result is never equal to RangeEnd), this is often convenient,
267 and makes @code(RandomIntRange(0, N)) consistent with @code(Random(N)).
268 Always pass arguments such that RangeBegin < RangeEnd. }
269 function RandomIntRange(const RangeBegin, RangeEnd: Integer): Integer; overload;
270 function RandomIntRange(const RangeBegin, RangeEnd: Int64): Int64; overload;
271
272 { Random int value in the given range,
273 result is always >= RangeBegin and <= RangeEnd.
274 Always pass arguments such that RangeBegin <= RangeEnd. }
275 function RandomIntRangeInclusive(const RangeBegin, RangeEnd: Integer): Integer; overload;
276 function RandomIntRangeInclusive(const RangeBegin, RangeEnd: Int64): Int64; overload;
277
278 function RandomBoolean: boolean;
279
280 { Random -1 or +1. }
281 function RandomPlusMinus: Integer;
282
283 { Angle between a 2D line segments and OX axis. Angle 0 means that Y1 = Y2
284 and X2 > X1, then the angle increases as you rotate CCW. In radians. }
285 function AngleRadPointToPoint(x1, y1, x2, y2: Single): Single;
286
287 { Calculate power Base to Exponent, knowing both arguments (and so,
288 also the result) are >= 0. }
289 function NatNatPower(Base, Exponent: Cardinal): Cardinal;
290
291 {$ifdef FPC}
292 { }
293 function ArcCot(x: Float): Float;
294 {$endif FPC}
295
296 { Trivial factorial with Int64 result. Beware, the results very quickly
297 stop to fit inside Int64 range. }
298 function SmallFactorial(n: Integer): Int64;
299
300 { Better DivMod version, in case Dividend may be < 0.
301
302 This fixes lack of DivMod with negative Remainder (for FPC at least
303 older than 2.2.4, see fpc-devel thread "Math.DivMod results should be signed"
304 on 2006-03-21, http://www.mail-archive.com/[email protected]/msg04565.html).
305
306 And workarounds faulty DivMod behavior with FPC 2.4.0,
307 see http://bugs.freepascal.org/view.php?id=15453 }
308 procedure CastleDivMod(Dividend: Integer; Divisor: Word;
309 out Result, Remainder: SmallInt);
310
311 { Like DivMod (return result of Integer division and a remainder),
312 but always return Remainder >= 0.
313
314 This is useful in case when Dividend < 0.
315 Standard DivMod (and Pascal div and mod operators) return then
316 Result rounded toward zero, and Remainder may be < 0.
317 This procedure will return Result rounded toward negative infinity,
318 and Remainder will always be >= 0. }
319 procedure DivUnsignedMod(Dividend: Integer; Divisor: Word;
320 out Result: Smallint; out Remainder: Word);
321
322 { Returns Ceil(A / B), but calculated faster and more precisely
323 (without floating-point help). }
324 function CeilDiv(const A, B: Cardinal): Cardinal; overload;
325 function CeilDiv(const A, B: Int64): Int64; overload;
326
327 { Calculate Integer division and modulo on two Float arguments.
328 Requires that A >= 0 and B > 0, and the Result is always >= 0.
329
330 Tries to secure against floating point imprecision.
331 It's always guaranteed that Remainder >= 0 and <= B (and usually should be
332 < B, but this cannot be guaranteed). }
333 procedure FloatDivMod(const A, B: Double;
334 out DivResult: Int64; out Remainder: Double);
335
336 { Calculate Float modulo of division on two Float arguments.
337 Works for any A (lesser than zero too). Assumes that B > 0.
338
339 Tries to secure against floating point imprecision.
340 It's always guaranteed that Remainder >= 0 and <= B (and usually should be
341 < B, but this cannot be guaranteed). }
342 function FloatModulo(const A, B: Double): Double;
343
344 procedure MinMax(const x0, x1, x2: Double; out min, max: Double); overload;
345 procedure MinMax(const x0, x1, x2: Single; out min, max: Single); overload;
346
347 { Our version of CoTan, to workaround @url(http://www.freepascal.org/mantis/view.php?id=9944 old FPC bug). }
348 function CastleCoTan(const Value: Single): Single;
349
350 { Floor from Sqrt(Value). }
351 function IntSqrt(const Value: Cardinal): Cardinal;
352
353 { Hermite interpolation between two values.
354 For X changing from Edge0 to Edge1, the resulting value changes from 0 to 1,
355 smoothly (not with a linear speed).
356 This is useful to make nicer transition from one value to another,
357 it often looks better than using directly @link(Lerp) or @link(MapRange).
358
359 Just like GLSL smoothstep:
360 http://www.khronos.org/opengles/sdk/docs/manglsl/xhtml/smoothstep.xml }
361 function SmoothStep(const Edge0, Edge1, X: Single): Single;
362
363 { Utility functions to control the endianess.
364 Note: Floating-point versions support Single and Double, but not Extended
365 (which means different things on different platforms/compilers,
366 see https://castle-engine.io/coding_conventions#no_extended ,
367 and in practice binary files don't contain numbers >= Double).
368
369 @groupBegin }
370 function Swap32(Value: UInt32): UInt32;
371 function Swap64(Value: QWord): QWord;
372 function SwapEndian(const Value: Single): Single; overload;
373 function SwapEndian(const Value: Double): Double; overload;
374
375 {$ifndef FPC}
376 function NtoLE(const Value: Word): Word; overload;
377 function NtoLE(const Value: DWord): DWord; overload;
378 function NtoLE(const Value: QWord): QWord; overload;
379 function NtoLE(const Value: Int64): Int64; overload;
380 {$endif}
381
382 function NtoLE(const Value: Single): Single; overload;
383 function NtoLE(const Value: Double): Double; overload;
384
385 {$ifndef FPC}
386 function NtoBE(const Value: Word): Word; overload;
387 function NtoBE(const Value: DWord): DWord; overload;
388 function NtoBE(const Value: QWord): QWord; overload;
389 function NtoBE(const Value: Int64): Int64; overload;
390 {$endif}
391
392 function NtoBE(const Value: Single): Single; overload;
393 function NtoBE(const Value: Double): Double; overload;
394
395 {$ifndef FPC}
396 function LEtoN(const Value: Word): Word; overload;
397 function LEtoN(const Value: DWord): DWord; overload;
398 function LEtoN(const Value: QWord): QWord; overload;
399 function LEtoN(const Value: Int64): Int64; overload;
400 {$endif}
401
402 function LEtoN(const Value: Single): Single; overload;
403 function LEtoN(const Value: Double): Double; overload;
404
405 {$ifndef FPC}
406 function BEtoN(const Value: Word): Word; overload;
407 function BEtoN(const Value: DWord): DWord; overload;
408 function BEtoN(const Value: QWord): QWord; overload;
409 function BEtoN(const Value: Int64): Int64; overload;
410 {$endif}
411 function BEtoN(const Value: Single): Single; overload;
412 function BEtoN(const Value: Double): Double; overload;
413 { @groupEnd }
414
415 { Convert degrees to radians, consistent with CastleScript deg()
416 function and display of rotations in CGE editor.
417 In CGE, all functions take rotations in radians. }
418 function Deg(const Degrees: Float): Float;
419
420 type
421 EVectorInvalidOp = class(Exception);
422 ETransformedResultInvalid = class(EVectorInvalidOp);
423
424 {$endif read_interface}
425
426 {$ifdef read_implementation}
427
428 procedure SwapValues(var a, b: Byte); var Tmp: Byte; begin Tmp := A; A := B; B := Tmp; end;
429 procedure SwapValues(var a, b: Int64); var Tmp: Int64; begin Tmp := A; A := B; B := Tmp; end;
430 procedure SwapValues(var a, b: QWord); var Tmp: QWord; begin Tmp := A; A := B; B := Tmp; end;
431 procedure SwapValues(var a, b: Int32); var Tmp: Int32; begin Tmp := A; A := B; B := Tmp; end;
432 procedure SwapValues(var a, b: UInt32); var Tmp: UInt32; begin Tmp := A; A := B; B := Tmp; end;
433 procedure SwapValues(var a, b: Single); var Tmp: Single; begin Tmp := A; A := B; B := Tmp; end;
434 procedure SwapValues(var a, b: Double); var Tmp: Double; begin Tmp := A; A := B; B := Tmp; end;
435 {$ifdef EXTENDED_OVERLOADS}
436 procedure SwapValues(var a, b: Extended); var Tmp: Extended; begin Tmp := A; A := B; B := Tmp; end;
437 {$endif}
438 procedure SwapValues(var a, b: char); var Tmp: char; begin Tmp := A; A := B; B := Tmp; end;
439 procedure SwapValues(var a, b: Pointer); var Tmp: Pointer; begin Tmp := A; A := B; B := Tmp; end;
440
441 procedure OrderUp(var Smaller, Larger: Int64); begin if Smaller > Larger then SwapValues(Smaller, Larger); end;
442 procedure OrderUp(var Smaller, Larger: Int32); begin if Smaller > Larger then SwapValues(Smaller, Larger); end;
443 procedure OrderUp(var Smaller, Larger: UInt32); begin if Smaller > Larger then SwapValues(Smaller, Larger); end;
444 procedure OrderUp(var Smaller, Larger: Single); begin if Smaller > Larger then SwapValues(Smaller, Larger); end;
445 procedure OrderUp(var Smaller, Larger: Double); begin if Smaller > Larger then SwapValues(Smaller, Larger); end;
446 {$ifdef EXTENDED_OVERLOADS}
447 procedure OrderUp(var Smaller, Larger: Extended); begin if Smaller > Larger then SwapValues(Smaller, Larger); end;
448 {$endif}
449
450 procedure OrderUp(x, y: Int32; var Smaller, Larger: Int32); begin if X <= Y then begin Smaller := X; Larger := Y end else begin Smaller := Y; Larger := X end; end;
451 procedure OrderUp(x, y: UInt32; var Smaller, Larger: UInt32); begin if X <= Y then begin Smaller := X; Larger := Y end else begin Smaller := Y; Larger := X end; end;
452 procedure OrderUp(x, y: Single; var Smaller, Larger: Single); begin if X <= Y then begin Smaller := X; Larger := Y end else begin Smaller := Y; Larger := X end; end;
453 procedure OrderUp(x, y: Double; var Smaller, Larger: Double); begin if X <= Y then begin Smaller := X; Larger := Y end else begin Smaller := Y; Larger := X end; end;
454 {$ifdef EXTENDED_OVERLOADS}
455 procedure OrderUp(x, y: Extended; var Smaller, Larger: Extended); begin if X <= Y then begin Smaller := X; Larger := Y end else begin Smaller := Y; Larger := X end; end;
456 {$endif}
457
458 procedure MinVar(var a: Word ; const b: Word ); begin if B < A then A := B end;
459 procedure MinVar(var a: Int64 ; const b: Int64 ); begin if B < A then A := B end;
460 procedure MinVar(var a: Int32 ; const b: Int32 ); begin if B < A then A := B end;
461 procedure MinVar(var a: UInt32 ; const b: UInt32 ); begin if B < A then A := B end;
462 procedure MinVar(var a: Single ; const b: Single ); begin if B < A then A := B end;
463 procedure MinVar(var a: Double ; const b: Double ); begin if B < A then A := B end;
464 {$ifdef EXTENDED_OVERLOADS}
465 procedure MinVar(var a: Extended; const b: Extended); begin if B < A then A := B end;
466 {$endif}
467
468 procedure MaxVar(var a: Word ; const b: Word ); begin if B > A then A := B end;
469 procedure MaxVar(var a: Int64 ; const b: Int64 ); begin if B > A then A := B end;
470 procedure MaxVar(var a: Int32 ; const b: Int32 ); begin if B > A then A := B end;
471 procedure MaxVar(var a: UInt32; const b: UInt32); begin if B > A then A := B end;
472 procedure MaxVar(var a: Single; const b: Single); begin if B > A then A := B end;
473 procedure MaxVar(var a: Double; const b: Double); begin if B > A then A := B end;
474 {$ifdef EXTENDED_OVERLOADS}
475 procedure MaxVar(var a: Extended; const b: Extended); begin if B > A then A := B end;
476 {$endif}
477
478 function IndexMax(const a0, a1, a2: Double): T3DAxis;
479 begin
480 if a0 > a1 then
481 begin
482 if a2 > a0 then result := 2 else result := 0
483 end else
484 begin
485 if a2 > a1 then result := 2 else result := 1
486 end;
487 end;
488
489 function IndexMin(const a0, a1, a2: Double): T3DAxis;
490 begin
491 if a0 < a1 then
492 begin
493 if a2 < a0 then result := 2 else result := 0
494 end else
495 begin
496 if a2 < a1 then result := 2 else result := 1
497 end;
498 end;
499
500 function Between(const a, vBegin, vEnd: Int64): boolean; begin Result := (vBegin <= a) and (a <= vend) end;
501 function Between(const a, vBegin, vEnd: Int32): boolean; begin Result := (vBegin <= a) and (a <= vend) end;
502 function Between(const a, vBegin, vEnd: cardinal): boolean; begin Result := (vBegin <= a) and (a <= vend) end;
503 function Between(const a, vBegin, vEnd: Float): boolean; begin Result := (vBegin <= a) and (a <= vend) end;
504 function Between(const a, vBegin, vEnd: Char): boolean; begin Result := (vBegin <= a) and (a <= vend) end;
505
506 function RoundClamp255(const A: Single): Byte;
507 var
508 I: Int64;
509 begin
510 I := Round(A);
511 if I < Low(Byte) then
512 Exit(Low(Byte)) else
513 if I > High(Byte) then
514 Exit(High(Byte)) else
515 Exit(I);
516 end;
517
518 function Clamped(const a, vBegin, vEnd: Int64): Int64;
519 begin
520 if a < vBegin then result := vBegin else
521 if a > vEnd then result := vEnd else
522 result := a;
523 end;
524
525 function Clamped(const a, vBegin, vEnd: QWord): QWord;
526 begin
527 if a < vBegin then result := vBegin else
528 if a > vEnd then result := vEnd else
529 result := a;
530 end;
531
532 function Clamped(const a, vBegin, vEnd: Int32): Int32;
533 begin
534 if a < vBegin then result := vBegin else
535 if a > vEnd then result := vEnd else
536 result := a;
537 end;
538
539 function Clamped(const a, vBegin, vEnd: cardinal): cardinal;
540 begin
541 if a < vBegin then result := vBegin else
542 if a > vEnd then result := vEnd else
543 result := a;
544 end;
545
546 function Clamped(const a, vBegin, vEnd: Single): Single;
547 begin
548 if a < vBegin then result := vBegin else
549 if a > vEnd then result := vEnd else
550 result := a;
551 end;
552
553 function Clamped(const a, vBegin, vEnd: Double): Double;
554 begin
555 if a < vBegin then result := vBegin else
556 if a > vEnd then result := vEnd else
557 result := a;
558 end;
559
560 {$ifdef EXTENDED_OVERLOADS}
561 function Clamped(const a, vBegin, vEnd: Extended): Extended;
562 begin
563 if a < vBegin then result := vBegin else
564 if a > vEnd then result := vEnd else
565 result := a;
566 end;
567 {$endif}
568
569 procedure ClampVar(var a: Int64; const vBegin, vEnd: Int64);
570 begin
571 if a < vBegin then a := vBegin else
572 if a > vEnd then a := vEnd;
573 end;
574
575 procedure ClampVar(var a: QWord; const vBegin, vEnd: QWord);
576 begin
577 if a < vBegin then a := vBegin else
578 if a > vEnd then a := vEnd;
579 end;
580
581 procedure ClampVar(var a: Int32; const vBegin, vEnd: Int32);
582 begin
583 if a < vBegin then a := vBegin else
584 if a > vEnd then a := vEnd;
585 end;
586
587 procedure ClampVar(var a: cardinal; const vBegin, vEnd: cardinal);
588 begin
589 if a < vBegin then a := vBegin else
590 if a > vEnd then a := vEnd;
591 end;
592
593 procedure ClampVar(var a: Single; const vBegin, vEnd: Single);
594 begin
595 if a < vBegin then a := vBegin else
596 if a > vEnd then a := vEnd;
597 end;
598
599 procedure ClampVar(var a: Double; const vBegin, vEnd: Double);
600 begin
601 if a < vBegin then a := vBegin else
602 if a > vEnd then a := vEnd;
603 end;
604
605 {$ifdef EXTENDED_OVERLOADS}
606 procedure ClampVar(var a: Extended; const vBegin, vEnd: Extended);
607 begin
608 if a < vBegin then a := vBegin else
609 if a > vEnd then a := vEnd;
610 end;
611 {$endif}
612
613 procedure Clamp(var a: Int64; const vBegin, vEnd: Int64);
614 begin
615 if a < vBegin then a := vBegin else
616 if a > vEnd then a := vEnd;
617 end;
618
619 procedure Clamp(var a: Int32; const vBegin, vEnd: Int32);
620 begin
621 if a < vBegin then a := vBegin else
622 if a > vEnd then a := vEnd;
623 end;
624
625 procedure Clamp(var a: cardinal; const vBegin, vEnd: cardinal);
626 begin
627 if a < vBegin then a := vBegin else
628 if a > vEnd then a := vEnd;
629 end;
630
631 procedure Clamp(var a: Single; const vBegin, vEnd: Single);
632 begin
633 if a < vBegin then a := vBegin else
634 if a > vEnd then a := vEnd;
635 end;
636
637 procedure Clamp(var a: Double; const vBegin, vEnd: Double);
638 begin
639 if a < vBegin then a := vBegin else
640 if a > vEnd then a := vEnd;
641 end;
642
643 {$ifdef EXTENDED_OVERLOADS}
644 procedure Clamp(var a: Extended; const vBegin, vEnd: Extended);
645 begin
646 if a < vBegin then a := vBegin else
647 if a > vEnd then a := vEnd;
648 end;
649 {$endif}
650
651 procedure RestOf3DCoords(const Coord: T3DAxis; out First, Second: T3DAxis);
652 begin
653 if Coord = 0 then First := 1 else First := 0;
654 if Coord = 2 then Second := 1 else Second := 2;
655 end;
656
657 procedure RestOf3DCoordsCycle(const Coord: T3DAxis; out First, Second: T3DAxis);
658 const
659 FirstArr: array [T3DAxis] of T3DAxis = (1, 2, 0);
660 SecondArr: array [T3DAxis] of T3DAxis = (2, 0, 1);
661 begin
662 First := FirstArr[Coord];
663 Second := SecondArr[Coord];
664 end;
665
666 function ChangeIntCycle(Value, Change, MaxValue: Integer): Integer;
667 begin
668 Inc(MaxValue);
669 Result := (Value + Change) mod MaxValue;
670 { We know Result is now in [-MaxValue .. MaxValue]
671 (where MaxValue is the original MaxValue passed to this function). }
672 if Result < 0 then
673 Result := result + MaxValue;
674 end;
675
676 { Lerp : l + (h-l) * a = l + a*h - a*l = (1-a) *l + a*h }
677 function Lerp(const a: Single; const l, h: Int32): Single; begin result := l + (h-l) * a; end;
678 function Lerp(const a: Single; const l, h: UInt32): Single; begin result := l + (h-l) * a; end;
679 function Lerp(const a, l, h: Single): Single; begin result := l + (h-l) * a; end;
680 function Lerp(const a: Double; const l, h: Int32): Double; begin result := l + (h-l) * a; end;
681 function Lerp(const a: Double; const l, h: UInt32): Double; begin result := l + (h-l) * a; end;
682 function Lerp(const a, l, h: Double): Double; begin result := l + (h-l) * a; end;
683
684 function AngleLerp(T, Angle1, Angle2: Single): Single;
685 begin
686 Angle1 := FloatModulo(Angle1, Pi2);
687 Angle2 := FloatModulo(Angle2, Pi2);
688
689 if Angle1 > Angle2 then
690 begin
691 T := 1 - T;
692 SwapValues(Angle1, Angle2);
693 end;
694 { rest of the implementation can assume Angle1 <= Angle2 }
695
696 if Angle1 + Pi2 - Angle2 >= Angle2 - Angle1 then
697 Result := Lerp(T, Angle1, Angle2)
698 else
699 Result := FloatModulo(Lerp(T, Angle1 + Pi2, Angle2), Pi2);
700 end;
701
702 function RoundUpToMultiply(value, multiplicator: Integer): Integer;
703 begin
704 if value mod multiplicator = 0 then
705 result := value
706 else
707 result := ((value div multiplicator)+1)*multiplicator;
708 end;
709
710 function BiggestPowerOf2(Value: Cardinal): Cardinal;
711 begin
712 { Find the first from the left ("oldest") bit = 1.
713 Set the rest of the bits (all the "younger" bits) to zero. }
714 result := Cardinal(1 shl (SizeOf(Cardinal)*8-1));
715 while (result <> 0) and ((result and Value) = 0) do result := result shr 1;
716 end;
717
718 function Biggest2Exponent(Value: Cardinal): Integer;
719 begin
720 result := -1;
721 while Value <> 0 do
722 begin Inc(result); Value := Value shr 1 end;
723 end;
724
725 function Smallest2Exponent(Value: Cardinal): Integer;
726 begin
727 if Value = 0 then begin result := -1; Exit end;
728
729 for result := SizeOf(Value)*8-1 downto 0 do
730 if ((UInt32(1) shl result) and Value) <> 0 then Break;
731
732 { since Value <> 0, so the above "for" is always finished by a Break.
733 Now Result is the position of the "oldest" bit = 1 in Value.
734 If all other bits are = 0 (IOW, "Value = 1 shl Result")
735 then Result is already OK. Otherwise increase Result
736 to make "2 ^ Result >= Value".}
737 if Value <> Cardinal(1) shl result then Inc(result);
738 end;
739
740 function Smallest2Power(Value: Cardinal): Cardinal;
741 var
742 Exp: Integer;
743 begin
744 Exp := Smallest2Exponent(Value);
745 if Exp = -1 then
746 result := 0
747 else
748 result := 1 shl Exp;
749 end;
750
751 function IsPowerOf2(Value: Cardinal): boolean;
752 var
753 i: Cardinal;
754 begin
755 i := Cardinal(1 shl (SizeOf(Cardinal)*8-1));
756 while (i <> 0) do
757 begin
758 if i = Value then begin result := true; exit end;
759 i := i shr 1;
760 end;
761 result := false;
762 end;
763
764 function DivRoundUp(const Value, Divider: Cardinal): Cardinal;
765 begin
766 Result := (Value + Divider - 1) div Divider;
767 end;
768
769 function DivRoundUp(const Value, Divider: Integer): Integer;
770 begin
771 Result := (Value + Divider - 1) div Divider;
772 end;
773
774 function MapRange(const SourceVal, SourceBegin, SourceEnd, DestBegin, DestEnd: Integer): Float;
775 begin
776 { Map range SourceBegin .. SourceEnd to 0..1, and express SourceVal there. }
777 Result := (SourceVal - SourceBegin) / (SourceEnd - SourceBegin);
778 { Map range 0..1 to DestBegin .. DestEnd }
779 Result := Result * (DestEnd - DestBegin) + DestBegin;
780 end;
781
782 function MapRange(const SourceVal, SourceBegin, SourceEnd, DestBegin, DestEnd: Int64 ): Float;
783 begin
784 { Map range SourceBegin .. SourceEnd to 0..1, and express SourceVal there. }
785 Result := (SourceVal - SourceBegin) / (SourceEnd - SourceBegin);
786 { Map range 0..1 to DestBegin .. DestEnd }
787 Result := Result * (DestEnd - DestBegin) + DestBegin;
788 end;
789
790 function MapRange(const SourceVal, SourceBegin, SourceEnd, DestBegin, DestEnd: QWord ): Float;
791 {$I norqcheckbegin.inc}
792 begin
793 { Map range SourceBegin .. SourceEnd to 0..1, and express SourceVal there. }
794 Result := (SourceVal - SourceBegin) / (SourceEnd - SourceBegin);
795 { Map range 0..1 to DestBegin .. DestEnd }
796 Result := Result * (DestEnd - DestBegin) + DestBegin;
797 end;
798 {$I norqcheckend.inc}
799
800 function MapRange(const SourceVal, SourceBegin, SourceEnd, DestBegin, DestEnd: Float ): Float;
801 begin
802 { Map range SourceBegin .. SourceEnd to 0..1, and express SourceVal there. }
803 Result := (SourceVal - SourceBegin) / (SourceEnd - SourceBegin);
804 { Map range 0..1 to DestBegin .. DestEnd }
805 Result := Result * (DestEnd - DestBegin) + DestBegin;
806 end;
807
808 function MapRangeClamped(const SourceVal, SourceBegin, SourceEnd, DestBegin, DestEnd: Integer): Float;
809 begin
810 Result := MapRange(SourceVal, SourceBegin, SourceEnd, DestBegin, DestEnd);
811 if DestEnd < DestBegin then
812 Result := Clamped(Result, DestEnd, DestBegin)
813 else
814 Result := Clamped(Result, DestBegin, DestEnd);
815 end;
816
817 function MapRangeClamped(const SourceVal, SourceBegin, SourceEnd, DestBegin, DestEnd: Int64 ): Float;
818 begin
819 Result := MapRange(SourceVal, SourceBegin, SourceEnd, DestBegin, DestEnd);
820 if DestEnd < DestBegin then
821 Result := Clamped(Result, DestEnd, DestBegin)
822 else
823 Result := Clamped(Result, DestBegin, DestEnd);
824 end;
825
826 function MapRangeClamped(const SourceVal, SourceBegin, SourceEnd, DestBegin, DestEnd: QWord ): Float;
827 begin
828 Result := MapRange(SourceVal, SourceBegin, SourceEnd, DestBegin, DestEnd);
829 if DestEnd < DestBegin then
830 Result := Clamped(Result, DestEnd, DestBegin)
831 else
832 Result := Clamped(Result, DestBegin, DestEnd);
833 end;
834
835 function MapRangeClamped(const SourceVal, SourceBegin, SourceEnd, DestBegin, DestEnd: Float ): Float;
836 begin
837 Result := MapRange(SourceVal, SourceBegin, SourceEnd, DestBegin, DestEnd);
838 if DestEnd < DestBegin then
839 Result := Clamped(Result, DestEnd, DestBegin)
840 else
841 Result := Clamped(Result, DestBegin, DestEnd);
842 end;
843
844 function MapRangeTo01(const SourceVal, SourceBegin, SourceEnd: Single): Single;
845 begin
846 Result := (SourceVal - SourceBegin) / (SourceEnd - SourceBegin);
847 end;
848
849 function RandomFloatRange(const RangeBegin, RangeEnd: Single): Single;
850 begin
851 Result := Random * (RangeEnd - RangeBegin) + RangeBegin;
852 end;
853
854 function RandomFloatRange(const RangeBegin, RangeEnd: Float): Float;
855 begin
856 Result := Random * (RangeEnd - RangeBegin) + RangeBegin;
857 end;
858
859 function RandomIntRange(const RangeBegin, RangeEnd: Integer): Integer;
860 begin
861 Result := RangeBegin + Random(RangeEnd - RangeBegin);
862 end;
863
864 function RandomIntRange(const RangeBegin, RangeEnd: Int64): Int64;
865 begin
866 Result := RangeBegin + Random(RangeEnd - RangeBegin);
867 end;
868
869 function RandomIntRangeInclusive(const RangeBegin, RangeEnd: Integer): Integer;
870 begin
871 Result := RangeBegin + Random(RangeEnd - RangeBegin + 1);
872 end;
873
874 function RandomIntRangeInclusive(const RangeBegin, RangeEnd: Int64): Int64;
875 begin
876 Result := RangeBegin + Random(RangeEnd - RangeBegin + 1);
877 end;
878
879 function RandomBoolean: boolean;
880 begin
881 Result := Random(2) = 0;
882 end;
883
884 function RandomPlusMinus: Integer;
885 const
886 PlusMinus: array [boolean] of Integer = (-1, +1);
887 begin
888 Result := PlusMinus[RandomBoolean];
889 end;
890
891 function AngleRadPointToPoint(x1, y1, x2, y2: Single): Single;
892 begin
893 if y2 > y1 then
894 begin
895 if SameValue(x1, x2) then
896 result := Pi/2 else
897 begin
898 result := ArcTan( (y2-y1) / (x2-x1) );
899 if result < 0 then result := result +Pi;
900 end;
901 end else
902 if y1 > y2 then
903 begin
904 if SameValue(x1, x2) then
905 result := Pi*3/2 else
906 begin
907 result := ArcTan( (y2-y1) / (x2-x1) );
908 if result < 0 then result := result +Pi;
909 result := result +Pi;
910 end;
911 end else
912 if x2 > x1 then result := 0 else result := Pi;
913 end;
914
915 function NatNatPower(Base, Exponent: Cardinal): Cardinal;
916 var i : Cardinal;
917 begin
918 result := 1;
919 i := Exponent;
920 while i > 0 do
921 begin
922 while not Odd(i) do
923 begin
924 i := i shr 1;
925 base := sqr(base);
926 end;
927 i := i-1;
928 result := result*base;
929 end;
930 end;
931
932 {$ifdef FPC}
933 function ArcCot(x: Float): Float;
934 begin
935 result := ArcTan(1/x);
936 end;
937 {$endif FPC}
938
939 function SmallFactorial(n: Integer): Int64;
940 begin
941 Result := 1;
942 while n > 1 do begin Result := Result * n; Dec(n) end;
943 end;
944
945 procedure CastleDivMod(Dividend: Integer; Divisor: Word;
946 out Result, Remainder: SmallInt);
947 var
948 UnsignedResult, UnsignedRemainder: Word;
949 begin
950 { This should not be necessary for FPC, we support only
951 FPC >= 3.2.0 and http://bugs.freepascal.org/view.php?id=15453
952 (treatment of negative Dividend) is fixed since FPC 2.4.2.
953 So FPC could just call DivMod?
954
955 Still this is necessary for Delphi (that supports only unsigned
956 1st param for DivMod).
957
958 For consistency we use this code with both FPC and Delphi. }
959
960 if Dividend < 0 then
961 begin
962 { Use DivMod with >=0 dividend }
963 DivMod(-Dividend, Divisor, UnsignedResult, UnsignedRemainder);
964 { The documented behavior of Pascal's div/mod operators and DivMod
965 on negative dividends is to return Result closer to zero and
966 a negative Remainder. Which means that we can just negate both
967 Result and Remainder, and all it's Ok. }
968 Result := -UnsignedResult;
969 Remainder := -UnsignedRemainder;
970 end else
971 begin
972 DivMod(Dividend, Divisor, UnsignedResult, UnsignedRemainder);
973 Result := UnsignedResult;
974 Remainder := UnsignedRemainder;
975 end;
976 end;
977
978 procedure DivUnsignedMod(Dividend: Integer; Divisor: Word;
979 out Result: Smallint; out Remainder: Word);
980 var
981 SignedRemainder: SmallInt;
982 begin
983 CastleDivMod(Dividend, Divisor, Result, SignedRemainder);
984 if SignedRemainder < 0 then
985 begin
986 Dec(Result);
987 Remainder := Divisor + SignedRemainder;
988 end else
989 Remainder := SignedRemainder;
990 end;
991
992 function CeilDiv(const A, B: Cardinal): Cardinal;
993 var
994 DivResult, ModResult: Cardinal;
995 begin
996 { DivMod was limited to 16-bit values in older FPC versions.
997 It may be possible to change below to
998 DivMod(A, B, DivResult, ModResult);
999 on new FPC, after adding auto-test that it works OK even with result
1000 that doesn't fit in 16-bit range. }
1001 DivResult := A div B;
1002 ModResult := A mod B;
1003 if ModResult = 0 then
1004 Result := DivResult
1005 else
1006 Result := DivResult + 1;
1007 end;
1008
1009 function CeilDiv(const A, B: Int64): Int64;
1010 var
1011 DivResult, ModResult: Int64;
1012 begin
1013 { DivMod is limited to 32-bit values, so we cannot use it below. }
1014 DivResult := A div B;
1015 ModResult := A mod B;
1016 if ModResult = 0 then
1017 Result := DivResult
1018 else
1019 Result := DivResult + 1;
1020 end;
1021
1022 procedure FloatDivMod(const A, B: Double;
1023 out DivResult: Int64; out Remainder: Double);
1024 begin
1025 DivResult := Trunc(A / B);
1026 Remainder := A - B * DivResult;
1027 { Now the tricks to make this (somewhat) more numerically stable,
1028 i.e. resistant to tricky cases without unprecise floating-point
1029 calculation. }
1030 if Remainder < 0 then
1031 Remainder := 0 else
1032 if Remainder >= B then
1033 begin
1034 Remainder := Remainder - B;
1035 Inc(DivResult);
1036
1037 { Mathematically, we could do above two lines in a loop
1038 "while Remainder >= A", but this could loop (practically)
1039 forever for very small (or zero) B.
1040
1041 Instead we just clamp Remainder to [0, B] once again, the hard way. }
1042
1043 ClampVar(Remainder, 0, B);
1044 end;
1045 end;
1046
1047 function FloatModulo(const A, B: Double): Double;
1048 var
1049 DivResult: Int64;
1050 begin
1051 if A < 0 then
1052 begin
1053 { FloatDivMod requires that 1st param be >= 0. }
1054 FloatDivMod(-A, B, DivResult, Result);
1055 Result := Clamped(B - Result, 0, B);
1056 end else
1057 FloatDivMod(A, B, DivResult, Result);
1058 end;
1059
1060 procedure MinMax(const x0, x1, x2: Double; out min, max: Double);
1061 begin
1062 min := x0;
1063 max := x0;
1064 if x1 < min then min := x1 else
1065 if x1 > max then max := x1;
1066 if x2 < min then min := x2 else
1067 if x2 > max then max := x2;
1068 end;
1069
1070 procedure MinMax(const x0, x1, x2: Single; out min, max: Single);
1071 begin
1072 min := x0;
1073 max := x0;
1074 if x1 < min then min := x1 else
1075 if x1 > max then max := x1;
1076 if x2 < min then min := x2 else
1077 if x2 > max then max := x2;
1078 end;
1079
1080 function CastleCoTan(const Value: Single): Single;
1081 var
1082 ASin, ACos: Single;
1083 begin
1084 SinCos(Value, ASin, ACos);
1085 Result := Acos / ASin;
1086 end;
1087
1088 function IntSqrt(const Value: Cardinal): Cardinal;
1089 { See http://en.wikipedia.org/wiki/Integer_square_root.
1090
1091 This isn't nice impl, as it uses floating-point numbers to calculate
1092 Integer function... However, doing it on ints, doesn't make this
1093 process terminate nicely. Hacks like in primes.cpp from
1094 http://freespace.virgin.net/hugo.elias/models/m_perlin.htm
1095 don't calculate floor sqrt properly (e.g. for 8 return 3).
1096
1097 So for now, calculate on floats. Autotests check it's Ok.
1098
1099 Actually, when calculating on floats, I can just use Trunc(Sqrt())...
1100 So this is quite pointless exercise. }
1101 (*
1102 var
1103 Old, New: Float;
1104 begin
1105 if Value = 0 then Exit(0); { to not fail by division by zero }
1106
1107 New := Value;
1108 repeat
1109 Old := New;
1110 New := (Old + Value / Old) / 2;
1111 until Abs(Old - New) < 0.5;
1112 Result := Trunc(New);
1113 end;
1114 *)
1115 begin
1116 Result := Trunc(Sqrt(Value));
1117 end;
1118
1119 function SmoothStep(const Edge0, Edge1, X: Single): Single;
1120 begin
1121 Result := Clamped((X - Edge0) / (Edge1 - Edge0), 0.0, 1.0);
1122 Result := Result * Result * (3.0 - 2.0 * Result);
1123 end;
1124
1125 { Use Swap32 / Swap64 names, instead of overloading one name like Swap
1126 or SwapEndian, to avoid easy mistakes. }
1127
1128 function Swap32(Value: UInt32): UInt32;
1129 begin
1130 {$ifndef FPC}
1131 // see
1132 // https://stackoverflow.com/questions/3065335/how-to-convert-big-endian-numbers-to-native-numbers-delphi
1133 // https://stackoverflow.com/questions/2882434/how-to-convert-big-endian-and-how-to-flip-the-highest-bit
1134 Result := Swap(Word(Value)) shl 16 + Swap(Word(Value shr 16));
1135 {$else}
1136 Result := SwapEndian(Value);
1137 {$endif}
1138 end;
1139
1140 function Swap64(Value: QWord): QWord;
1141 begin
1142 {$ifndef FPC}
1143 // see
1144 // https://stackoverflow.com/questions/3065335/how-to-convert-big-endian-numbers-to-native-numbers-delphi
1145 // https://stackoverflow.com/questions/2882434/how-to-convert-big-endian-and-how-to-flip-the-highest-bit
1146 Result := Swap32(UInt32(Value));
1147 Result := (Result shl 32) or Swap32(UInt32(Value shr 32));
1148 {$else}
1149 Result := SwapEndian(Value);
1150 {$endif}
1151 end;
1152
1153 function SwapEndian(const Value: Single): Single;
1154 begin
1155 PUInt32(@Result)^ := Swap32(PUInt32(@Value)^);
1156 end;
1157
1158 function SwapEndian(const Value: Double): Double;
1159 begin
1160 PQWord(@Result)^ := Swap64(PQWord(@Value)^);
1161 end;
1162
1163
1164 {$ifndef FPC}
1165 function NtoLE(const Value: Word): Word;
1166 begin
1167 {$ifdef ENDIAN_BIG}
1168 Result := Swap(Value);
1169 {$else ENDIAN_BIG}
1170 Result := Value;
1171 {$endif ENDIAN_BIG}
1172 end;
1173
1174 function NtoLE(const Value: DWord): DWord;
1175 begin
1176 {$ifdef ENDIAN_BIG}
1177 Result := Swap32(Value);
1178 {$else ENDIAN_BIG}
1179 Result := Value;
1180 {$endif ENDIAN_BIG}
1181 end;
1182
1183 function NtoLE(const Value: QWord): QWord;
1184 begin
1185 {$ifdef ENDIAN_BIG}
1186 Result := Swap64(Value);
1187 {$else ENDIAN_BIG}
1188 Result := Value;
1189 {$endif ENDIAN_BIG}
1190 end;
1191
1192 function NtoLE(const Value: Int64): Int64; overload;
1193 begin
1194 {$ifdef ENDIAN_BIG}
1195 Result := Swap64(Value);
1196 {$else ENDIAN_BIG}
1197 Result := Value;
1198 {$endif ENDIAN_BIG}
1199 end;
1200
1201 {$endif}
1202
1203
1204 function NtoLE(const Value: Single): Single;
1205 begin
1206 {$ifdef ENDIAN_BIG}
1207 Result := SwapEndian(Value);
1208 {$else ENDIAN_BIG}
1209 Result := Value;
1210 {$endif ENDIAN_BIG}
1211 end;
1212
1213 function NtoLE(const Value: Double): Double;
1214 begin
1215 {$ifdef ENDIAN_BIG}
1216 Result := SwapEndian(Value);
1217 {$else ENDIAN_BIG}
1218 Result := Value;
1219 {$endif ENDIAN_BIG}
1220 end;
1221
1222 {$ifndef FPC}
1223 function NtoBE(const Value: Word): Word;
1224 begin
1225 {$ifdef ENDIAN_BIG}
1226 Result := Value;
1227 {$else ENDIAN_BIG}
1228 Result := Swap(Value);
1229 {$endif ENDIAN_BIG}
1230 end;
1231
1232 function NtoBE(const Value: DWord): DWord;
1233 begin
1234 {$ifdef ENDIAN_BIG}
1235 Result := Value;
1236 {$else ENDIAN_BIG}
1237 Result := Swap32(Value);
1238 {$endif ENDIAN_BIG}
1239 end;
1240
1241
1242 function NtoBE(const Value: QWord): QWord;
1243 begin
1244 {$ifdef ENDIAN_BIG}
1245 Result := Value;
1246 {$else ENDIAN_BIG}
1247 Result := Swap64(Value);
1248 {$endif ENDIAN_BIG}
1249 end;
1250
1251 function NtoBE(const Value: Int64): Int64;
1252 begin
1253 {$ifdef ENDIAN_BIG}
1254 Result := Value;
1255 {$else ENDIAN_BIG}
1256 Result := Swap64(Value);
1257 {$endif ENDIAN_BIG}
1258 end;
1259
1260 {$endif}
1261
1262
1263 function NtoBE(const Value: Single): Single;
1264 begin
1265 {$ifdef ENDIAN_BIG}
1266 Result := Value;
1267 {$else ENDIAN_BIG}
1268 Result := SwapEndian(Value);
1269 {$endif ENDIAN_BIG}
1270 end;
1271
1272 function NtoBE(const Value: Double): Double;
1273 begin
1274 {$ifdef ENDIAN_BIG}
1275 Result := Value;
1276 {$else ENDIAN_BIG}
1277 Result := SwapEndian(Value);
1278 {$endif ENDIAN_BIG}
1279 end;
1280
1281 {$ifndef FPC}
1282 function LEtoN(const Value: Word): Word;
1283 begin
1284 {$ifdef ENDIAN_BIG}
1285 Result := Swap(Value);
1286 {$else ENDIAN_BIG}
1287 Result := Value;
1288 {$endif ENDIAN_BIG}
1289 end;
1290
1291 function LEtoN(const Value: DWord): DWord;
1292 begin
1293 {$ifdef ENDIAN_BIG}
1294 Result := Swap32(Value);
1295 {$else ENDIAN_BIG}
1296 Result := Value;
1297 {$endif ENDIAN_BIG}
1298 end;
1299
1300 function LEtoN(const Value: QWord): QWord;
1301 begin
1302 {$ifdef ENDIAN_BIG}
1303 Result := Swap64(Value);
1304 {$else ENDIAN_BIG}
1305 Result := Value;
1306 {$endif ENDIAN_BIG}
1307 end;
1308
1309 function LEtoN(const Value: Int64): Int64;
1310 begin
1311 {$ifdef ENDIAN_BIG}
1312 Result := Swap64(Value);
1313 {$else ENDIAN_BIG}
1314 Result := Value;
1315 {$endif ENDIAN_BIG}
1316 end;
1317
1318
1319 {$endif FPC}
1320
1321 function LEtoN(const Value: Single): Single;
1322 begin
1323 {$ifdef ENDIAN_BIG}
1324 Result := SwapEndian(Value);
1325 {$else ENDIAN_BIG}
1326 Result := Value;
1327 {$endif ENDIAN_BIG}
1328 end;
1329
1330 function LEtoN(const Value: Double): Double;
1331 begin
1332 {$ifdef ENDIAN_BIG}
1333 Result := SwapEndian(Value);
1334 {$else ENDIAN_BIG}
1335 Result := Value;
1336 {$endif ENDIAN_BIG}
1337 end;
1338
1339 {$ifndef FPC}
1340
1341 function BEtoN(const Value: Word): Word;
1342 begin
1343 {$ifdef ENDIAN_BIG}
1344 Result := Value;
1345 {$else ENDIAN_BIG}
1346 Result := Swap(Value);
1347 {$endif ENDIAN_BIG}
1348 end;
1349
1350 function BEtoN(const Value: DWord): DWord;
1351 begin
1352 {$ifdef ENDIAN_BIG}
1353 Result := Value;
1354 {$else ENDIAN_BIG}
1355 Result := Swap32(Value);
1356 {$endif ENDIAN_BIG}
1357 end;
1358
1359 function BEtoN(const Value: QWord): QWord;
1360 begin
1361 {$ifdef ENDIAN_BIG}
1362 Result := Value;
1363 {$else ENDIAN_BIG}
1364 Result := Swap64(Value);
1365 {$endif ENDIAN_BIG}
1366 end;
1367
1368 function BEtoN(const Value: Int64): Int64;
1369 begin
1370 {$ifdef ENDIAN_BIG}
1371 Result := Value;
1372 {$else ENDIAN_BIG}
1373 Result := Swap64(Value);
1374 {$endif ENDIAN_BIG}
1375 end;
1376
1377 {$endif}
1378
1379
1380 function BEtoN(const Value: Single): Single;
1381 begin
1382 {$ifdef ENDIAN_BIG}
1383 Result := Value;
1384 {$else ENDIAN_BIG}
1385 Result := SwapEndian(Value);
1386 {$endif ENDIAN_BIG}
1387 end;
1388
1389 function BEtoN(const Value: Double): Double;
1390 begin
1391 {$ifdef ENDIAN_BIG}
1392 Result := Value;
1393 {$else ENDIAN_BIG}
1394 Result := SwapEndian(Value);
1395 {$endif ENDIAN_BIG}
1396 end;
1397
1398 function Deg(const Degrees: Float): Float;
1399 begin
1400 Result := DegToRad(Degrees);
1401 end;
1402
1403 {$endif read_implementation}