Last active
July 22, 2019 14:14
-
-
Save chaidhat/d79bb9f1db0a370dfc67445dee2e256b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <windows.h> | |
#include <iostream> | |
#include <fstream> | |
#include <string> | |
#include <sstream> | |
#include <conio.h> /* getch() and kbhit() */ | |
using namespace std; | |
bool debug = false; | |
const string inConfig = "c:\\code\\cfg.txt"; | |
namespace Debug | |
{ | |
void WriteInColor(unsigned short color, string outputString) | |
{ | |
HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE); | |
SetConsoleTextAttribute(hcon, color); | |
cout << outputString; | |
} | |
void Log(string outputString) | |
{ | |
WriteInColor(8,outputString); | |
WriteInColor(7, "\r\n"); | |
} | |
void Caut(string outputString) | |
{ | |
WriteInColor(15,outputString); | |
WriteInColor(7, "\r\n"); | |
} | |
void Warn(string outputString) | |
{ | |
WriteInColor(12,outputString); | |
WriteInColor(7, "\r\n"); | |
} | |
} | |
struct Vector3 | |
{ | |
double x, y, z; | |
Vector3 multiply (double in) | |
{ | |
Vector3 out; | |
out.x = x * in; | |
out.y = y * in; | |
out.z = z * in; | |
return out; | |
} | |
double mag () | |
{ | |
Vector3 in; | |
in.x = x; | |
in.y = y; | |
in.z = z; | |
double out; | |
out = in.x * in.x + in.y * in.y; | |
out = sqrt(out); | |
out = sqrt(out * out + in.z * in.z); | |
return out; | |
} | |
Vector3 sub (Vector3 in) | |
{ | |
Vector3 out; | |
out.x = x - in.x; | |
out.y = y - in.y; | |
out.z = z - in.z; | |
return out; | |
} | |
}; | |
struct IO | |
{ | |
double value[150][715]; | |
string head[715]; | |
string text[128 * 90]; | |
void readCvs (const char *file, int maxFrame) | |
{ | |
maxFrame = min(maxFrame, 149); | |
ifstream myReadFile; | |
myReadFile.open(file); | |
int length = 100; | |
char output[100]; | |
long i = 0; | |
Debug::Caut("readCvs"); | |
int lastFrame = -1; | |
if (myReadFile.is_open()) { | |
bool breaker = false; | |
while (!myReadFile.eof() && !breaker) { | |
int frame = (int)floor((i - 715) / 714); | |
breaker = frame == maxFrame ? true : false; | |
myReadFile >> output; | |
if (i > 714 && !breaker) | |
{ | |
char val[8]; | |
strcpy(val, string(output).substr(0,5).c_str()); | |
value[frame][(i - 715) % 714] = strtod(val, NULL); | |
} | |
else if (i < 715) | |
{ | |
head[i] = string(output).substr(0,string(output).length() - 1); | |
} | |
i++; | |
if (lastFrame != frame) | |
{ | |
lastFrame = frame; | |
//Debug::Log("frame " + to_string(frame) + " readCvs"); | |
} | |
} | |
Debug::Caut("f readCvs"); | |
} | |
myReadFile.close(); | |
cout << head[10]; | |
return; | |
} | |
void readTxt(string filename) | |
{ | |
ifstream myReadFile; | |
myReadFile.open(filename); | |
char output[128 * 90]; | |
long i = 0; | |
if (myReadFile.is_open()) | |
{ | |
while (!myReadFile.eof()) | |
{ | |
myReadFile >> output; | |
text[i] = output; | |
i++; | |
} | |
} | |
myReadFile.close(); | |
return; | |
} | |
void writeTxt(string filename, string write[128 * 90]) | |
{ | |
ofstream myfile (filename); | |
if (myfile.is_open()) | |
{ | |
for (int i = 0; i < 128 * 90; i++) | |
{ | |
string c = write[i]; | |
c = c.append("\n"); | |
myfile << c.c_str(); | |
} | |
myfile.close(); | |
} | |
else | |
{ | |
Debug::Warn("unable to open file"); | |
return; | |
} | |
} | |
}; | |
double find(string s, string head[715], double val[150][715], int frame) | |
{ | |
int i = 0; | |
while (s != head[i]) | |
{ | |
i++; | |
} | |
return val[frame - 1][(i + 1)]; // | |
} | |
IO *reader = new IO; | |
int errorRate = 0; | |
double errorTotal = 0; | |
int successRate = 0; | |
int last = 0; | |
void read(string argv, int frame, IO *r) | |
{ | |
string inCvs = argv; | |
//cout << find("x_1",reader->head,reader->value) << "\n"; | |
Vector3 p[52]; // we ignore 1-16 (0-15) | |
for (int i = 0; i < 52; i++) | |
{ | |
string hx = "X_"; | |
string hy = "Y_"; | |
string hz = "Z_"; | |
char ci = i; | |
hx = hx + to_string(i + 16); | |
hy = hy + to_string(i + 16); | |
hz = hz + to_string(i + 16); | |
p[i].x = find(hx,reader->head,reader->value, frame); | |
p[i].y = find(hy,reader->head,reader->value, frame); | |
p[i].z = find(hz,reader->head,reader->value, frame); | |
//cout << frame << ", " << hx << ", " << p[i].x << "\n"; | |
//Debug::Caut("apa"); | |
} | |
for (int i = 0; i < 52; i++) | |
{ | |
if (i != 48) | |
{ | |
p[i] = p[i].sub(p[48]); | |
//cout << abs(pow(abs(p[i].mag() - scaling), 2)) << "\n"; | |
//p[i].x = (scaling - p[i].mag()) / scaling ; | |
//j += abs(p[i].x); | |
} | |
} | |
double error = 0; | |
double lasterr = 1000000; | |
double lastscale = 0; | |
double c = 0; | |
int w = 0; | |
for (double scale = 0.75; scale < 2; scale += 0.05) | |
{ | |
error = 0; | |
for (int i = 0; i < 52; i++) | |
{ | |
if (i != 48) | |
{ | |
Vector3 pa = p[i].multiply(scale); | |
double scalingx = strtod(r->text[i + 3].c_str(),NULL); | |
double scalingy = strtod(r->text[i + 3 + 52].c_str(),NULL); | |
error += pow(abs(pa.mag() - scalingx), 2) + pow(abs(pa.y - scalingy), 2); | |
c += p[i].mag(); | |
w++; | |
//cout << abs(pow(abs(p[i].mag() - scaling), 2)) << "\n"; | |
//p[i].x = (scaling - p[i].mag()) / scaling ; | |
//j += abs(p[i].x); | |
} | |
} | |
//Debug::Log(to_string(error) + to_string(scale)); | |
if (error < lasterr) | |
{ | |
lasterr = error; | |
lastscale = scale; | |
} | |
} | |
error = lasterr; | |
error /= 51; | |
if (c > 0) | |
{ | |
if (error < 30) | |
{ | |
errorTotal += error; | |
successRate++; | |
Debug::Log(to_string(lastscale)); | |
Debug::Caut("frame:" + to_string(frame) + " error:" + to_string(error)); | |
} | |
else if (error > 1500) | |
{ | |
Debug::Warn("frame:" + to_string(frame) + " error:" + to_string(error)); | |
} | |
else | |
{ | |
errorTotal += error; | |
errorRate++; | |
Debug::Log(to_string(lastscale)); | |
Debug::Log("frame:" + to_string(frame) + " error:" + to_string(error)); | |
} | |
} | |
} | |
int main (int argc, char *argv[]) | |
{ | |
Debug::WriteInColor(7, "\r\n"); | |
Debug::Log("Hello!"); | |
Debug::Caut("Hello!"); | |
Debug::Warn("Hello!"); | |
if (argc == 1) | |
{ | |
cout << "give me a flag!"; | |
return 1; | |
} | |
else | |
{ | |
string flag = string(argv[1]); | |
reader->readTxt(inConfig); | |
if (flag == "-l") | |
{ | |
string inCvs = argv[2]; | |
reader->readCvs(inCvs.c_str(), 1); | |
Vector3 p[52]; | |
for (int i = 0; i < 52; i++) | |
{ | |
string hx = "X_"; | |
string hy = "Y_"; | |
string hz = "Z_"; | |
hx.append(to_string(i + 16)); | |
hy.append(to_string(i + 16)); | |
hz.append(to_string(i + 16)); | |
p[i].x = find(hx,reader->head,reader->value, 1); | |
p[i].y = find(hy,reader->head,reader->value, 1); | |
p[i].z = find(hz,reader->head,reader->value, 1); | |
} | |
for (int i = 0; i < 52; i++) | |
{ | |
if (i != 48) | |
{ | |
p[i] = p[i].sub(p[48]); | |
reader->text[i + 3] = to_string(p[i].mag()); | |
reader->text[i + 3 + 52] = to_string(p[i].y); | |
} | |
cout << p[i].x << ", "; | |
} | |
reader->writeTxt(inConfig,reader->text); | |
} | |
else if (flag == "-c") | |
{ | |
reader->readTxt(inConfig); | |
reader->readCvs(argv[2], 149); | |
Debug::Caut("Read"); | |
for (int i = 1; i < 148; i++) | |
{ | |
read(string(argv[2]), i, reader); | |
} | |
float fn = ((double)successRate/(double)(errorRate+successRate)) * 100; | |
float av = (errorTotal/(double)(errorRate+successRate)); | |
Debug::Warn(to_string(fn) + "% result,"); | |
Debug::Warn(to_string(av) + " avg,"); | |
Debug::Warn(argv[2]); | |
fn -= 50; | |
fn += 50 - av; | |
double confidence = 1 / (1 + pow(2.7182818284,fn * -0.167)); | |
confidence *= 100; | |
if (confidence > 50) | |
{ | |
Debug::WriteInColor(10, to_string(confidence) + "% confidence\n"); | |
} | |
else | |
{ | |
Debug::WriteInColor(14, to_string(confidence) + "% confidence\n"); | |
} | |
} | |
else if (flag == "-face_dir") | |
{ | |
reader->readTxt(inConfig); | |
reader->text[1] = argv[2]; | |
reader->writeTxt(inConfig,reader->text); | |
cout << "fin\n"; | |
} | |
else if (flag == "-proc_dir") | |
{ | |
reader->readTxt(inConfig); | |
reader->text[0] = argv[2]; | |
reader->writeTxt(inConfig,reader->text); | |
cout << "fin\n"; | |
} | |
else if (flag == "-r") | |
{ | |
reader->readTxt(inConfig); | |
string cmd = reader->text[1]; | |
cmd = cmd.append(" -f "); | |
cmd = cmd.append(argv[2]); | |
system(cmd.c_str()); | |
system("cls"); | |
cmd = reader->text[0].append(argv[2]); | |
} | |
else | |
{ | |
Debug::Warn("No flag in"); | |
} | |
} | |
Debug::WriteInColor(7, "\r\n"); | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment