-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathSequence.cpp
More file actions
392 lines (336 loc) · 10.7 KB
/
Sequence.cpp
File metadata and controls
392 lines (336 loc) · 10.7 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#include "Sequence.hpp"
#include "detail/FileNumbers.hpp"
#include <boost/filesystem/path.hpp>
#include <boost/regex.hpp>
#include <boost/unordered_map.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/foreach.hpp>
#include <set>
#include <ostream>
namespace sequenceParser {
namespace bfs = boost::filesystem;
/// All regex to recognize a pattern
// common used pattern with # or @
static const boost::regex regexPatternStandard( "(.*?)" // anything but without priority
"\\[?" // if pattern is myimage[####].jpg, don't capture []
"(#+|@+)" // we capture all # or @
"\\]?" // possible end of []
"(.*?)" // anything
);
// C style pattern
static const boost::regex regexPatternCStyle( "(.*?)" // anything but without priority
"\\[?" // if pattern is myimage[%04d].jpg, don't capture []
"%([0-9]*)d" // we capture the padding value (eg. myimage%04d.jpg)
"\\]?" // possible end of []
"(.*?)" // anything
);
// image name
static const boost::regex regexPatternFrame( "(.*?" // anything but without priority
"[_\\.]?)" // if multiple numbers, the number surround with . _ get priority (eg. seq1shot04myimage.0123.jpg -> 0123)
"\\[?" // if pattern is myimage[0001].jpg, don't capture []
"([0-9]+)" // one frame number, can only be positive ( 0012 )
"\\]?" // possible end of []
"([_\\.]?" // if multiple numbers, the number surround with . _ get priority (eg. seq1shot04myimage.0123.jpg -> 0123)
".*\\.?" //
".*?)" // anything
);
// image name with negative indexes
static const boost::regex regexPatternFrameNeg( "(.*?" // anything but without priority
"[_\\.]?)" // if multiple numbers, the number surround with . _ get priority (eg. seq1shot04myimage.0123.jpg -> 0123)
"\\[?" // if pattern is myimage[0001].jpg, don't capture []
"([\\-\\+]?[0-9]+)" // one frame number, can be positive or negative values ( -0012 or +0012 or 0012)
"\\]?" // possible end of []
"([_\\.]?" // if multiple numbers, the number surround with . _ get priority (eg. seq1shot04myimage.0123.jpg -> 0123)
".*\\.?" //
".*?)" // anything
);
template<typename T>
inline T greatestCommonDivisor( T a, T b )
{
T r;
if( b == 0 )
return 0;
while( ( r = a % b ) != 0 )
{
a = b;
b = r;
}
return b;
}
/**
* @brief Find the biggest common step from a set of all steps.
*/
std::size_t greatestCommonDivisor( const std::set<std::size_t>& steps )
{
if( steps.size() == 1 )
{
return *steps.begin();
}
std::set<std::size_t> allSteps;
for( std::set<std::size_t>::const_iterator itA = steps.begin(), itB = ++steps.begin(), itEnd = steps.end(); itB != itEnd; ++itA, ++itB )
{
allSteps.insert( greatestCommonDivisor( *itB, *itA ) );
}
return greatestCommonDivisor( allSteps );
}
/**
* @brief Extract step from a sorted vector of time values.
*/
std::size_t extractStep( const std::vector<Time>& times )
{
if( times.size() <= 1 )
{
return 1;
}
std::set<std::size_t> allSteps;
for( std::vector<Time>::const_iterator itA = times.begin(), itB = ++times.begin(), itEnd = times.end(); itB != itEnd; ++itA, ++itB )
{
allSteps.insert( *itB - *itA );
}
return greatestCommonDivisor( allSteps );
}
/**
* @brief Extract step from a sorted vector of time values.
*/
std::size_t extractStep( const std::vector<detail::FileNumbers>::const_iterator& timesBegin, const std::vector<detail::FileNumbers>::const_iterator& timesEnd, const std::size_t i )
{
if( std::distance( timesBegin, timesEnd ) <= 1 )
{
return 1;
}
std::set<std::size_t> allSteps;
for( std::vector<detail::FileNumbers>::const_iterator itA = timesBegin, itB = boost::next(timesBegin), itEnd = timesEnd; itB != itEnd; ++itA, ++itB )
{
allSteps.insert( itB->getTime( i ) - itA->getTime( i ) );
}
return greatestCommonDivisor( allSteps );
}
std::size_t getFixedPaddingFromStringNumber( const std::string& timeStr )
{
if( timeStr.size() > 1 )
{
// if the number is signed, this charater does not count as padding.
if( timeStr[0] == '-' || timeStr[0] == '+' )
{
return timeStr.size() - 1;
}
}
return timeStr.size();
}
/**
* @brief extract the padding from a vector of frame numbers
* @param[in] timesStr vector of frame numbers in string format
*/
std::size_t extractPadding( const std::vector<std::string>& timesStr )
{
BOOST_ASSERT( timesStr.size() > 0 );
const std::size_t padding = getFixedPaddingFromStringNumber( timesStr.front() );
BOOST_FOREACH( const std::string& s, timesStr )
{
if( padding != getFixedPaddingFromStringNumber( s ) )
{
return 0;
}
}
return padding;
}
std::size_t extractPadding( const std::vector<detail::FileNumbers>::const_iterator& timesBegin, const std::vector<detail::FileNumbers>::const_iterator& timesEnd, const std::size_t i )
{
BOOST_ASSERT( timesBegin != timesEnd );
std::set<std::size_t> padding;
std::set<std::size_t> maxPadding;
for( std::vector<detail::FileNumbers>::const_iterator s = timesBegin;
s != timesEnd;
++s )
{
padding.insert( s->getFixedPadding(i) );
maxPadding.insert( s->getMaxPadding(i) );
}
std::set<std::size_t> pad = padding;
pad.erase(0);
if( pad.size() == 0 )
{
return 0;
}
else if( pad.size() == 1 )
{
return *pad.begin();
}
// @todo multi-padding !
// need to split into multiple sequences !
return 0;
}
std::string Sequence::getFilenameAt( const Time time ) const
{
std::ostringstream o;
if( time >= 0 )
{
// "prefix.0001.jpg"
o << _prefix << std::setw( _fixedPadding ) << std::setfill( _fillCar ) << time << _suffix;
}
else
{
// "prefix.-0001.jpg" (and not "prefix.000-1.jpg")
o << _prefix << "-" << std::setw( _fixedPadding ) << std::setfill( _fillCar ) << std::abs( (int) time ) << _suffix;
}
return o.str();
}
std::string Sequence::getCStylePattern() const
{
if( getFixedPadding() )
return getPrefix() + "%0" + boost::lexical_cast<std::string > ( getFixedPadding() ) + "d" + getSuffix();
else
return getPrefix() + "%d" + getSuffix();
}
Time Sequence::getNbFiles() const
{
Time nbFiles = 0;
BOOST_FOREACH(const FrameRange& frameRange, _ranges)
{
nbFiles += frameRange.getNbFrames();
}
return nbFiles;
}
bool Sequence::isIn( const std::string& filename, Time& time, std::string& timeStr )
{
// initialize the output arguments
time = 0;
timeStr = "";
const std::size_t min = _prefix.size() + _suffix.size();
// different size of the filename
if( filename.size() <= min )
return false;
// different prefix or suffix
if( filename.substr( 0, _prefix.size() ) != _prefix || filename.substr( filename.size() - _suffix.size(), _suffix.size() ) != _suffix )
return false;
// not an existing time
std::istringstream iss( filename.substr( _prefix.size(), filename.size() - _prefix.size() - _suffix.size() ) );
size_t expectedTime;
iss >> expectedTime;
bool timeIsIn = false;
BOOST_FOREACH( Time t, getFramesIterable() )
{
if(expectedTime == t)
{
timeIsIn = true;
break;
}
}
if( ! timeIsIn )
return false;
try
{
timeStr = filename.substr( _prefix.size(), filename.size() - _suffix.size() - _prefix.size() );
time = boost::lexical_cast<Time > ( timeStr );
}
catch( ... )
{
return false;
}
return true;
}
EPattern Sequence::checkPattern( const std::string& pattern, const EDetection detectionOptions )
{
if( regex_match( pattern.c_str(), regexPatternStandard ) )
{
return ePatternStandard;
}
else if( regex_match( pattern.c_str(), regexPatternCStyle ) )
{
return ePatternCStyle;
}
else if( ( detectionOptions & eDetectionNegative ) && regex_match( pattern.c_str(), regexPatternFrameNeg ) )
{
return ePatternFrameNeg;
}
else if( regex_match( pattern.c_str(), regexPatternFrame ) )
{
return ePatternFrame;
}
return ePatternNone;
}
/**
* @brief This function creates a regex from the pattern,
* and init internal values.
* @param[in] pattern
* @param[in] accept
*/
bool Sequence::initFromPattern( const std::string& filePattern, const EPattern& accept )
{
boost::cmatch matches;
//std::cout << filePattern << " / " << _prefix << " + " << _fixedPadding << " + " << _suffix << std::endl;
if( ( accept & ePatternStandard ) && regex_match( filePattern.c_str(), matches, regexPatternStandard ) )
{
std::string paddingStr( matches[2].first, matches[2].second );
_fixedPadding = paddingStr.size();
_maxPadding = _fixedPadding;
}
else if( ( accept & ePatternCStyle ) && regex_match( filePattern.c_str(), matches, regexPatternCStyle ) )
{
std::string paddingStr( matches[2].first, matches[2].second );
_fixedPadding = paddingStr.size() == 0 ? 0 : boost::lexical_cast<std::size_t > ( paddingStr ); // if no padding value: %d -> _fixedPadding = 0
_maxPadding = _fixedPadding;
}
else if( ( accept & ePatternFrame ) && regex_match( filePattern.c_str(), matches, regexPatternFrame ) )
{
std::string frame( matches[2].first, matches[2].second );
// Time t = boost::lexical_cast<Time>( frame );
_fixedPadding = frame.size();
_maxPadding = _fixedPadding;
}
else if( ( accept & ePatternFrameNeg ) && regex_match( filePattern.c_str(), matches, regexPatternFrameNeg ) )
{
std::string frame( matches[2].first, matches[2].second );
// Time t = boost::lexical_cast<Time>( frame );
_fixedPadding = frame.size();
_maxPadding = _fixedPadding;
}
else
{
// this is a file, not a sequence
return false;
}
_prefix = std::string( matches[1].first, matches[1].second );
_suffix = std::string( matches[3].first, matches[3].second );
return true;
}
void Sequence::init( const std::string& prefix, const std::size_t padding, const std::size_t maxPadding, const std::string& suffix, const Time firstTime, const Time lastTime, const Time step )
{
_prefix = prefix;
_fixedPadding = padding;
_maxPadding = maxPadding;
_suffix = suffix;
_ranges.clear();
_ranges.push_back(FrameRange(firstTime, lastTime, step));
}
std::vector<std::string> Sequence::getFiles() const
{
std::vector<std::string> allPaths;
BOOST_FOREACH(const FrameRange& range, _ranges)
{
for( Time t = range.first; t <= range.last; t += range.step )
allPaths.push_back(getFilenameAt(t));
}
return allPaths;
}
std::vector<boost::filesystem::path> Sequence::getAbsoluteFilesPath(boost::filesystem::path const& parentPath) const{
std::vector<boost::filesystem::path> allPaths;
BOOST_FOREACH(const FrameRange& range, _ranges)
{
for( Time t = range.first; t <= range.last; t += range.step )
allPaths.push_back(parentPath / getFilenameAt(t));
}
return allPaths;
}
std::string Sequence::string() const
{
std::ostringstream ss;
ss << *this;
return ss.str();
}
std::ostream& operator<<(std::ostream& os, const Sequence& sequence)
{
os << sequence.getFilenameWithStandardPattern() << " [" << sequence.getFrameRanges() << "]";
return os;
}
}