leap motionUsing the Tracking API.docx
- 文档编号:9401551
- 上传时间:2023-02-04
- 格式:DOCX
- 页数:31
- 大小:69.97KB
leap motionUsing the Tracking API.docx
《leap motionUsing the Tracking API.docx》由会员分享,可在线阅读,更多相关《leap motionUsing the Tracking API.docx(31页珍藏版)》请在冰豆网上搜索。
leapmotionUsingtheTrackingAPI
UsingtheTrackingAPI
HowtounderstandandusethemainclassesandfunctionsintheLeapMotiontrackingAPI:
ConnectingtotheLeapMotionConnectingtotheLeapMotioncontroller
TrackingDataWorkingwiththetrackingdata.
FramesHowtogettrackingdatafromtheAPI.
GesturesWorkingwithgestures.
TouchEmulationWorkingwiththevirtualtouchplane.
MotionsWorkingwithmotionfactors.
CoordinateSystemsConvertingfromLeapMotiontoapplicationcoordinates.
ConnectingtotheLeapMotion
ToconnecttotheLeapMotiondevice,createaControllerobject.TheControllerobjectautomaticallyestablishesaconnectiontotheLeapMotiondaemonorservicewhichthenpassestrackingdata,intheformofFrameobjectstoyourapplication.
Controllercontroller=newController();
UsetheControllerobjecttogetinformationaboutthestateoftheconnectionandconnectedhardwareandtosetconnectionoptionsforyourapplication.
GettingFrames
GetFrameobjectscontainingtrackingdatabycallingtheController.frame()function.YoucancallthisfunctionwheneveryourapplicationisreadytoprocessdatatogetthemostrecentsetofdatageneratedbytheLeapMotiondevice.YoucanalsoimplementaListenerobjectdefiningacallbackfunctionandtheControllerwillinvokethisfunctionwhenanewframeofdataisready.
SeeFramesformoreinformation.
ForegroundandBackgroundApplications
Ordinarily,theLeapMotionservice/daemononlysendstrackingdatatotheapplicationthathastheoperatingsysteminputfocus.Thispreventsapplicationsidlinginthebackgroundfromreceivinginputintendedforanotherapplications.
Ifyourapplicationrunsintheforegroundandyouwanttoknowwhethertheservice/daemonconsidersyourapplicationtobetheforegroundapplication,youcancheckController.hasFocus()orlistentoeventsdispatchedbythecontroller.Theservice/daemongetschangesinapplicationfocusstatusdirectlyfromtheoperatingsystem.NotethattheLinuxoperatingsystemdoesnotprovidethisinformation,soapplicationsrunningonLinuxalwaysreceivetrackingdata.
NotethatapplicationsconnectingtotheLeapMotionservice/daemonthroughtheWebSocketserverdonotautomaticallystartreceivingdatawhentheygaintheoperatingsystemfocus.ApplicationsusingtheWebSocketservermustexplicitlyrequestfocusfromtheservicewhentheymovetotheforeground.(TheLeapJSJavaScriptlibrarydoesmanagetheforeground-backgroundtransitionforyou,however.)
YoucantellthecontrollerthatyourapplicationwantsdataevenwhenitisinthebackgroundusingController.setPolicyFlags().SettheBACKGROUND_POLICY_FLAGpolicyforapplicationssuchasmouseemulatorsoroperatingsystemcontrolsthatneeddatawhentheyarenotthefocusedapplication.
if(controller.policyFlags()!
=Controller.PolicyFlag.POLICY_BACKGROUND_FRAMES){
controller.setPolicyFlags(Controller.PolicyFlag.POLICY_BACKGROUND_FRAMES);
}
EnablingGestures
Tousethebuilt-ingestures,youmustenablethemfirstusingController.enableGesture():
controller.enableGesture(Gesture.Type.TYPE_CIRCLE);
Enableonlythosegesturesthatyourapplicationuses.
ConnectionState
WhenyoucreateaController,theobjectattemptstoestablishaconnectiontotheLeapMotionservice/daemon.Ifitsucceeds,the|Controller.isServiceConnected|_propertychangestotrue.Likewise,iftheLeapMotionhardwareispluggedinanddetectedbytheservice/daemon,theController.isConnected()propertychangestotrue.
TheisServiceConnected()andisConnected()propertiescanchangewhileyourapplicationisrunning.Youcanmonitortheirvaluesorlistenforeventsdispatchedbythecontrollerwhentheconnectionstatuschanges.
ListeningforControllerEvents
TheControllerobjectdispatchesanumberofeventsusingtheListenermechanism.Tohandletheseevents,youcanextendtheListenerclasstoimplementcallbackfunctions.TheControllerinvokestherelevantcallbackfunctionwhenaneventoccurs.
Theeventsinclude:
onConnect()TheControllerconnectstotheLeapMotionservice/daemonandtheLeapMotionhardwareisattached.
onDeviceChange()ThestatusofaLeapMotionhardwaredevicechanges.
onDisconnect()TheControllerdisconnectsfromtheLeapMotionservice/daemonortheLeapMotionhardwareisremoved.
onExit()TheControllerobjectisdestroyed.
onFocusGained()Theapplicationhasgainedoperatingsysteminputfocusandwillstartreceivingtrackingdata.
onFocusLost()Theapplicationhaslostoperatingsysteminputfocus.TheapplicationwillstopreceivingtrackingdataunlessithassettheBACKGROUND_FRAMES_POLICY.
onFrame()AnewFrameoftrackingdataisavailable.
onInit()TheControllerobjectisinitialized.
onServiceConnect()TheControllerhasconnectedtotheLeapMotionservice/daemon.
onServiceDisconnect()TheControllerhaslostitsconnectiontotheLeapMotionservice/daemon.
ThedispatchingControllerpassesitselftothecallback,whichmakesiteasytothenquerytheControllerformoreinformationinthebodyofthecallbackfunction.
ThefollowingexampleshowsasimplisticimplementationofaListenersubclasswithsomerepresentativecallbackfunctions:
importcom.leapmotion.leap.*;
publicclassLeapEventListenerextendsListener{
publicvoidonFrame(Controllercontroller){
System.out.println("NewFrame");
}
publicvoidonInit(Controllercontroller){
System.out.println("Initialized");
}
publicvoidonConnect(Controllercontroller){
System.out.println("Connected");
}
publicvoidonDisconnect(Controllercontroller){
System.out.println("Disconnected");
}
publicvoidonFocusGained(Controllercontroller){
System.out.println("Focusgained");
}
publicvoidonFocusLost(Controllercontroller){
System.out.println("Focuslost");
}
}
Youcanthenaddaninstanceofyourlistenerimplementationtoacontroller:
Controllercontroller=newController();
LeapEventListenerlistener=newLeapEventListener();
controller.addListener(listener);
EacheventcallbackfunctionisinvokedonanindependentthreadcreatedbytheLeapMotionlibrary.Thetrackingdataobjectscreatedbythelibraryarethreadsafe,butyoumustmakesurethatotherapplicationdataaccessedfromthecallbackfunctioncanbesafelyalteredfromsepratethreads.YoucanavoidthecomplicationsassociatedwithmultithreadedapplicationsbypollingtheControllerforframesandstatewhenconvenientforyourapplicationinsteadofusinglisteners.
Note:
InJavaapplications,theuseofmultiplethreadscanbeproblematic.Unfortunately,threadsarenotreused,sousinglistenerscanaddalotofoverheadbecauseoftheadditionalnumberofthread-relatedobjectscreatedanddestroyedeverysecond.(Thisistrueevenifyoudonotimplementacallbackforthe|onFrame|event.)
DeviceTypes
TherearecurrentlythreetypesofLeapMotionhardwareavailable.Theperformanceofallofthesedevicesisequivalent.However,beawarethatthedevicesareverydifferentphysically.Forexample,devicesembeddedinlaptopscannotbeunplugged,butratherareactivatedordeactivatedwithkeyboardshortcuts.
YoucangetthedevicetypefromDevice.type().UseController.devices()togetthelistofattacheddevices(currentlyonlyonedeviceisrecognizedatatime).
TrackingData
TheLeapMotionAPIdefinesaclassrepresentingeachoftheprimarytrackedobjects.
Frame
TheFrameobjectisessentiallytherootofthedatamodelandprovidesaccesstoallthetrackedentities.AnewFrameiscreatedateachupdateinterval.
Theframecontainslistsofthehands,fingers,tools,andgesturestrackedinthatframe.(YoucanalsogetthefingersandtoolsforaspecifichandfromtherelevantHandobject.)
Controllercontroller=newController();
//waituntilController.isConnected()evaluatestotrue
//...
Frameframe=controller.frame();
HandListhands=frame.hands();
PointableListpointables=frame.pointables();
FingerListfingers=frame.fingers();
ToolListtools=frame.tools();
SeeFrames
Hand
Thehandobjectcontainslistsofthefingersandtoolsassociatedwiththathand.
//handisaHandobject
PointableListpointables=hand.pointables();//Bothfingersandtools
FingerListfingers=hand.fingers();
ToolListtools=hand.tools();
SeeHands
Pointable,Finger,Tool
Pointableobjectsdefinethebasiccharacteristicscommontofingersandtools.TheFingerandToolclassesextendPointablewithadditionalinformationspecifictothoseentities.
if(pointable.isTool()){
Tooltool=newTool(pointable);
}else{
Fingerfinger=newFinger(pointable);
}
SeeFingersandTools
Gesture
TheGestureclassanditssubclassesrepresentasnapshotofoneofthefourgesturesrecognizedbytheLeapMotionsoftware.
SeeGestures
ListsofTrackedObjects
TheFrameandHandobjectscontainlistsoftheothertrackedobjects.
HandList,PointableList,FingerList,andToolListclassesprovideafewconveniencefunctionsforfilteringthelistmembers.Forexample,thefrontmost()functionreturnstheitemthatisthemostforwardintheLeapMotioncontroller’sfieldofview(smallestzvalue).
UtilityClasses
TheVectorclassdescribespointsanddirections.TheVectorclassalsoprovidesseveralusefulmathfunctionsforworkingwithvectors.
Likewise,theMatrixclassrepresentsthingslikerotationsandothertransformsreturnedbysomefunctionsintheAPI.
TheInteractionBoxclassprovidesfunctionstohelpmaparectilinearportionoftheLeapMotioncontroller’sfieldofviewtoeither2Dscreenspaceor3Dspace.SeeTouchEmulation.
Frames
TheLeapMotionAPIpresentsmotiontrackingdatatoyourapplicationasaseriesofsnapshotscalled
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- leap motionUsing the Tracking API