~ubuntu-branches/ubuntu/oneiric/structure-synth/oneiric

« back to all changes in this revision

Viewing changes to StructureSynth/Model/Builder.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Miriam Ruiz
  • Date: 2009-04-13 13:28:45 UTC
  • Revision ID: james.westby@ubuntu.com-20090413132845-d7d42t4llxjxq0ez
Tags: upstream-0.9
ImportĀ upstreamĀ versionĀ 0.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "Builder.h"
 
2
#include "../../SyntopiaCore/Logging/Logging.h"
 
3
#include "../../SyntopiaCore/Exceptions/Exception.h"
 
4
#include "../../SyntopiaCore/Misc/ColorUtils.h"
 
5
#include "../../SyntopiaCore/Math/Vector3.h"
 
6
 
 
7
#include <QProgressDialog>
 
8
#include <QApplication>
 
9
 
 
10
using namespace SyntopiaCore::Logging;
 
11
using namespace SyntopiaCore::Math;
 
12
using namespace SyntopiaCore::Exceptions;
 
13
 
 
14
namespace StructureSynth {
 
15
        namespace Model {
 
16
 
 
17
                Builder::Builder(Rendering::Renderer* renderTarget, RuleSet* ruleSet) : renderTarget(renderTarget), ruleSet(ruleSet) {
 
18
                        maxGenerations = 1000;
 
19
                        maxObjects = 100000;
 
20
                        objects = 0;
 
21
                        newSeed = 0;
 
22
                        hasSeedChanged = false;
 
23
                };
 
24
                        
 
25
 
 
26
                void Builder::build() {
 
27
                        objects = 0;
 
28
                        INFO("Starting builder...");
 
29
                        
 
30
                        /// Push first generation state
 
31
                        stack.append(RuleState(ruleSet->getStartRule(), State()));
 
32
                        int generationCounter = 0;
 
33
                        
 
34
                        QProgressDialog progressDialog("Building objects...", "Cancel", 0, 100, 0);
 
35
                        progressDialog.setWindowModality(Qt::WindowModal);
 
36
                        progressDialog.setMinimumDuration(0);
 
37
                        progressDialog.show();
 
38
                        progressDialog.setValue(0);
 
39
 
 
40
                        int lastValue = 0;
 
41
 
 
42
                        while (generationCounter < maxGenerations && objects < maxObjects) {
 
43
 
 
44
                                double p = 0;
 
45
                                if (maxObjects>0) {
 
46
                                        p = objects/(double)maxObjects;
 
47
                                }
 
48
 
 
49
                                double p2 = 0;
 
50
                                if (maxGenerations>0) {
 
51
                                        p2 = generationCounter/(double)maxGenerations;
 
52
                                }
 
53
 
 
54
                                double progress = p;
 
55
                                if (p2 > p) progress = p2;
 
56
 
 
57
                                if (maxObjects<=0 && maxGenerations<=0) {
 
58
                                        progress = (generationCounter%9)/9.0;
 
59
                                }
 
60
 
 
61
                                if (lastValue != (int)(progress*100.0)) {
 
62
                                        progressDialog.setValue((int)(progress*100.0));
 
63
                                        progressDialog.setLabelText(
 
64
                                                QString("Building objects...\r\n\r\nGeneration: %1\r\nObjects: %2\r\nPending rules: %3")
 
65
                                                .arg(generationCounter).arg(objects).arg(stack.size()));
 
66
                                        qApp->processEvents();
 
67
                                }
 
68
 
 
69
                                lastValue = (int)(progress*100.0);
 
70
                                
 
71
                                if (progressDialog.wasCanceled()) {
 
72
                                        break;
 
73
                                }
 
74
 
 
75
                                generationCounter++;
 
76
 
 
77
                                // Now iterate though all RuleState's on stack and create next generation.
 
78
                                //INFO(QString("Executing generation %1 with %2 individuals").arg(generationCounter).arg(stack.size()));
 
79
                                nextStack.clear();
 
80
                                for (int i = 0; i < stack.size(); i++) {
 
81
                                        //      INFO("Executing: " + stack[i].rule->getName());
 
82
                                        state = stack[i].state;
 
83
                                        stack[i].rule->apply(this);
 
84
                                }
 
85
                                stack = nextStack;
 
86
 
 
87
                                if (stack.size() == 0) break; // no need to continue...
 
88
                        }
 
89
 
 
90
                        progressDialog.setValue(100); 
 
91
                        progressDialog.hide();
 
92
 
 
93
                        if (progressDialog.wasCanceled()) {
 
94
                                INFO("User terminated.");
 
95
                        }
 
96
 
 
97
                        if (objects == maxObjects) {
 
98
                                INFO(QString("Terminated because maximum number of objects reached (%1).").arg(maxObjects));
 
99
                                INFO(QString("Use 'Set MaxObjects' command to increase this number."));
 
100
                        }
 
101
                        
 
102
                        if (generationCounter == maxGenerations) {
 
103
                                INFO(QString("Terminated because maximum number of generations reached (%1).").arg(maxGenerations));
 
104
                                INFO(QString("Use 'Set Maxdepth' command to increase this number."));
 
105
                        }
 
106
                        INFO("Done building...");
 
107
                }
 
108
 
 
109
                void Builder::setCommand(QString command, QString param) {
 
110
                        if (command == "maxdepth") {
 
111
                                //INFO(QString("Setting 'maxgenerations' to %1").arg(param));
 
112
                                bool succes;
 
113
                                int i = param.toInt(&succes);
 
114
                                if (!succes) throw Exception(QString("Command 'maxdepth' expected integer parameter. Found: %1").arg(param));
 
115
                                maxGenerations = i;
 
116
                        } else if (command == "maxobjects") {
 
117
                                //INFO(QString("Setting 'maxgenerations' to %1").arg(param));
 
118
                                bool succes;
 
119
                                int i = param.toInt(&succes);
 
120
                                if (!succes) throw Exception(QString("Command 'maxobjects' expected integer parameter. Found: %1").arg(param));
 
121
                                maxObjects = i;
 
122
                        } else if (command == "seed") {
 
123
                                bool succes;
 
124
                                int i = param.toInt(&succes);
 
125
                                if (!succes) throw Exception(QString("Command 'seed' expected integer parameter. Found: %1").arg(param));
 
126
                                srand(i);
 
127
                                hasSeedChanged = true;
 
128
                                newSeed = i;
 
129
                        } else if (command == "background") {
 
130
                                QColor c(param);
 
131
                                if (!c.isValid()) throw Exception(QString("Command 'background' expected a valid color identifier: Found: %1").arg(param));
 
132
                                renderTarget->setBackgroundColor(Vector3f(c.red() / 255.0, c.green() / 255.0, c.blue() / 255.0) );
 
133
                        } else if (command == "scale") {
 
134
                                bool succes;
 
135
                                double s = param.toDouble(&succes);
 
136
                                if (!succes) throw Exception(QString("Command 'scale' expected floating point parameter. Found: %1").arg(param));
 
137
                                renderTarget->setScale(s);
 
138
                        } else if (command == "translation") {
 
139
                                bool succes;
 
140
                                Vector3f v3(param, succes);
 
141
                                if (!succes) throw Exception(QString("Command 'translation' expected vector (such as [1 3 -10.1]). Found: %1").arg(param));
 
142
                                renderTarget->setTranslation(v3);
 
143
                        } else if (command == "pivot") {
 
144
                                bool succes;
 
145
                                Vector3f v3(param, succes);
 
146
                                if (!succes) throw Exception(QString("Command 'pivot' expected vector (such as [1 3 -10.1]). Found: %1").arg(param));
 
147
                                renderTarget->setPivot(v3);
 
148
                        } else if (command == "rotation") {
 
149
                                bool succes;
 
150
                                Matrix4f m4(param, succes);
 
151
                                if (!succes) throw Exception(QString("Command 'rotation' expected matrix (such as [1 0 0 0 1 0 0 0 1]). Found: %1").arg(param));
 
152
                                renderTarget->setRotation(m4);
 
153
                        } else if (command == "opengl") {
 
154
                                INFO("Render commands for 'opengl' not impl'ed yet!");
 
155
                        } else if (command == "template") {
 
156
                                renderTarget->callCommand(command,param);
 
157
                        } else {
 
158
                                throw Exception(QString("Unknown command: %1").arg(command));
 
159
                        }
 
160
                }
 
161
                
 
162
                ExecutionStack& Builder::getNextStack() {
 
163
                        return nextStack;
 
164
                }
 
165
 
 
166
        }
 
167
}
 
168