From 700173786e96b78710f22a1d4e6b120b16c74518 Mon Sep 17 00:00:00 2001
From: jcasals <jcasals@iri.upc.edu>
Date: Wed, 17 Jun 2020 10:26:37 +0200
Subject: [PATCH] Add time tolerance check in CaptureBase

---
 include/core/processor/processor_base.h |  6 ++++++
 src/capture/capture_base.cpp            | 24 +++++++++++++++++++++++-
 2 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/include/core/processor/processor_base.h b/include/core/processor/processor_base.h
index dd4c37b27..df300c726 100644
--- a/include/core/processor/processor_base.h
+++ b/include/core/processor/processor_base.h
@@ -359,6 +359,8 @@ class ProcessorBase : public NodeBase, public std::enable_shared_from_this<Proce
 
         int getDim() const;
 
+        double getTimeTolerance() const;
+
         void link(SensorBasePtr);
         template<typename classType, typename... T>
         static std::shared_ptr<classType> emplace(SensorBasePtr _sen_ptr, T&&... all);
@@ -426,6 +428,10 @@ inline int ProcessorBase::getDim() const
 {
     return dim_;
 }
+inline double ProcessorBase::getTimeTolerance() const
+{
+    return params_->time_tolerance;
+}
 
 template<typename classType, typename... T>
 std::shared_ptr<classType> ProcessorBase::emplace(SensorBasePtr _sen_ptr, T&&... all)
diff --git a/src/capture/capture_base.cpp b/src/capture/capture_base.cpp
index 8b31edc53..2f49c352d 100644
--- a/src/capture/capture_base.cpp
+++ b/src/capture/capture_base.cpp
@@ -363,7 +363,7 @@ CheckLog CaptureBase::localCheck(bool _verbose, CaptureBasePtr _cap_ptr, std::os
         auto frm_cap = _cap_ptr->getFrame();
         inconsistency_explanation << "Cap" << id() << " @ " << _cap_ptr
                                   << " ---> Frm" << frm_cap->id() << " @ " << frm_cap
-                                  << " -X-> Frm" << id();
+                                  << " -X-> Frm" << id() << "\n";
         auto frm_cap_list = frm_cap->getCaptureList();
         auto frame_has_cap = std::find_if(frm_cap_list.begin(), frm_cap_list.end(), [&_cap_ptr](CaptureBasePtr cap){ return cap == _cap_ptr;});
         log.assertTrue(frame_has_cap != frm_cap_list.end(), inconsistency_explanation);
@@ -375,6 +375,28 @@ CheckLog CaptureBase::localCheck(bool _verbose, CaptureBasePtr _cap_ptr, std::os
                                       << " -X-> Cap" << id();
             log.assertTrue((f->getCapture() == _cap_ptr), inconsistency_explanation);
         }
+        //Check that the capture is within time tolerance of some processor
+        auto frame = getFrame();
+        double time_diff = fabs(getTimeStamp() - frame->getTimeStamp());
+
+        //It looks like some gtests add captures by hand, without using processors, so the processor list is empty.
+        //This inicialization is needed because if the list empty the execution does not go into the loop and the
+        //assertion fails
+        bool match_any_prc_timetolerance;
+        if(getSensor() != nullptr )
+        {
+            match_any_prc_timetolerance = getSensor()->getProcessorList().empty();
+            for(auto const& prc : getSensor()->getProcessorList())
+            {
+                match_any_prc_timetolerance = match_any_prc_timetolerance or (time_diff <= prc->getTimeTolerance());
+            }
+            inconsistency_explanation << "Cap " << id() << " @ " << _cap_ptr
+                                      << " ts =" << getTimeStamp() << ((frame->isKey()) ? "KFrm" : "Frm") << frame->id()
+                                      << " ts = " << frame->getTimeStamp() << " their time difference (" << time_diff << ") does not match any time tolerance of"
+                                      << " any processor in sensor " << getSensor()->id() << "\n";
+            log.assertTrue((match_any_prc_timetolerance), inconsistency_explanation);
+        }
+
     return log;
 }
 bool CaptureBase::check(CheckLog& _log, std::shared_ptr<NodeBase> _node_ptr, bool _verbose, std::ostream& _stream, std::string _tabs) const
-- 
GitLab