Kinect/NuiSkeleton.h Source File

NuiSkeleton.h
Go to the documentation of this file.
1 /************************************************************************
2 * *
3 * NuiSkeleton.h -- This module defines the APIs for the Natural *
4 * User Interface(NUI) skeleton services. *
5 * *
6 * Copyright (c) Microsoft Corp. All rights reserved. *
7 * *
8 ************************************************************************/
9 #pragma once
10 
11 #ifndef NUIAPI
12 #error "You must include nuiapi.h rather than including nuiskeleton.h directly"
13 #endif
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 #ifndef FLT_EPSILON
20  #define FLT_EPSILON 1.192092896e-07F /* smallest such that 1.0+FLT_EPSILON != 1.0 */
21 #endif
22 
23 #ifndef _Matrix4_
24 #define _Matrix4_
25 typedef struct _Matrix4
26 {
27  FLOAT M11;
28  FLOAT M12;
29  FLOAT M13;
30  FLOAT M14;
31  FLOAT M21;
32  FLOAT M22;
33  FLOAT M23;
34  FLOAT M24;
35  FLOAT M31;
36  FLOAT M32;
37  FLOAT M33;
38  FLOAT M34;
39  FLOAT M41;
40  FLOAT M42;
41  FLOAT M43;
42  FLOAT M44;
43 } Matrix4;
44 #endif
45 
46 #ifndef __NuiSensor_h__
47 
48 #ifndef _Vector4_
49 #define _Vector4_
50 typedef struct _Vector4
51 {
52  FLOAT x;
53  FLOAT y;
54  FLOAT z;
55  FLOAT w;
56 } Vector4;
57 #endif
58 
59 #ifdef __XNAMATH_H__
60 
61 #ifndef _Vector4_Functions_
62 #define _Vector4_Functions_
63 
64 inline XMVECTOR XMVectorFromVector4(const Vector4& vec4)
65 {
66  return XMVectorSet(vec4.x, vec4.y, vec4.z, vec4.w);
67 }
68 
69 inline Vector4 XMVectorToVector4(FXMVECTOR V)
70 {
71  Vector4 vec4 = { XMVectorGetX(V), XMVectorGetY(V), XMVectorGetZ(V), XMVectorGetW(V) };
72  return vec4;
73 }
74 
75 #endif
76 
77 #endif
78 
80 {
103 
104 //
105 // Number of NUI_SKELETON_DATA elements in NUI_SKELETON_FRAME
106 //
107 
108 #define NUI_SKELETON_COUNT 6
109 
110 #endif // __NuiSensor_h__
111 
112 //
113 // Number of NUI_SKELETON_DATA elements that can be in the NUI_SKELETON_TRACKED state
114 //
115 
116 #define NUI_SKELETON_MAX_TRACKED_COUNT 2
117 
118 //
119 // Tracking IDs start at 1
120 //
121 
122 #define NUI_SKELETON_INVALID_TRACKING_ID 0
123 
124 #ifndef __NuiSensor_h__
126 {
131 
133 {
138 
139 typedef struct _NUI_SKELETON_DATA
140 {
142  DWORD dwTrackingID;
143  DWORD dwEnrollmentIndex;
144  DWORD dwUserIndex;
148  DWORD dwQualityFlags;
150 #endif // __NuiSensor_h__
151 
152 
153 //
154 // Force a link error if the size of NUI_SKELETON_DATA is different
155 // between the static library and the header included by the title.
156 //
157 
158 __declspec(selectany) DWORD NuiSkeletonDataSize = sizeof(NUI_SKELETON_DATA);
159 
160 
161 #define NUI_SKELETON_QUALITY_CLIPPED_RIGHT 0x00000001
162 #define NUI_SKELETON_QUALITY_CLIPPED_LEFT 0x00000002
163 #define NUI_SKELETON_QUALITY_CLIPPED_TOP 0x00000004
164 #define NUI_SKELETON_QUALITY_CLIPPED_BOTTOM 0x00000008
165 
166 #ifndef __NuiSensor_h__
167 #pragma pack(push, 16)
168 typedef struct _NUI_SKELETON_FRAME
169 {
170  LARGE_INTEGER liTimeStamp;
171  DWORD dwFrameNumber;
172  DWORD dwFlags;
175  NUI_SKELETON_DATA SkeletonData[NUI_SKELETON_COUNT];
177 #pragma pack(pop)
178 
180 {
181  FLOAT fSmoothing; // [0..1], lower values closer to raw data
182  FLOAT fCorrection; // [0..1], lower values slower to correct towards the raw data
183  FLOAT fPrediction; // [0..n], the number of frames to predict into the future
184  FLOAT fJitterRadius; // The radius in meters for jitter reduction
185  FLOAT fMaxDeviationRadius; // The maximum radius in meters that filtered positions are allowed to deviate from raw data
187 #endif // __NuiSensor_h__
188 
189 // Flags returned by NuiSkeletonGetNextFrame in the dwFlags field of the NUI_SKELETON_FRAME structure
190 #define NUI_SKELETON_FRAME_FLAG_SEATED_SUPPORT_ENABLED 0x00000008
191 
192 // Flags for use with the NuiSkeletonTrackingEnable function
193 #define NUI_SKELETON_TRACKING_FLAG_SUPPRESS_NO_FRAME_DATA 0x00000001
194 #define NUI_SKELETON_TRACKING_FLAG_TITLE_SETS_TRACKED_SKELETONS 0x00000002
195 #define NUI_SKELETON_TRACKING_FLAG_ENABLE_SEATED_SUPPORT 0x00000004
196 #define NUI_SKELETON_TRACKING_FLAG_ENABLE_IN_NEAR_RANGE 0x00000008
197 
198 #ifndef __NuiSensor_h__
199 typedef struct _NUI_SKELETON_BONE_ROTATION
200 {
204 
205 typedef struct _NUI_SKELETON_BONE_ORIENTATION
206 {
212 #endif // __NuiSensor_h__
213 
263 _Check_return_ HRESULT NUIAPI NuiSkeletonTrackingEnable(
264  _In_opt_ HANDLE hNextFrameEvent,
265  _In_ DWORD dwFlags
266  );
267 
284 _Check_return_ HRESULT NUIAPI NuiSkeletonTrackingDisable(
285  );
286 
317 _Check_return_ HRESULT NUIAPI NuiSkeletonGetNextFrame(
318  _In_ DWORD dwMillisecondsToWait,
319  _Inout_ NUI_SKELETON_FRAME *pSkeletonFrame
320  );
321 
347 _Check_return_ HRESULT NUIAPI NuiSkeletonSetTrackedSkeletons(
348  _In_count_c_(NUI_SKELETON_MAX_TRACKED_COUNT) DWORD TrackingIDs[NUI_SKELETON_MAX_TRACKED_COUNT]
349  );
350 
351 #ifdef __cplusplus
352 } //close extern "C"
353 
354 // Assuming a pixel resolution of 320x240
355 // x_meters = (x_pixelcoord - 160) * NUI_CAMERA_DEPTH_IMAGE_TO_SKELETON_MULTIPLIER_320x240 * z_meters;
356 // y_meters = (y_pixelcoord - 120) * NUI_CAMERA_DEPTH_IMAGE_TO_SKELETON_MULTIPLIER_320x240 * z_meters;
357 #define NUI_CAMERA_DEPTH_IMAGE_TO_SKELETON_MULTIPLIER_320x240 (NUI_CAMERA_DEPTH_NOMINAL_INVERSE_FOCAL_LENGTH_IN_PIXELS)
358 
359 // Assuming a pixel resolution of 320x240
360 // x_pixelcoord = (x_meters) * NUI_CAMERA_SKELETON_TO_DEPTH_IMAGE_MULTIPLIER_320x240 / z_meters + 160;
361 // y_pixelcoord = (y_meters) * NUI_CAMERA_SKELETON_TO_DEPTH_IMAGE_MULTIPLIER_320x240 / z_meters + 120;
362 #define NUI_CAMERA_SKELETON_TO_DEPTH_IMAGE_MULTIPLIER_320x240 (NUI_CAMERA_DEPTH_NOMINAL_FOCAL_LENGTH_IN_PIXELS)
363 
402 inline
403 VOID
404 NuiTransformSkeletonToDepthImage(
405  _In_ Vector4 vPoint,
406  _Out_ LONG *plDepthX,
407  _Out_ LONG *plDepthY,
408  _Out_ USHORT *pusDepthValue,
409  _In_ NUI_IMAGE_RESOLUTION eResolution
410  )
411 {
412  if((plDepthX == NULL) || (plDepthY == NULL) || (pusDepthValue == NULL))
413  {
414  return;
415  }
416 
417  //
418  // Requires a valid depth value.
419  //
420 
421  if(vPoint.z > FLT_EPSILON)
422  {
423  DWORD width;
424  DWORD height;
425  NuiImageResolutionToSize( eResolution, width, height );
426 
427  //
428  // Center of depth sensor is at (0,0,0) in skeleton space, and
429  // and (width/2,height/2) in depth image coordinates. Note that positive Y
430  // is up in skeleton space and down in image coordinates.
431  //
432  // The 0.5f is to correct for casting to int truncating, not rounding
433 
434  *plDepthX = static_cast<INT>( width / 2 + vPoint.x * (width/320.f) * NUI_CAMERA_SKELETON_TO_DEPTH_IMAGE_MULTIPLIER_320x240 / vPoint.z + 0.5f);
435  *plDepthY = static_cast<INT>( height / 2 - vPoint.y * (height/240.f) * NUI_CAMERA_SKELETON_TO_DEPTH_IMAGE_MULTIPLIER_320x240 / vPoint.z + 0.5f);
436 
437  //
438  // Depth is in meters in skeleton space.
439  // The depth image pixel format has depth in millimeters shifted left by 3.
440  //
441 
442  *pusDepthValue = static_cast<USHORT>(vPoint.z *1000) << 3;
443  } else
444  {
445  *plDepthX = 0;
446  *plDepthY = 0;
447  *pusDepthValue = 0;
448  }
449 }
450 
486 inline
487 VOID
488 NuiTransformSkeletonToDepthImage(
489  _In_ Vector4 vPoint,
490  _Out_ LONG *plDepthX,
491  _Out_ LONG *plDepthY,
492  _Out_ USHORT *pusDepthValue
493  )
494 {
495  NuiTransformSkeletonToDepthImage( vPoint, plDepthX, plDepthY, pusDepthValue, NUI_IMAGE_RESOLUTION_320x240);
496 }
497 
525 inline
526 VOID
527 NuiTransformSkeletonToDepthImage(
528  _In_ Vector4 vPoint,
529  _Out_ FLOAT *pfDepthX,
530  _Out_ FLOAT *pfDepthY,
531  _In_ NUI_IMAGE_RESOLUTION eResolution
532  )
533 {
534  if((pfDepthX == NULL) || (pfDepthY == NULL))
535  {
536  return;
537  }
538 
539  //
540  // Requires a valid depth value.
541  //
542 
543  if(vPoint.z > FLT_EPSILON)
544  {
545  DWORD width;
546  DWORD height;
547  NuiImageResolutionToSize( eResolution, width, height );
548 
549  //
550  // Center of depth sensor is at (0,0,0) in skeleton space, and
551  // and (width/2,height/2) in depth image coordinates. Note that positive Y
552  // is up in skeleton space and down in image coordinates.
553  //
554 
555  *pfDepthX = width / 2 + vPoint.x * (width/320.f) * NUI_CAMERA_SKELETON_TO_DEPTH_IMAGE_MULTIPLIER_320x240 / vPoint.z;
556  *pfDepthY = height / 2 - vPoint.y * (height/240.f) * NUI_CAMERA_SKELETON_TO_DEPTH_IMAGE_MULTIPLIER_320x240 / vPoint.z;
557 
558  } else
559  {
560  *pfDepthX = 0.0f;
561  *pfDepthY = 0.0f;
562  }
563 }
564 
589 inline
590 VOID
591 NuiTransformSkeletonToDepthImage(
592  _In_ Vector4 vPoint,
593  _Out_ FLOAT *pfDepthX,
594  _Out_ FLOAT *pfDepthY
595  )
596 {
597  NuiTransformSkeletonToDepthImage(vPoint, pfDepthX, pfDepthY, NUI_IMAGE_RESOLUTION_320x240);
598 }
599 
615 inline
616 Vector4
617 NuiTransformDepthImageToSkeleton(
618  _In_ LONG lDepthX,
619  _In_ LONG lDepthY,
620  _In_ USHORT usDepthValue,
621  _In_ NUI_IMAGE_RESOLUTION eResolution
622  )
623 {
624  DWORD width;
625  DWORD height;
626  NuiImageResolutionToSize( eResolution, width, height );
627 
628  //
629  // Depth is in meters in skeleton space.
630  // The depth image pixel format has depth in millimeters shifted left by 3.
631  //
632 
633  FLOAT fSkeletonZ = static_cast<FLOAT>(usDepthValue >> 3) / 1000.0f;
634 
635  //
636  // Center of depth sensor is at (0,0,0) in skeleton space, and
637  // and (width/2,height/2) in depth image coordinates. Note that positive Y
638  // is up in skeleton space and down in image coordinates.
639  //
640 
641  FLOAT fSkeletonX = (lDepthX - width/2.0f) * (320.0f/width) * NUI_CAMERA_DEPTH_IMAGE_TO_SKELETON_MULTIPLIER_320x240 * fSkeletonZ;
642  FLOAT fSkeletonY = -(lDepthY - height/2.0f) * (240.0f/height) * NUI_CAMERA_DEPTH_IMAGE_TO_SKELETON_MULTIPLIER_320x240 * fSkeletonZ;
643 
644  //
645  // Return the result as a vector.
646  //
647 
648  Vector4 v4;
649  v4.x = fSkeletonX;
650  v4.y = fSkeletonY;
651  v4.z = fSkeletonZ;
652  v4.w = 1.0f;
653  return v4;
654 }
655 
668 inline
669 Vector4
670 NuiTransformDepthImageToSkeleton(
671  _In_ LONG lDepthX,
672  _In_ LONG lDepthY,
673  _In_ USHORT usDepthValue
674  )
675 {
676  return NuiTransformDepthImageToSkeleton(lDepthX, lDepthY, usDepthValue, NUI_IMAGE_RESOLUTION_320x240);
677 }
678 
679 #endif
680 
681 #ifdef __cplusplus
682 extern "C" {
683 #endif
684 
719 _Check_return_ HRESULT NUIAPI NuiTransformSmooth(
720  _Inout_ NUI_SKELETON_FRAME *pSkeletonFrame,
721  _In_opt_ CONST NUI_TRANSFORM_SMOOTH_PARAMETERS *pSmoothingParams
722  );
723 
763 _Check_return_ STDMETHODIMP
765  _In_ const NUI_SKELETON_DATA *pSkeletonData,
766  _Out_cap_c_(NUI_SKELETON_POSITION_COUNT) NUI_SKELETON_BONE_ORIENTATION *pBoneOrientations);
767 
768 #ifdef __cplusplus
769 } //close extern "C"
770 #endif
771 
772 
struct _NUI_SKELETON_BONE_ORIENTATION NUI_SKELETON_BONE_ORIENTATION
FLOAT M12
Definition: NuiSensor.h:143
Vector4 vNormalToGravity
Definition: NuiSensor.h:333
FLOAT z
Definition: NuiSensor.h:133
NUI_SKELETON_BONE_ROTATION hierarchicalRotation
Definition: NuiSensor.h:355
struct _NUI_SKELETON_BONE_ROTATION NUI_SKELETON_BONE_ROTATION
#define NULL
Definition: kaydara.h:169
enum _NUI_SKELETON_POSITION_INDEX NUI_SKELETON_POSITION_INDEX
_Check_return_ STDMETHODIMP NuiSkeletonCalculateBoneOrientations(_In_ const NUI_SKELETON_DATA *pSkeletonData, _Out_cap_c_(NUI_SKELETON_POSITION_COUNT) NUI_SKELETON_BONE_ORIENTATION *pBoneOrientations)
Calculate bone orientations for a skeleton.
NUI_SKELETON_POSITION_INDEX startJoint
Definition: NuiSensor.h:354
FLOAT M24
Definition: NuiSensor.h:149
FLOAT y
Definition: NuiSensor.h:132
NUI_SKELETON_TRACKING_STATE eTrackingState
Definition: NuiSensor.h:312
FLOAT x
Definition: NuiSensor.h:131
struct _Matrix4 Matrix4
FLOAT M32
Definition: NuiSensor.h:151
FLOAT M33
Definition: NuiSensor.h:152
struct _NUI_TRANSFORM_SMOOTH_PARAMETERS NUI_TRANSFORM_SMOOTH_PARAMETERS
NUI_SKELETON_DATA SkeletonData[6]
Definition: NuiSensor.h:334
FLOAT w
Definition: NuiSensor.h:134
_Check_return_ HRESULT NUIAPI NuiSkeletonSetTrackedSkeletons(_In_count_c_(2) DWORD TrackingIDs[2])
Sets an array of IDs for skeletal tracking.
FLOAT M11
Definition: NuiSensor.h:142
#define NUIAPI
Definition: NuiApi.h:13
FLOAT M13
Definition: NuiSensor.h:144
DWORD dwEnrollmentIndex
Definition: NuiSensor.h:314
FLOAT M14
Definition: NuiSensor.h:145
NUI_SKELETON_POSITION_TRACKING_STATE eSkeletonPositionTrackingState[20]
Definition: NuiSensor.h:318
_Check_return_ HRESULT NUIAPI NuiTransformSmooth(_Inout_ NUI_SKELETON_FRAME *pSkeletonFrame, _In_opt_ CONST NUI_TRANSFORM_SMOOTH_PARAMETERS *pSmoothingParams)
Filters skeleton positions to reduce jitter between frames.
FLOAT M22
Definition: NuiSensor.h:147
FLOAT M23
Definition: NuiSensor.h:148
FLOAT M21
Definition: NuiSensor.h:146
NUI_SKELETON_POSITION_INDEX endJoint
Definition: NuiSensor.h:353
enum _NUI_SKELETON_TRACKING_STATE NUI_SKELETON_TRACKING_STATE
void NuiImageResolutionToSize(_In_ NUI_IMAGE_RESOLUTION res, _Out_ DWORD &refWidth, _Out_ DWORD &refHeight)
Converts the resolution to a size.
FLOAT M34
Definition: NuiSensor.h:153
Vector4 SkeletonPositions[20]
Definition: NuiSensor.h:317
FLOAT M31
Definition: NuiSensor.h:150
enum _NUI_SKELETON_POSITION_TRACKING_STATE NUI_SKELETON_POSITION_TRACKING_STATE
_Check_return_ HRESULT NUIAPI NuiSkeletonTrackingEnable(_In_opt_ HANDLE hNextFrameEvent, _In_ DWORD dwFlags)
Enables skeleton tracking.
FLOAT M42
Definition: NuiSensor.h:155
_NUI_SKELETON_POSITION_TRACKING_STATE
Definition: NuiSensor.h:290
enum _NUI_SKELETON_POSITION_INDEX NUI_SKELETON_POSITION_INDEX
enum _NUI_SKELETON_POSITION_TRACKING_STATE NUI_SKELETON_POSITION_TRACKING_STATE
struct _Vector4 Vector4
enum _NUI_IMAGE_RESOLUTION NUI_IMAGE_RESOLUTION
struct _NUI_SKELETON_FRAME NUI_SKELETON_FRAME
#define NUI_SKELETON_MAX_TRACKED_COUNT
Definition: NuiSkeleton.h:116
LARGE_INTEGER liTimeStamp
Definition: NuiSensor.h:329
#define FLT_EPSILON
Definition: NuiSkeleton.h:20
#define NUI_SKELETON_COUNT
Definition: NuiSkeleton.h:108
_Check_return_ HRESULT NUIAPI NuiSkeletonTrackingDisable()
Disables skeleton tracking.
_NUI_SKELETON_POSITION_INDEX
Definition: NuiSensor.h:184
_Check_return_ HRESULT NUIAPI NuiSkeletonGetNextFrame(_In_ DWORD dwMillisecondsToWait, _Inout_ NUI_SKELETON_FRAME *pSkeletonFrame)
Gets the next frame of data from the skeleton stream.
Vector4 vFloorClipPlane
Definition: NuiSensor.h:332
NUI_SKELETON_BONE_ROTATION absoluteRotation
Definition: NuiSensor.h:356
FLOAT M43
Definition: NuiSensor.h:156
enum _NUI_SKELETON_TRACKING_STATE NUI_SKELETON_TRACKING_STATE
FLOAT M44
Definition: NuiSensor.h:157
__declspec(selectany) DWORD NuiSkeletonDataSize
_NUI_SKELETON_TRACKING_STATE
Definition: NuiSensor.h:300
struct _NUI_SKELETON_DATA NUI_SKELETON_DATA
FLOAT M41
Definition: NuiSensor.h:154