Advertisement
WarPie90

Cluster - kdtree

Mar 17th, 2023 (edited)
1,241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.98 KB | None | 0 0
  1. // This builds on TSlackTree which is a 2D implementation of KD-Tree.
  2. // wrote this to see how well it would work (Comparable to SplitTPA)
  3. // It's a lot faster as soon as we are talking about +/-400 points and up.
  4. // This is O(n log n), and effectively come close to linear performance.
  5.  
  6. function TSlackTree.Clusters(radX,radY: Double): T2DPointArray;
  7. var
  8.   rescount, qcount: Int32;
  9.   sqx, sqy, sqxy: Double;
  10.  
  11.   function Fits(p,c: TPoint): Boolean;
  12.   begin
  13.     //Result := (Abs(p.x-c.x) < radX) and (Abs(p.y-c.y) < radY);
  14.     Result := (Sqr(p.x-c.x)*sqy)+(Sqr(p.y-c.y)*sqx) <= sqxy;
  15.   end;
  16.  
  17.   procedure Cluster(test: TPoint; var result:TPointArray; this: PSlackNode; depth:Int32=0);
  18.   var
  19.     goright:Boolean = False;
  20.     goleft: Boolean = False;
  21.   begin
  22.     if depth and 1 = 0 then begin
  23.       goleft  := test.x - radX <= this^.split.x;
  24.       goright := test.x + radX >= this^.split.x;
  25.     end else begin
  26.       goleft  := test.y - radY <= this^.split.y;
  27.       goright := test.y + radY >= this^.split.y;
  28.     end;
  29.  
  30.     if (not this^.hidden) and ((goleft=goright)=True) and Fits(test, this^.split) then
  31.     begin
  32.       if rescount = Length(result) then Setlength(result, rescount*2);
  33.       result[rescount] := this^.split;
  34.       Inc(rescount);
  35.       this^.hidden := True;
  36.       Cluster(this^.split, result, @self.data[0], 0);
  37.     end;
  38.  
  39.     if goleft  and (this^.l <> -1) then Cluster(test, result, @self.data[this^.l], depth+1);
  40.     if goright and (this^.r <> -1) then Cluster(test, result, @self.data[this^.r], depth+1);
  41.   end;
  42. var
  43.   i,j,r:Int32;
  44.   t: TIntegerArray;
  45. begin
  46.   sqx := Sqr(radx);
  47.   sqy := Sqr(rady);
  48.   sqxy := sqx*sqy;
  49.  
  50.   j := 0;
  51.   for i:=0 to High(self.data) do
  52.   begin
  53.     if self.data[i].hidden then
  54.       continue;
  55.  
  56.     SetLength(result, j+1);
  57.     rescount := 0;
  58.     SetLength(result[j], 64);
  59.  
  60.     Cluster(self.data[i].split, result[j], @self.data[0]);
  61.     self.data[i].hidden := True;
  62.     SetLength(result[j], rescount);
  63.     Inc(j);
  64.   end;
  65. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement