sweep.cc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. /*
  2. * Poly2Tri Copyright (c) 2009-2010, Poly2Tri Contributors
  3. * http://code.google.com/p/poly2tri/
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted provided that the following conditions are met:
  9. *
  10. * * Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. * * Neither the name of Poly2Tri nor the names of its contributors may be
  16. * used to endorse or promote products derived from this software without specific
  17. * prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include <stdexcept>
  32. #include "sweep.h"
  33. #include "sweep_context.h"
  34. #include "advancing_front.h"
  35. #include "../common/utils.h"
  36. namespace p2t {
  37. // Triangulate simple polygon with holes
  38. void Sweep::Triangulate(SweepContext& tcx)
  39. {
  40. tcx.InitTriangulation();
  41. tcx.CreateAdvancingFront(nodes_);
  42. // Sweep points; build mesh
  43. SweepPoints(tcx);
  44. // Clean up
  45. FinalizationPolygon(tcx);
  46. }
  47. void Sweep::SweepPoints(SweepContext& tcx)
  48. {
  49. for (size_t i = 1; i < tcx.point_count(); i++) {
  50. Point& point = *tcx.GetPoint(i);
  51. Node* node = &PointEvent(tcx, point);
  52. for (unsigned int i = 0; i < point.edge_list.size(); i++) {
  53. EdgeEvent(tcx, point.edge_list[i], node);
  54. }
  55. }
  56. }
  57. void Sweep::FinalizationPolygon(SweepContext& tcx)
  58. {
  59. // Get an Internal triangle to start with
  60. Triangle* t = tcx.front()->head()->next->triangle;
  61. Point* p = tcx.front()->head()->next->point;
  62. while (!t->GetConstrainedEdgeCW(*p)) {
  63. t = t->NeighborCCW(*p);
  64. }
  65. // Collect interior triangles constrained by edges
  66. tcx.MeshClean(*t);
  67. }
  68. Node& Sweep::PointEvent(SweepContext& tcx, Point& point)
  69. {
  70. Node& node = tcx.LocateNode(point);
  71. Node& new_node = NewFrontTriangle(tcx, point, node);
  72. // Only need to check +epsilon since point never have smaller
  73. // x value than node due to how we fetch nodes from the front
  74. if (point.x <= node.point->x + EPSILON) {
  75. Fill(tcx, node);
  76. }
  77. //tcx.AddNode(new_node);
  78. FillAdvancingFront(tcx, new_node);
  79. return new_node;
  80. }
  81. void Sweep::EdgeEvent(SweepContext& tcx, Edge* edge, Node* node)
  82. {
  83. tcx.edge_event.constrained_edge = edge;
  84. tcx.edge_event.right = (edge->p->x > edge->q->x);
  85. if (IsEdgeSideOfTriangle(*node->triangle, *edge->p, *edge->q)) {
  86. return;
  87. }
  88. // For now we will do all needed filling
  89. // TODO: integrate with flip process might give some better performance
  90. // but for now this avoid the issue with cases that needs both flips and fills
  91. FillEdgeEvent(tcx, edge, node);
  92. EdgeEvent(tcx, *edge->p, *edge->q, node->triangle, *edge->q);
  93. }
  94. void Sweep::EdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle* triangle, Point& point)
  95. {
  96. if (IsEdgeSideOfTriangle(*triangle, ep, eq)) {
  97. return;
  98. }
  99. Point* p1 = triangle->PointCCW(point);
  100. Orientation o1 = Orient2d(eq, *p1, ep);
  101. if (o1 == COLLINEAR) {
  102. if( triangle->Contains(&eq, p1)) {
  103. triangle->MarkConstrainedEdge(&eq, p1 );
  104. // We are modifying the constraint maybe it would be better to
  105. // not change the given constraint and just keep a variable for the new constraint
  106. tcx.edge_event.constrained_edge->q = p1;
  107. triangle = &triangle->NeighborAcross(point);
  108. EdgeEvent( tcx, ep, *p1, triangle, *p1 );
  109. } else {
  110. std::runtime_error("EdgeEvent - collinear points not supported");
  111. assert(0);
  112. }
  113. return;
  114. }
  115. Point* p2 = triangle->PointCW(point);
  116. Orientation o2 = Orient2d(eq, *p2, ep);
  117. if (o2 == COLLINEAR) {
  118. if( triangle->Contains(&eq, p2)) {
  119. triangle->MarkConstrainedEdge(&eq, p2 );
  120. // We are modifying the constraint maybe it would be better to
  121. // not change the given constraint and just keep a variable for the new constraint
  122. tcx.edge_event.constrained_edge->q = p2;
  123. triangle = &triangle->NeighborAcross(point);
  124. EdgeEvent( tcx, ep, *p2, triangle, *p2 );
  125. } else {
  126. std::runtime_error("EdgeEvent - collinear points not supported");
  127. assert(0);
  128. }
  129. return;
  130. }
  131. if (o1 == o2) {
  132. // Need to decide if we are rotating CW or CCW to get to a triangle
  133. // that will cross edge
  134. if (o1 == CW) {
  135. triangle = triangle->NeighborCCW(point);
  136. } else{
  137. triangle = triangle->NeighborCW(point);
  138. }
  139. EdgeEvent(tcx, ep, eq, triangle, point);
  140. } else {
  141. // This triangle crosses constraint so lets flippin start!
  142. FlipEdgeEvent(tcx, ep, eq, triangle, point);
  143. }
  144. }
  145. bool Sweep::IsEdgeSideOfTriangle(Triangle& triangle, Point& ep, Point& eq)
  146. {
  147. const int index = triangle.EdgeIndex(&ep, &eq);
  148. if (index != -1) {
  149. triangle.MarkConstrainedEdge(index);
  150. Triangle* t = triangle.GetNeighbor(index);
  151. if (t) {
  152. t->MarkConstrainedEdge(&ep, &eq);
  153. }
  154. return true;
  155. }
  156. return false;
  157. }
  158. Node& Sweep::NewFrontTriangle(SweepContext& tcx, Point& point, Node& node)
  159. {
  160. Triangle* triangle = new Triangle(point, *node.point, *node.next->point);
  161. triangle->MarkNeighbor(*node.triangle);
  162. tcx.AddToMap(triangle);
  163. Node* new_node = new Node(point);
  164. nodes_.push_back(new_node);
  165. new_node->next = node.next;
  166. new_node->prev = &node;
  167. node.next->prev = new_node;
  168. node.next = new_node;
  169. if (!Legalize(tcx, *triangle)) {
  170. tcx.MapTriangleToNodes(*triangle);
  171. }
  172. return *new_node;
  173. }
  174. void Sweep::Fill(SweepContext& tcx, Node& node)
  175. {
  176. Triangle* triangle = new Triangle(*node.prev->point, *node.point, *node.next->point);
  177. // TODO: should copy the constrained_edge value from neighbor triangles
  178. // for now constrained_edge values are copied during the legalize
  179. triangle->MarkNeighbor(*node.prev->triangle);
  180. triangle->MarkNeighbor(*node.triangle);
  181. tcx.AddToMap(triangle);
  182. // Update the advancing front
  183. node.prev->next = node.next;
  184. node.next->prev = node.prev;
  185. // If it was legalized the triangle has already been mapped
  186. if (!Legalize(tcx, *triangle)) {
  187. tcx.MapTriangleToNodes(*triangle);
  188. }
  189. }
  190. void Sweep::FillAdvancingFront(SweepContext& tcx, Node& n)
  191. {
  192. // Fill right holes
  193. Node* node = n.next;
  194. while (node->next) {
  195. // if HoleAngle exceeds 90 degrees then break.
  196. if (LargeHole_DontFill(node)) break;
  197. Fill(tcx, *node);
  198. node = node->next;
  199. }
  200. // Fill left holes
  201. node = n.prev;
  202. while (node->prev) {
  203. // if HoleAngle exceeds 90 degrees then break.
  204. if (LargeHole_DontFill(node)) break;
  205. Fill(tcx, *node);
  206. node = node->prev;
  207. }
  208. // Fill right basins
  209. if (n.next && n.next->next) {
  210. const double angle = BasinAngle(n);
  211. if (angle < PI_3div4) {
  212. FillBasin(tcx, n);
  213. }
  214. }
  215. }
  216. // True if HoleAngle exceeds 90 degrees.
  217. bool Sweep::LargeHole_DontFill(const Node* node) const {
  218. const Node* nextNode = node->next;
  219. const Node* prevNode = node->prev;
  220. if (!AngleExceeds90Degrees(node->point, nextNode->point, prevNode->point))
  221. return false;
  222. // Check additional points on front.
  223. const Node* next2Node = nextNode->next;
  224. // "..Plus.." because only want angles on same side as point being added.
  225. if ((next2Node != NULL) && !AngleExceedsPlus90DegreesOrIsNegative(node->point, next2Node->point, prevNode->point))
  226. return false;
  227. const Node* prev2Node = prevNode->prev;
  228. // "..Plus.." because only want angles on same side as point being added.
  229. if ((prev2Node != NULL) && !AngleExceedsPlus90DegreesOrIsNegative(node->point, nextNode->point, prev2Node->point))
  230. return false;
  231. return true;
  232. }
  233. bool Sweep::AngleExceeds90Degrees(const Point* origin, const Point* pa, const Point* pb) const {
  234. const double angle = Angle(origin, pa, pb);
  235. return ((angle > PI_div2) || (angle < -PI_div2));
  236. }
  237. bool Sweep::AngleExceedsPlus90DegreesOrIsNegative(const Point* origin, const Point* pa, const Point* pb) const {
  238. const double angle = Angle(origin, pa, pb);
  239. return (angle > PI_div2) || (angle < 0);
  240. }
  241. double Sweep::Angle(const Point* origin, const Point* pa, const Point* pb) const {
  242. /* Complex plane
  243. * ab = cosA +i*sinA
  244. * ab = (ax + ay*i)(bx + by*i) = (ax*bx + ay*by) + i(ax*by-ay*bx)
  245. * atan2(y,x) computes the principal value of the argument function
  246. * applied to the complex number x+iy
  247. * Where x = ax*bx + ay*by
  248. * y = ax*by - ay*bx
  249. */
  250. const double px = origin->x;
  251. const double py = origin->y;
  252. const double ax = pa->x- px;
  253. const double ay = pa->y - py;
  254. const double bx = pb->x - px;
  255. const double by = pb->y - py;
  256. const double x = ax * by - ay * bx;
  257. const double y = ax * bx + ay * by;
  258. return atan2(x, y);
  259. }
  260. double Sweep::BasinAngle(const Node& node) const
  261. {
  262. const double ax = node.point->x - node.next->next->point->x;
  263. const double ay = node.point->y - node.next->next->point->y;
  264. return atan2(ay, ax);
  265. }
  266. double Sweep::HoleAngle(const Node& node) const
  267. {
  268. /* Complex plane
  269. * ab = cosA +i*sinA
  270. * ab = (ax + ay*i)(bx + by*i) = (ax*bx + ay*by) + i(ax*by-ay*bx)
  271. * atan2(y,x) computes the principal value of the argument function
  272. * applied to the complex number x+iy
  273. * Where x = ax*bx + ay*by
  274. * y = ax*by - ay*bx
  275. */
  276. const double ax = node.next->point->x - node.point->x;
  277. const double ay = node.next->point->y - node.point->y;
  278. const double bx = node.prev->point->x - node.point->x;
  279. const double by = node.prev->point->y - node.point->y;
  280. return atan2(ax * by - ay * bx, ax * bx + ay * by);
  281. }
  282. bool Sweep::Legalize(SweepContext& tcx, Triangle& t)
  283. {
  284. // To legalize a triangle we start by finding if any of the three edges
  285. // violate the Delaunay condition
  286. for (int i = 0; i < 3; i++) {
  287. if (t.delaunay_edge[i])
  288. continue;
  289. Triangle* ot = t.GetNeighbor(i);
  290. if (ot) {
  291. Point* p = t.GetPoint(i);
  292. Point* op = ot->OppositePoint(t, *p);
  293. int oi = ot->Index(op);
  294. // If this is a Constrained Edge or a Delaunay Edge(only during recursive legalization)
  295. // then we should not try to legalize
  296. if (ot->constrained_edge[oi] || ot->delaunay_edge[oi]) {
  297. t.constrained_edge[i] = ot->constrained_edge[oi];
  298. continue;
  299. }
  300. bool inside = Incircle(*p, *t.PointCCW(*p), *t.PointCW(*p), *op);
  301. if (inside) {
  302. // Lets mark this shared edge as Delaunay
  303. t.delaunay_edge[i] = true;
  304. ot->delaunay_edge[oi] = true;
  305. // Lets rotate shared edge one vertex CW to legalize it
  306. RotateTrianglePair(t, *p, *ot, *op);
  307. // We now got one valid Delaunay Edge shared by two triangles
  308. // This gives us 4 new edges to check for Delaunay
  309. // Make sure that triangle to node mapping is done only one time for a specific triangle
  310. bool not_legalized = !Legalize(tcx, t);
  311. if (not_legalized) {
  312. tcx.MapTriangleToNodes(t);
  313. }
  314. not_legalized = !Legalize(tcx, *ot);
  315. if (not_legalized)
  316. tcx.MapTriangleToNodes(*ot);
  317. // Reset the Delaunay edges, since they only are valid Delaunay edges
  318. // until we add a new triangle or point.
  319. // XXX: need to think about this. Can these edges be tried after we
  320. // return to previous recursive level?
  321. t.delaunay_edge[i] = false;
  322. ot->delaunay_edge[oi] = false;
  323. // If triangle have been legalized no need to check the other edges since
  324. // the recursive legalization will handles those so we can end here.
  325. return true;
  326. }
  327. }
  328. }
  329. return false;
  330. }
  331. bool Sweep::Incircle(const Point& pa, const Point& pb, const Point& pc, const Point& pd) const
  332. {
  333. const double adx = pa.x - pd.x;
  334. const double ady = pa.y - pd.y;
  335. const double bdx = pb.x - pd.x;
  336. const double bdy = pb.y - pd.y;
  337. const double adxbdy = adx * bdy;
  338. const double bdxady = bdx * ady;
  339. const double oabd = adxbdy - bdxady;
  340. if (oabd <= 0)
  341. return false;
  342. const double cdx = pc.x - pd.x;
  343. const double cdy = pc.y - pd.y;
  344. const double cdxady = cdx * ady;
  345. const double adxcdy = adx * cdy;
  346. const double ocad = cdxady - adxcdy;
  347. if (ocad <= 0)
  348. return false;
  349. const double bdxcdy = bdx * cdy;
  350. const double cdxbdy = cdx * bdy;
  351. const double alift = adx * adx + ady * ady;
  352. const double blift = bdx * bdx + bdy * bdy;
  353. const double clift = cdx * cdx + cdy * cdy;
  354. const double det = alift * (bdxcdy - cdxbdy) + blift * ocad + clift * oabd;
  355. return det > 0;
  356. }
  357. void Sweep::RotateTrianglePair(Triangle& t, Point& p, Triangle& ot, Point& op) const
  358. {
  359. Triangle* n1, *n2, *n3, *n4;
  360. n1 = t.NeighborCCW(p);
  361. n2 = t.NeighborCW(p);
  362. n3 = ot.NeighborCCW(op);
  363. n4 = ot.NeighborCW(op);
  364. bool ce1, ce2, ce3, ce4;
  365. ce1 = t.GetConstrainedEdgeCCW(p);
  366. ce2 = t.GetConstrainedEdgeCW(p);
  367. ce3 = ot.GetConstrainedEdgeCCW(op);
  368. ce4 = ot.GetConstrainedEdgeCW(op);
  369. bool de1, de2, de3, de4;
  370. de1 = t.GetDelunayEdgeCCW(p);
  371. de2 = t.GetDelunayEdgeCW(p);
  372. de3 = ot.GetDelunayEdgeCCW(op);
  373. de4 = ot.GetDelunayEdgeCW(op);
  374. t.Legalize(p, op);
  375. ot.Legalize(op, p);
  376. // Remap delaunay_edge
  377. ot.SetDelunayEdgeCCW(p, de1);
  378. t.SetDelunayEdgeCW(p, de2);
  379. t.SetDelunayEdgeCCW(op, de3);
  380. ot.SetDelunayEdgeCW(op, de4);
  381. // Remap constrained_edge
  382. ot.SetConstrainedEdgeCCW(p, ce1);
  383. t.SetConstrainedEdgeCW(p, ce2);
  384. t.SetConstrainedEdgeCCW(op, ce3);
  385. ot.SetConstrainedEdgeCW(op, ce4);
  386. // Remap neighbors
  387. // XXX: might optimize the markNeighbor by keeping track of
  388. // what side should be assigned to what neighbor after the
  389. // rotation. Now mark neighbor does lots of testing to find
  390. // the right side.
  391. t.ClearNeighbors();
  392. ot.ClearNeighbors();
  393. if (n1) ot.MarkNeighbor(*n1);
  394. if (n2) t.MarkNeighbor(*n2);
  395. if (n3) t.MarkNeighbor(*n3);
  396. if (n4) ot.MarkNeighbor(*n4);
  397. t.MarkNeighbor(ot);
  398. }
  399. void Sweep::FillBasin(SweepContext& tcx, Node& node)
  400. {
  401. if (Orient2d(*node.point, *node.next->point, *node.next->next->point) == CCW) {
  402. tcx.basin.left_node = node.next->next;
  403. } else {
  404. tcx.basin.left_node = node.next;
  405. }
  406. // Find the bottom and right node
  407. tcx.basin.bottom_node = tcx.basin.left_node;
  408. while (tcx.basin.bottom_node->next
  409. && tcx.basin.bottom_node->point->y >= tcx.basin.bottom_node->next->point->y) {
  410. tcx.basin.bottom_node = tcx.basin.bottom_node->next;
  411. }
  412. if (tcx.basin.bottom_node == tcx.basin.left_node) {
  413. // No valid basin
  414. return;
  415. }
  416. tcx.basin.right_node = tcx.basin.bottom_node;
  417. while (tcx.basin.right_node->next
  418. && tcx.basin.right_node->point->y < tcx.basin.right_node->next->point->y) {
  419. tcx.basin.right_node = tcx.basin.right_node->next;
  420. }
  421. if (tcx.basin.right_node == tcx.basin.bottom_node) {
  422. // No valid basins
  423. return;
  424. }
  425. tcx.basin.width = tcx.basin.right_node->point->x - tcx.basin.left_node->point->x;
  426. tcx.basin.left_highest = tcx.basin.left_node->point->y > tcx.basin.right_node->point->y;
  427. FillBasinReq(tcx, tcx.basin.bottom_node);
  428. }
  429. void Sweep::FillBasinReq(SweepContext& tcx, Node* node)
  430. {
  431. // if shallow stop filling
  432. if (IsShallow(tcx, *node)) {
  433. return;
  434. }
  435. Fill(tcx, *node);
  436. if (node->prev == tcx.basin.left_node && node->next == tcx.basin.right_node) {
  437. return;
  438. } else if (node->prev == tcx.basin.left_node) {
  439. Orientation o = Orient2d(*node->point, *node->next->point, *node->next->next->point);
  440. if (o == CW) {
  441. return;
  442. }
  443. node = node->next;
  444. } else if (node->next == tcx.basin.right_node) {
  445. Orientation o = Orient2d(*node->point, *node->prev->point, *node->prev->prev->point);
  446. if (o == CCW) {
  447. return;
  448. }
  449. node = node->prev;
  450. } else {
  451. // Continue with the neighbor node with lowest Y value
  452. if (node->prev->point->y < node->next->point->y) {
  453. node = node->prev;
  454. } else {
  455. node = node->next;
  456. }
  457. }
  458. FillBasinReq(tcx, node);
  459. }
  460. bool Sweep::IsShallow(SweepContext& tcx, Node& node)
  461. {
  462. double height;
  463. if (tcx.basin.left_highest) {
  464. height = tcx.basin.left_node->point->y - node.point->y;
  465. } else {
  466. height = tcx.basin.right_node->point->y - node.point->y;
  467. }
  468. // if shallow stop filling
  469. if (tcx.basin.width > height) {
  470. return true;
  471. }
  472. return false;
  473. }
  474. void Sweep::FillEdgeEvent(SweepContext& tcx, Edge* edge, Node* node)
  475. {
  476. if (tcx.edge_event.right) {
  477. FillRightAboveEdgeEvent(tcx, edge, node);
  478. } else {
  479. FillLeftAboveEdgeEvent(tcx, edge, node);
  480. }
  481. }
  482. void Sweep::FillRightAboveEdgeEvent(SweepContext& tcx, Edge* edge, Node* node)
  483. {
  484. while (node->next->point->x < edge->p->x) {
  485. // Check if next node is below the edge
  486. if (Orient2d(*edge->q, *node->next->point, *edge->p) == CCW) {
  487. FillRightBelowEdgeEvent(tcx, edge, *node);
  488. } else {
  489. node = node->next;
  490. }
  491. }
  492. }
  493. void Sweep::FillRightBelowEdgeEvent(SweepContext& tcx, Edge* edge, Node& node)
  494. {
  495. if (node.point->x < edge->p->x) {
  496. if (Orient2d(*node.point, *node.next->point, *node.next->next->point) == CCW) {
  497. // Concave
  498. FillRightConcaveEdgeEvent(tcx, edge, node);
  499. } else{
  500. // Convex
  501. FillRightConvexEdgeEvent(tcx, edge, node);
  502. // Retry this one
  503. FillRightBelowEdgeEvent(tcx, edge, node);
  504. }
  505. }
  506. }
  507. void Sweep::FillRightConcaveEdgeEvent(SweepContext& tcx, Edge* edge, Node& node)
  508. {
  509. Fill(tcx, *node.next);
  510. if (node.next->point != edge->p) {
  511. // Next above or below edge?
  512. if (Orient2d(*edge->q, *node.next->point, *edge->p) == CCW) {
  513. // Below
  514. if (Orient2d(*node.point, *node.next->point, *node.next->next->point) == CCW) {
  515. // Next is concave
  516. FillRightConcaveEdgeEvent(tcx, edge, node);
  517. } else {
  518. // Next is convex
  519. }
  520. }
  521. }
  522. }
  523. void Sweep::FillRightConvexEdgeEvent(SweepContext& tcx, Edge* edge, Node& node)
  524. {
  525. // Next concave or convex?
  526. if (Orient2d(*node.next->point, *node.next->next->point, *node.next->next->next->point) == CCW) {
  527. // Concave
  528. FillRightConcaveEdgeEvent(tcx, edge, *node.next);
  529. } else{
  530. // Convex
  531. // Next above or below edge?
  532. if (Orient2d(*edge->q, *node.next->next->point, *edge->p) == CCW) {
  533. // Below
  534. FillRightConvexEdgeEvent(tcx, edge, *node.next);
  535. } else{
  536. // Above
  537. }
  538. }
  539. }
  540. void Sweep::FillLeftAboveEdgeEvent(SweepContext& tcx, Edge* edge, Node* node)
  541. {
  542. while (node->prev->point->x > edge->p->x) {
  543. // Check if next node is below the edge
  544. if (Orient2d(*edge->q, *node->prev->point, *edge->p) == CW) {
  545. FillLeftBelowEdgeEvent(tcx, edge, *node);
  546. } else {
  547. node = node->prev;
  548. }
  549. }
  550. }
  551. void Sweep::FillLeftBelowEdgeEvent(SweepContext& tcx, Edge* edge, Node& node)
  552. {
  553. if (node.point->x > edge->p->x) {
  554. if (Orient2d(*node.point, *node.prev->point, *node.prev->prev->point) == CW) {
  555. // Concave
  556. FillLeftConcaveEdgeEvent(tcx, edge, node);
  557. } else {
  558. // Convex
  559. FillLeftConvexEdgeEvent(tcx, edge, node);
  560. // Retry this one
  561. FillLeftBelowEdgeEvent(tcx, edge, node);
  562. }
  563. }
  564. }
  565. void Sweep::FillLeftConvexEdgeEvent(SweepContext& tcx, Edge* edge, Node& node)
  566. {
  567. // Next concave or convex?
  568. if (Orient2d(*node.prev->point, *node.prev->prev->point, *node.prev->prev->prev->point) == CW) {
  569. // Concave
  570. FillLeftConcaveEdgeEvent(tcx, edge, *node.prev);
  571. } else{
  572. // Convex
  573. // Next above or below edge?
  574. if (Orient2d(*edge->q, *node.prev->prev->point, *edge->p) == CW) {
  575. // Below
  576. FillLeftConvexEdgeEvent(tcx, edge, *node.prev);
  577. } else{
  578. // Above
  579. }
  580. }
  581. }
  582. void Sweep::FillLeftConcaveEdgeEvent(SweepContext& tcx, Edge* edge, Node& node)
  583. {
  584. Fill(tcx, *node.prev);
  585. if (node.prev->point != edge->p) {
  586. // Next above or below edge?
  587. if (Orient2d(*edge->q, *node.prev->point, *edge->p) == CW) {
  588. // Below
  589. if (Orient2d(*node.point, *node.prev->point, *node.prev->prev->point) == CW) {
  590. // Next is concave
  591. FillLeftConcaveEdgeEvent(tcx, edge, node);
  592. } else{
  593. // Next is convex
  594. }
  595. }
  596. }
  597. }
  598. void Sweep::FlipEdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle* t, Point& p)
  599. {
  600. Triangle& ot = t->NeighborAcross(p);
  601. Point& op = *ot.OppositePoint(*t, p);
  602. if (InScanArea(p, *t->PointCCW(p), *t->PointCW(p), op)) {
  603. // Lets rotate shared edge one vertex CW
  604. RotateTrianglePair(*t, p, ot, op);
  605. tcx.MapTriangleToNodes(*t);
  606. tcx.MapTriangleToNodes(ot);
  607. if (p == eq && op == ep) {
  608. if (eq == *tcx.edge_event.constrained_edge->q && ep == *tcx.edge_event.constrained_edge->p) {
  609. t->MarkConstrainedEdge(&ep, &eq);
  610. ot.MarkConstrainedEdge(&ep, &eq);
  611. Legalize(tcx, *t);
  612. Legalize(tcx, ot);
  613. } else {
  614. // XXX: I think one of the triangles should be legalized here?
  615. }
  616. } else {
  617. Orientation o = Orient2d(eq, op, ep);
  618. t = &NextFlipTriangle(tcx, (int)o, *t, ot, p, op);
  619. FlipEdgeEvent(tcx, ep, eq, t, p);
  620. }
  621. } else {
  622. Point& newP = NextFlipPoint(ep, eq, ot, op);
  623. FlipScanEdgeEvent(tcx, ep, eq, *t, ot, newP);
  624. EdgeEvent(tcx, ep, eq, t, p);
  625. }
  626. }
  627. Triangle& Sweep::NextFlipTriangle(SweepContext& tcx, int o, Triangle& t, Triangle& ot, Point& p, Point& op)
  628. {
  629. if (o == CCW) {
  630. // ot is not crossing edge after flip
  631. int edge_index = ot.EdgeIndex(&p, &op);
  632. ot.delaunay_edge[edge_index] = true;
  633. Legalize(tcx, ot);
  634. ot.ClearDelunayEdges();
  635. return t;
  636. }
  637. // t is not crossing edge after flip
  638. int edge_index = t.EdgeIndex(&p, &op);
  639. t.delaunay_edge[edge_index] = true;
  640. Legalize(tcx, t);
  641. t.ClearDelunayEdges();
  642. return ot;
  643. }
  644. Point& Sweep::NextFlipPoint(Point& ep, Point& eq, Triangle& ot, Point& op)
  645. {
  646. Orientation o2d = Orient2d(eq, op, ep);
  647. if (o2d == CW) {
  648. // Right
  649. return *ot.PointCCW(op);
  650. } else if (o2d == CCW) {
  651. // Left
  652. return *ot.PointCW(op);
  653. }
  654. throw std::runtime_error("[Unsupported] Opposing point on constrained edge");
  655. }
  656. void Sweep::FlipScanEdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle& flip_triangle,
  657. Triangle& t, Point& p)
  658. {
  659. Triangle& ot = t.NeighborAcross(p);
  660. Point& op = *ot.OppositePoint(t, p);
  661. if (InScanArea(eq, *flip_triangle.PointCCW(eq), *flip_triangle.PointCW(eq), op)) {
  662. // flip with new edge op->eq
  663. FlipEdgeEvent(tcx, eq, op, &ot, op);
  664. // TODO: Actually I just figured out that it should be possible to
  665. // improve this by getting the next ot and op before the the above
  666. // flip and continue the flipScanEdgeEvent here
  667. // set new ot and op here and loop back to inScanArea test
  668. // also need to set a new flip_triangle first
  669. // Turns out at first glance that this is somewhat complicated
  670. // so it will have to wait.
  671. } else{
  672. Point& newP = NextFlipPoint(ep, eq, ot, op);
  673. FlipScanEdgeEvent(tcx, ep, eq, flip_triangle, ot, newP);
  674. }
  675. }
  676. Sweep::~Sweep() {
  677. // Clean up memory
  678. for(size_t i = 0; i < nodes_.size(); i++) {
  679. delete nodes_[i];
  680. }
  681. }
  682. }