File: DefaultTrackingInitializer.cpp

package info (click to toggle)
openni 1.5.4.0%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 45,208 kB
  • sloc: cpp: 116,706; ansic: 58,794; sh: 10,287; cs: 7,698; java: 7,402; python: 1,544; makefile: 492; xml: 167
file content (171 lines) | stat: -rw-r--r-- 7,325 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/****************************************************************************
*                                                                           *
*  OpenNI 1.x Alpha                                                         *
*  Copyright (C) 2011 PrimeSense Ltd.                                       *
*                                                                           *
*  This file is part of OpenNI.                                             *
*                                                                           *
*  OpenNI is free software: you can redistribute it and/or modify           *
*  it under the terms of the GNU Lesser General Public License as published *
*  by the Free Software Foundation, either version 3 of the License, or     *
*  (at your option) any later version.                                      *
*                                                                           *
*  OpenNI is distributed in the hope that it will be useful,                *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the             *
*  GNU Lesser General Public License for more details.                      *
*                                                                           *
*  You should have received a copy of the GNU Lesser General Public License *
*  along with OpenNI. If not, see <http://www.gnu.org/licenses/>.           *
*                                                                           *
****************************************************************************/
//---------------------------------------------------------------------------
// Includes
//---------------------------------------------------------------------------
#include "DefaultTrackingInitializer.h"

DefaultTrackingInitializer::DefaultTrackingInitializer(xn::UserGenerator *pUserGenerator) :
                                                          TrackingInitializer(pUserGenerator),
                                                          m_hCalibrationStartCallback(NULL),
                                                          m_hCalibrationCompleteCallback(NULL),
                                                          m_hInProgressCallback(NULL)
{
    if(m_pUserGenerator->IsCapabilitySupported(XN_CAPABILITY_SKELETON)==FALSE)
    {
        m_bValid=FALSE;
        return; // we need a skeleton, otherwise, we don't have calibration
    }

    XnStatus res=m_pUserGenerator->GetSkeletonCap().RegisterToCalibrationStart(CalibrationStartCallback,this,m_hCalibrationStartCallback);
    if(res!=XN_STATUS_OK)
    {
        m_hCalibrationStartCallback=NULL;
        m_bValid=FALSE;
        return; // if we can't register to the calibration, we shouldn't do anything
    }
    res=m_pUserGenerator->GetSkeletonCap().RegisterToCalibrationComplete(CalibrationCompleteCallback,this,m_hCalibrationStartCallback);
    if(res!=XN_STATUS_OK)
    {
        m_hCalibrationStartCallback=NULL;
        m_bValid=FALSE;
        return; // if we can't register to the calibration, we shouldn't do anything
    }
    res=m_pUserGenerator->GetSkeletonCap().RegisterToCalibrationInProgress(CalibrationInProgressCallback,this,m_hInProgressCallback);
    if(res!=XN_STATUS_OK)
    {
        m_hInProgressCallback=NULL;
        m_bValid=FALSE;
        return; // if we can't register to the calibration, we shouldn't do anything
    }
}

DefaultTrackingInitializer::~DefaultTrackingInitializer()
{
    // clean up
    if(m_pUserGenerator==NULL)
        return;
    if(m_hCalibrationStartCallback!=NULL)
    {
        m_pUserGenerator->GetSkeletonCap().UnregisterFromCalibrationStart(m_hCalibrationStartCallback);
        m_hCalibrationStartCallback=NULL;
    }

    if(m_hCalibrationCompleteCallback!=NULL)
    {
        m_pUserGenerator->GetSkeletonCap().UnregisterFromCalibrationComplete(m_hCalibrationCompleteCallback);
        m_hCalibrationCompleteCallback=NULL;
    }
    if(m_hInProgressCallback!=NULL)
    {
        m_pUserGenerator->GetSkeletonCap().UnregisterFromCalibrationInProgress(m_hInProgressCallback);
        m_hInProgressCallback=NULL;
    }
}



XnStatus DefaultTrackingInitializer::StartTracking(XnUserID nUserId, XnBool bForce)
{
    if(m_pUserGenerator->GetSkeletonCap().IsTracking(nUserId)==TRUE)
    {
        // we don't need to do anything (other than notifying the user selector) as we are 
        // already tracking
        if(m_pUserSelector!=NULL)
        {
            return m_pUserSelector->UpdateUserTracking(nUserId,TRUE,0); // already tracking
        }
        else
            return XN_STATUS_OK;
        
    }
    // request calibration.
    return m_pUserGenerator->GetSkeletonCap().RequestCalibration(nUserId,bForce);
}


XnStatus DefaultTrackingInitializer::AbortTracking(XnUserID nUserId)
{
    // note: the assumption is that this will not be called if not either calibrating or
    // tracking (although it will just fail if neither).
    if(m_pUserGenerator->GetSkeletonCap().IsTracking(nUserId)==TRUE)
    {
        // if we are tracking we just need to stop tracking
        XnStatus res=m_pUserGenerator->GetSkeletonCap().StopTracking(nUserId);
        return res;
    }
    // if we are not tracking we need to abort the calibration.
    return m_pUserGenerator->GetSkeletonCap().AbortCalibration(nUserId);
}

XnStatus DefaultTrackingInitializer::CalibrationStart(XnUserID /*nUserId*/)
{
    return XN_STATUS_OK;
}
XnStatus DefaultTrackingInitializer::CalibrationComplete(XnUserID nUserId,XnCalibrationStatus eStatus)
{
    XnBool retVal=FALSE;
    if (eStatus == XN_CALIBRATION_STATUS_OK)
    {
        // we start the actual tracking!
        if(m_pUserGenerator->GetSkeletonCap().StartTracking(nUserId)==XN_STATUS_OK) 
        {
            retVal=TRUE;
        }
        
    }
    if(m_pUserSelector!=NULL)
    {
        return m_pUserSelector->UpdateUserTracking(nUserId,retVal,eStatus);
    }
    return XN_STATUS_OK;
}

XnStatus DefaultTrackingInitializer::CalibrationInProgress(XnUserID nUserId,XnCalibrationStatus eStatus)
{
    if(m_pUserSelector!=NULL)
        return m_pUserSelector->UpdateUserTrackingProgress(nUserId,eStatus);
    return XN_STATUS_OK;
}




// Callback: Started calibration
void XN_CALLBACK_TYPE DefaultTrackingInitializer::CalibrationStartCallback(xn::SkeletonCapability& /*capability*/, XnUserID nUserId, void* pCookie)
{
    DefaultTrackingInitializer *pDefaultTrackingInitializer=(DefaultTrackingInitializer *)pCookie;
    pDefaultTrackingInitializer->CalibrationStart(nUserId);
}

void XN_CALLBACK_TYPE DefaultTrackingInitializer::CalibrationCompleteCallback(xn::SkeletonCapability& /*capability*/, XnUserID nUserId, XnCalibrationStatus eStatus, void* pCookie)
{
    DefaultTrackingInitializer *pDefaultTrackingInitializer=(DefaultTrackingInitializer *)pCookie;
    pDefaultTrackingInitializer->CalibrationComplete(nUserId,eStatus);
}

void XN_CALLBACK_TYPE DefaultTrackingInitializer::CalibrationInProgressCallback(xn::SkeletonCapability& /*capability*/, XnUserID nUserId, XnCalibrationStatus eStatus, void* pCookie)
{
    DefaultTrackingInitializer *pDefaultTrackingInitializer=(DefaultTrackingInitializer *)pCookie;
    pDefaultTrackingInitializer->CalibrationInProgress(nUserId,eStatus);
}