Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
9 changes: 8 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,11 @@ 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 +30,9 @@ 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
31 changes: 28 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,34 @@ 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; });
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