Guest User

Untitled

a guest
Feb 23rd, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.28 KB | None | 0 0
  1. package apt
  2.  
  3. import (
  4. "github.com/jackmott/noise"
  5. "math"
  6. "math/rand"
  7. "strconv"
  8. )
  9.  
  10. // + / * - Sin Cos Atan SimplexNoise X Y Constants...
  11. // Leaf Node (0 children)
  12. // Single Node (sin/cos)
  13. // BaseNode (+, -)
  14.  
  15. type Node interface {
  16. Eval(x, y float32) float32
  17. String() string
  18. SetParent(parent Node)
  19. GetParent() Node
  20. GetChildren() []Node
  21. AddRandom(node Node)
  22. AddLeaf(leaf Node) bool
  23. NodeCount() int
  24. }
  25.  
  26. type BaseNode struct {
  27. Parent Node
  28. Children []Node
  29. }
  30.  
  31. type OpLerp struct {
  32. BaseNode
  33. }
  34.  
  35. func GetNthNode(node Node, n, count int) (Node, int) {
  36. if n == count {
  37. return node, count
  38. }
  39. var result Node
  40. for _, child := range node.GetChildren() {
  41. count++
  42. result, count = GetNthNode(child, n, count)
  43. if result != nil {
  44. return result, count
  45. }
  46. }
  47. return nil, count
  48. }
  49.  
  50. func Mutate(node Node) Node {
  51. r := rand.Intn(23)
  52. var mutatedNode Node
  53. if r <= 19 {
  54. mutatedNode = GetRandomNode()
  55. } else {
  56. mutatedNode = GetRandomLeaf()
  57. }
  58.  
  59. // Fix up parents child pointer to point to the new node
  60. if node.GetParent() != nil {
  61. for i, parentChild := range node.GetParent().GetChildren() {
  62. if parentChild == node {
  63. node.GetParent().GetChildren()[i] = mutatedNode
  64. }
  65. }
  66. }
  67.  
  68. // Add children from the old node to the new mutated node
  69. for i, child := range node.GetChildren() {
  70. if i >= len(mutatedNode.GetChildren()) {
  71. break
  72. }
  73. mutatedNode.GetChildren()[i] = child
  74. child.SetParent(mutatedNode)
  75. }
  76.  
  77. // Any nil children get filled in with a random leaf
  78. for i, child := range mutatedNode.GetChildren() {
  79. if child == nil {
  80. leaf := GetRandomLeaf()
  81. leaf.SetParent(mutatedNode)
  82. mutatedNode.GetChildren()[i] = leaf
  83. }
  84. }
  85.  
  86. mutatedNode.SetParent(node.GetParent())
  87. return mutatedNode
  88. }
  89.  
  90. func (node *BaseNode) GetParent() Node {
  91. return node.Parent
  92. }
  93.  
  94. func (node *BaseNode) GetChildren() []Node {
  95. return node.Children
  96. }
  97.  
  98. func (node *BaseNode) NodeCount() int {
  99. count := 1
  100. for _, child := range node.Children {
  101. count += child.NodeCount()
  102. }
  103. return count
  104. }
  105.  
  106. func (node *BaseNode) Eval(x, y float32) float32 {
  107. panic("tried to call eval on basenode")
  108. }
  109.  
  110. func (node *BaseNode) String() string {
  111. panic("tried to call string on basenode")
  112. }
  113.  
  114. func (node *BaseNode) SetParent(parent Node) {
  115. node.Parent = parent
  116. }
  117.  
  118. func (node *BaseNode) AddRandom(nodeToAdd Node) {
  119. addIndex := rand.Intn(len(node.Children))
  120. if node.Children[addIndex] == nil {
  121. nodeToAdd.SetParent(node)
  122. node.Children[addIndex] = nodeToAdd
  123. } else {
  124. node.Children[addIndex].AddRandom(nodeToAdd)
  125. }
  126. }
  127.  
  128. func (node *BaseNode) AddLeaf(leaf Node) bool {
  129. for i, child := range node.Children {
  130. if child == nil {
  131. leaf.SetParent(node)
  132. node.Children[i] = leaf
  133. return true
  134. } else if node.Children[i].AddLeaf(leaf) {
  135. return true
  136. }
  137. }
  138. return false
  139. }
  140.  
  141. func NewOpLerp() *OpLerp {
  142. return &OpLerp{BaseNode{nil, make([]Node, 3)}}
  143. }
  144.  
  145. func (op *OpLerp) Eval(x, y float32) float32 {
  146.  
  147. a := op.Children[0].Eval(x, y)
  148. b := op.Children[1].Eval(x, y)
  149. pct := op.Children[2].Eval(x, y)
  150. return a + pct*(b-a)
  151. }
  152.  
  153. func (op *OpLerp) String() string {
  154. return "( Lerp " + op.Children[0].String() + " " + op.Children[1].String() + " " + op.Children[1].String() + " )"
  155. }
  156.  
  157. type OpClip struct {
  158. BaseNode
  159. }
  160.  
  161. func NewOpClip() *OpClip {
  162. return &OpClip{BaseNode{nil, make([]Node, 2)}}
  163. }
  164.  
  165. func (op *OpClip) Eval(x, y float32) float32 {
  166. value := op.Children[0].Eval(x, y)
  167. max := float32(math.Abs(float64(op.Children[1].Eval(x, y))))
  168. if value > max {
  169. return max
  170. } else if value < -max {
  171. return -max
  172. }
  173. return value
  174. }
  175.  
  176. func (op *OpClip) String() string {
  177. return "( Clip " + op.Children[0].String() + " " + op.Children[1].String() + " )"
  178. }
  179.  
  180. type OpWrap struct {
  181. BaseNode
  182. }
  183.  
  184. func NewOpWrap() *OpWrap {
  185. return &OpWrap{BaseNode{nil, make([]Node, 1)}}
  186. }
  187.  
  188. func (op *OpWrap) Eval(x, y float32) float32 {
  189. f := op.Children[0].Eval(x, y)
  190. temp := (f - -1.0) / (2.0)
  191. return -1.0 + 2.0*(temp-float32(math.Floor(float64(temp))))
  192. }
  193. func (op *OpWrap) String() string {
  194. return "(Wrap " + op.Children[0].String() + ")"
  195. }
  196.  
  197. type OpSin struct {
  198. BaseNode
  199. }
  200.  
  201. func NewOpSin() *OpSin {
  202. return &OpSin{BaseNode{nil, make([]Node, 1)}}
  203. }
  204.  
  205. func (op *OpSin) Eval(x, y float32) float32 {
  206. return float32(math.Sin(float64(op.Children[0].Eval(x, y))))
  207. }
  208.  
  209. func (op *OpSin) String() string {
  210. return "( Sin " + op.Children[0].String() + " )"
  211. }
  212.  
  213. type OpCos struct {
  214. BaseNode
  215. }
  216.  
  217. func NewOpCos() *OpCos {
  218. return &OpCos{BaseNode{nil, make([]Node, 1)}}
  219. }
  220.  
  221. func (op *OpCos) Eval(x, y float32) float32 {
  222. return float32(math.Cos(float64(op.Children[0].Eval(x, y))))
  223. }
  224.  
  225. func (op *OpCos) String() string {
  226. return "( Cos " + op.Children[0].String() + " )"
  227. }
  228.  
  229. type OpAtan struct {
  230. BaseNode
  231. }
  232.  
  233. func NewOpAtan() *OpAtan {
  234. return &OpAtan{BaseNode{nil, make([]Node, 1)}}
  235. }
  236.  
  237. func (op *OpAtan) Eval(x, y float32) float32 {
  238. return float32(math.Atan(float64(op.Children[0].Eval(x, y))))
  239. }
  240.  
  241. func (op *OpAtan) String() string {
  242. return "( Atan " + op.Children[0].String() + " )"
  243. }
  244.  
  245. type OpLog2 struct {
  246. BaseNode
  247. }
  248.  
  249. func NewOpLog2() *OpLog2 {
  250. return &OpLog2{BaseNode{nil, make([]Node, 1)}}
  251. }
  252.  
  253. func (op *OpLog2) Eval(x, y float32) float32 {
  254. return float32(math.Log2(float64(op.Children[0].Eval(x, y))))
  255. }
  256.  
  257. func (op *OpLog2) String() string {
  258. return "( Log2 " + op.Children[0].String() + " )"
  259. }
  260.  
  261. type OpSquare struct {
  262. BaseNode
  263. }
  264.  
  265. func NewOpSquare() *OpSquare {
  266. return &OpSquare{BaseNode{nil, make([]Node, 1)}}
  267. }
  268.  
  269. func (op *OpSquare) Eval(x, y float32) float32 {
  270. value := op.Children[0].Eval(x, y)
  271. return value * value
  272. }
  273.  
  274. func (op *OpSquare) String() string {
  275. return "( Square " + op.Children[0].String() + " )"
  276. }
  277.  
  278. type OpNegate struct {
  279. BaseNode
  280. }
  281.  
  282. func NewOpNegate() *OpNegate {
  283. return &OpNegate{BaseNode{nil, make([]Node, 1)}}
  284. }
  285.  
  286. func (op *OpNegate) Eval(x, y float32) float32 {
  287. return -op.Children[0].Eval(x, y)
  288. }
  289.  
  290. func (op *OpNegate) String() string {
  291. return "( Negate " + op.Children[0].String() + " )"
  292. }
  293.  
  294. type OpCeil struct {
  295. BaseNode
  296. }
  297.  
  298. func NewOpCeil() *OpCeil {
  299. return &OpCeil{BaseNode{nil, make([]Node, 1)}}
  300. }
  301.  
  302. func (op *OpCeil) Eval(x, y float32) float32 {
  303. return float32(math.Ceil(float64(op.Children[0].Eval(x, y))))
  304. }
  305.  
  306. func (op *OpCeil) String() string {
  307. return "( Ceil " + op.Children[0].String() + " )"
  308. }
  309.  
  310. type OpFloor struct {
  311. BaseNode
  312. }
  313.  
  314. func NewOpFloor() *OpFloor {
  315. return &OpFloor{BaseNode{nil, make([]Node, 1)}}
  316. }
  317.  
  318. func (op *OpFloor) Eval(x, y float32) float32 {
  319. return float32(math.Floor(float64(op.Children[0].Eval(x, y))))
  320. }
  321.  
  322. func (op *OpFloor) String() string {
  323. return "( Floor " + op.Children[0].String() + " )"
  324. }
  325.  
  326. type OpAbs struct {
  327. BaseNode
  328. }
  329.  
  330. func NewOpAbs() *OpAbs {
  331. return &OpAbs{BaseNode{nil, make([]Node, 1)}}
  332. }
  333.  
  334. func (op *OpAbs) Eval(x, y float32) float32 {
  335. return float32(math.Abs(float64(op.Children[0].Eval(x, y))))
  336. }
  337.  
  338. func (op *OpAbs) String() string {
  339. return "( Abs " + op.Children[0].String() + " )"
  340. }
  341.  
  342. type OpNoise struct {
  343. BaseNode
  344. }
  345.  
  346. func NewOpNoise() *OpNoise {
  347. return &OpNoise{BaseNode{nil, make([]Node, 2)}}
  348. }
  349.  
  350. func (op *OpNoise) Eval(x, y float32) float32 {
  351. return 80*noise.Snoise2(op.Children[0].Eval(x, y), op.Children[1].Eval(x, y)) - 2.0
  352. }
  353.  
  354. func (op *OpNoise) String() string {
  355. return "( SimplexNoise " + op.Children[0].String() + " " + op.Children[1].String() + " )"
  356. }
  357.  
  358. type OpFBM struct {
  359. BaseNode
  360. }
  361.  
  362. func NewOpFBM() *OpFBM {
  363. return &OpFBM{BaseNode{nil, make([]Node, 3)}}
  364. }
  365.  
  366. func (op *OpFBM) Eval(x, y float32) float32 {
  367. return 2*3.627*noise.Fbm2(op.Children[0].Eval(x, y), op.Children[1].Eval(x, y), 5*op.Children[2].Eval(x, y), 0.5, 2, 3) + .492 - 1
  368. }
  369.  
  370. func (op *OpFBM) String() string {
  371. return "( FBM " + op.Children[0].String() + " " + op.Children[1].String() + " " + op.Children[2].String() + " )"
  372. }
  373.  
  374. type OpTurbulence struct {
  375. BaseNode
  376. }
  377.  
  378. func NewOpTurbulence() *OpTurbulence {
  379. return &OpTurbulence{BaseNode{nil, make([]Node, 3)}}
  380. }
  381.  
  382. func (op *OpTurbulence) Eval(x, y float32) float32 {
  383. return 2*6.96*noise.Turbulence(op.Children[0].Eval(x, y), op.Children[1].Eval(x, y), 5*op.Children[2].Eval(x, y), 0.5, 2, 3) - 1
  384. }
  385.  
  386. func (op *OpTurbulence) String() string {
  387. return "( Turbulence " + op.Children[0].String() + " " + op.Children[1].String() + " " + op.Children[2].String() + " )"
  388. }
  389.  
  390. type OpPlus struct {
  391. BaseNode
  392. }
  393.  
  394. func NewOpPlus() *OpPlus {
  395. return &OpPlus{BaseNode{nil, make([]Node, 2)}}
  396. }
  397.  
  398. func (op *OpPlus) Eval(x, y float32) float32 {
  399. return op.Children[0].Eval(x, y) + op.Children[1].Eval(x, y)
  400. }
  401.  
  402. func (op *OpPlus) String() string {
  403. return "( + " + op.Children[0].String() + " " + op.Children[1].String() + " )"
  404. }
  405.  
  406. type OpMinus struct {
  407. BaseNode
  408. }
  409.  
  410. func NewOpMinus() *OpMinus {
  411. return &OpMinus{BaseNode{nil, make([]Node, 2)}}
  412. }
  413.  
  414. func (op *OpMinus) Eval(x, y float32) float32 {
  415. return op.Children[0].Eval(x, y) - op.Children[1].Eval(x, y)
  416. }
  417.  
  418. func (op *OpMinus) String() string {
  419. return "( - " + op.Children[0].String() + " " + op.Children[1].String() + " )"
  420. }
  421.  
  422. type OpMult struct {
  423. BaseNode
  424. }
  425.  
  426. func NewOpMult() *OpMult {
  427. return &OpMult{BaseNode{nil, make([]Node, 2)}}
  428. }
  429.  
  430. func (op *OpMult) Eval(x, y float32) float32 {
  431. return op.Children[0].Eval(x, y) * op.Children[1].Eval(x, y)
  432. }
  433.  
  434. func (op *OpMult) String() string {
  435. return "( * " + op.Children[0].String() + " " + op.Children[1].String() + " )"
  436. }
  437.  
  438. type OpDiv struct {
  439. BaseNode
  440. }
  441.  
  442. func NewOpDiv() *OpDiv {
  443. return &OpDiv{BaseNode{nil, make([]Node, 2)}}
  444. }
  445.  
  446. func (op *OpDiv) Eval(x, y float32) float32 {
  447. return op.Children[0].Eval(x, y) / op.Children[1].Eval(x, y)
  448. }
  449.  
  450. func (op *OpDiv) String() string {
  451. return "( / " + op.Children[0].String() + " " + op.Children[1].String() + " )"
  452. }
  453.  
  454. type OpAtan2 struct {
  455. BaseNode
  456. }
  457.  
  458. func NewOpAtan2() *OpAtan2 {
  459. return &OpAtan2{BaseNode{nil, make([]Node, 2)}}
  460. }
  461.  
  462. func (op *OpAtan2) Eval(x, y float32) float32 {
  463. return float32(math.Atan2(float64(op.Children[0].Eval(x, y)), float64(op.Children[1].Eval(x, y))))
  464. }
  465.  
  466. func (op *OpAtan2) String() string {
  467. return "( Atan2 " + op.Children[0].String() + " " + op.Children[1].String() + " )"
  468. }
  469.  
  470. type OpX struct {
  471. BaseNode
  472. }
  473.  
  474. func NewOpX() *OpX {
  475. return &OpX{BaseNode{nil, make([]Node, 0)}}
  476. }
  477.  
  478. func (op *OpX) Eval(x, y float32) float32 {
  479. return x
  480. }
  481.  
  482. func (op *OpX) String() string {
  483. return "X"
  484. }
  485.  
  486. type OpY struct {
  487. BaseNode
  488. }
  489.  
  490. func NewOpY() *OpY {
  491. return &OpY{BaseNode{nil, make([]Node, 0)}}
  492. }
  493.  
  494. func (op *OpY) Eval(x, y float32) float32 {
  495. return y
  496. }
  497.  
  498. func (op *OpY) String() string {
  499. return "Y"
  500. }
  501.  
  502. type OpConstant struct {
  503. BaseNode
  504. value float32
  505. }
  506.  
  507. func NewOpConstant() *OpConstant {
  508. return &OpConstant{BaseNode{nil, make([]Node, 0)}, rand.Float32()*2 - 1}
  509. }
  510.  
  511. func (op *OpConstant) Eval(x, y float32) float32 {
  512. return op.value
  513. }
  514.  
  515. func (op *OpConstant) String() string {
  516. return strconv.FormatFloat(float64(op.value), 'f', 9, 32)
  517. }
  518.  
  519. func GetRandomNode() Node {
  520. r := rand.Intn(20)
  521. switch r {
  522. case 0:
  523. return NewOpPlus()
  524. case 1:
  525. return NewOpMinus()
  526. case 2:
  527. return NewOpMult()
  528. case 3:
  529. return NewOpDiv()
  530. case 4:
  531. return NewOpAtan2()
  532. case 5:
  533. return NewOpAtan()
  534. case 6:
  535. return NewOpCos()
  536. case 7:
  537. return NewOpSin()
  538. case 8:
  539. return NewOpNoise()
  540. case 9:
  541. return NewOpSquare()
  542. case 10:
  543. return NewOpLog2()
  544. case 11:
  545. return NewOpNegate()
  546. case 12:
  547. return NewOpCeil()
  548. case 13:
  549. return NewOpFloor()
  550. case 14:
  551. return NewOpLerp()
  552. case 15:
  553. return NewOpAbs()
  554. case 16:
  555. return NewOpClip()
  556. case 17:
  557. return NewOpWrap()
  558. case 18:
  559. return NewOpFBM()
  560. case 19:
  561. return NewOpTurbulence()
  562. }
  563. panic("Get Random Noise Failed")
  564. }
  565.  
  566. func GetRandomLeaf() Node {
  567. r := rand.Intn(3)
  568. switch r {
  569. case 0:
  570. return NewOpX()
  571. case 1:
  572. return NewOpY()
  573. case 2:
  574. return NewOpConstant()
  575. }
  576. panic("Error in get random Leaf")
  577. }
Add Comment
Please, Sign In to add comment