Agrarsense
Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
ATreeHeightsFromImage Class Reference

#include <TreeHeightsFromImage.h>

Inheritance diagram for ATreeHeightsFromImage:
Inheritance graph
[legend]
Collaboration diagram for ATreeHeightsFromImage:
Collaboration graph
[legend]

Public Member Functions

 ATreeHeightsFromImage ()
 

Public Attributes

UTexture2D * treeMapImage
 

Protected Member Functions

virtual void BeginPlay () override
 
float EuclideanDistance (const FColor &Color1, const FColor &Color2)
 
FColor FindClosestColor (const FColor &TargetColor, const TMap< int32, FColor > &ColorMap)
 

Protected Attributes

const TMap< int, FColor > colorHeight
 

Detailed Description

Definition at line 15 of file TreeHeightsFromImage.h.

Constructor & Destructor Documentation

◆ ATreeHeightsFromImage()

ATreeHeightsFromImage::ATreeHeightsFromImage ( )

Definition at line 8 of file TreeHeightsFromImage.cpp.

9{
10 PrimaryActorTick.bCanEverTick = false;
11}

Member Function Documentation

◆ BeginPlay()

void ATreeHeightsFromImage::BeginPlay ( )
overrideprotectedvirtual

Definition at line 13 of file TreeHeightsFromImage.cpp.

14{
15 Super::BeginPlay();
16
17#if WITH_EDITOR
18 if (treeMapImage)
19 {
20 // Lock the texture to allow access to data
21 FTexture2DMipMap& Mip = treeMapImage->GetPlatformData()->Mips[0];
22 void* Data = Mip.BulkData.Lock(LOCK_READ_ONLY);
23
24 if (Data)
25 {
26 int32 Width = treeMapImage->GetSizeX();
27 int32 Height = treeMapImage->GetSizeY();
28
29 if (Width <= 0 || Height <= 0)
30 return;
31
32 UE_LOG(LogTemp, Warning, TEXT("Image width: %i, Height: %i"), Width, Height);
33
34 uint8* Pixels = static_cast<uint8*>(Data);
35 for (int32 y = 0; y < Height; y++)
36 {
37 for (int32 x = 0; x < Width; x++)
38 {
39 int32 Index = (y * Width + x) * 4; // 4 bytes per pixel (RGBA)
40 uint8 R = Pixels[Index];
41 uint8 G = Pixels[Index + 1];
42 uint8 B = Pixels[Index + 2];
43 uint8 A = Pixels[Index + 3];
44
45 FColor pixelColor = FColor(B, G, R);
46
47 if (x == 446 && y == 353)
48 {
49 UE_LOG(LogTemp, Warning, TEXT("Coordinates RGB = %i, %i, %i"), B, G, R);
50 FColor ClosestColor = FindClosestColor(pixelColor, colorHeight);
51 UE_LOG(LogTemp, Warning, TEXT("Closest color: R=%d, G=%d, B=%d"), ClosestColor.R, ClosestColor.G, ClosestColor.B);
52
53 for (const auto& Pair : colorHeight)
54 {
55 if (Pair.Value == ClosestColor)
56 UE_LOG(LogTemp, Warning, TEXT("This should be a %im size tree"), Pair.Key);
57 }
58
59 }
60
61 if (x == Width / 2 && y == Height / 2)
62 {
63 UE_LOG(LogTemp, Warning, TEXT("Coordinates middlepoint Width: %i, Height: %i"), x, y);
64 //UE_LOG(LogTemp, Warning, TEXT("ARGB: %i,%i,%i,%i"), A,R,G,B);
65 }
66 }
67 }
68
69 // Unlock the texture
70 Mip.BulkData.Unlock();
71 }
72 }
73#endif
74}
const TMap< int, FColor > colorHeight
FColor FindClosestColor(const FColor &TargetColor, const TMap< int32, FColor > &ColorMap)

References colorHeight, FindClosestColor(), and treeMapImage.

◆ EuclideanDistance()

float ATreeHeightsFromImage::EuclideanDistance ( const FColor &  Color1,
const FColor &  Color2 
)
protected

Definition at line 76 of file TreeHeightsFromImage.cpp.

77{
78 float R1 = Color1.R;
79 float G1 = Color1.G;
80 float B1 = Color1.B;
81 float R2 = Color2.R;
82 float G2 = Color2.G;
83 float B2 = Color2.B;
84 return FMath::Sqrt(FMath::Square(R2 - R1) + FMath::Square(G2 - G1) + FMath::Square(B2 - B1));
85}

Referenced by FindClosestColor().

◆ FindClosestColor()

FColor ATreeHeightsFromImage::FindClosestColor ( const FColor &  TargetColor,
const TMap< int32, FColor > &  ColorMap 
)
protected

Definition at line 87 of file TreeHeightsFromImage.cpp.

88{
89 float MinDistance = FLT_MAX;
90 FColor ClosestColor;
91
92 for (const auto& Pair : ColorMap)
93 {
94 float Distance = EuclideanDistance(TargetColor, Pair.Value);
95 if (Distance < MinDistance)
96 {
97 MinDistance = Distance;
98 ClosestColor = Pair.Value;
99 }
100 }
101
102 return ClosestColor;
103}
float EuclideanDistance(const FColor &Color1, const FColor &Color2)

References EuclideanDistance().

Referenced by BeginPlay().

Member Data Documentation

◆ colorHeight

const TMap<int, FColor> ATreeHeightsFromImage::colorHeight
protected
Initial value:
= {
{0, FColor(48, 18, 59)},
{1, FColor(59, 48, 129)},
{2, FColor(67, 77, 183)},
{3, FColor(70, 104, 222)},
{4, FColor(70, 130, 247)},
{5, FColor(61, 156, 252)},
{6, FColor(44, 182, 239)},
{7, FColor(30, 205, 216)},
{8, FColor(26, 223, 190)},
{9, FColor(42, 237, 163)},
{10, FColor(73, 247, 130)},
{11, FColor(111, 253, 97)},
{12, FColor(147, 253, 71)},
{13, FColor(177, 248, 56)},
{14, FColor(203, 236, 53)},
{15, FColor(226, 219, 56)},
{16, FColor(243, 199, 57)},
{17, FColor(252, 176, 53)},
{18, FColor(253, 148, 43)},
{19, FColor(249, 118, 30)},
{20, FColor(239, 89, 17)},
{21, FColor(224, 65, 9)},
{22, FColor(205, 45, 5)},
{23, FColor(181, 28, 2)},
{24, FColor(153, 14, 1)},
{25, FColor(122, 4, 3)}
}

Definition at line 31 of file TreeHeightsFromImage.h.

Referenced by BeginPlay().

◆ treeMapImage

UTexture2D* ATreeHeightsFromImage::treeMapImage

Definition at line 64 of file TreeHeightsFromImage.h.

Referenced by BeginPlay().


The documentation for this class was generated from the following files: