turtlelsys.vwf.yaml 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. # Turtle for L-System
  2. ---
  3. extends: http://vwf.example.com/aframe/asphere.vwf
  4. type: "turtle"
  5. properties:
  6. position: [1, 1.25, -4]
  7. color: "#e0e014"
  8. radius: 0.3
  9. wireframe: true
  10. angleInRadians: 0
  11. counter: 1
  12. iteration:
  13. set: |
  14. this.iteration = value;
  15. this.makeLSys();
  16. value: 3
  17. angle:
  18. set: |
  19. this.angle = value;
  20. this.makeLSys();
  21. value: 60
  22. stepLength:
  23. set: |
  24. this.stepLength = value;
  25. this.makeLSys();
  26. value: 0.5
  27. rule:
  28. set: |
  29. this.rule = value;
  30. this.makeLSys();
  31. value: 'F++F++F'
  32. axiomF:
  33. set: |
  34. this.axiomF = value;
  35. this.makeLSys();
  36. value: 'F-F++F-F'
  37. axiomG:
  38. set: |
  39. this.axiomG = value;
  40. this.makeLSys();
  41. value: ''
  42. lsys: ''
  43. ready: false
  44. children:
  45. drawNode:
  46. extends: http://vwf.example.com/aframe/aentity.vwf
  47. lsysLang:
  48. extends: http://vwf.example.com/ohm/node.vwf
  49. properties:
  50. grammar:
  51. semantics:
  52. ohmLang: |
  53. LSys { Gen<x>
  54. = ReadRule+
  55. ReadRule
  56. = letters | symbols
  57. letters = "F" | "G"
  58. symbols = "-" | "+" }
  59. methods:
  60. initLang:
  61. body: |
  62. console.log("add operations to semantics")
  63. this.addOperationLang();
  64. addOperationLang:
  65. body: |
  66. this.semantics.addOperation('gen(x)', {
  67. Gen: function(e)
  68. {
  69. return e.gen(this.args.x);
  70. },
  71. ReadRule: function(e)
  72. {
  73. return e.gen(this.args.x);
  74. },
  75. letters: function(_)
  76. {
  77. for (var propName in this.args.x)
  78. {
  79. if (propName == this.sourceString)
  80. return this.args.x[propName]
  81. }
  82. return this.sourceString
  83. },
  84. symbols: function(_)
  85. {
  86. return this.sourceString;
  87. }
  88. });
  89. turtleLang:
  90. extends: http://vwf.example.com/ohm/node.vwf
  91. properties:
  92. grammar:
  93. semantics:
  94. ohmLang: |
  95. Turtle {
  96. Draw<x, y>
  97. = (drawLetter | turn)+
  98. drawLetter
  99. = letter
  100. turn
  101. = "+" | "-" }
  102. methods:
  103. initLang:
  104. body: |
  105. console.log("add operations to semantics")
  106. this.addOperationLang();
  107. addOperationLang:
  108. body: |
  109. var turtleID = this.parent.id;
  110. var self = this;
  111. this.semantics.addOperation('draw(x,y)', {
  112. Draw: function(e)
  113. {
  114. e.draw(this.args.x, this.args.y);
  115. },
  116. drawLetter: function(e)
  117. {
  118. //vwf_view.kernel.callMethod(turtleID, 'goForward', [this.args.x]);
  119. self.parent.goForward(this.args.x);
  120. },
  121. turn: function(e)
  122. {
  123. if (this.sourceString == "+")
  124. //vwf_view.kernel.callMethod(turtleID, 'turn', [this.args.y]);
  125. self.parent.turn(this.args.y);
  126. if (this.sourceString == "-")
  127. // vwf_view.kernel.callMethod(turtleID, 'turn', [-1 * this.args.y]);
  128. self.parent.turn(-1*this.args.y);
  129. }
  130. });
  131. methods:
  132. clearDraw: |
  133. let self = this
  134. let drawDef = {
  135. "extends": "http://vwf.example.com/aframe/aentity.vwf",
  136. }
  137. this.children.delete(this.drawNode);
  138. this.children.create("drawNode", drawDef);
  139. this.angleInRadians = 0
  140. //this.drawNode.children.forEach(el => {
  141. // self.drawNode.children.delete(el)
  142. //})
  143. parseLSys: |
  144. var str = this.rule;
  145. var axioms = {"F": this.axiomF, "G": this.axiomG};
  146. for (var i = 1; i < this.iteration; i++)
  147. {
  148. var match = this.lsysLang.grammar.match(str, 'Gen<"y">');
  149. if (match.succeeded()){
  150. var res = this.lsysLang.semantics(match).gen(axioms);
  151. str = res.join("");
  152. }
  153. }
  154. console.log(str);
  155. this.lsys = str;
  156. makeLSys: |
  157. if (this.ready)
  158. {
  159. this.clearDraw();
  160. this.parseLSys();
  161. this.future(0.1).drawLSys();
  162. }
  163. drawLSys: |
  164. var match = this.turtleLang.grammar.match(this.lsys, 'Draw<"1","1">');
  165. if (match.succeeded()){
  166. var res = this.turtleLang.semantics(match).draw(this.stepLength, this.angle);
  167. }
  168. makeMe:
  169. parameters:
  170. - childName
  171. - startPosition
  172. - endPosition
  173. body: |
  174. //let nodeId = this.drawNode.id;
  175. var childComponent = {
  176. "extends": "http://vwf.example.com/aframe/lineComponent.vwf",
  177. "type": "component",
  178. "properties": {
  179. "start": startPosition,
  180. "end": endPosition,
  181. "color": "green"
  182. }
  183. }
  184. //vwf_view.kernel.createChild(this.id, childName, childComponent);
  185. this.drawNode.children.create(childName, childComponent);
  186. turn:
  187. parameters:
  188. - angle
  189. body: |
  190. var angle0 = this.angleInRadians;
  191. var targetAngle = angle * Math.PI / 180.0;
  192. this.angleInRadians = angle0 + targetAngle;
  193. goForward:
  194. parameters:
  195. - step
  196. body: |
  197. let pos = AFRAME.utils.coordinates.parse(this.drawNode.position);
  198. var x0 = pos.x;
  199. var y0 = pos.y;
  200. var xx = Math.sin(this.angleInRadians);
  201. var yy = Math.cos(this.angleInRadians);
  202. let endPosition = AFRAME.utils.coordinates.stringify({x: x0 + step * xx, y: y0 + step * yy, z: pos.z});
  203. this.counter = this.counter + 1;
  204. this.makeMe('line__' + this.counter, this.drawNode.position, endPosition);
  205. this.drawNode.position = endPosition;
  206. initialize:
  207. body: |
  208. this.ready = true;
  209. console.log("initialising turtle");
  210. //vwf_view.kernel.callMethod(this.id, "testDrawLsys");