Skip to content

Commit

Permalink
Implement MatchType in TimeseriesRef for precise data retrieval
Browse files Browse the repository at this point in the history
- Added an enumeration MatchType with options Exact and Nearest.
- Modified atTime and getRawIndexAtTime in TimeseriesRef to support MatchType, allowing callers to specify whether they need an exact timestamp match or the nearest available match.
- Adjusted the atTime method to throw an error when an exact match is required but not found, enhancing error handling for strict match requirements.
  • Loading branch information
Sebastian Almagro committed Apr 23, 2024
1 parent e1eaaaa commit e00e141
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
7 changes: 6 additions & 1 deletion plotjuggler_base/include/PlotJuggler/reactive_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct TimeseriesRef

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

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

int getRawIndexAtTime(double t) const;

Expand Down Expand Up @@ -63,6 +63,11 @@ struct CreatedSeriesXY : public CreatedSeriesBase
CreatedSeriesXY(PlotDataMapRef* data_map, const std::string& name);
};

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

//-----------------------

class ReactiveLuaFunction : public PJ::TransformFunction
Expand Down
29 changes: 24 additions & 5 deletions plotjuggler_base/src/reactive_function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +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;
}

int TimeseriesRef::getRawIndexAtTime(double t) const
std::optional<unsigned> TimeseriesRef::getRawIndexAtTime(double t, MatchType match_type) const
{
return _plot_data->getIndexFromX(t);
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

0 comments on commit e00e141

Please sign in to comment.