File: RuleSet.cpp

package info (click to toggle)
structure-synth 1.5.0-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,268 kB
  • ctags: 1,966
  • sloc: cpp: 10,209; python: 164; makefile: 71; sh: 15
file content (258 lines) | stat: -rw-r--r-- 8,380 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include "RuleSet.h"
#include "RuleRef.h"
#include "CustomRule.h"
#include "AmbiguousRule.h"
#include "PrimitiveRule.h"

#include <QStringList>
#include <QMap>
#include <typeinfo>

#include "../../SyntopiaCore/Exceptions/Exception.h"
#include "../../SyntopiaCore/Logging/Logging.h"
#include "../../SyntopiaCore/Math/Vector3.h"

using namespace SyntopiaCore::Exceptions;
using namespace SyntopiaCore::Logging;
using namespace SyntopiaCore::Math;

namespace StructureSynth {
	namespace Model {	

		/// Constructor. Automatically adds built-in rules.
		RuleSet::RuleSet() {
			topLevelRule = new CustomRule("TopLevelRule");
			recurseDepth = false;
			defaultClass = new PrimitiveClass();

			/// Add instances of predefined built-in types.
			rules.append(new PrimitiveRule(PrimitiveRule::Box,defaultClass));
			rules.append(new PrimitiveRule(PrimitiveRule::Sphere,defaultClass));
			rules.append(new PrimitiveRule(PrimitiveRule::Cylinder,defaultClass));
			rules.append(new PrimitiveRule(PrimitiveRule::Mesh,defaultClass));
			rules.append(new PrimitiveRule(PrimitiveRule::Line,defaultClass));
			rules.append(new PrimitiveRule(PrimitiveRule::Dot,defaultClass));
			rules.append(new PrimitiveRule(PrimitiveRule::Grid,defaultClass));
			rules.append(new PrimitiveRule(PrimitiveRule::Template,defaultClass));
			rules.append(topLevelRule);

			
		};

		void RuleSet::setRulesMaxDepth(int maxDepth) {
			for (int i = 0; i < rules.size(); i++) {
				int md = rules[i]->getMaxDepth();
				//INFO(QString("Rule: %1, %2 -> %3").arg(rules[i]->getName()).arg(md).arg(maxDepth));
				if (md <= 0) rules[i]->setMaxDepth(maxDepth);
			}
		}


		/// Deletes rules
		RuleSet::~RuleSet() {
			for (int i = 0; i < rules.size(); i++) delete(rules[i]);
			//for (int i = 0; i < primitiveClasses.size(); i++) delete(primitiveClasses[i]);
			
		}

		void RuleSet::addRule(Rule* rule) { 
			// Check if the rule name is already used...
			QString name = rule->getName();
			
			for (int i = 0; i < rules.size(); i++) {

				if (rules[i]->getName() == name) {
					if (typeid(*rules[i]) == typeid(CustomRule)) {
						// A Custom rule already exists with the same name.
						// Now we must remove the existing rule, and create a new ambiguous rule hosting them both.
			
						Rule* r = rules[i];
						rules.removeAll(r);
						CustomRule* cr1 = dynamic_cast<CustomRule*>(r);
					
						AmbiguousRule* ar = new AmbiguousRule(name);
						ar->appendRule(cr1);

						CustomRule* cr2 = dynamic_cast<CustomRule*>(rule);
						if (!cr2) throw Exception("Trying to add non-custom rule to ambiguous rule: '%1'. "+name);
						ar->appendRule(cr2);

						rules.append(ar);

						return;
					} else if (typeid(*rules[i]) == typeid(PrimitiveRule)) {
						// A primitive rule already exists with the same name. This is not acceptable.
						throw Exception(QString("A primitive rule already exists with the name: '%1'. New definitions can not merged.").arg(name));
					} else if (typeid(*rules[i]) == typeid(AmbiguousRule)) {
						// A ambiguous rule already exists with the same name. We will add to it.
						AmbiguousRule* ar = dynamic_cast<AmbiguousRule*>(rules[i]);
						CustomRule* cr = dynamic_cast<CustomRule*>(rule);
						if (!cr) throw Exception("Trying to add non-custom rule to ambiguous rule: '%1'. "+name);
						ar->appendRule(cr);
						return;
					} else {
						WARNING("Unknown typeid");
					}
				}

			}

			rules.append(rule);
		}


		/// Resolve symbolic names into pointers
		QStringList RuleSet::resolveNames() {

			// build map
			QMap<QString, Rule*> map;
			for (int i = 0; i < rules.size(); i++) map[rules[i]->getName()] = rules[i];

			QStringList usedPrimitives;
			


			// resolve rules.
			for (int i = 0; i < rules.size(); i++) {

				QList<RuleRef*> refs = rules[i]->getRuleRefs();


				for (int j = 0; j < refs.size(); j++) {
					QString name = refs[j]->getReference();
					if (!map.contains(name)) {
						// We could not resolve the name.
						// Check if it has a class specifier.
						QStringList sl = name.split("::");
						if (sl.size() == 2) {
							QString baseName = sl[0];
							QString classID = sl[1];

							if (!map.contains(baseName)) {
								throw Exception(QString("Unable to resolve base rule name: %1 for rule %2").arg(baseName).arg(name));
							}
					
							// Now we have to create a new instance of this rule.
							Rule* r = map[baseName];

							if (typeid(*r) != typeid(PrimitiveRule)) {
								throw Exception(QString("Only primitive rules (box, sphere, ...) may have a class specifier: %1 is invalid").arg(name));
							}

							PrimitiveRule* pr = (PrimitiveRule*)r;
							PrimitiveRule* newRule = new PrimitiveRule(*pr);
							newRule->setClass(getPrimitiveClass(classID));
							
							map[name] = newRule;
							
							//INFO("Created new class for rule: " + name);
						} else {
							// The Polygons rules (i.e. Triangle[x,y,z]) are special rules, each created on the fly.
							QRegExp r("triangle\\[(.*)\\]");
							if (r.exactMatch(name)) {
								// Check the arguments.
								INFO("Found:" + r.cap(1));
								QVector<Vector3f> v;
								QStringList l = r.cap(1).split(";");
								if (l.size() != 3) {
									throw Exception(QString("Unable to parse Triangle definition - must be triangle(p1;p2;p3) - found : %1").arg(name));
								}

								for (unsigned int i = 0; i < 3; i++) {
									QStringList l2 = l[i].split(",");
									if (l2.size() != 3) {
											throw Exception(QString("Unable to parse Triangle definition - coordinates must be like '0.1,0.2,0.3' - found : %1").arg(l[i]));
									}
									bool ok = false;
									float f1 = l2[0].toFloat(&ok);
									if (!ok) throw Exception(QString("Unable to parse Triangle definition - error in first coordinate - found in : %1").arg(name));
									float f2 = l2[1].toFloat(&ok);
									if (!ok) throw Exception(QString("Unable to parse Triangle definition - error in second coordinate - found in : %1").arg(name));
									float f3 = l2[2].toFloat(&ok);
									if (!ok) throw Exception(QString("Unable to parse Triangle definition - error in third coordinate - found in : %1").arg(name));
									v.append(Vector3f(f1,f2,f3));
								}	


								map[name] = new TriangleRule(v[0], v[1], v[2], defaultClass);
							
							} else {
								throw Exception(QString("Unable to resolve rule: %1").arg(name));
							}
						}
					}
					if ( dynamic_cast<PrimitiveRule*>(map[name]) ) {
						if (!usedPrimitives.contains(name)) usedPrimitives.append(name);
					}
					refs[j]->setRef(map[name]);
				}

			}

			
			return usedPrimitives;

		}

		///
		QStringList RuleSet::getUnreferencedNames() {

			WARNING("RuleSet::getUnreferencedNames(): Not implemented yet!");
			return QStringList();

		};

		Rule* RuleSet::getStartRule() const {
			return topLevelRule;
		};

		/// For debug
		void  RuleSet::dumpInfo() const {
			int custom = 0;
			int ambi = 0;
			int primitive = 0;
			int rulesCount = 0;

			for (int i = 0; i < rules.size(); i++) {
				rulesCount++; 

				CustomRule* cr = dynamic_cast<CustomRule*>(rules[i]);
				if (cr) custom++;

				AmbiguousRule* ar = dynamic_cast<AmbiguousRule*>(rules[i]);
				if (ar) ambi++;

				PrimitiveRule* pr = dynamic_cast<PrimitiveRule*>(rules[i]);
				if (pr) primitive++;
			}

			/*
			Debug(QString("Loaded %1 user rules: %2 Custom Rules, %3 Ambiguous Rules")
				.arg(rulesCount-primitive).arg(custom).arg(ambi));
			Debug(QString("Loaded %1 built-in rules.").arg(primitive));
			*/
		}

		bool RuleSet::existsPrimitiveClass(QString classLabel) {
			for (int i = 0; i < primitiveClasses.count(); i++) {
				if (primitiveClasses[i]->name == classLabel) return true;
			}
			return false;
		}


		PrimitiveClass* RuleSet::getPrimitiveClass(QString classLabel) {
			for (int i = 0; i < primitiveClasses.count(); i++) {
				if (primitiveClasses[i]->name == classLabel) return primitiveClasses[i];
			}
			PrimitiveClass* p = new PrimitiveClass(*defaultClass);
			p->name = classLabel;
			//INFO("Created new primitiveClass: " + classLabel);
			primitiveClasses.append(p);
			return p;
		}


	}
}