在弄 g2o 圖優(yōu)化的時(shí)候,需要設(shè)置一條邊的置信度温亲,大概如下:
auto e = new g2o::EdgeSE3ProjectXYZ();
e->setVertex(0, optimizer.vertex(idPoint(pMPt->mnId)));
e->setVertex(1, optimizer.vertex(idImu__(0)));
e->setMeasurement(...);
e->setInformation(Eigen::Matrix2d::Identity() * pF->confidence); // 這一行很蛋疼
e->setRobustKernel(newRobustKernel());
那行設(shè)置 information 矩陣蛋疼在于哟沫,這個(gè) info mat 的維度取決于你的 error 的維度。
也就是說如果 error 是 double[2]
变泄,你就 Eigen::Matrix2d
鸠信,如果 double[4]
汁掠,則是
Eigen::Matrix4d
孔祸。很多時(shí)候容易搞錯(cuò)脂男。然后 Eigen 就報(bào)錯(cuò)一堆東西出來(可能還不告訴你哪一行掛了)。
回顧一下 g2o 邊的定義:
class EdgeSE3ProjectXYZ : public BaseBinaryEdge<2, Vector2d, VertexSBAPointXYZ, VertexSE3Expmap> // ...
這里 2 是 error 的維度(可能“維度”有歧義蛙卤,就這個(gè)意思你懂得)狠半;
Vector2d
是邊的測(cè)量值類型
(通過edge->setMeamurement
設(shè)置進(jìn)去);后面就是兩個(gè)頂點(diǎn)類型颤难,分別對(duì)應(yīng) vertex 0 和 vertex 1神年。
昨天寫了一個(gè)類型推導(dǎo),這樣就能自己從 Edge 類型里面推導(dǎo)出 info mat 的類型行嗤,不用自己去改
Matrix2d
Matrix4d
這種東西已日。我查了挺久才找到我要的文檔:http://en.cppreference.com/w/cpp/language/sfinae。
寫好這個(gè)自動(dòng)推導(dǎo)后栅屏,設(shè)置 info mat 只需要這樣調(diào)用:
e->setInformation(utils::helpers::information(f->confidence, e));
有沒有覺得用起來很爽飘千?
這里是實(shí)現(xiàn):
template <typename T> struct E
{
typedef typename T::InformationType InformationType;
};
template <typename T, typename W, typename = typename T::InformationType,
typename U = typename E<T>::InformationType>
U information(W c, const T *t)
{
return U::Identity() * c;
}