DataComponent.cpp 14 KB
Newer Older
1 2 3 4 5 6 7 8
// Fill out your copyright notice in the Description page of Project Settings.

#include "DataComponent.h"
#include "Runtime/Core/Public/Misc/Paths.h"
#include "Runtime/Core/Public/HAL/FileManagerGeneric.h"
#include "HDF5Library.h"
#include "Plot3D.h"
#include "PawnBase.h"
Jan Kremer's avatar
Jan Kremer committed
9
#include "Plot2D.h"
10
#include "PlotCanvas.h"
11 12 13 14 15 16 17 18 19 20 21 22
#include "Runtime/Engine/Classes/Engine/World.h"
#include "Runtime/Engine/Classes/Kismet/KismetMathLibrary.h"
#include "Runtime/Core/Public/Async/ParallelFor.h"

UDataComponent::UDataComponent()
{
	D3_BrushAlpha = 0;
	D3_SpriteAlpha = 255;
	D3_BrushOverridesColor = false;
	D3_BrushColor = FColor(0, 0, 0, 0);
	D3_UseTransparency = true;
	D3_SpriteSizeFactor = 50;
23
	UseFileBounds = false;
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
}


// Called when the game starts
void UDataComponent::BeginPlay()
{
	Super::BeginPlay();

	if (!TransferFunction)
	{
		UE_LOG(LogTemp, Error, TEXT("No transfer function set!"));
		FGenericPlatformMisc::RequestExit(false);
	}

	// Find Files
	FString Directory = FPaths::ProjectDir();
	Directory += FString("/Resources/");
	FString Path = Directory + "*.h5*";
	FFileManagerGeneric::Get().FindFiles(FileNames, *Path, true, false);

	if (!FileNameToLoadOnStart.IsEmpty())
	{
		LoadFile(FileNameToLoadOnStart);
	}
}

void UDataComponent::LoadFile(FString FileName)
{
	// Load File
Jan Kremer's avatar
Jan Kremer committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
	TArray<FString> TempStringArray = TArray<FString>();
	LoadedFile = UHDF5Library::LoadFile(FileName, TempStringArray);
	CurrentFileName = FileName;

	if (!Cast<APawnBase>(GetOwner())->Plot3D)
	{
		FVector Location(-1610.0f, -341.0f, 100.0f);
		FRotator Rotation(0.0f, 0.0f, 0.0f);
		FActorSpawnParameters SpawnInfo;
		APlot3D* Plot3D = GetWorld()->SpawnActor<APlot3D>(Location, Rotation, SpawnInfo);
		Cast<APawnBase>(GetOwner())->Plot3D = Plot3D;
		Plot3D->Init(this);
	}

	/*
	The order would be "1,10,11,12,13,14,15,16,17,18,19,2,20,21,22,23,24" without sorting padded strings!
	*/
	for (int i = 0; i < TempStringArray.Num(); ++i)
	{
		int32 Length = TempStringArray[i].Len();
		for (int j = 0; j < (12 - Length); ++j) // pad the string to 10 characters
		{
			TempStringArray[i].InsertAt(5, " "); // 5 is the position after "step#"
		}
	}
	TempStringArray.Sort([](const FString& One, const FString& Two) {
		return One < Two;
	});
	for (int i = 0; i < TempStringArray.Num(); ++i)
	{
		TempStringArray[i] = TempStringArray[i].Replace(TEXT(" "), TEXT(""));
	}

	GroupNames.Empty();
	for (FString String : TempStringArray)
	{
		GroupNames.Emplace(String);
	}

92 93 94 95 96
	if (UseFileBounds)
	{
		SetFileBounds();
	}

Jan Kremer's avatar
Jan Kremer committed
97
	LoadStep(GroupNames[0], true);
98 99
}

Jan Kremer's avatar
Jan Kremer committed
100
void UDataComponent::LoadStep(FString GroupName, bool ResetBrush)
101 102 103
{
	UHDF5Library::LoadGroup(LoadedFile, GroupName, X, Y, Z, U, V, W);

Jan Kremer's avatar
Jan Kremer committed
104 105
	CurrentGroupName = GroupName;

106 107 108 109 110 111 112
	// Prepare Data
	Num = X.Num();
	if (OverrideMaxPointCount > 0 && OverrideMaxPointCount < Num)
	{
		Num = OverrideMaxPointCount;
	}

Jan Kremer's avatar
Jan Kremer committed
113 114 115 116 117 118 119
	if (ResetBrush) // TODO
	{
		IsBrushed = TArray<bool>();
		IsBrushed.Init(false, Num);
	}
	
	Length.Empty();
120
	for (int32 i = 0; i < X.Num(); i++)
121 122 123 124
	{
		Length.Emplace(FVector(U[i], V[i], W[i]).Size());
	}

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
	if (!UseFileBounds)
	{
		int32 MaxIndex, MinIndex;
		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
142 143

		UpdateDataScaleFactor();
144 145
	}
	
Jan Kremer's avatar
Jan Kremer committed
146
	PColor.Empty();
147 148 149 150 151 152 153
	for (int32 i = 0; i < Num; i++)
	{
		FColor TempColor = CalculateColorOfPoint(i, MaxLength, MinLength);

		PColor.Emplace(FVector(TempColor.R, TempColor.G, TempColor.B));
	}

Jan Kremer's avatar
Jan Kremer committed
154
	Cast<APawnBase>(GetOwner())->Plot3D->OnLoadStep(!ResetBrush);
155 156 157 158 159 160 161 162

	APawnBase* BasePawn = Cast<APawnBase>(GetOwner());
	for (APlot2D* Plot2D : BasePawn->Plots2D)
	{
		Plot2D->PlotCanvas->SetXAxisType(Plot2D->PlotCanvas->XAxisType); // this refreshes the min and max values
		Plot2D->PlotCanvas->SetYAxisType(Plot2D->PlotCanvas->YAxisType); // this refreshes the min and max values
		Plot2D->Redraw();
	}
163 164 165 166 167 168 169 170 171 172
}

FColor UDataComponent::CalculateColorOfPoint(int32 Index, float Min, float Max)
{
	float NormalizedValue = UKismetMathLibrary::NormalizeToRange(Length[Index], Min, Max);
	FVector ColorVec = TransferFunction->GetVectorValue(NormalizedValue);
	FColor Color = FColor((uint8)(ColorVec.X * 255), (uint8)(ColorVec.Y * 255), (uint8)(ColorVec.Z * 255));
	return Color;
}

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
void UDataComponent::SetFileBounds()
{
	TArray<float> XMinTemp = TArray<float>();
	TArray<float> XMaxTemp = TArray<float>();
	TArray<float> YMinTemp = TArray<float>();
	TArray<float> YMaxTemp = TArray<float>();
	TArray<float> ZMinTemp = TArray<float>();
	TArray<float> ZMaxTemp = TArray<float>();
	TArray<float> UMinTemp = TArray<float>();
	TArray<float> UMaxTemp = TArray<float>();
	TArray<float> VMinTemp = TArray<float>();
	TArray<float> VMaxTemp = TArray<float>();
	TArray<float> WMinTemp = TArray<float>();
	TArray<float> WMaxTemp = TArray<float>();
	TArray<float> LengthTemp = TArray<float>();

	for (FString Group : GroupNames)
	{
		TArray<float> XTemp = TArray<float>();
		TArray<float> YTemp = TArray<float>();
		TArray<float> ZTemp = TArray<float>();
		TArray<float> UTemp = TArray<float>();
		TArray<float> VTemp = TArray<float>();
		TArray<float> WTemp = TArray<float>();
		UHDF5Library::LoadGroup(LoadedFile, Group, XTemp, YTemp, ZTemp, UTemp, VTemp, WTemp);

		for (int32 i = 0; i < XTemp.Num(); i++)
		{
			LengthTemp.Emplace(FVector(UTemp[i], VTemp[i], WTemp[i]).Size());
		}

		float TempMaxX, TempMaxY, TempMaxZ, TempMaxU, TempMaxV, TempMaxW, TempMinX, TempMinY, TempMinZ, TempMinU, TempMinV, TempMinW;
		int32 MaxIndex, MinIndex;
		UKismetMathLibrary::MaxOfFloatArray(XTemp, MaxIndex, TempMaxX); XMaxTemp.Emplace(TempMaxX);
		UKismetMathLibrary::MinOfFloatArray(XTemp, MinIndex, TempMinX); XMinTemp.Emplace(TempMinX);
		UKismetMathLibrary::MaxOfFloatArray(YTemp, MaxIndex, TempMaxY); YMaxTemp.Emplace(TempMaxY);
		UKismetMathLibrary::MinOfFloatArray(YTemp, MinIndex, TempMinY); YMinTemp.Emplace(TempMinY);
		UKismetMathLibrary::MaxOfFloatArray(ZTemp, MaxIndex, TempMaxZ); ZMaxTemp.Emplace(TempMaxZ);
		UKismetMathLibrary::MinOfFloatArray(ZTemp, MinIndex, TempMinZ); ZMinTemp.Emplace(TempMinZ);
		UKismetMathLibrary::MaxOfFloatArray(UTemp, MaxIndex, TempMaxU); UMaxTemp.Emplace(TempMaxU);
		UKismetMathLibrary::MinOfFloatArray(UTemp, MinIndex, TempMinU); UMinTemp.Emplace(TempMinU);
		UKismetMathLibrary::MaxOfFloatArray(VTemp, MaxIndex, TempMaxV); VMaxTemp.Emplace(TempMaxV);
		UKismetMathLibrary::MinOfFloatArray(VTemp, MinIndex, TempMinV); VMinTemp.Emplace(TempMinV);
		UKismetMathLibrary::MaxOfFloatArray(WTemp, MaxIndex, TempMaxW); WMaxTemp.Emplace(TempMaxW);
		UKismetMathLibrary::MinOfFloatArray(WTemp, MinIndex, TempMinW); WMinTemp.Emplace(TempMinW);
	}

	int32 MaxIndex, MinIndex;
	UKismetMathLibrary::MaxOfFloatArray(LengthTemp, MaxIndex, MaxLength);
	UKismetMathLibrary::MinOfFloatArray(LengthTemp, MinIndex, MinLength);
	UKismetMathLibrary::MaxOfFloatArray(XMaxTemp, MaxIndex, MaxX);
	UKismetMathLibrary::MinOfFloatArray(XMinTemp, MinIndex, MinX);
	UKismetMathLibrary::MaxOfFloatArray(YMaxTemp, MaxIndex, MaxY);
	UKismetMathLibrary::MinOfFloatArray(YMinTemp, MinIndex, MinY);
	UKismetMathLibrary::MaxOfFloatArray(ZMaxTemp, MaxIndex, MaxZ);
	UKismetMathLibrary::MinOfFloatArray(ZMinTemp, MinIndex, MinZ);
	UKismetMathLibrary::MaxOfFloatArray(UMaxTemp, MaxIndex, MaxU);
	UKismetMathLibrary::MinOfFloatArray(UMinTemp, MinIndex, MinU);
	UKismetMathLibrary::MaxOfFloatArray(VMaxTemp, MaxIndex, MaxV);
	UKismetMathLibrary::MinOfFloatArray(VMinTemp, MinIndex, MinV);
	UKismetMathLibrary::MaxOfFloatArray(WMaxTemp, MaxIndex, MaxW);
	UKismetMathLibrary::MinOfFloatArray(WMinTemp, MinIndex, MinW);
Jan Kremer's avatar
Jan Kremer committed
235 236 237 238 239 240 241 242

	UpdateDataScaleFactor();
}

void UDataComponent::UpdateDataScaleFactor()
{
	float Range = FMath::Max3(MaxX - MinX, MaxY - MinY, MaxZ - MinZ);
	DataScaleFactor = 2 / Range;
243 244
}

245 246 247 248 249 250 251 252 253 254
void UDataComponent::RebuildAll()
{
	APawnBase* BasePawn = Cast<APawnBase>(GetOwner());
	BasePawn->Plot3D->RebuildCloud();
	for (APlot2D* Plot2D : BasePawn->Plots2D)
	{
		Plot2D->Redraw();
	}
}

255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
void UDataComponent::AddFilter_Planes(FVector NormalRight, FVector LocationRight, bool UseTwoPlanes, FVector NormalLeft, FVector LocationLeft, bool Add, bool Rebuild, bool Invert)
{
	TArray<int32> Indexes = TArray<int32>();
	Indexes.Init(-1, Num);

	APlot3D* Plot3D = Cast<APawnBase>(GetOwner())->Plot3D;
	ParallelFor(Num, [this, Invert, &Indexes, LocationRight, LocationLeft, NormalRight, NormalLeft, UseTwoPlanes, Add, Plot3D](int32 CurrIdx)
	{
		FVector WorldLoc = Plot3D->CalculateWorldLocOfPoint(CurrIdx);
		FVector DifferenceRight = WorldLoc - LocationRight;
		FVector DifferenceLeft = WorldLoc - LocationLeft;
		float DotRight = UKismetMathLibrary::Dot_VectorVector(DifferenceRight, NormalRight);
		float DotLeft = UKismetMathLibrary::Dot_VectorVector(DifferenceLeft, NormalLeft);

		if (Invert)
		{
271
			if (!UseTwoPlanes)
272
			{
273 274 275 276 277 278 279 280 281 282 283
				if (DotRight >= 0.f)
				{
					Indexes[CurrIdx] = CurrIdx;
				}
			}
			else
			{
				if (!(DotRight <= 0.f && DotLeft >= 0.f))
				{
					Indexes[CurrIdx] = CurrIdx;
				}
284 285 286 287
			}
		}
		else
		{
288
			if (DotRight <= 0.f && (DotLeft >= 0.f || !UseTwoPlanes))
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
			{
				Indexes[CurrIdx] = CurrIdx;
			}
		}
	});

	BrushPoints(Indexes, Add, Rebuild);
}

void UDataComponent::AddFilter_ValueRange(EAxisType XType, float XMin, float XMax, EAxisType YType, float YMin, float YMax, bool Add, bool Rebuild, bool UseTwoValues)
{
	TArray<float> * XArray = nullptr;
	TArray<float> * YArray = nullptr;
	TArray<int32> Indexes = TArray<int32>();

	switch (XType)
	{
	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;
	}

	/*
	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)
		{
			Indexes[CurrIdx] = CurrIdx;
		}
	});*/

	for (int i = 0; i < Num; ++i)
	{
		if ((*XArray)[i] >= XMin && (*XArray)[i] <= XMax && (!UseTwoValues || ((*YArray)[i] >= YMin && (*YArray)[i] <= YMax)))
		{
			Indexes.Emplace(i);
		}
	}

	BrushPoints(Indexes, Add, Rebuild);
}

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

	/*for (int i = 0; i < Points.Num(); ++i)
	{
		FVector WorldLoc = CalculateWorldLocOfPoint(&Points[i]);
		if (UKismetMathLibrary::IsPointInBoxWithTransform(WorldLoc, Transform, Extent))
		{
			Indexes.Emplace(i);
		}
	}*/

	APlot3D* Plot3D = Cast<APawnBase>(GetOwner())->Plot3D;
	Indexes.Init(-1, Num);
	ParallelFor(Num, [this, Transform, Extent, Add, &Indexes, Plot3D](int32 CurrIdx)
	{
		FVector WorldLoc = Plot3D->CalculateWorldLocOfPoint(CurrIdx);
		if (UKismetMathLibrary::IsPointInBoxWithTransform(WorldLoc, Transform, Extent))
		{
			Indexes[CurrIdx] = CurrIdx;
		}
	});

	BrushPoints(Indexes, Add, Rebuild);
}

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

	/*for (int i = 0; i < Points.Num(); ++i)
	{
		FVector WorldLoc = CalculateWorldLocOfPoint(&Points[i]);
		if ((Location - WorldLoc).Size() <= Radius)
		{
			Indexes.Emplace(i);
		}
	}*/

	APlot3D* Plot3D = Cast<APawnBase>(GetOwner())->Plot3D;
	Indexes.Init(-1, Num);
	ParallelFor(Num, [this, Location, Radius, Add, &Indexes, Plot3D](int32 CurrIdx)
	{
		FVector WorldLoc = Plot3D->CalculateWorldLocOfPoint(CurrIdx);
		if ((Location - WorldLoc).Size() <= Radius)
		{
			Indexes[CurrIdx] = CurrIdx;
		}
	});

	BrushPoints(Indexes, Add, Rebuild);
}

void UDataComponent::BrushPoints(TArray<int32> BrushPoints, bool Add, bool Rebuild)
{
	for (int i = 0; i < BrushPoints.Num(); ++i)
	{
		BrushPoint(BrushPoints[i], Add);
	}

Jan Kremer's avatar
Jan Kremer committed
440 441
	APawnBase* BasePawn = Cast<APawnBase>(GetOwner());
	BasePawn->Plot3D->BrushPoints(BrushPoints, Rebuild);
442
	if (Rebuild)
Jan Kremer's avatar
Jan Kremer committed
443
	{
444 445 446 447
		for (APlot2D* Plot2D : BasePawn->Plots2D)
		{
			Plot2D->Redraw();
		}
Jan Kremer's avatar
Jan Kremer committed
448
	}
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
}

void UDataComponent::BrushPoint(int32 PointIndex, bool Add)
{
	if (PointIndex == -1)
	{
		return;
	}

	IsBrushed[PointIndex] = Add;
}

// does not use the standard BrushPoints, for performance reasons
void UDataComponent::ResetPoints(bool Rebuild)
{
	for (int i = 0; i < Num; ++i)
	{
		BrushPoint(i, false);
	}

Jan Kremer's avatar
Jan Kremer committed
469 470
	APawnBase* BasePawn = Cast<APawnBase>(GetOwner());
	BasePawn->Plot3D->ResetPoints(Rebuild);
471
	if (Rebuild)
Jan Kremer's avatar
Jan Kremer committed
472
	{
473 474 475 476
		for (APlot2D* Plot2D : BasePawn->Plots2D)
		{
			Plot2D->Redraw();
		}
Jan Kremer's avatar
Jan Kremer committed
477
	}
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
}

// does not use the standard BrushPoints, for performance reasons
void UDataComponent::InvertPoints(bool Rebuild)
{
	for (int i = 0; i < Num; ++i)
	{
		BrushPoint(i, !IsBrushed[i]);
	}

	/*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
503 504
	APawnBase* BasePawn = Cast<APawnBase>(GetOwner());
	BasePawn->Plot3D->InvertPoints(Rebuild);
505
	if (Rebuild)
Jan Kremer's avatar
Jan Kremer committed
506
	{
507 508 509 510
		for (APlot2D* Plot2D : BasePawn->Plots2D)
		{
			Plot2D->Redraw();
		}
Jan Kremer's avatar
Jan Kremer committed
511
	}
512
}