File: AmbiguousRule.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 (52 lines) | stat: -rw-r--r-- 1,163 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
#include "AmbiguousRule.h"

#include "Builder.h"
#include "RandomStreams.h"

#include "../../SyntopiaCore/Logging/Logging.h"

using namespace SyntopiaCore::Logging;


namespace StructureSynth {
	namespace Model {	

		QList<RuleRef*> AmbiguousRule::getRuleRefs() const {
			QList<RuleRef*>  list;
			for (int i = 0; i < rules.size(); i++) {
				for (int j = 0; j < rules[i]->getRuleRefs().size(); j++) {
					list.append(rules[i]->getRuleRefs()[j]);
				}
			}
			return list;
		}

		void AmbiguousRule::apply(Builder* builder) const {
			// Calc sum of weigths
			double totalWeight = 0;
			for (int i = 0; i < rules.size(); i++) {
				totalWeight += rules[i]->getWeight();
			}


			double random = totalWeight*RandomStreams::Geometry()->getDouble();


			// Choose a random rule according to weights
			double accWeight = 0;
			for (int i = 0; i < rules.size(); i++) {
				accWeight += rules[i]->getWeight();
				if (random <= accWeight) {
					rules[i]->apply(builder);
					return;
				}
			}
			rules[rules.size()-1]->apply(builder);

			WARNING("Assertion failed: in AmbiguousRule::apply");

		};
	
	}
}