PlotCanvas.cpp 10.2 KB
Newer Older
1 2 3 4
// Fill out your copyright notice in the Description page of Project Settings.

#include "PlotCanvas.h"
#include "PawnBase.h"
5
#include "DataComponent.h"
6 7
#include "Plot3D.h"
#include "Runtime/Engine/Classes/Kismet/GameplayStatics.h"
8
#include "Runtime/Engine/Classes/Kismet/KismetMathLibrary.h"
9

10 11
void UPlotCanvas::SetXAxisType(EAxisType Type)
{
Jan Kremer's avatar
Jan Kremer committed
12 13
	XAxisType = Type;

14 15 16 17 18 19
	APawn* Pawn = UGameplayStatics::GetPlayerPawn(this, 0);
	APawnBase* PawnBase = Cast<APawnBase>(Pawn);

	switch (Type)
	{
	case EAxisType::X:
20 21 22
		XArray = &PawnBase->Data->X;
		XMin = PawnBase->Data->MinX;
		XMax = PawnBase->Data->MaxX;
23 24
		break;
	case EAxisType::Y:
25 26 27
		XArray = &PawnBase->Data->Y;
		XMin = PawnBase->Data->MinY;
		XMax = PawnBase->Data->MaxY;
28 29
		break;
	case EAxisType::Z:
30 31 32
		XArray = &PawnBase->Data->Z;
		XMin = PawnBase->Data->MinZ;
		XMax = PawnBase->Data->MaxZ;
33 34
		break;
	case EAxisType::U:
35 36 37
		XArray = &PawnBase->Data->U;
		XMin = PawnBase->Data->MinU;
		XMax = PawnBase->Data->MaxU;
38 39
		break;
	case EAxisType::V:
40 41 42
		XArray = &PawnBase->Data->V;
		XMin = PawnBase->Data->MinV;
		XMax = PawnBase->Data->MaxV;
43 44
		break;
	case EAxisType::W:
45 46 47
		XArray = &PawnBase->Data->W;
		XMin = PawnBase->Data->MinW;
		XMax = PawnBase->Data->MaxW;
48 49
		break;
	case EAxisType::LengthUVW:
50 51 52
		XArray = &PawnBase->Data->Length;
		XMin = PawnBase->Data->MinLength;
		XMax = PawnBase->Data->MaxLength;
53 54 55 56 57 58 59 60
		break;
	default:
		break;
	}
}

void UPlotCanvas::SetYAxisType(EAxisType Type)
{
Jan Kremer's avatar
Jan Kremer committed
61 62
	YAxisType = Type;

63 64 65 66 67 68
	APawn* Pawn = UGameplayStatics::GetPlayerPawn(this, 0);
	APawnBase* PawnBase = Cast<APawnBase>(Pawn);

	switch (Type)
	{
	case EAxisType::X:
69 70 71
		YArray = &PawnBase->Data->X;
		YMin = PawnBase->Data->MinX;
		YMax = PawnBase->Data->MaxX;
72 73
		break;
	case EAxisType::Y:
74 75 76
		YArray = &PawnBase->Data->Y;
		YMin = PawnBase->Data->MinY;
		YMax = PawnBase->Data->MaxY;
77 78
		break;
	case EAxisType::Z:
79 80 81
		YArray = &PawnBase->Data->Z;
		YMin = PawnBase->Data->MinZ;
		YMax = PawnBase->Data->MaxZ;
82 83
		break;
	case EAxisType::U:
84 85 86
		YArray = &PawnBase->Data->U;
		YMin = PawnBase->Data->MinU;
		YMax = PawnBase->Data->MaxU;
87 88
		break;
	case EAxisType::V:
89 90 91
		YArray = &PawnBase->Data->V;
		YMin = PawnBase->Data->MinV;
		YMax = PawnBase->Data->MaxV;
92 93
		break;
	case EAxisType::W:
94 95 96
		YArray = &PawnBase->Data->W;
		YMin = PawnBase->Data->MinW;
		YMax = PawnBase->Data->MaxW;
97 98
		break;
	case EAxisType::LengthUVW:
99 100 101
		YArray = &PawnBase->Data->Length;
		YMin = PawnBase->Data->MinLength;
		YMax = PawnBase->Data->MaxLength;
102 103 104 105 106
		break;
	default:
		break;
	}
}
107 108 109 110 111 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
void UPlotCanvas::InitializeCanvas(const int32 pixelsH, const int32 pixelsV)
{
	//dynamic texture initialization
	canvasWidth = pixelsH;
	canvasHeight = pixelsV;

	dynamicCanvas = UTexture2D::CreateTransient(canvasWidth, canvasHeight);
#if WITH_EDITORONLY_DATA
	dynamicCanvas->MipGenSettings = TextureMipGenSettings::TMGS_NoMipmaps;
#endif
	dynamicCanvas->CompressionSettings = TextureCompressionSettings::TC_VectorDisplacementmap;
	dynamicCanvas->SRGB = 1;
	dynamicCanvas->AddToRoot();
	dynamicCanvas->Filter = TextureFilter::TF_Nearest;
	dynamicCanvas->UpdateResource();

	echoUpdateTextureRegion = std::unique_ptr<FUpdateTextureRegion2D>(new FUpdateTextureRegion2D(0, 0, 0, 0, canvasWidth, canvasHeight));


	// buffers initialization
	bytesPerPixel = 4; // r g b a
	bufferPitch = canvasWidth * bytesPerPixel;
	bufferSize = canvasWidth * canvasHeight * bytesPerPixel;
	canvasPixelData = std::unique_ptr<uint8[]>(new uint8[bufferSize]);

	ClearCanvas();
}

void UPlotCanvas::InitializeDrawingTools(const int32 brushRadius, uint8 r, uint8 g, uint8 b)
{
	radius = brushRadius;
	brushBufferSize = radius * radius * 4 * bytesPerPixel; //2r*2r * bpp
	canvasBrushMask = std::unique_ptr<uint8[]>(new uint8[brushBufferSize]);
	uint8* canvasBrushPixelPtr = canvasBrushMask.get();
	for (int px = -radius; px < radius; ++px)
	{
		for (int py = -radius; py < radius; ++py)
		{
			int32 tx = px + radius;
			int32 ty = py + radius;
			canvasBrushPixelPtr = canvasBrushMask.get() + (tx + +ty * 2 * radius) * bytesPerPixel;
			if (px*px + py * py < radius*radius)
			{
				setPixelColor(canvasBrushPixelPtr, r,g,b, 255); //black alpha 255 - bgra
			}
			else
			{
				setPixelColor(canvasBrushPixelPtr, 0, 0, 0, 0); // alpha 0
			}
		}
	}
}

160
void UPlotCanvas::DrawScatterPlot()
161
{
162 163
	ClearCanvas();

164 165 166 167 168
	APawn* Pawn = UGameplayStatics::GetPlayerPawn(this, 0);
	APawnBase* PawnBase = Cast<APawnBase>(Pawn);

	for (int i = 0; i < PawnBase->Plot3D->Points.Num(); ++i)
	{
169
		if (PawnBase->Data->IsBrushed[i])
170 171 172 173 174 175 176 177
		{
			InitializeDrawingTools(1, 0, 0, 0);
		}
		else
		{
			InitializeDrawingTools(1, PawnBase->Plot3D->Points[i].Color.R, PawnBase->Plot3D->Points[i].Color.G, PawnBase->Plot3D->Points[i].Color.B);
		}
		DrawDot(((*XArray)[i] - XMin) / (XMax - XMin) * canvasWidth, ((*YArray)[i] - YMin) / (YMax - YMin) * canvasHeight);
178 179 180 181 182
	}

	UpdateCanvas();
}

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
void UPlotCanvas::DrawHistogramPlot(int32 bins)
{
	ClearCanvas();

	APawn* Pawn = UGameplayStatics::GetPlayerPawn(this, 0);
	APawnBase* PawnBase = Cast<APawnBase>(Pawn);

	float Diff = XMax - XMin;
	float Thickness = canvasWidth / (float)bins;

	TArray<int32> XCount = TArray<int32>();
	TArray<int32> XBrushedCount = TArray<int32>();
	XCount.Init(0, bins);
	XBrushedCount.Init(0, bins);

	for (int i = 0; i < PawnBase->Plot3D->Points.Num(); ++i)
	{
		float CurX = (*XArray)[i];
201 202 203 204 205 206 207 208 209
		int32 MyIndex;
		if (Diff == 0)
		{
			MyIndex = 0;
		}
		else
		{
			MyIndex = (CurX - XMin) / Diff * (bins - 1);
		}
210
		++XCount[MyIndex];
211
		if (PawnBase->Data->IsBrushed[i])
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
		{
			++XBrushedCount[MyIndex];
		}
	}

	int32 MaxIndex, MaxCount;
	UKismetMathLibrary::MaxOfIntArray(XCount, MaxIndex, MaxCount);

	for (int i = 0; i < bins; ++i)
	{
		int32 x0 = i * Thickness;
		int32 x1 = x0 + Thickness;

		int32 y0 = 0;
		int32 y1 = XCount[i] / (float)MaxCount * canvasHeight;

		FVector ColorVec;
		if (XBrushedCount[i] == XCount[i])
		{
			ColorVec = FVector(0, 0, 0);
		}
		else
		{
235
			ColorVec = PawnBase->Data->TransferFunction->GetVectorValue((i + 0.5) / (float)bins);
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
		}

		uint8 r = ColorVec.X * 255;
		uint8 g = ColorVec.Y * 255;
		uint8 b = ColorVec.Z * 255;

		DrawRectangle(x0, y0, x1, y1, r, g, b, 255);
	}

	UpdateCanvas();
}

void UPlotCanvas::DrawRectangle(int32 x1, int32 y1, int32 x2, int32 y2, const uint8 r, const uint8 g, const uint8 b, const uint8 a)
{
	for (int x = x1; x < x2; ++x)
	{
		for (int y = y1; y < y2; ++y)
		{
			DrawPixel(x, y, r, g, b, 255);
		}
	}
}

259 260
void UPlotCanvas::DrawParallelCoordinatesPlot()
{
261 262
	ClearCanvas();

263 264 265 266 267 268 269 270 271 272 273
	APawn* Pawn = UGameplayStatics::GetPlayerPawn(this, 0);
	APawnBase* PawnBase = Cast<APawnBase>(Pawn);

	for (int i = 0; i < PawnBase->Plot3D->Points.Num(); ++i)
	{
		int32 x0 = 0;
		int32 x1 = canvasWidth * 1 / 5;
		int32 x2 = canvasWidth * 2 / 5;
		int32 x3 = canvasWidth * 3 / 5;
		int32 x4 = canvasWidth * 4 / 5;
		int32 x5 = canvasWidth;
274 275 276 277 278 279
		int32 y0 = (PawnBase->Data->X[i] - PawnBase->Data->MinX) / (PawnBase->Data->MaxX - PawnBase->Data->MinX) * canvasHeight / 4;
		int32 y1 = (PawnBase->Data->Y[i] - PawnBase->Data->MinY) / (PawnBase->Data->MaxY - PawnBase->Data->MinY) * canvasHeight / 4;
		int32 y2 = (PawnBase->Data->Z[i] - PawnBase->Data->MinZ) / (PawnBase->Data->MaxZ - PawnBase->Data->MinZ) * canvasHeight / 4;
		int32 y3 = (PawnBase->Data->U[i] - PawnBase->Data->MinU) / (PawnBase->Data->MaxU - PawnBase->Data->MinU) * canvasHeight / 4;
		int32 y4 = (PawnBase->Data->V[i] - PawnBase->Data->MinV) / (PawnBase->Data->MaxV - PawnBase->Data->MinV) * canvasHeight / 4;
		int32 y5 = (PawnBase->Data->W[i] - PawnBase->Data->MinW) / (PawnBase->Data->MaxW - PawnBase->Data->MinW) * canvasHeight / 4;
280 281
		uint8 r, g, b;

282
		if (PawnBase->Data->IsBrushed[i])
283 284 285 286
		{
		}
		else
		{
287 288 289
			r = PawnBase->Data->PColor[i].X;
			g = PawnBase->Data->PColor[i].Y;
			b = PawnBase->Data->PColor[i].Z;
290 291 292 293 294 295 296 297 298

			DrawLine(x0, y0, x1, y1, r, g, b, 255);
			DrawLine(x1, y1, x2, y2, r, g, b, 255);
			DrawLine(x2, y2, x3, y3, r, g, b, 255);
			DrawLine(x3, y3, x4, y4, r, g, b, 255);
			DrawLine(x4, y4, x5, y5, r, g, b, 255);
		}


299 300 301 302 303
	}

	UpdateCanvas();
}

304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
void UPlotCanvas::DrawDot(const int32 pixelCoordX, const int32 pixelCoordY)
{
	uint8* canvasPixelPtr = canvasPixelData.get();
	const uint8* canvasBrushPixelPtr = canvasBrushMask.get();
	for (int px = -radius; px < radius; ++px)
	{
		for (int py = -radius; py < radius; ++py)
		{
			int32 tbx = px + radius;
			int32 tby = py + radius;

			canvasBrushPixelPtr = canvasBrushMask.get() + (tbx + tby * 2 * radius) * bytesPerPixel;
			if (*(canvasBrushPixelPtr + 3) == 255) // check the alpha value of the pixel of the brush mask
			{
				int32 tx = pixelCoordX + px;
				int32 ty = pixelCoordY + py;
320
				DrawPixel(tx, ty, *(canvasBrushPixelPtr + 2), *(canvasBrushPixelPtr + 1), *(canvasBrushPixelPtr), *(canvasBrushPixelPtr + 3));
321 322 323 324 325
			}
		}
	}
}

326 327 328 329 330 331 332 333 334
void UPlotCanvas::DrawPixel(const int32 x, const int32 y, const uint8 r, const uint8 g, const uint8 b, const uint8 a)
{
	if (x >= 0 && x < canvasWidth && y >= 0 && y < canvasHeight)
	{
		uint8* canvasPixelPtr = canvasPixelData.get() + (x + y * canvasWidth) * bytesPerPixel;
		setPixelColor(canvasPixelPtr, r, g, b, a);
	}
}

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
void UPlotCanvas::ClearCanvas()
{
	uint8* canvasPixelPtr = canvasPixelData.get();
	for (int i = 0; i < canvasWidth * canvasHeight; ++i)
	{
		setPixelColor(canvasPixelPtr, 128, 128, 128, 0);
		canvasPixelPtr += bytesPerPixel;
	}
	UpdateCanvas();
}

void UPlotCanvas::UpdateCanvas()
{
	if (echoUpdateTextureRegion)
	{
		dynamicCanvas->UpdateTextureRegions((int32)0, (uint32)1, echoUpdateTextureRegion.get(), (uint32)bufferPitch, (uint32)bytesPerPixel, canvasPixelData.get());
	}
}

void UPlotCanvas::setPixelColor(uint8*& pointer, uint8 red, uint8 green, uint8 blue, uint8 alpha)
{
	*pointer = blue; //b
	*(pointer + 1) = green; //g
	*(pointer + 2) = red; //r
	*(pointer + 3) = alpha; //a
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
}

void UPlotCanvas::DrawLine(int32 x1, int32 y1, int32 x2, int32 y2, const uint8 r, const uint8 g, const uint8 b, const uint8 a)
{
	// Bresenham's line algorithm
	const bool steep = (fabs(y2 - y1) > fabs(x2 - x1));
	if (steep)
	{
		std::swap(x1, y1);
		std::swap(x2, y2);
	}

	if (x1 > x2)
	{
		std::swap(x1, x2);
		std::swap(y1, y2);
	}

	const float dx = x2 - x1;
	const float dy = fabs(y2 - y1);

	float error = dx / 2.0f;
	const int ystep = (y1 < y2) ? 1 : -1;
	int y = (int)y1;

	const int maxX = (int)x2;

	for (int x = (int)x1; x < maxX; x++)
	{
		if (steep)
		{
			DrawPixel(y, x, r, g, b, a);
		}
		else
		{
			DrawPixel(x, y, r, g, b, a);
		}

		error -= dy;
		if (error < 0)
		{
			y += ystep;
			error += dx;
		}
	}
405
}