0

我目前正在使用 C++ 开发一个 kinect 项目。我在 Ubuntu 12.04(32 位)下工作,我使用 OpenNI/NITE。我想找到一只手并将其坐标保存在列表中,对于这一部分,我有以下代码:

标题:

#ifndef HAND_H_
#define HAND_H_
#include <list>
#include<iostream>
#include <stdio.h>

class Hand
{
public:
    std::list<double> Xpoints;
    std::list<double> Ypoints;
    std::list<double> Zpoints;
    Hand(int id);
    int handID;
    void AddPoint(double x, double y, double z);
    void ClearList(void);
    void ReleaseHand(void);
    int GetID(void);
    int Size();
};
#endif

共产党:

#include "Hand.h"

Hand::Hand(int id)
{
    handID = id;
}


void Hand::AddPoint(double x, double y, double z)
{
    ("Hand ID: %d: (%f,%f,%f)\n", handID, x, y, z);
    if(handID > 0)
    {
        Xpoints.push_back(x);
        Ypoints.push_back(y);
        Zpoints.push_back(z);
        ("Added to Hand ID: %d: (%f,%f,%f)\n", handID, x, y, z);
    }
    else
    {
        std::cout << "Hand does not exist anymore - Inconsistency!" << std::endl;
    }
}


void Hand::ClearList(void)
{
    Xpoints.clear();
    Ypoints.clear();
    Zpoints.clear();
}

void Hand::ReleaseHand(void)
{
    handID = -1; // if (ID==-1) no valid hand
    Xpoints.clear();
    Ypoints.clear();
    Zpoints.clear();
}

int Hand::GetID(void)

{
    return handID;
}

int Hand::Size()
{
    return Xpoints.size();
}

这里AddPoints()被称为:

Hand hand(cxt->nID);
hand.AddPoint(cxt->ptPosition.X, cxt->ptPosition.Y, cxt->ptPosition.Z);
handlist.Add(&hand);

它工作正常。后来,当我从我的 kinect 获得手的新坐标时,我称之为:

Hand tmp = handlist.Get(cxt->nID);
tmp.AddPoint(cxt->ptPosition.X, cxt->ptPosition.Y, cxt->ptPosition.Z);
std::cout << "Hand size " << tmp.Size() << std::endl;

在这里,当这是第一次调用时,第二组坐标被添加到我手中的列表中。但在那之后,列表不能大于大小 2。这意味着不是插入,而是替换最后一个点。(我已经把它们打印出来了,所以我可以看到坐标)我也尝试过push_front替换前面的坐标,以及向量而不是列表,这对于push_backand有同样的问题insert。我还在 Windows 中使用 VS2010 尝试了相同的代码,它运行良好。我完全不知道我做错了什么。

因此,如果有人可以帮助我,那就太好了。:)

4

1 回答 1

2

I see a couple problems.

One you're adding the pointer to a stack variable to the handlist. As soon as the hand goes out of scope, you have a dangling pointer.

Second, your handlist.Get is returning (presumably) by copy and thus anything you add to it isn't reflected in the version that handlist occupies. ie: You could call handlist.Get(), add 500 points, then call handlist.Get again and it would be the original version.

Your handlist.Get() function is also not returning a pointer, but you're passing handlist.Add() a pointer so your interface isn't clear.

于 2013-01-28T16:58:20.363 回答