Fraxinus  16.5.0-fx-rc7
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
34 
35 #include "cxLogger.h"
36 #include "cxImage.h"
37 
38 namespace cx
39 {
40 
42 {
43 public:
44  AbsDoubleLess(double center) : mCenter(center) { };
45 
46  bool operator()(const double& d1, const double& d2)
47  {
48  return fabs(d1 - mCenter) < fabs(d2 - mCenter);
49  }
50 
51  double mCenter;
52 };
53 
57 
59  mLastComputedTimestampShift(0),
60  mMaxWindowSize(20)
61 {
62 
63 }
64 
65 template<class ITER>
66 double StreamedTimestampSynchronizer::average(ITER begin, ITER end) const
67 {
68  int size = std::distance(begin, end);
69  double sum = 0;
70  for (; begin != end; ++begin)
71  sum += *begin;
72  return sum / size;
73 }
74 
76 {
77  QDateTime ts = imgMsg->getAcquisitionTime();
78  imgMsg->setOriginalAcquisitionTime(ts);
79  this->addTimestamp(ts);
80  ts = ts.addMSecs(this->getShift());
81 
82  imgMsg->setAcquisitionTime(ts);
83 }
84 
86 {
87  if (mDeltaWindow.empty())
88  return 0;
89 
90  QList<double> window = mDeltaWindow;
91  std::sort(window.begin(), window.end(), AbsDoubleLess(mLastComputedTimestampShift));
92 
93  double shrinkedWindowSize = double(window.size()) * 0.75;
94  int useCount = (int)(shrinkedWindowSize + 0.5);
95 
96  mLastComputedTimestampShift = this->average(window.begin(), window.begin()+useCount);
97  return mLastComputedTimestampShift;
98 }
99 
100 
102 {
103  double delta = timestamp.msecsTo(QDateTime::currentDateTime());
104  mDeltaWindow.push_back(delta);
105  while (mDeltaWindow.size() > mMaxWindowSize)
106  mDeltaWindow.pop_front();
107 }
108 
110 {
111  this->addTimestamp(QDateTime::fromMSecsSinceEpoch(timestamp));
112 }
113 
114 
115 } /* namespace cx */
116 
Scalar * end()
Scalar * begin()
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:48
bool operator()(const double &d1, const double &d2)