from_any_value.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2015 Aldebaran
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 
18 #include "from_any_value.hpp"
19 
20 namespace naoqi {
21 
22 namespace tools {
23 
24 NaoqiImage fromAnyValueToNaoqiImage(qi::AnyValue& value){
25  qi::AnyReferenceVector anyref;
26  NaoqiImage result;
27  std::ostringstream ss;
28  try{
29  anyref = value.asListValuePtr();
30  }
31  catch(std::runtime_error& e)
32  {
33  ss << "Could not transform AnyValue into list: " << e.what();
34  throw std::runtime_error(ss.str());
35  }
36  qi::AnyReference ref;
37 
39  ref = anyref[0].content();
40  if(ref.kind() == qi::TypeKind_Int)
41  {
42  result.width = ref.asInt32();
43  }
44  else
45  {
46  ss << "Could not retrieve width";
47  throw std::runtime_error(ss.str());
48  }
49 
51  ref = anyref[1].content();
52  if(ref.kind() == qi::TypeKind_Int)
53  {
54  result.height = ref.asInt32();
55  }
56  else
57  {
58  ss << "Could not retrieve height";
59  throw std::runtime_error(ss.str());
60  }
61 
63  ref = anyref[2].content();
64  if(ref.kind() == qi::TypeKind_Int)
65  {
66  result.number_of_layers = ref.asInt32();
67  }
68  else
69  {
70  ss << "Could not retrieve number of layers";
71  throw std::runtime_error(ss.str());
72  }
73 
75  ref = anyref[3].content();
76  if(ref.kind() == qi::TypeKind_Int)
77  {
78  result.colorspace = ref.asInt32();
79  }
80  else
81  {
82  ss << "Could not retrieve colorspace";
83  throw std::runtime_error(ss.str());
84  }
85 
87  ref = anyref[4].content();
88  if(ref.kind() == qi::TypeKind_Int)
89  {
90  result.timestamp_s = ref.asInt32();
91  }
92  else
93  {
94  ss << "Could not retrieve timestamp_s";
95  throw std::runtime_error(ss.str());
96  }
97 
99  ref = anyref[5].content();
100  if(ref.kind() == qi::TypeKind_Int)
101  {
102  result.timestamp_us = ref.asInt32();
103  }
104  else
105  {
106  ss << "Could not retrieve timestamp_us";
107  throw std::runtime_error(ss.str());
108  }
109 
111  ref = anyref[6].content();
112  if(ref.kind() == qi::TypeKind_Raw)
113  {
114  result.buffer = (void*)ref.asRaw().first;
115  }
116  else
117  {
118  ss << "Could not retrieve buffer";
119  throw std::runtime_error(ss.str());
120  }
121 
123  ref = anyref[7].content();
124  if(ref.kind() == qi::TypeKind_Int)
125  {
126  result.cam_id = ref.asInt32();
127  }
128  else
129  {
130  ss << "Could not retrieve cam_id";
131  throw std::runtime_error(ss.str());
132  }
133 
135  ref = anyref[8].content();
136  if(ref.kind() == qi::TypeKind_Float)
137  {
138  result.fov_left = ref.asFloat();
139  }
140  else
141  {
142  ss << "Could not retrieve fov_left";
143  throw std::runtime_error(ss.str());
144  }
145 
147  ref = anyref[9].content();
148  if(ref.kind() == qi::TypeKind_Float)
149  {
150  result.fov_top = ref.asFloat();
151  }
152  else
153  {
154  ss << "Could not retrieve fov_top";
155  throw std::runtime_error(ss.str());
156  }
157 
159  ref = anyref[10].content();
160  if(ref.kind() == qi::TypeKind_Float)
161  {
162  result.fov_right = ref.asFloat();
163  }
164  else
165  {
166  ss << "Could not retrieve fov_right";
167  throw std::runtime_error(ss.str());
168  }
169 
171  ref = anyref[11].content();
172  if(ref.kind() == qi::TypeKind_Float)
173  {
174  result.fov_bottom = ref.asFloat();
175  }
176  else
177  {
178  ss << "Could not retrieve fov_bottom";
179  throw std::runtime_error(ss.str());
180  }
181  return result;
182 }
183 
184 
185 std::vector<float> fromAnyValueToFloatVector(qi::AnyValue& value, std::vector<float>& result){
186  qi::AnyReferenceVector anyrefs = value.asListValuePtr();
187 
188  for(int i=0; i<anyrefs.size();i++)
189  {
190  try
191  {
192  result.push_back(anyrefs[i].content().toFloat());
193  }
194  catch(std::runtime_error& e)
195  {
196  result.push_back(-1.0);
197  std::cout << e.what() << "=> set to -1" << std::endl;
198  }
199  }
200  return result;
201 }
202 
203 std::vector<std::string> fromAnyValueToStringVector(qi::AnyValue& value, std::vector<std::string>& result){
204  qi::AnyReferenceVector anyrefs = value.asListValuePtr();
205 
206  for(int i=0; i<anyrefs.size();i++)
207  {
208  try
209  {
210  result.push_back(anyrefs[i].content().toString());
211  }
212  catch(std::runtime_error& e)
213  {
214  result.push_back("Not available");
215  std::cout << e.what() << " => set to 'Not available'" << std::endl;
216  }
217  }
218  return result;
219 }
220 
221 
223  qi::AnyValue &value,
224  std::vector< std::vector<float> > &result) {
225 
226  qi::AnyReferenceVector anyrefs;
227 
228  try {
229  anyrefs = value.asListValuePtr();
230 
231  } catch (const std::exception& e) {
232  throw std::exception(e);
233  }
234 
235  result.resize(anyrefs.size());
236 
237  for(int i=0; i<anyrefs.size(); i++) {
238  qi::AnyReferenceVector anyref;
239 
240  try {
241  anyref = anyrefs[i].asListValuePtr();
242 
243  } catch (const std::exception& e) {
244  throw std::exception(e);
245  }
246 
247  result[i].resize(anyref.size());
248 
249  for(int j=0; j<anyref.size(); j++) {
250  try {
251  result[i][j] = anyref[j].content().toFloat();
252 
253  } catch(std::runtime_error& e) {
254  throw std::exception(e);
255  }
256  }
257 
258  }
259 }
260 }
261 }
std::vector< std::string > fromAnyValueToStringVector(qi::AnyValue &value, std::vector< std::string > &result)
NaoqiImage fromAnyValueToNaoqiImage(qi::AnyValue &value)
std::vector< float > fromAnyValueToFloatVector(qi::AnyValue &value, std::vector< float > &result)
The struct describing an image retrieved by ALVideoDevice. This specification can be found here: http...
Definition: naoqi_image.hpp:30
void fromAnyValueToFloatVectorVector(qi::AnyValue &value, std::vector< std::vector< float > > &result)


naoqi_driver
Author(s): Karsten Knese
autogenerated on Sat Feb 15 2020 03:24:26