|
35 | 35 | /* Author: John Hsu */ |
36 | 36 |
|
37 | 37 | #include "urdf_parser/visual_sensor_parsers.h" |
| 38 | +#include "urdf_parser/utils.h" |
| 39 | +#include "urdf_parser/pose.h" |
38 | 40 | #include <urdf_sensor/camera.h> |
39 | 41 | #include <urdf_sensor/ray.h> |
40 | 42 |
|
41 | | -#include <fstream> |
42 | | -#include <locale> |
43 | | -#include <sstream> |
44 | | -#include <stdexcept> |
45 | | -#include <string> |
46 | | -#include <algorithm> |
47 | | -#include <tinyxml.h> |
48 | 43 | #include <console_bridge/console.h> |
49 | | -#include "urdf_parser/pose.h" |
50 | 44 |
|
51 | 45 | namespace urdf { |
52 | 46 |
|
53 | 47 | SensorBaseSharedPtr CameraParser::parse(TiXmlElement &config) |
54 | 48 | { |
55 | | - CameraSharedPtr camera(new Camera()); |
56 | | - |
57 | 49 | TiXmlElement *image = config.FirstChildElement("image"); |
58 | 50 | if (image) |
59 | 51 | { |
60 | | - const char* width_char = image->Attribute("width"); |
61 | | - if (width_char) |
62 | | - { |
63 | | - try |
64 | | - { |
65 | | - camera->width = std::stoul(width_char); |
66 | | - } |
67 | | - catch (std::invalid_argument &e) |
68 | | - { |
69 | | - CONSOLE_BRIDGE_logError("Camera image width [%s] is not a valid int: %s", width_char, e.what()); |
70 | | - return CameraSharedPtr(); |
71 | | - } |
72 | | - catch (std::out_of_range &e) |
73 | | - { |
74 | | - CONSOLE_BRIDGE_logError("Camera image width [%s] is out of range: %s", width_char, e.what()); |
75 | | - return CameraSharedPtr(); |
76 | | - } |
77 | | - } |
78 | | - else |
79 | | - { |
80 | | - CONSOLE_BRIDGE_logError("Camera sensor needs an image width attribute"); |
81 | | - return CameraSharedPtr(); |
82 | | - } |
83 | | - |
84 | | - const char* height_char = image->Attribute("height"); |
85 | | - if (height_char) |
86 | | - { |
87 | | - try |
88 | | - { |
89 | | - camera->height = std::stoul(height_char); |
90 | | - } |
91 | | - catch (std::invalid_argument &e) |
92 | | - { |
93 | | - CONSOLE_BRIDGE_logError("Camera image height [%s] is not a valid int: %s", height_char, e.what()); |
94 | | - return CameraSharedPtr(); |
95 | | - } |
96 | | - catch (std::out_of_range &e) |
97 | | - { |
98 | | - CONSOLE_BRIDGE_logError("Camera image height [%s] is out of range: %s", height_char, e.what()); |
99 | | - return CameraSharedPtr(); |
100 | | - } |
101 | | - } |
102 | | - else |
103 | | - { |
104 | | - CONSOLE_BRIDGE_logError("Camera sensor needs an image height attribute"); |
105 | | - return CameraSharedPtr(); |
106 | | - } |
107 | | - |
108 | | - const char* format_char = image->Attribute("format"); |
109 | | - if (format_char) |
110 | | - camera->format = std::string(format_char); |
111 | | - else |
112 | | - { |
113 | | - CONSOLE_BRIDGE_logError("Camera sensor needs an image format attribute"); |
114 | | - return CameraSharedPtr(); |
115 | | - } |
116 | | - |
117 | | - const char* hfov_char = image->Attribute("hfov"); |
118 | | - if (hfov_char) |
119 | | - { |
120 | | - try { |
121 | | - camera->hfov = strToDouble(hfov_char); |
122 | | - } catch(std::runtime_error &) { |
123 | | - CONSOLE_BRIDGE_logError("Camera image hfov [%s] is not a valid float", hfov_char); |
124 | | - return CameraSharedPtr(); |
125 | | - } |
126 | | - } |
127 | | - else |
128 | | - { |
129 | | - CONSOLE_BRIDGE_logError("Camera sensor needs an image hfov attribute"); |
130 | | - return CameraSharedPtr(); |
131 | | - } |
132 | | - |
133 | | - const char* near_char = image->Attribute("near"); |
134 | | - if (near_char) |
135 | | - { |
136 | | - try { |
137 | | - camera->near = strToDouble(near_char); |
138 | | - } catch(std::runtime_error &) { |
139 | | - CONSOLE_BRIDGE_logError("Camera image near [%s] is not a valid float", near_char); |
140 | | - return CameraSharedPtr(); |
141 | | - } |
142 | | - } |
143 | | - else |
144 | | - { |
145 | | - CONSOLE_BRIDGE_logError("Camera sensor needs an image near attribute"); |
| 52 | + try { |
| 53 | + CameraSharedPtr camera(new Camera()); |
| 54 | + camera->width = parseAttribute<unsigned int>(*image, "width"); |
| 55 | + camera->height = parseAttribute<unsigned int>(*image, "height"); |
| 56 | + camera->format = parseAttribute<std::string>(*image, "format"); |
| 57 | + camera->hfov = parseAttribute<double>(*image, "hfov"); |
| 58 | + camera->near = parseAttribute<double>(*image, "near"); |
| 59 | + camera->far = parseAttribute<double>(*image, "far"); |
| 60 | + return camera; |
| 61 | + } |
| 62 | + catch (const std::exception &e) |
| 63 | + { |
| 64 | + CONSOLE_BRIDGE_logError("Camera sensor %s", e.what()); |
146 | 65 | return CameraSharedPtr(); |
147 | 66 | } |
148 | | - |
149 | | - const char* far_char = image->Attribute("far"); |
150 | | - if (far_char) |
151 | | - { |
152 | | - try { |
153 | | - camera->far = strToDouble(far_char); |
154 | | - } catch(std::runtime_error &) { |
155 | | - CONSOLE_BRIDGE_logError("Camera image far [%s] is not a valid float", far_char); |
156 | | - return CameraSharedPtr(); |
157 | | - } |
158 | | - } |
159 | | - else |
160 | | - { |
161 | | - CONSOLE_BRIDGE_logError("Camera sensor needs an image far attribute"); |
162 | | - return CameraSharedPtr(); |
163 | | - } |
164 | | - |
165 | 67 | } |
166 | 68 | else |
167 | 69 | { |
168 | 70 | CONSOLE_BRIDGE_logError("Camera sensor has no <image> element"); |
169 | 71 | return CameraSharedPtr(); |
170 | 72 | } |
171 | | - return camera; |
172 | 73 | } |
173 | 74 |
|
174 | 75 |
|
175 | 76 | SensorBaseSharedPtr RayParser::parse(TiXmlElement &config) |
176 | 77 | { |
177 | | - RaySharedPtr ray (new Ray()); |
178 | | - |
179 | 78 | TiXmlElement *horizontal = config.FirstChildElement("horizontal"); |
180 | 79 | if (horizontal) |
181 | 80 | { |
182 | | - const char* samples_char = horizontal->Attribute("samples"); |
183 | | - if (samples_char) |
184 | | - { |
185 | | - try |
186 | | - { |
187 | | - ray->horizontal_samples = std::stoul(samples_char); |
188 | | - } |
189 | | - catch (std::invalid_argument &e) |
190 | | - { |
191 | | - CONSOLE_BRIDGE_logError("Ray horizontal samples [%s] is not a valid float: %s", samples_char, e.what()); |
192 | | - return RaySharedPtr(); |
193 | | - } |
194 | | - catch (std::out_of_range &e) |
195 | | - { |
196 | | - CONSOLE_BRIDGE_logError("Ray horizontal samples [%s] is out of range: %s", samples_char, e.what()); |
197 | | - return RaySharedPtr(); |
198 | | - } |
199 | | - } |
200 | | - |
201 | | - const char* resolution_char = horizontal->Attribute("resolution"); |
202 | | - if (resolution_char) |
203 | | - { |
204 | | - try { |
205 | | - ray->horizontal_resolution = strToDouble(resolution_char); |
206 | | - } catch(std::runtime_error &) { |
207 | | - CONSOLE_BRIDGE_logError("Ray horizontal resolution [%s] is not a valid float", resolution_char); |
208 | | - return RaySharedPtr(); |
209 | | - } |
| 81 | + try { |
| 82 | + RaySharedPtr ray (new Ray()); |
| 83 | + ray->horizontal_samples = parseAttribute<unsigned int>(*horizontal, "samples"); |
| 84 | + ray->horizontal_resolution = parseAttribute<double>(*horizontal, "resolution"); |
| 85 | + ray->horizontal_min_angle = parseAttribute<double>(*horizontal, "min_angle"); |
| 86 | + ray->horizontal_max_angle = parseAttribute<double>(*horizontal, "max_angle"); |
| 87 | + return ray; |
210 | 88 | } |
211 | | - |
212 | | - const char* min_angle_char = horizontal->Attribute("min_angle"); |
213 | | - if (min_angle_char) |
| 89 | + catch (const std::exception &e) |
214 | 90 | { |
215 | | - try { |
216 | | - ray->horizontal_min_angle = strToDouble(min_angle_char); |
217 | | - } catch(std::runtime_error &) { |
218 | | - CONSOLE_BRIDGE_logError("Ray horizontal min_angle [%s] is not a valid float", min_angle_char); |
219 | | - return RaySharedPtr(); |
220 | | - } |
221 | | - } |
222 | | - |
223 | | - const char* max_angle_char = horizontal->Attribute("max_angle"); |
224 | | - if (max_angle_char) |
225 | | - { |
226 | | - try { |
227 | | - ray->horizontal_max_angle = strToDouble(max_angle_char); |
228 | | - } catch(std::runtime_error &) { |
229 | | - CONSOLE_BRIDGE_logError("Ray horizontal max_angle [%s] is not a valid float", max_angle_char); |
230 | | - return RaySharedPtr(); |
231 | | - } |
| 91 | + CONSOLE_BRIDGE_logError("Ray horizontal: %s", e.what()); |
| 92 | + return RaySharedPtr(); |
232 | 93 | } |
233 | 94 | } |
234 | 95 |
|
235 | 96 | TiXmlElement *vertical = config.FirstChildElement("vertical"); |
236 | 97 | if (vertical) |
237 | 98 | { |
238 | | - const char* samples_char = vertical->Attribute("samples"); |
239 | | - if (samples_char) |
240 | | - { |
241 | | - try |
242 | | - { |
243 | | - ray->vertical_samples = std::stoul(samples_char); |
244 | | - } |
245 | | - catch (std::invalid_argument &e) |
246 | | - { |
247 | | - CONSOLE_BRIDGE_logError("Ray vertical samples [%s] is not a valid float: %s", samples_char, e.what()); |
248 | | - return RaySharedPtr(); |
249 | | - } |
250 | | - catch (std::out_of_range &e) |
251 | | - { |
252 | | - CONSOLE_BRIDGE_logError("Ray vertical samples [%s] is out of range: %s", samples_char, e.what()); |
253 | | - return RaySharedPtr(); |
254 | | - } |
| 99 | + try { |
| 100 | + RaySharedPtr ray (new Ray()); |
| 101 | + ray->vertical_samples = parseAttribute<unsigned int>(*vertical, "samples"); |
| 102 | + ray->vertical_resolution = parseAttribute<double>(*vertical, "resolution"); |
| 103 | + ray->vertical_min_angle = parseAttribute<double>(*vertical, "min_angle"); |
| 104 | + ray->vertical_max_angle = parseAttribute<double>(*vertical, "max_angle"); |
| 105 | + return ray; |
255 | 106 | } |
256 | | - |
257 | | - const char* resolution_char = vertical->Attribute("resolution"); |
258 | | - if (resolution_char) |
259 | | - { |
260 | | - try { |
261 | | - ray->vertical_resolution = strToDouble(resolution_char); |
262 | | - } catch(std::runtime_error &) { |
263 | | - CONSOLE_BRIDGE_logError("Ray vertical resolution [%s] is not a valid float", resolution_char); |
264 | | - return RaySharedPtr(); |
265 | | - } |
266 | | - } |
267 | | - |
268 | | - const char* min_angle_char = vertical->Attribute("min_angle"); |
269 | | - if (min_angle_char) |
270 | | - { |
271 | | - try { |
272 | | - ray->vertical_min_angle = strToDouble(min_angle_char); |
273 | | - } catch(std::runtime_error &) { |
274 | | - CONSOLE_BRIDGE_logError("Ray vertical min_angle [%s] is not a valid float", min_angle_char); |
275 | | - return RaySharedPtr(); |
276 | | - } |
277 | | - } |
278 | | - |
279 | | - const char* max_angle_char = vertical->Attribute("max_angle"); |
280 | | - if (max_angle_char) |
| 107 | + catch (const std::exception &e) |
281 | 108 | { |
282 | | - try { |
283 | | - ray->vertical_max_angle = strToDouble(max_angle_char); |
284 | | - } catch(std::runtime_error &) { |
285 | | - CONSOLE_BRIDGE_logError("Ray vertical max_angle [%s] is not a valid float", max_angle_char); |
286 | | - return RaySharedPtr(); |
287 | | - } |
| 109 | + CONSOLE_BRIDGE_logError("Ray horizontal: %s", e.what()); |
| 110 | + return RaySharedPtr(); |
288 | 111 | } |
289 | 112 | } |
290 | | - return ray; |
| 113 | + return RaySharedPtr(); |
291 | 114 | } |
292 | 115 |
|
293 | 116 | } |
0 commit comments