Skip to content

Update: Add a Kdtree collection type, collect from Kdtree at a box #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions Kdtree/Source/Kdtree/Private/AsyncKdtreeBPLibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,67 @@ void UAsyncKdtreeBPLibrary::CollectFromKdtreeAsync(const UObject* WorldContextOb
}
}
}

class FCollectFromKdtreeBoxTask : public FNonAbandonableTask
{
public:
FCollectFromKdtreeBoxTask(const FCollectFromKdtreeTaskParams& InParams, const FBox& InBox) : Params(InParams), Box(InBox)
{
}

void DoWork()
{
KdtreeInternal::CollectFromKdtree(Params.Tree->Internal, Box, Params.Indices);
for (int Index = 0; Index < Params.Indices->Num(); ++Index)
{
Params.Data->Add(Params.Tree->Internal.Data[(*Params.Indices)[Index]]);
}
}

FORCEINLINE TStatId GetStatId() const
{
RETURN_QUICK_DECLARE_CYCLE_STAT(FCollectFromKdtreeTask, STATGROUP_ThreadPoolAsyncTasks);
}

private:
FCollectFromKdtreeTaskParams Params;
FBox Box;
};

class FCollectFromKdtreeBoxAction : public FPendingLatentAction
{
public:
FLatentActionInfo LatentInfo;
FAsyncTask<FCollectFromKdtreeBoxTask>* Task;

FCollectFromKdtreeBoxAction(const FLatentActionInfo& InLatentInfo, const FKdtree* Tree, const FBox Box,
TArray<int>* Indices, TArray<FVector>* Data)
: LatentInfo(InLatentInfo), Task(nullptr)
{
FCollectFromKdtreeTaskParams Params;
Params.Tree = Tree;
Params.Indices = Indices;
Params.Data = Data;
Task = new FAsyncTask<FCollectFromKdtreeBoxTask>(Params, Box);
Task->StartBackgroundTask();
}

void UpdateOperation(FLatentResponse& Response) override
{
Response.FinishAndTriggerIf(Task->IsDone(), LatentInfo.ExecutionFunction, LatentInfo.Linkage, LatentInfo.CallbackTarget);
}
};

void UAsyncKdtreeBPLibrary::CollectFromKdtreeAsync_Box(const UObject* WorldContextObject, const FKdtree& Tree, const FBox Box,
TArray<int>& Indices, TArray<FVector>& Data, FLatentActionInfo LatentInfo)
{
if (UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::LogAndReturnNull))
{
FLatentActionManager& LatentManager = World->GetLatentActionManager();
if (LatentManager.FindExistingAction<FCollectFromKdtreeAction>(LatentInfo.CallbackTarget, LatentInfo.UUID) == nullptr)
{
FCollectFromKdtreeBoxAction* NewAction = new FCollectFromKdtreeBoxAction(LatentInfo, &Tree, Box, &Indices, &Data);
LatentManager.AddNewAction(LatentInfo.CallbackTarget, LatentInfo.UUID, NewAction);
}
}
}
10 changes: 10 additions & 0 deletions Kdtree/Source/Kdtree/Private/KdtreeBPLibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ void UKdtreeBPLibrary::CollectFromKdtree(
}
}

void UKdtreeBPLibrary::CollectFromKdtree_Box(const FKdtree& Tree, const FBox Box, TArray<int>& Indices,
TArray<FVector>& Data)
{
KdtreeInternal::CollectFromKdtree(Tree.Internal, Box, &Indices);
for (int Index = 0; Index < Indices.Num(); ++Index)
{
Data.Add(Tree.Internal.Data[Indices[Index]]);
}
}

void UKdtreeBPLibrary::ValidateKdtree(const FKdtree& Tree)
{
KdtreeInternal::ValidateKdtree(Tree.Internal);
Expand Down
32 changes: 31 additions & 1 deletion Kdtree/Source/Kdtree/Private/KdtreeInternal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,32 @@ void CollectFromKdtree(
}
}
}
} // namespace

void CollectFromKdtree(
const FKdtreeInternal& Tree, const FKdtreeNode* Node, const FBox& Box, TArray<int>* Result)
{
if (Node == nullptr)
{
return;
}

const FVector& Current = Tree.Data[Node->Index];
if (Box.IsInsideOrOn(Current))
{
Result->Add(Node->Index);
}

int Axis = Node->Axis;
if (Box.Min[Axis] < Current[Axis])
{
CollectFromKdtree(Tree, Node->ChildLeft, Box, Result);
}
if (Box.Max[Axis] > Current[Axis])
{
CollectFromKdtree(Tree, Node->ChildRight, Box, Result);
}
}
} // namespace

void BuildKdtree(FKdtreeInternal* Tree, const TArray<FVector>& Data)
{
Expand Down Expand Up @@ -262,6 +287,11 @@ void CollectFromKdtree(const FKdtreeInternal& Tree, const FVector& Center, float
CollectFromKdtree(Tree, Tree.Root, Center, Radius, Result);
}

void CollectFromKdtree(const FKdtreeInternal& Tree, const FBox& Box, TArray<int>* Result)
{
CollectFromKdtree(Tree, Tree.Root, Box, Result);
}

void ValidateKdtree(const FKdtreeInternal& Tree)
{
ValidateKdtree(Tree, Tree.Root, 0);
Expand Down
1 change: 1 addition & 0 deletions Kdtree/Source/Kdtree/Private/KdtreeInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ struct FKdtreeNode
void BuildKdtree(FKdtreeInternal* Tree, const TArray<FVector>& Data);
void ClearKdtree(FKdtreeInternal* Tree);
void CollectFromKdtree(const FKdtreeInternal& Tree, const FVector& Center, float Radius, TArray<int>* Result);
void CollectFromKdtree(const FKdtreeInternal& Tree, const FBox& Box, TArray<int>* Result);
void ValidateKdtree(const FKdtreeInternal& Tree);
void DumpKdTree(const FKdtreeInternal& Tree);
} // namespace KdtreeInternal
9 changes: 8 additions & 1 deletion Kdtree/Source/Kdtree/Public/AsyncKdtreeBPLibrary.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ class KDTREE_API UAsyncKdtreeBPLibrary : public UBlueprintFunctionLibrary
UFUNCTION(BlueprintCallable,
meta = (WorldContextObject = "WorldContextObject", Latent, LatentInfo = "LatentInfo", HidePin = "WorldContextObject",
DefaultToSelf = "WorldContextObject"),
Category = "SpacialDataStructure|kd-tree")
Category = "SpacialDataStructure|kd-tree", DisplayName = "Collect From Kdtree Async (Sphere)")
static void CollectFromKdtreeAsync(const UObject* WorldContextObject, const FKdtree& Tree, const FVector Center, float Radius,
TArray<int>& Indices, TArray<FVector>& Data, FLatentActionInfo LatentInfo);

UFUNCTION(BlueprintCallable,
meta = (WorldContextObject = "WorldContextObject", Latent, LatentInfo = "LatentInfo", HidePin = "WorldContextObject",
DefaultToSelf = "WorldContextObject"),
Category = "SpacialDataStructure|kd-tree", DisplayName= "Collect From Kdtree Async (Box)")
static void CollectFromKdtreeAsync_Box(const UObject* WorldContextObject, const FKdtree& Tree, const FBox Box,
TArray<int>& Indices, TArray<FVector>& Data, FLatentActionInfo LatentInfo);
};
6 changes: 5 additions & 1 deletion Kdtree/Source/Kdtree/Public/KdtreeBPLibrary.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ class KDTREE_API UKdtreeBPLibrary : public UBlueprintFunctionLibrary
UFUNCTION(BluePrintCallable, Category = "SpacialDataStructure|kd-tree")
static void ClearKdtree(UPARAM(ref) FKdtree& Tree);

UFUNCTION(BluePrintCallable, Category = "SpacialDataStructure|kd-tree")
UFUNCTION(BlueprintCallable, Category = "SpacialDataStructure|kd-tree", DisplayName="Collect From Kdtree (Sphere)")
static void CollectFromKdtree(
const FKdtree& Tree, const FVector Center, float Radius, TArray<int>& Indices, TArray<FVector>& Data);

UFUNCTION(BlueprintCallable, Category = "SpacialDataStructure|kd-tree", DisplayName="Collect From Kdtree (Box)")
static void CollectFromKdtree_Box(
const FKdtree& Tree, const FBox Box, TArray<int>& Indices, TArray<FVector>& Data);

UFUNCTION(BluePrintCallable, Category = "SpacialDataStructure|kd-tree")
static void ValidateKdtree(const FKdtree& Tree);

Expand Down