From c5bcec500017b57d0d51d3b08615921f88cc899b Mon Sep 17 00:00:00 2001 From: Joshua Moerman Date: Fri, 24 Jun 2011 19:31:02 +0200 Subject: [PATCH] fix in stfu and some newline shit, png-cnavas to header only --- canvae/PNG.cpp | 134 -------------------------------------------- canvae/PNG.hpp | 129 ++++++++++++++++++++++++++++++++++++++++-- kernels/Ikeda3D.hpp | 8 +-- stfu/stf.hpp | 4 +- 4 files changed, 132 insertions(+), 143 deletions(-) delete mode 100644 canvae/PNG.cpp diff --git a/canvae/PNG.cpp b/canvae/PNG.cpp deleted file mode 100644 index e4a372d..0000000 --- a/canvae/PNG.cpp +++ /dev/null @@ -1,134 +0,0 @@ -#include "../Logger.hpp" -#include -#include -#include -#include - -#include "../pngwriter/pngwriter.h" - -#include "PNG.hpp" - - -PNG::PNG(unsigned int width, unsigned int height, unsigned int num_colors): - Canvas(2), width(width), height(height), num_colors(num_colors), v(0) { - - int_array = new unsigned int[width*height*num_colors]; - - assert(int_array != NULL); - - clear(); - - LogDebug("New Canvas\n"); -} - -void PNG::clear() { - for(unsigned int i = 0; i < width*height*num_colors; i++) { - int_array[i] = 0; - } -} - -void PNG::plot(const double* position) { - const double& x = position[0]; - const double& y = position[1]; - - const unsigned int x_int = x*width + width*.5; - const unsigned int y_int = y*width + height*.5; - const unsigned int index = x_int + width * y_int; - - if(x_int < width && y_int < height) { - int_array[index]++; - } -} - -/* - I/O functions -*/ - -void PNG::output_file(const char* filename_in) const { - std::string filename = filename_in; - filename += ".png"; - unsigned int* max_int = new unsigned int[num_colors]; - double* power = new double[num_colors]; - - for(unsigned int i = 0; i < num_colors; i++) { - max_int[i] = 0; - double cumulative = 0; - unsigned int n = 0; - - for(unsigned int j = 0; j < width*height; j++) { - if(max_int[i] < int_array[j+i*width*height]) { - max_int[i] = int_array[j+i*width*height]; - } - if(int_array[j+i*width*height]) { - cumulative += int_array[j+i*width*height]; - n++; - } - } - - if(n > 100) { - const double average = cumulative / (double)n; - power[i] = -2.5/log(average/(double)max_int[i]); - if(power[i] < 0) - power[i] = 1; - } else { - power[i] = 1; - } - - if(n <= 10) { - LogInfo("not enough data\n"); - return; - } - } - - const double vibrancy = 2.0; - double averagePower = 0; - for(unsigned int i = 0; i < num_colors; i++) { - averagePower += power[i]; - } - averagePower /= (double)num_colors; - for(unsigned int i = 0; i < num_colors; i++) { - power[i] = vibrancy*power[i] + (1.0 - vibrancy)*averagePower; - } - - pngwriter* pngFile = new pngwriter(width, height, 0.0, filename.c_str()); - pngFile->setcompressionlevel(9); - pngFile->settext("Attractor", "Joshua Moerman", "A awesome attractor", "AwesomeAttractor"); - - for(unsigned int x = 0; x < width; x++) { - for(unsigned int y = 0; y < height; y++) { - double r = 0.0; - double g = 0.0; - double b = 0.0; - for(unsigned int c = 0; c < num_colors; c++) { - const double norm_value = (double)int_array[x + y*width + c*width*height]/max_int[c]; - switch(c) { - case 0: { - r = (pow(norm_value, power[c]))*3.0; - //break; - } - case 1: { - g = (pow(norm_value, power[c]*2.0))*3.0; - //break; - } - case 2: { - b = (pow(norm_value, power[c]*3.0))*3.0; - //break; - } - default: - break; - } - } - //pngwriter clips values for me - pngFile->plot(x, y, r, g, b); - } - } - - - delete[] max_int; - delete[] power; - - LogInfo("Writing %s\n", filename.c_str()); - pngFile->close(); - - LogMoreInfo("File written"); -} diff --git a/canvae/PNG.hpp b/canvae/PNG.hpp index 09e6a53..61179b8 100644 --- a/canvae/PNG.hpp +++ b/canvae/PNG.hpp @@ -1,6 +1,12 @@ #ifndef PNG_HPP #define PNG_HPP +#include +#include +#include + +#include "../pngwriter/pngwriter.h" + #include "../Canvas.hpp" class PNG : public Canvas { @@ -9,16 +15,131 @@ class PNG : public Canvas { unsigned int num_colors; unsigned int* int_array; + //std::shared_ptr> int_array; public: double v; - PNG(unsigned int width, unsigned int height, unsigned int num_colors = 1); + PNG(unsigned int width, unsigned int height, unsigned int num_colors = 1): + Canvas(2), width(width), height(height), num_colors(num_colors), v(0) { + + //int_array = std::make_shared>(width*height*num_colors); + int_array = new unsigned int[width*height*num_colors]; + + assert(int_array != nullptr); + + clear(); + + LogDebug("New Canvas\n"); + } + + virtual void clear() { + std::fill_n(int_array, width*height*num_colors, 0); + } + + virtual void plot(const double* normalizedPosition) { + const double& x = normalizedPosition[0]; + const double& y = normalizedPosition[1]; + + const unsigned int x_int = x*width + width*.5; + const unsigned int y_int = y*width + height*.5; + const unsigned int index = x_int + width * y_int; + + if(x_int < width && y_int < height) { + int_array[index]++; + } + } + + virtual void output_file(const char* filename_in) const { + std::string filename = filename_in; + filename += ".png"; + unsigned int* max_int = new unsigned int[num_colors]; + double* power = new double[num_colors]; + + for(unsigned int i = 0; i < num_colors; i++) { + max_int[i] = 0; + double cumulative = 0; + unsigned int n = 0; + + for(unsigned int j = 0; j < width*height; j++) { + if(max_int[i] < int_array[j+i*width*height]) { + max_int[i] = int_array[j+i*width*height]; + } + if(int_array[j+i* width* height] != 0) { + cumulative += int_array[j+i*width*height]; + n++; + } + } + + if(n > 100) { + const double average = cumulative / (double)n; + power[i] = -2.5/log(average/(double)max_int[i]); + if(power[i] < 0) + power[i] = 1; + } else { + power[i] = 1; + } + + if(n <= width) { + LogInfo("not enough data\n"); + return; + } + } + + const double vibrancy = 2.0; + double averagePower = 0; + for(unsigned int i = 0; i < num_colors; i++) { + averagePower += power[i]; + } + averagePower /= (double)num_colors; + for(unsigned int i = 0; i < num_colors; i++) { + power[i] = vibrancy*power[i] + (1.0 - vibrancy)*averagePower; + } + + pngwriter* pngFile = new pngwriter(width, height, 0.0, filename.c_str()); + pngFile->setcompressionlevel(9); + pngFile->settext("Attractor", "Joshua Moerman", "A awesome attractor", "AwesomeAttractor"); + + for(unsigned int x = 0; x < width; x++) { + for(unsigned int y = 0; y < height; y++) { + double r = 0.0; + double g = 0.0; + double b = 0.0; + for(unsigned int c = 0; c < num_colors; c++) { + const double norm_value = (double)int_array[x + y*width + c*width*height]/max_int[c]; + switch(c) { + case 0: { + r = (pow(norm_value, power[c]))*3.0; + //break; + } + case 1: { + g = (pow(norm_value, power[c]*2.0))*3.0; + //break; + } + case 2: { + b = (pow(norm_value, power[c]*3.0))*3.0; + //break; + } + default: + break; + } + } + //pngwriter clips values for me + pngFile->plot(x, y, r, g, b); + } + } + + delete[] max_int; + delete[] power; + + LogInfo("Writing %s\n", filename.c_str()); + pngFile->close(); + + delete pngFile; - virtual void clear(); - virtual void plot(const double* normalizedPosition); - virtual void output_file(const char* filename) const; + LogMoreInfo("File written"); + } }; diff --git a/kernels/Ikeda3D.hpp b/kernels/Ikeda3D.hpp index b0b7bcf..3df88cb 100644 --- a/kernels/Ikeda3D.hpp +++ b/kernels/Ikeda3D.hpp @@ -22,7 +22,7 @@ public: virtual std::string type() { return "ikeda"; }; - virtual void generate_random_parameters() { + virtual void generate_random_parameters() { parameters[0] = rand() / double(RAND_MAX) *2.0 - 0.5; init(); } @@ -30,9 +30,9 @@ public: virtual void operator()() { std::swap(vectorNew, vectorOld); - vectorOld[2] = 0.4 - 6.0/(1 + vectorOld[0]*vectorOld[0] + vectorOld[1]*vectorNew[1]); - - vectorNew[0] = 1 + parameters[0] * (vectorOld[0]*cos(vectorOld[2]) - vectorOld[1]*sin(vectorOld[2])); + vectorOld[2] = 0.4 - 6.0/(1 + vectorOld[0]*vectorOld[0] + vectorOld[1]*vectorNew[1]); + + vectorNew[0] = 1 + parameters[0] * (vectorOld[0]*cos(vectorOld[2]) - vectorOld[1]*sin(vectorOld[2])); vectorNew[0] = parameters[0] * (vectorOld[0]*sin(vectorOld[2]) + vectorOld[1]*cos(vectorOld[2])); } }; diff --git a/stfu/stf.hpp b/stfu/stf.hpp index 47a5b5d..91f7f44 100644 --- a/stfu/stf.hpp +++ b/stfu/stf.hpp @@ -72,6 +72,8 @@ public: std::multimap children; //@} + node() : values(), children() {} + /** Clears the whole node recursively. */ @@ -319,7 +321,7 @@ private: while(index--) it++; return const_cast(it->second); } else { - throw std::out_of_range((std::string)"get_indexed->"+"Element " + key + "doesn't exist!"); + throw std::out_of_range((std::string)"get_indexed->"+"Element " + key + " doesn't exist!"); } }