diff --git a/gui/include/calibration_workbench/gui/camera_calibration_data_manager_widget.h b/gui/include/calibration_workbench/gui/camera_calibration_data_manager_widget.h index 7f9ba22..4c7c79a 100644 --- a/gui/include/calibration_workbench/gui/camera_calibration_data_manager_widget.h +++ b/gui/include/calibration_workbench/gui/camera_calibration_data_manager_widget.h @@ -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; diff --git a/gui/src/app/camera_intrinsic_calibration_app.cpp b/gui/src/app/camera_intrinsic_calibration_app.cpp index 23276a0..63e80b9 100644 --- a/gui/src/app/camera_intrinsic_calibration_app.cpp +++ b/gui/src/app/camera_intrinsic_calibration_app.cpp @@ -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()) { diff --git a/gui/src/app/extrinsic_hand_eye_calibration_app.cpp b/gui/src/app/extrinsic_hand_eye_calibration_app.cpp index df04de6..e2ee645 100644 --- a/gui/src/app/extrinsic_hand_eye_calibration_app.cpp +++ b/gui/src/app/extrinsic_hand_eye_calibration_app.cpp @@ -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()) { diff --git a/gui/src/app/headless_calibration.h b/gui/src/app/headless_calibration.h index d0a8529..80eb082 100644 --- a/gui/src/app/headless_calibration.h +++ b/gui/src/app/headless_calibration.h @@ -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) diff --git a/gui/src/app/headless_result_paths.h b/gui/src/app/headless_result_paths.h index 1c63225..a33c8dc 100644 --- a/gui/src/app/headless_result_paths.h +++ b/gui/src/app/headless_result_paths.h @@ -1,68 +1,14 @@ #pragma once -#include -#include -#include -#include +#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); diff --git a/gui/src/camera_calibration_data_manager_widget.cpp b/gui/src/camera_calibration_data_manager_widget.cpp index 019d233..90a3ec3 100644 --- a/gui/src/camera_calibration_data_manager_widget.cpp +++ b/gui/src/camera_calibration_data_manager_widget.cpp @@ -1,4 +1,5 @@ #include "ui_camera_calibration_data_manager_widget.h" +#include "output_path_identity.h" #include #include @@ -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 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 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)) diff --git a/gui/src/output_path_identity.h b/gui/src/output_path_identity.h new file mode 100644 index 0000000..34645bf --- /dev/null +++ b/gui/src/output_path_identity.h @@ -0,0 +1,66 @@ +#pragma once + +#include +#include +#include +#include + +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 diff --git a/test/headless_calibration_workflow.cmake b/test/headless_calibration_workflow.cmake index f8ff93c..eecffb1 100644 --- a/test/headless_calibration_workflow.cmake +++ b/test/headless_calibration_workflow.cmake @@ -201,6 +201,20 @@ endforeach() set(collision_image "${OUTPUT_DIR}/report-image.png") set(collision_pose "${OUTPUT_DIR}/report-pose.yaml") set(collision_manifest "${OUTPUT_DIR}/report-collision.yaml") +set(image_hardlink "${OUTPUT_DIR}/image-hardlink.png") +set(pose_hardlink "${OUTPUT_DIR}/pose-hardlink.yaml") +set(pose_symlink "${OUTPUT_DIR}/pose-link.yaml") +set(loop_image "${OUTPUT_DIR}/loop-image.png") +set(collision_result "${OUTPUT_DIR}/collision-calibration.yaml") +set(collision_ros "${OUTPUT_DIR}/collision-ros.yaml") +file( + REMOVE + "${image_hardlink}" + "${pose_hardlink}" + "${pose_symlink}" + "${loop_image}" + "${collision_result}" + "${collision_ros}") configure_file("${SAMPLE_DIR}/images/0.png" "${collision_image}" COPYONLY) configure_file("${SAMPLE_DIR}/poses/0.yaml" "${collision_pose}" COPYONLY) file(SHA256 "${collision_image}" image_before) @@ -215,6 +229,67 @@ foreach(image IN LISTS images) endif() endforeach() +execute_process(COMMAND ln "${collision_image}" "${image_hardlink}" RESULT_VARIABLE image_link_status) +execute_process(COMMAND ln "${collision_pose}" "${pose_hardlink}" RESULT_VARIABLE pose_link_status) +execute_process(COMMAND "${CMAKE_COMMAND}" -E create_symlink "report-pose.yaml" "${pose_symlink}" + RESULT_VARIABLE pose_symlink_status) +execute_process(COMMAND "${CMAKE_COMMAND}" -E create_symlink "loop-image.png" "${loop_image}" + RESULT_VARIABLE loop_status) +if(NOT + image_link_status + EQUAL + 0 + OR NOT + pose_link_status + EQUAL + 0 + OR NOT + pose_symlink_status + EQUAL + 0 + OR NOT + loop_status + EQUAL + 0) + message(FATAL_ERROR "Could not create observation path collision fixtures") +endif() + +run_calibration( + "${INTRINSIC_APP}" + --headless + 1 + "calibration result path.*image-hardlink.png.*observation image path.*report-image.png" + "${config}" + "${collision_manifest}" + "${image_hardlink}") +run_calibration( + "${INTRINSIC_APP}" + --headless + 1 + "ROS calibration result path.*pose-link.yaml.*observation pose path.*report-pose.yaml" + "${OUTPUT_DIR}/with-guesses.yaml" + "${collision_manifest}" + "${collision_result}" + "${pose_symlink}") +run_calibration( + "${HAND_EYE_APP}" + --headless + 1 + "calibration result path.*report-pose.yaml.*observation pose path.*report-pose.yaml" + "${config}" + "${collision_manifest}" + "${collision_pose}") +run_calibration( + "${INTRINSIC_APP}" + --headless + 1 + "image and pose paths" + "${config}" + "${collision_manifest}" + "${collision_result}" + "${collision_ros}" + --observation-report + "${collision_image}") run_calibration( "${INTRINSIC_APP}" --validate-only @@ -230,9 +305,40 @@ run_calibration( "image and pose paths" "${config}" "${collision_manifest}" - "${OUTPUT_DIR}/collision-calibration.yaml" + "${collision_result}" --observation-report "${collision_pose}") +if(EXISTS "${collision_result}" OR EXISTS "${collision_ros}") + message(FATAL_ERROR "An observation path conflict left partial calibration results") +endif() + +file(APPEND "${collision_manifest}" " - image: loop-image.png\n pose: report-pose.yaml\n") +run_calibration( + "${HAND_EYE_APP}" + --validate-only + 0 + "" + "${config}" + "${collision_manifest}" + "${pose_hardlink}") +file(READ "${pose_hardlink}" hardlink_csv) +file(STRINGS "${pose_hardlink}" accepted_rows REGEX "Included in calibration") +list(LENGTH accepted_rows accepted_count) +if(NOT + accepted_count + EQUAL + 15 + OR NOT + hardlink_csv + MATCHES + "^image_path,pose_path," + OR NOT + hardlink_csv + MATCHES + "loop-image.png.*(Failed to load file|Image file does not exist)") + message(FATAL_ERROR "The report did not replace only the hard-link entry with the expected diagnostics") +endif() + file(SHA256 "${collision_image}" image_after) file(SHA256 "${collision_pose}" pose_after) if(NOT @@ -243,5 +349,6 @@ if(NOT pose_before STREQUAL pose_after) - message(FATAL_ERROR "Observation report modified an input file") + message(FATAL_ERROR "An output modified an observation input file") endif() +message(STATUS "Observation output collisions left inputs and all result paths intact")