geometry.cpp 25 KB

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