Interpolated Frames ExampleΒΆ

The Interpolation example demonstrates how to request tracking data by timestamp. This involves the following main tasks:

  1. Maintaining synchronization between the application and Leap clocks.

  2. Translating the target application frame time to a Leap timestamp.

  3. Allocating a large enough buffer to hold the frame data at that time.

  4. Requesting the frame at the target time.

See Interpolating Frames

 1 #undef __cplusplus
 2
 3 #include <stdio.h>
 4 #include <stdlib.h>
 5
 6 #ifdef _WIN32
 7 #include <Windows.h>
 8 #else
 9 #include <unistd.h>
10 #endif
11
12 #include <time.h>
13 #include "LeapC.h"
14 #include "ExampleConnection.h"
15
16 LEAP_CLOCK_REBASER clockSynchronizer;
17
18 int main(int argc, char** argv) {
19   LEAP_CONNECTION* connHandle = OpenConnection();
20
21   while(!IsConnected){
22     millisleep(250);
23   }
24
25   printf("Connected.\n");
26   //Create the clock synchronizer
27   LeapCreateClockRebaser(&clockSynchronizer);
28   clock_t cpuTime;
29   int64_t targetFrameTime = 0;
30   uint64_t targetFrameSize = 0;
31   eLeapRS result;
32   for(;;){
33     //Calculate the application time
34     cpuTime = (clock_t).000001 * clock()/CLOCKS_PER_SEC;//microseconds
35     //Synchronize the clocks
36     LeapUpdateRebase(clockSynchronizer, cpuTime, LeapGetNow());
37
38     //Simulate delay (i.e. processing load, v-sync, etc)
39     millisleep(10);
40
41     //Now get the updated application time
42     cpuTime = (clock_t) .000001 * clock()/CLOCKS_PER_SEC;
43
44     //Translate application time to Leap time
45     LeapRebaseClock(clockSynchronizer, cpuTime, &targetFrameTime);
46
47     //Get the buffer size needed to hold the tracking data
48     result = LeapGetFrameSize(*connHandle, targetFrameTime, &targetFrameSize);
49     if(result == eLeapRS_Success){
50       //Allocate enough memory
51       LEAP_TRACKING_EVENT* interpolatedFrame = malloc((size_t)targetFrameSize);
52       //Get the frame
53       result = LeapInterpolateFrame(*connHandle, targetFrameTime, interpolatedFrame, targetFrameSize);
54       if(result == eLeapRS_Success){
55         //Use the data...
56         printf("Frame %lli with %i hands with delay of %lli microseconds.\n",
57                (long long int)interpolatedFrame->tracking_frame_id,
58                interpolatedFrame->nHands,
59                (long long int)LeapGetNow() - interpolatedFrame->info.timestamp);
60         for(uint32_t h = 0; h < interpolatedFrame->nHands; h++){
61         LEAP_HAND* hand = &interpolatedFrame->pHands[h];
62         printf("    Hand id %i is a %s hand with position (%f, %f, %f).\n",
63                     hand->id,
64                     (hand->type == eLeapHandType_Left ? "left" : "right"),
65                     hand->palm.position.x,
66                     hand->palm.position.y,
67                     hand->palm.position.z);
68         }
69         //Free the allocated buffer when done.
70         free(interpolatedFrame);
71       }
72       else {
73         printf("LeapInterpolateFrame() result was %s.\n", ResultString(result));
74       }
75     }
76     else {
77       printf("LeapGetFrameSize() result was %s.\n", ResultString(result));
78     }
79   } //ctrl-c to exit
80   return 0;
81 }
82 //End-of-Sample

This example uses ExampleConnection.c, like the other examples, but for the most part interacts directly with the LeapC library.


Back to top