geometry_3d.cpp 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  1. /*************************************************************************/
  2. /* geometry_3d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_3d.h"
  31. #include "core/string/print_string.h"
  32. #include "thirdparty/misc/clipper.hpp"
  33. #include "thirdparty/misc/polypartition.h"
  34. void Geometry3D::MeshData::optimize_vertices() {
  35. Map<int, int> vtx_remap;
  36. for (int i = 0; i < faces.size(); i++) {
  37. for (int j = 0; j < faces[i].indices.size(); j++) {
  38. int idx = faces[i].indices[j];
  39. if (!vtx_remap.has(idx)) {
  40. int ni = vtx_remap.size();
  41. vtx_remap[idx] = ni;
  42. }
  43. faces.write[i].indices.write[j] = vtx_remap[idx];
  44. }
  45. }
  46. for (int i = 0; i < edges.size(); i++) {
  47. int a = edges[i].a;
  48. int b = edges[i].b;
  49. if (!vtx_remap.has(a)) {
  50. int ni = vtx_remap.size();
  51. vtx_remap[a] = ni;
  52. }
  53. if (!vtx_remap.has(b)) {
  54. int ni = vtx_remap.size();
  55. vtx_remap[b] = ni;
  56. }
  57. edges.write[i].a = vtx_remap[a];
  58. edges.write[i].b = vtx_remap[b];
  59. }
  60. Vector<Vector3> new_vertices;
  61. new_vertices.resize(vtx_remap.size());
  62. for (int i = 0; i < vertices.size(); i++) {
  63. if (vtx_remap.has(i)) {
  64. new_vertices.write[vtx_remap[i]] = vertices[i];
  65. }
  66. }
  67. vertices = new_vertices;
  68. }
  69. struct _FaceClassify {
  70. struct _Link {
  71. int face = -1;
  72. int edge = -1;
  73. void clear() {
  74. face = -1;
  75. edge = -1;
  76. }
  77. _Link() {}
  78. };
  79. bool valid = false;
  80. int group = -1;
  81. _Link links[3];
  82. Face3 face;
  83. _FaceClassify() {}
  84. };
  85. static bool _connect_faces(_FaceClassify *p_faces, int len, int p_group) {
  86. // Connect faces, error will occur if an edge is shared between more than 2 faces.
  87. // Clear connections.
  88. bool error = false;
  89. for (int i = 0; i < len; i++) {
  90. for (int j = 0; j < 3; j++) {
  91. p_faces[i].links[j].clear();
  92. }
  93. }
  94. for (int i = 0; i < len; i++) {
  95. if (p_faces[i].group != p_group) {
  96. continue;
  97. }
  98. for (int j = i + 1; j < len; j++) {
  99. if (p_faces[j].group != p_group) {
  100. continue;
  101. }
  102. for (int k = 0; k < 3; k++) {
  103. Vector3 vi1 = p_faces[i].face.vertex[k];
  104. Vector3 vi2 = p_faces[i].face.vertex[(k + 1) % 3];
  105. for (int l = 0; l < 3; l++) {
  106. Vector3 vj2 = p_faces[j].face.vertex[l];
  107. Vector3 vj1 = p_faces[j].face.vertex[(l + 1) % 3];
  108. if (vi1.distance_to(vj1) < 0.00001 &&
  109. vi2.distance_to(vj2) < 0.00001) {
  110. if (p_faces[i].links[k].face != -1) {
  111. ERR_PRINT("already linked\n");
  112. error = true;
  113. break;
  114. }
  115. if (p_faces[j].links[l].face != -1) {
  116. ERR_PRINT("already linked\n");
  117. error = true;
  118. break;
  119. }
  120. p_faces[i].links[k].face = j;
  121. p_faces[i].links[k].edge = l;
  122. p_faces[j].links[l].face = i;
  123. p_faces[j].links[l].edge = k;
  124. }
  125. }
  126. if (error) {
  127. break;
  128. }
  129. }
  130. if (error) {
  131. break;
  132. }
  133. }
  134. if (error) {
  135. break;
  136. }
  137. }
  138. for (int i = 0; i < len; i++) {
  139. p_faces[i].valid = true;
  140. for (int j = 0; j < 3; j++) {
  141. if (p_faces[i].links[j].face == -1) {
  142. p_faces[i].valid = false;
  143. }
  144. }
  145. }
  146. return error;
  147. }
  148. static bool _group_face(_FaceClassify *p_faces, int len, int p_index, int p_group) {
  149. if (p_faces[p_index].group >= 0) {
  150. return false;
  151. }
  152. p_faces[p_index].group = p_group;
  153. for (int i = 0; i < 3; i++) {
  154. ERR_FAIL_INDEX_V(p_faces[p_index].links[i].face, len, true);
  155. _group_face(p_faces, len, p_faces[p_index].links[i].face, p_group);
  156. }
  157. return true;
  158. }
  159. Vector<Vector<Face3>> Geometry3D::separate_objects(Vector<Face3> p_array) {
  160. Vector<Vector<Face3>> objects;
  161. int len = p_array.size();
  162. const Face3 *arrayptr = p_array.ptr();
  163. Vector<_FaceClassify> fc;
  164. fc.resize(len);
  165. _FaceClassify *_fcptr = fc.ptrw();
  166. for (int i = 0; i < len; i++) {
  167. _fcptr[i].face = arrayptr[i];
  168. }
  169. bool error = _connect_faces(_fcptr, len, -1);
  170. ERR_FAIL_COND_V_MSG(error, Vector<Vector<Face3>>(), "Invalid geometry.");
  171. // Group connected faces in separate objects.
  172. int group = 0;
  173. for (int i = 0; i < len; i++) {
  174. if (!_fcptr[i].valid) {
  175. continue;
  176. }
  177. if (_group_face(_fcptr, len, i, group)) {
  178. group++;
  179. }
  180. }
  181. // Group connected faces in separate objects.
  182. for (int i = 0; i < len; i++) {
  183. _fcptr[i].face = arrayptr[i];
  184. }
  185. if (group >= 0) {
  186. objects.resize(group);
  187. Vector<Face3> *group_faces = objects.ptrw();
  188. for (int i = 0; i < len; i++) {
  189. if (!_fcptr[i].valid) {
  190. continue;
  191. }
  192. if (_fcptr[i].group >= 0 && _fcptr[i].group < group) {
  193. group_faces[_fcptr[i].group].push_back(_fcptr[i].face);
  194. }
  195. }
  196. }
  197. return objects;
  198. }
  199. /*** GEOMETRY WRAPPER ***/
  200. enum _CellFlags {
  201. _CELL_SOLID = 1,
  202. _CELL_EXTERIOR = 2,
  203. _CELL_STEP_MASK = 0x1C,
  204. _CELL_STEP_NONE = 0 << 2,
  205. _CELL_STEP_Y_POS = 1 << 2,
  206. _CELL_STEP_Y_NEG = 2 << 2,
  207. _CELL_STEP_X_POS = 3 << 2,
  208. _CELL_STEP_X_NEG = 4 << 2,
  209. _CELL_STEP_Z_POS = 5 << 2,
  210. _CELL_STEP_Z_NEG = 6 << 2,
  211. _CELL_STEP_DONE = 7 << 2,
  212. _CELL_PREV_MASK = 0xE0,
  213. _CELL_PREV_NONE = 0 << 5,
  214. _CELL_PREV_Y_POS = 1 << 5,
  215. _CELL_PREV_Y_NEG = 2 << 5,
  216. _CELL_PREV_X_POS = 3 << 5,
  217. _CELL_PREV_X_NEG = 4 << 5,
  218. _CELL_PREV_Z_POS = 5 << 5,
  219. _CELL_PREV_Z_NEG = 6 << 5,
  220. _CELL_PREV_FIRST = 7 << 5,
  221. };
  222. 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) {
  223. AABB aabb(Vector3(x, y, z), Vector3(len_x, len_y, len_z));
  224. aabb.position = aabb.position * voxelsize;
  225. aabb.size = aabb.size * voxelsize;
  226. if (!p_face.intersects_aabb(aabb)) {
  227. return;
  228. }
  229. if (len_x == 1 && len_y == 1 && len_z == 1) {
  230. p_cell_status[x][y][z] = _CELL_SOLID;
  231. return;
  232. }
  233. int div_x = len_x > 1 ? 2 : 1;
  234. int div_y = len_y > 1 ? 2 : 1;
  235. int div_z = len_z > 1 ? 2 : 1;
  236. #define _SPLIT(m_i, m_div, m_v, m_len_v, m_new_v, m_new_len_v) \
  237. if (m_div == 1) { \
  238. m_new_v = m_v; \
  239. m_new_len_v = 1; \
  240. } else if (m_i == 0) { \
  241. m_new_v = m_v; \
  242. m_new_len_v = m_len_v / 2; \
  243. } else { \
  244. m_new_v = m_v + m_len_v / 2; \
  245. m_new_len_v = m_len_v - m_len_v / 2; \
  246. }
  247. int new_x;
  248. int new_len_x;
  249. int new_y;
  250. int new_len_y;
  251. int new_z;
  252. int new_len_z;
  253. for (int i = 0; i < div_x; i++) {
  254. _SPLIT(i, div_x, x, len_x, new_x, new_len_x);
  255. for (int j = 0; j < div_y; j++) {
  256. _SPLIT(j, div_y, y, len_y, new_y, new_len_y);
  257. for (int k = 0; k < div_z; k++) {
  258. _SPLIT(k, div_z, z, len_z, new_z, new_len_z);
  259. _plot_face(p_cell_status, new_x, new_y, new_z, new_len_x, new_len_y, new_len_z, voxelsize, p_face);
  260. }
  261. }
  262. }
  263. }
  264. 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) {
  265. if (p_cell_status[x][y][z] & 3) {
  266. return; // Nothing to do, already used and/or visited.
  267. }
  268. p_cell_status[x][y][z] = _CELL_PREV_FIRST;
  269. while (true) {
  270. uint8_t &c = p_cell_status[x][y][z];
  271. if ((c & _CELL_STEP_MASK) == _CELL_STEP_NONE) {
  272. // Haven't been in here, mark as outside.
  273. p_cell_status[x][y][z] |= _CELL_EXTERIOR;
  274. }
  275. if ((c & _CELL_STEP_MASK) != _CELL_STEP_DONE) {
  276. // If not done, increase step.
  277. c += 1 << 2;
  278. }
  279. if ((c & _CELL_STEP_MASK) == _CELL_STEP_DONE) {
  280. // Go back.
  281. switch (c & _CELL_PREV_MASK) {
  282. case _CELL_PREV_FIRST: {
  283. return;
  284. } break;
  285. case _CELL_PREV_Y_POS: {
  286. y++;
  287. ERR_FAIL_COND(y >= len_y);
  288. } break;
  289. case _CELL_PREV_Y_NEG: {
  290. y--;
  291. ERR_FAIL_COND(y < 0);
  292. } break;
  293. case _CELL_PREV_X_POS: {
  294. x++;
  295. ERR_FAIL_COND(x >= len_x);
  296. } break;
  297. case _CELL_PREV_X_NEG: {
  298. x--;
  299. ERR_FAIL_COND(x < 0);
  300. } break;
  301. case _CELL_PREV_Z_POS: {
  302. z++;
  303. ERR_FAIL_COND(z >= len_z);
  304. } break;
  305. case _CELL_PREV_Z_NEG: {
  306. z--;
  307. ERR_FAIL_COND(z < 0);
  308. } break;
  309. default: {
  310. ERR_FAIL();
  311. }
  312. }
  313. continue;
  314. }
  315. int next_x = x, next_y = y, next_z = z;
  316. uint8_t prev = 0;
  317. switch (c & _CELL_STEP_MASK) {
  318. case _CELL_STEP_Y_POS: {
  319. next_y++;
  320. prev = _CELL_PREV_Y_NEG;
  321. } break;
  322. case _CELL_STEP_Y_NEG: {
  323. next_y--;
  324. prev = _CELL_PREV_Y_POS;
  325. } break;
  326. case _CELL_STEP_X_POS: {
  327. next_x++;
  328. prev = _CELL_PREV_X_NEG;
  329. } break;
  330. case _CELL_STEP_X_NEG: {
  331. next_x--;
  332. prev = _CELL_PREV_X_POS;
  333. } break;
  334. case _CELL_STEP_Z_POS: {
  335. next_z++;
  336. prev = _CELL_PREV_Z_NEG;
  337. } break;
  338. case _CELL_STEP_Z_NEG: {
  339. next_z--;
  340. prev = _CELL_PREV_Z_POS;
  341. } break;
  342. default:
  343. ERR_FAIL();
  344. }
  345. if (next_x < 0 || next_x >= len_x) {
  346. continue;
  347. }
  348. if (next_y < 0 || next_y >= len_y) {
  349. continue;
  350. }
  351. if (next_z < 0 || next_z >= len_z) {
  352. continue;
  353. }
  354. if (p_cell_status[next_x][next_y][next_z] & 3) {
  355. continue;
  356. }
  357. x = next_x;
  358. y = next_y;
  359. z = next_z;
  360. p_cell_status[x][y][z] |= prev;
  361. }
  362. }
  363. 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, Vector<Face3> &p_faces) {
  364. ERR_FAIL_INDEX(x, len_x);
  365. ERR_FAIL_INDEX(y, len_y);
  366. ERR_FAIL_INDEX(z, len_z);
  367. if (p_cell_status[x][y][z] & _CELL_EXTERIOR) {
  368. return;
  369. }
  370. #define vert(m_idx) Vector3(((m_idx)&4) >> 2, ((m_idx)&2) >> 1, (m_idx)&1)
  371. static const uint8_t indices[6][4] = {
  372. { 7, 6, 4, 5 },
  373. { 7, 3, 2, 6 },
  374. { 7, 5, 1, 3 },
  375. { 0, 2, 3, 1 },
  376. { 0, 1, 5, 4 },
  377. { 0, 4, 6, 2 },
  378. };
  379. for (int i = 0; i < 6; i++) {
  380. Vector3 face_points[4];
  381. int disp_x = x + ((i % 3) == 0 ? ((i < 3) ? 1 : -1) : 0);
  382. int disp_y = y + (((i - 1) % 3) == 0 ? ((i < 3) ? 1 : -1) : 0);
  383. int disp_z = z + (((i - 2) % 3) == 0 ? ((i < 3) ? 1 : -1) : 0);
  384. bool plot = false;
  385. if (disp_x < 0 || disp_x >= len_x) {
  386. plot = true;
  387. }
  388. if (disp_y < 0 || disp_y >= len_y) {
  389. plot = true;
  390. }
  391. if (disp_z < 0 || disp_z >= len_z) {
  392. plot = true;
  393. }
  394. if (!plot && (p_cell_status[disp_x][disp_y][disp_z] & _CELL_EXTERIOR)) {
  395. plot = true;
  396. }
  397. if (!plot) {
  398. continue;
  399. }
  400. for (int j = 0; j < 4; j++) {
  401. face_points[j] = vert(indices[i][j]) + Vector3(x, y, z);
  402. }
  403. p_faces.push_back(
  404. Face3(
  405. face_points[0],
  406. face_points[1],
  407. face_points[2]));
  408. p_faces.push_back(
  409. Face3(
  410. face_points[2],
  411. face_points[3],
  412. face_points[0]));
  413. }
  414. }
  415. Vector<Face3> Geometry3D::wrap_geometry(Vector<Face3> p_array, real_t *p_error) {
  416. #define _MIN_SIZE 1.0
  417. #define _MAX_LENGTH 20
  418. int face_count = p_array.size();
  419. const Face3 *faces = p_array.ptr();
  420. AABB global_aabb;
  421. for (int i = 0; i < face_count; i++) {
  422. if (i == 0) {
  423. global_aabb = faces[i].get_aabb();
  424. } else {
  425. global_aabb.merge_with(faces[i].get_aabb());
  426. }
  427. }
  428. global_aabb.grow_by(0.01); // Avoid numerical error.
  429. // Determine amount of cells in grid axis.
  430. int div_x, div_y, div_z;
  431. if (global_aabb.size.x / _MIN_SIZE < _MAX_LENGTH) {
  432. div_x = (int)(global_aabb.size.x / _MIN_SIZE) + 1;
  433. } else {
  434. div_x = _MAX_LENGTH;
  435. }
  436. if (global_aabb.size.y / _MIN_SIZE < _MAX_LENGTH) {
  437. div_y = (int)(global_aabb.size.y / _MIN_SIZE) + 1;
  438. } else {
  439. div_y = _MAX_LENGTH;
  440. }
  441. if (global_aabb.size.z / _MIN_SIZE < _MAX_LENGTH) {
  442. div_z = (int)(global_aabb.size.z / _MIN_SIZE) + 1;
  443. } else {
  444. div_z = _MAX_LENGTH;
  445. }
  446. Vector3 voxelsize = global_aabb.size;
  447. voxelsize.x /= div_x;
  448. voxelsize.y /= div_y;
  449. voxelsize.z /= div_z;
  450. // Create and initialize cells to zero.
  451. uint8_t ***cell_status = memnew_arr(uint8_t **, div_x);
  452. for (int i = 0; i < div_x; i++) {
  453. cell_status[i] = memnew_arr(uint8_t *, div_y);
  454. for (int j = 0; j < div_y; j++) {
  455. cell_status[i][j] = memnew_arr(uint8_t, div_z);
  456. for (int k = 0; k < div_z; k++) {
  457. cell_status[i][j][k] = 0;
  458. }
  459. }
  460. }
  461. // Plot faces into cells.
  462. for (int i = 0; i < face_count; i++) {
  463. Face3 f = faces[i];
  464. for (int j = 0; j < 3; j++) {
  465. f.vertex[j] -= global_aabb.position;
  466. }
  467. _plot_face(cell_status, 0, 0, 0, div_x, div_y, div_z, voxelsize, f);
  468. }
  469. // Determine which cells connect to the outside by traversing the outside and recursively flood-fill marking.
  470. for (int i = 0; i < div_x; i++) {
  471. for (int j = 0; j < div_y; j++) {
  472. _mark_outside(cell_status, i, j, 0, div_x, div_y, div_z);
  473. _mark_outside(cell_status, i, j, div_z - 1, div_x, div_y, div_z);
  474. }
  475. }
  476. for (int i = 0; i < div_z; i++) {
  477. for (int j = 0; j < div_y; j++) {
  478. _mark_outside(cell_status, 0, j, i, div_x, div_y, div_z);
  479. _mark_outside(cell_status, div_x - 1, j, i, div_x, div_y, div_z);
  480. }
  481. }
  482. for (int i = 0; i < div_x; i++) {
  483. for (int j = 0; j < div_z; j++) {
  484. _mark_outside(cell_status, i, 0, j, div_x, div_y, div_z);
  485. _mark_outside(cell_status, i, div_y - 1, j, div_x, div_y, div_z);
  486. }
  487. }
  488. // Build faces for the inside-outside cell divisors.
  489. Vector<Face3> wrapped_faces;
  490. for (int i = 0; i < div_x; i++) {
  491. for (int j = 0; j < div_y; j++) {
  492. for (int k = 0; k < div_z; k++) {
  493. _build_faces(cell_status, i, j, k, div_x, div_y, div_z, wrapped_faces);
  494. }
  495. }
  496. }
  497. // Transform face vertices to global coords.
  498. int wrapped_faces_count = wrapped_faces.size();
  499. Face3 *wrapped_faces_ptr = wrapped_faces.ptrw();
  500. for (int i = 0; i < wrapped_faces_count; i++) {
  501. for (int j = 0; j < 3; j++) {
  502. Vector3 &v = wrapped_faces_ptr[i].vertex[j];
  503. v = v * voxelsize;
  504. v += global_aabb.position;
  505. }
  506. }
  507. // clean up grid
  508. for (int i = 0; i < div_x; i++) {
  509. for (int j = 0; j < div_y; j++) {
  510. memdelete_arr(cell_status[i][j]);
  511. }
  512. memdelete_arr(cell_status[i]);
  513. }
  514. memdelete_arr(cell_status);
  515. if (p_error) {
  516. *p_error = voxelsize.length();
  517. }
  518. return wrapped_faces;
  519. }
  520. Geometry3D::MeshData Geometry3D::build_convex_mesh(const Vector<Plane> &p_planes) {
  521. MeshData mesh;
  522. #define SUBPLANE_SIZE 1024.0
  523. real_t subplane_size = 1024.0; // Should compute this from the actual plane.
  524. for (int i = 0; i < p_planes.size(); i++) {
  525. Plane p = p_planes[i];
  526. Vector3 ref = Vector3(0.0, 1.0, 0.0);
  527. if (ABS(p.normal.dot(ref)) > 0.95) {
  528. ref = Vector3(0.0, 0.0, 1.0); // Change axis.
  529. }
  530. Vector3 right = p.normal.cross(ref).normalized();
  531. Vector3 up = p.normal.cross(right).normalized();
  532. Vector<Vector3> vertices;
  533. Vector3 center = p.center();
  534. // make a quad clockwise
  535. vertices.push_back(center - up * subplane_size + right * subplane_size);
  536. vertices.push_back(center - up * subplane_size - right * subplane_size);
  537. vertices.push_back(center + up * subplane_size - right * subplane_size);
  538. vertices.push_back(center + up * subplane_size + right * subplane_size);
  539. for (int j = 0; j < p_planes.size(); j++) {
  540. if (j == i) {
  541. continue;
  542. }
  543. Vector<Vector3> new_vertices;
  544. Plane clip = p_planes[j];
  545. if (clip.normal.dot(p.normal) > 0.95) {
  546. continue;
  547. }
  548. if (vertices.size() < 3) {
  549. break;
  550. }
  551. for (int k = 0; k < vertices.size(); k++) {
  552. int k_n = (k + 1) % vertices.size();
  553. Vector3 edge0_A = vertices[k];
  554. Vector3 edge1_A = vertices[k_n];
  555. real_t dist0 = clip.distance_to(edge0_A);
  556. real_t dist1 = clip.distance_to(edge1_A);
  557. if (dist0 <= 0) { // Behind plane.
  558. new_vertices.push_back(vertices[k]);
  559. }
  560. // Check for different sides and non coplanar.
  561. if ((dist0 * dist1) < 0) {
  562. // Calculate intersection.
  563. Vector3 rel = edge1_A - edge0_A;
  564. real_t den = clip.normal.dot(rel);
  565. if (Math::is_zero_approx(den)) {
  566. continue; // Point too short.
  567. }
  568. real_t dist = -(clip.normal.dot(edge0_A) - clip.d) / den;
  569. Vector3 inters = edge0_A + rel * dist;
  570. new_vertices.push_back(inters);
  571. }
  572. }
  573. vertices = new_vertices;
  574. }
  575. if (vertices.size() < 3) {
  576. continue;
  577. }
  578. // Result is a clockwise face.
  579. MeshData::Face face;
  580. // Add face indices.
  581. for (int j = 0; j < vertices.size(); j++) {
  582. int idx = -1;
  583. for (int k = 0; k < mesh.vertices.size(); k++) {
  584. if (mesh.vertices[k].distance_to(vertices[j]) < 0.001) {
  585. idx = k;
  586. break;
  587. }
  588. }
  589. if (idx == -1) {
  590. idx = mesh.vertices.size();
  591. mesh.vertices.push_back(vertices[j]);
  592. }
  593. face.indices.push_back(idx);
  594. }
  595. face.plane = p;
  596. mesh.faces.push_back(face);
  597. // Add edge.
  598. for (int j = 0; j < face.indices.size(); j++) {
  599. int a = face.indices[j];
  600. int b = face.indices[(j + 1) % face.indices.size()];
  601. bool found = false;
  602. for (int k = 0; k < mesh.edges.size(); k++) {
  603. if (mesh.edges[k].a == a && mesh.edges[k].b == b) {
  604. found = true;
  605. break;
  606. }
  607. if (mesh.edges[k].b == a && mesh.edges[k].a == b) {
  608. found = true;
  609. break;
  610. }
  611. }
  612. if (found) {
  613. continue;
  614. }
  615. MeshData::Edge edge;
  616. edge.a = a;
  617. edge.b = b;
  618. mesh.edges.push_back(edge);
  619. }
  620. }
  621. return mesh;
  622. }
  623. Vector<Plane> Geometry3D::build_box_planes(const Vector3 &p_extents) {
  624. Vector<Plane> planes;
  625. planes.push_back(Plane(Vector3(1, 0, 0), p_extents.x));
  626. planes.push_back(Plane(Vector3(-1, 0, 0), p_extents.x));
  627. planes.push_back(Plane(Vector3(0, 1, 0), p_extents.y));
  628. planes.push_back(Plane(Vector3(0, -1, 0), p_extents.y));
  629. planes.push_back(Plane(Vector3(0, 0, 1), p_extents.z));
  630. planes.push_back(Plane(Vector3(0, 0, -1), p_extents.z));
  631. return planes;
  632. }
  633. Vector<Plane> Geometry3D::build_cylinder_planes(real_t p_radius, real_t p_height, int p_sides, Vector3::Axis p_axis) {
  634. Vector<Plane> planes;
  635. const double sides_step = Math_TAU / p_sides;
  636. for (int i = 0; i < p_sides; i++) {
  637. Vector3 normal;
  638. normal[(p_axis + 1) % 3] = Math::cos(i * sides_step);
  639. normal[(p_axis + 2) % 3] = Math::sin(i * sides_step);
  640. planes.push_back(Plane(normal, p_radius));
  641. }
  642. Vector3 axis;
  643. axis[p_axis] = 1.0;
  644. planes.push_back(Plane(axis, p_height * 0.5));
  645. planes.push_back(Plane(-axis, p_height * 0.5));
  646. return planes;
  647. }
  648. Vector<Plane> Geometry3D::build_sphere_planes(real_t p_radius, int p_lats, int p_lons, Vector3::Axis p_axis) {
  649. Vector<Plane> planes;
  650. Vector3 axis;
  651. axis[p_axis] = 1.0;
  652. Vector3 axis_neg;
  653. axis_neg[(p_axis + 1) % 3] = 1.0;
  654. axis_neg[(p_axis + 2) % 3] = 1.0;
  655. axis_neg[p_axis] = -1.0;
  656. const double lon_step = Math_TAU / p_lons;
  657. for (int i = 0; i < p_lons; i++) {
  658. Vector3 normal;
  659. normal[(p_axis + 1) % 3] = Math::cos(i * lon_step);
  660. normal[(p_axis + 2) % 3] = Math::sin(i * lon_step);
  661. planes.push_back(Plane(normal, p_radius));
  662. for (int j = 1; j <= p_lats; j++) {
  663. // FIXME: This is stupid.
  664. Vector3 angle = normal.lerp(axis, j / (real_t)p_lats).normalized();
  665. Vector3 pos = angle * p_radius;
  666. planes.push_back(Plane(pos, angle));
  667. planes.push_back(Plane(pos * axis_neg, angle * axis_neg));
  668. }
  669. }
  670. return planes;
  671. }
  672. Vector<Plane> Geometry3D::build_capsule_planes(real_t p_radius, real_t p_height, int p_sides, int p_lats, Vector3::Axis p_axis) {
  673. Vector<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. const double sides_step = Math_TAU / p_sides;
  681. for (int i = 0; i < p_sides; i++) {
  682. Vector3 normal;
  683. normal[(p_axis + 1) % 3] = Math::cos(i * sides_step);
  684. normal[(p_axis + 2) % 3] = Math::sin(i * sides_step);
  685. planes.push_back(Plane(normal, p_radius));
  686. for (int j = 1; j <= p_lats; j++) {
  687. Vector3 angle = normal.lerp(axis, j / (real_t)p_lats).normalized();
  688. Vector3 pos = axis * p_height * 0.5 + 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. Vector<Vector3> Geometry3D::compute_convex_mesh_points(const Plane *p_planes, int p_plane_count) {
  696. Vector<Vector3> points;
  697. // Iterate through every unique combination of any three planes.
  698. for (int i = p_plane_count - 1; i >= 0; i--) {
  699. for (int j = i - 1; j >= 0; j--) {
  700. for (int k = j - 1; k >= 0; k--) {
  701. // Find the point where these planes all cross over (if they
  702. // do at all).
  703. Vector3 convex_shape_point;
  704. if (p_planes[i].intersect_3(p_planes[j], p_planes[k], &convex_shape_point)) {
  705. // See if any *other* plane excludes this point because it's
  706. // on the wrong side.
  707. bool excluded = false;
  708. for (int n = 0; n < p_plane_count; n++) {
  709. if (n != i && n != j && n != k) {
  710. real_t dp = p_planes[n].normal.dot(convex_shape_point);
  711. if (dp - p_planes[n].d > CMP_EPSILON) {
  712. excluded = true;
  713. break;
  714. }
  715. }
  716. }
  717. // Only add the point if it passed all tests.
  718. if (!excluded) {
  719. points.push_back(convex_shape_point);
  720. }
  721. }
  722. }
  723. }
  724. }
  725. return points;
  726. }
  727. #define square(m_s) ((m_s) * (m_s))
  728. #define INF 1e20
  729. /* dt of 1d function using squared distance */
  730. static void edt(float *f, int stride, int n) {
  731. float *d = (float *)alloca(sizeof(float) * n + sizeof(int) * n + sizeof(float) * (n + 1));
  732. int *v = (int *)&(d[n]);
  733. float *z = (float *)&v[n];
  734. int k = 0;
  735. v[0] = 0;
  736. z[0] = -INF;
  737. z[1] = +INF;
  738. for (int q = 1; q <= n - 1; q++) {
  739. float s = ((f[q * stride] + square(q)) - (f[v[k] * stride] + square(v[k]))) / (2 * q - 2 * v[k]);
  740. while (s <= z[k]) {
  741. k--;
  742. s = ((f[q * stride] + square(q)) - (f[v[k] * stride] + square(v[k]))) / (2 * q - 2 * v[k]);
  743. }
  744. k++;
  745. v[k] = q;
  746. z[k] = s;
  747. z[k + 1] = +INF;
  748. }
  749. k = 0;
  750. for (int q = 0; q <= n - 1; q++) {
  751. while (z[k + 1] < q) {
  752. k++;
  753. }
  754. d[q] = square(q - v[k]) + f[v[k] * stride];
  755. }
  756. for (int i = 0; i < n; i++) {
  757. f[i * stride] = d[i];
  758. }
  759. }
  760. #undef square
  761. Vector<uint32_t> Geometry3D::generate_edf(const Vector<bool> &p_voxels, const Vector3i &p_size, bool p_negative) {
  762. uint32_t float_count = p_size.x * p_size.y * p_size.z;
  763. ERR_FAIL_COND_V((uint32_t)p_voxels.size() != float_count, Vector<uint32_t>());
  764. float *work_memory = memnew_arr(float, float_count);
  765. for (uint32_t i = 0; i < float_count; i++) {
  766. work_memory[i] = INF;
  767. }
  768. uint32_t y_mult = p_size.x;
  769. uint32_t z_mult = y_mult * p_size.y;
  770. //plot solid cells
  771. {
  772. const bool *voxr = p_voxels.ptr();
  773. for (uint32_t i = 0; i < float_count; i++) {
  774. bool plot = voxr[i];
  775. if (p_negative) {
  776. plot = !plot;
  777. }
  778. if (plot) {
  779. work_memory[i] = 0;
  780. }
  781. }
  782. }
  783. //process in each direction
  784. //xy->z
  785. for (int i = 0; i < p_size.x; i++) {
  786. for (int j = 0; j < p_size.y; j++) {
  787. edt(&work_memory[i + j * y_mult], z_mult, p_size.z);
  788. }
  789. }
  790. //xz->y
  791. for (int i = 0; i < p_size.x; i++) {
  792. for (int j = 0; j < p_size.z; j++) {
  793. edt(&work_memory[i + j * z_mult], y_mult, p_size.y);
  794. }
  795. }
  796. //yz->x
  797. for (int i = 0; i < p_size.y; i++) {
  798. for (int j = 0; j < p_size.z; j++) {
  799. edt(&work_memory[i * y_mult + j * z_mult], 1, p_size.x);
  800. }
  801. }
  802. Vector<uint32_t> ret;
  803. ret.resize(float_count);
  804. {
  805. uint32_t *w = ret.ptrw();
  806. for (uint32_t i = 0; i < float_count; i++) {
  807. w[i] = uint32_t(Math::sqrt(work_memory[i]));
  808. }
  809. }
  810. memdelete_arr(work_memory);
  811. return ret;
  812. }
  813. Vector<int8_t> Geometry3D::generate_sdf8(const Vector<uint32_t> &p_positive, const Vector<uint32_t> &p_negative) {
  814. ERR_FAIL_COND_V(p_positive.size() != p_negative.size(), Vector<int8_t>());
  815. Vector<int8_t> sdf8;
  816. int s = p_positive.size();
  817. sdf8.resize(s);
  818. const uint32_t *rpos = p_positive.ptr();
  819. const uint32_t *rneg = p_negative.ptr();
  820. int8_t *wsdf = sdf8.ptrw();
  821. for (int i = 0; i < s; i++) {
  822. int32_t diff = int32_t(rpos[i]) - int32_t(rneg[i]);
  823. wsdf[i] = CLAMP(diff, -128, 127);
  824. }
  825. return sdf8;
  826. }