Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance Data Retrieval Accuracy with MatchType in TimeseriesRef #969

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ repos:

# Standard hooks
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
rev: v4.6.0
hooks:
- id: check-added-large-files
- id: check-ast
Expand All @@ -38,7 +38,7 @@ repos:

# CPP hooks
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v17.0.6
rev: v18.1.4
hooks:
- id: clang-format
args: ['-fallback-style=none', '-i']
11 changes: 10 additions & 1 deletion plotjuggler_base/include/PlotJuggler/reactive_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ class CreatedSeriesXY;

namespace PJ
{
enum class MatchType
{
Exact, // Returns an index only if the exact time is found
Nearest // Returns the nearest time index (current behavior)
};

struct TimeseriesRef
{
TimeseriesRef(PlotData* data);
Expand All @@ -25,7 +31,10 @@ struct TimeseriesRef

void set(unsigned index, double x, double y);

double atTime(double t) const;
double atTime(double t, MatchType match_type) const;

std::optional<unsigned>
getRawIndexAtTime(double t, MatchType match_type) const; // Method signature updated

unsigned size() const;

Expand Down
30 changes: 27 additions & 3 deletions plotjuggler_base/src/reactive_function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
_timeseries_ref["at"] = &TimeseriesRef::at;
_timeseries_ref["set"] = &TimeseriesRef::set;
_timeseries_ref["atTime"] = &TimeseriesRef::atTime;
_timeseries_ref["getRawIndexAtTime"] = &TimeseriesRef::getRawIndexAtTime;
_timeseries_ref["clear"] = &TimeseriesRef::clear;

//---------------------------------------
Expand Down Expand Up @@ -184,10 +185,33 @@
p = { x, y };
}

double TimeseriesRef::atTime(double t) const
double TimeseriesRef::atTime(double t, MatchType match_type) const
{
int i = _plot_data->getIndexFromX(t);
return _plot_data->at(i).y;
auto index = getRawIndexAtTime(t, match_type);
if (!index)
{
throw std::runtime_error("Time point not found for exact match requirement");
}
return _plot_data->at(*index).y;
}

std::optional<unsigned> TimeseriesRef::getRawIndexAtTime(double t,
MatchType match_type) const
{
if (match_type == MatchType::Exact)
{
auto it = std::find_if(_plot_data->begin(), _plot_data->end(),
[t](const auto& point) { return point.x == t; });

Check notice

Code scanning / CodeQL

Equality test on floating-point values Note

Equality checks on floating point values can yield unexpected results.
if (it != _plot_data->end())
{
return std::distance(_plot_data->begin(), it);
}
return std::nullopt; // Exact time not found
}
else
{
return _plot_data->getIndexFromX(t); // Nearest match
}
}

unsigned TimeseriesRef::size() const
Expand Down
Loading