diff options
Diffstat (limited to 'main/tests/test_math.cpp')
-rw-r--r-- | main/tests/test_math.cpp | 124 |
1 files changed, 81 insertions, 43 deletions
diff --git a/main/tests/test_math.cpp b/main/tests/test_math.cpp index d91503501d..5f84bad4e9 100644 --- a/main/tests/test_math.cpp +++ b/main/tests/test_math.cpp @@ -32,8 +32,11 @@ #include "core/math/basis.h" #include "core/math/camera_matrix.h" +#include "core/math/delaunay_3d.h" +#include "core/math/geometry_2d.h" #include "core/math/math_funcs.h" #include "core/math/transform.h" +#include "core/method_ptrcall.h" #include "core/os/file_access.h" #include "core/os/keyboard.h" #include "core/os/os.h" @@ -43,14 +46,11 @@ #include "core/vmap.h" #include "scene/main/node.h" #include "scene/resources/texture.h" -#include "servers/visual/shader_language.h" - -#include "core/method_ptrcall.h" +#include "servers/rendering/shader_language.h" namespace TestMath { class GetClassAndNamespace { - String code; int idx; int line; @@ -77,12 +77,9 @@ class GetClassAndNamespace { }; Token get_token() { - while (true) { switch (code[idx]) { - case '\n': { - line++; idx++; break; @@ -92,37 +89,30 @@ class GetClassAndNamespace { } break; case '{': { - idx++; return TK_CURLY_BRACKET_OPEN; }; case '}': { - idx++; return TK_CURLY_BRACKET_CLOSE; }; case '[': { - idx++; return TK_BRACKET_OPEN; }; case ']': { - idx++; return TK_BRACKET_CLOSE; }; case ':': { - idx++; return TK_COLON; }; case ',': { - idx++; return TK_COMMA; }; case '.': { - idx++; return TK_PERIOD; }; @@ -134,7 +124,6 @@ class GetClassAndNamespace { continue; } break; case '/': { - switch (code[idx + 1]) { case '*': { // block comment @@ -145,7 +134,6 @@ class GetClassAndNamespace { error = true; return TK_ERROR; } else if (code[idx] == '*' && code[idx + 1] == '/') { - idx += 2; break; } else if (code[idx] == '\n') { @@ -174,7 +162,6 @@ class GetClassAndNamespace { } break; case '\'': case '"': { - CharType begin_str = code[idx]; idx++; String tk_string = String(); @@ -198,15 +185,24 @@ class GetClassAndNamespace { CharType res = 0; switch (next) { - - case 'b': res = 8; break; - case 't': res = 9; break; - case 'n': res = 10; break; - case 'f': res = 12; break; + case 'b': + res = 8; + break; + case 't': + res = 9; + break; + case 'n': + res = 10; + break; + case 'f': + res = 12; + break; case 'r': res = 13; break; - case '\"': res = '\"'; break; + case '\"': + res = '\"'; + break; case '\\': res = '\\'; break; @@ -218,8 +214,9 @@ class GetClassAndNamespace { tk_string += res; } else { - if (code[idx] == '\n') + if (code[idx] == '\n') { line++; + } tk_string += code[idx]; } idx++; @@ -231,7 +228,6 @@ class GetClassAndNamespace { } break; default: { - if (code[idx] <= 32) { idx++; break; @@ -252,11 +248,9 @@ class GetClassAndNamespace { return TK_NUMBER; } else if ((code[idx] >= 'A' && code[idx] <= 'Z') || (code[idx] >= 'a' && code[idx] <= 'z') || code[idx] > 127) { - String id; while ((code[idx] >= 'A' && code[idx] <= 'Z') || (code[idx] >= 'a' && code[idx] <= 'z') || code[idx] > 127) { - id += code[idx]; idx++; } @@ -275,7 +269,6 @@ class GetClassAndNamespace { public: Error parse(const String &p_code, const String &p_known_class_name = String()) { - code = p_code; idx = 0; line = 0; @@ -291,7 +284,6 @@ public: int curly_stack = 0; while (!error || tk != TK_EOF) { - if (tk == TK_BRACKET_OPEN) { tk = get_token(); if (tk == TK_IDENTIFIER && String(value) == "ScriptClass") { @@ -348,8 +340,9 @@ public: tk = get_token(); } - if (error) + if (error) { return ERR_PARSE_ERROR; + } return OK; } @@ -364,7 +357,6 @@ public: }; void test_vec(Plane p_vec) { - CameraMatrix cm; cm.set_perspective(45, 1, 0, 100); Plane v0 = cm.xform4(p_vec); @@ -403,6 +395,54 @@ uint32_t ihash3(uint32_t a) { } MainLoop *test() { + { + Vector<Vector3> points; + points.push_back(Vector3(0, 0, 0)); + points.push_back(Vector3(0, 0, 1)); + points.push_back(Vector3(0, 1, 0)); + points.push_back(Vector3(0, 1, 1)); + points.push_back(Vector3(1, 1, 0)); + points.push_back(Vector3(1, 0, 0)); + points.push_back(Vector3(1, 0, 1)); + points.push_back(Vector3(1, 1, 1)); + + for (int i = 0; i < 800; i++) { + points.push_back(Vector3(Math::randf() * 2.0 - 1.0, Math::randf() * 2.0 - 1.0, Math::randf() * 2.0 - 1.0) * Vector3(25, 30, 33)); + } + + Vector<Delaunay3D::OutputSimplex> os = Delaunay3D::tetrahedralize(points); + print_line("simplices in the end: " + itos(os.size())); + for (int i = 0; i < os.size(); i++) { + print_line("Simplex " + itos(i) + ": "); + print_line(points[os[i].points[0]]); + print_line(points[os[i].points[1]]); + print_line(points[os[i].points[2]]); + print_line(points[os[i].points[3]]); + } + + { + FileAccessRef f = FileAccess::open("res://bsp.obj", FileAccess::WRITE); + for (int i = 0; i < os.size(); i++) { + f->store_line("o Simplex" + itos(i)); + for (int j = 0; j < 4; j++) { + f->store_line(vformat("v %f %f %f", points[os[i].points[j]].x, points[os[i].points[j]].y, points[os[i].points[j]].z)); + } + static const int face_order[4][3] = { + { 1, 2, 3 }, + { 1, 3, 4 }, + { 1, 2, 4 }, + { 2, 3, 4 } + }; + + for (int j = 0; j < 4; j++) { + f->store_line(vformat("f %d %d %d", 4 * i + face_order[j][0], 4 * i + face_order[j][1], 4 * i + face_order[j][2])); + } + } + f->close(); + } + + return nullptr; + } { float r = 1; @@ -443,7 +483,7 @@ MainLoop *test() { float gb = (rgbe >> 9) & 0x1ff; float bb = (rgbe >> 18) & 0x1ff; float eb = (rgbe >> 27); - float mb = Math::pow(2, eb - 15.0 - 9.0); + float mb = Math::pow(2.0, eb - 15.0 - 9.0); float rd = rb * mb; float gd = gb * mb; float bd = bb * mb; @@ -475,18 +515,18 @@ MainLoop *test() { if (cmdlargs.empty()) { //try editor! - return NULL; + return nullptr; } String test = cmdlargs.back()->get(); if (test == "math") { // Not a file name but the test name, abort. // FIXME: This test is ugly as heck, needs fixing :) - return NULL; + return nullptr; } FileAccess *fa = FileAccess::open(test, FileAccess::READ); - ERR_FAIL_COND_V_MSG(!fa, NULL, "Could not open file: " + test); + ERR_FAIL_COND_V_MSG(!fa, nullptr, "Could not open file: " + test); Vector<uint8_t> buf; int flen = fa->get_len(); @@ -505,26 +545,22 @@ MainLoop *test() { } { - Vector<int> hashes; List<StringName> tl; ClassDB::get_class_list(&tl); for (List<StringName>::Element *E = tl.front(); E; E = E->next()) { - Vector<uint8_t> m5b = E->get().operator String().md5_buffer(); hashes.push_back(hashes.size()); } for (int i = nearest_shift(hashes.size()); i < 20; i++) { - bool success = true; for (int s = 0; s < 10000; s++) { Set<uint32_t> existing; success = true; for (int j = 0; j < hashes.size(); j++) { - uint32_t eh = ihash2(ihash3(hashes[j] + ihash(s) + s)) & ((1 << i) - 1); if (existing.has(eh)) { success = false; @@ -538,8 +574,9 @@ MainLoop *test() { break; } } - if (success) + if (success) { break; + } } print_line("DONE"); @@ -580,7 +617,7 @@ MainLoop *test() { List<String> args; args.push_back("-l"); - Error err = OS::get_singleton()->execute("/bin/ls", args, true, NULL, &ret); + Error err = OS::get_singleton()->execute("/bin/ls", args, true, nullptr, &ret); print_line("error: " + itos(err)); print_line(ret); @@ -599,7 +636,7 @@ MainLoop *test() { b["44"] = 4; } - print_line("inters: " + rtos(Geometry::segment_intersects_circle(Vector2(-5, 0), Vector2(-2, 0), Vector2(), 1.0))); + print_line("inters: " + rtos(Geometry2D::segment_intersects_circle(Vector2(-5, 0), Vector2(-2, 0), Vector2(), 1.0))); print_line("cross: " + Vector3(1, 2, 3).cross(Vector3(4, 5, 7))); print_line("dot: " + rtos(Vector3(1, 2, 3).dot(Vector3(4, 5, 7)))); @@ -660,6 +697,7 @@ MainLoop *test() { print_line("scalar /=: " + v); } - return NULL; + return nullptr; } + } // namespace TestMath |