Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions 3rdparty/csv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ SOFTWARE.

#include <algorithm>
#include <deque>
#include <exception>
#include <fstream>
#include <iterator>
#include <memory>
#include <mutex>
#include <thread>
#include <utility>
#include <sstream>
#include <string>
#include <vector>
Expand Down Expand Up @@ -6413,12 +6415,22 @@ namespace csv {
/** @name Multi-Threaded File Reading: Flags and State */
///@{
std::thread read_csv_worker; /**< Worker thread for read_csv() */
// HDMapping: an exception escaping the worker thread called std::terminate; it is kept here instead and
// rethrown on the reading thread once the worker has been joined
std::exception_ptr read_csv_exception = nullptr;
///@}

/** Rethrow what read_csv() threw on the worker thread; call after joining it */
void rethrow_read_csv_exception() {
if (this->read_csv_exception)
std::rethrow_exception(std::exchange(this->read_csv_exception, nullptr));
}

/** Read initial chunk to get metadata */
void initial_read() {
this->read_csv_worker = std::thread(&CSVReader::read_csv, this, internals::ITERATION_CHUNK_SIZE);
this->read_csv_worker.join();
this->rethrow_read_csv_exception();
}

void trim_header();
Expand Down Expand Up @@ -7130,7 +7142,8 @@ namespace csv {
std::error_code error;
this->data_ptr->_data = std::make_shared<mio::basic_mmap_source<char>>(mio::make_mmap_source(this->_filename, this->mmap_pos, length, error));
this->mmap_pos += length;
if (error) throw error;
// HDMapping: a std::exception naming the file instead of a bare std::error_code, which no caller catches
if (error) throw std::system_error(error, "cannot read '" + this->_filename + "'");

auto mmap_ptr = (mio::basic_mmap_source<char>*)(this->data_ptr->_data.get());

Expand Down Expand Up @@ -7517,11 +7530,16 @@ namespace csv {
// Tell read_row() to listen for CSV rows
this->records->notify_all();

this->parser->set_output(*this->records);
this->parser->next(bytes);
try {
this->parser->set_output(*this->records);
this->parser->next(bytes);

if (!this->header_trimmed) {
this->trim_header();
if (!this->header_trimmed) {
this->trim_header();
}
}
catch (...) {
this->read_csv_exception = std::current_exception(); // HDMapping: rethrown by the reading thread
}

// Tell read_row() to stop waiting
Expand Down Expand Up @@ -7557,6 +7575,7 @@ namespace csv {
// Reading thread is not active => start another one
if (this->read_csv_worker.joinable())
this->read_csv_worker.join();
this->rethrow_read_csv_exception(); // HDMapping: a failed chunk would otherwise be read again forever

this->read_csv_worker = std::thread(&CSVReader::read_csv, this, internals::ITERATION_CHUNK_SIZE);
}
Expand Down Expand Up @@ -7594,6 +7613,7 @@ namespace csv {
if (this->records->empty()) {
this->read_csv_worker = std::thread(&CSVReader::read_csv, this, internals::ITERATION_CHUNK_SIZE);
this->read_csv_worker.join();
this->rethrow_read_csv_exception();

// Still empty => return end iterator
if (this->records->empty()) return this->end();
Expand Down
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ Nothing fails if you miss one of these steps:
1. `add_subdirectory(apps/<tool>)` in the top-level `CMakeLists.txt`, under the right `BUILD_WITH_*` option. Without
it the tool isn't built.
2. `hdmapping_install_app(<target>)` in the tool's `CMakeLists.txt`. Without it the tool is missing from the install
and the Debian package.
and the Debian package, and on Windows it lacks the UTF-8 manifest (`cmake/utf8.manifest`) and fails on non-ASCII paths.
3. The `EXPECTED` list in `deploy_mandeye.bat`. Without it the Windows deploy doesn't warn when the tool stops being
built.

Expand Down
1 change: 1 addition & 0 deletions apps/lidar_odometry_step_1/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ set(SOURCES
lidar_odometry.cpp lidar_odometry.h
points_global_spill.h points_global_spill.cpp
raw_cloud_loader.h raw_cloud_loader.cpp
output_folders.h output_folders.cpp
toml_io.h toml_io.cpp
)

Expand Down
38 changes: 24 additions & 14 deletions apps/lidar_odometry_step_1/lidar_odometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ static bool save_cache_data(LidarOdometryParams& params, const std::string& path
return true;
}

//! Prints why compute_step_1 stopped and records it in the run report, whose errors the GUI shows. Returns false.
static bool step_1_failed(const LidarOdometryParams& params, const std::string& reason)
{
std::cerr << reason << std::endl;
if (params.run_report)
params.run_report->fail(reason);
return false;
}

static void release_freed_heap()
{
#if defined(__GLIBC__)
Expand Down Expand Up @@ -433,8 +442,10 @@ bool load_data(

fs::path wdp = fs::path(working_directory) / "preview";
params.working_directory_preview = wdp.string();
fs::path wdp_cache = fs::path(working_directory) / "cache";
params.working_directory_cache = wdp_cache.string();
const CacheLocation cache = current_cache_location(working_directory, params.session_cache_choice);
params.working_directory_cache = new_run_cache_folder(cache, working_directory).string();
if (params.run_report && cache.kind != CacheKind::DataFolder)
params.run_report->addVolume(cache.base, "cache");

params.raw_cloud_loader.reset();
if (params.lazy_load_raw_clouds)
Expand Down Expand Up @@ -471,10 +482,11 @@ bool load_data(
minSize,
[&](size_t i)
{
auto data = L.load(i);
// after the read: a cloud drive may report other metadata once it has downloaded the file
std::error_code ec;
L.file_size[i] = fs::file_size(L.files[i], ec);
L.file_time[i] = fs::last_write_time(L.files[i], ec);
auto data = L.load(i);
L.n[i] = data.size();
if (!data.empty())
{
Expand Down Expand Up @@ -741,10 +753,7 @@ bool compute_step_1(
if (loader)
{
if (ppf.size() != loader->n.size())
{
std::cerr << "lazy_load_raw_clouds: the cloud vector does not belong to the last load_data\n";
return false;
}
return step_1_failed(params, "lazy_load_raw_clouds: the cloud vector does not belong to the last load_data");
// the flags describe THIS vector: a re-run reads every file again, and a file counts as loaded only if it is
for (size_t i = 0; i < ppf.size(); i++)
{
Expand All @@ -755,7 +764,7 @@ bool compute_step_1(
for (size_t fi = 0; fi < pointsPerFile.size(); fi++)
{
if (!lazy_ensure(loader, ppf, fi))
return false;
return step_1_failed(params, loader->error);
const auto& pp = pointsPerFile[fi];
// number_of_points += pp.size();
for (const auto& p : pp)
Expand Down Expand Up @@ -797,10 +806,10 @@ bool compute_step_1(
std::cout << "number of poses: " << poses.size() << std::endl;

if (poses.empty())
{
std::cerr << "Loading poses went wrong! Could not load poses!" << std::endl;
return false;
}
return step_1_failed(
params,
"No trajectory poses after the initial points: the IMU data is missing or ends before the LiDAR data (" +
std::to_string(trajectory.size()) + " IMU poses in total)");

int threshold = params.threshold_nr_poses;

Expand All @@ -813,7 +822,7 @@ bool compute_step_1(
if (!fs::exists(wdp_cache))
{
std::cout << "Creating folder: " << wdp_cache << "\n";
fs::create_directory(wdp_cache);
fs::create_directories(wdp_cache); // with a cache location that does not exist yet
}

for (size_t i = 0; i < n_iter; i++)
Expand Down Expand Up @@ -860,7 +869,7 @@ bool compute_step_1(
else if (L.t_first[index] < t1) // may hold points in [t0, t1): load; the search below decides
{
if (!lazy_ensure(loader, ppf, index))
return false;
return step_1_failed(params, loader->error);
}
}
}
Expand Down Expand Up @@ -1480,6 +1489,7 @@ void remove_cache(const LidarOdometryParams& params)
std::vector<WorkerData> run_lidar_odometry(const std::string& input_dir, LidarOdometryParams& params)
{
HDMAP_ZONE_SCOPE("run_lidar_odometry");
check_output_folders(input_dir, params.session_cache_choice); // before anything is loaded
Session session;
std::vector<WorkerData> worker_data;
std::vector<std::string> input_file_names;
Expand Down
58 changes: 56 additions & 2 deletions apps/lidar_odometry_step_1/lidar_odometry_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,7 @@ void step1(const std::string& input_folder_name, const std::atomic<bool>& loPaus

if (fs::exists(input_folder_name))
{
check_output_folders(input_folder_name, params.session_cache_choice); // before anything is loaded
RunReport::Scope discovery(params.run_report.get(), RunReport::Stage::Loading);
for (const auto& entry : fs::directory_iterator(input_folder_name))
if (entry.is_regular_file())
Expand All @@ -724,11 +725,13 @@ void step1(const std::string& input_folder_name, const std::atomic<bool>& loPaus
else
{
std::string message_info = "Problem with loading data from folder '" + input_folder_name +
"' (Pease check if folder exists). Program will close once You click OK!!!";
"' (Please check if folder exists). Program will close once You click OK!!!";
std::cout << message_info << std::endl;
throw std::runtime_error("Input data loading failed: " + input_folder_name);
}
}
else
throw std::runtime_error("Data folder not found: " + input_folder_name);
}

void step2(const std::atomic<bool>& loPause)
Expand Down Expand Up @@ -1989,17 +1992,22 @@ static void launchPipeline(const std::string& folder, bool compute, bool save)
}
} catch (const std::exception& error)
{
// Loading a folder starts a new run report, so its errors are this run's reasons for the failure.
const auto reasons = params.run_report && !folder.empty() ? params.run_report->errors() : std::vector<std::string>{};
if (params.run_report)
{
params.run_report->fail(error.what());
params.run_report->finishOperation("failed");
}
std::lock_guard lock(completionMutex);
completionMessage = std::string("Operation failed: ") + error.what();
for (const auto& reason : reasons)
completionMessage += "\n" + reason;
try
{
const fs::path input = folder.empty() ? fs::path(working_directory) : fs::path(folder);
if (fs::is_directory(input))
// a folder that is not writable gets no failure report: the popup is the report
if (!dynamic_cast<const NotWritableError*>(&error) && fs::is_directory(input))
{
const auto output = get_next_result_path(input.string());
if (save_failed_run_report(params, output, error.what()))
Expand Down Expand Up @@ -2027,6 +2035,7 @@ bool step1(
const std::atomic<bool>& loPause)
{
params.run_report = std::make_shared<RunReport>(folder, params.points_global_spill_directory, "cli");
check_output_folders(folder, params.session_cache_choice); // before anything is loaded
std::vector<std::string> input_file_names;
RunReport::Scope discovery(params.run_report.get(), RunReport::Stage::Loading);

Expand Down Expand Up @@ -2083,6 +2092,49 @@ bool confirm_close()
return false;
}

//! Menu bar entry next to "Load & process scanning": shows where the cache goes and picks another location for this
//! session. The menu bar is hidden during a run, so the location cannot change under one.
static void cache_location_menu()
{
const CacheLocation location = current_cache_location({}, params.session_cache_choice);
const char* kind = location.kind == CacheKind::DataFolder ? "data folder"
: location.kind == CacheKind::SystemTemp ? "system temp"
: "custom";
const std::string title = std::string("Cache: ") + kind + "###cache_location";
if (ImGui::BeginMenu(title.c_str()))
{
ImGui::TextDisabled("Where step 1 keeps its intermediate files (%s)", cache_source_label(location.source));
if (location.kind == CacheKind::DataFolder)
ImGui::TextUnformatted("Next to the data: <data folder>/cache_<time>_<process id>");
else
{
const std::u8string base = location.base.u8string();
ImGui::TextUnformatted(std::string(base.begin(), base.end()).c_str());
std::error_code error;
const auto space = fs::space(location.base, error);
if (error)
ImGui::TextDisabled("Free space unknown (%s)", error.message().c_str());
else
ImGui::Text("Free: %.1f GiB", static_cast<double>(space.available) / (1024.0 * 1024.0 * 1024.0));
}
ImGui::Separator();
if (ImGui::MenuItem("Data folder", nullptr, location.kind == CacheKind::DataFolder))
params.session_cache_choice = CacheChoice{ CacheKind::DataFolder, {} };
if (ImGui::MenuItem("System temporary folder", nullptr, location.kind == CacheKind::SystemTemp))
params.session_cache_choice = CacheChoice{ CacheKind::SystemTemp, {} };
if (ImGui::MenuItem("Custom folder...", nullptr, location.kind == CacheKind::Custom))
{
const std::string folder = mandeye::fd::SelectFolder("Select the cache location");
if (!folder.empty())
params.session_cache_choice = CacheChoice{ CacheKind::Custom, fs::path(folder) };
}
ImGui::EndMenu();
}
if (ImGui::IsItemHovered())
ImGui::SetTooltip(
"Cache location for intermediate files (gigabytes for long recordings). Set HD_MAPPING_CACHE_DIR to change the default.");
}

//! The detached run thread can't be joined and still uses the globals, so the process ends
//! without destructors, after removing the cache the parked (or finished) run no longer reads.
static void quit_abandoned_run()
Expand Down Expand Up @@ -2697,6 +2749,8 @@ void display()
ImGui::SetTooltip(
"Select folder from where to load data and start processing (" SHORTCUT_CTRL "+O), or drop the folder on the window");

cache_location_menu();

ImGui::SameLine();
ImGui::Dummy(ImVec2(20, 0));
ImGui::SameLine();
Expand Down
4 changes: 4 additions & 0 deletions apps/lidar_odometry_step_1/lidar_odometry_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <cstring>
#include <filesystem>
#include <regex>
#include <system_error>
#include <tbb/parallel_for_each.h>
#include <tbb/parallel_invoke.h>

Expand Down Expand Up @@ -489,6 +490,9 @@ std::vector<std::tuple<std::pair<double, double>, Eigen::Vector3f, Eigen::Vector
myfile.close();
}
}
} catch (const std::system_error&)
{
throw; // the file could not be read (e.g. a network or cloud drive failed): stop rather than lose its IMU data
} catch (const std::exception& e)
{
std::cout << "load_imu error for file: '" << imu_file << "'" << e.what() << std::endl;
Expand Down
6 changes: 5 additions & 1 deletion apps/lidar_odometry_step_1/lidar_odometry_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

#include <HDMapping/HDMAPPING_ConfigureInfo.hpp>

#include "output_folders.h"

#include <Core/ndt.h>
#include <Core/structures.h>
#include <Core/transformations.h>
Expand Down Expand Up @@ -209,7 +211,9 @@ struct LidarOdometryParams
// paths
std::string current_output_dir = "";
std::string working_directory_preview = "";
std::string working_directory_cache = "";
std::string working_directory_cache = ""; // the per-run cache folder, set by load_data
//! Cache location picked in the GUI; deliberately not a TOML parameter (HD_MAPPING_CACHE_DIR is the persistent one)
std::optional<CacheChoice> session_cache_choice;

// other
Eigen::Affine3d m_g = Eigen::Affine3d::Identity();
Expand Down
Loading
Loading