From 0c6521b8f2ab24afff2d274516c6976e5b2eeb4b Mon Sep 17 00:00:00 2001 From: Sebastian Almagro Date: Tue, 23 Apr 2024 10:36:47 +0200 Subject: [PATCH 1/3] Added method to expose raw index to the Lua interface. --- plotjuggler_base/include/PlotJuggler/reactive_function.h | 2 ++ plotjuggler_base/src/reactive_function.cpp | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/plotjuggler_base/include/PlotJuggler/reactive_function.h b/plotjuggler_base/include/PlotJuggler/reactive_function.h index 601cfe43e..cf05e4229 100644 --- a/plotjuggler_base/include/PlotJuggler/reactive_function.h +++ b/plotjuggler_base/include/PlotJuggler/reactive_function.h @@ -27,6 +27,8 @@ struct TimeseriesRef double atTime(double t) const; + int getRawIndexAtTime(double t) const; + unsigned size() const; void clear() const; diff --git a/plotjuggler_base/src/reactive_function.cpp b/plotjuggler_base/src/reactive_function.cpp index 27d54d1bf..b861b87ec 100644 --- a/plotjuggler_base/src/reactive_function.cpp +++ b/plotjuggler_base/src/reactive_function.cpp @@ -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; //--------------------------------------- @@ -190,6 +191,11 @@ double TimeseriesRef::atTime(double t) const return _plot_data->at(i).y; } +int TimeseriesRef::getRawIndexAtTime(double t) const +{ + return _plot_data->getIndexFromX(t); +} + unsigned TimeseriesRef::size() const { return _plot_data->size(); From ae9850ee7f3c4e7e6867293eb38f18b44425d18d Mon Sep 17 00:00:00 2001 From: Sebastian Almagro Date: Tue, 23 Apr 2024 10:48:59 +0200 Subject: [PATCH 2/3] 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. --- .../include/PlotJuggler/reactive_function.h | 9 ++++-- plotjuggler_base/src/reactive_function.cpp | 29 +++++++++++++++---- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/plotjuggler_base/include/PlotJuggler/reactive_function.h b/plotjuggler_base/include/PlotJuggler/reactive_function.h index cf05e4229..aa1e69de5 100644 --- a/plotjuggler_base/include/PlotJuggler/reactive_function.h +++ b/plotjuggler_base/include/PlotJuggler/reactive_function.h @@ -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); @@ -25,9 +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; - int getRawIndexAtTime(double t) const; + std::optional getRawIndexAtTime(double t, MatchType match_type) const; // Method signature updated unsigned size() const; diff --git a/plotjuggler_base/src/reactive_function.cpp b/plotjuggler_base/src/reactive_function.cpp index b861b87ec..a5a6124cf 100644 --- a/plotjuggler_base/src/reactive_function.cpp +++ b/plotjuggler_base/src/reactive_function.cpp @@ -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 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 From d620d622fcfc2dcc518c38cd5b4696938b97432e Mon Sep 17 00:00:00 2001 From: Sebastian Almagro Date: Mon, 29 Apr 2024 11:23:48 +0200 Subject: [PATCH 3/3] [precommit] - Apply clang-format --- .pre-commit-config.yaml | 4 ++-- .../include/PlotJuggler/reactive_function.h | 10 ++++++---- plotjuggler_base/src/reactive_function.cpp | 13 ++++++------- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 301a1912c..6c19894bc 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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 @@ -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'] diff --git a/plotjuggler_base/include/PlotJuggler/reactive_function.h b/plotjuggler_base/include/PlotJuggler/reactive_function.h index aa1e69de5..55d81a839 100644 --- a/plotjuggler_base/include/PlotJuggler/reactive_function.h +++ b/plotjuggler_base/include/PlotJuggler/reactive_function.h @@ -17,9 +17,10 @@ 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) +enum class MatchType +{ + Exact, // Returns an index only if the exact time is found + Nearest // Returns the nearest time index (current behavior) }; struct TimeseriesRef @@ -32,7 +33,8 @@ struct TimeseriesRef double atTime(double t, MatchType match_type) const; - std::optional getRawIndexAtTime(double t, MatchType match_type) const; // Method signature updated + std::optional + getRawIndexAtTime(double t, MatchType match_type) const; // Method signature updated unsigned size() const; diff --git a/plotjuggler_base/src/reactive_function.cpp b/plotjuggler_base/src/reactive_function.cpp index a5a6124cf..e331ccca6 100644 --- a/plotjuggler_base/src/reactive_function.cpp +++ b/plotjuggler_base/src/reactive_function.cpp @@ -195,23 +195,22 @@ double TimeseriesRef::atTime(double t, MatchType match_type) const return _plot_data->at(*index).y; } -std::optional TimeseriesRef::getRawIndexAtTime(double t, MatchType match_type) const +std::optional 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; }); + 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 + return std::nullopt; // Exact time not found } else { - return _plot_data->getIndexFromX(t); // Nearest match + return _plot_data->getIndexFromX(t); // Nearest match } }