"矢量迭代器不可增量"与OpenGL,LeapMotion应用程序

"vector iterator not incrementable" with OpenGL, LeapMotion application

本文关键字:OpenGL LeapMotion 应用程序 迭代器      更新时间:2023-10-16

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

我现在想做的是显示记录的最后100个位置——所以,你不会看到一个点四处移动,而是看到一个蜿蜒的点集合,这些点沿着你的指尖移动。

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

它编译得很好,但在画了几点之后,我得到了一个运行时错误,说"矢量迭代器不可增量"。不知道如何处理。

代码:

#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;
}

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