From 97a9b87354d9affa45db1c257d132dbd15d73541 Mon Sep 17 00:00:00 2001 From: "Joshua Moerman (joshua-desktop)" Date: Sat, 23 Apr 2011 23:05:23 +0200 Subject: [PATCH] stf output, and possibly even more to header. (and a fix in stfu) --- Attractor.hpp | 9 +++++++++ AttractorKernel.hpp | 19 +++++++++++++++++++ canvae/PNG.cpp | 14 +++++--------- kernels/Logistic.hpp | 2 ++ kernels/Lorenz3D.hpp | 2 ++ kernels/Polynomial.hpp | 2 ++ kernels/PolynomialA3D.hpp | 2 ++ kernels/Unravel3D.hpp | 2 ++ main.cpp | 6 +++++- stfu/stf.cpp | 9 +++------ stfu/stf.hpp | 5 +---- 11 files changed, 52 insertions(+), 20 deletions(-) diff --git a/Attractor.hpp b/Attractor.hpp index e4d12ca..c530b7e 100644 --- a/Attractor.hpp +++ b/Attractor.hpp @@ -57,6 +57,15 @@ public: LogMoreInfo("\n"); } + stfu::node stf_output(){ + stfu::node kernel_node = kernel-> stf_output(); + stfu::node projector_node;/* = projector->stf_output();*/ + stfu::node system; + system.addChild("AttractorKernel", kernel_node); + system.addChild("Projector", projector_node); + return system; + } + friend std::ostream& operator<<(std::ostream& os, Attractor const& x); private: diff --git a/AttractorKernel.hpp b/AttractorKernel.hpp index 5966cff..b67e9c9 100644 --- a/AttractorKernel.hpp +++ b/AttractorKernel.hpp @@ -2,6 +2,7 @@ #define ATTRACTORKERNEL_HPP #include "Logger.hpp" +#include #include #include @@ -55,6 +56,24 @@ public: // iterate his formula, implemented by subclasses virtual void operator()() = 0; + stfu::node stf_output() { + stfu::node output; + output.value("type") = type(); + output.value("dimension") = std::to_string((long long unsigned)dimension); + stf_other(output); + + stfu::node parameters_node; + for(unsigned int i = 0; i < numberOfParameters; ++i) + parameters_node.value(i) = std::to_string((long double)parameters[i]); + + output.addChild("parameters", parameters_node); + return output; + } + + virtual std::string type() = 0; + + virtual void stf_other(stfu::node&) {} + #pragma mark - #pragma mark vector double const* vector() const { diff --git a/canvae/PNG.cpp b/canvae/PNG.cpp index a540200..759c03d 100644 --- a/canvae/PNG.cpp +++ b/canvae/PNG.cpp @@ -44,7 +44,9 @@ void PNG::plot(const double* position) { I/O functions */ -void PNG::output_file(const char* filename) const { +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]; @@ -88,7 +90,7 @@ void PNG::output_file(const char* filename) const { power[i] = vibrancy*power[i] + (1.0 - vibrancy)*averagePower; } - pngwriter* pngFile = new pngwriter(width, height, 0.0, filename); + pngwriter* pngFile = new pngwriter(width, height, 0.0, filename.c_str()); pngFile->setcompressionlevel(9); pngFile->settext("Attractor", "Joshua Moerman", "A awesome attractor", "AwesomeAttractor"); @@ -125,14 +127,8 @@ void PNG::output_file(const char* filename) const { delete[] max_int; delete[] power; - LogInfo("Writing %s\n", filename); - - std::ofstream file(filename); - if(!file) { - LogError("Couldn't write to file"); - } + LogInfo("Writing %s\n", filename.c_str()); pngFile->close(); LogMoreInfo("File written"); - } diff --git a/kernels/Logistic.hpp b/kernels/Logistic.hpp index 75939cc..5c7ef6d 100644 --- a/kernels/Logistic.hpp +++ b/kernels/Logistic.hpp @@ -25,6 +25,8 @@ public: init(); } + virtual std::string type() { return "logistic"; }; + virtual void generate_random_parameters() { for(unsigned int i = 0; i < numberOfParameters; ++i) { parameters[i] = rand() / double(RAND_MAX) + 3.0; diff --git a/kernels/Lorenz3D.hpp b/kernels/Lorenz3D.hpp index 92c5b3b..a6f5447 100644 --- a/kernels/Lorenz3D.hpp +++ b/kernels/Lorenz3D.hpp @@ -16,6 +16,8 @@ public: init(); } + virtual std::string type() { return "lorenz"; }; + virtual void generate_random_parameters() { parameters[0] = rand() / double(RAND_MAX) * 0.02; parameters[1] = rand() / double(RAND_MAX) * 4.0 + 8.0; diff --git a/kernels/Polynomial.hpp b/kernels/Polynomial.hpp index 82da539..76a41e3 100644 --- a/kernels/Polynomial.hpp +++ b/kernels/Polynomial.hpp @@ -38,6 +38,8 @@ public: Polynomial(const unsigned int dimension, const unsigned int orde): AttractorKernel(dimension, calculateNumberOfParameters(dimension, orde)), orde(orde) {} + virtual std::string type() { return "polynomial"; }; + virtual void operator()() { std::swap(vectorNew, vectorOld); diff --git a/kernels/PolynomialA3D.hpp b/kernels/PolynomialA3D.hpp index 76aeb43..49e232e 100644 --- a/kernels/PolynomialA3D.hpp +++ b/kernels/PolynomialA3D.hpp @@ -8,6 +8,8 @@ public: PolynomialA3D(): AttractorKernel(3, 3) {} + virtual std::string type() { return "polynomial a"; }; + virtual void operator()() { std::swap(vectorNew, vectorOld); diff --git a/kernels/Unravel3D.hpp b/kernels/Unravel3D.hpp index 69f9b74..fd9427c 100644 --- a/kernels/Unravel3D.hpp +++ b/kernels/Unravel3D.hpp @@ -10,6 +10,8 @@ public: Unravel3D(): AttractorKernel(3, 7) {} + virtual std::string type() { return "unravel"; }; + virtual void operator()() { std::swap(vectorNew, vectorOld); diff --git a/main.cpp b/main.cpp index 8f9fe1b..d8f46a9 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,6 @@ #include "Logger.hpp" #include +#include #include #include #include @@ -112,7 +113,7 @@ int main(int argc, char* argv[]) { time_t t = time(0); struct tm* lt = localtime(&t); int r = rand() % 10; - sprintf(filename, "render/attractor_%04d-%02d-%02d_%02d-%02d-%02d-%01d.png", lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec, r); + sprintf(filename, "render/attractor_%04d-%02d-%02d_%02d-%02d-%02d-%01d", lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec, r); start = clock(); myAttractor.projector->canvas->output_file(filename); @@ -122,6 +123,9 @@ int main(int argc, char* argv[]) { LogInfo("Total clock time for writing png: %f\n", totalTime); + std::ofstream file(std::string(filename)+".stf"); + file << myAttractor.stf_output() << std::endl; + delete my_attractor_ptr; return 0; diff --git a/stfu/stf.cpp b/stfu/stf.cpp index 97a6f8b..acb90e4 100644 --- a/stfu/stf.cpp +++ b/stfu/stf.cpp @@ -48,12 +48,9 @@ bool operator<< (const char* filename, const node& root) { return root.write(filename); } -bool operator<< (std::ofstream& out, const node& root) { - return root.write(out); -} - -bool operator>> (std::istream& in, node& root) { - return root.read(in); +std::istream& operator>> (std::istream& in, node& root) { + root.read(in); + return in; } bool operator>> (const char* filename, node& root) { diff --git a/stfu/stf.hpp b/stfu/stf.hpp index f19f4f9..47a5b5d 100644 --- a/stfu/stf.hpp +++ b/stfu/stf.hpp @@ -56,14 +56,11 @@ class node { /** Overloaded ostream's operator<< */ friend std::ostream& operator<< (std::ostream& out, const node& root); - /** Returns whether it was succesful or not*/ - friend bool operator<< (std::ofstream& out, const node& root); - /** Acts like write(), but looks like this: "filename" << node */ friend bool operator<< (const char* filename, const node& root); /** Overloaded istream's operator>> */ - friend bool operator>> (std::istream& in, node& root); + friend std::istream& operator>> (std::istream& in, node& root); /** Acts like read(), but looks like this: "filename" >> node */ friend bool operator>> (const char* filename, node& root);