test_math.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. /*************************************************************************/
  2. /* test_math.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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/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/rendering/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': res = 8; break;
  172. case 't': res = 9; break;
  173. case 'n': res = 10; break;
  174. case 'f': res = 12; break;
  175. case 'r':
  176. res = 13;
  177. break;
  178. case '\"': res = '\"'; 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. tk_string += code[idx];
  191. }
  192. idx++;
  193. }
  194. value = tk_string;
  195. return TK_STRING;
  196. } break;
  197. default: {
  198. if (code[idx] <= 32) {
  199. idx++;
  200. break;
  201. }
  202. 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)) {
  203. value = String::chr(code[idx]);
  204. idx++;
  205. return TK_SYMBOL;
  206. }
  207. if (code[idx] == '-' || (code[idx] >= '0' && code[idx] <= '9')) {
  208. //a number
  209. const CharType *rptr;
  210. double number = String::to_double(&code[idx], &rptr);
  211. idx += (rptr - &code[idx]);
  212. value = number;
  213. return TK_NUMBER;
  214. } else if ((code[idx] >= 'A' && code[idx] <= 'Z') || (code[idx] >= 'a' && code[idx] <= 'z') || code[idx] > 127) {
  215. String id;
  216. while ((code[idx] >= 'A' && code[idx] <= 'Z') || (code[idx] >= 'a' && code[idx] <= 'z') || code[idx] > 127) {
  217. id += code[idx];
  218. idx++;
  219. }
  220. value = id;
  221. return TK_IDENTIFIER;
  222. } else {
  223. error_str = "Unexpected character.";
  224. error = true;
  225. return TK_ERROR;
  226. }
  227. }
  228. }
  229. }
  230. }
  231. public:
  232. Error parse(const String &p_code, const String &p_known_class_name = String()) {
  233. code = p_code;
  234. idx = 0;
  235. line = 0;
  236. error_str = String();
  237. error = false;
  238. value = Variant();
  239. class_name = String();
  240. bool use_next_class = false;
  241. Token tk = get_token();
  242. Map<int, String> namespace_stack;
  243. int curly_stack = 0;
  244. while (!error || tk != TK_EOF) {
  245. if (tk == TK_BRACKET_OPEN) {
  246. tk = get_token();
  247. if (tk == TK_IDENTIFIER && String(value) == "ScriptClass") {
  248. if (get_token() == TK_BRACKET_CLOSE) {
  249. use_next_class = true;
  250. }
  251. }
  252. } else if (tk == TK_IDENTIFIER && String(value) == "class") {
  253. tk = get_token();
  254. if (tk == TK_IDENTIFIER) {
  255. String name = value;
  256. if (use_next_class || p_known_class_name == name) {
  257. for (Map<int, String>::Element *E = namespace_stack.front(); E; E = E->next()) {
  258. class_name += E->get() + ".";
  259. }
  260. class_name += String(value);
  261. break;
  262. }
  263. }
  264. } else if (tk == TK_IDENTIFIER && String(value) == "namespace") {
  265. String name;
  266. int at_level = curly_stack;
  267. while (true) {
  268. tk = get_token();
  269. if (tk == TK_IDENTIFIER) {
  270. name += String(value);
  271. }
  272. tk = get_token();
  273. if (tk == TK_PERIOD) {
  274. name += ".";
  275. } else if (tk == TK_CURLY_BRACKET_OPEN) {
  276. curly_stack++;
  277. break;
  278. } else {
  279. break; //whathever else
  280. }
  281. }
  282. if (name != String()) {
  283. namespace_stack[at_level] = name;
  284. }
  285. } else if (tk == TK_CURLY_BRACKET_OPEN) {
  286. curly_stack++;
  287. } else if (tk == TK_CURLY_BRACKET_CLOSE) {
  288. curly_stack--;
  289. if (namespace_stack.has(curly_stack)) {
  290. namespace_stack.erase(curly_stack);
  291. }
  292. }
  293. tk = get_token();
  294. }
  295. if (error)
  296. return ERR_PARSE_ERROR;
  297. return OK;
  298. }
  299. String get_error() {
  300. return error_str;
  301. }
  302. String get_class() {
  303. return class_name;
  304. }
  305. };
  306. void test_vec(Plane p_vec) {
  307. CameraMatrix cm;
  308. cm.set_perspective(45, 1, 0, 100);
  309. Plane v0 = cm.xform4(p_vec);
  310. print_line("out: " + v0);
  311. v0.normal.z = (v0.distance / 100.0 * 2.0 - 1.0) * v0.distance;
  312. print_line("out_F: " + v0);
  313. }
  314. uint32_t ihash(uint32_t a) {
  315. a = (a + 0x7ed55d16) + (a << 12);
  316. a = (a ^ 0xc761c23c) ^ (a >> 19);
  317. a = (a + 0x165667b1) + (a << 5);
  318. a = (a + 0xd3a2646c) ^ (a << 9);
  319. a = (a + 0xfd7046c5) + (a << 3);
  320. a = (a ^ 0xb55a4f09) ^ (a >> 16);
  321. return a;
  322. }
  323. uint32_t ihash2(uint32_t a) {
  324. a = (a ^ 61) ^ (a >> 16);
  325. a = a + (a << 3);
  326. a = a ^ (a >> 4);
  327. a = a * 0x27d4eb2d;
  328. a = a ^ (a >> 15);
  329. return a;
  330. }
  331. uint32_t ihash3(uint32_t a) {
  332. a = (a + 0x479ab41d) + (a << 8);
  333. a = (a ^ 0xe4aa10ce) ^ (a >> 5);
  334. a = (a + 0x9942f0a6) - (a << 14);
  335. a = (a ^ 0x5aedd67d) ^ (a >> 3);
  336. a = (a + 0x17bea992) + (a << 7);
  337. return a;
  338. }
  339. MainLoop *test() {
  340. {
  341. float r = 1;
  342. float g = 0.5;
  343. float b = 0.1;
  344. const float pow2to9 = 512.0f;
  345. const float B = 15.0f;
  346. const float N = 9.0f;
  347. float sharedexp = 65408.000f;
  348. float cRed = MAX(0.0f, MIN(sharedexp, r));
  349. float cGreen = MAX(0.0f, MIN(sharedexp, g));
  350. float cBlue = MAX(0.0f, MIN(sharedexp, b));
  351. float cMax = MAX(cRed, MAX(cGreen, cBlue));
  352. float expp = MAX(-B - 1.0f, floor(Math::log(cMax) / Math_LN2)) + 1.0f + B;
  353. float sMax = (float)floor((cMax / Math::pow(2.0f, expp - B - N)) + 0.5f);
  354. float exps = expp + 1.0f;
  355. if (0.0 <= sMax && sMax < pow2to9) {
  356. exps = expp;
  357. }
  358. float sRed = Math::floor((cRed / pow(2.0f, exps - B - N)) + 0.5f);
  359. float sGreen = Math::floor((cGreen / pow(2.0f, exps - B - N)) + 0.5f);
  360. float sBlue = Math::floor((cBlue / pow(2.0f, exps - B - N)) + 0.5f);
  361. print_line("R: " + rtos(sRed) + " G: " + rtos(sGreen) + " B: " + rtos(sBlue) + " EXP: " + rtos(exps));
  362. 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);
  363. float rb = rgbe & 0x1ff;
  364. float gb = (rgbe >> 9) & 0x1ff;
  365. float bb = (rgbe >> 18) & 0x1ff;
  366. float eb = (rgbe >> 27);
  367. float mb = Math::pow(2, eb - 15.0 - 9.0);
  368. float rd = rb * mb;
  369. float gd = gb * mb;
  370. float bd = bb * mb;
  371. print_line("RGBE: " + Color(rd, gd, bd));
  372. }
  373. Vector<int> ints;
  374. ints.resize(20);
  375. {
  376. int *w;
  377. w = ints.ptrw();
  378. for (int i = 0; i < ints.size(); i++) {
  379. w[i] = i;
  380. }
  381. }
  382. Vector<int> posho = ints;
  383. {
  384. const int *r = posho.ptr();
  385. for (int i = 0; i < posho.size(); i++) {
  386. print_line(itos(i) + " : " + itos(r[i]));
  387. }
  388. }
  389. List<String> cmdlargs = OS::get_singleton()->get_cmdline_args();
  390. if (cmdlargs.empty()) {
  391. //try editor!
  392. return nullptr;
  393. }
  394. String test = cmdlargs.back()->get();
  395. if (test == "math") {
  396. // Not a file name but the test name, abort.
  397. // FIXME: This test is ugly as heck, needs fixing :)
  398. return nullptr;
  399. }
  400. FileAccess *fa = FileAccess::open(test, FileAccess::READ);
  401. ERR_FAIL_COND_V_MSG(!fa, nullptr, "Could not open file: " + test);
  402. Vector<uint8_t> buf;
  403. int flen = fa->get_len();
  404. buf.resize(fa->get_len() + 1);
  405. fa->get_buffer(buf.ptrw(), flen);
  406. buf.write[flen] = 0;
  407. String code;
  408. code.parse_utf8((const char *)&buf[0]);
  409. GetClassAndNamespace getclass;
  410. if (getclass.parse(code)) {
  411. print_line("Parse error: " + getclass.get_error());
  412. } else {
  413. print_line("Found class: " + getclass.get_class());
  414. }
  415. {
  416. Vector<int> hashes;
  417. List<StringName> tl;
  418. ClassDB::get_class_list(&tl);
  419. for (List<StringName>::Element *E = tl.front(); E; E = E->next()) {
  420. Vector<uint8_t> m5b = E->get().operator String().md5_buffer();
  421. hashes.push_back(hashes.size());
  422. }
  423. for (int i = nearest_shift(hashes.size()); i < 20; i++) {
  424. bool success = true;
  425. for (int s = 0; s < 10000; s++) {
  426. Set<uint32_t> existing;
  427. success = true;
  428. for (int j = 0; j < hashes.size(); j++) {
  429. uint32_t eh = ihash2(ihash3(hashes[j] + ihash(s) + s)) & ((1 << i) - 1);
  430. if (existing.has(eh)) {
  431. success = false;
  432. break;
  433. }
  434. existing.insert(eh);
  435. }
  436. if (success) {
  437. print_line("success at " + itos(i) + "/" + itos(nearest_shift(hashes.size())) + " shift " + itos(s));
  438. break;
  439. }
  440. }
  441. if (success)
  442. break;
  443. }
  444. print_line("DONE");
  445. }
  446. {
  447. print_line("NUM: " + itos(-128));
  448. }
  449. {
  450. Vector3 v(1, 2, 3);
  451. v.normalize();
  452. float a = 0.3;
  453. Basis m(v, a);
  454. Vector3 v2(7, 3, 1);
  455. v2.normalize();
  456. float a2 = 0.8;
  457. Basis m2(v2, a2);
  458. Quat q = m;
  459. Quat q2 = m2;
  460. Basis m3 = m.inverse() * m2;
  461. Quat q3 = (q.inverse() * q2); //.normalized();
  462. print_line(Quat(m3));
  463. print_line(q3);
  464. print_line("before v: " + v + " a: " + rtos(a));
  465. q.get_axis_angle(v, a);
  466. print_line("after v: " + v + " a: " + rtos(a));
  467. }
  468. String ret;
  469. List<String> args;
  470. args.push_back("-l");
  471. Error err = OS::get_singleton()->execute("/bin/ls", args, true, nullptr, &ret);
  472. print_line("error: " + itos(err));
  473. print_line(ret);
  474. Basis m3;
  475. m3.rotate(Vector3(1, 0, 0), 0.2);
  476. m3.rotate(Vector3(0, 1, 0), 1.77);
  477. m3.rotate(Vector3(0, 0, 1), 212);
  478. Basis m32;
  479. m32.set_euler(m3.get_euler());
  480. print_line("ELEULEEEEEEEEEEEEEEEEEER: " + m3.get_euler() + " vs " + m32.get_euler());
  481. {
  482. Dictionary d;
  483. d["momo"] = 1;
  484. Dictionary b = d;
  485. b["44"] = 4;
  486. }
  487. print_line("inters: " + rtos(Geometry::segment_intersects_circle(Vector2(-5, 0), Vector2(-2, 0), Vector2(), 1.0)));
  488. print_line("cross: " + Vector3(1, 2, 3).cross(Vector3(4, 5, 7)));
  489. print_line("dot: " + rtos(Vector3(1, 2, 3).dot(Vector3(4, 5, 7))));
  490. print_line("abs: " + Vector3(-1, 2, -3).abs());
  491. print_line("distance_to: " + rtos(Vector3(1, 2, 3).distance_to(Vector3(4, 5, 7))));
  492. print_line("distance_squared_to: " + rtos(Vector3(1, 2, 3).distance_squared_to(Vector3(4, 5, 7))));
  493. print_line("plus: " + (Vector3(1, 2, 3) + Vector3(Vector3(4, 5, 7))));
  494. print_line("minus: " + (Vector3(1, 2, 3) - Vector3(Vector3(4, 5, 7))));
  495. print_line("mul: " + (Vector3(1, 2, 3) * Vector3(Vector3(4, 5, 7))));
  496. print_line("div: " + (Vector3(1, 2, 3) / Vector3(Vector3(4, 5, 7))));
  497. print_line("mul scalar: " + (Vector3(1, 2, 3) * 2.0));
  498. print_line("premul scalar: " + (2.0 * Vector3(1, 2, 3)));
  499. print_line("div scalar: " + (Vector3(1, 2, 3) / 3.0));
  500. print_line("length: " + rtos(Vector3(1, 2, 3).length()));
  501. print_line("length squared: " + rtos(Vector3(1, 2, 3).length_squared()));
  502. print_line("normalized: " + Vector3(1, 2, 3).normalized());
  503. print_line("inverse: " + Vector3(1, 2, 3).inverse());
  504. {
  505. Vector3 v(4, 5, 7);
  506. v.normalize();
  507. print_line("normalize: " + v);
  508. }
  509. {
  510. Vector3 v(4, 5, 7);
  511. v += Vector3(1, 2, 3);
  512. print_line("+=: " + v);
  513. }
  514. {
  515. Vector3 v(4, 5, 7);
  516. v -= Vector3(1, 2, 3);
  517. print_line("-=: " + v);
  518. }
  519. {
  520. Vector3 v(4, 5, 7);
  521. v *= Vector3(1, 2, 3);
  522. print_line("*=: " + v);
  523. }
  524. {
  525. Vector3 v(4, 5, 7);
  526. v /= Vector3(1, 2, 3);
  527. print_line("/=: " + v);
  528. }
  529. {
  530. Vector3 v(4, 5, 7);
  531. v *= 2.0;
  532. print_line("scalar *=: " + v);
  533. }
  534. {
  535. Vector3 v(4, 5, 7);
  536. v /= 2.0;
  537. print_line("scalar /=: " + v);
  538. }
  539. return nullptr;
  540. }
  541. } // namespace TestMath