Giter Club home page Giter Club logo

voronoidiagram's Introduction

VoronoiDiagram

Updated this repository to work with at least 4.26 -> https://github.com/afuzzyllama/VoronoiDiagramUE4

Hey, i uploaded a little modified and updated version from this guy.

All credits go to him. Feel free to use it and improve it.

Just copy and install the folder into the plugins section of your project.

The template :

Project.build.cs ( add 'VoronoiDiagram' into plubicDependency)
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "VoronoiDiagram" });
file.cpp
// Fill out your copyright notice in the Description page of Project Settings.


#include "MyBlueprintFunctionLibrary.h"

#include "Math/UnrealMathUtility.h"
#include "Kismet/KismetMathLibrary.h"
#include "VoronoiDiagram.h"

TArray<FVector2D> VoronoiPointsVar;
TArray<int32> VerticesVar;

void UMyBlueprintFunctionLibrary::MyCustomFunction(int32 RDNNumber, int32 width, int32 height, bool random, TArray<FVector> InPoints, TArray<FVector2D>& VoronoiPoints, TArray<int32>& NbrVertices)
{

	VoronoiPointsVar.Empty();
	VerticesVar.Empty();

	TSharedPtr<FVoronoiDiagram> VoronoiDiagram(new FVoronoiDiagram(FIntRect(0, 0, width, height)));
	TArray<FIntPoint> Points;


	if (random == true) {
		for (int32 i = 0; i < RDNNumber; ++i)
		{
			Points.AddUnique(FIntPoint(FMath::RandRange(0, width - 1), FMath::RandRange(0, height - 1)));
		}
	}
	else {
		for (int32 i = 0; i < InPoints.Num(); ++i)
		{
			Points.AddUnique(FIntPoint(InPoints[i].X, InPoints[i].Y));
		}
	}
	VoronoiDiagram->AddPoints(Points);


	VoronoiDiagram->GenerateSites(2);

	TArray<FVector2D> test;

	for (int32 i = 0; i < VoronoiDiagram->GeneratedSites.Num(); ++i)
	{

		test = VoronoiDiagram->GeneratedSites[i].Vertices;
		// UE_LOG(LogTemp, Error, TEXT("%d"), VoronoiDiagram->GeneratedSites[i].Vertices.Num());
		//VoronoiDiagram->GeneratedSites[i].Vertices;
		NbrVertices.Add(VoronoiDiagram->GeneratedSites[i].Vertices.Num());
		VoronoiPoints.Append(test);
	}

	VoronoiPointsVar = VoronoiPoints;
	VerticesVar = NbrVertices;
	

	// FVoronoiDiagramHelper::GenerateTexture(VoronoiDiagram, MyTexture);
}

void UMyBlueprintFunctionLibrary::calculateAllPoints(TArray<FisNeighbour>& isNeighbourArrayFirst)
{
	int32 FirstIndex = 0;
	int32 loopIt = 0;
	TArray<FVector> CurrentsVectorPoints;
	TArray<FisNeighbour> isNeighbourArray;

	for (int32 i = 0; i < VerticesVar.Num(); ++i)
	{
		// replace if / else
		(i == 0) ? loopIt = VerticesVar[i] : loopIt = FirstIndex + VerticesVar[i];
		
		FVector Centroid = FVector(0.0f, 0.0f, 0.0f);
		CurrentsVectorPoints.Empty();

		

		for (FirstIndex; FirstIndex < loopIt; ++FirstIndex)
		{			
			CurrentsVectorPoints.Add(FVector(FMath::RoundHalfFromZero(VoronoiPointsVar[FirstIndex].X), FMath::RoundHalfFromZero(VoronoiPointsVar[FirstIndex].Y), 0.0f));
			Centroid = Centroid + FVector(FMath::RoundHalfFromZero(VoronoiPointsVar[FirstIndex].X), FMath::RoundHalfFromZero(VoronoiPointsVar[FirstIndex].Y), 0.0f);

			//UE_LOG(LogTemp, Error, TEXT("%d -- %d || %d -- %d ---- %s"), FirstIndex, loopIt, VerticesVar[i], i, *VoronoiPointsVar[FirstIndex].ToString());
		}


		// Add the points into a structure to use it elsewhere.		
		FisNeighbour temp;
		temp.CellID = i;
		temp.Color = FLinearColor(FMath::RandRange(0.0f, 1.0f), FMath::RandRange(0.0f, 1.0f), FMath::RandRange(0.0f, 1.0f), 1.0f);
		temp.Centroid = UKismetMathLibrary::Divide_VectorInt(Centroid, VerticesVar[i]);
		temp.VoronoiPoints = CurrentsVectorPoints;
		temp.NeighbourCellID.Add(0);
		isNeighbourArray.Add(temp);

	}

	isNeighbourArrayFirst = isNeighbourArray;
}

void UMyBlueprintFunctionLibrary::CalculateNeighbour(TArray<FisNeighbour>& isCalculateNeighbourArray) {

	TArray<int32> TmpAdjZone;

	TArray<FisNeighbour> TempStrucArray;
	calculateAllPoints(TempStrucArray);

	TArray<FisNeighbour> secondStruc = TempStrucArray;


	for (int32 i = 0; i < TempStrucArray.Num(); ++i) {

		TempStrucArray[i].NeighbourCellID.Empty();

		for (int32 x = 0; x < TempStrucArray[i].VoronoiPoints.Num(); ++x)
		{

			//UE_LOG(LogTemp, Error, TEXT("%d -- %d -- %s"), x, TempStrucArray[i].VoronoiPoints.Num(), *TempStrucArray[i].VoronoiPoints[x].ToString());
						
			for (int32 y = 0; y < secondStruc.Num(); ++y)
			{

				if (secondStruc[y].VoronoiPoints.Contains(TempStrucArray[i].VoronoiPoints[x]) == true) {
					if (TempStrucArray[i].CellID != secondStruc[y].CellID) {
						TmpAdjZone.AddUnique(secondStruc[y].CellID);
						//UE_LOG(LogTemp, Warning, TEXT("%d, True: %d - %s"), i, y, *TempStrucArray[i].VoronoiPoints[x].ToString());
					}
				}
				else {
					//UE_LOG(LogTemp, Warning, TEXT("%d, False: %d"), i, y);
				}

			}
			//UE_LOG(LogTemp, Warning, TEXT("------"));
			// complete the 3rd loop

		}

		// complete the 2nd loop
		TempStrucArray[i].NeighbourCellID = TmpAdjZone;
		TmpAdjZone.Empty();

	}

	isCalculateNeighbourArray = TempStrucArray;

	// complete the 1st loop

}
File.h
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "MyBlueprintFunctionLibrary.generated.h"

/**
 * 
 */
USTRUCT(BlueprintType)
struct FisNeighbour
{
    GENERATED_USTRUCT_BODY()

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
        int32 CellID;
    UPROPERTY(EditAnywhere, BlueprintReadWrite)
        TArray<FVector> VoronoiPoints;
    UPROPERTY(EditAnywhere, BlueprintReadWrite)
        FLinearColor Color;
    UPROPERTY(EditAnywhere, BlueprintReadWrite)
        TArray<int32> NeighbourCellID;
    UPROPERTY(EditAnywhere, BlueprintReadWrite)
        FVector Centroid;
};

UCLASS()
class PROCEDURALGENERATION_API UMyBlueprintFunctionLibrary : public UBlueprintFunctionLibrary
{
    GENERATED_BODY()

public:
        
        UFUNCTION(BlueprintCallable, Category = "Custom")
            static void MyCustomFunction(int32 RDNNumber, int32 width, int32 height, bool random, TArray<FVector> InPoints, TArray<FVector2D>& VoronoiPoints, TArray<int32>& NbrVertices);
    
        UFUNCTION(BlueprintCallable, Category = "Custom")
            static void calculateAllPoints(TArray<FisNeighbour>& isNeighbourArrayFirst);

        UFUNCTION(BlueprintCallable, Category = "Custom")
            static void CalculateNeighbour(TArray<FisNeighbour>& isCalculateNeighbourArray);
};
Blueprint

the link : https://blueprintue.com/blueprint/2-6wgxij/

blueprint

And the result

result result

voronoidiagram's People

Contributors

vlad221 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.