YARP
Yet Another Robot Platform
RGBDSensorParamParser.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
10 #include <yarp/os/LogComponent.h>
11 #include <yarp/os/LogStream.h>
12 
13 using namespace yarp::os;
14 using namespace yarp::dev;
15 using namespace std;
16 
17 namespace {
18 YARP_LOG_COMPONENT(RGBDSENSORPARAMPARSER, "yarp.dev.RGBDSensorParamParser")
19 }
20 
21 static bool checkParam(const Bottle& input, RGBDSensorParamParser::RGBDParam& param, bool& found)
22 {
23  bool ret = false;
24  Bottle bt=input.findGroup(param.name).tail(); // the first element is the name of the parameter
25 
26  if (!bt.isNull())
27  {
28  Bottle* b;
29  if (param.size>1 && bt.size()==1)
30  {
31  b = bt.get(0).asList();
32  }
33  else
34  {
35  b = &bt;
36  }
37  if (b->isNull())
38  {
39  yCError(RGBDSENSORPARAMPARSER) << "Check"<<param.name<<"in config file";
40  return false;
41  }
42  if (b->size() != param.size)
43  {
44  yCError(RGBDSENSORPARAMPARSER) << "Parameter" << param.name << "size should be" << param.size;
45  return false;
46  }
47  param.val.resize(param.size);
48  for (size_t i=0;i<b->size();i++)
49  {
50  ret = true;
51  param.val[i] = b->get(i);
52  found = true;
53  }
54  }
55  else
56  {
57  ret = true;
58  found = false;
59  }
60  return ret;
61 }
62 
63 static bool checkParam(const Bottle& settings, const Bottle& description, RGBDSensorParamParser::RGBDParam &param)
64 {
65  bool ret1, ret2, ret3;
66 
67  ret3 = true;
68  ret1 = checkParam(settings, param, param.isSetting); // look for settings
69  ret2 = checkParam(description, param, param.isDescription);// look for HW_DESCRIPTION
70 
71  if ( (param.isSetting) && (param.isDescription) )
72  {
73  yCError(RGBDSENSORPARAMPARSER) << "Setting " << param.name << " can either be a 'SETTING' or 'HW_DESCRIPTION', not both. Fix the config file. \
74  Look for documentation online.";
75  ret3 = false;
76  }
77  return (ret1 && ret2 && ret3);
78 }
79 
80 
81 static bool parseIntrinsic(const Searchable& config, const string& groupName, yarp::sig::IntrinsicParams &params)
82 {
83 
84  pair<string, double*> realparam;
85  vector<pair<string, double*> > realParams;
86  size_t i;
87 
88  if (!config.check(groupName))
89  {
90  if (params.isOptional)
91  {
92  return true;
93  }
94  else
95  {
96  return false;
97  }
98  }
99  Bottle& intrinsic = config.findGroup(groupName);
100 
101  realparam.first = "physFocalLength"; realparam.second = &params.physFocalLength; realParams.push_back(realparam);
102  realparam.first = "focalLengthX"; realparam.second = &params.focalLengthX; realParams.push_back(realparam);
103  realparam.first = "focalLengthY"; realparam.second = &params.focalLengthY; realParams.push_back(realparam);
104  realparam.first = "principalPointX"; realparam.second = &params.principalPointX; realParams.push_back(realparam);
105  realparam.first = "principalPointY"; realparam.second = &params.principalPointY; realParams.push_back(realparam);
106 
107  for(i = 0; i < realParams.size(); i++)
108  {
109  if (!intrinsic.check(realParams[i].first))
110  {
111  yCError(RGBDSENSORPARAMPARSER) << "Missing" << realParams[i].first << "param in" << groupName << "group in the configuration file";
112  return false;
113  }
114 
115  *(realParams[i].second) = intrinsic.find(realParams[i].first).asFloat64();
116  }
117 
118  if (!intrinsic.check("distortionModel"))
119  {
120  yCError(RGBDSENSORPARAMPARSER) << "Missing distortionModel param in configuration";
121  return false;
122  }
123 
124  if (!config.check(intrinsic.find("distortionModel").asString()))
125  {
126  yCError(RGBDSENSORPARAMPARSER) << "Missing" << intrinsic.find("distortionModel").asString() << "group in configuration file";
127  return false;
128  }
129 
130  Bottle& distortion = config.findGroup(intrinsic.find("distortionModel").asString());
131 
132  if (!distortion.check("name"))
133  {
134  yCError(RGBDSENSORPARAMPARSER) << "Missing name param in" << config.find("distortionModel").asString() << "group in configuration file";
135  return false;
136  }
137  if (distortion.find("name").asString() != "plumb_bob")
138  {
139  yCError(RGBDSENSORPARAMPARSER) << "Only plumb_bob distortion model is supported at the moment";
140  return false;
141  }
142 
143  realParams.clear();
144  realparam.first = "k1"; realparam.second = &params.distortionModel.k1; realParams.push_back(realparam);
145  realparam.first = "k2"; realparam.second = &params.distortionModel.k2; realParams.push_back(realparam);
146  realparam.first = "t1"; realparam.second = &params.distortionModel.t1; realParams.push_back(realparam);
147  realparam.first = "t2"; realparam.second = &params.distortionModel.t2; realParams.push_back(realparam);
148  realparam.first = "k3"; realparam.second = &params.distortionModel.k3; realParams.push_back(realparam);
149 
150  for(i = 0; i < realParams.size(); i++)
151  {
152  if (!distortion.check(realParams[i].first))
153  {
154  yCError(RGBDSENSORPARAMPARSER) << "Missing" << realParams[i].first << "param in" << intrinsic.find("distortionModel").asString() << "group in the configuration file";
155  return false;
156  }
157  *(realParams[i].second) = distortion.find(realParams[i].first).asFloat64();
158  }
159 
160  return true;
161 }
162 
163 bool RGBDSensorParamParser::parseParam(const Searchable &config, std::vector<RGBDParam*>& params)
164 {
165  bool ret = true;
166 
167  if (!config.check("SETTINGS"))
168  {
169  yCError(RGBDSENSORPARAMPARSER) << "Missing SETTINGS section on the configuration file";
170  return false;
171  }
172 
173  Bottle& settings = config.findGroup("SETTINGS");
174 
175  if (!config.check("HW_DESCRIPTION"))
176  {
177  yCError(RGBDSENSORPARAMPARSER) << "Missing HW_DESCRIPTION section on the configuration file";
178  return false;
179  }
180 
181  Bottle& description = config.findGroup("HW_DESCRIPTION");
182 
183 
184  for (auto& v: params)
185  {
186  if (!checkParam(settings, description, *v) ) {ret = false;}
187  }
188 
189  if (!ret)
190  {
191  yCError(RGBDSENSORPARAMPARSER) << "Driver input file not correct, please fix it!";
192  return false;
193  }
194 
195  if (!parseIntrinsic(config, "RGB_INTRINSIC_PARAMETERS", rgbIntrinsic))
196  {
197  yCError(RGBDSENSORPARAMPARSER) << "Incomplete or missing RGB_INTRINSIC_PARAMETERS section on the configuration file";
198  return false;
199  }
200 
201 
202  if (!parseIntrinsic(config, "DEPTH_INTRINSIC_PARAMETERS", depthIntrinsic))
203  {
204  yCError(RGBDSENSORPARAMPARSER) << "Incomplete or missing DEPTH_INTRINSIC_PARAMETERS section on the configuration file";
205  return false;
206  }
207 
208  if (!config.check("EXTRINSIC_PARAMETERS"))
209  {
210  if (!isOptionalExtrinsic)
211  {
212  yCError(RGBDSENSORPARAMPARSER) << "Missing EXTRINSIC_PARAMETERS section on the configuration file";
213  return false;
214  }
215  else
216  {
217  return true;
218  }
219  }
220  else
221  {
222 
223  Bottle& extrinsic = config.findGroup("EXTRINSIC_PARAMETERS");
224 
225  if (!extrinsic.check("transformation"))
226  {
227  yCError(RGBDSENSORPARAMPARSER) << "Missing transformation parameter under EXTRINSIC_PARAMETERS group in configuration file";
228  return false;
229  }
230 
231  Bottle transformation = extrinsic.findGroup("transformation").tail();
232  Bottle* tf;
233 
234  if (transformation.size()==1)
235  {
236  tf = transformation.get(0).asList();
237  }
238  else
239  {
240  tf=&transformation;
241  }
242  if (!(tf->size() == 4*4))
243  {
244  yCError(RGBDSENSORPARAMPARSER) << "The size of the transformation matrix is wrong";
245  return false;
246  }
247 
248  for(int i = 0; i < 4; i++)
249  {
250  for(int j = 0; j < 4; j++)
251  {
252  int k = i*4+j;
253  Value& v = tf->get(k);
254  if (!v.isFloat64())
255  {
256  yCError(RGBDSENSORPARAMPARSER) << "Wrong data format on transformation matrix (position" << k << ")";
257  return false;
258  }
259  transformationMatrix[i][j] = v.asFloat64();
260  }
261  }
262  }
263 
264  return ret;
265 }
bool ret
static bool parseIntrinsic(const Searchable &config, const string &groupName, yarp::sig::IntrinsicParams &params)
static bool checkParam(const Bottle &input, RGBDSensorParamParser::RGBDParam &param, bool &found)
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
size_type size() const
Gets the number of elements in the bottle.
Definition: Bottle.cpp:254
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition: Bottle.cpp:249
Bottle & findGroup(const std::string &key) const override
Gets a list corresponding to a given keyword.
Definition: Bottle.cpp:305
bool check(const std::string &key) const override
Check if there exists a property of the given name.
Definition: Bottle.cpp:280
Bottle tail() const
Get all but the first element of a bottle.
Definition: Bottle.cpp:391
bool isNull() const override
Checks if the object is invalid.
Definition: Bottle.cpp:373
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
Definition: Bottle.cpp:290
A base class for nested structures that can be searched.
Definition: Searchable.h:69
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
virtual Bottle & findGroup(const std::string &key) const =0
Gets a list corresponding to a given keyword.
A single value (typically within a Bottle).
Definition: Value.h:47
virtual yarp::conf::float64_t asFloat64() const
Get 64-bit floating point value.
Definition: Value.cpp:225
virtual Bottle * asList() const
Get list value.
Definition: Value.cpp:243
virtual bool isFloat64() const
Checks if value is a 64-bit floating point number.
Definition: Value.cpp:153
virtual std::string asString() const
Get string value.
Definition: Value.cpp:237
#define yCError(component,...)
Definition: LogComponent.h:157
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:80
Definition: FrameGraph.h:22
An interface for the device drivers.
An interface to the operating system, including Port based communication.
The IntrinsicParams struct to handle the intrinsic parameter of cameras(RGB and RGBD either).
double focalLengthY
Result of the product of the physical focal length(mm) and the size sy of the individual imager eleme...
DistortionModel distortionModel
Distortion model of the image.
double focalLengthX
Result of the product of the physical focal length(mm) and the size sx of the individual imager eleme...
double physFocalLength
Physical focal length of the lens (m)
double principalPointX
Horizontal coordinate of the principal point of the image, as a pixel offset from the left edge.
double principalPointY
Vertical coordinate of the principal point of the image, as a pixel offset from the top edge.