test_math.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. /*************************************************************************/
  2. /* test_math.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "test_math.h"
  31. #include "core/math/camera_matrix.h"
  32. #include "core/math/delaunay_3d.h"
  33. #include "core/math/geometry_2d.h"
  34. #include "core/os/main_loop.h"
  35. #include "core/os/os.h"
  36. namespace TestMath {
  37. class GetClassAndNamespace {
  38. String code;
  39. int idx;
  40. int line;
  41. String error_str;
  42. bool error;
  43. Variant value;
  44. String class_name;
  45. enum Token {
  46. TK_BRACKET_OPEN,
  47. TK_BRACKET_CLOSE,
  48. TK_CURLY_BRACKET_OPEN,
  49. TK_CURLY_BRACKET_CLOSE,
  50. TK_PERIOD,
  51. TK_COLON,
  52. TK_COMMA,
  53. TK_SYMBOL,
  54. TK_IDENTIFIER,
  55. TK_STRING,
  56. TK_NUMBER,
  57. TK_EOF,
  58. TK_ERROR
  59. };
  60. Token get_token() {
  61. while (true) {
  62. switch (code[idx]) {
  63. case '\n': {
  64. line++;
  65. idx++;
  66. break;
  67. };
  68. case 0: {
  69. return TK_EOF;
  70. } break;
  71. case '{': {
  72. idx++;
  73. return TK_CURLY_BRACKET_OPEN;
  74. };
  75. case '}': {
  76. idx++;
  77. return TK_CURLY_BRACKET_CLOSE;
  78. };
  79. case '[': {
  80. idx++;
  81. return TK_BRACKET_OPEN;
  82. };
  83. case ']': {
  84. idx++;
  85. return TK_BRACKET_CLOSE;
  86. };
  87. case ':': {
  88. idx++;
  89. return TK_COLON;
  90. };
  91. case ',': {
  92. idx++;
  93. return TK_COMMA;
  94. };
  95. case '.': {
  96. idx++;
  97. return TK_PERIOD;
  98. };
  99. case '#': {
  100. //compiler directive
  101. while (code[idx] != '\n' && code[idx] != 0) {
  102. idx++;
  103. }
  104. continue;
  105. } break;
  106. case '/': {
  107. switch (code[idx + 1]) {
  108. case '*': { // block comment
  109. idx += 2;
  110. while (true) {
  111. if (code[idx] == 0) {
  112. error_str = "Unterminated comment";
  113. error = true;
  114. return TK_ERROR;
  115. } else if (code[idx] == '*' && code[idx + 1] == '/') {
  116. idx += 2;
  117. break;
  118. } else if (code[idx] == '\n') {
  119. line++;
  120. }
  121. idx++;
  122. }
  123. } break;
  124. case '/': { // line comment skip
  125. while (code[idx] != '\n' && code[idx] != 0) {
  126. idx++;
  127. }
  128. } break;
  129. default: {
  130. value = "/";
  131. idx++;
  132. return TK_SYMBOL;
  133. }
  134. }
  135. continue; // a comment
  136. } break;
  137. case '\'':
  138. case '"': {
  139. char32_t begin_str = code[idx];
  140. idx++;
  141. String tk_string = String();
  142. while (true) {
  143. if (code[idx] == 0) {
  144. error_str = "Unterminated String";
  145. error = true;
  146. return TK_ERROR;
  147. } else if (code[idx] == begin_str) {
  148. idx++;
  149. break;
  150. } else if (code[idx] == '\\') {
  151. //escaped characters...
  152. idx++;
  153. char32_t next = code[idx];
  154. if (next == 0) {
  155. error_str = "Unterminated String";
  156. error = true;
  157. return TK_ERROR;
  158. }
  159. char32_t res = 0;
  160. switch (next) {
  161. case 'b':
  162. res = 8;
  163. break;
  164. case 't':
  165. res = 9;
  166. break;
  167. case 'n':
  168. res = 10;
  169. break;
  170. case 'f':
  171. res = 12;
  172. break;
  173. case 'r':
  174. res = 13;
  175. break;
  176. case '\"':
  177. res = '\"';
  178. break;
  179. case '\\':
  180. res = '\\';
  181. break;
  182. default: {
  183. res = next;
  184. } break;
  185. }
  186. tk_string += res;
  187. } else {
  188. if (code[idx] == '\n') {
  189. line++;
  190. }
  191. tk_string += code[idx];
  192. }
  193. idx++;
  194. }
  195. value = tk_string;
  196. return TK_STRING;
  197. } break;
  198. default: {
  199. if (code[idx] <= 32) {
  200. idx++;
  201. break;
  202. }
  203. if ((code[idx] >= 33 && code[idx] <= 47) || (code[idx] >= 58 && code[idx] <= 64) || (code[idx] >= 91 && code[idx] <= 96) || (code[idx] >= 123 && code[idx] <= 127)) {
  204. value = String::chr(code[idx]);
  205. idx++;
  206. return TK_SYMBOL;
  207. }
  208. if (code[idx] == '-' || is_digit(code[idx])) {
  209. //a number
  210. const char32_t *rptr;
  211. double number = String::to_float(&code[idx], &rptr);
  212. idx += (rptr - &code[idx]);
  213. value = number;
  214. return TK_NUMBER;
  215. } else if (is_ascii_char(code[idx]) || code[idx] > 127) {
  216. String id;
  217. while (is_ascii_char(code[idx]) || code[idx] > 127) {
  218. id += code[idx];
  219. idx++;
  220. }
  221. value = id;
  222. return TK_IDENTIFIER;
  223. } else {
  224. error_str = "Unexpected character.";
  225. error = true;
  226. return TK_ERROR;
  227. }
  228. }
  229. }
  230. }
  231. }
  232. public:
  233. Error parse(const String &p_code, const String &p_known_class_name = String()) {
  234. code = p_code;
  235. idx = 0;
  236. line = 0;
  237. error_str = String();
  238. error = false;
  239. value = Variant();
  240. class_name = String();
  241. bool use_next_class = false;
  242. Token tk = get_token();
  243. Map<int, String> namespace_stack;
  244. int curly_stack = 0;
  245. while (!error || tk != TK_EOF) {
  246. if (tk == TK_BRACKET_OPEN) {
  247. tk = get_token();
  248. if (tk == TK_IDENTIFIER && String(value) == "ScriptClass") {
  249. if (get_token() == TK_BRACKET_CLOSE) {
  250. use_next_class = true;
  251. }
  252. }
  253. } else if (tk == TK_IDENTIFIER && String(value) == "class") {
  254. tk = get_token();
  255. if (tk == TK_IDENTIFIER) {
  256. String name = value;
  257. if (use_next_class || p_known_class_name == name) {
  258. for (const KeyValue<int, String> &E : namespace_stack) {
  259. class_name += E.value + ".";
  260. }
  261. class_name += String(value);
  262. break;
  263. }
  264. }
  265. } else if (tk == TK_IDENTIFIER && String(value) == "namespace") {
  266. String name;
  267. int at_level = curly_stack;
  268. while (true) {
  269. tk = get_token();
  270. if (tk == TK_IDENTIFIER) {
  271. name += String(value);
  272. }
  273. tk = get_token();
  274. if (tk == TK_PERIOD) {
  275. name += ".";
  276. } else if (tk == TK_CURLY_BRACKET_OPEN) {
  277. curly_stack++;
  278. break;
  279. } else {
  280. break; //whatever else
  281. }
  282. }
  283. if (!name.is_empty()) {
  284. namespace_stack[at_level] = name;
  285. }
  286. } else if (tk == TK_CURLY_BRACKET_OPEN) {
  287. curly_stack++;
  288. } else if (tk == TK_CURLY_BRACKET_CLOSE) {
  289. curly_stack--;
  290. if (namespace_stack.has(curly_stack)) {
  291. namespace_stack.erase(curly_stack);
  292. }
  293. }
  294. tk = get_token();
  295. }
  296. if (error) {
  297. return ERR_PARSE_ERROR;
  298. }
  299. return OK;
  300. }
  301. String get_error() {
  302. return error_str;
  303. }
  304. String get_class() {
  305. return class_name;
  306. }
  307. };
  308. void test_vec(Plane p_vec) {
  309. CameraMatrix cm;
  310. cm.set_perspective(45, 1, 0, 100);
  311. Plane v0 = cm.xform4(p_vec);
  312. print_line("out: " + v0);
  313. v0.normal.z = (v0.d / 100.0 * 2.0 - 1.0) * v0.d;
  314. print_line("out_F: " + v0);
  315. }
  316. uint32_t ihash(uint32_t a) {
  317. a = (a + 0x7ed55d16) + (a << 12);
  318. a = (a ^ 0xc761c23c) ^ (a >> 19);
  319. a = (a + 0x165667b1) + (a << 5);
  320. a = (a + 0xd3a2646c) ^ (a << 9);
  321. a = (a + 0xfd7046c5) + (a << 3);
  322. a = (a ^ 0xb55a4f09) ^ (a >> 16);
  323. return a;
  324. }
  325. uint32_t ihash2(uint32_t a) {
  326. a = (a ^ 61) ^ (a >> 16);
  327. a = a + (a << 3);
  328. a = a ^ (a >> 4);
  329. a = a * 0x27d4eb2d;
  330. a = a ^ (a >> 15);
  331. return a;
  332. }
  333. uint32_t ihash3(uint32_t a) {
  334. a = (a + 0x479ab41d) + (a << 8);
  335. a = (a ^ 0xe4aa10ce) ^ (a >> 5);
  336. a = (a + 0x9942f0a6) - (a << 14);
  337. a = (a ^ 0x5aedd67d) ^ (a >> 3);
  338. a = (a + 0x17bea992) + (a << 7);
  339. return a;
  340. }
  341. MainLoop *test() {
  342. {
  343. Vector<Vector3> points;
  344. points.push_back(Vector3(0, 0, 0));
  345. points.push_back(Vector3(0, 0, 1));
  346. points.push_back(Vector3(0, 1, 0));
  347. points.push_back(Vector3(0, 1, 1));
  348. points.push_back(Vector3(1, 1, 0));
  349. points.push_back(Vector3(1, 0, 0));
  350. points.push_back(Vector3(1, 0, 1));
  351. points.push_back(Vector3(1, 1, 1));
  352. for (int i = 0; i < 800; i++) {
  353. points.push_back(Vector3(Math::randf() * 2.0 - 1.0, Math::randf() * 2.0 - 1.0, Math::randf() * 2.0 - 1.0) * Vector3(25, 30, 33));
  354. }
  355. Vector<Delaunay3D::OutputSimplex> os = Delaunay3D::tetrahedralize(points);
  356. print_line("simplices in the end: " + itos(os.size()));
  357. for (int i = 0; i < os.size(); i++) {
  358. print_line("Simplex " + itos(i) + ": ");
  359. print_line(points[os[i].points[0]]);
  360. print_line(points[os[i].points[1]]);
  361. print_line(points[os[i].points[2]]);
  362. print_line(points[os[i].points[3]]);
  363. }
  364. {
  365. FileAccessRef f = FileAccess::open("res://bsp.obj", FileAccess::WRITE);
  366. for (int i = 0; i < os.size(); i++) {
  367. f->store_line("o Simplex" + itos(i));
  368. for (int j = 0; j < 4; j++) {
  369. f->store_line(vformat("v %f %f %f", points[os[i].points[j]].x, points[os[i].points[j]].y, points[os[i].points[j]].z));
  370. }
  371. static const int face_order[4][3] = {
  372. { 1, 2, 3 },
  373. { 1, 3, 4 },
  374. { 1, 2, 4 },
  375. { 2, 3, 4 }
  376. };
  377. for (int j = 0; j < 4; j++) {
  378. f->store_line(vformat("f %d %d %d", 4 * i + face_order[j][0], 4 * i + face_order[j][1], 4 * i + face_order[j][2]));
  379. }
  380. }
  381. f->close();
  382. }
  383. return nullptr;
  384. }
  385. {
  386. float r = 1;
  387. float g = 0.5;
  388. float b = 0.1;
  389. const float pow2to9 = 512.0f;
  390. const float B = 15.0f;
  391. const float N = 9.0f;
  392. float sharedexp = 65408.000f;
  393. float cRed = MAX(0.0f, MIN(sharedexp, r));
  394. float cGreen = MAX(0.0f, MIN(sharedexp, g));
  395. float cBlue = MAX(0.0f, MIN(sharedexp, b));
  396. float cMax = MAX(cRed, MAX(cGreen, cBlue));
  397. float expp = MAX(-B - 1.0f, floor(Math::log(cMax) / Math_LN2)) + 1.0f + B;
  398. float sMax = (float)floor((cMax / Math::pow(2.0f, expp - B - N)) + 0.5f);
  399. float exps = expp + 1.0f;
  400. if (0.0 <= sMax && sMax < pow2to9) {
  401. exps = expp;
  402. }
  403. float sRed = Math::floor((cRed / pow(2.0f, exps - B - N)) + 0.5f);
  404. float sGreen = Math::floor((cGreen / pow(2.0f, exps - B - N)) + 0.5f);
  405. float sBlue = Math::floor((cBlue / pow(2.0f, exps - B - N)) + 0.5f);
  406. print_line("R: " + rtos(sRed) + " G: " + rtos(sGreen) + " B: " + rtos(sBlue) + " EXP: " + rtos(exps));
  407. uint32_t rgbe = (Math::fast_ftoi(sRed) & 0x1FF) | ((Math::fast_ftoi(sGreen) & 0x1FF) << 9) | ((Math::fast_ftoi(sBlue) & 0x1FF) << 18) | ((Math::fast_ftoi(exps) & 0x1F) << 27);
  408. float rb = rgbe & 0x1ff;
  409. float gb = (rgbe >> 9) & 0x1ff;
  410. float bb = (rgbe >> 18) & 0x1ff;
  411. float eb = (rgbe >> 27);
  412. float mb = Math::pow(2.0, eb - 15.0 - 9.0);
  413. float rd = rb * mb;
  414. float gd = gb * mb;
  415. float bd = bb * mb;
  416. print_line("RGBE: " + Color(rd, gd, bd));
  417. }
  418. Vector<int> ints;
  419. ints.resize(20);
  420. {
  421. int *w;
  422. w = ints.ptrw();
  423. for (int i = 0; i < ints.size(); i++) {
  424. w[i] = i;
  425. }
  426. }
  427. Vector<int> posho = ints;
  428. {
  429. const int *r = posho.ptr();
  430. for (int i = 0; i < posho.size(); i++) {
  431. print_line(itos(i) + " : " + itos(r[i]));
  432. }
  433. }
  434. List<String> cmdlargs = OS::get_singleton()->get_cmdline_args();
  435. if (cmdlargs.is_empty()) {
  436. //try editor!
  437. return nullptr;
  438. }
  439. String test = cmdlargs.back()->get();
  440. if (test == "math") {
  441. // Not a file name but the test name, abort.
  442. // FIXME: This test is ugly as heck, needs fixing :)
  443. return nullptr;
  444. }
  445. FileAccess *fa = FileAccess::open(test, FileAccess::READ);
  446. ERR_FAIL_COND_V_MSG(!fa, nullptr, "Could not open file: " + test);
  447. Vector<uint8_t> buf;
  448. uint64_t flen = fa->get_length();
  449. buf.resize(fa->get_length() + 1);
  450. fa->get_buffer(buf.ptrw(), flen);
  451. buf.write[flen] = 0;
  452. String code;
  453. code.parse_utf8((const char *)&buf[0]);
  454. GetClassAndNamespace getclass;
  455. if (getclass.parse(code)) {
  456. print_line("Parse error: " + getclass.get_error());
  457. } else {
  458. print_line("Found class: " + getclass.get_class());
  459. }
  460. {
  461. Vector<int> hashes;
  462. List<StringName> tl;
  463. ClassDB::get_class_list(&tl);
  464. for (const StringName &E : tl) {
  465. Vector<uint8_t> m5b = E.operator String().md5_buffer();
  466. hashes.push_back(hashes.size());
  467. }
  468. for (int i = nearest_shift(hashes.size()); i < 20; i++) {
  469. bool success = true;
  470. for (int s = 0; s < 10000; s++) {
  471. Set<uint32_t> existing;
  472. success = true;
  473. for (int j = 0; j < hashes.size(); j++) {
  474. uint32_t eh = ihash2(ihash3(hashes[j] + ihash(s) + s)) & ((1 << i) - 1);
  475. if (existing.has(eh)) {
  476. success = false;
  477. break;
  478. }
  479. existing.insert(eh);
  480. }
  481. if (success) {
  482. print_line("success at " + itos(i) + "/" + itos(nearest_shift(hashes.size())) + " shift " + itos(s));
  483. break;
  484. }
  485. }
  486. if (success) {
  487. break;
  488. }
  489. }
  490. print_line("DONE");
  491. }
  492. {
  493. print_line("NUM: " + itos(-128));
  494. }
  495. {
  496. Vector3 v(1, 2, 3);
  497. v.normalize();
  498. real_t a = 0.3;
  499. Basis m(v, a);
  500. Vector3 v2(7, 3, 1);
  501. v2.normalize();
  502. real_t a2 = 0.8;
  503. Basis m2(v2, a2);
  504. Quaternion q = m;
  505. Quaternion q2 = m2;
  506. Basis m3 = m.inverse() * m2;
  507. Quaternion q3 = (q.inverse() * q2); //.normalized();
  508. print_line(Quaternion(m3));
  509. print_line(q3);
  510. print_line("before v: " + v + " a: " + rtos(a));
  511. q.get_axis_angle(v, a);
  512. print_line("after v: " + v + " a: " + rtos(a));
  513. }
  514. String ret;
  515. List<String> args;
  516. args.push_back("-l");
  517. Error err = OS::get_singleton()->execute("/bin/ls", args, &ret);
  518. print_line("error: " + itos(err));
  519. print_line(ret);
  520. Basis m3;
  521. m3.rotate(Vector3(1, 0, 0), 0.2);
  522. m3.rotate(Vector3(0, 1, 0), 1.77);
  523. m3.rotate(Vector3(0, 0, 1), 212);
  524. Basis m32;
  525. m32.set_euler(m3.get_euler());
  526. print_line("ELEULEEEEEEEEEEEEEEEEEER: " + m3.get_euler() + " vs " + m32.get_euler());
  527. {
  528. Dictionary d;
  529. d["momo"] = 1;
  530. Dictionary b = d;
  531. b["44"] = 4;
  532. }
  533. print_line("inters: " + rtos(Geometry2D::segment_intersects_circle(Vector2(-5, 0), Vector2(-2, 0), Vector2(), 1.0)));
  534. print_line("cross: " + Vector3(1, 2, 3).cross(Vector3(4, 5, 7)));
  535. print_line("dot: " + rtos(Vector3(1, 2, 3).dot(Vector3(4, 5, 7))));
  536. print_line("abs: " + Vector3(-1, 2, -3).abs());
  537. print_line("distance_to: " + rtos(Vector3(1, 2, 3).distance_to(Vector3(4, 5, 7))));
  538. print_line("distance_squared_to: " + rtos(Vector3(1, 2, 3).distance_squared_to(Vector3(4, 5, 7))));
  539. print_line("plus: " + (Vector3(1, 2, 3) + Vector3(Vector3(4, 5, 7))));
  540. print_line("minus: " + (Vector3(1, 2, 3) - Vector3(Vector3(4, 5, 7))));
  541. print_line("mul: " + (Vector3(1, 2, 3) * Vector3(Vector3(4, 5, 7))));
  542. print_line("div: " + (Vector3(1, 2, 3) / Vector3(Vector3(4, 5, 7))));
  543. print_line("mul scalar: " + (Vector3(1, 2, 3) * 2.0));
  544. print_line("premul scalar: " + (2.0 * Vector3(1, 2, 3)));
  545. print_line("div scalar: " + (Vector3(1, 2, 3) / 3.0));
  546. print_line("length: " + rtos(Vector3(1, 2, 3).length()));
  547. print_line("length squared: " + rtos(Vector3(1, 2, 3).length_squared()));
  548. print_line("normalized: " + Vector3(1, 2, 3).normalized());
  549. print_line("inverse: " + Vector3(1, 2, 3).inverse());
  550. {
  551. Vector3 v(4, 5, 7);
  552. v.normalize();
  553. print_line("normalize: " + v);
  554. }
  555. {
  556. Vector3 v(4, 5, 7);
  557. v += Vector3(1, 2, 3);
  558. print_line("+=: " + v);
  559. }
  560. {
  561. Vector3 v(4, 5, 7);
  562. v -= Vector3(1, 2, 3);
  563. print_line("-=: " + v);
  564. }
  565. {
  566. Vector3 v(4, 5, 7);
  567. v *= Vector3(1, 2, 3);
  568. print_line("*=: " + v);
  569. }
  570. {
  571. Vector3 v(4, 5, 7);
  572. v /= Vector3(1, 2, 3);
  573. print_line("/=: " + v);
  574. }
  575. {
  576. Vector3 v(4, 5, 7);
  577. v *= 2.0;
  578. print_line("scalar *=: " + v);
  579. }
  580. {
  581. Vector3 v(4, 5, 7);
  582. v /= 2.0;
  583. print_line("scalar /=: " + v);
  584. }
  585. return nullptr;
  586. }
  587. } // namespace TestMath