carve-capi.cc 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. /*
  2. * ***** BEGIN GPL LICENSE BLOCK *****
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. *
  18. * The Original Code is Copyright (C) 2014 Blender Foundation.
  19. * All rights reserved.
  20. *
  21. * Contributor(s): Blender Foundation,
  22. * Sergey Sharybin
  23. *
  24. * ***** END GPL LICENSE BLOCK *****
  25. */
  26. #include "carve-capi.h"
  27. #include "carve-util.h"
  28. #include <carve/interpolator.hpp>
  29. #include <carve/rescale.hpp>
  30. #include <carve/csg_triangulator.hpp>
  31. #include <carve/mesh_simplify.hpp>
  32. using carve::mesh::MeshSet;
  33. typedef std::pair<int, int> OrigIndex;
  34. typedef std::pair<MeshSet<3>::vertex_t *, MeshSet<3>::vertex_t *> VertexPair;
  35. typedef carve::interpolate::VertexAttr<OrigIndex> OrigVertMapping;
  36. typedef carve::interpolate::FaceAttr<OrigIndex> OrigFaceMapping;
  37. typedef carve::interpolate::SwapableFaceEdgeAttr<OrigIndex> OrigFaceEdgeMapping;
  38. typedef carve::interpolate::SimpleFaceEdgeAttr<bool> FaceEdgeTriangulatedFlag;
  39. typedef struct CarveMeshDescr {
  40. // Stores mesh data itself.
  41. MeshSet<3> *poly;
  42. // N-th element of the vector indicates index of an original mesh loop.
  43. std::unordered_map<std::pair<int, int>, int> orig_loop_index_map;
  44. // Mapping from carve face to an original face index in DM.
  45. std::unordered_map<const MeshSet<3>::face_t *, int> orig_poly_index_map;
  46. // The folloving mapping is only filled in for output mesh.
  47. // Mapping from the face verts back to original vert index.
  48. OrigVertMapping orig_vert_mapping;
  49. // Mapping from the face edges back to (original edge index, original loop index).
  50. OrigFaceEdgeMapping orig_face_edge_mapping;
  51. FaceEdgeTriangulatedFlag face_edge_triangulated_flag;
  52. // Mapping from the faces back to original poly index.
  53. OrigFaceMapping orig_face_mapping;
  54. } CarveMeshDescr;
  55. namespace {
  56. template <typename T1, typename T2>
  57. void edgeIndexMap_put(std::unordered_map<std::pair<T1, T1>, T2> *edge_map,
  58. const T1 &v1,
  59. const T1 &v2,
  60. const T2 &index)
  61. {
  62. if (v1 < v2) {
  63. (*edge_map)[std::make_pair(v1, v2)] = index;
  64. }
  65. else {
  66. (*edge_map)[std::make_pair(v2, v1)] = index;
  67. }
  68. }
  69. template <typename T1, typename T2>
  70. const T2 &edgeIndexMap_get(const std::unordered_map<std::pair<T1, T1>, T2> &edge_map,
  71. const T1 &v1,
  72. const T1 &v2)
  73. {
  74. typedef std::unordered_map<std::pair<T1, T1>, T2> Map;
  75. typename Map::const_iterator found;
  76. if (v1 < v2) {
  77. found = edge_map.find(std::make_pair(v1, v2));
  78. }
  79. else {
  80. found = edge_map.find(std::make_pair(v2, v1));
  81. }
  82. assert(found != edge_map.end());
  83. return found->second;
  84. }
  85. template <typename T1, typename T2>
  86. bool edgeIndexMap_get_if_exists(const std::unordered_map<std::pair<T1, T1>, T2> &edge_map,
  87. const T1 &v1,
  88. const T1 &v2,
  89. T2 *out)
  90. {
  91. typedef std::unordered_map<std::pair<T1, T1>, T2> Map;
  92. typename Map::const_iterator found;
  93. if (v1 < v2) {
  94. found = edge_map.find(std::make_pair(v1, v2));
  95. }
  96. else {
  97. found = edge_map.find(std::make_pair(v2, v1));
  98. }
  99. if (found == edge_map.end()) {
  100. return false;
  101. }
  102. *out = found->second;
  103. return true;
  104. }
  105. template <typename T1, typename T2>
  106. bool edgeIndexMap_exists(const std::unordered_map<std::pair<T1, T1>, T2> &edge_map,
  107. const T1 &v1,
  108. const T1 &v2)
  109. {
  110. typedef std::unordered_map<std::pair<T1, T1>, T2> Map;
  111. typename Map::const_iterator found;
  112. if (v1 < v2) {
  113. found = edge_map.find(std::make_pair(v1, v2));
  114. }
  115. else {
  116. found = edge_map.find(std::make_pair(v2, v1));
  117. }
  118. return found != edge_map.end();
  119. }
  120. template <typename T>
  121. inline int indexOf(const T *element, const std::vector<T> &vector_from)
  122. {
  123. return element - &vector_from.at(0);
  124. }
  125. void initOrigIndexMeshFaceMapping(CarveMeshDescr *mesh,
  126. int which_mesh,
  127. std::unordered_map<std::pair<int, int>, int> &orig_loop_index_map,
  128. std::unordered_map<const MeshSet<3>::face_t*, int> &orig_poly_index_map,
  129. OrigVertMapping *orig_vert_mapping,
  130. OrigFaceEdgeMapping *orig_face_edge_mapping,
  131. FaceEdgeTriangulatedFlag *face_edge_triangulated_flag,
  132. OrigFaceMapping *orig_face_attr)
  133. {
  134. MeshSet<3> *poly = mesh->poly;
  135. std::vector<MeshSet<3>::vertex_t>::iterator vertex_iter =
  136. poly->vertex_storage.begin();
  137. for (int i = 0;
  138. vertex_iter != poly->vertex_storage.end();
  139. ++i, ++vertex_iter)
  140. {
  141. MeshSet<3>::vertex_t *vertex = &(*vertex_iter);
  142. orig_vert_mapping->setAttribute(vertex,
  143. std::make_pair(which_mesh, i));
  144. }
  145. MeshSet<3>::face_iter face_iter = poly->faceBegin();
  146. for (int i = 0, loop_map_index = 0;
  147. face_iter != poly->faceEnd();
  148. ++face_iter, ++i)
  149. {
  150. const MeshSet<3>::face_t *face = *face_iter;
  151. // Mapping from carve face back to original poly index.
  152. int orig_poly_index = orig_poly_index_map[face];
  153. orig_face_attr->setAttribute(face, std::make_pair(which_mesh, orig_poly_index));
  154. for (MeshSet<3>::face_t::const_edge_iter_t edge_iter = face->begin();
  155. edge_iter != face->end();
  156. ++edge_iter, ++loop_map_index)
  157. {
  158. int v1 = indexOf(edge_iter->v1(), poly->vertex_storage);
  159. int v2 = indexOf(edge_iter->v2(), poly->vertex_storage);
  160. int orig_loop_index;
  161. if (!edgeIndexMap_get_if_exists(orig_loop_index_map,
  162. v1, v2,
  163. &orig_loop_index))
  164. {
  165. orig_loop_index = -1;
  166. }
  167. if (orig_loop_index != -1) {
  168. // Mapping from carve face edge back to original loop index.
  169. orig_face_edge_mapping->setAttribute(face,
  170. edge_iter.idx(),
  171. std::make_pair(which_mesh,
  172. orig_loop_index));
  173. }
  174. else {
  175. face_edge_triangulated_flag->setAttribute(face,
  176. edge_iter.idx(),
  177. true);
  178. }
  179. }
  180. }
  181. }
  182. void initOrigIndexMapping(CarveMeshDescr *left_mesh,
  183. CarveMeshDescr *right_mesh,
  184. OrigVertMapping *orig_vert_mapping,
  185. OrigFaceEdgeMapping *orig_face_edge_mapping,
  186. FaceEdgeTriangulatedFlag *face_edge_triangulated_flag,
  187. OrigFaceMapping *orig_face_mapping)
  188. {
  189. initOrigIndexMeshFaceMapping(left_mesh,
  190. CARVE_MESH_LEFT,
  191. left_mesh->orig_loop_index_map,
  192. left_mesh->orig_poly_index_map,
  193. orig_vert_mapping,
  194. orig_face_edge_mapping,
  195. face_edge_triangulated_flag,
  196. orig_face_mapping);
  197. initOrigIndexMeshFaceMapping(right_mesh,
  198. CARVE_MESH_RIGHT,
  199. right_mesh->orig_loop_index_map,
  200. right_mesh->orig_poly_index_map,
  201. orig_vert_mapping,
  202. orig_face_edge_mapping,
  203. face_edge_triangulated_flag,
  204. orig_face_mapping);
  205. }
  206. void origEdgeMappingForFace(MeshSet<3>::face_t *face,
  207. OrigFaceEdgeMapping *orig_face_edge_mapping,
  208. std::unordered_map<VertexPair, OrigIndex> *edge_origindex_map)
  209. {
  210. OrigIndex origindex_none = std::make_pair((int)CARVE_MESH_NONE, -1);
  211. MeshSet<3>::edge_t *edge = face->edge;
  212. for (int i = 0;
  213. i < face->nEdges();
  214. ++i, edge = edge->next)
  215. {
  216. MeshSet<3>::vertex_t *v1 = edge->v1();
  217. MeshSet<3>::vertex_t *v2 = edge->v2();
  218. OrigIndex orig_edge_index =
  219. orig_face_edge_mapping->getAttribute(edge->face, i, origindex_none);
  220. edgeIndexMap_put(edge_origindex_map, v1, v2, orig_edge_index);
  221. }
  222. }
  223. void dissolveTriangulatedEdges(MeshSet<3>::mesh_t *mesh,
  224. const std::set< std::pair<int, int> > &open_edges,
  225. FaceEdgeTriangulatedFlag *face_edge_triangulated_flag,
  226. OrigFaceEdgeMapping *orig_face_edge_mapping)
  227. {
  228. typedef std::unordered_set<MeshSet<3>::edge_t *> edge_set_t;
  229. typedef std::unordered_set<MeshSet<3>::face_t *> face_set_t;
  230. edge_set_t triangulated_face_edges;
  231. for (int face_index = 0; face_index < mesh->faces.size(); ++face_index) {
  232. MeshSet<3>::face_t *face = mesh->faces[face_index];
  233. MeshSet<3>::edge_t *edge = face->edge;
  234. for (int edge_index = 0;
  235. edge_index < face->nEdges();
  236. ++edge_index, edge = edge->next)
  237. {
  238. if (edge->rev) {
  239. const bool is_triangulated_edge =
  240. face_edge_triangulated_flag->getAttribute(face,
  241. edge_index,
  242. false);
  243. if (is_triangulated_edge) {
  244. MeshSet<3>::edge_t *e1 = std::min(edge, edge->rev);
  245. int v1 = indexOf(e1->v1(), mesh->meshset->vertex_storage),
  246. v2 = indexOf(e1->v2(), mesh->meshset->vertex_storage);
  247. bool is_open = false;
  248. if (v1 < v2) {
  249. is_open = open_edges.find(std::make_pair(v1, v2)) != open_edges.end();
  250. }
  251. else {
  252. is_open = open_edges.find(std::make_pair(v2, v1)) != open_edges.end();
  253. }
  254. if (is_open == false) {
  255. triangulated_face_edges.insert(e1);
  256. }
  257. }
  258. }
  259. }
  260. }
  261. if (triangulated_face_edges.size()) {
  262. face_set_t triangulated_faces;
  263. std::unordered_map<VertexPair, OrigIndex> edge_origindex_map;
  264. for (edge_set_t::iterator it = triangulated_face_edges.begin();
  265. it != triangulated_face_edges.end();
  266. ++it)
  267. {
  268. MeshSet<3>::edge_t *edge = *it;
  269. origEdgeMappingForFace(edge->face,
  270. orig_face_edge_mapping,
  271. &edge_origindex_map);
  272. triangulated_faces.insert(edge->face);
  273. origEdgeMappingForFace(edge->rev->face,
  274. orig_face_edge_mapping,
  275. &edge_origindex_map);
  276. triangulated_faces.insert(edge->rev->face);
  277. }
  278. carve::mesh::MeshSimplifier simplifier;
  279. simplifier.dissolveMeshEdges(mesh, triangulated_face_edges);
  280. for (int face_index = 0; face_index < mesh->faces.size(); face_index++) {
  281. MeshSet<3>::face_t *face = mesh->faces[face_index];
  282. if (triangulated_faces.find(face) != triangulated_faces.end()) {
  283. MeshSet<3>::edge_t *edge = face->edge;
  284. for (int edge_index = 0;
  285. edge_index < face->nEdges();
  286. ++edge_index, edge = edge->next)
  287. {
  288. MeshSet<3>::vertex_t *v1 = edge->v1();
  289. MeshSet<3>::vertex_t *v2 = edge->v2();
  290. OrigIndex orig_edge_index =
  291. edgeIndexMap_get(edge_origindex_map,
  292. v1,
  293. v2);
  294. orig_face_edge_mapping->setAttribute(face, edge_index, orig_edge_index);
  295. }
  296. }
  297. }
  298. }
  299. }
  300. void dissolveTriangulatedEdges(CarveMeshDescr *mesh_descr)
  301. {
  302. MeshSet<3> *poly = mesh_descr->poly;
  303. FaceEdgeTriangulatedFlag *face_edge_triangulated_flag =
  304. &mesh_descr->face_edge_triangulated_flag;
  305. std::set< std::pair<int, int> > open_edges;
  306. for (int mesh_index = 0;
  307. mesh_index < poly->meshes.size();
  308. ++mesh_index)
  309. {
  310. const MeshSet<3>::mesh_t *mesh = poly->meshes[mesh_index];
  311. for (int edge_index = 0;
  312. edge_index < mesh->open_edges.size();
  313. ++edge_index)
  314. {
  315. const MeshSet<3>::edge_t *edge = mesh->open_edges[edge_index];
  316. int v1 = indexOf(edge->v1(), poly->vertex_storage),
  317. v2 = indexOf(edge->v2(), poly->vertex_storage);
  318. if (v1 < v2) {
  319. open_edges.insert(std::make_pair(v1, v2));
  320. }
  321. else {
  322. open_edges.insert(std::make_pair(v2, v1));
  323. }
  324. }
  325. }
  326. for (int mesh_index = 0; mesh_index < poly->meshes.size(); ++mesh_index) {
  327. MeshSet<3>::mesh_t *mesh = poly->meshes[mesh_index];
  328. dissolveTriangulatedEdges(mesh,
  329. open_edges,
  330. face_edge_triangulated_flag,
  331. &mesh_descr->orig_face_edge_mapping);
  332. }
  333. }
  334. void clipEar(MeshSet<3>::edge_t *ear)
  335. {
  336. MeshSet<3>::edge_t *p_edge = ear->prev;
  337. MeshSet<3>::edge_t *n_edge = ear->next;
  338. p_edge->next = n_edge;
  339. n_edge->prev = p_edge;
  340. if (ear->face->edge == ear) {
  341. ear->face->edge = n_edge;
  342. }
  343. ear->face->n_edges--;
  344. delete ear;
  345. }
  346. MeshSet<3>::edge_t *findDegenerateEar(MeshSet<3>::face_t *face)
  347. {
  348. for (MeshSet<3>::face_t::edge_iter_t edge_iter = face->begin();
  349. edge_iter != face->end();
  350. ++edge_iter)
  351. {
  352. MeshSet<3>::edge_t &edge = *edge_iter;
  353. if (edge.vert == edge.next->next->vert) {
  354. return edge.next->next;
  355. }
  356. }
  357. return NULL;
  358. }
  359. class EarClipper : public carve::csg::CSG::Hook {
  360. public:
  361. virtual ~EarClipper() {
  362. }
  363. virtual void processOutputFace(std::vector<MeshSet<3>::face_t *> &faces,
  364. const MeshSet<3>::face_t *orig,
  365. bool flipped) {
  366. for (size_t face_index = 0; face_index < faces.size(); ++face_index) {
  367. carve::mesh::MeshSet<3>::face_t *face = faces[face_index];
  368. // There's no ears in quads and tris.
  369. if (face->nVertices() <= 4) {
  370. continue;
  371. }
  372. MeshSet<3>::edge_t *ear;
  373. while ((ear = findDegenerateEar(face)) != NULL) {
  374. clipEar(ear);
  375. }
  376. }
  377. }
  378. };
  379. class HoleResolver : public carve::csg::CarveHoleResolver {
  380. void removeDuplicatedFaces(std::vector<MeshSet<3>::face_t *> &faces) {
  381. std::vector<MeshSet<3>::face_t *> out_faces;
  382. std::vector<MeshSet<3>::face_t *> duplicated_faces;
  383. for (size_t face_index = 0; face_index < faces.size(); ++face_index) {
  384. carve::mesh::MeshSet<3>::face_t *face = faces[face_index];
  385. face->canonicalize();
  386. }
  387. for (size_t i = 0; i < faces.size(); ++i) {
  388. carve::mesh::MeshSet<3>::face_t *face = faces[i];
  389. bool found = false;
  390. for (size_t j = i + 1; j < faces.size() && found == false; ++j) {
  391. MeshSet<3>::face_t *cur_face = faces[j];
  392. if (cur_face->nEdges() == face->nEdges() &&
  393. cur_face->edge->vert == face->edge->vert)
  394. {
  395. MeshSet<3>::edge_t *cur_edge = cur_face->edge,
  396. *forward_edge = face->edge,
  397. *backward_edge = face->edge;
  398. bool forward_matches = true, backward_matches = true;
  399. for (int a = 0; a < cur_face->nEdges(); ++a) {
  400. if (forward_edge->vert != cur_edge->vert) {
  401. forward_matches = false;
  402. if (backward_matches == false) {
  403. break;
  404. }
  405. }
  406. if (backward_edge->vert != cur_edge->vert) {
  407. backward_matches = false;
  408. if (forward_matches == false) {
  409. break;
  410. }
  411. }
  412. cur_edge = cur_edge->next;
  413. forward_edge = forward_edge->next;
  414. backward_edge = backward_edge->prev;
  415. }
  416. if (forward_matches || backward_matches) {
  417. found = true;
  418. break;
  419. }
  420. }
  421. }
  422. if (found) {
  423. duplicated_faces.push_back(face);
  424. }
  425. else {
  426. out_faces.push_back(face);
  427. }
  428. }
  429. for (int i = 0; i < duplicated_faces.size(); ++i) {
  430. delete duplicated_faces[i];
  431. }
  432. std::swap(faces, out_faces);
  433. }
  434. public:
  435. virtual ~HoleResolver() {
  436. }
  437. virtual void processOutputFace(std::vector<MeshSet<3>::face_t *> &faces,
  438. const MeshSet<3>::face_t *orig,
  439. bool flipped) {
  440. carve::csg::CarveHoleResolver::processOutputFace(faces, orig, flipped);
  441. if (faces.size() > 1) {
  442. removeDuplicatedFaces(faces);
  443. }
  444. }
  445. };
  446. template <typename Interpolator>
  447. void copyFaceEdgeAttrs(const MeshSet<3> *poly,
  448. Interpolator *old_interpolator,
  449. Interpolator *new_interpolator)
  450. {
  451. for (MeshSet<3>::const_face_iter face_iter = poly->faceBegin();
  452. face_iter != poly->faceEnd();
  453. ++face_iter)
  454. {
  455. const MeshSet<3>::face_t *face = *face_iter;
  456. for (int edge_index = 0;
  457. edge_index < face->nEdges();
  458. ++edge_index)
  459. {
  460. new_interpolator->copyAttribute(face,
  461. edge_index,
  462. old_interpolator);
  463. }
  464. }
  465. }
  466. template <typename Interpolator>
  467. void cleanupFaceEdgeAttrs(const MeshSet<3> *left,
  468. const MeshSet<3> *right,
  469. Interpolator *interpolator)
  470. {
  471. Interpolator new_interpolator;
  472. copyFaceEdgeAttrs(left, interpolator, &new_interpolator);
  473. copyFaceEdgeAttrs(right, interpolator, &new_interpolator);
  474. interpolator->swapAttributes(&new_interpolator);
  475. }
  476. void cleanupFaceEdgeAttrsCallback(const MeshSet<3> *left,
  477. const MeshSet<3> *right,
  478. void *descr_v)
  479. {
  480. CarveMeshDescr *descr = (CarveMeshDescr *) descr_v;
  481. cleanupFaceEdgeAttrs(left,
  482. right,
  483. &descr->face_edge_triangulated_flag);
  484. cleanupFaceEdgeAttrs(left,
  485. right,
  486. &descr->orig_face_edge_mapping);
  487. }
  488. void copyVertexAttrsCallback(const carve::mesh::MeshSet<3>::vertex_t *orig_vert,
  489. const carve::mesh::MeshSet<3>::vertex_t *new_vert,
  490. void *descr_v)
  491. {
  492. CarveMeshDescr *descr = (CarveMeshDescr *) descr_v;
  493. if (!descr->orig_vert_mapping.hasAttribute(orig_vert)) {
  494. return;
  495. }
  496. if (descr->orig_vert_mapping.hasAttribute(new_vert)) {
  497. return;
  498. }
  499. OrigIndex attr = descr->orig_vert_mapping.getAttribute(orig_vert);
  500. descr->orig_vert_mapping.setAttribute(new_vert, attr);
  501. descr->orig_vert_mapping.removeAttribute(orig_vert);
  502. }
  503. } // namespace
  504. CarveMeshDescr *carve_addMesh(struct ImportMeshData *import_data,
  505. CarveMeshImporter *mesh_importer)
  506. {
  507. #define MAX_STATIC_VERTS 64
  508. CarveMeshDescr *mesh_descr = new CarveMeshDescr;
  509. // Import verices from external mesh to Carve.
  510. int num_verts = mesh_importer->getNumVerts(import_data);
  511. std::vector<MeshSet<3>::vertex_t> vertex_storage;
  512. vertex_storage.reserve(num_verts);
  513. for (int i = 0; i < num_verts; i++) {
  514. float position[3];
  515. mesh_importer->getVertCoord(import_data, i, position);
  516. vertex_storage.push_back(carve::geom::VECTOR(position[0],
  517. position[1],
  518. position[2]));
  519. }
  520. // Import polys from external mesh to Carve.
  521. int verts_of_poly_static[MAX_STATIC_VERTS];
  522. int *verts_of_poly_dynamic = NULL;
  523. int verts_of_poly_dynamic_size = 0;
  524. int num_polys = mesh_importer->getNumPolys(import_data);
  525. int loop_index = 0;
  526. std::vector<int> face_indices;
  527. TrianglesStorage triangles_storage;
  528. std::vector<MeshSet<3>::face_t *> faces;
  529. std::vector<MeshSet<3>::vertex_t *> face_vertices;
  530. faces.reserve(num_polys);
  531. for (int i = 0; i < num_polys; i++) {
  532. int verts_per_poly =
  533. mesh_importer->getNumPolyVerts(import_data, i);
  534. int *verts_of_poly;
  535. if (verts_per_poly <= MAX_STATIC_VERTS) {
  536. verts_of_poly = verts_of_poly_static;
  537. }
  538. else {
  539. if (verts_of_poly_dynamic_size < verts_per_poly) {
  540. if (verts_of_poly_dynamic != NULL) {
  541. delete [] verts_of_poly_dynamic;
  542. }
  543. verts_of_poly_dynamic = new int[verts_per_poly];
  544. verts_of_poly_dynamic_size = verts_per_poly;
  545. }
  546. verts_of_poly = verts_of_poly_dynamic;
  547. }
  548. mesh_importer->getPolyVerts(import_data, i, verts_of_poly);
  549. carve::math::Matrix3 axis_matrix;
  550. if (!carve_checkPolyPlanarAndGetNormal(vertex_storage,
  551. verts_per_poly,
  552. verts_of_poly,
  553. &axis_matrix)) {
  554. face_indices.clear();
  555. int num_triangles = carve_triangulatePoly(import_data,
  556. mesh_importer,
  557. vertex_storage,
  558. verts_per_poly,
  559. verts_of_poly,
  560. axis_matrix,
  561. &face_indices,
  562. &triangles_storage);
  563. for (int j = 0; j < num_triangles; ++j) {
  564. MeshSet<3>::face_t *face = new MeshSet<3>::face_t(
  565. &vertex_storage[face_indices[j * 3]],
  566. &vertex_storage[face_indices[j * 3 + 1]],
  567. &vertex_storage[face_indices[j * 3 + 2]]);
  568. mesh_descr->orig_poly_index_map[face] = i;
  569. faces.push_back(face);
  570. }
  571. }
  572. else {
  573. face_vertices.clear();
  574. face_vertices.reserve(verts_per_poly);
  575. for (int j = 0; j < verts_per_poly; ++j) {
  576. face_vertices.push_back(&vertex_storage[verts_of_poly[j]]);
  577. }
  578. MeshSet<3>::face_t *face =
  579. new MeshSet<3>::face_t(face_vertices.begin(),
  580. face_vertices.end());
  581. mesh_descr->orig_poly_index_map[face] = i;
  582. faces.push_back(face);
  583. }
  584. for (int j = 0; j < verts_per_poly; ++j) {
  585. int v1 = verts_of_poly[j];
  586. int v2 = verts_of_poly[(j + 1) % verts_per_poly];
  587. edgeIndexMap_put(&mesh_descr->orig_loop_index_map, v1, v2, loop_index++);
  588. }
  589. }
  590. if (verts_of_poly_dynamic != NULL) {
  591. delete [] verts_of_poly_dynamic;
  592. }
  593. std::vector<MeshSet<3>::mesh_t *> meshes;
  594. MeshSet<3>::mesh_t::create(faces.begin(), faces.end(), meshes, carve::mesh::MeshOptions());
  595. mesh_descr->poly = new MeshSet<3> (vertex_storage, meshes);
  596. return mesh_descr;
  597. #undef MAX_STATIC_VERTS
  598. }
  599. void carve_deleteMesh(CarveMeshDescr *mesh_descr)
  600. {
  601. delete mesh_descr->poly;
  602. delete mesh_descr;
  603. }
  604. bool carve_performBooleanOperation(CarveMeshDescr *left_mesh,
  605. CarveMeshDescr *right_mesh,
  606. int operation,
  607. CarveMeshDescr **output_mesh)
  608. {
  609. *output_mesh = NULL;
  610. carve::csg::CSG::OP op;
  611. switch (operation) {
  612. #define OP_CONVERT(the_op) \
  613. case CARVE_OP_ ## the_op: \
  614. op = carve::csg::CSG::the_op; \
  615. break;
  616. OP_CONVERT(UNION)
  617. OP_CONVERT(INTERSECTION)
  618. OP_CONVERT(A_MINUS_B)
  619. default:
  620. return false;
  621. #undef OP_CONVERT
  622. }
  623. CarveMeshDescr *output_descr = new CarveMeshDescr;
  624. output_descr->poly = NULL;
  625. try {
  626. MeshSet<3> *left = left_mesh->poly, *right = right_mesh->poly;
  627. carve::geom3d::Vector min, max;
  628. // TODO(sergey): Make importer/exporter to care about re-scale
  629. // to save extra mesh iteration here.
  630. carve_getRescaleMinMax(left, right, &min, &max);
  631. carve::rescale::rescale scaler(min.x, min.y, min.z, max.x, max.y, max.z);
  632. carve::rescale::fwd fwd_r(scaler);
  633. carve::rescale::rev rev_r(scaler);
  634. left->transform(fwd_r);
  635. right->transform(fwd_r);
  636. // Initialize attributes for maping from boolean result mesh back to
  637. // original geometry indices.
  638. initOrigIndexMapping(left_mesh, right_mesh,
  639. &output_descr->orig_vert_mapping,
  640. &output_descr->orig_face_edge_mapping,
  641. &output_descr->face_edge_triangulated_flag,
  642. &output_descr->orig_face_mapping);
  643. carve::csg::CSG csg;
  644. csg.hooks.registerHook(new HoleResolver,
  645. carve::csg::CSG::Hooks::PROCESS_OUTPUT_FACE_BIT);
  646. csg.hooks.registerHook(new EarClipper,
  647. carve::csg::CSG::Hooks::PROCESS_OUTPUT_FACE_BIT);
  648. output_descr->orig_vert_mapping.installHooks(csg);
  649. output_descr->orig_face_edge_mapping.installHooks(csg);
  650. output_descr->face_edge_triangulated_flag.installHooks(csg);
  651. output_descr->orig_face_mapping.installHooks(csg);
  652. // Prepare operands for actual boolean operation.
  653. //
  654. // It's needed because operands might consist of several intersecting
  655. // meshes and in case of another operands intersect an edge loop of
  656. // intersecting that meshes tessellation of operation result can't be
  657. // done properly. The only way to make such situations working is to
  658. // union intersecting meshes of the same operand.
  659. carve_unionIntersections(&csg, &left, &right,
  660. copyVertexAttrsCallback,
  661. cleanupFaceEdgeAttrsCallback,
  662. (void *) output_descr);
  663. left_mesh->poly = left;
  664. right_mesh->poly = right;
  665. if (left->meshes.size() == 0 || right->meshes.size() == 0) {
  666. // Normally shouldn't happen (zero-faces objects are handled by
  667. // modifier itself), but unioning intersecting meshes which doesn't
  668. // have consistent normals might lead to empty result which
  669. // wouldn't work here.
  670. return false;
  671. }
  672. output_descr->poly = csg.compute(left,
  673. right,
  674. op,
  675. NULL,
  676. carve::csg::CSG::CLASSIFY_EDGE);
  677. if (output_descr->poly) {
  678. output_descr->poly->transform(rev_r);
  679. dissolveTriangulatedEdges(output_descr);
  680. }
  681. }
  682. catch (carve::exception e) {
  683. std::cerr << "CSG failed, exception " << e.str() << std::endl;
  684. }
  685. catch (...) {
  686. std::cerr << "Unknown error in Carve library" << std::endl;
  687. }
  688. *output_mesh = output_descr;
  689. return output_descr->poly != NULL;
  690. }
  691. static int exportMesh_handle_edges_list(MeshSet<3> *poly,
  692. const std::vector<MeshSet<3>::edge_t*> &edges,
  693. int start_edge_index,
  694. CarveMeshExporter *mesh_exporter,
  695. struct ExportMeshData *export_data,
  696. std::unordered_map<VertexPair, OrigIndex> &edge_origindex_map,
  697. std::unordered_map<VertexPair, int> *edge_map)
  698. {
  699. int num_exported_edges = 0;
  700. for (int i = 0, edge_index = start_edge_index;
  701. i < edges.size();
  702. ++i)
  703. {
  704. MeshSet<3>::edge_t *edge = edges.at(i);
  705. MeshSet<3>::vertex_t *v1 = edge->v1();
  706. MeshSet<3>::vertex_t *v2 = edge->v2();
  707. if (edgeIndexMap_exists(*edge_map, v1, v2)) {
  708. continue;
  709. }
  710. const OrigIndex &orig_edge_index = edgeIndexMap_get(edge_origindex_map,
  711. v1,
  712. v2);
  713. mesh_exporter->setEdge(export_data,
  714. edge_index,
  715. indexOf(v1, poly->vertex_storage),
  716. indexOf(v2, poly->vertex_storage),
  717. orig_edge_index.first,
  718. orig_edge_index.second);
  719. edgeIndexMap_put(edge_map, v1, v2, edge_index);
  720. ++edge_index;
  721. ++num_exported_edges;
  722. }
  723. return num_exported_edges;
  724. }
  725. void carve_exportMesh(CarveMeshDescr *mesh_descr,
  726. CarveMeshExporter *mesh_exporter,
  727. struct ExportMeshData *export_data)
  728. {
  729. OrigIndex origindex_none = std::make_pair((int)CARVE_MESH_NONE, -1);
  730. MeshSet<3> *poly = mesh_descr->poly;
  731. int num_vertices = poly->vertex_storage.size();
  732. int num_edges = 0, num_loops = 0, num_polys = 0;
  733. // Get mapping from edge denoted by vertex pair to original edge index,
  734. //
  735. // This is needed because internally Carve interpolates data for per-face
  736. // edges rather then having some global edge storage.
  737. std::unordered_map<VertexPair, OrigIndex> edge_origindex_map;
  738. for (MeshSet<3>::face_iter face_iter = poly->faceBegin();
  739. face_iter != poly->faceEnd();
  740. ++face_iter)
  741. {
  742. MeshSet<3>::face_t *face = *face_iter;
  743. for (MeshSet<3>::face_t::edge_iter_t edge_iter = face->begin();
  744. edge_iter != face->end();
  745. ++edge_iter)
  746. {
  747. MeshSet<3>::edge_t &edge = *edge_iter;
  748. int edge_iter_index = edge_iter.idx();
  749. const OrigIndex &orig_loop_index =
  750. mesh_descr->orig_face_edge_mapping.getAttribute(face,
  751. edge_iter_index,
  752. origindex_none);
  753. OrigIndex orig_edge_index;
  754. if (orig_loop_index.first != CARVE_MESH_NONE) {
  755. orig_edge_index.first = orig_loop_index.first;
  756. orig_edge_index.second =
  757. mesh_exporter->mapLoopToEdge(export_data,
  758. orig_loop_index.first,
  759. orig_loop_index.second);
  760. }
  761. else {
  762. orig_edge_index.first = CARVE_MESH_NONE;
  763. orig_edge_index.second = -1;
  764. }
  765. MeshSet<3>::vertex_t *v1 = edge.v1();
  766. MeshSet<3>::vertex_t *v2 = edge.v2();
  767. edgeIndexMap_put(&edge_origindex_map, v1, v2, orig_edge_index);
  768. }
  769. }
  770. num_edges = edge_origindex_map.size();
  771. // Count polys and loops from all manifolds.
  772. for (MeshSet<3>::face_iter face_iter = poly->faceBegin();
  773. face_iter != poly->faceEnd();
  774. ++face_iter, ++num_polys)
  775. {
  776. MeshSet<3>::face_t *face = *face_iter;
  777. num_loops += face->nEdges();
  778. }
  779. // Initialize arrays for geometry in exported mesh.
  780. mesh_exporter->initGeomArrays(export_data,
  781. num_vertices,
  782. num_edges,
  783. num_loops,
  784. num_polys);
  785. // Export all the vertices.
  786. std::vector<MeshSet<3>::vertex_t>::iterator vertex_iter = poly->vertex_storage.begin();
  787. for (int i = 0; vertex_iter != poly->vertex_storage.end(); ++i, ++vertex_iter) {
  788. MeshSet<3>::vertex_t *vertex = &(*vertex_iter);
  789. OrigIndex orig_vert_index =
  790. mesh_descr->orig_vert_mapping.getAttribute(vertex, origindex_none);
  791. float coord[3];
  792. coord[0] = vertex->v[0];
  793. coord[1] = vertex->v[1];
  794. coord[2] = vertex->v[2];
  795. mesh_exporter->setVert(export_data, i, coord,
  796. orig_vert_index.first,
  797. orig_vert_index.second);
  798. }
  799. // Export all the edges.
  800. std::unordered_map<VertexPair, int> edge_map;
  801. for (int i = 0, edge_index = 0; i < poly->meshes.size(); ++i) {
  802. carve::mesh::Mesh<3> *mesh = poly->meshes[i];
  803. // Export closed edges.
  804. edge_index += exportMesh_handle_edges_list(poly,
  805. mesh->closed_edges,
  806. edge_index,
  807. mesh_exporter,
  808. export_data,
  809. edge_origindex_map,
  810. &edge_map);
  811. // Export open edges.
  812. edge_index += exportMesh_handle_edges_list(poly,
  813. mesh->open_edges,
  814. edge_index,
  815. mesh_exporter,
  816. export_data,
  817. edge_origindex_map,
  818. &edge_map);
  819. }
  820. // Export all the loops and polys.
  821. MeshSet<3>::face_iter face_iter = poly->faceBegin();
  822. for (int loop_index = 0, poly_index = 0;
  823. face_iter != poly->faceEnd();
  824. ++face_iter, ++poly_index)
  825. {
  826. int start_loop_index = loop_index;
  827. MeshSet<3>::face_t *face = *face_iter;
  828. const OrigIndex &orig_face_index =
  829. mesh_descr->orig_face_mapping.getAttribute(face, origindex_none);
  830. for (MeshSet<3>::face_t::edge_iter_t edge_iter = face->begin();
  831. edge_iter != face->end();
  832. ++edge_iter, ++loop_index)
  833. {
  834. MeshSet<3>::edge_t &edge = *edge_iter;
  835. const OrigIndex &orig_loop_index =
  836. mesh_descr->orig_face_edge_mapping.getAttribute(face,
  837. edge_iter.idx(),
  838. origindex_none);
  839. mesh_exporter->setLoop(export_data,
  840. loop_index,
  841. indexOf(edge.vert, poly->vertex_storage),
  842. edgeIndexMap_get(edge_map, edge.v1(), edge.v2()),
  843. orig_loop_index.first,
  844. orig_loop_index.second);
  845. }
  846. mesh_exporter->setPoly(export_data,
  847. poly_index, start_loop_index, face->nEdges(),
  848. orig_face_index.first, orig_face_index.second);
  849. }
  850. }