Debian Codegraph @

src/base/castlevectors_lists.inc

raw
1 {%MainUnit castlevectors.pas}
2 {
3 Copyright 2003-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 { CastleVectors lists of vectors and matrices. }
18
19 {$ifdef read_interface}
20
21 type
22 TVector4List = class;
23
24 { List of @link(TVector2).
25 Note that the notification mechanism (OnNotify) is broken here by routines like AddRange
26 and AddSubRange (for maximum speed). }
27 TVector2List = class({$ifdef FPC}specialize{$endif} TStructList<TVector2>)
28 public
29 { Calculate minimum and maximum values for both dimensions of
30 this set of points. Returns @false when Count = 0. }
31 function MinMax(out Min, Max: TVector2): boolean;
32
33 procedure AssignLerpRange(const Fraction: Single;
34 const V1, V2: {$ifdef FPC}specialize{$endif} TStructList<TVector2>;
35 const Index1, Index2, ACount: TListSize); override;
36
37 procedure AddRange(const Source: TVector2List); overload;
38 procedure AddSubRange(const Source: TVector2List; const Index, AddCount: Integer);
39 procedure AddListRange(const Source: TVector2List; const Index, AddCount: Integer); deprecated 'use AddSubRange';
40
41 { Does the SecondValue have equal length and content.
42 The values are compared with an Epsilon tolerance, as usual for floating-point
43 values. }
44 function Equals(SecondValue: TObject): boolean; override;
45 end;
46
47 { List of @link(TVector3).
48 Note that the notification mechanism (OnNotify) is broken here by routines like AddRange
49 and AddSubRange (for maximum speed). }
50 TVector3List = class({$ifdef FPC}specialize{$endif} TStructList<TVector3>)
51 public
52 procedure AssignNegated(const Source: TVector3List); deprecated 'use Assign and Negate separately';
53
54 { Negate all items. }
55 procedure Negate;
56
57 { Normalize all items. Zero vectors are left as zero. }
58 procedure Normalize;
59
60 { Multiply each item, component-wise, with V. }
61 procedure MultiplyComponents(const V: TVector3);
62
63 procedure AssignLerpRange(const Fraction: Single;
64 const V1, V2: {$ifdef FPC}specialize{$endif} TStructList<TVector3>;
65 const Index1, Index2, ACount: TListSize); override;
66
67 { Assign linear interpolation between two other vector arrays,
68 and normalize resulting vectors.
69 @seealso AssignLerp }
70 procedure AssignLerpNormalize(const Fraction: Single;
71 const V1, V2: TVector3List;
72 const Index1, Index2, ACount: TListSize);
73
74 { Assign linear interpolation between two other vector arrays,
75 treating vectors as RGB colors and interpolating in HSV space.
76 @seealso AssignLerp }
77 procedure AssignLerpRgbInHsv(const Fraction: Single;
78 const V1, V2: TVector3List;
79 const Index1, Index2, ACount: TListSize);
80
81 procedure AddRange(const Source: TVector3List); overload;
82 procedure AddSubRange(const Source: TVector3List; const Index, AddCount: Integer);
83 procedure AddListRange(const Source: TVector3List; const Index, AddCount: Integer); deprecated 'use AddSubRange';
84
85 procedure AddRangeTransformed(const Source: TVector3List; const Transform: TMatrix4);
86
87 { Convert to TVector4List, with 4th vector component in
88 new array set to constant W. }
89 function ToVector4(const W: Single): TVector4List;
90
91 { When two vertexes on the list are closer than MergeDistance,
92 set them truly (exactly) equal.
93 Returns how many vertex positions were changed. }
94 function MergeCloseVertexes(MergeDistance: Single): Cardinal;
95
96 { Does the SecondValue have equal length and content.
97 The values are compared with an Epsilon tolerance, as usual for floating-point
98 values. }
99 function Equals(SecondValue: TObject): boolean; override;
100 end;
101
102 { List of @link(TVector4).
103 Note that the notification mechanism (OnNotify) is broken here by routines like AddRange
104 and AddSubRange (for maximum speed). }
105 TVector4List = class({$ifdef FPC}specialize{$endif} TStructList<TVector4>)
106 public
107 procedure AddRange(const Source: TVector4List);
108 procedure AddSubRange(const Source: TVector4List; const Index, AddCount: Integer);
109 procedure AddListRange(const Source: TVector4List; const Index, AddCount: Integer); deprecated 'use AddSubRange';
110
111 procedure AssignLerpRange(const Fraction: Single;
112 const V1, V2: {$ifdef FPC}specialize{$endif} TStructList<TVector4>;
113 const Index1, Index2, ACount: TListSize); override;
114
115 { Does the SecondValue have equal length and content.
116 The values are compared with an Epsilon tolerance, as usual for floating-point
117 values. }
118 function Equals(SecondValue: TObject): boolean; override;
119 end;
120
121 { List of @link(TVector4Integer).
122 Note that the notification mechanism (OnNotify) is broken here by routines like AddRange
123 and AddSubRange (for maximum speed). }
124 TVector4IntegerList = class({$ifdef FPC}specialize{$endif} TStructList<TVector4Integer>)
125 public
126 procedure AddRange(const Source: TVector4IntegerList);
127 procedure AddSubRange(const Source: TVector4IntegerList; const Index, AddCount: Integer);
128 procedure AddListRange(const Source: TVector4IntegerList; const Index, AddCount: Integer); deprecated 'use AddSubRange';
129
130 { Does the SecondValue have equal length and content. }
131 function Equals(SecondValue: TObject): boolean; override;
132 end;
133
134 { List of @link(TMatrix3).
135 Note that the notification mechanism (OnNotify) is broken here by routines like AddRange
136 and AddSubRange (for maximum speed). }
137 TMatrix3List = class({$ifdef FPC}specialize{$endif} TStructList<TMatrix3>)
138 public
139 procedure AddRange(const Source: TMatrix3List);
140 procedure AddSubRange(const Source: TMatrix3List; const Index, AddCount: Integer);
141
142 procedure AssignLerpRange(const Fraction: Single;
143 const V1, V2: {$ifdef FPC}specialize{$endif} TStructList<TMatrix3>;
144 const Index1, Index2, ACount: TListSize); override;
145
146 { Does the SecondValue have equal length and content.
147 The values are compared with an Epsilon tolerance, as usual for floating-point
148 values. }
149 function Equals(SecondValue: TObject): boolean; override;
150 end;
151
152 { List of @link(TMatrix4).
153 Note that the notification mechanism (OnNotify) is broken here by routines like AddRange
154 and AddSubRange (for maximum speed). }
155 TMatrix4List = class({$ifdef FPC}specialize{$endif} TStructList<TMatrix4>)
156 public
157 procedure AddRange(const Source: TMatrix4List);
158 procedure AddSubRange(const Source: TMatrix4List; const Index, AddCount: Integer);
159
160 procedure AssignLerpRange(const Fraction: Single;
161 const V1, V2: {$ifdef FPC}specialize{$endif} TStructList<TMatrix4>;
162 const Index1, Index2, ACount: TListSize); override;
163
164 { Does the SecondValue have equal length and content.
165 The values are compared with an Epsilon tolerance, as usual for floating-point
166 values. }
167 function Equals(SecondValue: TObject): boolean; override;
168 end;
169
170 { List of @link(TVector2SmallInt). }
171 TVector2SmallIntList = {$ifdef FPC}specialize{$endif} TStructList<TVector2SmallInt>;
172
173 { List of @link(TVector2Cardinal). }
174 TVector3CardinalList = {$ifdef FPC}specialize{$endif} TStructList<TVector3Cardinal>;
175
176 {$endif read_interface}
177
178 {$ifdef read_implementation}
179
180 { TVector2List ----------------------------------------------------- }
181
182 function TVector2List.MinMax(out Min, Max: TVector2): boolean;
183 var
184 I: Integer;
185 begin
186 Result := Count > 0;
187 if Result then
188 begin
189 Min := L[0];
190 Max := L[0];
191 for I := 1 to Count - 1 do
192 begin
193 if L[I].X < Min.X then Min.X := L[I].X else
194 if L[I].X > Max.X then Max.X := L[I].X;
195
196 if L[I].Y < Min.Y then Min.Y := L[I].Y else
197 if L[I].Y > Max.Y then Max.Y := L[I].Y;
198 end;
199 end;
200 end;
201
202 procedure TVector2List.AssignLerpRange(const Fraction: Single;
203 const V1, V2: {$ifdef FPC}specialize{$endif} TStructList<TVector2>;
204 const Index1, Index2, ACount: TListSize);
205 var
206 I: TListSize;
207 PtrTarget, Ptr1, Ptr2: PtrT;
208 begin
209 Count := ACount;
210 PtrTarget := L;
211 Ptr1 := V1.Ptr(Index1);
212 Ptr2 := V2.Ptr(Index2);
213 for I := 0 to Count - 1 do
214 begin
215 PtrTarget^ := Lerp(Fraction, Ptr1^, Ptr2^);
216 Inc(PtrTarget);
217 Inc(Ptr1);
218 Inc(Ptr2);
219 end;
220 end;
221
222 procedure TVector2List.AddRange(const Source: TVector2List);
223 begin
224 AddSubRange(Source, 0, Source.Count);
225 end;
226
227 procedure TVector2List.AddSubRange(const Source: TVector2List; const Index, AddCount: Integer);
228 var
229 OldCount: Integer;
230 begin
231 OldCount := Count;
232 Count := Count + AddCount;
233 if AddCount <> 0 then
234 System.Move(Source.L[Index], L[OldCount], SizeOf(TVector2) * AddCount);
235 end;
236
237 procedure TVector2List.AddListRange(const Source: TVector2List; const Index, AddCount: Integer);
238 begin
239 AddSubRange(Source, Index, AddCount);
240 end;
241
242 function TVector2List.Equals(SecondValue: TObject): boolean;
243 var
244 I: Integer;
245 begin
246 Result := SecondValue is TVector2List;
247 if Result then
248 begin
249 Result := Count = TVector2List(SecondValue).Count;
250 if Result then
251 for I := 0 to Count - 1 do
252 if not TVector2.Equals(TVector2List(SecondValue).L[I], L[I]) then
253 begin
254 Result := false;
255 Exit;
256 end;
257 end;
258 end;
259
260 { TVector3List ----------------------------------------------------- }
261
262 procedure TVector3List.AssignNegated(const Source: TVector3List);
263 begin
264 Assign(Source);
265 Negate;
266 end;
267
268 procedure TVector3List.Negate;
269 var
270 I: Integer;
271 begin
272 for I := 0 to Count - 1 do
273 L[I] := -L[I];
274 end;
275
276 procedure TVector3List.Normalize;
277 var
278 I: Integer;
279 begin
280 for I := 0 to Count - 1 do
281 L[I] := L[I].Normalize;
282 end;
283
284 procedure TVector3List.MultiplyComponents(const V: TVector3);
285 var
286 I: Integer;
287 begin
288 for I := 0 to Count - 1 do
289 L[I] := L[I] * V;
290 end;
291
292 procedure TVector3List.AssignLerpRange(const Fraction: Single;
293 const V1, V2: {$ifdef FPC}specialize{$endif} TStructList<TVector3>;
294 const Index1, Index2, ACount: TListSize);
295 var
296 I: TListSize;
297 PtrTarget, Ptr1, Ptr2: PtrT;
298 begin
299 Count := ACount;
300 PtrTarget := L;
301 Ptr1 := V1.Ptr(Index1);
302 Ptr2 := V2.Ptr(Index2);
303 for I := 0 to Count - 1 do
304 begin
305 PtrTarget^ := Lerp(Fraction, Ptr1^, Ptr2^);
306 Inc(PtrTarget);
307 Inc(Ptr1);
308 Inc(Ptr2);
309 end;
310 end;
311
312 procedure TVector3List.AssignLerpNormalize(const Fraction: Single;
313 const V1, V2: TVector3List; const Index1, Index2, ACount: TListSize);
314 var
315 I: Integer;
316 begin
317 Count := ACount;
318 for I := 0 to Count - 1 do
319 begin
320 L[I] := Lerp(Fraction, V1.L[Index1 + I], V2.L[Index2 + I]).Normalize;
321 end;
322 end;
323
324 procedure TVector3List.AssignLerpRgbInHsv(const Fraction: Single;
325 const V1, V2: TVector3List; const Index1, Index2, ACount: TListSize);
326 var
327 I: Integer;
328 begin
329 Count := ACount;
330 for I := 0 to Count - 1 do
331 L[I] := LerpRgbInHsv(Fraction, V1.L[Index1 + I], V2.L[Index2 + I]);
332 end;
333
334 function TVector3List.ToVector4(const W: Single): TVector4List;
335 var
336 I: Integer;
337 begin
338 Result := TVector4List.Create;
339 Result.Count := Count;
340 for I := 0 to Count - 1 do
341 Result.L[I] := Vector4(L[I], W);
342 end;
343
344 function TVector3List.MergeCloseVertexes(MergeDistance: Single): Cardinal;
345 var
346 V1, V2: PtrT;
347 I, J: Integer;
348 begin
349 MergeDistance := Sqr(MergeDistance);
350 Result := 0;
351
352 V1 := L;
353 for I := 0 to Count - 1 do
354 begin
355 { Find vertexes closer to L[I], and merge them.
356
357 Note that this is not optimal: we could avoid processing
358 here L[I] that were detected previously (and possibly merged)
359 as being equal to some previous items. But in practice this seems
360 not needed, as there are not many merged vertices in typical situation,
361 so time saving would be minimal (and small temporary memory cost
362 introduced). }
363
364 V2 := Ptr(I + 1);
365 for J := I + 1 to Count - 1 do
366 begin
367 if PointsDistanceSqr(V1^, V2^) < MergeDistance then
368 { We do the TVector3.PerfectlyEquals comparison only to get nice Result.
369 But this *is* an important value for the user, so it's worth it. }
370 if not TVector3.PerfectlyEquals(V1^, V2^) then
371 begin
372 V2^ := V1^;
373 Inc(Result);
374 end;
375 Inc(V2);
376 end;
377
378 Inc(V1);
379 end;
380 end;
381
382 procedure TVector3List.AddRange(const Source: TVector3List);
383 begin
384 AddSubRange(Source, 0, Source.Count);
385 end;
386
387 procedure TVector3List.AddSubRange(const Source: TVector3List; const Index, AddCount: Integer);
388 var
389 OldCount: Integer;
390 begin
391 OldCount := Count;
392 Count := Count + AddCount;
393 if AddCount <> 0 then
394 System.Move(Source.L[Index], L[OldCount], SizeOf(TVector3) * AddCount);
395 end;
396
397 procedure TVector3List.AddListRange(const Source: TVector3List; const Index, AddCount: Integer);
398 begin
399 AddSubRange(Source, Index, AddCount);
400 end;
401
402 function TVector3List.Equals(SecondValue: TObject): boolean;
403 var
404 I: Integer;
405 begin
406 Result := SecondValue is TVector3List;
407 if Result then
408 begin
409 Result := Count = TVector3List(SecondValue).Count;
410 if Result then
411 for I := 0 to Count - 1 do
412 if not TVector3.Equals(TVector3List(SecondValue).L[I], L[I]) then
413 begin
414 Result := false;
415 Exit;
416 end;
417 end;
418 end;
419
420 procedure TVector3List.AddRangeTransformed(const Source: TVector3List; const Transform: TMatrix4);
421 var
422 I, OldCount: Integer;
423 begin
424 OldCount := Count;
425 Count := Count + Source.Count;
426 for I := 0 to Source.Count - 1 do
427 L[OldCount + I] := Transform.MultPoint(Source.L[I]);
428 end;
429
430 { TVector4List ----------------------------------------------------- }
431
432 procedure TVector4List.AddRange(const Source: TVector4List);
433 begin
434 AddSubRange(Source, 0, Source.Count);
435 end;
436
437 procedure TVector4List.AddSubRange(const Source: TVector4List; const Index, AddCount: Integer);
438 var
439 OldCount: Integer;
440 begin
441 OldCount := Count;
442 Count := Count + AddCount;
443 if AddCount <> 0 then
444 System.Move(Source.L[Index], L[OldCount], SizeOf(TVector4) * AddCount);
445 end;
446
447 procedure TVector4List.AddListRange(const Source: TVector4List; const Index, AddCount: Integer);
448 begin
449 AddSubRange(Source, Index, AddCount);
450 end;
451
452 procedure TVector4List.AssignLerpRange(const Fraction: Single;
453 const V1, V2: {$ifdef FPC}specialize{$endif} TStructList<TVector4>;
454 const Index1, Index2, ACount: TListSize);
455 var
456 I: TListSize;
457 PtrTarget, Ptr1, Ptr2: PtrT;
458 begin
459 Count := ACount;
460 PtrTarget := L;
461 Ptr1 := V1.Ptr(Index1);
462 Ptr2 := V2.Ptr(Index2);
463 for I := 0 to Count - 1 do
464 begin
465 PtrTarget^ := Lerp(Fraction, Ptr1^, Ptr2^);
466 Inc(PtrTarget);
467 Inc(Ptr1);
468 Inc(Ptr2);
469 end;
470 end;
471
472 function TVector4List.Equals(SecondValue: TObject): boolean;
473 var
474 I: Integer;
475 begin
476 Result := SecondValue is TVector4List;
477 if Result then
478 begin
479 Result := Count = TVector4List(SecondValue).Count;
480 if Result then
481 for I := 0 to Count - 1 do
482 if not TVector4.Equals(TVector4List(SecondValue).L[I], L[I]) then
483 begin
484 Result := false;
485 Exit;
486 end;
487 end;
488 end;
489
490 { TVector4IntegerList ----------------------------------------------------- }
491
492 procedure TVector4IntegerList.AddRange(const Source: TVector4IntegerList);
493 begin
494 AddSubRange(Source, 0, Source.Count);
495 end;
496
497 procedure TVector4IntegerList.AddSubRange(const Source: TVector4IntegerList; const Index, AddCount: Integer);
498 var
499 OldCount: Integer;
500 begin
501 OldCount := Count;
502 Count := Count + AddCount;
503 if AddCount <> 0 then
504 System.Move(Source.L[Index], L[OldCount], SizeOf(TVector4Integer) * AddCount);
505 end;
506
507 procedure TVector4IntegerList.AddListRange(const Source: TVector4IntegerList; const Index, AddCount: Integer);
508 begin
509 AddSubRange(Source, Index, AddCount);
510 end;
511
512 function TVector4IntegerList.Equals(SecondValue: TObject): boolean;
513 var
514 I: Integer;
515 begin
516 Result := SecondValue is TVector4IntegerList;
517 if Result then
518 begin
519 Result := Count = TVector4IntegerList(SecondValue).Count;
520 if Result then
521 for I := 0 to Count - 1 do
522 if not TVector4Integer.Equals(TVector4IntegerList(SecondValue).L[I], L[I]) then
523 begin
524 Result := false;
525 Exit;
526 end;
527 end;
528 end;
529
530 { TMatrix3List --------------------------------------------------------------- }
531
532 procedure TMatrix3List.AssignLerpRange(const Fraction: Single;
533 const V1, V2: {$ifdef FPC}specialize{$endif} TStructList<TMatrix3>;
534 const Index1, Index2, ACount: TListSize);
535 var
536 I: TListSize;
537 PtrTarget, Ptr1, Ptr2: PtrT;
538 begin
539 Count := ACount;
540 PtrTarget := L;
541 Ptr1 := V1.Ptr(Index1);
542 Ptr2 := V2.Ptr(Index2);
543 for I := 0 to Count - 1 do
544 begin
545 PtrTarget^ := Lerp(Fraction, Ptr1^, Ptr2^);
546 Inc(PtrTarget);
547 Inc(Ptr1);
548 Inc(Ptr2);
549 end;
550 end;
551
552 function TMatrix3List.Equals(SecondValue: TObject): boolean;
553 var
554 I: Integer;
555 begin
556 Result := SecondValue is TMatrix3List;
557 if Result then
558 begin
559 Result := Count = TMatrix3List(SecondValue).Count;
560 if Result then
561 for I := 0 to Count - 1 do
562 if not TMatrix3.Equals(TMatrix3List(SecondValue).L[I], L[I]) then
563 begin
564 Result := false;
565 Exit;
566 end;
567 end;
568 end;
569
570 procedure TMatrix3List.AddRange(const Source: TMatrix3List);
571 begin
572 AddSubRange(Source, 0, Source.Count);
573 end;
574
575 procedure TMatrix3List.AddSubRange(const Source: TMatrix3List; const Index, AddCount: Integer);
576 var
577 OldCount: Integer;
578 begin
579 OldCount := Count;
580 Count := Count + AddCount;
581 if AddCount <> 0 then
582 System.Move(Source.L[Index], L[OldCount], SizeOf(TMatrix3) * AddCount);
583 end;
584
585 { TMatrix4List --------------------------------------------------------------- }
586
587 procedure TMatrix4List.AssignLerpRange(const Fraction: Single;
588 const V1, V2: {$ifdef FPC}specialize{$endif} TStructList<TMatrix4>;
589 const Index1, Index2, ACount: TListSize);
590 var
591 I: TListSize;
592 PtrTarget, Ptr1, Ptr2: PtrT;
593 begin
594 Count := ACount;
595 PtrTarget := L;
596 Ptr1 := V1.Ptr(Index1);
597 Ptr2 := V2.Ptr(Index2);
598 for I := 0 to Count - 1 do
599 begin
600 PtrTarget^ := Lerp(Fraction, Ptr1^, Ptr2^);
601 Inc(PtrTarget);
602 Inc(Ptr1);
603 Inc(Ptr2);
604 end;
605 end;
606
607 function TMatrix4List.Equals(SecondValue: TObject): boolean;
608 var
609 I: Integer;
610 begin
611 Result := SecondValue is TMatrix4List;
612 if Result then
613 begin
614 Result := Count = TMatrix4List(SecondValue).Count;
615 if Result then
616 for I := 0 to Count - 1 do
617 if not TMatrix4.Equals(TMatrix4List(SecondValue).L[I], L[I]) then
618 begin
619 Result := false;
620 Exit;
621 end;
622 end;
623 end;
624
625 procedure TMatrix4List.AddRange(const Source: TMatrix4List);
626 begin
627 AddSubRange(Source, 0, Source.Count);
628 end;
629
630 procedure TMatrix4List.AddSubRange(const Source: TMatrix4List; const Index, AddCount: Integer);
631 var
632 OldCount: Integer;
633 begin
634 OldCount := Count;
635 Count := Count + AddCount;
636 if AddCount <> 0 then
637 System.Move(Source.L[Index], L[OldCount], SizeOf(TMatrix4) * AddCount);
638 end;
639
640 {$endif read_implementation}