forked from VIML/NIController
-
Notifications
You must be signed in to change notification settings - Fork 1
/
NIControl.cpp
191 lines (166 loc) · 5.42 KB
/
NIControl.cpp
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "NIControl.h"
QNIControl::QNIControl( QString sINIFile ) :
m_qSetting( sINIFile, QSettings::IniFormat ),
QWidget(), m_qScene(), m_qView( &m_qScene, this ), m_qLayout(this), m_mUserMap( m_niUserTracker )
{
m_qRect = QRectF( 0, 0, 640, 480 );
m_fJointConfidence = m_qSetting.value( "OpenNI/JointConfidence", 0.5f ).toFloat();
m_eControlHand = NICH_NO_HAND;
// configurate window
setAttribute(Qt::WA_NoSystemBackground, true);
setAttribute(Qt::WA_TranslucentBackground, true);
setAttribute(Qt::WA_PaintOnScreen);
// sub-widget
m_qLayout.setSpacing(0);
m_qLayout.setMargin(0);
m_qLayout.addWidget( &m_qView, 0, 0 );
m_qView.setStyleSheet( "background: transparent; border: 0px;" );
//m_qView.setBackgroundBrush(QBrush(qRgba( 255, 255, 255, 0 ), Qt::SolidPattern));
m_qView.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_qView.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_qView.installEventFilter( this );
// Scene
m_qScene.addItem( &m_mUserMap );
m_mUserMap.setZValue( 2 );
//m_pUserMap->setOpacity( 0.5 );
m_qScene.addItem( &m_mHandControl );
m_mHandControl.setZValue( 1 );
QONI_UserMap& rUMap = m_mUserMap;
m_mHandControl.m_funcStartInput = [&rUMap](){ rUMap.KeepSkeletonTransform( true ); };
m_mHandControl.m_funcEndInput = [&rUMap](){ rUMap.KeepSkeletonTransform( false ); };
m_mHandControl.m_fHandMoveThreshold = m_qSetting.value( "Control/MoveThreshold", 25 ).toFloat();
m_mHandControl.m_fHandForwardDistance = m_qSetting.value( "Control/ForwardDistance", 250 ).toFloat();
m_mHandControl.m_tdPreFixTime = boost::chrono::milliseconds( m_qSetting.value( "Control/PreFixTime", 100 ).toInt() );
m_mHandControl.m_tdFixTime = boost::chrono::milliseconds( m_qSetting.value( "Control/FixTime", 500 ).toInt() );
m_mHandControl.m_tdInvokeTime = boost::chrono::milliseconds( m_qSetting.value( "Control/InvokeTime", 200 ).toInt() );
SetFramless( false );
}
QNIControl::~QNIControl()
{
m_niUserTracker.destroy();
nite::NiTE::shutdown();
m_niDepthStream.destroy();
m_niDevice.close();
openni::OpenNI::shutdown();
}
bool QNIControl::InitialNIDevice( int w, int h )
{
#pragma region OpenNI
using namespace openni;
if( OpenNI::initialize() != STATUS_OK )
{
QMessageBox::critical( NULL, "OpenNI initialize error", OpenNI::getExtendedError() );
return false;
}
if( m_niDevice.open( ANY_DEVICE ) != STATUS_OK )
{
QMessageBox::critical( NULL, "Can't open OpenNI Device", OpenNI::getExtendedError() );
return false;
}
if( m_niDepthStream.create( m_niDevice, SENSOR_DEPTH ) != STATUS_OK )
{
QMessageBox::critical( NULL, "Can't create depth stream", OpenNI::getExtendedError() );
return false;
}
else
{
openni::VideoMode mMode;
mMode.setFps( 30 );
mMode.setResolution( w, h );
mMode.setPixelFormat( openni::PIXEL_FORMAT_DEPTH_1_MM );
m_niDepthStream.setVideoMode( mMode );
}
#pragma endregion
#pragma region NiTE
using namespace nite;
if( NiTE::initialize() != nite::STATUS_OK )
{
QMessageBox::critical( NULL, "NiTE", "NiTE initialize error" );
return false;
}
if( m_niUserTracker.create( &m_niDevice ) != nite::STATUS_OK )
{
QMessageBox::critical( NULL, "User Tracker", "UserTracker created failed" );
return false;
}
SetSkeletonSmoothing( m_qSetting.value( "OpenNI/SkeletonSmooth", 0.75f ).toFloat() );
#pragma endregion
resize( m_qRect.width(), m_qRect.height() );
m_mUserMap.SetSize( m_qRect.width(), m_qRect.height() );
m_mHandControl.SetRect( m_qRect );
return true;
}
void QNIControl::SetFramless( bool bTrue )
{
m_bFrameless = bTrue;
auto mPos = pos();
if( m_bFrameless )
{
setWindowOpacity(0.5);
setWindowFlags( Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint );
}
else
{
setWindowOpacity(1.0);
setWindowFlags( Qt::WindowStaysOnTopHint );
}
this->show();
this->move( mPos );
}
void QNIControl::timerEvent( QTimerEvent* pEvent )
{
if( m_mUserMap.Update() )
{
EControlHand eHandStatus = NICH_NO_HAND;
#pragma region select nearest hand
float fRC = m_mUserMap.GetActiveUserJoint( nite::JOINT_RIGHT_HAND ).getPositionConfidence(),
fLC = m_mUserMap.GetActiveUserJoint( nite::JOINT_LEFT_HAND ).getPositionConfidence();
if( fRC > m_fJointConfidence )
{
if( fLC > m_fJointConfidence )
{
QVector3D posR = m_mUserMap.GetActiveUserJointTR( nite::JOINT_RIGHT_HAND ),
posL = m_mUserMap.GetActiveUserJointTR( nite::JOINT_LEFT_HAND );
if( posR.z() > posL.z() )
eHandStatus = NICH_LEFT_HAND;
else
eHandStatus = NICH_RIGHT_HAND;
}
else
{
eHandStatus = NICH_RIGHT_HAND;
}
}
else if( fLC > m_fJointConfidence )
{
eHandStatus = NICH_LEFT_HAND;
}
#pragma endregion
if( eHandStatus == NICH_NO_HAND || eHandStatus != m_eControlHand )
{
m_mHandControl.HandLost();
m_eControlHand = eHandStatus;
}
if( m_eControlHand != NICH_NO_HAND )
{
#pragma region General Hand position process
// get hand info
QVector3D mHandPos3D;
QPointF mHandPos2D;
if( eHandStatus == NICH_RIGHT_HAND )
{
mHandPos3D = m_mUserMap.GetActiveUserJointTR( nite::JOINT_RIGHT_HAND );
mHandPos2D = m_mUserMap.GetActiveUserJoint2D( nite::JOINT_RIGHT_HAND );
}
else
{
mHandPos3D = m_mUserMap.GetActiveUserJointTR( nite::JOINT_LEFT_HAND );
mHandPos2D = m_mUserMap.GetActiveUserJoint2D( nite::JOINT_LEFT_HAND );
}
// add current position into track list
m_mHandControl.UpdateHandPoint( mHandPos2D, mHandPos3D );
#pragma endregion
}
}
m_qView.fitInView( m_qRect, Qt::KeepAspectRatio );
}