PlotCanvas.cpp 6.52 KB
Newer Older
1 2 3 4 5 6 7 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
// 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"

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

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

	for (int i = 0; i < PawnBase->Plot3D->Points.Num(); ++i)
	{
		InitializeDrawingTools(1, PawnBase->Plot3D->Points[i].Color.R, PawnBase->Plot3D->Points[i].Color.G, PawnBase->Plot3D->Points[i].Color.B);
69
		DrawDot((PawnBase->Plot3D->Points[i].Location.X - PawnBase->Plot3D->MinX) / (PawnBase->Plot3D->MaxX - PawnBase->Plot3D->MinX) * canvasWidth, (PawnBase->Plot3D->U[i] - PawnBase->Plot3D->MinU) / (PawnBase->Plot3D->MaxU - PawnBase->Plot3D->MinU) * canvasHeight);
70 71 72 73 74
	}

	UpdateCanvas();
}

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 102 103 104 105 106
void UPlotCanvas::DrawParallelCoordinatesPlot()
{
	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;
		uint8 r = PawnBase->Plot3D->Points[i].Color.R;
		uint8 g = PawnBase->Plot3D->Points[i].Color.G;
		uint8 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);
	}

	UpdateCanvas();
}

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
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;
123
				DrawPixel(tx, ty, *(canvasBrushPixelPtr + 2), *(canvasBrushPixelPtr + 1), *(canvasBrushPixelPtr), *(canvasBrushPixelPtr + 3));
124 125 126 127 128
			}
		}
	}
}

129 130 131 132 133 134 135 136 137
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);
	}
}

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

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