Skip to content
Snippets Groups Projects
Commit 492eab0a authored by Timon Römer's avatar Timon Römer
Browse files

optimizes MagicWand by Paralellizing

parent 01357f06
No related branches found
No related tags found
No related merge requests found
No preview for this file type
This diff is collapsed.
#pragma once
#include "CoreMinimal.h"
#include "ProceduralMeshComponent.h"
#include "MetaCastBaseline.h"
#include "Generators/MarchingCubes.h"
#include "Templates/Atomic.h"
#include "MagicWand.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
......@@ -12,48 +12,68 @@ class UMagicWand : public UMetaCastBaseline
{
GENERATED_BODY()
// INTERNAL
TAtomic<bool> AbortMagicWand = false;
FDensityField* MyDensityField;
UPROPERTY()
UWorld* World;
bool IsMarchingCubesRunning = false;
bool AbortMarchingCubes = false;
// Critical section to protect shared variables
FThreadSafeCounter NumberThreads = 0;
mutable FCriticalSection ProceduralMeshGuard;
mutable FCriticalSection ThreadNumberLock;
UPROPERTY()
UProceduralMeshComponent* ProceduralMesh;
UPROPERTY(EditAnywhere)
UMaterialInterface* SelectionVolumeMat;
TArray<int32> FloodedIndices;
UPROPERTY()
UWorld* World;
TAtomic<float> AccumulatedTime = 0.0;
FVector SelectionStartPosition;
// USER EXPORTED
UPROPERTY(EditAnywhere)
float MarchingCubeSize = 0;
UPROPERTY(EditAnywhere)
int EvaluationsPerSecond = 10;
UPROPERTY(EditAnywhere)
float AccumulatedTime = 0.0;
UPROPERTY(EditAnywhere)
float ProximityThreshold = 0.1f;
TArray<int32> SelectedClusterIndices;
UPROPERTY(EditAnywhere)
UMaterialInterface* SelectionVolumeMat;
UPROPERTY(EditAnywhere)
int MaxThreadCount = 100;
public:
UMagicWand();
// INITIALIZATION
UMagicWand();
virtual void BeginPlay() override;
void InitReferences();
void InitProceduralMesh() const;
virtual void SelectParticles(const FVector& InputPosition) override;
virtual void EraseParticles(const FVector& InputPosition) override;
// INPUT HANDLING
virtual void HandleMetaSelectReleased(const FInputActionInstance& Instance) override;
virtual void HandleMetaSelectPressed(const FInputActionInstance& Instance) override;
virtual void HandleMetaEraseReleased(const FInputActionInstance& Instance) override;
void InitMagicWandSelection();
virtual void EraseParticles(const FVector& InputPosition) override;
// VISUALIZATION
void GenerateVoxelMeshWithCubes(const TArray<int32> Voxels) const;
void GenerateVoxelMeshSmooth(const TArray<int32> Voxels);
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
// MAGIC WAND SELECTION
void InitMagicWandSelection();
void FindSeedIndex(const FVector& InputPosition, int32 InputVoxelIndex, int32& SeedIndex) const;
void PerformMagicWandSelection(const FVector& InputPosition);
void ExpandSelection(int32 SeedIndex);
void ExpandSelectionFromPointIndex(int32 SeedIndex);
void ExpandSelectionFromPointIndexQueue(TQueue<int32>* ProcessQueue);
void ExpandThroughNeighborsInRange(TQueue<int32>* ProcessQueue, const float SquaredProximityThreshold, int& NumberToProcess, const FVector& CurrentQueuePointPosition) const;
void AbortSelection();
void CreateNewExpansionThread(TQueue<int32>* ProcessQueue, int& NumberToProcess);
// TICK
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
virtual void SelectParticles(const FVector& InputPosition) override;
};
\ No newline at end of file
This diff is collapsed.
......@@ -10,6 +10,16 @@ UMetaCastBaseline::UMetaCastBaseline() :
PrimaryComponentTick.bCanEverTick = true;
}
void UMetaCastBaseline::InitLeftHand()
{
//check if left hand is assigned otherwise assign it to self
if(!LeftHandComponent)
{
UE_LOG(LogTemp, Warning, TEXT("Left Hand not assigned. Please assign in Blueprint => Defaulting to self"));
LeftHandComponent = this;
}
}
// Called when the game starts
void UMetaCastBaseline::BeginPlay()
{
......@@ -18,13 +28,7 @@ void UMetaCastBaseline::BeginPlay()
InitPointCloudReference();
InitInputBindings();
InitSelectionObject();
//check if left hand is assigned otherwise assign it to self
if(!LeftHandComponent)
{
UE_LOG(LogTemp, Warning, TEXT("Left Hand not assigned. Please assign in Blueprint => Defaulting to self"));
LeftHandComponent = this;
}
InitLeftHand();
}
void UMetaCastBaseline::InitPointCloudReference()
......
......@@ -60,6 +60,7 @@ public:
// Sets default values for this component's properties
UMetaCastBaseline();
void InitLeftHand();
void InitInputBindings();
virtual void HandleMetaSelectPressed(const FInputActionInstance& Instance);
virtual void HandleMetaErasePressed(const FInputActionInstance& Instance);
......
......@@ -91,7 +91,7 @@ void APointCloud::SetupDensityFieldFromPointCloud() const
{
UE_LOG(LogTemp, Log, TEXT("Initializing DensityField!"));
const FIntVector AxisNumbers(100, 100, 100);
const FIntVector AxisNumbers(200, 200, 200);
MyDensityField->InitializeDensityField(this, MinBounds, MaxBounds, AxisNumbers);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment