FREObject LNLeapDevice::getClosestScreenHit(int pointableId) { ScreenList screenList = controller->calibratedScreens(); Frame frame = controller->frame(); PointableList pointables = frame.pointables(); Pointable pointable; // TODO: Create a fake pointable width tipPosition and direction instead of looping bool didFind = false; for (int i = 0; i < pointables.count(); i++) { if (pointables[i].id() == pointableId) { pointable = pointables[i]; didFind = true; break; } } FREObject freScreenId; if(didFind) { Screen screen = screenList.closestScreenHit(pointable); FRENewObjectFromInt32(screen.id(), &freScreenId); } else { FRENewObjectFromInt32(0, &freScreenId); } return freScreenId; }
//-------------------------------------------------------------- void ofApp::draw(){ string touchStatus; stringstream info; cam.begin(); ofDrawGrid(500, 5, true); light1.enable(); light2.enable(); ofSetColor(255, 215, 0); Frame frame = controller.frame(); for (int i=0; i < frame.hands().count(); i++) { Hand hand = frame.hands()[i]; for (int j = 0; j < hand.fingers().count(); j ++) { if (j == 0) { Finger finger = frame.fingers()[j]; ofSpherePrimitive sphere; sphere.setPosition(finger.tipPosition().x, finger.tipPosition().y, finger.tipPosition().z); sphere.draw(); info << "Finger position x : " << finger.tipPosition().x << " y : " << finger.tipPosition().y << " z : " << finger.tipPosition().z << endl; } } } cam.end(); PointableList pointables = controller.frame().pointables(); InteractionBox iBox = controller.frame().interactionBox(); for (int p = 0; p < pointables.count(); p++) { Pointable pointable = pointables[p]; Vector normalizedPosition = iBox.normalizePoint(pointable.stabilizedTipPosition()); float x = normalizedPosition.x * ofGetWidth(); float y = ofGetHeight() - normalizedPosition.y * ofGetHeight(); if (pointable.touchDistance() > 0 && pointable.touchZone() != Leap::Pointable::ZONE_NONE) { ofSetColor(0, 255, 0); touchStatus = "Hover"; } else if (pointable.touchDistance() <= 0) { ofSetColor(255, 0, 0); touchStatus = "Touch"; } else { ofSetColor(0, 0, 255); touchStatus = "None"; } ofCircle(x, y, 30); info << "Point Number : " << p << endl; info << "Touch distance : " << ofToString(pointable.touchDistance()) << endl; info << "Circle x : " << x << " y : " << y << endl; } ofDrawBitmapString("Touch Status : " + touchStatus, 20, 20); ofDrawBitmapString(info.str(), 20, 40); }
void SampleListener::onFrame(const Controller& controller) { // Get the most recent frame and report some basic information const Frame frame = controller.frame(); /* std::cout << "Frame id: " << frame.id() << ", timestamp: " << frame.timestamp() << ", hands: " << frame.hands().count() << ", fingers: " << frame.fingers().count() << ", tools: " << frame.tools().count() << std::endl; */ const PointableList plist = frame.pointables(); if (!plist.empty()) { // get mutex boost::mutex::scoped_lock lk(mtx); // clear last datas vpoint.clear(); for(int i=0;i<plist.count();i++){ vpoint.push_back(plist[i]); Vector t = plist[i].tipVelocity(); float speed = t.x*t.x + t.y*t.y + t.z*t.z; if(speed < 10.0f){ Vector v = screen.intersect(plist[i], true); float dist = sqrt(pow(target_x-v.x*screen_width, 2) + pow(target_y-v.y*screen_height, 2)); if(dist <= (float)target_width[test_c]){ for(unsigned int j=0;j<start_position.size();j++){ if(start_position[j].id() == plist[i].id()){ Vector sv = screen.intersect(start_position[j], true); target_time[test_c] = glutGet(GLUT_ELAPSED_TIME) - start_time; target_distance[test_c] = (int)sqrt(pow((v.x - sv.x)*screen_width, 2) + pow((v.y - sv.y)*screen_height, 2)); test_c++; break; } } new_target = 1; } } } } }
void SampleListener::onFrame(const Controller& controller) { // This function needs to be broken up, it's way too long const Frame frame = controller.frame(); //int currid = 1; //check if we already processed this frame if (frame.id() == this->lastFrameID) return; this->lastFrameID = frame.id(); //stop activity if we've not been used for ACTIVETIMEOUT if ((ACTIVETIMEOUT + this->lastEvent) < time(0) && this->active) { std::cout << "auto deactivated" << std::endl; this->active = false; } if (frame.fingers().count() == 2) { GestureList gestures = frame.gestures(); for (int it = 0; it < gestures.count(); ++it) { if (gestures[it].type() != Gesture::TYPE_KEY_TAP) continue; if ((this->lastToggle + TOGGLE_FRAME_LIMIT) > frame.id()) //avoid detecting the same tap twice continue; KeyTapGesture gesture = gestures[it]; Vector v = gesture.direction(); this->active = !this->active; this->lastToggle = frame.id(); this->lastEvent = time(0); std::cout << (this->active ? "activated" : "deactivated") << std::endl; break; //make sure we don't accidentally use the same list twice } } if (!this->active) return; if (frame.fingers().count() == 1 & side != 2) { PointableList pointables = frame.pointables(); InteractionBox iBox = frame.interactionBox(); for (int p = 0; p < pointables.count(); ++p) { Pointable pointable = pointables[p]; Vector normalizedPosition = iBox.normalizePoint(pointable.stabilizedTipPosition()); float distance = pointable.touchDistance(); float x = normalizedPosition.x * (Mouse->w_width+250); float y = (Mouse->w_height + 250) - normalizedPosition.y * (Mouse->w_height + 250); if (side == 0) { Mouse->move((int)x, (int)y); pressedDelay = 0; } if (distance < 0) { clickcount++; side = 1; if (clickcount > sensibility) { if (pressstate == 0) { Mouse->leftPress(); pressstate = 1; } else { if (clickcount >= 30) { if (rclick == 0) Mouse->move((int)x,(int)y); std::cout << "move side: "<< side << std::endl; clickcount = 20; } } } } else if (side == 1) { Mouse->leftRelease(); std::cout << "released side: " << side << std::endl; side = 0; pressstate = 0; pressedDelay = 0; clickcount = 0; rclick = 0; } } this->lastEvent = time(0); } else if (frame.fingers().count() > 1 & side != 1) { clickcount = 0; std::cout << "active side: " << side << std::endl; // Get gestures const GestureList gestures = frame.gestures(); for (int g = 0; g < gestures.count(); ++g) { Gesture gesture = gestures[g]; switch (gesture.type()) { case Gesture::TYPE_CIRCLE : { CircleGesture circle = gesture; if (gesture.state() == 3 & circle.progress() > 1) { side = 0; rclick = 1; Mouse->rightPress(); Mouse->rightRelease(); } break; } case Gesture::TYPE_SWIPE : { side = 2; SwipeGesture swipe = gesture; Vector d = swipe.direction(); // std::cout << "Swipe id: " << d(1) << std::endl; if (d[1] > 0.8) { this->mystate = 1; //up // std::cout << "wheel up " <<std::endl; } else if (d[1] < -0.8) { this->mystate = 2; //down //std::cout << "wheel down " <<std::endl; } if (swipe.state() == 3) { if (this->mystate == 1) { side = 0; this->mystate = 0; Mouse->wheelUp(); } else if (this->mystate == 2) { side = 0; this->mystate = 0; Mouse->wheelDown(); } } break; } default: //std::cout << "Unknown gesture type." << std::endl; break; } } this->lastEvent = time(0); } }