geometry.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  1. /*************************************************************************/
  2. /* geometry.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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 "geometry.h"
  31. #include "print_string.h"
  32. void Geometry::MeshData::optimize_vertices() {
  33. Map<int, int> vtx_remap;
  34. for (int i = 0; i < faces.size(); i++) {
  35. for (int j = 0; j < faces[i].indices.size(); j++) {
  36. int idx = faces[i].indices[j];
  37. if (!vtx_remap.has(idx)) {
  38. int ni = vtx_remap.size();
  39. vtx_remap[idx] = ni;
  40. }
  41. faces[i].indices[j] = vtx_remap[idx];
  42. }
  43. }
  44. for (int i = 0; i < edges.size(); i++) {
  45. int a = edges[i].a;
  46. int b = edges[i].b;
  47. if (!vtx_remap.has(a)) {
  48. int ni = vtx_remap.size();
  49. vtx_remap[a] = ni;
  50. }
  51. if (!vtx_remap.has(b)) {
  52. int ni = vtx_remap.size();
  53. vtx_remap[b] = ni;
  54. }
  55. edges[i].a = vtx_remap[a];
  56. edges[i].b = vtx_remap[b];
  57. }
  58. Vector<Vector3> new_vertices;
  59. new_vertices.resize(vtx_remap.size());
  60. for (int i = 0; i < vertices.size(); i++) {
  61. if (vtx_remap.has(i))
  62. new_vertices[vtx_remap[i]] = vertices[i];
  63. }
  64. vertices = new_vertices;
  65. }
  66. Vector<Vector<Vector2> > (*Geometry::_decompose_func)(const Vector<Vector2> &p_polygon) = NULL;
  67. struct _FaceClassify {
  68. struct _Link {
  69. int face;
  70. int edge;
  71. void clear() {
  72. face = -1;
  73. edge = -1;
  74. }
  75. _Link() {
  76. face = -1;
  77. edge = -1;
  78. }
  79. };
  80. bool valid;
  81. int group;
  82. _Link links[3];
  83. Face3 face;
  84. _FaceClassify() {
  85. group = -1;
  86. valid = false;
  87. };
  88. };
  89. static bool _connect_faces(_FaceClassify *p_faces, int len, int p_group) {
  90. /* connect faces, error will occur if an edge is shared between more than 2 faces */
  91. /* clear connections */
  92. bool error = false;
  93. for (int i = 0; i < len; i++) {
  94. for (int j = 0; j < 3; j++) {
  95. p_faces[i].links[j].clear();
  96. }
  97. }
  98. for (int i = 0; i < len; i++) {
  99. if (p_faces[i].group != p_group)
  100. continue;
  101. for (int j = i + 1; j < len; j++) {
  102. if (p_faces[j].group != p_group)
  103. continue;
  104. for (int k = 0; k < 3; k++) {
  105. Vector3 vi1 = p_faces[i].face.vertex[k];
  106. Vector3 vi2 = p_faces[i].face.vertex[(k + 1) % 3];
  107. for (int l = 0; l < 3; l++) {
  108. Vector3 vj2 = p_faces[j].face.vertex[l];
  109. Vector3 vj1 = p_faces[j].face.vertex[(l + 1) % 3];
  110. if (vi1.distance_to(vj1) < 0.00001 &&
  111. vi2.distance_to(vj2) < 0.00001) {
  112. if (p_faces[i].links[k].face != -1) {
  113. ERR_PRINT("already linked\n");
  114. error = true;
  115. break;
  116. }
  117. if (p_faces[j].links[l].face != -1) {
  118. ERR_PRINT("already linked\n");
  119. error = true;
  120. break;
  121. }
  122. p_faces[i].links[k].face = j;
  123. p_faces[i].links[k].edge = l;
  124. p_faces[j].links[l].face = i;
  125. p_faces[j].links[l].edge = k;
  126. }
  127. }
  128. if (error)
  129. break;
  130. }
  131. if (error)
  132. break;
  133. }
  134. if (error)
  135. break;
  136. }
  137. for (int i = 0; i < len; i++) {
  138. p_faces[i].valid = true;
  139. for (int j = 0; j < 3; j++) {
  140. if (p_faces[i].links[j].face == -1)
  141. p_faces[i].valid = false;
  142. }
  143. /*printf("face %i is valid: %i, group %i. connected to %i:%i,%i:%i,%i:%i\n",i,p_faces[i].valid,p_faces[i].group,
  144. p_faces[i].links[0].face,
  145. p_faces[i].links[0].edge,
  146. p_faces[i].links[1].face,
  147. p_faces[i].links[1].edge,
  148. p_faces[i].links[2].face,
  149. p_faces[i].links[2].edge);*/
  150. }
  151. return error;
  152. }
  153. static bool _group_face(_FaceClassify *p_faces, int len, int p_index, int p_group) {
  154. if (p_faces[p_index].group >= 0)
  155. return false;
  156. p_faces[p_index].group = p_group;
  157. for (int i = 0; i < 3; i++) {
  158. ERR_FAIL_INDEX_V(p_faces[p_index].links[i].face, len, true);
  159. _group_face(p_faces, len, p_faces[p_index].links[i].face, p_group);
  160. }
  161. return true;
  162. }
  163. DVector<DVector<Face3> > Geometry::separate_objects(DVector<Face3> p_array) {
  164. DVector<DVector<Face3> > objects;
  165. int len = p_array.size();
  166. DVector<Face3>::Read r = p_array.read();
  167. const Face3 *arrayptr = r.ptr();
  168. DVector<_FaceClassify> fc;
  169. fc.resize(len);
  170. DVector<_FaceClassify>::Write fcw = fc.write();
  171. _FaceClassify *_fcptr = fcw.ptr();
  172. for (int i = 0; i < len; i++) {
  173. _fcptr[i].face = arrayptr[i];
  174. }
  175. bool error = _connect_faces(_fcptr, len, -1);
  176. if (error) {
  177. ERR_FAIL_COND_V(error, DVector<DVector<Face3> >()); // invalid geometry
  178. }
  179. /* group connected faces in separate objects */
  180. int group = 0;
  181. for (int i = 0; i < len; i++) {
  182. if (!_fcptr[i].valid)
  183. continue;
  184. if (_group_face(_fcptr, len, i, group)) {
  185. group++;
  186. }
  187. }
  188. /* group connected faces in separate objects */
  189. for (int i = 0; i < len; i++) {
  190. _fcptr[i].face = arrayptr[i];
  191. }
  192. if (group >= 0) {
  193. objects.resize(group);
  194. DVector<DVector<Face3> >::Write obw = objects.write();
  195. DVector<Face3> *group_faces = obw.ptr();
  196. for (int i = 0; i < len; i++) {
  197. if (!_fcptr[i].valid)
  198. continue;
  199. if (_fcptr[i].group >= 0 && _fcptr[i].group < group) {
  200. group_faces[_fcptr[i].group].push_back(_fcptr[i].face);
  201. }
  202. }
  203. }
  204. return objects;
  205. }
  206. /*** GEOMETRY WRAPPER ***/
  207. enum _CellFlags {
  208. _CELL_SOLID = 1,
  209. _CELL_EXTERIOR = 2,
  210. _CELL_STEP_MASK = 0x1C,
  211. _CELL_STEP_NONE = 0 << 2,
  212. _CELL_STEP_Y_POS = 1 << 2,
  213. _CELL_STEP_Y_NEG = 2 << 2,
  214. _CELL_STEP_X_POS = 3 << 2,
  215. _CELL_STEP_X_NEG = 4 << 2,
  216. _CELL_STEP_Z_POS = 5 << 2,
  217. _CELL_STEP_Z_NEG = 6 << 2,
  218. _CELL_STEP_DONE = 7 << 2,
  219. _CELL_PREV_MASK = 0xE0,
  220. _CELL_PREV_NONE = 0 << 5,
  221. _CELL_PREV_Y_POS = 1 << 5,
  222. _CELL_PREV_Y_NEG = 2 << 5,
  223. _CELL_PREV_X_POS = 3 << 5,
  224. _CELL_PREV_X_NEG = 4 << 5,
  225. _CELL_PREV_Z_POS = 5 << 5,
  226. _CELL_PREV_Z_NEG = 6 << 5,
  227. _CELL_PREV_FIRST = 7 << 5,
  228. };
  229. static inline void _plot_face(uint8_t ***p_cell_status, int x, int y, int z, int len_x, int len_y, int len_z, const Vector3 &voxelsize, const Face3 &p_face) {
  230. AABB aabb(Vector3(x, y, z), Vector3(len_x, len_y, len_z));
  231. aabb.pos = aabb.pos * voxelsize;
  232. aabb.size = aabb.size * voxelsize;
  233. if (!p_face.intersects_aabb(aabb))
  234. return;
  235. if (len_x == 1 && len_y == 1 && len_z == 1) {
  236. p_cell_status[x][y][z] = _CELL_SOLID;
  237. return;
  238. }
  239. int div_x = len_x > 1 ? 2 : 1;
  240. int div_y = len_y > 1 ? 2 : 1;
  241. int div_z = len_z > 1 ? 2 : 1;
  242. #define _SPLIT(m_i, m_div, m_v, m_len_v, m_new_v, m_new_len_v) \
  243. if (m_div == 1) { \
  244. m_new_v = m_v; \
  245. m_new_len_v = 1; \
  246. } else if (m_i == 0) { \
  247. m_new_v = m_v; \
  248. m_new_len_v = m_len_v / 2; \
  249. } else { \
  250. m_new_v = m_v + m_len_v / 2; \
  251. m_new_len_v = m_len_v - m_len_v / 2; \
  252. }
  253. int new_x;
  254. int new_len_x;
  255. int new_y;
  256. int new_len_y;
  257. int new_z;
  258. int new_len_z;
  259. for (int i = 0; i < div_x; i++) {
  260. _SPLIT(i, div_x, x, len_x, new_x, new_len_x);
  261. for (int j = 0; j < div_y; j++) {
  262. _SPLIT(j, div_y, y, len_y, new_y, new_len_y);
  263. for (int k = 0; k < div_z; k++) {
  264. _SPLIT(k, div_z, z, len_z, new_z, new_len_z);
  265. _plot_face(p_cell_status, new_x, new_y, new_z, new_len_x, new_len_y, new_len_z, voxelsize, p_face);
  266. }
  267. }
  268. }
  269. }
  270. static inline void _mark_outside(uint8_t ***p_cell_status, int x, int y, int z, int len_x, int len_y, int len_z) {
  271. if (p_cell_status[x][y][z] & 3)
  272. return; // nothing to do, already used and/or visited
  273. p_cell_status[x][y][z] = _CELL_PREV_FIRST;
  274. while (true) {
  275. uint8_t &c = p_cell_status[x][y][z];
  276. //printf("at %i,%i,%i\n",x,y,z);
  277. if ((c & _CELL_STEP_MASK) == _CELL_STEP_NONE) {
  278. /* Haven't been in here, mark as outside */
  279. p_cell_status[x][y][z] |= _CELL_EXTERIOR;
  280. //printf("not marked as anything, marking exterior\n");
  281. }
  282. //printf("cell step is %i\n",(c&_CELL_STEP_MASK));
  283. if ((c & _CELL_STEP_MASK) != _CELL_STEP_DONE) {
  284. /* if not done, increase step */
  285. c += 1 << 2;
  286. //printf("incrementing cell step\n");
  287. }
  288. if ((c & _CELL_STEP_MASK) == _CELL_STEP_DONE) {
  289. /* Go back */
  290. //printf("done, going back a cell\n");
  291. switch (c & _CELL_PREV_MASK) {
  292. case _CELL_PREV_FIRST: {
  293. //printf("at end, finished marking\n");
  294. return;
  295. } break;
  296. case _CELL_PREV_Y_POS: {
  297. y++;
  298. ERR_FAIL_COND(y >= len_y);
  299. } break;
  300. case _CELL_PREV_Y_NEG: {
  301. y--;
  302. ERR_FAIL_COND(y < 0);
  303. } break;
  304. case _CELL_PREV_X_POS: {
  305. x++;
  306. ERR_FAIL_COND(x >= len_x);
  307. } break;
  308. case _CELL_PREV_X_NEG: {
  309. x--;
  310. ERR_FAIL_COND(x < 0);
  311. } break;
  312. case _CELL_PREV_Z_POS: {
  313. z++;
  314. ERR_FAIL_COND(z >= len_z);
  315. } break;
  316. case _CELL_PREV_Z_NEG: {
  317. z--;
  318. ERR_FAIL_COND(z < 0);
  319. } break;
  320. default: {
  321. ERR_FAIL();
  322. }
  323. }
  324. continue;
  325. }
  326. //printf("attempting new cell!\n");
  327. int next_x = x, next_y = y, next_z = z;
  328. uint8_t prev = 0;
  329. switch (c & _CELL_STEP_MASK) {
  330. case _CELL_STEP_Y_POS: {
  331. next_y++;
  332. prev = _CELL_PREV_Y_NEG;
  333. } break;
  334. case _CELL_STEP_Y_NEG: {
  335. next_y--;
  336. prev = _CELL_PREV_Y_POS;
  337. } break;
  338. case _CELL_STEP_X_POS: {
  339. next_x++;
  340. prev = _CELL_PREV_X_NEG;
  341. } break;
  342. case _CELL_STEP_X_NEG: {
  343. next_x--;
  344. prev = _CELL_PREV_X_POS;
  345. } break;
  346. case _CELL_STEP_Z_POS: {
  347. next_z++;
  348. prev = _CELL_PREV_Z_NEG;
  349. } break;
  350. case _CELL_STEP_Z_NEG: {
  351. next_z--;
  352. prev = _CELL_PREV_Z_POS;
  353. } break;
  354. default: ERR_FAIL();
  355. }
  356. //printf("testing if new cell will be ok...!\n");
  357. if (next_x < 0 || next_x >= len_x)
  358. continue;
  359. if (next_y < 0 || next_y >= len_y)
  360. continue;
  361. if (next_z < 0 || next_z >= len_z)
  362. continue;
  363. //printf("testing if new cell is traversable\n");
  364. if (p_cell_status[next_x][next_y][next_z] & 3)
  365. continue;
  366. //printf("move to it\n");
  367. x = next_x;
  368. y = next_y;
  369. z = next_z;
  370. p_cell_status[x][y][z] |= prev;
  371. }
  372. }
  373. static inline void _build_faces(uint8_t ***p_cell_status, int x, int y, int z, int len_x, int len_y, int len_z, DVector<Face3> &p_faces) {
  374. ERR_FAIL_INDEX(x, len_x);
  375. ERR_FAIL_INDEX(y, len_y);
  376. ERR_FAIL_INDEX(z, len_z);
  377. if (p_cell_status[x][y][z] & _CELL_EXTERIOR)
  378. return;
  379. /* static const Vector3 vertices[8]={
  380. Vector3(0,0,0),
  381. Vector3(0,0,1),
  382. Vector3(0,1,0),
  383. Vector3(0,1,1),
  384. Vector3(1,0,0),
  385. Vector3(1,0,1),
  386. Vector3(1,1,0),
  387. Vector3(1,1,1),
  388. };
  389. */
  390. #define vert(m_idx) Vector3((m_idx & 4) >> 2, (m_idx & 2) >> 1, m_idx & 1)
  391. static const uint8_t indices[6][4] = {
  392. { 7, 6, 4, 5 },
  393. { 7, 3, 2, 6 },
  394. { 7, 5, 1, 3 },
  395. { 0, 2, 3, 1 },
  396. { 0, 1, 5, 4 },
  397. { 0, 4, 6, 2 },
  398. };
  399. /*
  400. {0,1,2,3},
  401. {0,1,4,5},
  402. {0,2,4,6},
  403. {4,5,6,7},
  404. {2,3,7,6},
  405. {1,3,5,7},
  406. {0,2,3,1},
  407. {0,1,5,4},
  408. {0,4,6,2},
  409. {7,6,4,5},
  410. {7,3,2,6},
  411. {7,5,1,3},
  412. */
  413. for (int i = 0; i < 6; i++) {
  414. Vector3 face_points[4];
  415. int disp_x = x + ((i % 3) == 0 ? ((i < 3) ? 1 : -1) : 0);
  416. int disp_y = y + (((i - 1) % 3) == 0 ? ((i < 3) ? 1 : -1) : 0);
  417. int disp_z = z + (((i - 2) % 3) == 0 ? ((i < 3) ? 1 : -1) : 0);
  418. bool plot = false;
  419. if (disp_x < 0 || disp_x >= len_x)
  420. plot = true;
  421. if (disp_y < 0 || disp_y >= len_y)
  422. plot = true;
  423. if (disp_z < 0 || disp_z >= len_z)
  424. plot = true;
  425. if (!plot && (p_cell_status[disp_x][disp_y][disp_z] & _CELL_EXTERIOR))
  426. plot = true;
  427. if (!plot)
  428. continue;
  429. for (int j = 0; j < 4; j++)
  430. face_points[j] = vert(indices[i][j]) + Vector3(x, y, z);
  431. p_faces.push_back(
  432. Face3(
  433. face_points[0],
  434. face_points[1],
  435. face_points[2]));
  436. p_faces.push_back(
  437. Face3(
  438. face_points[2],
  439. face_points[3],
  440. face_points[0]));
  441. }
  442. }
  443. DVector<Face3> Geometry::wrap_geometry(DVector<Face3> p_array, float *p_error) {
  444. #define _MIN_SIZE 1.0
  445. #define _MAX_LENGTH 20
  446. int face_count = p_array.size();
  447. DVector<Face3>::Read facesr = p_array.read();
  448. const Face3 *faces = facesr.ptr();
  449. AABB global_aabb;
  450. for (int i = 0; i < face_count; i++) {
  451. if (i == 0) {
  452. global_aabb = faces[i].get_aabb();
  453. } else {
  454. global_aabb.merge_with(faces[i].get_aabb());
  455. }
  456. }
  457. global_aabb.grow_by(0.01); // avoid numerical error
  458. // determine amount of cells in grid axis
  459. int div_x, div_y, div_z;
  460. if (global_aabb.size.x / _MIN_SIZE < _MAX_LENGTH)
  461. div_x = (int)(global_aabb.size.x / _MIN_SIZE) + 1;
  462. else
  463. div_x = _MAX_LENGTH;
  464. if (global_aabb.size.y / _MIN_SIZE < _MAX_LENGTH)
  465. div_y = (int)(global_aabb.size.y / _MIN_SIZE) + 1;
  466. else
  467. div_y = _MAX_LENGTH;
  468. if (global_aabb.size.z / _MIN_SIZE < _MAX_LENGTH)
  469. div_z = (int)(global_aabb.size.z / _MIN_SIZE) + 1;
  470. else
  471. div_z = _MAX_LENGTH;
  472. Vector3 voxelsize = global_aabb.size;
  473. voxelsize.x /= div_x;
  474. voxelsize.y /= div_y;
  475. voxelsize.z /= div_z;
  476. // create and initialize cells to zero
  477. //print_line("Wrapper: Initializing Cells");
  478. uint8_t ***cell_status = memnew_arr(uint8_t **, div_x);
  479. for (int i = 0; i < div_x; i++) {
  480. cell_status[i] = memnew_arr(uint8_t *, div_y);
  481. for (int j = 0; j < div_y; j++) {
  482. cell_status[i][j] = memnew_arr(uint8_t, div_z);
  483. for (int k = 0; k < div_z; k++) {
  484. cell_status[i][j][k] = 0;
  485. }
  486. }
  487. }
  488. // plot faces into cells
  489. //print_line("Wrapper (1/6): Plotting Faces");
  490. for (int i = 0; i < face_count; i++) {
  491. Face3 f = faces[i];
  492. for (int j = 0; j < 3; j++) {
  493. f.vertex[j] -= global_aabb.pos;
  494. }
  495. _plot_face(cell_status, 0, 0, 0, div_x, div_y, div_z, voxelsize, f);
  496. }
  497. // determine which cells connect to the outside by traversing the outside and recursively flood-fill marking
  498. //print_line("Wrapper (2/6): Flood Filling");
  499. for (int i = 0; i < div_x; i++) {
  500. for (int j = 0; j < div_y; j++) {
  501. _mark_outside(cell_status, i, j, 0, div_x, div_y, div_z);
  502. _mark_outside(cell_status, i, j, div_z - 1, div_x, div_y, div_z);
  503. }
  504. }
  505. for (int i = 0; i < div_z; i++) {
  506. for (int j = 0; j < div_y; j++) {
  507. _mark_outside(cell_status, 0, j, i, div_x, div_y, div_z);
  508. _mark_outside(cell_status, div_x - 1, j, i, div_x, div_y, div_z);
  509. }
  510. }
  511. for (int i = 0; i < div_x; i++) {
  512. for (int j = 0; j < div_z; j++) {
  513. _mark_outside(cell_status, i, 0, j, div_x, div_y, div_z);
  514. _mark_outside(cell_status, i, div_y - 1, j, div_x, div_y, div_z);
  515. }
  516. }
  517. // build faces for the inside-outside cell divisors
  518. //print_line("Wrapper (3/6): Building Faces");
  519. DVector<Face3> wrapped_faces;
  520. for (int i = 0; i < div_x; i++) {
  521. for (int j = 0; j < div_y; j++) {
  522. for (int k = 0; k < div_z; k++) {
  523. _build_faces(cell_status, i, j, k, div_x, div_y, div_z, wrapped_faces);
  524. }
  525. }
  526. }
  527. //print_line("Wrapper (4/6): Transforming Back Vertices");
  528. // transform face vertices to global coords
  529. int wrapped_faces_count = wrapped_faces.size();
  530. DVector<Face3>::Write wrapped_facesw = wrapped_faces.write();
  531. Face3 *wrapped_faces_ptr = wrapped_facesw.ptr();
  532. for (int i = 0; i < wrapped_faces_count; i++) {
  533. for (int j = 0; j < 3; j++) {
  534. Vector3 &v = wrapped_faces_ptr[i].vertex[j];
  535. v = v * voxelsize;
  536. v += global_aabb.pos;
  537. }
  538. }
  539. // clean up grid
  540. //print_line("Wrapper (5/6): Grid Cleanup");
  541. for (int i = 0; i < div_x; i++) {
  542. for (int j = 0; j < div_y; j++) {
  543. memdelete_arr(cell_status[i][j]);
  544. }
  545. memdelete_arr(cell_status[i]);
  546. }
  547. memdelete_arr(cell_status);
  548. if (p_error)
  549. *p_error = voxelsize.length();
  550. //print_line("Wrapper (6/6): Finished.");
  551. return wrapped_faces;
  552. }
  553. Geometry::MeshData Geometry::build_convex_mesh(const DVector<Plane> &p_planes) {
  554. MeshData mesh;
  555. #define SUBPLANE_SIZE 1024.0
  556. float subplane_size = 1024.0; // should compute this from the actual plane
  557. for (int i = 0; i < p_planes.size(); i++) {
  558. Plane p = p_planes[i];
  559. Vector3 ref = Vector3(0.0, 1.0, 0.0);
  560. if (ABS(p.normal.dot(ref)) > 0.95)
  561. ref = Vector3(0.0, 0.0, 1.0); // change axis
  562. Vector3 right = p.normal.cross(ref).normalized();
  563. Vector3 up = p.normal.cross(right).normalized();
  564. Vector<Vector3> vertices;
  565. Vector3 center = p.get_any_point();
  566. // make a quad clockwise
  567. vertices.push_back(center - up * subplane_size + right * subplane_size);
  568. vertices.push_back(center - up * subplane_size - right * subplane_size);
  569. vertices.push_back(center + up * subplane_size - right * subplane_size);
  570. vertices.push_back(center + up * subplane_size + right * subplane_size);
  571. for (int j = 0; j < p_planes.size(); j++) {
  572. if (j == i)
  573. continue;
  574. Vector<Vector3> new_vertices;
  575. Plane clip = p_planes[j];
  576. if (clip.normal.dot(p.normal) > 0.95)
  577. continue;
  578. if (vertices.size() < 3)
  579. break;
  580. for (int k = 0; k < vertices.size(); k++) {
  581. int k_n = (k + 1) % vertices.size();
  582. Vector3 edge0_A = vertices[k];
  583. Vector3 edge1_A = vertices[k_n];
  584. real_t dist0 = clip.distance_to(edge0_A);
  585. real_t dist1 = clip.distance_to(edge1_A);
  586. if (dist0 <= 0) { // behind plane
  587. new_vertices.push_back(vertices[k]);
  588. }
  589. // check for different sides and non coplanar
  590. if ((dist0 * dist1) < 0) {
  591. // calculate intersection
  592. Vector3 rel = edge1_A - edge0_A;
  593. real_t den = clip.normal.dot(rel);
  594. if (Math::abs(den) < CMP_EPSILON)
  595. continue; // point too short
  596. real_t dist = -(clip.normal.dot(edge0_A) - clip.d) / den;
  597. Vector3 inters = edge0_A + rel * dist;
  598. new_vertices.push_back(inters);
  599. }
  600. }
  601. vertices = new_vertices;
  602. }
  603. if (vertices.size() < 3)
  604. continue;
  605. //result is a clockwise face
  606. MeshData::Face face;
  607. // add face indices
  608. for (int j = 0; j < vertices.size(); j++) {
  609. int idx = -1;
  610. for (int k = 0; k < mesh.vertices.size(); k++) {
  611. if (mesh.vertices[k].distance_to(vertices[j]) < 0.001) {
  612. idx = k;
  613. break;
  614. }
  615. }
  616. if (idx == -1) {
  617. idx = mesh.vertices.size();
  618. mesh.vertices.push_back(vertices[j]);
  619. }
  620. face.indices.push_back(idx);
  621. }
  622. face.plane = p;
  623. mesh.faces.push_back(face);
  624. //add edge
  625. for (int j = 0; j < face.indices.size(); j++) {
  626. int a = face.indices[j];
  627. int b = face.indices[(j + 1) % face.indices.size()];
  628. bool found = false;
  629. for (int k = 0; k < mesh.edges.size(); k++) {
  630. if (mesh.edges[k].a == a && mesh.edges[k].b == b) {
  631. found = true;
  632. break;
  633. }
  634. if (mesh.edges[k].b == a && mesh.edges[k].a == b) {
  635. found = true;
  636. break;
  637. }
  638. }
  639. if (found)
  640. continue;
  641. MeshData::Edge edge;
  642. edge.a = a;
  643. edge.b = b;
  644. mesh.edges.push_back(edge);
  645. }
  646. }
  647. return mesh;
  648. }
  649. DVector<Plane> Geometry::build_box_planes(const Vector3 &p_extents) {
  650. DVector<Plane> planes;
  651. planes.push_back(Plane(Vector3(1, 0, 0), p_extents.x));
  652. planes.push_back(Plane(Vector3(-1, 0, 0), p_extents.x));
  653. planes.push_back(Plane(Vector3(0, 1, 0), p_extents.y));
  654. planes.push_back(Plane(Vector3(0, -1, 0), p_extents.y));
  655. planes.push_back(Plane(Vector3(0, 0, 1), p_extents.z));
  656. planes.push_back(Plane(Vector3(0, 0, -1), p_extents.z));
  657. return planes;
  658. }
  659. DVector<Plane> Geometry::build_cylinder_planes(float p_radius, float p_height, int p_sides, Vector3::Axis p_axis) {
  660. DVector<Plane> planes;
  661. for (int i = 0; i < p_sides; i++) {
  662. Vector3 normal;
  663. normal[(p_axis + 1) % 3] = Math::cos(i * (2.0 * Math_PI) / p_sides);
  664. normal[(p_axis + 2) % 3] = Math::sin(i * (2.0 * Math_PI) / p_sides);
  665. planes.push_back(Plane(normal, p_radius));
  666. }
  667. Vector3 axis;
  668. axis[p_axis] = 1.0;
  669. planes.push_back(Plane(axis, p_height * 0.5));
  670. planes.push_back(Plane(-axis, p_height * 0.5));
  671. return planes;
  672. }
  673. DVector<Plane> Geometry::build_sphere_planes(float p_radius, int p_lats, int p_lons, Vector3::Axis p_axis) {
  674. DVector<Plane> planes;
  675. Vector3 axis;
  676. axis[p_axis] = 1.0;
  677. Vector3 axis_neg;
  678. axis_neg[(p_axis + 1) % 3] = 1.0;
  679. axis_neg[(p_axis + 2) % 3] = 1.0;
  680. axis_neg[p_axis] = -1.0;
  681. for (int i = 0; i < p_lons; i++) {
  682. Vector3 normal;
  683. normal[(p_axis + 1) % 3] = Math::cos(i * (2.0 * Math_PI) / p_lons);
  684. normal[(p_axis + 2) % 3] = Math::sin(i * (2.0 * Math_PI) / p_lons);
  685. planes.push_back(Plane(normal, p_radius));
  686. for (int j = 1; j <= p_lats; j++) {
  687. //todo this is stupid, fix
  688. Vector3 angle = normal.linear_interpolate(axis, j / (float)p_lats).normalized();
  689. Vector3 pos = angle * p_radius;
  690. planes.push_back(Plane(pos, angle));
  691. planes.push_back(Plane(pos * axis_neg, angle * axis_neg));
  692. }
  693. }
  694. return planes;
  695. }
  696. DVector<Plane> Geometry::build_capsule_planes(float p_radius, float p_height, int p_sides, int p_lats, Vector3::Axis p_axis) {
  697. DVector<Plane> planes;
  698. Vector3 axis;
  699. axis[p_axis] = 1.0;
  700. Vector3 axis_neg;
  701. axis_neg[(p_axis + 1) % 3] = 1.0;
  702. axis_neg[(p_axis + 2) % 3] = 1.0;
  703. axis_neg[p_axis] = -1.0;
  704. for (int i = 0; i < p_sides; i++) {
  705. Vector3 normal;
  706. normal[(p_axis + 1) % 3] = Math::cos(i * (2.0 * Math_PI) / p_sides);
  707. normal[(p_axis + 2) % 3] = Math::sin(i * (2.0 * Math_PI) / p_sides);
  708. planes.push_back(Plane(normal, p_radius));
  709. for (int j = 1; j <= p_lats; j++) {
  710. Vector3 angle = normal.linear_interpolate(axis, j / (float)p_lats).normalized();
  711. Vector3 pos = axis * p_height * 0.5 + angle * p_radius;
  712. planes.push_back(Plane(pos, angle));
  713. planes.push_back(Plane(pos * axis_neg, angle * axis_neg));
  714. }
  715. }
  716. return planes;
  717. }
  718. struct _AtlasWorkRect {
  719. Size2i s;
  720. Point2i p;
  721. int idx;
  722. _FORCE_INLINE_ bool operator<(const _AtlasWorkRect &p_r) const { return s.width > p_r.s.width; };
  723. };
  724. struct _AtlasWorkRectResult {
  725. Vector<_AtlasWorkRect> result;
  726. int max_w;
  727. int max_h;
  728. };
  729. void Geometry::make_atlas(const Vector<Size2i> &p_rects, Vector<Point2i> &r_result, Size2i &r_size) {
  730. //super simple, almost brute force scanline stacking fitter
  731. //it's pretty basic for now, but it tries to make sure that the aspect ratio of the
  732. //resulting atlas is somehow square. This is necesary because video cards have limits
  733. //on texture size (usually 2048 or 4096), so the more square a texture, the more chances
  734. //it will work in every hardware.
  735. // for example, it will prioritize a 1024x1024 atlas (works everywhere) instead of a
  736. // 256x8192 atlas (won't work anywhere).
  737. ERR_FAIL_COND(p_rects.size() == 0);
  738. Vector<_AtlasWorkRect> wrects;
  739. wrects.resize(p_rects.size());
  740. for (int i = 0; i < p_rects.size(); i++) {
  741. wrects[i].s = p_rects[i];
  742. wrects[i].idx = i;
  743. }
  744. wrects.sort();
  745. int widest = wrects[0].s.width;
  746. Vector<_AtlasWorkRectResult> results;
  747. for (int i = 0; i <= 12; i++) {
  748. int w = 1 << i;
  749. int max_h = 0;
  750. int max_w = 0;
  751. if (w < widest)
  752. continue;
  753. Vector<int> hmax;
  754. hmax.resize(w);
  755. for (int j = 0; j < w; j++)
  756. hmax[j] = 0;
  757. //place them
  758. int ofs = 0;
  759. int limit_h = 0;
  760. for (int j = 0; j < wrects.size(); j++) {
  761. if (ofs + wrects[j].s.width > w) {
  762. ofs = 0;
  763. }
  764. int from_y = 0;
  765. for (int k = 0; k < wrects[j].s.width; k++) {
  766. if (hmax[ofs + k] > from_y)
  767. from_y = hmax[ofs + k];
  768. }
  769. wrects[j].p.x = ofs;
  770. wrects[j].p.y = from_y;
  771. int end_h = from_y + wrects[j].s.height;
  772. int end_w = ofs + wrects[j].s.width;
  773. if (ofs == 0)
  774. limit_h = end_h;
  775. for (int k = 0; k < wrects[j].s.width; k++) {
  776. hmax[ofs + k] = end_h;
  777. }
  778. if (end_h > max_h)
  779. max_h = end_h;
  780. if (end_w > max_w)
  781. max_w = end_w;
  782. if (ofs == 0 || end_h > limit_h) //while h limit not reched, keep stacking
  783. ofs += wrects[j].s.width;
  784. }
  785. _AtlasWorkRectResult result;
  786. result.result = wrects;
  787. result.max_h = max_h;
  788. result.max_w = max_w;
  789. results.push_back(result);
  790. }
  791. //find the result with the best aspect ratio
  792. int best = -1;
  793. float best_aspect = 1e20;
  794. for (int i = 0; i < results.size(); i++) {
  795. float h = next_power_of_2(results[i].max_h);
  796. float w = next_power_of_2(results[i].max_w);
  797. float aspect = h > w ? h / w : w / h;
  798. if (aspect < best_aspect) {
  799. best = i;
  800. best_aspect = aspect;
  801. }
  802. }
  803. r_result.resize(p_rects.size());
  804. for (int i = 0; i < p_rects.size(); i++) {
  805. r_result[results[best].result[i].idx] = results[best].result[i].p;
  806. }
  807. r_size = Size2(results[best].max_w, results[best].max_h);
  808. }