-
Notifications
You must be signed in to change notification settings - Fork 154
/
AppMain.cpp
293 lines (244 loc) · 8.95 KB
/
AppMain.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
#include "pch.h"
#include "AppMain.h"
namespace ComputeOnDevice
{
AppMain::AppMain(
const std::shared_ptr<Graphics::DeviceResources>& deviceResources)
: Holographic::AppMainBase(deviceResources)
, _selectedHoloLensMediaFrameSourceGroupType(
HoloLensForCV::MediaFrameSourceGroupType::PhotoVideoCamera)
, _holoLensMediaFrameSourceGroupStarted(false)
, _undistortMapsInitialized(false)
, _isActiveRenderer(false)
{
}
void AppMain::OnHolographicSpaceChanged(
Windows::Graphics::Holographic::HolographicSpace^ holographicSpace)
{
//
// Initialize the HoloLens media frame readers
//
StartHoloLensMediaFrameSourceGroup();
}
void AppMain::OnSpatialInput(
_In_ Windows::UI::Input::Spatial::SpatialInteractionSourceState^ pointerState)
{
Windows::Perception::Spatial::SpatialCoordinateSystem^ currentCoordinateSystem =
_spatialPerception->GetOriginFrameOfReference()->CoordinateSystem;
if (!_isActiveRenderer)
{
_currentSlateRenderer =
std::make_shared<Rendering::SlateRenderer>(
_deviceResources);
_slateRendererList.push_back(_currentSlateRenderer);
// When a Pressed gesture is detected, the sample hologram will be repositioned
// two meters in front of the user.
_currentSlateRenderer->PositionHologram(
pointerState->TryGetPointerPose(currentCoordinateSystem));
_isActiveRenderer = true;
}
else
{
// Freeze frame
_visualizationTextureList.push_back(_currentVisualizationTexture);
_currentVisualizationTexture = nullptr;
_isActiveRenderer = false;
}
}
void AppMain::OnUpdate(
_In_ Windows::Graphics::Holographic::HolographicFrame^ holographicFrame,
_In_ const Graphics::StepTimer& stepTimer)
{
UNREFERENCED_PARAMETER(holographicFrame);
dbg::TimerGuard timerGuard(
L"AppMain::OnUpdate",
30.0 /* minimum_time_elapsed_in_milliseconds */);
//
// Update scene objects.
//
// Put time-based updates here. By default this code will run once per frame,
// but if you change the StepTimer to use a fixed time step this code will
// run as many times as needed to get to the current step.
//
for (auto& r : _slateRendererList)
{
r->Update(
stepTimer);
}
//
// Process sensor data received through the HoloLensForCV component.
//
if (!_holoLensMediaFrameSourceGroupStarted)
{
return;
}
HoloLensForCV::SensorFrame^ latestFrame;
latestFrame =
_holoLensMediaFrameSourceGroup->GetLatestSensorFrame(
HoloLensForCV::SensorType::PhotoVideo);
if (nullptr == latestFrame)
{
return;
}
if (_latestSelectedCameraTimestamp.UniversalTime == latestFrame->Timestamp.UniversalTime)
{
return;
}
_latestSelectedCameraTimestamp = latestFrame->Timestamp;
cv::Mat wrappedImage;
rmcv::WrapHoloLensSensorFrameWithCvMat(
latestFrame,
wrappedImage);
if (!_undistortMapsInitialized)
{
Windows::Media::Devices::Core::CameraIntrinsics^ cameraIntrinsics =
latestFrame->CoreCameraIntrinsics;
if (nullptr != cameraIntrinsics)
{
cv::Mat cameraMatrix(3, 3, CV_64FC1);
cv::setIdentity(cameraMatrix);
cameraMatrix.at<double>(0, 0) = cameraIntrinsics->FocalLength.x;
cameraMatrix.at<double>(1, 1) = cameraIntrinsics->FocalLength.y;
cameraMatrix.at<double>(0, 2) = cameraIntrinsics->PrincipalPoint.x;
cameraMatrix.at<double>(1, 2) = cameraIntrinsics->PrincipalPoint.y;
cv::Mat distCoeffs(5, 1, CV_64FC1);
distCoeffs.at<double>(0, 0) = cameraIntrinsics->RadialDistortion.x;
distCoeffs.at<double>(1, 0) = cameraIntrinsics->RadialDistortion.y;
distCoeffs.at<double>(2, 0) = cameraIntrinsics->TangentialDistortion.x;
distCoeffs.at<double>(3, 0) = cameraIntrinsics->TangentialDistortion.y;
distCoeffs.at<double>(4, 0) = cameraIntrinsics->RadialDistortion.z;
cv::initUndistortRectifyMap(
cameraMatrix,
distCoeffs,
cv::Mat_<double>::eye(3, 3) /* R */,
cameraMatrix,
cv::Size(wrappedImage.cols, wrappedImage.rows),
CV_32FC1 /* type */,
_undistortMap1,
_undistortMap2);
_undistortMapsInitialized = true;
}
}
if (_undistortMapsInitialized)
{
cv::remap(
wrappedImage,
_undistortedPVCameraImage,
_undistortMap1,
_undistortMap2,
cv::INTER_LINEAR);
cv::resize(
_undistortedPVCameraImage,
_resizedPVCameraImage,
cv::Size(),
0.5 /* fx */,
0.5 /* fy */,
cv::INTER_AREA);
}
else
{
cv::resize(
wrappedImage,
_resizedPVCameraImage,
cv::Size(),
0.5 /* fx */,
0.5 /* fy */,
cv::INTER_AREA);
}
cv::medianBlur(
_resizedPVCameraImage,
_blurredPVCameraImage,
3 /* ksize */);
cv::Canny(
_blurredPVCameraImage,
_cannyPVCameraImage,
50.0,
200.0);
for (int32_t y = 0; y < _blurredPVCameraImage.rows; ++y)
{
for (int32_t x = 0; x < _blurredPVCameraImage.cols; ++x)
{
if (_cannyPVCameraImage.at<uint8_t>(y, x) > 64)
{
*(_blurredPVCameraImage.ptr<uint32_t>(y, x)) = 0xFFFF00FF;
}
}
}
OpenCVHelpers::CreateOrUpdateTexture2D(
_deviceResources,
_blurredPVCameraImage,
_currentVisualizationTexture);
}
void AppMain::OnPreRender()
{
}
// Renders the current frame to each holographic camera, according to the
// current application and spatial positioning state.
void AppMain::OnRender()
{
// Draw the sample hologram.
for (size_t i = 0; i < _visualizationTextureList.size(); ++i)
{
_slateRendererList[i]->Render(
_visualizationTextureList[i]);
}
if (_isActiveRenderer)
{
_currentSlateRenderer->Render(_currentVisualizationTexture);
}
}
// Notifies classes that use Direct3D device resources that the device resources
// need to be released before this method returns.
void AppMain::OnDeviceLost()
{
for (auto& r : _slateRendererList)
{
r->ReleaseDeviceDependentResources();
}
_holoLensMediaFrameSourceGroup = nullptr;
_holoLensMediaFrameSourceGroupStarted = false;
for (auto& v : _visualizationTextureList)
{
v.reset();
}
_currentVisualizationTexture.reset();
}
// Notifies classes that use Direct3D device resources that the device resources
// may now be recreated.
void AppMain::OnDeviceRestored()
{
for (auto& r : _slateRendererList)
{
r->CreateDeviceDependentResources();
}
StartHoloLensMediaFrameSourceGroup();
}
void AppMain::StartHoloLensMediaFrameSourceGroup()
{
_sensorFrameStreamer =
ref new HoloLensForCV::SensorFrameStreamer();
_sensorFrameStreamer->EnableAll();
_holoLensMediaFrameSourceGroup =
ref new HoloLensForCV::MediaFrameSourceGroup(
_selectedHoloLensMediaFrameSourceGroupType,
_spatialPerception,
_sensorFrameStreamer);
_holoLensMediaFrameSourceGroup->Enable(
HoloLensForCV::SensorType::PhotoVideo);
concurrency::create_task(_holoLensMediaFrameSourceGroup->StartAsync()).then(
[&]()
{
_holoLensMediaFrameSourceGroupStarted = true;
});
}
}