Recent

Author Topic: [solved]sorting array of path pointers by distance in 3D model  (Read 687 times)

tygraphics

  • New Member
  • *
  • Posts: 19
Hi,

I am working on a 3D vector application project using Free Pascal, and I need to sort an array of pointers referencing Path records. Each Path record contains a field that stores the distance from a camera to the center point of that path. The goal is to sort this array of pointers in descending order based on the distance values so that during the rendering process, paths that are furthest away from the camera are drawn first, and those closest are drawn last.

I appreciate any help you can provide.
Thomas Young
« Last Edit: August 11, 2024, 08:52:34 pm by tygraphics »

Hartmut

  • Hero Member
  • *****
  • Posts: 812
Re: sorting array of path pointers by distance in 3D model
« Reply #1 on: August 09, 2024, 06:55:49 pm »
can you show as the declaration of the records and the array of pointers? And how many data (about) do you want to sort?

tooldeveloper

  • Newbie
  • Posts: 5
Re: sorting array of path pointers by distance in 3D model
« Reply #2 on: August 09, 2024, 08:26:24 pm »
The TFPObjectList class in the Contnrs unit may solve your issue.  Assuming I am reading your question correctly then below is a quick example of doing what I think you are asking.

Code: Pascal  [Select][+][-]
  1. uses
  2.   Contnrs;
  3.  
  4. { TForm1 }
  5.  
  6.   type
  7.     T3DVector = record
  8.       CamDistance : Int64;
  9.     end;
  10.     P3DVector = ^T3DVector;
  11.  
  12. function VectorSort( Item1 : pointer; Item2: Pointer ) : Integer;
  13.   var
  14.     Vec1 : P3DVector;
  15.     Vec2 : P3DVector;
  16.   begin
  17.     // <0 Item1 is closer, 0=Same Distance, >0 Item2 is closer
  18.     Vec1 := Item1;
  19.     Vec2 := Item2;
  20.  
  21.     result := (Vec2^.CamDistance - Vec1^.CamDistance);
  22.   end;
  23.  
  24. procedure TForm1.Button1Click(Sender: TObject);
  25.   var
  26.     fpo : TFPObjectList;
  27.     idx : integer;
  28.     Item1 : T3DVector;
  29.     Item2 : T3DVector;
  30.     Item3 : T3DVector;
  31.     Item4 : T3DVector;
  32.   begin
  33.     fpo := TFPObjectList.Create;
  34.     fpo.OwnsObjects := False;
  35.  
  36.     Item1.CamDistance := 111;
  37.     Item2.CamDistance := 222;
  38.     Item3.CamDistance := 333;
  39.     Item4.CamDistance := 444;
  40.  
  41.     fpo.Add(TObject(@Item1));
  42.     fpo.Add(TObject(@Item2));
  43.     fpo.Add(TObject(@Item3));
  44.     fpo.Add(TObject(@Item4));
  45.  
  46.     Memo1.Lines.Clear;
  47.     Memo1.Lines.Add( 'Pre-Sort' );
  48.     for idx := 0 to fpo.Count-1 do
  49.       begin
  50.         Memo1.Lines.Add( Format('Items[%d]=%d', [idx, P3DVector(fpo.Items[idx])^.CamDistance]));
  51.       end;
  52.  
  53.     Memo1.Lines.Add( '' );
  54.     Memo1.Lines.Add( 'Sorting the vectors' );
  55.     fpo.Sort(@VectorSort);
  56.  
  57.     Memo1.Lines.Add( '' );
  58.     Memo1.Lines.Add( 'Post-Sort' );
  59.     for idx := 0 to fpo.Count-1 do
  60.       begin
  61.         Memo1.Lines.Add( Format('Items[%d]=%d', [idx, P3DVector(fpo.Items[idx])^.CamDistance]));
  62.       end;
  63.  
  64.     fpo.Free;
  65.   end;
  66.  


The resulting output is:
Code: [Select]
Pre-Sort
Items[0]=111
Items[1]=222
Items[2]=333
Items[3]=444

Sorting the vectors

Post-Sort
Items[0]=444
Items[1]=333
Items[2]=222
Items[3]=111


Explanation:
The TFPObjectList is storing the pointers to all of the items, in the above case they are simply pointers to simple records existing on the stack.

When I call the "Sort" method on the TFPObjectList object I'm supplying the custom VectorSort comparison function that compares the CamDistance for each of the two objects.

In a default Sort function it would "subtract" the second item from the first item. In the above example, the VectorSort function is subtracting the first item from the second to achieve the reverse sort.
« Last Edit: August 09, 2024, 08:44:42 pm by tooldeveloper »

bytebites

  • Hero Member
  • *****
  • Posts: 674
Re: sorting array of path pointers by distance in 3D model
« Reply #3 on: August 09, 2024, 10:24:24 pm »
Use code tags.

 

TinyPortal © 2005-2018