151 lines
5.4 KiB
C++
151 lines
5.4 KiB
C++
#include "vslam/height_map.h"
|
|
|
|
#include <Eigen/Sparse>
|
|
#include <Eigen/SparseCholesky>
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <limits>
|
|
|
|
namespace vslam {
|
|
|
|
std::map<int, double> SolveZGraph(const std::map<int, NodeHeight>& meas,
|
|
const std::vector<ZGraphLink>& links,
|
|
const ZGraphConfig& cfg) {
|
|
std::map<int, double> out;
|
|
if (meas.empty()) return out;
|
|
std::map<int, int> index;
|
|
for (const auto& kv : meas) {
|
|
const int k = static_cast<int>(index.size());
|
|
index[kv.first] = k;
|
|
}
|
|
const int n = static_cast<int>(index.size());
|
|
|
|
// Phuong trinh chuan H z = g, H = sum w a a^T.
|
|
std::vector<Eigen::Triplet<double>> trip;
|
|
Eigen::VectorXd g = Eigen::VectorXd::Zero(n);
|
|
auto add_rel = [&](int i, int j, double dz, double sigma) {
|
|
// z_j - z_i = dz
|
|
const double w = 1.0 / (sigma * sigma);
|
|
trip.emplace_back(i, i, w);
|
|
trip.emplace_back(j, j, w);
|
|
trip.emplace_back(i, j, -w);
|
|
trip.emplace_back(j, i, -w);
|
|
g(i) -= w * dz;
|
|
g(j) += w * dz;
|
|
};
|
|
auto add_abs = [&](int i, double z, double sigma) {
|
|
const double w = 1.0 / (sigma * sigma);
|
|
trip.emplace_back(i, i, w);
|
|
g(i) += w * z;
|
|
};
|
|
|
|
for (const auto& kv : meas) add_abs(index[kv.first], kv.second.z, cfg.sigma_prior);
|
|
add_abs(index[meas.begin()->first], meas.begin()->second.z, cfg.sigma_first);
|
|
|
|
for (const ZGraphLink& l : links) {
|
|
auto a = meas.find(l.from), b = meas.find(l.to);
|
|
if (a == meas.end() || b == meas.end() || l.from == l.to) continue;
|
|
const int i = index[l.from], j = index[l.to];
|
|
if (l.neighbor) {
|
|
const double dz = b->second.z - a->second.z;
|
|
add_rel(i, j, dz, cfg.sigma_neighbor + cfg.sigma_rel * std::fabs(dz));
|
|
continue;
|
|
}
|
|
const bool flat_a = std::fabs(a->second.pitch) < cfg.clamp_rad;
|
|
const bool flat_b = std::fabs(b->second.pitch) < cfg.clamp_rad;
|
|
if (flat_a && flat_b) {
|
|
add_rel(i, j, 0.0, cfg.sigma_loop_flat);
|
|
} else {
|
|
// Mat phang doc cua node `from`, keo toi `to`.
|
|
FloorNode fa;
|
|
fa.roll = a->second.roll;
|
|
fa.pitch = a->second.pitch;
|
|
const double dz = PlaneZ(fa, l.dx, l.dy);
|
|
add_rel(i, j, dz, cfg.sigma_loop_slope + cfg.sigma_rel * std::fabs(dz));
|
|
}
|
|
}
|
|
|
|
Eigen::SparseMatrix<double> H(n, n);
|
|
H.setFromTriplets(trip.begin(), trip.end());
|
|
Eigen::SimplicialLDLT<Eigen::SparseMatrix<double>> solver(H);
|
|
if (solver.info() != Eigen::Success) {
|
|
for (const auto& kv : meas) out[kv.first] = kv.second.z;
|
|
return out;
|
|
}
|
|
const Eigen::VectorXd z = solver.solve(g);
|
|
for (const auto& kv : index) out[kv.first] = z(kv.second);
|
|
return out;
|
|
}
|
|
|
|
double PlaneZ(const FloorNode& n, double x, double y) {
|
|
const double dx = x - n.x, dy = y - n.y;
|
|
const double c = std::cos(n.theta), s = std::sin(n.theta);
|
|
const double fwd = c * dx + s * dy;
|
|
const double left = -s * dx + c * dy;
|
|
// Ry(pitch): +x chuc xuong khi pitch duong; Rx(roll): +y len khi roll duong.
|
|
return n.z - fwd * std::tan(n.pitch) + left * std::tan(n.roll);
|
|
}
|
|
|
|
void FloorModel::Build(const std::vector<FloorNode>& nodes, double radius, double cell) {
|
|
nodes_ = nodes;
|
|
radius_ = radius;
|
|
cell_ = cell;
|
|
nearest_.clear();
|
|
w_ = h_ = 0;
|
|
if (nodes_.empty()) return;
|
|
double xmin = std::numeric_limits<double>::max(), ymin = xmin;
|
|
double xmax = std::numeric_limits<double>::lowest(), ymax = xmax;
|
|
for (const FloorNode& f : nodes_) {
|
|
xmin = std::min(xmin, f.x);
|
|
xmax = std::max(xmax, f.x);
|
|
ymin = std::min(ymin, f.y);
|
|
ymax = std::max(ymax, f.y);
|
|
}
|
|
x0_ = xmin - radius - cell;
|
|
y0_ = ymin - radius - cell;
|
|
w_ = static_cast<int>(std::ceil((xmax - xmin + 2 * radius + 2 * cell) / cell));
|
|
h_ = static_cast<int>(std::ceil((ymax - ymin + 2 * radius + 2 * cell) / cell));
|
|
nearest_.assign(static_cast<size_t>(w_) * h_, -1);
|
|
std::vector<float> best(nearest_.size(), std::numeric_limits<float>::max());
|
|
const int rc = static_cast<int>(std::ceil(radius / cell));
|
|
const double r2 = radius * radius;
|
|
for (size_t k = 0; k < nodes_.size(); ++k) {
|
|
const FloorNode& f = nodes_[k];
|
|
const int cx = static_cast<int>((f.x - x0_) / cell), cy = static_cast<int>((f.y - y0_) / cell);
|
|
for (int iy = std::max(0, cy - rc); iy <= std::min(h_ - 1, cy + rc); ++iy) {
|
|
for (int ix = std::max(0, cx - rc); ix <= std::min(w_ - 1, cx + rc); ++ix) {
|
|
const double px = x0_ + (ix + 0.5) * cell, py = y0_ + (iy + 0.5) * cell;
|
|
const double d2 = (px - f.x) * (px - f.x) + (py - f.y) * (py - f.y);
|
|
if (d2 > r2) continue;
|
|
const size_t i = static_cast<size_t>(iy) * w_ + ix;
|
|
if (d2 < best[i]) {
|
|
best[i] = static_cast<float>(d2);
|
|
nearest_[i] = static_cast<int>(k);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
bool FloorModel::NearestNode(double x, double y, FloorNode* node, double* dist) const {
|
|
if (nodes_.empty()) return false;
|
|
const int ix = static_cast<int>(std::floor((x - x0_) / cell_));
|
|
const int iy = static_cast<int>(std::floor((y - y0_) / cell_));
|
|
if (ix < 0 || iy < 0 || ix >= w_ || iy >= h_) return false;
|
|
const int k = nearest_[static_cast<size_t>(iy) * w_ + ix];
|
|
if (k < 0) return false;
|
|
*node = nodes_[static_cast<size_t>(k)];
|
|
if (dist != nullptr) *dist = std::hypot(x - node->x, y - node->y);
|
|
return true;
|
|
}
|
|
|
|
bool FloorModel::FloorAt(double x, double y, double* z, double* dist) const {
|
|
FloorNode f;
|
|
if (!NearestNode(x, y, &f, dist)) return false;
|
|
*z = PlaneZ(f, x, y);
|
|
return true;
|
|
}
|
|
|
|
} // namespace vslam
|