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 #pragma once
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "ProceduralMeshComponent.h" #include "ProceduralMeshComponent.h"
#include "MetaCastBaseline.h" #include "MetaCastBaseline.h"
#include "Generators/MarchingCubes.h" #include "Generators/MarchingCubes.h"
#include "Templates/Atomic.h"
#include "MagicWand.generated.h" #include "MagicWand.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) ) UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
...@@ -12,48 +12,68 @@ class UMagicWand : public UMetaCastBaseline ...@@ -12,48 +12,68 @@ class UMagicWand : public UMetaCastBaseline
{ {
GENERATED_BODY() GENERATED_BODY()
// INTERNAL
TAtomic<bool> AbortMagicWand = false;
FDensityField* MyDensityField; FDensityField* MyDensityField;
UPROPERTY()
UWorld* World;
bool IsMarchingCubesRunning = false; bool IsMarchingCubesRunning = false;
bool AbortMarchingCubes = false; bool AbortMarchingCubes = false;
FThreadSafeCounter NumberThreads = 0;
// Critical section to protect shared variables
mutable FCriticalSection ProceduralMeshGuard; mutable FCriticalSection ProceduralMeshGuard;
mutable FCriticalSection ThreadNumberLock;
UPROPERTY() UPROPERTY()
UProceduralMeshComponent* ProceduralMesh; UProceduralMeshComponent* ProceduralMesh;
UPROPERTY(EditAnywhere) UPROPERTY()
UMaterialInterface* SelectionVolumeMat; UWorld* World;
TArray<int32> FloodedIndices; TAtomic<float> AccumulatedTime = 0.0;
FVector SelectionStartPosition;
// USER EXPORTED
UPROPERTY(EditAnywhere) UPROPERTY(EditAnywhere)
float MarchingCubeSize = 0; float MarchingCubeSize = 0;
UPROPERTY(EditAnywhere) UPROPERTY(EditAnywhere)
int EvaluationsPerSecond = 10; int EvaluationsPerSecond = 10;
UPROPERTY(EditAnywhere) UPROPERTY(EditAnywhere)
float AccumulatedTime = 0.0;
UPROPERTY(EditAnywhere)
float ProximityThreshold = 0.1f; float ProximityThreshold = 0.1f;
TArray<int32> SelectedClusterIndices; UPROPERTY(EditAnywhere)
UMaterialInterface* SelectionVolumeMat;
UPROPERTY(EditAnywhere)
int MaxThreadCount = 100;
public: public:
UMagicWand(); // INITIALIZATION
UMagicWand();
virtual void BeginPlay() override; virtual void BeginPlay() override;
void InitReferences();
void InitProceduralMesh() const;
virtual void SelectParticles(const FVector& InputPosition) override; // INPUT HANDLING
virtual void EraseParticles(const FVector& InputPosition) override;
virtual void HandleMetaSelectReleased(const FInputActionInstance& Instance) override; virtual void HandleMetaSelectReleased(const FInputActionInstance& Instance) override;
virtual void HandleMetaSelectPressed(const FInputActionInstance& Instance) override; virtual void HandleMetaSelectPressed(const FInputActionInstance& Instance) override;
virtual void HandleMetaEraseReleased(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 GenerateVoxelMeshWithCubes(const TArray<int32> Voxels) const;
void GenerateVoxelMeshSmooth(const TArray<int32> Voxels); 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 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() : ...@@ -10,6 +10,16 @@ UMetaCastBaseline::UMetaCastBaseline() :
PrimaryComponentTick.bCanEverTick = true; 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 // Called when the game starts
void UMetaCastBaseline::BeginPlay() void UMetaCastBaseline::BeginPlay()
{ {
...@@ -18,13 +28,7 @@ void UMetaCastBaseline::BeginPlay() ...@@ -18,13 +28,7 @@ void UMetaCastBaseline::BeginPlay()
InitPointCloudReference(); InitPointCloudReference();
InitInputBindings(); InitInputBindings();
InitSelectionObject(); InitSelectionObject();
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;
}
} }
void UMetaCastBaseline::InitPointCloudReference() void UMetaCastBaseline::InitPointCloudReference()
......
...@@ -60,6 +60,7 @@ public: ...@@ -60,6 +60,7 @@ public:
// Sets default values for this component's properties // Sets default values for this component's properties
UMetaCastBaseline(); UMetaCastBaseline();
void InitLeftHand();
void InitInputBindings(); void InitInputBindings();
virtual void HandleMetaSelectPressed(const FInputActionInstance& Instance); virtual void HandleMetaSelectPressed(const FInputActionInstance& Instance);
virtual void HandleMetaErasePressed(const FInputActionInstance& Instance); virtual void HandleMetaErasePressed(const FInputActionInstance& Instance);
......
...@@ -91,7 +91,7 @@ void APointCloud::SetupDensityFieldFromPointCloud() const ...@@ -91,7 +91,7 @@ void APointCloud::SetupDensityFieldFromPointCloud() const
{ {
UE_LOG(LogTemp, Log, TEXT("Initializing DensityField!")); 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); 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