Skip to content

Commit

Permalink
Tangent + operation handling update
Browse files Browse the repository at this point in the history
  • Loading branch information
Nebukam committed Sep 15, 2024
1 parent efe92f4 commit 63de8cb
Show file tree
Hide file tree
Showing 21 changed files with 197 additions and 132 deletions.
64 changes: 38 additions & 26 deletions Source/PCGExtendedToolkit/Private/Graph/Edges/PCGExRefineEdges.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ bool FPCGExRefineEdgesElement::Boot(FPCGExContext* InContext) const
return false;
}

PCGEX_OPERATION_BIND(Refinement, UPCGExEdgeRefinePrimMST)
PCGEX_OPERATION_BIND(Refinement, UPCGExEdgeRefineOperation)
PCGEX_FWD(GraphBuilderDetails)

if (Context->Refinement->RequiresHeuristics() && !Context->bHasValidHeuristics)
Expand Down Expand Up @@ -157,46 +157,58 @@ namespace PCGExRefineEdges
if (!EdgeFilterManager->Init(Context, TypedContext->EdgeFilterFactories)) { return false; }
}

if (EdgeFilterManager)
if (Refinement->RequiresIndividualEdgeProcessing())
{
StartParallelLoopForEdges();
}
else
{
const int32 PLI = GetDefault<UPCGExGlobalSettings>()->GetClusterBatchChunkSize();

PCGEX_ASYNC_GROUP(AsyncManagerPtr, FilteredLoop)
FilteredLoop->SetOnCompleteCallback([&]() { StartRefinement(); });
FilteredLoop->StartRanges(
[&](const int32 Index, const int32 Count, const int32 LoopIdx)
FilteredLoop->SetOnCompleteCallback(
[&]()
{
EdgeFilterCache[Index] = EdgeFilterManager->Test(Index);
}, EdgesIO->GetNum(), PLI);
}
else
{
StartRefinement();
if (Refinement->RequiresIndividualNodeProcessing()) { StartParallelLoopForNodes(); }
else { Refinement->Process(); }
});
FilteredLoop->SetOnIterationRangeStartCallback(
[&](const int32 StartIndex, const int32 Count, const int32 LoopIdx) { PrepareSingleLoopScopeForEdges(StartIndex, Count); });
}

return true;
}

void FProcessor::StartRefinement()
void FProcessor::ProcessSingleNode(const int32 Index, PCGExCluster::FNode& Node, const int32 LoopIdx, const int32 Count)
{
if (Refinement->InvalidateAllEdgesBeforeProcessing())
Refinement->ProcessNode(Node);
}

void FProcessor::PrepareSingleLoopScopeForEdges(const uint32 StartIndex, const int32 Count)
{
const int32 MaxIndex = StartIndex + Count;

if (EdgeFilterManager)
{
TArray<PCGExGraph::FIndexedEdge>& Edges = *Cluster->Edges;
for (int i = 0; i < Cluster->Edges->Num(); i++)
if (Refinement->InvalidateAllEdgesBeforeProcessing())
{
PCGExGraph::FIndexedEdge& Edge = Edges[i];
Edge.bValid = !EdgeFilterCache[i];
TArray<PCGExGraph::FIndexedEdge>& Edges = *Cluster->Edges;
for (int i = StartIndex; i < MaxIndex; i++)
{
EdgeFilterCache[i] = EdgeFilterManager->Test(i);
Edges[i].bValid = !EdgeFilterCache[i];
}
}
else
{
for (int i = StartIndex; i < MaxIndex; i++) { EdgeFilterCache[i] = EdgeFilterManager->Test(i); }
}
}

if (Refinement->RequiresIndividualNodeProcessing()) { StartParallelLoopForNodes(); }
else if (Refinement->RequiresIndividualEdgeProcessing()) { StartParallelLoopForEdges(); }
else { Refinement->Process(); }
}

void FProcessor::ProcessSingleNode(const int32 Index, PCGExCluster::FNode& Node, const int32 LoopIdx, const int32 Count)
{
Refinement->ProcessNode(Node);
else if (Refinement->InvalidateAllEdgesBeforeProcessing())
{
TArray<PCGExGraph::FIndexedEdge>& Edges = *Cluster->Edges;
for (int i = StartIndex; i < MaxIndex; i++) { Edges[i].bValid = !EdgeFilterCache[i]; }
}
}

void FProcessor::ProcessSingleEdge(const int32 EdgeIndex, PCGExGraph::FIndexedEdge& Edge, const int32 LoopIdx, const int32 Count)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ bool FPCGExRelaxClustersElement::Boot(FPCGExContext* InContext) const

PCGEX_CONTEXT_AND_SETTINGS(RelaxClusters)

PCGEX_OPERATION_BIND(Relaxing, UPCGExForceDirectedRelax)
PCGEX_OPERATION_BIND(Relaxing, UPCGExRelaxClusterOperation)

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ bool FPCGExPathfindingEdgesElement::Boot(FPCGExContext* InContext) const

PCGEX_CONTEXT_AND_SETTINGS(PathfindingEdges)

PCGEX_OPERATION_BIND(GoalPicker, UPCGExGoalPickerRandom)
PCGEX_OPERATION_BIND(SearchAlgorithm, UPCGExSearchAStar)
PCGEX_OPERATION_BIND(GoalPicker, UPCGExGoalPicker)
PCGEX_OPERATION_BIND(SearchAlgorithm, UPCGExSearchOperation)

PCGExData::FPointIO* SeedsPoints = nullptr;
PCGExData::FPointIO* GoalsPoints = nullptr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ bool FPCGExPathfindingNavmeshElement::Boot(FPCGExContext* InContext) const

PCGEX_CONTEXT_AND_SETTINGS(PathfindingNavmesh)

PCGEX_OPERATION_BIND(GoalPicker, UPCGExGoalPickerRandom)
PCGEX_OPERATION_BIND(Blending, UPCGExSubPointsBlendInterpolate)
PCGEX_OPERATION_BIND(GoalPicker, UPCGExGoalPicker)
PCGEX_OPERATION_BIND(Blending, UPCGExSubPointsBlendOperation)

PCGExData::FPointIO* SeedsPoints = nullptr;
PCGExData::FPointIO* GoalsPoints = nullptr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ bool FPCGExPathfindingPlotEdgesElement::Boot(FPCGExContext* InContext) const

PCGEX_CONTEXT_AND_SETTINGS(PathfindingPlotEdges)

PCGEX_OPERATION_BIND(SearchAlgorithm, UPCGExSearchAStar)
PCGEX_OPERATION_BIND(SearchAlgorithm, UPCGExSearchOperation)

Context->OutputPaths = new PCGExData::FPointIOCollection(Context);
Context->Plots = new PCGExData::FPointIOCollection(Context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ bool FPCGExPathfindingPlotNavmeshElement::Boot(FPCGExContext* InContext) const

PCGEX_CONTEXT_AND_SETTINGS(PathfindingPlotNavmesh)

PCGEX_OPERATION_BIND(Blending, UPCGExSubPointsBlendInterpolate)
PCGEX_OPERATION_BIND(Blending, UPCGExSubPointsBlendOperation)

Context->OutputPaths = new PCGExData::FPointIOCollection(Context);

Expand Down
2 changes: 1 addition & 1 deletion Source/PCGExtendedToolkit/Private/Paths/PCGExOrient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ bool FPCGExOrientElement::Boot(FPCGExContext* InContext) const
PCGEX_VALIDATE_NAME(Settings->DotAttribute);
}

PCGEX_OPERATION_BIND(Orientation, UPCGExOrientAverage)
PCGEX_OPERATION_BIND(Orientation, UPCGExOrientOperation)
Context->Orientation->bClosedPath = Settings->bClosedPath;
Context->Orientation->OrientAxis = Settings->OrientAxis;
Context->Orientation->UpAxis = Settings->UpAxis;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ bool FPCGExPathCrossingsElement::Boot(FPCGExContext* InContext) const
if (Settings->bWriteAlpha) { PCGEX_VALIDATE_NAME(Settings->CrossingAlphaAttributeName) }
if (Settings->bWriteCrossDirection) { PCGEX_VALIDATE_NAME(Settings->CrossDirectionAttributeName) }

PCGEX_OPERATION_BIND(Blending, UPCGExSubPointsBlendInterpolate)
PCGEX_OPERATION_BIND(Blending, UPCGExSubPointsBlendOperation)
Context->Blending->bClosedPath = Settings->bClosedPath;

GetInputFactories(Context, PCGExPaths::SourceCanCutFilters, Context->CanCutFilterFactories, PCGExFactories::PointFilters, false);
Expand Down
2 changes: 1 addition & 1 deletion Source/PCGExtendedToolkit/Private/Paths/PCGExSmooth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ bool FPCGExSmoothElement::Boot(FPCGExContext* InContext) const
if (!FPCGExPathProcessorElement::Boot(InContext)) { return false; }

PCGEX_CONTEXT_AND_SETTINGS(Smooth)
PCGEX_OPERATION_BIND(SmoothingMethod, UPCGExMovingAverageSmoothing)
PCGEX_OPERATION_BIND(SmoothingMethod, UPCGExSmoothingOperation)

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion Source/PCGExtendedToolkit/Private/Paths/PCGExSubdivide.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ bool FPCGExSubdivideElement::Boot(FPCGExContext* InContext) const
if (Settings->bFlagSubPoints) { PCGEX_VALIDATE_NAME(Settings->SubPointFlagName) }
if (Settings->bWriteAlpha) { PCGEX_VALIDATE_NAME(Settings->AlphaAttributeName) }

PCGEX_OPERATION_BIND(Blending, UPCGExSubPointsBlendInterpolate)
PCGEX_OPERATION_BIND(Blending, UPCGExSubPointsBlendOperation)
Context->Blending->bClosedPath = Settings->bClosedPath;

return true;
Expand Down
56 changes: 47 additions & 9 deletions Source/PCGExtendedToolkit/Private/Paths/PCGExWriteTangents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,27 @@ bool FPCGExWriteTangentsElement::Boot(FPCGExContext* InContext) const

PCGEX_CONTEXT_AND_SETTINGS(WriteTangents)

PCGEX_OPERATION_BIND(Tangents, UPCGExZeroTangents)

PCGEX_OPERATION_BIND(Tangents, UPCGExTangentsOperation)
Context->Tangents->bClosedPath = Settings->bClosedPath;
Context->Tangents->ArriveScale = Settings->ArriveScale;
Context->Tangents->LeaveScale = Settings->LeaveScale;

if (Settings->StartTangents)
{
Context->StartTangents = Context->RegisterOperation<UPCGExTangentsOperation>(Settings->StartTangents);
Context->StartTangents->bClosedPath = Settings->bClosedPath;
Context->StartTangents->ArriveScale = Settings->ArriveScale;
Context->StartTangents->LeaveScale = Settings->LeaveScale;
}

if (Settings->EndTangents)
{
Context->EndTangents = Context->RegisterOperation<UPCGExTangentsOperation>(Settings->EndTangents);
Context->EndTangents->bClosedPath = Settings->bClosedPath;
Context->EndTangents->ArriveScale = Settings->ArriveScale;
Context->EndTangents->LeaveScale = Settings->LeaveScale;
}


PCGEX_VALIDATE_NAME(Settings->ArriveName)
PCGEX_VALIDATE_NAME(Settings->LeaveName)
Expand Down Expand Up @@ -82,6 +100,8 @@ namespace PCGExWriteTangents
{
FProcessor::~FProcessor()
{
if (LocalTypedContext->StartTangents) { PCGEX_DELETE_OPERATION(StartTangents) }
if (LocalTypedContext->EndTangents) { PCGEX_DELETE_OPERATION(EndTangents) }
}

bool FProcessor::Process(PCGExMT::FTaskManager* AsyncManager)
Expand All @@ -93,10 +113,28 @@ namespace PCGExWriteTangents
if (!FPointsProcessor::Process(AsyncManager)) { return false; }

LocalSettings = Settings;
LocalTypedContext = TypedContext;

bClosedPath = Settings->bClosedPath;

Tangents = Cast<UPCGExTangentsOperation>(PrimaryOperation);
Tangents->PrepareForData(PointDataFacade);
Tangents->PrepareForData();

if (TypedContext->StartTangents)
{
StartTangents = TypedContext->StartTangents->CopyOperation<UPCGExTangentsOperation>();
StartTangents->PrimaryDataFacade = PointDataFacade;
StartTangents->PrepareForData();
}
else { StartTangents = Tangents; }

if (TypedContext->EndTangents)
{
EndTangents = TypedContext->EndTangents->CopyOperation<UPCGExTangentsOperation>();
EndTangents->PrimaryDataFacade = PointDataFacade;
EndTangents->PrepareForData();
}
else { EndTangents = Tangents; }

ArriveWriter = PointDataFacade->GetWriter(Settings->ArriveName, FVector::ZeroVector, true, false);
LeaveWriter = PointDataFacade->GetWriter(Settings->LeaveName, FVector::ZeroVector, true, false);
Expand Down Expand Up @@ -130,17 +168,17 @@ namespace PCGExWriteTangents
}
else
{
if (PrevIndex >= 0 && NextIndex <= LastIndex)
if (Index == 0)
{
Tangents->ProcessPoint(PointIO->GetIn()->GetPoints(), Index, NextIndex, PrevIndex, OutArrive, OutLeave);
StartTangents->ProcessFirstPoint(PointIO->GetIn()->GetPoints(), OutArrive, OutLeave);
}
else if (PrevIndex < 0)
else if (Index == LastIndex)
{
Tangents->ProcessFirstPoint(PointIO->GetIn()->GetPoints(), OutArrive, OutLeave);
EndTangents->ProcessLastPoint(PointIO->GetIn()->GetPoints(), OutArrive, OutLeave);
}
else if (NextIndex > LastIndex)
else
{
Tangents->ProcessLastPoint(PointIO->GetIn()->GetPoints(), OutArrive, OutLeave);
Tangents->ProcessPoint(PointIO->GetIn()->GetPoints(), Index, NextIndex, PrevIndex, OutArrive, OutLeave);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ namespace PCGExRefineEdges
protected:
PCGExPointFilter::TManager* EdgeFilterManager = nullptr;
EPCGExRefineSanitization Sanitization = EPCGExRefineSanitization::None;

virtual PCGExCluster::FCluster* HandleCachedCluster(const PCGExCluster::FCluster* InClusterRef) override;
mutable FRWLock NodeLock;

Expand All @@ -127,8 +127,9 @@ namespace PCGExRefineEdges
virtual ~FProcessor() override;

virtual bool Process(PCGExMT::FTaskManager* AsyncManager) override;
void StartRefinement();
virtual void ProcessSingleNode(const int32 Index, PCGExCluster::FNode& Node, const int32 LoopIdx, const int32 Count) override;

virtual void PrepareSingleLoopScopeForEdges(const uint32 StartIndex, const int32 Count) override;
virtual void ProcessSingleEdge(const int32 EdgeIndex, PCGExGraph::FIndexedEdge& Edge, const int32 LoopIdx, const int32 Count) override;
void Sanitize();
void InsertEdges() const;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright Timothé Lapetite 2024
// Released under the MIT license https://opensource.org/license/MIT/

#pragma once

#include "CoreMinimal.h"
#include "PCGExEdgeRefineOperation.h"
#include "PCGExEdgeRefineKeepByFilter.generated.h"

namespace PCGExCluster
{
struct FNode;
}

/**
*
*/
UCLASS(MinimalAPI, BlueprintType, meta=(DisplayName="Keep by Filter"))
class /*PCGEXTENDEDTOOLKIT_API*/ UPCGExEdgeKeepByFilter : public UPCGExEdgeRefineOperation
{
GENERATED_BODY()

public:
virtual bool RequiresIndividualEdgeProcessing() override { return true; }

virtual void ProcessEdge(PCGExGraph::FIndexedEdge& Edge) override
{
FPlatformAtomics::InterlockedExchange(&Edge.bValid, *(EdgesFilters->GetData() + Edge.EdgeIndex) ? 0 : 1);
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright Timothé Lapetite 2024
// Released under the MIT license https://opensource.org/license/MIT/

#pragma once

#include "CoreMinimal.h"
#include "PCGExEdgeRefineOperation.h"
#include "PCGExEdgeRefineRemoveByFilter.generated.h"

namespace PCGExCluster
{
struct FNode;
}

/**
*
*/
UCLASS(MinimalAPI, BlueprintType, meta=(DisplayName="Remove by Filter"))
class /*PCGEXTENDEDTOOLKIT_API*/ UPCGExEdgeRemoveByFilter : public UPCGExEdgeRefineOperation
{
GENERATED_BODY()

public:
virtual bool RequiresIndividualEdgeProcessing() override { return true; }

virtual void ProcessEdge(PCGExGraph::FIndexedEdge& Edge) override
{
FPlatformAtomics::InterlockedExchange(&Edge.bValid, *(EdgesFilters->GetData() + Edge.EdgeIndex) ? 1 : 0);
}

};
19 changes: 5 additions & 14 deletions Source/PCGExtendedToolkit/Public/PCGExPointsProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,21 +222,12 @@ struct /*PCGEXTENDEDTOOLKIT_API*/ FPCGExPointsProcessorContext : public FPCGExCo
#pragma endregion

template <typename T>
T* RegisterOperation(UPCGExOperation* Operation = nullptr)
T* RegisterOperation(UPCGExOperation* BaseOperation)
{
T* RetValue;
if (!Operation)
{
FGCScopeGuard GCGuarded;
RetValue = NewObject<T>();
RetValue->AddToRoot();
OwnedProcessorOperations.Add(RetValue);
}
else
{
RetValue = static_cast<T*>(Operation);
PCGEX_SETTINGS_LOCAL(PointsProcessor)
}
BaseOperation->BindContext(this); // Temp so Copy doesn't crash

T* RetValue = BaseOperation->CopyOperation<T>();
OwnedProcessorOperations.Add(RetValue);
RetValue->BindContext(this);
return RetValue;
}
Expand Down
Loading

0 comments on commit 63de8cb

Please sign in to comment.