vkgs-sim / tests /cpp /test_sensors.cpp
kimjunhan's picture
Add files using upload-large-folder tool
4c75661 verified
Raw History Blame Contribute Delete
3.25 kB
// sensors.hpp ์‹œํ—˜ (tests/test_sensors_cpp.py ๊ฐ€ ๋ณต์…€ ์›”๋“œ๋ฅผ ๋งŒ๋“ค์–ด ๋„˜๊ธด๋‹ค): ํ‰์ง€(z 0) + x = 20 ์˜ ๋ฒฝ.
#include <cmath>
#include <cstdio>
#include "sensors.hpp"
using namespace vkgs;
#define CHECK(c) \
do { \
if (!(c)) { std::printf("์‹คํŒจ %s:%d %s\n", __FILE__, __LINE__, #c); return 1; } \
} while (0)
int main(int, char** argv) {
VoxelWorld w;
w.load(argv[1]);
// 1) ๊ด‘์„ : ์œ„์—์„œ ์•„๋ž˜๋กœ โ†’ ์ง€๋ฉด ์นธ ์œ„์ชฝ ๋ฉด (z 0 ~ 0.2 ์นธ) ์— ๋‹ฟ๋Š”๋‹ค
float o[3] = {5.1f, 0.1f, 3.0f}, down[3] = {0, 0, -1}, fwd[3] = {1, 0, 0};
float t = w.raycast(o, down, 50);
CHECK(t > 2.7f && t < 3.05f);
float o2[3] = {5.1f, 0.1f, 1.0f};
float tw = w.raycast(o2, fwd, 50); // ๋ฒฝ x = 20
CHECK(std::abs(tw - (20.0f - 5.1f)) < 0.25f);
// 2) ๋ผ์ด๋‹ค: ์ง€๋ฉด ์ ์€ z โ‰ˆ 0, ๋ฒฝ ์ ์€ x โ‰ˆ 20, ์•„๋ž˜ ์ฑ„๋„(โˆ’30.67ยฐ) ์˜ ์ง€๋ฉด ๊ฑฐ๋ฆฌ โ‰ˆ 1.84 / sin(30.67ยฐ)
LidarConfig lc;
lc.noise = 0;
EgoState e; // ์›์ , ๋™์ชฝ(+x)
auto pts = lidar_scan(w, {}, lc, e);
CHECK(pts.size() > 20000);
int ground = 0, wall = 0;
float r0 = 0;
int n0 = 0;
for (const auto& p : pts) {
if (std::abs(p.p[2] - 0.2f) < 0.25f) ++ground;
if (std::abs(p.p[0] - 20.0f) < 0.3f && p.p[2] > 0.5f) ++wall;
if (p.ring == 0) r0 += p.range, ++n0;
}
std::printf("lidar %zu์ , ์ง€๋ฉด %d, ๋ฒฝ %d, 0๋ฒˆ ๋ง ํ‰๊ท  ๊ฑฐ๋ฆฌ %.2f (๊ธฐ๋Œ€ %.2f)\n", pts.size(), ground, wall, r0 / n0,
(1.84f - 0.1f) / std::sin(30.67f * 3.14159265f / 180.0f));
CHECK(ground > 10000 && wall > 500);
CHECK(std::abs(r0 / n0 - (1.84f - 0.1f) / std::sin(30.67f * 3.14159265f / 180.0f)) < 0.1f);
// 3) ๊ฐ์ฒด ์ƒ์ž๊ฐ€ ๋ฒฝ๋ณด๋‹ค ์•ž์— ์žˆ์œผ๋ฉด ๊ฐ€๋ฆฐ๋‹ค
std::vector<SensorBox> boxes{{10.0f, 0.0f, 0.2f, 0.0f, 2.0f, 1.0f, 1.6f, -5.0f, 0.0f}}; // x 10, ๋‹ค๊ฐ€์˜ค๋Š” ์ฐจ (โˆ’5 m/s)
auto pts2 = lidar_scan(w, boxes, lc, e);
int on_box = 0;
for (const auto& p : pts2) on_box += p.object == 0;
CHECK(on_box > 50);
// 4) ๋ ˆ์ด๋” (์•ž): ์ž์ฐจ 10 m/s ๋กœ ์ „์ง„ โ€” ์ •์  ๋ฒฝ์˜ vr โ‰ˆ โˆ’10 cosฮธ, ๋‹ค๊ฐ€์˜ค๋Š” ์ฐจ๋Š” vr โ‰ˆ โˆ’15
RadarConfig rc;
rc.mount = {"RADAR_FRONT", {3.4f, 0, 0.5f}, {0, 0, 0}};
rc.noise_range = rc.noise_vel = 0;
e.v = 10;
auto det = radar_scan(w, boxes, rc, e);
CHECK(!det.empty() && int(det.size()) <= rc.max_points);
bool car = false, wall_ok = true;
for (const auto& d : det) {
if (d.object == 0 && std::abs(d.azimuth) < 3) car = car || std::abs(d.vr + 15.0f) < 0.5f;
if (d.object < 0 && std::abs(d.azimuth) < 30) {
float expect = -10.0f * d.s[0] / std::sqrt(d.s[0] * d.s[0] + d.s[1] * d.s[1] + d.s[2] * d.s[2]);
wall_ok = wall_ok && std::abs(d.vr - expect) < 0.3f;
}
CHECK(d.object >= 0 || d.p[2] >= e.z + rc.min_height - 1e-3f); // ๋…ธ๋ฉด ๋ฐ˜์‚ฌ๋Š” ์—†๋‹ค
}
std::printf("radar %zu์ , ๋‹ค๊ฐ€์˜ค๋Š” ์ฐจ vr โˆ’15 %s, ์ •์  ๋ฐ˜์‚ฌ ๋„ํ”Œ๋Ÿฌ %s\n", det.size(), car ? "ok" : "์—†์Œ", wall_ok ? "ok" : "์–ด๊ธ‹๋‚จ");
CHECK(car && wall_ok);
std::printf("ok\n");
return 0;
}