diff --git a/simul/.gitignore b/simul/.gitignore new file mode 100644 index 0000000..2f7896d --- /dev/null +++ b/simul/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/simul/Cargo.lock b/simul/Cargo.lock new file mode 100644 index 0000000..017a1b2 --- /dev/null +++ b/simul/Cargo.lock @@ -0,0 +1,66 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "bitflags" +version = "2.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "843867be96c8daad0d758b57df9392b6d8d271134fce549de6ce169ff98a92af" + +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + +[[package]] +name = "libc" +version = "0.2.184" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48f5d2a454e16a5ea0f4ced81bd44e4cfc7bd3a507b61887c99fd3538b28e4af" + +[[package]] +name = "sdl3" +version = "0.17.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dce0693cede7f7901e968e5539f87174d0fb5fac5d489a3e3be52b2b4a0b49c5" +dependencies = [ + "bitflags", + "lazy_static", + "libc", + "sdl3-image-sys", + "sdl3-sys", + "sdl3-ttf-sys", +] + +[[package]] +name = "sdl3-image-sys" +version = "0.6.1+SDL-image-3.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f61a8cc9c72e2cab2e500752f513f65043ff1d36f994b90c7b423abcb796330d" +dependencies = [ + "sdl3-sys", +] + +[[package]] +name = "sdl3-sys" +version = "0.6.1+SDL-3.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d73fc820b0b6b9bd0557de7b799ec9da1ca4f35fcd1cb42f7ceb85e640d1477" + +[[package]] +name = "sdl3-ttf-sys" +version = "0.6.0+SDL-ttf-3.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c30da3dcd7e062f19350b47a532f7629c42801b801687535fd31e8ba2adfd71d" +dependencies = [ + "sdl3-sys", +] + +[[package]] +name = "simul" +version = "0.1.0" +dependencies = [ + "sdl3", +] diff --git a/simul/Cargo.toml b/simul/Cargo.toml new file mode 100644 index 0000000..5666a20 --- /dev/null +++ b/simul/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "simul" +version = "0.1.0" +edition = "2024" + +[dependencies] +sdl3 = { version = "0.17.3", features = ["ttf", "image"] } diff --git a/simul/inputs_moving.csv b/simul/inputs_moving.csv new file mode 100644 index 0000000..f02d05c --- /dev/null +++ b/simul/inputs_moving.csv @@ -0,0 +1,202 @@ +time_delta_us,accel_x,accel_y,accel_z,rotation_x,rotation_y,rotation_z +40027,0.0013,0.0035,-1.0068,0.0266,-0.0666,0.1786 +39993,0.0021,0.0016,-1.0038,-0.0803,-0.1582,-0.1191 +40002,0.0006,0.0021,-0.9960,0.0953,0.1395,-0.0504 +39998,-0.0021,-0.0006,-1.0058,-0.1032,0.0250,0.1710 +40000,-0.0028,0.0038,-0.9975,0.0342,-0.0208,0.0565 +40000,-0.0036,0.0016,-1.0021,-0.0192,-0.0971,-0.0962 +40000,0.0006,0.0018,-1.0038,-0.0040,0.1624,0.1328 +40000,-0.0026,-0.0001,-0.9987,-0.0956,0.0250,-0.0656 +40000,-0.0060,-0.0006,-1.0048,-0.0803,0.1166,-0.1420 +40000,-0.0014,-0.0001,-0.9967,0.0647,0.0632,-0.2260 +40000,-0.0014,0.0023,-1.0002,-0.0574,-0.0208,-0.0962 +40000,-0.0072,-0.0004,-1.0053,0.0800,0.2082,0.0183 +40000,-0.0026,-0.0014,-0.9953,-0.1490,0.0632,0.0260 +40000,0.0018,0.0064,-1.0019,-0.0040,0.0021,0.0947 +40000,-0.0036,0.0038,-1.0068,-0.0421,0.1090,0.0183 +40000,0.0025,0.0016,-1.0021,0.1334,0.0250,-0.1573 +40000,0.0052,0.0008,-1.0075,-0.0574,-0.0437,0.2626 +40000,0.0030,0.0064,-0.9936,-0.0345,0.0632,0.0183 +40000,0.0023,-0.0033,-0.9992,-0.0498,0.0403,0.1252 +40000,0.0025,-0.0043,-0.9914,0.0647,-0.0284,0.1634 +40000,-0.0014,-0.0021,-0.9970,0.0724,-0.0513,0.2321 +40000,0.0016,0.0008,-1.0007,-0.1414,0.0174,0.0107 +40000,0.0003,-0.0021,-0.9975,-0.0421,0.2006,0.0870 +40000,0.0008,-0.0019,-0.9992,-0.0803,0.0556,0.1176 +40000,-0.0045,0.0035,-1.0019,0.1105,0.1395,-0.0351 +40000,0.0003,-0.0023,-0.9989,0.1411,0.0174,0.0565 +40000,0.0001,0.0028,-0.9987,0.0724,-0.1505,-0.1802 +40000,0.0033,-0.0021,-1.0036,0.0266,0.1930,0.1023 +40000,0.0038,0.0008,-0.9997,0.2632,-0.4330,0.0641 +40000,0.0506,0.0018,-0.9723,1.6449,-50.5093,-13.0351 +40000,-0.0165,0.0047,-1.0971,-1.6757,-22.4635,-6.9511 +40000,-0.0099,-0.0607,-1.1467,-1.9505,-87.3337,-25.0046 +40000,-0.1339,-0.0102,-1.3625,-15.0727,7.4220,-5.1344 +40000,-0.1015,0.0746,-1.0260,8.1716,42.2082,-14.8595 +40000,-0.0197,0.0548,-1.1093,-1.8131,23.3762,2.8427 +40000,-0.0207,0.0292,-1.0282,-11.0879,49.5976,5.4763 +40000,-0.0124,0.0729,-0.9242,-1.3627,28.2388,-0.5008 +40000,0.0538,0.0453,-1.0246,-8.7292,14.4678,6.3542 +40000,0.0260,0.0030,-1.0094,-13.2940,31.6205,7.6748 +40000,0.0511,0.0279,-0.9621,-14.0879,14.4296,8.6595 +40000,0.0533,0.0069,-0.9987,7.7823,6.0785,9.6290 +40000,0.0399,0.0296,-0.8827,0.0876,8.6663,7.2779 +40000,0.0335,-0.0019,-0.8967,6.1182,-4.0818,4.8046 +40000,0.0169,0.0062,-0.8508,4.5304,-10.9902,0.3160 +40000,-0.0026,-0.0194,-0.8945,16.9044,-20.2421,0.9420 +40000,-0.0294,0.0487,-1.0195,-4.1872,-16.0208,-2.4092 +40000,-0.0807,0.0868,-1.0053,38.2861,-3.3337,-2.4092 +40000,-0.0358,-0.0204,-0.9194,4.2632,-19.5322,4.0260 +40000,0.0016,0.0284,-0.8576,-26.2024,0.3151,-13.6382 +40000,0.0228,0.0125,-1.0732,12.5380,12.4983,-5.4244 +40000,0.0558,-0.0111,-0.9826,34.8892,108.4831,38.9649 +40000,0.1119,0.0770,-1.1088,5.8510,124.6510,23.8656 +40000,0.3009,-0.0807,-0.9504,-14.6605,88.9869,24.7740 +40000,0.3265,-0.0128,-0.9577,8.7823,185.8571,73.5450 +40000,0.2774,-0.0180,-0.8342,-5.4467,168.5441,32.6672 +40000,0.4522,-0.0368,-0.8549,3.3014,215.2998,34.0794 +40000,0.6808,-0.0636,-0.7858,2.6907,124.4144,28.3618 +40000,0.6158,-0.0946,-0.7475,-12.0650,-55.9597,-23.1649 +40000,0.5628,-0.0148,-0.8654,-36.6528,-128.6086,-60.9206 +40000,0.5758,0.0035,-0.8957,2.6907,-141.8528,-42.7145 +40000,0.4034,0.0150,-0.9931,26.1869,-170.8681,-68.4626 +40000,0.4059,-0.0148,-0.8495,-9.6070,-160.1887,-47.1420 +40000,0.2604,0.0262,-1.0668,-4.7368,-139.3261,-32.8748 +40000,0.0936,0.0472,-1.0195,10.9197,-134.0895,-37.0046 +40000,0.0736,0.0033,-0.9955,11.4617,-137.9444,-34.0428 +40000,-0.0587,0.0074,-0.9960,13.9350,-130.5780,-28.4244 +40000,-0.1320,-0.0104,-1.0160,17.0113,-132.9979,-21.0809 +40000,-0.1264,-0.0229,-1.0241,20.2403,-140.0284,-11.3710 +40000,-0.3998,-0.0189,-0.9291,28.3548,-143.2803,-5.2412 +40000,-0.4491,-0.0834,-0.8935,28.5304,-75.5017,-19.8137 +40000,-0.4696,-0.1056,-0.8300,24.8892,-26.7612,-4.5771 +40000,-0.3866,-0.1662,-0.7995,16.0647,62.8800,28.3924 +40000,-0.2577,-0.1278,-0.8696,3.5915,185.0479,71.0412 +40000,-0.2492,-0.0719,-0.9184,31.5304,184.3533,75.5985 +40000,-0.2084,-0.1276,-0.7682,-10.5765,194.5060,50.3847 +40000,0.3248,-0.0622,-1.2599,17.6220,249.0021,69.4840 +40000,0.0782,-0.1164,-0.9130,18.7212,170.7426,37.2473 +40000,0.2545,-0.1032,-0.8903,-10.7673,243.8037,39.4534 +40000,0.5404,-0.0822,-0.9626,13.5380,219.1930,23.3084 +40000,0.6239,-0.0814,-0.8417,-28.2711,121.3762,19.2092 +40000,0.7325,-0.1137,-0.8869,6.0342,100.1166,21.4458 +40000,0.5870,-0.0917,-0.8246,18.7899,-14.4788,-8.5771 +40000,0.6219,-0.0827,-0.7807,0.5533,-116.4406,-44.3863 +40000,0.4671,-0.0692,-0.9194,15.1029,-203.8757,-72.9130 +40000,0.4164,-0.0941,-0.8449,27.5914,-209.9139,-91.2260 +40000,0.3453,-0.1056,-0.8674,-1.9811,-191.9597,-72.4779 +40000,0.1217,-0.0512,-0.9880,-11.5002,-168.3337,-45.4779 +40000,-0.0006,-0.0087,-1.0900,15.4464,-160.4025,-59.6992 +40000,0.0057,-0.0453,-1.0212,7.0800,-187.5017,-64.1115 +40000,-0.2084,-0.0563,-1.0414,15.9808,-130.6162,-44.8824 +40000,-0.2899,-0.0954,-0.9833,24.2937,-111.1734,-38.0122 +40000,-0.3231,-0.1710,-0.8544,13.7289,-33.3872,-17.9130 +40003,-0.1605,-0.2396,-0.8207,11.9044,74.3609,35.5679 +39997,-0.1205,-0.2047,-0.7714,-6.1108,202.8189,105.3542 +40000,-0.0517,-0.0783,-0.9357,-6.0727,249.0021,94.6672 +40000,0.1671,-0.0814,-0.9797,-28.2788,203.4220,75.2397 +40000,0.2320,-0.0453,-0.9850,-23.9963,210.0937,66.2626 +40000,0.4010,-0.0414,-1.0802,12.3777,180.9945,71.0565 +40000,0.3792,-0.1222,-0.8449,23.1411,166.5212,46.9878 +40000,0.5477,-0.1261,-0.8427,-0.3627,161.3304,40.6290 +40000,0.7640,-0.1950,-0.7968,0.5838,82.1777,33.9267 +40000,0.5892,-0.1308,-0.8822,11.0342,-34.5475,-2.6458 +40000,0.6087,-0.1413,-0.8302,26.5838,-127.0895,-33.0198 +40000,0.5277,-0.1483,-0.8288,6.6602,-218.2421,-69.3328 +40000,0.3971,-0.1608,-0.8107,-15.9124,-167.7002,-50.7756 +40000,0.2660,-0.1039,-0.9906,-17.5154,-146.7841,-48.8748 +40000,0.1747,-0.0990,-0.9745,-7.3017,-155.6849,-50.0046 +40000,0.0389,-0.1007,-1.0478,-6.7063,-146.5093,-55.4397 +40000,0.0089,-0.0980,-0.9967,-16.9124,-171.3490,-53.7985 +40000,-0.1901,-0.0480,-1.0519,-4.9276,-173.4635,-62.5695 +40000,-0.2458,-0.0709,-1.0260,14.1258,-138.9139,-49.8595 +40000,-0.3146,-0.1337,-0.8893,18.1563,-86.2879,-34.8977 +40000,-0.3827,-0.1339,-0.9042,13.6602,-43.9673,-22.9969 +40000,-0.3686,-0.1630,-0.8632,16.4235,-17.0666,-19.2412 +40000,-0.1935,-0.2294,-0.7309,-1.7597,96.9563,42.1863 +40000,-0.2169,-0.1117,-0.9701,-5.4544,208.5746,103.0565 +40000,-0.1786,-0.1605,-0.7995,-19.7979,183.2846,83.6290 +40000,0.0650,-0.0465,-0.9518,-39.5002,242.3609,95.5374 +40000,0.2164,-0.0199,-1.1952,-27.1185,169.1777,74.5221 +40000,0.2459,-0.0514,-0.9538,-14.7902,147.8189,63.6061 +40000,0.4134,-0.0756,-1.0270,4.4311,164.0174,56.1405 +40000,0.3802,-0.1012,-0.8586,-5.7215,73.3304,36.1176 +40000,0.5226,-0.1078,-0.8429,-9.8895,99.0556,41.5908 +40000,0.4972,-0.0888,-0.9225,7.2327,52.0632,28.6443 +40000,0.4291,-0.0963,-0.9196,-10.1185,-78.4864,-23.2870 +40000,0.4977,-0.0941,-0.7626,-32.3169,-93.1505,-18.4550 +40000,0.3338,-0.0399,-0.9530,-27.9124,-114.2727,-17.1649 +40000,0.1883,0.0165,-0.9753,-26.4391,-218.1353,-75.6611 +40000,0.3189,-0.0263,-0.9499,-14.6070,-132.8681,-43.4473 +40000,0.0511,0.0106,-1.0146,-16.8131,-120.5322,-41.8290 +40000,0.0208,-0.0119,-1.0397,-9.2101,-157.6925,-75.5313 +40000,-0.1046,0.0072,-0.9887,-2.7292,-116.3643,-55.9282 +40000,-0.1796,0.0133,-1.0343,-4.5231,-95.3872,-36.3099 +40000,-0.2731,0.0123,-0.9828,14.3701,-111.2879,-63.3099 +40000,-0.2909,-0.0297,-0.8554,16.3243,-59.3566,-28.4092 +40000,-0.3219,-0.0604,-0.8837,12.7899,27.0861,6.1634 +40000,-0.2770,-0.0724,-0.8742,6.2861,89.0327,39.2855 +40000,-0.1437,-0.0275,-1.0827,28.0266,215.3762,72.7893 +40000,-0.0692,-0.0561,-0.9093,48.5609,201.1624,56.8809 +40000,0.0963,-0.0519,-1.0107,8.4388,189.9640,34.7282 +40000,0.1566,-0.0998,-0.9284,16.7976,184.5594,29.8427 +40000,0.3392,-0.0741,-0.9474,-10.5002,139.5975,11.3237 +40000,0.3626,-0.0956,-0.8893,15.6907,139.8571,29.2931 +40000,0.5099,-0.0854,-0.8449,20.7289,88.0479,12.7206 +40000,0.4332,-0.0695,-0.9696,32.3014,-90.7307,-7.2718 +40000,0.3797,-0.1220,-0.8822,13.8968,-139.7612,-27.8519 +40000,0.3651,-0.1691,-0.8547,11.7518,-62.4024,-14.7603 +40000,0.3214,-0.1525,-0.9499,-14.7292,-109.9063,-3.3786 +40000,0.1546,-0.1020,-1.0485,20.5762,-150.8299,-45.9664 +40000,0.2191,-0.1435,-0.8693,-0.8284,-126.8605,-41.9664 +40000,0.0248,-0.1439,-1.0016,-3.7063,-140.8452,-38.0886 +40000,-0.0529,-0.1369,-1.0180,-18.7215,-132.1505,-51.8748 +40000,-0.0800,-0.1681,-1.0041,-11.6834,-104.6391,-47.9817 +40000,-0.1562,-0.1471,-0.9848,-31.4620,-115.2116,-36.4168 +40000,-0.2641,-0.1129,-1.0697,-20.0956,-98.9673,-28.0580 +40000,-0.3078,-0.1049,-0.9443,-7.5078,-49.8376,-26.6229 +40000,-0.3478,-0.0910,-0.9584,-7.0269,-15.8986,-0.1802 +40000,-0.2860,-0.1330,-0.9357,10.7060,61.8953,22.3466 +40000,-0.2128,-0.1061,-1.0053,42.8510,186.2311,77.7359 +40000,-0.2211,-0.0878,-0.9601,-3.9353,124.0098,60.1328 +40000,-0.0492,-0.1361,-1.0278,26.8968,201.2159,55.1557 +40000,0.0677,-0.1098,-0.9975,39.7976,247.2464,56.8046 +40000,0.2589,-0.1466,-0.9784,-0.6528,137.8189,14.6519 +40000,0.3414,-0.1686,-0.8063,-12.8895,203.4678,9.2473 +40000,0.4793,-0.1300,-1.0607,70.5762,249.0021,33.4992 +40000,0.6000,-0.1957,-0.7602,76.6449,173.8113,10.5145 +40000,0.7042,-0.2672,-0.4201,-28.4009,14.7502,-16.2641 +40000,0.6393,-0.1898,-0.8586,-41.9047,-36.0971,-13.9740 +40000,0.6451,-0.1962,-0.7150,-27.7521,-83.2727,-10.1038 +40000,0.5301,-0.1593,-0.7724,-31.6070,-112.5856,-8.4855 +40000,0.5526,-0.1469,-0.9384,-17.6834,-115.0818,-6.7221 +40000,0.2538,-0.0072,-1.0468,-56.4696,-251.2650,-84.4702 +40000,0.2984,-0.1574,-0.8437,-25.8818,-195.5399,-80.7756 +40000,0.1251,-0.0966,-1.1110,-3.4162,-111.7078,-23.8519 +40000,0.0177,-0.0290,-1.0024,16.9426,-138.2269,-25.9817 +40000,-0.0043,-0.0773,-1.0768,10.2708,-163.9979,-24.9435 +40000,-0.1728,-0.1093,-0.8061,25.2937,-59.6849,-5.6992 +40000,-0.1889,-0.1410,-0.9391,7.5227,-74.8147,-4.1878 +40000,-0.1657,-0.1127,-0.8664,5.6525,-12.8910,30.9573 +40000,-0.3175,-0.0709,-1.2196,43.1945,64.5289,54.6901 +40000,-0.2438,-0.1466,-1.1637,181.5227,57.4144,20.7282 +40000,-0.0890,-0.2565,-0.9384,160.8128,57.9182,-1.8290 +40000,-0.1803,-0.3703,-0.9142,130.2937,24.4373,-5.6305 +40000,-0.0670,-0.4188,-0.7580,96.8281,13.8876,-36.0351 +40000,-0.0214,-0.4904,-0.7804,71.0724,20.6892,-13.2031 +40000,-0.0343,-0.5338,-0.7399,11.1258,14.8495,-3.5313 +40000,-0.0985,-0.5421,-0.7265,-43.2177,11.6205,3.3008 +40000,-0.0873,-0.3944,-0.6347,-148.5231,14.7579,19.9573 +40000,-0.1256,-0.2963,-0.9223,-198.7979,20.3609,7.9420 +40000,0.0514,-0.1913,-1.1122,-141.9124,11.9640,11.6748 +40000,-0.1347,-0.1269,-0.9872,-103.5536,-9.0208,-5.8901 +40000,0.0594,-0.1217,-0.8813,-92.2788,37.0861,-0.8443 +40003,-0.0214,0.0216,-1.1484,-81.5154,21.4754,5.0718 +39997,0.0545,0.0365,-1.0751,-59.0421,21.3533,11.8656 +40000,0.0040,0.0721,-1.1127,0.9808,21.5441,6.9496 +40000,0.0406,0.0577,-1.1217,36.9960,22.4449,-2.2412 +40000,0.0694,-0.0387,-1.1364,137.9044,32.2159,-2.3634 +40000,0.1168,-0.1154,-1.0627,194.2098,43.4678,-13.0885 +40000,0.1161,-0.2560,-0.8837,166.0571,42.2006,-3.3863 +40000,0.2018,-0.3476,-0.9196,88.5151,19.3227,2.0870 + diff --git a/simul/inputs_still.csv b/simul/inputs_still.csv new file mode 100644 index 0000000..3df6c09 --- /dev/null +++ b/simul/inputs_still.csv @@ -0,0 +1,202 @@ +time_delta_us,accel_x,accel_y,accel_z,rotation_x,rotation_y,rotation_z +40024,-0.0034,-0.0021,-0.9975,0.0803,-0.1545,-0.3072 +39995,0.0030,0.0010,-1.0006,-0.2174,-0.0324,-0.1392 +40002,0.0005,-0.0073,-1.0006,-0.0418,0.1585,0.1508 +39998,-0.0007,-0.0033,-0.9940,0.0269,0.0058,0.0898 +40000,0.0025,0.0008,-0.9960,-0.1411,0.0516,0.1356 +40000,-0.0014,0.0020,-1.0070,0.0116,0.0287,0.1356 +40000,0.0008,-0.0024,-0.9879,0.0650,0.1661,0.0134 +40000,-0.0004,-0.0002,-0.9970,-0.0953,-0.0095,0.0134 +40000,-0.0051,-0.0055,-1.0036,0.0498,-0.0324,-0.0018 +40000,0.0052,0.0030,-1.0050,0.0879,-0.1545,-0.2232 +40000,0.0032,-0.0033,-0.9967,0.0116,0.0440,0.0592 +40000,-0.0012,0.0052,-1.0060,0.0879,-0.0247,-0.0629 +40000,-0.0009,-0.0046,-0.9923,-0.1258,0.0211,-0.2461 +40000,-0.0002,-0.0031,-0.9994,-0.0724,-0.0095,0.0592 +40000,0.0045,-0.0092,-0.9965,0.1108,-0.1850,0.0058 +40000,0.0025,0.0001,-1.0019,-0.1487,0.0974,0.0592 +40000,-0.0026,-0.0075,-0.9965,0.0727,-0.0324,0.1127 +40000,0.0001,0.0001,-0.9975,-0.0418,0.1203,-0.1698 +40000,0.0042,-0.0051,-1.0033,0.0345,-0.0095,0.0821 +40000,-0.0007,-0.0026,-0.9999,0.0574,0.0134,-0.0782 +40000,0.0088,-0.0002,-0.9992,-0.0113,0.0287,-0.0095 +40000,0.0005,-0.0038,-1.0038,-0.1258,-0.0782,-0.0553 +40000,0.0005,-0.0063,-0.9984,0.2253,-0.0553,-0.3072 +40000,0.0071,-0.0036,-0.9938,0.0345,0.0211,-0.0171 +40000,0.0020,-0.0014,-0.9870,-0.1182,0.1585,-0.0324 +40000,0.0020,-0.0041,-0.9994,-0.0113,-0.2079,-0.1240 +40000,-0.0063,-0.0046,-1.0019,-0.0113,0.1585,0.0821 +40000,-0.0019,-0.0014,-0.9921,-0.0953,-0.2003,0.0745 +40000,0.0081,0.0045,-1.0043,-0.0418,0.1508,-0.2003 +40000,-0.0017,0.0013,-0.9992,-0.1563,-0.0705,-0.1469 +40000,0.0020,-0.0026,-0.9972,-0.1869,0.0745,0.0058 +40000,-0.0026,-0.0024,-0.9982,-0.0495,-0.0095,-0.1545 +40000,0.0042,0.0008,-0.9979,-0.0495,-0.0934,0.0592 +40000,0.0086,0.0010,-0.9977,0.1948,-0.0324,-0.1163 +40000,0.0064,-0.0021,-1.0014,-0.0418,-0.0171,-0.1240 +40000,0.0030,0.0040,-0.9950,-0.0266,-0.0095,0.1279 +40000,-0.0017,-0.0060,-0.9943,0.0040,0.1661,-0.1011 +40000,0.0003,-0.0055,-0.9999,-0.1258,-0.0018,-0.0705 +40000,-0.0002,-0.0046,-0.9982,-0.0037,-0.0247,0.0516 +40000,0.0001,-0.0021,-1.0065,0.1185,-0.1011,0.0440 +40000,-0.0002,0.0030,-1.0023,-0.1182,0.0211,0.0974 +40000,0.0005,-0.0060,-1.0001,-0.0647,0.2272,0.1432 +40000,-0.0007,-0.0041,-1.0004,-0.0113,0.0134,-0.0400 +40000,0.0010,-0.0014,-0.9926,-0.0647,-0.1011,-0.1469 +40000,-0.0017,-0.0009,-0.9933,0.0650,0.1432,-0.0324 +40000,-0.0038,-0.0077,-0.9958,0.1566,-0.0705,-0.0934 +40000,-0.0004,-0.0090,-0.9955,0.1032,-0.0018,-0.1774 +40000,-0.0004,-0.0033,-1.0023,-0.0647,0.0592,-0.0247 +40000,0.0035,-0.0021,-0.9940,0.1566,-0.0934,0.2043 +40000,-0.0007,-0.0060,-0.9887,0.1337,0.0745,-0.0553 +40000,0.0013,0.0010,-0.9926,0.1108,-0.1927,0.0287 +40000,0.0030,-0.0004,-0.9962,0.0269,0.0287,-0.1927 +40000,0.0040,0.0010,-0.9987,-0.0418,0.0745,-0.0705 +40000,0.0018,0.0003,-0.9984,0.0269,0.1203,-0.0934 +40000,-0.0026,-0.0033,-0.9979,0.1261,-0.0171,0.0592 +40000,0.0030,-0.0068,-1.0001,-0.1411,0.0516,0.1356 +40000,0.0071,0.0018,-0.9955,-0.0113,0.0134,-0.1545 +40000,0.0001,-0.0016,-0.9958,0.0116,-0.1011,0.0821 +40000,0.0042,0.0015,-1.0004,0.0498,-0.0171,-0.1469 +40000,0.0032,-0.0063,-1.0094,-0.0953,0.0745,0.1585 +40000,-0.0031,-0.0070,-0.9933,-0.1334,0.0363,0.0898 +40000,0.0010,0.0003,-0.9892,0.0116,0.0058,-0.0553 +40000,0.0015,-0.0026,-0.9965,0.0956,0.0134,-0.1240 +40000,0.0010,-0.0009,-1.0009,0.2406,0.0287,-0.1392 +40000,0.0020,-0.0053,-0.9984,-0.1411,0.0363,-0.2003 +40000,0.0084,-0.0019,-0.9982,-0.0571,-0.1392,-0.2614 +40000,0.0074,-0.0038,-1.0077,-0.0342,0.0669,-0.2079 +40000,-0.0004,-0.0009,-1.0082,-0.1716,0.0058,0.3188 +40000,-0.0036,-0.0033,-0.9965,0.0956,-0.0018,0.0745 +40000,0.0018,-0.0087,-0.9992,-0.0647,-0.2079,-0.3606 +40000,0.0020,0.0047,-0.9997,0.0650,-0.1392,-0.0476 +40000,0.0013,-0.0002,-0.9999,-0.0495,0.1966,0.1661 +40000,-0.0034,-0.0024,-0.9999,0.0345,0.0898,-0.0247 +40000,0.0025,-0.0012,-0.9955,0.0269,-0.1469,-0.1087 +40000,0.0047,-0.0014,-0.9992,-0.0266,0.0287,-0.1163 +40000,-0.0017,-0.0021,-0.9999,0.1414,0.0821,-0.0782 +40000,0.0015,-0.0016,-0.9926,-0.0037,0.0134,0.1279 +40000,0.0027,-0.0051,-1.0019,-0.1792,-0.0324,-0.0934 +40000,-0.0009,-0.0051,-1.0006,-0.1792,-0.0476,0.1050 +40000,0.0010,-0.0038,-1.0014,-0.1258,0.2043,-0.0018 +40000,-0.0021,-0.0004,-0.9999,0.0116,0.0440,-0.0400 +40000,0.0023,-0.0016,-0.9945,-0.1182,-0.0476,0.0898 +40000,0.0001,0.0008,-0.9977,0.0727,0.0440,0.1508 +40000,-0.0024,-0.0024,-1.0006,-0.0953,0.0363,-0.2766 +40000,-0.0017,-0.0016,-1.0041,0.0116,0.0516,-0.1621 +40000,0.0074,0.0018,-0.9933,-0.0800,-0.0247,-0.0476 +40000,0.0052,-0.0048,-0.9928,0.0650,-0.1240,-0.0934 +40000,-0.0043,-0.0033,-0.9958,0.0269,0.0745,0.1585 +40000,0.0042,-0.0060,-0.9992,0.0727,-0.1011,0.0516 +40000,-0.0029,0.0015,-0.9967,-0.0876,-0.0476,0.1203 +40000,0.0023,-0.0031,-1.0067,-0.0495,0.0211,-0.0095 +40000,-0.0002,0.0003,-0.9997,-0.1411,0.2653,0.2119 +40006,0.0037,0.0023,-0.9940,-0.0724,0.2272,0.0363 +39994,0.0008,0.0018,-1.0004,0.1185,0.1127,0.0669 +40000,0.0005,-0.0033,-1.0016,0.0727,0.1890,-0.0171 +40000,-0.0004,0.0010,-1.0009,-0.0647,0.1127,0.0745 +40000,0.0027,-0.0107,-0.9982,0.2101,-0.0324,-0.1469 +40000,0.0015,0.0023,-0.9970,0.0803,0.0058,-0.2079 +40000,0.0040,0.0001,-0.9987,-0.0495,-0.0858,0.0669 +40000,0.0040,0.0025,-0.9970,0.1337,0.0134,-0.0553 +40000,0.0027,-0.0021,-0.9950,0.0345,0.0745,0.2653 +40000,0.0054,-0.0012,-0.9938,0.0421,-0.0247,0.0440 +40000,0.0023,0.0001,-1.0014,-0.0418,-0.0782,-0.0553 +40000,-0.0002,-0.0073,-1.0016,0.0421,0.1432,0.0440 +40000,-0.0024,-0.0021,-0.9938,-0.0189,0.1050,-0.0476 +40000,-0.0007,-0.0009,-0.9928,-0.0571,0.0134,0.2119 +40000,0.0027,0.0003,-1.0023,-0.0953,-0.2843,0.2043 +40000,0.0005,0.0008,-1.0023,-0.0189,-0.0018,-0.1774 +40000,0.0037,0.0001,-1.0031,-0.0342,0.0440,-0.0095 +40000,-0.0024,0.0028,-1.0006,-0.0953,0.0058,0.1203 +40000,0.0015,-0.0004,-0.9936,0.0421,0.0516,-0.0171 +40000,0.0042,-0.0053,-0.9979,0.0040,-0.0629,0.0516 +40000,0.0027,0.0025,-0.9926,0.0116,0.0745,-0.3072 +40000,-0.0021,-0.0065,-0.9960,-0.0418,0.0058,0.1356 +40000,-0.0009,-0.0060,-0.9972,-0.0800,0.1127,-0.0400 +40000,0.0005,-0.0065,-0.9950,0.0116,-0.0858,-0.1163 +40000,-0.0012,0.0020,-0.9958,0.0269,0.1356,0.1356 +40000,0.0013,-0.0016,-0.9943,0.0421,-0.1240,-0.0247 +40000,0.0062,-0.0085,-1.0016,-0.1411,0.2501,-0.0476 +40000,0.0005,-0.0004,-0.9933,0.0116,0.0898,0.0516 +40000,0.0018,-0.0009,-0.9977,0.0574,-0.2461,-0.1163 +40000,0.0040,0.0025,-0.9977,0.1643,0.0516,0.0974 +40000,-0.0014,-0.0024,-1.0023,0.1108,0.1585,-0.0171 +40000,-0.0021,-0.0007,-1.0014,-0.1029,-0.0476,-0.1698 +40000,0.0018,-0.0007,-0.9933,-0.2708,0.0440,-0.0705 +40000,0.0032,0.0015,-0.9945,-0.0495,0.0898,-0.0782 +40000,-0.0017,-0.0016,-0.9972,-0.0266,-0.0171,0.0974 +40000,0.0023,-0.0019,-0.9955,0.0574,0.1050,0.0745 +40000,-0.0046,0.0025,-0.9962,0.2024,-0.1545,0.1661 +40000,-0.0026,-0.0007,-0.9994,0.0574,0.0592,0.0669 +40000,0.0037,-0.0070,-0.9953,0.0574,-0.1392,-0.0934 +40000,0.0032,-0.0048,-1.0006,-0.0876,0.0211,-0.0476 +40000,0.0027,-0.0077,-0.9931,-0.1792,0.0516,-0.1927 +40000,0.0025,0.0010,-0.9906,-0.1182,0.0669,0.0287 +40000,0.0010,-0.0046,-0.9938,0.0421,0.1127,0.0287 +40000,0.0032,-0.0004,-0.9999,0.0192,-0.0705,-0.2537 +40000,-0.0009,0.0047,-0.9901,-0.1029,0.1966,0.0745 +40000,0.0064,-0.0033,-0.9911,-0.0953,0.1356,0.0134 +40000,0.0008,-0.0048,-0.9962,-0.0189,0.0745,0.0134 +40000,0.0010,-0.0090,-0.9982,0.0727,0.0669,-0.1698 +40000,0.0010,-0.0002,-1.0019,0.0727,-0.0782,0.0058 +40000,0.0059,-0.0048,-1.0011,-0.0647,-0.0247,0.1127 +40000,0.0032,-0.0073,-0.9975,0.0421,-0.0782,0.2195 +40000,0.0045,-0.0012,-0.9945,0.0727,0.1432,-0.1469 +40000,-0.0002,-0.0082,-0.9918,-0.0953,-0.0018,0.2577 +40000,0.0008,-0.0043,-0.9953,0.0192,-0.1316,0.0821 +40000,-0.0021,-0.0016,-0.9936,0.0345,0.1203,0.3340 +40000,0.0040,-0.0014,-0.9962,-0.0724,0.0516,-0.1087 +40000,0.0013,-0.0065,-0.9999,-0.0647,-0.2308,0.2043 +40000,-0.0034,-0.0041,-0.9992,0.0421,0.0516,0.2806 +40000,-0.0043,-0.0002,-0.9955,-0.1563,0.0287,-0.1545 +40000,0.0020,-0.0031,-0.9926,0.0727,-0.0247,-0.0553 +40000,0.0042,-0.0026,-0.9904,0.0192,0.0974,0.1356 +40000,-0.0002,0.0032,-0.9967,-0.1563,0.0592,0.1203 +40000,0.0005,-0.0055,-0.9997,-0.0037,0.0821,0.0287 +40000,0.0015,0.0015,-0.9955,0.0574,0.0592,0.0440 +40000,0.0040,-0.0031,-0.9982,-0.0418,-0.0171,0.0821 +40000,-0.0007,0.0037,-1.0031,-0.0266,0.1508,-0.0171 +40000,-0.0070,-0.0048,-1.0045,-0.0800,0.0287,0.0592 +40000,0.0030,-0.0014,-0.9943,-0.0495,-0.1316,-0.2919 +40000,0.0025,-0.0007,-1.0067,-0.0495,-0.0247,-0.1545 +40000,-0.0029,0.0064,-0.9923,0.0192,0.0592,0.2195 +40000,-0.0012,-0.0026,-1.0016,-0.2250,-0.0171,-0.1163 +40000,0.0001,-0.0024,-1.0065,0.1261,-0.0171,0.0211 +40000,-0.0034,0.0025,-1.0060,0.0956,-0.0171,0.0592 +40000,0.0032,-0.0043,-0.9979,-0.0037,0.0974,0.0440 +40000,0.0003,0.0018,-1.0023,-0.0418,-0.0247,-0.1240 +40000,0.0027,-0.0012,-1.0006,0.0269,-0.0095,-0.1087 +40000,0.0015,-0.0009,-0.9911,0.0269,-0.0247,0.0058 +40000,-0.0012,-0.0014,-0.9987,0.0269,0.0592,0.2730 +40000,-0.0056,-0.0004,-1.0026,0.0803,-0.0247,-0.0476 +40000,0.0032,0.0076,-0.9965,-0.1945,0.0287,-0.1087 +40000,0.0010,0.0025,-0.9889,-0.1334,0.0287,-0.2079 +40000,0.0008,-0.0014,-0.9933,-0.0953,0.1737,-0.1698 +40000,-0.0004,-0.0031,-0.9931,0.0803,0.0974,-0.0629 +40000,-0.0007,-0.0016,-0.9989,-0.0495,-0.0705,0.0211 +40000,0.0015,-0.0058,-0.9979,0.1261,0.0974,-0.0782 +40000,0.0081,-0.0085,-1.0067,0.1108,-0.0476,0.0592 +40000,-0.0031,0.0042,-0.9994,-0.0113,-0.0018,-0.0476 +40000,0.0008,-0.0048,-0.9970,-0.0266,0.0363,-0.0171 +40000,-0.0014,0.0013,-0.9972,0.0116,-0.0934,0.0440 +40000,0.0005,0.0023,-0.9960,0.0345,-0.0247,-0.1240 +40000,0.0049,0.0003,-0.9948,0.0803,-0.0018,-0.2003 +40000,0.0037,-0.0014,-0.9992,0.1872,-0.0858,-0.2385 +40000,-0.0009,-0.0014,-1.0045,0.1490,-0.0858,-0.1163 +40000,-0.0004,-0.0004,-1.0104,-0.0953,0.1127,-0.0400 +40000,-0.0009,0.0047,-1.0036,-0.0113,-0.0858,0.0058 +40000,-0.0017,-0.0009,-1.0004,-0.0037,0.0440,-0.1850 +40000,0.0047,0.0018,-0.9914,-0.0037,0.0592,-0.0782 +40000,0.0047,0.0047,-0.9936,-0.1563,0.0745,-0.2079 +40000,0.0054,0.0008,-0.9955,0.0574,0.0898,-0.0553 +40000,-0.0009,0.0010,-0.9933,-0.0189,0.1737,-0.1621 +40000,0.0042,-0.0055,-1.0004,0.0574,-0.0247,-0.1240 +40000,0.0008,-0.0016,-0.9943,-0.0647,0.0974,0.1356 +40000,-0.0012,-0.0058,-0.9931,0.0956,0.1050,-0.2079 +40000,0.0008,0.0032,-1.0036,0.0345,0.1356,-0.0018 +40000,0.0013,-0.0029,-0.9989,-0.0647,0.0821,0.0516 +40000,0.0013,-0.0031,-0.9921,-0.0189,0.0898,-0.0171 +40000,0.0013,-0.0021,-0.9938,0.0192,0.0363,-0.1698 +40000,-0.0007,0.0020,-1.0045,-0.0495,0.1127,-0.0934 + diff --git a/simul/src/m3x3.rs b/simul/src/m3x3.rs new file mode 100644 index 0000000..2737220 --- /dev/null +++ b/simul/src/m3x3.rs @@ -0,0 +1,48 @@ +use std::ops::Mul; + +use crate::v3::V3; + +pub struct M3x3([f64; 9]); + +impl M3x3 { + #[rustfmt::skip] + pub fn new_rotate_x(angle: f64) -> Self { + Self([ + 1.0, 0.0, 0.0, + 0.0, f64::cos(angle), -f64::sin(angle), + 0.0, f64::sin(angle), f64::cos(angle), + ]) + } + + #[rustfmt::skip] + pub fn new_rotate_y(angle: f64) -> Self { + Self([ + f64::cos(angle), 0.0, f64::sin(angle), + 0.0, 1.0, 0.0, + -f64::sin(angle), 0.0, f64::cos(angle), + ]) + } + + #[rustfmt::skip] + pub fn new_rotate_z(angle: f64) -> Self { + Self([ + f64::cos(angle), -f64::sin(angle), 0.0, + f64::sin(angle), f64::cos(angle), 0.0, + 0.0, 0.0, 1.0, + ]) + } +} + +impl Mul for M3x3 { + type Output = V3; + + fn mul(self, rhs: V3) -> Self::Output { + let V3(x, y, z) = rhs; + let [xx, xy, xz, yx, yy, yz, zx, zy, zz] = self.0; + V3( + x * xx + y * xy + z * xz, + x * yx + y * yy + z * yz, + x * zx + y * zy + z * zz, + ) + } +} diff --git a/simul/src/main.rs b/simul/src/main.rs new file mode 100644 index 0000000..f0378fc --- /dev/null +++ b/simul/src/main.rs @@ -0,0 +1,47 @@ +#![allow(unused)] + +use crate::{ + scene::Scene, + tri2::Tri2, + tri3::Tri3, + v2::V2, + v3::V3, + window::{Color, Event, Keycode, Renderer, Window}, +}; + +mod m3x3; +mod scene; +mod tri2; +mod tri3; +mod v2; +mod v3; +mod window; + +pub struct App {} + +impl window::App for App { + fn update(&mut self, delta_time: std::time::Duration) {} + + fn render(&self, r: &mut R) { + r.draw_line(V2(0.0, 0.0), V2(0.6, 0.2), Color::RED); + + let mut scene = Scene::new(); + + scene.draw_triangle( + Tri3(V3(0.0, 0.0, 0.0), V3(0.3, 0.3, 0.0), V3(0.3, 0.0, 0.0)), + Color::GREEN, + Color::BLACK, + ); + + scene.render(r, V3(0.0, 0.0, -1.0)); + } + + fn event(&mut self, event: Event) {} +} + +fn main() { + let mut window = Window::new(); + let mut app = App {}; + + window.run(&mut app); +} diff --git a/simul/src/scene.rs b/simul/src/scene.rs new file mode 100644 index 0000000..a425405 --- /dev/null +++ b/simul/src/scene.rs @@ -0,0 +1,59 @@ +use crate::{ + tri2::Tri2, + tri3::Tri3, + v3::V3, + window::{Color, Renderer}, +}; + +pub struct Scene { + tris: Vec<(Tri3, Color, Color)>, +} + +impl Scene { + pub fn new() -> Self { + Self { tris: Vec::new() } + } + + pub fn render(&self, r: &mut impl Renderer, camera_pos: V3) { + let mut indices_with_scores = self + .tris + .iter() + .enumerate() + .map(|(i, (tri, ..))| { + let mut point_scores = tri.points().map(|p| (camera_pos - p).len()); + point_scores.sort_by(|a, b| a.total_cmp(b)); + + (i, point_scores[0]) + }) + .rev() + .collect::>(); + + indices_with_scores.sort_by(|a, b| b.1.total_cmp(&a.1)); + + for (i, _) in indices_with_scores { + let (tri3, outline_color, fill_color) = &self.tris[i]; + + // if !(tri3.0.2 >= -1.0 && tri3.1.2 >= -1.0 && tri3.2.2 >= -1.0) { + // continue; + // } + + // if tri3.normal().dot(tri3.0 - camera_pos) >= 0.0 { + // continue; + // } + // + println!("{tri3:?}"); + + let tri2 = tri3.project_2d(camera_pos); + + println!("{tri2:?}"); + + r.draw_triangles(&[tri2], *fill_color); + r.draw_line(tri2.0, tri2.1, *outline_color); + r.draw_line(tri2.0, tri2.2, *outline_color); + } + } + + pub fn draw_triangle(&mut self, tri: Tri3, outline_color: Color, fill_color: Color) { + self.tris.push((tri, outline_color, fill_color)); + } +} diff --git a/simul/src/tri2.rs b/simul/src/tri2.rs new file mode 100644 index 0000000..2ae4925 --- /dev/null +++ b/simul/src/tri2.rs @@ -0,0 +1,4 @@ +use crate::v2::V2; + +#[derive(Clone, Copy, Debug)] +pub struct Tri2(pub V2, pub V2, pub V2); diff --git a/simul/src/tri3.rs b/simul/src/tri3.rs new file mode 100644 index 0000000..cc159fd --- /dev/null +++ b/simul/src/tri3.rs @@ -0,0 +1,46 @@ +use crate::{tri2::Tri2, v3::V3}; + +#[derive(Clone, Copy, Debug)] +pub struct Tri3(pub V3, pub V3, pub V3); + +impl Tri3 { + pub fn init(v: V3) -> Self { + Self(v, v, v) + } + + pub fn map V3>(&self, func: F) -> Self { + Self(func(self.0), func(self.1), func(self.2)) + } + + pub fn zip V3>(&self, rhs: Tri3, func: F) -> Self { + Self( + func(self.0, rhs.0), + func(self.1, rhs.1), + func(self.2, rhs.2), + ) + } + + pub fn rotate(&self, rot: V3) -> Self { + self.map(|v| v.rotate(rot)) + } + + pub fn project_2d(&self, camera_pos: V3) -> Tri2 { + Tri2( + self.0.project_2d(camera_pos), + self.1.project_2d(camera_pos), + self.2.project_2d(camera_pos), + ) + } + + pub fn translate(&self, offset: V3) -> Self { + self.map(|v| v + offset) + } + + pub fn points(&self) -> [V3; 3] { + [self.0, self.1, self.2] + } + + pub fn normal(&self) -> V3 { + (self.1 - self.0).cross(self.2 - self.1) + } +} diff --git a/simul/src/v2.rs b/simul/src/v2.rs new file mode 100644 index 0000000..57ff770 --- /dev/null +++ b/simul/src/v2.rs @@ -0,0 +1,81 @@ +use std::ops::{Add, Div, Mul, Neg, Sub}; + +#[derive(Clone, Copy, Debug)] +pub struct V2(pub f64, pub f64); + +impl V2 { + pub fn init(v: f64) -> Self { + Self(v, v) + } + + pub fn map f64>(&self, func: F) -> Self { + Self(func(self.0), func(self.1)) + } + + pub fn zip f64>(&self, rhs: Self, func: F) -> Self { + Self(func(self.0, rhs.0), func(self.1, rhs.1)) + } + + pub fn reduce f64>(&self, initial: f64, func: F) -> f64 { + let acc = initial; + let acc = func(acc, self.0); + let acc = func(acc, self.1); + acc + } + + pub fn normal(&self) -> V2 { + V2(-self.1, self.0) + } + + pub fn translate(&self, trans: V2) -> V2 { + *self + trans + } + + pub fn len(&self) -> f64 { + self.map(|v| v.powi(2)).reduce(0.0, |acc, v| acc + v) + } + + pub fn unit(&self) -> V2 { + *self / self.len() + } +} + +impl Add for V2 { + type Output = V2; + + fn add(self, rhs: V2) -> Self::Output { + self.zip(rhs, |a, b| a + b) + } +} + +impl Sub for V2 { + type Output = V2; + + fn sub(self, rhs: V2) -> Self::Output { + self.zip(rhs, |a, b| a - b) + } +} + +impl Mul for V2 { + type Output = V2; + + fn mul(self, rhs: f64) -> Self::Output { + self.map(|v| v * rhs) + } +} + +impl Div for V2 { + type Output = V2; + + fn div(self, rhs: f64) -> Self::Output { + self.map(|v| v / rhs) + } +} + +impl Neg for V2 { + type Output = V2; + + fn neg(self) -> Self::Output { + V2::init(0.0) - self + } +} diff --git a/simul/src/v3.rs b/simul/src/v3.rs new file mode 100644 index 0000000..d46ed89 --- /dev/null +++ b/simul/src/v3.rs @@ -0,0 +1,106 @@ +use std::ops::{Add, Div, Mul, Neg, Sub}; + +use crate::{m3x3::M3x3, v2::V2}; + +#[derive(Clone, Copy, Debug)] +pub struct V3(pub f64, pub f64, pub f64); + +impl V3 { + pub fn init(v: f64) -> Self { + Self(v, v, v) + } + + pub fn map f64>(&self, func: F) -> Self { + Self(func(self.0), func(self.1), func(self.2)) + } + + pub fn zip f64>(&self, rhs: V3, func: F) -> Self { + Self( + func(self.0, rhs.0), + func(self.1, rhs.1), + func(self.2, rhs.2), + ) + } + + pub fn reduce f64>(&self, initial: f64, func: F) -> f64 { + let acc = initial; + let acc = func(acc, self.0); + let acc = func(acc, self.1); + let acc = func(acc, self.0); + acc + } + + pub fn cross(&self, rhs: Self) -> Self { + let V3(ax, ay, az) = self; + let V3(bx, by, bz) = rhs; + V3(ay * bz - az * by, az * bx - ax * bz, ax * by - ay * bx) + } + + pub fn rotate(&self, rot: Self) -> Self { + M3x3::new_rotate_z(rot.2) + * (M3x3::new_rotate_y(rot.1) * (M3x3::new_rotate_x(rot.0) * *self)) + } + + pub fn project_2d(&self, camera_pos: V3) -> V2 { + let c = camera_pos; + let d = *self - c; + let e = V3(0.0, 0.0, 0.0) - c; + V2(e.2 / d.2 * d.0 + e.0, e.2 / d.2 * d.1 + e.1) + } + + pub fn dot(&self, rhs: Self) -> f64 { + self.zip(rhs, |a, b| a * b).reduce(0.0, |acc, v| acc + v) + } + + pub fn translate(&self, trans: V3) -> V3 { + *self + trans + } + + pub fn len(&self) -> f64 { + self.map(|v| v.powi(2)).reduce(0.0, |acc, v| acc + v) + } + + pub fn unit(&self) -> V3 { + *self / self.len() + } +} + +impl Add for V3 { + type Output = V3; + + fn add(self, rhs: V3) -> Self::Output { + self.zip(rhs, |a, b| a + b) + } +} + +impl Sub for V3 { + type Output = V3; + + fn sub(self, rhs: V3) -> Self::Output { + self.zip(rhs, |a, b| a - b) + } +} + +impl Mul for V3 { + type Output = V3; + + fn mul(self, rhs: f64) -> Self::Output { + self.map(|v| v * rhs) + } +} + +impl Div for V3 { + type Output = V3; + + fn div(self, rhs: f64) -> Self::Output { + self.map(|v| v / rhs) + } +} + +impl Neg for V3 { + type Output = V3; + + fn neg(self) -> Self::Output { + V3::init(0.0) - self + } +} diff --git a/simul/src/window.rs b/simul/src/window.rs new file mode 100644 index 0000000..b69f7fc --- /dev/null +++ b/simul/src/window.rs @@ -0,0 +1,164 @@ +use std::time::{Duration, Instant}; + +use sdl3::{ + Sdl, + event::Event as SdlEvent, + render::{Canvas, FPoint, FRect, Vertex, VertexIndices}, + video::Window as SdlWindow, +}; + +pub use sdl3::{keyboard::Keycode, pixels::Color}; + +use crate::{tri2::Tri2, v2::V2}; + +pub static WIDTH: f64 = 1280.0; +pub static HEIGHT: f64 = 720.0; + +pub trait App { + fn update(&mut self, delta_time: Duration); + fn render(&self, r: &mut R); + fn event(&mut self, event: Event); +} + +pub trait Renderer { + fn draw_rect(&mut self, pos: V2, size: V2, color: Color); + fn draw_line(&mut self, from: V2, to: V2, color: Color); + fn draw_triangles(&mut self, tris: &[Tri2], color: Color); +} + +pub enum Event { + KeyDown(Keycode), + KeyUp(Keycode), +} + +pub struct Window { + sdl_context: Sdl, + canvas: Canvas, +} + +impl Window { + pub fn new() -> Self { + let sdl_context = sdl3::init().unwrap(); + let video_subsystem = sdl_context.video().unwrap(); + let window = video_subsystem + .window("Game", WIDTH as u32, HEIGHT as u32) + .position_centered() + .build() + .unwrap(); + let mut canvas = window.into_canvas(); + + canvas.set_draw_color(Color::BLACK); + canvas.clear(); + canvas.present(); + + Self { + sdl_context, + canvas, + } + } + + pub fn run(&mut self, app: &mut impl App) { + let mut event_pump = self.sdl_context.event_pump().unwrap(); + + let mut time_before = Instant::now(); + let time_per_frame = Duration::from_secs_f64(1.0 / 60.0); + let mut print_fps_timer = Instant::now(); + let mut fps_count = 0; + + 'running: loop { + for event in event_pump.poll_iter() { + match event { + SdlEvent::Quit { .. } + | SdlEvent::KeyDown { + keycode: Some(Keycode::Escape), + .. + } => break 'running, + SdlEvent::KeyDown { + keycode: Some(key), .. + } => app.event(Event::KeyDown(key)), + SdlEvent::KeyUp { + keycode: Some(key), .. + } => app.event(Event::KeyUp(key)), + _ => {} + } + } + + let time_now = Instant::now(); + let delta_time = time_now - time_before; + time_before = time_now; + app.update(delta_time); + + self.canvas.set_draw_color(Color::RGB(20, 20, 20)); + self.canvas.clear(); + app.render(self); + self.canvas.present(); + + fps_count += 1; + if (time_now - print_fps_timer).as_secs_f64() > 1.0 { + println!("fps: {fps_count}"); + fps_count = 0; + print_fps_timer = time_now; + } + if delta_time < time_per_frame { + std::thread::sleep(time_per_frame - delta_time); + } + } + } + + fn scale_w2s(&self, v: V2) -> V2 { + let factor = WIDTH / 2.0; + V2(v.0 * factor, v.1 * factor) + } + fn translate_w2s(&self, v: V2) -> V2 { + let aspect_ratio_h2w = HEIGHT / WIDTH; + let middle = V2(1.0, 1.0 * aspect_ratio_h2w); + V2(v.0 + middle.0, middle.1 - v.1) + } + fn point_w2s(&self, v: V2) -> V2 { + self.scale_w2s(self.translate_w2s(v)) + } +} + +impl Renderer for Window { + fn draw_rect(&mut self, pos: V2, size: V2, color: Color) { + let size = self.scale_w2s(size); + let pos = self.point_w2s(pos).translate(V2(0.0, -size.0)); + self.canvas.set_draw_color(color); + self.canvas + .fill_rect(FRect::new(pos.0 as _, pos.1 as _, size.0 as _, size.1 as _)) + .unwrap(); + } + fn draw_line(&mut self, from: V2, to: V2, color: Color) { + let n = (to - from).normal().unit() * 0.0005; + + let p0 = from - n; + let p1 = to - n; + let p2 = from + n; + let p3 = to + n; + + self.draw_triangles(&[Tri2(p0, p1, p2), Tri2(p3, p2, p1)], color); + } + + fn draw_triangles(&mut self, tris: &[Tri2], color: Color) { + let vertices = tris + .iter() + .flat_map(|t| [t.0, t.1, t.2]) + .map(|p| self.point_w2s(p)) + .map(|p| Vertex { + position: p.into(), + color: color.into(), + tex_coord: FPoint::new(0.0, 0.0), + }) + .collect::>(); + + self.canvas + .render_geometry(&vertices, None, VertexIndices::Sequential) + .unwrap(); + } +} + +impl From for FPoint { + fn from(value: V2) -> Self { + FPoint::new(value.0 as _, value.1 as _) + } +}