Skip to content

Commit cac5e47

Browse files
author
Sebastian Almagro
committed
Implement MatchType in TimeseriesRef for precise data retrieval
- 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.
1 parent 0c6521b commit cac5e47

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

plotjuggler_base/include/PlotJuggler/reactive_function.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ struct TimeseriesRef
2525

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

28-
double atTime(double t) const;
28+
double atTime(double t, MatchType match_type) const;
2929

3030
int getRawIndexAtTime(double t) const;
3131

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

66+
enum class MatchType {
67+
Exact, // Returns an index only if the exact time is found
68+
Nearest // Returns the nearest time index (current behavior)
69+
};
70+
6671
//-----------------------
6772

6873
class ReactiveLuaFunction : public PJ::TransformFunction

plotjuggler_base/src/reactive_function.cpp

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,34 @@ void TimeseriesRef::set(unsigned index, double x, double y)
185185
p = { x, y };
186186
}
187187

188-
double TimeseriesRef::atTime(double t) const
188+
double TimeseriesRef::atTime(double t, MatchType match_type) const
189189
{
190-
int i = _plot_data->getIndexFromX(t);
191-
return _plot_data->at(i).y;
190+
auto index = getRawIndexAtTime(t, match_type);
191+
if (!index)
192+
{
193+
throw std::runtime_error("Time point not found for exact match requirement");
194+
}
195+
return _plot_data->at(*index).y;
192196
}
193197

194-
int TimeseriesRef::getRawIndexAtTime(double t) const
198+
std::optional<unsigned> TimeseriesRef::getRawIndexAtTime(double t, MatchType match_type) const
195199
{
196-
return _plot_data->getIndexFromX(t);
200+
if (match_type == MatchType::Exact)
201+
{
202+
auto it = std::find_if(
203+
_plot_data->begin(),
204+
_plot_data->end(),
205+
[t](const auto& point) { return point.x == t; });
206+
if (it != _plot_data->end())
207+
{
208+
return std::distance(_plot_data->begin(), it);
209+
}
210+
return std::nullopt; // Exact time not found
211+
}
212+
else
213+
{
214+
return _plot_data->getIndexFromX(t); // Nearest match
215+
}
197216
}
198217

199218
unsigned TimeseriesRef::size() const

0 commit comments

Comments
 (0)