test_math.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. /**************************************************************************/
  2. /* test_math.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/basis.h"
  32. #include "core/math/camera_matrix.h"
  33. #include "core/math/math_funcs.h"
  34. #include "core/math/transform.h"
  35. #include "core/os/file_access.h"
  36. #include "core/os/keyboard.h"
  37. #include "core/os/os.h"
  38. #include "core/print_string.h"
  39. #include "core/ustring.h"
  40. #include "core/variant.h"
  41. #include "core/vmap.h"
  42. #include "scene/main/node.h"
  43. #include "scene/resources/texture.h"
  44. #include "servers/visual/shader_language.h"
  45. #include "core/method_ptrcall.h"
  46. namespace TestMath {
  47. class GetClassAndNamespace {
  48. String code;
  49. int idx;
  50. int line;
  51. String error_str;
  52. bool error;
  53. Variant value;
  54. String class_name;
  55. enum Token {
  56. TK_BRACKET_OPEN,
  57. TK_BRACKET_CLOSE,
  58. TK_CURLY_BRACKET_OPEN,
  59. TK_CURLY_BRACKET_CLOSE,
  60. TK_PERIOD,
  61. TK_COLON,
  62. TK_COMMA,
  63. TK_SYMBOL,
  64. TK_IDENTIFIER,
  65. TK_STRING,
  66. TK_NUMBER,
  67. TK_EOF,
  68. TK_ERROR
  69. };
  70. Token get_token() {
  71. while (true) {
  72. switch (code[idx]) {
  73. case '\n': {
  74. line++;
  75. idx++;
  76. break;
  77. };
  78. case 0: {
  79. return TK_EOF;
  80. } break;
  81. case '{': {
  82. idx++;
  83. return TK_CURLY_BRACKET_OPEN;
  84. };
  85. case '}': {
  86. idx++;
  87. return TK_CURLY_BRACKET_CLOSE;
  88. };
  89. case '[': {
  90. idx++;
  91. return TK_BRACKET_OPEN;
  92. };
  93. case ']': {
  94. idx++;
  95. return TK_BRACKET_CLOSE;
  96. };
  97. case ':': {
  98. idx++;
  99. return TK_COLON;
  100. };
  101. case ',': {
  102. idx++;
  103. return TK_COMMA;
  104. };
  105. case '.': {
  106. idx++;
  107. return TK_PERIOD;
  108. };
  109. case '#': {
  110. //compiler directive
  111. while (code[idx] != '\n' && code[idx] != 0) {
  112. idx++;
  113. }
  114. continue;
  115. } break;
  116. case '/': {
  117. switch (code[idx + 1]) {
  118. case '*': { // block comment
  119. idx += 2;
  120. while (true) {
  121. if (code[idx] == 0) {
  122. error_str = "Unterminated comment";
  123. error = true;
  124. return TK_ERROR;
  125. } else if (code[idx] == '*' && code[idx + 1] == '/') {
  126. idx += 2;
  127. break;
  128. } else if (code[idx] == '\n') {
  129. line++;
  130. }
  131. idx++;
  132. }
  133. } break;
  134. case '/': { // line comment skip
  135. while (code[idx] != '\n' && code[idx] != 0) {
  136. idx++;
  137. }
  138. } break;
  139. default: {
  140. value = "/";
  141. idx++;
  142. return TK_SYMBOL;
  143. }
  144. }
  145. continue; // a comment
  146. } break;
  147. case '\'':
  148. case '"': {
  149. CharType begin_str = code[idx];
  150. idx++;
  151. String tk_string = String();
  152. while (true) {
  153. if (code[idx] == 0) {
  154. error_str = "Unterminated String";
  155. error = true;
  156. return TK_ERROR;
  157. } else if (code[idx] == begin_str) {
  158. idx++;
  159. break;
  160. } else if (code[idx] == '\\') {
  161. //escaped characters...
  162. idx++;
  163. CharType next = code[idx];
  164. if (next == 0) {
  165. error_str = "Unterminated String";
  166. error = true;
  167. return TK_ERROR;
  168. }
  169. CharType res = 0;
  170. switch (next) {
  171. case 'b':
  172. res = 8;
  173. break;
  174. case 't':
  175. res = 9;
  176. break;
  177. case 'n':
  178. res = 10;
  179. break;
  180. case 'f':
  181. res = 12;
  182. break;
  183. case 'r':
  184. res = 13;
  185. break;
  186. case '\"':
  187. res = '\"';
  188. break;
  189. case '\\':
  190. res = '\\';
  191. break;
  192. default: {
  193. res = next;
  194. } break;
  195. }
  196. tk_string += res;
  197. } else {
  198. if (code[idx] == '\n') {
  199. line++;
  200. }
  201. tk_string += code[idx];
  202. }
  203. idx++;
  204. }
  205. value = tk_string;
  206. return TK_STRING;
  207. } break;
  208. default: {
  209. if (code[idx] <= 32) {
  210. idx++;
  211. break;
  212. }
  213. 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)) {
  214. value = String::chr(code[idx]);
  215. idx++;
  216. return TK_SYMBOL;
  217. }
  218. if (code[idx] == '-' || (code[idx] >= '0' && code[idx] <= '9')) {
  219. //a number
  220. const CharType *rptr;
  221. double number = String::to_double(&code[idx], &rptr);
  222. idx += (rptr - &code[idx]);
  223. value = number;
  224. return TK_NUMBER;
  225. } else if ((code[idx] >= 'A' && code[idx] <= 'Z') || (code[idx] >= 'a' && code[idx] <= 'z') || code[idx] > 127) {
  226. String id;
  227. while ((code[idx] >= 'A' && code[idx] <= 'Z') || (code[idx] >= 'a' && code[idx] <= 'z') || code[idx] > 127) {
  228. id += code[idx];
  229. idx++;
  230. }
  231. value = id;
  232. return TK_IDENTIFIER;
  233. } else {
  234. error_str = "Unexpected character.";
  235. error = true;
  236. return TK_ERROR;
  237. }
  238. }
  239. }
  240. }
  241. }
  242. public:
  243. Error parse(const String &p_code, const String &p_known_class_name = String()) {
  244. code = p_code;
  245. idx = 0;
  246. line = 0;
  247. error_str = String();
  248. error = false;
  249. value = Variant();
  250. class_name = String();
  251. bool use_next_class = false;
  252. Token tk = get_token();
  253. Map<int, String> namespace_stack;
  254. int curly_stack = 0;
  255. while (!error || tk != TK_EOF) {
  256. if (tk == TK_BRACKET_OPEN) {
  257. tk = get_token();
  258. if (tk == TK_IDENTIFIER && String(value) == "ScriptClass") {
  259. if (get_token() == TK_BRACKET_CLOSE) {
  260. use_next_class = true;
  261. }
  262. }
  263. } else if (tk == TK_IDENTIFIER && String(value) == "class") {
  264. tk = get_token();
  265. if (tk == TK_IDENTIFIER) {
  266. String name = value;
  267. if (use_next_class || p_known_class_name == name) {
  268. for (Map<int, String>::Element *E = namespace_stack.front(); E; E = E->next()) {
  269. class_name += E->get() + ".";
  270. }
  271. class_name += String(value);
  272. break;
  273. }
  274. }
  275. } else if (tk == TK_IDENTIFIER && String(value) == "namespace") {
  276. String name;
  277. int at_level = curly_stack;
  278. while (true) {
  279. tk = get_token();
  280. if (tk == TK_IDENTIFIER) {
  281. name += String(value);
  282. }
  283. tk = get_token();
  284. if (tk == TK_PERIOD) {
  285. name += ".";
  286. } else if (tk == TK_CURLY_BRACKET_OPEN) {
  287. curly_stack++;
  288. break;
  289. } else {
  290. break; //whatever else
  291. }
  292. }
  293. if (name != String()) {
  294. namespace_stack[at_level] = name;
  295. }
  296. } else if (tk == TK_CURLY_BRACKET_OPEN) {
  297. curly_stack++;
  298. } else if (tk == TK_CURLY_BRACKET_CLOSE) {
  299. curly_stack--;
  300. if (namespace_stack.has(curly_stack)) {
  301. namespace_stack.erase(curly_stack);
  302. }
  303. }
  304. tk = get_token();
  305. }
  306. if (error) {
  307. return ERR_PARSE_ERROR;
  308. }
  309. return OK;
  310. }
  311. String get_error() {
  312. return error_str;
  313. }
  314. String get_class() {
  315. return class_name;
  316. }
  317. };
  318. void test_vec(Plane p_vec) {
  319. CameraMatrix cm;
  320. cm.set_perspective(45, 1, 0, 100);
  321. Plane v0 = cm.xform4(p_vec);
  322. print_line("out: " + v0);
  323. v0.normal.z = (v0.d / 100.0 * 2.0 - 1.0) * v0.d;
  324. print_line("out_F: " + v0);
  325. }
  326. uint32_t ihash(uint32_t a) {
  327. a = (a + 0x7ed55d16) + (a << 12);
  328. a = (a ^ 0xc761c23c) ^ (a >> 19);
  329. a = (a + 0x165667b1) + (a << 5);
  330. a = (a + 0xd3a2646c) ^ (a << 9);
  331. a = (a + 0xfd7046c5) + (a << 3);
  332. a = (a ^ 0xb55a4f09) ^ (a >> 16);
  333. return a;
  334. }
  335. uint32_t ihash2(uint32_t a) {
  336. a = (a ^ 61) ^ (a >> 16);
  337. a = a + (a << 3);
  338. a = a ^ (a >> 4);
  339. a = a * 0x27d4eb2d;
  340. a = a ^ (a >> 15);
  341. return a;
  342. }
  343. uint32_t ihash3(uint32_t a) {
  344. a = (a + 0x479ab41d) + (a << 8);
  345. a = (a ^ 0xe4aa10ce) ^ (a >> 5);
  346. a = (a + 0x9942f0a6) - (a << 14);
  347. a = (a ^ 0x5aedd67d) ^ (a >> 3);
  348. a = (a + 0x17bea992) + (a << 7);
  349. return a;
  350. }
  351. MainLoop *test() {
  352. {
  353. float r = 1;
  354. float g = 0.5;
  355. float b = 0.1;
  356. const float pow2to9 = 512.0f;
  357. const float B = 15.0f;
  358. const float N = 9.0f;
  359. float sharedexp = 65408.000f;
  360. float cRed = MAX(0.0f, MIN(sharedexp, r));
  361. float cGreen = MAX(0.0f, MIN(sharedexp, g));
  362. float cBlue = MAX(0.0f, MIN(sharedexp, b));
  363. float cMax = MAX(cRed, MAX(cGreen, cBlue));
  364. float expp = MAX(-B - 1.0f, floor(Math::log(cMax) / Math_LN2)) + 1.0f + B;
  365. float sMax = (float)floor((cMax / Math::pow(2.0f, expp - B - N)) + 0.5f);
  366. float exps = expp + 1.0f;
  367. if (0.0 <= sMax && sMax < pow2to9) {
  368. exps = expp;
  369. }
  370. float sRed = Math::floor((cRed / pow(2.0f, exps - B - N)) + 0.5f);
  371. float sGreen = Math::floor((cGreen / pow(2.0f, exps - B - N)) + 0.5f);
  372. float sBlue = Math::floor((cBlue / pow(2.0f, exps - B - N)) + 0.5f);
  373. print_line("R: " + rtos(sRed) + " G: " + rtos(sGreen) + " B: " + rtos(sBlue) + " EXP: " + rtos(exps));
  374. 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);
  375. float rb = rgbe & 0x1ff;
  376. float gb = (rgbe >> 9) & 0x1ff;
  377. float bb = (rgbe >> 18) & 0x1ff;
  378. float eb = (rgbe >> 27);
  379. float mb = Math::pow(2, eb - 15.0 - 9.0);
  380. float rd = rb * mb;
  381. float gd = gb * mb;
  382. float bd = bb * mb;
  383. print_line("RGBE: " + Color(rd, gd, bd));
  384. }
  385. print_line("Dvectors: " + itos(MemoryPool::allocs_used));
  386. print_line("Mem used: " + itos(MemoryPool::total_memory));
  387. print_line("MAx mem used: " + itos(MemoryPool::max_memory));
  388. PoolVector<int> ints;
  389. ints.resize(20);
  390. {
  391. PoolVector<int>::Write w;
  392. w = ints.write();
  393. for (int i = 0; i < ints.size(); i++) {
  394. w[i] = i;
  395. }
  396. }
  397. PoolVector<int> posho = ints;
  398. {
  399. PoolVector<int>::Read r = posho.read();
  400. for (int i = 0; i < posho.size(); i++) {
  401. print_line(itos(i) + " : " + itos(r[i]));
  402. }
  403. }
  404. print_line("later Dvectors: " + itos(MemoryPool::allocs_used));
  405. print_line("later Mem used: " + itos(MemoryPool::total_memory));
  406. print_line("Mlater Ax mem used: " + itos(MemoryPool::max_memory));
  407. List<String> cmdlargs = OS::get_singleton()->get_cmdline_args();
  408. if (cmdlargs.empty()) {
  409. //try editor!
  410. return nullptr;
  411. }
  412. String test = cmdlargs.back()->get();
  413. if (test == "math") {
  414. // Not a file name but the test name, abort.
  415. // FIXME: This test is ugly as heck, needs fixing :)
  416. return nullptr;
  417. }
  418. FileAccess *fa = FileAccess::open(test, FileAccess::READ);
  419. ERR_FAIL_COND_V_MSG(!fa, nullptr, "Could not open file: " + test);
  420. Vector<uint8_t> buf;
  421. uint64_t flen = fa->get_len();
  422. buf.resize(fa->get_len() + 1);
  423. fa->get_buffer(buf.ptrw(), flen);
  424. buf.write[flen] = 0;
  425. String code;
  426. code.parse_utf8((const char *)&buf[0]);
  427. GetClassAndNamespace getclass;
  428. if (getclass.parse(code)) {
  429. print_line("Parse error: " + getclass.get_error());
  430. } else {
  431. print_line("Found class: " + getclass.get_class());
  432. }
  433. {
  434. Vector<int> hashes;
  435. List<StringName> tl;
  436. ClassDB::get_class_list(&tl);
  437. for (List<StringName>::Element *E = tl.front(); E; E = E->next()) {
  438. Vector<uint8_t> m5b = E->get().operator String().md5_buffer();
  439. hashes.push_back(hashes.size());
  440. }
  441. for (int i = nearest_shift(hashes.size()); i < 20; i++) {
  442. bool success = true;
  443. for (int s = 0; s < 10000; s++) {
  444. Set<uint32_t> existing;
  445. success = true;
  446. for (int j = 0; j < hashes.size(); j++) {
  447. uint32_t eh = ihash2(ihash3(hashes[j] + ihash(s) + s)) & ((1 << i) - 1);
  448. if (existing.has(eh)) {
  449. success = false;
  450. break;
  451. }
  452. existing.insert(eh);
  453. }
  454. if (success) {
  455. print_line("success at " + itos(i) + "/" + itos(nearest_shift(hashes.size())) + " shift " + itos(s));
  456. break;
  457. }
  458. }
  459. if (success) {
  460. break;
  461. }
  462. }
  463. print_line("DONE");
  464. }
  465. {
  466. print_line("NUM: " + itos(-128));
  467. }
  468. {
  469. Vector3 v(1, 2, 3);
  470. v.normalize();
  471. float a = 0.3;
  472. Basis m(v, a);
  473. Vector3 v2(7, 3, 1);
  474. v2.normalize();
  475. float a2 = 0.8;
  476. Basis m2(v2, a2);
  477. Quat q = m;
  478. Quat q2 = m2;
  479. Basis m3 = m.inverse() * m2;
  480. Quat q3 = (q.inverse() * q2); //.normalized();
  481. print_line(Quat(m3));
  482. print_line(q3);
  483. print_line("before v: " + v + " a: " + rtos(a));
  484. q.get_axis_angle(v, a);
  485. print_line("after v: " + v + " a: " + rtos(a));
  486. }
  487. String ret;
  488. List<String> args;
  489. args.push_back("-l");
  490. Error err = OS::get_singleton()->execute("/bin/ls", args, true, nullptr, &ret);
  491. print_line("error: " + itos(err));
  492. print_line(ret);
  493. Basis m3;
  494. m3.rotate(Vector3(1, 0, 0), 0.2);
  495. m3.rotate(Vector3(0, 1, 0), 1.77);
  496. m3.rotate(Vector3(0, 0, 1), 212);
  497. Basis m32;
  498. m32.set_euler(m3.get_euler());
  499. print_line("ELEULEEEEEEEEEEEEEEEEEER: " + m3.get_euler() + " vs " + m32.get_euler());
  500. {
  501. Dictionary d;
  502. d["momo"] = 1;
  503. Dictionary b = d;
  504. b["44"] = 4;
  505. }
  506. print_line("inters: " + rtos(Geometry::segment_intersects_circle(Vector2(-5, 0), Vector2(-2, 0), Vector2(), 1.0)));
  507. print_line("cross: " + Vector3(1, 2, 3).cross(Vector3(4, 5, 7)));
  508. print_line("dot: " + rtos(Vector3(1, 2, 3).dot(Vector3(4, 5, 7))));
  509. print_line("abs: " + Vector3(-1, 2, -3).abs());
  510. print_line("distance_to: " + rtos(Vector3(1, 2, 3).distance_to(Vector3(4, 5, 7))));
  511. print_line("distance_squared_to: " + rtos(Vector3(1, 2, 3).distance_squared_to(Vector3(4, 5, 7))));
  512. print_line("plus: " + (Vector3(1, 2, 3) + Vector3(Vector3(4, 5, 7))));
  513. print_line("minus: " + (Vector3(1, 2, 3) - Vector3(Vector3(4, 5, 7))));
  514. print_line("mul: " + (Vector3(1, 2, 3) * Vector3(Vector3(4, 5, 7))));
  515. print_line("div: " + (Vector3(1, 2, 3) / Vector3(Vector3(4, 5, 7))));
  516. print_line("mul scalar: " + (Vector3(1, 2, 3) * 2));
  517. print_line("premul scalar: " + (2 * Vector3(1, 2, 3)));
  518. print_line("div scalar: " + (Vector3(1, 2, 3) / 3.0));
  519. print_line("length: " + rtos(Vector3(1, 2, 3).length()));
  520. print_line("length squared: " + rtos(Vector3(1, 2, 3).length_squared()));
  521. print_line("normalized: " + Vector3(1, 2, 3).normalized());
  522. print_line("inverse: " + Vector3(1, 2, 3).inverse());
  523. {
  524. Vector3 v(4, 5, 7);
  525. v.normalize();
  526. print_line("normalize: " + v);
  527. }
  528. {
  529. Vector3 v(4, 5, 7);
  530. v += Vector3(1, 2, 3);
  531. print_line("+=: " + v);
  532. }
  533. {
  534. Vector3 v(4, 5, 7);
  535. v -= Vector3(1, 2, 3);
  536. print_line("-=: " + v);
  537. }
  538. {
  539. Vector3 v(4, 5, 7);
  540. v *= Vector3(1, 2, 3);
  541. print_line("*=: " + v);
  542. }
  543. {
  544. Vector3 v(4, 5, 7);
  545. v /= Vector3(1, 2, 3);
  546. print_line("/=: " + v);
  547. }
  548. {
  549. Vector3 v(4, 5, 7);
  550. v *= 2.0;
  551. print_line("scalar *=: " + v);
  552. }
  553. {
  554. Vector3 v(4, 5, 7);
  555. v /= 2.0;
  556. print_line("scalar /=: " + v);
  557. }
  558. return nullptr;
  559. }
  560. } // namespace TestMath