Commit f8aa9ac7 authored by Patric Schmitz's avatar Patric Schmitz

Merge branch 'visualizeData'

parents ae6002ab 6a2440b5
......@@ -306,4 +306,6 @@ paket-files/
#Blender
*.blend1
*.blend2
*.blend3
\ No newline at end of file
*.blend3
StudyDataPostProcessing/build-*
......@@ -37,7 +37,7 @@
<ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">
......
......@@ -32,24 +32,25 @@ namespace {
const string lineSeparator = "\n";
}
FILE * saveDataFile;
//=== === === === === === === === === === === === === === ===
//Public interface:
void DataCaptureManager::Start(Application* app)
{
if (!capturing)
{
char buffer[64];
std::time_t now = std::time(NULL);
tm * ptm = localtime(&now);
strftime(buffer, sizeof(buffer), "%Y%b%d_%H-%M-%S", ptm);
string filenameSuffix = {buffer};
capturing = true;
string filenameSuffix = currentDateAndTimeAsString();
std::string fileRootDirString =
std::string filePath =
app->GetNativeToJava()->GetExternalStorageFolder(app->GetCurrentEnv()) +
recordDirectoryName + recordFilenamePrefix + filenameSuffix;
saveDataFile = fopen(fileRootDirString.c_str(), "w");
capturing = true;
saveDataFile = fopen(filePath.c_str(), "w");
appendHeadLineToFile();
}
}
......@@ -195,7 +196,20 @@ void DataCaptureManager::AddUserKinematicData(
}
}
//Convert data to strings:
//=== === === === === === === === === === === === === === ===
//Get properly formatted strings:
string DataCaptureManager::currentDateAndTimeAsString() {
char buffer[64];
std::time_t now = std::time(NULL);
tm * ptm = localtime(&now);
strftime(buffer, sizeof(buffer), "%Y%b%d_%H-%M-%S", ptm);
return {buffer};
}
string DataCaptureManager::getMatlabString(glm::mat4 v) {
std::stringstream stringStream;
stringStream << "[ "
......@@ -230,4 +244,4 @@ const char* DataCaptureManager::LineTypes::kinematic = "KINEMATIC";
const char* DataCaptureManager::LineTypes::reset = "RESET";
const char* DataCaptureManager::LineTypes::user_reset = "USER_RESET";
const char* DataCaptureManager::LineTypes::tracking_loss = "TRACKING_LOSS";
const char* DataCaptureManager::LineTypes::target = "TARGET_COLLECTED";
\ No newline at end of file
const char* DataCaptureManager::LineTypes::target = "TARGET_COLLECTED";
......@@ -58,11 +58,18 @@ private:
void appendKinematic(UserKinematic& kinematic, std::string& to);
void appendHeadLineToFile();
string currentDateAndTimeAsString();
string getMatlabString(glm::mat4 v);
string getMatlabString(glm::vec3 v);
string getMatlabString(float v);
bool capturing = false;
FILE * saveDataFile;
};
......
QT += core
QT -= gui
CONFIG += c++11
TARGET = RecordFilePP
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp \
datareader.cpp
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
HEADERS += \
datareader.h
#include "datareader.h"
#include <sstream>
#include <vector>
#include <iostream>
#include <iomanip>
#include <glm/ext.hpp>
DataReader::DataReader()
{
}
glm::mat4 DataReader::ReadMat4(std::string strMatrix) {
glm::mat4 mat;
// remove [ ] brackets
strMatrix.erase(0, 1);
strMatrix.erase(strMatrix.length() - 1, 1);
std::istringstream ssMatrix(strMatrix);
std::string strRow;
for(int row = 0 ; row < 4 ; ++row) {
std::getline(ssMatrix, strRow, ';');
strRow.erase(0, 1);
std::string strEntry;
std::istringstream ssRow(strRow);
for(int col = 0 ; col < 4 ; ++col) {
ssRow >> std::setprecision(std::numeric_limits<double>::digits10 + 1)
>> mat[col][row];
}
}
return mat;
}
// glm::vec3 DataReader::ReadVec3(std::string str) {
// return glm::vec3(0.0f, 0.0f, 0.0f);
// }
// float DataReader::ReadFloat(std::string str) {
// return 0.0f;
// }
#ifndef DATAREADER_H
#define DATAREADER_H
#include <glm/glm.hpp>
#include <string>
class DataReader
{
public:
DataReader();
glm::mat4 ReadMat4(std::string str);
// glm::vec3 ReadVec3(std::string str);
// float ReadFloat(std::string str);
};
#endif // DATAREADER_H
#include <sstream>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <glm/ext.hpp>
#include <glm/gtx/matrix_decompose.hpp>
#include "datareader.h"
#define DEBUG_OUTPUT 0
glm::mat4 realHeadTransform;
glm::mat4 realFrontTransform;
glm::mat4 realBackTransform;
glm::mat4 realTorsoTransform;
glm::mat4 realHeadTransformLast;
glm::mat4 realFrontTransformLast;
glm::mat4 realBackTransformLast;
glm::vec3 offsetTorsoFrontToCenter;
glm::vec3 offsetTorsoBackToCenter;
glm::mat4 correctBodyTransform(glm::mat4 transform);
glm::mat4 cleanMatrix(glm::mat4 matrix);
void computeTorsoTransform();
std::string getMatlabString(glm::mat4 v);
int main()
{
std::ifstream file( "testfile" );
std::ofstream fileOut( "testfile.out" );
if(file) {
DataReader reader;
std::string line;
while(std::getline(file, line)) {
std::istringstream ssLine(line);
std::string token;
std::getline(ssLine, token, ',');
std::getline(ssLine, token, ',');
std::getline(ssLine, token, ',');
std::getline(ssLine, token, ',');
std::getline(ssLine, token, ',');
realHeadTransform = reader.ReadMat4(token);
if(realHeadTransform[3] == realHeadTransformLast[3])
continue;
std::getline(ssLine, token, ',');
std::getline(ssLine, token, ',');
std::getline(ssLine, token, ',');
realFrontTransform = reader.ReadMat4(token);
realFrontTransform = correctBodyTransform(realFrontTransform);
std::getline(ssLine, token, ',');
realBackTransform = reader.ReadMat4(token);
realBackTransform = correctBodyTransform(realBackTransform);
#if DEBUG_OUTPUT
std::cout << "+++++++++++++++++++++++++++++++" << std::endl;
std::cout << "realHeadTransform" << std::endl;
std::cout << glm::to_string(realHeadTransform) << std::endl;
std::cout << "realFrontTransform" << std::endl;
std::cout << glm::to_string(realFrontTransform) << std::endl;
std::cout << "realBackTransform" << std::endl;
std::cout << glm::to_string(realBackTransform) << std::endl;
#endif
computeTorsoTransform();
#if DEBUG_OUTPUT
std::cout << "realTorsoTransform" << std::endl;
std::cout << glm::to_string(realTorsoTransform) << std::endl;
#endif
realHeadTransformLast = realHeadTransform;
realFrontTransformLast = realFrontTransform;
realBackTransformLast = realBackTransform;
std::string lineOut =
getMatlabString(realHeadTransform) + "," +
getMatlabString(realFrontTransform) + "," +
getMatlabString(realBackTransform) + "," +
getMatlabString(realTorsoTransform) + ",";
fileOut << lineOut << std::endl;
}
file.close();
} else {
std::cout << "Record file not found." << std::endl;
}
return 0;
}
std::string getMatlabString(glm::mat4 v) {
std::stringstream stringStream;
stringStream << std::setprecision(std::numeric_limits<double>::digits10 + 1)
<< "[ "
<< v[0][0] << " " << v[1][0] << " " << v[2][0] << " " << v[3][0] << "; "
<< v[0][1] << " " << v[1][1] << " " << v[2][1] << " " << v[3][1] << "; "
<< v[0][2] << " " << v[1][2] << " " << v[2][2] << " " << v[3][2] << "; "
<< v[0][3] << " " << v[1][3] << " " << v[2][3] << " " << v[3][3] << "]";
return stringStream.str();
}
void computeTorsoTransform() {
glm::vec3 realTorsoPosition;
glm::mat4 realTorsoOrientation;
bool hasBodyTorsoFront = false;
bool hasBodyTorsoBack = false;
if(realFrontTransform != realFrontTransformLast)
hasBodyTorsoFront = true;
if(realBackTransform != realBackTransformLast)
hasBodyTorsoBack = true;
// instead skip first line in main loop?
static bool firstFrame = true;
if(firstFrame) {
hasBodyTorsoFront = false;
hasBodyTorsoBack = false;
firstFrame = false;
}
if (!hasBodyTorsoFront && hasBodyTorsoBack) {
realTorsoPosition =
realBackTransform *
glm::vec4(offsetTorsoBackToCenter, 1.);
realTorsoOrientation = glm::mat3(realBackTransform);
}
else if (!hasBodyTorsoBack && hasBodyTorsoFront) {
realTorsoPosition =
realFrontTransform *
glm::vec4(offsetTorsoFrontToCenter, 1.);
realTorsoOrientation = glm::mat3(realFrontTransform);
}
if (hasBodyTorsoFront && hasBodyTorsoBack) {
realTorsoPosition = 0.5f *
(realFrontTransform[3] + realBackTransform[3]);
glm::quat torsoFrontOrientationQuat =
glm::quat_cast(realFrontTransform);
glm::quat torsoBackOrientationQuat =
glm::quat_cast(realBackTransform);
if(glm::dot(torsoFrontOrientationQuat,
torsoBackOrientationQuat) < 0.f)
torsoFrontOrientationQuat = -torsoFrontOrientationQuat;
glm::quat realTorsoOrientationQuat = glm::mix(
torsoFrontOrientationQuat, torsoBackOrientationQuat, 0.5f);
realTorsoOrientation = glm::mat4_cast(realTorsoOrientationQuat);
}
if (hasBodyTorsoFront) {
offsetTorsoFrontToCenter =
glm::transpose(glm::mat3(realFrontTransform)) *
(realTorsoPosition - glm::vec3(realFrontTransform[3]));
}
if (hasBodyTorsoBack) {
offsetTorsoBackToCenter =
glm::transpose(glm::mat3(realBackTransform)) *
(realTorsoPosition - glm::vec3(realBackTransform[3]));
}
#if DEBUG_OUTPUT
std::cout << std::boolalpha
<< "hasBodyTorsoFront: " << hasBodyTorsoFront << std::endl;
std::cout << std::boolalpha
<< "hasBodyTorsoBack: " << hasBodyTorsoBack << std::endl;
std::cout << "offsetTorsoFrontToCenter: "
<< glm::to_string(offsetTorsoFrontToCenter) << std::endl;
std::cout << "offsetTorsoBackToCenter: "
<< glm::to_string(offsetTorsoBackToCenter) << std::endl;
std::cout << "realTorsoPosition: "
<< glm::to_string(realTorsoPosition) << std::endl;
#endif
realTorsoTransform =
glm::translate(glm::mat4(1.0f), realTorsoPosition) *
realTorsoOrientation;
for(auto & element : realTorsoTransform)
assert(glm::isnan(element) == false);
}
glm::mat4 correctBodyTransform(glm::mat4 transform) {
for(int entry = 0 ; entry < 3 ; ++entry) {
transform[3][entry] /= 1000.0f;
}
transform[3] += glm::vec4(4, 0, -4, 0);
return transform;
}
glm::mat4 cleanMatrix(glm::mat4 matrix) {
glm::vec3 scale;
glm::quat rotation;
glm::vec3 translation;
glm::vec3 skew;
glm::vec4 perspective;
glm::decompose(matrix,
scale, rotation, translation, skew, perspective);
glm::mat4 result = glm::toMat4(rotation);
result[3] = glm::vec4(translation, 1);
for(int col = 0 ; col < 3 ; ++col) {
result[col] = glm::normalize(result[col]);
}
return result;
}
This diff is collapsed.
This diff is collapsed.
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