Browse Source

fix in stfu and some newline shit, png-cnavas to header only

master
Joshua Moerman 13 years ago
parent
commit
c5bcec5000
  1. 134
      canvae/PNG.cpp
  2. 129
      canvae/PNG.hpp
  3. 8
      kernels/Ikeda3D.hpp
  4. 4
      stfu/stf.hpp

134
canvae/PNG.cpp

@ -1,134 +0,0 @@
#include "../Logger.hpp"
#include <fstream>
#include <cmath>
#include <cstdlib>
#include <cassert>
#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");
}

129
canvae/PNG.hpp

@ -1,6 +1,12 @@
#ifndef PNG_HPP
#define PNG_HPP
#include <memory>
#include <vector>
#include <cassert>
#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<std::vector<unsigned int>> 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<vstd::vector<unsigned int>>(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");
}
};

8
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]));
}
};

4
stfu/stf.hpp

@ -72,6 +72,8 @@ public:
std::multimap<std::string, node> children;
//@}
node() : values(), children() {}
/**
Clears the whole node recursively.
*/
@ -319,7 +321,7 @@ private:
while(index--) it++;
return const_cast<T2&>(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!");
}
}