using System.Security.Cryptography; using System.Text; using Newtonsoft.Json; using SharpDX; using SparkClient.Views.UserControl.ViewportData.Entity; using SparkClient.Views.UserControl.ViewportData.Enum; namespace SparkClient.Views.UserControl.ViewportData.Helper; public class CommonHelper { public static Viewport3DTriangleEntity CreateByJsonStr(string json) { // 解析 JSON 数据 var jsonObject = JsonConvert.DeserializeObject(json); if (jsonObject == null) throw new ArgumentException("Json object is null"); // 提取坐标 var coords = jsonObject.coords.ToObject(); if (coords.Length < 3) { throw new ArgumentException("The input JSON does not have enough points to form a triangle."); } // 创建三角形顶点 var point1 = new Vector3((float)coords[0].x, (float)coords[0].z, (float)coords[0].y); var point2 = new Vector3((float)coords[1].x, (float)coords[1].z, (float)coords[1].y); var point3 = new Vector3((float)coords[2].x, (float)coords[2].z, (float)coords[2].y); // var point1 = new Vector3((float)coords[0].x, (float)coords[0].y, (float)coords[0].z); // var point2 = new Vector3((float)coords[1].x, (float)coords[1].y, (float)coords[1].z); // var point3 = new Vector3((float)coords[2].x, (float)coords[2].y, (float)coords[2].z); // 提取 PlaneCode 和 PlaneType string planeCode = jsonObject.facet_id ?? (string)jsonObject.facet_id; PlaneType planeType = (PlaneType)(int)jsonObject.facet_type; // 生成 TriangleCode var triangleCode = GenerateTriangleCode(point1, point2, point3); // 创建并返回实体对象 return new Viewport3DTriangleEntity { Point1 = point1, Point2 = point2, Point3 = point3, TriangleCode = triangleCode, PlaneCode = planeCode, PlaneType = planeType }; } /// /// 生成三角形签名 /// /// /// /// /// public static string GenerateTriangleCode(Vector3 p1, Vector3 p2, Vector3 p3) { var concatenated = $"{p1.X},{p1.Y},{p1.Z};{p2.X},{p2.Y},{p2.Z};{p3.X},{p3.Y},{p3.Z}"; return GenerateMd5Hash(concatenated); } /// /// 文本转32大写MD5 /// /// /// private static string GenerateMd5Hash(string input) { using (var md5 = MD5.Create()) { var inputBytes = Encoding.UTF8.GetBytes(input); var hashBytes = md5.ComputeHash(inputBytes); return string.Concat(hashBytes.Select(b => b.ToString("X2"))); } } }