using Jobberwocky.GeometryAlgorithms.Source.Core;
using Jobberwocky.GeometryAlgorithms.Source.Parameters;
using UnityEngine;
namespace Jobberwocky.GeometryAlgorithms.Source.Algorithms.Hull2D
{
public class Hull2DWrapper
{
public Hull2DWrapper()
{
}
///
/// Create a hull from a set of points based on the concavity defined in the parameters object
///
///
///
public Geometry Hull2D(Hull2DParameters parameters)
{
return Hull2DBase(parameters);
}
///
/// Create a hull from a set of points based on the concavity defined in the parameters object
///
///
///
private Geometry Hull2DBase(Hull2DParameters parameters)
{
Geometry geometry = new Geometry();
if (parameters == null)
{
parameters = new Hull2DParameters();
}
var points = parameters.Points;
if (points != null && points.Length > 2)
{
var hull2DAlgorithm = new Hull2DAlgorithm();
var hull = hull2DAlgorithm.GenerateHull(VectorToVertex(points, parameters.CoordinateSystem), parameters.Concavity);
for (int i = 0; i < hull.Length - 1; i++) {
hull[i].Position = Utils.FromCoordinateSystemDefaultTo(hull[i].Position, parameters.CoordinateSystem);
}
geometry.Vertices = hull;
geometry.Indices = new int[(hull.Length - 1) * 2];
geometry.Topology = MeshTopology.Lines;
for (int i = 0; i < hull.Length - 1; i++)
{
geometry.Indices[i * 2 + 0] = i;
geometry.Indices[i * 2 + 1] = i + 1 % (hull.Length - 1);
}
}
return geometry;
}
///
/// Transforms a Vector3 array to and Vertex array
///
///
///
private Vertex[] VectorToVertex(Vector3[] vectors, CoordinateSystem coordinateSystem)
{
Vertex[] vertices = new Vertex[vectors.Length];
for (int i = 0; i < vectors.Length; i++)
{
var vector = Utils.ToCoordinateSystemDefault(vectors[i], coordinateSystem);
vertices[i] = new Vertex(vector.x, vector.y, vector.z, i);
}
return vertices;
}
}
}