|
| 1 | +/* _ |
| 2 | + _ __ ___ ___ | | __ _ |
| 3 | +| '_ ` _ \ / _ \| |/ _` | Modular Optimization framework for |
| 4 | +| | | | | | (_) | | (_| | Localization and mApping (MOLA) |
| 5 | +|_| |_| |_|\___/|_|\__,_| https://github.com/MOLAorg/mola |
| 6 | +
|
| 7 | + Copyright (C) 2018-2026 Jose Luis Blanco, University of Almeria, |
| 8 | + and individual contributors. |
| 9 | + SPDX-License-Identifier: GPL-3.0 |
| 10 | + See LICENSE for full license information. |
| 11 | +*/ |
| 12 | + |
| 13 | +/** |
| 14 | + * @file mm-ipc-bake-kdtree/main.cpp |
| 15 | + * @brief CLI tool to bake the k-d tree index of IncrementalPointCloud layers |
| 16 | + * into a .mm file so it does not have to be rebuilt (an O(N log N) |
| 17 | + * bulk build) every time the map is loaded. |
| 18 | + * @author Jose Luis Blanco Claraco |
| 19 | + * @date Jul 31, 2026 |
| 20 | + */ |
| 21 | + |
| 22 | +#include <mola_metric_maps/IncrementalPointCloud.h> |
| 23 | +#include <mp2p_icp/metricmap.h> |
| 24 | + |
| 25 | +#include <CLI/CLI.hpp> |
| 26 | +#include <iostream> |
| 27 | +#include <stdexcept> |
| 28 | + |
| 29 | +#include "../mm_cli_utils.h" |
| 30 | + |
| 31 | +namespace |
| 32 | +{ |
| 33 | +struct Args |
| 34 | +{ |
| 35 | + std::string input; |
| 36 | + std::string output; |
| 37 | + std::string layer; // empty: process all IncrementalPointCloud layers |
| 38 | + std::string plugins; |
| 39 | + bool disable = false; // if set, strip the cached k-d tree instead of adding it |
| 40 | + bool kdtree = true; // bake the k-d tree index |
| 41 | +}; |
| 42 | + |
| 43 | +void run_bake(const Args& args) |
| 44 | +{ |
| 45 | +#if !defined(MOLA_METRIC_MAPS_HAS_INCREMENTAL_KDTREE_BAKE) |
| 46 | + if (!args.disable && args.kdtree) |
| 47 | + { |
| 48 | + std::cout << "[mm-ipc-bake-kdtree] WARNING: this build's nanoflann lacks the incremental " |
| 49 | + "index's save/load API (requires nanoflann >= 1.11.0); k-d tree baking is a " |
| 50 | + "no-op and no k-d tree data will be written." |
| 51 | + << std::endl; |
| 52 | + } |
| 53 | +#endif |
| 54 | + |
| 55 | + mp2p_icp::metric_map_t mm = mola::mm_cli::loadMap(args.input, args.plugins, "mm-ipc-bake-kdtree"); |
| 56 | + |
| 57 | + const size_t numProcessed = mola::mm_cli::forEachMapLayerOfType<mola::IncrementalPointCloud>( |
| 58 | + mm, args.layer, "mola::IncrementalPointCloud", |
| 59 | + [&](const std::string& layerName, const mola::IncrementalPointCloud::Ptr& ipcMap, |
| 60 | + mrpt::maps::CMetricMap::Ptr& /*layerSlot*/) |
| 61 | + { |
| 62 | + ipcMap->creationOptions.serialize_kdtree = !args.disable && args.kdtree; |
| 63 | + std::cout << "[mm-ipc-bake-kdtree] Layer '" << layerName << "': serialize_kdtree -> " |
| 64 | + << (ipcMap->creationOptions.serialize_kdtree ? "true" : "false") |
| 65 | + << " (cached data is (re)built on save only if not already current)." |
| 66 | + << std::endl; |
| 67 | + }); |
| 68 | + |
| 69 | + std::cout << "[mm-ipc-bake-kdtree] Writing output map to: '" << args.output << "'..." |
| 70 | + << std::endl; |
| 71 | + if (!mm.save_to_file(args.output)) |
| 72 | + { |
| 73 | + throw std::runtime_error("Error writing output map file: '" + args.output + "'"); |
| 74 | + } |
| 75 | + |
| 76 | + std::cout << "[mm-ipc-bake-kdtree] Done. Processed " << numProcessed << " layer(s)." << std::endl; |
| 77 | +} |
| 78 | +} // namespace |
| 79 | + |
| 80 | +int main(int argc, char** argv) |
| 81 | +{ |
| 82 | + try |
| 83 | + { |
| 84 | + CLI::App cli{ |
| 85 | + "mm-ipc-bake-kdtree: cache the IncrementalPointCloud k-d tree index inside a .mm file so " |
| 86 | + "it is not rebuilt on every load"}; |
| 87 | + |
| 88 | + Args args; |
| 89 | + |
| 90 | + cli.add_option("-i,--input", args.input, "Input metric map file (*.mm)") |
| 91 | + ->required() |
| 92 | + ->check(CLI::ExistingFile); |
| 93 | + |
| 94 | + cli.add_option("-o,--output", args.output, "Output metric map file (*.mm)")->required(); |
| 95 | + |
| 96 | + cli.add_option( |
| 97 | + "--layer", args.layer, |
| 98 | + "Name of the IncrementalPointCloud layer to process. If omitted, all such layers are " |
| 99 | + "processed."); |
| 100 | + |
| 101 | + cli.add_flag( |
| 102 | + "--disable", args.disable, |
| 103 | + "Instead of baking, strip any cached data (sets serialize_kdtree=false)."); |
| 104 | + |
| 105 | + cli.add_flag( |
| 106 | + "--kdtree,!--no-kdtree", args.kdtree, |
| 107 | + "Bake the k-d tree index (default: on). Requires a nanoflann build with the incremental " |
| 108 | + "index's save/load API."); |
| 109 | + |
| 110 | + cli.add_option( |
| 111 | + "-l,--load-plugins", args.plugins, |
| 112 | + "One or more (comma separated) *.so files to load as plugins"); |
| 113 | + |
| 114 | + try |
| 115 | + { |
| 116 | + cli.parse(argc, argv); |
| 117 | + } |
| 118 | + catch (const CLI::ParseError& e) |
| 119 | + { |
| 120 | + return cli.exit(e); |
| 121 | + } |
| 122 | + |
| 123 | + run_bake(args); |
| 124 | + } |
| 125 | + catch (const std::exception& e) |
| 126 | + { |
| 127 | + std::cerr << e.what() << std::endl; |
| 128 | + return 1; |
| 129 | + } |
| 130 | + return 0; |
| 131 | +} |
0 commit comments