test_math.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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-2015 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. namespace TestMath {
  44. void test_vec(Plane p_vec) {
  45. CameraMatrix cm;
  46. cm.set_perspective(45,1,0,100);
  47. Plane v0=cm.xform4(p_vec);
  48. print_line("out: "+v0);
  49. v0.normal.z = (v0.d/100.0 *2.0-1.0) * v0.d;
  50. print_line("out_F: "+v0);
  51. /*v0: 0, 0, -0.1, 0.1
  52. v1: 0, 0, 0, 0.1
  53. fix: 0, 0, 0, 0.1
  54. v0: 0, 0, 1.302803, 1.5
  55. v1: 0, 0, 1.401401, 1.5
  56. fix: 0, 0, 1.401401, 1.5
  57. v0: 0, 0, 25.851850, 26
  58. v1: 0, 0, 25.925926, 26
  59. fix: 0, 0, 25.925924, 26
  60. v0: 0, 0, 49.899902, 50
  61. v1: 0, 0, 49.949947, 50
  62. fix: 0, 0, 49.949951, 50
  63. v0: 0, 0, 100, 100
  64. v1: 0, 0, 100, 100
  65. fix: 0, 0, 100, 100
  66. */
  67. }
  68. MainLoop* test() {
  69. {
  70. // print_line("NUM: "+itos(237641278346127));
  71. print_line("NUM: "+itos(-128));
  72. return NULL;
  73. }
  74. {
  75. Vector3 v(1,2,3);
  76. v.normalize();
  77. float a=0.3;
  78. //Quat q(v,a);
  79. Matrix3 m(v,a);
  80. Vector3 v2(7,3,1);
  81. v2.normalize();
  82. float a2=0.8;
  83. //Quat q(v,a);
  84. Matrix3 m2(v2,a2);
  85. Quat q=m;
  86. Quat q2=m2;
  87. Matrix3 m3 = m.inverse() * m2;
  88. Quat q3 = (q.inverse() * q2);//.normalized();
  89. print_line(Quat(m3));
  90. print_line(q3);
  91. print_line("before v: "+v+" a: "+rtos(a));
  92. q.get_axis_and_angle(v,a);
  93. print_line("after v: "+v+" a: "+rtos(a));
  94. }
  95. return NULL;
  96. String ret;
  97. List<String> args;
  98. args.push_back("-l");
  99. Error err = OS::get_singleton()->execute("/bin/ls",args,true,NULL,&ret);
  100. print_line("error: "+itos(err));
  101. print_line(ret);
  102. return NULL;
  103. Matrix3 m3;
  104. m3.rotate(Vector3(1,0,0),0.2);
  105. m3.rotate(Vector3(0,1,0),1.77);
  106. m3.rotate(Vector3(0,0,1),212);
  107. Matrix3 m32;
  108. m32.set_euler(m3.get_euler());
  109. print_line("ELEULEEEEEEEEEEEEEEEEEER: "+m3.get_euler()+" vs "+m32.get_euler());
  110. return NULL;
  111. {
  112. Dictionary d;
  113. d["momo"]=1;
  114. Dictionary b=d;
  115. b["44"]=4;
  116. }
  117. return NULL;
  118. print_line("inters: "+rtos(Geometry::segment_intersects_circle(Vector2(-5,0),Vector2(-2,0),Vector2(),1.0)));
  119. print_line("cross: "+Vector3(1,2,3).cross(Vector3(4,5,7)));
  120. print_line("dot: "+rtos(Vector3(1,2,3).dot(Vector3(4,5,7))));
  121. print_line("abs: "+Vector3(-1,2,-3).abs());
  122. print_line("distance_to: "+rtos(Vector3(1,2,3).distance_to(Vector3(4,5,7))));
  123. print_line("distance_squared_to: "+rtos(Vector3(1,2,3).distance_squared_to(Vector3(4,5,7))));
  124. print_line("plus: "+(Vector3(1,2,3)+Vector3(Vector3(4,5,7))));
  125. print_line("minus: "+(Vector3(1,2,3)-Vector3(Vector3(4,5,7))));
  126. print_line("mul: "+(Vector3(1,2,3)*Vector3(Vector3(4,5,7))));
  127. print_line("div: "+(Vector3(1,2,3)/Vector3(Vector3(4,5,7))));
  128. print_line("mul scalar: "+(Vector3(1,2,3)*2));
  129. print_line("premul scalar: "+(2*Vector3(1,2,3)));
  130. print_line("div scalar: "+(Vector3(1,2,3)/3.0));
  131. print_line("length: "+rtos(Vector3(1,2,3).length()));
  132. print_line("length squared: "+rtos(Vector3(1,2,3).length_squared()));
  133. print_line("normalized: "+Vector3(1,2,3).normalized());
  134. print_line("inverse: "+Vector3(1,2,3).inverse());
  135. {
  136. Vector3 v(4,5,7);
  137. v.normalize();
  138. print_line("normalize: "+v);
  139. }
  140. {
  141. Vector3 v(4,5,7);
  142. v+=Vector3(1,2,3);
  143. print_line("+=: "+v);
  144. }
  145. {
  146. Vector3 v(4,5,7);
  147. v-=Vector3(1,2,3);
  148. print_line("-=: "+v);
  149. }
  150. {
  151. Vector3 v(4,5,7);
  152. v*=Vector3(1,2,3);
  153. print_line("*=: "+v);
  154. }
  155. {
  156. Vector3 v(4,5,7);
  157. v/=Vector3(1,2,3);
  158. print_line("/=: "+v);
  159. }
  160. {
  161. Vector3 v(4,5,7);
  162. v*=2.0;
  163. print_line("scalar *=: "+v);
  164. }
  165. {
  166. Vector3 v(4,5,7);
  167. v/=2.0;
  168. print_line("scalar /=: "+v);
  169. }
  170. #if 0
  171. print_line(String("C:\\momo\\.\\popo\\..\\gongo").simplify_path());
  172. print_line(String("res://../popo/..//gongo").simplify_path());
  173. print_line(String("res://..").simplify_path());
  174. DVector<uint8_t> a;
  175. DVector<uint8_t> b;
  176. a.resize(20);
  177. b=a;
  178. b.resize(30);
  179. a=b;
  180. #endif
  181. #if 0
  182. String za = String::utf8("á");
  183. printf("unicode: %x\n",za[0]);
  184. CharString cs=za.utf8();
  185. for(int i=0;i<cs.size();i++) {
  186. uint32_t v = uint8_t(cs[i]);
  187. printf("%i - %x\n",i,v);
  188. }
  189. return NULL;
  190. print_line(String("C:\\window\\system\\momo").path_to("C:\\window\\momonga"));
  191. print_line(String("res://momo/sampler").path_to("res://pindonga"));
  192. print_line(String("/margarito/terere").path_to("/margarito/pilates"));
  193. print_line(String("/algo").path_to("/algo"));
  194. print_line(String("c:").path_to("c:\\"));
  195. print_line(String("/").path_to("/"));
  196. print_line(itos(sizeof(Variant)));
  197. return NULL;
  198. Vector<StringName> path;
  199. path.push_back("three");
  200. path.push_back("two");
  201. path.push_back("one");
  202. path.push_back("comeon");
  203. path.revert();
  204. NodePath np(path,true);
  205. print_line(np);
  206. return NULL;
  207. bool a=2;
  208. print_line(Variant(a));
  209. Matrix32 mat2_1;
  210. mat2_1.rotate(0.5);
  211. Matrix32 mat2_2;
  212. mat2_2.translate(Vector2(1,2));
  213. Matrix32 mat2_3 = mat2_1 * mat2_2;
  214. mat2_3.affine_invert();
  215. print_line(mat2_3.elements[0]);
  216. print_line(mat2_3.elements[1]);
  217. print_line(mat2_3.elements[2]);
  218. Transform mat3_1;
  219. mat3_1.basis.rotate(Vector3(0,0,1),0.5);
  220. Transform mat3_2;
  221. mat3_2.translate(Vector3(1,2,0));
  222. Transform mat3_3 = mat3_1 * mat3_2;
  223. mat3_3.affine_invert();
  224. print_line(mat3_3.basis.get_axis(0));
  225. print_line(mat3_3.basis.get_axis(1));
  226. print_line(mat3_3.origin);
  227. #endif
  228. return NULL;
  229. }
  230. }