test_math.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. /*************************************************************************/
  2. /* test_math.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "camera_matrix.h"
  32. #include "math_funcs.h"
  33. #include "matrix3.h"
  34. #include "os/file_access.h"
  35. #include "os/keyboard.h"
  36. #include "os/os.h"
  37. #include "print_string.h"
  38. #include "scene/main/node.h"
  39. #include "scene/resources/texture.h"
  40. #include "servers/visual/shader_language.h"
  41. #include "transform.h"
  42. #include "ustring.h"
  43. #include "variant.h"
  44. #include "vmap.h"
  45. #include "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.d / 100.0 * 2.0 - 1.0) * v0.d;
  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. ;
  369. float rd = rb * mb;
  370. float gd = gb * mb;
  371. float bd = bb * mb;
  372. print_line("RGBE: " + Color(rd, gd, bd));
  373. return NULL;
  374. }
  375. print_line("Dvectors: " + itos(MemoryPool::allocs_used));
  376. print_line("Mem used: " + itos(MemoryPool::total_memory));
  377. print_line("MAx mem used: " + itos(MemoryPool::max_memory));
  378. PoolVector<int> ints;
  379. ints.resize(20);
  380. {
  381. PoolVector<int>::Write w;
  382. w = ints.write();
  383. for (int i = 0; i < ints.size(); i++) {
  384. w[i] = i;
  385. }
  386. }
  387. PoolVector<int> posho = ints;
  388. {
  389. PoolVector<int>::Read r = posho.read();
  390. for (int i = 0; i < posho.size(); i++) {
  391. print_line(itos(i) + " : " + itos(r[i]));
  392. }
  393. }
  394. print_line("later Dvectors: " + itos(MemoryPool::allocs_used));
  395. print_line("later Mem used: " + itos(MemoryPool::total_memory));
  396. print_line("Mlater Ax mem used: " + itos(MemoryPool::max_memory));
  397. return NULL;
  398. List<String> cmdlargs = OS::get_singleton()->get_cmdline_args();
  399. if (cmdlargs.empty()) {
  400. //try editor!
  401. return NULL;
  402. }
  403. String test = cmdlargs.back()->get();
  404. FileAccess *fa = FileAccess::open(test, FileAccess::READ);
  405. if (!fa) {
  406. ERR_EXPLAIN("Could not open file: " + test);
  407. ERR_FAIL_V(NULL);
  408. }
  409. Vector<uint8_t> buf;
  410. int flen = fa->get_len();
  411. buf.resize(fa->get_len() + 1);
  412. fa->get_buffer(&buf[0], flen);
  413. buf[flen] = 0;
  414. String code;
  415. code.parse_utf8((const char *)&buf[0]);
  416. GetClassAndNamespace getclass;
  417. if (getclass.parse(code)) {
  418. print_line("Parse error: " + getclass.get_error());
  419. } else {
  420. print_line("Found class: " + getclass.get_class());
  421. }
  422. return NULL;
  423. {
  424. Vector<int> hashes;
  425. List<StringName> tl;
  426. ClassDB::get_class_list(&tl);
  427. for (List<StringName>::Element *E = tl.front(); E; E = E->next()) {
  428. Vector<uint8_t> m5b = E->get().operator String().md5_buffer();
  429. hashes.push_back(hashes.size());
  430. }
  431. for (int i = nearest_shift(hashes.size()); i < 20; i++) {
  432. bool success = true;
  433. for (int s = 0; s < 10000; s++) {
  434. Set<uint32_t> existing;
  435. success = true;
  436. for (int j = 0; j < hashes.size(); j++) {
  437. uint32_t eh = ihash2(ihash3(hashes[j] + ihash(s) + s)) & ((1 << i) - 1);
  438. if (existing.has(eh)) {
  439. success = false;
  440. break;
  441. }
  442. existing.insert(eh);
  443. }
  444. if (success) {
  445. print_line("success at " + itos(i) + "/" + itos(nearest_shift(hashes.size())) + " shift " + itos(s));
  446. break;
  447. }
  448. }
  449. if (success)
  450. break;
  451. }
  452. print_line("DONE");
  453. return NULL;
  454. }
  455. {
  456. print_line("NUM: " + itos(-128));
  457. return NULL;
  458. }
  459. {
  460. Vector3 v(1, 2, 3);
  461. v.normalize();
  462. float a = 0.3;
  463. Basis m(v, a);
  464. Vector3 v2(7, 3, 1);
  465. v2.normalize();
  466. float a2 = 0.8;
  467. Basis m2(v2, a2);
  468. Quat q = m;
  469. Quat q2 = m2;
  470. Basis m3 = m.inverse() * m2;
  471. Quat q3 = (q.inverse() * q2); //.normalized();
  472. print_line(Quat(m3));
  473. print_line(q3);
  474. print_line("before v: " + v + " a: " + rtos(a));
  475. q.get_axis_angle(v, a);
  476. print_line("after v: " + v + " a: " + rtos(a));
  477. }
  478. return NULL;
  479. String ret;
  480. List<String> args;
  481. args.push_back("-l");
  482. Error err = OS::get_singleton()->execute("/bin/ls", args, true, NULL, &ret);
  483. print_line("error: " + itos(err));
  484. print_line(ret);
  485. return NULL;
  486. Basis m3;
  487. m3.rotate(Vector3(1, 0, 0), 0.2);
  488. m3.rotate(Vector3(0, 1, 0), 1.77);
  489. m3.rotate(Vector3(0, 0, 1), 212);
  490. Basis m32;
  491. m32.set_euler(m3.get_euler());
  492. print_line("ELEULEEEEEEEEEEEEEEEEEER: " + m3.get_euler() + " vs " + m32.get_euler());
  493. return NULL;
  494. {
  495. Dictionary d;
  496. d["momo"] = 1;
  497. Dictionary b = d;
  498. b["44"] = 4;
  499. }
  500. return NULL;
  501. print_line("inters: " + rtos(Geometry::segment_intersects_circle(Vector2(-5, 0), Vector2(-2, 0), Vector2(), 1.0)));
  502. print_line("cross: " + Vector3(1, 2, 3).cross(Vector3(4, 5, 7)));
  503. print_line("dot: " + rtos(Vector3(1, 2, 3).dot(Vector3(4, 5, 7))));
  504. print_line("abs: " + Vector3(-1, 2, -3).abs());
  505. print_line("distance_to: " + rtos(Vector3(1, 2, 3).distance_to(Vector3(4, 5, 7))));
  506. print_line("distance_squared_to: " + rtos(Vector3(1, 2, 3).distance_squared_to(Vector3(4, 5, 7))));
  507. print_line("plus: " + (Vector3(1, 2, 3) + Vector3(Vector3(4, 5, 7))));
  508. print_line("minus: " + (Vector3(1, 2, 3) - Vector3(Vector3(4, 5, 7))));
  509. print_line("mul: " + (Vector3(1, 2, 3) * Vector3(Vector3(4, 5, 7))));
  510. print_line("div: " + (Vector3(1, 2, 3) / Vector3(Vector3(4, 5, 7))));
  511. print_line("mul scalar: " + (Vector3(1, 2, 3) * 2));
  512. print_line("premul scalar: " + (2 * Vector3(1, 2, 3)));
  513. print_line("div scalar: " + (Vector3(1, 2, 3) / 3.0));
  514. print_line("length: " + rtos(Vector3(1, 2, 3).length()));
  515. print_line("length squared: " + rtos(Vector3(1, 2, 3).length_squared()));
  516. print_line("normalized: " + Vector3(1, 2, 3).normalized());
  517. print_line("inverse: " + Vector3(1, 2, 3).inverse());
  518. {
  519. Vector3 v(4, 5, 7);
  520. v.normalize();
  521. print_line("normalize: " + v);
  522. }
  523. {
  524. Vector3 v(4, 5, 7);
  525. v += Vector3(1, 2, 3);
  526. print_line("+=: " + 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 *= 2.0;
  546. print_line("scalar *=: " + v);
  547. }
  548. {
  549. Vector3 v(4, 5, 7);
  550. v /= 2.0;
  551. print_line("scalar /=: " + v);
  552. }
  553. return NULL;
  554. }
  555. } // namespace TestMath