HyperspaceExplorer 0.7.1
LSystem.h
00001 /*
00002     <one line to give the program's name and a brief idea of what it does.>
00003     Copyright (C) <year>  <name of author>
00004 
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License along
00016     with this program; if not, write to the Free Software Foundation, Inc.,
00017     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00018 
00019 */
00020 
00021 #ifndef LSYSTEM_H
00022 #define LSYSTEM_H
00023 
00024 #include "Composite.h"
00025 #include "Object.h"
00026 
00027 #define USE_LSYSTEM
00028 
00029 
00030 
00031   class Axiom {
00032 
00033   public:
00034     Axiom (const std::string &axiom);
00035 
00036     const std::string &get() const;
00037     std::string::const_iterator begin() const;
00038     std::string::const_iterator end() const;
00039 
00040   private:
00041     std::string axiom_;
00042   };
00043 
00044   class Rule {
00045 
00046   public:
00047     static const char SEPARATOR = ':';
00048 
00049     Rule(const std::string &rule);
00050 
00051     std::string apply(char atom) const;
00052 
00053     char getPredecessor() const;
00054     const std::string &getSuccessor() const;
00055 
00056     std::string toString() const;
00057 
00058   private:
00059       char predecessor_;
00060       std::string successor_;
00061 
00062   };
00063 
00064 
00065   class Ruleset {
00066 
00067     typedef std::map<char, Rule> storage_type;
00068 
00069   public:
00070     Ruleset(const std::string &rules);
00071 
00072     std::string apply(const Axiom &axiom) const;
00073     storage_type::size_type size() const;
00074     std::string toString() const;
00075 
00076   private:
00077     storage_type rules_;
00078   };
00079 
00080 class LSystem : public Composite {
00082 
00095   class Alphabet {
00096 
00097   public:
00098 
00099     static const char LEFT_X = '+';
00100     static const char RIGHT_X = '-';
00101     static const char LEFT_Y = '^';
00102     static const char RIGHT_Y = 'v';
00103     static const char LEFT_Z = '<';
00104     static const char RIGHT_Z = '>';
00105     static const char LEFT_W = '[';
00106     static const char RIGHT_W = ']';
00107 
00108     static const char FORWARD = 'f';
00109     static const char BACK = 'b';
00110 
00111     static const char OPEN_GROUP = '{';
00112     static const char CLOSE_GROUP = '}';
00113 
00114     static const char SCALE = 's';
00115     static const char ANGLE = 'a';
00116 
00117     Alphabet(const std::string &letters);
00118 
00119     std::shared_ptr<Displayable> getDisplayable(char letter);
00120 
00121     std::string toString() const;
00122 
00123   private:
00124       std::vector<char> letters_;
00125 
00126   };
00127 
00128 public:
00129 
00130   LSystem(unsigned level = 3);
00131   virtual ~LSystem();
00132     
00133   virtual std::string getFunctionName() const;
00134 
00135   virtual void SetParameters(const ParameterMap &parameters);
00136 
00137 protected:
00138 
00139   virtual void Initialize();
00140 
00141 private:
00142 
00143     LSystem generate(unsigned level);
00144     void parse(const Axiom &axiom, const Ruleset &rules);
00145 
00146     std::string expand(const std::string &axiom, unsigned level);
00147 
00148     unsigned level_;
00149     Alphabet alphabet_;
00150     Ruleset rules_;
00151     Axiom axiom_;
00152 
00153     std::vector< VecMath::Matrix<4> > Rstate_;
00154     std::vector< VecMath::Vector<4> > xstate_;
00155     std::vector<double> scalestate_;
00156 
00157 };
00158 
00159 namespace {
00160   Displayable *createLSystem() { return new LSystem(); }
00161 # ifdef USE_LSYSTEM
00162   const bool registered_lsystem = TheFunctionFactory::Instance().registerFunction(createLSystem, "Object");
00163 # endif  
00164 }
00165 
00166 #endif // LSYSTEM_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends

Generated on Mon Apr 9 2012 20:25:14 for HyperspaceExplorer 0.7.1 by doxygen 1.7.4  -  Hosted bySourceForge.net Logo