forked from gazebosim/gz-sensors
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrgbd_camera.cc
More file actions
765 lines (669 loc) · 22.9 KB
/
rgbd_camera.cc
File metadata and controls
765 lines (669 loc) · 22.9 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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
/*
* Copyright (C) 2019 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <cstring>
#include <gtest/gtest.h>
#include <gz/msgs/camera_info.pb.h>
#include <gz/msgs/image.pb.h>
#include <gz/msgs/Utility.hh>
#include <gz/common/Filesystem.hh>
#include <gz/common/Event.hh>
#include <gz/sensors/Manager.hh>
#include <gz/sensors/RgbdCameraSensor.hh>
// TODO(louise) Remove these pragmas once gz-rendering is disabling the
// warnings
#ifdef _WIN32
#pragma warning(push)
#pragma warning(disable: 4251)
#endif
#include <gz/rendering/Material.hh>
#include <gz/rendering/RenderEngine.hh>
#include <gz/rendering/RenderingIface.hh>
#include <gz/rendering/Scene.hh>
#include <gz/rendering/Visual.hh>
#ifdef _WIN32
#pragma warning(pop)
#endif
#include "test_config.hh" // NOLINT(build/include)
#include "TransportTestTools.hh"
#include "PointCloudUtil.hh"
#define DEPTH_TOL 1e-4
#define DOUBLE_TOL 1e-6
using namespace gz;
std::mutex g_mutex;
unsigned int g_depthCounter = 0;
float *g_depthBuffer = nullptr;
std::mutex g_imgMutex;
unsigned int g_imgCounter = 0;
unsigned char *g_imgBuffer = nullptr;
std::mutex g_pcMutex;
unsigned int g_pcCounter = 0;
float *g_pointsXYZBuffer = nullptr;
unsigned char *g_pointsRGBBuffer = nullptr;
std::mutex g_infoMutex;
unsigned int g_infoCounter = 0;
msgs::CameraInfo g_infoMsg;
void UnpackPointCloudMsg(const msgs::PointCloudPacked &_msg,
float *_xyzBuffer, unsigned char *_rgbBuffer)
{
std::string msgBuffer = _msg.data();
char *msgBufferIndex = msgBuffer.data();
for (uint32_t j = 0; j < _msg.height(); ++j)
{
for (uint32_t i = 0; i < _msg.width(); ++i)
{
int fieldIndex = 0;
int pointIndex = j*_msg.width()*3 + i*3;
_xyzBuffer[pointIndex] = *reinterpret_cast<float *>(
msgBufferIndex + _msg.field(fieldIndex++).offset());
_xyzBuffer[pointIndex + 1] = *reinterpret_cast<float *>(
msgBufferIndex + _msg.field(fieldIndex++).offset());
_xyzBuffer[pointIndex + 2] = *reinterpret_cast<float *>(
msgBufferIndex + _msg.field(fieldIndex++).offset());
int fieldOffset = _msg.field(fieldIndex).offset();
if (_msg.is_bigendian())
{
_rgbBuffer[pointIndex] = *(msgBufferIndex + fieldOffset + 0);
_rgbBuffer[pointIndex + 1] = *(msgBufferIndex + fieldOffset + 1);
_rgbBuffer[pointIndex + 2] = *(msgBufferIndex + fieldOffset + 2);
}
else
{
_rgbBuffer[pointIndex] = *(msgBufferIndex + fieldOffset + 2);
_rgbBuffer[pointIndex + 1] = *(msgBufferIndex + fieldOffset + 1);
_rgbBuffer[pointIndex + 2] = *(msgBufferIndex + fieldOffset + 0);
}
msgBufferIndex += _msg.point_step();
}
}
}
void OnCameraInfo(const msgs::CameraInfo & _msg)
{
g_infoMutex.lock();
g_infoCounter++;
g_infoMsg.CopyFrom(_msg);
g_infoMutex.unlock();
}
void OnDepthImage(const msgs::Image &_msg)
{
g_mutex.lock();
unsigned int depthSamples = _msg.width() * _msg.height();
unsigned int depthBufferSize = depthSamples * sizeof(float);
if (!g_depthBuffer)
g_depthBuffer = new float[depthSamples];
memcpy(g_depthBuffer, _msg.data().c_str(), depthBufferSize);
g_depthCounter++;
g_mutex.unlock();
}
void OnImage(const msgs::Image &_msg)
{
g_imgMutex.lock();
unsigned int imgSize = _msg.width() * _msg.height() * 3;
if (!g_imgBuffer)
g_imgBuffer = new unsigned char[imgSize];
memcpy(g_imgBuffer, _msg.data().c_str(), imgSize);
g_imgCounter++;
g_imgMutex.unlock();
}
void OnPointCloud(const msgs::PointCloudPacked &_msg)
{
g_pcMutex.lock();
unsigned int pointCloudSamples = _msg.width() * _msg.height();
unsigned int pointCloudBufferSize = pointCloudSamples * 3;
if (!g_pointsXYZBuffer)
g_pointsXYZBuffer = new float[pointCloudBufferSize];
if (!g_pointsRGBBuffer)
g_pointsRGBBuffer = new unsigned char[pointCloudBufferSize];
UnpackPointCloudMsg(_msg, g_pointsXYZBuffer, g_pointsRGBBuffer);
g_pcCounter++;
g_pcMutex.unlock();
}
class RgbdCameraSensorTest: public testing::Test,
public testing::WithParamInterface<const char *>
{
// Documentation inherited
protected: void SetUp() override
{
// Disable Ogre tests on windows. See
// https://github.com/gazebosim/gz-sensors/issues/284
#ifdef _WIN32
if (strcmp(GetParam(), "ogre") == 0)
{
GTEST_SKIP() << "Ogre tests disabled on windows. See #284.";
}
#endif
}
// Create a Camera sensor from a SDF and gets a image message
public: void ImagesWithBuiltinSDF(const std::string &_renderEngine);
};
void RgbdCameraSensorTest::ImagesWithBuiltinSDF(
const std::string &_renderEngine)
{
// get the darn test data
std::string path = common::joinPaths(PROJECT_SOURCE_PATH, "test",
"sdf", "rgbd_camera_sensor_builtin.sdf");
sdf::SDFPtr doc(new sdf::SDF());
sdf::init(doc);
ASSERT_TRUE(sdf::readFile(path, doc));
ASSERT_NE(nullptr, doc->Root());
ASSERT_TRUE(doc->Root()->HasElement("model"));
auto modelPtr = doc->Root()->GetElement("model");
ASSERT_TRUE(modelPtr->HasElement("link"));
auto linkPtr = modelPtr->GetElement("link");
ASSERT_TRUE(linkPtr->HasElement("sensor"));
auto sensorPtr = linkPtr->GetElement("sensor");
ASSERT_TRUE(sensorPtr->HasElement("camera"));
auto cameraPtr = sensorPtr->GetElement("camera");
ASSERT_TRUE(cameraPtr->HasElement("image"));
auto imagePtr = cameraPtr->GetElement("image");
ASSERT_TRUE(cameraPtr->HasElement("clip"));
auto clipPtr = cameraPtr->GetElement("clip");
int imgWidth = imagePtr->Get<int>("width");
int imgHeight = imagePtr->Get<int>("height");
double far_ = clipPtr->Get<double>("far");
double near_ = clipPtr->Get<double>("near");
double unitBoxSize = 1.0;
math::Vector3d boxPosition(3.0, 0.0, 0.0);
// If ogre is not the engine, don't run the test
if ((_renderEngine.compare("ogre") != 0) &&
(_renderEngine.compare("ogre2") != 0))
{
gzdbg << "Engine '" << _renderEngine
<< "' doesn't support rgbd cameras" << std::endl;
return;
}
// Setup gz-rendering with an empty scene
auto *engine = rendering::engine(_renderEngine);
if (!engine)
{
gzdbg << "Engine '" << _renderEngine
<< "' is not supported" << std::endl;
return;
}
rendering::ScenePtr scene = engine->CreateScene("scene");
// Create an scene with a box in it
scene->SetAmbientLight(0.0, 0.0, 1.0);
rendering::VisualPtr root = scene->RootVisual();
// red background
scene->SetBackgroundColor(1.0, 0.0, 0.0);
// create blue material
rendering::MaterialPtr blue = scene->CreateMaterial();
blue->SetAmbient(0.0, 0.0, 1.0);
blue->SetDiffuse(0.0, 0.0, 1.0);
blue->SetSpecular(0.0, 0.0, 1.0);
// create box visual
rendering::VisualPtr box = scene->CreateVisual();
box->AddGeometry(scene->CreateBox());
box->SetOrigin(0.0, 0.0, 0.0);
box->SetLocalPosition(boxPosition);
box->SetLocalRotation(0, 0, 0);
box->SetLocalScale(unitBoxSize, unitBoxSize, unitBoxSize);
box->SetMaterial(blue);
scene->DestroyMaterial(blue);
root->AddChild(box);
// do the test
sensors::Manager mgr;
sensors::RgbdCameraSensor *rgbdSensor =
mgr.CreateSensor<sensors::RgbdCameraSensor>(sensorPtr);
ASSERT_NE(rgbdSensor, nullptr);
EXPECT_FALSE(rgbdSensor->HasConnections());
rgbdSensor->SetScene(scene);
EXPECT_EQ(rgbdSensor->ImageWidth(), static_cast<unsigned int>(imgWidth));
EXPECT_EQ(rgbdSensor->ImageHeight(), static_cast<unsigned int>(imgHeight));
std::string depthTopic =
"/test/integration/RgbdCameraPlugin_imagesWithBuiltinSDF/depth_image";
WaitForMessageTestHelper<msgs::Image> depthHelper(depthTopic);
EXPECT_TRUE(rgbdSensor->HasConnections());
std::string imageTopic =
"/test/integration/RgbdCameraPlugin_imagesWithBuiltinSDF/image";
WaitForMessageTestHelper<msgs::Image> imageHelper(imageTopic);
std::string pointsTopic =
"/test/integration/RgbdCameraPlugin_imagesWithBuiltinSDF/points";
WaitForMessageTestHelper<msgs::PointCloudPacked>
pointsHelper(pointsTopic);
std::string infoTopic =
"/test/integration/RgbdCameraPlugin_imagesWithBuiltinSDF/camera_info";
WaitForMessageTestHelper<msgs::CameraInfo> infoHelper(infoTopic);
// Update once to create image
mgr.RunOnce(std::chrono::steady_clock::duration::zero());
EXPECT_TRUE(depthHelper.WaitForMessage()) << depthHelper;
EXPECT_TRUE(imageHelper.WaitForMessage()) << imageHelper;
EXPECT_TRUE(pointsHelper.WaitForMessage()) << pointsHelper;
// subscribe to the depth camera topic
transport::Node node;
node.Subscribe(depthTopic, &OnDepthImage);
// subscribe to the image topic
node.Subscribe(imageTopic, &OnImage);
// subscribe to the point cloud topic
node.Subscribe(pointsTopic, &OnPointCloud);
// subscribe to the camera info topic
node.Subscribe(infoTopic, &OnCameraInfo);
// Update once more
mgr.RunOnce(std::chrono::steady_clock::duration::zero(), true);
// wait for an image
auto waitTime = std::chrono::duration_cast< std::chrono::milliseconds >(
std::chrono::duration< double >(0.001));
int counter = 0;
int infoCounter = 0;
int imgCounter = 0;
int pcCounter = 0;
msgs::CameraInfo infoMsg;
for (int sleep = 0; sleep < 300 &&
(counter == 0 || infoCounter == 0 || imgCounter == 0 || pcCounter == 0);
++sleep)
{
g_mutex.lock();
g_infoMutex.lock();
g_imgMutex.lock();
g_pcMutex.lock();
counter = g_depthCounter;
infoCounter = g_infoCounter;
imgCounter = g_imgCounter;
pcCounter = g_pcCounter;
infoMsg = g_infoMsg;
g_mutex.unlock();
g_infoMutex.unlock();
g_imgMutex.unlock();
g_pcMutex.unlock();
std::this_thread::sleep_for(waitTime);
}
g_mutex.lock();
g_infoMutex.lock();
g_pcMutex.lock();
g_imgMutex.lock();
g_depthCounter = 0;
g_infoCounter = 0;
g_imgCounter = 0;
g_pcCounter = 0;
EXPECT_GT(counter, 0);
EXPECT_EQ(counter, infoCounter);
EXPECT_EQ(counter, imgCounter);
EXPECT_EQ(counter, pcCounter);
counter = 0;
infoCounter = 0;
imgCounter = 0;
pcCounter = 0;
// depth image indices
int midWidth = static_cast<int>(rgbdSensor->ImageWidth() * 0.5);
int midHeight = static_cast<int>(rgbdSensor->ImageHeight() * 0.5);
int mid = midHeight * rgbdSensor->ImageWidth() + midWidth -1;
double expectedRangeAtMidPoint = boxPosition.X() - unitBoxSize * 0.5;
int left = midHeight * rgbdSensor->ImageWidth();
int right = (midHeight+1) * rgbdSensor->ImageWidth() - 1;
// xyz and rgb image indices
int imgChannelCount = 3;
int imgMid = midHeight * rgbdSensor->ImageWidth() * imgChannelCount
+ (midWidth-1) * imgChannelCount;
int imgLeft = midHeight * rgbdSensor->ImageWidth() * imgChannelCount;
int imgRight = (midHeight+1)
* (rgbdSensor->ImageWidth() * imgChannelCount)
- imgChannelCount;
// check depth
{
// Depth sensor should see box in the middle of the image
EXPECT_NEAR(g_depthBuffer[mid], expectedRangeAtMidPoint, DEPTH_TOL);
// The left and right side of the depth frame should be inf
EXPECT_DOUBLE_EQ(g_depthBuffer[left], math::INF_D);
EXPECT_DOUBLE_EQ(g_depthBuffer[right], math::INF_D);
}
// check color
{
unsigned int mr = g_imgBuffer[imgMid];
unsigned int mg = g_imgBuffer[imgMid + 1];
unsigned int mb = g_imgBuffer[imgMid + 2];
EXPECT_EQ(0u, mr);
EXPECT_EQ(0u, mg);
#ifndef __APPLE__
// See https://github.com/gazebosim/gz-sensors/issues/66
EXPECT_GT(mb, 0u);
#endif
unsigned int lr = g_imgBuffer[imgLeft];
unsigned int lg = g_imgBuffer[imgLeft + 1];
unsigned int lb = g_imgBuffer[imgLeft + 2];
EXPECT_GT(lr, 0u);
EXPECT_EQ(0u, lg);
EXPECT_EQ(0u, lb);
unsigned int rr = g_imgBuffer[imgRight];
unsigned int rg = g_imgBuffer[imgRight + 1];
unsigned int rb = g_imgBuffer[imgRight + 2];
EXPECT_GT(rr, 0u);
EXPECT_EQ(0u, rg);
EXPECT_EQ(0u, rb);
}
// check point cloud
{
// check mid point
float mx = g_pointsXYZBuffer[imgMid];
float my = g_pointsXYZBuffer[imgMid + 1];
float mz = g_pointsXYZBuffer[imgMid + 2];
EXPECT_FLOAT_EQ(g_depthBuffer[mid], mx);
// check left and right points
float lx = g_pointsXYZBuffer[imgLeft];
float ly = g_pointsXYZBuffer[imgLeft + 1];
float lz = g_pointsXYZBuffer[imgLeft + 2];
EXPECT_FLOAT_EQ(math::INF_D, lx);
EXPECT_FLOAT_EQ(math::INF_D, ly);
EXPECT_FLOAT_EQ(math::INF_D, lz);
float rx = g_pointsXYZBuffer[imgRight];
float ry = g_pointsXYZBuffer[imgRight + 1];
float rz = g_pointsXYZBuffer[imgRight + 2];
EXPECT_FLOAT_EQ(math::INF_D, rx);
EXPECT_FLOAT_EQ(math::INF_D, ry);
EXPECT_FLOAT_EQ(math::INF_D, rz);
// point to the left of mid point should have larger y value than mid
// point, which in turn should have large y value than point to the
// right of mid point
float midLeftY = g_pointsXYZBuffer[imgMid + 1 - imgChannelCount];
float midRightY = g_pointsXYZBuffer[imgMid + 1 + imgChannelCount];
EXPECT_GT(midLeftY, my);
EXPECT_GT(my, midRightY);
EXPECT_GT(midLeftY, 0.0);
EXPECT_LT(midRightY, 0.0);
// all points on the box should have the same z position
float midLeftZ = g_pointsXYZBuffer[imgMid + 2 - imgChannelCount];
float midRightZ = g_pointsXYZBuffer[imgMid + 2 + imgChannelCount];
EXPECT_NEAR(mz, midLeftZ, DEPTH_TOL);
EXPECT_NEAR(mz, midRightZ, DEPTH_TOL);
// Verify Point Cloud RGB values
// The mid point should be blue
unsigned int mr = g_pointsRGBBuffer[imgMid];
unsigned int mg = g_pointsRGBBuffer[imgMid + 1];
unsigned int mb = g_pointsRGBBuffer[imgMid + 2];
EXPECT_EQ(0u, mr);
EXPECT_EQ(0u, mg);
#ifndef __APPLE__
// See https://github.com/gazebosim/gz-sensors/issues/66
EXPECT_GT(mb, 0u);
#endif
// Far left and right points should be red (background color)
unsigned int lr = g_pointsRGBBuffer[imgLeft];
unsigned int lg = g_pointsRGBBuffer[imgLeft + 1];
unsigned int lb = g_pointsRGBBuffer[imgLeft + 2];
EXPECT_EQ(255u, lr);
EXPECT_EQ(0u, lg);
EXPECT_EQ(0u, lb);
unsigned int rr = g_pointsRGBBuffer[imgRight];
unsigned int rg = g_pointsRGBBuffer[imgRight + 1];
unsigned int rb = g_pointsRGBBuffer[imgRight + 2];
EXPECT_EQ(255u, rr);
EXPECT_EQ(0u, rg);
EXPECT_EQ(0u, rb);
}
// Use PointCloudUtil to convert depth data into point clouds and compare
// result against actual point cloud data
{
// init the point cloud msg to be filled
msgs::PointCloudPacked pointsMsg;
msgs::InitPointCloudPacked(pointsMsg, "depth2Image", false,
{{"xyz", msgs::PointCloudPacked::Field::FLOAT32},
{"rgb", msgs::PointCloudPacked::Field::FLOAT32}});
pointsMsg.set_width(rgbdSensor->ImageWidth());
pointsMsg.set_height(rgbdSensor->ImageHeight());
pointsMsg.set_row_step(pointsMsg.point_step() * rgbdSensor->ImageWidth());
// fill msg does the conversion from depth to points
sensors::PointCloudUtil pointsUtil;
pointsUtil.FillMsg(pointsMsg, 1.05, g_pointsRGBBuffer, g_depthBuffer);
// Unpack the point cloud msg into separate rgb and xyz buffers
unsigned char *rgbBuffer =
new unsigned char[pointsMsg.width() * pointsMsg.height() * 3];
float *xyzBuffer = new float[pointsMsg.width() * pointsMsg.height() * 3];
UnpackPointCloudMsg(pointsMsg, xyzBuffer, rgbBuffer);
for (unsigned int i = 0; i < pointsMsg.height(); ++i)
{
for (unsigned int j = 0; j < pointsMsg.width(); ++j)
{
int index = i*pointsMsg.width()*3 + j*3;
float x = g_pointsXYZBuffer[index];
float y = g_pointsXYZBuffer[index + 1];
float z = g_pointsXYZBuffer[index + 2];
float x2 = xyzBuffer[index];
float y2 = xyzBuffer[index + 1];
float z2 = xyzBuffer[index + 2];
// Add special check for inf case. Conversion from depth image to
// point cloud in the FillMsg call does not keep the sign of inf
if (std::isinf(x))
EXPECT_TRUE(std::isinf(x2));
else
EXPECT_NEAR(x2, x, DEPTH_TOL);
if (std::isinf(y))
EXPECT_TRUE(std::isinf(y2));
else
EXPECT_NEAR(y2, y, DEPTH_TOL);
if (std::isinf(z))
EXPECT_TRUE(std::isinf(z2));
else
EXPECT_NEAR(z2, z, DEPTH_TOL);
}
}
}
// Check camera info
EXPECT_TRUE(infoMsg.has_header());
ASSERT_EQ(1, infoMsg.header().data().size());
EXPECT_EQ("frame_id", infoMsg.header().data(0).key());
ASSERT_EQ(1, infoMsg.header().data(0).value().size());
EXPECT_EQ("camera1", infoMsg.header().data(0).value(0));
g_infoMutex.unlock();
g_mutex.unlock();
g_pcMutex.unlock();
g_imgMutex.unlock();
// Check that for a box really close it returns -inf
math::Vector3d boxPositionNear(
unitBoxSize * 0.5 + near_ * 0.5, 0.0, 0.0);
box->SetLocalPosition(boxPositionNear);
mgr.RunOnce(std::chrono::steady_clock::duration::zero(), true);
for (int sleep = 0; sleep < 300 &&
(counter == 0 || infoCounter == 0 || imgCounter == 0 || pcCounter == 0);
++sleep)
{
g_mutex.lock();
g_infoMutex.lock();
g_imgMutex.lock();
g_pcMutex.lock();
counter = g_depthCounter;
infoCounter = g_infoCounter;
imgCounter = g_imgCounter;
pcCounter = g_pcCounter;
infoMsg = g_infoMsg;
g_mutex.unlock();
g_infoMutex.unlock();
g_imgMutex.unlock();
g_pcMutex.unlock();
std::this_thread::sleep_for(waitTime);
}
g_mutex.lock();
g_infoMutex.lock();
g_pcMutex.lock();
g_imgMutex.lock();
g_depthCounter = 0;
g_infoCounter = 0;
g_imgCounter = 0;
g_pcCounter = 0;
EXPECT_GT(counter, 0);
EXPECT_EQ(counter, infoCounter);
EXPECT_EQ(counter, imgCounter);
EXPECT_EQ(counter, pcCounter);
counter = 0;
infoCounter = 0;
imgCounter = 0;
pcCounter = 0;
// check depth
{
for (unsigned int i = 0; i < rgbdSensor->ImageHeight(); ++i)
{
unsigned int step = i * rgbdSensor->ImageWidth();
for (unsigned int j = 0; j < rgbdSensor->ImageWidth(); ++j)
{
unsigned int index = step + j;
EXPECT_FLOAT_EQ(-math::INF_D, g_depthBuffer[index]);
}
}
}
// check color
{
for (unsigned int i = 0; i < rgbdSensor->ImageHeight(); ++i)
{
unsigned int step = i * rgbdSensor->ImageWidth() * 3;
for (unsigned int j = 0; j < rgbdSensor->ImageWidth(); ++j)
{
unsigned int index = step + j*3;
unsigned int r = g_imgBuffer[index];
unsigned int g = g_imgBuffer[index + 1];
unsigned int b = g_imgBuffer[index + 2];
EXPECT_GT(r, 0u);
EXPECT_EQ(0u, g);
EXPECT_EQ(0u, b);
}
}
}
// check point cloud xyz and rgb values
{
for (unsigned int i = 0; i < rgbdSensor->ImageHeight(); ++i)
{
unsigned int step = i * rgbdSensor->ImageWidth() * 3;
for (unsigned int j = 0; j < rgbdSensor->ImageWidth(); ++j)
{
unsigned int index = step + j*3;
float x = g_pointsXYZBuffer[index];
float y = g_pointsXYZBuffer[index + 1];
float z = g_pointsXYZBuffer[index + 2];
EXPECT_FLOAT_EQ(-math::INF_D, x);
EXPECT_FLOAT_EQ(-math::INF_D, y);
EXPECT_FLOAT_EQ(-math::INF_D, z);
unsigned int r = g_pointsRGBBuffer[index];
unsigned int g = g_pointsRGBBuffer[index + 1];
unsigned int b = g_pointsRGBBuffer[index + 2];
EXPECT_GT(r, 0u);
EXPECT_EQ(0u, g);
EXPECT_EQ(0u, b);
}
}
}
g_infoMutex.unlock();
g_mutex.unlock();
g_imgMutex.unlock();
g_pcMutex.unlock();
// Check that for a box really far it returns inf
math::Vector3d boxPositionFar(
unitBoxSize * 0.5 + far_ * 1.5, 0.0, 0.0);
box->SetLocalPosition(boxPositionFar);
mgr.RunOnce(std::chrono::steady_clock::duration::zero(), true);
for (int sleep = 0; sleep < 300 &&
(counter == 0 || infoCounter == 0 || imgCounter == 0 || pcCounter == 0);
++sleep)
{
g_mutex.lock();
g_infoMutex.lock();
g_imgMutex.lock();
g_pcMutex.lock();
counter = g_depthCounter;
infoCounter = g_infoCounter;
imgCounter = g_imgCounter;
pcCounter = g_pcCounter;
infoMsg = g_infoMsg;
g_mutex.unlock();
g_infoMutex.unlock();
g_imgMutex.unlock();
g_pcMutex.unlock();
std::this_thread::sleep_for(waitTime);
}
g_mutex.lock();
g_infoMutex.lock();
g_pcMutex.lock();
g_imgMutex.lock();
g_depthCounter = 0;
g_infoCounter = 0;
g_imgCounter = 0;
g_pcCounter = 0;
EXPECT_GT(counter, 0);
EXPECT_EQ(counter, infoCounter);
EXPECT_EQ(counter, imgCounter);
EXPECT_EQ(counter, pcCounter);
counter = 0;
infoCounter = 0;
imgCounter = 0;
pcCounter = 0;
// check depth
{
for (unsigned int i = 0; i < rgbdSensor->ImageHeight(); ++i)
{
unsigned int step = i * rgbdSensor->ImageWidth();
for (unsigned int j = 0; j < rgbdSensor->ImageWidth(); ++j)
{
unsigned int index = step + j;
EXPECT_FLOAT_EQ(math::INF_D, g_depthBuffer[index]);
}
}
}
// check color
{
for (unsigned int i = 0; i < rgbdSensor->ImageHeight(); ++i)
{
unsigned int step = i * rgbdSensor->ImageWidth() * 3;
for (unsigned int j = 0; j < rgbdSensor->ImageWidth(); ++j)
{
unsigned int index = step + j*3;
unsigned int r = g_imgBuffer[index];
unsigned int g = g_imgBuffer[index + 1];
unsigned int b = g_imgBuffer[index + 2];
EXPECT_GT(r, 0u);
EXPECT_EQ(0u, g);
EXPECT_EQ(0u, b);
}
}
}
// check point cloud xyz and rgb values
{
for (unsigned int i = 0; i < rgbdSensor->ImageHeight(); ++i)
{
unsigned int step = i * rgbdSensor->ImageWidth() * 3;
for (unsigned int j = 0; j < rgbdSensor->ImageWidth(); ++j)
{
unsigned int index = step + j*3;
float x = g_pointsXYZBuffer[index];
float y = g_pointsXYZBuffer[index + 1];
float z = g_pointsXYZBuffer[index + 2];
EXPECT_FLOAT_EQ(math::INF_D, x);
EXPECT_FLOAT_EQ(math::INF_D, y);
EXPECT_FLOAT_EQ(math::INF_D, z);
unsigned int r = g_pointsRGBBuffer[index];
unsigned int g = g_pointsRGBBuffer[index + 1];
unsigned int b = g_pointsRGBBuffer[index + 2];
EXPECT_GT(r, 0u);
EXPECT_EQ(0u, g);
EXPECT_EQ(0u, b);
}
}
}
g_infoMutex.unlock();
g_mutex.unlock();
g_imgMutex.unlock();
g_pcMutex.unlock();
// Clean up rendering ptrs
box.reset();
blue.reset();
// Clean up
mgr.Remove(rgbdSensor->Id());
engine->DestroyScene(scene);
rendering::unloadEngine(engine->Name());
}
//////////////////////////////////////////////////
TEST_P(RgbdCameraSensorTest, ImagesWithBuiltinSDF)
{
common::Console::SetVerbosity(4);
ImagesWithBuiltinSDF(GetParam());
}
INSTANTIATE_TEST_SUITE_P(RgbdCameraSensor, RgbdCameraSensorTest,
RENDER_ENGINE_VALUES, rendering::PrintToStringParam());