| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkRealTimeClock.cxx.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:46 $ |
| 7 |
|
Version: $Revision: 1.4 $ |
| 8 |
|
|
| 9 |
|
Copyright (c) Insight Software Consortium. All rights reserved. |
| 10 |
|
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details. |
| 11 |
|
|
| 12 |
|
This software is distributed WITHOUT ANY WARRANTY; without even |
| 13 |
|
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 |
|
PURPOSE. See the above copyright notices for more information. |
| 15 |
|
|
| 16 |
DEF |
=========================================================================*/ |
| 17 |
|
|
| 18 |
|
#include <iostream> |
| 19 |
|
#include "itkRealTimeClock.h" |
| 20 |
|
|
| 21 |
|
#if defined(WIN32) || defined(_WIN32) |
| 22 |
|
|
| 23 |
|
#include <windows.h> |
| 24 |
|
|
| 25 |
|
#else |
| 26 |
|
|
| 27 |
|
#include <sys/time.h> |
| 28 |
|
|
| 29 |
|
#endif // defined(WIN32) || defined(_WIN32) |
| 30 |
|
|
| 31 |
|
|
| 32 |
|
namespace itk |
| 33 |
|
{ |
| 34 |
|
|
| 35 |
|
/** Constructor */ |
| 36 |
|
RealTimeClock::RealTimeClock():m_Frequency(1) |
| 37 |
|
{ |
| 38 |
|
#if defined(WIN32) || defined(_WIN32) |
| 39 |
|
|
| 40 |
|
LARGE_INTEGER frequency; |
| 41 |
|
::QueryPerformanceFrequency(&frequency); |
| 42 |
|
|
| 43 |
|
this->m_Frequency = |
| 44 |
IND |
****static_cast< FrequencyType >( (__int64)frequency.QuadPart ); |
| 45 |
|
|
| 46 |
|
SYSTEMTIME st1; |
| 47 |
|
SYSTEMTIME st2; |
| 48 |
|
FILETIME ft1; |
| 49 |
|
FILETIME ft2; |
| 50 |
|
|
| 51 |
|
::memset( &st1, 0, sizeof( st1 ) ); |
| 52 |
|
::memset( &st2, 0, sizeof( st2 ) ); |
| 53 |
|
|
| 54 |
|
st1.wYear = 1601; |
| 55 |
|
st1.wMonth = 1; |
| 56 |
|
st1.wDay = 1; |
| 57 |
|
|
| 58 |
|
st2.wYear = 1970; |
| 59 |
|
st2.wMonth = 1; |
| 60 |
|
st2.wDay = 1; |
| 61 |
|
|
| 62 |
|
::SystemTimeToFileTime(&st1, &ft1); |
| 63 |
|
::SystemTimeToFileTime(&st2, &ft2); |
| 64 |
|
|
| 65 |
|
LARGE_INTEGER ui1; |
| 66 |
|
LARGE_INTEGER ui2; |
| 67 |
|
|
| 68 |
|
memcpy( &ui1, &ft1, sizeof( ui1 ) ); |
| 69 |
|
memcpy( &ui2, &ft2, sizeof( ui2 ) ); |
| 70 |
|
|
| 71 |
|
this->m_Difference = |
| 72 |
IND |
****static_cast< TimeStampType >( ui2.QuadPart - ui1.QuadPart) / |
| 73 |
IND |
****static_cast< TimeStampType >( 1e7 ); |
| 74 |
|
|
| 75 |
|
FILETIME currentTime; |
| 76 |
|
LARGE_INTEGER intTime; |
| 77 |
|
LARGE_INTEGER tick; |
| 78 |
|
|
| 79 |
|
::GetSystemTimeAsFileTime( ¤tTime ); |
| 80 |
|
::QueryPerformanceCounter( &tick ); |
| 81 |
|
|
| 82 |
|
memcpy( &intTime, ¤tTime, sizeof( intTime ) ); |
| 83 |
|
|
| 84 |
|
this->m_Origin = |
| 85 |
IND |
****static_cast< TimeStampType >( intTime.QuadPart ) / |
| 86 |
IND |
****static_cast< TimeStampType >( 1e7 ); |
| 87 |
|
|
| 88 |
|
this->m_Origin -= |
| 89 |
IND |
****static_cast< TimeStampType >( (__int64)tick.QuadPart ) / |
| 90 |
IND |
****this->m_Frequency; |
| 91 |
|
|
| 92 |
|
this->m_Origin += this->m_Difference; |
| 93 |
|
|
| 94 |
|
|
| 95 |
IND |
#else |
| 96 |
|
|
| 97 |
SEM |
this->m_Frequency = 1e6;; |
| 98 |
|
|
| 99 |
|
#endif // defined(WIN32) || defined(_WIN32) |
| 100 |
|
} |
| 101 |
|
|
| 102 |
|
/** Destructor */ |
| 103 |
|
RealTimeClock::~RealTimeClock() |
| 104 |
|
{ |
| 105 |
|
} |
| 106 |
|
|
| 107 |
|
/** Returns a timestamp in seconds */ |
| 108 |
|
RealTimeClock::TimeStampType |
| 109 |
|
RealTimeClock::GetTimeStamp() const |
| 110 |
|
{ |
| 111 |
|
#if defined(WIN32) || defined(_WIN32) |
| 112 |
|
|
| 113 |
|
LARGE_INTEGER tick; |
| 114 |
|
|
| 115 |
|
::QueryPerformanceCounter( &tick ); |
| 116 |
|
|
| 117 |
|
TimeStampType value = |
| 118 |
IND |
******static_cast< TimeStampType >( (__int64)tick.QuadPart ) / |
| 119 |
IND |
******this->m_Frequency; |
| 120 |
|
|
| 121 |
|
value += this->m_Origin; |
| 122 |
|
|
| 123 |
|
return value; |
| 124 |
|
|
| 125 |
IND |
#else |
| 126 |
|
|
| 127 |
|
struct timeval tval; |
| 128 |
|
|
| 129 |
|
::gettimeofday( &tval, 0 ); |
| 130 |
|
|
| 131 |
|
TimeStampType value = |
| 132 |
IND |
****static_cast< TimeStampType >( tval.tv_sec ) + |
| 133 |
IND |
****static_cast< TimeStampType >( tval.tv_usec ) / this->m_Frequency; |
| 134 |
|
|
| 135 |
|
return value; |
| 136 |
|
|
| 137 |
|
#endif // defined(WIN32) || defined(_WIN32) |
| 138 |
|
} |
| 139 |
|
|
| 140 |
|
|
| 141 |
|
/** Print the object */ |
| 142 |
|
void RealTimeClock::PrintSelf( std::ostream& os, itk::Indent indent ) const |
| 143 |
|
{ |
| 144 |
|
Superclass::PrintSelf(os, indent); |
| 145 |
|
|
| 146 |
|
os << indent << "Frequency of the clock: " |
| 147 |
|
<< this->m_Frequency << std::endl; |
| 148 |
|
os << indent << "Difference : " |
| 149 |
|
<< this->m_Difference << std::endl; |
| 150 |
|
os << indent << "Origin : " |
| 151 |
|
<< this->m_Origin << std::endl; |
| 152 |
|
} |
| 153 |
|
|
| 154 |
|
} |
| 155 |
|
|