From e9377de01f0c57c0651da01ab8afbd1a97896389 Mon Sep 17 00:00:00 2001 From: hal8174 Date: Mon, 23 Sep 2024 01:02:50 +0200 Subject: [PATCH] Refactoring layout. --- Cargo.lock | 296 ++++++++++++++----------- examples/layout.rs | 36 +-- src/belt_finding/conflict_avoidance.rs | 16 +- src/belt_finding/mod.rs | 50 ++--- src/bin/beltfinding.rs | 1 + src/bin/create_beltfinding_image.rs | 32 +++ src/common/aabb.rs | 1 + src/common/block.rs | 47 +++- src/common/transformation.rs | 43 +++- src/layout/mod.rs | 203 ++++++----------- src/lib.rs | 2 +- 11 files changed, 407 insertions(+), 320 deletions(-) create mode 100644 src/bin/create_beltfinding_image.rs diff --git a/Cargo.lock b/Cargo.lock index 6360c5e..e8c2d40 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,12 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "adler2" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" + [[package]] name = "aho-corasick" version = "1.1.3" @@ -25,50 +31,51 @@ checksum = "4aa90d7ce82d4be67b64039a3d588d38dbcc6736577de4a847025ce5b0c468d1" [[package]] name = "anstream" -version = "0.6.13" +version = "0.6.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d96bd03f33fe50a863e394ee9718a706f988b9079b20c3784fb726e7678b62fb" +checksum = "64e15c1ab1f89faffbf04a634d5e1962e9074f2741eef6d97f3c4e322426d526" dependencies = [ "anstyle", "anstyle-parse", "anstyle-query", "anstyle-wincon", "colorchoice", + "is_terminal_polyfill", "utf8parse", ] [[package]] name = "anstyle" -version = "1.0.6" +version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8901269c6307e8d93993578286ac0edf7f195079ffff5ebdeea6a59ffb7e36bc" +checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1" [[package]] name = "anstyle-parse" -version = "0.2.3" +version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c" +checksum = "eb47de1e80c2b463c735db5b217a0ddc39d612e7ac9e2e96a5aed1f57616c1cb" dependencies = [ "utf8parse", ] [[package]] name = "anstyle-query" -version = "1.0.2" +version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648" +checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a" dependencies = [ - "windows-sys", + "windows-sys 0.52.0", ] [[package]] name = "anstyle-wincon" -version = "3.0.2" +version = "3.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7" +checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8" dependencies = [ "anstyle", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -113,9 +120,9 @@ dependencies = [ [[package]] name = "autocfg" -version = "1.1.0" +version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" [[package]] name = "av1-grain" @@ -172,9 +179,9 @@ checksum = "236e6289eda5a812bc6b53c3b024039382a2895fbbeef2d748b2931546d392c4" [[package]] name = "bumpalo" -version = "3.15.4" +version = "3.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ff69b9dd49fd426c69a0db9fc04dd934cdb6645ff000864d98f7e2af8830eaa" +checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" [[package]] name = "bytemuck" @@ -202,9 +209,9 @@ checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" [[package]] name = "cc" -version = "1.1.18" +version = "1.1.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b62ac837cdb5cb22e10a256099b4fc502b1dfe560cb282963a974d7abd80e476" +checksum = "07b1695e2c7e8fc85310cde85aeaab7e3097f593c91d209d3f9df76c928100f0" dependencies = [ "jobserver", "libc", @@ -240,9 +247,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.3" +version = "4.5.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "949626d00e063efc93b6dca932419ceb5432f99769911c0b995f7e884c778813" +checksum = "3e5a21b8495e732f1b3c364c9949b201ca7bae518c502c80256c96ad79eaf6ac" dependencies = [ "clap_builder", "clap_derive", @@ -250,9 +257,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.2" +version = "4.5.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae129e2e766ae0ec03484e609954119f123cc1fe650337e155d03b022f24f7b4" +checksum = "8cf2dd12af7a047ad9d6da2b6b249759a22a7abc0f474c1dae1777afa4b21a73" dependencies = [ "anstream", "anstyle", @@ -262,9 +269,9 @@ dependencies = [ [[package]] name = "clap_derive" -version = "4.5.3" +version = "4.5.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90239a040c80f5e14809ca132ddc4176ab33d5e17e49691793296e3fcb34d72f" +checksum = "501d359d5f3dcaf6ecdeee48833ae73ec6e42723a1e52419c79abf9507eec0a0" dependencies = [ "heck", "proc-macro2", @@ -274,9 +281,9 @@ dependencies = [ [[package]] name = "clap_lex" -version = "0.7.0" +version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce" +checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97" [[package]] name = "color_quant" @@ -286,15 +293,15 @@ checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" [[package]] name = "colorchoice" -version = "1.0.0" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" +checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0" [[package]] name = "crc32fast" -version = "1.4.0" +version = "1.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b3855a8a784b474f333699ef2bbca9db2c4a1f6d9088a90a2d25b1eb53111eaa" +checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3" dependencies = [ "cfg-if", ] @@ -356,9 +363,9 @@ dependencies = [ [[package]] name = "crossbeam-utils" -version = "0.8.19" +version = "0.8.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "248e3bacc7dc6baa3b21e405ee045c3047101a49145e7e9eca583ab4c2ca5345" +checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" [[package]] name = "crunchy" @@ -389,9 +396,9 @@ dependencies = [ [[package]] name = "either" -version = "1.10.0" +version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "11157ac094ffbdde99aa67b23417ebdd801842852b500e395a45a9c0aac03e4a" +checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" [[package]] name = "equivalent" @@ -409,7 +416,7 @@ dependencies = [ "flume", "half 2.4.1", "lebe", - "miniz_oxide", + "miniz_oxide 0.7.4", "rayon-core", "smallvec", "zune-inflate", @@ -420,7 +427,7 @@ name = "factorio_blueprint" version = "0.1.0" dependencies = [ "base64", - "clap 4.5.3", + "clap 4.5.17", "criterion", "flate2", "image", @@ -442,12 +449,12 @@ dependencies = [ [[package]] name = "flate2" -version = "1.0.28" +version = "1.0.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46303f565772937ffe1d394a4fac6f411c6013172fadde9dcdb1e147a086940e" +checksum = "324a1be68054ef05ad64b861cc9eaf1d623d2d8cb25b4bf2cb9cdd902b4bf253" dependencies = [ "crc32fast", - "miniz_oxide", + "miniz_oxide 0.8.0", ] [[package]] @@ -558,9 +565,9 @@ checksum = "44feda355f4159a7c757171a77de25daf6411e217b4cabd03bd6650690468126" [[package]] name = "indexmap" -version = "2.4.0" +version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93ead53efc7ea8ed3cfb0c79fc8023fbb782a5432b52830b6518941cebe6505c" +checksum = "68b900aa2f7301e21c36462b170ee99994de34dff39a4a6a528e80e7376d07e5" dependencies = [ "equivalent", "hashbrown", @@ -577,6 +584,12 @@ dependencies = [ "syn", ] +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + [[package]] name = "itertools" version = "0.10.5" @@ -597,9 +610,9 @@ dependencies = [ [[package]] name = "itoa" -version = "1.0.10" +version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" +checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" [[package]] name = "jobserver" @@ -618,18 +631,18 @@ checksum = "f5d4a7da358eff58addd2877a45865158f0d78c911d43a5784ceb7bbf52833b0" [[package]] name = "js-sys" -version = "0.3.69" +version = "0.3.70" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29c15563dc2726973df627357ce0c9ddddbea194836909d655df6a75d2cf296d" +checksum = "1868808506b929d7b0cfa8f75951347aa71bb21144b7791bae35d9bccfcfe37a" dependencies = [ "wasm-bindgen", ] [[package]] name = "lazy_static" -version = "1.4.0" +version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" [[package]] name = "lebe" @@ -666,9 +679,9 @@ dependencies = [ [[package]] name = "log" -version = "0.4.21" +version = "0.4.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" [[package]] name = "loop9" @@ -690,9 +703,9 @@ dependencies = [ [[package]] name = "memchr" -version = "2.7.1" +version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" [[package]] name = "minimal-lexical" @@ -702,14 +715,23 @@ checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" [[package]] name = "miniz_oxide" -version = "0.7.2" +version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d811f3e15f28568be3407c8e7fdb6514c1cda3cb30683f15b6a1a1dc4ea14a7" +checksum = "b8a240ddb74feaf34a79a7add65a741f3167852fba007066dcac1ca548d89c08" dependencies = [ "adler", "simd-adler32", ] +[[package]] +name = "miniz_oxide" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2d80299ef12ff69b16a84bb182e3b9df68b5a91574d3d4fa6e41b65deec4df1" +dependencies = [ + "adler2", +] + [[package]] name = "new_debug_unreachable" version = "1.0.6" @@ -775,9 +797,9 @@ dependencies = [ [[package]] name = "num-traits" -version = "0.2.18" +version = "0.2.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da0df0e5185db44f69b44f26786fe401b6c293d1907744beaa7fa62b2e5a517a" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" dependencies = [ "autocfg", ] @@ -790,9 +812,9 @@ checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" [[package]] name = "oorandom" -version = "11.1.3" +version = "11.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575" +checksum = "b410bbe7e14ab526a0e86877eb47c6996a2bd7746f027ba551028c925390e4e9" [[package]] name = "paste" @@ -808,9 +830,9 @@ checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" [[package]] name = "plotters" -version = "0.3.5" +version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d2c224ba00d7cadd4d5c660deaf2098e5e80e07846537c51f9cfa4be50c1fd45" +checksum = "5aeb6f403d7a4911efb1e33402027fc44f29b5bf6def3effcc22d7bb75f2b747" dependencies = [ "num-traits", "plotters-backend", @@ -821,15 +843,15 @@ dependencies = [ [[package]] name = "plotters-backend" -version = "0.3.5" +version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e76628b4d3a7581389a35d5b6e2139607ad7c75b17aed325f210aa91f4a9609" +checksum = "df42e13c12958a16b3f7f4386b9ab1f3e7933914ecea48da7139435263a4172a" [[package]] name = "plotters-svg" -version = "0.3.5" +version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38f6d39893cca0701371e3c27294f09797214b86f1fb951b89ade8ec04e2abab" +checksum = "51bae2ac328883f7acdfea3d66a7c35751187f870bc81f94563733a154d7a670" dependencies = [ "plotters-backend", ] @@ -844,7 +866,7 @@ dependencies = [ "crc32fast", "fdeflate", "flate2", - "miniz_oxide", + "miniz_oxide 0.7.4", ] [[package]] @@ -858,9 +880,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.79" +version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e835ff2298f5721608eb1a980ecaee1aef2c132bf95ecc026a11b7bf3c01c02e" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" dependencies = [ "unicode-ident", ] @@ -901,9 +923,9 @@ checksum = "a993555f31e5a609f617c12db6250dedcac1b0a85076912c436e6fc9b2c8e6a3" [[package]] name = "quote" -version = "1.0.35" +version = "1.0.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" dependencies = [ "proc-macro2", ] @@ -989,9 +1011,9 @@ dependencies = [ [[package]] name = "rayon" -version = "1.9.0" +version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e4963ed1bc86e4f3ee217022bd855b297cef07fb9eac5dfa1f788b220b49b3bd" +checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa" dependencies = [ "either", "rayon-core", @@ -1009,9 +1031,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.10.4" +version = "1.10.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c117dbdfde9c8308975b6a18d71f3f385c89461f7b3fb054288ecf2a2058ba4c" +checksum = "4219d74c6b67a3654a9fbebc4b419e22126d13d2f3c4a07ee0cb61ff79a79619" dependencies = [ "aho-corasick", "memchr", @@ -1021,9 +1043,9 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.4.6" +version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86b83b8b9847f9bf95ef68afb0b8e6cdb80f498442f5179a29fad448fcc1eaea" +checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" dependencies = [ "aho-corasick", "memchr", @@ -1032,9 +1054,9 @@ dependencies = [ [[package]] name = "regex-syntax" -version = "0.8.2" +version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" +checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" [[package]] name = "rgb" @@ -1047,9 +1069,9 @@ dependencies = [ [[package]] name = "ryu" -version = "1.0.17" +version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e86697c916019a8588c99b5fac3cead74ec0b4b819707a682fd4d23fa0ce1ba1" +checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" [[package]] name = "same-file" @@ -1068,9 +1090,9 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" [[package]] name = "serde" -version = "1.0.197" +version = "1.0.210" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fb1c873e1b9b056a4dc4c0c198b24c3ffa059243875552b2bd0933b1aee4ce2" +checksum = "c8e3592472072e6e22e0a54d5904d9febf8508f65fb8552499a1abc7d1078c3a" dependencies = [ "serde_derive", ] @@ -1087,9 +1109,9 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.197" +version = "1.0.210" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" +checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f" dependencies = [ "proc-macro2", "quote", @@ -1098,11 +1120,12 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.114" +version = "1.0.128" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c5f09b1bd632ef549eaa9f60a1f8de742bdbc698e6cee2095fc84dde5f549ae0" +checksum = "6ff5456707a1de34e7e37f2a6fd3d3f808c318259cbd01ab6377795054b483d8" dependencies = [ "itoa", + "memchr", "ryu", "serde", ] @@ -1167,15 +1190,15 @@ dependencies = [ [[package]] name = "strsim" -version = "0.11.0" +version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ee073c9e4cd00e28217186dbe12796d692868f432bf2e97ee73bed0c56dfa01" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" [[package]] name = "syn" -version = "2.0.53" +version = "2.0.77" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7383cd0e49fff4b6b90ca5670bfd3e9d6a733b3f90c686605aa7eec8c4996032" +checksum = "9f35bcdf61fd8e7be6caf75f429fdca8beb3ed76584befb503b1569faee373ed" dependencies = [ "proc-macro2", "quote", @@ -1283,9 +1306,9 @@ dependencies = [ [[package]] name = "toml_edit" -version = "0.22.20" +version = "0.22.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "583c44c02ad26b0c3f3066fe629275e50627026c51ac2e595cca4c230ce1ce1d" +checksum = "3b072cee73c449a636ffd6f32bd8de3a9f7119139aff882f44943ce2986dc5cf" dependencies = [ "indexmap", "serde", @@ -1296,15 +1319,15 @@ dependencies = [ [[package]] name = "unicode-ident" -version = "1.0.12" +version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" [[package]] name = "unicode-width" -version = "0.1.11" +version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85" +checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" [[package]] name = "unsafe-libyaml" @@ -1314,9 +1337,9 @@ checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861" [[package]] name = "utf8parse" -version = "0.2.1" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" [[package]] name = "v_frame" @@ -1353,19 +1376,20 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasm-bindgen" -version = "0.2.92" +version = "0.2.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8" +checksum = "a82edfc16a6c469f5f44dc7b571814045d60404b55a0ee849f9bcfa2e63dd9b5" dependencies = [ "cfg-if", + "once_cell", "wasm-bindgen-macro", ] [[package]] name = "wasm-bindgen-backend" -version = "0.2.92" +version = "0.2.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "614d787b966d3989fa7bb98a654e369c762374fd3213d212cfc0251257e747da" +checksum = "9de396da306523044d3302746f1208fa71d7532227f15e347e2d93e4145dd77b" dependencies = [ "bumpalo", "log", @@ -1378,9 +1402,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.92" +version = "0.2.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1f8823de937b71b9460c0c34e25f3da88250760bec0ebac694b49997550d726" +checksum = "585c4c91a46b072c92e908d99cb1dcdf95c5218eeb6f3bf1efa991ee7a68cccf" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -1388,9 +1412,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.92" +version = "0.2.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" +checksum = "afc340c74d9005395cf9dd098506f7f44e38f2b4a21c6aaacf9a105ea5e1e836" dependencies = [ "proc-macro2", "quote", @@ -1401,15 +1425,15 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.92" +version = "0.2.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96" +checksum = "c62a0a307cb4a311d3a07867860911ca130c3494e8c2719593806c08bc5d0484" [[package]] name = "web-sys" -version = "0.3.69" +version = "0.3.70" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77afa9a11836342370f4817622a2f0f418b134426d91a82dfb48f532d2ec13ef" +checksum = "26fdeaafd9bd129f65e7c031593c24d62186301e0c72c8978fa1678be7d532c0" dependencies = [ "js-sys", "wasm-bindgen", @@ -1439,11 +1463,11 @@ checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] name = "winapi-util" -version = "0.1.6" +version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" +checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" dependencies = [ - "winapi", + "windows-sys 0.59.0", ] [[package]] @@ -1462,14 +1486,24 @@ dependencies = [ ] [[package]] -name = "windows-targets" -version = "0.52.4" +name = "windows-sys" +version = "0.59.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7dd37b7e5ab9018759f893a1952c9420d060016fc19a472b4bb20d1bdd694d1b" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ "windows_aarch64_gnullvm", "windows_aarch64_msvc", "windows_i686_gnu", + "windows_i686_gnullvm", "windows_i686_msvc", "windows_x86_64_gnu", "windows_x86_64_gnullvm", @@ -1478,45 +1512,51 @@ dependencies = [ [[package]] name = "windows_aarch64_gnullvm" -version = "0.52.4" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bcf46cf4c365c6f2d1cc93ce535f2c8b244591df96ceee75d8e83deb70a9cac9" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" [[package]] name = "windows_aarch64_msvc" -version = "0.52.4" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da9f259dd3bcf6990b55bffd094c4f7235817ba4ceebde8e6d11cd0c5633b675" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" [[package]] name = "windows_i686_gnu" -version = "0.52.4" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b474d8268f99e0995f25b9f095bc7434632601028cf86590aea5c8a5cb7801d3" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" [[package]] name = "windows_i686_msvc" -version = "0.52.4" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1515e9a29e5bed743cb4415a9ecf5dfca648ce85ee42e15873c3cd8610ff8e02" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" [[package]] name = "windows_x86_64_gnu" -version = "0.52.4" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5eee091590e89cc02ad514ffe3ead9eb6b660aedca2183455434b93546371a03" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" [[package]] name = "windows_x86_64_gnullvm" -version = "0.52.4" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77ca79f2451b49fa9e2af39f0747fe999fcda4f5e241b2898624dca97a1f2177" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" [[package]] name = "windows_x86_64_msvc" -version = "0.52.4" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32b752e52a2da0ddfbdbcc6fceadfeede4c939ed16d13e648833a61dfb611ed8" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] name = "winnow" diff --git a/examples/layout.rs b/examples/layout.rs index 432bb2a..556ed4f 100644 --- a/examples/layout.rs +++ b/examples/layout.rs @@ -22,25 +22,25 @@ fn main() { dbg!(&p); - let mut g = GeneticAlgorithm::new(&p, 20, 2, 0, &mut rng); + // let mut g = GeneticAlgorithm::new(&p, 20, 2, 0, &mut rng); - for i in 0..100 { - println!("Generatrion {i}"); - g.generation(&mut rng); - } - - g.output_population(); - - // let mut m: Option = None; - // for _ in 0..20 { - // let g = genetic_algorithm2(&p, 10, 320, &mut rng); - - // g.print_visualization(); - // g.png_visualization("test.png"); - // if m.as_ref().is_none_or(|m| g.score() < m.score()) { - // m = Some(g); - // } + // for i in 0..100 { + // println!("Generatrion {i}"); + // g.generation(&mut rng); // } - // m.unwrap().print_visualization(); + // g.output_population(); + + let mut m: Option = None; + for _ in 0..1 { + let g = genetic_algorithm2(&p, 10, 320, &mut rng); + + g.print_visualization(); + g.png_visualization("test.png"); + if m.as_ref().is_none_or(|m| g.score() < m.score()) { + m = Some(g); + } + } + + m.unwrap().print_visualization(); } diff --git a/src/belt_finding/conflict_avoidance.rs b/src/belt_finding/conflict_avoidance.rs index 29315d4..24413ed 100644 --- a/src/belt_finding/conflict_avoidance.rs +++ b/src/belt_finding/conflict_avoidance.rs @@ -255,8 +255,7 @@ impl ConflictAvoidance { let mut b = b.build(); - // b.print(); - // self.print(); + // self.print_visualization(); let mut min_cost = f64::INFINITY; let mut solutions = Vec::new(); @@ -271,6 +270,8 @@ impl ConflictAvoidance { } } + // b.print_visualization(); + if b.solution_count() == 0 { return None; } @@ -566,6 +567,17 @@ impl Visualize for ConflictAvoidance { } } + if let Some((xrange, yrange)) = self.range.clone() { + for x in xrange { + for y in yrange.clone() { + v.overwrite_background( + Position::new(x, y), + Some(crate::common::visualize::Color::new(150, 150, 0)), + ); + } + } + } + for x in 0..self.map.width { for y in 0..self.map.height { if conflicts.get(x, y) > &1 { diff --git a/src/belt_finding/mod.rs b/src/belt_finding/mod.rs index 7d3a8be..ab621f7 100644 --- a/src/belt_finding/mod.rs +++ b/src/belt_finding/mod.rs @@ -38,41 +38,37 @@ impl Problem { pub fn from_layout(l: &Layout) -> Self { let mut p = Self::new(l.problem.size.x as usize, l.problem.size.y as usize); - for ((pos, dir), b) in l.blocks.iter().zip(l.problem.blocks.iter()) { - let (npos, nsize) = Layout::normalize_pos((b, *pos, *dir)); - - let nend = npos + nsize - Position::new(1, 1); + for b in &l.blocks { + let aabb = b.get_aabb(); p.set_blocked_range( - npos.x as usize, - npos.y as usize, - nend.x as usize, - nend.y as usize, + aabb.min().x as usize, + aabb.min().y as usize, + aabb.max().x as usize, + aabb.max().y as usize, true, ); } for c in &l.problem.connections { - let startpos = Layout::transform( - l.blocks[c.startblock].0, - l.blocks[c.startblock].1, - l.problem.blocks[c.startblock].output[c.startpoint].offset, + let start_transform = l.blocks[c.startblock].block_to_world(); + let startpos = l.problem.blocks[c.startblock].output[c.startpoint] + .offset + .transform(start_transform); + let startdir = l.problem.blocks[c.startblock].output[c.startpoint] + .dir + .transform(start_transform); + let end_transform = l.blocks[c.endblock].block_to_world(); + let endpos = l.problem.blocks[c.endblock].input[c.endpoint] + .offset + .transform(end_transform); + let enddir = l.problem.blocks[c.endblock].input[c.endpoint] + .dir + .transform(end_transform); + p.add_connection( + (startpos, startdir), + (endpos.in_direction(&enddir, -1), enddir), ); - let startdir = Layout::rotate( - l.problem.blocks[c.startblock].output[c.startpoint].dir, - l.blocks[c.startblock].1, - ); - let enddir = Layout::rotate( - l.problem.blocks[c.endblock].input[c.endpoint].dir, - l.blocks[c.endblock].1, - ); - let endpos = Layout::transform( - l.blocks[c.endblock].0, - l.blocks[c.endblock].1, - l.problem.blocks[c.endblock].input[c.endpoint].offset, - ) - .in_direction(&enddir, -1); - p.add_connection((startpos, startdir), (endpos, enddir)); } p diff --git a/src/bin/beltfinding.rs b/src/bin/beltfinding.rs index de00fd4..b0b26ef 100644 --- a/src/bin/beltfinding.rs +++ b/src/bin/beltfinding.rs @@ -60,6 +60,7 @@ fn main() { Mode::ConflictAvoidance => { p.print_visualization(); p.find_path(); + p.find_path(); p.print_visualization(); let mut c = ConflictAvoidance::new(&p); c.print_visualization(); diff --git a/src/bin/create_beltfinding_image.rs b/src/bin/create_beltfinding_image.rs new file mode 100644 index 0000000..a037e3b --- /dev/null +++ b/src/bin/create_beltfinding_image.rs @@ -0,0 +1,32 @@ +use clap::Parser; +use factorio_blueprint::{ + belt_finding::{conflict_avoidance::ConflictAvoidance, Problem}, + common::visualize::{image_grid, Visualize}, +}; +use std::{fs::File, path::PathBuf}; + +#[derive(Parser)] +struct Args { + inputfiles: Vec, + outputfile: PathBuf, +} + +fn main() { + let args = Args::parse(); + + let visualizations: Vec<_> = args + .inputfiles + .iter() + .map(|f| { + let f = File::open(f).unwrap(); + let mut p = serde_json::from_reader::(f).unwrap(); + + let c = ConflictAvoidance::new(&p); + c.visualize() + }) + .collect(); + + let image = image_grid(&visualizations, 50, 50, 5); + + image.save(args.outputfile).unwrap(); +} diff --git a/src/common/aabb.rs b/src/common/aabb.rs index dcf6d1a..ab8c950 100644 --- a/src/common/aabb.rs +++ b/src/common/aabb.rs @@ -38,6 +38,7 @@ impl AABB { (self.max - self.min) + Position::new(1, 1) } + // Returns true in case the aabb overlap pub fn collision(self, other: Self) -> bool { self.min.x <= other.max.x && self.max.x >= other.min.x diff --git a/src/common/block.rs b/src/common/block.rs index 2cf014a..26baf1f 100644 --- a/src/common/block.rs +++ b/src/common/block.rs @@ -8,14 +8,21 @@ pub struct Block { } impl Block { - pub fn new(size: Position) -> Self { - Self { - pos: Position::new(0, 0), - dir: Direction::Up, - size, - } + pub fn new(pos: Position, dir: Direction, size: Position) -> Self { + Self { pos, dir, size } } + pub fn pos(&self) -> Position { + self.pos + } + + pub fn dir(&self) -> Direction { + self.dir + } + + pub fn size(&self) -> Position { + self.size + } pub fn get_aabb(&self) -> AABB { let npos = match self.dir { Direction::Up => self.pos, @@ -33,4 +40,32 @@ impl Block { AABB::new(npos, npos + nsize - Position::new(1, 1)) } + + pub fn world_to_block(&self) -> Transformation { + todo!() + } + + pub fn block_to_world(&self) -> Transformation { + Transformation::new(self.dir, self.pos) + } +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn transformations() { + let b = Block::new(Position::new(10, 5), Direction::Right, Position::new(4, 3)); + + let t = b.block_to_world(); + + dbg!(t); + + let p = Position::new(3, 2); + + dbg!(p.transform(t)); + + assert!(false); + } } diff --git a/src/common/transformation.rs b/src/common/transformation.rs index cd74aa9..6ce8221 100644 --- a/src/common/transformation.rs +++ b/src/common/transformation.rs @@ -1,5 +1,9 @@ use crate::prelude::*; +pub trait Transformable { + fn transform(&self, t: Transformation) -> Self; +} + #[derive(Clone, Copy, Debug)] pub struct Transformation { rot: Direction, @@ -11,9 +15,9 @@ impl Transformation { Self { rot, pos } } - pub fn transform_position(&self, pos: Position) -> Position { + pub fn transform_position(&self, pos: &Position) -> Position { (match self.rot { - Direction::Up => pos, + Direction::Up => *pos, Direction::Right => Position::new(-pos.y, pos.x), Direction::Down => Position::new(-pos.x, -pos.y), Direction::Left => Position::new(pos.y, -pos.x), @@ -25,6 +29,37 @@ impl Transformation { } } +impl Transformable for Position { + fn transform(&self, t: Transformation) -> Self { + (match t.rot { + Direction::Up => *self, + Direction::Right => Position::new(-self.y, self.x), + Direction::Down => Position::new(-self.x, -self.y), + Direction::Left => Position::new(self.y, -self.x), + }) + t.pos + } +} + +impl Transformable for Direction { + fn transform(&self, t: Transformation) -> Self { + match (t.rot, self) { + (Direction::Up, _) => Direction::Up, + (Direction::Right, Direction::Up) => Direction::Right, + (Direction::Right, Direction::Right) => Direction::Down, + (Direction::Right, Direction::Down) => Direction::Left, + (Direction::Right, Direction::Left) => Direction::Up, + (Direction::Down, Direction::Up) => Direction::Down, + (Direction::Down, Direction::Right) => Direction::Left, + (Direction::Down, Direction::Down) => Direction::Up, + (Direction::Down, Direction::Left) => Direction::Right, + (Direction::Left, Direction::Up) => Direction::Left, + (Direction::Left, Direction::Right) => Direction::Up, + (Direction::Left, Direction::Down) => Direction::Right, + (Direction::Left, Direction::Left) => Direction::Down, + } + } +} + #[cfg(test)] mod test { use crate::prelude::*; @@ -34,9 +69,9 @@ mod test { let p = Position::new(3, 5); let t = Transformation::new(Direction::Up, Position::new(-3, -5)); - assert_eq!(t.transform_position(p), Position::new(0, 0)); + assert_eq!(t.transform_position(&p), Position::new(0, 0)); let t = Transformation::new(Direction::Down, Position::new(-3, -5)); - assert_eq!(t.transform_position(p), Position::new(-6, -10)); + assert_eq!(t.transform_position(&p), Position::new(-6, -10)); } } diff --git a/src/layout/mod.rs b/src/layout/mod.rs index 98c22e1..0470653 100644 --- a/src/layout/mod.rs +++ b/src/layout/mod.rs @@ -1,12 +1,11 @@ -use std::sync::atomic::AtomicU32; -use std::time::Instant; - use crate::belt_finding::common::PathField; use crate::belt_finding::conflict_avoidance::ConflictAvoidance; use crate::common::visualize::{image_grid, Color, Symbol, Visualization, Visualize}; use crate::prelude::*; use rand::{seq::SliceRandom, Rng}; use serde::{Deserialize, Serialize}; +use std::sync::atomic::AtomicU32; +use std::time::Instant; static OUTFILEINDEX: AtomicU32 = AtomicU32::new(0); @@ -152,7 +151,7 @@ pub fn valid_path_layout<'a, R: Rng + ?Sized>( } #[derive(Debug, Serialize, Deserialize)] -pub(crate) struct Block { +pub(crate) struct MacroBlock { pub(crate) size: Position, pub(crate) input: Vec, pub(crate) output: Vec, @@ -175,7 +174,7 @@ pub(crate) struct Connection { #[derive(Debug, Serialize, Deserialize)] pub struct Problem { pub(crate) size: Position, - pub(crate) blocks: Vec, + pub(crate) blocks: Vec, pub(crate) connections: Vec, } @@ -185,7 +184,7 @@ pub struct Problem { #[derive(Debug, Clone)] pub struct Layout<'a> { pub(crate) problem: &'a Problem, - pub(crate) blocks: Vec<(Position, Direction)>, + pub(crate) blocks: Vec, } pub struct PathLayout<'a> { @@ -331,7 +330,7 @@ impl Layout<'_> { fn place_block( problem: &'_ Problem, - blocks: &mut Vec<(Position, Direction)>, + blocks: &mut Vec, rng: &'_ mut R, ) -> bool { if problem.blocks.len() == blocks.len() { @@ -361,12 +360,15 @@ impl Layout<'_> { ), }; + let current = Block::new(pos, dir, problem.blocks[blocks.len()].size); + + let current_aabb = current.get_aabb(); + if blocks .iter() - .enumerate() - .all(|(i, (p, d))| !Self::collision((&problem.blocks[i], *p, *d), (b, pos, dir))) + .all(|b| !AABB::collision(b.get_aabb(), current_aabb)) { - blocks.push((pos, dir)); + blocks.push(current); if Self::place_block(problem, blocks, rng) { return true; @@ -427,10 +429,16 @@ impl Layout<'_> { ), }; - if layout.blocks.iter().enumerate().all(|(j, (p, d))| { - j == i || !Self::collision((&layout.problem.blocks[j], *p, *d), (b, pos, dir)) - }) { - layout.blocks[i] = (pos, dir); + let current = Block::new(pos, dir, b.size); + let current_aabb = current.get_aabb(); + + if layout + .blocks + .iter() + .enumerate() + .all(|(j, b)| j == i || !AABB::collision(b.get_aabb(), current_aabb)) + { + layout.blocks[i] = current; true } else { false @@ -442,13 +450,15 @@ impl Layout<'_> { let b = &mut layout.blocks[i]; let block = &layout.problem.blocks[i]; - b.0 = match &b.1 { - Direction::Up => b.0 + block.size - Position::new(1, 1), - Direction::Right => b.0 + Position::new(1 - block.size.y, block.size.x - 1), - Direction::Down => b.0 - block.size + Position::new(1, 1), - Direction::Left => b.0 + Position::new(block.size.y - 1, 1 - block.size.x), + let new_pos = match b.dir() { + Direction::Up => b.pos() + block.size - Position::new(1, 1), + Direction::Right => b.pos() + Position::new(1 - block.size.y, block.size.x - 1), + Direction::Down => b.pos() - block.size + Position::new(1, 1), + Direction::Left => b.pos() + Position::new(block.size.y - 1, 1 - block.size.x), }; - b.1 = b.1.reverse(); + let new_dir = b.dir().reverse(); + + *b = Block::new(new_pos, new_dir, b.size()); true } @@ -456,125 +466,44 @@ impl Layout<'_> { fn mutate_jiggle(layout: &mut Layout, rng: &mut R) -> bool { let i = rng.gen_range(0..layout.blocks.len()); let dir = rng.gen::(); - // let step = [(1, 10), (2, 5), (3, 5)] - // .choose_weighted(rng, |i| i.1) - // .unwrap() - // .0; - let step = 1; + let step = [(1, 10), (2, 5), (3, 5)] + .choose_weighted(rng, |i| i.1) + .unwrap() + .0; + // let step = 1; - let b = &layout.problem.blocks[i]; + let b = &layout.blocks[i]; - let new_pos = layout.blocks[i].0.in_direction(&dir, step); + let current = Block::new(b.pos().in_direction(&dir, step), b.dir(), b.size()); + let current_aabb = current.get_aabb(); - let (npos, nsize) = Self::normalize_pos((b, new_pos, layout.blocks[i].1)); - - if npos.x < 0 - || npos.y < 0 - || npos.x + nsize.x > layout.problem.size.x - || npos.y + nsize.y > layout.problem.size.y + if current_aabb.min().x < 0 + || current_aabb.min().y < 0 + || current_aabb.max().x >= layout.problem.size.x + || current_aabb.max().y >= layout.problem.size.y { return false; } - if layout.blocks.iter().enumerate().all(|(j, (p, d))| { - j == i - || !Self::collision( - (&layout.problem.blocks[j], *p, *d), - (b, new_pos, layout.blocks[i].1), - ) - }) { - layout.blocks[i].0 = new_pos; + if layout + .blocks + .iter() + .enumerate() + .all(|(j, b)| j == i || !AABB::collision(b.get_aabb(), current_aabb)) + { + layout.blocks[i] = current; true } else { false } } - - fn collision( - block1: (&Block, Position, Direction), - block2: (&Block, Position, Direction), - ) -> bool { - let (npos1, nsize1) = Self::normalize_pos(block1); - let (npos2, nsize2) = Self::normalize_pos(block2); - - npos1.x < npos2.x + nsize2.x - && npos1.x + nsize1.x > npos2.x - && npos1.y < npos2.y + nsize2.y - && npos1.y + nsize1.y > npos2.y - } - - pub(crate) fn normalize_pos(block: (&Block, Position, Direction)) -> (Position, Position) { - let npos = match block.2 { - Direction::Up => block.1, - Direction::Right => block.1.in_direction(&Direction::Left, block.0.size.y - 1), - Direction::Down => block.1 - (block.0.size - Position::new(1, 1)), - Direction::Left => block.1.in_direction(&Direction::Up, block.0.size.x - 1), - }; - let nsize = match block.2 { - Direction::Up | Direction::Down => block.0.size, - Direction::Right | Direction::Left => Position { - x: block.0.size.y, - y: block.0.size.x, - }, - }; - - (npos, nsize) - } - - pub fn score(&self) -> i32 { - let mut sum = 0; - - for c in &self.problem.connections { - let startpos = Self::transform( - self.blocks[c.startblock].0, - self.blocks[c.startblock].1, - self.problem.blocks[c.startblock].output[c.startpoint].offset, - ); - let endpos = Self::transform( - self.blocks[c.endblock].0, - self.blocks[c.endblock].1, - self.problem.blocks[c.endblock].input[c.endpoint].offset, - ); - - sum += (startpos.x - endpos.x).abs() + (startpos.y - endpos.y).abs(); - } - - sum - } - - pub(crate) fn transform(pos: Position, dir: Direction, offset: Position) -> Position { - match dir { - Direction::Up => pos + offset, - Direction::Right => pos + Position::new(-offset.y, offset.x), - Direction::Down => pos - offset, - Direction::Left => pos + Position::new(offset.y, -offset.x), - } - } - - pub(crate) fn rotate(dir: Direction, rot: Direction) -> Direction { - match (rot, dir) { - (Direction::Up, _) => dir, - (Direction::Right, Direction::Up) => Direction::Right, - (Direction::Right, Direction::Right) => Direction::Down, - (Direction::Right, Direction::Down) => Direction::Left, - (Direction::Right, Direction::Left) => Direction::Up, - (Direction::Down, Direction::Up) => Direction::Down, - (Direction::Down, Direction::Right) => Direction::Left, - (Direction::Down, Direction::Down) => Direction::Up, - (Direction::Down, Direction::Left) => Direction::Right, - (Direction::Left, Direction::Up) => Direction::Left, - (Direction::Left, Direction::Right) => Direction::Up, - (Direction::Left, Direction::Down) => Direction::Right, - (Direction::Left, Direction::Left) => Direction::Down, - } - } } impl<'a> Visualize for Layout<'a> { fn visualize(&self) -> Visualization { let mut v = Visualization::new(self.problem.size); - for (i, ((p, d), b)) in self + for (i, (b, mb)) in self .blocks .iter() .zip(self.problem.blocks.iter()) @@ -582,28 +511,34 @@ impl<'a> Visualize for Layout<'a> { { let c = Color::index(i); - let (npos, nsize) = Self::normalize_pos((b, *p, *d)); + let aabb = b.get_aabb(); - for x in npos.x..(npos.x + nsize.x) { - for y in npos.y..(npos.y + nsize.y) { + for x in aabb.min().x..=aabb.max().x { + for y in aabb.min().y..=aabb.max().y { v.add_symbol(Position::new(x, y), Symbol::Block, Some(c), None); } } - let pos = Self::transform(*p, *d, Position::new(0, 0)); + v.add_symbol(b.pos(), Symbol::Char('X'), Some(c), None); - v.add_symbol(pos, Symbol::Char('X'), Some(c), None); + let transform = b.block_to_world(); - for input in &b.input { - let pos = Self::transform(*p, *d, input.offset); - - v.add_symbol(pos, Symbol::Char('i'), Some(c), None); + for input in &mb.input { + v.add_symbol( + input.offset.transform(transform), + Symbol::Char('i'), + Some(c), + None, + ); } - for output in &b.output { - let pos = Self::transform(*p, *d, output.offset); - - v.add_symbol(pos, Symbol::Char('o'), Some(c), None); + for output in &mb.output { + v.add_symbol( + output.offset.transform(transform), + Symbol::Char('o'), + Some(c), + None, + ); } } diff --git a/src/lib.rs b/src/lib.rs index 642ca7b..0442acc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,6 +12,6 @@ pub mod prelude { block::Block, direction::Direction, position::{Position, PositionType}, - transformation::Transformation, + transformation::{Transformable, Transformation}, }; }