DataComponent.cpp 9.82 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
Cleanup  
Jan Kremer committed
9
#include "Plot2D.h"
10 11 12 13 14 15 16 17 18 19 20 21 22 23 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
#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;
}


// 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
51 52 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);
	}

	IsFirstTime = true;
	LoadStep(GroupNames[0], true);
92 93
}

Jan Kremer's avatar
Jan Kremer committed
94
void UDataComponent::LoadStep(FString GroupName, bool ResetBrush)
95 96 97
{
	UHDF5Library::LoadGroup(LoadedFile, GroupName, X, Y, Z, U, V, W);

Jan Kremer's avatar
Jan Kremer committed
98 99
	CurrentGroupName = GroupName;

100 101 102 103 104 105 106
	// Prepare Data
	Num = X.Num();
	if (OverrideMaxPointCount > 0 && OverrideMaxPointCount < Num)
	{
		Num = OverrideMaxPointCount;
	}

Jan Kremer's avatar
Jan Kremer committed
107 108 109 110 111 112 113
	if (ResetBrush) // TODO
	{
		IsBrushed = TArray<bool>();
		IsBrushed.Init(false, Num);
	}
	
	Length.Empty();
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
	for (int32 i = 0; i < Num; i++)
	{
		Length.Emplace(FVector(U[i], V[i], W[i]).Size());
	}

	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
135
	PColor.Empty();
136 137 138 139 140 141 142
	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
143 144
	Cast<APawnBase>(GetOwner())->Plot3D->OnLoadStep(IsFirstTime, !ResetBrush);
	IsFirstTime = false;
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 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 235 236 237 238 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 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
}

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;
}

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)
		{
			if (DotRight >= 0.f && (DotLeft >= 0.f || !UseTwoPlanes))
			{
				Indexes[CurrIdx] = CurrIdx;
			}
		}
		else
		{
			if (DotRight <= 0.f && (DotLeft <= 0.f || !UseTwoPlanes))
			{
				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
Cleanup  
Jan Kremer committed
330 331 332 333 334 335
	APawnBase* BasePawn = Cast<APawnBase>(GetOwner());
	BasePawn->Plot3D->BrushPoints(BrushPoints, Rebuild);
	for (APlot2D* Plot2D : BasePawn->Plots2D)
	{
		Plot2D->Redraw();
	}
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
}

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
Cleanup  
Jan Kremer committed
356 357 358 359 360 361
	APawnBase* BasePawn = Cast<APawnBase>(GetOwner());
	BasePawn->Plot3D->ResetPoints(Rebuild);
	for (APlot2D* Plot2D : BasePawn->Plots2D)
	{
		Plot2D->Redraw();
	}
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
}

// 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
Cleanup  
Jan Kremer committed
387 388 389 390 391 392
	APawnBase* BasePawn = Cast<APawnBase>(GetOwner());
	BasePawn->Plot3D->InvertPoints(Rebuild);
	for (APlot2D* Plot2D : BasePawn->Plots2D)
	{
		Plot2D->Redraw();
	}
393
}