• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

paulmthompson / WhiskerToolbox / 17270491352

27 Aug 2025 02:57PM UTC coverage: 65.333%. Remained the same
17270491352

push

github

paulmthompson
Merge branch 'main' of https://github.com/paulmthompson/WhiskerToolbox

352 of 628 new or added lines in 92 files covered. (56.05%)

357 existing lines in 24 files now uncovered.

26429 of 40453 relevant lines covered (65.33%)

1119.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/DataManager/transforms/Lines/line_resample.cpp
1
#include "line_resample.hpp"
2

3
#include "CoreGeometry/line_resampling.hpp"// For the actual resampling
4
#include "Lines/Line_Data.hpp"
5

6
#include <iostream>// For error messages / cout
7
#include <map>     // for std::map
8

9
std::string LineResampleOperation::getName() const {
×
10
    return "Resample Line";
×
11
}
12

13
std::type_index LineResampleOperation::getTargetInputTypeIndex() const {
×
14
    return typeid(std::shared_ptr<LineData>);
×
15
}
16

17
bool LineResampleOperation::canApply(DataTypeVariant const & dataVariant) const {
×
18
    if (!std::holds_alternative<std::shared_ptr<LineData>>(dataVariant)) {
×
19
        return false;
×
20
    }
21
    auto const * ptr_ptr = std::get_if<std::shared_ptr<LineData>>(&dataVariant);
×
22
    return ptr_ptr && *ptr_ptr;// Check if it's a valid, non-null LineData
×
23
}
24

25
std::unique_ptr<TransformParametersBase> LineResampleOperation::getDefaultParameters() const {
×
26
    return std::make_unique<LineResampleParameters>();
×
27
}
28

29
DataTypeVariant LineResampleOperation::execute(DataTypeVariant const & dataVariant,
×
30
                                               TransformParametersBase const * transformParameters) {
31
    // Call the version with progress reporting but ignore progress here
32
    return execute(dataVariant, transformParameters, [](int) {});
×
33
}
34

35
DataTypeVariant LineResampleOperation::execute(DataTypeVariant const & dataVariant,
×
36
                                               TransformParametersBase const * transformParameters,
37
                                               ProgressCallback progressCallback) {
38
    auto const * line_data_sptr_ptr = std::get_if<std::shared_ptr<LineData>>(&dataVariant);
×
39
    if (!line_data_sptr_ptr || !(*line_data_sptr_ptr)) {
×
40
        std::cerr << "LineResampleOperation::execute called with incompatible variant type or null data." << std::endl;
×
41
        return {};// Return empty variant
×
42
    }
43
    LineData const * input_line_data = (*line_data_sptr_ptr).get();
×
44

45
    LineResampleParameters const * params = nullptr;
×
46
    std::unique_ptr<TransformParametersBase> default_params_owner;// Keep owner alive
×
47

48
    if (transformParameters) {
×
49
        params = dynamic_cast<LineResampleParameters const *>(transformParameters);
×
50
        if (!params) {
×
51
            std::cerr << "LineResampleOperation::execute: Invalid parameter type. Using defaults." << std::endl;
×
52
            default_params_owner = getDefaultParameters();
×
53
            params = static_cast<LineResampleParameters const *>(default_params_owner.get());
×
54
        }
55
    } else {
56
        default_params_owner = getDefaultParameters();
×
57
        params = static_cast<LineResampleParameters const *>(default_params_owner.get());
×
58
    }
59

60
    if (!params) {// Should not happen if getDefaultParameters is correct and dynamic_cast was not null before
×
61
        std::cerr << "LineResampleOperation::execute: Failed to get parameters." << std::endl;
×
62
        return {};
×
63
    }
64

65
    std::cout << "LineResampleOperation: algorithm = "
66
              << (params->algorithm == LineSimplificationAlgorithm::FixedSpacing ? "FixedSpacing" : "DouglasPeucker")
×
67
              << ", target_spacing = " << params->target_spacing
×
68
              << ", epsilon = " << params->epsilon << std::endl;
×
69

70
    auto resampled_line_map = std::map<TimeFrameIndex, std::vector<Line2D>>();
×
71
    int total_lines = 0;
×
72
    for (auto const & time_lines_pair: input_line_data->GetAllLinesAsRange()) {
×
NEW
73
        total_lines += static_cast<int>(time_lines_pair.lines.size());
×
74
    }
75
    if (total_lines == 0) {
×
76
        progressCallback(100);
×
77
        auto empty_result = std::make_shared<LineData>();
×
78
        empty_result->setImageSize(input_line_data->getImageSize());
×
79
        return empty_result;
×
80
    }
×
81

82
    int processed_lines = 0;
×
83
    progressCallback(0);
×
84

85
    for (auto const & time_lines_pair: input_line_data->GetAllLinesAsRange()) {
×
86
        TimeFrameIndex time = time_lines_pair.time;
×
87
        std::vector<Line2D> new_lines_at_time;
×
88
        new_lines_at_time.reserve(time_lines_pair.lines.size());
×
89

90
        for (auto const & single_line: time_lines_pair.lines) {
×
91
            if (single_line.empty()) {
×
92
                new_lines_at_time.push_back(Line2D());// Keep empty lines as empty
×
93
            } else {
94
                Line2D simplified_line;
×
95
                switch (params->algorithm) {
×
96
                    case LineSimplificationAlgorithm::FixedSpacing:
×
97
                        simplified_line = resample_line_points(single_line, params->target_spacing);
×
98
                        break;
×
99
                    case LineSimplificationAlgorithm::DouglasPeucker:
×
100
                        simplified_line = douglas_peucker_simplify(single_line, params->epsilon);
×
101
                        break;
×
102
                }
103
                new_lines_at_time.push_back(simplified_line);
×
104
            }
×
105
            processed_lines++;
×
106
            if (total_lines > 0) {
×
107
                progressCallback(static_cast<int>((static_cast<double>(processed_lines) / total_lines) * 100.0));
×
108
            }
109
        }
110
        if (!new_lines_at_time.empty() || input_line_data->getAtTime(time).empty()) {
×
111
            // Add to map if there are new lines OR if the original time had lines (even if all became empty)
112
            // This ensures that times that previously had data but now have all empty lines are still represented.
113
            // However, if a time originally had no lines, we don't add an empty entry for it.
114
            if (!input_line_data->getAtTime(time).empty() || !new_lines_at_time.empty()) {
×
115
                resampled_line_map[time] = std::move(new_lines_at_time);
×
116
            }
117
        }
118
    }
×
119

120
    auto result_line_data = std::make_shared<LineData>(resampled_line_map);
×
121
    result_line_data->setImageSize(input_line_data->getImageSize());// Preserve image size
×
122

123
    progressCallback(100);
×
124
    std::cout << "LineResampleOperation executed successfully." << std::endl;
×
125
    return result_line_data;
×
126
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc