intersect.cpp 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736
  1. // Begin License:
  2. // Copyright (C) 2006-2014 Tobias Sargeant (tobias.sargeant@gmail.com).
  3. // All rights reserved.
  4. //
  5. // This file is part of the Carve CSG Library (http://carve-csg.com/)
  6. //
  7. // This file may be used under the terms of either the GNU General
  8. // Public License version 2 or 3 (at your option) as published by the
  9. // Free Software Foundation and appearing in the files LICENSE.GPL2
  10. // and LICENSE.GPL3 included in the packaging of this file.
  11. //
  12. // This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
  13. // INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
  14. // A PARTICULAR PURPOSE.
  15. // End:
  16. #if defined(HAVE_CONFIG_H)
  17. # include <carve_config.h>
  18. #endif
  19. #include <carve/csg.hpp>
  20. #include <carve/pointset.hpp>
  21. #include <carve/polyline.hpp>
  22. #include <list>
  23. #include <set>
  24. #include <iostream>
  25. #include <algorithm>
  26. #include "csg_detail.hpp"
  27. #include "csg_data.hpp"
  28. #include "intersect_debug.hpp"
  29. #include "intersect_common.hpp"
  30. #include "intersect_classify_common.hpp"
  31. #include "csg_collector.hpp"
  32. #include <carve/timing.hpp>
  33. #include <carve/colour.hpp>
  34. #include <memory>
  35. carve::csg::VertexPool::VertexPool() {
  36. }
  37. carve::csg::VertexPool::~VertexPool() {
  38. }
  39. void carve::csg::VertexPool::reset() {
  40. pool.clear();
  41. }
  42. carve::csg::VertexPool::vertex_t *carve::csg::VertexPool::get(const vertex_t::vector_t &v) {
  43. if (!pool.size() || pool.back().size() == blocksize) {
  44. pool.push_back(std::vector<vertex_t>());
  45. pool.back().reserve(blocksize);
  46. }
  47. pool.back().push_back(vertex_t(v));
  48. return &pool.back().back();
  49. }
  50. bool carve::csg::VertexPool::inPool(vertex_t *v) const {
  51. for (pool_t::const_iterator i = pool.begin(); i != pool.end(); ++i) {
  52. if (v >= &(i->front()) && v <= &(i->back())) return true;
  53. }
  54. return false;
  55. }
  56. #if defined(CARVE_DEBUG_WRITE_PLY_DATA)
  57. void writePLY(const std::string &out_file, const carve::point::PointSet *points, bool ascii);
  58. void writePLY(const std::string &out_file, const carve::line::PolylineSet *lines, bool ascii);
  59. void writePLY(const std::string &out_file, const carve::mesh::MeshSet<3> *poly, bool ascii);
  60. static carve::mesh::MeshSet<3> *faceLoopsToPolyhedron(const carve::csg::FaceLoopList &fl) {
  61. std::vector<carve::mesh::MeshSet<3>::face_t *> faces;
  62. faces.reserve(fl.size());
  63. for (carve::csg::FaceLoop *f = fl.head; f; f = f->next) {
  64. faces.push_back(f->orig_face->create(f->vertices.begin(), f->vertices.end(), false));
  65. }
  66. carve::mesh::MeshSet<3> *poly = new carve::mesh::MeshSet<3>(faces);
  67. return poly;
  68. }
  69. #endif
  70. namespace {
  71. /**
  72. * \brief Sort a range [\a beg, \a end) of vertices in order of increasing dot product of vertex - \a base on \dir.
  73. *
  74. * @tparam[in] T a forward iterator type.
  75. * @param[in] dir The direction in which to sort vertices.
  76. * @param[in] base
  77. * @param[in] beg The start of the vertex range to sort.
  78. * @param[in] end The end of the vertex range to sort.
  79. * @param[out] out The sorted vertex result.
  80. * @param[in] size_hint A hint regarding the size of the output
  81. * vector (to avoid needing to be able to calculate \a
  82. * end - \a beg).
  83. */
  84. template<typename iter_t>
  85. void orderVertices(iter_t beg, const iter_t end,
  86. const carve::mesh::MeshSet<3>::vertex_t::vector_t &dir,
  87. const carve::mesh::MeshSet<3>::vertex_t::vector_t &base,
  88. std::vector<carve::mesh::MeshSet<3>::vertex_t *> &out) {
  89. typedef std::vector<std::pair<double, carve::mesh::MeshSet<3>::vertex_t *> > DVVector;
  90. std::vector<std::pair<double, carve::mesh::MeshSet<3>::vertex_t *> > ordered_vertices;
  91. ordered_vertices.reserve(std::distance(beg, end));
  92. for (; beg != end; ++beg) {
  93. carve::mesh::MeshSet<3>::vertex_t *v = *beg;
  94. ordered_vertices.push_back(std::make_pair(carve::geom::dot(v->v - base, dir), v));
  95. }
  96. std::sort(ordered_vertices.begin(), ordered_vertices.end());
  97. out.clear();
  98. out.reserve(ordered_vertices.size());
  99. for (DVVector::const_iterator
  100. i = ordered_vertices.begin(), e = ordered_vertices.end();
  101. i != e;
  102. ++i) {
  103. out.push_back((*i).second);
  104. }
  105. }
  106. template<typename iter_t>
  107. void orderEdgeIntersectionVertices(iter_t beg, const iter_t end,
  108. const carve::mesh::MeshSet<3>::vertex_t::vector_t &dir,
  109. const carve::mesh::MeshSet<3>::vertex_t::vector_t &base,
  110. std::vector<carve::mesh::MeshSet<3>::vertex_t *> &out) {
  111. typedef std::vector<std::pair<std::pair<double, double>, carve::mesh::MeshSet<3>::vertex_t *> > DVVector;
  112. DVVector ordered_vertices;
  113. ordered_vertices.reserve(std::distance(beg, end));
  114. for (; beg != end; ++beg) {
  115. carve::mesh::MeshSet<3>::vertex_t *v = (*beg).first;
  116. double ovec = 0.0;
  117. for (carve::csg::detail::EdgeIntInfo::mapped_type::const_iterator j = (*beg).second.begin(); j != (*beg).second.end(); ++j) {
  118. ovec += (*j).second;
  119. }
  120. ordered_vertices.push_back(std::make_pair(std::make_pair(carve::geom::dot(v->v - base, dir), -ovec), v));
  121. }
  122. std::sort(ordered_vertices.begin(), ordered_vertices.end());
  123. out.clear();
  124. out.reserve(ordered_vertices.size());
  125. for (DVVector::const_iterator
  126. i = ordered_vertices.begin(), e = ordered_vertices.end();
  127. i != e;
  128. ++i) {
  129. out.push_back((*i).second);
  130. }
  131. }
  132. /**
  133. *
  134. *
  135. * @param dir
  136. * @param base
  137. * @param beg
  138. * @param end
  139. */
  140. template<typename iter_t>
  141. void selectOrderingProjection(iter_t beg, const iter_t end,
  142. carve::mesh::MeshSet<3>::vertex_t::vector_t &dir,
  143. carve::mesh::MeshSet<3>::vertex_t::vector_t &base) {
  144. double dx, dy, dz;
  145. carve::mesh::MeshSet<3>::vertex_t *min_x, *min_y, *min_z, *max_x, *max_y, *max_z;
  146. if (beg == end) return;
  147. min_x = max_x = min_y = max_y = min_z = max_z = *beg++;
  148. for (; beg != end; ++beg) {
  149. if (min_x->v.x > (*beg)->v.x) min_x = *beg;
  150. if (min_y->v.y > (*beg)->v.y) min_y = *beg;
  151. if (min_z->v.z > (*beg)->v.z) min_z = *beg;
  152. if (max_x->v.x < (*beg)->v.x) max_x = *beg;
  153. if (max_y->v.y < (*beg)->v.y) max_y = *beg;
  154. if (max_z->v.z < (*beg)->v.z) max_z = *beg;
  155. }
  156. dx = max_x->v.x - min_x->v.x;
  157. dy = max_y->v.y - min_y->v.y;
  158. dz = max_z->v.z - min_z->v.z;
  159. if (dx > dy) {
  160. if (dx > dz) {
  161. dir = max_x->v - min_x->v; base = min_x->v;
  162. } else {
  163. dir = max_z->v - min_z->v; base = min_z->v;
  164. }
  165. } else {
  166. if (dy > dz) {
  167. dir = max_y->v - min_y->v; base = min_y->v;
  168. } else {
  169. dir = max_z->v - min_z->v; base = min_z->v;
  170. }
  171. }
  172. }
  173. }
  174. namespace {
  175. struct dump_data {
  176. carve::mesh::MeshSet<3>::vertex_t *i_pt;
  177. carve::csg::IObj i_src;
  178. carve::csg::IObj i_tgt;
  179. dump_data(carve::mesh::MeshSet<3>::vertex_t *_i_pt,
  180. carve::csg::IObj _i_src,
  181. carve::csg::IObj _i_tgt) : i_pt(_i_pt), i_src(_i_src), i_tgt(_i_tgt) {
  182. }
  183. };
  184. struct dump_sort {
  185. bool operator()(const dump_data &a, const dump_data &b) const {
  186. if (a.i_pt->v.x < b.i_pt->v.x) return true;
  187. if (a.i_pt->v.x > b.i_pt->v.x) return false;
  188. if (a.i_pt->v.y < b.i_pt->v.y) return true;
  189. if (a.i_pt->v.y > b.i_pt->v.y) return false;
  190. if (a.i_pt->v.z < b.i_pt->v.z) return true;
  191. if (a.i_pt->v.z > b.i_pt->v.z) return false;
  192. return false;
  193. }
  194. };
  195. #if 0
  196. void dump_intersections(std::ostream &out, carve::csg::Intersections &csg_intersections) {
  197. std::vector<dump_data> temp;
  198. for (carve::csg::Intersections::const_iterator
  199. i = csg_intersections.begin(),
  200. ie = csg_intersections.end();
  201. i != ie;
  202. ++i) {
  203. const carve::csg::IObj &i_src = ((*i).first);
  204. for (carve::csg::Intersections::mapped_type::const_iterator
  205. j = (*i).second.begin(),
  206. je = (*i).second.end();
  207. j != je;
  208. ++j) {
  209. const carve::csg::IObj &i_tgt = ((*j).first);
  210. carve::mesh::MeshSet<3>::vertex_t *i_pt = ((*j).second);
  211. temp.push_back(dump_data(i_pt, i_src, i_tgt));
  212. }
  213. }
  214. std::sort(temp.begin(), temp.end(), dump_sort());
  215. for (size_t i = 0; i < temp.size(); ++i) {
  216. const carve::csg::IObj &i_src = temp[i].i_src;
  217. const carve::csg::IObj &i_tgt = temp[i].i_tgt;
  218. out
  219. << "INTERSECTION: " << temp[i].i_pt << " (" << temp[i].i_pt->v << ") "
  220. << "is " << i_src << ".." << i_tgt << std::endl;
  221. }
  222. #if defined(CARVE_DEBUG_WRITE_PLY_DATA)
  223. std::vector<carve::geom3d::Vector> vertices;
  224. for (carve::csg::Intersections::const_iterator
  225. i = csg_intersections.begin(),
  226. ie = csg_intersections.end();
  227. i != ie;
  228. ++i) {
  229. for (carve::csg::Intersections::mapped_type::const_iterator
  230. j = (*i).second.begin(),
  231. je = (*i).second.end();
  232. j != je;
  233. ++j) {
  234. carve::mesh::MeshSet<3>::vertex_t *i_pt = ((*j).second);
  235. vertices.push_back(i_pt->v);
  236. }
  237. }
  238. #endif
  239. carve::point::PointSet points(vertices);
  240. std::string outf("/tmp/intersection-points.ply");
  241. ::writePLY(outf, &points, true);
  242. }
  243. #endif
  244. /**
  245. * \brief Populate a collection with the faces adjoining an edge.
  246. *
  247. * @tparam face_set_t A collection type.
  248. * @param e The edge for which to collect adjoining faces.
  249. * @param faces
  250. */
  251. template<typename face_set_t>
  252. inline void facesForVertex(carve::mesh::MeshSet<3>::vertex_t *v,
  253. const carve::csg::detail::VEVecMap &ve,
  254. face_set_t &faces) {
  255. carve::csg::detail::VEVecMap::const_iterator vi = ve.find(v);
  256. if (vi != ve.end()) {
  257. for (carve::csg::detail::VEVecMap::data_type::const_iterator i = (*vi).second.begin(); i != (*vi).second.end(); ++i) {
  258. faces.insert((*i)->face);
  259. }
  260. }
  261. }
  262. /**
  263. * \brief Populate a collection with the faces adjoining an edge.
  264. *
  265. * @tparam face_set_t A collection type.
  266. * @param e The edge for which to collect adjoining faces.
  267. * @param faces
  268. */
  269. template<typename face_set_t>
  270. inline void facesForEdge(carve::mesh::MeshSet<3>::edge_t *e,
  271. face_set_t &faces) {
  272. faces.insert(e->face);
  273. }
  274. /**
  275. * \brief Populate a collection with the faces adjoining a face.
  276. *
  277. * @tparam face_set_t A collection type.
  278. * @param f The face for which to collect adjoining faces.
  279. * @param faces
  280. */
  281. template<typename face_set_t>
  282. inline void facesForFace(carve::mesh::MeshSet<3>::face_t *f,
  283. face_set_t &faces) {
  284. faces.insert(f);
  285. }
  286. /**
  287. * \brief Populate a collection with the faces adjoining an intersection object.
  288. *
  289. * @tparam face_set_t A collection type holding const carve::poly::Polyhedron::face_t *.
  290. * @param obj The intersection object for which to collect adjoining faces.
  291. * @param faces
  292. */
  293. template<typename face_set_t>
  294. void facesForObject(const carve::csg::IObj &obj,
  295. const carve::csg::detail::VEVecMap &ve,
  296. face_set_t &faces) {
  297. switch (obj.obtype) {
  298. case carve::csg::IObj::OBTYPE_VERTEX:
  299. facesForVertex(obj.vertex, ve, faces);
  300. break;
  301. case carve::csg::IObj::OBTYPE_EDGE:
  302. facesForEdge(obj.edge, faces);
  303. break;
  304. case carve::csg::IObj::OBTYPE_FACE:
  305. facesForFace(obj.face, faces);
  306. break;
  307. default:
  308. break;
  309. }
  310. }
  311. }
  312. bool carve::csg::CSG::Hooks::hasHook(unsigned hook_num) {
  313. return hooks[hook_num].size() > 0;
  314. }
  315. void carve::csg::CSG::Hooks::intersectionVertex(const meshset_t::vertex_t *vertex,
  316. const IObjPairSet &intersections) {
  317. for (std::list<Hook *>::iterator j = hooks[INTERSECTION_VERTEX_HOOK].begin();
  318. j != hooks[INTERSECTION_VERTEX_HOOK].end();
  319. ++j) {
  320. (*j)->intersectionVertex(vertex, intersections);
  321. }
  322. }
  323. void carve::csg::CSG::Hooks::processOutputFace(std::vector<meshset_t::face_t *> &faces,
  324. const meshset_t::face_t *orig_face,
  325. bool flipped) {
  326. for (std::list<Hook *>::iterator j = hooks[PROCESS_OUTPUT_FACE_HOOK].begin();
  327. j != hooks[PROCESS_OUTPUT_FACE_HOOK].end();
  328. ++j) {
  329. (*j)->processOutputFace(faces, orig_face, flipped);
  330. }
  331. }
  332. void carve::csg::CSG::Hooks::resultFace(const meshset_t::face_t *new_face,
  333. const meshset_t::face_t *orig_face,
  334. bool flipped) {
  335. for (std::list<Hook *>::iterator j = hooks[RESULT_FACE_HOOK].begin();
  336. j != hooks[RESULT_FACE_HOOK].end();
  337. ++j) {
  338. (*j)->resultFace(new_face, orig_face, flipped);
  339. }
  340. }
  341. void carve::csg::CSG::Hooks::edgeDivision(const meshset_t::edge_t *orig_edge,
  342. size_t orig_edge_idx,
  343. const meshset_t::vertex_t *v1,
  344. const meshset_t::vertex_t *v2) {
  345. for (std::list<Hook *>::iterator j = hooks[EDGE_DIVISION_HOOK].begin();
  346. j != hooks[EDGE_DIVISION_HOOK].end();
  347. ++j) {
  348. (*j)->edgeDivision(orig_edge, orig_edge_idx, v1, v2);
  349. }
  350. }
  351. void carve::csg::CSG::Hooks::registerHook(Hook *hook, unsigned hook_bits) {
  352. for (unsigned i = 0; i < HOOK_MAX; ++i) {
  353. if (hook_bits & (1U << i)) {
  354. hooks[i].push_back(hook);
  355. }
  356. }
  357. }
  358. void carve::csg::CSG::Hooks::unregisterHook(Hook *hook) {
  359. for (unsigned i = 0; i < HOOK_MAX; ++i) {
  360. hooks[i].erase(std::remove(hooks[i].begin(), hooks[i].end(), hook), hooks[i].end());
  361. }
  362. }
  363. void carve::csg::CSG::Hooks::reset() {
  364. std::set<Hook *> to_delete;
  365. for (unsigned i = 0; i < HOOK_MAX; ++i) {
  366. for (std::list<Hook *>::iterator j = hooks[i].begin(); j != hooks[i].end(); ++j) {
  367. to_delete.insert(*j);
  368. }
  369. hooks[i].clear();
  370. }
  371. for (std::set<Hook *>::iterator i = to_delete.begin(); i != to_delete.end(); ++i) {
  372. delete *i;
  373. }
  374. }
  375. carve::csg::CSG::Hooks::Hooks() : hooks() {
  376. hooks.resize(HOOK_MAX);
  377. }
  378. carve::csg::CSG::Hooks::~Hooks() {
  379. reset();
  380. }
  381. void carve::csg::CSG::makeVertexIntersections() {
  382. static carve::TimingName FUNC_NAME("CSG::makeVertexIntersections()");
  383. carve::TimingBlock block(FUNC_NAME);
  384. vertex_intersections.clear();
  385. for (Intersections::const_iterator
  386. i = intersections.begin(),
  387. ie = intersections.end();
  388. i != ie;
  389. ++i) {
  390. const IObj &i_src = ((*i).first);
  391. for (Intersections::mapped_type::const_iterator
  392. j = (*i).second.begin(),
  393. je = (*i).second.end();
  394. j != je;
  395. ++j) {
  396. const IObj &i_tgt = ((*j).first);
  397. meshset_t::vertex_t *i_pt = ((*j).second);
  398. vertex_intersections[i_pt].insert(std::make_pair(i_src, i_tgt));
  399. }
  400. }
  401. }
  402. #if 0
  403. static carve::mesh::MeshSet<3>::vertex_t *chooseWeldPoint(
  404. const carve::csg::detail::VSet &equivalent,
  405. carve::csg::VertexPool &vertex_pool) {
  406. // XXX: choose a better weld point.
  407. if (!equivalent.size()) return NULL;
  408. for (carve::csg::detail::VSet::const_iterator
  409. i = equivalent.begin(), e = equivalent.end();
  410. i != e;
  411. ++i) {
  412. if (!vertex_pool.inPool((*i))) return (*i);
  413. }
  414. return *equivalent.begin();
  415. }
  416. static const carve::mesh::MeshSet<3>::vertex_t *weld(
  417. const carve::csg::detail::VSet &equivalent,
  418. carve::csg::VertexIntersections &vertex_intersections,
  419. carve::csg::VertexPool &vertex_pool) {
  420. carve::mesh::MeshSet<3>::vertex_t *weld_point = chooseWeldPoint(equivalent, vertex_pool);
  421. #if defined(CARVE_DEBUG)
  422. std::cerr << "weld: " << equivalent.size() << " vertices ( ";
  423. for (carve::csg::detail::VSet::const_iterator
  424. i = equivalent.begin(), e = equivalent.end();
  425. i != e;
  426. ++i) {
  427. const carve::mesh::MeshSet<3>::vertex_t *v = (*i);
  428. std::cerr << " " << v;
  429. }
  430. std::cerr << ") to " << weld_point << std::endl;
  431. #endif
  432. if (!weld_point) return NULL;
  433. carve::csg::VertexIntersections::mapped_type &weld_tgt = (vertex_intersections[weld_point]);
  434. for (carve::csg::detail::VSet::const_iterator
  435. i = equivalent.begin(), e = equivalent.end();
  436. i != e;
  437. ++i) {
  438. carve::mesh::MeshSet<3>::vertex_t *v = (*i);
  439. if (v != weld_point) {
  440. carve::csg::VertexIntersections::iterator j = vertex_intersections.find(v);
  441. if (j != vertex_intersections.end()) {
  442. weld_tgt.insert((*j).second.begin(), (*j).second.end());
  443. vertex_intersections.erase(j);
  444. }
  445. }
  446. }
  447. return weld_point;
  448. }
  449. #endif
  450. void carve::csg::CSG::groupIntersections() {
  451. #if 0 // old code, to be removed.
  452. static carve::TimingName GROUP_INTERSECTONS("groupIntersections()");
  453. carve::TimingBlock block(GROUP_INTERSECTONS);
  454. std::vector<meshset_t::vertex_t *> vertices;
  455. detail::VVSMap graph;
  456. #if defined(CARVE_DEBUG)
  457. std::cerr << "groupIntersections()" << ": vertex_intersections.size()==" << vertex_intersections.size() << std::endl;
  458. #endif
  459. vertices.reserve(vertex_intersections.size());
  460. for (carve::csg::VertexIntersections::const_iterator
  461. i = vertex_intersections.begin(),
  462. e = vertex_intersections.end();
  463. i != e;
  464. ++i)
  465. {
  466. vertices.push_back((*i).first);
  467. }
  468. carve::geom3d::AABB aabb;
  469. aabb.fit(vertices.begin(), vertices.end(), carve::poly::vec_adapt_vertex_ptr());
  470. Octree vertex_intersections_octree;
  471. vertex_intersections_octree.setBounds(aabb);
  472. vertex_intersections_octree.addVertices(vertices);
  473. std::vector<meshset_t::vertex_t *> out;
  474. for (size_t i = 0, l = vertices.size(); i != l; ++i) {
  475. // let's find all the vertices near this one.
  476. out.clear();
  477. vertex_intersections_octree.findVerticesNearAllowDupes(vertices[i]->v, out);
  478. for (size_t j = 0; j < out.size(); ++j) {
  479. if (vertices[i] != out[j] && carve::geom::equal(vertices[i]->v, out[j]->v)) {
  480. #if defined(CARVE_DEBUG)
  481. std::cerr << "EQ: " << vertices[i] << "," << out[j] << " " << vertices[i]->v << "," << out[j]->v << std::endl;
  482. #endif
  483. graph[vertices[i]].insert(out[j]);
  484. graph[out[j]].insert(vertices[i]);
  485. }
  486. }
  487. }
  488. detail::VSet visited, open;
  489. while (graph.size()) {
  490. visited.clear();
  491. open.clear();
  492. detail::VVSMap::iterator i = graph.begin();
  493. open.insert((*i).first);
  494. while (open.size()) {
  495. detail::VSet::iterator t = open.begin();
  496. const meshset_t::vertex_t *o = (*t);
  497. open.erase(t);
  498. i = graph.find(o);
  499. CARVE_ASSERT(i != graph.end());
  500. visited.insert(o);
  501. for (detail::VVSMap::mapped_type::const_iterator
  502. j = (*i).second.begin(),
  503. je = (*i).second.end();
  504. j != je;
  505. ++j) {
  506. if (visited.count((*j)) == 0) {
  507. open.insert((*j));
  508. }
  509. }
  510. graph.erase(i);
  511. }
  512. weld(visited, vertex_intersections, vertex_pool);
  513. }
  514. #endif
  515. }
  516. static void recordEdgeIntersectionInfo(carve::mesh::MeshSet<3>::vertex_t *intersection,
  517. carve::mesh::MeshSet<3>::edge_t *edge,
  518. const carve::csg::detail::VFSMap::mapped_type &intersected_faces,
  519. carve::csg::detail::Data &data) {
  520. carve::mesh::MeshSet<3>::vertex_t::vector_t edge_dir = edge->v2()->v - edge->v1()->v;
  521. carve::csg::detail::EdgeIntInfo::mapped_type &eint_info = data.emap[edge][intersection];
  522. for (carve::csg::detail::VFSMap::mapped_type::const_iterator i = intersected_faces.begin(); i != intersected_faces.end(); ++i) {
  523. carve::mesh::MeshSet<3>::vertex_t::vector_t normal = (*i)->plane.N;
  524. eint_info.insert(std::make_pair((*i), carve::geom::dot(edge_dir, normal)));
  525. }
  526. }
  527. void carve::csg::CSG::intersectingFacePairs(detail::Data &data) {
  528. static carve::TimingName FUNC_NAME("CSG::intersectingFacePairs()");
  529. carve::TimingBlock block(FUNC_NAME);
  530. // iterate over all intersection points.
  531. for (VertexIntersections::const_iterator i = vertex_intersections.begin(), ie = vertex_intersections.end(); i != ie; ++i) {
  532. meshset_t::vertex_t *i_pt = ((*i).first);
  533. detail::VFSMap::mapped_type &face_set = (data.fmap_rev[i_pt]);
  534. detail::VFSMap::mapped_type src_face_set;
  535. detail::VFSMap::mapped_type tgt_face_set;
  536. // for all pairs of intersecting objects at this point
  537. for (VertexIntersections::data_type::const_iterator j = (*i).second.begin(), je = (*i).second.end(); j != je; ++j) {
  538. const IObj &i_src = ((*j).first);
  539. const IObj &i_tgt = ((*j).second);
  540. src_face_set.clear();
  541. tgt_face_set.clear();
  542. // work out the faces involved.
  543. facesForObject(i_src, data.vert_to_edges, src_face_set);
  544. facesForObject(i_tgt, data.vert_to_edges, tgt_face_set);
  545. // this updates fmap_rev.
  546. std::copy(src_face_set.begin(), src_face_set.end(), set_inserter(face_set));
  547. std::copy(tgt_face_set.begin(), tgt_face_set.end(), set_inserter(face_set));
  548. // record the intersection with respect to any involved vertex.
  549. if (i_src.obtype == IObj::OBTYPE_VERTEX) data.vmap[i_src.vertex] = i_pt;
  550. if (i_tgt.obtype == IObj::OBTYPE_VERTEX) data.vmap[i_tgt.vertex] = i_pt;
  551. // record the intersection with respect to any involved edge.
  552. if (i_src.obtype == IObj::OBTYPE_EDGE) recordEdgeIntersectionInfo(i_pt, i_src.edge, tgt_face_set, data);
  553. if (i_tgt.obtype == IObj::OBTYPE_EDGE) recordEdgeIntersectionInfo(i_pt, i_tgt.edge, src_face_set, data);
  554. }
  555. // record the intersection with respect to each face.
  556. for (carve::csg::detail::VFSMap::mapped_type::const_iterator k = face_set.begin(), ke = face_set.end(); k != ke; ++k) {
  557. meshset_t::face_t *f = (*k);
  558. data.fmap[f].insert(i_pt);
  559. }
  560. }
  561. }
  562. void carve::csg::CSG::_generateVertexVertexIntersections(meshset_t::vertex_t *va,
  563. meshset_t::edge_t *eb) {
  564. if (intersections.intersects(va, eb->v1())) {
  565. return;
  566. }
  567. double d_v1 = carve::geom::distance2(va->v, eb->v1()->v);
  568. if (d_v1 < carve::EPSILON2) {
  569. intersections.record(va, eb->v1(), va);
  570. }
  571. }
  572. void carve::csg::CSG::generateVertexVertexIntersections(meshset_t::face_t *a,
  573. const std::vector<meshset_t::face_t *> &b) {
  574. meshset_t::edge_t *ea, *eb;
  575. ea = a->edge;
  576. do {
  577. for (size_t i = 0; i < b.size(); ++i) {
  578. meshset_t::face_t *t = b[i];
  579. eb = t->edge;
  580. do {
  581. _generateVertexVertexIntersections(ea->v1(), eb);
  582. eb = eb->next;
  583. } while (eb != t->edge);
  584. }
  585. ea = ea->next;
  586. } while (ea != a->edge);
  587. }
  588. void carve::csg::CSG::_generateVertexEdgeIntersections(meshset_t::vertex_t *va,
  589. meshset_t::edge_t *eb) {
  590. if (intersections.intersects(va, eb)) {
  591. return;
  592. }
  593. carve::geom::aabb<3> eb_aabb;
  594. eb_aabb.fit(eb->v1()->v, eb->v2()->v);
  595. if (eb_aabb.maxAxisSeparation(va->v) > carve::EPSILON) {
  596. return;
  597. }
  598. double a = cross(eb->v2()->v - eb->v1()->v, va->v - eb->v1()->v).length2();
  599. double b = (eb->v2()->v - eb->v1()->v).length2();
  600. if (a < b * carve::EPSILON2) {
  601. // vertex-edge intersection
  602. intersections.record(eb, va, va);
  603. if (eb->rev) intersections.record(eb->rev, va, va);
  604. }
  605. }
  606. void carve::csg::CSG::generateVertexEdgeIntersections(meshset_t::face_t *a,
  607. const std::vector<meshset_t::face_t *> &b) {
  608. meshset_t::edge_t *ea, *eb;
  609. ea = a->edge;
  610. do {
  611. for (size_t i = 0; i < b.size(); ++i) {
  612. meshset_t::face_t *t = b[i];
  613. eb = t->edge;
  614. do {
  615. _generateVertexEdgeIntersections(ea->v1(), eb);
  616. eb = eb->next;
  617. } while (eb != t->edge);
  618. }
  619. ea = ea->next;
  620. } while (ea != a->edge);
  621. }
  622. void carve::csg::CSG::_generateEdgeEdgeIntersections(meshset_t::edge_t *ea,
  623. meshset_t::edge_t *eb) {
  624. if (intersections.intersects(ea, eb)) {
  625. return;
  626. }
  627. meshset_t::vertex_t *v1 = ea->v1(), *v2 = ea->v2();
  628. meshset_t::vertex_t *v3 = eb->v1(), *v4 = eb->v2();
  629. carve::geom::aabb<3> ea_aabb, eb_aabb;
  630. ea_aabb.fit(v1->v, v2->v);
  631. eb_aabb.fit(v3->v, v4->v);
  632. if (ea_aabb.maxAxisSeparation(eb_aabb) > EPSILON) return;
  633. meshset_t::vertex_t::vector_t p1, p2;
  634. double mu1, mu2;
  635. switch (carve::geom3d::rayRayIntersection(carve::geom3d::Ray(v2->v - v1->v, v1->v),
  636. carve::geom3d::Ray(v4->v - v3->v, v3->v),
  637. p1, p2, mu1, mu2)) {
  638. case carve::RR_INTERSECTION: {
  639. // edges intersect
  640. if (mu1 >= 0.0 && mu1 <= 1.0 && mu2 >= 0.0 && mu2 <= 1.0) {
  641. meshset_t::vertex_t *p = vertex_pool.get((p1 + p2) / 2.0);
  642. intersections.record(ea, eb, p);
  643. if (ea->rev) intersections.record(ea->rev, eb, p);
  644. if (eb->rev) intersections.record(ea, eb->rev, p);
  645. if (ea->rev && eb->rev) intersections.record(ea->rev, eb->rev, p);
  646. }
  647. break;
  648. }
  649. case carve::RR_PARALLEL: {
  650. // edges parallel. any intersection of this type should have
  651. // been handled by generateVertexEdgeIntersections().
  652. break;
  653. }
  654. case carve::RR_DEGENERATE: {
  655. throw carve::exception("degenerate edge");
  656. break;
  657. }
  658. case carve::RR_NO_INTERSECTION: {
  659. break;
  660. }
  661. }
  662. }
  663. void carve::csg::CSG::generateEdgeEdgeIntersections(meshset_t::face_t *a,
  664. const std::vector<meshset_t::face_t *> &b) {
  665. meshset_t::edge_t *ea, *eb;
  666. ea = a->edge;
  667. do {
  668. for (size_t i = 0; i < b.size(); ++i) {
  669. meshset_t::face_t *t = b[i];
  670. eb = t->edge;
  671. do {
  672. _generateEdgeEdgeIntersections(ea, eb);
  673. eb = eb->next;
  674. } while (eb != t->edge);
  675. }
  676. ea = ea->next;
  677. } while (ea != a->edge);
  678. }
  679. void carve::csg::CSG::_generateVertexFaceIntersections(meshset_t::face_t *fa,
  680. meshset_t::edge_t *eb) {
  681. if (intersections.intersects(eb->v1(), fa)) {
  682. return;
  683. }
  684. double d1 = carve::geom::distance(fa->plane, eb->v1()->v);
  685. if (fabs(d1) < carve::EPSILON &&
  686. fa->containsPoint(eb->v1()->v)) {
  687. intersections.record(eb->v1(), fa, eb->v1());
  688. }
  689. }
  690. void carve::csg::CSG::generateVertexFaceIntersections(meshset_t::face_t *a,
  691. const std::vector<meshset_t::face_t *> &b) {
  692. meshset_t::edge_t *eb;
  693. for (size_t i = 0; i < b.size(); ++i) {
  694. meshset_t::face_t *t = b[i];
  695. eb = t->edge;
  696. do {
  697. _generateVertexFaceIntersections(a, eb);
  698. eb = eb->next;
  699. } while (eb != t->edge);
  700. }
  701. }
  702. void carve::csg::CSG::_generateEdgeFaceIntersections(meshset_t::face_t *fa,
  703. meshset_t::edge_t *eb) {
  704. if (intersections.intersects(eb, fa)) {
  705. return;
  706. }
  707. meshset_t::vertex_t::vector_t _p;
  708. if (fa->simpleLineSegmentIntersection(carve::geom3d::LineSegment(eb->v1()->v, eb->v2()->v), _p)) {
  709. meshset_t::vertex_t *p = vertex_pool.get(_p);
  710. intersections.record(eb, fa, p);
  711. if (eb->rev) intersections.record(eb->rev, fa, p);
  712. }
  713. }
  714. void carve::csg::CSG::generateEdgeFaceIntersections(meshset_t::face_t *a,
  715. const std::vector<meshset_t::face_t *> &b) {
  716. meshset_t::edge_t *eb;
  717. for (size_t i = 0; i < b.size(); ++i) {
  718. meshset_t::face_t *t = b[i];
  719. eb = t->edge;
  720. do {
  721. _generateEdgeFaceIntersections(a, eb);
  722. eb = eb->next;
  723. } while (eb != t->edge);
  724. }
  725. }
  726. void carve::csg::CSG::generateIntersectionCandidates(meshset_t *a,
  727. const face_rtree_t *a_node,
  728. meshset_t *b,
  729. const face_rtree_t *b_node,
  730. face_pairs_t &face_pairs,
  731. bool descend_a) {
  732. if (!a_node->bbox.intersects(b_node->bbox)) {
  733. return;
  734. }
  735. if (a_node->child && (descend_a || !b_node->child)) {
  736. for (face_rtree_t *node = a_node->child; node; node = node->sibling) {
  737. generateIntersectionCandidates(a, node, b, b_node, face_pairs, false);
  738. }
  739. } else if (b_node->child) {
  740. for (face_rtree_t *node = b_node->child; node; node = node->sibling) {
  741. generateIntersectionCandidates(a, a_node, b, node, face_pairs, true);
  742. }
  743. } else {
  744. for (size_t i = 0; i < a_node->data.size(); ++i) {
  745. meshset_t::face_t *fa = a_node->data[i];
  746. carve::geom::aabb<3> aabb_a = fa->getAABB();
  747. if (aabb_a.maxAxisSeparation(b_node->bbox) > carve::EPSILON) continue;
  748. for (size_t j = 0; j < b_node->data.size(); ++j) {
  749. meshset_t::face_t *fb = b_node->data[j];
  750. carve::geom::aabb<3> aabb_b = fb->getAABB();
  751. if (aabb_b.maxAxisSeparation(aabb_a) > carve::EPSILON) continue;
  752. std::pair<double, double> a_ra = fa->rangeInDirection(fa->plane.N, fa->edge->vert->v);
  753. std::pair<double, double> b_ra = fb->rangeInDirection(fa->plane.N, fa->edge->vert->v);
  754. if (carve::rangeSeparation(a_ra, b_ra) > carve::EPSILON) continue;
  755. std::pair<double, double> a_rb = fa->rangeInDirection(fb->plane.N, fb->edge->vert->v);
  756. std::pair<double, double> b_rb = fb->rangeInDirection(fb->plane.N, fb->edge->vert->v);
  757. if (carve::rangeSeparation(a_rb, b_rb) > carve::EPSILON) continue;
  758. if (!facesAreCoplanar(fa, fb)) {
  759. face_pairs[fa].push_back(fb);
  760. face_pairs[fb].push_back(fa);
  761. }
  762. }
  763. }
  764. }
  765. }
  766. void carve::csg::CSG::generateIntersections(meshset_t *a,
  767. const face_rtree_t *a_rtree,
  768. meshset_t *b,
  769. const face_rtree_t *b_rtree,
  770. detail::Data &data) {
  771. face_pairs_t face_pairs;
  772. generateIntersectionCandidates(a, a_rtree, b, b_rtree, face_pairs);
  773. for (face_pairs_t::const_iterator i = face_pairs.begin(); i != face_pairs.end(); ++i) {
  774. meshset_t::face_t *f = (*i).first;
  775. meshset_t::edge_t *e = f->edge;
  776. do {
  777. data.vert_to_edges[e->v1()].push_back(e);
  778. e = e->next;
  779. } while (e != f->edge);
  780. }
  781. for (face_pairs_t::const_iterator i = face_pairs.begin(); i != face_pairs.end(); ++i) {
  782. generateVertexVertexIntersections((*i).first, (*i).second);
  783. }
  784. for (face_pairs_t::const_iterator i = face_pairs.begin(); i != face_pairs.end(); ++i) {
  785. generateVertexEdgeIntersections((*i).first, (*i).second);
  786. }
  787. for (face_pairs_t::const_iterator i = face_pairs.begin(); i != face_pairs.end(); ++i) {
  788. generateEdgeEdgeIntersections((*i).first, (*i).second);
  789. }
  790. for (face_pairs_t::const_iterator i = face_pairs.begin(); i != face_pairs.end(); ++i) {
  791. generateVertexFaceIntersections((*i).first, (*i).second);
  792. }
  793. for (face_pairs_t::const_iterator i = face_pairs.begin(); i != face_pairs.end(); ++i) {
  794. generateEdgeFaceIntersections((*i).first, (*i).second);
  795. }
  796. #if defined(CARVE_DEBUG)
  797. std::cerr << "makeVertexIntersections" << std::endl;
  798. #endif
  799. makeVertexIntersections();
  800. #if defined(CARVE_DEBUG)
  801. std::cerr << " intersections.size() " << intersections.size() << std::endl;
  802. map_histogram(std::cerr, intersections);
  803. std::cerr << " vertex_intersections.size() " << vertex_intersections.size() << std::endl;
  804. map_histogram(std::cerr, vertex_intersections);
  805. #endif
  806. #if defined(CARVE_DEBUG) && defined(DEBUG_DRAW_INTERSECTIONS)
  807. HOOK(drawIntersections(vertex_intersections););
  808. #endif
  809. #if defined(CARVE_DEBUG)
  810. std::cerr << " intersections.size() " << intersections.size() << std::endl;
  811. std::cerr << " vertex_intersections.size() " << vertex_intersections.size() << std::endl;
  812. #endif
  813. // notify about intersections.
  814. if (hooks.hasHook(Hooks::INTERSECTION_VERTEX_HOOK)) {
  815. for (VertexIntersections::const_iterator i = vertex_intersections.begin();
  816. i != vertex_intersections.end();
  817. ++i) {
  818. hooks.intersectionVertex((*i).first, (*i).second);
  819. }
  820. }
  821. // from here on, only vertex_intersections is used for intersection
  822. // information.
  823. // intersections still contains the vertex_to_face map. maybe that
  824. // should be moved out into another class.
  825. static_cast<Intersections::super>(intersections).clear();
  826. }
  827. carve::csg::CSG::CSG() {
  828. }
  829. /**
  830. * \brief For each intersected edge, decompose into a set of vertex pairs representing an ordered set of edge fragments.
  831. *
  832. * @tparam[in,out] data Internal intersection data. data.emap is used to produce data.divided_edges.
  833. */
  834. void carve::csg::CSG::divideIntersectedEdges(detail::Data &data) {
  835. static carve::TimingName FUNC_NAME("CSG::divideIntersectedEdges()");
  836. carve::TimingBlock block(FUNC_NAME);
  837. for (detail::EIntMap::const_iterator i = data.emap.begin(), ei = data.emap.end(); i != ei; ++i) {
  838. meshset_t::edge_t *edge = (*i).first;
  839. const detail::EIntMap::mapped_type &int_info = (*i).second;
  840. std::vector<meshset_t::vertex_t *> &verts = data.divided_edges[edge];
  841. orderEdgeIntersectionVertices(int_info.begin(), int_info.end(),
  842. edge->v2()->v - edge->v1()->v, edge->v1()->v,
  843. verts);
  844. }
  845. }
  846. carve::csg::CSG::~CSG() {
  847. }
  848. void carve::csg::CSG::makeFaceEdges(carve::csg::EdgeClassification &eclass,
  849. detail::Data &data) {
  850. detail::FSet face_b_set;
  851. for (detail::FVSMap::const_iterator
  852. i = data.fmap.begin(), ie = data.fmap.end();
  853. i != ie;
  854. ++i) {
  855. meshset_t::face_t *face_a = (*i).first;
  856. const detail::FVSMap::mapped_type &face_a_intersections = ((*i).second);
  857. face_b_set.clear();
  858. // work out the set of faces from the opposing polyhedron that intersect face_a.
  859. for (detail::FVSMap::mapped_type::const_iterator
  860. j = face_a_intersections.begin(), je = face_a_intersections.end();
  861. j != je;
  862. ++j) {
  863. for (detail::VFSMap::mapped_type::const_iterator
  864. k = data.fmap_rev[*j].begin(), ke = data.fmap_rev[*j].end();
  865. k != ke;
  866. ++k) {
  867. meshset_t::face_t *face_b = (*k);
  868. if (face_a != face_b && face_b->mesh->meshset != face_a->mesh->meshset) {
  869. face_b_set.insert(face_b);
  870. }
  871. }
  872. }
  873. // run through each intersecting face.
  874. for (detail::FSet::const_iterator
  875. j = face_b_set.begin(), je = face_b_set.end();
  876. j != je;
  877. ++j) {
  878. meshset_t::face_t *face_b = (*j);
  879. const detail::FVSMap::mapped_type &face_b_intersections = (data.fmap[face_b]);
  880. std::vector<meshset_t::vertex_t *> vertices;
  881. vertices.reserve(std::min(face_a_intersections.size(), face_b_intersections.size()));
  882. // record the points of intersection between face_a and face_b
  883. std::set_intersection(face_a_intersections.begin(),
  884. face_a_intersections.end(),
  885. face_b_intersections.begin(),
  886. face_b_intersections.end(),
  887. std::back_inserter(vertices));
  888. #if defined(CARVE_DEBUG)
  889. std::cerr << "face pair: "
  890. << face_a << ":" << face_b
  891. << " N(verts) " << vertices.size() << std::endl;
  892. for (std::vector<meshset_t::vertex_t *>::const_iterator i = vertices.begin(), e = vertices.end(); i != e; ++i) {
  893. std::cerr << (*i) << " " << (*i)->v << " ("
  894. << carve::geom::distance(face_a->plane, (*i)->v) << ","
  895. << carve::geom::distance(face_b->plane, (*i)->v) << ")"
  896. << std::endl;
  897. //CARVE_ASSERT(carve::geom3d::distance(face_a->plane_eqn, *(*i)) < EPSILON);
  898. //CARVE_ASSERT(carve::geom3d::distance(face_b->plane_eqn, *(*i)) < EPSILON);
  899. }
  900. #endif
  901. // if there are two points of intersection, then the added edge is simple to determine.
  902. if (vertices.size() == 2) {
  903. meshset_t::vertex_t *v1 = vertices[0];
  904. meshset_t::vertex_t *v2 = vertices[1];
  905. carve::geom3d::Vector c = (v1->v + v2->v) / 2;
  906. // determine whether the midpoint of the implied edge is contained in face_a and face_b
  907. #if defined(CARVE_DEBUG)
  908. std::cerr << "face_a->nVertices() = " << face_a->nVertices() << " face_a->containsPointInProjection(c) = " << face_a->containsPointInProjection(c) << std::endl;
  909. std::cerr << "face_b->nVertices() = " << face_b->nVertices() << " face_b->containsPointInProjection(c) = " << face_b->containsPointInProjection(c) << std::endl;
  910. #endif
  911. if (face_a->containsPointInProjection(c) && face_b->containsPointInProjection(c)) {
  912. #if defined(CARVE_DEBUG)
  913. std::cerr << "adding edge: " << v1 << "-" << v2 << std::endl;
  914. #if defined(DEBUG_DRAW_FACE_EDGES)
  915. HOOK(drawEdge(v1, v2, 1, 1, 1, 1, 1, 1, 1, 1, 2.0););
  916. #endif
  917. #endif
  918. // record the edge, with class information.
  919. if (v1 > v2) std::swap(v1, v2);
  920. eclass[ordered_edge(v1, v2)] = carve::csg::EC2(carve::csg::EDGE_ON, carve::csg::EDGE_ON);
  921. data.face_split_edges[face_a].insert(std::make_pair(v1, v2));
  922. data.face_split_edges[face_b].insert(std::make_pair(v1, v2));
  923. }
  924. continue;
  925. }
  926. // otherwise, it's more complex.
  927. carve::geom3d::Vector base, dir;
  928. std::vector<meshset_t::vertex_t *> ordered;
  929. // skip coplanar edges. this simplifies the resulting
  930. // mesh. eventually all coplanar face regions of two polyhedra
  931. // must reach a point where they are no longer coplanar (or the
  932. // polyhedra are identical).
  933. if (!facesAreCoplanar(face_a, face_b)) {
  934. // order the intersection vertices (they must lie along a
  935. // vector, as the faces aren't coplanar).
  936. selectOrderingProjection(vertices.begin(), vertices.end(), dir, base);
  937. orderVertices(vertices.begin(), vertices.end(), dir, base, ordered);
  938. // for each possible edge in the ordering, test the midpoint,
  939. // and record if it's contained in face_a and face_b.
  940. for (int k = 0, ke = (int)ordered.size() - 1; k < ke; ++k) {
  941. meshset_t::vertex_t *v1 = ordered[k];
  942. meshset_t::vertex_t *v2 = ordered[k + 1];
  943. carve::geom3d::Vector c = (v1->v + v2->v) / 2;
  944. #if defined(CARVE_DEBUG)
  945. std::cerr << "testing edge: " << v1 << "-" << v2 << " at " << c << std::endl;
  946. std::cerr << "a: " << face_a->containsPointInProjection(c) << " b: " << face_b->containsPointInProjection(c) << std::endl;
  947. std::cerr << "face_a->containsPointInProjection(c): " << face_a->containsPointInProjection(c) << std::endl;
  948. std::cerr << "face_b->containsPointInProjection(c): " << face_b->containsPointInProjection(c) << std::endl;
  949. #endif
  950. if (face_a->containsPointInProjection(c) && face_b->containsPointInProjection(c)) {
  951. #if defined(CARVE_DEBUG)
  952. std::cerr << "adding edge: " << v1 << "-" << v2 << std::endl;
  953. #if defined(DEBUG_DRAW_FACE_EDGES)
  954. HOOK(drawEdge(v1, v2, .5, .5, .5, 1, .5, .5, .5, 1, 2.0););
  955. #endif
  956. #endif
  957. // record the edge, with class information.
  958. if (v1 > v2) std::swap(v1, v2);
  959. eclass[ordered_edge(v1, v2)] = carve::csg::EC2(carve::csg::EDGE_ON, carve::csg::EDGE_ON);
  960. data.face_split_edges[face_a].insert(std::make_pair(v1, v2));
  961. data.face_split_edges[face_b].insert(std::make_pair(v1, v2));
  962. }
  963. }
  964. }
  965. }
  966. }
  967. #if defined(CARVE_DEBUG_WRITE_PLY_DATA)
  968. {
  969. V2Set edges;
  970. for (detail::FV2SMap::const_iterator i = data.face_split_edges.begin(); i != data.face_split_edges.end(); ++i) {
  971. edges.insert((*i).second.begin(), (*i).second.end());
  972. }
  973. detail::VSet vertices;
  974. for (V2Set::const_iterator i = edges.begin(); i != edges.end(); ++i) {
  975. vertices.insert((*i).first);
  976. vertices.insert((*i).second);
  977. }
  978. carve::line::PolylineSet intersection_graph;
  979. intersection_graph.vertices.resize(vertices.size());
  980. std::map<const meshset_t::vertex_t *, size_t> vmap;
  981. size_t j = 0;
  982. for (detail::VSet::const_iterator i = vertices.begin(); i != vertices.end(); ++i) {
  983. intersection_graph.vertices[j].v = (*i)->v;
  984. vmap[(*i)] = j++;
  985. }
  986. for (V2Set::const_iterator i = edges.begin(); i != edges.end(); ++i) {
  987. size_t line[2];
  988. line[0] = vmap[(*i).first];
  989. line[1] = vmap[(*i).second];
  990. intersection_graph.addPolyline(false, line, line + 2);
  991. }
  992. std::string out("/tmp/intersection-edges.ply");
  993. ::writePLY(out, &intersection_graph, true);
  994. }
  995. #endif
  996. }
  997. /**
  998. *
  999. *
  1000. * @param fll
  1001. */
  1002. #if 0
  1003. static void checkFaceLoopIntegrity(carve::csg::FaceLoopList &fll) {
  1004. static carve::TimingName FUNC_NAME("CSG::checkFaceLoopIntegrity()");
  1005. carve::TimingBlock block(FUNC_NAME);
  1006. std::unordered_map<carve::csg::V2, int> counts;
  1007. for (carve::csg::FaceLoop *fl = fll.head; fl; fl = fl->next) {
  1008. std::vector<carve::mesh::MeshSet<3>::vertex_t *> &loop = (fl->vertices);
  1009. carve::mesh::MeshSet<3>::vertex_t *v1, *v2;
  1010. v1 = loop[loop.size() - 1];
  1011. for (unsigned i = 0; i < loop.size(); ++i) {
  1012. v2 = loop[i];
  1013. if (v1 < v2) {
  1014. counts[std::make_pair(v1, v2)]++;
  1015. } else {
  1016. counts[std::make_pair(v2, v1)]--;
  1017. }
  1018. v1 = v2;
  1019. }
  1020. }
  1021. for (std::unordered_map<carve::csg::V2, int>::const_iterator
  1022. x = counts.begin(), xe = counts.end(); x != xe; ++x) {
  1023. if ((*x).second) {
  1024. std::cerr << "FACE LOOP ERROR: " << (*x).first.first << "-" << (*x).first.second << " : " << (*x).second << std::endl;
  1025. }
  1026. }
  1027. }
  1028. #endif
  1029. /**
  1030. *
  1031. *
  1032. * @param a
  1033. * @param b
  1034. * @param vclass
  1035. * @param eclass
  1036. * @param a_face_loops
  1037. * @param b_face_loops
  1038. * @param a_edge_count
  1039. * @param b_edge_count
  1040. * @param hooks
  1041. */
  1042. void carve::csg::CSG::calc(meshset_t *a,
  1043. const face_rtree_t *a_rtree,
  1044. meshset_t *b,
  1045. const face_rtree_t *b_rtree,
  1046. carve::csg::VertexClassification &vclass,
  1047. carve::csg::EdgeClassification &eclass,
  1048. carve::csg::FaceLoopList &a_face_loops,
  1049. carve::csg::FaceLoopList &b_face_loops,
  1050. size_t &a_edge_count,
  1051. size_t &b_edge_count) {
  1052. detail::Data data;
  1053. #if defined(CARVE_DEBUG)
  1054. std::cerr << "init" << std::endl;
  1055. #endif
  1056. init();
  1057. generateIntersections(a, a_rtree, b, b_rtree, data);
  1058. #if defined(CARVE_DEBUG)
  1059. std::cerr << "intersectingFacePairs" << std::endl;
  1060. #endif
  1061. intersectingFacePairs(data);
  1062. #if defined(CARVE_DEBUG)
  1063. std::cerr << "emap:" << std::endl;
  1064. map_histogram(std::cerr, data.emap);
  1065. std::cerr << "fmap:" << std::endl;
  1066. map_histogram(std::cerr, data.fmap);
  1067. std::cerr << "fmap_rev:" << std::endl;
  1068. map_histogram(std::cerr, data.fmap_rev);
  1069. #endif
  1070. // std::cerr << "removeCoplanarFaces" << std::endl;
  1071. // fp_intersections.removeCoplanarFaces();
  1072. #if defined(CARVE_DEBUG) && defined(DEBUG_DRAW_OCTREE)
  1073. HOOK(drawOctree(a->octree););
  1074. HOOK(drawOctree(b->octree););
  1075. #endif
  1076. #if defined(CARVE_DEBUG)
  1077. std::cerr << "divideIntersectedEdges" << std::endl;
  1078. #endif
  1079. divideIntersectedEdges(data);
  1080. #if defined(CARVE_DEBUG)
  1081. std::cerr << "makeFaceEdges" << std::endl;
  1082. #endif
  1083. // makeFaceEdges(data.face_split_edges, eclass, data.fmap, data.fmap_rev);
  1084. makeFaceEdges(eclass, data);
  1085. #if defined(CARVE_DEBUG)
  1086. std::cerr << "generateFaceLoops" << std::endl;
  1087. #endif
  1088. a_edge_count = generateFaceLoops(a, data, a_face_loops);
  1089. b_edge_count = generateFaceLoops(b, data, b_face_loops);
  1090. #if defined(CARVE_DEBUG)
  1091. std::cerr << "generated " << a_edge_count << " edges for poly a" << std::endl;
  1092. std::cerr << "generated " << b_edge_count << " edges for poly b" << std::endl;
  1093. #endif
  1094. #if defined(CARVE_DEBUG_WRITE_PLY_DATA)
  1095. {
  1096. std::auto_ptr<carve::mesh::MeshSet<3> > poly(faceLoopsToPolyhedron(a_face_loops));
  1097. writePLY("/tmp/a_split.ply", poly.get(), false);
  1098. }
  1099. {
  1100. std::auto_ptr<carve::mesh::MeshSet<3> > poly(faceLoopsToPolyhedron(b_face_loops));
  1101. writePLY("/tmp/b_split.ply", poly.get(), false);
  1102. }
  1103. #endif
  1104. // checkFaceLoopIntegrity(a_face_loops);
  1105. // checkFaceLoopIntegrity(b_face_loops);
  1106. #if defined(CARVE_DEBUG)
  1107. std::cerr << "classify" << std::endl;
  1108. #endif
  1109. // initialize some classification information.
  1110. for (std::vector<meshset_t::vertex_t>::iterator
  1111. i = a->vertex_storage.begin(), e = a->vertex_storage.end(); i != e; ++i) {
  1112. vclass[map_vertex(data.vmap, &(*i))].cls[0] = POINT_ON;
  1113. }
  1114. for (std::vector<meshset_t::vertex_t>::iterator
  1115. i = b->vertex_storage.begin(), e = b->vertex_storage.end(); i != e; ++i) {
  1116. vclass[map_vertex(data.vmap, &(*i))].cls[1] = POINT_ON;
  1117. }
  1118. for (VertexIntersections::const_iterator
  1119. i = vertex_intersections.begin(), e = vertex_intersections.end(); i != e; ++i) {
  1120. vclass[(*i).first] = PC2(POINT_ON, POINT_ON);
  1121. }
  1122. #if defined(CARVE_DEBUG)
  1123. std::cerr << data.divided_edges.size() << " edges are split" << std::endl;
  1124. std::cerr << data.face_split_edges.size() << " faces are split" << std::endl;
  1125. std::cerr << "poly a: " << a_face_loops.size() << " face loops" << std::endl;
  1126. std::cerr << "poly b: " << b_face_loops.size() << " face loops" << std::endl;
  1127. #endif
  1128. // std::cerr << "OCTREE A:" << std::endl;
  1129. // dump_octree_stats(a->octree.root, 0);
  1130. // std::cerr << "OCTREE B:" << std::endl;
  1131. // dump_octree_stats(b->octree.root, 0);
  1132. }
  1133. /**
  1134. *
  1135. *
  1136. * @param shared_edges
  1137. * @param result_list
  1138. * @param shared_edge_ptr
  1139. */
  1140. static void returnSharedEdges(carve::csg::V2Set &shared_edges,
  1141. std::list<carve::mesh::MeshSet<3> *> &result_list,
  1142. carve::csg::V2Set *shared_edge_ptr) {
  1143. // need to convert shared edges to point into result
  1144. typedef std::map<carve::geom3d::Vector, carve::mesh::MeshSet<3>::vertex_t *> remap_type;
  1145. remap_type remap;
  1146. for (std::list<carve::mesh::MeshSet<3> *>::iterator list_it =
  1147. result_list.begin(); list_it != result_list.end(); list_it++) {
  1148. carve::mesh::MeshSet<3> *result = *list_it;
  1149. if (result) {
  1150. for (std::vector<carve::mesh::MeshSet<3>::vertex_t>::iterator it =
  1151. result->vertex_storage.begin(); it != result->vertex_storage.end(); it++) {
  1152. remap.insert(std::make_pair((*it).v, &(*it)));
  1153. }
  1154. }
  1155. }
  1156. for (carve::csg::V2Set::iterator it = shared_edges.begin();
  1157. it != shared_edges.end(); it++) {
  1158. remap_type::iterator first_it = remap.find(((*it).first)->v);
  1159. remap_type::iterator second_it = remap.find(((*it).second)->v);
  1160. CARVE_ASSERT(first_it != remap.end() && second_it != remap.end());
  1161. shared_edge_ptr->insert(std::make_pair(first_it->second, second_it->second));
  1162. }
  1163. }
  1164. /**
  1165. *
  1166. *
  1167. * @param a
  1168. * @param b
  1169. * @param collector
  1170. * @param hooks
  1171. * @param shared_edges_ptr
  1172. * @param classify_type
  1173. *
  1174. * @return
  1175. */
  1176. carve::mesh::MeshSet<3> *carve::csg::CSG::compute(meshset_t *a,
  1177. meshset_t *b,
  1178. carve::csg::CSG::Collector &collector,
  1179. carve::csg::V2Set *shared_edges_ptr,
  1180. CLASSIFY_TYPE classify_type) {
  1181. static carve::TimingName FUNC_NAME("CSG::compute");
  1182. carve::TimingBlock block(FUNC_NAME);
  1183. VertexClassification vclass;
  1184. EdgeClassification eclass;
  1185. FLGroupList a_loops_grouped;
  1186. FLGroupList b_loops_grouped;
  1187. FaceLoopList a_face_loops;
  1188. FaceLoopList b_face_loops;
  1189. size_t a_edge_count;
  1190. size_t b_edge_count;
  1191. std::auto_ptr<face_rtree_t> a_rtree(face_rtree_t::construct_STR(a->faceBegin(), a->faceEnd(), 4, 4));
  1192. std::auto_ptr<face_rtree_t> b_rtree(face_rtree_t::construct_STR(b->faceBegin(), b->faceEnd(), 4, 4));
  1193. {
  1194. static carve::TimingName FUNC_NAME("CSG::compute - calc()");
  1195. carve::TimingBlock block(FUNC_NAME);
  1196. calc(a, a_rtree.get(), b, b_rtree.get(), vclass, eclass,a_face_loops, b_face_loops, a_edge_count, b_edge_count);
  1197. }
  1198. detail::LoopEdges a_edge_map;
  1199. detail::LoopEdges b_edge_map;
  1200. {
  1201. static carve::TimingName FUNC_NAME("CSG::compute - makeEdgeMap()");
  1202. carve::TimingBlock block(FUNC_NAME);
  1203. makeEdgeMap(a_face_loops, a_edge_count, a_edge_map);
  1204. makeEdgeMap(b_face_loops, b_edge_count, b_edge_map);
  1205. }
  1206. {
  1207. static carve::TimingName FUNC_NAME("CSG::compute - sortFaceLoopLists()");
  1208. carve::TimingBlock block(FUNC_NAME);
  1209. a_edge_map.sortFaceLoopLists();
  1210. b_edge_map.sortFaceLoopLists();
  1211. }
  1212. V2Set shared_edges;
  1213. {
  1214. static carve::TimingName FUNC_NAME("CSG::compute - findSharedEdges()");
  1215. carve::TimingBlock block(FUNC_NAME);
  1216. findSharedEdges(a_edge_map, b_edge_map, shared_edges);
  1217. }
  1218. {
  1219. static carve::TimingName FUNC_NAME("CSG::compute - groupFaceLoops()");
  1220. carve::TimingBlock block(FUNC_NAME);
  1221. groupFaceLoops(a, a_face_loops, a_edge_map, shared_edges, a_loops_grouped);
  1222. groupFaceLoops(b, b_face_loops, b_edge_map, shared_edges, b_loops_grouped);
  1223. #if defined(CARVE_DEBUG)
  1224. std::cerr << "*** a_loops_grouped.size(): " << a_loops_grouped.size() << std::endl;
  1225. std::cerr << "*** b_loops_grouped.size(): " << b_loops_grouped.size() << std::endl;
  1226. #endif
  1227. }
  1228. #if defined(CARVE_DEBUG) && defined(DEBUG_DRAW_GROUPS)
  1229. {
  1230. float n = 1.0 / (a_loops_grouped.size() + b_loops_grouped.size() + 1);
  1231. float H = 0.0, S = 1.0, V = 1.0;
  1232. float r, g, b;
  1233. for (FLGroupList::const_iterator i = a_loops_grouped.begin(); i != a_loops_grouped.end(); ++i) {
  1234. carve::colour::HSV2RGB(H, S, V, r, g, b); H += n;
  1235. drawFaceLoopList((*i).face_loops, r, g, b, 1.0, r * .5, g * .5, b * .5, 1.0, true);
  1236. }
  1237. for (FLGroupList::const_iterator i = b_loops_grouped.begin(); i != b_loops_grouped.end(); ++i) {
  1238. carve::colour::HSV2RGB(H, S, V, r, g, b); H += n;
  1239. drawFaceLoopList((*i).face_loops, r, g, b, 1.0, r * .5, g * .5, b * .5, 1.0, true);
  1240. }
  1241. for (FLGroupList::const_iterator i = a_loops_grouped.begin(); i != a_loops_grouped.end(); ++i) {
  1242. drawFaceLoopListWireframe((*i).face_loops);
  1243. }
  1244. for (FLGroupList::const_iterator i = b_loops_grouped.begin(); i != b_loops_grouped.end(); ++i) {
  1245. drawFaceLoopListWireframe((*i).face_loops);
  1246. }
  1247. }
  1248. #endif
  1249. switch (classify_type) {
  1250. case CLASSIFY_EDGE:
  1251. classifyFaceGroupsEdge(shared_edges,
  1252. vclass,
  1253. a,
  1254. a_rtree.get(),
  1255. a_loops_grouped,
  1256. a_edge_map,
  1257. b,
  1258. b_rtree.get(),
  1259. b_loops_grouped,
  1260. b_edge_map,
  1261. collector);
  1262. break;
  1263. case CLASSIFY_NORMAL:
  1264. classifyFaceGroups(shared_edges,
  1265. vclass,
  1266. a,
  1267. a_rtree.get(),
  1268. a_loops_grouped,
  1269. a_edge_map,
  1270. b,
  1271. b_rtree.get(),
  1272. b_loops_grouped,
  1273. b_edge_map,
  1274. collector);
  1275. break;
  1276. }
  1277. meshset_t *result = collector.done(hooks);
  1278. if (result != NULL && shared_edges_ptr != NULL) {
  1279. std::list<meshset_t *> result_list;
  1280. result_list.push_back(result);
  1281. returnSharedEdges(shared_edges, result_list, shared_edges_ptr);
  1282. }
  1283. return result;
  1284. }
  1285. /**
  1286. *
  1287. *
  1288. * @param a
  1289. * @param b
  1290. * @param op
  1291. * @param hooks
  1292. * @param shared_edges
  1293. * @param classify_type
  1294. *
  1295. * @return
  1296. */
  1297. carve::mesh::MeshSet<3> *carve::csg::CSG::compute(meshset_t *a,
  1298. meshset_t *b,
  1299. carve::csg::CSG::OP op,
  1300. carve::csg::V2Set *shared_edges,
  1301. CLASSIFY_TYPE classify_type) {
  1302. Collector *coll = makeCollector(op, a, b);
  1303. if (!coll) return NULL;
  1304. meshset_t *result = compute(a, b, *coll, shared_edges, classify_type);
  1305. delete coll;
  1306. return result;
  1307. }
  1308. /**
  1309. *
  1310. *
  1311. * @param closed
  1312. * @param open
  1313. * @param FaceClass
  1314. * @param result
  1315. * @param hooks
  1316. * @param shared_edges_ptr
  1317. *
  1318. * @return
  1319. */
  1320. bool carve::csg::CSG::sliceAndClassify(meshset_t *closed,
  1321. meshset_t *open,
  1322. std::list<std::pair<FaceClass, meshset_t *> > &result,
  1323. carve::csg::V2Set *shared_edges_ptr) {
  1324. if (!closed->isClosed()) return false;
  1325. carve::csg::VertexClassification vclass;
  1326. carve::csg::EdgeClassification eclass;
  1327. carve::csg::FLGroupList a_loops_grouped;
  1328. carve::csg::FLGroupList b_loops_grouped;
  1329. carve::csg::FaceLoopList a_face_loops;
  1330. carve::csg::FaceLoopList b_face_loops;
  1331. size_t a_edge_count;
  1332. size_t b_edge_count;
  1333. std::auto_ptr<face_rtree_t> closed_rtree(face_rtree_t::construct_STR(closed->faceBegin(), closed->faceEnd(), 4, 4));
  1334. std::auto_ptr<face_rtree_t> open_rtree(face_rtree_t::construct_STR(open->faceBegin(), open->faceEnd(), 4, 4));
  1335. calc(closed, closed_rtree.get(), open, open_rtree.get(), vclass, eclass,a_face_loops, b_face_loops, a_edge_count, b_edge_count);
  1336. detail::LoopEdges a_edge_map;
  1337. detail::LoopEdges b_edge_map;
  1338. makeEdgeMap(a_face_loops, a_edge_count, a_edge_map);
  1339. makeEdgeMap(b_face_loops, b_edge_count, b_edge_map);
  1340. carve::csg::V2Set shared_edges;
  1341. findSharedEdges(a_edge_map, b_edge_map, shared_edges);
  1342. groupFaceLoops(closed, a_face_loops, a_edge_map, shared_edges, a_loops_grouped);
  1343. groupFaceLoops(open, b_face_loops, b_edge_map, shared_edges, b_loops_grouped);
  1344. halfClassifyFaceGroups(shared_edges,
  1345. vclass,
  1346. closed,
  1347. closed_rtree.get(),
  1348. a_loops_grouped,
  1349. a_edge_map,
  1350. open,
  1351. open_rtree.get(),
  1352. b_loops_grouped,
  1353. b_edge_map,
  1354. result);
  1355. if (shared_edges_ptr != NULL) {
  1356. std::list<meshset_t *> result_list;
  1357. for (std::list<std::pair<FaceClass, meshset_t *> >::iterator it = result.begin(); it != result.end(); it++) {
  1358. result_list.push_back(it->second);
  1359. }
  1360. returnSharedEdges(shared_edges, result_list, shared_edges_ptr);
  1361. }
  1362. return true;
  1363. }
  1364. /**
  1365. *
  1366. *
  1367. * @param a
  1368. * @param b
  1369. * @param a_sliced
  1370. * @param b_sliced
  1371. * @param hooks
  1372. * @param shared_edges_ptr
  1373. */
  1374. void carve::csg::CSG::slice(meshset_t *a,
  1375. meshset_t *b,
  1376. std::list<meshset_t *> &a_sliced,
  1377. std::list<meshset_t *> &b_sliced,
  1378. carve::csg::V2Set *shared_edges_ptr) {
  1379. carve::csg::VertexClassification vclass;
  1380. carve::csg::EdgeClassification eclass;
  1381. carve::csg::FLGroupList a_loops_grouped;
  1382. carve::csg::FLGroupList b_loops_grouped;
  1383. carve::csg::FaceLoopList a_face_loops;
  1384. carve::csg::FaceLoopList b_face_loops;
  1385. size_t a_edge_count;
  1386. size_t b_edge_count;
  1387. std::auto_ptr<face_rtree_t> a_rtree(face_rtree_t::construct_STR(a->faceBegin(), a->faceEnd(), 4, 4));
  1388. std::auto_ptr<face_rtree_t> b_rtree(face_rtree_t::construct_STR(b->faceBegin(), b->faceEnd(), 4, 4));
  1389. calc(a, a_rtree.get(), b, b_rtree.get(), vclass, eclass,a_face_loops, b_face_loops, a_edge_count, b_edge_count);
  1390. detail::LoopEdges a_edge_map;
  1391. detail::LoopEdges b_edge_map;
  1392. makeEdgeMap(a_face_loops, a_edge_count, a_edge_map);
  1393. makeEdgeMap(b_face_loops, b_edge_count, b_edge_map);
  1394. carve::csg::V2Set shared_edges;
  1395. findSharedEdges(a_edge_map, b_edge_map, shared_edges);
  1396. groupFaceLoops(a, a_face_loops, a_edge_map, shared_edges, a_loops_grouped);
  1397. groupFaceLoops(b, b_face_loops, b_edge_map, shared_edges, b_loops_grouped);
  1398. for (carve::csg::FLGroupList::iterator
  1399. i = a_loops_grouped.begin(), e = a_loops_grouped.end();
  1400. i != e; ++i) {
  1401. Collector *all = makeCollector(ALL, a, b);
  1402. all->collect(&*i, hooks);
  1403. a_sliced.push_back(all->done(hooks));
  1404. delete all;
  1405. }
  1406. for (carve::csg::FLGroupList::iterator
  1407. i = b_loops_grouped.begin(), e = b_loops_grouped.end();
  1408. i != e; ++i) {
  1409. Collector *all = makeCollector(ALL, a, b);
  1410. all->collect(&*i, hooks);
  1411. b_sliced.push_back(all->done(hooks));
  1412. delete all;
  1413. }
  1414. if (shared_edges_ptr != NULL) {
  1415. std::list<meshset_t *> result_list;
  1416. result_list.insert(result_list.end(), a_sliced.begin(), a_sliced.end());
  1417. result_list.insert(result_list.end(), b_sliced.begin(), b_sliced.end());
  1418. returnSharedEdges(shared_edges, result_list, shared_edges_ptr);
  1419. }
  1420. }
  1421. /**
  1422. *
  1423. *
  1424. */
  1425. void carve::csg::CSG::init() {
  1426. intersections.clear();
  1427. vertex_intersections.clear();
  1428. vertex_pool.reset();
  1429. }