字典中的不同输入都是相同的

时间:2012-12-14 01:32:14

标签: c# dictionary kinect

所以我将一些数据放入字典中。字符串(名称)和骨骼(用于保存位置和东西)。我正在从kinect读取骨骼的位置,然后我将它放入字典中,然后我将它们保存到.txt文件中。现在我很困惑,因为他们都是一样的。有一段时间我有一些标签显示了几块骨头的不同位置,它们都在变化。这意味着它与我放入字典有关吗?

这是我的输入法:

        JointCollection joints = skele.Joints;
        List<Bone> temp = current_bones;
        Frame temp_f = new Frame(temp);
        Joint h_j = joints[JointType.Head];
        foreach (Joint j in joints)
        {
            try
            {
                switch (j.JointType)
                {
                    #region cases
                    case (JointType.Head):
                        temp_f.frame_bones["Head"].setPosition(j.Position.X, j.Position.Y, j.Position.Z);
                        break;
...Other stuff that basically repeats but with different jointtypes
                }
            }
            catch (Exception ex) { /*MessageBox.Show("Error getting joints: " + ex.Message);*/ }
        }
        if (recording)
        {
            if (newR)
            {
                bvhBuilder.resetAnimation();    //Resets a list of frames
                newR = false;
            }
            bvhBuilder.add_frame(temp_f);//adds a frame to the list
        }

Bone类中的主要内容:

public Bone(String b_name)
    {
        name = b_name;
    }

    public void setPosition(float _x, float _y, float _z)
    {
        x = _x; y = _y; z = _z; 
    }

我的BVHBuilder课程中的相关内容:

List<Bone> current_bones = new List<Bone>();
    Frame current_frame;
    List<Frame> animation_frames = new List<Frame>();
    public BVHBuilder(List<Bone> bones)
    {
        current_bones = bones;
    }
public String build()
    {
        StringBuilder builder = new StringBuilder();
        builder.AppendLine(build_bones());
        builder.AppendLine(build_anim());
        return builder.ToString();
    }

 public String build_anim()
    {
        StringBuilder sb = new StringBuilder();
        sb.AppendLine("MOTION");
        sb.AppendLine("Frames: " + getFrameCount());
        sb.AppendLine("Frame Time: " + (float)((float)1/(float)30));
        foreach (Frame frame in animation_frames)
        {
            String line = "";
            int b_i = 0;
            float root_x = frame.root.x;
            float root_y = frame.root.y;
            float root_z = frame.root.z;
            Console.WriteLine("Root: (" + root_x + "," + root_y + "," + root_z + ")"); 
            for (int i = 0; i < frame.frame_bones.Count; i++)
            {
                Bone bone = frame.frame_bones.Values.ElementAt(i);
                line += (bone.x - root_x) + " ";
                line += (bone.y - root_y) + " ";
                if (b_i >= frame.frame_bones.Count) { line += (root_z - bone.z); }   //Don't want a space at the end
                else { line += (root_z - bone.z) + " "; }

                b_i++;
            }
            sb.AppendLine(line);
        }
        return sb.ToString();
    }

框架类:

class Frame
{
    public Dictionary<String, Bone> frame_bones = new Dictionary<String, Bone>();
    public Bone root = null;
    int root_pos = 0;

    public Frame(List<Bone> bones)
    {
        int i = 0;
        foreach (Bone b in bones)
        {
            frame_bones.Add(b.name,b);
            if (b.root) { root_pos = i; root = b; }
            i++;
        }
    }
}

这就是它保存的地方:

private void saveAnim()
    {
        int times = 0;
        String path = "";
        Boolean exists = true;
        while (exists)
        {
            path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\" + "nectrecord" + times + ".txt";
            exists = File.Exists(path);
            times++;
        }
        currP = path;
        Console.WriteLine(currP);
        StreamWriter sw = new StreamWriter(currP);
        String write = bvhBuilder.build();
        int max_i = write.Length;
        progressBar1.Maximum = max_i;
        int c_i = 0;
        foreach (char c in write)
        {
            sw.Write(c);
            c_i++;
            progressBar1.Value = c_i;
        }
        sw.Flush();//make sure everything wrote
        sw.Close();
        MessageBox.Show("Saved to :" + currP);
    }

我知道这是很多代码,但我现在已经尝试调试这个代码大概4个小时了。希望一双新鲜的眼睛能帮我找到问题。

顺便说一下,这就是输出文件的样子:

HIERARCHY
ROOT Hip_C
{
OFFSET 0 0 0
CHANNELS 3 Xposition Yposition Zposition
JOINT Spine
{
    OFFSET 0 0 0
    CHANNELS 3 Xposition Yposition Zposition
    JOINT Shoulder_C
    {
        OFFSET 0 0 0
        CHANNELS 3 Xposition Yposition Zposition
        JOINT Head
        {
            OFFSET 0 0 0
            CHANNELS 3 Xposition Yposition Zposition
        }
        JOINT Shoulder_R
        {
            OFFSET 0 0 0
            CHANNELS 3 Xposition Yposition Zposition
            JOINT Elbow_R
            {
                OFFSET 0 0 0
                CHANNELS 3 Xposition Yposition Zposition
                JOINT Wrist_R
                {
                    OFFSET 0 0 0
                    CHANNELS 3 Xposition Yposition Zposition
                    JOINT Hand_R
                    {
                        OFFSET 0 0 0
                        CHANNELS 3 Xposition Yposition Zposition
                    }
                }
            }
        }
        JOINT Shoulder_L
        {
            OFFSET 0 0 0
            CHANNELS 3 Xposition Yposition Zposition
            JOINT Elbow_L
            {
                OFFSET 0 0 0
                CHANNELS 3 Xposition Yposition Zposition
                JOINT Wrist_L
                {
                    OFFSET 0 0 0
                    CHANNELS 3 Xposition Yposition Zposition
                    JOINT Hand_L
                    {
                        OFFSET 0 0 0
                        CHANNELS 3 Xposition Yposition Zposition
                    }
                }
            }
        }
    }
}
JOINT Hip_R
{
    OFFSET 0 0 0
    CHANNELS 3 Xposition Yposition Zposition
    JOINT Knee_R
    {
        OFFSET 0 0 0
        CHANNELS 3 Xposition Yposition Zposition
        JOINT Ankle_R
        {
            OFFSET 0 0 0
            CHANNELS 3 Xposition Yposition Zposition
            JOINT Foot_R
            {
                OFFSET 0 0 0
                CHANNELS 3 Xposition Yposition Zposition
            }
        }
    }
}
JOINT Hip_L
{
    OFFSET 0 0 0
    CHANNELS 3 Xposition Yposition Zposition
    JOINT Knee_L
    {
        OFFSET 0 0 0
        CHANNELS 3 Xposition Yposition Zposition
        JOINT Ankle_L
        {
            OFFSET 0 0 0
            CHANNELS 3 Xposition Yposition Zposition
            JOINT Foot_L
            {
                OFFSET 0 0 0
                CHANNELS 3 Xposition Yposition Zposition
            }
        }
    }
}
}



MOTION
Frames: 210
 Frame Time: 0.03333334
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.05713073 -0.009174455 -0.2937571 0.2087359 -0.2028138 -0.902887 0.2087359 -0.2028138 -0.9028869 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.05713073 -0.009174455 -0.2937571 0.2087359 -0.2028138 -0.902887 0.2087359 -0.2028138 -0.9028869 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.05713073 -0.009174455 -0.2937571 0.2087359 -0.2028138 -0.902887 0.2087359 -0.2028138 -0.9028869 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.05713073 -0.009174455 -0.2937571 0.2087359 -0.2028138 -0.902887 0.2087359 -0.2028138 -0.9028869 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.05713073 -0.009174455 -0.2937571 0.2087359 -0.2028138 -0.902887 0.2087359 -0.2028138 -0.9028869 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.05713073 -0.009174455 -0.2937571 0.2087359 -0.2028138 -0.902887 0.2087359 -0.2028138 -0.9028869 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.05713073 -0.009174455 -0.2937571 0.2087359 -0.2028138 -0.902887 0.2087359 -0.2028138 -0.9028869 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.05713073 -0.009174455 -0.2937571 0.2087359 -0.2028138 -0.902887 0.2087359 -0.2028138 -0.9028869 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.05713073 -0.009174455 -0.2937571 0.2087359 -0.2028138 -0.902887 0.2087359 -0.2028138 -0.9028869 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

......而且这种情况持续了一段时间

2 个答案:

答案 0 :(得分:1)

您只能在temp_f之前拨打foreach一次但是您总是将相同的列表添加到字典中吗?

此外,如果没有宣布BVHBuilder.add_frame,则使用add_frame ...我想您的意思是Frame或省略了代码。

答案 1 :(得分:0)

嗯,它可能与框架类的某些问题有关吗?因为我疯了一个List并将数据直接放入其中它现在工作正常。