You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
129 lines
4.1 KiB
129 lines
4.1 KiB
using System.Security.Cryptography; |
|
using System.Text; |
|
using SharpDX; |
|
using SparkClient.Model.Enums; |
|
|
|
namespace SparkClient.Model.Entity; |
|
/// <summary> |
|
/// 三角形 |
|
/// </summary> |
|
public class Viewport3DTriangleEntity |
|
{ |
|
/// <summary> |
|
/// 点1 |
|
/// </summary> |
|
public Vector3 Point1 { get; set; } |
|
/// <summary> |
|
/// 点2 |
|
/// </summary> |
|
public Vector3 Point2 { get; set; } |
|
/// <summary> |
|
/// 点3 |
|
/// </summary> |
|
public Vector3 Point3 { get; set; } |
|
/// <summary> |
|
/// 三角形代码[生成] |
|
/// 按顺序:p1.x,p1.y,p1.z;p2.x,p2.y,p2.z;p3.x,p3.y,p3.z 拼接后使用生成大写16位md5 |
|
/// </summary> |
|
public String TriangleCode { get; set; } |
|
/// <summary> |
|
/// 面代码 |
|
/// 由多个三角形组成的面的代码:entity1、entity2组成了一个正方形,那么他俩的TriangleCode属性值一致 |
|
/// </summary> |
|
public String PlaneCode { get; set; } |
|
/// <summary> |
|
/// 面类型 |
|
/// 比如这个面时钻石的腰部分,那么type统一,当面类型为err时则该面是一个异常面 |
|
/// 可选值:PlaneType |
|
/// </summary> |
|
public PlaneType PlaneType { get; set; } |
|
|
|
public static Viewport3DTriangleEntity CreateByString(string str) |
|
{ |
|
if (string.IsNullOrWhiteSpace(str)) |
|
{ |
|
throw new ArgumentException("Input string cannot be null or empty"); |
|
} |
|
|
|
// 分割为四段 |
|
var parts = str.Split('|'); |
|
if (parts.Length != 4) |
|
{ |
|
throw new ArgumentException("Input string format is incorrect. Expected 4 parts separated by '|'."); |
|
} |
|
|
|
// 解析第一段:坐标 |
|
var coordinates = parts[0].Split(';'); |
|
if (coordinates.Length != 3) |
|
{ |
|
throw new ArgumentException("Coordinates part must contain exactly 3 points separated by ';'."); |
|
} |
|
|
|
var points = coordinates.Select(ParseVector3).ToArray(); |
|
|
|
// 解析第二段:TriangleCode [内部标记] |
|
|
|
// 解析第三段:PlaneCode |
|
string planeCode = parts[2]; |
|
if (string.IsNullOrWhiteSpace(planeCode)) |
|
{ |
|
throw new ArgumentException("PlaneCode cannot be null or empty"); |
|
} |
|
|
|
// 解析第四段:PlaneType |
|
if (!int.TryParse(parts[3], out int planeTypeValue) || !Enum.IsDefined(typeof(PlaneType), planeTypeValue)) |
|
{ |
|
throw new ArgumentException("Invalid PlaneType value"); |
|
} |
|
PlaneType planeType = (PlaneType)planeTypeValue; |
|
|
|
// 生成 TriangleCode 格式:p1.x,p1.y,p1.z;p2.x,p2.y,p2.z;p3.x,p3.y,p3.z 生成 MD5 |
|
string concatenatedPoints = string.Join(";", points.Select(p => $"{p.X},{p.Y},{p.Z}")); |
|
string generatedTriangleCode = GenerateMD5Hash(concatenatedPoints); |
|
|
|
return new Viewport3DTriangleEntity |
|
{ |
|
Point1 = points[0], |
|
Point2 = points[1], |
|
Point3 = points[2], |
|
TriangleCode = generatedTriangleCode, |
|
PlaneCode = planeCode, |
|
PlaneType = planeType |
|
}; |
|
} |
|
|
|
// public static Viewport3DTriangleEntity CreateByJsonStr(string str) |
|
// { |
|
// |
|
// } |
|
|
|
private static Vector3 ParseVector3(string coordinate) |
|
{ |
|
var values = coordinate.Split(','); |
|
if (values.Length != 3) |
|
{ |
|
throw new ArgumentException("Each coordinate must contain exactly 3 values separated by ','"); |
|
} |
|
|
|
if (!float.TryParse(values[0], out float x) || |
|
!float.TryParse(values[1], out float y) || |
|
!float.TryParse(values[2], out float z)) |
|
{ |
|
throw new ArgumentException("Coordinate values must be valid floating-point numbers"); |
|
} |
|
|
|
return new Vector3(x, y, z); |
|
} |
|
|
|
public static string GenerateMD5Hash(string input) |
|
{ |
|
using (var md5 = MD5.Create()) |
|
{ |
|
var inputBytes = Encoding.UTF8.GetBytes(input); |
|
var hashBytes = md5.ComputeHash(inputBytes); |
|
|
|
// 转换为大写的16位十六进制字符串 |
|
return string.Concat(hashBytes.Take(8).Select(b => b.ToString("X2"))); |
|
} |
|
} |
|
} |