-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathOpenNI2Interface.h
189 lines (146 loc) · 4.58 KB
/
OpenNI2Interface.h
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
#pragma once
#ifdef WITH_OPENNI2
#include <string>
#include <iostream>
#include <algorithm>
#include <map>
#include <atomic>
#include <OpenNI.h>
#include "CameraInterface.h"
typedef struct { uint8_t r,g,b; } ColorType;
template<typename T>
void copyWithReversedRows(void *destination,const void* source,int height,int width)
{
// rowwise
T *dst = (T*)destination;
const T* src = (const T*)source;
for(int i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
dst[width*i+(width-j-1)] = src[width*i+j];
}
}
}
class OpenNI2Interface : public CameraInterface
{
public:
OpenNI2Interface(bool flipRows = false,int depthWidth = 640,int depthHeight = 480,
int rgbWidth = 640, int rgbHeight = 480,int fps = 30);
virtual ~OpenNI2Interface();
virtual bool ok()
{
return initSuccessful;
}
virtual std::string error()
{
return errorText;
}
virtual float depthScale()
{
return 0.001f;
}
class RGBCallback : public openni::VideoStream::NewFrameListener
{
public:
RGBCallback(int64_t & lastRgbTime,
std::atomic<int> & latestRgbIndex,
std::pair<uint8_t *,int64_t> * rgbBuffers,
bool flipRows)
: lastRgbTime(lastRgbTime),
latestRgbIndex(latestRgbIndex),
rgbBuffers(rgbBuffers),
flipRows(flipRows)
{
}
virtual ~RGBCallback() {}
void onNewFrame(openni::VideoStream& stream)
{
stream.readFrame(&frame);
lastRgbTime = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
int bufferIndex = (latestRgbIndex + 1) % numBuffers;
if(flipRows)
copyWithReversedRows<ColorType>(rgbBuffers[bufferIndex].first,frame.getData(),
frame.getHeight(),frame.getWidth());
else
memcpy(rgbBuffers[bufferIndex].first,frame.getData(),frame.getHeight() * frame.getWidth() * 3);
rgbBuffers[bufferIndex].second = lastRgbTime;
latestRgbIndex++;
}
private:
openni::VideoFrameRef frame;
int64_t & lastRgbTime;
std::atomic<int> & latestRgbIndex;
std::pair<uint8_t *,int64_t> * rgbBuffers;
bool flipRows;
};
class DepthCallback : public openni::VideoStream::NewFrameListener
{
public:
DepthCallback(int64_t & lastDepthTime,
std::atomic<int> & latestDepthIndex,
std::atomic<int> & latestRgbIndex,
std::pair<uint8_t *,int64_t> * rgbBuffers,
std::pair<std::pair<uint8_t *,uint8_t *>,int64_t> * frameBuffers,
bool flipRows,
int rgbPixels)
: lastDepthTime(lastDepthTime),
latestDepthIndex(latestDepthIndex),
latestRgbIndex(latestRgbIndex),
rgbBuffers(rgbBuffers),
frameBuffers(frameBuffers),
flipRows(flipRows),
rgbPixels(rgbPixels)
{
}
virtual ~DepthCallback() {}
void onNewFrame(openni::VideoStream& stream)
{
stream.readFrame(&frame);
lastDepthTime = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
int bufferIndex = (latestDepthIndex + 1) % numBuffers;
if(flipRows)
copyWithReversedRows<uint16_t>(frameBuffers[bufferIndex].first.first,frame.getData(),
frame.getHeight(),frame.getWidth());
else
memcpy(frameBuffers[bufferIndex].first.first,frame.getData(),frame.getHeight()*frame.getWidth()*2);
frameBuffers[bufferIndex].second = lastDepthTime;
int lastImageVal = latestRgbIndex;
if(lastImageVal == -1)
{
return;
}
lastImageVal %= numBuffers;
memcpy(frameBuffers[bufferIndex].first.second,rgbBuffers[lastImageVal].first,rgbPixels * 3);
latestDepthIndex++;
}
private:
openni::VideoFrameRef frame;
int64_t & lastDepthTime;
std::atomic<int> & latestDepthIndex;
std::atomic<int> & latestRgbIndex;
std::pair<uint8_t *,int64_t> * rgbBuffers;
std::pair<std::pair<uint8_t *,uint8_t *>,int64_t> * frameBuffers;
bool flipRows;
int rgbPixels;
};
private:
bool isModeSupported(const openni::VideoStream& stream,const openni::VideoMode& mode);
void printModes(const openni::VideoStream& stream,const openni::VideoMode& requestedMode);
void printMode(const openni::VideoMode& mode);
const bool flipRows;
openni::Device device;
openni::VideoStream depthStream;
openni::VideoStream rgbStream;
RGBCallback * rgbCallback;
DepthCallback * depthCallback;
bool initSuccessful;
std::string errorText;
std::atomic<int> latestRgbIndex;
std::pair<uint8_t *,int64_t> rgbBuffers[numBuffers];
int64_t lastRgbTime;
int64_t lastDepthTime;
};
#endif