QTrk
Classes | Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
ResultManager Class Reference

Class that handles data gathering and saving from QueuedTracker instances. More...

#include <ResultManager.h>

Classes

struct  FrameCounters
 Structure to keep track of frame counts. More...
 
struct  FrameResult
 Structure to save all bead results of a single frame in memory. More...
 

Public Member Functions

 ResultManager (const char *outfile, const char *frameinfo, ResultManagerConfig *cfg, std::vector< std::string > colnames)
 Create an instance of ResultManager. More...
 
 ~ResultManager ()
 Destroy an instance of ResultManager. More...
 
void SaveSection (int start, int end, const char *beadposfile, const char *infofile)
 Save an interval of frames to a file. More...
 
void SetTracker (QueuedTracker *qtrk)
 Set the tracker from which to fetch results. More...
 
QueuedTrackerGetTracker ()
 Get the tracker from which results are fetched. More...
 
int GetBeadPositions (int startFrame, int endFrame, int bead, LocalizationResult *r)
 Get the positions of a single bead over an interval of frames. More...
 
int GetResults (LocalizationResult *results, int startFrame, int numResults)
 Get the positions of all beads over an interval of frames. More...
 
void Flush ()
 Write all available data regardless of ResultManagerConfig::writeInterval. More...
 
FrameCounters GetFrameCounters ()
 Returns a FrameCounters structure with the current counts. More...
 
void StoreFrameInfo (int frame, double timestamp, float *columns)
 Store metadata for a frame. This data will be saved in the info file. More...
 
int GetFrameCount ()
 Returns the number of captured frames. More...
 
bool RemoveBeadResults (int bead)
 Remove all results of a certain bead. More...
 
const ResultManagerConfigConfig ()
 Returns a reference to the used configuration to review. More...
 

Protected Member Functions

bool CheckResultSpace (int fr)
 Checks if a frame index is valid. More...
 
void Write ()
 Write available results to output files. More...
 
void WriteBinaryResults ()
 Write available data to a binary file. More...
 
void WriteTextResults ()
 Write available data to a text file. More...
 
void StoreResult (LocalizationResult *r)
 Copies results from QueuedTracker to internal data structures. More...
 
bool Update ()
 General worker function called from ThreadLoop. More...
 
void WriteBinaryFileHeader ()
 Writes metadata to the header of a binary data file. More...
 

Static Protected Member Functions

static void ThreadLoop (void *param)
 The base loop of the gathering and saving thread. More...
 

Protected Attributes

Threads::Mutex resultMutex
 Mutex to govern access to frameResults. More...
 
Threads::Mutex trackerMutex
 Mutex to govern access to the linked QueuedTracker instance. More...
 
std::vector< std::string > frameInfoNames
 Vector with frame info column names. See WriteBinaryFileHeader. More...
 
std::deque< FrameResult *> frameResults
 Vector to hold the results for all frames. More...
 
FrameCounters cnt
 Local instance of FrameCounters to maintain counts accross functions. More...
 
ResultManagerConfig config
 Local instance of ResultManagerConfig with the settings used by this instance of ResultManager. More...
 
ResultFileresultFile
 Local instance of ResultFile to generalize output to text or binary files. Not used. More...
 
QueuedTrackerqtrk
 Pointer to the QueuedTracker instance to check. More...
 
std::string outputFile
 Path and filename of the main data output file. More...
 
std::string frameInfoFile
 Path and filename of the frame info (metadata) file. More...
 
Threads::Handlethread
 Handle to the thread running the ThreadLoop. More...
 
Atomic< bool > quit
 Flag to exit the threads. More...
 

Detailed Description

Class that handles data gathering and saving from QueuedTracker instances.

Creates a separate thread to check a linked QTrk instance for new data. Gathers data and saves to the disk at regular intervals. Mutexes used to ensure thread safety.
Results from QueuedTracker are sorted as [frame][bead] to enable saving data on a frame-per-line basis.

Definition at line 91 of file ResultManager.h.

Constructor & Destructor Documentation

§ ResultManager()

ResultManager::ResultManager ( const char *  outfile,
const char *  frameinfo,
ResultManagerConfig cfg,
std::vector< std::string >  colnames 
)

Create an instance of ResultManager.

Parameters
[in]outfileString (char array) with full path and filename of desired output file for data.
[in]frameinfoString (char array) with full path and filename of desired output file for frame metadata.
[in]cfgPointer to structure ResultManagerConfig with settings to use.
[in]colnamesVector of names for the columns in the frame info file. Size must equal ResultManagerConfig::numFrameInfoColumns.
Only used for binary files.

Definition at line 42 of file ResultManager.cpp.

43 {
44  config = *cfg;
45  outputFile = outfile;
47 
48  qtrk = 0;
49 
51  quit=false;
52 
53  if (!outputFile.empty())
54  remove(outfile);
55  if (!this->frameInfoFile.empty())
56  remove(frameInfoFile);
57 
58  frameInfoNames = colnames;
59 
60  if (config.binaryOutput) {
62  }
63 
64  dbgprintf("Allocating ResultManager with %d beads, %d motor columns and writeinterval %d\n", cfg->numBeads, cfg->numFrameInfoColumns, cfg->writeInterval);
65 }
int numBeads
Number of beads for which to grab results. Should always equal the amount of beads in a single frame...
Definition: ResultManager.h:64
static void ThreadLoop(void *param)
The base loop of the gathering and saving thread.
ResultManagerConfig config
Local instance of ResultManagerConfig with the settings used by this instance of ResultManager.
QueuedTracker * qtrk
Pointer to the QueuedTracker instance to check.
static Handle * Create(ThreadEntryPoint method, void *param)
Definition: threads.h:99
int numFrameInfoColumns
Number of columns in the frame info metadata file. Additional columns can be added to save more data ...
Definition: ResultManager.h:65
Threads::Handle * thread
Handle to the thread running the ThreadLoop.
void WriteBinaryFileHeader()
Writes metadata to the header of a binary data file.
Atomic< bool > quit
Flag to exit the threads.
void dbgprintf(const char *fmt,...)
Definition: utils.cpp:149
int writeInterval
Interval of number of gathered frames at which to write the data.
Definition: ResultManager.h:79
uint8_t binaryOutput
Flag (boolean) to output a binary file instead of a text file.
Definition: ResultManager.h:81
std::string outputFile
Path and filename of the main data output file.
std::string frameInfoFile
Path and filename of the frame info (metadata) file.
std::vector< std::string > frameInfoNames
Vector with frame info column names. See WriteBinaryFileHeader.

§ ~ResultManager()

ResultManager::~ResultManager ( )

Destroy an instance of ResultManager.

Sets a flag to stop the thread and frees all results from memory.

Definition at line 95 of file ResultManager.cpp.

96 {
97  quit = true;
99 
101 }
std::deque< FrameResult *> frameResults
Vector to hold the results for all frames.
Threads::Handle * thread
Handle to the thread running the ThreadLoop.
void DeleteAllElems(T &c)
Definition: utils.h:19
static void WaitAndClose(Handle *h)
Definition: threads.h:128
Atomic< bool > quit
Flag to exit the threads.

Member Function Documentation

§ CheckResultSpace()

bool ResultManager::CheckResultSpace ( int  fr)
protected

Checks if a frame index is valid.

Frame index is valid if within bounds of the current frames allowed in memory. Initializes the required FrameResult to store data for the frame fr if it is valid.

Parameters
[in]frFrame index to be checked.
Return values
TrueIndex is fine. New FrameResult initialized and added to results vector.
FalseIndex is invalid. Can't process this frame index.

Definition at line 361 of file ResultManager.cpp.

362 {
363  if(fr < cnt.startFrame)
364  return false; // already removed, ignore
365 
366  if (fr > cnt.processedFrames + 20000) {
367 
368  dbgprintf("ResultManager: Ignoring suspiciously large frame number (%d).\n", fr);
369 
370  return false;
371  }
372 
373  while ((uint)fr >= cnt.startFrame + frameResults.size()) {
374  frameResults.push_back (new FrameResult( config.numBeads, config.numFrameInfoColumns));
375  }
376  return true;
377 }
int numBeads
Number of beads for which to grab results. Should always equal the amount of beads in a single frame...
Definition: ResultManager.h:64
int processedFrames
Number of frames processed by the ResultManager.
unsigned int uint
Definition: std_incl.h:127
ResultManagerConfig config
Local instance of ResultManagerConfig with the settings used by this instance of ResultManager.
int startFrame
Index of the first frame for which results are available.
std::deque< FrameResult *> frameResults
Vector to hold the results for all frames.
int numFrameInfoColumns
Number of columns in the frame info metadata file. Additional columns can be added to save more data ...
Definition: ResultManager.h:65
FrameCounters cnt
Local instance of FrameCounters to maintain counts accross functions.
void dbgprintf(const char *fmt,...)
Definition: utils.cpp:149

§ Config()

const ResultManagerConfig& ResultManager::Config ( )
inline

Returns a reference to the used configuration to review.

Returns
The ResultManagerConfig structure used for this instance of ResultManager. Read-only.

Definition at line 208 of file ResultManager.h.

208 { return config; }
ResultManagerConfig config
Local instance of ResultManagerConfig with the settings used by this instance of ResultManager.

§ Flush()

void ResultManager::Flush ( )

Write all available data regardless of ResultManagerConfig::writeInterval.

Use to write unsaved frames from memory when no new ones are expected anymore. Writes all leftover data after waiting for the linked QueuedTracker to finish its queued localizations.

Definition at line 307 of file ResultManager.cpp.

308 {
309  trackerMutex.lock();
310  if (qtrk) qtrk->Flush();
312 
313  Update();
314 
315  resultMutex.lock();
316 
317  Write();
318 
319  // Dump stats about unfinished frames for debugging
320 #ifdef _DEBUG
321  for (uint i=0;i<frameResults.size();i++) {
322  FrameResult *fr = frameResults[i];
323 // dbgprintf("Frame %d. TS: %f, Count: %d\n", i, fr->timestamp, fr->count);
324  if (fr->count != config.numBeads) {
325  for (int j=0;j<fr->results.size();j++) {
326  if( fr->results[j].job.frame == 0)
327  dbgprintf("%d, ", j );
328  }
329  dbgprintf("\n");
330  }
331  }
332 #endif
334 }
int numBeads
Number of beads for which to grab results. Should always equal the amount of beads in a single frame...
Definition: ResultManager.h:64
Threads::Mutex trackerMutex
Mutex to govern access to the linked QueuedTracker instance.
virtual void Flush()=0
Stop waiting for more jobs to do, and just process the current batch.
Threads::Mutex resultMutex
Mutex to govern access to frameResults.
void lock()
Definition: threads.h:74
unsigned int uint
Definition: std_incl.h:127
ResultManagerConfig config
Local instance of ResultManagerConfig with the settings used by this instance of ResultManager.
QueuedTracker * qtrk
Pointer to the QueuedTracker instance to check.
std::deque< FrameResult *> frameResults
Vector to hold the results for all frames.
void dbgprintf(const char *fmt,...)
Definition: utils.cpp:149
bool Update()
General worker function called from ThreadLoop.
void unlock()
Definition: threads.h:79
void Write()
Write available results to output files.

§ GetBeadPositions()

int ResultManager::GetBeadPositions ( int  startFrame,
int  endFrame,
int  bead,
LocalizationResult r 
)

Get the positions of a single bead over an interval of frames.

Parameters
[in]startFrameFramenumber of the start of the interval to save.
[in]endFrameFramenumber of the end of the interval to save.
[in]beadBeadnumber of the bead for which the results are requested.
[out]rArray of LocalizationResult in which to store the data.
Has to be initialized before calling this function. Size has to be equal to endFrame - startFrame.
Returns
Number of results obtained.

Definition at line 285 of file ResultManager.cpp.

286 {
287  int count = end-startfr;
288 
289  resultMutex.lock();
290  if (end > cnt.processedFrames)
291  end = cnt.processedFrames;
292 
293  int start = startfr - cnt.startFrame;
294  if (start < 0) start = 0;
295  if (count > cnt.processedFrames-cnt.startFrame)
297 
298  for (int i=0;i<count;i++){
299  results[i] = frameResults[i+start]->results[bead];
300  }
302 
303  return count;
304 }
int processedFrames
Number of frames processed by the ResultManager.
Threads::Mutex resultMutex
Mutex to govern access to frameResults.
void lock()
Definition: threads.h:74
int startFrame
Index of the first frame for which results are available.
std::deque< FrameResult *> frameResults
Vector to hold the results for all frames.
FrameCounters cnt
Local instance of FrameCounters to maintain counts accross functions.
void unlock()
Definition: threads.h:79

§ GetFrameCount()

int ResultManager::GetFrameCount ( )

Returns the number of captured frames.

Returns
Number of captured frames

Definition at line 399 of file ResultManager.cpp.

400 {
401  resultMutex.lock();
402  int nfr = cnt.capturedFrames;
404  return nfr;
405 }
Threads::Mutex resultMutex
Mutex to govern access to frameResults.
void lock()
Definition: threads.h:74
int capturedFrames
Number of frames captured. Counted through calls to StoreFrameInfo.
FrameCounters cnt
Local instance of FrameCounters to maintain counts accross functions.
void unlock()
Definition: threads.h:79

§ GetFrameCounters()

ResultManager::FrameCounters ResultManager::GetFrameCounters ( )

Returns a FrameCounters structure with the current counts.

Definition at line 337 of file ResultManager.cpp.

338 {
339  resultMutex.lock();
340  FrameCounters c = cnt;
342  return c;
343 }
Threads::Mutex resultMutex
Mutex to govern access to frameResults.
void lock()
Definition: threads.h:74
FrameCounters cnt
Local instance of FrameCounters to maintain counts accross functions.
void unlock()
Definition: threads.h:79

§ GetResults()

int ResultManager::GetResults ( LocalizationResult results,
int  startFrame,
int  numResults 
)

Get the positions of all beads over an interval of frames.

Parameters
[in]startFrameFramenumber of the start of the interval to save.
[in]numResultsNumber of frames to save.
[out]resultsArray of LocalizationResult in which to store the data.
Has to be initialized before calling this function. Size has to be equal to numResults.
Returns
Number of results obtained.

Definition at line 345 of file ResultManager.cpp.

346 {
347  resultMutex.lock();
348 
349  if (startFrame >= cnt.startFrame && numFrames+startFrame <= cnt.processedFrames) {
350  for (int f=0;f<numFrames;f++) {
351  int index = f + startFrame - cnt.startFrame;
352  for (int j=0;j<config.numBeads;j++)
353  results[config.numBeads*f+j] = frameResults[index]->results[j];
354  }
355  }
357 
358  return numFrames;
359 }
int numBeads
Number of beads for which to grab results. Should always equal the amount of beads in a single frame...
Definition: ResultManager.h:64
int processedFrames
Number of frames processed by the ResultManager.
Threads::Mutex resultMutex
Mutex to govern access to frameResults.
void lock()
Definition: threads.h:74
ResultManagerConfig config
Local instance of ResultManagerConfig with the settings used by this instance of ResultManager.
int startFrame
Index of the first frame for which results are available.
std::deque< FrameResult *> frameResults
Vector to hold the results for all frames.
FrameCounters cnt
Local instance of FrameCounters to maintain counts accross functions.
void unlock()
Definition: threads.h:79

§ GetTracker()

QueuedTracker * ResultManager::GetTracker ( )

Get the tracker from which results are fetched.

Returns
Pointer to the current QueuedTracker instance.

Definition at line 207 of file ResultManager.cpp.

208 {
209  trackerMutex.lock();
210  QueuedTracker* trk = qtrk;
212  return trk;
213 }
Threads::Mutex trackerMutex
Mutex to govern access to the linked QueuedTracker instance.
void lock()
Definition: threads.h:74
QueuedTracker * qtrk
Pointer to the QueuedTracker instance to check.
void unlock()
Definition: threads.h:79
Abstract tracker interface, implemented by QueuedCUDATracker and QueuedCPUTracker.
Definition: QueuedTracker.h:86

§ RemoveBeadResults()

bool ResultManager::RemoveBeadResults ( int  bead)

Remove all results of a certain bead.

Currently only removes from memory.

Parameters
[in]beadThe index of the bead for which to remove the data.
Todo:
We need to modify the saved data file

Definition at line 407 of file ResultManager.cpp.

408 {
410 
411  for (uint i=0;i<frameResults.size();i++) {
412  auto fr = frameResults[i];
413 
414  fr->count--;
415  fr->results.erase(fr->results.begin()+bead);
416  }
417 
418  return true;
419 }
unsigned int uint
Definition: std_incl.h:127
std::deque< FrameResult *> frameResults
Vector to hold the results for all frames.

§ SaveSection()

void ResultManager::SaveSection ( int  start,
int  end,
const char *  beadposfile,
const char *  infofile 
)

Save an interval of frames to a file.

Warning
Unimplemented.
Parameters
[in]startFramenumber of the start of the interval to save.
[in]endFramenumber of the end of the interval to save.
[in]beadposfileString (char array) with full path and filename of desired output file for data.
[in]infofileString (char array) with full path and filename of desired output file for frame metadata.

§ SetTracker()

void ResultManager::SetTracker ( QueuedTracker qtrk)

Set the tracker from which to fetch results.

Parameters
[in]qtrkPointer to the QueuedTracker instance to fetch results from.

Definition at line 215 of file ResultManager.cpp.

216 {
217  trackerMutex.lock();
218  this->qtrk = qtrk;
220 }
Threads::Mutex trackerMutex
Mutex to govern access to the linked QueuedTracker instance.
void lock()
Definition: threads.h:74
QueuedTracker * qtrk
Pointer to the QueuedTracker instance to check.
void unlock()
Definition: threads.h:79

§ StoreFrameInfo()

void ResultManager::StoreFrameInfo ( int  frame,
double  timestamp,
float *  columns 
)

Store metadata for a frame. This data will be saved in the info file.

Parameters
[in]frameFrame number for which to store the data.
[in]timestampTimestamp for the frame.
[in]columnsArray of float values for the metadata columns. Size must equal ResultManagerConfig::numFrameInfoColumns.

Definition at line 379 of file ResultManager.cpp.

380 {
381  resultMutex.lock();
382 
383  if (CheckResultSpace(frame)) {
384  FrameResult* fr = frameResults[frame-cnt.startFrame];
385 
386  if (!fr->hasFrameInfo) {
387  fr->timestamp = timestamp;
388  for(int i=0;i<config.numFrameInfoColumns;i++)
389  fr->frameInfo[i]=columns[i];
391  fr->hasFrameInfo=true;
392  }
393  }
394 
396 }
bool CheckResultSpace(int fr)
Checks if a frame index is valid.
Threads::Mutex resultMutex
Mutex to govern access to frameResults.
void lock()
Definition: threads.h:74
ResultManagerConfig config
Local instance of ResultManagerConfig with the settings used by this instance of ResultManager.
int startFrame
Index of the first frame for which results are available.
std::deque< FrameResult *> frameResults
Vector to hold the results for all frames.
int numFrameInfoColumns
Number of columns in the frame info metadata file. Additional columns can be added to save more data ...
Definition: ResultManager.h:65
int capturedFrames
Number of frames captured. Counted through calls to StoreFrameInfo.
FrameCounters cnt
Local instance of FrameCounters to maintain counts accross functions.
void unlock()
Definition: threads.h:79

§ StoreResult()

void ResultManager::StoreResult ( LocalizationResult r)
protected

Copies results from QueuedTracker to internal data structures.

Adds a result to the pre-allocated memory of frame results so it can later be saved on a per-frame basis.

Note
Results are changed from pixel to physical values here, using input given on initialization through ResultManagerConfig.
Parameters
[in]rA pointer to a LocalizationResult of a single localization.

Definition at line 104 of file ResultManager.cpp.

105 {
106  if (CheckResultSpace(r->job.frame)) {
107 
108  LocalizationResult scaled = *r;
109  // Make roi-centered pos
110  scaled.pos = scaled.pos - vector3f( qtrk->cfg.width*0.5f, qtrk->cfg.height*0.5f, 0);
111  scaled.pos = ( scaled.pos + config.offset ) * config.scaling;
112  FrameResult* fr = frameResults[r->job.frame - cnt.startFrame];
113  fr->results[r->job.zlutIndex] = scaled;
114  fr->count++;
115 
116  // Advance processedFrames, either because measurements have been completed or because frames have been lost
117  while (cnt.processedFrames - cnt.startFrame < (int) frameResults.size() &&
119  {
121  cnt.lostFrames ++;
122 
123  cnt.processedFrames ++;
124  }
125 
127  } else
128  cnt.lostFrames++;
129 }
int localizationsDone
Amount of localizations finished by the linked QueuedTracker instance.
int numBeads
Number of beads for which to grab results. Should always equal the amount of beads in a single frame...
Definition: ResultManager.h:64
bool CheckResultSpace(int fr)
Checks if a frame index is valid.
int width
Width of regions of interest to be handled. Typically equals height (square ROI). ...
Definition: qtrk_c_api.h:106
vector3f scaling
Scaling factor for each of the three dimensions.
Definition: ResultManager.h:71
int processedFrames
Number of frames processed by the ResultManager.
int zlutIndex
Bead number of this ROI. Used to get the right ZLUT from memory.
Definition: qtrk_c_api.h:58
vector3f offset
Offset value for each of the three dimensions.
Definition: ResultManager.h:78
ResultManagerConfig config
Local instance of ResultManagerConfig with the settings used by this instance of ResultManager.
QueuedTracker * qtrk
Pointer to the QueuedTracker instance to check.
Structure for job results.
Definition: qtrk_c_api.h:67
int height
Height of regions of interest to be handled. Typically equals width (square ROI). ...
Definition: qtrk_c_api.h:107
int startFrame
Index of the first frame for which results are available.
std::deque< FrameResult *> frameResults
Vector to hold the results for all frames.
uint frame
Frame number this ROI belongs to.
Definition: qtrk_c_api.h:56
int lostFrames
Number of frames deleted from memory before results were finished and/or saved.
vector3f pos
Final 3D position found. If no z localization was performed, the value of z will be 0...
Definition: qtrk_c_api.h:69
LocalizationJob job
Job metadata. See LocalizationJob.
Definition: qtrk_c_api.h:68
QTrkComputedConfig cfg
The settings used by this instance of QueuedTracker.
int capturedFrames
Number of frames captured. Counted through calls to StoreFrameInfo.
FrameCounters cnt
Local instance of FrameCounters to maintain counts accross functions.
vector3< float > vector3f
Definition: std_incl.h:114

§ ThreadLoop()

void ResultManager::ThreadLoop ( void *  param)
staticprotected

The base loop of the gathering and saving thread.

Called when initialization is done.

Parameters
[in]paramPointer to the instance of ResultManager to work for.

Definition at line 272 of file ResultManager.cpp.

273 {
274  ResultManager* rm = (ResultManager*)param;
275 
276  while(true) {
277  if (!rm->Update())
278  Threads::Sleep(20);
279 
280  if (rm->quit)
281  break;
282  }
283 }
Class that handles data gathering and saving from QueuedTracker instances.
Definition: ResultManager.h:91
Atomic< bool > quit
Flag to exit the threads.
bool Update()
General worker function called from ThreadLoop.
static void Sleep(int ms)
Definition: threads.h:134

§ Update()

bool ResultManager::Update ( )
protected

General worker function called from ThreadLoop.

Fetches results from QueuedTracker (FetchResults), sorts them (StoreResult) and saves them if needed (Write).

Definition at line 222 of file ResultManager.cpp.

223 {
224  trackerMutex.lock();
225 
226  if (!qtrk) {
228  return 0;
229  }
230 
231  const int NResultBuf = 40;
232  LocalizationResult resultbuf[NResultBuf];
233 
234  int count = qtrk->FetchResults( resultbuf, NResultBuf );
235 
236  resultMutex.lock();
237  for (int i=0;i<count;i++)
238  StoreResult(&resultbuf[i]);
239 
241 
243  Write();
244  }
245 
247 
248  int del = frameResults.size()-config.maxFramesInMemory;
249 
251  // write away any results that might be in there, unfinished localizations will be zero.
252  int lost = cnt.startFrame-cnt.processedFrames;
253  cnt.processedFrames += lost;
254  cnt.lostFrames += lost;
255 
256  Write();
257  }
258 
259  //dbgprintf("Removing %d frames from memory\n", del);
260 
261  for (int i=0;i<del;i++)
262  delete frameResults[i];
263  frameResults.erase(frameResults.begin(), frameResults.begin()+del);
264 
265  cnt.startFrame += del;
266  }
268 
269  return count>0;
270 }
int lastSaveFrame
Index of the last frame saved to the output file.
Threads::Mutex trackerMutex
Mutex to govern access to the linked QueuedTracker instance.
uint maxFramesInMemory
Number of frames for which to keep the data in memory. 0 for infinite.
Definition: ResultManager.h:80
int processedFrames
Number of frames processed by the ResultManager.
Threads::Mutex resultMutex
Mutex to govern access to frameResults.
void lock()
Definition: threads.h:74
ResultManagerConfig config
Local instance of ResultManagerConfig with the settings used by this instance of ResultManager.
QueuedTracker * qtrk
Pointer to the QueuedTracker instance to check.
Structure for job results.
Definition: qtrk_c_api.h:67
int startFrame
Index of the first frame for which results are available.
std::deque< FrameResult *> frameResults
Vector to hold the results for all frames.
int lostFrames
Number of frames deleted from memory before results were finished and/or saved.
FrameCounters cnt
Local instance of FrameCounters to maintain counts accross functions.
void StoreResult(LocalizationResult *r)
Copies results from QueuedTracker to internal data structures.
int writeInterval
Interval of number of gathered frames at which to write the data.
Definition: ResultManager.h:79
virtual int FetchResults(LocalizationResult *results, int maxResults)=0
Fetch available results.
void unlock()
Definition: threads.h:79
void Write()
Write available results to output files.

§ Write()

void ResultManager::Write ( )
protected

Write available results to output files.

Chooses between WriteBinaryResults and WriteTextResults depending on settings.

Definition at line 193 of file ResultManager.cpp.

194 {
195  resultMutex.lock();
196  if (config.binaryOutput)
198  else
200 
201  dbgprintf("Saved frame %d to %d\n", cnt.lastSaveFrame, cnt.processedFrames);
203 
205 }
int lastSaveFrame
Index of the last frame saved to the output file.
int processedFrames
Number of frames processed by the ResultManager.
Threads::Mutex resultMutex
Mutex to govern access to frameResults.
void lock()
Definition: threads.h:74
void WriteTextResults()
Write available data to a text file.
ResultManagerConfig config
Local instance of ResultManagerConfig with the settings used by this instance of ResultManager.
void WriteBinaryResults()
Write available data to a binary file.
FrameCounters cnt
Local instance of FrameCounters to maintain counts accross functions.
void dbgprintf(const char *fmt,...)
Definition: utils.cpp:149
uint8_t binaryOutput
Flag (boolean) to output a binary file instead of a text file.
Definition: ResultManager.h:81
void unlock()
Definition: threads.h:79

§ WriteBinaryFileHeader()

void ResultManager::WriteBinaryFileHeader ( )
protected

Writes metadata to the header of a binary data file.

The saved header for file version 3 is:

Name Description Size (bytes)
version Binary file version 4 (int)
numBead Number of beads in the file 4 (int)
numFrameInfoColumns Number of metadata columns 4 (int)
data_offset File offset to starting point of frame data 8 (long)
frameInfoNames The names/identifiers for the metadata columns variable (numFrameInfoColumns * strings)

Definition at line 67 of file ResultManager.cpp.

68 {
69  FILE* f = fopen(outputFile.c_str(), "wb");
70  if (!f) {
71  throw std::runtime_error( SPrintf("Unable to open file %s", outputFile.c_str()));
72  }
73 
74  // Write file header
75  int version = BINFILE_VERSION;
76  fwrite(&version, sizeof(int), 1, f);
77  fwrite(&config.numBeads, sizeof(int), 1, f);
78  fwrite(&config.numFrameInfoColumns, sizeof(int), 1, f);
79  long data_offset_pos = ftell(f);
80  long tmp=1234;
81  fwrite(&tmp, sizeof(long), 1,f);
82  for (int i=0;i<config.numFrameInfoColumns;i++) {
83  auto& n = frameInfoNames[i];
84  fwrite(n.c_str(), n.length()+1, 1, f);
85  }
86  long data_offset = ftell(f);
87  dbgprintf("frame data offset: %d\n", data_offset);
88  fseek(f, data_offset_pos, SEEK_SET);
89  fwrite(&data_offset, sizeof(long), 1, f);
90 
91  dbgprintf("writing %d beads and %d frame-info columns into file %s\n", config.numBeads, config.numFrameInfoColumns, outputFile.c_str());
92  fclose(f);
93 }
int numBeads
Number of beads for which to grab results. Should always equal the amount of beads in a single frame...
Definition: ResultManager.h:64
ResultManagerConfig config
Local instance of ResultManagerConfig with the settings used by this instance of ResultManager.
int numFrameInfoColumns
Number of columns in the frame info metadata file. Additional columns can be added to save more data ...
Definition: ResultManager.h:65
#define BINFILE_VERSION
void dbgprintf(const char *fmt,...)
Definition: utils.cpp:149
std::string outputFile
Path and filename of the main data output file.
std::vector< std::string > frameInfoNames
Vector with frame info column names. See WriteBinaryFileHeader.
std::string SPrintf(const char *fmt,...)
Definition: utils.cpp:132

§ WriteBinaryResults()

void ResultManager::WriteBinaryResults ( )
protected

Write available data to a binary file.

Warning
Does not update frame counters. Use Write and proper settings instead.

Definition at line 131 of file ResultManager.cpp.

132 {
133  if (outputFile.empty())
134  return;
135 
136  FILE* f = fopen(outputFile.c_str(), "ab");
137  if (!f) {
138  dbgprintf("ResultManager::WriteBinaryResult() Unable to open file %s\n", outputFile.c_str());
139  cnt.fileError ++;
140  return;
141  }
142 
143  for (int j=cnt.lastSaveFrame; j<cnt.processedFrames;j++)
144  {
145  auto fr = frameResults[j-cnt.startFrame];
146  if (f) {
147  fwrite(&j, sizeof(uint), 1, f);
148  fwrite(&fr->timestamp, sizeof(double), 1, f);
149  fwrite(&fr->frameInfo[0], sizeof(float), config.numFrameInfoColumns, f);
150  for (int i=0;i<config.numBeads;i++)
151  {
152  LocalizationResult *r = &fr->results[i];
153  fwrite(&r->pos, sizeof(vector3f), 1, f);
154  }
155  for (int i=0;i<config.numBeads;i++)
156  fwrite(&fr->results[i].error, sizeof(int), 1, f);
157  for (int i=0;i<config.numBeads;i++) {
158  fwrite(&fr->results[i].imageMean, sizeof(float), 1, f);
159  }
160  }
161  }
162  fclose(f);
163 }
int lastSaveFrame
Index of the last frame saved to the output file.
int fileError
Count of reported errors while handling output files. Currently only counts on error when opening bin...
int numBeads
Number of beads for which to grab results. Should always equal the amount of beads in a single frame...
Definition: ResultManager.h:64
int processedFrames
Number of frames processed by the ResultManager.
unsigned int uint
Definition: std_incl.h:127
ResultManagerConfig config
Local instance of ResultManagerConfig with the settings used by this instance of ResultManager.
Structure for job results.
Definition: qtrk_c_api.h:67
int startFrame
Index of the first frame for which results are available.
std::deque< FrameResult *> frameResults
Vector to hold the results for all frames.
int numFrameInfoColumns
Number of columns in the frame info metadata file. Additional columns can be added to save more data ...
Definition: ResultManager.h:65
vector3f pos
Final 3D position found. If no z localization was performed, the value of z will be 0...
Definition: qtrk_c_api.h:69
FrameCounters cnt
Local instance of FrameCounters to maintain counts accross functions.
void dbgprintf(const char *fmt,...)
Definition: utils.cpp:149
std::string outputFile
Path and filename of the main data output file.

§ WriteTextResults()

void ResultManager::WriteTextResults ( )
protected

Write available data to a text file.

Warning
Does not update frame counters. Use Write and proper settings instead.

Definition at line 165 of file ResultManager.cpp.

166 {
167  FILE* f = outputFile.empty () ? 0 : fopen(outputFile.c_str(), "a");
168  FILE* finfo = frameInfoFile.empty() ? 0 : fopen(frameInfoFile.c_str(), "a");
169 
170  for (int k=cnt.lastSaveFrame; k<cnt.processedFrames;k++)
171  {
172  auto fr = frameResults[k-cnt.startFrame];
173  if (f) {
174  fprintf(f,"%d\t%f\t", k, fr->timestamp);
175  for (int i=0;i<config.numBeads;i++)
176  {
177  LocalizationResult *r = &fr->results[i];
178  fprintf(f, "%.7f\t%.7f\t%.7f\t", r->pos.x,r->pos.y,r->pos.z);
179  }
180  fputs("\n", f);
181  }
182  if (finfo) {
183  fprintf(finfo,"%d\t%f\t", k, fr->timestamp);
184  for (int i=0;i<config.numFrameInfoColumns;i++)
185  fprintf(finfo, "%.7f\t", fr->frameInfo[i]);
186  fputs("\n", finfo);
187  }
188  }
189  if(finfo) fclose(finfo);
190  if(f) fclose(f);
191 }
int lastSaveFrame
Index of the last frame saved to the output file.
int numBeads
Number of beads for which to grab results. Should always equal the amount of beads in a single frame...
Definition: ResultManager.h:64
int processedFrames
Number of frames processed by the ResultManager.
ResultManagerConfig config
Local instance of ResultManagerConfig with the settings used by this instance of ResultManager.
Structure for job results.
Definition: qtrk_c_api.h:67
int startFrame
Index of the first frame for which results are available.
std::deque< FrameResult *> frameResults
Vector to hold the results for all frames.
int numFrameInfoColumns
Number of columns in the frame info metadata file. Additional columns can be added to save more data ...
Definition: ResultManager.h:65
vector3f pos
Final 3D position found. If no z localization was performed, the value of z will be 0...
Definition: qtrk_c_api.h:69
FrameCounters cnt
Local instance of FrameCounters to maintain counts accross functions.
std::string outputFile
Path and filename of the main data output file.
std::string frameInfoFile
Path and filename of the frame info (metadata) file.

Member Data Documentation

§ cnt

FrameCounters ResultManager::cnt
protected

Local instance of FrameCounters to maintain counts accross functions.

Definition at line 299 of file ResultManager.h.

§ config

ResultManagerConfig ResultManager::config
protected

Local instance of ResultManagerConfig with the settings used by this instance of ResultManager.

Definition at line 300 of file ResultManager.h.

§ frameInfoFile

std::string ResultManager::frameInfoFile
protected

Path and filename of the frame info (metadata) file.

Definition at line 307 of file ResultManager.h.

§ frameInfoNames

std::vector<std::string> ResultManager::frameInfoNames
protected

Vector with frame info column names. See WriteBinaryFileHeader.

Definition at line 296 of file ResultManager.h.

§ frameResults

std::deque< FrameResult* > ResultManager::frameResults
protected

Vector to hold the results for all frames.

Definition at line 298 of file ResultManager.h.

§ outputFile

std::string ResultManager::outputFile
protected

Path and filename of the main data output file.

Definition at line 306 of file ResultManager.h.

§ qtrk

QueuedTracker* ResultManager::qtrk
protected

Pointer to the QueuedTracker instance to check.

Definition at line 304 of file ResultManager.h.

§ quit

Atomic<bool> ResultManager::quit
protected

Flag to exit the threads.

Definition at line 309 of file ResultManager.h.

§ resultFile

ResultFile* ResultManager::resultFile
protected

Local instance of ResultFile to generalize output to text or binary files. Not used.

Definition at line 302 of file ResultManager.h.

§ resultMutex

Threads::Mutex ResultManager::resultMutex
protected

Mutex to govern access to frameResults.

Definition at line 293 of file ResultManager.h.

§ thread

Threads::Handle* ResultManager::thread
protected

Handle to the thread running the ThreadLoop.

Definition at line 308 of file ResultManager.h.

§ trackerMutex

Threads::Mutex ResultManager::trackerMutex
protected

Mutex to govern access to the linked QueuedTracker instance.

Definition at line 294 of file ResultManager.h.


The documentation for this class was generated from the following files: