-
Notifications
You must be signed in to change notification settings - Fork 14
/
lindenmayer.js
439 lines (368 loc) · 15.4 KB
/
lindenmayer.js
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import './polyfills/objectEntries'
import {
transformClassicStochasticProductions,
transformClassicCSProduction,
transformClassicParametricAxiom,
testClassicParametricSyntax
} from './transformersClassicSyntax';
import {stringToObjects, normalizeProduction} from './transformers';
export default class LSystem {
constructor({
axiom = '',
productions,
finals,
branchSymbols = '[]',
ignoredSymbols = '+-&^/|\\',
allowClassicSyntax = true,
classicParametricSyntax = false,
forceObjects = false,
debug = false
}) {
this.ignoredSymbols = ignoredSymbols;
this.debug = debug;
this.branchSymbols = branchSymbols;
this.allowClassicSyntax = allowClassicSyntax;
this.classicParametricSyntax = classicParametricSyntax;
this.forceObjects = forceObjects;
this.setAxiom(axiom);
this.clearProductions();
if (productions) this.setProductions(productions);
if (finals) this.setFinals(finals);
}
// TODO: forceObject to be more intelligent based on other productions??
setAxiom(axiom) {
this.axiom = (this.forceObjects) ? stringToObjects(axiom) : axiom;
}
getRaw() {
return this.axiom;
}
// if using objects in axioms, as used in parametric L-Systems
getString(onlySymbols = true) {
if (typeof this.axiom === 'string') return this.axiom;
if (onlySymbols === true) {
return this.axiom.reduce((prev, current) => {
if (current.symbol === undefined) {
console.log('found:', current);
throw new Error('L-Systems that use only objects as symbols (eg: {symbol: \'F\', params: []}), cant use string symbols (eg. \'F\')! Check if you always return objects in your productions and no strings.');
}
return prev + current.symbol;
}, '');
} else {
return JSON.stringify(this.axiom);
}
}
setProduction(from, to, allowAppendingMultiSuccessors = false) {
let newProduction = [from, to];
if (newProduction === undefined) {
throw new Error('no production specified.');
}
if (to.successor && to.successors) {
throw new Error('You can not have both a "successor" and a "successors" field in your production!');
}
// Apply production transformers and normalizations
if (this.allowClassicSyntax === true) {
newProduction = transformClassicCSProduction(newProduction);
}
newProduction = normalizeProduction(newProduction, this.forceObjects);
// check wether production is stochastic
newProduction[1].isStochastic = newProduction[1].successors !== undefined && newProduction[1].successors.every(successor => successor.weight !== undefined);
if (newProduction[1].isStochastic) {
// calculate weight sum
newProduction[1].weightSum = 0;
for (let s of newProduction[1].successors) {
newProduction[1].weightSum += s.weight;
}
}
let symbol = newProduction[0];
if (allowAppendingMultiSuccessors === true && this.productions.has(symbol)) {
let existingProduction = this.productions.get(symbol);
let singleSuccessor = existingProduction.successor;
let multiSuccessors = existingProduction.successors;
if (singleSuccessor && !multiSuccessors) {
// replace existing prod with new obj and add previous successor as first elem
// to new successors field.
existingProduction = {successors: [existingProduction]};
}
existingProduction.successors.push(newProduction[1]);
this.productions.set(symbol, existingProduction);
} else {
this.productions.set(symbol, newProduction[1]);
}
}
// set multiple productions from name:value Object
setProductions(newProductions) {
if (newProductions === undefined) throw new Error('no production specified.');
this.clearProductions();
for (let [from, to] of Object.entries(newProductions)) {
this.setProduction(from, to, true);
}
}
clearProductions() {
this.productions = new Map();
}
setFinal(symbol, final) {
let newFinal = [symbol, final];
if (newFinal === undefined) {
throw new Error('no final specified.');
}
this.finals.set(newFinal[0], newFinal[1]);
}
// set multiple finals from name:value Object
setFinals(newFinals) {
if (newFinals === undefined) throw new Error('no finals specified.');
this.finals = new Map();
for (let symbol in newFinals) {
if (newFinals.hasOwnProperty(symbol)) {
this.setFinal(symbol, newFinals[symbol]);
}
}
}
//var hasWeight = el => el.weight !== undefined;
getProductionResult(p, index, part, params, recursive = false) {
let contextSensitive = (p.leftCtx !== undefined || p.rightCtx !== undefined);
let conditional = p.condition !== undefined;
let stochastic = false;
let result = false;
let precheck = true;
// Check if condition is true, only then continue to check left and right contexts
if (conditional && p.condition({index, currentAxiom: this.axiom, part, params}) === false) {
precheck = false;
}
else if (contextSensitive) {
if (p.leftCtx !== undefined && p.rightCtx !== undefined) {
precheck = this.match({
direction: 'left',
match: p.leftCtx,
index: index,
branchSymbols: this.branchSymbols,
ignoredSymbols: this.ignoredSymbols
}).result && this.match({
direction: 'right',
match: p.rightCtx,
index: index,
branchSymbols: this.branchSymbols,
ignoredSymbols: this.ignoredSymbols
}).result;
} else if (p.leftCtx !== undefined) {
precheck = this.match({
direction: 'left',
match: p.leftCtx,
index: index,
branchSymbols: this.branchSymbols,
ignoredSymbols: this.ignoredSymbols
}).result;
} else if (p.rightCtx !== undefined) {
precheck = this.match({
direction: 'right',
match: p.rightCtx,
index: index,
branchSymbols: this.branchSymbols,
ignoredSymbols: this.ignoredSymbols
}).result;
}
}
// If conditions and context don't allow product, keep result = false
if (precheck === false) {
result = false;
}
// If p has multiple successors
else if (p.successors) {
// This could be stochastic successors or multiple functions
// Treat every element in the list as an individual production object
// For stochastic productions (if all prods in the list have a 'weight' property)
// Get a random number then pick a production from the list according to their weight
let currentWeight, threshWeight;
if (p.isStochastic) {
threshWeight = Math.random() * p.weightSum;
currentWeight = 0;
}
/*
go through the list and use
the first valid production in that list. (that returns true)
This assumes, it's a list of functions.
No recursion here: no successors inside successors.
*/
for (let _p of p.successors) {
if (p.isStochastic) {
currentWeight += _p.weight;
if (currentWeight < threshWeight) continue;
}
// If currentWeight >= thresWeight, a production is choosen stochastically
// and evaluated recursively because it , kax also have rightCtx, leftCtx and condition to further inhibit production. This is not standard L-System behaviour though!
// last true is for recursiv call
// TODO: refactor getProductionResult to use an object if not a hit on perf
let _result = this.getProductionResult(_p, index, part, params, true);
if (_result !== undefined && _result !== false) {
result = _result;
break;
}
}
}
// if successor is a function, execute function and append return value
else if (typeof p.successor === 'function') {
result = p.successor({index, currentAxiom: this.axiom, part, params});
} else {
result = p.successor;
}
if (!result) {
// Allow undefined or false results for recursive calls of this func
return recursive ? result : part;
}
return result;
}
applyProductions() {
// a axiom can be a string or an array of objects that contain the key/value 'symbol'
let newAxiom = (typeof this.axiom === 'string') ? '' : [];
let index = 0;
// iterate all symbols/characters of the axiom and lookup according productions
for (let part of this.axiom) {
// Stuff for classic parametric L-Systems: get actual symbol and possible parameters
// params will be given the production function, if applicable.
let symbol = part.symbol || part;
let params = part.params || [];
let result = part;
if (this.productions.has(symbol)) {
let p = this.productions.get(symbol);
result = this.getProductionResult(p, index, part, params);
}
// Got result. Now add result to new axiom.
if (typeof newAxiom === 'string') {
newAxiom += result;
} else if (result instanceof Array) {
// If result is an array, merge result into new axiom instead of pushing.
newAxiom.push(...result);
} else {
newAxiom.push(result);
}
index++;
}
// finally set new axiom and also return it for convenience.
this.axiom = newAxiom;
return newAxiom;
}
iterate(n = 1) {
this.iterations = n;
let lastIteration;
for (let iteration = 0; iteration < n; iteration++) {
lastIteration = this.applyProductions();
}
return lastIteration;
}
final(externalArg) {
let index = 0;
for (let part of this.axiom) {
// if we have objects for each symbol, (when using parametric L-Systems)
// get actual identifiable symbol character
let symbol = part;
if (typeof part === 'object' && part.symbol) symbol = part.symbol;
if (this.finals.has(symbol)) {
let finalFunction = this.finals.get(symbol);
let typeOfFinalFunction = typeof finalFunction;
if ((typeOfFinalFunction !== 'function')) {
throw Error('\'' + symbol + '\'' + ' has an object for a final function. But it is __not a function__ but a ' + typeOfFinalFunction + '!');
}
// execute symbols function
// supply in first argument an details object with current index and part
// and in the first argument inject the external argument (like a render target)
finalFunction({index, part}, externalArg);
} else {
// symbol has no final function
}
index++;
}
}
/*
how to use match():
-----------------------
It is mainly a helper function for context sensitive productions.
If you use the classic syntax, it will by default be automatically transformed to proper
JS-Syntax.
Howerver, you can use the match helper function in your on productions:
index is the index of a production using `match`
eg. in a classic L-System
LSYS = ABCDE
B<C>DE -> 'Z'
the index of the `B<C>D -> 'Z'` production would be the index of C (which is 2) when the
production would perform match(). so (if not using the ClassicLSystem class) you'd construction your context-sensitive production from C to Z like so:
LSYS.setProduction('C', (index, axiom) => {
(LSYS.match({index, match: 'B', direction: 'left'}) &&
LSYS.match({index, match: 'DE', direction: 'right'}) ? 'Z' : 'C')
})
You can just write match({index, ...} instead of match({index: index, ..}) because of new ES6 Object initialization, see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#New_notations_in_ECMAScript_6
*/
match({axiom_, match, ignoredSymbols, branchSymbols, index, direction}) {
let branchCount = 0;
let explicitBranchCount = 0;
axiom_ = axiom_ || this.axiom;
if (branchSymbols === undefined) branchSymbols = (this.branchSymbols !== undefined) ? this.branchSymbols : [];
if (ignoredSymbols === undefined) ignoredSymbols = (this.ignoredSymbols !== undefined) ? this.ignoredSymbols : [];
let returnMatchIndices = [];
let branchStart, branchEnd, axiomIndex, loopIndexChange, matchIndex, matchIndexChange, matchIndexOverflow;
// set some variables depending on the direction to match
if (direction === 'right') {
loopIndexChange = matchIndexChange = +1;
axiomIndex = index + 1;
matchIndex = 0;
matchIndexOverflow = match.length;
if (branchSymbols.length > 0) [branchStart, branchEnd] = branchSymbols;
} else if (direction === 'left') {
loopIndexChange = matchIndexChange = -1;
axiomIndex = index - 1;
matchIndex = match.length - 1;
matchIndexOverflow = -1;
if (branchSymbols.length > 0) [branchEnd, branchStart] = branchSymbols;
} else {
throw Error(direction, 'is not a valid direction for matching.');
}
for (; axiomIndex < axiom_.length && axiomIndex >= 0; axiomIndex += loopIndexChange) {
let axiomSymbol = axiom_[axiomIndex].symbol || axiom_[axiomIndex];
let matchSymbol = match[matchIndex];
// compare current symbol of axiom with current symbol of match
if (axiomSymbol === matchSymbol) {
if (branchCount === 0 || explicitBranchCount > 0) {
// if its a match and previously NOT inside branch (branchCount===0) or in explicitly wanted branch (explicitBranchCount > 0)
// if a bracket was explicitly stated in match axiom
if (axiomSymbol === branchStart) {
explicitBranchCount++;
branchCount++;
matchIndex += matchIndexChange;
} else if (axiomSymbol === branchEnd) {
explicitBranchCount = Math.max(0, explicitBranchCount - 1);
branchCount = Math.max(0, branchCount - 1);
// only increase match if we are out of explicit branch
if (explicitBranchCount === 0) {
matchIndex += matchIndexChange;
}
} else {
returnMatchIndices.push(axiomIndex);
matchIndex += matchIndexChange;
}
}
// overflowing matchIndices (matchIndex + 1 for right match, matchIndexEnd for left match )?
// -> no more matches to do. return with true, as everything matched until here
// *yay*
if (matchIndex === matchIndexOverflow) {
return {result: true, matchIndices: returnMatchIndices};
}
} else if (axiomSymbol === branchStart) {
branchCount++;
if (explicitBranchCount > 0) explicitBranchCount++;
} else if (axiomSymbol === branchEnd) {
branchCount = Math.max(0, branchCount - 1);
if (explicitBranchCount > 0) explicitBranchCount = Math.max(0, explicitBranchCount - 1);
} else if ((branchCount === 0 || (explicitBranchCount > 0 && matchSymbol !== branchEnd)) && ignoredSymbols.includes(axiomSymbol) === false) {
// not in branchSymbols/branch? or if in explicit branch, and not at the very end of
// condition (at the ]), and symbol not in ignoredSymbols ? then false
return {result: false, matchIndices: returnMatchIndices};
}
}
return {result: false, matchIndices: returnMatchIndices};
}
}
LSystem.getStringResult = LSystem.getString;
// Set classic syntax helpers to library scope to be used outside of library context
// for users eg.
LSystem.transformClassicStochasticProductions = transformClassicStochasticProductions;
LSystem.transformClassicCSProduction = transformClassicCSProduction;
LSystem.transformClassicParametricAxiom = transformClassicParametricAxiom;
LSystem.testClassicParametricSyntax = testClassicParametricSyntax;