-
Notifications
You must be signed in to change notification settings - Fork 58
/
Camera_TEST.cc
223 lines (197 loc) · 6.64 KB
/
Camera_TEST.cc
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
/*
* 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 <gtest/gtest.h>
#include <sdf/sdf.hh>
#include <ignition/sensors/Export.hh>
#include <ignition/sensors/CameraSensor.hh>
#include <ignition/sensors/Manager.hh>
sdf::ElementPtr cameraToBadSdf()
{
std::ostringstream stream;
stream
<< "<?xml version='1.0'?>"
<< "<sdf version='1.6'>"
<< " <model name='m1'>"
<< " <link name='link1'>"
<< " <sensor name='cam_name' type='not_camera'>"
<< " <not_camera>"
<< " </not_camera>"
<< " </sensor>"
<< " </link>"
<< " </model>"
<< "</sdf>";
sdf::SDFPtr sdfParsed(new sdf::SDF());
sdf::init(sdfParsed);
if (!sdf::readString(stream.str(), sdfParsed))
return sdf::ElementPtr();
return sdfParsed->Root()->GetElement("model")->GetElement("link")
->GetElement("sensor");
}
sdf::ElementPtr CameraToSdf(const std::string &_type,
const std::string &_name, double _updateRate,
const std::string &_topic, bool _alwaysOn, bool _visualize)
{
std::ostringstream stream;
stream
<< "<?xml version='1.0'?>"
<< "<sdf version='1.6'>"
<< " <model name='m1'>"
<< " <link name='link1'>"
<< " <sensor name='" << _name << "' type='" << _type << "'>"
<< " <topic>" << _topic << "</topic>"
<< " <update_rate>"<< _updateRate <<"</update_rate>"
<< " <always_on>"<< _alwaysOn <<"</always_on>"
<< " <visualize>" << _visualize << "</visualize>"
<< " <camera>"
<< " <horizontal_fov>.75</horizontal_fov>"
<< " <image>"
<< " <width>640</width>"
<< " <height>480</height>"
<< " <format>R8G8B8</format>"
<< " </image>"
<< " <clip>"
<< " <near>0.2</near>"
<< " <far>12.3</far>"
<< " </clip>"
<< " <save enabled='true'>"
<< " <path>/tmp/cam</path>"
<< " </save>"
<< " <noise>"
<< " <type>gaussian</type>"
<< " <mean>0.5</mean>"
<< " <stddev>0.1</stddev>"
<< " </noise>"
<< " <distortion>"
<< " <k1>0.1</k1>"
<< " <k2>0.2</k2>"
<< " <k3>0.3</k3>"
<< " <p1>0.4</p1>"
<< " <p2>0.5</p2>"
<< " <center>0.2 0.4</center>"
<< " </distortion>"
<< " <lens>"
<< " <type>custom</type>"
<< " <scale_to_hfov>false</scale_to_hfov>"
<< " <custom_function>"
<< " <c1>1.1</c1>"
<< " <c2>2.2</c2>"
<< " <c3>3.3</c3>"
<< " <f>1.2</f>"
<< " <fun>sin</fun>"
<< " </custom_function>"
<< " <cutoff_angle>0.7505</cutoff_angle>"
<< " <env_texture_size>128</env_texture_size>"
<< " <intrinsics>"
<< " <fx>280</fx>"
<< " <fy>281</fy>"
<< " <cx>162</cx>"
<< " <cy>124</cy>"
<< " <s>1.2</s>"
<< " </intrinsics>"
<< " </lens>"
<< " </camera>"
<< " </sensor>"
<< " </link>"
<< " </model>"
<< "</sdf>";
sdf::SDFPtr sdfParsed(new sdf::SDF());
sdf::init(sdfParsed);
if (!sdf::readString(stream.str(), sdfParsed))
return sdf::ElementPtr();
return sdfParsed->Root()->GetElement("model")->GetElement("link")
->GetElement("sensor");
}
/// \brief Test camera sensor
class Camera_TEST : public ::testing::Test
{
// Documentation inherited
protected: void SetUp() override
{
ignition::common::Console::SetVerbosity(4);
}
};
//////////////////////////////////////////////////
TEST(Camera_TEST, CreateCamera)
{
ignition::sensors::Manager mgr;
sdf::ElementPtr camSdf = CameraToSdf("camera", "my_camera", 60.0, "/cam",
true, true);
// Create a CameraSensor
ignition::sensors::CameraSensor *cam =
mgr.CreateSensor<ignition::sensors::CameraSensor>(camSdf);
// Make sure the above dynamic cast worked.
ASSERT_NE(nullptr, cam);
// Check topics
EXPECT_EQ("/cam", cam->Topic());
EXPECT_EQ("/camera_info", cam->InfoTopic());
// however camera is not loaded because a rendering scene is missing so
// updates will not be successful and image size will be 0
EXPECT_FALSE(cam->Update(std::chrono::steady_clock::duration::zero()));
EXPECT_EQ(0u, cam->ImageWidth());
EXPECT_EQ(0u, cam->ImageHeight());
// Bad camera
sdf::ElementPtr camBadSdf = cameraToBadSdf();
// Create a CameraSensor
ignition::sensors::CameraSensor *badCam =
mgr.CreateSensor<ignition::sensors::CameraSensor>(camBadSdf);
EXPECT_TRUE(badCam == nullptr);
}
/////////////////////////////////////////////////
TEST(Camera_TEST, Topic)
{
const std::string type = "camera";
const std::string name = "TestCamera";
const double updateRate = 30;
const bool alwaysOn = 1;
const bool visualize = 1;
// Factory
ignition::sensors::Manager mgr;
// Default topic
{
const std::string topic;
auto cameraSdf = CameraToSdf(type, name, updateRate, topic, alwaysOn,
visualize);
auto camera = mgr.CreateSensor<ignition::sensors::CameraSensor>(cameraSdf);
ASSERT_NE(nullptr, camera);
EXPECT_NE(ignition::sensors::NO_SENSOR, camera->Id());
EXPECT_EQ("/camera", camera->Topic());
}
// Convert to valid topic
{
const std::string topic = "/topic with spaces/@~characters//";
auto cameraSdf = CameraToSdf(type, name, updateRate, topic, alwaysOn,
visualize);
auto camera = mgr.CreateSensor<ignition::sensors::CameraSensor>(cameraSdf);
ASSERT_NE(nullptr, camera);
EXPECT_NE(ignition::sensors::NO_SENSOR, camera->Id());
EXPECT_EQ("/topic_with_spaces/characters", camera->Topic());
}
// Invalid topic
{
const std::string topic = "@@@";
auto cameraSdf = CameraToSdf(type, name, updateRate, topic, alwaysOn,
visualize);
auto sensor = mgr.CreateSensor<ignition::sensors::CameraSensor>(cameraSdf);
EXPECT_EQ(nullptr, sensor);
}
}
//////////////////////////////////////////////////
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}