Commit f043046c authored by VAN TOLL Wouter's avatar VAN TOLL Wouter

Writing CSVs and screenshots:

- HelperFunctions: Added functions for checking/creating directories, taken from CSVWriter.
- Cleaned up CSVWriter accordingly.
- OpenGLWidget: Screenshots and CSVs are now added to a folder that is automatically created.
parent 7bbda013
......@@ -24,6 +24,10 @@
#include <sstream>
#include <iomanip>
#if defined(_WIN32)
#include <direct.h>
#endif
namespace HelperFunctions
{
Timestamp GetCurrentTime()
......@@ -162,4 +166,34 @@ namespace HelperFunctions
output[1] = (int)((Gs + m) * 255);
output[2] = (int)((Bs + m) * 255);
}
bool DirectoryExists(const std::string &dirname)
{
struct stat info;
if (stat(dirname.c_str(), &info) != 0)
return false;
else if (info.st_mode & S_IFDIR)
return true;
else
return false;
}
bool CreateDirectoryIfNonExistent(const std::string& dirname)
{
// if the directory does not yet exist, try to create it
if (!DirectoryExists(dirname))
{
int nError = 0;
#if defined(_WIN32)
nError = _mkdir(dirname.c_str()); // can be used on Windows
#else
mode_t nMode = 0733;
nError = mkdir(dirname.c_str(), nMode); // can be used on non-Windows
#endif
return (nError == 0);
}
return true;
}
};
\ No newline at end of file
......@@ -56,6 +56,9 @@ namespace HelperFunctions
return val;
}
bool DirectoryExists(const std::string& dirname);
bool CreateDirectoryIfNonExistent(const std::string& dirname);
};
#endif //HELPER_FUNCTIONS_H
\ No newline at end of file
......@@ -29,30 +29,10 @@
*/
//========================================================================
#include "tools/csvwriter.h"
#include <tools/csvwriter.h>
#include <tools/HelperFunctions.h>
#include <fstream>
#if defined(_WIN32)
#include <direct.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <iostream>
#include <cstdint>
bool dirExists(const std::string &path)
{
struct stat info;
if (stat(path.c_str(), &info) != 0)
return false;
else if (info.st_mode & S_IFDIR)
return true;
else
return false;
}
bool CSVWriter::SetOutputDirectory(const std::string &dirname)
{
......@@ -62,24 +42,9 @@ bool CSVWriter::SetOutputDirectory(const std::string &dirname)
else
this->dirname_ = dirname + '/';
// if the directory does not yet exist, try to create it
if (!dirExists(dirname_))
{
int nError = 0;
#if defined(_WIN32)
nError = _mkdir(dirname_.c_str()); // can be used on Windows
#else
mode_t nMode = 0733;
nError = mkdir(dirname_.c_str(), nMode); // can be used on non-Windows
#endif
if (nError != 0)
{
dirname_ = std::string();
return false;
}
}
return true;
// try to create the directory, and return if the directory exists
HelperFunctions::CreateDirectoryIfNonExistent(this->dirname_);
return HelperFunctions::DirectoryExists(this->dirname_);
}
bool CSVWriter::Flush()
......
......@@ -32,12 +32,12 @@
#ifndef _CSV_WRITER_H
#define _CSV_WRITER_H
#include <tools/vector2D.h>
#include <string>
#include <vector>
#include <mutex>
#include <map>
#include <iostream>
#include "tools/vector2D.h"
/// <summary>A class for writing agent trajectories to CSV files in a specified folder.</summary>
class CSVWriter
......
......@@ -221,15 +221,6 @@ void UMANSOpenGLWidget::OpenScenarioFileDialog()
startNewSimulation(fileName.toStdString());
}
void UMANSOpenGLWidget::ToggleCSVOutput()
{
writeCSVOutput = !writeCSVOutput;
if (writeCSVOutput)
simulator->StartCSVOutput("../output/", true); // true = write output continuously
else
simulator->StopCSVOutput();
}
void UMANSOpenGLWidget::ZoomToFit()
{
// find the bbox and aspect ratio of the environment
......@@ -268,17 +259,45 @@ void UMANSOpenGLWidget::ZoomToFit()
update();
}
std::string getScenarioNameFromFullPath(const std::string& scenarioPath)
{
// remove the path path
auto lastSlash = scenarioPath.find_last_of("/");
std::string scenarioName = (lastSlash == std::string::npos ? scenarioPath : scenarioPath.substr(lastSlash + 1));
// remove the file extension
auto lastPoint = scenarioName.find_last_of(".");
scenarioName = (lastPoint == std::string::npos ? scenarioName : scenarioName.substr(0, lastPoint));
return scenarioName;
}
void UMANSOpenGLWidget::ToggleCSVOutput()
{
writeCSVOutput = !writeCSVOutput;
if (writeCSVOutput)
{
const auto& scenarioName = getScenarioNameFromFullPath(simulator->GetScenarioFilename());
simulator->StartCSVOutput("../output/" + scenarioName + "/", true); // true = write output continuously
}
else
simulator->StopCSVOutput();
}
void UMANSOpenGLWidget::MakeScreenshot()
{
const QRect rect(0, 0, width(), height());
QPixmap pixmap = grab(rect);
std::string scenarioPath = simulator->GetScenarioFilename();
auto lastSlash = scenarioPath.find_last_of("/");
std::string scenarioName = (lastSlash == std::string::npos ? scenarioPath : scenarioPath.substr(lastSlash + 1));
const std::string& scenarioName = getScenarioNameFromFullPath(simulator->GetScenarioFilename());
const std::string& dirName = "../screenshots/" + scenarioName + "/";
bool dirExists = HelperFunctions::CreateDirectoryIfNonExistent(dirName);
std::string filename = "../screenshots/" + scenarioName + "-" + HelperFunctions::ToStringWithLeadingZeros((int)(simulator->GetWorld()->GetCurrentTime()*10), 4) + ".png";
pixmap.save(filename.c_str());
if (dirExists)
{
std::string filename = dirName + HelperFunctions::ToStringWithLeadingZeros((int)(simulator->GetWorld()->GetCurrentTime() * 10), 4) + ".png";
pixmap.save(filename.c_str());
}
}
#pragma endregion
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment