Created
August 3, 2010 22:41
-
-
Save larsfu/507292 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <boost/filesystem.hpp> | |
#include <boost/algorithm/string.hpp> | |
#include <iostream> | |
#include <fstream> | |
#include <string> | |
#include <vector> | |
#include <map> | |
#include <stdlib.h> | |
#include <math.h> | |
#include "CImg.h" | |
#include <time.h> | |
using namespace std; | |
using namespace cimg_library; | |
//Variables | |
boost::filesystem::path logpath ("logs/"); | |
boost::filesystem::path output ("output2/"); | |
string Map; | |
int lastset; | |
int shrinkfactor; | |
ifstream logfile; | |
string logfile_path; | |
string logstr; | |
int length; | |
int color_start[3] = {0, 255, 0}; | |
int color_end[3] = {255, 0, 0}; | |
vector<string> deaths; | |
vector<int> xarray; | |
vector<int> yarray; | |
int xmax; | |
int ymax; | |
map<int, map<int, double> > blood; | |
//Choose a color from the gradient. | |
//int value is the given per mil value | |
int colorFromGradient(int value) | |
{ | |
int diff_r = color_end[0] - color_start[0]; | |
int diff_g = color_end[1] - color_start[1]; | |
int diff_b = color_end[2] - color_start[2]; | |
double factor = round(value) / 1000; | |
int r = round(color_start[0] + diff_r * factor); | |
int g = round(color_start[1] + diff_g * factor); | |
int b = round(color_start[2] + diff_b * factor); | |
int endcol[3] = {r, g, b}; | |
return *endcol; | |
} | |
void visualize(string Map) | |
{ | |
/* | |
if(lastset != 1) | |
{ | |
cout << "PNG shrink factor (1/x) > "; | |
cin >> shrinkfactor; | |
} | |
else | |
{ | |
//TODO: Load shrinkfactor from settings file | |
} | |
*/ | |
//Dev -> | |
shrinkfactor = 3; | |
//Parse logfile | |
cout << "Parsing logfile..."; | |
logfile_path = "logs/"+Map+".log"; | |
logfile.open(logfile_path.c_str(), ios::in); | |
getline(logfile, logstr); | |
boost::split(deaths, logstr, boost::is_any_of(",")); | |
//Find the maximum values | |
for (vector<string>::iterator death = deaths.begin(); death != deaths.end(); death++) | |
{ | |
vector<string> split; | |
boost::split(split, *death, boost::is_any_of(" ")); | |
xarray.push_back(atoi(split[0].c_str())); | |
yarray.push_back(atoi(split[1].c_str())); | |
} | |
sort(xarray.begin(), xarray.end()); | |
sort(yarray.begin(), yarray.end()); | |
reverse(xarray.begin(), xarray.end()); | |
reverse(yarray.begin(), yarray.end()); | |
xmax = xarray[0]; | |
ymax = yarray[0]; | |
cout << " done." << endl; | |
//Render heatmap | |
cout << "Rendering heatmap..." << endl; | |
CImg<double> heatmap(xmax, ymax); | |
vector<string> split; | |
for(int x=0; x <= xmax; x++) | |
{ | |
cout << x << endl; | |
for(int y=0; y <= ymax; y++) | |
{ | |
for (vector<string>::iterator death = deaths.begin(); death != deaths.end(); death++) | |
{ | |
int i = 1; | |
char * pch; | |
char buffer[10]; | |
sprintf(buffer, "%s", *death); | |
pch = strtok(buffer, " "); | |
while (pch != NULL) | |
{ | |
//if(i = 1) | |
//else | |
pch = strtok (NULL, " "); | |
} | |
/*double diffx = x - atoi(split[0].c_str()); | |
double diffy = y - atoi(split[1].c_str()); | |
if(abs(diffx) < 50 && abs(diffy) < 50) | |
{ | |
double space = pow((pow(diffx, 2) + pow(diffy, 2)), 0.5); | |
if(space != 0) | |
{ | |
blood[x][y] += 1/space; | |
} | |
}*/ | |
} | |
} | |
} | |
} | |
int main(int argc, char *argv[]) | |
{ | |
cout << "This script will visualize the logged deaths.\n"; | |
//cout << "Map to parse > "; | |
//cin >> map; | |
//For dev | |
Map = "jan"; | |
if(Map == "ALL") | |
{ | |
cout << "Use settings from last run? (1/0) > "; | |
cin >> lastset; | |
// get all files ending in .log | |
vector<string> filenames; | |
boost::filesystem::directory_iterator end_it; | |
// loop through each file in the directory | |
for(boost::filesystem::directory_iterator it(logpath); it != end_it; ++it) { | |
if( !is_directory(it->status()) && extension(it->path()) == ".log" ) { | |
cout << "Parsing map: " << basename(it->path()) << "\n"; | |
visualize(basename(it->path())); | |
} | |
} | |
} | |
else | |
{ | |
lastset = 0; | |
visualize(Map); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment