1

所以我将一些数据放入字典中。一个字符串(名称)和一个骨骼(用于保存位置和东西)。我正在从 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
        }

骨骼类的主要内容:

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 

...并且持续了一段时间

4

2 回答 2

1

您只temp_f在 the 之前做了一次foreach,但您总是将相同的列表添加到字典中?

此外,您BVHBuilder.add_frame在没有add_frame声明时使用...我想您的意思是Frame或省略了代码。

于 2012-12-14T01:41:26.240 回答
0

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

于 2012-12-14T02:38:50.743 回答