VisionServer  v2.1.1-1-g21dc5465
FRC vision library
calib.h
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
4#include <array>
5#include <string>
6#include <map>
7#include <unordered_map>
8
9#include <opencv2/core/types.hpp>
10#include <wpi/json.h>
11#include <wpi/StringMap.h>
12
13
14template<typename first_T, typename second_T>
15using VecMap = std::vector<std::pair<first_T, second_T> >;
16
17#ifndef CALIB_DEFAULT_FLOAT_TYPE
18#define CALIB_DEFAULT_FLOAT_TYPE float
19#endif
20#ifndef CALIB_DEFAULT_STRING_TYPE
21#define CALIB_DEFAULT_STRING_TYPE std::string
22#endif
23#ifndef CALIB_DEFAULT_MAP_TYPE
24#define CALIB_DEFAULT_MAP_TYPE VecMap
25#endif
26#ifndef CALIB_DEFAULT_SMAP_TYPE
27#define CALIB_DEFAULT_SMAP_TYPE wpi::StringMap
28#endif
29
30// template<
31// typename str_T = CALIB_DEFAULT_STRING_TYPE,
32// template<typename...> typename map_T = CALIB_DEFAULT_MAP_TYPE>
33// struct StringMapGen {
34// template<typename V, typename S = str_T>
35// using type = map_T<S, V>;
36// };
37// template<typename str_T = CALIB_DEFAULT_STRING_TYPE>
38// using VecSMap_ = StringMapGen<str_T, VecMap>::type;
39// typedef VecSMap_<> VecSMap;
40// template<typename str_T = CALIB_DEFAULT_STRING_TYPE>
41// using StdSMap_ = StringMapGen<str_T, std::map>::type;
42// typedef StdSMap_<> StdSMap;
43// template<typename str_T = CALIB_DEFAULT_STRING_TYPE>
44// using StdSUmap_ = StringMapGen<str_T, std::unordered_map>::type;
45// typedef StdSUmap_<> StdSUmap;
46
47
48template<
49 typename fp_T = CALIB_DEFAULT_FLOAT_TYPE>
51std::array<cv::Mat_<fp_T>, 2>; // a camera matx and dist coeff matx pair
53
54struct __CalibSet { protected: __CalibSet() {} };
55template<
56 typename fp_T = CALIB_DEFAULT_FLOAT_TYPE,
57 template<class, class, class...> typename map_T = CALIB_DEFAULT_MAP_TYPE>
58struct CalibSet_ : public map_T<cv::Size2i, Calibration_<fp_T> >, __CalibSet { // map of resolution <--> calibration
59 using map_T<cv::Size2i, Calibration_<fp_T> >::map_T;
60
61 template<
62 typename F,
63 template<class, class, class...> typename M>
65
67 using Set_T = map_T<cv::Size2i, Calibration_<fp_T> >;
68 typedef Set_T Super;
69
70 using Float_T = fp_T;
71 template<class A, class B>
72 using CalMap_T = map_T<A, B>;
73};
75
76struct __CalibMap { protected: __CalibMap() {} };
77struct __CalibSMap : __CalibMap { protected: __CalibSMap() {} };
78template<
79 typename fp_T = CALIB_DEFAULT_FLOAT_TYPE,
80 template<class, class, class...> typename cmap_T = CALIB_DEFAULT_MAP_TYPE,
81 template<class, class...> typename smap_T = CALIB_DEFAULT_SMAP_TYPE>
82struct CalibSMap_ : public smap_T<CalibSet_<fp_T, cmap_T> >, __CalibSMap { // stringmap of calibration sets
83 using smap_T<CalibSet_<fp_T, cmap_T> >::smap_T;
84
85 template<
86 typename F,
87 template<class, class, class...> typename C,
88 template<class, class...> typename S>
90
93 using Map_T = smap_T<CalibSet_<fp_T, cmap_T> >;
94 typedef Map_T Super;
95
96 using Float_T = fp_T;
97 template<class A, class B>
98 using CalMap_T = cmap_T<A, B>;
99 template<class A>
100 using StrMap_T = smap_T<A>;
101};
103
104struct __CalibMat : __CalibMap { protected: __CalibMat() {} };
105template<
106 typename fp_T = CALIB_DEFAULT_FLOAT_TYPE,
107 typename str_T = CALIB_DEFAULT_STRING_TYPE,
108 template<class, class, class...> typename cmap_T = CALIB_DEFAULT_MAP_TYPE,
109 template<class, class, class...> typename nmap_T = CALIB_DEFAULT_MAP_TYPE>
110struct CalibMat_ : public nmap_T<str_T, CalibSet_<fp_T, cmap_T> >, __CalibMat { // normal outer map type with string key type
111 using nmap_T<str_T, CalibSet_<fp_T, cmap_T> >::nmap_T;
112
113 template<
114 typename F, typename S,
115 template<class, class, class...> typename C,
116 template<class, class...> typename N>
118
121 using Map_T = nmap_T<str_T, CalibSet_<fp_T, cmap_T> >;
122 typedef Map_T Super;
123
124 using Float_T = fp_T;
125 using String_T = str_T;
126 template<class A, class B>
127 using CalMap_T = cmap_T<A, B>;
128 template<class A, class B>
129 using NameMap_T = nmap_T<A, B>;
130};
132
133// template<
134// typename fp_T = CALIB_DEFAULT_FLOAT_TYPE,
135// template<typename, typename> typename map_T = CALIB_DEFAULT_MAP_TYPE>
136// using CalibSet_ =
137// map_T<cv::Size2i, Calibration_<fp_T> >; // map of resolution <--> calibration
138// typedef CalibSet_<> CalibSet;
139
140// template<
141// typename fp_T = CALIB_DEFAULT_FLOAT_TYPE,
142// template<typename, typename> typename cmap_T = CALIB_DEFAULT_MAP_TYPE,
143// template<typename> typename smap_T = CALIB_DEFAULT_SMAP_TYPE>
144// using CalibSMap_ =
145// smap_T<CalibSet_<fp_T, cmap_T> >; // stringmap of calibration sets
146// typedef CalibSMap_<> CalibSMap;
147
148// template<
149// typename fp_T = CALIB_DEFAULT_FLOAT_TYPE,
150// typename str_T = CALIB_DEFAULT_STRING_TYPE,
151// template<typename, typename> typename cmap_T = CALIB_DEFAULT_MAP_TYPE,
152// template<typename, typename> typename nmap_T = CALIB_DEFAULT_MAP_TYPE>
153// using CalibMat_ =
154// nmap_T<str_T, CalibSet_<fp_T, cmap_T> >; // normal outer map type with string key type
155// typedef CalibMat_<> CalibMat;
156
157template<
158 typename fp_T = CALIB_DEFAULT_FLOAT_TYPE,
159 typename str_T = CALIB_DEFAULT_STRING_TYPE,
160 template<typename, typename> typename map_T = CALIB_DEFAULT_MAP_TYPE>
162CalibMat_<fp_T, str_T, map_T, map_T>; // ^ but the same map type for inner and outer
164
165template<typename fp_T = CALIB_DEFAULT_FLOAT_TYPE>
166using CalibSList_ = CalibSMap_<fp_T, VecMap, wpi::StringMap>; // stringmap but with internal map as vecmap
168
169template<
170 typename fp_T = CALIB_DEFAULT_FLOAT_TYPE,
171 typename str_T = CALIB_DEFAULT_STRING_TYPE>
172using CalibList_ = CalibMap_<fp_T, str_T, VecMap>; // both maps are vecmap
174
175
176
177/* Searches for calibrations given the following json layout:
178 "calibrations" : {
179 "<camera type>" : {
180 "<WIDTH>x<HEIGHT>" : {
181 "camera_matrix" : [ [x3], [x3], [x3] ],
182 "distortion" : [ [x5] ]
183 },
184 ...
185 },
186 ...
187 }
188*/
189// template<
190// typename fp_T = CALIB_DEFAULT_FLOAT_TYPE,
191// typename str_T = CALIB_DEFAULT_STRING_TYPE,
192// template<class...> typename map_T = CALIB_DEFAULT_MAP_TYPE>
193// int loadJsonCalibs(const wpi::json& src, CalibMap_<fp_T, str_T, map_T>& calibs) {
194// using CalibMap_T = CalibMap_<fp_T, str_T, map_T>;
195// using CalibSet_T = CalibSet_<fp_T, map_T>;
196// using Calib_T = Calibration_<fp_T>;
197// // test map implementation qualifiers
198// int count = -1;
199// if((count = src.count("calibrations")) > 0) {
200// for(auto& calib : src.at("calibrations")) {
201
202// }
203// }
204// return count;
205// }
206
207#include <charconv>
208template<typename num_T = int>
209cv::Size_<num_T> extractSize(std::string_view str) {
210 cv::Size_<num_T> ret;
211 size_t a = str.find('x');
212 std::from_chars<num_T>(str.data(), str.data() + a, std::ref(ret.width));
213 std::from_chars<num_T>(str.data() + a + 1, str.data() + str.length(), std::ref(ret.height));
214 return ret;
215}
216
217
218#include <type_traits>
219#include <cpp-tools/src/types.h>
220template<typename>
221struct CalProps {
222 using Float_T = void;
223 using String_T = void;
224
225 using Cal_T = void;
226 using Set_T = void;
227 using Map_T = void;
228
229 static constexpr inline bool
230 is_wpi_smap = false,
231 is_seq_smap = false,
233 is_seq_cmap = false,
235 ;
236};
237template<
238 typename fp_T,
239 template<class, class, class...> typename cmap_T>
240struct CalProps<CalibSet_<fp_T, cmap_T>> {
241 static_assert(std::is_floating_point<fp_T>::value, "Template param error: fp_T must be floating point type.");
242
243 using Float_T = fp_T;
244 using String_T = void;
245
248 using Map_T = void;
249
250 static constexpr inline bool
251 is_wpi_smap = false,
252 is_seq_smap = false,
254 is_seq_cmap = is_same_template<cmap_T, VecMap>::value,
255 is_assoc_cmap = any_same_template<cmap_T, std::map, std::unordered_map>::value
256 ;
257};
258template<
259 typename fp_T,
260 template<class, class, class...> typename cmap_T,
261 template<class, class...> typename smap_T>
262struct CalProps<CalibSMap_<fp_T, cmap_T, smap_T>> {
263 static_assert(std::is_floating_point<fp_T>::value, "Template param error: fp_T must be floating point type.");
264
265 using Float_T = fp_T;
267
271
272 static constexpr inline bool
273 is_wpi_smap = is_same_template<smap_T, wpi::StringMap>::value,
274 is_seq_smap = false,
276 is_seq_cmap = is_same_template<cmap_T, VecMap>::value,
277 is_assoc_cmap = any_same_template<cmap_T, std::map, std::unordered_map>::value
278 ;
279};
280template<
281 typename fp_T,
282 typename str_T,
283 template<class, class, class...> typename cmap_T,
284 template<class, class, class...> typename nmap_T>
285struct CalProps<CalibMat_<fp_T, str_T, cmap_T, nmap_T>> {
286 static_assert(std::is_floating_point<fp_T>::value, "Template param error: fp_T must be floating point type.");
287
288 using Float_T = fp_T;
289 using String_T = str_T;
290
294
295 static constexpr inline bool
296 is_wpi_smap = false,
297 is_seq_smap = is_same_template<nmap_T, VecMap>::value,
298 is_assoc_smap = any_same_template<nmap_T, std::map, std::multimap, std::unordered_map, std::unordered_multimap>::value,
299 is_seq_cmap = is_same_template<cmap_T, VecMap>::value,
300 is_assoc_cmap = any_same_template<cmap_T, std::map, std::multimap, std::unordered_map, std::unordered_multimap>::value
301 ;
302};
303
304
305
306template<typename Map>
307const typename Map::Set_T* findSet(std::string_view name, const Map& map) {
308 static_assert(std::is_base_of<__CalibMap, Map>::value, "Template param error - Map must be CalibMap type.");
309 if constexpr (CalProps<Map>::is_wpi_smap) {
310 return wpi_find_set_impl<Map>(name, map);
311 }
312 else if constexpr (CalProps<Map>::is_seq_smap) {
313 return seq_find_set_impl<Map>(name, map);
314 }
315 else if constexpr (CalProps<Map>::is_assoc_smap) {
316 return assoc_find_set_impl<Map>(name, map);
317 }
318 return nullptr;
319}
320
321template<typename Set>
322const typename Set::Cal_T* findCalib(const cv::Size2i& sz, const Set& set) {
323 static_assert(std::is_base_of<__CalibSet, Set>::value, "Template param error - Set must be CalibSet type.");
324 if constexpr (CalProps<Set>::is_seq_cmap) {
325 return seq_find_calib_impl<Set>(sz, set);
326 }
327 else if constexpr (CalProps<Set>::is_assoc_cmap) {
328 return assoc_find_calib_impl<Set>(sz, set);
329 }
330 return nullptr;
331}
332template<typename Map>
333const typename Map::Cal_T* findCalib(std::string_view name, const cv::Size2i& sz, const Map& map) {
334 static_assert(std::is_base_of<__CalibMap, Map>::value, "Template param error - Map must be CalibMap type.");
335 if (auto* set = findSet<Map>(name, map)) {
336 return findCalib<typename Map::Set_T>(sz, *set);
337 }
338 return nullptr;
339}
340template<typename Map>
341const typename Map::Cal_T* findAnyCalib(
342 std::initializer_list<std::string_view> names, const cv::Size2i& sz, const Map& map
343) {
344 static_assert(std::is_base_of<__CalibMap, Map>::value, "Template param error - Map must be CalibMap type.");
345 for (auto n : names) {
346 if (auto* set = findSet<Map>(n, map)) {
347 if (auto* cal = findCalib<typename Map::Set_T>(sz, *set)) {
348 return cal;
349 }
350 }
351 }
352 return nullptr;
353}
354
355
356template<typename Map>
357const typename Map::Set_T* wpi_find_set_impl(std::string_view name, const Map& map) {
358 static_assert(CalProps<Map>::is_wpi_smap, "Template param error - Map must have wpi::StringMap as smap type.");
359 auto it = map.find(name);
360 if (it != map.end()) {
361 return &it->second;
362 }
363 return nullptr;
364}
365template<typename Map>
366const typename Map::Set_T* seq_find_set_impl(std::string_view name, const Map& map) {
367 static_assert(CalProps<Map>::is_seq_smap, "Template param error - Map must have sequential map type.");
368 for(auto& set : map) {
369 if(std::string_view(set.first) == name) {
370 return &set.second;
371 }
372 }
373 return nullptr;
374}
375template<typename Map>
376const typename Map::Set_T* assoc_find_set_impl(std::string_view name, const Map& map) {
377 static_assert(CalProps<Map>::is_assoc_smap, "Template param error - Map must have associative map type.");
378 // auto it = map.find(name);
379 // if (it != map.end()) {
380 // return &it->second;
381 // }
382 return nullptr;
383}
384
385template<typename Set>
386const typename Set::Cal_T* seq_find_calib_impl(const cv::Size2i& sz, const Set& set) {
387 static_assert(CalProps<Set>::is_seq_cmap, "Template param error - Set must have sequential map type.");
388 for (auto& calib : set) {
389 if (calib.first == sz) {
390 return &calib.second;
391 }
392 }
393 return nullptr;
394}
395template<typename Set>
396const typename Set::Cal_T* assoc_find_calib_impl(const cv::Size2i& sz, const Set& set) {
397 static_assert(CalProps<Set>::is_assoc_cmap, "Template param error - Set must have associative map type.");
398 auto it = set.find(sz);
399 if (it != set.end()) {
400 return &it->second;
401 }
402 return nullptr;
403}
CalibSMap_ CalibSMap
Definition: calib.h:102
const Map::Set_T * assoc_find_set_impl(std::string_view name, const Map &map)
Definition: calib.h:376
const Set::Cal_T * findCalib(const cv::Size2i &sz, const Set &set)
Definition: calib.h:322
const Map::Set_T * findSet(std::string_view name, const Map &map)
Definition: calib.h:307
CalibSList_ CalibSList
Definition: calib.h:167
const Set::Cal_T * seq_find_calib_impl(const cv::Size2i &sz, const Set &set)
Definition: calib.h:386
std::array< cv::Mat_< fp_T >, 2 > Calibration_
Definition: calib.h:51
CalibList_ CalibList
Definition: calib.h:173
const Map::Set_T * wpi_find_set_impl(std::string_view name, const Map &map)
Definition: calib.h:357
std::vector< std::pair< first_T, second_T > > VecMap
Definition: calib.h:15
const Set::Cal_T * assoc_find_calib_impl(const cv::Size2i &sz, const Set &set)
Definition: calib.h:396
const Map::Set_T * seq_find_set_impl(std::string_view name, const Map &map)
Definition: calib.h:366
const Map::Cal_T * findAnyCalib(std::initializer_list< std::string_view > names, const cv::Size2i &sz, const Map &map)
Definition: calib.h:341
CalibSet_ CalibSet
Definition: calib.h:74
#define CALIB_DEFAULT_FLOAT_TYPE
Definition: calib.h:18
CalibMap_ CalibMap
Definition: calib.h:163
#define CALIB_DEFAULT_SMAP_TYPE
Definition: calib.h:27
Calibration_ Calibration
Definition: calib.h:52
CalibMat_ CalibMat
Definition: calib.h:131
#define CALIB_DEFAULT_STRING_TYPE
Definition: calib.h:21
cv::Size_< num_T > extractSize(std::string_view str)
Definition: calib.h:209
#define CALIB_DEFAULT_MAP_TYPE
Definition: calib.h:24
CALIB_DEFAULT_STRING_TYPE String_T
Definition: calib.h:266
Calibration_< fp_T > Cal_T
Definition: calib.h:246
static constexpr bool is_seq_smap
Definition: calib.h:231
void String_T
Definition: calib.h:223
static constexpr bool is_seq_cmap
Definition: calib.h:233
void Map_T
Definition: calib.h:227
static constexpr bool is_assoc_smap
Definition: calib.h:232
void Cal_T
Definition: calib.h:225
void Float_T
Definition: calib.h:222
void Set_T
Definition: calib.h:226
static constexpr bool is_assoc_cmap
Definition: calib.h:234
static constexpr bool is_wpi_smap
Definition: calib.h:230
cmap_T< A, B > CalMap_T
Definition: calib.h:127
str_T String_T
Definition: calib.h:125
Map_T Super
Definition: calib.h:122
nmap_T< str_T, CalibSet_< fp_T, cmap_T > > Map_T
Definition: calib.h:121
nmap_T< A, B > NameMap_T
Definition: calib.h:129
Calibration_< fp_T > Cal_T
Definition: calib.h:119
fp_T Float_T
Definition: calib.h:124
smap_T< A > StrMap_T
Definition: calib.h:100
cmap_T< A, B > CalMap_T
Definition: calib.h:98
Map_T Super
Definition: calib.h:94
Calibration_< fp_T > Cal_T
Definition: calib.h:91
smap_T< CalibSet_< fp_T, cmap_T > > Map_T
Definition: calib.h:93
fp_T Float_T
Definition: calib.h:96
map_T< cv::Size2i, Calibration_< fp_T > > Set_T
Definition: calib.h:67
map_T< A, B > CalMap_T
Definition: calib.h:72
Set_T Super
Definition: calib.h:68
fp_T Float_T
Definition: calib.h:70
Calibration_< fp_T > Cal_T
Definition: calib.h:66
__CalibMap()
Definition: calib.h:76
__CalibMat()
Definition: calib.h:104
__CalibSMap()
Definition: calib.h:77
__CalibSet()
Definition: calib.h:54