sweep.cc 23 KB

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