using Objects.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Mesh = Objects.Geometry.Mesh;
namespace Objects.Converter.Unity
{
public partial class ConverterUnity
{
///
///
///
///
///
///
///
public Vector3 PointByCoordinates(double x, double y, double z)
{
// switch y and z
return new Vector3((float)x, (float)z, (float)y);
}
///
///
///
///
///
public Vector3 ArrayToPoint(double[] ptValues)
{
double x = ptValues[0];
double y = ptValues[1];
double z = ptValues[2];
// switch y and z
return new Vector3((float)x, (float)z, (float)y);
}
///
///
///
///
///
public Vector3[] ArrayToPoints(IEnumerable arr)
{
if (arr.Count() % 3 != 0) throw new Exception("Array malformed: length%3 != 0.");
Vector3[] points = new Vector3[arr.Count() / 3];
var asArray = arr.ToArray();
for (int i = 2, k = 0; i < arr.Count(); i += 3)
points[k++] = PointByCoordinates(asArray[i - 2], asArray[i - 1], asArray[i]);
return points;
}
///
///
///
///
///
public Point PointToSpeckle(UnityPoint obj)
{
Vector3 p = obj.point;
//switch y and z
return new Point(p.x, p.z, p.y);
}
///
///
///
///
///
public UnityPoint PointToNative(Point point)
{
Vector3 newPt = ArrayToPoint(point.value.ToArray());
return new UnityPoint(point.speckle_type, newPt);
}
///
///
///
///
///
public UnityPolyline LineToNative(Line line)
{
Vector3[] points = ArrayToPoints(line.value);
if (points.Length == 0) return null;
return new UnityPolyline(line.speckle_type, points);
}
///
///
///
///
///
public UnityPolyline PolylineToNative(Polyline polyline)
{
Vector3[] points = ArrayToPoints(polyline.value);
if (points.Length == 0) return null;
return new UnityPolyline(polyline.speckle_type, points);
}
///
///
///
///
///
public UnityPolyline CurveToNative(Curve curve)
{
Vector3[] points = ArrayToPoints(curve.displayValue.value);
if (points.Length == 0) return null;
return new UnityPolyline(curve.speckle_type, points);
}
///
///
///
///
///
public UnityMesh MeshToNative(Mesh speckleMesh)
{
if (speckleMesh.vertices.Count == 0 || speckleMesh.faces.Count == 0)
{
return null;
}
//convert speckleMesh.faces into triangle array
List tris = new List();
int i = 0;
while (i < speckleMesh.faces.Count)
{
if (speckleMesh.faces[i] == 0)
{
//Triangles
tris.Add(speckleMesh.faces[i + 1]);
tris.Add(speckleMesh.faces[i + 3]);
tris.Add(speckleMesh.faces[i + 2]);
i += 4;
}
else
{
//Quads to triangles
tris.Add(speckleMesh.faces[i + 1]);
tris.Add(speckleMesh.faces[i + 3]);
tris.Add(speckleMesh.faces[i + 2]);
tris.Add(speckleMesh.faces[i + 3]);
tris.Add(speckleMesh.faces[i + 1]);
tris.Add(speckleMesh.faces[i + 4]);
i += 5;
}
}
return new UnityMesh(speckleMesh.speckle_type, ArrayToPoints(speckleMesh.vertices), tris.ToArray());
}
///
///
///
///
///
//public static UnityMesh ToNative(this Brep brep)
//{
// Mesh speckleMesh = brep.displayValue;
// return MeshToNative(speckleMesh);
//}
}
}