Plot3D.cpp 6.02 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"
Jan Kremer's avatar
Jan Kremer committed
7
8
9
10
11

// Sets default values
APlot3D::APlot3D()
{
	PrimaryActorTick.bCanEverTick = true;
Jan Kremer's avatar
Jan Kremer committed
12
13
14
15
16
17
18
	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
19
20
21
22
23
24
25
26
27
28
29
30
}

// 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
31
32
33
34
35
36
37
38
39
40
41
42
	TArray<float> X;
	TArray<float> Y;
	TArray<float> Z;

	UHDF5Library::ImportData(X, Y, Z, U, V, W);

	int32 Num;
	if (OverridePointCount)
	{
		Num = PointCount;
	}
	else
Jan Kremer's avatar
Jan Kremer committed
43
	{
Jan Kremer's avatar
Jan Kremer committed
44
		Num = X.Num();
Jan Kremer's avatar
Jan Kremer committed
45
	}
Jan Kremer's avatar
Jan Kremer committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
	
	for (int32 i = 0; i < Num; i++)
	{
		Length.Emplace(FVector(U[i], V[i], W[i]).Size());
	}

	int32 MaxIndex, MinIndex;
	float Max, Min;
	UKismetMathLibrary::MaxOfFloatArray(Length, MaxIndex, Max);
	UKismetMathLibrary::MinOfFloatArray(Length, MinIndex, Min);

	for (int32 i = 0; i < Num; i++)
	{
		FColor Color = CalculateColorOfPoint(i, Min, Max);
Jan Kremer's avatar
Jan Kremer committed
60

Jan Kremer's avatar
Jan Kremer committed
61
62
		Points.Emplace(FVector(X[i], Y[i], Z[i]), Color, true);
	}
Jan Kremer's avatar
Jan Kremer committed
63
64
65
66
}

void APlot3D::InitCloud()
{
Jan Kremer's avatar
Jan Kremer committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
	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
82

Jan Kremer's avatar
Jan Kremer committed
83
84
	check(TransferFunction != nullptr);
	FVector ColorVec = TransferFunction->GetVectorValue(NormalizedValue);
Jan Kremer's avatar
Jan Kremer committed
85

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

Jan Kremer's avatar
Jan Kremer committed
88
89
	return Color;
}
Jan Kremer's avatar
Jan Kremer committed
90

Jan Kremer's avatar
Jan Kremer committed
91
92
93
94
void APlot3D::RebuildCloud()
{
	PointCloud->SetPointCloudData(Points, true);
}
Jan Kremer's avatar
Jan Kremer committed
95

Jan Kremer's avatar
Jan Kremer committed
96
97
98
99
100
101
102
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
103
104
105
106
107
108
109
110
111
}

// Called every frame
void APlot3D::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

Jan Kremer's avatar
Jan Kremer committed
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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
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)
{
	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);
	}

	if (Rebuild)
	{
		RebuildCloud();
	}
}

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

	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>();

	for (int i = 0; i < Points.Num(); ++i)
	{
		FVector WorldLoc = CalculateWorldLocOfPoint(&Points[i]);
		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.Emplace(i);
			}
		}
		else
		{
			if (DotRight <= 0.f && (DotLeft <= 0.f || !UseTwoPlanes))
			{
				Indexes.Emplace(i);
			}
		}
	}

	BrushPoints(Indexes, Add, Rebuild);
}

void APlot3D::AddFilter_ValueRange(EAxisType XType, float XMin, float XMax, EAxisType YType, float YMin, float YMax, bool Add, bool Rebuild)
{
	float XValue, YValue;
	TArray<int32> Indexes = TArray<int32>();

	for (int i = 0; i < Points.Num(); ++i)
	{
		switch (XType)
		{
		case EAxisType::X:
			XValue = Points[i].Location.X;
			break;
		case EAxisType::Y:
			XValue = Points[i].Location.Y;
			break;
		case EAxisType::Z:
			XValue = Points[i].Location.Z;
			break;
		default:
			break;
		}

		switch (YType)
		{
		case EAxisType::U:
			YValue = U[i];
			break;
		case EAxisType::V:
			YValue = V[i];
			break;
		case EAxisType::W:
			YValue = W[i];
			break;
		default:
			break;
		}

		if (XValue >= XMin && XValue <= XMax && YValue >= YMin && YValue <= YMax)
		{
			Indexes.Emplace(i);
		}
	}

	BrushPoints(Indexes, Add, Rebuild);
}

void APlot3D::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);
		}
	}

	BrushPoints(Indexes, Add, Rebuild);
}

void APlot3D::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);
		}
	}

	BrushPoints(Indexes, Add, Rebuild);
}