Lindenmayer System
A Simple L-system Image Generator Featuring Turtle Graphics
Lsystem.h
Go to the documentation of this file.
1 
4 // MIT License
5 //
6 // Copyright (c) 2020 Ian Parberry
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to
10 // deal in the Software without restriction, including without limitation the
11 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12 // sell copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 
26 #pragma once
27 
28 #include "Random.h"
29 #include "Includes.h"
30 
32 // class LProduction
33 
34 #pragma region LProduction
35 
42 
44  public:
45  char m_chLHS = '\0';
46  std::wstring m_wstrRHS;
47  float m_fProb;
48 
49  LProduction(char lhs, const std::wstring rhs, float fProb=1);
50 }; //LProduction
51 
52 #pragma endregion LProduction
53 
55 // class LSystem
56 
57 #pragma region LSystem
58 
69 
70 class LSystem{
71  private:
73 
74  std::wstring m_wstrRoot;
75 
76  std::map<wchar_t, std::vector<LProduction>> m_mapRules;
77  std::wstring m_wstrRuleString;
78 
79  std::wstring m_wstrBuffer[2];
80  std::wstring* m_pResult = m_wstrBuffer;
81 
82  bool m_bStochastic = false;
83  UINT m_nGenerations = 0;
84 
85  public:
86  void SetRoot(const std::wstring& omega);
87  void AddRule(const LProduction& rule);
88 
89  void Clear();
90  void Generate(const UINT n);
91 
92  const std::wstring& GetString() const;
93  const std::wstring& GetRuleString() const;
94  const UINT GetGenerations() const;
95 
96  const bool IsStochastic() const;
97 }; //LSystem
98 
99 #pragma endregion LSystem
std::wstring m_wstrRHS
Right-hand side of production.
Definition: Lsystem.h:46
std::map< wchar_t, std::vector< LProduction > > m_mapRules
Productions.
Definition: Lsystem.h:76
void Clear()
Clear the rules, buffers, and settings.
Definition: Lsystem.cpp:101
LProduction(char lhs, const std::wstring rhs, float fProb=1)
Constructor.
Definition: Lsystem.cpp:40
Useful includes.
std::wstring * m_pResult
Pointer to generated string.
Definition: Lsystem.h:80
const bool IsStochastic() const
Is a stochastic L-system.
Definition: Lsystem.cpp:197
std::wstring m_wstrBuffer[2]
Generation buffers.
Definition: Lsystem.h:79
A stochastic bracketed context-free L-system.
Definition: Lsystem.h:70
Stochastic context-free production.
Definition: Lsystem.h:43
const std::wstring & GetRuleString() const
Get rule string.
Definition: Lsystem.cpp:183
Pseudorandom Number Generator (PRNG for short).
Definition: Random.h:36
Interface for the pseudorandom number generator CRandom.
CRandom m_cRandom
PRNG.
Definition: Lsystem.h:72
std::wstring m_wstrRuleString
Rule string.
Definition: Lsystem.h:77
char m_chLHS
Left-hand side of production.
Definition: Lsystem.h:45
std::wstring m_wstrRoot
Root string.
Definition: Lsystem.h:74
const std::wstring & GetString() const
Get generated string.
Definition: Lsystem.cpp:176
const UINT GetGenerations() const
Get number of generations.
Definition: Lsystem.cpp:190
void AddRule(const LProduction &rule)
AddRule rule.
Definition: Lsystem.cpp:59
void Generate(const UINT n)
Generate L-system from stored root and rules.
Definition: Lsystem.cpp:125
float m_fProb
Probability of production applying.
Definition: Lsystem.h:47
bool m_bStochastic
Includes a stochastic rule.
Definition: Lsystem.h:82
void SetRoot(const std::wstring &omega)
Set the root string.
Definition: Lsystem.cpp:93
UINT m_nGenerations
Number of generations.
Definition: Lsystem.h:83