|
| 1 | + |
| 2 | +#include "threepp/extras/imgui/ImguiContext.hpp" |
| 3 | +#include "threepp/helpers/AxesHelper.hpp" |
| 4 | +#include "threepp/helpers/DepthSensor.hpp" |
| 5 | +#include "threepp/helpers/LidarSensor.hpp" |
| 6 | +#include "threepp/objects/Points.hpp" |
| 7 | +#include "threepp/threepp.hpp" |
| 8 | + |
| 9 | +#include <cmath> |
| 10 | +#include <cstdlib> |
| 11 | + |
| 12 | +using namespace threepp; |
| 13 | + |
| 14 | +namespace { |
| 15 | + |
| 16 | + // Build a simple scene: ground plane + scattered boxes |
| 17 | + void setupScene(Scene& scene) { |
| 18 | + // Ground |
| 19 | + auto ground = Mesh::create( |
| 20 | + BoxGeometry::create(30, 0.2f, 30), |
| 21 | + MeshStandardMaterial::create({{"color", Color(0x888888)}})); |
| 22 | + scene.add(ground); |
| 23 | + |
| 24 | + // Random boxes |
| 25 | + std::srand(42); |
| 26 | + auto boxMat = MeshStandardMaterial::create({{"color", Color(0x4488cc)}}); |
| 27 | + for (int i = 0; i < 20; ++i) { |
| 28 | + float w = 0.5f + (std::rand() % 100) / 50.f; |
| 29 | + float h = 0.5f + (std::rand() % 100) / 25.f; |
| 30 | + float d = 0.5f + (std::rand() % 100) / 50.f; |
| 31 | + auto box = Mesh::create(BoxGeometry::create(w, h, d), boxMat); |
| 32 | + box->position.set( |
| 33 | + (std::rand() % 240 - 120) / 10.f, |
| 34 | + h / 2.f + 0.1f, |
| 35 | + (std::rand() % 240 - 120) / 10.f); |
| 36 | + scene.add(box); |
| 37 | + } |
| 38 | + |
| 39 | + // Lights |
| 40 | + scene.add(AmbientLight::create(0xffffff, 0.4f)); |
| 41 | + auto dirLight = DirectionalLight::create(0xffffff, 1.f); |
| 42 | + dirLight->position.set(5, 10, 5); |
| 43 | + scene.add(dirLight); |
| 44 | + } |
| 45 | + |
| 46 | + // Update a Points object's position and color attributes from a point cloud. |
| 47 | + // Colors are mapped by distance: near=green, far=red. |
| 48 | + void updatePointCloud(const Points& points, const std::vector<Vector3>& cloud, |
| 49 | + const Vector3& sensorPos, float maxDist) { |
| 50 | + |
| 51 | + auto& geom = *points.geometry(); |
| 52 | + auto* posAttr = geom.getAttribute<float>("position"); |
| 53 | + auto* colAttr = geom.getAttribute<float>("color"); |
| 54 | + |
| 55 | + Color c; |
| 56 | + int i = 0; |
| 57 | + for (const auto& p : cloud) { |
| 58 | + posAttr->setXYZ(i, p.x, p.y, p.z); |
| 59 | + |
| 60 | + c.setHSL(0.33f * (1.f - std::min(p.distanceTo(sensorPos) / maxDist, 1.f)), 1.f, 0.5f); |
| 61 | + colAttr->setXYZ(i, c.r, c.g, c.b); |
| 62 | + |
| 63 | + ++i; |
| 64 | + } |
| 65 | + |
| 66 | + geom.setDrawRange(0, i); |
| 67 | + posAttr->needsUpdate(); |
| 68 | + colAttr->needsUpdate(); |
| 69 | + } |
| 70 | + |
| 71 | +}// namespace |
| 72 | + |
| 73 | +int main() { |
| 74 | + |
| 75 | + Canvas canvas("Lidar", {{"antialiasing", 4}}); |
| 76 | + GLRenderer renderer(canvas.size()); |
| 77 | + |
| 78 | + auto scene = Scene::create(); |
| 79 | + scene->background = Color(0x111122); |
| 80 | + |
| 81 | + auto camera = PerspectiveCamera::create(60, canvas.aspect(), 0.1f, 200.f); |
| 82 | + camera->position.set(0, 12, 18); |
| 83 | + |
| 84 | + setupScene(*scene); |
| 85 | + |
| 86 | + // --- Lidar sensor --- |
| 87 | + auto lidar = std::make_unique<LidarSensor>(LidarModel::OS0_128(), 512, 0.5f, 20.f); |
| 88 | + lidar->position.set(0, 2, 0); |
| 89 | + scene->add(*lidar); |
| 90 | + |
| 91 | + OrbitControls controls{*camera, canvas}; |
| 92 | + |
| 93 | + // --- Point cloud visualisation --- |
| 94 | + const size_t maxPoints = 6 * std::pow(lidar->faceSize(), 2); |
| 95 | + auto pcGeom = BufferGeometry::create(); |
| 96 | + pcGeom->setAttribute("position", FloatBufferAttribute::create(std::vector<float>(maxPoints * 3), 3)); |
| 97 | + pcGeom->setAttribute("color", FloatBufferAttribute::create(std::vector<float>(maxPoints * 3), 3)); |
| 98 | + pcGeom->getAttribute<float>("position")->setUsage(DrawUsage::Dynamic); |
| 99 | + pcGeom->getAttribute<float>("color")->setUsage(DrawUsage::Dynamic); |
| 100 | + |
| 101 | + auto pcMaterial = PointsMaterial::create({{"size", 0.1f}, {"vertexColors", true}}); |
| 102 | + auto points = Points::create(pcGeom, pcMaterial); |
| 103 | + points->layers.set(1); |
| 104 | + points->frustumCulled = false; |
| 105 | + scene->add(points); |
| 106 | + |
| 107 | + |
| 108 | + const char* modeNames[] = {"Dense Grid", "VLP-16", "HDL-32E", "OS1-64", "OS0-128"}; |
| 109 | + int currentMode = 4;// start on OS0-128 |
| 110 | + |
| 111 | + auto changeLidar = [&](std::unique_ptr<LidarSensor> newLidar) { |
| 112 | + scene->remove(*lidar); |
| 113 | + newLidar->rangeNoise = lidar->rangeNoise; |
| 114 | + newLidar->position.copy(lidar->position); |
| 115 | + newLidar->rotation.copy(lidar->rotation); |
| 116 | + lidar = std::move(newLidar); |
| 117 | + scene->add(*lidar); |
| 118 | + }; |
| 119 | + |
| 120 | + bool senorDataOnly = false; |
| 121 | + ImguiFunctionalContext ui(canvas, [&] { |
| 122 | + ImGui::SetNextWindowPos({}); |
| 123 | + ImGui::SetNextWindowSize({}); |
| 124 | + ImGui::Begin("Settings"); |
| 125 | + ImGui::Checkbox("Show senor data only", &senorDataOnly); |
| 126 | + ImGui::SliderFloat("Range noise", &lidar->rangeNoise, 0.f, 0.1f); |
| 127 | + |
| 128 | + int prevMode = currentMode; |
| 129 | + ImGui::Combo("Mode", ¤tMode, modeNames, 5); |
| 130 | + if (currentMode != prevMode) { |
| 131 | + switch (currentMode) { |
| 132 | + case 0: changeLidar(std::make_unique<LidarSensor>(64, 0.5f, 20.f)); break; |
| 133 | + case 1: changeLidar(std::make_unique<LidarSensor>(LidarModel::VLP16(), 512, 0.5f, 20.f)); break; |
| 134 | + case 2: changeLidar(std::make_unique<LidarSensor>(LidarModel::HDL32E(), 512, 0.5f, 20.f)); break; |
| 135 | + case 3: changeLidar(std::make_unique<LidarSensor>(LidarModel::OS1_64(), 512, 0.5f, 20.f)); break; |
| 136 | + case 4: changeLidar(std::make_unique<LidarSensor>(LidarModel::OS0_128(), 512, 0.5f, 20.f)); break; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + ImGui::End(); |
| 141 | + }); |
| 142 | + |
| 143 | + IOCapture capture; |
| 144 | + capture.preventMouseEvent = [] { |
| 145 | + return ImGui::GetIO().WantCaptureMouse; |
| 146 | + }; |
| 147 | + canvas.setIOCapture(&capture); |
| 148 | + |
| 149 | + canvas.onWindowResize([&](WindowSize size) { |
| 150 | + camera->aspect = size.aspect(); |
| 151 | + camera->updateProjectionMatrix(); |
| 152 | + renderer.setSize(size); |
| 153 | + }); |
| 154 | + |
| 155 | + Clock clock; |
| 156 | + std::vector<Vector3> cloud; |
| 157 | + std::vector<Color> colors; |
| 158 | + canvas.animate([&] { |
| 159 | + const float t = clock.getElapsedTime(); |
| 160 | + |
| 161 | + // Slowly sweep the sensor in yaw and pitch |
| 162 | + lidar->rotation.y = t * 0.4f; |
| 163 | + lidar->rotation.x = -0.4f + 0.25f * std::sin(t * 0.3f); |
| 164 | + |
| 165 | + // Scan the scene and update the visualised point cloud |
| 166 | + points->visible = false; |
| 167 | + colors.clear(); |
| 168 | + lidar->scan(renderer, *scene, cloud); |
| 169 | + points->visible = true; |
| 170 | + |
| 171 | + Vector3 sensorWorld; |
| 172 | + lidar->getWorldPosition(sensorWorld); |
| 173 | + updatePointCloud(*points, cloud, sensorWorld, lidar->far()); |
| 174 | + |
| 175 | + if (senorDataOnly) { |
| 176 | + camera->layers.set(1); |
| 177 | + } else { |
| 178 | + camera->layers.enableAll(); |
| 179 | + } |
| 180 | + |
| 181 | + renderer.render(*scene, *camera); |
| 182 | + ui.render(); |
| 183 | + }); |
| 184 | +} |
0 commit comments