Plot3D.cpp 8.6 KB
Newer Older
Jan Kremer's avatar
Jan Kremer committed
1 2 3
// Fill out your copyright notice in the Description page of Project Settings.

#include "Plot3D.h"
Jan Kremer's avatar
Jan Kremer committed
4 5 6
#include "HDF5Library.h"
#include "PointCloudComponent.h"
#include "Runtime/Engine/Classes/Kismet/KismetMathLibrary.h"
7
#include "Runtime/Core/Public/Async/ParallelFor.h"
Jan Kremer's avatar
Jan Kremer committed
8 9 10 11 12

// Sets default values
APlot3D::APlot3D()
{
	PrimaryActorTick.bCanEverTick = true;
Jan Kremer's avatar
Jan Kremer committed
13 14 15 16 17 18 19
	USceneComponent* SceneComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
	RootComponent = SceneComponent;
	SceneComponent->SetMobility(EComponentMobility::Movable);
	PointCloudComponent = CreateDefaultSubobject<UPointCloudComponent>(TEXT("PointCloudComponent"));
	PointCloudComponent->SetupAttachment(RootComponent);
	PointCloudComponent->bCastVolumetricTranslucentShadow = true;
	PointCloudComponent->bCastShadowAsTwoSided = true;
Jan Kremer's avatar
Jan Kremer committed
20 21 22 23 24 25 26 27 28 29 30 31
}

// Called when the game starts or when spawned
void APlot3D::BeginPlay()
{
	Super::BeginPlay();
	InitPoints();
	InitCloud();
}

void APlot3D::InitPoints()
{
Jan Kremer's avatar
Jan Kremer committed
32 33 34 35 36 37 38 39
	UHDF5Library::ImportData(X, Y, Z, U, V, W);

	int32 Num;
	if (OverridePointCount)
	{
		Num = PointCount;
	}
	else
Jan Kremer's avatar
Jan Kremer committed
40
	{
Jan Kremer's avatar
Jan Kremer committed
41
		Num = X.Num();
Jan Kremer's avatar
Jan Kremer committed
42
	}
Jan Kremer's avatar
Jan Kremer committed
43 44 45 46 47 48 49
	
	for (int32 i = 0; i < Num; i++)
	{
		Length.Emplace(FVector(U[i], V[i], W[i]).Size());
	}

	int32 MaxIndex, MinIndex;
50 51 52 53 54 55 56 57 58 59 60 61 62 63
	UKismetMathLibrary::MaxOfFloatArray(Length, MaxIndex, MaxLength);
	UKismetMathLibrary::MinOfFloatArray(Length, MinIndex, MinLength);
	UKismetMathLibrary::MaxOfFloatArray(X, MaxIndex, MaxX);
	UKismetMathLibrary::MinOfFloatArray(X, MinIndex, MinX);
	UKismetMathLibrary::MaxOfFloatArray(Y, MaxIndex, MaxY);
	UKismetMathLibrary::MinOfFloatArray(Y, MinIndex, MinY);
	UKismetMathLibrary::MaxOfFloatArray(Z, MaxIndex, MaxZ);
	UKismetMathLibrary::MinOfFloatArray(Z, MinIndex, MinZ);
	UKismetMathLibrary::MaxOfFloatArray(U, MaxIndex, MaxU);
	UKismetMathLibrary::MinOfFloatArray(U, MinIndex, MinU);
	UKismetMathLibrary::MaxOfFloatArray(V, MaxIndex, MaxV);
	UKismetMathLibrary::MinOfFloatArray(V, MinIndex, MinV);
	UKismetMathLibrary::MaxOfFloatArray(W, MaxIndex, MaxW);
	UKismetMathLibrary::MinOfFloatArray(W, MinIndex, MinW);
Jan Kremer's avatar
Jan Kremer committed
64 65 66

	for (int32 i = 0; i < Num; i++)
	{
67
		FColor TempColor = CalculateColorOfPoint(i, MaxLength, MinLength);
Jan Kremer's avatar
Jan Kremer committed
68

69 70
		Color.Emplace(FVector(TempColor.R, TempColor.G, TempColor.B));
		Points.Emplace(FVector(X[i], Y[i], Z[i]), TempColor, true);
Jan Kremer's avatar
Jan Kremer committed
71
	}
Jan Kremer's avatar
Jan Kremer committed
72 73 74 75
}

void APlot3D::InitCloud()
{
Jan Kremer's avatar
Jan Kremer committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
	PointCloud = NewObject<UPointCloud>();
	PointCloud->Offset = EPointCloudOffset::None;
	PointCloud->SetRenderingMethod(EPointCloudRenderMethod::Sprite_Lit_RGB, false);
	PointCloud->SpriteMask = EPointCloudSpriteMask::Circle;
	//PointCloud->SpriteTexture = 
	PointCloud->ColorOverride = EPointCloudColorOverride::None;
	PointCloud->SpriteSize = FVector2D(0.5, 0.5);

	GetPointCloudComponent()->SetPointCloud(PointCloud);
	RebuildCloud();
}

FColor APlot3D::CalculateColorOfPoint(int32 Index, float Min, float Max)
{
	float NormalizedValue = UKismetMathLibrary::NormalizeToRange(Length[Index], Min, Max);
Jan Kremer's avatar
Jan Kremer committed
91

Jan Kremer's avatar
Jan Kremer committed
92 93
	check(TransferFunction != nullptr);
	FVector ColorVec = TransferFunction->GetVectorValue(NormalizedValue);
Jan Kremer's avatar
Jan Kremer committed
94

Jan Kremer's avatar
Jan Kremer committed
95
	FColor Color = FColor((uint8)(ColorVec.X * 255), (uint8)(ColorVec.Y * 255), (uint8)(ColorVec.Z * 255));
Jan Kremer's avatar
Jan Kremer committed
96

Jan Kremer's avatar
Jan Kremer committed
97 98
	return Color;
}
Jan Kremer's avatar
Jan Kremer committed
99

Jan Kremer's avatar
Jan Kremer committed
100 101 102 103
void APlot3D::RebuildCloud()
{
	PointCloud->SetPointCloudData(Points, true);
}
Jan Kremer's avatar
Jan Kremer committed
104

Jan Kremer's avatar
Jan Kremer committed
105 106 107 108 109 110 111
FVector APlot3D::CalculateWorldLocOfPoint(FPointCloudPoint * Point)
{
	FVector ActorScale = GetActorScale3D() * 100;
	FVector Vec = FVector(ActorScale.X * Point->Location.X * -1, ActorScale.Y * Point->Location.Y, ActorScale.Z * Point->Location.Z);
	FVector RotatedVec = GetActorRotation().RotateVector(Vec);
	FVector SumVec = GetActorLocation() + RotatedVec;
	return SumVec;
Jan Kremer's avatar
Jan Kremer committed
112 113 114 115 116 117 118 119
}

void APlot3D::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

Jan Kremer's avatar
Jan Kremer committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
void APlot3D::BrushPoints(TArray<int32> BrushPoints, bool Add, bool Rebuild)
{
	for (int i = 0; i < BrushPoints.Num(); ++i)
	{
		BrushPoint(BrushPoints[i], Add);
	}

	if (Rebuild)
	{
		RebuildCloud();
	}
}

void APlot3D::BrushPoint(int32 PointIndex, bool Add)
{
135 136 137 138 139
	if (PointIndex == -1)
	{
		return;
	}

Jan Kremer's avatar
Jan Kremer committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
	Points[PointIndex].SetEnabled(!Add);

	/*
	if (Add)
	{
		Points[PointIndex].Color = FColor();
	}
	else
	{
		Points[PointIndex].Color = Points[PointIndex].OriginalColor;
	}
	*/
}

void APlot3D::ResetPoints(bool Rebuild)
{
	for (int i = 0; i < Points.Num(); ++i)
	{
		BrushPoint(i, false);
	}

161 162 163 164 165
	/*ParallelFor(Points.Num(), [this](int32 CurrIdx)
	{
		BrushPoint(CurrIdx, false);
	});*/

Jan Kremer's avatar
Jan Kremer committed
166 167 168 169 170 171 172 173 174 175 176 177 178
	if (Rebuild)
	{
		RebuildCloud();
	}
}

void APlot3D::InvertPoints(bool Rebuild)
{
	for (int i = 0; i < Points.Num(); ++i)
	{
		BrushPoint(i, Points[i].IsEnabled());
	}

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
	/*ParallelFor(Points.Num(), [this](int32 CurrIdx)
	{
		BrushPoint(CurrIdx, Points[CurrIdx].IsEnabled());
	});*/

	/*ParallelFor(4, [this](int32 CurrIdx)
	{
		int32 Start = Points.Num() / 4 * CurrIdx;
		int32 End = Points.Num() / 4 * (CurrIdx + 1);
		for (int i = Start; i < End; ++i)
		{
			BrushPoint(i, Points[i].IsEnabled());
		}
	});*/

Jan Kremer's avatar
Jan Kremer committed
194 195 196 197 198 199 200 201 202
	if (Rebuild)
	{
		RebuildCloud();
	}
}

void APlot3D::AddFilter_Planes(FVector NormalRight, FVector LocationRight, bool UseTwoPlanes, FVector NormalLeft, FVector LocationLeft, bool Add, bool Rebuild, bool Invert)
{
	TArray<int32> Indexes = TArray<int32>();
203
	Indexes.Init(-1, Points.Num());
Jan Kremer's avatar
Jan Kremer committed
204

205
	ParallelFor(Points.Num(), [this, Invert, &Indexes, LocationRight, LocationLeft, NormalRight, NormalLeft, UseTwoPlanes, Add](int32 CurrIdx)
Jan Kremer's avatar
Jan Kremer committed
206
	{
207
		FVector WorldLoc = CalculateWorldLocOfPoint(&Points[CurrIdx]);
Jan Kremer's avatar
Jan Kremer committed
208 209 210 211 212 213 214 215 216
		FVector DifferenceRight = WorldLoc - LocationRight;
		FVector DifferenceLeft = WorldLoc - LocationLeft;
		float DotRight = UKismetMathLibrary::Dot_VectorVector(DifferenceRight, NormalRight);
		float DotLeft = UKismetMathLibrary::Dot_VectorVector(DifferenceLeft, NormalLeft);

		if (Invert)
		{
			if (DotRight >= 0.f && (DotLeft >= 0.f || !UseTwoPlanes))
			{
217
				Indexes[CurrIdx] = CurrIdx;
Jan Kremer's avatar
Jan Kremer committed
218 219 220 221 222 223
			}
		}
		else
		{
			if (DotRight <= 0.f && (DotLeft <= 0.f || !UseTwoPlanes))
			{
224
				Indexes[CurrIdx] = CurrIdx;
Jan Kremer's avatar
Jan Kremer committed
225 226
			}
		}
227
	});
Jan Kremer's avatar
Jan Kremer committed
228 229 230 231 232 233

	BrushPoints(Indexes, Add, Rebuild);
}

void APlot3D::AddFilter_ValueRange(EAxisType XType, float XMin, float XMax, EAxisType YType, float YMin, float YMax, bool Add, bool Rebuild)
{
234 235
	TArray<float> * XArray = nullptr;
	TArray<float> * YArray = nullptr;
Jan Kremer's avatar
Jan Kremer committed
236 237
	TArray<int32> Indexes = TArray<int32>();

238
	switch (XType)
Jan Kremer's avatar
Jan Kremer committed
239
	{
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
	case EAxisType::X:
		XArray = &X;
		break;
	case EAxisType::Y:
		XArray = &Y;
		break;
	case EAxisType::Z:
		XArray = &Z;
		break;
	case EAxisType::U:
		XArray = &U;
		break;
	case EAxisType::V:
		XArray = &V;
		break;
	case EAxisType::W:
		XArray = &W;
		break;
	case EAxisType::LengthUVW:
		XArray = &Length;
		break;
	default:
		break;
	}

	switch (YType)
	{
	case EAxisType::X:
		YArray = &X;
		break;
	case EAxisType::Y:
		YArray = &Y;
		break;
	case EAxisType::Z:
		YArray = &Z;
		break;
	case EAxisType::U:
		YArray = &U;
		break;
	case EAxisType::V:
		YArray = &V;
		break;
	case EAxisType::W:
		YArray = &W;
		break;
	case EAxisType::LengthUVW:
		YArray = &Length;
		break;
	default:
		break;
	}
Jan Kremer's avatar
Jan Kremer committed
291

292 293 294 295 296
	/*
	Indexes.Init(-1, Points.Num());
	ParallelFor(Points.Num(), [this, XArray, YArray, XMin, XMax, YMin, YMax, Add, &Indexes](int32 CurrIdx)
	{
		if ((*XArray)[CurrIdx] >= XMin && (*XArray)[CurrIdx] <= XMax && (*YArray)[CurrIdx] >= YMin && (*YArray)[CurrIdx] <= YMax)
Jan Kremer's avatar
Jan Kremer committed
297
		{
298
			Indexes[CurrIdx] = CurrIdx;
Jan Kremer's avatar
Jan Kremer committed
299
		}
300
	});*/
Jan Kremer's avatar
Jan Kremer committed
301

302 303 304
	for (int i = 0; i < Points.Num(); ++i)
	{
		if ((*XArray)[i] >= XMin && (*XArray)[i] <= XMax && (*YArray)[i] >= YMin && (*YArray)[i] <= YMax)
Jan Kremer's avatar
Jan Kremer committed
305 306 307 308 309 310 311 312 313 314 315 316
		{
			Indexes.Emplace(i);
		}
	}

	BrushPoints(Indexes, Add, Rebuild);
}

void APlot3D::AddFilter_Box(FTransform Transform, FVector Extent, bool Add, bool Rebuild)
{
	TArray<int32> Indexes = TArray<int32>();

317
	/*for (int i = 0; i < Points.Num(); ++i)
Jan Kremer's avatar
Jan Kremer committed
318 319 320 321 322 323
	{
		FVector WorldLoc = CalculateWorldLocOfPoint(&Points[i]);
		if (UKismetMathLibrary::IsPointInBoxWithTransform(WorldLoc, Transform, Extent))
		{
			Indexes.Emplace(i);
		}
324 325 326 327 328 329 330 331 332 333 334
	}*/

	Indexes.Init(-1, Points.Num());
	ParallelFor(Points.Num(), [this, Transform, Extent, Add, &Indexes](int32 CurrIdx)
	{
		FVector WorldLoc = CalculateWorldLocOfPoint(&Points[CurrIdx]);
		if (UKismetMathLibrary::IsPointInBoxWithTransform(WorldLoc, Transform, Extent))
		{
			Indexes[CurrIdx] = CurrIdx;
		}
	});
Jan Kremer's avatar
Jan Kremer committed
335 336 337 338 339 340 341 342

	BrushPoints(Indexes, Add, Rebuild);
}

void APlot3D::AddFilter_Sphere(FVector Location, float Radius, bool Add, bool Rebuild)
{
	TArray<int32> Indexes = TArray<int32>();

343
	/*for (int i = 0; i < Points.Num(); ++i)
Jan Kremer's avatar
Jan Kremer committed
344 345 346 347 348 349
	{
		FVector WorldLoc = CalculateWorldLocOfPoint(&Points[i]);
		if ((Location - WorldLoc).Size() <= Radius)
		{
			Indexes.Emplace(i);
		}
350 351 352 353 354 355 356 357 358 359 360
	}*/
	
	Indexes.Init(-1, Points.Num());
	ParallelFor(Points.Num(), [this, Location, Radius, Add, &Indexes](int32 CurrIdx)
	{
		FVector WorldLoc = CalculateWorldLocOfPoint(&Points[CurrIdx]);
		if ((Location - WorldLoc).Size() <= Radius)
		{
			Indexes[CurrIdx] = CurrIdx;
		}
	});
Jan Kremer's avatar
Jan Kremer committed
361 362 363 364

	BrushPoints(Indexes, Add, Rebuild);
}