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
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class CameraCalibrationDataManagerWidget : public QWidget
*/
void loadObservations(const std::string& observations_file);

void validateObservationOutputPaths(const char* result, const char* ros, const char* report) const;

/** @brief Saves the displayed observation paths and quality diagnostics as CSV. */
void saveObservationReport(const std::string& file) const;

Expand Down
6 changes: 3 additions & 3 deletions gui/src/app/camera_intrinsic_calibration_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,17 @@ int main(int argc, char** argv)
try
{
const int first_file = headless ? 2 : 1;
const char* ros = headless && output->positional_argc > first_file + 3 ? argv[first_file + 3] : nullptr;
if (headless)
{
const char* ros = output->positional_argc > first_file + 3 ? argv[first_file + 3] : nullptr;
calibration_workbench::validateHeadlessResultPaths(argv[first_file], argv[first_file + 1], argv[first_file + 2],
ros);
}
if (output)
calibration_workbench::validateObservationReportPath(output->observation_report, argv + 2,
argv + output->positional_argc);
cal->loadConfig(argv[first_file]);
cal->loadObservations(argv[first_file + 1]);
if (output)
cal->validateObservationOutputPaths(argv[first_file + 2], ros, output->observation_report);
cal->calibrate();
if (headless && !cal->calibrationConverged())
{
Expand Down
2 changes: 2 additions & 0 deletions gui/src/app/extrinsic_hand_eye_calibration_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ int main(int argc, char** argv)
argv + output->positional_argc);
cal->loadConfig(argv[first_file]);
cal->loadObservations(argv[first_file + 1]);
if (output)
cal->validateObservationOutputPaths(argv[first_file + 2], nullptr, output->observation_report);
cal->calibrate();
if (headless && !cal->calibrationConverged())
{
Expand Down
1 change: 1 addition & 0 deletions gui/src/app/headless_calibration.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ int runObservationValidation(Widget& widget, char** argv)
validateObservationReportPath(argv[4], argv + 2, argv + 4);
widget.loadConfig(argv[2]);
widget.loadObservations(argv[3]);
widget.validateObservationOutputPaths(nullptr, nullptr, argv[4]);
const auto usable = widget.validateObservations();
widget.saveObservationReport(argv[4]);
if (usable == 0)
Expand Down
60 changes: 3 additions & 57 deletions gui/src/app/headless_result_paths.h
Original file line number Diff line number Diff line change
@@ -1,68 +1,14 @@
#pragma once

#include <filesystem>
#include <stdexcept>
#include <string>
#include <system_error>
#include "../output_path_identity.h"

namespace calibration_workbench
{
namespace headless_result_paths
{
struct NamedPath
{
const char* role;
const char* argument;
std::filesystem::path resolved;
};

inline std::string describe(const NamedPath& path) { return std::string(path.role) + " path '" + path.argument + "'"; }

inline NamedPath resolvePath(const char* role, const char* argument)
{
namespace fs = std::filesystem;
NamedPath path{ role, argument, {} };
if (argument[0] == '\0')
throw std::runtime_error(describe(path) + " is empty");

try
{
fs::path candidate = fs::absolute(argument);
for (unsigned links = 0;; ++links)
{
std::error_code ec;
if (!fs::is_symlink(fs::symlink_status(candidate, ec)))
{
path.resolved = fs::weakly_canonical(candidate);
return path;
}
if (links == 40)
break;
candidate = candidate.parent_path() / fs::read_symlink(candidate);
}
}
catch (const fs::filesystem_error& ex)
{
throw std::runtime_error("Cannot resolve " + describe(path) + ": " + ex.what());
}

throw std::runtime_error("Too many symbolic links in " + describe(path));
}

inline void requireDistinct(const NamedPath& output, const NamedPath& other)
{
namespace fs = std::filesystem;
if (output.resolved == other.resolved ||
(fs::exists(output.resolved) && fs::exists(other.resolved) && fs::equivalent(output.resolved, other.resolved)))
throw std::runtime_error(describe(output) + " conflicts with " + describe(other));
}
} // namespace headless_result_paths

inline void validateHeadlessResultPaths(const char* config, const char* observations, const char* result,
const char* ros = nullptr)
{
using headless_result_paths::requireDistinct;
using headless_result_paths::resolvePath;
using output_path_identity::requireDistinct;
using output_path_identity::resolvePath;
const auto config_path = resolvePath("configuration", config);
const auto observations_path = resolvePath("observation manifest", observations);
const auto result_path = resolvePath("calibration result", result);
Expand Down
77 changes: 60 additions & 17 deletions gui/src/camera_calibration_data_manager_widget.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "ui_camera_calibration_data_manager_widget.h"
#include "output_path_identity.h"

#include <calibration_workbench/gui/camera_calibration_data_manager_widget.h>
#include <calibration_workbench/gui/target_finder.h>
Expand Down Expand Up @@ -226,30 +227,72 @@ void CameraCalibrationDataManagerWidget::onSaveObservationReport()
}
}

void CameraCalibrationDataManagerWidget::saveObservationReport(const std::string& file) const
void CameraCalibrationDataManagerWidget::validateObservationOutputPaths(const char* result, const char* ros,
const char* report) const
{
const auto tree = ui_->tree_widget_observations;
if (tree->topLevelItemCount() == 0)
throw ICalException("No observations have been added");
using namespace output_path_identity;
struct Output
{
NamedPath path;
bool replaces_directory_entry;
};

const auto normalizedPath = [](const QString& path) {
const QFileInfo info(path);
const QString canonical = info.canonicalFilePath();
if (!canonical.isEmpty())
return canonical;
std::vector<Output> outputs;
if (result)
outputs.push_back({ resolvePath("calibration result", result), false });
if (ros)
outputs.push_back({ resolvePath("ROS calibration result", ros), false });
if (report)
outputs.push_back({ resolvePath("observation report", report), true });

const QString parent = info.absoluteDir().canonicalPath();
return parent.isEmpty() ? QDir::cleanPath(info.absoluteFilePath()) : QDir(parent).filePath(info.fileName());
};
const QString report_path = normalizedPath(QString::fromStdString(file));
const std::pair<int, const char*> assets[] = { { IMAGE_FILE_NAME_ROLE, "observation image" },
{ POSE_FILE_NAME_ROLE, "observation pose" } };
const auto tree = ui_->tree_widget_observations;
for (int i = 0; i < tree->topLevelItemCount(); ++i)
{
const auto item = tree->topLevelItem(i);
const QString image = item->data(0, IMAGE_FILE_NAME_ROLE).toString();
const QString pose = item->data(0, POSE_FILE_NAME_ROLE).toString();
if (report_path == normalizedPath(image) || (!pose.isEmpty() && report_path == normalizedPath(pose)))
throw ICalException("Observation report path must differ from image and pose paths: '" + file + "'");
for (const auto& [role, label] : assets)
{
const auto reference = item->data(0, role).toString().toStdString();
if (reference.empty())
continue;

const Output* collision = nullptr;
try
{
const auto asset = resolvePath(label, reference.c_str());
for (const auto& output : outputs)
{
if (aliases(output.path, asset, !output.replaces_directory_entry))
{
collision = &output;
break;
}
}
}
catch (const std::runtime_error&)
{
continue;
}

if (collision)
{
if (collision->replaces_directory_entry)
throw ICalException("Observation report path must differ from image and pose paths: '" + std::string(report) +
"'");
throw ICalException(describe(collision->path) + " conflicts with " + label + " path '" + reference + "'");
}
}
}
}

void CameraCalibrationDataManagerWidget::saveObservationReport(const std::string& file) const
{
const auto tree = ui_->tree_widget_observations;
if (tree->topLevelItemCount() == 0)
throw ICalException("No observations have been added");

validateObservationOutputPaths(nullptr, nullptr, file.c_str());

QSaveFile output(QString::fromStdString(file));
if (!output.open(QIODevice::WriteOnly | QIODevice::Text))
Expand Down
66 changes: 66 additions & 0 deletions gui/src/output_path_identity.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#pragma once

#include <filesystem>
#include <stdexcept>
#include <string>
#include <system_error>

namespace calibration_workbench
{
namespace output_path_identity
{
struct NamedPath
{
const char* role;
const char* argument;
std::filesystem::path resolved;
};

inline std::string describe(const NamedPath& path) { return std::string(path.role) + " path '" + path.argument + "'"; }

inline NamedPath resolvePath(const char* role, const char* argument)
{
namespace fs = std::filesystem;
NamedPath path{ role, argument, {} };
if (argument[0] == '\0')
throw std::runtime_error(describe(path) + " is empty");

try
{
fs::path candidate = fs::absolute(argument);
for (unsigned links = 0;; ++links)
{
std::error_code ec;
if (!fs::is_symlink(fs::symlink_status(candidate, ec)))
{
path.resolved = fs::weakly_canonical(candidate);
return path;
}
if (links == 40)
break;
candidate = candidate.parent_path() / fs::read_symlink(candidate);
}
}
catch (const fs::filesystem_error& ex)
{
throw std::runtime_error("Cannot resolve " + describe(path) + ": " + ex.what());
}

throw std::runtime_error("Too many symbolic links in " + describe(path));
}

inline bool aliases(const NamedPath& output, const NamedPath& other, bool check_hard_links)
{
namespace fs = std::filesystem;
return output.resolved == other.resolved ||
(check_hard_links && fs::exists(output.resolved) && fs::exists(other.resolved) &&
fs::equivalent(output.resolved, other.resolved));
}

inline void requireDistinct(const NamedPath& output, const NamedPath& other)
{
if (aliases(output, other, true))
throw std::runtime_error(describe(output) + " conflicts with " + describe(other));
}
} // namespace output_path_identity
} // namespace calibration_workbench
Loading
Loading