-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReadTimestampRawFile.cpp
executable file
·209 lines (178 loc) · 4.89 KB
/
ReadTimestampRawFile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
* @file ReadTimestampRawFile.cpp
* @ingroup DataManagement
* @author Dominique Vaufreydaz, Grenoble Alpes University, Inria
* @copyright All right reserved.
*/
#include "ReadTimestampRawFile.h"
#include <string.h>
using namespace MobileRGBD;
/** @brief Constructor. Create a ReadTimestampRawFile object using specific files (timestamp + raw).
*
* @param WorkingFile [in] Name of the timestamp file to open (even with '/' separator under Windows as Windows handles it also as a folder/file separator).
* @param RawFile [in] Name of the associated raw file.
* @param SizeOfFrame [in] Size of each frame in raw file.
*/
ReadTimestampRawFile::ReadTimestampRawFile( const std::string &WorkingFile, const std::string& RawFile, int SizeOfFrame )
: ReadTimestampFile( WorkingFile )
{
Mode = SimpleFrameMode;
NumberOfSubFrames = 0;
StartingFrame = 0;
CurrentIndex = 0;
RawFileName = RawFile;
FrameSize = SizeOfFrame;
FrameBuffer.SetNewBufferSize(SizeOfFrame+1);
IndexofFrameBuffer = -1;
}
/** @brief Restart file at beginning (if file is closed, file is re-opened).
* * Overload of TimestampFile::Reinit.
*/
void ReadTimestampRawFile::Reinit()
{
ReadTimestampFile::Reinit();
if ( fin.IsOpen() == false )
{
return;
}
// Reinit number of subframes
NumberOfSubFrames = 0;
// try to read a line
LineBuffer[0] = '\0';
if ( fgets( LineBuffer, LineBufferSize-1, fin ) == (char*)NULL )
{
return;
}
// Get Starting Frame (in case it is not 0)
if ( sscanf( LineBuffer, "%*d.%*d%*[ \t]%d", &StartingFrame ) != 1 )
{
return;
}
// Reset file to begining
fin.Seek( 0L, SEEK_SET );
// Restore starting current indexes
IndexofFrameBuffer = -1;
CurrentIndex = 0;
}
/** @brief Search for the timestamp and if found and process it by calling ProcessElement.
* * Overload of ReadTimestampFile::Process.
*
* @param RequestedTimestamp [in] Requested timestamp.
* @param UserData [in] Pointer on a UserData pass to the ProcessElement function (default=nullptr).
* @return True if everything went fine.
*/
bool ReadTimestampRawFile::Process( const TimeB &RequestTimestamp, void * UserData /* = nullptr */ )
{
if ( LoadFrame( RequestTimestamp ) == true )
{
return ProcessElement( RequestTimestamp, UserData );
}
return false;
}
/** @brief Get the frame number for the current data.
*
* @return -1 if an error occured or the zero based index for the frame.
*/
int ReadTimestampRawFile::GetFrameNumber()
{
int FrameIndex = -1;
// If sscanf failed, FrameIndex remains -1
sscanf( DataBuffer, "%d", &FrameIndex );
return FrameIndex;
}
/** @brief Load frame for a specific timestamp
*
* @param RequestedTimestamp [in] Requested timestamp.
* @return True if the full frame was loaded.
*/
bool ReadTimestampRawFile::LoadFrame( const TimeB &RequestTimestamp )
{
if ( GetDataForTimestamp( RequestTimestamp ) == false )
{
// Can not get data
return false;
}
// Get corresponding frame index
int FrameIndex = GetFrameNumber();
if( FrameIndex >= 0 )
{
return GetFrame( FrameIndex );
}
return false;
}
/** @brief Get frame for a specific index
*
* @param WantedIndex [in] Frame number in the raw file.
* @return True if the full frame was loaded.
*/
bool ReadTimestampRawFile::GetFrame( int WantedIndex )
{
int Index = WantedIndex - StartingFrame;
NumberOfSubFrames = 0;
if ( fRaw.IsOpen() == false )
{
if ( fRaw.Open( RawFileName.c_str(), DataFile::READ_MODE ) == false )
{
return false;
}
}
// The frame buffer contains already data for this index
if ( Index == IndexofFrameBuffer )
{
return true;
}
// If we are in multiple mode, we want to load all frame at once
// Default values for single mode
int LoadSize;
if ( Mode == SubFramesMode )
{
// Here we need to get the number of sub-frames
if ( sscanf( DataBuffer, "%*d, %d", &NumberOfSubFrames ) != 1 )
{
// Could not find subframe number
fprintf( stderr, "Could not retrieve number of subFrame in SubFramesMode\n" );
return false;
}
if ( NumberOfSubFrames == 0 )
{
// Here, nothing to load timestamp with empty data, loaded !
return true;
}
// Compute new load size
LoadSize = FrameSize * NumberOfSubFrames;
// Increase if needed size of buffer
FrameBuffer.SetNewBufferSize( LoadSize );
}
else
{
// In simple mode, there is alsways a frame
NumberOfSubFrames = 1;
LoadSize = FrameSize;
}
if ( Index == CurrentIndex )
{
// idilic case
if ( fRaw.Read( FrameBuffer, LoadSize, 1) != 1 )
{
return false;
}
IndexofFrameBuffer = Index;
CurrentIndex += NumberOfSubFrames;
return true;
}
// Try to go to new position and read a frame
int64_t NewPos = (int64_t)Index*(int64_t)FrameSize;
if ( fRaw.Seek( NewPos, SEEK_SET ) != 0 )
{
return false;
}
if ( fRaw.Read( FrameBuffer, LoadSize, 1 ) != 1 )
{
return false;
}
// Data in memory buffer is Index
IndexofFrameBuffer = Index;
// Current Index is now the next one
CurrentIndex = Index + NumberOfSubFrames;
return true;
}