Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding new argument breaks older scripts. The best way it should be a default value but ain't sure it's possible with lua.


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 @@ void ReactiveLuaFunction::prepareLua()
_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 @@ void TimeseriesRef::set(unsigned index, double x, double y)
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