CustusX  2023.01.05-dev+develop.0da12
An IGT application
cxStreamedTimestampSynchronizer.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
13 
14 #include "cxLogger.h"
15 #include "cxImage.h"
16 
17 namespace cx
18 {
19 
21 {
22 public:
23  AbsDoubleLess(double center) : mCenter(center) { };
24 
25  bool operator()(const double& d1, const double& d2)
26  {
27  return fabs(d1 - mCenter) < fabs(d2 - mCenter);
28  }
29 
30  double mCenter;
31 };
32 
36 
38  mLastComputedTimestampShift(0),
39  mMaxWindowSize(20)
40 {
41 
42 }
43 
44 template<class ITER>
45 double StreamedTimestampSynchronizer::average(ITER begin, ITER end) const
46 {
47  int size = std::distance(begin, end);
48  double sum = 0;
49  for (; begin != end; ++begin)
50  sum += *begin;
51  return sum / size;
52 }
53 
55 {
56  QDateTime ts = imgMsg->getAcquisitionTime();
57  imgMsg->setOriginalAcquisitionTime(ts);
58  this->addTimestamp(ts);
59  ts = ts.addMSecs(this->getShift());
60 
61  imgMsg->setAcquisitionTime(ts);
62 }
63 
65 {
66  if (mDeltaWindow.empty())
67  return 0;
68 
69  QList<double> window = mDeltaWindow;
70  std::sort(window.begin(), window.end(), AbsDoubleLess(mLastComputedTimestampShift));
71 
72  double shrinkedWindowSize = double(window.size()) * 0.75;
73  int useCount = (int)(shrinkedWindowSize + 0.5);
74 
75  mLastComputedTimestampShift = this->average(window.begin(), window.begin()+useCount);
76  return mLastComputedTimestampShift;
77 }
78 
79 
81 {
82  double delta = timestamp.msecsTo(QDateTime::currentDateTime());
83  mDeltaWindow.push_back(delta);
84  while (mDeltaWindow.size() > mMaxWindowSize)
85  mDeltaWindow.pop_front();
86 }
87 
89 {
90  this->addTimestamp(QDateTime::fromMSecsSinceEpoch(timestamp));
91 }
92 
93 
94 } /* namespace cx */
95 
Scalar * end()
Scalar * begin()
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:27
bool operator()(const double &d1, const double &d2)
Namespace for all CustusX production code.