GeometrizeLib

Turn your images into geometric primitives!

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/521159/1615943/GeometrizeLib.js

  1. (function ($hx_exports, $global, worker_code) {
  2. "use strict";
  3. $hx_exports["geometrize"] = $hx_exports["geometrize"] || {};
  4. $hx_exports["geometrize"]["bitmap"] = $hx_exports["geometrize"]["bitmap"] || {};
  5. $hx_exports["geometrize"]["exporter"] = $hx_exports["geometrize"]["exporter"] || {};
  6. $hx_exports["geometrize"]["runner"] = $hx_exports["geometrize"]["runner"] || {};
  7. $hx_exports["geometrize"]["shape"] = $hx_exports["geometrize"]["shape"] || {};
  8. function $extend(from, fields) {
  9. var proto = Object.create(from);
  10. for (var name in fields) proto[name] = fields[name];
  11. if (fields.toString !== Object.prototype.toString) proto.toString = fields.toString;
  12. return proto;
  13. }
  14. var CanvasTools = function () {};
  15. CanvasTools.__name__ = true;
  16. CanvasTools.downScaleCanvas = function (cv, scale) {
  17. if (scale <= 0.0 || scale >= 1.0) {
  18. throw haxe_Exception.thrown("Scale must be a positive number < 1");
  19. }
  20. var sqScale = scale * scale;
  21. var sw = cv.width;
  22. var sh = cv.height;
  23. var tw = (sw * scale) | 0;
  24. var th = (sh * scale) | 0;
  25. var sx = 0;
  26. var sy = 0;
  27. var sIndex = 0;
  28. var tx = 0;
  29. var ty = 0;
  30. var yIndex = 0;
  31. var tIndex = 0;
  32. var tX = 0;
  33. var tY = 0;
  34. var w = 0.0;
  35. var nw = 0.0;
  36. var wx = 0.0;
  37. var nwx = 0.0;
  38. var wy = 0.0;
  39. var nwy = 0.0;
  40. var crossX = false;
  41. var crossY = false;
  42. var sBuffer = cv.getContext("2d").getImageData(0, 0, sw, sh).data;
  43. var this1 = new Float32Array(3 * tw * th);
  44. var tBuffer = this1;
  45. var sR = 0.0;
  46. var sG = 0.0;
  47. var sB = 0.0;
  48. while (sy < sh) {
  49. ty = (sy * scale) | 0;
  50. tY = ty | 0;
  51. yIndex = (3 * tY * tw) | 0;
  52. crossY = tY != ((ty + scale) | 0);
  53. if (crossY) {
  54. wy = tY + 1 - ty;
  55. nwy = ty + scale - tY - 1;
  56. }
  57. sx = 0;
  58. while (sx < sw) {
  59. tx = (sx * scale) | 0;
  60. tX = tx | 0;
  61. tIndex = (yIndex + tX * 3) | 0;
  62. crossX = tX != Math.floor(tx + scale);
  63. if (crossX) {
  64. wx = tX + 1 - tx;
  65. nwx = (tx + scale - tX - 1) | 0;
  66. }
  67. sR = sBuffer[sIndex];
  68. sG = sBuffer[sIndex + 1];
  69. sB = sBuffer[sIndex + 2];
  70. if (!crossX && !crossY) {
  71. tBuffer[tIndex] += sR * sqScale;
  72. tBuffer[tIndex + 1] += sG * sqScale;
  73. tBuffer[tIndex + 2] += sB * sqScale;
  74. } else if (crossX && !crossY) {
  75. w = wx * scale;
  76. tBuffer[tIndex] += sR * w;
  77. tBuffer[tIndex + 1] += sG * w;
  78. tBuffer[tIndex + 2] += sB * w;
  79. nw = nwx * scale;
  80. tBuffer[tIndex + 3] += sR * nw;
  81. tBuffer[tIndex + 4] += sG * nw;
  82. tBuffer[tIndex + 5] += sB * nw;
  83. } else if (crossY && !crossX) {
  84. w = wy * scale;
  85. tBuffer[tIndex] += sR * w;
  86. tBuffer[tIndex + 1] += sG * w;
  87. tBuffer[tIndex + 2] += sB * w;
  88. nw = nwy * scale;
  89. tBuffer[tIndex + 3 * tw] += sR * nw;
  90. tBuffer[tIndex + 3 * tw + 1] += sG * nw;
  91. tBuffer[tIndex + 3 * tw + 2] += sB * nw;
  92. } else {
  93. w = wx * wy;
  94. tBuffer[tIndex] += sR * w;
  95. tBuffer[tIndex + 1] += sG * w;
  96. tBuffer[tIndex + 2] += sB * w;
  97. nw = nwx * wy;
  98. tBuffer[tIndex + 3] += sR * nw;
  99. tBuffer[tIndex + 4] += sG * nw;
  100. tBuffer[tIndex + 5] += sB * nw;
  101. nw = wx * nwy;
  102. tBuffer[tIndex + 3 * tw] += sR * nw;
  103. tBuffer[tIndex + 3 * tw + 1] += sG * nw;
  104. tBuffer[tIndex + 3 * tw + 2] += sB * nw;
  105. nw = nwx * nwy;
  106. tBuffer[tIndex + 3 * tw + 3] += sR * nw;
  107. tBuffer[tIndex + 3 * tw + 4] += sG * nw;
  108. tBuffer[tIndex + 3 * tw + 5] += sB * nw;
  109. }
  110. sIndex += 4;
  111. ++sx;
  112. }
  113. ++sy;
  114. }
  115. var result = window.document.createElement("canvas");
  116. result.width = tw;
  117. result.height = th;
  118. var resultContext = result.getContext("2d");
  119. var resultImage = resultContext.getImageData(0, 0, tw, th);
  120. var tByteBuffer = resultImage.data;
  121. var pxIndex = 0;
  122. sIndex = 0;
  123. tIndex = 0;
  124. while (pxIndex < tw * th) {
  125. tByteBuffer[tIndex] = Math.ceil(tBuffer[sIndex]);
  126. tByteBuffer[tIndex + 1] = Math.ceil(tBuffer[sIndex + 1]);
  127. tByteBuffer[tIndex + 2] = Math.ceil(tBuffer[sIndex + 2]);
  128. tByteBuffer[tIndex + 3] = 255;
  129. sIndex += 3;
  130. tIndex += 4;
  131. ++pxIndex;
  132. }
  133. resultContext.putImageData(resultImage, 0, 0);
  134. return result;
  135. };
  136. var GeometrizeWorkerInterface = function () {
  137. var _gthis = this;
  138. // this.worker = new Worker("js/geometrize_worker.js");
  139. // this.worker = new Worker("https://www.samcodes.co.uk/project/geometrize-haxe-web/js/geometrize_worker.js");
  140. // this.worker = new Worker("/userscripts/geometrize_worker.js");
  141. this.worker = new Worker(window.URL.createObjectURL(new Blob([worker_code], { type: "text/javascript" })));
  142. console.log(this.worker);
  143. this.worker.onmessage = function (message) {
  144. _gthis.onMessage(message.data);
  145. };
  146. };
  147. GeometrizeWorkerInterface.__name__ = true;
  148. GeometrizeWorkerInterface.prototype = {
  149. postMessage: function (message) {
  150. this.worker.postMessage(message);
  151. },
  152. terminate: function () {
  153. this.worker.terminate();
  154. },
  155. onMessage: function (message) {},
  156. };
  157. var HxOverrides = function () {};
  158. HxOverrides.__name__ = true;
  159. HxOverrides.remove = function (a, obj) {
  160. var i = a.indexOf(obj);
  161. if (i == -1) {
  162. return false;
  163. }
  164. a.splice(i, 1);
  165. return true;
  166. };
  167. HxOverrides.now = function () {
  168. return Date.now();
  169. };
  170. var Main = function () {
  171. this.targetImage = null;
  172. this.shapeJsonData = [];
  173. this.shapeSvgData = [];
  174. this.shapeMutationsPerStep = 100;
  175. this.candidateShapesPerStep = 50;
  176. this.initialBackgroundOpacity = 255;
  177. this.shapeOpacity = 128;
  178. this.shapeTypes = geometrize_ArraySet.create([5, 6]);
  179. this.maxInputImageSize = 768;
  180. window.addEventListener("GeometrizeInitialize", $bind(this, this.onWindowLoaded));
  181. // window.onload = $bind(this, this.onWindowLoaded);
  182. };
  183. Main.__name__ = true;
  184. Main.main = function () {
  185. var main = new Main();
  186. };
  187. Main.prototype = {
  188. get_shapeCount: function () {
  189. return this.shapeSvgData.length;
  190. },
  191. get_maxShapeCountLimit: function () {
  192. var text = Main.maxShapesCapTextEdit.value;
  193. var value = Std.parseInt(text);
  194. if (value != null) {
  195. return value;
  196. }
  197. return 3000;
  198. },
  199. set_maxShapeCountLimit: function (limit) {
  200. Main.maxShapesCapTextEdit.value = limit == null ? "null" : "" + limit;
  201. return limit;
  202. },
  203. onWindowLoaded: function () {
  204. var _gthis = this;
  205. Main.runPauseButton = window.document.getElementById("runpausebutton");
  206. Main.stepButton = window.document.getElementById("stepbutton");
  207. Main.openImageFileInput = window.document.getElementById("openimageinput");
  208. Main.randomImageButton = window.document.getElementById("randomimagebutton");
  209. Main.resetButton = window.document.getElementById("resetbutton");
  210. Main.saveImageButton = window.document.getElementById("saveimagebutton");
  211. Main.saveSvgButton = window.document.getElementById("savesvgbutton");
  212. Main.saveJsonButton = window.document.getElementById("savejsonbutton");
  213. Main.rectanglesCheckbox = window.document.getElementById("rectangles");
  214. Main.rotatedRectanglesCheckbox = window.document.getElementById("rotatedrectangles");
  215. Main.trianglesCheckbox = window.document.getElementById("triangles");
  216. Main.ellipsesCheckbox = window.document.getElementById("ellipses");
  217. Main.rotatedEllipsesCheckbox = window.document.getElementById("rotatedellipses");
  218. Main.circlesCheckbox = window.document.getElementById("circles");
  219. Main.linesCheckbox = window.document.getElementById("lines");
  220. Main.quadraticBeziersCheckbox = window.document.getElementById("quadraticbeziers");
  221. Main.shapeOpacitySlider = window.document.getElementById("shapeopacity");
  222. Main.initialBackgroundOpacitySlider = window.document.getElementById("initialbackgroundopacity");
  223. Main.randomShapesPerStepSlider = window.document.getElementById("randomshapesperstep");
  224. Main.shapeMutationsPerStepSlider = window.document.getElementById("shapemutationsperstep");
  225. Main.shapesAddedText = window.document.getElementById("shapesaddedtext");
  226. Main.maxShapesCapTextEdit = window.document.getElementById("maxshapescaptextedit");
  227. Main.currentSvgContainer = window.document.getElementById("currentsvgcontainer");
  228. Main.sampleImagesContainer = window.document.getElementById("sampleimages");
  229. Main.defaultImageElement = window.document.getElementById("defaultimage");
  230.  
  231. _gthis.shapeOpacity = 255;
  232. _gthis.initialBackgroundOpacity = 0;
  233.  
  234. var _gthis1 = this;
  235. Main.runPauseButton.addEventListener(
  236. "click",
  237. function () {
  238. _gthis1.set_running(!_gthis1.running);
  239. },
  240. false
  241. );
  242. Main.openImageFileInput.addEventListener(
  243. "change",
  244. function (e) {
  245. if (Main.openImageFileInput.files == null || Main.openImageFileInput.files.length == 0) {
  246. return;
  247. }
  248. var file = Main.openImageFileInput.files[0];
  249. var fileReader = new FileReader();
  250. fileReader.onload = function (e) {
  251. var image = new Image();
  252. image.onload = function (e) {
  253. var canvas = _gthis1.imageToCanvas(image);
  254. while (canvas.width > _gthis1.maxInputImageSize || canvas.height > _gthis1.maxInputImageSize) canvas = CanvasTools.downScaleCanvas(canvas, 0.5);
  255. _gthis1.targetImage = _gthis1.canvasToBitmap(canvas);
  256. _gthis1.onTargetImageChanged();
  257. };
  258. image.src = fileReader.result;
  259. };
  260. fileReader.readAsDataURL(file);
  261. Main.openImageFileInput.files = null;
  262. },
  263. false
  264. );
  265. Main.stepButton.addEventListener(
  266. "click",
  267. function () {
  268. _gthis1.stepRunner();
  269. },
  270. false
  271. );
  272. Main.randomImageButton.addEventListener(
  273. "click",
  274. function () {
  275. var image = new Image();
  276. image.onload = function () {
  277. Main.sampleImagesContainer.appendChild(image);
  278. var _gthis = _gthis1;
  279. var tmp = _gthis1.imageToCanvas(image);
  280. _gthis1.targetImage = _gthis.canvasToBitmap(tmp);
  281. _gthis1.onTargetImageChanged();
  282. };
  283. var tmp = Main.imagesToGeometrize;
  284. var tmp1 = Math.floor(Math.random() * Main.imagesToGeometrize.length);
  285. image.src = tmp[tmp1];
  286. },
  287. false
  288. );
  289. Main.resetButton.addEventListener(
  290. "click",
  291. function () {
  292. _gthis1.targetImage = _gthis1.targetImage;
  293. _gthis1.onTargetImageChanged();
  294. },
  295. false
  296. );
  297. Main.saveImageButton.addEventListener(
  298. "click",
  299. function (e) {
  300. var svgData = "data:image/svg+xml;base64," + window.btoa(_gthis1.makeSvgData());
  301. var svgImage = new Image();
  302. svgImage.onload = function () {
  303. var canvas = _gthis1.imageToCanvas(svgImage);
  304. if (canvas.msToBlob != null) {
  305. var blob = canvas.msToBlob();
  306. var navigator = window.navigator;
  307. navigator.msSaveBlob(blob, "geometrized_image.png");
  308. } else {
  309. var data = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
  310. var tempLink = window.document.createElement("a");
  311. tempLink.href = data;
  312. tempLink.download = "geometrized_image.png";
  313. window.document.body.appendChild(tempLink);
  314. tempLink.click();
  315. window.document.body.removeChild(tempLink);
  316. }
  317. };
  318. svgImage.setAttribute("src", svgData);
  319. },
  320. false
  321. );
  322. var saveBlob = function (data, dataType, filename, anchor) {
  323. var blob = new Blob([data], { type: dataType });
  324. var navigator = window.navigator;
  325. if (navigator.msSaveBlob != null) {
  326. navigator.msSaveBlob(blob, filename);
  327. } else {
  328. var dataUrl = URL.createObjectURL(blob);
  329. anchor.download = filename;
  330. anchor.href = dataUrl;
  331. }
  332. };
  333. Main.saveSvgButton.addEventListener(
  334. "click",
  335. function (e) {
  336. saveBlob(Main.currentSvgContainer.innerHTML, "image/svg+xml;charset=utf-8", "geometrized_svg.svg", Main.saveSvgButton);
  337. },
  338. false
  339. );
  340. Main.saveJsonButton.addEventListener(
  341. "click",
  342. function (e) {
  343. saveBlob("[\r\n" + _gthis1.shapeJsonData.join(",\r\n") + "\r\n]", "data:text/json;charset=utf-8", "geometrized_json.json", Main.saveJsonButton);
  344. },
  345. false
  346. );
  347. var setShapeOption = function (option, enable) {
  348. if (enable) {
  349. geometrize_ArraySet.add(_gthis1.shapeTypes, option);
  350. } else {
  351. HxOverrides.remove(_gthis1.shapeTypes, option);
  352. }
  353. };
  354. Main.rectanglesCheckbox.addEventListener(
  355. "click",
  356. function () {
  357. setShapeOption(0, Main.rectanglesCheckbox.checked);
  358. },
  359. false
  360. );
  361. Main.rotatedRectanglesCheckbox.addEventListener(
  362. "click",
  363. function () {
  364. setShapeOption(1, Main.rotatedRectanglesCheckbox.checked);
  365. },
  366. false
  367. );
  368. Main.trianglesCheckbox.addEventListener(
  369. "click",
  370. function () {
  371. setShapeOption(2, Main.trianglesCheckbox.checked);
  372. },
  373. false
  374. );
  375. Main.ellipsesCheckbox.addEventListener(
  376. "click",
  377. function () {
  378. setShapeOption(3, Main.ellipsesCheckbox.checked);
  379. },
  380. false
  381. );
  382. Main.rotatedEllipsesCheckbox.addEventListener(
  383. "click",
  384. function () {
  385. setShapeOption(4, Main.rotatedEllipsesCheckbox.checked);
  386. },
  387. false
  388. );
  389. Main.circlesCheckbox.addEventListener(
  390. "click",
  391. function () {
  392. setShapeOption(5, Main.circlesCheckbox.checked);
  393. },
  394. false
  395. );
  396. Main.linesCheckbox.addEventListener(
  397. "click",
  398. function () {
  399. setShapeOption(6, Main.linesCheckbox.checked);
  400. },
  401. false
  402. );
  403. Main.quadraticBeziersCheckbox.addEventListener(
  404. "click",
  405. function () {
  406. setShapeOption(7, Main.quadraticBeziersCheckbox.checked);
  407. },
  408. false
  409. );
  410. this.set_maxShapeCountLimit(500);
  411. if (this.worker != null) {
  412. this.worker.terminate();
  413. }
  414. this.worker = new GeometrizeWorkerInterface();
  415. this.worker.onMessage = $bind(this, this.onWorkerMessageReceived);
  416. // this.targetImage = this.createDefaultBitmap();
  417. // this.onTargetImageChanged();
  418. this.set_running(false);
  419.  
  420. globalThis["geometrize"] = this;
  421. },
  422. createTooltips: function (slider) {
  423. var tipHandles = slider.getElementsByClassName("noUi-handle");
  424. var _g = 0;
  425. var _g1 = tipHandles.length;
  426. while (_g < _g1) {
  427. var i = _g++;
  428. var div = window.document.createElement("div");
  429. div.className += "tooltip";
  430. tipHandles[i].appendChild(div);
  431. this.updateTooltips(slider, i, 0);
  432. }
  433. },
  434. updateTooltips: function (slider, handleIdx, value) {
  435. var tipHandles = slider.getElementsByClassName("noUi-handle");
  436. tipHandles[handleIdx].innerHTML = "<span class='tooltip'>" + (value == null ? "null" : "" + value) + "</span>";
  437. },
  438. checkStopConditions: function () {
  439. if (this.get_shapeCount() >= this.get_maxShapeCountLimit()) {
  440. this.set_running(false);
  441. }
  442. },
  443. stepRunner: function () {
  444. var options = {
  445. shapeTypes: this.shapeTypes.length == 0 ? [2] : geometrize_ArraySet.toArray(this.shapeTypes),
  446. alpha: this.shapeOpacity | 0,
  447. candidateShapesPerStep: this.candidateShapesPerStep,
  448. shapeMutationsPerStep: this.shapeMutationsPerStep,
  449. };
  450. this.worker.postMessage({ id: "should_step", data: options });
  451. },
  452. onWorkerMessageReceived: function (message) {
  453. switch (message.id) {
  454. case "did_set_target_image":
  455. break;
  456. case "did_step":
  457. this.shapeJsonData.push(message.jsonData);
  458. this.appendSvgShapeData(message.svgData);
  459. this.checkStopConditions();
  460. break;
  461. }
  462. if (this.running) {
  463. this.stepRunner();
  464. }
  465. },
  466. appendSvgShapeData: function (data) {
  467. this.shapeSvgData.push(data);
  468. var tmp = this.get_shapeCount();
  469. Main.shapesAddedText.innerHTML = Std.string(tmp);
  470. var data = this.makeSvgData();
  471. this.setSvgElement(data);
  472. },
  473. setSvgElement: function (svgCode) {
  474. Main.currentSvgContainer.innerHTML = svgCode;
  475. },
  476. canvasToBitmap: function (canvas) {
  477. var context = canvas.getContext("2d", null);
  478. var imageData = context.getImageData(0, 0, canvas.width, canvas.height);
  479. var bytesData = new haxe_io_Bytes(new ArrayBuffer(imageData.data.length));
  480. var _g = 0;
  481. var _g1 = bytesData.length;
  482. while (_g < _g1) {
  483. var i = _g++;
  484. bytesData.b[i] = imageData.data[i];
  485. }
  486. var w = canvas.width;
  487. var h = canvas.height;
  488. var bitmap = new geometrize_bitmap_Bitmap();
  489. if (bytesData == null) {
  490. throw haxe_Exception.thrown("FAIL: bytes != null");
  491. }
  492. var actual = bytesData.length;
  493. var expected = w * h * 4;
  494. if (actual != expected) {
  495. throw haxe_Exception.thrown("FAIL: values are not equal (expected: " + expected + ", actual: " + actual + ")");
  496. }
  497. bitmap.width = w;
  498. bitmap.height = h;
  499. var this1 = new Array((bytesData.length / 4) | 0);
  500. bitmap.data = this1;
  501. var i = 0;
  502. var x = 0;
  503. while (i < bytesData.length) {
  504. var red = bytesData.b[i];
  505. var green = bytesData.b[i + 1];
  506. var blue = bytesData.b[i + 2];
  507. var alpha = bytesData.b[i + 3];
  508. bitmap.data[x] =
  509. ((red < 0 ? 0 : red > 255 ? 255 : red) << 24) +
  510. ((green < 0 ? 0 : green > 255 ? 255 : green) << 16) +
  511. ((blue < 0 ? 0 : blue > 255 ? 255 : blue) << 8) +
  512. (alpha < 0 ? 0 : alpha > 255 ? 255 : alpha);
  513. i += 4;
  514. ++x;
  515. }
  516. var bitmap1 = bitmap;
  517. return bitmap1;
  518. },
  519. imageToCanvas: function (image) {
  520. var canvas = window.document.createElement("canvas");
  521. canvas.width = image.width;
  522. canvas.height = image.height;
  523. var context = canvas.getContext("2d", null);
  524. context.drawImage(image, 0, 0);
  525. return canvas;
  526. },
  527. createDefaultBitmap: function () {
  528. return this.canvasToBitmap(this.imageToCanvas(Main.defaultImageElement));
  529. },
  530. onTargetImageChanged: function () {
  531. var backgroundColor = geometrize_Util.getAverageImageColor(this.targetImage);
  532. var red = ((((backgroundColor >> 24) & 255) * this.initialBackgroundOpacity) / 255.0) | 0;
  533. var green = ((((backgroundColor >> 16) & 255) * this.initialBackgroundOpacity) / 255.0) | 0;
  534. var blue = ((((backgroundColor >> 8) & 255) * this.initialBackgroundOpacity) / 255.0) | 0;
  535. var alpha = this.initialBackgroundOpacity | 0;
  536. var premultipliedColor =
  537. ((red < 0 ? 0 : red > 255 ? 255 : red) << 24) +
  538. ((green < 0 ? 0 : green > 255 ? 255 : green) << 16) +
  539. ((blue < 0 ? 0 : blue > 255 ? 255 : blue) << 8) +
  540. (alpha < 0 ? 0 : alpha > 255 ? 255 : alpha);
  541. var backgroundRect = new geometrize_shape_Rectangle(this.targetImage.width, this.targetImage.height);
  542. backgroundRect.x1 = 0;
  543. backgroundRect.y1 = 0;
  544. backgroundRect.x2 = this.targetImage.width - 1;
  545. backgroundRect.y2 = this.targetImage.height - 1;
  546. this.shapeSvgData = [];
  547. this.shapeJsonData = [];
  548. this.appendSvgShapeData(geometrize_exporter_SvgExporter.exportShape({ score: 0.0, color: premultipliedColor, shape: backgroundRect }));
  549. this.shapeJsonData.push(
  550. geometrize_exporter_ShapeJsonExporter.exportShape({
  551. score: 0.0,
  552. color: premultipliedColor,
  553. shape: backgroundRect,
  554. })
  555. );
  556. if (this.worker != null) {
  557. this.worker.terminate();
  558. }
  559. this.worker = new GeometrizeWorkerInterface();
  560. this.worker.onMessage = $bind(this, this.onWorkerMessageReceived);
  561. this.worker.postMessage({ id: "should_set_target_image", data: this.targetImage });
  562. if (this.running) {
  563. this.stepRunner();
  564. }
  565. window.dispatchEvent(new CustomEvent('geometrize.targetImageChanged'));
  566. },
  567. makeSvgData: function () {
  568. return (
  569. geometrize_exporter_SvgExporter.getSvgPrelude() +
  570. geometrize_exporter_SvgExporter.getSvgNodeOpen(this.targetImage.width, this.targetImage.height) +
  571. Std.string(this.shapeSvgData) +
  572. geometrize_exporter_SvgExporter.getSvgNodeClose()
  573. );
  574. },
  575. set_running: function (running) {
  576. // Main.runPauseButton.innerHTML = running ? "<h2>Pause</h2>" : "<h2>Run</h2>";
  577. var wasRunning = this.running;
  578. this.running = running;
  579. if (!wasRunning && this.running) {
  580. this.stepRunner();
  581. }
  582. window.dispatchEvent(new CustomEvent('geometrize.changedState'));
  583. return this.running;
  584. },
  585. };
  586. Math.__name__ = true;
  587. var Std = function () {};
  588. Std.__name__ = true;
  589. Std.string = function (s) {
  590. return js_Boot.__string_rec(s, "");
  591. };
  592. Std.parseInt = function (x) {
  593. if (x != null) {
  594. var _g = 0;
  595. var _g1 = x.length;
  596. while (_g < _g1) {
  597. var i = _g++;
  598. var c = x.charCodeAt(i);
  599. if (c <= 8 || (c >= 14 && c != 32 && c != 45)) {
  600. var nc = x.charCodeAt(i + 1);
  601. var v = parseInt(x, nc == 120 || nc == 88 ? 16 : 10);
  602. if (isNaN(v)) {
  603. return null;
  604. } else {
  605. return v;
  606. }
  607. }
  608. }
  609. }
  610. return null;
  611. };
  612. Std.random = function (x) {
  613. if (x <= 0) {
  614. return 0;
  615. } else {
  616. return Math.floor(Math.random() * x);
  617. }
  618. };
  619. var StringTools = function () {};
  620. StringTools.__name__ = true;
  621. StringTools.replace = function (s, sub, by) {
  622. return s.split(sub).join(by);
  623. };
  624. var UInt = {};
  625. UInt.toFloat = function (this1) {
  626. var int = this1;
  627. if (int < 0) {
  628. return 4294967296.0 + int;
  629. } else {
  630. return int + 0.0;
  631. }
  632. };
  633. var geometrize_ArraySet = {};
  634. geometrize_ArraySet.create = function (array) {
  635. if (array == null) {
  636. var this1 = [];
  637. return this1;
  638. }
  639. return geometrize_ArraySet.toSet(array);
  640. };
  641. geometrize_ArraySet.add = function (this1, element) {
  642. if (element == null) {
  643. throw haxe_Exception.thrown("FAIL: element != null");
  644. }
  645. if (geometrize_ArraySet.contains(this1, element)) {
  646. return false;
  647. }
  648. this1.push(element);
  649. return true;
  650. };
  651. geometrize_ArraySet.contains = function (this1, element) {
  652. var _g = 0;
  653. while (_g < this1.length) {
  654. var i = this1[_g];
  655. ++_g;
  656. if (i == element) {
  657. return true;
  658. }
  659. }
  660. return false;
  661. };
  662. geometrize_ArraySet.toArray = function (this1) {
  663. return this1.slice();
  664. };
  665. geometrize_ArraySet.toSet = function (array) {
  666. var this1 = [];
  667. var set = this1;
  668. var _g = 0;
  669. while (_g < array.length) {
  670. var v = array[_g];
  671. ++_g;
  672. geometrize_ArraySet.add(set, v);
  673. }
  674. return set;
  675. };
  676. var geometrize_Core = function () {};
  677. geometrize_Core.__name__ = true;
  678. geometrize_Core.computeColor = function (target, current, lines, alpha) {
  679. if (target == null) {
  680. throw haxe_Exception.thrown("FAIL: target != null");
  681. }
  682. if (current == null) {
  683. throw haxe_Exception.thrown("FAIL: current != null");
  684. }
  685. if (lines == null) {
  686. throw haxe_Exception.thrown("FAIL: lines != null");
  687. }
  688. if (alpha < 0) {
  689. throw haxe_Exception.thrown("FAIL: alpha >= 0");
  690. }
  691. var totalRed = 0;
  692. var totalGreen = 0;
  693. var totalBlue = 0;
  694. var count = 0;
  695. var f = 65535 / alpha;
  696. var a = f | 0;
  697. var _g = 0;
  698. while (_g < lines.length) {
  699. var line = lines[_g];
  700. ++_g;
  701. var y = line.y;
  702. var _g1 = line.x1;
  703. var _g2 = line.x2 + 1;
  704. while (_g1 < _g2) {
  705. var x = _g1++;
  706. var t = target.data[target.width * y + x];
  707. var c = current.data[current.width * y + x];
  708. totalRed += (((t >> 24) & 255) - ((c >> 24) & 255)) * a + ((c >> 24) & 255) * 257;
  709. totalGreen += (((t >> 16) & 255) - ((c >> 16) & 255)) * a + ((c >> 16) & 255) * 257;
  710. totalBlue += (((t >> 8) & 255) - ((c >> 8) & 255)) * a + ((c >> 8) & 255) * 257;
  711. ++count;
  712. }
  713. }
  714. if (count == 0) {
  715. return 0;
  716. }
  717. var value = ((totalRed / count) | 0) >> 8;
  718. var r = value < 0 ? 0 : value > 255 ? 255 : value;
  719. var value = ((totalGreen / count) | 0) >> 8;
  720. var g = value < 0 ? 0 : value > 255 ? 255 : value;
  721. var value = ((totalBlue / count) | 0) >> 8;
  722. var b = value < 0 ? 0 : value > 255 ? 255 : value;
  723. return ((r < 0 ? 0 : r > 255 ? 255 : r) << 24) + ((g < 0 ? 0 : g > 255 ? 255 : g) << 16) + ((b < 0 ? 0 : b > 255 ? 255 : b) << 8) + (alpha < 0 ? 0 : alpha > 255 ? 255 : alpha);
  724. };
  725. geometrize_Core.differenceFull = function (first, second) {
  726. if (first == null) {
  727. throw haxe_Exception.thrown("FAIL: first != null");
  728. }
  729. if (second == null) {
  730. throw haxe_Exception.thrown("FAIL: second != null");
  731. }
  732. if (first.width == 0) {
  733. throw haxe_Exception.thrown("FAIL: first.width != 0");
  734. }
  735. if (first.height == 0) {
  736. throw haxe_Exception.thrown("FAIL: first.height != 0");
  737. }
  738. if (second.width == 0) {
  739. throw haxe_Exception.thrown("FAIL: second.width != 0");
  740. }
  741. if (second.height == 0) {
  742. throw haxe_Exception.thrown("FAIL: second.height != 0");
  743. }
  744. var actual = first.width;
  745. var expected = second.width;
  746. if (actual != expected) {
  747. throw haxe_Exception.thrown("FAIL: values are not equal (expected: " + expected + ", actual: " + actual + ")");
  748. }
  749. var actual = first.height;
  750. var expected = second.height;
  751. if (actual != expected) {
  752. throw haxe_Exception.thrown("FAIL: values are not equal (expected: " + expected + ", actual: " + actual + ")");
  753. }
  754. var total = 0;
  755. var width = first.width;
  756. var height = first.height;
  757. var _g = 0;
  758. var _g1 = height;
  759. while (_g < _g1) {
  760. var y = _g++;
  761. var _g2 = 0;
  762. var _g3 = width;
  763. while (_g2 < _g3) {
  764. var x = _g2++;
  765. var f = first.data[first.width * y + x];
  766. var s = second.data[second.width * y + x];
  767. var dr = ((f >> 24) & 255) - ((s >> 24) & 255);
  768. var dg = ((f >> 16) & 255) - ((s >> 16) & 255);
  769. var db = ((f >> 8) & 255) - ((s >> 8) & 255);
  770. var da = (f & 255) - (s & 255);
  771. total += dr * dr + dg * dg + db * db + da * da;
  772. }
  773. }
  774. var result = Math.sqrt(total / (width * height * 4.0)) / 255;
  775. if (!isFinite(result)) {
  776. throw haxe_Exception.thrown("FAIL: Math.isFinite(result)");
  777. }
  778. return result;
  779. };
  780. geometrize_Core.differencePartial = function (target, before, after, score, lines) {
  781. if (target == null) {
  782. throw haxe_Exception.thrown("FAIL: target != null");
  783. }
  784. if (before == null) {
  785. throw haxe_Exception.thrown("FAIL: before != null");
  786. }
  787. if (after == null) {
  788. throw haxe_Exception.thrown("FAIL: after != null");
  789. }
  790. if (lines == null) {
  791. throw haxe_Exception.thrown("FAIL: lines != null");
  792. }
  793. if (lines.length == 0) {
  794. throw haxe_Exception.thrown("FAIL: lines.length != 0");
  795. }
  796. var width = target.width;
  797. var height = target.height;
  798. var rgbaCount = width * height * 4;
  799. var total = Math.pow(score * 255, 2) * rgbaCount;
  800. var _g = 0;
  801. while (_g < lines.length) {
  802. var line = lines[_g];
  803. ++_g;
  804. var y = line.y;
  805. var _g1 = line.x1;
  806. var _g2 = line.x2 + 1;
  807. while (_g1 < _g2) {
  808. var x = _g1++;
  809. var t = target.data[target.width * y + x];
  810. var b = before.data[before.width * y + x];
  811. var a = after.data[after.width * y + x];
  812. var dtbr = ((t >> 24) & 255) - ((b >> 24) & 255);
  813. var dtbg = ((t >> 16) & 255) - ((b >> 16) & 255);
  814. var dtbb = ((t >> 8) & 255) - ((b >> 8) & 255);
  815. var dtba = (t & 255) - (b & 255);
  816. var dtar = ((t >> 24) & 255) - ((a >> 24) & 255);
  817. var dtag = ((t >> 16) & 255) - ((a >> 16) & 255);
  818. var dtab = ((t >> 8) & 255) - ((a >> 8) & 255);
  819. var dtaa = (t & 255) - (a & 255);
  820. total -= dtbr * dtbr + dtbg * dtbg + dtbb * dtbb + dtba * dtba;
  821. total += dtar * dtar + dtag * dtag + dtab * dtab + dtaa * dtaa;
  822. }
  823. }
  824. var result = Math.sqrt(total / rgbaCount) / 255;
  825. if (!isFinite(result)) {
  826. throw haxe_Exception.thrown("FAIL: Math.isFinite(result)");
  827. }
  828. return result;
  829. };
  830. geometrize_Core.bestRandomState = function (shapes, alpha, n, target, current, buffer, lastScore) {
  831. var bestEnergy = 0;
  832. var bestState = null;
  833. var _g = 0;
  834. var _g1 = n;
  835. while (_g < _g1) {
  836. var i = _g++;
  837. var state = new geometrize_State(geometrize_shape_ShapeFactory.randomShapeOf(shapes, current.width, current.height), alpha, target, current, buffer);
  838. var energy = state.energy(lastScore);
  839. if (i == 0 || energy < bestEnergy) {
  840. bestEnergy = energy;
  841. bestState = state;
  842. }
  843. }
  844. return bestState;
  845. };
  846. geometrize_Core.bestHillClimbState = function (shapes, alpha, n, age, target, current, buffer, lastScore) {
  847. var state = geometrize_Core.bestRandomState(shapes, alpha, n, target, current, buffer, lastScore);
  848. state = geometrize_Core.hillClimb(state, age, lastScore);
  849. return state;
  850. };
  851. geometrize_Core.hillClimb = function (state, maxAge, lastScore) {
  852. if (state == null) {
  853. throw haxe_Exception.thrown("FAIL: state != null");
  854. }
  855. if (maxAge < 0) {
  856. throw haxe_Exception.thrown("FAIL: maxAge >= 0");
  857. }
  858. var state1 = state.clone();
  859. var bestState = state1.clone();
  860. var bestEnergy = state1.energy(lastScore);
  861. var age = 0;
  862. while (age < maxAge) {
  863. var undo = state1.mutate();
  864. var energy = state1.energy(lastScore);
  865. if (energy >= bestEnergy) {
  866. state1 = undo;
  867. } else {
  868. bestEnergy = energy;
  869. bestState = state1.clone();
  870. age = -1;
  871. }
  872. ++age;
  873. }
  874. return bestState;
  875. };
  876. geometrize_Core.energy = function (shape, alpha, target, current, buffer, score) {
  877. if (shape == null) {
  878. throw haxe_Exception.thrown("FAIL: shape != null");
  879. }
  880. if (target == null) {
  881. throw haxe_Exception.thrown("FAIL: target != null");
  882. }
  883. if (current == null) {
  884. throw haxe_Exception.thrown("FAIL: current != null");
  885. }
  886. if (buffer == null) {
  887. throw haxe_Exception.thrown("FAIL: buffer != null");
  888. }
  889. var lines = shape.rasterize();
  890. if (lines == null) {
  891. throw haxe_Exception.thrown("FAIL: lines != null");
  892. }
  893. if (lines.length == 0) {
  894. throw haxe_Exception.thrown("FAIL: lines.length != 0");
  895. }
  896. var color = geometrize_Core.computeColor(target, current, lines, alpha);
  897. geometrize_rasterizer_Rasterizer.copyLines(buffer, current, lines);
  898. geometrize_rasterizer_Rasterizer.drawLines(buffer, color, lines);
  899. return geometrize_Core.differencePartial(target, current, buffer, score, lines);
  900. };
  901. var geometrize_Model = function (target, backgroundColor) {
  902. if (target == null) {
  903. throw haxe_Exception.thrown("FAIL: target != null");
  904. }
  905. this.width = target.width;
  906. this.height = target.height;
  907. this.target = target;
  908. var w = target.width;
  909. var h = target.height;
  910. var bitmap = new geometrize_bitmap_Bitmap();
  911. bitmap.width = w;
  912. bitmap.height = h;
  913. var this1 = new Array(w * h);
  914. bitmap.data = this1;
  915. var i = 0;
  916. while (i < bitmap.data.length) {
  917. bitmap.data[i] = backgroundColor;
  918. ++i;
  919. }
  920. this.current = bitmap;
  921. var w = target.width;
  922. var h = target.height;
  923. var bitmap = new geometrize_bitmap_Bitmap();
  924. bitmap.width = w;
  925. bitmap.height = h;
  926. var this1 = new Array(w * h);
  927. bitmap.data = this1;
  928. var i = 0;
  929. while (i < bitmap.data.length) {
  930. bitmap.data[i] = backgroundColor;
  931. ++i;
  932. }
  933. this.buffer = bitmap;
  934. this.score = geometrize_Core.differenceFull(target, this.current);
  935. };
  936. geometrize_Model.__name__ = true;
  937. geometrize_Model.prototype = {
  938. step: function (shapeTypes, alpha, n, age) {
  939. var state = geometrize_Core.bestHillClimbState(shapeTypes, alpha, n, age, this.target, this.current, this.buffer, this.score);
  940. var results = [this.addShape(state.shape, state.alpha)];
  941. return results;
  942. },
  943. addShape: function (shape, alpha) {
  944. if (shape == null) {
  945. throw haxe_Exception.thrown("FAIL: shape != null");
  946. }
  947. var _this = this.current;
  948. var bitmap = new geometrize_bitmap_Bitmap();
  949. bitmap.width = _this.width;
  950. bitmap.height = _this.height;
  951. var this1 = new Array(_this.data.length);
  952. bitmap.data = this1;
  953. var _g = 0;
  954. var _g1 = _this.data.length;
  955. while (_g < _g1) {
  956. var i = _g++;
  957. bitmap.data[i] = _this.data[i];
  958. }
  959. var before = bitmap;
  960. var lines = shape.rasterize();
  961. var color = geometrize_Core.computeColor(this.target, this.current, lines, alpha);
  962. geometrize_rasterizer_Rasterizer.drawLines(this.current, color, lines);
  963. this.score = geometrize_Core.differencePartial(this.target, before, this.current, this.score, lines);
  964. var result = { score: this.score, color: color, shape: shape };
  965. return result;
  966. },
  967. };
  968. var geometrize_State = function (shape, alpha, target, current, buffer) {
  969. if (shape == null) {
  970. throw haxe_Exception.thrown("FAIL: shape != null");
  971. }
  972. this.shape = shape;
  973. this.alpha = alpha;
  974. this.score = -1;
  975. this.target = target;
  976. this.current = current;
  977. this.buffer = buffer;
  978. };
  979. geometrize_State.__name__ = true;
  980. geometrize_State.prototype = {
  981. energy: function (lastScore) {
  982. if (this.score < 0) {
  983. this.score = geometrize_Core.energy(this.shape, this.alpha, this.target, this.current, this.buffer, lastScore);
  984. }
  985. return this.score;
  986. },
  987. mutate: function () {
  988. var oldState = this.clone();
  989. this.shape.mutate();
  990. return oldState;
  991. },
  992. clone: function () {
  993. return new geometrize_State(this.shape.clone(), this.alpha, this.target, this.current, this.buffer);
  994. },
  995. };
  996. var geometrize_Util = function () {};
  997. geometrize_Util.__name__ = true;
  998. geometrize_Util.getAverageImageColor = function (image, alpha) {
  999. if (alpha == null) {
  1000. alpha = 255;
  1001. }
  1002. if (image == null) {
  1003. throw haxe_Exception.thrown("FAIL: image != null");
  1004. }
  1005. var totalRed = 0;
  1006. var totalGreen = 0;
  1007. var totalBlue = 0;
  1008. var _g = 0;
  1009. var _g1 = image.width;
  1010. while (_g < _g1) {
  1011. var x = _g++;
  1012. var _g2 = 0;
  1013. var _g3 = image.height;
  1014. while (_g2 < _g3) {
  1015. var y = _g2++;
  1016. var pixel = image.data[image.width * y + x];
  1017. totalRed += (pixel >> 24) & 255;
  1018. totalGreen += (pixel >> 16) & 255;
  1019. totalBlue += (pixel >> 8) & 255;
  1020. }
  1021. }
  1022. var size = image.width * image.height;
  1023. var red = (totalRed / size) | 0;
  1024. var green = (totalGreen / size) | 0;
  1025. var blue = (totalBlue / size) | 0;
  1026. return (
  1027. ((red < 0 ? 0 : red > 255 ? 255 : red) << 24) +
  1028. ((green < 0 ? 0 : green > 255 ? 255 : green) << 16) +
  1029. ((blue < 0 ? 0 : blue > 255 ? 255 : blue) << 8) +
  1030. (alpha < 0 ? 0 : alpha > 255 ? 255 : alpha)
  1031. );
  1032. };
  1033. var geometrize_bitmap_Bitmap = ($hx_exports["geometrize"]["bitmap"]["Bitmap"] = function () {});
  1034. geometrize_bitmap_Bitmap.__name__ = true;
  1035. geometrize_bitmap_Bitmap.create = function (w, h, color) {
  1036. var bitmap = new geometrize_bitmap_Bitmap();
  1037. bitmap.width = w;
  1038. bitmap.height = h;
  1039. var this1 = new Array(w * h);
  1040. bitmap.data = this1;
  1041. var i = 0;
  1042. while (i < bitmap.data.length) {
  1043. bitmap.data[i] = color;
  1044. ++i;
  1045. }
  1046. return bitmap;
  1047. };
  1048. geometrize_bitmap_Bitmap.createFromBytes = function (w, h, bytes) {
  1049. var bitmap = new geometrize_bitmap_Bitmap();
  1050. if (bytes == null) {
  1051. throw haxe_Exception.thrown("FAIL: bytes != null");
  1052. }
  1053. var actual = bytes.length;
  1054. var expected = w * h * 4;
  1055. if (actual != expected) {
  1056. throw haxe_Exception.thrown("FAIL: values are not equal (expected: " + expected + ", actual: " + actual + ")");
  1057. }
  1058. bitmap.width = w;
  1059. bitmap.height = h;
  1060. var this1 = new Array((bytes.length / 4) | 0);
  1061. bitmap.data = this1;
  1062. var i = 0;
  1063. var x = 0;
  1064. while (i < bytes.length) {
  1065. var red = bytes.b[i];
  1066. var green = bytes.b[i + 1];
  1067. var blue = bytes.b[i + 2];
  1068. var alpha = bytes.b[i + 3];
  1069. bitmap.data[x] =
  1070. ((red < 0 ? 0 : red > 255 ? 255 : red) << 24) +
  1071. ((green < 0 ? 0 : green > 255 ? 255 : green) << 16) +
  1072. ((blue < 0 ? 0 : blue > 255 ? 255 : blue) << 8) +
  1073. (alpha < 0 ? 0 : alpha > 255 ? 255 : alpha);
  1074. i += 4;
  1075. ++x;
  1076. }
  1077. return bitmap;
  1078. };
  1079. geometrize_bitmap_Bitmap.createFromByteArray = function (w, h, bytes) {
  1080. var data = new haxe_io_Bytes(new ArrayBuffer(bytes.length));
  1081. var i = 0;
  1082. while (i < bytes.length) {
  1083. data.b[i] = bytes[i];
  1084. ++i;
  1085. }
  1086. var bitmap = new geometrize_bitmap_Bitmap();
  1087. if (data == null) {
  1088. throw haxe_Exception.thrown("FAIL: bytes != null");
  1089. }
  1090. var actual = data.length;
  1091. var expected = w * h * 4;
  1092. if (actual != expected) {
  1093. throw haxe_Exception.thrown("FAIL: values are not equal (expected: " + expected + ", actual: " + actual + ")");
  1094. }
  1095. bitmap.width = w;
  1096. bitmap.height = h;
  1097. var this1 = new Array((data.length / 4) | 0);
  1098. bitmap.data = this1;
  1099. var i = 0;
  1100. var x = 0;
  1101. while (i < data.length) {
  1102. var red = data.b[i];
  1103. var green = data.b[i + 1];
  1104. var blue = data.b[i + 2];
  1105. var alpha = data.b[i + 3];
  1106. bitmap.data[x] =
  1107. ((red < 0 ? 0 : red > 255 ? 255 : red) << 24) +
  1108. ((green < 0 ? 0 : green > 255 ? 255 : green) << 16) +
  1109. ((blue < 0 ? 0 : blue > 255 ? 255 : blue) << 8) +
  1110. (alpha < 0 ? 0 : alpha > 255 ? 255 : alpha);
  1111. i += 4;
  1112. ++x;
  1113. }
  1114. return bitmap;
  1115. };
  1116. geometrize_bitmap_Bitmap.prototype = {
  1117. getPixel: function (x, y) {
  1118. return this.data[this.width * y + x];
  1119. },
  1120. setPixel: function (x, y, color) {
  1121. this.data[this.width * y + x] = color;
  1122. },
  1123. clone: function () {
  1124. var bitmap = new geometrize_bitmap_Bitmap();
  1125. bitmap.width = this.width;
  1126. bitmap.height = this.height;
  1127. var this1 = new Array(this.data.length);
  1128. bitmap.data = this1;
  1129. var _g = 0;
  1130. var _g1 = this.data.length;
  1131. while (_g < _g1) {
  1132. var i = _g++;
  1133. bitmap.data[i] = this.data[i];
  1134. }
  1135. return bitmap;
  1136. },
  1137. fill: function (color) {
  1138. var idx = 0;
  1139. while (idx < this.data.length) {
  1140. this.data[idx] = (color >> 24) & 255;
  1141. this.data[idx + 1] = (color >> 16) & 255;
  1142. this.data[idx + 2] = (color >> 8) & 255;
  1143. this.data[idx + 3] = color & 255;
  1144. idx += 4;
  1145. }
  1146. },
  1147. getBytes: function () {
  1148. var bytes = new haxe_io_Bytes(new ArrayBuffer(this.data.length * 4));
  1149. var i = 0;
  1150. while (i < this.data.length) {
  1151. var idx = i * 4;
  1152. bytes.b[idx] = (this.data[i] >> 24) & 255;
  1153. bytes.b[idx + 1] = (this.data[i] >> 16) & 255;
  1154. bytes.b[idx + 2] = (this.data[i] >> 8) & 255;
  1155. bytes.b[idx + 3] = this.data[i] & 255;
  1156. ++i;
  1157. }
  1158. return bytes;
  1159. },
  1160. };
  1161. var geometrize_exporter_ShapeJsonExporter = ($hx_exports["geometrize"]["exporter"]["ShapeJsonExporter"] = function () {});
  1162. geometrize_exporter_ShapeJsonExporter.__name__ = true;
  1163. geometrize_exporter_ShapeJsonExporter.export = function (shapes) {
  1164. return "[\n" + geometrize_exporter_ShapeJsonExporter.exportShapes(shapes) + "\n]";
  1165. };
  1166. geometrize_exporter_ShapeJsonExporter.exportShapes = function (shapes) {
  1167. var results = "";
  1168. var _g = 0;
  1169. var _g1 = shapes.length;
  1170. while (_g < _g1) {
  1171. var i = _g++;
  1172. results += geometrize_exporter_ShapeJsonExporter.exportShape(shapes[i]);
  1173. if (i != shapes.length - 1) {
  1174. results += ",\n";
  1175. }
  1176. }
  1177. return results;
  1178. };
  1179. geometrize_exporter_ShapeJsonExporter.exportShape = function (shape) {
  1180. var result = " {\n";
  1181. var type = shape.shape.getType();
  1182. var data = shape.shape.getRawShapeData();
  1183. var color = shape.color;
  1184. var score = shape.score;
  1185. result += ' "type":' + type + ",\n";
  1186. result += ' "data":' + "[";
  1187. var _g = 0;
  1188. var _g1 = data.length;
  1189. while (_g < _g1) {
  1190. var item = _g++;
  1191. result += data[item];
  1192. if (item <= data.length - 2) {
  1193. result += ",";
  1194. }
  1195. }
  1196. result += "],\n";
  1197. result += ' "color":' + "[";
  1198. result += ((color >> 24) & 255) + ",";
  1199. result += ((color >> 16) & 255) + ",";
  1200. result += ((color >> 8) & 255) + ",";
  1201. result += color & 255;
  1202. result += "],\n";
  1203. result += ' "score":' + score + "\n";
  1204. result += " }";
  1205. return result;
  1206. };
  1207. var geometrize_exporter_SvgExporter = ($hx_exports["geometrize"]["exporter"]["SvgExporter"] = function () {});
  1208. geometrize_exporter_SvgExporter.__name__ = true;
  1209. geometrize_exporter_SvgExporter.export = function (shapes, width, height) {
  1210. var results = geometrize_exporter_SvgExporter.getSvgPrelude();
  1211. results += geometrize_exporter_SvgExporter.getSvgNodeOpen(width, height);
  1212. results += geometrize_exporter_SvgExporter.exportShapes(shapes);
  1213. results += geometrize_exporter_SvgExporter.getSvgNodeClose();
  1214. return results;
  1215. };
  1216. geometrize_exporter_SvgExporter.exportShapes = function (shapes) {
  1217. var results = "";
  1218. var _g = 0;
  1219. var _g1 = shapes.length;
  1220. while (_g < _g1) {
  1221. var i = _g++;
  1222. results += geometrize_exporter_SvgExporter.exportShape(shapes[i]);
  1223. if (i != shapes.length - 1) {
  1224. results += "\n";
  1225. }
  1226. }
  1227. return results;
  1228. };
  1229. geometrize_exporter_SvgExporter.exportShape = function (shape) {
  1230. return StringTools.replace(shape.shape.getSvgShapeData(), geometrize_exporter_SvgExporter.SVG_STYLE_HOOK, geometrize_exporter_SvgExporter.stylesForShape(shape));
  1231. };
  1232. geometrize_exporter_SvgExporter.getSvgPrelude = function () {
  1233. return '<?xml version="1.0" standalone="no"?>\n';
  1234. };
  1235. geometrize_exporter_SvgExporter.getSvgNodeOpen = function (width, height) {
  1236. return '<svg xmlns="http://www.w3.org/2000/svg" version="1.2" baseProfile="tiny" width="100" height="100" viewbox="0 0 ' + width + ' ' + height + '">\n';
  1237. };
  1238. geometrize_exporter_SvgExporter.getSvgNodeClose = function () {
  1239. return "</svg>";
  1240. };
  1241. geometrize_exporter_SvgExporter.stylesForShape = function (shape) {
  1242. switch (shape.shape.getType()) {
  1243. case 6:
  1244. case 7:
  1245. return geometrize_exporter_SvgExporter.strokeForColor(shape.color) + ' stroke-width="1" fill="none" ' + geometrize_exporter_SvgExporter.strokeOpacityForAlpha(shape.color & 255);
  1246. default:
  1247. return geometrize_exporter_SvgExporter.fillForColor(shape.color) + " " + geometrize_exporter_SvgExporter.fillOpacityForAlpha(shape.color & 255);
  1248. }
  1249. };
  1250. geometrize_exporter_SvgExporter.rgbForColor = function (color) {
  1251. return "rgb(" + ((color >> 24) & 255) + "," + ((color >> 16) & 255) + "," + ((color >> 8) & 255) + ")";
  1252. };
  1253. geometrize_exporter_SvgExporter.strokeForColor = function (color) {
  1254. return 'stroke="' + geometrize_exporter_SvgExporter.rgbForColor(color) + '"';
  1255. };
  1256. geometrize_exporter_SvgExporter.fillForColor = function (color) {
  1257. return 'fill="' + geometrize_exporter_SvgExporter.rgbForColor(color) + '"';
  1258. };
  1259. geometrize_exporter_SvgExporter.fillOpacityForAlpha = function (alpha) {
  1260. return 'fill-opacity="' + alpha / 255.0 + '"';
  1261. };
  1262. geometrize_exporter_SvgExporter.strokeOpacityForAlpha = function (alpha) {
  1263. return 'stroke-opacity="' + alpha / 255.0 + '"';
  1264. };
  1265. var geometrize_rasterizer_Rasterizer = function () {};
  1266. geometrize_rasterizer_Rasterizer.__name__ = true;
  1267. geometrize_rasterizer_Rasterizer.drawLines = function (image, c, lines) {
  1268. if (image == null) {
  1269. throw haxe_Exception.thrown("FAIL: image != null");
  1270. }
  1271. if (lines == null) {
  1272. throw haxe_Exception.thrown("FAIL: lines != null");
  1273. }
  1274. var sr = (c >> 24) & 255;
  1275. sr |= sr << 8;
  1276. sr *= c & 255;
  1277. sr = (sr / 255) | 0;
  1278. var sg = (c >> 16) & 255;
  1279. sg |= sg << 8;
  1280. sg *= c & 255;
  1281. sg = (sg / 255) | 0;
  1282. var sb = (c >> 8) & 255;
  1283. sb |= sb << 8;
  1284. sb *= c & 255;
  1285. sb = (sb / 255) | 0;
  1286. var sa = c & 255;
  1287. sa |= sa << 8;
  1288. var _g = 0;
  1289. while (_g < lines.length) {
  1290. var line = lines[_g];
  1291. ++_g;
  1292. var y = line.y;
  1293. var ma = 65535;
  1294. var m = 65535;
  1295. var as = (m - sa * (ma / m)) * 257;
  1296. var a = as | 0;
  1297. var _g1 = line.x1;
  1298. var _g2 = line.x2 + 1;
  1299. while (_g1 < _g2) {
  1300. var x = _g1++;
  1301. var d = image.data[image.width * y + x];
  1302. var dr = (d >> 24) & 255;
  1303. var dg = (d >> 16) & 255;
  1304. var db = (d >> 8) & 255;
  1305. var da = d & 255;
  1306. var r = ((UInt.toFloat(dr * a + sr * ma) / UInt.toFloat(m)) | 0) >> 8;
  1307. var g = ((UInt.toFloat(dg * a + sg * ma) / UInt.toFloat(m)) | 0) >> 8;
  1308. var b = ((UInt.toFloat(db * a + sb * ma) / UInt.toFloat(m)) | 0) >> 8;
  1309. var a1 = ((UInt.toFloat(da * a + sa * ma) / UInt.toFloat(m)) | 0) >> 8;
  1310. image.data[image.width * y + x] =
  1311. ((r < 0 ? 0 : r > 255 ? 255 : r) << 24) + ((g < 0 ? 0 : g > 255 ? 255 : g) << 16) + ((b < 0 ? 0 : b > 255 ? 255 : b) << 8) + (a1 < 0 ? 0 : a1 > 255 ? 255 : a1);
  1312. }
  1313. }
  1314. };
  1315. geometrize_rasterizer_Rasterizer.copyLines = function (destination, source, lines) {
  1316. if (destination == null) {
  1317. throw haxe_Exception.thrown("FAIL: destination != null");
  1318. }
  1319. if (source == null) {
  1320. throw haxe_Exception.thrown("FAIL: source != null");
  1321. }
  1322. if (lines == null) {
  1323. throw haxe_Exception.thrown("FAIL: lines != null");
  1324. }
  1325. var _g = 0;
  1326. while (_g < lines.length) {
  1327. var line = lines[_g];
  1328. ++_g;
  1329. var y = line.y;
  1330. var _g1 = line.x1;
  1331. var _g2 = line.x2 + 1;
  1332. while (_g1 < _g2) {
  1333. var x = _g1++;
  1334. destination.data[destination.width * y + x] = source.data[source.width * y + x];
  1335. }
  1336. }
  1337. };
  1338. geometrize_rasterizer_Rasterizer.bresenham = function (x1, y1, x2, y2) {
  1339. var dx = x2 - x1;
  1340. var ix = (dx > 0 ? 1 : 0) - (dx < 0 ? 1 : 0);
  1341. dx = (dx < 0 ? -dx : dx) << 1;
  1342. var dy = y2 - y1;
  1343. var iy = (dy > 0 ? 1 : 0) - (dy < 0 ? 1 : 0);
  1344. dy = (dy < 0 ? -dy : dy) << 1;
  1345. var points = [];
  1346. points.push({ x: x1, y: y1 });
  1347. if (dx >= dy) {
  1348. var error = dy - (dx >> 1);
  1349. while (x1 != x2) {
  1350. if (error >= 0 && (error != 0 || ix > 0)) {
  1351. error -= dx;
  1352. y1 += iy;
  1353. }
  1354. error += dy;
  1355. x1 += ix;
  1356. points.push({ x: x1, y: y1 });
  1357. }
  1358. } else {
  1359. var error = dx - (dy >> 1);
  1360. while (y1 != y2) {
  1361. if (error >= 0 && (error != 0 || iy > 0)) {
  1362. error -= dy;
  1363. x1 += ix;
  1364. }
  1365. error += dx;
  1366. y1 += iy;
  1367. points.push({ x: x1, y: y1 });
  1368. }
  1369. }
  1370. return points;
  1371. };
  1372. geometrize_rasterizer_Rasterizer.scanlinesForPolygon = function (points) {
  1373. var lines = [];
  1374. var edges = [];
  1375. var _g = 0;
  1376. var _g1 = points.length;
  1377. while (_g < _g1) {
  1378. var i = _g++;
  1379. var p1 = points[i];
  1380. var p2 = i == points.length - 1 ? points[0] : points[i + 1];
  1381. var p1p2 = geometrize_rasterizer_Rasterizer.bresenham(p1.x, p1.y, p2.x, p2.y);
  1382. edges = edges.concat(p1p2);
  1383. }
  1384. var yToXs = new haxe_ds_IntMap();
  1385. var _g = 0;
  1386. while (_g < edges.length) {
  1387. var point = edges[_g];
  1388. ++_g;
  1389. var s = yToXs.h[point.y];
  1390. if (s != null) {
  1391. geometrize_ArraySet.add(s, point.x);
  1392. } else {
  1393. s = geometrize_ArraySet.create();
  1394. geometrize_ArraySet.add(s, point.x);
  1395. yToXs.h[point.y] = s;
  1396. }
  1397. }
  1398. var key = yToXs.keys();
  1399. while (key.hasNext()) {
  1400. var key1 = key.next();
  1401. var a = geometrize_ArraySet.toArray(yToXs.h[key1]);
  1402. var minMaxElements;
  1403. if (a == null || a.length == 0) {
  1404. minMaxElements = { x: 0, y: 0 };
  1405. } else {
  1406. var min = a[0];
  1407. var max = a[0];
  1408. var _g = 0;
  1409. while (_g < a.length) {
  1410. var value = a[_g];
  1411. ++_g;
  1412. if (min > value) {
  1413. min = value;
  1414. }
  1415. if (max < value) {
  1416. max = value;
  1417. }
  1418. }
  1419. minMaxElements = { x: min, y: max };
  1420. }
  1421. lines.push(new geometrize_rasterizer_Scanline(key1, minMaxElements.x, minMaxElements.y));
  1422. }
  1423. return lines;
  1424. };
  1425. var geometrize_rasterizer_Scanline = function (y, x1, x2) {
  1426. this.y = y;
  1427. this.x1 = x1;
  1428. this.x2 = x2;
  1429. };
  1430. geometrize_rasterizer_Scanline.__name__ = true;
  1431. geometrize_rasterizer_Scanline.trim = function (scanlines, w, h) {
  1432. if (scanlines == null) {
  1433. throw haxe_Exception.thrown("FAIL: scanlines != null");
  1434. }
  1435. var w1 = w;
  1436. var h1 = h;
  1437. var f = function (line) {
  1438. if (line.y < 0 || line.y >= h1 || line.x1 >= w1 || line.x2 < 0) {
  1439. return false;
  1440. } else {
  1441. var value = line.x1;
  1442. var max = w1 - 1;
  1443. if (0 > max) {
  1444. throw haxe_Exception.thrown("FAIL: min <= max");
  1445. }
  1446. line.x1 = value < 0 ? 0 : value > max ? max : value;
  1447. var value = line.x2;
  1448. var max = w1 - 1;
  1449. if (0 > max) {
  1450. throw haxe_Exception.thrown("FAIL: min <= max");
  1451. }
  1452. line.x2 = value < 0 ? 0 : value > max ? max : value;
  1453. return line.x1 <= line.x2;
  1454. }
  1455. };
  1456. var _g = [];
  1457. var _g1 = 0;
  1458. var _g2 = scanlines;
  1459. while (_g1 < _g2.length) {
  1460. var v = _g2[_g1];
  1461. ++_g1;
  1462. if (f(v)) {
  1463. _g.push(v);
  1464. }
  1465. }
  1466. return _g;
  1467. };
  1468. var geometrize_runner_ImageRunner = ($hx_exports["geometrize"]["runner"]["ImageRunner"] = function (inputImage, backgroundColor) {
  1469. this.model = null;
  1470. this.model = new geometrize_Model(inputImage, backgroundColor);
  1471. });
  1472. geometrize_runner_ImageRunner.__name__ = true;
  1473. geometrize_runner_ImageRunner.prototype = {
  1474. step: function (options) {
  1475. var finalOptions_shapeTypes = options.shapeTypes != null && options.shapeTypes.length != 0 ? options.shapeTypes : geometrize_runner_Default.options.shapeTypes;
  1476. var finalOptions_alpha = options.alpha != null ? options.alpha : geometrize_runner_Default.options.alpha;
  1477. var finalOptions_candidateShapesPerStep = options.candidateShapesPerStep != null ? options.candidateShapesPerStep : geometrize_runner_Default.options.candidateShapesPerStep;
  1478. var finalOptions_shapeMutationsPerStep = options.shapeMutationsPerStep != null ? options.shapeMutationsPerStep : geometrize_runner_Default.options.shapeMutationsPerStep;
  1479. return this.model.step(finalOptions_shapeTypes, finalOptions_alpha, finalOptions_candidateShapesPerStep, finalOptions_shapeMutationsPerStep);
  1480. },
  1481. getImageData: function () {
  1482. if (this.model == null) {
  1483. throw haxe_Exception.thrown("FAIL: model != null");
  1484. }
  1485. return this.model.current;
  1486. },
  1487. };
  1488. var geometrize_runner_Default = function () {};
  1489. geometrize_runner_Default.__name__ = true;
  1490. var geometrize_shape_Ellipse = function (xBound, yBound) {
  1491. this.x = Std.random(xBound);
  1492. this.y = Std.random(yBound);
  1493. this.rx = Std.random(32) + 1;
  1494. this.ry = Std.random(32) + 1;
  1495. this.xBound = xBound;
  1496. this.yBound = yBound;
  1497. };
  1498. geometrize_shape_Ellipse.__name__ = true;
  1499. geometrize_shape_Ellipse.prototype = {
  1500. rasterize: function () {
  1501. var lines = [];
  1502. var aspect = this.rx / this.ry;
  1503. var w = this.xBound;
  1504. var h = this.yBound;
  1505. var _g = 0;
  1506. var _g1 = this.ry;
  1507. while (_g < _g1) {
  1508. var dy = _g++;
  1509. var y1 = this.y - dy;
  1510. var y2 = this.y + dy;
  1511. if ((y1 < 0 || y1 >= h) && (y2 < 0 || y2 >= h)) {
  1512. continue;
  1513. }
  1514. var s = (Math.sqrt(this.ry * this.ry - dy * dy) * aspect) | 0;
  1515. var x1 = this.x - s;
  1516. var x2 = this.x + s;
  1517. if (x1 < 0) {
  1518. x1 = 0;
  1519. }
  1520. if (x2 >= w) {
  1521. x2 = w - 1;
  1522. }
  1523. if (y1 >= 0 && y1 < h) {
  1524. lines.push(new geometrize_rasterizer_Scanline(y1, x1, x2));
  1525. }
  1526. if (y2 >= 0 && y2 < h && dy > 0) {
  1527. lines.push(new geometrize_rasterizer_Scanline(y2, x1, x2));
  1528. }
  1529. }
  1530. return lines;
  1531. },
  1532. mutate: function () {
  1533. var r = Std.random(3);
  1534. switch (r) {
  1535. case 0:
  1536. var value = this.x + (-16 + Math.floor(33 * Math.random()));
  1537. var max = this.xBound - 1;
  1538. if (0 > max) {
  1539. throw haxe_Exception.thrown("FAIL: min <= max");
  1540. }
  1541. this.x = value < 0 ? 0 : value > max ? max : value;
  1542. var value = this.y + (-16 + Math.floor(33 * Math.random()));
  1543. var max = this.yBound - 1;
  1544. if (0 > max) {
  1545. throw haxe_Exception.thrown("FAIL: min <= max");
  1546. }
  1547. this.y = value < 0 ? 0 : value > max ? max : value;
  1548. break;
  1549. case 1:
  1550. var value = this.rx + (-16 + Math.floor(33 * Math.random()));
  1551. var max = this.xBound - 1;
  1552. if (1 > max) {
  1553. throw haxe_Exception.thrown("FAIL: min <= max");
  1554. }
  1555. this.rx = value < 1 ? 1 : value > max ? max : value;
  1556. break;
  1557. case 2:
  1558. var value = this.ry + (-16 + Math.floor(33 * Math.random()));
  1559. var max = this.xBound - 1;
  1560. if (1 > max) {
  1561. throw haxe_Exception.thrown("FAIL: min <= max");
  1562. }
  1563. this.ry = value < 1 ? 1 : value > max ? max : value;
  1564. break;
  1565. }
  1566. },
  1567. clone: function () {
  1568. var ellipse = new geometrize_shape_Ellipse(this.xBound, this.yBound);
  1569. ellipse.x = this.x;
  1570. ellipse.y = this.y;
  1571. ellipse.rx = this.rx;
  1572. ellipse.ry = this.ry;
  1573. return ellipse;
  1574. },
  1575. getType: function () {
  1576. return 3;
  1577. },
  1578. getRawShapeData: function () {
  1579. return [this.x, this.y, this.rx, this.ry];
  1580. },
  1581. getSvgShapeData: function () {
  1582. return '<ellipse cx="' + this.x + '" cy="' + this.y + '" rx="' + this.rx + '" ry="' + this.ry + '" ' + geometrize_exporter_SvgExporter.SVG_STYLE_HOOK + " />";
  1583. },
  1584. };
  1585. var geometrize_shape_Circle = function (xBound, yBound) {
  1586. geometrize_shape_Ellipse.call(this, xBound, yBound);
  1587. this.rx = Std.random(32) + 1;
  1588. this.ry = this.rx;
  1589. };
  1590. geometrize_shape_Circle.__name__ = true;
  1591. geometrize_shape_Circle.__super__ = geometrize_shape_Ellipse;
  1592. geometrize_shape_Circle.prototype = $extend(geometrize_shape_Ellipse.prototype, {
  1593. mutate: function () {
  1594. var r = Std.random(2);
  1595. switch (r) {
  1596. case 0:
  1597. var value = this.x + (-16 + Math.floor(33 * Math.random()));
  1598. var max = this.xBound - 1;
  1599. if (0 > max) {
  1600. throw haxe_Exception.thrown("FAIL: min <= max");
  1601. }
  1602. this.x = value < 0 ? 0 : value > max ? max : value;
  1603. var value = this.y + (-16 + Math.floor(33 * Math.random()));
  1604. var max = this.yBound - 1;
  1605. if (0 > max) {
  1606. throw haxe_Exception.thrown("FAIL: min <= max");
  1607. }
  1608. this.y = value < 0 ? 0 : value > max ? max : value;
  1609. break;
  1610. case 1:
  1611. var value = this.rx + (-16 + Math.floor(33 * Math.random()));
  1612. var max = this.xBound - 1;
  1613. if (1 > max) {
  1614. throw haxe_Exception.thrown("FAIL: min <= max");
  1615. }
  1616. var r = value < 1 ? 1 : value > max ? max : value;
  1617. this.rx = r;
  1618. this.ry = r;
  1619. break;
  1620. }
  1621. },
  1622. clone: function () {
  1623. var circle = new geometrize_shape_Circle(this.xBound, this.yBound);
  1624. circle.x = this.x;
  1625. circle.y = this.y;
  1626. circle.rx = this.rx;
  1627. circle.ry = this.ry;
  1628. return circle;
  1629. },
  1630. getType: function () {
  1631. return 5;
  1632. },
  1633. getRawShapeData: function () {
  1634. return [this.x, this.y, this.rx];
  1635. },
  1636. getSvgShapeData: function () {
  1637. return '<circle cx="' + this.x + '" cy="' + this.y + '" r="' + this.rx + '" ' + geometrize_exporter_SvgExporter.SVG_STYLE_HOOK + " />";
  1638. },
  1639. });
  1640. var geometrize_shape_Line = function (xBound, yBound) {
  1641. this.x1 = Std.random(xBound);
  1642. this.y1 = Std.random(yBound);
  1643. var value = this.x1 + Std.random(32) + 1;
  1644. if (0 > xBound) {
  1645. throw haxe_Exception.thrown("FAIL: min <= max");
  1646. }
  1647. this.x2 = value < 0 ? 0 : value > xBound ? xBound : value;
  1648. var value = this.y1 + Std.random(32) + 1;
  1649. if (0 > yBound) {
  1650. throw haxe_Exception.thrown("FAIL: min <= max");
  1651. }
  1652. this.y2 = value < 0 ? 0 : value > yBound ? yBound : value;
  1653. this.xBound = xBound;
  1654. this.yBound = yBound;
  1655. };
  1656. geometrize_shape_Line.__name__ = true;
  1657. geometrize_shape_Line.prototype = {
  1658. rasterize: function () {
  1659. var lines = [];
  1660. var points = geometrize_rasterizer_Rasterizer.bresenham(this.x1, this.y1, this.x2, this.y2);
  1661. var _g = 0;
  1662. while (_g < points.length) {
  1663. var point = points[_g];
  1664. ++_g;
  1665. lines.push(new geometrize_rasterizer_Scanline(point.y, point.x, point.x));
  1666. }
  1667. return geometrize_rasterizer_Scanline.trim(lines, this.xBound, this.yBound);
  1668. },
  1669. mutate: function () {
  1670. var r = Std.random(4);
  1671. switch (r) {
  1672. case 0:
  1673. var value = this.x1 + (-16 + Math.floor(33 * Math.random()));
  1674. var max = this.xBound - 1;
  1675. if (0 > max) {
  1676. throw haxe_Exception.thrown("FAIL: min <= max");
  1677. }
  1678. this.x1 = value < 0 ? 0 : value > max ? max : value;
  1679. var value = this.y1 + (-16 + Math.floor(33 * Math.random()));
  1680. var max = this.yBound - 1;
  1681. if (0 > max) {
  1682. throw haxe_Exception.thrown("FAIL: min <= max");
  1683. }
  1684. this.y1 = value < 0 ? 0 : value > max ? max : value;
  1685. break;
  1686. case 1:
  1687. var value = this.x2 + (-16 + Math.floor(33 * Math.random()));
  1688. var max = this.xBound - 1;
  1689. if (0 > max) {
  1690. throw haxe_Exception.thrown("FAIL: min <= max");
  1691. }
  1692. this.x2 = value < 0 ? 0 : value > max ? max : value;
  1693. var value = this.y2 + (-16 + Math.floor(33 * Math.random()));
  1694. var max = this.yBound - 1;
  1695. if (0 > max) {
  1696. throw haxe_Exception.thrown("FAIL: min <= max");
  1697. }
  1698. this.y2 = value < 0 ? 0 : value > max ? max : value;
  1699. break;
  1700. }
  1701. },
  1702. clone: function () {
  1703. var line = new geometrize_shape_Line(this.xBound, this.yBound);
  1704. line.x1 = this.x1;
  1705. line.y1 = this.y1;
  1706. line.x2 = this.x2;
  1707. line.y2 = this.y2;
  1708. return line;
  1709. },
  1710. getType: function () {
  1711. return 6;
  1712. },
  1713. getRawShapeData: function () {
  1714. return [this.x1, this.y1, this.x2, this.y2];
  1715. },
  1716. getSvgShapeData: function () {
  1717. return '<line x1="' + this.x1 + '" y1="' + this.y1 + '" x2="' + this.x2 + '" y2="' + this.y2 + '" ' + geometrize_exporter_SvgExporter.SVG_STYLE_HOOK + " />";
  1718. },
  1719. };
  1720. var geometrize_shape_QuadraticBezier = function (xBound, yBound) {
  1721. var upper = xBound - 1;
  1722. if (0 > upper) {
  1723. throw haxe_Exception.thrown("FAIL: lower <= upper");
  1724. }
  1725. this.x1 = Math.floor((upper + 1) * Math.random());
  1726. var upper = yBound - 1;
  1727. if (0 > upper) {
  1728. throw haxe_Exception.thrown("FAIL: lower <= upper");
  1729. }
  1730. this.y1 = Math.floor((upper + 1) * Math.random());
  1731. var upper = xBound - 1;
  1732. if (0 > upper) {
  1733. throw haxe_Exception.thrown("FAIL: lower <= upper");
  1734. }
  1735. this.cx = Math.floor((upper + 1) * Math.random());
  1736. var upper = yBound - 1;
  1737. if (0 > upper) {
  1738. throw haxe_Exception.thrown("FAIL: lower <= upper");
  1739. }
  1740. this.cy = Math.floor((upper + 1) * Math.random());
  1741. var upper = xBound - 1;
  1742. if (0 > upper) {
  1743. throw haxe_Exception.thrown("FAIL: lower <= upper");
  1744. }
  1745. this.x2 = Math.floor((upper + 1) * Math.random());
  1746. var upper = yBound - 1;
  1747. if (0 > upper) {
  1748. throw haxe_Exception.thrown("FAIL: lower <= upper");
  1749. }
  1750. this.y2 = Math.floor((upper + 1) * Math.random());
  1751. this.xBound = xBound;
  1752. this.yBound = yBound;
  1753. };
  1754. geometrize_shape_QuadraticBezier.__name__ = true;
  1755. geometrize_shape_QuadraticBezier.prototype = {
  1756. rasterize: function () {
  1757. var lines = [];
  1758. var points = [];
  1759. var pointCount = 20;
  1760. var _g = 0;
  1761. var _g1 = pointCount - 1;
  1762. while (_g < _g1) {
  1763. var i = _g++;
  1764. var t = i / pointCount;
  1765. var tp = 1 - t;
  1766. var x = (tp * (tp * this.x1 + t * this.cx) + t * (tp * this.cx + t * this.x2)) | 0;
  1767. var y = (tp * (tp * this.y1 + t * this.cy) + t * (tp * this.cy + t * this.y2)) | 0;
  1768. points.push({ x: x, y: y });
  1769. }
  1770. var _g = 0;
  1771. var _g1 = points.length - 1;
  1772. while (_g < _g1) {
  1773. var i = _g++;
  1774. var p0 = points[i];
  1775. var p1 = points[i + 1];
  1776. var pts = geometrize_rasterizer_Rasterizer.bresenham(p0.x, p0.y, p1.x, p1.y);
  1777. var _g2 = 0;
  1778. while (_g2 < pts.length) {
  1779. var point = pts[_g2];
  1780. ++_g2;
  1781. if (lines.length > 0) {
  1782. var lastLine = lines[lines.length - 1];
  1783. if (lastLine.y == point.y && lastLine.x1 == point.x && lastLine.x2 == point.x) {
  1784. continue;
  1785. }
  1786. }
  1787. lines.push(new geometrize_rasterizer_Scanline(point.y, point.x, point.x));
  1788. }
  1789. }
  1790. return geometrize_rasterizer_Scanline.trim(lines, this.xBound, this.yBound);
  1791. },
  1792. mutate: function () {
  1793. var r = Math.floor(3 * Math.random());
  1794. switch (r) {
  1795. case 0:
  1796. var value = this.cx + (-8 + Math.floor(17 * Math.random()));
  1797. var max = this.xBound - 1;
  1798. if (0 > max) {
  1799. throw haxe_Exception.thrown("FAIL: min <= max");
  1800. }
  1801. this.cx = value < 0 ? 0 : value > max ? max : value;
  1802. var value = this.cy + (-8 + Math.floor(17 * Math.random()));
  1803. var max = this.yBound - 1;
  1804. if (0 > max) {
  1805. throw haxe_Exception.thrown("FAIL: min <= max");
  1806. }
  1807. this.cy = value < 0 ? 0 : value > max ? max : value;
  1808. break;
  1809. case 1:
  1810. var value = this.x1 + (-8 + Math.floor(17 * Math.random()));
  1811. var max = this.xBound - 1;
  1812. if (1 > max) {
  1813. throw haxe_Exception.thrown("FAIL: min <= max");
  1814. }
  1815. this.x1 = value < 1 ? 1 : value > max ? max : value;
  1816. var value = this.y1 + (-8 + Math.floor(17 * Math.random()));
  1817. var max = this.yBound - 1;
  1818. if (1 > max) {
  1819. throw haxe_Exception.thrown("FAIL: min <= max");
  1820. }
  1821. this.y1 = value < 1 ? 1 : value > max ? max : value;
  1822. break;
  1823. case 2:
  1824. var value = this.x2 + (-8 + Math.floor(17 * Math.random()));
  1825. var max = this.xBound - 1;
  1826. if (1 > max) {
  1827. throw haxe_Exception.thrown("FAIL: min <= max");
  1828. }
  1829. this.x2 = value < 1 ? 1 : value > max ? max : value;
  1830. var value = this.y2 + (-8 + Math.floor(17 * Math.random()));
  1831. var max = this.yBound - 1;
  1832. if (1 > max) {
  1833. throw haxe_Exception.thrown("FAIL: min <= max");
  1834. }
  1835. this.y2 = value < 1 ? 1 : value > max ? max : value;
  1836. break;
  1837. }
  1838. },
  1839. clone: function () {
  1840. var bezier = new geometrize_shape_QuadraticBezier(this.xBound, this.yBound);
  1841. bezier.cx = this.cx;
  1842. bezier.cy = this.cy;
  1843. bezier.x1 = this.x1;
  1844. bezier.y1 = this.y1;
  1845. bezier.x2 = this.x2;
  1846. bezier.y2 = this.y2;
  1847. return bezier;
  1848. },
  1849. getType: function () {
  1850. return 7;
  1851. },
  1852. getRawShapeData: function () {
  1853. return [this.x1, this.y1, this.cx, this.cy, this.x2, this.y2];
  1854. },
  1855. getSvgShapeData: function () {
  1856. return '<path d="M' + this.x1 + " " + this.y1 + " Q " + this.cx + " " + this.cy + " " + this.x2 + " " + this.y2 + '" ' + geometrize_exporter_SvgExporter.SVG_STYLE_HOOK + " />";
  1857. },
  1858. };
  1859. var geometrize_shape_Rectangle = function (xBound, yBound) {
  1860. this.x1 = Std.random(xBound);
  1861. this.y1 = Std.random(yBound);
  1862. var value = this.x1 + Std.random(32) + 1;
  1863. var max = xBound - 1;
  1864. if (0 > max) {
  1865. throw haxe_Exception.thrown("FAIL: min <= max");
  1866. }
  1867. this.x2 = value < 0 ? 0 : value > max ? max : value;
  1868. var value = this.y1 + Std.random(32) + 1;
  1869. var max = yBound - 1;
  1870. if (0 > max) {
  1871. throw haxe_Exception.thrown("FAIL: min <= max");
  1872. }
  1873. this.y2 = value < 0 ? 0 : value > max ? max : value;
  1874. this.xBound = xBound;
  1875. this.yBound = yBound;
  1876. };
  1877. geometrize_shape_Rectangle.__name__ = true;
  1878. geometrize_shape_Rectangle.prototype = {
  1879. rasterize: function () {
  1880. var lines = [];
  1881. var first = this.y1;
  1882. var second = this.y2;
  1883. var yMin = first < second ? first : second;
  1884. var first = this.y1;
  1885. var second = this.y2;
  1886. var yMax = first > second ? first : second;
  1887. if (yMin == yMax) {
  1888. var first = this.x1;
  1889. var second = this.x2;
  1890. var first1 = this.x1;
  1891. var second1 = this.x2;
  1892. lines.push(new geometrize_rasterizer_Scanline(yMin, first < second ? first : second, first1 > second1 ? first1 : second1));
  1893. } else {
  1894. var _g = yMin;
  1895. var _g1 = yMax;
  1896. while (_g < _g1) {
  1897. var y = _g++;
  1898. var first = this.x1;
  1899. var second = this.x2;
  1900. var first1 = this.x1;
  1901. var second1 = this.x2;
  1902. lines.push(new geometrize_rasterizer_Scanline(y, first < second ? first : second, first1 > second1 ? first1 : second1));
  1903. }
  1904. }
  1905. return lines;
  1906. },
  1907. mutate: function () {
  1908. var r = Std.random(2);
  1909. switch (r) {
  1910. case 0:
  1911. var value = this.x1 + (-16 + Math.floor(33 * Math.random()));
  1912. var max = this.xBound - 1;
  1913. if (0 > max) {
  1914. throw haxe_Exception.thrown("FAIL: min <= max");
  1915. }
  1916. this.x1 = value < 0 ? 0 : value > max ? max : value;
  1917. var value = this.y1 + (-16 + Math.floor(33 * Math.random()));
  1918. var max = this.yBound - 1;
  1919. if (0 > max) {
  1920. throw haxe_Exception.thrown("FAIL: min <= max");
  1921. }
  1922. this.y1 = value < 0 ? 0 : value > max ? max : value;
  1923. break;
  1924. case 1:
  1925. var value = this.x2 + (-16 + Math.floor(33 * Math.random()));
  1926. var max = this.xBound - 1;
  1927. if (0 > max) {
  1928. throw haxe_Exception.thrown("FAIL: min <= max");
  1929. }
  1930. this.x2 = value < 0 ? 0 : value > max ? max : value;
  1931. var value = this.y2 + (-16 + Math.floor(33 * Math.random()));
  1932. var max = this.yBound - 1;
  1933. if (0 > max) {
  1934. throw haxe_Exception.thrown("FAIL: min <= max");
  1935. }
  1936. this.y2 = value < 0 ? 0 : value > max ? max : value;
  1937. break;
  1938. }
  1939. },
  1940. clone: function () {
  1941. var rectangle = new geometrize_shape_Rectangle(this.xBound, this.yBound);
  1942. rectangle.x1 = this.x1;
  1943. rectangle.y1 = this.y1;
  1944. rectangle.x2 = this.x2;
  1945. rectangle.y2 = this.y2;
  1946. return rectangle;
  1947. },
  1948. getType: function () {
  1949. return 0;
  1950. },
  1951. getRawShapeData: function () {
  1952. var first = this.x1;
  1953. var second = this.x2;
  1954. var first1 = this.y1;
  1955. var second1 = this.y2;
  1956. var first2 = this.x1;
  1957. var second2 = this.x2;
  1958. var first3 = this.y1;
  1959. var second3 = this.y2;
  1960. return [first < second ? first : second, first1 < second1 ? first1 : second1, first2 > second2 ? first2 : second2, first3 > second3 ? first3 : second3];
  1961. },
  1962. getSvgShapeData: function () {
  1963. var first = this.x1;
  1964. var second = this.x2;
  1965. var first1 = this.y1;
  1966. var second1 = this.y2;
  1967. var first2 = this.x1;
  1968. var second2 = this.x2;
  1969. var first3 = this.x1;
  1970. var second3 = this.x2;
  1971. var first4 = this.y1;
  1972. var second4 = this.y2;
  1973. var first5 = this.y1;
  1974. var second5 = this.y2;
  1975. return (
  1976. '<rect x="' +
  1977. (first < second ? first : second) +
  1978. '" y="' +
  1979. (first1 < second1 ? first1 : second1) +
  1980. '" width="' +
  1981. ((first2 > second2 ? first2 : second2) - (first3 < second3 ? first3 : second3)) +
  1982. '" height="' +
  1983. ((first4 > second4 ? first4 : second4) - (first5 < second5 ? first5 : second5)) +
  1984. '" ' +
  1985. geometrize_exporter_SvgExporter.SVG_STYLE_HOOK +
  1986. " />"
  1987. );
  1988. },
  1989. };
  1990. var geometrize_shape_RotatedEllipse = function (xBound, yBound) {
  1991. this.x = Std.random(xBound);
  1992. this.y = Std.random(yBound);
  1993. this.rx = Std.random(32) + 1;
  1994. this.ry = Std.random(32) + 1;
  1995. this.angle = Std.random(360);
  1996. this.xBound = xBound;
  1997. this.yBound = yBound;
  1998. };
  1999. geometrize_shape_RotatedEllipse.__name__ = true;
  2000. geometrize_shape_RotatedEllipse.prototype = {
  2001. rasterize: function () {
  2002. var pointCount = 20;
  2003. var points = [];
  2004. var rads = this.angle * (Math.PI / 180.0);
  2005. var c = Math.cos(rads);
  2006. var s = Math.sin(rads);
  2007. var _g = 0;
  2008. var _g1 = pointCount;
  2009. while (_g < _g1) {
  2010. var i = _g++;
  2011. var rot = (360.0 / pointCount) * i * (Math.PI / 180.0);
  2012. var crx = this.rx * Math.cos(rot);
  2013. var cry = this.ry * Math.sin(rot);
  2014. var tx = (crx * c - cry * s + this.x) | 0;
  2015. var ty = (crx * s + cry * c + this.y) | 0;
  2016. points.push({ x: tx, y: ty });
  2017. }
  2018. return geometrize_rasterizer_Scanline.trim(geometrize_rasterizer_Rasterizer.scanlinesForPolygon(points), this.xBound, this.yBound);
  2019. },
  2020. mutate: function () {
  2021. var r = Std.random(4);
  2022. switch (r) {
  2023. case 0:
  2024. var value = this.x + (-16 + Math.floor(33 * Math.random()));
  2025. var max = this.xBound - 1;
  2026. if (0 > max) {
  2027. throw haxe_Exception.thrown("FAIL: min <= max");
  2028. }
  2029. this.x = value < 0 ? 0 : value > max ? max : value;
  2030. var value = this.y + (-16 + Math.floor(33 * Math.random()));
  2031. var max = this.yBound - 1;
  2032. if (0 > max) {
  2033. throw haxe_Exception.thrown("FAIL: min <= max");
  2034. }
  2035. this.y = value < 0 ? 0 : value > max ? max : value;
  2036. break;
  2037. case 1:
  2038. var value = this.rx + (-16 + Math.floor(33 * Math.random()));
  2039. var max = this.xBound - 1;
  2040. if (1 > max) {
  2041. throw haxe_Exception.thrown("FAIL: min <= max");
  2042. }
  2043. this.rx = value < 1 ? 1 : value > max ? max : value;
  2044. break;
  2045. case 2:
  2046. var value = this.ry + (-16 + Math.floor(33 * Math.random()));
  2047. var max = this.yBound - 1;
  2048. if (1 > max) {
  2049. throw haxe_Exception.thrown("FAIL: min <= max");
  2050. }
  2051. this.ry = value < 1 ? 1 : value > max ? max : value;
  2052. break;
  2053. case 3:
  2054. var value = this.angle + (-4 + Math.floor(9 * Math.random()));
  2055. this.angle = value < 0 ? 0 : value > 360 ? 360 : value;
  2056. break;
  2057. }
  2058. },
  2059. clone: function () {
  2060. var ellipse = new geometrize_shape_RotatedEllipse(this.xBound, this.yBound);
  2061. ellipse.x = this.x;
  2062. ellipse.y = this.y;
  2063. ellipse.rx = this.rx;
  2064. ellipse.ry = this.ry;
  2065. ellipse.angle = this.angle;
  2066. return ellipse;
  2067. },
  2068. getType: function () {
  2069. return 4;
  2070. },
  2071. getRawShapeData: function () {
  2072. return [this.x, this.y, this.rx, this.ry, this.angle];
  2073. },
  2074. getSvgShapeData: function () {
  2075. var s = '<g transform="translate(' + this.x + " " + this.y + ") rotate(" + this.angle + ") scale(" + this.rx + " " + this.ry + ')">';
  2076. s += '<ellipse cx="' + 0 + '" cy="' + 0 + '" rx="' + 1 + '" ry="' + 1 + '" ' + geometrize_exporter_SvgExporter.SVG_STYLE_HOOK + " />";
  2077. s += "</g>";
  2078. return s;
  2079. },
  2080. };
  2081. var geometrize_shape_RotatedRectangle = function (xBound, yBound) {
  2082. this.x1 = Std.random(xBound);
  2083. this.y1 = Std.random(yBound);
  2084. var value = this.x1 + Std.random(32) + 1;
  2085. if (0 > xBound) {
  2086. throw haxe_Exception.thrown("FAIL: min <= max");
  2087. }
  2088. this.x2 = value < 0 ? 0 : value > xBound ? xBound : value;
  2089. var value = this.y1 + Std.random(32) + 1;
  2090. if (0 > yBound) {
  2091. throw haxe_Exception.thrown("FAIL: min <= max");
  2092. }
  2093. this.y2 = value < 0 ? 0 : value > yBound ? yBound : value;
  2094. this.angle = Math.floor(361 * Math.random());
  2095. this.xBound = xBound;
  2096. this.yBound = yBound;
  2097. };
  2098. geometrize_shape_RotatedRectangle.__name__ = true;
  2099. geometrize_shape_RotatedRectangle.prototype = {
  2100. rasterize: function () {
  2101. var first = this.x1;
  2102. var second = this.x2;
  2103. var xm1 = first < second ? first : second;
  2104. var first = this.x1;
  2105. var second = this.x2;
  2106. var xm2 = first > second ? first : second;
  2107. var first = this.y1;
  2108. var second = this.y2;
  2109. var ym1 = first < second ? first : second;
  2110. var first = this.y1;
  2111. var second = this.y2;
  2112. var ym2 = first > second ? first : second;
  2113. var cx = ((xm1 + xm2) / 2) | 0;
  2114. var cy = ((ym1 + ym2) / 2) | 0;
  2115. var ox1 = xm1 - cx;
  2116. var ox2 = xm2 - cx;
  2117. var oy1 = ym1 - cy;
  2118. var oy2 = ym2 - cy;
  2119. var rads = (this.angle * Math.PI) / 180.0;
  2120. var c = Math.cos(rads);
  2121. var s = Math.sin(rads);
  2122. var ulx = (ox1 * c - oy1 * s + cx) | 0;
  2123. var uly = (ox1 * s + oy1 * c + cy) | 0;
  2124. var blx = (ox1 * c - oy2 * s + cx) | 0;
  2125. var bly = (ox1 * s + oy2 * c + cy) | 0;
  2126. var urx = (ox2 * c - oy1 * s + cx) | 0;
  2127. var ury = (ox2 * s + oy1 * c + cy) | 0;
  2128. var brx = (ox2 * c - oy2 * s + cx) | 0;
  2129. var bry = (ox2 * s + oy2 * c + cy) | 0;
  2130. return geometrize_rasterizer_Scanline.trim(
  2131. geometrize_rasterizer_Rasterizer.scanlinesForPolygon([
  2132. { x: ulx, y: uly },
  2133. { x: urx, y: ury },
  2134. { x: brx, y: bry },
  2135. { x: blx, y: bly },
  2136. ]),
  2137. this.xBound,
  2138. this.yBound
  2139. );
  2140. },
  2141. mutate: function () {
  2142. var r = Std.random(3);
  2143. switch (r) {
  2144. case 0:
  2145. var value = this.x1 + (-16 + Math.floor(33 * Math.random()));
  2146. var max = this.xBound - 1;
  2147. if (0 > max) {
  2148. throw haxe_Exception.thrown("FAIL: min <= max");
  2149. }
  2150. this.x1 = value < 0 ? 0 : value > max ? max : value;
  2151. var value = this.y1 + (-16 + Math.floor(33 * Math.random()));
  2152. var max = this.yBound - 1;
  2153. if (0 > max) {
  2154. throw haxe_Exception.thrown("FAIL: min <= max");
  2155. }
  2156. this.y1 = value < 0 ? 0 : value > max ? max : value;
  2157. break;
  2158. case 1:
  2159. var value = this.x2 + (-16 + Math.floor(33 * Math.random()));
  2160. var max = this.xBound - 1;
  2161. if (0 > max) {
  2162. throw haxe_Exception.thrown("FAIL: min <= max");
  2163. }
  2164. this.x2 = value < 0 ? 0 : value > max ? max : value;
  2165. var value = this.y2 + (-16 + Math.floor(33 * Math.random()));
  2166. var max = this.yBound - 1;
  2167. if (0 > max) {
  2168. throw haxe_Exception.thrown("FAIL: min <= max");
  2169. }
  2170. this.y2 = value < 0 ? 0 : value > max ? max : value;
  2171. break;
  2172. case 2:
  2173. var value = this.angle + (-4 + Math.floor(9 * Math.random()));
  2174. this.angle = value < 0 ? 0 : value > 360 ? 360 : value;
  2175. break;
  2176. }
  2177. },
  2178. clone: function () {
  2179. var rectangle = new geometrize_shape_RotatedRectangle(this.xBound, this.yBound);
  2180. rectangle.x1 = this.x1;
  2181. rectangle.y1 = this.y1;
  2182. rectangle.x2 = this.x2;
  2183. rectangle.y2 = this.y2;
  2184. rectangle.angle = this.angle;
  2185. return rectangle;
  2186. },
  2187. getType: function () {
  2188. return 1;
  2189. },
  2190. getRawShapeData: function () {
  2191. var first = this.x1;
  2192. var second = this.x2;
  2193. var first1 = this.y1;
  2194. var second1 = this.y2;
  2195. var first2 = this.x1;
  2196. var second2 = this.x2;
  2197. var first3 = this.y1;
  2198. var second3 = this.y2;
  2199. return [first < second ? first : second, first1 < second1 ? first1 : second1, first2 > second2 ? first2 : second2, first3 > second3 ? first3 : second3, this.angle];
  2200. },
  2201. getSvgShapeData: function () {
  2202. var first = this.x1;
  2203. var second = this.x2;
  2204. var xm1 = first < second ? first : second;
  2205. var first = this.x1;
  2206. var second = this.x2;
  2207. var xm2 = first > second ? first : second;
  2208. var first = this.y1;
  2209. var second = this.y2;
  2210. var ym1 = first < second ? first : second;
  2211. var first = this.y1;
  2212. var second = this.y2;
  2213. var ym2 = first > second ? first : second;
  2214. var cx = ((xm1 + xm2) / 2) | 0;
  2215. var cy = ((ym1 + ym2) / 2) | 0;
  2216. var ox1 = xm1 - cx;
  2217. var ox2 = xm2 - cx;
  2218. var oy1 = ym1 - cy;
  2219. var oy2 = ym2 - cy;
  2220. var rads = (this.angle * Math.PI) / 180.0;
  2221. var c = Math.cos(rads);
  2222. var s = Math.sin(rads);
  2223. var ulx = (ox1 * c - oy1 * s + cx) | 0;
  2224. var uly = (ox1 * s + oy1 * c + cy) | 0;
  2225. var blx = (ox1 * c - oy2 * s + cx) | 0;
  2226. var bly = (ox1 * s + oy2 * c + cy) | 0;
  2227. var urx = (ox2 * c - oy1 * s + cx) | 0;
  2228. var ury = (ox2 * s + oy1 * c + cy) | 0;
  2229. var brx = (ox2 * c - oy2 * s + cx) | 0;
  2230. var bry = (ox2 * s + oy2 * c + cy) | 0;
  2231. var points = [
  2232. { x: ulx, y: uly },
  2233. { x: urx, y: ury },
  2234. { x: brx, y: bry },
  2235. { x: blx, y: bly },
  2236. ];
  2237. var s = '<polygon points="';
  2238. var _g = 0;
  2239. var _g1 = points.length;
  2240. while (_g < _g1) {
  2241. var i = _g++;
  2242. s += points[i].x + " " + points[i].y;
  2243. if (i != points.length - 1) {
  2244. s += " ";
  2245. }
  2246. }
  2247. s += '" ' + geometrize_exporter_SvgExporter.SVG_STYLE_HOOK + "/>";
  2248. return s;
  2249. },
  2250. };
  2251. var geometrize_shape_ShapeFactory = function () {};
  2252. geometrize_shape_ShapeFactory.__name__ = true;
  2253. geometrize_shape_ShapeFactory.create = function (type, xBound, yBound) {
  2254. switch (type) {
  2255. case 0:
  2256. return new geometrize_shape_Rectangle(xBound, yBound);
  2257. case 1:
  2258. return new geometrize_shape_RotatedRectangle(xBound, yBound);
  2259. case 2:
  2260. return new geometrize_shape_Triangle(xBound, yBound);
  2261. case 3:
  2262. return new geometrize_shape_Ellipse(xBound, yBound);
  2263. case 4:
  2264. return new geometrize_shape_RotatedEllipse(xBound, yBound);
  2265. case 5:
  2266. return new geometrize_shape_Circle(xBound, yBound);
  2267. case 6:
  2268. return new geometrize_shape_Line(xBound, yBound);
  2269. case 7:
  2270. return new geometrize_shape_QuadraticBezier(xBound, yBound);
  2271. }
  2272. };
  2273. geometrize_shape_ShapeFactory.randomShapeOf = function (types, xBound, yBound) {
  2274. if (!(types != null && types.length > 0)) {
  2275. throw haxe_Exception.thrown("FAIL: a != null && a.length > 0");
  2276. }
  2277. var upper = types.length - 1;
  2278. if (0 > upper) {
  2279. throw haxe_Exception.thrown("FAIL: lower <= upper");
  2280. }
  2281. return geometrize_shape_ShapeFactory.create(types[Math.floor((upper + 1) * Math.random())], xBound, yBound);
  2282. };
  2283. var geometrize_shape_ShapeTypes = ($hx_exports["geometrize"]["shape"]["ShapeTypes"] = function () {});
  2284. geometrize_shape_ShapeTypes.__name__ = true;
  2285. var geometrize_shape_Triangle = function (xBound, yBound) {
  2286. this.x1 = Std.random(xBound);
  2287. this.y1 = Std.random(yBound);
  2288. this.x2 = this.x1 + (-16 + Math.floor(33 * Math.random()));
  2289. this.y2 = this.y1 + (-16 + Math.floor(33 * Math.random()));
  2290. this.x3 = this.x1 + (-16 + Math.floor(33 * Math.random()));
  2291. this.y3 = this.y1 + (-16 + Math.floor(33 * Math.random()));
  2292. this.xBound = xBound;
  2293. this.yBound = yBound;
  2294. };
  2295. geometrize_shape_Triangle.__name__ = true;
  2296. geometrize_shape_Triangle.prototype = {
  2297. rasterize: function () {
  2298. return geometrize_rasterizer_Scanline.trim(
  2299. geometrize_rasterizer_Rasterizer.scanlinesForPolygon([
  2300. { x: this.x1, y: this.y1 },
  2301. { x: this.x2, y: this.y2 },
  2302. { x: this.x3, y: this.y3 },
  2303. ]),
  2304. this.xBound,
  2305. this.yBound
  2306. );
  2307. },
  2308. mutate: function () {
  2309. var r = Std.random(3);
  2310. switch (r) {
  2311. case 0:
  2312. var value = this.x1 + (-16 + Math.floor(33 * Math.random()));
  2313. var max = this.xBound - 1;
  2314. if (0 > max) {
  2315. throw haxe_Exception.thrown("FAIL: min <= max");
  2316. }
  2317. this.x1 = value < 0 ? 0 : value > max ? max : value;
  2318. var value = this.y1 + (-16 + Math.floor(33 * Math.random()));
  2319. var max = this.yBound - 1;
  2320. if (0 > max) {
  2321. throw haxe_Exception.thrown("FAIL: min <= max");
  2322. }
  2323. this.y1 = value < 0 ? 0 : value > max ? max : value;
  2324. break;
  2325. case 1:
  2326. var value = this.x2 + (-16 + Math.floor(33 * Math.random()));
  2327. var max = this.xBound - 1;
  2328. if (0 > max) {
  2329. throw haxe_Exception.thrown("FAIL: min <= max");
  2330. }
  2331. this.x2 = value < 0 ? 0 : value > max ? max : value;
  2332. var value = this.y2 + (-16 + Math.floor(33 * Math.random()));
  2333. var max = this.yBound - 1;
  2334. if (0 > max) {
  2335. throw haxe_Exception.thrown("FAIL: min <= max");
  2336. }
  2337. this.y2 = value < 0 ? 0 : value > max ? max : value;
  2338. break;
  2339. case 2:
  2340. var value = this.x3 + (-16 + Math.floor(33 * Math.random()));
  2341. var max = this.xBound - 1;
  2342. if (0 > max) {
  2343. throw haxe_Exception.thrown("FAIL: min <= max");
  2344. }
  2345. this.x3 = value < 0 ? 0 : value > max ? max : value;
  2346. var value = this.y3 + (-16 + Math.floor(33 * Math.random()));
  2347. var max = this.yBound - 1;
  2348. if (0 > max) {
  2349. throw haxe_Exception.thrown("FAIL: min <= max");
  2350. }
  2351. this.y3 = value < 0 ? 0 : value > max ? max : value;
  2352. break;
  2353. }
  2354. },
  2355. clone: function () {
  2356. var triangle = new geometrize_shape_Triangle(this.xBound, this.yBound);
  2357. triangle.x1 = this.x1;
  2358. triangle.y1 = this.y1;
  2359. triangle.x2 = this.x2;
  2360. triangle.y2 = this.y2;
  2361. triangle.x3 = this.x3;
  2362. triangle.y3 = this.y3;
  2363. return triangle;
  2364. },
  2365. getType: function () {
  2366. return 2;
  2367. },
  2368. getRawShapeData: function () {
  2369. return [this.x1, this.y1, this.x2, this.y2, this.x3, this.y3];
  2370. },
  2371. getSvgShapeData: function () {
  2372. return '<polygon points="' + this.x1 + "," + this.y1 + " " + this.x2 + "," + this.y2 + " " + this.x3 + "," + this.y3 + '" ' + geometrize_exporter_SvgExporter.SVG_STYLE_HOOK + "/>";
  2373. },
  2374. };
  2375. var haxe_Exception = function (message, previous, native) {
  2376. Error.call(this, message);
  2377. this.message = message;
  2378. this.__previousException = previous;
  2379. this.__nativeException = native != null ? native : this;
  2380. };
  2381. haxe_Exception.__name__ = true;
  2382. haxe_Exception.thrown = function (value) {
  2383. if (value instanceof haxe_Exception) {
  2384. return value.get_native();
  2385. } else if (value instanceof Error) {
  2386. return value;
  2387. } else {
  2388. var e = new haxe_ValueException(value);
  2389. return e;
  2390. }
  2391. };
  2392. haxe_Exception.__super__ = Error;
  2393. haxe_Exception.prototype = $extend(Error.prototype, {
  2394. get_native: function () {
  2395. return this.__nativeException;
  2396. },
  2397. });
  2398. var haxe_ValueException = function (value, previous, native) {
  2399. haxe_Exception.call(this, String(value), previous, native);
  2400. this.value = value;
  2401. };
  2402. haxe_ValueException.__name__ = true;
  2403. haxe_ValueException.__super__ = haxe_Exception;
  2404. haxe_ValueException.prototype = $extend(haxe_Exception.prototype, {});
  2405. var haxe_ds_IntMap = function () {
  2406. this.h = {};
  2407. };
  2408. haxe_ds_IntMap.__name__ = true;
  2409. haxe_ds_IntMap.prototype = {
  2410. keys: function () {
  2411. var a = [];
  2412. for (var key in this.h) if (this.h.hasOwnProperty(key)) a.push(+key);
  2413. return new haxe_iterators_ArrayIterator(a);
  2414. },
  2415. };
  2416. var haxe_io_Bytes = function (data) {
  2417. this.length = data.byteLength;
  2418. this.b = new Uint8Array(data);
  2419. this.b.bufferValue = data;
  2420. data.hxBytes = this;
  2421. data.bytes = this.b;
  2422. };
  2423. haxe_io_Bytes.__name__ = true;
  2424. var haxe_iterators_ArrayIterator = function (array) {
  2425. this.current = 0;
  2426. this.array = array;
  2427. };
  2428. haxe_iterators_ArrayIterator.__name__ = true;
  2429. haxe_iterators_ArrayIterator.prototype = {
  2430. hasNext: function () {
  2431. return this.current < this.array.length;
  2432. },
  2433. next: function () {
  2434. return this.array[this.current++];
  2435. },
  2436. };
  2437. var js_Boot = function () {};
  2438. js_Boot.__name__ = true;
  2439. js_Boot.__string_rec = function (o, s) {
  2440. if (o == null) {
  2441. return "null";
  2442. }
  2443. if (s.length >= 5) {
  2444. return "<...>";
  2445. }
  2446. var t = typeof o;
  2447. if (t == "function" && (o.__name__ || o.__ename__)) {
  2448. t = "object";
  2449. }
  2450. switch (t) {
  2451. case "function":
  2452. return "<function>";
  2453. case "object":
  2454. if (o instanceof Array) {
  2455. var str = "[";
  2456. s += "\t";
  2457. var _g = 0;
  2458. var _g1 = o.length;
  2459. while (_g < _g1) {
  2460. var i = _g++;
  2461. str += (i > 0 ? "," : "") + js_Boot.__string_rec(o[i], s);
  2462. }
  2463. str += "]";
  2464. return str;
  2465. }
  2466. var tostr;
  2467. try {
  2468. tostr = o.toString;
  2469. } catch (_g) {
  2470. return "???";
  2471. }
  2472. if (tostr != null && tostr != Object.toString && typeof tostr == "function") {
  2473. var s2 = o.toString();
  2474. if (s2 != "[object Object]") {
  2475. return s2;
  2476. }
  2477. }
  2478. var str = "{\n";
  2479. s += "\t";
  2480. var hasp = o.hasOwnProperty != null;
  2481. var k = null;
  2482. for (k in o) {
  2483. if (hasp && !o.hasOwnProperty(k)) {
  2484. continue;
  2485. }
  2486. if (k == "prototype" || k == "__class__" || k == "__super__" || k == "__interfaces__" || k == "__properties__") {
  2487. continue;
  2488. }
  2489. if (str.length != 2) {
  2490. str += ", \n";
  2491. }
  2492. str += s + k + " : " + js_Boot.__string_rec(o[k], s);
  2493. }
  2494. s = s.substring(1);
  2495. str += "\n" + s + "}";
  2496. return str;
  2497. case "string":
  2498. return o;
  2499. default:
  2500. return String(o);
  2501. }
  2502. };
  2503. var $_;
  2504. function $bind(o, m) {
  2505. if (m == null) return null;
  2506. if (m.__id__ == null) m.__id__ = $global.$haxeUID++;
  2507. var f;
  2508. if (o.hx__closures__ == null) o.hx__closures__ = {};
  2509. else f = o.hx__closures__[m.__id__];
  2510. if (f == null) {
  2511. f = m.bind(o);
  2512. o.hx__closures__[m.__id__] = f;
  2513. }
  2514. return f;
  2515. }
  2516. $global.$haxeUID |= 0;
  2517. if (typeof performance != "undefined" ? typeof performance.now == "function" : false) {
  2518. HxOverrides.now = performance.now.bind(performance);
  2519. }
  2520. String.__name__ = true;
  2521. Array.__name__ = true;
  2522. js_Boot.__toStr = {}.toString;
  2523. Main.imagesToGeometrize = [];
  2524. geometrize_exporter_SvgExporter.SVG_STYLE_HOOK = "::svg_style_hook::";
  2525. geometrize_runner_Default.options = {
  2526. shapeTypes: [5, 6],
  2527. candidateShapesPerStep: 50,
  2528. shapeMutationsPerStep: 100,
  2529. alpha: 255,
  2530. };
  2531. geometrize_shape_ShapeTypes.RECTANGLE = 0;
  2532. geometrize_shape_ShapeTypes.ROTATED_RECTANGLE = 1;
  2533. geometrize_shape_ShapeTypes.TRIANGLE = 2;
  2534. geometrize_shape_ShapeTypes.ELLIPSE = 3;
  2535. geometrize_shape_ShapeTypes.ROTATED_ELLIPSE = 4;
  2536. geometrize_shape_ShapeTypes.CIRCLE = 5;
  2537. geometrize_shape_ShapeTypes.LINE = 6;
  2538. geometrize_shape_ShapeTypes.QUADRATIC_BEZIER = 7;
  2539. Main.main();
  2540. })(
  2541. typeof exports != "undefined" ? exports : typeof window != "undefined" ? window : typeof self != "undefined" ? self : this,
  2542. typeof window != "undefined" ? window : typeof global != "undefined" ? global : typeof self != "undefined" ? self : this,
  2543. `(function ($hx_exports, $global) {
  2544. "use strict";
  2545. $hx_exports["geometrize"] = $hx_exports["geometrize"] || {};
  2546. $hx_exports["geometrize"]["bitmap"] = $hx_exports["geometrize"]["bitmap"] || {};
  2547. $hx_exports["geometrize"]["exporter"] = $hx_exports["geometrize"]["exporter"] || {};
  2548. $hx_exports["geometrize"]["runner"] = $hx_exports["geometrize"]["runner"] || {};
  2549. $hx_exports["geometrize"]["shape"] = $hx_exports["geometrize"]["shape"] || {};
  2550. function $extend(from, fields) {
  2551. var proto = Object.create(from);
  2552. for (var name in fields) proto[name] = fields[name];
  2553. if (fields.toString !== Object.prototype.toString) proto.toString = fields.toString;
  2554. return proto;
  2555. }
  2556. var GeometrizeWorker = function () {};
  2557. GeometrizeWorker.prototype = {
  2558. messageHandler: function (event) {
  2559. if (event == null || event.data == null) {
  2560. return;
  2561. }
  2562. var message = event.data;
  2563. switch (message.id) {
  2564. case "should_set_target_image":
  2565. var target = message.data;
  2566. this.runner = new geometrize_runner_ImageRunner(target, geometrize_Util.getAverageImageColor(target));
  2567. this.postMessage({ id: "did_set_target_image" });
  2568. break;
  2569. case "should_step":
  2570. var options = message.data;
  2571. var results = this.runner.step(options);
  2572. var svgData = geometrize_exporter_SvgExporter.exportShapes(results);
  2573. var jsonData = geometrize_exporter_ShapeJsonExporter.exportShapes(results);
  2574. this.postMessage({ id: "did_step", svgData: svgData, jsonData: jsonData });
  2575. break;
  2576. }
  2577. },
  2578. postMessage: function (message) {},
  2579. };
  2580. var Std = function () {};
  2581. Std.random = function (x) {
  2582. if (x <= 0) {
  2583. return 0;
  2584. } else {
  2585. return Math.floor(Math.random() * x);
  2586. }
  2587. };
  2588. var StringTools = function () {};
  2589. StringTools.replace = function (s, sub, by) {
  2590. return s.split(sub).join(by);
  2591. };
  2592. var UInt = {};
  2593. UInt.toFloat = function (this1) {
  2594. var int = this1;
  2595. if (int < 0) {
  2596. return 4294967296.0 + int;
  2597. } else {
  2598. return int + 0.0;
  2599. }
  2600. };
  2601. var geometrize_ArraySet = {};
  2602. geometrize_ArraySet.create = function (array) {
  2603. if (array == null) {
  2604. var this1 = [];
  2605. return this1;
  2606. }
  2607. return geometrize_ArraySet.toSet(array);
  2608. };
  2609. geometrize_ArraySet.add = function (this1, element) {
  2610. if (element == null) {
  2611. throw haxe_Exception.thrown("FAIL: element != null");
  2612. }
  2613. if (geometrize_ArraySet.contains(this1, element)) {
  2614. return false;
  2615. }
  2616. this1.push(element);
  2617. return true;
  2618. };
  2619. geometrize_ArraySet.contains = function (this1, element) {
  2620. var _g = 0;
  2621. while (_g < this1.length) {
  2622. var i = this1[_g];
  2623. ++_g;
  2624. if (i == element) {
  2625. return true;
  2626. }
  2627. }
  2628. return false;
  2629. };
  2630. geometrize_ArraySet.toArray = function (this1) {
  2631. return this1.slice();
  2632. };
  2633. geometrize_ArraySet.toSet = function (array) {
  2634. var this1 = [];
  2635. var set = this1;
  2636. var _g = 0;
  2637. while (_g < array.length) {
  2638. var v = array[_g];
  2639. ++_g;
  2640. geometrize_ArraySet.add(set, v);
  2641. }
  2642. return set;
  2643. };
  2644. var geometrize_Core = function () {};
  2645. geometrize_Core.computeColor = function (target, current, lines, alpha) {
  2646. if (target == null) {
  2647. throw haxe_Exception.thrown("FAIL: target != null");
  2648. }
  2649. if (current == null) {
  2650. throw haxe_Exception.thrown("FAIL: current != null");
  2651. }
  2652. if (lines == null) {
  2653. throw haxe_Exception.thrown("FAIL: lines != null");
  2654. }
  2655. if (alpha < 0) {
  2656. throw haxe_Exception.thrown("FAIL: alpha >= 0");
  2657. }
  2658. var totalRed = 0;
  2659. var totalGreen = 0;
  2660. var totalBlue = 0;
  2661. var count = 0;
  2662. var f = 65535 / alpha;
  2663. var a = f | 0;
  2664. var _g = 0;
  2665. while (_g < lines.length) {
  2666. var line = lines[_g];
  2667. ++_g;
  2668. var y = line.y;
  2669. var _g1 = line.x1;
  2670. var _g2 = line.x2 + 1;
  2671. while (_g1 < _g2) {
  2672. var x = _g1++;
  2673. var t = target.data[target.width * y + x];
  2674. var c = current.data[current.width * y + x];
  2675. totalRed += (((t >> 24) & 255) - ((c >> 24) & 255)) * a + ((c >> 24) & 255) * 257;
  2676. totalGreen += (((t >> 16) & 255) - ((c >> 16) & 255)) * a + ((c >> 16) & 255) * 257;
  2677. totalBlue += (((t >> 8) & 255) - ((c >> 8) & 255)) * a + ((c >> 8) & 255) * 257;
  2678. ++count;
  2679. }
  2680. }
  2681. if (count == 0) {
  2682. return 0;
  2683. }
  2684. var value = ((totalRed / count) | 0) >> 8;
  2685. var r = value < 0 ? 0 : value > 255 ? 255 : value;
  2686. var value = ((totalGreen / count) | 0) >> 8;
  2687. var g = value < 0 ? 0 : value > 255 ? 255 : value;
  2688. var value = ((totalBlue / count) | 0) >> 8;
  2689. var b = value < 0 ? 0 : value > 255 ? 255 : value;
  2690. return ((r < 0 ? 0 : r > 255 ? 255 : r) << 24) + ((g < 0 ? 0 : g > 255 ? 255 : g) << 16) + ((b < 0 ? 0 : b > 255 ? 255 : b) << 8) + (alpha < 0 ? 0 : alpha > 255 ? 255 : alpha);
  2691. };
  2692. geometrize_Core.differenceFull = function (first, second) {
  2693. if (first == null) {
  2694. throw haxe_Exception.thrown("FAIL: first != null");
  2695. }
  2696. if (second == null) {
  2697. throw haxe_Exception.thrown("FAIL: second != null");
  2698. }
  2699. if (first.width == 0) {
  2700. throw haxe_Exception.thrown("FAIL: first.width != 0");
  2701. }
  2702. if (first.height == 0) {
  2703. throw haxe_Exception.thrown("FAIL: first.height != 0");
  2704. }
  2705. if (second.width == 0) {
  2706. throw haxe_Exception.thrown("FAIL: second.width != 0");
  2707. }
  2708. if (second.height == 0) {
  2709. throw haxe_Exception.thrown("FAIL: second.height != 0");
  2710. }
  2711. var actual = first.width;
  2712. var expected = second.width;
  2713. if (actual != expected) {
  2714. throw haxe_Exception.thrown("FAIL: values are not equal (expected: " + expected + ", actual: " + actual + ")");
  2715. }
  2716. var actual = first.height;
  2717. var expected = second.height;
  2718. if (actual != expected) {
  2719. throw haxe_Exception.thrown("FAIL: values are not equal (expected: " + expected + ", actual: " + actual + ")");
  2720. }
  2721. var total = 0;
  2722. var width = first.width;
  2723. var height = first.height;
  2724. var _g = 0;
  2725. var _g1 = height;
  2726. while (_g < _g1) {
  2727. var y = _g++;
  2728. var _g2 = 0;
  2729. var _g3 = width;
  2730. while (_g2 < _g3) {
  2731. var x = _g2++;
  2732. var f = first.data[first.width * y + x];
  2733. var s = second.data[second.width * y + x];
  2734. var dr = ((f >> 24) & 255) - ((s >> 24) & 255);
  2735. var dg = ((f >> 16) & 255) - ((s >> 16) & 255);
  2736. var db = ((f >> 8) & 255) - ((s >> 8) & 255);
  2737. var da = (f & 255) - (s & 255);
  2738. total += dr * dr + dg * dg + db * db + da * da;
  2739. }
  2740. }
  2741. var result = Math.sqrt(total / (width * height * 4.0)) / 255;
  2742. if (!isFinite(result)) {
  2743. throw haxe_Exception.thrown("FAIL: Math.isFinite(result)");
  2744. }
  2745. return result;
  2746. };
  2747. geometrize_Core.differencePartial = function (target, before, after, score, lines) {
  2748. if (target == null) {
  2749. throw haxe_Exception.thrown("FAIL: target != null");
  2750. }
  2751. if (before == null) {
  2752. throw haxe_Exception.thrown("FAIL: before != null");
  2753. }
  2754. if (after == null) {
  2755. throw haxe_Exception.thrown("FAIL: after != null");
  2756. }
  2757. if (lines == null) {
  2758. throw haxe_Exception.thrown("FAIL: lines != null");
  2759. }
  2760. if (lines.length == 0) {
  2761. throw haxe_Exception.thrown("FAIL: lines.length != 0");
  2762. }
  2763. var width = target.width;
  2764. var height = target.height;
  2765. var rgbaCount = width * height * 4;
  2766. var total = Math.pow(score * 255, 2) * rgbaCount;
  2767. var _g = 0;
  2768. while (_g < lines.length) {
  2769. var line = lines[_g];
  2770. ++_g;
  2771. var y = line.y;
  2772. var _g1 = line.x1;
  2773. var _g2 = line.x2 + 1;
  2774. while (_g1 < _g2) {
  2775. var x = _g1++;
  2776. var t = target.data[target.width * y + x];
  2777. var b = before.data[before.width * y + x];
  2778. var a = after.data[after.width * y + x];
  2779. var dtbr = ((t >> 24) & 255) - ((b >> 24) & 255);
  2780. var dtbg = ((t >> 16) & 255) - ((b >> 16) & 255);
  2781. var dtbb = ((t >> 8) & 255) - ((b >> 8) & 255);
  2782. var dtba = (t & 255) - (b & 255);
  2783. var dtar = ((t >> 24) & 255) - ((a >> 24) & 255);
  2784. var dtag = ((t >> 16) & 255) - ((a >> 16) & 255);
  2785. var dtab = ((t >> 8) & 255) - ((a >> 8) & 255);
  2786. var dtaa = (t & 255) - (a & 255);
  2787. total -= dtbr * dtbr + dtbg * dtbg + dtbb * dtbb + dtba * dtba;
  2788. total += dtar * dtar + dtag * dtag + dtab * dtab + dtaa * dtaa;
  2789. }
  2790. }
  2791. var result = Math.sqrt(total / rgbaCount) / 255;
  2792. if (!isFinite(result)) {
  2793. throw haxe_Exception.thrown("FAIL: Math.isFinite(result)");
  2794. }
  2795. return result;
  2796. };
  2797. geometrize_Core.bestRandomState = function (shapes, alpha, n, target, current, buffer, lastScore) {
  2798. var bestEnergy = 0;
  2799. var bestState = null;
  2800. var _g = 0;
  2801. var _g1 = n;
  2802. while (_g < _g1) {
  2803. var i = _g++;
  2804. var state = new geometrize_State(geometrize_shape_ShapeFactory.randomShapeOf(shapes, current.width, current.height), alpha, target, current, buffer);
  2805. var energy = state.energy(lastScore);
  2806. if (i == 0 || energy < bestEnergy) {
  2807. bestEnergy = energy;
  2808. bestState = state;
  2809. }
  2810. }
  2811. return bestState;
  2812. };
  2813. geometrize_Core.bestHillClimbState = function (shapes, alpha, n, age, target, current, buffer, lastScore) {
  2814. var state = geometrize_Core.bestRandomState(shapes, alpha, n, target, current, buffer, lastScore);
  2815. state = geometrize_Core.hillClimb(state, age, lastScore);
  2816. return state;
  2817. };
  2818. geometrize_Core.hillClimb = function (state, maxAge, lastScore) {
  2819. if (state == null) {
  2820. throw haxe_Exception.thrown("FAIL: state != null");
  2821. }
  2822. if (maxAge < 0) {
  2823. throw haxe_Exception.thrown("FAIL: maxAge >= 0");
  2824. }
  2825. var state1 = state.clone();
  2826. var bestState = state1.clone();
  2827. var bestEnergy = state1.energy(lastScore);
  2828. var age = 0;
  2829. while (age < maxAge) {
  2830. var undo = state1.mutate();
  2831. var energy = state1.energy(lastScore);
  2832. if (energy >= bestEnergy) {
  2833. state1 = undo;
  2834. } else {
  2835. bestEnergy = energy;
  2836. bestState = state1.clone();
  2837. age = -1;
  2838. }
  2839. ++age;
  2840. }
  2841. return bestState;
  2842. };
  2843. geometrize_Core.energy = function (shape, alpha, target, current, buffer, score) {
  2844. if (shape == null) {
  2845. throw haxe_Exception.thrown("FAIL: shape != null");
  2846. }
  2847. if (target == null) {
  2848. throw haxe_Exception.thrown("FAIL: target != null");
  2849. }
  2850. if (current == null) {
  2851. throw haxe_Exception.thrown("FAIL: current != null");
  2852. }
  2853. if (buffer == null) {
  2854. throw haxe_Exception.thrown("FAIL: buffer != null");
  2855. }
  2856. var lines = shape.rasterize();
  2857. if (lines == null) {
  2858. throw haxe_Exception.thrown("FAIL: lines != null");
  2859. }
  2860. if (lines.length == 0) {
  2861. throw haxe_Exception.thrown("FAIL: lines.length != 0");
  2862. }
  2863. var color = geometrize_Core.computeColor(target, current, lines, alpha);
  2864. geometrize_rasterizer_Rasterizer.copyLines(buffer, current, lines);
  2865. geometrize_rasterizer_Rasterizer.drawLines(buffer, color, lines);
  2866. return geometrize_Core.differencePartial(target, current, buffer, score, lines);
  2867. };
  2868. var geometrize_Model = function (target, backgroundColor) {
  2869. if (target == null) {
  2870. throw haxe_Exception.thrown("FAIL: target != null");
  2871. }
  2872. this.width = target.width;
  2873. this.height = target.height;
  2874. this.target = target;
  2875. var w = target.width;
  2876. var h = target.height;
  2877. var bitmap = new geometrize_bitmap_Bitmap();
  2878. bitmap.width = w;
  2879. bitmap.height = h;
  2880. var this1 = new Array(w * h);
  2881. bitmap.data = this1;
  2882. var i = 0;
  2883. while (i < bitmap.data.length) {
  2884. bitmap.data[i] = backgroundColor;
  2885. ++i;
  2886. }
  2887. this.current = bitmap;
  2888. var w = target.width;
  2889. var h = target.height;
  2890. var bitmap = new geometrize_bitmap_Bitmap();
  2891. bitmap.width = w;
  2892. bitmap.height = h;
  2893. var this1 = new Array(w * h);
  2894. bitmap.data = this1;
  2895. var i = 0;
  2896. while (i < bitmap.data.length) {
  2897. bitmap.data[i] = backgroundColor;
  2898. ++i;
  2899. }
  2900. this.buffer = bitmap;
  2901. this.score = geometrize_Core.differenceFull(target, this.current);
  2902. };
  2903. geometrize_Model.prototype = {
  2904. step: function (shapeTypes, alpha, n, age) {
  2905. var state = geometrize_Core.bestHillClimbState(shapeTypes, alpha, n, age, this.target, this.current, this.buffer, this.score);
  2906. var results = [this.addShape(state.shape, state.alpha)];
  2907. return results;
  2908. },
  2909. addShape: function (shape, alpha) {
  2910. if (shape == null) {
  2911. throw haxe_Exception.thrown("FAIL: shape != null");
  2912. }
  2913. var _this = this.current;
  2914. var bitmap = new geometrize_bitmap_Bitmap();
  2915. bitmap.width = _this.width;
  2916. bitmap.height = _this.height;
  2917. var this1 = new Array(_this.data.length);
  2918. bitmap.data = this1;
  2919. var _g = 0;
  2920. var _g1 = _this.data.length;
  2921. while (_g < _g1) {
  2922. var i = _g++;
  2923. bitmap.data[i] = _this.data[i];
  2924. }
  2925. var before = bitmap;
  2926. var lines = shape.rasterize();
  2927. var color = geometrize_Core.computeColor(this.target, this.current, lines, alpha);
  2928. geometrize_rasterizer_Rasterizer.drawLines(this.current, color, lines);
  2929. this.score = geometrize_Core.differencePartial(this.target, before, this.current, this.score, lines);
  2930. var result = { score: this.score, color: color, shape: shape };
  2931. return result;
  2932. },
  2933. };
  2934. var geometrize_State = function (shape, alpha, target, current, buffer) {
  2935. if (shape == null) {
  2936. throw haxe_Exception.thrown("FAIL: shape != null");
  2937. }
  2938. this.shape = shape;
  2939. this.alpha = alpha;
  2940. this.score = -1;
  2941. this.target = target;
  2942. this.current = current;
  2943. this.buffer = buffer;
  2944. };
  2945. geometrize_State.prototype = {
  2946. energy: function (lastScore) {
  2947. if (this.score < 0) {
  2948. this.score = geometrize_Core.energy(this.shape, this.alpha, this.target, this.current, this.buffer, lastScore);
  2949. }
  2950. return this.score;
  2951. },
  2952. mutate: function () {
  2953. var oldState = this.clone();
  2954. this.shape.mutate();
  2955. return oldState;
  2956. },
  2957. clone: function () {
  2958. return new geometrize_State(this.shape.clone(), this.alpha, this.target, this.current, this.buffer);
  2959. },
  2960. };
  2961. var geometrize_Util = function () {};
  2962. geometrize_Util.getAverageImageColor = function (image, alpha) {
  2963. if (alpha == null) {
  2964. alpha = 255;
  2965. }
  2966. if (image == null) {
  2967. throw haxe_Exception.thrown("FAIL: image != null");
  2968. }
  2969. var totalRed = 0;
  2970. var totalGreen = 0;
  2971. var totalBlue = 0;
  2972. var _g = 0;
  2973. var _g1 = image.width;
  2974. while (_g < _g1) {
  2975. var x = _g++;
  2976. var _g2 = 0;
  2977. var _g3 = image.height;
  2978. while (_g2 < _g3) {
  2979. var y = _g2++;
  2980. var pixel = image.data[image.width * y + x];
  2981. totalRed += (pixel >> 24) & 255;
  2982. totalGreen += (pixel >> 16) & 255;
  2983. totalBlue += (pixel >> 8) & 255;
  2984. }
  2985. }
  2986. var size = image.width * image.height;
  2987. var red = (totalRed / size) | 0;
  2988. var green = (totalGreen / size) | 0;
  2989. var blue = (totalBlue / size) | 0;
  2990. return (
  2991. ((red < 0 ? 0 : red > 255 ? 255 : red) << 24) +
  2992. ((green < 0 ? 0 : green > 255 ? 255 : green) << 16) +
  2993. ((blue < 0 ? 0 : blue > 255 ? 255 : blue) << 8) +
  2994. (alpha < 0 ? 0 : alpha > 255 ? 255 : alpha)
  2995. );
  2996. };
  2997. var geometrize_bitmap_Bitmap = ($hx_exports["geometrize"]["bitmap"]["Bitmap"] = function () {});
  2998. geometrize_bitmap_Bitmap.create = function (w, h, color) {
  2999. var bitmap = new geometrize_bitmap_Bitmap();
  3000. bitmap.width = w;
  3001. bitmap.height = h;
  3002. var this1 = new Array(w * h);
  3003. bitmap.data = this1;
  3004. var i = 0;
  3005. while (i < bitmap.data.length) {
  3006. bitmap.data[i] = color;
  3007. ++i;
  3008. }
  3009. return bitmap;
  3010. };
  3011. geometrize_bitmap_Bitmap.createFromBytes = function (w, h, bytes) {
  3012. var bitmap = new geometrize_bitmap_Bitmap();
  3013. if (bytes == null) {
  3014. throw haxe_Exception.thrown("FAIL: bytes != null");
  3015. }
  3016. var actual = bytes.length;
  3017. var expected = w * h * 4;
  3018. if (actual != expected) {
  3019. throw haxe_Exception.thrown("FAIL: values are not equal (expected: " + expected + ", actual: " + actual + ")");
  3020. }
  3021. bitmap.width = w;
  3022. bitmap.height = h;
  3023. var this1 = new Array((bytes.length / 4) | 0);
  3024. bitmap.data = this1;
  3025. var i = 0;
  3026. var x = 0;
  3027. while (i < bytes.length) {
  3028. var red = bytes.b[i];
  3029. var green = bytes.b[i + 1];
  3030. var blue = bytes.b[i + 2];
  3031. var alpha = bytes.b[i + 3];
  3032. bitmap.data[x] =
  3033. ((red < 0 ? 0 : red > 255 ? 255 : red) << 24) +
  3034. ((green < 0 ? 0 : green > 255 ? 255 : green) << 16) +
  3035. ((blue < 0 ? 0 : blue > 255 ? 255 : blue) << 8) +
  3036. (alpha < 0 ? 0 : alpha > 255 ? 255 : alpha);
  3037. i += 4;
  3038. ++x;
  3039. }
  3040. return bitmap;
  3041. };
  3042. geometrize_bitmap_Bitmap.createFromByteArray = function (w, h, bytes) {
  3043. var data = new haxe_io_Bytes(new ArrayBuffer(bytes.length));
  3044. var i = 0;
  3045. while (i < bytes.length) {
  3046. data.b[i] = bytes[i];
  3047. ++i;
  3048. }
  3049. var bitmap = new geometrize_bitmap_Bitmap();
  3050. if (data == null) {
  3051. throw haxe_Exception.thrown("FAIL: bytes != null");
  3052. }
  3053. var actual = data.length;
  3054. var expected = w * h * 4;
  3055. if (actual != expected) {
  3056. throw haxe_Exception.thrown("FAIL: values are not equal (expected: " + expected + ", actual: " + actual + ")");
  3057. }
  3058. bitmap.width = w;
  3059. bitmap.height = h;
  3060. var this1 = new Array((data.length / 4) | 0);
  3061. bitmap.data = this1;
  3062. var i = 0;
  3063. var x = 0;
  3064. while (i < data.length) {
  3065. var red = data.b[i];
  3066. var green = data.b[i + 1];
  3067. var blue = data.b[i + 2];
  3068. var alpha = data.b[i + 3];
  3069. bitmap.data[x] =
  3070. ((red < 0 ? 0 : red > 255 ? 255 : red) << 24) +
  3071. ((green < 0 ? 0 : green > 255 ? 255 : green) << 16) +
  3072. ((blue < 0 ? 0 : blue > 255 ? 255 : blue) << 8) +
  3073. (alpha < 0 ? 0 : alpha > 255 ? 255 : alpha);
  3074. i += 4;
  3075. ++x;
  3076. }
  3077. return bitmap;
  3078. };
  3079. geometrize_bitmap_Bitmap.prototype = {
  3080. getPixel: function (x, y) {
  3081. return this.data[this.width * y + x];
  3082. },
  3083. setPixel: function (x, y, color) {
  3084. this.data[this.width * y + x] = color;
  3085. },
  3086. clone: function () {
  3087. var bitmap = new geometrize_bitmap_Bitmap();
  3088. bitmap.width = this.width;
  3089. bitmap.height = this.height;
  3090. var this1 = new Array(this.data.length);
  3091. bitmap.data = this1;
  3092. var _g = 0;
  3093. var _g1 = this.data.length;
  3094. while (_g < _g1) {
  3095. var i = _g++;
  3096. bitmap.data[i] = this.data[i];
  3097. }
  3098. return bitmap;
  3099. },
  3100. fill: function (color) {
  3101. var idx = 0;
  3102. while (idx < this.data.length) {
  3103. this.data[idx] = (color >> 24) & 255;
  3104. this.data[idx + 1] = (color >> 16) & 255;
  3105. this.data[idx + 2] = (color >> 8) & 255;
  3106. this.data[idx + 3] = color & 255;
  3107. idx += 4;
  3108. }
  3109. },
  3110. getBytes: function () {
  3111. var bytes = new haxe_io_Bytes(new ArrayBuffer(this.data.length * 4));
  3112. var i = 0;
  3113. while (i < this.data.length) {
  3114. var idx = i * 4;
  3115. bytes.b[idx] = (this.data[i] >> 24) & 255;
  3116. bytes.b[idx + 1] = (this.data[i] >> 16) & 255;
  3117. bytes.b[idx + 2] = (this.data[i] >> 8) & 255;
  3118. bytes.b[idx + 3] = this.data[i] & 255;
  3119. ++i;
  3120. }
  3121. return bytes;
  3122. },
  3123. };
  3124. var geometrize_exporter_ShapeJsonExporter = ($hx_exports["geometrize"]["exporter"]["ShapeJsonExporter"] = function () {});
  3125. geometrize_exporter_ShapeJsonExporter.export = function (shapes) {
  3126. return "[\\n" + geometrize_exporter_ShapeJsonExporter.exportShapes(shapes) + "\\n]";
  3127. };
  3128. geometrize_exporter_ShapeJsonExporter.exportShapes = function (shapes) {
  3129. var results = "";
  3130. var _g = 0;
  3131. var _g1 = shapes.length;
  3132. while (_g < _g1) {
  3133. var i = _g++;
  3134. results += geometrize_exporter_ShapeJsonExporter.exportShape(shapes[i]);
  3135. if (i != shapes.length - 1) {
  3136. results += ",\\n";
  3137. }
  3138. }
  3139. return results;
  3140. };
  3141. geometrize_exporter_ShapeJsonExporter.exportShape = function (shape) {
  3142. var result = " {\\n";
  3143. var type = shape.shape.getType();
  3144. var data = shape.shape.getRawShapeData();
  3145. var color = shape.color;
  3146. var score = shape.score;
  3147. result += ' "type":' + type + ",\\n";
  3148. result += ' "data":' + "[";
  3149. var _g = 0;
  3150. var _g1 = data.length;
  3151. while (_g < _g1) {
  3152. var item = _g++;
  3153. result += data[item];
  3154. if (item <= data.length - 2) {
  3155. result += ",";
  3156. }
  3157. }
  3158. result += "],\\n";
  3159. result += ' "color":' + "[";
  3160. result += ((color >> 24) & 255) + ",";
  3161. result += ((color >> 16) & 255) + ",";
  3162. result += ((color >> 8) & 255) + ",";
  3163. result += color & 255;
  3164. result += "],\\n";
  3165. result += ' "score":' + score + "\\n";
  3166. result += " }";
  3167. return result;
  3168. };
  3169. var geometrize_exporter_SvgExporter = ($hx_exports["geometrize"]["exporter"]["SvgExporter"] = function () {});
  3170. geometrize_exporter_SvgExporter.export = function (shapes, width, height) {
  3171. var results = geometrize_exporter_SvgExporter.getSvgPrelude();
  3172. results += geometrize_exporter_SvgExporter.getSvgNodeOpen(width, height);
  3173. results += geometrize_exporter_SvgExporter.exportShapes(shapes);
  3174. results += geometrize_exporter_SvgExporter.getSvgNodeClose();
  3175. return results;
  3176. };
  3177. geometrize_exporter_SvgExporter.exportShapes = function (shapes) {
  3178. var results = "";
  3179. var _g = 0;
  3180. var _g1 = shapes.length;
  3181. while (_g < _g1) {
  3182. var i = _g++;
  3183. results += geometrize_exporter_SvgExporter.exportShape(shapes[i]);
  3184. if (i != shapes.length - 1) {
  3185. results += "\\n";
  3186. }
  3187. }
  3188. return results;
  3189. };
  3190. geometrize_exporter_SvgExporter.exportShape = function (shape) {
  3191. return StringTools.replace(shape.shape.getSvgShapeData(), geometrize_exporter_SvgExporter.SVG_STYLE_HOOK, geometrize_exporter_SvgExporter.stylesForShape(shape));
  3192. };
  3193. geometrize_exporter_SvgExporter.getSvgPrelude = function () {
  3194. return '<?xml version="1.0" standalone="no"?>\\n';
  3195. };
  3196. geometrize_exporter_SvgExporter.getSvgNodeOpen = function (width, height) {
  3197. return '<svg xmlns="http://www.w3.org/2000/svg" version="1.2" baseProfile="tiny" width="' + width + '" height="' + height + '">\\n';
  3198. };
  3199. geometrize_exporter_SvgExporter.getSvgNodeClose = function () {
  3200. return "</svg>";
  3201. };
  3202. geometrize_exporter_SvgExporter.stylesForShape = function (shape) {
  3203. switch (shape.shape.getType()) {
  3204. case 6:
  3205. case 7:
  3206. return geometrize_exporter_SvgExporter.strokeForColor(shape.color) + ' stroke-width="1" fill="none" ' + geometrize_exporter_SvgExporter.strokeOpacityForAlpha(shape.color & 255);
  3207. default:
  3208. return geometrize_exporter_SvgExporter.fillForColor(shape.color) + " " + geometrize_exporter_SvgExporter.fillOpacityForAlpha(shape.color & 255);
  3209. }
  3210. };
  3211. geometrize_exporter_SvgExporter.rgbForColor = function (color) {
  3212. return "rgb(" + ((color >> 24) & 255) + "," + ((color >> 16) & 255) + "," + ((color >> 8) & 255) + ")";
  3213. };
  3214. geometrize_exporter_SvgExporter.strokeForColor = function (color) {
  3215. return 'stroke="' + geometrize_exporter_SvgExporter.rgbForColor(color) + '"';
  3216. };
  3217. geometrize_exporter_SvgExporter.fillForColor = function (color) {
  3218. return 'fill="' + geometrize_exporter_SvgExporter.rgbForColor(color) + '"';
  3219. };
  3220. geometrize_exporter_SvgExporter.fillOpacityForAlpha = function (alpha) {
  3221. return 'fill-opacity="' + alpha / 255.0 + '"';
  3222. };
  3223. geometrize_exporter_SvgExporter.strokeOpacityForAlpha = function (alpha) {
  3224. return 'stroke-opacity="' + alpha / 255.0 + '"';
  3225. };
  3226. var geometrize_rasterizer_Rasterizer = function () {};
  3227. geometrize_rasterizer_Rasterizer.drawLines = function (image, c, lines) {
  3228. if (image == null) {
  3229. throw haxe_Exception.thrown("FAIL: image != null");
  3230. }
  3231. if (lines == null) {
  3232. throw haxe_Exception.thrown("FAIL: lines != null");
  3233. }
  3234. var sr = (c >> 24) & 255;
  3235. sr |= sr << 8;
  3236. sr *= c & 255;
  3237. sr = (sr / 255) | 0;
  3238. var sg = (c >> 16) & 255;
  3239. sg |= sg << 8;
  3240. sg *= c & 255;
  3241. sg = (sg / 255) | 0;
  3242. var sb = (c >> 8) & 255;
  3243. sb |= sb << 8;
  3244. sb *= c & 255;
  3245. sb = (sb / 255) | 0;
  3246. var sa = c & 255;
  3247. sa |= sa << 8;
  3248. var _g = 0;
  3249. while (_g < lines.length) {
  3250. var line = lines[_g];
  3251. ++_g;
  3252. var y = line.y;
  3253. var ma = 65535;
  3254. var m = 65535;
  3255. var as = (m - sa * (ma / m)) * 257;
  3256. var a = as | 0;
  3257. var _g1 = line.x1;
  3258. var _g2 = line.x2 + 1;
  3259. while (_g1 < _g2) {
  3260. var x = _g1++;
  3261. var d = image.data[image.width * y + x];
  3262. var dr = (d >> 24) & 255;
  3263. var dg = (d >> 16) & 255;
  3264. var db = (d >> 8) & 255;
  3265. var da = d & 255;
  3266. var r = ((UInt.toFloat(dr * a + sr * ma) / UInt.toFloat(m)) | 0) >> 8;
  3267. var g = ((UInt.toFloat(dg * a + sg * ma) / UInt.toFloat(m)) | 0) >> 8;
  3268. var b = ((UInt.toFloat(db * a + sb * ma) / UInt.toFloat(m)) | 0) >> 8;
  3269. var a1 = ((UInt.toFloat(da * a + sa * ma) / UInt.toFloat(m)) | 0) >> 8;
  3270. image.data[image.width * y + x] =
  3271. ((r < 0 ? 0 : r > 255 ? 255 : r) << 24) + ((g < 0 ? 0 : g > 255 ? 255 : g) << 16) + ((b < 0 ? 0 : b > 255 ? 255 : b) << 8) + (a1 < 0 ? 0 : a1 > 255 ? 255 : a1);
  3272. }
  3273. }
  3274. };
  3275. geometrize_rasterizer_Rasterizer.copyLines = function (destination, source, lines) {
  3276. if (destination == null) {
  3277. throw haxe_Exception.thrown("FAIL: destination != null");
  3278. }
  3279. if (source == null) {
  3280. throw haxe_Exception.thrown("FAIL: source != null");
  3281. }
  3282. if (lines == null) {
  3283. throw haxe_Exception.thrown("FAIL: lines != null");
  3284. }
  3285. var _g = 0;
  3286. while (_g < lines.length) {
  3287. var line = lines[_g];
  3288. ++_g;
  3289. var y = line.y;
  3290. var _g1 = line.x1;
  3291. var _g2 = line.x2 + 1;
  3292. while (_g1 < _g2) {
  3293. var x = _g1++;
  3294. destination.data[destination.width * y + x] = source.data[source.width * y + x];
  3295. }
  3296. }
  3297. };
  3298. geometrize_rasterizer_Rasterizer.bresenham = function (x1, y1, x2, y2) {
  3299. var dx = x2 - x1;
  3300. var ix = (dx > 0 ? 1 : 0) - (dx < 0 ? 1 : 0);
  3301. dx = (dx < 0 ? -dx : dx) << 1;
  3302. var dy = y2 - y1;
  3303. var iy = (dy > 0 ? 1 : 0) - (dy < 0 ? 1 : 0);
  3304. dy = (dy < 0 ? -dy : dy) << 1;
  3305. var points = [];
  3306. points.push({ x: x1, y: y1 });
  3307. if (dx >= dy) {
  3308. var error = dy - (dx >> 1);
  3309. while (x1 != x2) {
  3310. if (error >= 0 && (error != 0 || ix > 0)) {
  3311. error -= dx;
  3312. y1 += iy;
  3313. }
  3314. error += dy;
  3315. x1 += ix;
  3316. points.push({ x: x1, y: y1 });
  3317. }
  3318. } else {
  3319. var error = dx - (dy >> 1);
  3320. while (y1 != y2) {
  3321. if (error >= 0 && (error != 0 || iy > 0)) {
  3322. error -= dy;
  3323. x1 += ix;
  3324. }
  3325. error += dx;
  3326. y1 += iy;
  3327. points.push({ x: x1, y: y1 });
  3328. }
  3329. }
  3330. return points;
  3331. };
  3332. geometrize_rasterizer_Rasterizer.scanlinesForPolygon = function (points) {
  3333. var lines = [];
  3334. var edges = [];
  3335. var _g = 0;
  3336. var _g1 = points.length;
  3337. while (_g < _g1) {
  3338. var i = _g++;
  3339. var p1 = points[i];
  3340. var p2 = i == points.length - 1 ? points[0] : points[i + 1];
  3341. var p1p2 = geometrize_rasterizer_Rasterizer.bresenham(p1.x, p1.y, p2.x, p2.y);
  3342. edges = edges.concat(p1p2);
  3343. }
  3344. var yToXs = new haxe_ds_IntMap();
  3345. var _g = 0;
  3346. while (_g < edges.length) {
  3347. var point = edges[_g];
  3348. ++_g;
  3349. var s = yToXs.h[point.y];
  3350. if (s != null) {
  3351. geometrize_ArraySet.add(s, point.x);
  3352. } else {
  3353. s = geometrize_ArraySet.create();
  3354. geometrize_ArraySet.add(s, point.x);
  3355. yToXs.h[point.y] = s;
  3356. }
  3357. }
  3358. var key = yToXs.keys();
  3359. while (key.hasNext()) {
  3360. var key1 = key.next();
  3361. var a = geometrize_ArraySet.toArray(yToXs.h[key1]);
  3362. var minMaxElements;
  3363. if (a == null || a.length == 0) {
  3364. minMaxElements = { x: 0, y: 0 };
  3365. } else {
  3366. var min = a[0];
  3367. var max = a[0];
  3368. var _g = 0;
  3369. while (_g < a.length) {
  3370. var value = a[_g];
  3371. ++_g;
  3372. if (min > value) {
  3373. min = value;
  3374. }
  3375. if (max < value) {
  3376. max = value;
  3377. }
  3378. }
  3379. minMaxElements = { x: min, y: max };
  3380. }
  3381. lines.push(new geometrize_rasterizer_Scanline(key1, minMaxElements.x, minMaxElements.y));
  3382. }
  3383. return lines;
  3384. };
  3385. var geometrize_rasterizer_Scanline = function (y, x1, x2) {
  3386. this.y = y;
  3387. this.x1 = x1;
  3388. this.x2 = x2;
  3389. };
  3390. geometrize_rasterizer_Scanline.trim = function (scanlines, w, h) {
  3391. if (scanlines == null) {
  3392. throw haxe_Exception.thrown("FAIL: scanlines != null");
  3393. }
  3394. var w1 = w;
  3395. var h1 = h;
  3396. var f = function (line) {
  3397. if (line.y < 0 || line.y >= h1 || line.x1 >= w1 || line.x2 < 0) {
  3398. return false;
  3399. } else {
  3400. var value = line.x1;
  3401. var max = w1 - 1;
  3402. if (0 > max) {
  3403. throw haxe_Exception.thrown("FAIL: min <= max");
  3404. }
  3405. line.x1 = value < 0 ? 0 : value > max ? max : value;
  3406. var value = line.x2;
  3407. var max = w1 - 1;
  3408. if (0 > max) {
  3409. throw haxe_Exception.thrown("FAIL: min <= max");
  3410. }
  3411. line.x2 = value < 0 ? 0 : value > max ? max : value;
  3412. return line.x1 <= line.x2;
  3413. }
  3414. };
  3415. var _g = [];
  3416. var _g1 = 0;
  3417. var _g2 = scanlines;
  3418. while (_g1 < _g2.length) {
  3419. var v = _g2[_g1];
  3420. ++_g1;
  3421. if (f(v)) {
  3422. _g.push(v);
  3423. }
  3424. }
  3425. return _g;
  3426. };
  3427. var geometrize_runner_ImageRunner = ($hx_exports["geometrize"]["runner"]["ImageRunner"] = function (inputImage, backgroundColor) {
  3428. this.model = null;
  3429. this.model = new geometrize_Model(inputImage, backgroundColor);
  3430. });
  3431. geometrize_runner_ImageRunner.prototype = {
  3432. step: function (options) {
  3433. var finalOptions_shapeTypes = options.shapeTypes != null && options.shapeTypes.length != 0 ? options.shapeTypes : geometrize_runner_Default.options.shapeTypes;
  3434. var finalOptions_alpha = options.alpha != null ? options.alpha : geometrize_runner_Default.options.alpha;
  3435. var finalOptions_candidateShapesPerStep = options.candidateShapesPerStep != null ? options.candidateShapesPerStep : geometrize_runner_Default.options.candidateShapesPerStep;
  3436. var finalOptions_shapeMutationsPerStep = options.shapeMutationsPerStep != null ? options.shapeMutationsPerStep : geometrize_runner_Default.options.shapeMutationsPerStep;
  3437. return this.model.step(finalOptions_shapeTypes, finalOptions_alpha, finalOptions_candidateShapesPerStep, finalOptions_shapeMutationsPerStep);
  3438. },
  3439. getImageData: function () {
  3440. if (this.model == null) {
  3441. throw haxe_Exception.thrown("FAIL: model != null");
  3442. }
  3443. return this.model.current;
  3444. },
  3445. };
  3446. var geometrize_runner_Default = function () {};
  3447. var geometrize_shape_Ellipse = function (xBound, yBound) {
  3448. this.x = Std.random(xBound);
  3449. this.y = Std.random(yBound);
  3450. this.rx = Std.random(32) + 1;
  3451. this.ry = Std.random(32) + 1;
  3452. this.xBound = xBound;
  3453. this.yBound = yBound;
  3454. };
  3455. geometrize_shape_Ellipse.prototype = {
  3456. rasterize: function () {
  3457. var lines = [];
  3458. var aspect = this.rx / this.ry;
  3459. var w = this.xBound;
  3460. var h = this.yBound;
  3461. var _g = 0;
  3462. var _g1 = this.ry;
  3463. while (_g < _g1) {
  3464. var dy = _g++;
  3465. var y1 = this.y - dy;
  3466. var y2 = this.y + dy;
  3467. if ((y1 < 0 || y1 >= h) && (y2 < 0 || y2 >= h)) {
  3468. continue;
  3469. }
  3470. var s = (Math.sqrt(this.ry * this.ry - dy * dy) * aspect) | 0;
  3471. var x1 = this.x - s;
  3472. var x2 = this.x + s;
  3473. if (x1 < 0) {
  3474. x1 = 0;
  3475. }
  3476. if (x2 >= w) {
  3477. x2 = w - 1;
  3478. }
  3479. if (y1 >= 0 && y1 < h) {
  3480. lines.push(new geometrize_rasterizer_Scanline(y1, x1, x2));
  3481. }
  3482. if (y2 >= 0 && y2 < h && dy > 0) {
  3483. lines.push(new geometrize_rasterizer_Scanline(y2, x1, x2));
  3484. }
  3485. }
  3486. return lines;
  3487. },
  3488. mutate: function () {
  3489. var r = Std.random(3);
  3490. switch (r) {
  3491. case 0:
  3492. var value = this.x + (-16 + Math.floor(33 * Math.random()));
  3493. var max = this.xBound - 1;
  3494. if (0 > max) {
  3495. throw haxe_Exception.thrown("FAIL: min <= max");
  3496. }
  3497. this.x = value < 0 ? 0 : value > max ? max : value;
  3498. var value = this.y + (-16 + Math.floor(33 * Math.random()));
  3499. var max = this.yBound - 1;
  3500. if (0 > max) {
  3501. throw haxe_Exception.thrown("FAIL: min <= max");
  3502. }
  3503. this.y = value < 0 ? 0 : value > max ? max : value;
  3504. break;
  3505. case 1:
  3506. var value = this.rx + (-16 + Math.floor(33 * Math.random()));
  3507. var max = this.xBound - 1;
  3508. if (1 > max) {
  3509. throw haxe_Exception.thrown("FAIL: min <= max");
  3510. }
  3511. this.rx = value < 1 ? 1 : value > max ? max : value;
  3512. break;
  3513. case 2:
  3514. var value = this.ry + (-16 + Math.floor(33 * Math.random()));
  3515. var max = this.xBound - 1;
  3516. if (1 > max) {
  3517. throw haxe_Exception.thrown("FAIL: min <= max");
  3518. }
  3519. this.ry = value < 1 ? 1 : value > max ? max : value;
  3520. break;
  3521. }
  3522. },
  3523. clone: function () {
  3524. var ellipse = new geometrize_shape_Ellipse(this.xBound, this.yBound);
  3525. ellipse.x = this.x;
  3526. ellipse.y = this.y;
  3527. ellipse.rx = this.rx;
  3528. ellipse.ry = this.ry;
  3529. return ellipse;
  3530. },
  3531. getType: function () {
  3532. return 3;
  3533. },
  3534. getRawShapeData: function () {
  3535. return [this.x, this.y, this.rx, this.ry];
  3536. },
  3537. getSvgShapeData: function () {
  3538. return '<ellipse cx="' + this.x + '" cy="' + this.y + '" rx="' + this.rx + '" ry="' + this.ry + '" ' + geometrize_exporter_SvgExporter.SVG_STYLE_HOOK + " />";
  3539. },
  3540. };
  3541. var geometrize_shape_Circle = function (xBound, yBound) {
  3542. geometrize_shape_Ellipse.call(this, xBound, yBound);
  3543. this.rx = Std.random(32) + 1;
  3544. this.ry = this.rx;
  3545. };
  3546. geometrize_shape_Circle.__super__ = geometrize_shape_Ellipse;
  3547. geometrize_shape_Circle.prototype = $extend(geometrize_shape_Ellipse.prototype, {
  3548. mutate: function () {
  3549. var r = Std.random(2);
  3550. switch (r) {
  3551. case 0:
  3552. var value = this.x + (-16 + Math.floor(33 * Math.random()));
  3553. var max = this.xBound - 1;
  3554. if (0 > max) {
  3555. throw haxe_Exception.thrown("FAIL: min <= max");
  3556. }
  3557. this.x = value < 0 ? 0 : value > max ? max : value;
  3558. var value = this.y + (-16 + Math.floor(33 * Math.random()));
  3559. var max = this.yBound - 1;
  3560. if (0 > max) {
  3561. throw haxe_Exception.thrown("FAIL: min <= max");
  3562. }
  3563. this.y = value < 0 ? 0 : value > max ? max : value;
  3564. break;
  3565. case 1:
  3566. var value = this.rx + (-16 + Math.floor(33 * Math.random()));
  3567. var max = this.xBound - 1;
  3568. if (1 > max) {
  3569. throw haxe_Exception.thrown("FAIL: min <= max");
  3570. }
  3571. var r = value < 1 ? 1 : value > max ? max : value;
  3572. this.rx = r;
  3573. this.ry = r;
  3574. break;
  3575. }
  3576. },
  3577. clone: function () {
  3578. var circle = new geometrize_shape_Circle(this.xBound, this.yBound);
  3579. circle.x = this.x;
  3580. circle.y = this.y;
  3581. circle.rx = this.rx;
  3582. circle.ry = this.ry;
  3583. return circle;
  3584. },
  3585. getType: function () {
  3586. return 5;
  3587. },
  3588. getRawShapeData: function () {
  3589. return [this.x, this.y, this.rx];
  3590. },
  3591. getSvgShapeData: function () {
  3592. return '<circle cx="' + this.x + '" cy="' + this.y + '" r="' + this.rx + '" ' + geometrize_exporter_SvgExporter.SVG_STYLE_HOOK + " />";
  3593. },
  3594. });
  3595. var geometrize_shape_Line = function (xBound, yBound) {
  3596. this.x1 = Std.random(xBound);
  3597. this.y1 = Std.random(yBound);
  3598. var value = this.x1 + Std.random(32) + 1;
  3599. if (0 > xBound) {
  3600. throw haxe_Exception.thrown("FAIL: min <= max");
  3601. }
  3602. this.x2 = value < 0 ? 0 : value > xBound ? xBound : value;
  3603. var value = this.y1 + Std.random(32) + 1;
  3604. if (0 > yBound) {
  3605. throw haxe_Exception.thrown("FAIL: min <= max");
  3606. }
  3607. this.y2 = value < 0 ? 0 : value > yBound ? yBound : value;
  3608. this.xBound = xBound;
  3609. this.yBound = yBound;
  3610. };
  3611. geometrize_shape_Line.prototype = {
  3612. rasterize: function () {
  3613. var lines = [];
  3614. var points = geometrize_rasterizer_Rasterizer.bresenham(this.x1, this.y1, this.x2, this.y2);
  3615. var _g = 0;
  3616. while (_g < points.length) {
  3617. var point = points[_g];
  3618. ++_g;
  3619. lines.push(new geometrize_rasterizer_Scanline(point.y, point.x, point.x));
  3620. }
  3621. return geometrize_rasterizer_Scanline.trim(lines, this.xBound, this.yBound);
  3622. },
  3623. mutate: function () {
  3624. var r = Std.random(4);
  3625. switch (r) {
  3626. case 0:
  3627. var value = this.x1 + (-16 + Math.floor(33 * Math.random()));
  3628. var max = this.xBound - 1;
  3629. if (0 > max) {
  3630. throw haxe_Exception.thrown("FAIL: min <= max");
  3631. }
  3632. this.x1 = value < 0 ? 0 : value > max ? max : value;
  3633. var value = this.y1 + (-16 + Math.floor(33 * Math.random()));
  3634. var max = this.yBound - 1;
  3635. if (0 > max) {
  3636. throw haxe_Exception.thrown("FAIL: min <= max");
  3637. }
  3638. this.y1 = value < 0 ? 0 : value > max ? max : value;
  3639. break;
  3640. case 1:
  3641. var value = this.x2 + (-16 + Math.floor(33 * Math.random()));
  3642. var max = this.xBound - 1;
  3643. if (0 > max) {
  3644. throw haxe_Exception.thrown("FAIL: min <= max");
  3645. }
  3646. this.x2 = value < 0 ? 0 : value > max ? max : value;
  3647. var value = this.y2 + (-16 + Math.floor(33 * Math.random()));
  3648. var max = this.yBound - 1;
  3649. if (0 > max) {
  3650. throw haxe_Exception.thrown("FAIL: min <= max");
  3651. }
  3652. this.y2 = value < 0 ? 0 : value > max ? max : value;
  3653. break;
  3654. }
  3655. },
  3656. clone: function () {
  3657. var line = new geometrize_shape_Line(this.xBound, this.yBound);
  3658. line.x1 = this.x1;
  3659. line.y1 = this.y1;
  3660. line.x2 = this.x2;
  3661. line.y2 = this.y2;
  3662. return line;
  3663. },
  3664. getType: function () {
  3665. return 6;
  3666. },
  3667. getRawShapeData: function () {
  3668. return [this.x1, this.y1, this.x2, this.y2];
  3669. },
  3670. getSvgShapeData: function () {
  3671. return '<line x1="' + this.x1 + '" y1="' + this.y1 + '" x2="' + this.x2 + '" y2="' + this.y2 + '" ' + geometrize_exporter_SvgExporter.SVG_STYLE_HOOK + " />";
  3672. },
  3673. };
  3674. var geometrize_shape_QuadraticBezier = function (xBound, yBound) {
  3675. var upper = xBound - 1;
  3676. if (0 > upper) {
  3677. throw haxe_Exception.thrown("FAIL: lower <= upper");
  3678. }
  3679. this.x1 = Math.floor((upper + 1) * Math.random());
  3680. var upper = yBound - 1;
  3681. if (0 > upper) {
  3682. throw haxe_Exception.thrown("FAIL: lower <= upper");
  3683. }
  3684. this.y1 = Math.floor((upper + 1) * Math.random());
  3685. var upper = xBound - 1;
  3686. if (0 > upper) {
  3687. throw haxe_Exception.thrown("FAIL: lower <= upper");
  3688. }
  3689. this.cx = Math.floor((upper + 1) * Math.random());
  3690. var upper = yBound - 1;
  3691. if (0 > upper) {
  3692. throw haxe_Exception.thrown("FAIL: lower <= upper");
  3693. }
  3694. this.cy = Math.floor((upper + 1) * Math.random());
  3695. var upper = xBound - 1;
  3696. if (0 > upper) {
  3697. throw haxe_Exception.thrown("FAIL: lower <= upper");
  3698. }
  3699. this.x2 = Math.floor((upper + 1) * Math.random());
  3700. var upper = yBound - 1;
  3701. if (0 > upper) {
  3702. throw haxe_Exception.thrown("FAIL: lower <= upper");
  3703. }
  3704. this.y2 = Math.floor((upper + 1) * Math.random());
  3705. this.xBound = xBound;
  3706. this.yBound = yBound;
  3707. };
  3708. geometrize_shape_QuadraticBezier.prototype = {
  3709. rasterize: function () {
  3710. var lines = [];
  3711. var points = [];
  3712. var pointCount = 20;
  3713. var _g = 0;
  3714. var _g1 = pointCount - 1;
  3715. while (_g < _g1) {
  3716. var i = _g++;
  3717. var t = i / pointCount;
  3718. var tp = 1 - t;
  3719. var x = (tp * (tp * this.x1 + t * this.cx) + t * (tp * this.cx + t * this.x2)) | 0;
  3720. var y = (tp * (tp * this.y1 + t * this.cy) + t * (tp * this.cy + t * this.y2)) | 0;
  3721. points.push({ x: x, y: y });
  3722. }
  3723. var _g = 0;
  3724. var _g1 = points.length - 1;
  3725. while (_g < _g1) {
  3726. var i = _g++;
  3727. var p0 = points[i];
  3728. var p1 = points[i + 1];
  3729. var pts = geometrize_rasterizer_Rasterizer.bresenham(p0.x, p0.y, p1.x, p1.y);
  3730. var _g2 = 0;
  3731. while (_g2 < pts.length) {
  3732. var point = pts[_g2];
  3733. ++_g2;
  3734. if (lines.length > 0) {
  3735. var lastLine = lines[lines.length - 1];
  3736. if (lastLine.y == point.y && lastLine.x1 == point.x && lastLine.x2 == point.x) {
  3737. continue;
  3738. }
  3739. }
  3740. lines.push(new geometrize_rasterizer_Scanline(point.y, point.x, point.x));
  3741. }
  3742. }
  3743. return geometrize_rasterizer_Scanline.trim(lines, this.xBound, this.yBound);
  3744. },
  3745. mutate: function () {
  3746. var r = Math.floor(3 * Math.random());
  3747. switch (r) {
  3748. case 0:
  3749. var value = this.cx + (-8 + Math.floor(17 * Math.random()));
  3750. var max = this.xBound - 1;
  3751. if (0 > max) {
  3752. throw haxe_Exception.thrown("FAIL: min <= max");
  3753. }
  3754. this.cx = value < 0 ? 0 : value > max ? max : value;
  3755. var value = this.cy + (-8 + Math.floor(17 * Math.random()));
  3756. var max = this.yBound - 1;
  3757. if (0 > max) {
  3758. throw haxe_Exception.thrown("FAIL: min <= max");
  3759. }
  3760. this.cy = value < 0 ? 0 : value > max ? max : value;
  3761. break;
  3762. case 1:
  3763. var value = this.x1 + (-8 + Math.floor(17 * Math.random()));
  3764. var max = this.xBound - 1;
  3765. if (1 > max) {
  3766. throw haxe_Exception.thrown("FAIL: min <= max");
  3767. }
  3768. this.x1 = value < 1 ? 1 : value > max ? max : value;
  3769. var value = this.y1 + (-8 + Math.floor(17 * Math.random()));
  3770. var max = this.yBound - 1;
  3771. if (1 > max) {
  3772. throw haxe_Exception.thrown("FAIL: min <= max");
  3773. }
  3774. this.y1 = value < 1 ? 1 : value > max ? max : value;
  3775. break;
  3776. case 2:
  3777. var value = this.x2 + (-8 + Math.floor(17 * Math.random()));
  3778. var max = this.xBound - 1;
  3779. if (1 > max) {
  3780. throw haxe_Exception.thrown("FAIL: min <= max");
  3781. }
  3782. this.x2 = value < 1 ? 1 : value > max ? max : value;
  3783. var value = this.y2 + (-8 + Math.floor(17 * Math.random()));
  3784. var max = this.yBound - 1;
  3785. if (1 > max) {
  3786. throw haxe_Exception.thrown("FAIL: min <= max");
  3787. }
  3788. this.y2 = value < 1 ? 1 : value > max ? max : value;
  3789. break;
  3790. }
  3791. },
  3792. clone: function () {
  3793. var bezier = new geometrize_shape_QuadraticBezier(this.xBound, this.yBound);
  3794. bezier.cx = this.cx;
  3795. bezier.cy = this.cy;
  3796. bezier.x1 = this.x1;
  3797. bezier.y1 = this.y1;
  3798. bezier.x2 = this.x2;
  3799. bezier.y2 = this.y2;
  3800. return bezier;
  3801. },
  3802. getType: function () {
  3803. return 7;
  3804. },
  3805. getRawShapeData: function () {
  3806. return [this.x1, this.y1, this.cx, this.cy, this.x2, this.y2];
  3807. },
  3808. getSvgShapeData: function () {
  3809. return '<path d="M' + this.x1 + " " + this.y1 + " Q " + this.cx + " " + this.cy + " " + this.x2 + " " + this.y2 + '" ' + geometrize_exporter_SvgExporter.SVG_STYLE_HOOK + " />";
  3810. },
  3811. };
  3812. var geometrize_shape_Rectangle = function (xBound, yBound) {
  3813. this.x1 = Std.random(xBound);
  3814. this.y1 = Std.random(yBound);
  3815. var value = this.x1 + Std.random(32) + 1;
  3816. var max = xBound - 1;
  3817. if (0 > max) {
  3818. throw haxe_Exception.thrown("FAIL: min <= max");
  3819. }
  3820. this.x2 = value < 0 ? 0 : value > max ? max : value;
  3821. var value = this.y1 + Std.random(32) + 1;
  3822. var max = yBound - 1;
  3823. if (0 > max) {
  3824. throw haxe_Exception.thrown("FAIL: min <= max");
  3825. }
  3826. this.y2 = value < 0 ? 0 : value > max ? max : value;
  3827. this.xBound = xBound;
  3828. this.yBound = yBound;
  3829. };
  3830. geometrize_shape_Rectangle.prototype = {
  3831. rasterize: function () {
  3832. var lines = [];
  3833. var first = this.y1;
  3834. var second = this.y2;
  3835. var yMin = first < second ? first : second;
  3836. var first = this.y1;
  3837. var second = this.y2;
  3838. var yMax = first > second ? first : second;
  3839. if (yMin == yMax) {
  3840. var first = this.x1;
  3841. var second = this.x2;
  3842. var first1 = this.x1;
  3843. var second1 = this.x2;
  3844. lines.push(new geometrize_rasterizer_Scanline(yMin, first < second ? first : second, first1 > second1 ? first1 : second1));
  3845. } else {
  3846. var _g = yMin;
  3847. var _g1 = yMax;
  3848. while (_g < _g1) {
  3849. var y = _g++;
  3850. var first = this.x1;
  3851. var second = this.x2;
  3852. var first1 = this.x1;
  3853. var second1 = this.x2;
  3854. lines.push(new geometrize_rasterizer_Scanline(y, first < second ? first : second, first1 > second1 ? first1 : second1));
  3855. }
  3856. }
  3857. return lines;
  3858. },
  3859. mutate: function () {
  3860. var r = Std.random(2);
  3861. switch (r) {
  3862. case 0:
  3863. var value = this.x1 + (-16 + Math.floor(33 * Math.random()));
  3864. var max = this.xBound - 1;
  3865. if (0 > max) {
  3866. throw haxe_Exception.thrown("FAIL: min <= max");
  3867. }
  3868. this.x1 = value < 0 ? 0 : value > max ? max : value;
  3869. var value = this.y1 + (-16 + Math.floor(33 * Math.random()));
  3870. var max = this.yBound - 1;
  3871. if (0 > max) {
  3872. throw haxe_Exception.thrown("FAIL: min <= max");
  3873. }
  3874. this.y1 = value < 0 ? 0 : value > max ? max : value;
  3875. break;
  3876. case 1:
  3877. var value = this.x2 + (-16 + Math.floor(33 * Math.random()));
  3878. var max = this.xBound - 1;
  3879. if (0 > max) {
  3880. throw haxe_Exception.thrown("FAIL: min <= max");
  3881. }
  3882. this.x2 = value < 0 ? 0 : value > max ? max : value;
  3883. var value = this.y2 + (-16 + Math.floor(33 * Math.random()));
  3884. var max = this.yBound - 1;
  3885. if (0 > max) {
  3886. throw haxe_Exception.thrown("FAIL: min <= max");
  3887. }
  3888. this.y2 = value < 0 ? 0 : value > max ? max : value;
  3889. break;
  3890. }
  3891. },
  3892. clone: function () {
  3893. var rectangle = new geometrize_shape_Rectangle(this.xBound, this.yBound);
  3894. rectangle.x1 = this.x1;
  3895. rectangle.y1 = this.y1;
  3896. rectangle.x2 = this.x2;
  3897. rectangle.y2 = this.y2;
  3898. return rectangle;
  3899. },
  3900. getType: function () {
  3901. return 0;
  3902. },
  3903. getRawShapeData: function () {
  3904. var first = this.x1;
  3905. var second = this.x2;
  3906. var first1 = this.y1;
  3907. var second1 = this.y2;
  3908. var first2 = this.x1;
  3909. var second2 = this.x2;
  3910. var first3 = this.y1;
  3911. var second3 = this.y2;
  3912. return [first < second ? first : second, first1 < second1 ? first1 : second1, first2 > second2 ? first2 : second2, first3 > second3 ? first3 : second3];
  3913. },
  3914. getSvgShapeData: function () {
  3915. var first = this.x1;
  3916. var second = this.x2;
  3917. var first1 = this.y1;
  3918. var second1 = this.y2;
  3919. var first2 = this.x1;
  3920. var second2 = this.x2;
  3921. var first3 = this.x1;
  3922. var second3 = this.x2;
  3923. var first4 = this.y1;
  3924. var second4 = this.y2;
  3925. var first5 = this.y1;
  3926. var second5 = this.y2;
  3927. return (
  3928. '<rect x="' +
  3929. (first < second ? first : second) +
  3930. '" y="' +
  3931. (first1 < second1 ? first1 : second1) +
  3932. '" width="' +
  3933. ((first2 > second2 ? first2 : second2) - (first3 < second3 ? first3 : second3)) +
  3934. '" height="' +
  3935. ((first4 > second4 ? first4 : second4) - (first5 < second5 ? first5 : second5)) +
  3936. '" ' +
  3937. geometrize_exporter_SvgExporter.SVG_STYLE_HOOK +
  3938. " />"
  3939. );
  3940. },
  3941. };
  3942. var geometrize_shape_RotatedEllipse = function (xBound, yBound) {
  3943. this.x = Std.random(xBound);
  3944. this.y = Std.random(yBound);
  3945. this.rx = Std.random(32) + 1;
  3946. this.ry = Std.random(32) + 1;
  3947. this.angle = Std.random(360);
  3948. this.xBound = xBound;
  3949. this.yBound = yBound;
  3950. };
  3951. geometrize_shape_RotatedEllipse.prototype = {
  3952. rasterize: function () {
  3953. var pointCount = 20;
  3954. var points = [];
  3955. var rads = this.angle * (Math.PI / 180.0);
  3956. var c = Math.cos(rads);
  3957. var s = Math.sin(rads);
  3958. var _g = 0;
  3959. var _g1 = pointCount;
  3960. while (_g < _g1) {
  3961. var i = _g++;
  3962. var rot = (360.0 / pointCount) * i * (Math.PI / 180.0);
  3963. var crx = this.rx * Math.cos(rot);
  3964. var cry = this.ry * Math.sin(rot);
  3965. var tx = (crx * c - cry * s + this.x) | 0;
  3966. var ty = (crx * s + cry * c + this.y) | 0;
  3967. points.push({ x: tx, y: ty });
  3968. }
  3969. return geometrize_rasterizer_Scanline.trim(geometrize_rasterizer_Rasterizer.scanlinesForPolygon(points), this.xBound, this.yBound);
  3970. },
  3971. mutate: function () {
  3972. var r = Std.random(4);
  3973. switch (r) {
  3974. case 0:
  3975. var value = this.x + (-16 + Math.floor(33 * Math.random()));
  3976. var max = this.xBound - 1;
  3977. if (0 > max) {
  3978. throw haxe_Exception.thrown("FAIL: min <= max");
  3979. }
  3980. this.x = value < 0 ? 0 : value > max ? max : value;
  3981. var value = this.y + (-16 + Math.floor(33 * Math.random()));
  3982. var max = this.yBound - 1;
  3983. if (0 > max) {
  3984. throw haxe_Exception.thrown("FAIL: min <= max");
  3985. }
  3986. this.y = value < 0 ? 0 : value > max ? max : value;
  3987. break;
  3988. case 1:
  3989. var value = this.rx + (-16 + Math.floor(33 * Math.random()));
  3990. var max = this.xBound - 1;
  3991. if (1 > max) {
  3992. throw haxe_Exception.thrown("FAIL: min <= max");
  3993. }
  3994. this.rx = value < 1 ? 1 : value > max ? max : value;
  3995. break;
  3996. case 2:
  3997. var value = this.ry + (-16 + Math.floor(33 * Math.random()));
  3998. var max = this.yBound - 1;
  3999. if (1 > max) {
  4000. throw haxe_Exception.thrown("FAIL: min <= max");
  4001. }
  4002. this.ry = value < 1 ? 1 : value > max ? max : value;
  4003. break;
  4004. case 3:
  4005. var value = this.angle + (-4 + Math.floor(9 * Math.random()));
  4006. this.angle = value < 0 ? 0 : value > 360 ? 360 : value;
  4007. break;
  4008. }
  4009. },
  4010. clone: function () {
  4011. var ellipse = new geometrize_shape_RotatedEllipse(this.xBound, this.yBound);
  4012. ellipse.x = this.x;
  4013. ellipse.y = this.y;
  4014. ellipse.rx = this.rx;
  4015. ellipse.ry = this.ry;
  4016. ellipse.angle = this.angle;
  4017. return ellipse;
  4018. },
  4019. getType: function () {
  4020. return 4;
  4021. },
  4022. getRawShapeData: function () {
  4023. return [this.x, this.y, this.rx, this.ry, this.angle];
  4024. },
  4025. getSvgShapeData: function () {
  4026. var s = '<g transform="translate(' + this.x + " " + this.y + ") rotate(" + this.angle + ") scale(" + this.rx + " " + this.ry + ')">';
  4027. s += '<ellipse cx="' + 0 + '" cy="' + 0 + '" rx="' + 1 + '" ry="' + 1 + '" ' + geometrize_exporter_SvgExporter.SVG_STYLE_HOOK + " />";
  4028. s += "</g>";
  4029. return s;
  4030. },
  4031. };
  4032. var geometrize_shape_RotatedRectangle = function (xBound, yBound) {
  4033. this.x1 = Std.random(xBound);
  4034. this.y1 = Std.random(yBound);
  4035. var value = this.x1 + Std.random(32) + 1;
  4036. if (0 > xBound) {
  4037. throw haxe_Exception.thrown("FAIL: min <= max");
  4038. }
  4039. this.x2 = value < 0 ? 0 : value > xBound ? xBound : value;
  4040. var value = this.y1 + Std.random(32) + 1;
  4041. if (0 > yBound) {
  4042. throw haxe_Exception.thrown("FAIL: min <= max");
  4043. }
  4044. this.y2 = value < 0 ? 0 : value > yBound ? yBound : value;
  4045. this.angle = Math.floor(361 * Math.random());
  4046. this.xBound = xBound;
  4047. this.yBound = yBound;
  4048. };
  4049. geometrize_shape_RotatedRectangle.prototype = {
  4050. rasterize: function () {
  4051. var first = this.x1;
  4052. var second = this.x2;
  4053. var xm1 = first < second ? first : second;
  4054. var first = this.x1;
  4055. var second = this.x2;
  4056. var xm2 = first > second ? first : second;
  4057. var first = this.y1;
  4058. var second = this.y2;
  4059. var ym1 = first < second ? first : second;
  4060. var first = this.y1;
  4061. var second = this.y2;
  4062. var ym2 = first > second ? first : second;
  4063. var cx = ((xm1 + xm2) / 2) | 0;
  4064. var cy = ((ym1 + ym2) / 2) | 0;
  4065. var ox1 = xm1 - cx;
  4066. var ox2 = xm2 - cx;
  4067. var oy1 = ym1 - cy;
  4068. var oy2 = ym2 - cy;
  4069. var rads = (this.angle * Math.PI) / 180.0;
  4070. var c = Math.cos(rads);
  4071. var s = Math.sin(rads);
  4072. var ulx = (ox1 * c - oy1 * s + cx) | 0;
  4073. var uly = (ox1 * s + oy1 * c + cy) | 0;
  4074. var blx = (ox1 * c - oy2 * s + cx) | 0;
  4075. var bly = (ox1 * s + oy2 * c + cy) | 0;
  4076. var urx = (ox2 * c - oy1 * s + cx) | 0;
  4077. var ury = (ox2 * s + oy1 * c + cy) | 0;
  4078. var brx = (ox2 * c - oy2 * s + cx) | 0;
  4079. var bry = (ox2 * s + oy2 * c + cy) | 0;
  4080. return geometrize_rasterizer_Scanline.trim(
  4081. geometrize_rasterizer_Rasterizer.scanlinesForPolygon([
  4082. { x: ulx, y: uly },
  4083. { x: urx, y: ury },
  4084. { x: brx, y: bry },
  4085. { x: blx, y: bly },
  4086. ]),
  4087. this.xBound,
  4088. this.yBound
  4089. );
  4090. },
  4091. mutate: function () {
  4092. var r = Std.random(3);
  4093. switch (r) {
  4094. case 0:
  4095. var value = this.x1 + (-16 + Math.floor(33 * Math.random()));
  4096. var max = this.xBound - 1;
  4097. if (0 > max) {
  4098. throw haxe_Exception.thrown("FAIL: min <= max");
  4099. }
  4100. this.x1 = value < 0 ? 0 : value > max ? max : value;
  4101. var value = this.y1 + (-16 + Math.floor(33 * Math.random()));
  4102. var max = this.yBound - 1;
  4103. if (0 > max) {
  4104. throw haxe_Exception.thrown("FAIL: min <= max");
  4105. }
  4106. this.y1 = value < 0 ? 0 : value > max ? max : value;
  4107. break;
  4108. case 1:
  4109. var value = this.x2 + (-16 + Math.floor(33 * Math.random()));
  4110. var max = this.xBound - 1;
  4111. if (0 > max) {
  4112. throw haxe_Exception.thrown("FAIL: min <= max");
  4113. }
  4114. this.x2 = value < 0 ? 0 : value > max ? max : value;
  4115. var value = this.y2 + (-16 + Math.floor(33 * Math.random()));
  4116. var max = this.yBound - 1;
  4117. if (0 > max) {
  4118. throw haxe_Exception.thrown("FAIL: min <= max");
  4119. }
  4120. this.y2 = value < 0 ? 0 : value > max ? max : value;
  4121. break;
  4122. case 2:
  4123. var value = this.angle + (-4 + Math.floor(9 * Math.random()));
  4124. this.angle = value < 0 ? 0 : value > 360 ? 360 : value;
  4125. break;
  4126. }
  4127. },
  4128. clone: function () {
  4129. var rectangle = new geometrize_shape_RotatedRectangle(this.xBound, this.yBound);
  4130. rectangle.x1 = this.x1;
  4131. rectangle.y1 = this.y1;
  4132. rectangle.x2 = this.x2;
  4133. rectangle.y2 = this.y2;
  4134. rectangle.angle = this.angle;
  4135. return rectangle;
  4136. },
  4137. getType: function () {
  4138. return 1;
  4139. },
  4140. getRawShapeData: function () {
  4141. var first = this.x1;
  4142. var second = this.x2;
  4143. var first1 = this.y1;
  4144. var second1 = this.y2;
  4145. var first2 = this.x1;
  4146. var second2 = this.x2;
  4147. var first3 = this.y1;
  4148. var second3 = this.y2;
  4149. return [first < second ? first : second, first1 < second1 ? first1 : second1, first2 > second2 ? first2 : second2, first3 > second3 ? first3 : second3, this.angle];
  4150. },
  4151. getSvgShapeData: function () {
  4152. var first = this.x1;
  4153. var second = this.x2;
  4154. var xm1 = first < second ? first : second;
  4155. var first = this.x1;
  4156. var second = this.x2;
  4157. var xm2 = first > second ? first : second;
  4158. var first = this.y1;
  4159. var second = this.y2;
  4160. var ym1 = first < second ? first : second;
  4161. var first = this.y1;
  4162. var second = this.y2;
  4163. var ym2 = first > second ? first : second;
  4164. var cx = ((xm1 + xm2) / 2) | 0;
  4165. var cy = ((ym1 + ym2) / 2) | 0;
  4166. var ox1 = xm1 - cx;
  4167. var ox2 = xm2 - cx;
  4168. var oy1 = ym1 - cy;
  4169. var oy2 = ym2 - cy;
  4170. var rads = (this.angle * Math.PI) / 180.0;
  4171. var c = Math.cos(rads);
  4172. var s = Math.sin(rads);
  4173. var ulx = (ox1 * c - oy1 * s + cx) | 0;
  4174. var uly = (ox1 * s + oy1 * c + cy) | 0;
  4175. var blx = (ox1 * c - oy2 * s + cx) | 0;
  4176. var bly = (ox1 * s + oy2 * c + cy) | 0;
  4177. var urx = (ox2 * c - oy1 * s + cx) | 0;
  4178. var ury = (ox2 * s + oy1 * c + cy) | 0;
  4179. var brx = (ox2 * c - oy2 * s + cx) | 0;
  4180. var bry = (ox2 * s + oy2 * c + cy) | 0;
  4181. var points = [
  4182. { x: ulx, y: uly },
  4183. { x: urx, y: ury },
  4184. { x: brx, y: bry },
  4185. { x: blx, y: bly },
  4186. ];
  4187. var s = '<polygon points="';
  4188. var _g = 0;
  4189. var _g1 = points.length;
  4190. while (_g < _g1) {
  4191. var i = _g++;
  4192. s += points[i].x + " " + points[i].y;
  4193. if (i != points.length - 1) {
  4194. s += " ";
  4195. }
  4196. }
  4197. s += '" ' + geometrize_exporter_SvgExporter.SVG_STYLE_HOOK + "/>";
  4198. return s;
  4199. },
  4200. };
  4201. var geometrize_shape_ShapeFactory = function () {};
  4202. geometrize_shape_ShapeFactory.create = function (type, xBound, yBound) {
  4203. switch (type) {
  4204. case 0:
  4205. return new geometrize_shape_Rectangle(xBound, yBound);
  4206. case 1:
  4207. return new geometrize_shape_RotatedRectangle(xBound, yBound);
  4208. case 2:
  4209. return new geometrize_shape_Triangle(xBound, yBound);
  4210. case 3:
  4211. return new geometrize_shape_Ellipse(xBound, yBound);
  4212. case 4:
  4213. return new geometrize_shape_RotatedEllipse(xBound, yBound);
  4214. case 5:
  4215. return new geometrize_shape_Circle(xBound, yBound);
  4216. case 6:
  4217. return new geometrize_shape_Line(xBound, yBound);
  4218. case 7:
  4219. return new geometrize_shape_QuadraticBezier(xBound, yBound);
  4220. }
  4221. };
  4222. geometrize_shape_ShapeFactory.randomShapeOf = function (types, xBound, yBound) {
  4223. if (!(types != null && types.length > 0)) {
  4224. throw haxe_Exception.thrown("FAIL: a != null && a.length > 0");
  4225. }
  4226. var upper = types.length - 1;
  4227. if (0 > upper) {
  4228. throw haxe_Exception.thrown("FAIL: lower <= upper");
  4229. }
  4230. return geometrize_shape_ShapeFactory.create(types[Math.floor((upper + 1) * Math.random())], xBound, yBound);
  4231. };
  4232. var geometrize_shape_ShapeTypes = ($hx_exports["geometrize"]["shape"]["ShapeTypes"] = function () {});
  4233. var geometrize_shape_Triangle = function (xBound, yBound) {
  4234. this.x1 = Std.random(xBound);
  4235. this.y1 = Std.random(yBound);
  4236. this.x2 = this.x1 + (-16 + Math.floor(33 * Math.random()));
  4237. this.y2 = this.y1 + (-16 + Math.floor(33 * Math.random()));
  4238. this.x3 = this.x1 + (-16 + Math.floor(33 * Math.random()));
  4239. this.y3 = this.y1 + (-16 + Math.floor(33 * Math.random()));
  4240. this.xBound = xBound;
  4241. this.yBound = yBound;
  4242. };
  4243. geometrize_shape_Triangle.prototype = {
  4244. rasterize: function () {
  4245. return geometrize_rasterizer_Scanline.trim(
  4246. geometrize_rasterizer_Rasterizer.scanlinesForPolygon([
  4247. { x: this.x1, y: this.y1 },
  4248. { x: this.x2, y: this.y2 },
  4249. { x: this.x3, y: this.y3 },
  4250. ]),
  4251. this.xBound,
  4252. this.yBound
  4253. );
  4254. },
  4255. mutate: function () {
  4256. var r = Std.random(3);
  4257. switch (r) {
  4258. case 0:
  4259. var value = this.x1 + (-16 + Math.floor(33 * Math.random()));
  4260. var max = this.xBound - 1;
  4261. if (0 > max) {
  4262. throw haxe_Exception.thrown("FAIL: min <= max");
  4263. }
  4264. this.x1 = value < 0 ? 0 : value > max ? max : value;
  4265. var value = this.y1 + (-16 + Math.floor(33 * Math.random()));
  4266. var max = this.yBound - 1;
  4267. if (0 > max) {
  4268. throw haxe_Exception.thrown("FAIL: min <= max");
  4269. }
  4270. this.y1 = value < 0 ? 0 : value > max ? max : value;
  4271. break;
  4272. case 1:
  4273. var value = this.x2 + (-16 + Math.floor(33 * Math.random()));
  4274. var max = this.xBound - 1;
  4275. if (0 > max) {
  4276. throw haxe_Exception.thrown("FAIL: min <= max");
  4277. }
  4278. this.x2 = value < 0 ? 0 : value > max ? max : value;
  4279. var value = this.y2 + (-16 + Math.floor(33 * Math.random()));
  4280. var max = this.yBound - 1;
  4281. if (0 > max) {
  4282. throw haxe_Exception.thrown("FAIL: min <= max");
  4283. }
  4284. this.y2 = value < 0 ? 0 : value > max ? max : value;
  4285. break;
  4286. case 2:
  4287. var value = this.x3 + (-16 + Math.floor(33 * Math.random()));
  4288. var max = this.xBound - 1;
  4289. if (0 > max) {
  4290. throw haxe_Exception.thrown("FAIL: min <= max");
  4291. }
  4292. this.x3 = value < 0 ? 0 : value > max ? max : value;
  4293. var value = this.y3 + (-16 + Math.floor(33 * Math.random()));
  4294. var max = this.yBound - 1;
  4295. if (0 > max) {
  4296. throw haxe_Exception.thrown("FAIL: min <= max");
  4297. }
  4298. this.y3 = value < 0 ? 0 : value > max ? max : value;
  4299. break;
  4300. }
  4301. },
  4302. clone: function () {
  4303. var triangle = new geometrize_shape_Triangle(this.xBound, this.yBound);
  4304. triangle.x1 = this.x1;
  4305. triangle.y1 = this.y1;
  4306. triangle.x2 = this.x2;
  4307. triangle.y2 = this.y2;
  4308. triangle.x3 = this.x3;
  4309. triangle.y3 = this.y3;
  4310. return triangle;
  4311. },
  4312. getType: function () {
  4313. return 2;
  4314. },
  4315. getRawShapeData: function () {
  4316. return [this.x1, this.y1, this.x2, this.y2, this.x3, this.y3];
  4317. },
  4318. getSvgShapeData: function () {
  4319. return '<polygon points="' + this.x1 + "," + this.y1 + " " + this.x2 + "," + this.y2 + " " + this.x3 + "," + this.y3 + '" ' + geometrize_exporter_SvgExporter.SVG_STYLE_HOOK + "/>";
  4320. },
  4321. };
  4322. var haxe_Exception = function (message, previous, native) {
  4323. Error.call(this, message);
  4324. this.message = message;
  4325. this.__previousException = previous;
  4326. this.__nativeException = native != null ? native : this;
  4327. };
  4328. haxe_Exception.thrown = function (value) {
  4329. if (value instanceof haxe_Exception) {
  4330. return value.get_native();
  4331. } else if (value instanceof Error) {
  4332. return value;
  4333. } else {
  4334. var e = new haxe_ValueException(value);
  4335. return e;
  4336. }
  4337. };
  4338. haxe_Exception.__super__ = Error;
  4339. haxe_Exception.prototype = $extend(Error.prototype, {
  4340. get_native: function () {
  4341. return this.__nativeException;
  4342. },
  4343. });
  4344. var haxe_ValueException = function (value, previous, native) {
  4345. haxe_Exception.call(this, String(value), previous, native);
  4346. this.value = value;
  4347. };
  4348. haxe_ValueException.__super__ = haxe_Exception;
  4349. haxe_ValueException.prototype = $extend(haxe_Exception.prototype, {});
  4350. var haxe_ds_IntMap = function () {
  4351. this.h = {};
  4352. };
  4353. haxe_ds_IntMap.prototype = {
  4354. keys: function () {
  4355. var a = [];
  4356. for (var key in this.h) if (this.h.hasOwnProperty(key)) a.push(+key);
  4357. return new haxe_iterators_ArrayIterator(a);
  4358. },
  4359. };
  4360. var haxe_io_Bytes = function (data) {
  4361. this.length = data.byteLength;
  4362. this.b = new Uint8Array(data);
  4363. this.b.bufferValue = data;
  4364. data.hxBytes = this;
  4365. data.bytes = this.b;
  4366. };
  4367. var haxe_iterators_ArrayIterator = function (array) {
  4368. this.current = 0;
  4369. this.array = array;
  4370. };
  4371. haxe_iterators_ArrayIterator.prototype = {
  4372. hasNext: function () {
  4373. return this.current < this.array.length;
  4374. },
  4375. next: function () {
  4376. return this.array[this.current++];
  4377. },
  4378. };
  4379. onmessage = GeometrizeWorker.prototype.messageHandler;
  4380. geometrize_exporter_SvgExporter.SVG_STYLE_HOOK = "::svg_style_hook::";
  4381. geometrize_runner_Default.options = {
  4382. shapeTypes: [2],
  4383. candidateShapesPerStep: 50,
  4384. shapeMutationsPerStep: 100,
  4385. alpha: 128,
  4386. };
  4387. geometrize_shape_ShapeTypes.RECTANGLE = 0;
  4388. geometrize_shape_ShapeTypes.ROTATED_RECTANGLE = 1;
  4389. geometrize_shape_ShapeTypes.TRIANGLE = 2;
  4390. geometrize_shape_ShapeTypes.ELLIPSE = 3;
  4391. geometrize_shape_ShapeTypes.ROTATED_ELLIPSE = 4;
  4392. geometrize_shape_ShapeTypes.CIRCLE = 5;
  4393. geometrize_shape_ShapeTypes.LINE = 6;
  4394. geometrize_shape_ShapeTypes.QUADRATIC_BEZIER = 7;
  4395. })(typeof exports != "undefined" ? exports : typeof window != "undefined" ? window : typeof self != "undefined" ? self : this, {});`
  4396. );