Flexiv AIDK APIs  1.2
defs.hpp
Go to the documentation of this file.
1 
9 #pragma once
10 #include <chrono>
11 #include <fstream>
12 #include <iomanip>
13 #include <iostream>
14 #include <memory>
15 #include <sstream>
16 #include <string>
17 #include <thread>
18 #include <unordered_map>
19 #include <vector>
20 
21 namespace flexiv {
22 namespace ai {
23 
24 // define available keys
25 extern std::vector<std::string> PROTO_DATA;
26 extern std::vector<std::string> SINGLE_DATA;
27 extern std::unordered_map<std::string, std::string> MAPPING;
28 extern std::vector<std::string> SUPPORTED_KEYS;
29 
30 // define the AI command
31 extern std::unordered_map<std::string, int> AI_CMD;
32 
33 extern std::unordered_map<std::string, std::string> ALIAS;
34 
35 // define the AI state enum
36 enum AIState
37 {
38  UNKNOWN = 0,
53 
56  NUM = LAST - FIRST + 1
57 };
58 
59 // Data structure for object meta information
61 {
63  {
64  coordinate_id = 0;
65  is_valid = true;
66  double_value = 0.0;
67  int_value = 0;
68  }
69 
70  // left-top corner of the bbox [xmin, ymin] in the image coordinate [pixel]
71  std::vector<int> bbox_min;
72 
73  // right-bottom corner of the bbox [xmax, ymax] in the image coordinate
74  // [pixel]
75  std::vector<int> bbox_max;
76 
77  // image feature (key point) position [u;v] in image coordinate [pixel]
78  std::vector<std::vector<double>> img_pts;
79 
80  // image feature (key point) 3D position [x;y;z] in camera coordinate [m]
81  std::vector<std::vector<double>> img_pts_pos;
82 
83  // coordinate_id defines which camera/global coordinate to use, object pose
84  // in camera/global coordinate [m]
85  std::vector<double> obj_pose;
86 
87  // coordinate_id defines which camera/global coordinate to use, grasp pose
88  // in camera/global coordinate [m]
89  std::vector<std::vector<double>> grasp_pose;
90 
91  // uncertainty of the object pose 6-dim values, in range [0,1], [x; y; z;
92  // theta_x; theta_y; theta_z]
93  std::vector<double> uncertainty;
94 
95  // coordinate system. 0 for world (global) coordinate. 1 for camera (local)
96  // coordinate
98 
99  // flag: if meta data is valid, robot only process this instance when
100  // is_valid is true
101  bool is_valid;
102 
103  // custom data - DOUBLE
104  double double_value;
105 
106  // custom data - INT
107  double int_value;
108 
109  // object type distinguish by string name
110  std::string name;
111 };
112 
113 // Data structure for object information
114 struct ObjState
115 {
117  {
118  obj_name = "";
119  ai_index = 0;
120  }
121 
122  // object name
123  std::string obj_name;
124 
125  // time index in AI module
126  uint32_t ai_index;
127 
128  // synchronized timestamp
130 
131  // object meta data
132  std::vector<ObjMetaData> obj_meta_data;
133 };
134 
135 // transfer meta data to vector
136 std::vector<std::vector<double>> meta_to_vector(const ObjMetaData &obj,
137  const std::string &key);
138 
139 // calculate md5 of string
140 std::string calculate_string_md5(const std::string &input);
141 
142 // Data structure for result store
143 struct Result
144 {
145  bool valid;
146 
148 
149  double double_value;
150 
151  std::string name;
152 
153  std::vector<std::vector<double>> vect;
154 };
155 
156 // Data structure for ai status
157 struct AIStatus
158 {
159  int status_code = -1;
160 
161  std::string status_name;
162 
163  std::string status_message;
164 };
165 
166 // Data structure for return response
167 struct Response
168 {
169  int error_code = 1;
170 
171  std::string error_msg;
172 };
173 
174 // Custom variant inplementation for different types
175 template <size_t arg1, size_t... others>
176 struct static_max;
177 
178 template <size_t arg>
179 struct static_max<arg>
180 {
181  static const size_t value = arg;
182 };
183 
184 template <size_t arg1, size_t arg2, size_t... others>
185 struct static_max<arg1, arg2, others...>
186 {
187  static const size_t value = arg1 >= arg2
188  ? static_max<arg1, others...>::value
189  : static_max<arg2, others...>::value;
190 };
191 
192 template <typename... Ts>
194 
195 template <typename F, typename... Ts>
196 struct variant_helper<F, Ts...>
197 {
198  inline static void destroy(size_t id, void *data)
199  {
200  if (id == typeid(F).hash_code())
201  reinterpret_cast<F *>(data)->~F();
202  else
204  }
205 
206  inline static void move(size_t old_t, void *old_v, void *new_v)
207  {
208  if (old_t == typeid(F).hash_code())
209  new (new_v) F(std::move(*reinterpret_cast<F *>(old_v)));
210  else
211  variant_helper<Ts...>::move(old_t, old_v, new_v);
212  }
213 
214  inline static void copy(size_t old_t, const void *old_v, void *new_v)
215  {
216  if (old_t == typeid(F).hash_code())
217  new (new_v) F(*reinterpret_cast<const F *>(old_v));
218  else
219  variant_helper<Ts...>::copy(old_t, old_v, new_v);
220  }
221 };
222 
223 template <>
225 {
226  inline static void destroy(size_t id, void *data)
227  {
228  (void)id;
229  (void)data;
230  }
231 
232  inline static void move(size_t old_t, void *old_v, void *new_v)
233  {
234  (void)old_t;
235  (void)old_v;
236  (void)new_v;
237  }
238 
239  inline static void copy(size_t old_t, const void *old_v, void *new_v)
240  {
241  (void)old_t;
242  (void)old_v;
243  (void)new_v;
244  }
245 };
246 
247 template <typename... Ts>
248 struct variant
249 {
250 private:
251  static const size_t data_size = static_max<sizeof(Ts)...>::value;
252  static const size_t data_align = static_max<alignof(Ts)...>::value;
253 
254  using data_t = typename std::aligned_storage<data_size, data_align>::type;
255 
256  using helper_t = variant_helper<Ts...>;
257 
258  static inline size_t invalid_type() { return typeid(void).hash_code(); }
259 
260  size_t type_id;
261  data_t data;
262 
263 public:
265  : type_id(invalid_type())
266  {}
267 
269  : type_id(old.type_id)
270  {
271  helper_t::copy(old.type_id, &old.data, &data);
272  }
273 
275  : type_id(old.type_id)
276  {
277  helper_t::move(old.type_id, &old.data, &data);
278  }
279 
280  // Serves as both the move and the copy asignment operator.
282  {
283  std::swap(type_id, old.type_id);
284  std::swap(data, old.data);
285 
286  return *this;
287  }
288 
289  template <typename T>
290  bool is()
291  {
292  return (type_id == typeid(T).hash_code());
293  }
294 
295  bool valid() { return (type_id != invalid_type()); }
296 
297  template <typename T, typename... Args>
298  void set(Args &&... args)
299  {
300  // First we destroy the current contents
301  helper_t::destroy(type_id, &data);
302  new (&data) T(std::forward<Args>(args)...);
303  type_id = typeid(T).hash_code();
304  }
305 
306  template <typename T>
307  T &get()
308  {
309  // It is a dynamic_cast-like behaviour
310  if (type_id == typeid(T).hash_code())
311  return *reinterpret_cast<T *>(&data);
312  else
313  throw std::bad_cast();
314  }
315 
316  ~variant() { helper_t::destroy(type_id, &data); }
317 };
318 
320 
321 } /* namespace ai */
322 } /* namespace flexiv */
flexiv::ai::ObjMetaData::uncertainty
std::vector< double > uncertainty
Definition: defs.hpp:93
flexiv::ai::calculate_string_md5
std::string calculate_string_md5(const std::string &input)
flexiv::ai::ObjState::ai_index
uint32_t ai_index
Definition: defs.hpp:126
flexiv::ai::ObjMetaData::bbox_max
std::vector< int > bbox_max
Definition: defs.hpp:75
flexiv::ai::variant
Definition: defs.hpp:248
flexiv::ai::POSE6D
@ POSE6D
Definition: defs.hpp:42
flexiv::ai::variant::get
T & get()
Definition: defs.hpp:307
flexiv::ai::ObjMetaData::int_value
double int_value
Definition: defs.hpp:107
flexiv::ai::NUM
@ NUM
Definition: defs.hpp:56
flexiv::ai::SINGLE_DATA
std::vector< std::string > SINGLE_DATA
flexiv::ai::variant::operator=
variant< Ts... > & operator=(variant< Ts... > old)
Definition: defs.hpp:281
flexiv::ai::variant::set
void set(Args &&... args)
Definition: defs.hpp:298
flexiv::ai::ObjMetaData::img_pts
std::vector< std::vector< double > > img_pts
Definition: defs.hpp:78
flexiv::ai::ALIAS
std::unordered_map< std::string, std::string > ALIAS
flexiv::ai::ObjState
Definition: defs.hpp:114
flexiv::ai::variant_helper< F, Ts... >::copy
static void copy(size_t old_t, const void *old_v, void *new_v)
Definition: defs.hpp:214
flexiv::ai::ObjMetaData::double_value
double double_value
Definition: defs.hpp:104
flexiv::ai::LAST
@ LAST
Definition: defs.hpp:55
flexiv::ai::variant::is
bool is()
Definition: defs.hpp:290
flexiv::ai::BBOX
@ BBOX
Definition: defs.hpp:45
flexiv::ai::variant::~variant
~variant()
Definition: defs.hpp:316
flexiv::ai::ObjState::obj_meta_data
std::vector< ObjMetaData > obj_meta_data
Definition: defs.hpp:132
flexiv::ai::AIStatus::status_name
std::string status_name
Definition: defs.hpp:161
flexiv::ai::GET_INT_VALUE
@ GET_INT_VALUE
Definition: defs.hpp:49
flexiv::ai::PROTO_DATA
std::vector< std::string > PROTO_DATA
flexiv::ai::Response
Definition: defs.hpp:167
flexiv::ai::ObjMetaData::grasp_pose
std::vector< std::vector< double > > grasp_pose
Definition: defs.hpp:89
flexiv::ai::ERROR
@ ERROR
Definition: defs.hpp:39
flexiv::ai::CLASSIFY
@ CLASSIFY
Definition: defs.hpp:47
flexiv::ai::variant::variant
variant(variant< Ts... > &&old)
Definition: defs.hpp:274
flexiv::ai::ObjMetaData::obj_pose
std::vector< double > obj_pose
Definition: defs.hpp:85
flexiv::ai::variant_helper<>::destroy
static void destroy(size_t id, void *data)
Definition: defs.hpp:226
flexiv::ai::AIStatus
Definition: defs.hpp:157
flexiv::ai::Response::error_code
int error_code
Definition: defs.hpp:169
flexiv::ai::variant_helper
Definition: defs.hpp:193
flexiv::ai::GET_DOUBLE_VALUE
@ GET_DOUBLE_VALUE
Definition: defs.hpp:50
flexiv::ai::ObjMetaData::coordinate_id
int coordinate_id
Definition: defs.hpp:97
flexiv::ai::Result::name
std::string name
Definition: defs.hpp:151
flexiv::ai::MULTIVIEW
@ MULTIVIEW
Definition: defs.hpp:46
flexiv::ai::variant::variant
variant(const variant< Ts... > &old)
Definition: defs.hpp:268
flexiv::ai::UNKNOWN
@ UNKNOWN
Definition: defs.hpp:38
flexiv::ai::variant_helper< F, Ts... >::destroy
static void destroy(size_t id, void *data)
Definition: defs.hpp:198
flexiv::ai::KEYPOINT3D
@ KEYPOINT3D
Definition: defs.hpp:51
flexiv::ai::ObjMetaData::is_valid
bool is_valid
Definition: defs.hpp:101
flexiv::ai::CUSTOM
@ CUSTOM
Definition: defs.hpp:52
flexiv::ai::ObjMetaData::ObjMetaData
ObjMetaData()
Definition: defs.hpp:62
flexiv::ai::IDLE
@ IDLE
Definition: defs.hpp:40
flexiv::ai::variant::variant
variant()
Definition: defs.hpp:264
flexiv::ai::SCENE
@ SCENE
Definition: defs.hpp:48
flexiv
Definition: aidk.hpp:14
flexiv::ai::Result::vect
std::vector< std::vector< double > > vect
Definition: defs.hpp:153
flexiv::ai::Result::double_value
double double_value
Definition: defs.hpp:149
flexiv::ai::KEYPOINT
@ KEYPOINT
Definition: defs.hpp:44
flexiv::ai::ObjMetaData::name
std::string name
Definition: defs.hpp:110
flexiv::ai::meta_to_vector
std::vector< std::vector< double > > meta_to_vector(const ObjMetaData &obj, const std::string &key)
flexiv::ai::static_max
Definition: defs.hpp:176
flexiv::ai::ObjState::synced_timestamp
double synced_timestamp
Definition: defs.hpp:129
flexiv::ai::POS3D
@ POS3D
Definition: defs.hpp:41
flexiv::ai::ObjMetaData::img_pts_pos
std::vector< std::vector< double > > img_pts_pos
Definition: defs.hpp:81
flexiv::ai::AIState
AIState
Definition: defs.hpp:36
flexiv::ai::variant_helper<>::copy
static void copy(size_t old_t, const void *old_v, void *new_v)
Definition: defs.hpp:239
flexiv::ai::variant_helper<>::move
static void move(size_t old_t, void *old_v, void *new_v)
Definition: defs.hpp:232
flexiv::ai::MAPPING
std::unordered_map< std::string, std::string > MAPPING
flexiv::ai::variant_helper< F, Ts... >::move
static void move(size_t old_t, void *old_v, void *new_v)
Definition: defs.hpp:206
flexiv::ai::ObjMetaData
Definition: defs.hpp:60
flexiv::ai::GRASP_POSE
@ GRASP_POSE
Definition: defs.hpp:43
flexiv::ai::Response::error_msg
std::string error_msg
Definition: defs.hpp:171
flexiv::ai::FIRST
@ FIRST
Definition: defs.hpp:54
flexiv::ai::AIStatus::status_code
int status_code
Definition: defs.hpp:159
flexiv::ai::Result::valid
bool valid
Definition: defs.hpp:145
flexiv::ai::variant::valid
bool valid()
Definition: defs.hpp:295
flexiv::ai::ObjMetaData::bbox_min
std::vector< int > bbox_min
Definition: defs.hpp:71
flexiv::ai::ObjState::obj_name
std::string obj_name
Definition: defs.hpp:123
flexiv::ai::SUPPORTED_KEYS
std::vector< std::string > SUPPORTED_KEYS
flexiv::ai::AIStatus::status_message
std::string status_message
Definition: defs.hpp:163
flexiv::ai::Result
Definition: defs.hpp:143
flexiv::ai::AI_CMD
std::unordered_map< std::string, int > AI_CMD
flexiv::ai::ObjState::ObjState
ObjState()
Definition: defs.hpp:116
flexiv::ai::Result::int_value
int int_value
Definition: defs.hpp:147