mirror of
https://github.com/storytold/LiveScan3D.git
synced 2026-10-09 00:09:58 +00:00
109 lines
2.5 KiB
C++
109 lines
2.5 KiB
C++
#include "framesFileWriterReader.h"
|
|
|
|
#include <ctime>
|
|
|
|
FramesFileWriterReader::FramesFileWriterReader()
|
|
{
|
|
|
|
}
|
|
|
|
void FramesFileWriterReader::closeFileIfOpened()
|
|
{
|
|
if (m_pFileHandle == nullptr)
|
|
return;
|
|
|
|
fclose(m_pFileHandle);
|
|
m_pFileHandle = nullptr;
|
|
m_bFileOpenedForReading = false;
|
|
m_bFileOpenedForWriting = false;
|
|
}
|
|
|
|
void FramesFileWriterReader::resetTimer()
|
|
{
|
|
recording_start_time = std::chrono::steady_clock::now();
|
|
}
|
|
|
|
int FramesFileWriterReader::getRecordingTimeMilliseconds()
|
|
{
|
|
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
|
|
return static_cast<int>(std::chrono::duration_cast<std::chrono::milliseconds >(end - recording_start_time).count());
|
|
}
|
|
|
|
void FramesFileWriterReader::openCurrentFileForReading()
|
|
{
|
|
closeFileIfOpened();
|
|
|
|
m_pFileHandle = fopen(m_sFilename.c_str(), "rb");
|
|
|
|
m_bFileOpenedForReading = true;
|
|
m_bFileOpenedForWriting = false;
|
|
}
|
|
|
|
void FramesFileWriterReader::openNewFileForWriting()
|
|
{
|
|
closeFileIfOpened();
|
|
|
|
char filename[1024];
|
|
time_t t = time(0);
|
|
struct tm * now = localtime(&t);
|
|
sprintf(filename, "recording_%04d_%02d_%02d_%02d_%02d.bin", now->tm_year, now->tm_mon, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec);
|
|
m_sFilename = filename;
|
|
m_pFileHandle = fopen(filename, "wb");
|
|
|
|
m_bFileOpenedForReading = false;
|
|
m_bFileOpenedForWriting = true;
|
|
|
|
resetTimer();
|
|
}
|
|
|
|
bool FramesFileWriterReader::readFrame(std::vector<Point3s> &outPoints, std::vector<RGB> &outColors)
|
|
{
|
|
if (!m_bFileOpenedForReading)
|
|
openCurrentFileForReading();
|
|
|
|
outPoints.clear();
|
|
outColors.clear();
|
|
FILE *f = m_pFileHandle;
|
|
int nPoints, timestamp;
|
|
char tmp[1024];
|
|
int nread = fscanf_s(f, "%s %d %s %d", tmp, 1024, &nPoints, tmp, 1024, ×tamp);
|
|
|
|
if (nread < 4)
|
|
return false;
|
|
|
|
if (nPoints == 0)
|
|
return true;
|
|
|
|
fgetc(f); // '\n'
|
|
outPoints.resize(nPoints);
|
|
outColors.resize(nPoints);
|
|
|
|
fread((void*)outPoints.data(), sizeof(outPoints[0]), nPoints, f);
|
|
fread((void*)outColors.data(), sizeof(outColors[0]), nPoints, f);
|
|
fgetc(f); // '\n'
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
void FramesFileWriterReader::writeFrame(std::vector<Point3s> points, std::vector<RGB> colors)
|
|
{
|
|
if (!m_bFileOpenedForWriting)
|
|
openNewFileForWriting();
|
|
|
|
FILE *f = m_pFileHandle;
|
|
|
|
int nPoints = static_cast<int>(points.size());
|
|
fprintf(f, "n_points= %d\nframe_timestamp= %d\n", nPoints, getRecordingTimeMilliseconds());
|
|
if (nPoints > 0)
|
|
{
|
|
fwrite((void*)points.data(), sizeof(points[0]), nPoints, f);
|
|
fwrite((void*)colors.data(), sizeof(colors[0]), nPoints, f);
|
|
}
|
|
fprintf(f, "\n");
|
|
}
|
|
|
|
FramesFileWriterReader::~FramesFileWriterReader()
|
|
{
|
|
closeFileIfOpened();
|
|
} |