refact: 使用int重构了PoseLandmarkType

This commit is contained in:
jackfiled 2023-02-28 23:35:49 +08:00
parent 87831fd869
commit aaba183c48
5 changed files with 97 additions and 68 deletions

View File

@ -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<Bond> _bonds = new List<Bond>();
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<PoseLandmark> 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()
/// </summary>
/// <param name="nodes">需要连接起来的关键点 需要按顺序设置</param>
/// <returns></returns>
private List<Bond> GenerateBondsList(PoseLandmarkType[] nodes)
private List<Bond> GenerateBondsList(int[] nodes)
{
var bonds = new List<Bond>();
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));
}

View File

@ -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<PoseLandmark> 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<PoseLandmark> landmarks)
/// </summary>
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()
/// </summary>
/// <param name="target">需要添加的节点</param>
/// <param name="parent">添加的父节点</param>
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);
}
}

View File

@ -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<PoseLandmark> 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

View File

@ -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();
}
}
}

View File

@ -49,12 +49,12 @@ public struct PoseTransform
private readonly Queue<PoseLandmark> _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<PoseLandmarkType>();
PreviousQuaternion = new Quaternion();
@ -108,7 +108,7 @@ public static void CalculateRotation(ref PoseTransform poseTransform, PoseLandma
/// <returns>关联骨骼的种类</returns>
private static HumanBodyBones GetRelatedBone(PoseLandmarkType type)
{
switch (type)
switch (type.Value)
{
case PoseLandmarkType.LeftHip:
return HumanBodyBones.LeftUpperLeg;