From aaba183c48c26747ef38d6e7a198bd5bd4ce87be Mon Sep 17 00:00:00 2001 From: jackfiled Date: Tue, 28 Feb 2023 23:35:49 +0800 Subject: [PATCH] =?UTF-8?q?refact:=20=E4=BD=BF=E7=94=A8int=E9=87=8D?= =?UTF-8?q?=E6=9E=84=E4=BA=86PoseLandmarkType?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/MainBehaviour.cs | 24 +++---- Assets/ModelBehaviour.cs | 18 +++--- Assets/Models/PoseLandmark.cs | 12 ++-- Assets/Models/PoseLandmarkType.cs | 103 +++++++++++++++++++----------- Assets/Models/PoseTransform.cs | 8 +-- 5 files changed, 97 insertions(+), 68 deletions(-) diff --git a/Assets/MainBehaviour.cs b/Assets/MainBehaviour.cs index 00ff8bb..b4499c5 100644 --- a/Assets/MainBehaviour.cs +++ b/Assets/MainBehaviour.cs @@ -4,9 +4,9 @@ public class MainBehaviour : MonoBehaviour { - private readonly GameObject[] _nodes = new GameObject[(int)PoseLandmarkType.MaxValue]; + private readonly GameObject[] _nodes = new GameObject[PoseLandmarkType.MaxValue]; private readonly List _bonds = new List(); - private readonly PoseTransform[] _poseTransforms = new PoseTransform[(int)PoseLandmarkType.MaxValue]; + private readonly PoseTransform[] _poseTransforms = new PoseTransform[PoseLandmarkType.MaxValue]; private readonly UdpListener _listener = new UdpListener(); private const float Scale = 0.2f; @@ -25,7 +25,7 @@ private void Start() // Update is called once per frame private void Update() { - for (var i = 0; i < (int)PoseLandmarkType.MaxValue; i++) + for (var i = 0; i < PoseLandmarkType.MaxValue; i++) { _nodes[i].transform.position = _poseTransforms[i].AveragePos * -5; } @@ -45,7 +45,7 @@ private void OnReceive(List landmarks) { foreach (var landmark in landmarks) { - PoseTransform.UpdatePosition(ref _poseTransforms[(int)landmark.Type], landmark); + PoseTransform.UpdatePosition(ref _poseTransforms[landmark.Type.Value], landmark); } } @@ -71,7 +71,7 @@ private void CreateNodes() } _nodes[i] = ball; - _poseTransforms[i] = new PoseTransform((PoseLandmarkType)i); + _poseTransforms[i] = new PoseTransform(i); } } @@ -148,12 +148,12 @@ private void CreateBonds() // 最后手动添加身体上的两条横线 _bonds.Add(new Bond( - _nodes[(int)PoseLandmarkType.LeftShoulder], - _nodes[(int)PoseLandmarkType.RightShoulder], + _nodes[PoseLandmarkType.LeftShoulder], + _nodes[PoseLandmarkType.RightShoulder], Scale)); _bonds.Add(new Bond( - _nodes[(int)PoseLandmarkType.LeftHip], - _nodes[(int)PoseLandmarkType.RightHip], + _nodes[PoseLandmarkType.LeftHip], + _nodes[PoseLandmarkType.RightHip], Scale)); } @@ -162,15 +162,15 @@ private void CreateBonds() /// /// 需要连接起来的关键点 需要按顺序设置 /// - private List GenerateBondsList(PoseLandmarkType[] nodes) + private List GenerateBondsList(int[] nodes) { var bonds = new List(); for (var i = 0; i < nodes.Length - 1; i++) { bonds.Add(new Bond( - _nodes[(int)nodes[i]], - _nodes[(int)nodes[i + 1]], + _nodes[nodes[i]], + _nodes[nodes[i + 1]], Scale)); } diff --git a/Assets/ModelBehaviour.cs b/Assets/ModelBehaviour.cs index 56a81dd..826aff0 100644 --- a/Assets/ModelBehaviour.cs +++ b/Assets/ModelBehaviour.cs @@ -7,7 +7,7 @@ public class ModelBehaviour : MonoBehaviour private Animator _animator; private readonly UdpListener _listener = new UdpListener(); - private static readonly PoseTransform[] PoseTransforms = new PoseTransform[(int)PoseLandmarkType.MaxValue]; + private static readonly PoseTransform[] PoseTransforms = new PoseTransform[PoseLandmarkType.MaxValue]; private void Start() { @@ -34,7 +34,7 @@ private void Update() foreach (var parent in landmark.Parent) { - parentsum += PoseTransforms[(int)parent].AveragePos; + parentsum += PoseTransforms[parent.Value].AveragePos; } parentsum /= landmark.Parent.Count; @@ -69,9 +69,9 @@ private static void RigPoint(List landmarks) foreach (var landmark in landmarks) { - PoseTransform.UpdatePosition(ref PoseTransforms[(int)landmark.Type], landmark); + PoseTransform.UpdatePosition(ref PoseTransforms[landmark.Type.Value], landmark); - Debug.Log(PoseTransforms[(int)landmark.Type].UnityName+":"+PoseTransforms[(int)landmark.Type].AveragePos); + Debug.Log(PoseTransforms[landmark.Type.Value].UnityName+":"+PoseTransforms[landmark.Type.Value].AveragePos); } } @@ -81,9 +81,9 @@ private static void RigPoint(List landmarks) /// private void InitPoseTransformList() { - for (var type = 0; type <= (int)PoseLandmarkType.RightFootIndex; type++) + for (var type = 0; type <= PoseLandmarkType.RightFootIndex; type++) { - var item = new PoseTransform((PoseLandmarkType)type); + var item = new PoseTransform(type); if (item.UnityName != HumanBodyBones.LastBone) { @@ -125,10 +125,10 @@ private void InitPoseTransformList() /// /// 需要添加的节点 /// 添加的父节点 - private void AddParentTransform(PoseLandmarkType target, PoseLandmarkType parent) + private void AddParentTransform(int target, int parent) { - var parents = PoseTransforms[(int)target].Parent; - parents.Add(PoseTransforms[(int)parent].MediaPipeName); + var parents = PoseTransforms[target].Parent; + parents.Add(PoseTransforms[parent].MediaPipeName); } } diff --git a/Assets/Models/PoseLandmark.cs b/Assets/Models/PoseLandmark.cs index 6520ef2..45608f0 100644 --- a/Assets/Models/PoseLandmark.cs +++ b/Assets/Models/PoseLandmark.cs @@ -47,9 +47,9 @@ public class PoseLandmark public long TimeStamp { get; } - public PoseLandmark(PoseLandmarkType type, float x, float y, float z, float visibility, long timeStamp) + public PoseLandmark(int type, float x, float y, float z, float visibility, long timeStamp) { - Type = type; + Type = new PoseLandmarkType(type); X = x; Y = y; Z = z; @@ -66,7 +66,7 @@ public byte[] ToByteArray() { var result = new byte[PacketLength]; - BitConverter.GetBytes((int)Type).CopyTo(result, 0); + BitConverter.GetBytes(Type.Value).CopyTo(result, 0); BitConverter.GetBytes(X).CopyTo(result, 4); BitConverter.GetBytes(Y).CopyTo(result, 8); BitConverter.GetBytes(Z).CopyTo(result, 12); @@ -84,7 +84,7 @@ public byte[] ToByteArray() public static PoseLandmark ValueOf(byte[] bytes) { var result = new PoseLandmark( - (PoseLandmarkType)BitConverter.ToInt32(bytes, 0), + BitConverter.ToInt32(bytes, 0), BitConverter.ToSingle(bytes, 4), BitConverter.ToSingle(bytes, 8), BitConverter.ToSingle(bytes, 12), @@ -106,7 +106,7 @@ public static List ArrayOf(byte[] bytes) for (var i = 0; i < bytes.Length; i = i + PacketLength) { - var landmark = new PoseLandmark((PoseLandmarkType)BitConverter.ToInt32(bytes, i), + var landmark = new PoseLandmark(BitConverter.ToInt32(bytes, i), BitConverter.ToSingle(bytes, i + 4), BitConverter.ToSingle(bytes, i + 8), BitConverter.ToSingle(bytes, i + 12), @@ -127,7 +127,7 @@ public override bool Equals(object obj) } else { - return Type == landmark.Type + return Type.Value == landmark.Type.Value && Math.Abs(X - landmark.X) < Tolerance && Math.Abs(Y - landmark.Y) < Tolerance && Math.Abs(Z - landmark.Z) < Tolerance diff --git a/Assets/Models/PoseLandmarkType.cs b/Assets/Models/PoseLandmarkType.cs index 7b86f39..a781564 100644 --- a/Assets/Models/PoseLandmarkType.cs +++ b/Assets/Models/PoseLandmarkType.cs @@ -1,41 +1,70 @@ +using System; + namespace Models { //名字 - public enum PoseLandmarkType - { - Nose, - LeftEyeInner, - LeftEye, - LeftEyeOuter, - RightEyeInner, - RightEye, - RightEyeOuter, - LeftEar, - RightEar, - MouthLeft, - MouthRight, - LeftShoulder, - RightShoulder, - LeftElbow, - RightElbow, - LeftWrist, - RightWrist, - LeftPinky, - RightPinky, - LeftIndex, - RightIndex, - LeftThumb, - RightThumb, - LeftHip, - RightHip, - LeftKnee, - RightKnee, - LeftAnkle, - RightAnkle, - LeftHeel, - RightHeel, - LeftFootIndex, - RightFootIndex, - MaxValue - } + public struct PoseLandmarkType + { + public const int Nose = 0; + public const int LeftEyeInner = 1; + public const int LeftEye = 2; + public const int LeftEyeOuter = 3; + public const int RightEyeInner = 4; + public const int RightEye = 5; + public const int RightEyeOuter = 6; + public const int LeftEar = 7; + public const int RightEar = 8; + public const int MouthLeft = 9; + public const int MouthRight = 10; + public const int LeftShoulder = 11; + public const int RightShoulder = 12; + public const int LeftElbow = 13; + public const int RightElbow = 14; + public const int LeftWrist = 15; + public const int RightWrist = 16; + public const int LeftPinky = 17; + public const int RightPinky = 18; + public const int LeftIndex = 19; + public const int RightIndex = 20; + public const int LeftThumb = 21; + public const int RightThumb = 22; + public const int LeftHip = 23; + public const int RightHip = 24; + public const int LeftKnee = 25; + public const int RightKnee = 26; + public const int LeftAnkle = 27; + public const int RightAnkle = 28; + public const int LeftHeel = 29; + public const int RightHeel = 30; + public const int LeftFootIndex = 31; + public const int RightFootIndex = 32; + public const int MaxValue = 33; + + public int Value { get; } + + public PoseLandmarkType(int value) + { + if (value >= MaxValue) + { + throw new ArgumentOutOfRangeException(nameof(value)); + } + + Value = value; + } + + public override bool Equals(object obj) + { + return Value.Equals(obj); + } + + public override int GetHashCode() + { + return Value.GetHashCode(); + } + + public override string ToString() + { + return Value.ToString(); + } + } } \ No newline at end of file diff --git a/Assets/Models/PoseTransform.cs b/Assets/Models/PoseTransform.cs index fbe50e1..293a22c 100644 --- a/Assets/Models/PoseTransform.cs +++ b/Assets/Models/PoseTransform.cs @@ -49,12 +49,12 @@ public struct PoseTransform private readonly Queue _landmarkQueue; public PoseTransform( - PoseLandmarkType type, + int type, int averageLength = 5 ) { - MediaPipeName = type; - UnityName = GetRelatedBone(type); + MediaPipeName = new PoseLandmarkType(type); + UnityName = GetRelatedBone(MediaPipeName); AveragePos = new Vector3(); Parent = new List(); PreviousQuaternion = new Quaternion(); @@ -108,7 +108,7 @@ public static void CalculateRotation(ref PoseTransform poseTransform, PoseLandma /// 关联骨骼的种类 private static HumanBodyBones GetRelatedBone(PoseLandmarkType type) { - switch (type) + switch (type.Value) { case PoseLandmarkType.LeftHip: return HumanBodyBones.LeftUpperLeg;