2013-12-15 32 views
-1

我已經成功編寫了代碼,該代碼將我的一個手指的(x,y)位置用我的Leap Motion控制器(跟蹤手指和手部運動的紅外掃描設備),並使用OpenGL繪製具有類似(x,y)座標的點。基本上,您移動手指,移動屏幕上顯示的點。使用OpenGL,LeapMotion應用程序「矢量迭代器不可增量」

我現在想要做的是顯示記錄的最後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; 

} 
+0

* i ++?爲什麼不是++我?或我++我猜 –

+0

只是代碼的最新迭代。在過去嘗試過,但它沒有改變任何東西。 – Connor

+0

以及迭代集合,它肯定應該是++ i或i ++,那個星星是錯誤的。讓我繼續尋找 –

回答

1

如果這個線程,那麼你的collection.erase()SampleListener::onFrame()將在display()無效的迭代器。你需要一個互斥體。哎呀,push_back()可能會使您的迭代器失效。似乎你想要一個具有頭部/尾部指針的固定長度結構而不是這個動態矢量。