Skip to content

Commit

Permalink
Clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
slackydev committed Dec 27, 2024
1 parent d144a4c commit 36737cf
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 30 deletions.
25 changes: 14 additions & 11 deletions Source/script/imports/simba.import_kdtree.pas
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,20 @@ implementation
simba.container_kdtree;

(*
function RefArray(): TKDNodeRefArray;
function GetItem(i:Int32): PKDNode;
function InitBranch(): Int32;
function Copy(): TKDTree;
procedure Init(const AData: TKDItems);
function IndexOf(const Value: TSingleArray): Int32;
function KNearest(Vector: TSingleArray; K: Int32; NotEqual: Boolean = False): TKDItems;
function RangeQuery(Low, High: TSingleArray): TKDItems;
function RangeQueryEx(Center: TSingleArray; Radii: TSingleArray; Hide: Boolean): TKDItems;
function KNearestClassify(Vector: TSingleArray; K: Int32): Int32;
function WeightedKNearestClassify(Vector: TSingleArray; K: Int32; NotEqual: Boolean = False): Int32;
Exposed:
function TKDTree.RefArray(): TKDNodeRefArray;
function TKDTree.GetItem(i:Int32): PKDNode;
function TKDTree.InitBranch(): Int32;
function TKDTree.Copy(): TKDTree;
procedure TKDTree.Init(const AData: TKDItems);
function TKDTree.IndexOf(const Value: TSingleArray): Int32;
function TKDTree.KNearest(Vector: TSingleArray; K: Int32; NotEqual: Boolean = False): TKDItems;
function TKDTree.RangeQuery(Low, High: TSingleArray): TKDItems;
function TKDTree.RangeQueryEx(Center: TSingleArray; Radii: TSingleArray; Hide: Boolean): TKDItems;
function TKDTree.KNearestClassify(Vector: TSingleArray; K: Int32): Int32;
function TKDTree.WeightedKNearestClassify(Vector: TSingleArray; K: Int32; NotEqual: Boolean = False): Int32;
function TKDTree.Clusters(Radii: TSingleArray): T2DKDItems;
*)

procedure _LapeKDTreeRefArray(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
Expand Down
38 changes: 20 additions & 18 deletions Source/simba.container_kdtree.pas
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ TKDTree = record
function RangeQueryEx(Center: TSingleArray; Radii: TSingleArray; Hide: Boolean): TKDItems;
function KNearestClassify(Vector: TSingleArray; K: Int32): Int32;
function WeightedKNearestClassify(Vector: TSingleArray; K: Int32): Int32;
function Clusters(radii: TSingleArray): T2DKDItems;
function Clusters(Radii: TSingleArray): T2DKDItems;
end;

PKDTree = ^TKDTree;
Expand Down Expand Up @@ -672,10 +672,10 @@ function TKDTree.WeightedKNearestClassify(Vector: TSingleArray; K: Int32): Int32
TODO: Add a version that returns T2DIntegerArray where each version represents
an index in KDTree.Data.
*)
function TKDTree.Clusters(radii: TSingleArray): T2DKDItems;
function TKDTree.Clusters(Radii: TSingleArray): T2DKDItems;
var
rescount, qcount: Int32;
sqradii: TSingleArray;
resCount, qCount: Int32;
sqRadii: TSingleArray;
sqrProduct: Double;
queue: array of PKDNode;

Expand All @@ -688,7 +688,7 @@ function TKDTree.Clusters(radii: TSingleArray): T2DKDItems;
dsqr := 0;
for i := 0 to High(p.vector) do
begin
dsqr += (Sqr(p.vector[i] - c.vector[i]) * sqradii[i]);
dsqr += (Sqr(p.vector[i] - c.vector[i]) * sqRadii[i]);
if dsqr > sqrProduct then Exit(False);
end;

Expand Down Expand Up @@ -723,12 +723,14 @@ function TKDTree.Clusters(radii: TSingleArray): T2DKDItems;
end;

var
i,j,r:Int32;
t: TIntegerArray;
i,j:Int32;
begin
SetLength(sqradii, Length(radii));
if Length(Radii) <> self.Dimensions then
raise Exception.Create('TKDTree.Clusters: Input dimensions does not match the tree');

SetLength(sqRadii, Self.Dimensions);
for i:=0 to High(radii) do
sqradii[i] := Sqr(radii[i]);
sqRadii[i] := Sqr(Radii[i]);

sqrProduct := 1.0;
for i:=0 to High(radii) do
Expand All @@ -737,28 +739,28 @@ function TKDTree.Clusters(radii: TSingleArray): T2DKDItems;
SetLength(queue, Length(self.Data));

j := 0;
for i:=0 to High(self.data) do
for i:=0 to High(self.Data) do
begin
if self.data[i].hidden then
if self.Data[i].Hidden then
continue;

SetLength(result, j+1);
SetLength(Result, j+1);
rescount := 0;
SetLength(result[j], 64);
SetLength(Result[j], 64);

qcount := 0;
queue[0] := @self.data[i];
queue[0] := @Self.Data[i];
while qcount >= 0 do
begin
Dec(qcount);
Cluster(queue[qcount+1]^.split, result[j], @self.data[0]);
Cluster(queue[qcount+1]^.Split, Result[j], @Self.Data[0]);
end;
SetLength(result[j], rescount);
SetLength(Result[j], resCount);
Inc(j);
end;

for i:=0 to High(self.data) do
self.data[i].hidden := False;
for i:=0 to High(Self.Data) do
self.data[i].Hidden := False;
end;

end.
Expand Down
10 changes: 9 additions & 1 deletion Source/simba.container_slacktree.pas
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ implementation
const
NONE = -1;

(*
TODO:
- Replace this with what's in TKDTree structure
Can for simplicty cast TPA into array of array[0..1] of Int32;
*)

function TPASelectNth_Axis(var arr:TPointArray; k, start, stop:Int32; axis:Byte=0): TPoint;
var
l,r:Int32;
Expand Down Expand Up @@ -153,7 +159,7 @@ function TSlackTree.Copy: TSlackTree;
Result.Size := Size;
end;

procedure TSlackTree.Init(TPA:TPointArray);
procedure TSlackTree.Init(TPA: TPointArray);
procedure __build(var node:TNode; left, right:Int32; depth:Int32=0);
var mid: Int32;
begin
Expand All @@ -173,6 +179,8 @@ procedure TSlackTree.Init(TPA:TPointArray);
end;
end;
begin
if Length(TPA) = 0 then Exit;

Self.Size := 0;
SetLength(self.data, Length(TPA));
__build(self.data[InitBranch()], 0, High(TPA));
Expand Down

0 comments on commit 36737cf

Please sign in to comment.