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

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

9 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 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 92 93 94 95 96 97 98 99 100 101
void UPlotCanvas::SetXAxisType(EAxisType Type)
{
	APawn* Pawn = UGameplayStatics::GetPlayerPawn(this, 0);
	APawnBase* PawnBase = Cast<APawnBase>(Pawn);

	switch (Type)
	{
	case EAxisType::X:
		XArray = &PawnBase->Plot3D->X;
		XMin = PawnBase->Plot3D->MinX;
		XMax = PawnBase->Plot3D->MaxX;
		break;
	case EAxisType::Y:
		XArray = &PawnBase->Plot3D->Y;
		XMin = PawnBase->Plot3D->MinY;
		XMax = PawnBase->Plot3D->MaxY;
		break;
	case EAxisType::Z:
		XArray = &PawnBase->Plot3D->Z;
		XMin = PawnBase->Plot3D->MinZ;
		XMax = PawnBase->Plot3D->MaxZ;
		break;
	case EAxisType::U:
		XArray = &PawnBase->Plot3D->U;
		XMin = PawnBase->Plot3D->MinU;
		XMax = PawnBase->Plot3D->MaxU;
		break;
	case EAxisType::V:
		XArray = &PawnBase->Plot3D->V;
		XMin = PawnBase->Plot3D->MinV;
		XMax = PawnBase->Plot3D->MaxV;
		break;
	case EAxisType::W:
		XArray = &PawnBase->Plot3D->W;
		XMin = PawnBase->Plot3D->MinW;
		XMax = PawnBase->Plot3D->MaxW;
		break;
	case EAxisType::LengthUVW:
		XArray = &PawnBase->Plot3D->Length;
		XMin = PawnBase->Plot3D->MinLength;
		XMax = PawnBase->Plot3D->MaxLength;
		break;
	default:
		break;
	}
}

void UPlotCanvas::SetYAxisType(EAxisType Type)
{
	APawn* Pawn = UGameplayStatics::GetPlayerPawn(this, 0);
	APawnBase* PawnBase = Cast<APawnBase>(Pawn);

	switch (Type)
	{
	case EAxisType::X:
		YArray = &PawnBase->Plot3D->X;
		YMin = PawnBase->Plot3D->MinX;
		YMax = PawnBase->Plot3D->MaxX;
		break;
	case EAxisType::Y:
		YArray = &PawnBase->Plot3D->Y;
		YMin = PawnBase->Plot3D->MinY;
		YMax = PawnBase->Plot3D->MaxY;
		break;
	case EAxisType::Z:
		YArray = &PawnBase->Plot3D->Z;
		YMin = PawnBase->Plot3D->MinZ;
		YMax = PawnBase->Plot3D->MaxZ;
		break;
	case EAxisType::U:
		YArray = &PawnBase->Plot3D->U;
		YMin = PawnBase->Plot3D->MinU;
		YMax = PawnBase->Plot3D->MaxU;
		break;
	case EAxisType::V:
		YArray = &PawnBase->Plot3D->V;
		YMin = PawnBase->Plot3D->MinV;
		YMax = PawnBase->Plot3D->MaxV;
		break;
	case EAxisType::W:
		YArray = &PawnBase->Plot3D->W;
		YMin = PawnBase->Plot3D->MinW;
		YMax = PawnBase->Plot3D->MaxW;
		break;
	case EAxisType::LengthUVW:
		YArray = &PawnBase->Plot3D->Length;
		YMin = PawnBase->Plot3D->MinLength;
		YMax = PawnBase->Plot3D->MaxLength;
		break;
	default:
		break;
	}
}
102 103 104 105 106 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
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
			}
		}
	}
}

155
void UPlotCanvas::DrawScatterPlot()
156
{
157 158
	ClearCanvas();

159 160 161 162 163
	APawn* Pawn = UGameplayStatics::GetPlayerPawn(this, 0);
	APawnBase* PawnBase = Cast<APawnBase>(Pawn);

	for (int i = 0; i < PawnBase->Plot3D->Points.Num(); ++i)
	{
164 165 166 167 168 169 170 171 172
		if (PawnBase->Plot3D->IsBrushed[i])
		{
			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);
173 174 175 176 177
	}

	UpdateCanvas();
}

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
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];
196 197 198 199 200 201 202 203 204
		int32 MyIndex;
		if (Diff == 0)
		{
			MyIndex = 0;
		}
		else
		{
			MyIndex = (CurX - XMin) / Diff * (bins - 1);
		}
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
		++XCount[MyIndex];
		if (PawnBase->Plot3D->IsBrushed[i])
		{
			++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
		{
			ColorVec = PawnBase->Plot3D->TransferFunction->GetVectorValue((i + 0.5) / (float)bins);
		}

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

254 255
void UPlotCanvas::DrawParallelCoordinatesPlot()
{
256 257
	ClearCanvas();

258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
	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;
		int32 y0 = (PawnBase->Plot3D->Points[i].Location.X - PawnBase->Plot3D->MinX) / (PawnBase->Plot3D->MaxX - PawnBase->Plot3D->MinX) * canvasHeight / 4;
		int32 y1 = (PawnBase->Plot3D->Points[i].Location.Y - PawnBase->Plot3D->MinY) / (PawnBase->Plot3D->MaxY - PawnBase->Plot3D->MinY) * canvasHeight / 4;
		int32 y2 = (PawnBase->Plot3D->Points[i].Location.Z - PawnBase->Plot3D->MinZ) / (PawnBase->Plot3D->MaxZ - PawnBase->Plot3D->MinZ) * canvasHeight / 4;
		int32 y3 = (PawnBase->Plot3D->U[i] - PawnBase->Plot3D->MinU) / (PawnBase->Plot3D->MaxU - PawnBase->Plot3D->MinU) * canvasHeight / 4;
		int32 y4 = (PawnBase->Plot3D->V[i] - PawnBase->Plot3D->MinV) / (PawnBase->Plot3D->MaxV - PawnBase->Plot3D->MinV) * canvasHeight / 4;
		int32 y5 = (PawnBase->Plot3D->W[i] - PawnBase->Plot3D->MinW) / (PawnBase->Plot3D->MaxW - PawnBase->Plot3D->MinW) * canvasHeight / 4;
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
		uint8 r, g, b;

		if (PawnBase->Plot3D->IsBrushed[i])
		{
		}
		else
		{
			r = PawnBase->Plot3D->Points[i].Color.R;
			g = PawnBase->Plot3D->Points[i].Color.G;
			b = PawnBase->Plot3D->Points[i].Color.B;

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


294 295 296 297 298
	}

	UpdateCanvas();
}

299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
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;
315
				DrawPixel(tx, ty, *(canvasBrushPixelPtr + 2), *(canvasBrushPixelPtr + 1), *(canvasBrushPixelPtr), *(canvasBrushPixelPtr + 3));
316 317 318 319 320
			}
		}
	}
}

321 322 323 324 325 326 327 328 329
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);
	}
}

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

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