test_math.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. /*************************************************************************/
  2. /* test_math.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "test_math.h"
  30. #include "ustring.h"
  31. #include "print_string.h"
  32. #include "transform.h"
  33. #include "matrix3.h"
  34. #include "math_funcs.h"
  35. #include "camera_matrix.h"
  36. #include "scene/main/node.h"
  37. #include "variant.h"
  38. #include "servers/visual/shader_language.h"
  39. #include "os/keyboard.h"
  40. #include "scene/resources/texture.h"
  41. #include "vmap.h"
  42. #include "os/os.h"
  43. #include "os/file_access.h"
  44. #include "method_ptrcall.h"
  45. namespace TestMath {
  46. class GetClassAndNamespace {
  47. String code;
  48. int idx;
  49. int line;
  50. String error_str;
  51. bool error;
  52. Variant value;
  53. String class_name;
  54. enum Token {
  55. TK_BRACKET_OPEN,
  56. TK_BRACKET_CLOSE,
  57. TK_CURLY_BRACKET_OPEN,
  58. TK_CURLY_BRACKET_CLOSE,
  59. TK_PERIOD,
  60. TK_COLON,
  61. TK_COMMA,
  62. TK_SYMBOL,
  63. TK_IDENTIFIER,
  64. TK_STRING,
  65. TK_NUMBER,
  66. TK_EOF,
  67. TK_ERROR
  68. };
  69. Token get_token() {
  70. while (true) {
  71. switch(code[idx]) {
  72. case '\n': {
  73. line++;
  74. idx++;
  75. break;
  76. };
  77. case 0: {
  78. return TK_EOF;
  79. } break;
  80. case '{': {
  81. idx++;
  82. return TK_CURLY_BRACKET_OPEN;
  83. };
  84. case '}': {
  85. idx++;
  86. return TK_CURLY_BRACKET_CLOSE;
  87. };
  88. case '[': {
  89. idx++;
  90. return TK_BRACKET_OPEN;
  91. };
  92. case ']': {
  93. idx++;
  94. return TK_BRACKET_CLOSE;
  95. };
  96. case ':': {
  97. idx++;
  98. return TK_COLON;
  99. };
  100. case ',': {
  101. idx++;
  102. return TK_COMMA;
  103. };
  104. case '.': {
  105. idx++;
  106. return TK_PERIOD;
  107. };
  108. case '#': {
  109. //compiler directive
  110. while(code[idx]!='\n' && code[idx]!=0) {
  111. idx++;
  112. }
  113. continue;
  114. } break;
  115. case '/': {
  116. switch(code[idx+1]) {
  117. case '*': { // block comment
  118. idx+=2;
  119. while(true) {
  120. if (code[idx]==0) {
  121. error_str="Unterminated comment";
  122. error=true;
  123. return TK_ERROR;
  124. } if (code[idx]=='*' &&code[idx+1]=='/') {
  125. idx+=2;
  126. break;
  127. } if (code[idx]=='\n') {
  128. line++;
  129. }
  130. idx++;
  131. }
  132. } break;
  133. case '/': { // line comment skip
  134. while(code[idx]!='\n' && code[idx]!=0) {
  135. idx++;
  136. }
  137. } break;
  138. default: {
  139. value="/";
  140. idx++;
  141. return TK_SYMBOL;
  142. }
  143. }
  144. continue; // a comment
  145. } break;
  146. case '\'':
  147. case '"': {
  148. CharType begin_str = code[idx];
  149. idx++;
  150. String tk_string=String();
  151. while(true) {
  152. if (code[idx]==0) {
  153. error_str="Unterminated String";
  154. error=true;
  155. return TK_ERROR;
  156. } else if (code[idx]==begin_str) {
  157. idx++;
  158. break;
  159. } else if (code[idx]=='\\') {
  160. //escaped characters...
  161. idx++;
  162. CharType next = code[idx];
  163. if (next==0) {
  164. error_str="Unterminated String";
  165. error=true;
  166. return TK_ERROR;
  167. }
  168. CharType res=0;
  169. switch(next) {
  170. case 'b': res=8; break;
  171. case 't': res=9; break;
  172. case 'n': res=10; break;
  173. case 'f': res=12; break;
  174. case 'r': res=13; break;
  175. /* too much, not needed for now
  176. case 'u': {
  177. //hexnumbarh - oct is deprecated
  178. for(int j=0;j<4;j++) {
  179. CharType c = code[idx+j+1];
  180. if (c==0) {
  181. r_err_str="Unterminated String";
  182. return ERR_PARSE_ERROR;
  183. }
  184. if (!((c>='0' && c<='9') || (c>='a' && c<='f') || (c>='A' && c<='F'))) {
  185. r_err_str="Malformed hex constant in string";
  186. return ERR_PARSE_ERROR;
  187. }
  188. CharType v;
  189. if (c>='0' && c<='9') {
  190. v=c-'0';
  191. } else if (c>='a' && c<='f') {
  192. v=c-'a';
  193. v+=10;
  194. } else if (c>='A' && c<='F') {
  195. v=c-'A';
  196. v+=10;
  197. } else {
  198. ERR_PRINT("BUG");
  199. v=0;
  200. }
  201. res<<=4;
  202. res|=v;
  203. }
  204. idx+=4; //will add at the end anyway
  205. } break;*/
  206. case '\"': res='\"'; break;
  207. case '\\': res='\\'; break;
  208. //case '/': res='/'; break;
  209. default: {
  210. res = next;
  211. //r_err_str="Invalid escape sequence";
  212. //return ERR_PARSE_ERROR;
  213. } break;
  214. }
  215. tk_string+=res;
  216. } else {
  217. if (code[idx]=='\n')
  218. line++;
  219. tk_string+=code[idx];
  220. }
  221. idx++;
  222. }
  223. value=tk_string;
  224. return TK_STRING;
  225. } break;
  226. default: {
  227. if (code[idx]<=32) {
  228. idx++;
  229. break;
  230. }
  231. 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)){
  232. value=String::chr(code[idx]);
  233. idx++;
  234. return TK_SYMBOL;
  235. }
  236. if (code[idx]=='-' || (code[idx]>='0' && code[idx]<='9')) {
  237. //a number
  238. const CharType *rptr;
  239. double number = String::to_double(&code[idx],&rptr);
  240. idx+=(rptr - &code[idx]);
  241. value=number;
  242. return TK_NUMBER;
  243. } else if ((code[idx]>='A' && code[idx]<='Z') || (code[idx]>='a' && code[idx]<='z') || code[idx]>127) {
  244. String id;
  245. while((code[idx]>='A' && code[idx]<='Z') || (code[idx]>='a' && code[idx]<='z') || code[idx]>127) {
  246. id+=code[idx];
  247. idx++;
  248. }
  249. value=id;
  250. return TK_IDENTIFIER;
  251. } else {
  252. error_str="Unexpected character.";
  253. error=true;
  254. return TK_ERROR;
  255. }
  256. }
  257. }
  258. }
  259. }
  260. public:
  261. Error parse(const String& p_code,const String& p_known_class_name=String()) {
  262. code=p_code;
  263. idx=0;
  264. line=0;
  265. error_str=String();
  266. error=false;
  267. value=Variant();
  268. class_name=String();
  269. bool use_next_class=false;
  270. Token tk = get_token();
  271. Map<int,String> namespace_stack;
  272. int curly_stack=0;
  273. while(!error || tk!=TK_EOF) {
  274. if (tk==TK_BRACKET_OPEN) {
  275. tk = get_token();
  276. if (tk==TK_IDENTIFIER && String(value)=="ScriptClass") {
  277. if (get_token()==TK_BRACKET_CLOSE) {
  278. use_next_class=true;
  279. }
  280. }
  281. } else if (tk==TK_IDENTIFIER && String(value)=="class") {
  282. tk = get_token();
  283. if (tk==TK_IDENTIFIER) {
  284. String name = value;
  285. if (use_next_class || p_known_class_name==name) {
  286. for (Map<int,String>::Element *E=namespace_stack.front();E;E=E->next()) {
  287. class_name+=E->get()+".";
  288. }
  289. class_name+=String(value);
  290. break;
  291. }
  292. }
  293. } else if (tk==TK_IDENTIFIER && String(value)=="namespace") {
  294. String name;
  295. int at_level = curly_stack;
  296. while(true) {
  297. tk = get_token();
  298. if (tk==TK_IDENTIFIER) {
  299. name+=String(value);
  300. }
  301. tk = get_token();
  302. if (tk==TK_PERIOD) {
  303. name+=".";
  304. } else if (tk==TK_CURLY_BRACKET_OPEN) {
  305. curly_stack++;
  306. break;
  307. } else {
  308. break; //whathever else
  309. }
  310. }
  311. if (name!=String()) {
  312. namespace_stack[at_level]=name;
  313. }
  314. } else if (tk==TK_CURLY_BRACKET_OPEN) {
  315. curly_stack++;
  316. } else if (tk==TK_CURLY_BRACKET_CLOSE) {
  317. curly_stack--;
  318. if (namespace_stack.has(curly_stack)) {
  319. namespace_stack.erase(curly_stack);
  320. }
  321. }
  322. tk = get_token();
  323. }
  324. if (error)
  325. return ERR_PARSE_ERROR;
  326. return OK;
  327. }
  328. String get_error() {
  329. return error_str;
  330. }
  331. String get_class() {
  332. return class_name;
  333. }
  334. };
  335. void test_vec(Plane p_vec) {
  336. CameraMatrix cm;
  337. cm.set_perspective(45,1,0,100);
  338. Plane v0=cm.xform4(p_vec);
  339. print_line("out: "+v0);
  340. v0.normal.z = (v0.d/100.0 *2.0-1.0) * v0.d;
  341. print_line("out_F: "+v0);
  342. /*v0: 0, 0, -0.1, 0.1
  343. v1: 0, 0, 0, 0.1
  344. fix: 0, 0, 0, 0.1
  345. v0: 0, 0, 1.302803, 1.5
  346. v1: 0, 0, 1.401401, 1.5
  347. fix: 0, 0, 1.401401, 1.5
  348. v0: 0, 0, 25.851850, 26
  349. v1: 0, 0, 25.925926, 26
  350. fix: 0, 0, 25.925924, 26
  351. v0: 0, 0, 49.899902, 50
  352. v1: 0, 0, 49.949947, 50
  353. fix: 0, 0, 49.949951, 50
  354. v0: 0, 0, 100, 100
  355. v1: 0, 0, 100, 100
  356. fix: 0, 0, 100, 100
  357. */
  358. }
  359. uint32_t ihash( uint32_t a)
  360. {
  361. a = (a+0x7ed55d16) + (a<<12);
  362. a = (a^0xc761c23c) ^ (a>>19);
  363. a = (a+0x165667b1) + (a<<5);
  364. a = (a+0xd3a2646c) ^ (a<<9);
  365. a = (a+0xfd7046c5) + (a<<3);
  366. a = (a^0xb55a4f09) ^ (a>>16);
  367. return a;
  368. }
  369. uint32_t ihash2( uint32_t a) {
  370. a = (a ^ 61) ^ (a >> 16);
  371. a = a + (a << 3);
  372. a = a ^ (a >> 4);
  373. a = a * 0x27d4eb2d;
  374. a = a ^ (a >> 15);
  375. return a;
  376. }
  377. uint32_t ihash3( uint32_t a)
  378. {
  379. a = (a+0x479ab41d) + (a<<8);
  380. a = (a^0xe4aa10ce) ^ (a>>5);
  381. a = (a+0x9942f0a6) - (a<<14);
  382. a = (a^0x5aedd67d) ^ (a>>3);
  383. a = (a+0x17bea992) + (a<<7);
  384. return a;
  385. }
  386. MainLoop* test() {
  387. List<String> cmdlargs = OS::get_singleton()->get_cmdline_args();
  388. if (cmdlargs.empty()) {
  389. //try editor!
  390. return NULL;
  391. }
  392. String test = cmdlargs.back()->get();
  393. FileAccess *fa = FileAccess::open(test,FileAccess::READ);
  394. if (!fa) {
  395. ERR_EXPLAIN("Could not open file: "+test);
  396. ERR_FAIL_V(NULL);
  397. }
  398. Vector<uint8_t> buf;
  399. int flen = fa->get_len();
  400. buf.resize(fa->get_len()+1);
  401. fa->get_buffer(&buf[0],flen);
  402. buf[flen]=0;
  403. String code;
  404. code.parse_utf8((const char*)&buf[0]);
  405. GetClassAndNamespace getclass;
  406. if (getclass.parse(code)) {
  407. print_line("Parse error: "+getclass.get_error());
  408. } else {
  409. print_line("Found class: "+getclass.get_class());
  410. }
  411. return NULL;
  412. {
  413. Vector<int> hashes;
  414. List<StringName> tl;
  415. ClassDB::get_class_list(&tl);
  416. for (List<StringName>::Element *E=tl.front();E;E=E->next()) {
  417. Vector<uint8_t> m5b = E->get().operator String().md5_buffer();
  418. hashes.push_back(hashes.size());
  419. }
  420. //hashes.resize(50);
  421. for(int i=nearest_shift(hashes.size());i<20;i++) {
  422. bool success=true;
  423. for(int s=0;s<10000;s++) {
  424. Set<uint32_t> existing;
  425. success=true;
  426. for(int j=0;j<hashes.size();j++) {
  427. uint32_t eh = ihash2(ihash3(hashes[j]+ihash(s)+s))&((1<<i)-1);
  428. if (existing.has(eh)) {
  429. success=false;
  430. break;
  431. }
  432. existing.insert(eh);
  433. }
  434. if (success) {
  435. print_line("success at "+itos(i)+"/"+itos(nearest_shift(hashes.size()))+" shift "+itos(s));
  436. break;
  437. }
  438. }
  439. if (success)
  440. break;
  441. }
  442. print_line("DONE");
  443. return NULL;
  444. }
  445. {
  446. // print_line("NUM: "+itos(237641278346127));
  447. print_line("NUM: "+itos(-128));
  448. return NULL;
  449. }
  450. {
  451. Vector3 v(1,2,3);
  452. v.normalize();
  453. float a=0.3;
  454. //Quat q(v,a);
  455. Matrix3 m(v,a);
  456. Vector3 v2(7,3,1);
  457. v2.normalize();
  458. float a2=0.8;
  459. //Quat q(v,a);
  460. Matrix3 m2(v2,a2);
  461. Quat q=m;
  462. Quat q2=m2;
  463. Matrix3 m3 = m.inverse() * m2;
  464. Quat q3 = (q.inverse() * q2);//.normalized();
  465. print_line(Quat(m3));
  466. print_line(q3);
  467. print_line("before v: "+v+" a: "+rtos(a));
  468. q.get_axis_and_angle(v,a);
  469. print_line("after v: "+v+" a: "+rtos(a));
  470. }
  471. return NULL;
  472. String ret;
  473. List<String> args;
  474. args.push_back("-l");
  475. Error err = OS::get_singleton()->execute("/bin/ls",args,true,NULL,&ret);
  476. print_line("error: "+itos(err));
  477. print_line(ret);
  478. return NULL;
  479. Matrix3 m3;
  480. m3.rotate(Vector3(1,0,0),0.2);
  481. m3.rotate(Vector3(0,1,0),1.77);
  482. m3.rotate(Vector3(0,0,1),212);
  483. Matrix3 m32;
  484. m32.set_euler(m3.get_euler());
  485. print_line("ELEULEEEEEEEEEEEEEEEEEER: "+m3.get_euler()+" vs "+m32.get_euler());
  486. return NULL;
  487. {
  488. Dictionary d;
  489. d["momo"]=1;
  490. Dictionary b=d;
  491. b["44"]=4;
  492. }
  493. return NULL;
  494. print_line("inters: "+rtos(Geometry::segment_intersects_circle(Vector2(-5,0),Vector2(-2,0),Vector2(),1.0)));
  495. print_line("cross: "+Vector3(1,2,3).cross(Vector3(4,5,7)));
  496. print_line("dot: "+rtos(Vector3(1,2,3).dot(Vector3(4,5,7))));
  497. print_line("abs: "+Vector3(-1,2,-3).abs());
  498. print_line("distance_to: "+rtos(Vector3(1,2,3).distance_to(Vector3(4,5,7))));
  499. print_line("distance_squared_to: "+rtos(Vector3(1,2,3).distance_squared_to(Vector3(4,5,7))));
  500. print_line("plus: "+(Vector3(1,2,3)+Vector3(Vector3(4,5,7))));
  501. print_line("minus: "+(Vector3(1,2,3)-Vector3(Vector3(4,5,7))));
  502. print_line("mul: "+(Vector3(1,2,3)*Vector3(Vector3(4,5,7))));
  503. print_line("div: "+(Vector3(1,2,3)/Vector3(Vector3(4,5,7))));
  504. print_line("mul scalar: "+(Vector3(1,2,3)*2));
  505. print_line("premul scalar: "+(2*Vector3(1,2,3)));
  506. print_line("div scalar: "+(Vector3(1,2,3)/3.0));
  507. print_line("length: "+rtos(Vector3(1,2,3).length()));
  508. print_line("length squared: "+rtos(Vector3(1,2,3).length_squared()));
  509. print_line("normalized: "+Vector3(1,2,3).normalized());
  510. print_line("inverse: "+Vector3(1,2,3).inverse());
  511. {
  512. Vector3 v(4,5,7);
  513. v.normalize();
  514. print_line("normalize: "+v);
  515. }
  516. {
  517. Vector3 v(4,5,7);
  518. v+=Vector3(1,2,3);
  519. print_line("+=: "+v);
  520. }
  521. {
  522. Vector3 v(4,5,7);
  523. v-=Vector3(1,2,3);
  524. print_line("-=: "+v);
  525. }
  526. {
  527. Vector3 v(4,5,7);
  528. v*=Vector3(1,2,3);
  529. print_line("*=: "+v);
  530. }
  531. {
  532. Vector3 v(4,5,7);
  533. v/=Vector3(1,2,3);
  534. print_line("/=: "+v);
  535. }
  536. {
  537. Vector3 v(4,5,7);
  538. v*=2.0;
  539. print_line("scalar *=: "+v);
  540. }
  541. {
  542. Vector3 v(4,5,7);
  543. v/=2.0;
  544. print_line("scalar /=: "+v);
  545. }
  546. #if 0
  547. print_line(String("C:\\momo\\.\\popo\\..\\gongo").simplify_path());
  548. print_line(String("res://../popo/..//gongo").simplify_path());
  549. print_line(String("res://..").simplify_path());
  550. DVector<uint8_t> a;
  551. DVector<uint8_t> b;
  552. a.resize(20);
  553. b=a;
  554. b.resize(30);
  555. a=b;
  556. #endif
  557. #if 0
  558. String za = String::utf8("á");
  559. printf("unicode: %x\n",za[0]);
  560. CharString cs=za.utf8();
  561. for(int i=0;i<cs.size();i++) {
  562. uint32_t v = uint8_t(cs[i]);
  563. printf("%i - %x\n",i,v);
  564. }
  565. return NULL;
  566. print_line(String("C:\\window\\system\\momo").path_to("C:\\window\\momonga"));
  567. print_line(String("res://momo/sampler").path_to("res://pindonga"));
  568. print_line(String("/margarito/terere").path_to("/margarito/pilates"));
  569. print_line(String("/algo").path_to("/algo"));
  570. print_line(String("c:").path_to("c:\\"));
  571. print_line(String("/").path_to("/"));
  572. print_line(itos(sizeof(Variant)));
  573. return NULL;
  574. Vector<StringName> path;
  575. path.push_back("three");
  576. path.push_back("two");
  577. path.push_back("one");
  578. path.push_back("comeon");
  579. path.revert();
  580. NodePath np(path,true);
  581. print_line(np);
  582. return NULL;
  583. bool a=2;
  584. print_line(Variant(a));
  585. Matrix32 mat2_1;
  586. mat2_1.rotate(0.5);
  587. Matrix32 mat2_2;
  588. mat2_2.translate(Vector2(1,2));
  589. Matrix32 mat2_3 = mat2_1 * mat2_2;
  590. mat2_3.affine_invert();
  591. print_line(mat2_3.elements[0]);
  592. print_line(mat2_3.elements[1]);
  593. print_line(mat2_3.elements[2]);
  594. Transform mat3_1;
  595. mat3_1.basis.rotate(Vector3(0,0,1),0.5);
  596. Transform mat3_2;
  597. mat3_2.translate(Vector3(1,2,0));
  598. Transform mat3_3 = mat3_1 * mat3_2;
  599. mat3_3.affine_invert();
  600. print_line(mat3_3.basis.get_axis(0));
  601. print_line(mat3_3.basis.get_axis(1));
  602. print_line(mat3_3.origin);
  603. #endif
  604. return NULL;
  605. }
  606. }