Plot3D.cpp 9.5 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;
20 21 22 23 24 25

	BrushAlpha = 0;
	SpriteAlpha = 255;
	BrushOverridesColor = false;
	BrushColor = FColor(0, 0, 0, 0);
	UseTransparency = true;
Jan Kremer's avatar
Jan Kremer committed
26 27 28 29 30 31 32 33 34 35 36 37
}

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

void APlot3D::InitPoints()
{
38
	UHDF5Library::ImportData(FileName, X, Y, Z, U, V, W);
Jan Kremer's avatar
Jan Kremer committed
39

40 41
	int32 Num = X.Num();
	if (OverridePointCount && PointCount <= Num)
Jan Kremer's avatar
Jan Kremer committed
42 43 44
	{
		Num = PointCount;
	}
45 46 47

	IsBrushed = TArray<bool>();
	IsBrushed.Init(false, Num);
Jan Kremer's avatar
Jan Kremer committed
48
	
49
	float XSum = 0;
Jan Kremer's avatar
Jan Kremer committed
50 51 52
	for (int32 i = 0; i < Num; i++)
	{
		Length.Emplace(FVector(U[i], V[i], W[i]).Size());
53
		XSum += X[i];
Jan Kremer's avatar
Jan Kremer committed
54
	}
55 56 57 58
	float XMean = XSum / Num;
	DataScaleFactor = 0.5 / XMean;

	SetActorScale3D(FVector(DataScaleFactor, DataScaleFactor, DataScaleFactor));
Jan Kremer's avatar
Jan Kremer committed
59 60

	int32 MaxIndex, MinIndex;
61 62 63 64 65 66 67 68 69 70 71 72 73 74
	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
75 76 77

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

80 81
		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
82
	}
Jan Kremer's avatar
Jan Kremer committed
83 84 85 86
}

void APlot3D::InitCloud()
{
Jan Kremer's avatar
Jan Kremer committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
	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
102

Jan Kremer's avatar
Jan Kremer committed
103 104
	check(TransferFunction != nullptr);
	FVector ColorVec = TransferFunction->GetVectorValue(NormalizedValue);
Jan Kremer's avatar
Jan Kremer committed
105

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

Jan Kremer's avatar
Jan Kremer committed
108 109
	return Color;
}
Jan Kremer's avatar
Jan Kremer committed
110

Jan Kremer's avatar
Jan Kremer committed
111 112 113 114
void APlot3D::RebuildCloud()
{
	PointCloud->SetPointCloudData(Points, true);
}
Jan Kremer's avatar
Jan Kremer committed
115

Jan Kremer's avatar
Jan Kremer committed
116 117 118 119 120 121 122
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
123 124 125 126 127 128 129 130
}

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

}

Jan Kremer's avatar
Jan Kremer committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
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)
{
146 147 148 149 150
	if (PointIndex == -1)
	{
		return;
	}

151
	IsBrushed[PointIndex] = Add;
152 153
	
	if (UseTransparency)
Jan Kremer's avatar
Jan Kremer committed
154
	{
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
		if (Add)
		{
			FColor NewColor = Points[PointIndex].OriginalColor;
			if (BrushOverridesColor)
			{
				NewColor = BrushColor;
			}
			NewColor.A = BrushAlpha;
			Points[PointIndex].Color = NewColor;
		}
		else
		{
			Points[PointIndex].Color = Points[PointIndex].OriginalColor;
			Points[PointIndex].Color.A = SpriteAlpha;
		}
Jan Kremer's avatar
Jan Kremer committed
170 171 172
	}
	else
	{
173
		Points[PointIndex].SetEnabled(!Add);
Jan Kremer's avatar
Jan Kremer committed
174 175 176 177 178 179 180 181 182 183
	}
}

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

184 185 186 187 188
	/*ParallelFor(Points.Num(), [this](int32 CurrIdx)
	{
		BrushPoint(CurrIdx, false);
	});*/

Jan Kremer's avatar
Jan Kremer committed
189 190 191 192 193 194 195 196 197 198
	if (Rebuild)
	{
		RebuildCloud();
	}
}

void APlot3D::InvertPoints(bool Rebuild)
{
	for (int i = 0; i < Points.Num(); ++i)
	{
199
		BrushPoint(i, !IsBrushed[i]);
Jan Kremer's avatar
Jan Kremer committed
200 201
	}

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
	/*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
217 218 219 220 221 222 223 224 225
	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>();
226
	Indexes.Init(-1, Points.Num());
Jan Kremer's avatar
Jan Kremer committed
227

228
	ParallelFor(Points.Num(), [this, Invert, &Indexes, LocationRight, LocationLeft, NormalRight, NormalLeft, UseTwoPlanes, Add](int32 CurrIdx)
Jan Kremer's avatar
Jan Kremer committed
229
	{
230
		FVector WorldLoc = CalculateWorldLocOfPoint(&Points[CurrIdx]);
Jan Kremer's avatar
Jan Kremer committed
231 232 233 234 235 236 237 238 239
		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))
			{
240
				Indexes[CurrIdx] = CurrIdx;
Jan Kremer's avatar
Jan Kremer committed
241 242 243 244 245 246
			}
		}
		else
		{
			if (DotRight <= 0.f && (DotLeft <= 0.f || !UseTwoPlanes))
			{
247
				Indexes[CurrIdx] = CurrIdx;
Jan Kremer's avatar
Jan Kremer committed
248 249
			}
		}
250
	});
Jan Kremer's avatar
Jan Kremer committed
251 252 253 254

	BrushPoints(Indexes, Add, Rebuild);
}

255
void APlot3D::AddFilter_ValueRange(EAxisType XType, float XMin, float XMax, EAxisType YType, float YMin, float YMax, bool Add, bool Rebuild, bool UseTwoValues)
Jan Kremer's avatar
Jan Kremer committed
256
{
257 258
	TArray<float> * XArray = nullptr;
	TArray<float> * YArray = nullptr;
Jan Kremer's avatar
Jan Kremer committed
259 260
	TArray<int32> Indexes = TArray<int32>();

261
	switch (XType)
Jan Kremer's avatar
Jan Kremer committed
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
	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
314

315 316 317 318 319
	/*
	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
320
		{
321
			Indexes[CurrIdx] = CurrIdx;
Jan Kremer's avatar
Jan Kremer committed
322
		}
323
	});*/
Jan Kremer's avatar
Jan Kremer committed
324

325 326
	for (int i = 0; i < Points.Num(); ++i)
	{
327
		if ((*XArray)[i] >= XMin && (*XArray)[i] <= XMax && ( !UseTwoValues || ((*YArray)[i] >= YMin && (*YArray)[i] <= YMax) ) )
Jan Kremer's avatar
Jan Kremer committed
328 329 330 331 332 333 334 335 336 337 338 339
		{
			Indexes.Emplace(i);
		}
	}

	BrushPoints(Indexes, Add, Rebuild);
}

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

340
	/*for (int i = 0; i < Points.Num(); ++i)
Jan Kremer's avatar
Jan Kremer committed
341 342 343 344 345 346
	{
		FVector WorldLoc = CalculateWorldLocOfPoint(&Points[i]);
		if (UKismetMathLibrary::IsPointInBoxWithTransform(WorldLoc, Transform, Extent))
		{
			Indexes.Emplace(i);
		}
347 348 349 350 351 352 353 354 355 356 357
	}*/

	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
358 359 360 361 362 363 364 365

	BrushPoints(Indexes, Add, Rebuild);
}

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

366
	/*for (int i = 0; i < Points.Num(); ++i)
Jan Kremer's avatar
Jan Kremer committed
367 368 369 370 371 372
	{
		FVector WorldLoc = CalculateWorldLocOfPoint(&Points[i]);
		if ((Location - WorldLoc).Size() <= Radius)
		{
			Indexes.Emplace(i);
		}
373 374 375 376 377 378 379 380 381 382 383
	}*/
	
	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
384 385 386 387

	BrushPoints(Indexes, Add, Rebuild);
}

388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
void APlot3D::RefreshAfterSettingsChange()
{
	for (int i = 0; i < Points.Num(); ++i)
	{
		BrushPoint(i, IsBrushed[i]);
	};

	RebuildCloud();
}

void APlot3D::EnableAllPoints()
{
	for (int i = 0; i < Points.Num(); ++i)
	{
		Points[i].SetEnabled(true);
	};
}