-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathRecord3DUnityStreaming.cpp
More file actions
executable file
·147 lines (117 loc) · 5.27 KB
/
Record3DUnityStreaming.cpp
File metadata and controls
executable file
·147 lines (117 loc) · 5.27 KB
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
#include "include/record3d_unity_streaming/Record3DUnityStreaming.h"
#include <iostream>
#include <record3d/Record3DStructs.h>
#include <record3d/Record3DStream.h>
#include <cmath>
FrameMetadata GetFrameMetadata()
{
FrameMetadata fmd;
fmd.numComponentsPerPositionTexturePixel = 4;
fmd.numComponentsPerColorTexturePixel = 3;
return fmd;
}
DeviceHandlesInfo ListAllDeviceHandles()
{
auto connectedDevices = Record3D::Record3DStream::GetConnectedDevices();
auto* connectedDevicesHandles = (int32_t*) malloc(connectedDevices.size() * sizeof(int32_t));
for (int i = 0; i < connectedDevices.size(); i++)
{
connectedDevicesHandles[i] = static_cast<int32_t>(connectedDevices[i].handle);
}
DeviceHandlesInfo info;
info.arr_ptr = connectedDevicesHandles;
info.arr_size = static_cast<uint32_t>(connectedDevices.size());
return info;
}
void FinishDeviceInfoHandling(DeviceHandlesInfo $devInfo)
{
free($devInfo.arr_ptr);
}
static float InterpolateDepth(const float* $depthData, float $x, float $y, int $imgWidth, int $imgHeight)
{
int wX = static_cast<int>($x);
int wY = static_cast<int>($y);
float fracX = $x - static_cast<float>(wX);
float fracY = $y - static_cast<float>(wY);
int topLeftIdx = wY * $imgWidth + wX;
int topRightIdx = wY * $imgWidth + std::min(wX, $imgWidth - 1);
int bottomLeftIdx = std::min(wY, $imgHeight - 1) * $imgWidth + wX;
int bottomRightIdx = std::min(wY, $imgHeight - 1) * $imgWidth + std::min(wX, $imgWidth - 1);
float interpVal =
($depthData[topLeftIdx] * (1.0f - fracX) + fracX * $depthData[topRightIdx]) * (1.0f - fracY) +
($depthData[bottomLeftIdx] * (1.0f - fracX) + fracX * $depthData[bottomRightIdx]) * fracY;
return interpVal;
}
bool StartStreaming(Record3DDevice $deviceHandle, OnNewFrameCallback $newFrameCallback, OnStreamStoppedCallback $streamStoppedCallback)
{
auto* stream = new Record3D::Record3DStream{};
constexpr int numComponentsPerPointPosition = 4;
size_t positionsBufferSize = 0;
float* positionsBuffer = nullptr;
stream->onNewFrame = [=](const Record3D::BufferRGB &$rgbFrame,
const Record3D::BufferDepth &$depthFrame,
const Record3D::BufferConfidence &$confFrame,
const Record3D::BufferMisc &$miscData,
uint32_t $rgbWidth,
uint32_t $rgbHeight,
uint32_t $depthWidth,
uint32_t $depthHeight,
uint32_t $confWidth,
uint32_t $confHeight,
Record3D::DeviceType $deviceType,
Record3D::IntrinsicMatrixCoeffs $K,
Record3D::CameraPose $cameraPose ) mutable
{
size_t currPositionsBufferSize = $rgbWidth * $rgbHeight * numComponentsPerPointPosition;
if ( positionsBufferSize < currPositionsBufferSize )
{
if ( positionsBuffer != nullptr )
{
delete[] positionsBuffer;
}
positionsBuffer = new float[currPositionsBufferSize];
positionsBufferSize = currPositionsBufferSize;
}
float ifx = 1.0f / $K.fx;
float ify = 1.0f / $K.fy;
float itx = -$K.tx / $K.fx;
float ity = -$K.ty / $K.fy;
bool needToInterpolate = $rgbWidth != $depthWidth || $rgbHeight != $depthHeight;
float invRGBWidth = 1.0f / $rgbWidth;
float invRGBHeight = 1.0f / $rgbHeight;
const float* depthDataPtr = (float*) $depthFrame.data();
for (int i = 0; i < $rgbHeight; i++)
for ( int j = 0; j < $rgbWidth; j++ )
{
int idx = $rgbWidth * i + j;
int posBuffIdx = numComponentsPerPointPosition * idx;
float depthX = invRGBWidth * $depthWidth * j;
float depthY = invRGBHeight * $depthHeight * i;
const float currDepth = needToInterpolate ? InterpolateDepth(depthDataPtr, depthX, depthY, $depthWidth, $depthHeight) : depthDataPtr[ idx ];
positionsBuffer[ posBuffIdx + 0 ] = (ifx * j + itx) * currDepth;
positionsBuffer[ posBuffIdx + 1 ] = -(ify * i + ity) * currDepth;
positionsBuffer[ posBuffIdx + 2 ] = -currDepth;
positionsBuffer[ posBuffIdx + 3 ] = idx;
}
constexpr int numRGBChannels = 3;
FrameInfo conf;
conf.depthFrameBufferPtr = (void*)positionsBuffer;
conf.rgbFrameBufferPtr = (void*)$rgbFrame.data();
conf.depthFrameBufferSize = static_cast<int32_t>($depthWidth * $depthHeight * sizeof(float) * 4);
conf.rgbFrameBufferSize = static_cast<int32_t>($rgbWidth * $rgbHeight * numRGBChannels * sizeof(uint8_t));
conf.frameWidth = static_cast<int32_t>($rgbWidth);
conf.frameHeight = static_cast<int32_t>($rgbHeight);
$newFrameCallback(conf);
};
stream->onStreamStopped = [=]() {
$streamStoppedCallback();
delete stream;
delete[] positionsBuffer;
};
Record3D::DeviceInfo dev;
dev.handle = static_cast<uint32_t>($deviceHandle.handle);
dev.udid = "";
dev.productId = 0;
bool connectionEstablished = stream->ConnectToDevice(dev);
return connectionEstablished;
}