Skip to content

Commit 7bac928

Browse files
902D9902D9
andauthored
Add: Add the library function "Collect From Kdtree (Box)" (#9)
Co-authored-by: 902D9 <[email protected]>
1 parent 946139d commit 7bac928

File tree

6 files changed

+127
-3
lines changed

6 files changed

+127
-3
lines changed

Kdtree/Source/Kdtree/Private/AsyncKdtreeBPLibrary.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,75 @@ void UAsyncKdtreeBPLibrary::CollectFromKdtreeAsync(const UObject* WorldContextOb
155155
}
156156
}
157157
}
158+
159+
struct FCollectFromKdtreeBoxTaskParams
160+
{
161+
const FKdtree* Tree;
162+
FBox Box;
163+
TArray<int>* Indices;
164+
TArray<FVector>* Data;
165+
};
166+
167+
class FCollectFromKdtreeBoxTask : public FNonAbandonableTask
168+
{
169+
public:
170+
FCollectFromKdtreeBoxTask(const FCollectFromKdtreeBoxTaskParams& InParams) : Params(InParams)
171+
{
172+
}
173+
174+
void DoWork()
175+
{
176+
KdtreeInternal::CollectFromKdtree(Params.Tree->Internal, Params.Box, Params.Indices);
177+
for (int Index = 0; Index < Params.Indices->Num(); ++Index)
178+
{
179+
Params.Data->Add(Params.Tree->Internal.Data[(*Params.Indices)[Index]]);
180+
}
181+
}
182+
183+
FORCEINLINE TStatId GetStatId() const
184+
{
185+
RETURN_QUICK_DECLARE_CYCLE_STAT(FCollectFromKdtreeBoxTask, STATGROUP_ThreadPoolAsyncTasks);
186+
}
187+
188+
private:
189+
FCollectFromKdtreeBoxTaskParams Params;
190+
};
191+
192+
class FCollectFromKdtreeBoxAction : public FPendingLatentAction
193+
{
194+
public:
195+
FLatentActionInfo LatentInfo;
196+
FAsyncTask<FCollectFromKdtreeBoxTask>* Task;
197+
198+
FCollectFromKdtreeBoxAction(const FLatentActionInfo& InLatentInfo, const FKdtree* Tree, const FBox Box,
199+
TArray<int>* Indices, TArray<FVector>* Data)
200+
: LatentInfo(InLatentInfo), Task(nullptr)
201+
{
202+
FCollectFromKdtreeBoxTaskParams Params;
203+
Params.Tree = Tree;
204+
Params.Box = Box;
205+
Params.Indices = Indices;
206+
Params.Data = Data;
207+
Task = new FAsyncTask<FCollectFromKdtreeBoxTask>(Params);
208+
Task->StartBackgroundTask();
209+
}
210+
211+
void UpdateOperation(FLatentResponse& Response) override
212+
{
213+
Response.FinishAndTriggerIf(Task->IsDone(), LatentInfo.ExecutionFunction, LatentInfo.Linkage, LatentInfo.CallbackTarget);
214+
}
215+
};
216+
217+
void UAsyncKdtreeBPLibrary::CollectFromKdtreeAsyncBox(const UObject* WorldContextObject, const FKdtree& Tree, const FBox Box,
218+
TArray<int>& Indices, TArray<FVector>& Data, FLatentActionInfo LatentInfo)
219+
{
220+
if (UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::LogAndReturnNull))
221+
{
222+
FLatentActionManager& LatentManager = World->GetLatentActionManager();
223+
if (LatentManager.FindExistingAction<FCollectFromKdtreeBoxAction>(LatentInfo.CallbackTarget, LatentInfo.UUID) == nullptr)
224+
{
225+
FCollectFromKdtreeBoxAction* NewAction = new FCollectFromKdtreeBoxAction(LatentInfo, &Tree, Box, &Indices, &Data);
226+
LatentManager.AddNewAction(LatentInfo.CallbackTarget, LatentInfo.UUID, NewAction);
227+
}
228+
}
229+
}

Kdtree/Source/Kdtree/Private/KdtreeBPLibrary.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ void UKdtreeBPLibrary::CollectFromKdtree(
3636
}
3737
}
3838

39+
void UKdtreeBPLibrary::CollectFromKdtreeBox(const FKdtree& Tree, const FBox Box, TArray<int>& Indices,
40+
TArray<FVector>& Data)
41+
{
42+
KdtreeInternal::CollectFromKdtree(Tree.Internal, Box, &Indices);
43+
for (int Index = 0; Index < Indices.Num(); ++Index)
44+
{
45+
Data.Add(Tree.Internal.Data[Indices[Index]]);
46+
}
47+
}
48+
3949
void UKdtreeBPLibrary::ValidateKdtree(const FKdtree& Tree)
4050
{
4151
KdtreeInternal::ValidateKdtree(Tree.Internal);

Kdtree/Source/Kdtree/Private/KdtreeInternal.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,32 @@ void CollectFromKdtree(
234234
}
235235
}
236236
}
237-
} // namespace
237+
238+
void CollectFromKdtree(
239+
const FKdtreeInternal& Tree, const FKdtreeNode* Node, const FBox& Box, TArray<int>* Result)
240+
{
241+
if (Node == nullptr)
242+
{
243+
return;
244+
}
245+
246+
const FVector& Current = Tree.Data[Node->Index];
247+
if (Box.IsInsideOrOn(Current))
248+
{
249+
Result->Add(Node->Index);
250+
}
251+
252+
int Axis = Node->Axis;
253+
if (Box.Min[Axis] < Current[Axis])
254+
{
255+
CollectFromKdtree(Tree, Node->ChildLeft, Box, Result);
256+
}
257+
if (Box.Max[Axis] > Current[Axis])
258+
{
259+
CollectFromKdtree(Tree, Node->ChildRight, Box, Result);
260+
}
261+
}
262+
} // namespace
238263

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

290+
void CollectFromKdtree(const FKdtreeInternal& Tree, const FBox& Box, TArray<int>* Result)
291+
{
292+
CollectFromKdtree(Tree, Tree.Root, Box, Result);
293+
}
294+
265295
void ValidateKdtree(const FKdtreeInternal& Tree)
266296
{
267297
ValidateKdtree(Tree, Tree.Root, 0);

Kdtree/Source/Kdtree/Private/KdtreeInternal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ struct FKdtreeNode
2424
void BuildKdtree(FKdtreeInternal* Tree, const TArray<FVector>& Data);
2525
void ClearKdtree(FKdtreeInternal* Tree);
2626
void CollectFromKdtree(const FKdtreeInternal& Tree, const FVector& Center, float Radius, TArray<int>* Result);
27+
void CollectFromKdtree(const FKdtreeInternal& Tree, const FBox& Box, TArray<int>* Result);
2728
void ValidateKdtree(const FKdtreeInternal& Tree);
2829
void DumpKdTree(const FKdtreeInternal& Tree);
2930
} // namespace KdtreeInternal

Kdtree/Source/Kdtree/Public/AsyncKdtreeBPLibrary.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,14 @@ class KDTREE_API UAsyncKdtreeBPLibrary : public UBlueprintFunctionLibrary
3232
UFUNCTION(BlueprintCallable,
3333
meta = (WorldContextObject = "WorldContextObject", Latent, LatentInfo = "LatentInfo", HidePin = "WorldContextObject",
3434
DefaultToSelf = "WorldContextObject"),
35-
Category = "SpacialDataStructure|kd-tree")
35+
Category = "SpacialDataStructure|kd-tree", DisplayName = "Collect From Kdtree Async (Sphere)")
3636
static void CollectFromKdtreeAsync(const UObject* WorldContextObject, const FKdtree& Tree, const FVector Center, float Radius,
3737
TArray<int>& Indices, TArray<FVector>& Data, FLatentActionInfo LatentInfo);
38+
39+
UFUNCTION(BlueprintCallable,
40+
meta = (WorldContextObject = "WorldContextObject", Latent, LatentInfo = "LatentInfo", HidePin = "WorldContextObject",
41+
DefaultToSelf = "WorldContextObject"),
42+
Category = "SpacialDataStructure|kd-tree", DisplayName= "Collect From Kdtree Async (Box)")
43+
static void CollectFromKdtreeAsyncBox(const UObject* WorldContextObject, const FKdtree& Tree, const FBox Box,
44+
TArray<int>& Indices, TArray<FVector>& Data, FLatentActionInfo LatentInfo);
3845
};

Kdtree/Source/Kdtree/Public/KdtreeBPLibrary.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,14 @@ class KDTREE_API UKdtreeBPLibrary : public UBlueprintFunctionLibrary
2828
UFUNCTION(BluePrintCallable, Category = "SpacialDataStructure|kd-tree")
2929
static void ClearKdtree(UPARAM(ref) FKdtree& Tree);
3030

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

35+
UFUNCTION(BlueprintCallable, Category = "SpacialDataStructure|kd-tree", DisplayName="Collect From Kdtree (Box)")
36+
static void CollectFromKdtreeBox(
37+
const FKdtree& Tree, const FBox Box, TArray<int>& Indices, TArray<FVector>& Data);
38+
3539
UFUNCTION(BluePrintCallable, Category = "SpacialDataStructure|kd-tree")
3640
static void ValidateKdtree(const FKdtree& Tree);
3741

0 commit comments

Comments
 (0)