using System.Globalization; using System.Security.Cryptography; using System.Text; using System.Windows.Input; 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 bool IsTextNumeric(string text) { return int.TryParse(text, out _); // 只允许数字输入 } public static bool IsKeyNumeric(Key key) { // 判断按键是否是数字键 return (key >= Key.D0 && key <= Key.D9) || (key >= Key.NumPad0 && key <= Key.NumPad9); } 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"))); } } public static string Color4ToHex(Color4 color) { int a = (int)(color.Alpha * 255); int r = (int)(color.Red * 255); int g = (int)(color.Green * 255); int b = (int)(color.Blue * 255); // 返回 Hex 字符串 return $"#{a:X2}{r:X2}{g:X2}{b:X2}"; } public static Color4 HexToColor4(string hex) { // 移除可能的前导 '#' hex = hex.TrimStart('#'); // 根据长度判断是 ARGB 还是 RGB if (hex.Length == 8) { // ARGB 格式:AARRGGBB return ConvertArgb(hex); } else if (hex.Length == 6) { // RGB 格式:RRGGBB,默认 Alpha 为 255 return ConvertRgb(hex); } else { throw new ArgumentException("Hex string must be in the format #AARRGGBB or #RRGGBB."); } } private static Color4 ConvertArgb(string hex) { // 解析 ARGB 组件 byte a = byte.Parse(hex.Substring(0, 2), NumberStyles.HexNumber); byte r = byte.Parse(hex.Substring(2, 2), NumberStyles.HexNumber); byte g = byte.Parse(hex.Substring(4, 2), NumberStyles.HexNumber); byte b = byte.Parse(hex.Substring(6, 2), NumberStyles.HexNumber); // 创建 Color4 对象,RGBA 的范围是 0 到 1,因此除以 255 return new Color4(r / 255f, g / 255f, b / 255f, a / 255f); } private static Color4 ConvertRgb(string hex) { // 解析 RGB 组件,Alpha 默认 255 byte r = byte.Parse(hex.Substring(0, 2), NumberStyles.HexNumber); byte g = byte.Parse(hex.Substring(2, 2), NumberStyles.HexNumber); byte b = byte.Parse(hex.Substring(4, 2), NumberStyles.HexNumber); byte a = 255; // 默认 Alpha 为 255 // 创建 Color4 对象,RGBA 的范围是 0 到 1,因此除以 255 return new Color4(r / 255f, g / 255f, b / 255f, a / 255f); } }