update
This commit is contained in:
@@ -4,6 +4,9 @@
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
#include <functional>
|
||||
#include <limits>
|
||||
#include <vector>
|
||||
#include <fstream>
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
@@ -736,6 +739,133 @@ StatusResponse RasterizeGazeboWorld(const GazeboWorld& world,
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
StatusResponse BuildWorldSegments(const GazeboWorld& world,
|
||||
const WorldRasterOptions& opt,
|
||||
const OccupancyGrid& grid,
|
||||
WorldSegments* out) {
|
||||
if (out == nullptr) {
|
||||
return StatusResponse::Error(StatusCode::kInvalidArgument, "out == null");
|
||||
}
|
||||
out->seg.clear();
|
||||
out->cell_items.clear();
|
||||
out->cell_start.assign(static_cast<size_t>(grid.width) * grid.height + 1, 0);
|
||||
out->width = grid.width;
|
||||
out->height = grid.height;
|
||||
out->resolution = grid.resolution;
|
||||
out->origin_x = grid.origin.position.x;
|
||||
out->origin_y = grid.origin.position.y;
|
||||
|
||||
const double res = grid.resolution;
|
||||
const double ox = out->origin_x, oy = out->origin_y;
|
||||
const int W = static_cast<int>(grid.width), H = static_cast<int>(grid.height);
|
||||
|
||||
// --- Gom doan tu canh cua tam giac trong dai z ---
|
||||
//
|
||||
// Lay CA BA canh cua moi tam giac. Canh nam trong long vat the (vd canh cua
|
||||
// tam giac mat tren chieu xuong) khong gay hai: chung luon nam XA hon mat
|
||||
// truoc doc theo tia, va phep cat lay t NHO NHAT.
|
||||
std::map<std::string, std::vector<Tri>> cache;
|
||||
for (const WorldModel& m : world.models) {
|
||||
if (m.collision_obj.empty()) continue;
|
||||
auto it = cache.find(m.model_name);
|
||||
if (it == cache.end()) {
|
||||
std::vector<Tri> tris;
|
||||
const bool ok = IsDae(m.collision_obj)
|
||||
? ReadDaeTriangles(m.collision_obj, &tris)
|
||||
: ReadObjTriangles(m.collision_obj, &tris);
|
||||
if (!ok) tris.clear();
|
||||
it = cache.emplace(m.model_name, std::move(tris)).first;
|
||||
}
|
||||
if (it->second.empty()) continue;
|
||||
|
||||
const double cy = std::cos(m.yaw), sny = std::sin(m.yaw);
|
||||
for (const Tri& t0 : it->second) {
|
||||
double X[3], Y[3], Z[3];
|
||||
for (int k = 0; k < 3; ++k) {
|
||||
const double sx = t0.x[k] * m.sx, sy = t0.y[k] * m.sy;
|
||||
X[k] = sx * cy - sy * sny + m.x;
|
||||
Y[k] = sx * sny + sy * cy + m.y;
|
||||
Z[k] = t0.z[k] * m.sz + m.z;
|
||||
}
|
||||
const double zlo = std::min({Z[0], Z[1], Z[2]});
|
||||
const double zhi = std::max({Z[0], Z[1], Z[2]});
|
||||
if (zhi < opt.z_min || zlo > opt.z_max) continue;
|
||||
for (int e = 0; e < 3; ++e) {
|
||||
const int a = e, b = (e + 1) % 3;
|
||||
out->seg.push_back(static_cast<float>(X[a]));
|
||||
out->seg.push_back(static_cast<float>(Y[a]));
|
||||
out->seg.push_back(static_cast<float>(X[b]));
|
||||
out->seg.push_back(static_cast<float>(Y[b]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const size_t n_seg = out->seg.size() / 4;
|
||||
if (n_seg == 0) {
|
||||
return StatusResponse::Error(StatusCode::kNotFound,
|
||||
"khong dung duoc doan nao tu mesh va cham");
|
||||
}
|
||||
|
||||
// --- Chi muc CSR: hai luot, dem roi dien ---
|
||||
//
|
||||
// Moi doan duoc ghi vao MOI O no cat qua, di bang DDA supercover. Dung bao
|
||||
// loi cua doan se re hon nhung mot buc tuong cheo dai 5 m se dinh vao hang
|
||||
// nghin o no khong he cham.
|
||||
auto walk = [&](size_t s, const std::function<void(int, int)>& visit) {
|
||||
double ax = out->seg[s * 4], ay = out->seg[s * 4 + 1];
|
||||
double bx = out->seg[s * 4 + 2], by = out->seg[s * 4 + 3];
|
||||
double px = (ax - ox) / res, py = (ay - oy) / res;
|
||||
const double qx = (bx - ox) / res, qy = (by - oy) / res;
|
||||
int cx = static_cast<int>(std::floor(px));
|
||||
int cyi = static_cast<int>(std::floor(py));
|
||||
const int ex = static_cast<int>(std::floor(qx));
|
||||
const int ey = static_cast<int>(std::floor(qy));
|
||||
const double dx = qx - px, dy = qy - py;
|
||||
const int sx = dx > 0 ? 1 : (dx < 0 ? -1 : 0);
|
||||
const int sy = dy > 0 ? 1 : (dy < 0 ? -1 : 0);
|
||||
const double inf = std::numeric_limits<double>::infinity();
|
||||
const double tdx = dx != 0.0 ? std::fabs(1.0 / dx) : inf;
|
||||
const double tdy = dy != 0.0 ? std::fabs(1.0 / dy) : inf;
|
||||
double tmx = dx != 0.0 ? ((dx > 0 ? (cx + 1 - px) : (px - cx)) * tdx) : inf;
|
||||
double tmy = dy != 0.0 ? ((dy > 0 ? (cyi + 1 - py) : (py - cyi)) * tdy) : inf;
|
||||
for (int guard = 0; guard < 4096; ++guard) {
|
||||
if (cx >= 0 && cyi >= 0 && cx < W && cyi < H) visit(cx, cyi);
|
||||
if (cx == ex && cyi == ey) break;
|
||||
if (tmx < tmy) { tmx += tdx; cx += sx; }
|
||||
else { tmy += tdy; cyi += sy; }
|
||||
if (tmx > 1.0 && tmy > 1.0) {
|
||||
if (cx >= 0 && cyi >= 0 && cx < W && cyi < H) visit(cx, cyi);
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
for (size_t s = 0; s < n_seg; ++s) {
|
||||
walk(s, [&](int cx, int cyi) {
|
||||
out->cell_start[static_cast<size_t>(cyi) * grid.width + cx + 1]++;
|
||||
});
|
||||
}
|
||||
for (size_t i = 1; i < out->cell_start.size(); ++i) {
|
||||
out->cell_start[i] += out->cell_start[i - 1];
|
||||
}
|
||||
out->cell_items.assign(out->cell_start.back(), 0);
|
||||
std::vector<uint32_t> fill(out->cell_start.begin(), out->cell_start.end() - 1);
|
||||
for (size_t s = 0; s < n_seg; ++s) {
|
||||
walk(s, [&](int cx, int cyi) {
|
||||
out->cell_items[fill[static_cast<size_t>(cyi) * grid.width + cx]++] =
|
||||
static_cast<uint32_t>(s);
|
||||
});
|
||||
}
|
||||
|
||||
VSLAM_INFO("world -> doan: " << n_seg << " doan, " << out->cell_items.size()
|
||||
<< " muc chi muc (" << (out->seg.size() * 4 +
|
||||
out->cell_items.size() * 4) / 1048576.0
|
||||
<< " MB)");
|
||||
return StatusResponse::Ok();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
StatusResponse ExportWorldMeshes(const GazeboWorld& world, bool visual,
|
||||
WorldMeshes* out) {
|
||||
if (out == nullptr) {
|
||||
|
||||
Reference in New Issue
Block a user