Frames are now saved to the file, added FramesFileWriterReader class.

This commit is contained in:
JacekNaruniec
2017-01-14 18:48:27 +01:00
parent e068058868
commit e453d927d3
8 changed files with 202 additions and 14 deletions
@@ -0,0 +1,109 @@
#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, &timestamp);
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();
}
+38 -13
View File
@@ -53,6 +53,7 @@ LiveScanClient::LiveScanClient() :
m_bFilter(false),
m_bStreamOnlyBodies(false),
m_bCaptureFrame(false),
m_bCaptureToFile(true),
m_bConnected(false),
m_bConfirmCaptured(false),
m_bConfirmCalibrated(false),
@@ -183,6 +184,8 @@ int LiveScanClient::Run(HINSTANCE hInstance, int nCmdShow)
return static_cast<int>(msg.wParam);
}
void LiveScanClient::UpdateFrame()
{
if (!pCapture->bInitialized)
@@ -203,10 +206,19 @@ void LiveScanClient::UpdateFrame()
if (m_bCaptureFrame)
{
m_vGatheredVertices.push_back(m_vLastFrameVertices);
m_vGatheredRGBPoints.push_back(m_vLastFrameRGB);
m_bConfirmCaptured = true;
m_bCaptureFrame = false;
if (m_bCaptureToFile)
{
m_framesFileWriterReader.writeFrame(m_vLastFrameVertices, m_vLastFrameRGB);
m_bConfirmCaptured = true;
m_bCaptureFrame = false;
}
else
{
m_vGatheredVertices.push_back(m_vLastFrameVertices);
m_vGatheredRGBPoints.push_back(m_vLastFrameRGB);
m_bConfirmCaptured = true;
m_bCaptureFrame = false;
}
}
}
@@ -515,18 +527,32 @@ void LiveScanClient::HandleSocket()
{
byteToSend = MSG_STORED_FRAME;
m_pClientSocket->SendBytes(&byteToSend, 1);
if (m_vGatheredRGBPoints.size() > 0)
if (m_bCaptureToFile)
{
SendFrame(m_vGatheredVertices[0], m_vGatheredRGBPoints[0], m_vLastFrameBody);
m_vGatheredRGBPoints.erase(m_vGatheredRGBPoints.begin(), m_vGatheredRGBPoints.begin() + 1);
m_vGatheredVertices.erase(m_vGatheredVertices.begin(), m_vGatheredVertices.begin() + 1);
vector<Point3s> points;
vector<RGB> colors;
bool res = m_framesFileWriterReader.readFrame(points, colors);
if (res == false)
{
int size = -1;
m_pClientSocket->SendBytes((char*)&size, 4);
} else
SendFrame(points, colors, m_vLastFrameBody);
}
else
{
int size = -1;
m_pClientSocket->SendBytes((char*)&size, 4);
if (m_vGatheredRGBPoints.size() > 0)
{
SendFrame(m_vGatheredVertices[0], m_vGatheredRGBPoints[0], m_vLastFrameBody);
m_vGatheredRGBPoints.erase(m_vGatheredRGBPoints.begin(), m_vGatheredRGBPoints.begin() + 1);
m_vGatheredVertices.erase(m_vGatheredVertices.begin(), m_vGatheredVertices.begin() + 1);
}
else
{
int size = -1;
m_pClientSocket->SendBytes((char*)&size, 4);
}
}
}
//send last frame
@@ -692,7 +718,6 @@ void LiveScanClient::SendFrame(vector<Point3s> vertices, vector<RGB> RGB, vector
if (m_bFrameCompression)
{
char buf[8];
// *2, because according to zstd documentation, increasing the size of the output buffer above a
// bound should speed up the compression.
int cBuffSize = ZSTD_compressBound(size) * 2;