C++ &引用;向量迭代器不可递增“;使用OpenGL,LeapMotion应用程序

C++ &引用;向量迭代器不可递增“;使用OpenGL,LeapMotion应用程序,c++,opengl,vector,leap-motion,C++,Opengl,Vector,Leap Motion,我已经成功地编写了代码,通过我的Leap Motion controller(跟踪手指和手的运动的红外扫描设备)测量我的一个手指的(x,y)位置,并使用OpenGL绘制具有类似(x,y)坐标的点。基本上,移动手指,移动屏幕上显示的点 我现在想做的是显示记录的最后100个位置-因此,你看到的不是一个点的移动,而是一个点的蛇形集合,这些点绕着你的指尖移动 我试着用两个向量来做这件事——一个在给定的帧中保持x和y的位置,另一个保持前面提到的一组向量。通过在向量大小超过100后弹出向量中的第一个元素,我

我已经成功地编写了代码,通过我的Leap Motion controller(跟踪手指和手的运动的红外扫描设备)测量我的一个手指的(x,y)位置,并使用OpenGL绘制具有类似(x,y)坐标的点。基本上,移动手指,移动屏幕上显示的点

我现在想做的是显示记录的最后100个位置-因此,你看到的不是一个点的移动,而是一个点的蛇形集合,这些点绕着你的指尖移动

我试着用两个向量来做这件事——一个在给定的帧中保持x和y的位置,另一个保持前面提到的一组向量。通过在向量大小超过100后弹出向量中的第一个元素,我想我可以循环遍历所有这些点,并在显示方法中绘制它们

它编译得很好,但是在画了几个点之后,我得到了一个运行时错误,说“向量迭代器不可递增”。我不知道如何处理这个问题

代码:

#包括
#包括
#包括“Leap.h”
#包括
#包括“GL/glut.h”
#包括
使用名称空间Leap;
使用名称空间std;
布尔一=真;
双x=10,y=100;
双屏宽=480,屏高=480;
双时间,时间显示=5.0*(功率(10.0,9.0));
向量输入,*temp;
向量>集合;
类SampleListener:公共侦听器{
公众:
int firstFrame,第一次;
布尔优先;
虚拟void onInit(const Controller&);
虚拟void onConnect(const Controller&);
虚拟void onExit(const Controller&);
虚拟空帧(常量控制器&);
};
void SampleListener::onInit(常量控制器和控制器){

cout如果这是线程化的,那么
SampleListener::onFrame()
中的
collection.erase()
将使
display()
中的迭代器无效。您需要一个互斥锁。见鬼,
推回()
可能会使您的迭代器无效。似乎您想要一个具有头/尾指针的固定长度结构,而不是此动态向量。

*i++?为什么不使用++i?或i++?我猜这只是代码的最新迭代。在过去尝试过这两种方法,但没有改变任何东西。要在集合上迭代,它肯定应该是++i或i++,即星型错误。让我继续查找如果这是线程化的,那么
SampleListener::onFrame()
中的
collection.erase()
将使
display()
中的迭代器无效。您需要一个互斥锁。见鬼,
推回()
可能会使您的迭代器无效。您似乎想要一个带有头/尾指针的固定长度结构,而不是这个动态向量。@JoeZ调用得很好。我用固定长度的二维数组编写了一个小类,它现在就可以按我希望的方式工作了。
#include <iostream>
#include <fstream>
#include "Leap.h"
#include <math.h>
#include "GL/glut.h"
#include <deque>

using namespace Leap;
using namespace std;

bool one = true;

double x = 10, y = 100;
double screenWidth = 480, screenHeight = 480;
double time, timeDisplayed = 5.0 * (pow(10.0, 9.0));

vector < double > entry, *temp;
vector < vector< double > > collection;

class SampleListener : public Listener {
  public:
      int firstFrame, firstTime;
      bool first;
    virtual void onInit(const Controller&);
    virtual void onConnect(const Controller&);
    virtual void onExit(const Controller&);
    virtual void onFrame(const Controller&);
};

void SampleListener::onInit(const Controller& controller){
    cout << "Initialized.\n";
    first = true;
}

void SampleListener::onConnect(const Controller& controller){
    cout << "Connected.\n";
    controller.enableGesture(Gesture::TYPE_SCREEN_TAP);
}

void SampleListener::onExit(const Controller& controller){
    cout << "Exited.\n";
}

void SampleListener::onFrame(const Controller& controller) {
    const Frame frame = controller.frame();

    //some initial frame processing/file writing
    if (first){
        first = !first;
        firstFrame = frame.id();
        firstTime = frame.timestamp();
    }
    else {

    //record hand information
        if (!frame.hands().isEmpty()){
            const Hand hand = frame.hands().frontmost();
            const Finger track = hand.fingers().frontmost();
            x = track.tipPosition().x;
            y = track.tipPosition().y;
            time = frame.timestamp() - firstTime;
            entry.push_back(x);
            entry.push_back(y);
            entry.push_back(time);
            collection.push_back(entry);
            entry.clear();
            while (collection.size() > 100 && !collection.empty()){
                collection.erase(collection.begin());
            }
        }
    }

}

// Create a sample listener and controller
SampleListener listener;
Controller controller;

void display(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glBegin(GL_POINTS);
    for (auto i = collection.begin(); i != collection.end(); *i++){
    cout << collection.size() << endl;
        glVertex2f(i->at(0), i->at(1));
    }

    glEnd();

    glFlush();
    glutSwapBuffers();
}

void init(){
    cout << "in init" << endl;
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glColor3f(1.0, 1.0, 1.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-screenWidth/2, screenWidth/2, 0, screenHeight);
    glViewport(0, 0, screenWidth, screenHeight);
    glPointSize(3.0f);
    cout << "leaving init" << endl;

}


void idle(void){
    glutPostRedisplay();
}

//<<<<<<<<<<<<<<<<<<<<<<< myKeys >>>>>>>>>>>>>>>>>>>>>>>>
void myKeys(unsigned char key, int x, int y)
{

    switch(key)  
    {
        case 'q':   // Quit
        // Remove the sample listener when done
        controller.removeListener(listener);

            exit(0);
    }
}

int main (int argc, char** argv) {

    listener.first = true;

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(screenWidth, screenHeight);
    glutInitWindowPosition(800, 100);
    glutCreateWindow("test");
    init();
    glutDisplayFunc(display);
    glutKeyboardFunc(myKeys);
    glutIdleFunc(idle);

    controller.addListener(listener);

    glutMainLoop();


  return 0;

}