1 /**************************************************************************/
2 /*  Copyright 2012 Tim Day                                                */
3 /*                                                                        */
4 /*  This file is part of Evolvotron                                       */
5 /*                                                                        */
6 /*  Evolvotron is free software: you can redistribute it and/or modify    */
7 /*  it under the terms of the GNU General Public License as published by  */
8 /*  the Free Software Foundation, either version 3 of the License, or     */
9 /*  (at your option) any later version.                                   */
10 /*                                                                        */
11 /*  Evolvotron is distributed in the hope that it will be useful,         */
12 /*  but WITHOUT ANY WARRANTY; without even the implied warranty of        */
13 /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         */
14 /*  GNU General Public License for more details.                          */
15 /*                                                                        */
16 /*  You should have received a copy of the GNU General Public License     */
17 /*  along with Evolvotron.  If not, see <http://www.gnu.org/licenses/>.   */
18 /**************************************************************************/
19 
20 /*! \file
21   \brief Standalone mutator for evolvotron function files.
22 */
23 
24 #include "mutatable_image.h"
25 #include "mutation_parameters.h"
26 #include "function_top.h"
27 
28 #include <boost/program_options.hpp>
29 
30 //! Application code
main(int argc,char * argv[])31 int main(int argc,char* argv[])
32 {
33   {
34     bool genesis;
35     bool help;
36     bool linear;
37     bool spheremap;
38     bool verbose;
39 
40     boost::program_options::options_description options_desc("Options");
41     {
42       using namespace boost::program_options;
43       options_desc.add_options()
44 	("genesis,g"  ,bool_switch(&genesis)  ,"Create a new function to stdout (without this option, a function will be read from stdin)")
45 	("help,h"     ,bool_switch(&help)     ,"Print command-line options help message and exit")
46 	("linear,l"   ,bool_switch(&linear)   ,"Sweep z linearly in animations")
47 	("spheremap,p",bool_switch(&spheremap),"Generate spheremap")
48 	("verbose,v"  ,bool_switch(&verbose)  ,"Log some details to stderr")
49 	;
50     }
51 
52     boost::program_options::variables_map options;
53     boost::program_options::store(boost::program_options::parse_command_line(argc,argv,options_desc),options);
54     boost::program_options::notify(options);
55 
56     if (help)
57       {
58 	std::cerr << options_desc;
59 	return 0;
60       }
61 
62     if (verbose)
63       std::clog.rdbuf(std::cerr.rdbuf());
64     else
65       std::clog.rdbuf(sink_ostream.rdbuf());
66 
67     // Normally would use time(0) to seed random number generator
68     // but can imagine several of these starting up virtually simultaneously
69     // so need something with higher resolution.
70     // Adding the process id too to keep things unique.
71 
72     QTime t(QTime::currentTime());
73     uint seed=getpid()+t.msec()+1000*t.second()+60000*t.minute()+3600000*t.hour();
74 
75     std::clog << "Random seed is " << seed << "\n";
76 
77     MutationParameters mutation_parameters(seed,false,false);
78 
79     std::string report;
80     boost::shared_ptr<const MutatableImage> imagefn_out;
81 
82     if (genesis)
83       {
84 	std::unique_ptr<FunctionTop> fn_top(FunctionTop::initial(mutation_parameters));
85 
86 	imagefn_out=boost::shared_ptr<const MutatableImage>(new MutatableImage(fn_top,!linear,spheremap,false));
87       }
88     else
89       {
90 	const boost::shared_ptr<const MutatableImage> imagefn_in
91 	  (
92 	   MutatableImage::load_function(mutation_parameters.function_registry(),std::cin,report)
93 	   );
94 
95 	if (imagefn_in.get()==0)
96 	  {
97 	    std::cerr << "evolvotron_mutate: Error: Function not loaded due to errors:\n" << report;
98 	    return 1;
99 	  }
100 	else if (!report.empty())
101 	  {
102 	    std::cerr << "evolvotron_mutate: Warning: Function loaded with warnings:\n" << report;
103 	  }
104 
105 	imagefn_out=imagefn_in->mutated(mutation_parameters);
106       }
107 
108     imagefn_out->save_function(std::cout);
109   }
110 
111   return 0;
112 }
113