using Autodesk.Revit.DB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GraphicsStudy
{
class MeshData
{
#region 数据
///
/// 顶点集合
///
public List VerticeList { get; set; }
///
/// 面索引集合
///
public List IndiceList { get; set; }
///
/// 顶点法向量集合
///
public List VerticeNormalList { get; set; }
///
/// 对应材质数组编号
///
public int MaterialIndex { get; set; }
#endregion
#region 构造函数
public MeshData()
{
VerticeList = new List();
IndiceList = new List();
VerticeNormalList = new List();
}
#endregion
#region 方法
///
/// 把顶点信息添加到集合中,然后返回对应的点在顶点集合中的索引
///
///
///
public List AddVertice(IList meshVerticeList, XYZ faceNormal)
{
List verticelIndexList = new List();
foreach (XYZ meshVertice in meshVerticeList)
{
int index = VerticeList.FindIndex(x => x.DistanceTo(meshVertice) < 1.ToFeet());
if (index == -1)
{
VerticeList.Add(meshVertice);
VerticeNormalList.Add(faceNormal);
verticelIndexList.Add(VerticeList.Count() - 1);
}
else
{
verticelIndexList.Add(index);
//法向量相加再单位化
if (VerticeNormalList[index].AngleTo(faceNormal) < 91.ToRad())
{
VerticeNormalList[index] = (faceNormal + VerticeNormalList[index]).Normalize();
}
}
}
return verticelIndexList;
}
public string ToJosn()
{
string result = null;
result = string.Format
(
"\n \"vertices\":[\n{0}\n],"
+ "\n \"indices\":[\n{1}\n],"
+ "\n \"normals\":[\n{2}\n],"
+ "\n \"material\":{3}",
string.Join(",\n", VerticeList.Select(x => XYZTransform(x,false))),
string.Join(",\n", IndiceList.Select(x => x.ToString())),
string.Join(",\n", VerticeNormalList.Select(x => XYZTransform(x))),
MaterialIndex
);
return "\n{" + result + "\n}";
}
private string XYZTransform(XYZ point,bool isDirection=true)
{
string result = null;
if (isDirection)
{
point = point.Normalize();
result = $"{point.X.Round(0)},\n{point.Y.Round(0)},\n{point.Z.Round(0)}";
}
else
{
result = $"{point.X.ToMM().Round(0)},\n{point.Y.ToMM().Round(0)},\n{point.Z.ToMM().Round(0)}";
}
return result;
}
#endregion
}
}