ComputerGraphics/20210129/zzx-20210129作业/Data/MeshData.cs
2021-02-25 15:53:31 +08:00

112 lines
3.4 KiB
C#

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
/// <summary>
/// 顶点集合
/// </summary>
public List<XYZ> VerticeList { get; set; }
/// <summary>
/// 面索引集合
/// </summary>
public List<int> IndiceList { get; set; }
/// <summary>
/// 顶点法向量集合
/// </summary>
public List<XYZ> VerticeNormalList { get; set; }
/// <summary>
/// 对应材质数组编号
/// </summary>
public int MaterialIndex { get; set; }
#endregion
#region
public MeshData()
{
VerticeList = new List<XYZ>();
IndiceList = new List<int>();
VerticeNormalList = new List<XYZ>();
}
#endregion
#region
/// <summary>
/// 把顶点信息添加到集合中,然后返回对应的点在顶点集合中的索引
/// </summary>
/// <param name="meshVerticeList"></param>
/// <returns></returns>
public List<int> AddVertice(IList<XYZ> meshVerticeList, XYZ faceNormal)
{
List<int> verticelIndexList = new List<int>();
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<XYZ, string>(x => XYZTransform(x,false))),
string.Join(",\n", IndiceList.Select<int, string>(x => x.ToString())),
string.Join(",\n", VerticeNormalList.Select<XYZ, string>(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
}
}