test_navigation_server_3d.h 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. /**************************************************************************/
  2. /* test_navigation_server_3d.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #ifndef TEST_NAVIGATION_SERVER_3D_H
  31. #define TEST_NAVIGATION_SERVER_3D_H
  32. #include "modules/navigation/nav_utils.h"
  33. #include "scene/3d/mesh_instance_3d.h"
  34. #include "scene/resources/3d/primitive_meshes.h"
  35. #include "servers/navigation_server_3d.h"
  36. namespace TestNavigationServer3D {
  37. // TODO: Find a more generic way to create `Callable` mocks.
  38. class CallableMock : public Object {
  39. GDCLASS(CallableMock, Object);
  40. public:
  41. void function1(Variant arg0) {
  42. function1_calls++;
  43. function1_latest_arg0 = arg0;
  44. }
  45. unsigned function1_calls{ 0 };
  46. Variant function1_latest_arg0;
  47. };
  48. static inline Array build_array() {
  49. return Array();
  50. }
  51. template <typename... Targs>
  52. static inline Array build_array(Variant item, Targs... Fargs) {
  53. Array a = build_array(Fargs...);
  54. a.push_front(item);
  55. return a;
  56. }
  57. struct GreaterThan {
  58. bool operator()(int p_a, int p_b) const { return p_a > p_b; }
  59. };
  60. struct CompareArrayValues {
  61. const int *array;
  62. CompareArrayValues(const int *p_array) :
  63. array(p_array) {}
  64. bool operator()(uint32_t p_index_a, uint32_t p_index_b) const {
  65. return array[p_index_a] < array[p_index_b];
  66. }
  67. };
  68. struct RegisterHeapIndexes {
  69. uint32_t *indexes;
  70. RegisterHeapIndexes(uint32_t *p_indexes) :
  71. indexes(p_indexes) {}
  72. void operator()(uint32_t p_vector_index, uint32_t p_heap_index) {
  73. indexes[p_vector_index] = p_heap_index;
  74. }
  75. };
  76. TEST_SUITE("[Navigation]") {
  77. TEST_CASE("[NavigationServer3D] Server should be empty when initialized") {
  78. NavigationServer3D *navigation_server = NavigationServer3D::get_singleton();
  79. CHECK_EQ(navigation_server->get_maps().size(), 0);
  80. SUBCASE("'ProcessInfo' should report all counters empty as well") {
  81. CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_ACTIVE_MAPS), 0);
  82. CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_REGION_COUNT), 0);
  83. CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_AGENT_COUNT), 0);
  84. CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_LINK_COUNT), 0);
  85. CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_POLYGON_COUNT), 0);
  86. CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_EDGE_COUNT), 0);
  87. CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_EDGE_MERGE_COUNT), 0);
  88. CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_EDGE_CONNECTION_COUNT), 0);
  89. CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_EDGE_FREE_COUNT), 0);
  90. }
  91. }
  92. TEST_CASE("[NavigationServer3D] Server should manage agent properly") {
  93. NavigationServer3D *navigation_server = NavigationServer3D::get_singleton();
  94. RID agent = navigation_server->agent_create();
  95. CHECK(agent.is_valid());
  96. SUBCASE("'ProcessInfo' should not report dangling agent") {
  97. CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_AGENT_COUNT), 0);
  98. }
  99. SUBCASE("Setters/getters should work") {
  100. bool initial_use_3d_avoidance = navigation_server->agent_get_use_3d_avoidance(agent);
  101. navigation_server->agent_set_use_3d_avoidance(agent, !initial_use_3d_avoidance);
  102. navigation_server->process(0.0); // Give server some cycles to commit.
  103. CHECK_EQ(navigation_server->agent_get_use_3d_avoidance(agent), !initial_use_3d_avoidance);
  104. // TODO: Add remaining setters/getters once the missing getters are added.
  105. }
  106. SUBCASE("'ProcessInfo' should report agent with active map") {
  107. RID map = navigation_server->map_create();
  108. CHECK(map.is_valid());
  109. navigation_server->map_set_active(map, true);
  110. navigation_server->agent_set_map(agent, map);
  111. navigation_server->process(0.0); // Give server some cycles to commit.
  112. CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_AGENT_COUNT), 1);
  113. navigation_server->agent_set_map(agent, RID());
  114. navigation_server->free(map);
  115. navigation_server->process(0.0); // Give server some cycles to commit.
  116. CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_AGENT_COUNT), 0);
  117. }
  118. navigation_server->free(agent);
  119. }
  120. TEST_CASE("[NavigationServer3D] Server should manage map properly") {
  121. NavigationServer3D *navigation_server = NavigationServer3D::get_singleton();
  122. RID map;
  123. CHECK_FALSE(map.is_valid());
  124. SUBCASE("Queries against invalid map should return empty or invalid values") {
  125. ERR_PRINT_OFF;
  126. CHECK_EQ(navigation_server->map_get_closest_point(map, Vector3(7, 7, 7)), Vector3());
  127. CHECK_EQ(navigation_server->map_get_closest_point_normal(map, Vector3(7, 7, 7)), Vector3());
  128. CHECK_FALSE(navigation_server->map_get_closest_point_owner(map, Vector3(7, 7, 7)).is_valid());
  129. CHECK_EQ(navigation_server->map_get_closest_point_to_segment(map, Vector3(7, 7, 7), Vector3(8, 8, 8), true), Vector3());
  130. CHECK_EQ(navigation_server->map_get_closest_point_to_segment(map, Vector3(7, 7, 7), Vector3(8, 8, 8), false), Vector3());
  131. CHECK_EQ(navigation_server->map_get_path(map, Vector3(7, 7, 7), Vector3(8, 8, 8), true).size(), 0);
  132. CHECK_EQ(navigation_server->map_get_path(map, Vector3(7, 7, 7), Vector3(8, 8, 8), false).size(), 0);
  133. Ref<NavigationPathQueryParameters3D> query_parameters = memnew(NavigationPathQueryParameters3D);
  134. query_parameters->set_map(map);
  135. query_parameters->set_start_position(Vector3(7, 7, 7));
  136. query_parameters->set_target_position(Vector3(8, 8, 8));
  137. Ref<NavigationPathQueryResult3D> query_result = memnew(NavigationPathQueryResult3D);
  138. navigation_server->query_path(query_parameters, query_result);
  139. CHECK_EQ(query_result->get_path().size(), 0);
  140. CHECK_EQ(query_result->get_path_types().size(), 0);
  141. CHECK_EQ(query_result->get_path_rids().size(), 0);
  142. CHECK_EQ(query_result->get_path_owner_ids().size(), 0);
  143. ERR_PRINT_ON;
  144. }
  145. map = navigation_server->map_create();
  146. CHECK(map.is_valid());
  147. CHECK_EQ(navigation_server->get_maps().size(), 1);
  148. SUBCASE("'ProcessInfo' should not report inactive map") {
  149. CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_ACTIVE_MAPS), 0);
  150. }
  151. SUBCASE("Setters/getters should work") {
  152. navigation_server->map_set_cell_size(map, 0.55);
  153. navigation_server->map_set_edge_connection_margin(map, 0.66);
  154. navigation_server->map_set_link_connection_radius(map, 0.77);
  155. navigation_server->map_set_up(map, Vector3(1, 0, 0));
  156. bool initial_use_edge_connections = navigation_server->map_get_use_edge_connections(map);
  157. navigation_server->map_set_use_edge_connections(map, !initial_use_edge_connections);
  158. navigation_server->process(0.0); // Give server some cycles to commit.
  159. CHECK_EQ(navigation_server->map_get_cell_size(map), doctest::Approx(0.55));
  160. CHECK_EQ(navigation_server->map_get_edge_connection_margin(map), doctest::Approx(0.66));
  161. CHECK_EQ(navigation_server->map_get_link_connection_radius(map), doctest::Approx(0.77));
  162. CHECK_EQ(navigation_server->map_get_up(map), Vector3(1, 0, 0));
  163. CHECK_EQ(navigation_server->map_get_use_edge_connections(map), !initial_use_edge_connections);
  164. }
  165. SUBCASE("'ProcessInfo' should report map iff active") {
  166. navigation_server->map_set_active(map, true);
  167. navigation_server->process(0.0); // Give server some cycles to commit.
  168. CHECK(navigation_server->map_is_active(map));
  169. CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_ACTIVE_MAPS), 1);
  170. navigation_server->map_set_active(map, false);
  171. navigation_server->process(0.0); // Give server some cycles to commit.
  172. CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_ACTIVE_MAPS), 0);
  173. }
  174. SUBCASE("Number of agents should be reported properly") {
  175. RID agent = navigation_server->agent_create();
  176. CHECK(agent.is_valid());
  177. navigation_server->agent_set_map(agent, map);
  178. navigation_server->process(0.0); // Give server some cycles to commit.
  179. CHECK_EQ(navigation_server->map_get_agents(map).size(), 1);
  180. navigation_server->free(agent);
  181. navigation_server->process(0.0); // Give server some cycles to commit.
  182. CHECK_EQ(navigation_server->map_get_agents(map).size(), 0);
  183. }
  184. SUBCASE("Number of links should be reported properly") {
  185. RID link = navigation_server->link_create();
  186. CHECK(link.is_valid());
  187. navigation_server->link_set_map(link, map);
  188. navigation_server->process(0.0); // Give server some cycles to commit.
  189. CHECK_EQ(navigation_server->map_get_links(map).size(), 1);
  190. navigation_server->free(link);
  191. navigation_server->process(0.0); // Give server some cycles to commit.
  192. CHECK_EQ(navigation_server->map_get_links(map).size(), 0);
  193. }
  194. SUBCASE("Number of obstacles should be reported properly") {
  195. RID obstacle = navigation_server->obstacle_create();
  196. CHECK(obstacle.is_valid());
  197. navigation_server->obstacle_set_map(obstacle, map);
  198. navigation_server->process(0.0); // Give server some cycles to commit.
  199. CHECK_EQ(navigation_server->map_get_obstacles(map).size(), 1);
  200. navigation_server->free(obstacle);
  201. navigation_server->process(0.0); // Give server some cycles to commit.
  202. CHECK_EQ(navigation_server->map_get_obstacles(map).size(), 0);
  203. }
  204. SUBCASE("Number of regions should be reported properly") {
  205. RID region = navigation_server->region_create();
  206. CHECK(region.is_valid());
  207. navigation_server->region_set_map(region, map);
  208. navigation_server->process(0.0); // Give server some cycles to commit.
  209. CHECK_EQ(navigation_server->map_get_regions(map).size(), 1);
  210. navigation_server->free(region);
  211. navigation_server->process(0.0); // Give server some cycles to commit.
  212. CHECK_EQ(navigation_server->map_get_regions(map).size(), 0);
  213. }
  214. SUBCASE("Queries against empty map should return empty or invalid values") {
  215. navigation_server->map_set_active(map, true);
  216. navigation_server->process(0.0); // Give server some cycles to commit.
  217. ERR_PRINT_OFF;
  218. CHECK_EQ(navigation_server->map_get_closest_point(map, Vector3(7, 7, 7)), Vector3());
  219. CHECK_EQ(navigation_server->map_get_closest_point_normal(map, Vector3(7, 7, 7)), Vector3());
  220. CHECK_FALSE(navigation_server->map_get_closest_point_owner(map, Vector3(7, 7, 7)).is_valid());
  221. CHECK_EQ(navigation_server->map_get_closest_point_to_segment(map, Vector3(7, 7, 7), Vector3(8, 8, 8), true), Vector3());
  222. CHECK_EQ(navigation_server->map_get_closest_point_to_segment(map, Vector3(7, 7, 7), Vector3(8, 8, 8), false), Vector3());
  223. CHECK_EQ(navigation_server->map_get_path(map, Vector3(7, 7, 7), Vector3(8, 8, 8), true).size(), 0);
  224. CHECK_EQ(navigation_server->map_get_path(map, Vector3(7, 7, 7), Vector3(8, 8, 8), false).size(), 0);
  225. Ref<NavigationPathQueryParameters3D> query_parameters = memnew(NavigationPathQueryParameters3D);
  226. query_parameters->set_map(map);
  227. query_parameters->set_start_position(Vector3(7, 7, 7));
  228. query_parameters->set_target_position(Vector3(8, 8, 8));
  229. Ref<NavigationPathQueryResult3D> query_result = memnew(NavigationPathQueryResult3D);
  230. navigation_server->query_path(query_parameters, query_result);
  231. CHECK_EQ(query_result->get_path().size(), 0);
  232. CHECK_EQ(query_result->get_path_types().size(), 0);
  233. CHECK_EQ(query_result->get_path_rids().size(), 0);
  234. CHECK_EQ(query_result->get_path_owner_ids().size(), 0);
  235. ERR_PRINT_ON;
  236. navigation_server->map_set_active(map, false);
  237. navigation_server->process(0.0); // Give server some cycles to commit.
  238. }
  239. navigation_server->free(map);
  240. navigation_server->process(0.0); // Give server some cycles to actually remove map.
  241. CHECK_EQ(navigation_server->get_maps().size(), 0);
  242. }
  243. TEST_CASE("[NavigationServer3D] Server should manage link properly") {
  244. NavigationServer3D *navigation_server = NavigationServer3D::get_singleton();
  245. RID link = navigation_server->link_create();
  246. CHECK(link.is_valid());
  247. SUBCASE("'ProcessInfo' should not report dangling link") {
  248. CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_LINK_COUNT), 0);
  249. }
  250. SUBCASE("Setters/getters should work") {
  251. bool initial_bidirectional = navigation_server->link_is_bidirectional(link);
  252. navigation_server->link_set_bidirectional(link, !initial_bidirectional);
  253. navigation_server->link_set_end_position(link, Vector3(7, 7, 7));
  254. navigation_server->link_set_enter_cost(link, 0.55);
  255. navigation_server->link_set_navigation_layers(link, 6);
  256. navigation_server->link_set_owner_id(link, ObjectID((int64_t)7));
  257. navigation_server->link_set_start_position(link, Vector3(8, 8, 8));
  258. navigation_server->link_set_travel_cost(link, 0.66);
  259. navigation_server->process(0.0); // Give server some cycles to commit.
  260. CHECK_EQ(navigation_server->link_is_bidirectional(link), !initial_bidirectional);
  261. CHECK_EQ(navigation_server->link_get_end_position(link), Vector3(7, 7, 7));
  262. CHECK_EQ(navigation_server->link_get_enter_cost(link), doctest::Approx(0.55));
  263. CHECK_EQ(navigation_server->link_get_navigation_layers(link), 6);
  264. CHECK_EQ(navigation_server->link_get_owner_id(link), ObjectID((int64_t)7));
  265. CHECK_EQ(navigation_server->link_get_start_position(link), Vector3(8, 8, 8));
  266. CHECK_EQ(navigation_server->link_get_travel_cost(link), doctest::Approx(0.66));
  267. }
  268. SUBCASE("'ProcessInfo' should report link with active map") {
  269. RID map = navigation_server->map_create();
  270. CHECK(map.is_valid());
  271. navigation_server->map_set_active(map, true);
  272. navigation_server->link_set_map(link, map);
  273. navigation_server->process(0.0); // Give server some cycles to commit.
  274. CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_LINK_COUNT), 1);
  275. navigation_server->link_set_map(link, RID());
  276. navigation_server->free(map);
  277. navigation_server->process(0.0); // Give server some cycles to commit.
  278. CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_LINK_COUNT), 0);
  279. }
  280. navigation_server->free(link);
  281. }
  282. TEST_CASE("[NavigationServer3D] Server should manage obstacles properly") {
  283. NavigationServer3D *navigation_server = NavigationServer3D::get_singleton();
  284. RID obstacle = navigation_server->obstacle_create();
  285. CHECK(obstacle.is_valid());
  286. // TODO: Add tests for setters/getters once getters are added.
  287. navigation_server->free(obstacle);
  288. }
  289. TEST_CASE("[NavigationServer3D] Server should manage regions properly") {
  290. NavigationServer3D *navigation_server = NavigationServer3D::get_singleton();
  291. RID region = navigation_server->region_create();
  292. CHECK(region.is_valid());
  293. SUBCASE("'ProcessInfo' should not report dangling region") {
  294. CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_REGION_COUNT), 0);
  295. }
  296. SUBCASE("Setters/getters should work") {
  297. bool initial_use_edge_connections = navigation_server->region_get_use_edge_connections(region);
  298. navigation_server->region_set_enter_cost(region, 0.55);
  299. navigation_server->region_set_navigation_layers(region, 5);
  300. navigation_server->region_set_owner_id(region, ObjectID((int64_t)7));
  301. navigation_server->region_set_travel_cost(region, 0.66);
  302. navigation_server->region_set_use_edge_connections(region, !initial_use_edge_connections);
  303. navigation_server->process(0.0); // Give server some cycles to commit.
  304. CHECK_EQ(navigation_server->region_get_enter_cost(region), doctest::Approx(0.55));
  305. CHECK_EQ(navigation_server->region_get_navigation_layers(region), 5);
  306. CHECK_EQ(navigation_server->region_get_owner_id(region), ObjectID((int64_t)7));
  307. CHECK_EQ(navigation_server->region_get_travel_cost(region), doctest::Approx(0.66));
  308. CHECK_EQ(navigation_server->region_get_use_edge_connections(region), !initial_use_edge_connections);
  309. }
  310. SUBCASE("'ProcessInfo' should report region with active map") {
  311. RID map = navigation_server->map_create();
  312. CHECK(map.is_valid());
  313. navigation_server->map_set_active(map, true);
  314. navigation_server->region_set_map(region, map);
  315. navigation_server->process(0.0); // Give server some cycles to commit.
  316. CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_REGION_COUNT), 1);
  317. navigation_server->region_set_map(region, RID());
  318. navigation_server->free(map);
  319. navigation_server->process(0.0); // Give server some cycles to commit.
  320. CHECK_EQ(navigation_server->get_process_info(NavigationServer3D::INFO_REGION_COUNT), 0);
  321. }
  322. SUBCASE("Queries against empty region should return empty or invalid values") {
  323. ERR_PRINT_OFF;
  324. CHECK_EQ(navigation_server->region_get_connections_count(region), 0);
  325. CHECK_EQ(navigation_server->region_get_connection_pathway_end(region, 55), Vector3());
  326. CHECK_EQ(navigation_server->region_get_connection_pathway_start(region, 55), Vector3());
  327. ERR_PRINT_ON;
  328. }
  329. navigation_server->free(region);
  330. }
  331. // This test case does not check precise values on purpose - to not be too sensitivte.
  332. TEST_CASE("[NavigationServer3D] Server should move agent properly") {
  333. NavigationServer3D *navigation_server = NavigationServer3D::get_singleton();
  334. RID map = navigation_server->map_create();
  335. RID agent = navigation_server->agent_create();
  336. navigation_server->map_set_active(map, true);
  337. navigation_server->agent_set_map(agent, map);
  338. navigation_server->agent_set_avoidance_enabled(agent, true);
  339. navigation_server->agent_set_velocity(agent, Vector3(1, 0, 1));
  340. CallableMock agent_avoidance_callback_mock;
  341. navigation_server->agent_set_avoidance_callback(agent, callable_mp(&agent_avoidance_callback_mock, &CallableMock::function1));
  342. CHECK_EQ(agent_avoidance_callback_mock.function1_calls, 0);
  343. navigation_server->process(0.0); // Give server some cycles to commit.
  344. CHECK_EQ(agent_avoidance_callback_mock.function1_calls, 1);
  345. CHECK_NE(agent_avoidance_callback_mock.function1_latest_arg0, Vector3(0, 0, 0));
  346. navigation_server->free(agent);
  347. navigation_server->free(map);
  348. }
  349. // This test case does not check precise values on purpose - to not be too sensitivte.
  350. TEST_CASE("[NavigationServer3D] Server should make agents avoid each other when avoidance enabled") {
  351. NavigationServer3D *navigation_server = NavigationServer3D::get_singleton();
  352. RID map = navigation_server->map_create();
  353. RID agent_1 = navigation_server->agent_create();
  354. RID agent_2 = navigation_server->agent_create();
  355. navigation_server->map_set_active(map, true);
  356. navigation_server->agent_set_map(agent_1, map);
  357. navigation_server->agent_set_avoidance_enabled(agent_1, true);
  358. navigation_server->agent_set_position(agent_1, Vector3(0, 0, 0));
  359. navigation_server->agent_set_radius(agent_1, 1);
  360. navigation_server->agent_set_velocity(agent_1, Vector3(1, 0, 0));
  361. CallableMock agent_1_avoidance_callback_mock;
  362. navigation_server->agent_set_avoidance_callback(agent_1, callable_mp(&agent_1_avoidance_callback_mock, &CallableMock::function1));
  363. navigation_server->agent_set_map(agent_2, map);
  364. navigation_server->agent_set_avoidance_enabled(agent_2, true);
  365. navigation_server->agent_set_position(agent_2, Vector3(2.5, 0, 0.5));
  366. navigation_server->agent_set_radius(agent_2, 1);
  367. navigation_server->agent_set_velocity(agent_2, Vector3(-1, 0, 0));
  368. CallableMock agent_2_avoidance_callback_mock;
  369. navigation_server->agent_set_avoidance_callback(agent_2, callable_mp(&agent_2_avoidance_callback_mock, &CallableMock::function1));
  370. CHECK_EQ(agent_1_avoidance_callback_mock.function1_calls, 0);
  371. CHECK_EQ(agent_2_avoidance_callback_mock.function1_calls, 0);
  372. navigation_server->process(0.0); // Give server some cycles to commit.
  373. CHECK_EQ(agent_1_avoidance_callback_mock.function1_calls, 1);
  374. CHECK_EQ(agent_2_avoidance_callback_mock.function1_calls, 1);
  375. Vector3 agent_1_safe_velocity = agent_1_avoidance_callback_mock.function1_latest_arg0;
  376. Vector3 agent_2_safe_velocity = agent_2_avoidance_callback_mock.function1_latest_arg0;
  377. CHECK_MESSAGE(agent_1_safe_velocity.x > 0, "agent 1 should move a bit along desired velocity (+X)");
  378. CHECK_MESSAGE(agent_2_safe_velocity.x < 0, "agent 2 should move a bit along desired velocity (-X)");
  379. CHECK_MESSAGE(agent_1_safe_velocity.z < 0, "agent 1 should move a bit to the side so that it avoids agent 2");
  380. CHECK_MESSAGE(agent_2_safe_velocity.z > 0, "agent 2 should move a bit to the side so that it avoids agent 1");
  381. navigation_server->free(agent_2);
  382. navigation_server->free(agent_1);
  383. navigation_server->free(map);
  384. }
  385. TEST_CASE("[NavigationServer3D] Server should make agents avoid dynamic obstacles when avoidance enabled") {
  386. NavigationServer3D *navigation_server = NavigationServer3D::get_singleton();
  387. RID map = navigation_server->map_create();
  388. RID agent_1 = navigation_server->agent_create();
  389. RID obstacle_1 = navigation_server->obstacle_create();
  390. navigation_server->map_set_active(map, true);
  391. navigation_server->agent_set_map(agent_1, map);
  392. navigation_server->agent_set_avoidance_enabled(agent_1, true);
  393. navigation_server->agent_set_position(agent_1, Vector3(0, 0, 0));
  394. navigation_server->agent_set_radius(agent_1, 1);
  395. navigation_server->agent_set_velocity(agent_1, Vector3(1, 0, 0));
  396. CallableMock agent_1_avoidance_callback_mock;
  397. navigation_server->agent_set_avoidance_callback(agent_1, callable_mp(&agent_1_avoidance_callback_mock, &CallableMock::function1));
  398. navigation_server->obstacle_set_map(obstacle_1, map);
  399. navigation_server->obstacle_set_avoidance_enabled(obstacle_1, true);
  400. navigation_server->obstacle_set_position(obstacle_1, Vector3(2.5, 0, 0.5));
  401. navigation_server->obstacle_set_radius(obstacle_1, 1);
  402. CHECK_EQ(agent_1_avoidance_callback_mock.function1_calls, 0);
  403. navigation_server->process(0.0); // Give server some cycles to commit.
  404. CHECK_EQ(agent_1_avoidance_callback_mock.function1_calls, 1);
  405. Vector3 agent_1_safe_velocity = agent_1_avoidance_callback_mock.function1_latest_arg0;
  406. CHECK_MESSAGE(agent_1_safe_velocity.x > 0, "Agent 1 should move a bit along desired velocity (+X).");
  407. CHECK_MESSAGE(agent_1_safe_velocity.z < 0, "Agent 1 should move a bit to the side so that it avoids obstacle.");
  408. navigation_server->free(obstacle_1);
  409. navigation_server->free(agent_1);
  410. navigation_server->free(map);
  411. navigation_server->process(0.0); // Give server some cycles to commit.
  412. }
  413. TEST_CASE("[NavigationServer3D] Server should make agents avoid static obstacles when avoidance enabled") {
  414. NavigationServer3D *navigation_server = NavigationServer3D::get_singleton();
  415. RID map = navigation_server->map_create();
  416. RID agent_1 = navigation_server->agent_create();
  417. RID agent_2 = navigation_server->agent_create();
  418. RID obstacle_1 = navigation_server->obstacle_create();
  419. navigation_server->map_set_active(map, true);
  420. navigation_server->agent_set_map(agent_1, map);
  421. navigation_server->agent_set_avoidance_enabled(agent_1, true);
  422. navigation_server->agent_set_radius(agent_1, 1.6); // Have hit the obstacle already.
  423. navigation_server->agent_set_velocity(agent_1, Vector3(1, 0, 0));
  424. CallableMock agent_1_avoidance_callback_mock;
  425. navigation_server->agent_set_avoidance_callback(agent_1, callable_mp(&agent_1_avoidance_callback_mock, &CallableMock::function1));
  426. navigation_server->agent_set_map(agent_2, map);
  427. navigation_server->agent_set_avoidance_enabled(agent_2, true);
  428. navigation_server->agent_set_radius(agent_2, 1.4); // Haven't hit the obstacle yet.
  429. navigation_server->agent_set_velocity(agent_2, Vector3(1, 0, 0));
  430. CallableMock agent_2_avoidance_callback_mock;
  431. navigation_server->agent_set_avoidance_callback(agent_2, callable_mp(&agent_2_avoidance_callback_mock, &CallableMock::function1));
  432. navigation_server->obstacle_set_map(obstacle_1, map);
  433. navigation_server->obstacle_set_avoidance_enabled(obstacle_1, true);
  434. PackedVector3Array obstacle_1_vertices;
  435. SUBCASE("Static obstacles should work on ground level") {
  436. navigation_server->agent_set_position(agent_1, Vector3(0, 0, 0));
  437. navigation_server->agent_set_position(agent_2, Vector3(0, 0, 5));
  438. obstacle_1_vertices.push_back(Vector3(1.5, 0, 0.5));
  439. obstacle_1_vertices.push_back(Vector3(1.5, 0, 4.5));
  440. }
  441. SUBCASE("Static obstacles should work when elevated") {
  442. navigation_server->agent_set_position(agent_1, Vector3(0, 5, 0));
  443. navigation_server->agent_set_position(agent_2, Vector3(0, 5, 5));
  444. obstacle_1_vertices.push_back(Vector3(1.5, 0, 0.5));
  445. obstacle_1_vertices.push_back(Vector3(1.5, 0, 4.5));
  446. navigation_server->obstacle_set_position(obstacle_1, Vector3(0, 5, 0));
  447. }
  448. navigation_server->obstacle_set_vertices(obstacle_1, obstacle_1_vertices);
  449. CHECK_EQ(agent_1_avoidance_callback_mock.function1_calls, 0);
  450. CHECK_EQ(agent_2_avoidance_callback_mock.function1_calls, 0);
  451. navigation_server->process(0.0); // Give server some cycles to commit.
  452. CHECK_EQ(agent_1_avoidance_callback_mock.function1_calls, 1);
  453. CHECK_EQ(agent_2_avoidance_callback_mock.function1_calls, 1);
  454. Vector3 agent_1_safe_velocity = agent_1_avoidance_callback_mock.function1_latest_arg0;
  455. Vector3 agent_2_safe_velocity = agent_2_avoidance_callback_mock.function1_latest_arg0;
  456. CHECK_MESSAGE(agent_1_safe_velocity.x > 0, "Agent 1 should move a bit along desired velocity (+X).");
  457. CHECK_MESSAGE(agent_1_safe_velocity.z < 0, "Agent 1 should move a bit to the side so that it avoids obstacle.");
  458. CHECK_MESSAGE(agent_2_safe_velocity.x > 0, "Agent 2 should move a bit along desired velocity (+X).");
  459. CHECK_MESSAGE(agent_2_safe_velocity.z == 0, "Agent 2 should not move to the side.");
  460. navigation_server->free(obstacle_1);
  461. navigation_server->free(agent_2);
  462. navigation_server->free(agent_1);
  463. navigation_server->free(map);
  464. navigation_server->process(0.0); // Give server some cycles to commit.
  465. }
  466. #ifndef DISABLE_DEPRECATED
  467. // This test case uses only public APIs on purpose - other test cases use simplified baking.
  468. // FIXME: Remove once deprecated `region_bake_navigation_mesh()` is removed.
  469. TEST_CASE("[NavigationServer3D][SceneTree][DEPRECATED] Server should be able to bake map correctly") {
  470. NavigationServer3D *navigation_server = NavigationServer3D::get_singleton();
  471. // Prepare scene tree with simple mesh to serve as an input geometry.
  472. Node3D *node_3d = memnew(Node3D);
  473. SceneTree::get_singleton()->get_root()->add_child(node_3d);
  474. Ref<PlaneMesh> plane_mesh = memnew(PlaneMesh);
  475. plane_mesh->set_size(Size2(10.0, 10.0));
  476. MeshInstance3D *mesh_instance = memnew(MeshInstance3D);
  477. mesh_instance->set_mesh(plane_mesh);
  478. node_3d->add_child(mesh_instance);
  479. // Prepare anything necessary to bake navigation mesh.
  480. RID map = navigation_server->map_create();
  481. RID region = navigation_server->region_create();
  482. Ref<NavigationMesh> navigation_mesh = memnew(NavigationMesh);
  483. navigation_server->map_set_active(map, true);
  484. navigation_server->region_set_map(region, map);
  485. navigation_server->region_set_navigation_mesh(region, navigation_mesh);
  486. navigation_server->process(0.0); // Give server some cycles to commit.
  487. CHECK_EQ(navigation_mesh->get_polygon_count(), 0);
  488. CHECK_EQ(navigation_mesh->get_vertices().size(), 0);
  489. ERR_PRINT_OFF;
  490. navigation_server->region_bake_navigation_mesh(navigation_mesh, node_3d);
  491. ERR_PRINT_ON;
  492. // FIXME: The above line should trigger the update (line below) under the hood.
  493. navigation_server->region_set_navigation_mesh(region, navigation_mesh); // Force update.
  494. CHECK_EQ(navigation_mesh->get_polygon_count(), 2);
  495. CHECK_EQ(navigation_mesh->get_vertices().size(), 4);
  496. SUBCASE("Map should emit signal and take newly baked navigation mesh into account") {
  497. SIGNAL_WATCH(navigation_server, "map_changed");
  498. SIGNAL_CHECK_FALSE("map_changed");
  499. navigation_server->process(0.0); // Give server some cycles to commit.
  500. SIGNAL_CHECK("map_changed", build_array(build_array(map)));
  501. SIGNAL_UNWATCH(navigation_server, "map_changed");
  502. CHECK_NE(navigation_server->map_get_closest_point(map, Vector3(0, 0, 0)), Vector3(0, 0, 0));
  503. }
  504. navigation_server->free(region);
  505. navigation_server->free(map);
  506. navigation_server->process(0.0); // Give server some cycles to commit.
  507. memdelete(mesh_instance);
  508. memdelete(node_3d);
  509. }
  510. #endif // DISABLE_DEPRECATED
  511. TEST_CASE("[NavigationServer3D][SceneTree] Server should be able to parse geometry") {
  512. NavigationServer3D *navigation_server = NavigationServer3D::get_singleton();
  513. // Prepare scene tree with simple mesh to serve as an input geometry.
  514. Node3D *node_3d = memnew(Node3D);
  515. SceneTree::get_singleton()->get_root()->add_child(node_3d);
  516. Ref<PlaneMesh> plane_mesh = memnew(PlaneMesh);
  517. plane_mesh->set_size(Size2(10.0, 10.0));
  518. MeshInstance3D *mesh_instance = memnew(MeshInstance3D);
  519. mesh_instance->set_mesh(plane_mesh);
  520. node_3d->add_child(mesh_instance);
  521. Ref<NavigationMesh> navigation_mesh = memnew(NavigationMesh);
  522. Ref<NavigationMeshSourceGeometryData3D> source_geometry = memnew(NavigationMeshSourceGeometryData3D);
  523. CHECK_EQ(source_geometry->get_vertices().size(), 0);
  524. CHECK_EQ(source_geometry->get_indices().size(), 0);
  525. navigation_server->parse_source_geometry_data(navigation_mesh, source_geometry, mesh_instance);
  526. CHECK_EQ(source_geometry->get_vertices().size(), 12);
  527. CHECK_EQ(source_geometry->get_indices().size(), 6);
  528. SUBCASE("By default, parsing should remove any data that was parsed before") {
  529. navigation_server->parse_source_geometry_data(navigation_mesh, source_geometry, mesh_instance);
  530. CHECK_EQ(source_geometry->get_vertices().size(), 12);
  531. CHECK_EQ(source_geometry->get_indices().size(), 6);
  532. }
  533. SUBCASE("Parsed geometry should be extendible with other geometry") {
  534. source_geometry->merge(source_geometry); // Merging with itself.
  535. const Vector<float> vertices = source_geometry->get_vertices();
  536. const Vector<int> indices = source_geometry->get_indices();
  537. REQUIRE_EQ(vertices.size(), 24);
  538. REQUIRE_EQ(indices.size(), 12);
  539. // Check if first newly added vertex is the same as first vertex.
  540. CHECK_EQ(vertices[0], vertices[12]);
  541. CHECK_EQ(vertices[1], vertices[13]);
  542. CHECK_EQ(vertices[2], vertices[14]);
  543. // Check if first newly added index is the same as first index.
  544. CHECK_EQ(indices[0] + 4, indices[6]);
  545. }
  546. memdelete(mesh_instance);
  547. memdelete(node_3d);
  548. }
  549. // This test case uses only public APIs on purpose - other test cases use simplified baking.
  550. TEST_CASE("[NavigationServer3D][SceneTree] Server should be able to bake map correctly") {
  551. NavigationServer3D *navigation_server = NavigationServer3D::get_singleton();
  552. // Prepare scene tree with simple mesh to serve as an input geometry.
  553. Node3D *node_3d = memnew(Node3D);
  554. SceneTree::get_singleton()->get_root()->add_child(node_3d);
  555. Ref<PlaneMesh> plane_mesh = memnew(PlaneMesh);
  556. plane_mesh->set_size(Size2(10.0, 10.0));
  557. MeshInstance3D *mesh_instance = memnew(MeshInstance3D);
  558. mesh_instance->set_mesh(plane_mesh);
  559. node_3d->add_child(mesh_instance);
  560. // Prepare anything necessary to bake navigation mesh.
  561. RID map = navigation_server->map_create();
  562. RID region = navigation_server->region_create();
  563. Ref<NavigationMesh> navigation_mesh = memnew(NavigationMesh);
  564. navigation_server->map_set_active(map, true);
  565. navigation_server->region_set_map(region, map);
  566. navigation_server->region_set_navigation_mesh(region, navigation_mesh);
  567. navigation_server->process(0.0); // Give server some cycles to commit.
  568. CHECK_EQ(navigation_mesh->get_polygon_count(), 0);
  569. CHECK_EQ(navigation_mesh->get_vertices().size(), 0);
  570. Ref<NavigationMeshSourceGeometryData3D> source_geometry = memnew(NavigationMeshSourceGeometryData3D);
  571. navigation_server->parse_source_geometry_data(navigation_mesh, source_geometry, node_3d);
  572. navigation_server->bake_from_source_geometry_data(navigation_mesh, source_geometry, Callable());
  573. // FIXME: The above line should trigger the update (line below) under the hood.
  574. navigation_server->region_set_navigation_mesh(region, navigation_mesh); // Force update.
  575. CHECK_EQ(navigation_mesh->get_polygon_count(), 2);
  576. CHECK_EQ(navigation_mesh->get_vertices().size(), 4);
  577. SUBCASE("Map should emit signal and take newly baked navigation mesh into account") {
  578. SIGNAL_WATCH(navigation_server, "map_changed");
  579. SIGNAL_CHECK_FALSE("map_changed");
  580. navigation_server->process(0.0); // Give server some cycles to commit.
  581. SIGNAL_CHECK("map_changed", build_array(build_array(map)));
  582. SIGNAL_UNWATCH(navigation_server, "map_changed");
  583. CHECK_NE(navigation_server->map_get_closest_point(map, Vector3(0, 0, 0)), Vector3(0, 0, 0));
  584. }
  585. navigation_server->free(region);
  586. navigation_server->free(map);
  587. navigation_server->process(0.0); // Give server some cycles to commit.
  588. memdelete(mesh_instance);
  589. memdelete(node_3d);
  590. }
  591. // This test case does not check precise values on purpose - to not be too sensitivte.
  592. TEST_CASE("[NavigationServer3D] Server should respond to queries against valid map properly") {
  593. NavigationServer3D *navigation_server = NavigationServer3D::get_singleton();
  594. Ref<NavigationMesh> navigation_mesh = memnew(NavigationMesh);
  595. Ref<NavigationMeshSourceGeometryData3D> source_geometry = memnew(NavigationMeshSourceGeometryData3D);
  596. Array arr;
  597. arr.resize(RS::ARRAY_MAX);
  598. BoxMesh::create_mesh_array(arr, Vector3(10.0, 0.001, 10.0));
  599. source_geometry->add_mesh_array(arr, Transform3D());
  600. navigation_server->bake_from_source_geometry_data(navigation_mesh, source_geometry, Callable());
  601. CHECK_NE(navigation_mesh->get_polygon_count(), 0);
  602. CHECK_NE(navigation_mesh->get_vertices().size(), 0);
  603. RID map = navigation_server->map_create();
  604. RID region = navigation_server->region_create();
  605. navigation_server->map_set_active(map, true);
  606. navigation_server->region_set_map(region, map);
  607. navigation_server->region_set_navigation_mesh(region, navigation_mesh);
  608. navigation_server->process(0.0); // Give server some cycles to commit.
  609. SUBCASE("Simple queries should return non-default values") {
  610. CHECK_NE(navigation_server->map_get_closest_point(map, Vector3(0, 0, 0)), Vector3(0, 0, 0));
  611. CHECK_NE(navigation_server->map_get_closest_point_normal(map, Vector3(0, 0, 0)), Vector3());
  612. CHECK(navigation_server->map_get_closest_point_owner(map, Vector3(0, 0, 0)).is_valid());
  613. CHECK_NE(navigation_server->map_get_closest_point_to_segment(map, Vector3(0, 0, 0), Vector3(1, 1, 1), false), Vector3());
  614. CHECK_NE(navigation_server->map_get_closest_point_to_segment(map, Vector3(0, 0, 0), Vector3(1, 1, 1), true), Vector3());
  615. CHECK_NE(navigation_server->map_get_path(map, Vector3(0, 0, 0), Vector3(10, 0, 10), true).size(), 0);
  616. CHECK_NE(navigation_server->map_get_path(map, Vector3(0, 0, 0), Vector3(10, 0, 10), false).size(), 0);
  617. }
  618. SUBCASE("'map_get_closest_point_to_segment' with 'use_collision' should return default if segment doesn't intersect map") {
  619. CHECK_EQ(navigation_server->map_get_closest_point_to_segment(map, Vector3(1, 2, 1), Vector3(1, 1, 1), true), Vector3());
  620. }
  621. SUBCASE("Elaborate query with 'CORRIDORFUNNEL' post-processing should yield non-empty result") {
  622. Ref<NavigationPathQueryParameters3D> query_parameters = memnew(NavigationPathQueryParameters3D);
  623. query_parameters->set_map(map);
  624. query_parameters->set_start_position(Vector3(0, 0, 0));
  625. query_parameters->set_target_position(Vector3(10, 0, 10));
  626. query_parameters->set_path_postprocessing(NavigationPathQueryParameters3D::PATH_POSTPROCESSING_CORRIDORFUNNEL);
  627. Ref<NavigationPathQueryResult3D> query_result = memnew(NavigationPathQueryResult3D);
  628. navigation_server->query_path(query_parameters, query_result);
  629. CHECK_NE(query_result->get_path().size(), 0);
  630. CHECK_NE(query_result->get_path_types().size(), 0);
  631. CHECK_NE(query_result->get_path_rids().size(), 0);
  632. CHECK_NE(query_result->get_path_owner_ids().size(), 0);
  633. }
  634. SUBCASE("Elaborate query with 'EDGECENTERED' post-processing should yield non-empty result") {
  635. Ref<NavigationPathQueryParameters3D> query_parameters = memnew(NavigationPathQueryParameters3D);
  636. query_parameters->set_map(map);
  637. query_parameters->set_start_position(Vector3(10, 0, 10));
  638. query_parameters->set_target_position(Vector3(0, 0, 0));
  639. query_parameters->set_path_postprocessing(NavigationPathQueryParameters3D::PATH_POSTPROCESSING_EDGECENTERED);
  640. Ref<NavigationPathQueryResult3D> query_result = memnew(NavigationPathQueryResult3D);
  641. navigation_server->query_path(query_parameters, query_result);
  642. CHECK_NE(query_result->get_path().size(), 0);
  643. CHECK_NE(query_result->get_path_types().size(), 0);
  644. CHECK_NE(query_result->get_path_rids().size(), 0);
  645. CHECK_NE(query_result->get_path_owner_ids().size(), 0);
  646. }
  647. SUBCASE("Elaborate query with non-matching navigation layer mask should yield empty result") {
  648. Ref<NavigationPathQueryParameters3D> query_parameters = memnew(NavigationPathQueryParameters3D);
  649. query_parameters->set_map(map);
  650. query_parameters->set_start_position(Vector3(10, 0, 10));
  651. query_parameters->set_target_position(Vector3(0, 0, 0));
  652. query_parameters->set_navigation_layers(2);
  653. Ref<NavigationPathQueryResult3D> query_result = memnew(NavigationPathQueryResult3D);
  654. navigation_server->query_path(query_parameters, query_result);
  655. CHECK_EQ(query_result->get_path().size(), 0);
  656. CHECK_EQ(query_result->get_path_types().size(), 0);
  657. CHECK_EQ(query_result->get_path_rids().size(), 0);
  658. CHECK_EQ(query_result->get_path_owner_ids().size(), 0);
  659. }
  660. SUBCASE("Elaborate query without metadata flags should yield path only") {
  661. Ref<NavigationPathQueryParameters3D> query_parameters = memnew(NavigationPathQueryParameters3D);
  662. query_parameters->set_map(map);
  663. query_parameters->set_start_position(Vector3(10, 0, 10));
  664. query_parameters->set_target_position(Vector3(0, 0, 0));
  665. query_parameters->set_metadata_flags(0);
  666. Ref<NavigationPathQueryResult3D> query_result = memnew(NavigationPathQueryResult3D);
  667. navigation_server->query_path(query_parameters, query_result);
  668. CHECK_NE(query_result->get_path().size(), 0);
  669. CHECK_EQ(query_result->get_path_types().size(), 0);
  670. CHECK_EQ(query_result->get_path_rids().size(), 0);
  671. CHECK_EQ(query_result->get_path_owner_ids().size(), 0);
  672. }
  673. navigation_server->free(region);
  674. navigation_server->free(map);
  675. navigation_server->process(0.0); // Give server some cycles to commit.
  676. }
  677. // FIXME: The race condition mentioned below is actually a problem and fails on CI (GH-90613).
  678. /*
  679. TEST_CASE("[NavigationServer3D] Server should be able to bake asynchronously") {
  680. NavigationServer3D *navigation_server = NavigationServer3D::get_singleton();
  681. Ref<NavigationMesh> navigation_mesh = memnew(NavigationMesh);
  682. Ref<NavigationMeshSourceGeometryData3D> source_geometry = memnew(NavigationMeshSourceGeometryData3D);
  683. Array arr;
  684. arr.resize(RS::ARRAY_MAX);
  685. BoxMesh::create_mesh_array(arr, Vector3(10.0, 0.001, 10.0));
  686. source_geometry->add_mesh_array(arr, Transform3D());
  687. // Race condition is present below, but baking should take many orders of magnitude
  688. // longer than basic checks on the main thread, so it's fine.
  689. navigation_server->bake_from_source_geometry_data_async(navigation_mesh, source_geometry, Callable());
  690. CHECK(navigation_server->is_baking_navigation_mesh(navigation_mesh));
  691. CHECK_EQ(navigation_mesh->get_polygon_count(), 0);
  692. CHECK_EQ(navigation_mesh->get_vertices().size(), 0);
  693. }
  694. */
  695. TEST_CASE("[NavigationServer3D] Server should simplify path properly") {
  696. real_t simplify_epsilon = 0.2;
  697. Vector<Vector3> source_path;
  698. source_path.resize(7);
  699. source_path.write[0] = Vector3(0.0, 0.0, 0.0);
  700. source_path.write[1] = Vector3(0.0, 0.0, 1.0); // This point needs to go.
  701. source_path.write[2] = Vector3(0.0, 0.0, 2.0); // This point needs to go.
  702. source_path.write[3] = Vector3(0.0, 0.0, 2.0);
  703. source_path.write[4] = Vector3(2.0, 1.0, 3.0);
  704. source_path.write[5] = Vector3(2.0, 1.5, 4.0); // This point needs to go.
  705. source_path.write[6] = Vector3(2.0, 2.0, 5.0);
  706. Vector<Vector3> simplified_path = NavigationServer3D::get_singleton()->simplify_path(source_path, simplify_epsilon);
  707. CHECK_EQ(simplified_path.size(), 4);
  708. }
  709. TEST_CASE("[Heap] size") {
  710. gd::Heap<int> heap;
  711. CHECK(heap.size() == 0);
  712. heap.push(0);
  713. CHECK(heap.size() == 1);
  714. heap.push(1);
  715. CHECK(heap.size() == 2);
  716. heap.pop();
  717. CHECK(heap.size() == 1);
  718. heap.pop();
  719. CHECK(heap.size() == 0);
  720. }
  721. TEST_CASE("[Heap] is_empty") {
  722. gd::Heap<int> heap;
  723. CHECK(heap.is_empty() == true);
  724. heap.push(0);
  725. CHECK(heap.is_empty() == false);
  726. heap.pop();
  727. CHECK(heap.is_empty() == true);
  728. }
  729. TEST_CASE("[Heap] push/pop") {
  730. SUBCASE("Default comparator") {
  731. gd::Heap<int> heap;
  732. heap.push(2);
  733. heap.push(7);
  734. heap.push(5);
  735. heap.push(3);
  736. heap.push(4);
  737. CHECK(heap.pop() == 7);
  738. CHECK(heap.pop() == 5);
  739. CHECK(heap.pop() == 4);
  740. CHECK(heap.pop() == 3);
  741. CHECK(heap.pop() == 2);
  742. }
  743. SUBCASE("Custom comparator") {
  744. GreaterThan greaterThan;
  745. gd::Heap<int, GreaterThan> heap(greaterThan);
  746. heap.push(2);
  747. heap.push(7);
  748. heap.push(5);
  749. heap.push(3);
  750. heap.push(4);
  751. CHECK(heap.pop() == 2);
  752. CHECK(heap.pop() == 3);
  753. CHECK(heap.pop() == 4);
  754. CHECK(heap.pop() == 5);
  755. CHECK(heap.pop() == 7);
  756. }
  757. SUBCASE("Intermediate pops") {
  758. gd::Heap<int> heap;
  759. heap.push(0);
  760. heap.push(3);
  761. heap.pop();
  762. heap.push(1);
  763. heap.push(2);
  764. CHECK(heap.pop() == 2);
  765. CHECK(heap.pop() == 1);
  766. CHECK(heap.pop() == 0);
  767. }
  768. }
  769. TEST_CASE("[Heap] shift") {
  770. int values[] = { 5, 3, 6, 7, 1 };
  771. uint32_t heap_indexes[] = { 0, 0, 0, 0, 0 };
  772. CompareArrayValues comparator(values);
  773. RegisterHeapIndexes indexer(heap_indexes);
  774. gd::Heap<uint32_t, CompareArrayValues, RegisterHeapIndexes> heap(comparator, indexer);
  775. heap.push(0);
  776. heap.push(1);
  777. heap.push(2);
  778. heap.push(3);
  779. heap.push(4);
  780. // Shift down: 6 -> 2
  781. values[2] = 2;
  782. heap.shift(heap_indexes[2]);
  783. // Shift up: 5 -> 8
  784. values[0] = 8;
  785. heap.shift(heap_indexes[0]);
  786. CHECK(heap.pop() == 0);
  787. CHECK(heap.pop() == 3);
  788. CHECK(heap.pop() == 1);
  789. CHECK(heap.pop() == 2);
  790. CHECK(heap.pop() == 4);
  791. CHECK(heap_indexes[0] == UINT32_MAX);
  792. CHECK(heap_indexes[1] == UINT32_MAX);
  793. CHECK(heap_indexes[2] == UINT32_MAX);
  794. CHECK(heap_indexes[3] == UINT32_MAX);
  795. CHECK(heap_indexes[4] == UINT32_MAX);
  796. }
  797. TEST_CASE("[Heap] clear") {
  798. uint32_t heap_indexes[] = { 0, 0, 0, 0 };
  799. RegisterHeapIndexes indexer(heap_indexes);
  800. gd::Heap<uint32_t, Comparator<uint32_t>, RegisterHeapIndexes> heap(indexer);
  801. heap.push(0);
  802. heap.push(2);
  803. heap.push(1);
  804. heap.push(3);
  805. heap.clear();
  806. CHECK(heap.size() == 0);
  807. CHECK(heap_indexes[0] == UINT32_MAX);
  808. CHECK(heap_indexes[1] == UINT32_MAX);
  809. CHECK(heap_indexes[2] == UINT32_MAX);
  810. CHECK(heap_indexes[3] == UINT32_MAX);
  811. }
  812. }
  813. } //namespace TestNavigationServer3D
  814. #endif // TEST_NAVIGATION_SERVER_3D_H