navigation_mesh_source_geometry_data_2d.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /**************************************************************************/
  2. /* navigation_mesh_source_geometry_data_2d.cpp */
  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. #include "navigation_mesh_source_geometry_data_2d.h"
  31. #include "scene/resources/mesh.h"
  32. void NavigationMeshSourceGeometryData2D::clear() {
  33. RWLockWrite write_lock(geometry_rwlock);
  34. traversable_outlines.clear();
  35. obstruction_outlines.clear();
  36. _projected_obstructions.clear();
  37. bounds_dirty = true;
  38. }
  39. bool NavigationMeshSourceGeometryData2D::has_data() {
  40. RWLockRead read_lock(geometry_rwlock);
  41. return traversable_outlines.size();
  42. }
  43. void NavigationMeshSourceGeometryData2D::clear_projected_obstructions() {
  44. RWLockWrite write_lock(geometry_rwlock);
  45. _projected_obstructions.clear();
  46. bounds_dirty = true;
  47. }
  48. void NavigationMeshSourceGeometryData2D::_set_traversable_outlines(const Vector<Vector<Vector2>> &p_traversable_outlines) {
  49. RWLockWrite write_lock(geometry_rwlock);
  50. traversable_outlines = p_traversable_outlines;
  51. bounds_dirty = true;
  52. }
  53. void NavigationMeshSourceGeometryData2D::_set_obstruction_outlines(const Vector<Vector<Vector2>> &p_obstruction_outlines) {
  54. RWLockWrite write_lock(geometry_rwlock);
  55. obstruction_outlines = p_obstruction_outlines;
  56. bounds_dirty = true;
  57. }
  58. const Vector<Vector<Vector2>> &NavigationMeshSourceGeometryData2D::_get_traversable_outlines() const {
  59. RWLockRead read_lock(geometry_rwlock);
  60. return traversable_outlines;
  61. }
  62. const Vector<Vector<Vector2>> &NavigationMeshSourceGeometryData2D::_get_obstruction_outlines() const {
  63. RWLockRead read_lock(geometry_rwlock);
  64. return obstruction_outlines;
  65. }
  66. void NavigationMeshSourceGeometryData2D::_add_traversable_outline(const Vector<Vector2> &p_shape_outline) {
  67. if (p_shape_outline.size() > 1) {
  68. RWLockWrite write_lock(geometry_rwlock);
  69. traversable_outlines.push_back(p_shape_outline);
  70. bounds_dirty = true;
  71. }
  72. }
  73. void NavigationMeshSourceGeometryData2D::_add_obstruction_outline(const Vector<Vector2> &p_shape_outline) {
  74. if (p_shape_outline.size() > 1) {
  75. RWLockWrite write_lock(geometry_rwlock);
  76. obstruction_outlines.push_back(p_shape_outline);
  77. bounds_dirty = true;
  78. }
  79. }
  80. void NavigationMeshSourceGeometryData2D::set_traversable_outlines(const TypedArray<Vector<Vector2>> &p_traversable_outlines) {
  81. RWLockWrite write_lock(geometry_rwlock);
  82. traversable_outlines.resize(p_traversable_outlines.size());
  83. for (int i = 0; i < p_traversable_outlines.size(); i++) {
  84. traversable_outlines.write[i] = p_traversable_outlines[i];
  85. }
  86. bounds_dirty = true;
  87. }
  88. TypedArray<Vector<Vector2>> NavigationMeshSourceGeometryData2D::get_traversable_outlines() const {
  89. RWLockRead read_lock(geometry_rwlock);
  90. TypedArray<Vector<Vector2>> typed_array_traversable_outlines;
  91. typed_array_traversable_outlines.resize(traversable_outlines.size());
  92. for (int i = 0; i < typed_array_traversable_outlines.size(); i++) {
  93. typed_array_traversable_outlines[i] = traversable_outlines[i];
  94. }
  95. return typed_array_traversable_outlines;
  96. }
  97. void NavigationMeshSourceGeometryData2D::set_obstruction_outlines(const TypedArray<Vector<Vector2>> &p_obstruction_outlines) {
  98. RWLockWrite write_lock(geometry_rwlock);
  99. obstruction_outlines.resize(p_obstruction_outlines.size());
  100. for (int i = 0; i < p_obstruction_outlines.size(); i++) {
  101. obstruction_outlines.write[i] = p_obstruction_outlines[i];
  102. }
  103. bounds_dirty = true;
  104. }
  105. TypedArray<Vector<Vector2>> NavigationMeshSourceGeometryData2D::get_obstruction_outlines() const {
  106. RWLockRead read_lock(geometry_rwlock);
  107. TypedArray<Vector<Vector2>> typed_array_obstruction_outlines;
  108. typed_array_obstruction_outlines.resize(obstruction_outlines.size());
  109. for (int i = 0; i < typed_array_obstruction_outlines.size(); i++) {
  110. typed_array_obstruction_outlines[i] = obstruction_outlines[i];
  111. }
  112. return typed_array_obstruction_outlines;
  113. }
  114. void NavigationMeshSourceGeometryData2D::append_traversable_outlines(const TypedArray<Vector<Vector2>> &p_traversable_outlines) {
  115. RWLockWrite write_lock(geometry_rwlock);
  116. int traversable_outlines_size = traversable_outlines.size();
  117. traversable_outlines.resize(traversable_outlines_size + p_traversable_outlines.size());
  118. for (int i = traversable_outlines_size; i < p_traversable_outlines.size(); i++) {
  119. traversable_outlines.write[i] = p_traversable_outlines[i];
  120. }
  121. bounds_dirty = true;
  122. }
  123. void NavigationMeshSourceGeometryData2D::append_obstruction_outlines(const TypedArray<Vector<Vector2>> &p_obstruction_outlines) {
  124. RWLockWrite write_lock(geometry_rwlock);
  125. int obstruction_outlines_size = obstruction_outlines.size();
  126. obstruction_outlines.resize(obstruction_outlines_size + p_obstruction_outlines.size());
  127. for (int i = obstruction_outlines_size; i < p_obstruction_outlines.size(); i++) {
  128. obstruction_outlines.write[i] = p_obstruction_outlines[i];
  129. }
  130. bounds_dirty = true;
  131. }
  132. void NavigationMeshSourceGeometryData2D::add_traversable_outline(const PackedVector2Array &p_shape_outline) {
  133. if (p_shape_outline.size() > 1) {
  134. RWLockWrite write_lock(geometry_rwlock);
  135. Vector<Vector2> traversable_outline;
  136. traversable_outline.resize(p_shape_outline.size());
  137. for (int i = 0; i < p_shape_outline.size(); i++) {
  138. traversable_outline.write[i] = p_shape_outline[i];
  139. }
  140. traversable_outlines.push_back(traversable_outline);
  141. bounds_dirty = true;
  142. }
  143. }
  144. void NavigationMeshSourceGeometryData2D::add_obstruction_outline(const PackedVector2Array &p_shape_outline) {
  145. if (p_shape_outline.size() > 1) {
  146. RWLockWrite write_lock(geometry_rwlock);
  147. Vector<Vector2> obstruction_outline;
  148. obstruction_outline.resize(p_shape_outline.size());
  149. for (int i = 0; i < p_shape_outline.size(); i++) {
  150. obstruction_outline.write[i] = p_shape_outline[i];
  151. }
  152. obstruction_outlines.push_back(obstruction_outline);
  153. bounds_dirty = true;
  154. }
  155. }
  156. void NavigationMeshSourceGeometryData2D::merge(const Ref<NavigationMeshSourceGeometryData2D> &p_other_geometry) {
  157. ERR_FAIL_COND(p_other_geometry.is_null());
  158. Vector<Vector<Vector2>> other_traversable_outlines;
  159. Vector<Vector<Vector2>> other_obstruction_outlines;
  160. Vector<ProjectedObstruction> other_projected_obstructions;
  161. p_other_geometry->get_data(other_traversable_outlines, other_obstruction_outlines, other_projected_obstructions);
  162. RWLockWrite write_lock(geometry_rwlock);
  163. traversable_outlines.append_array(other_traversable_outlines);
  164. obstruction_outlines.append_array(other_obstruction_outlines);
  165. _projected_obstructions.append_array(other_projected_obstructions);
  166. bounds_dirty = true;
  167. }
  168. void NavigationMeshSourceGeometryData2D::add_projected_obstruction(const Vector<Vector2> &p_vertices, bool p_carve) {
  169. ERR_FAIL_COND(p_vertices.size() < 2);
  170. ProjectedObstruction projected_obstruction;
  171. projected_obstruction.vertices.resize(p_vertices.size() * 2);
  172. projected_obstruction.carve = p_carve;
  173. float *obstruction_vertices_ptrw = projected_obstruction.vertices.ptrw();
  174. int vertex_index = 0;
  175. for (const Vector2 &vertex : p_vertices) {
  176. obstruction_vertices_ptrw[vertex_index++] = vertex.x;
  177. obstruction_vertices_ptrw[vertex_index++] = vertex.y;
  178. }
  179. RWLockWrite write_lock(geometry_rwlock);
  180. _projected_obstructions.push_back(projected_obstruction);
  181. bounds_dirty = true;
  182. }
  183. void NavigationMeshSourceGeometryData2D::set_projected_obstructions(const Array &p_array) {
  184. clear_projected_obstructions();
  185. for (int i = 0; i < p_array.size(); i++) {
  186. Dictionary data = p_array[i];
  187. ERR_FAIL_COND(!data.has("version"));
  188. uint32_t po_version = data["version"];
  189. if (po_version == 1) {
  190. ERR_FAIL_COND(!data.has("vertices"));
  191. ERR_FAIL_COND(!data.has("carve"));
  192. }
  193. ProjectedObstruction projected_obstruction;
  194. projected_obstruction.vertices = Vector<float>(data["vertices"]);
  195. projected_obstruction.carve = data["carve"];
  196. RWLockWrite write_lock(geometry_rwlock);
  197. _projected_obstructions.push_back(projected_obstruction);
  198. bounds_dirty = true;
  199. }
  200. }
  201. Vector<NavigationMeshSourceGeometryData2D::ProjectedObstruction> NavigationMeshSourceGeometryData2D::_get_projected_obstructions() const {
  202. RWLockRead read_lock(geometry_rwlock);
  203. return _projected_obstructions;
  204. }
  205. Array NavigationMeshSourceGeometryData2D::get_projected_obstructions() const {
  206. RWLockRead read_lock(geometry_rwlock);
  207. Array ret;
  208. ret.resize(_projected_obstructions.size());
  209. for (int i = 0; i < _projected_obstructions.size(); i++) {
  210. const ProjectedObstruction &projected_obstruction = _projected_obstructions[i];
  211. Dictionary data;
  212. data["version"] = (int)ProjectedObstruction::VERSION;
  213. data["vertices"] = projected_obstruction.vertices;
  214. data["carve"] = projected_obstruction.carve;
  215. ret[i] = data;
  216. }
  217. return ret;
  218. }
  219. bool NavigationMeshSourceGeometryData2D::_set(const StringName &p_name, const Variant &p_value) {
  220. if (p_name == "projected_obstructions") {
  221. set_projected_obstructions(p_value);
  222. return true;
  223. }
  224. return false;
  225. }
  226. bool NavigationMeshSourceGeometryData2D::_get(const StringName &p_name, Variant &r_ret) const {
  227. if (p_name == "projected_obstructions") {
  228. r_ret = get_projected_obstructions();
  229. return true;
  230. }
  231. return false;
  232. }
  233. void NavigationMeshSourceGeometryData2D::set_data(const Vector<Vector<Vector2>> &p_traversable_outlines, const Vector<Vector<Vector2>> &p_obstruction_outlines, Vector<ProjectedObstruction> &p_projected_obstructions) {
  234. RWLockWrite write_lock(geometry_rwlock);
  235. traversable_outlines = p_traversable_outlines;
  236. obstruction_outlines = p_obstruction_outlines;
  237. _projected_obstructions = p_projected_obstructions;
  238. bounds_dirty = true;
  239. }
  240. void NavigationMeshSourceGeometryData2D::get_data(Vector<Vector<Vector2>> &r_traversable_outlines, Vector<Vector<Vector2>> &r_obstruction_outlines, Vector<ProjectedObstruction> &r_projected_obstructions) {
  241. RWLockRead read_lock(geometry_rwlock);
  242. r_traversable_outlines = traversable_outlines;
  243. r_obstruction_outlines = obstruction_outlines;
  244. r_projected_obstructions = _projected_obstructions;
  245. }
  246. Rect2 NavigationMeshSourceGeometryData2D::get_bounds() {
  247. geometry_rwlock.read_lock();
  248. if (bounds_dirty) {
  249. geometry_rwlock.read_unlock();
  250. RWLockWrite write_lock(geometry_rwlock);
  251. bounds_dirty = false;
  252. bounds = Rect2();
  253. bool first_vertex = true;
  254. for (const Vector<Vector2> &traversable_outline : traversable_outlines) {
  255. for (const Vector2 &traversable_point : traversable_outline) {
  256. if (first_vertex) {
  257. first_vertex = false;
  258. bounds.position = traversable_point;
  259. } else {
  260. bounds.expand_to(traversable_point);
  261. }
  262. }
  263. }
  264. for (const Vector<Vector2> &obstruction_outline : obstruction_outlines) {
  265. for (const Vector2 &obstruction_point : obstruction_outline) {
  266. if (first_vertex) {
  267. first_vertex = false;
  268. bounds.position = obstruction_point;
  269. } else {
  270. bounds.expand_to(obstruction_point);
  271. }
  272. }
  273. }
  274. for (const ProjectedObstruction &projected_obstruction : _projected_obstructions) {
  275. for (int i = 0; i < projected_obstruction.vertices.size() / 2; i++) {
  276. const Vector2 vertex = Vector2(projected_obstruction.vertices[i * 2], projected_obstruction.vertices[i * 2 + 1]);
  277. if (first_vertex) {
  278. first_vertex = false;
  279. bounds.position = vertex;
  280. } else {
  281. bounds.expand_to(vertex);
  282. }
  283. }
  284. }
  285. } else {
  286. geometry_rwlock.read_unlock();
  287. }
  288. RWLockRead read_lock(geometry_rwlock);
  289. return bounds;
  290. }
  291. void NavigationMeshSourceGeometryData2D::_bind_methods() {
  292. ClassDB::bind_method(D_METHOD("clear"), &NavigationMeshSourceGeometryData2D::clear);
  293. ClassDB::bind_method(D_METHOD("has_data"), &NavigationMeshSourceGeometryData2D::has_data);
  294. ClassDB::bind_method(D_METHOD("set_traversable_outlines", "traversable_outlines"), &NavigationMeshSourceGeometryData2D::set_traversable_outlines);
  295. ClassDB::bind_method(D_METHOD("get_traversable_outlines"), &NavigationMeshSourceGeometryData2D::get_traversable_outlines);
  296. ClassDB::bind_method(D_METHOD("set_obstruction_outlines", "obstruction_outlines"), &NavigationMeshSourceGeometryData2D::set_obstruction_outlines);
  297. ClassDB::bind_method(D_METHOD("get_obstruction_outlines"), &NavigationMeshSourceGeometryData2D::get_obstruction_outlines);
  298. ClassDB::bind_method(D_METHOD("append_traversable_outlines", "traversable_outlines"), &NavigationMeshSourceGeometryData2D::append_traversable_outlines);
  299. ClassDB::bind_method(D_METHOD("append_obstruction_outlines", "obstruction_outlines"), &NavigationMeshSourceGeometryData2D::append_obstruction_outlines);
  300. ClassDB::bind_method(D_METHOD("add_traversable_outline", "shape_outline"), &NavigationMeshSourceGeometryData2D::add_traversable_outline);
  301. ClassDB::bind_method(D_METHOD("add_obstruction_outline", "shape_outline"), &NavigationMeshSourceGeometryData2D::add_obstruction_outline);
  302. ClassDB::bind_method(D_METHOD("merge", "other_geometry"), &NavigationMeshSourceGeometryData2D::merge);
  303. ClassDB::bind_method(D_METHOD("add_projected_obstruction", "vertices", "carve"), &NavigationMeshSourceGeometryData2D::add_projected_obstruction);
  304. ClassDB::bind_method(D_METHOD("clear_projected_obstructions"), &NavigationMeshSourceGeometryData2D::clear_projected_obstructions);
  305. ClassDB::bind_method(D_METHOD("set_projected_obstructions", "projected_obstructions"), &NavigationMeshSourceGeometryData2D::set_projected_obstructions);
  306. ClassDB::bind_method(D_METHOD("get_projected_obstructions"), &NavigationMeshSourceGeometryData2D::get_projected_obstructions);
  307. ClassDB::bind_method(D_METHOD("get_bounds"), &NavigationMeshSourceGeometryData2D::get_bounds);
  308. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "traversable_outlines", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "set_traversable_outlines", "get_traversable_outlines");
  309. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "obstruction_outlines", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "set_obstruction_outlines", "get_obstruction_outlines");
  310. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "projected_obstructions", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "set_projected_obstructions", "get_projected_obstructions");
  311. }