Pregunta

I've been working on these classes for some time now:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Kinect;
using System.Threading.Tasks;
using System.Windows.Media.Media3D;

namespace KinectFysioterapi
{
    public class BoneVector
    {
        public Vector3D vector;
        private double x;
        private double y;
        private double z;

        #region Constructor
        public BoneVector(Joint startJoint, Joint endJoint)
        {
            if (!(startJoint.Equals(endJoint)))
            {
                x = endJoint.Position.X - startJoint.Position.X;
                y = endJoint.Position.Y - startJoint.Position.Y;
                z = endJoint.Position.Z - startJoint.Position.Z;
                vector = new Vector3D(x, y, z);
            }
        }
        #endregion

    }
    public class SkeletonVectorCollection : Skeleton
    {
        public SkeletonVectorCollection(Skeleton input)
        {

            foreach (BoneOrientation orientation in input.BoneOrientations)
            {
                this[orientation.EndJoint] = new BoneVector(input.Joints[orientation.StartJoint], input.Joints[orientation.EndJoint]);   
            }
        }


        //Not sure how to do this correctly
        public BoneVector this[JointType jointType] 
        {
            get
            {
                return this[jointType];
            }
            protected set
            {
                this[jointType] = value;
            }
        }
    }
}

I having huge problems getting the last part running without problems.

What i'm looking for is to input a kinect skeleton and get out a new skeleton with additional information about some defined vectors between the joints.

My goal is to be able to do the following:

SkeletonVectorCollection collection = new SkeletonVectorCollection(skeleton);
skeleton[Jointtype.Head].vector.x.ToString();

Im very unsure how to use this[JointType jointType] correctly.

¿Fue útil?

Solución

As defined your SkeletonVectorCollection is recursive as your this[JointType] implementation has no backing property and is trying to store to itself. If you don't need/want to inherit from Skeleton you can switch to a Dictionary and get the implementation for free.

public class SkeletonVectorCollection : Dictionary<JointType, BoneVector>
{
    public SkeletonVectorCollection(Skeleton input)
    {
        foreach (BoneOrientation orientation in input.BoneOrientations)
        {
            this[orientation.EndJoint] = new BoneVector(input.Joints[orientation.StartJoint], input.Joints[orientation.EndJoint]);   
        }
    }
}

If you must inherit from Skeleton then you need to provide an implementation with some sort of storage behind it here is a version that uses a dictionary internally.

public class SkeletonVectorCollection : Skeleton
{
    private Dictionary<JointType, BoneVector> _boneVectors = new Dictionary<JointType, BoneVector>();

    public SkeletonVectorCollection(Skeleton input)
    {
        foreach (BoneOrientation orientation in input.BoneOrientations)
        {
            this[orientation.EndJoint] = new BoneVector(input.Joints[orientation.StartJoint], input.Joints[orientation.EndJoint]);   
        }
    }

    public BoneVector this[JointType jointType] 
    {
        get
        {
            return _boneVectors[jointType];
        }
        protected set
        {
            _boneVectors[jointType] = value;
        }
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top