polypartition.cpp 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849
  1. /*************************************************************************/
  2. /* Copyright (c) 2011-2021 Ivan Fratric and contributors. */
  3. /* */
  4. /* Permission is hereby granted, free of charge, to any person obtaining */
  5. /* a copy of this software and associated documentation files (the */
  6. /* "Software"), to deal in the Software without restriction, including */
  7. /* without limitation the rights to use, copy, modify, merge, publish, */
  8. /* distribute, sublicense, and/or sell copies of the Software, and to */
  9. /* permit persons to whom the Software is furnished to do so, subject to */
  10. /* the following conditions: */
  11. /* */
  12. /* The above copyright notice and this permission notice shall be */
  13. /* included in all copies or substantial portions of the Software. */
  14. /* */
  15. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  16. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  17. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  18. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  19. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  20. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  21. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  22. /*************************************************************************/
  23. #include "polypartition.h"
  24. #include <algorithm>
  25. TPPLPoly::TPPLPoly() {
  26. hole = false;
  27. numpoints = 0;
  28. points = NULL;
  29. }
  30. TPPLPoly::~TPPLPoly() {
  31. if (points) {
  32. delete[] points;
  33. }
  34. }
  35. void TPPLPoly::Clear() {
  36. if (points) {
  37. delete[] points;
  38. }
  39. hole = false;
  40. numpoints = 0;
  41. points = NULL;
  42. }
  43. void TPPLPoly::Init(long numpoints) {
  44. Clear();
  45. this->numpoints = numpoints;
  46. points = new TPPLPoint[numpoints];
  47. }
  48. void TPPLPoly::Triangle(TPPLPoint &p1, TPPLPoint &p2, TPPLPoint &p3) {
  49. Init(3);
  50. points[0] = p1;
  51. points[1] = p2;
  52. points[2] = p3;
  53. }
  54. TPPLPoly::TPPLPoly(const TPPLPoly &src) :
  55. TPPLPoly() {
  56. hole = src.hole;
  57. numpoints = src.numpoints;
  58. if (numpoints > 0) {
  59. points = new TPPLPoint[numpoints];
  60. memcpy(points, src.points, numpoints * sizeof(TPPLPoint));
  61. }
  62. }
  63. TPPLPoly &TPPLPoly::operator=(const TPPLPoly &src) {
  64. Clear();
  65. hole = src.hole;
  66. numpoints = src.numpoints;
  67. if (numpoints > 0) {
  68. points = new TPPLPoint[numpoints];
  69. memcpy(points, src.points, numpoints * sizeof(TPPLPoint));
  70. }
  71. return *this;
  72. }
  73. TPPLOrientation TPPLPoly::GetOrientation() const {
  74. long i1, i2;
  75. tppl_float area = 0;
  76. for (i1 = 0; i1 < numpoints; i1++) {
  77. i2 = i1 + 1;
  78. if (i2 == numpoints) {
  79. i2 = 0;
  80. }
  81. area += points[i1].x * points[i2].y - points[i1].y * points[i2].x;
  82. }
  83. if (area > 0) {
  84. return TPPL_ORIENTATION_CCW;
  85. }
  86. if (area < 0) {
  87. return TPPL_ORIENTATION_CW;
  88. }
  89. return TPPL_ORIENTATION_NONE;
  90. }
  91. void TPPLPoly::SetOrientation(TPPLOrientation orientation) {
  92. TPPLOrientation polyorientation = GetOrientation();
  93. if (polyorientation != TPPL_ORIENTATION_NONE && polyorientation != orientation) {
  94. Invert();
  95. }
  96. }
  97. void TPPLPoly::Invert() {
  98. std::reverse(points, points + numpoints);
  99. }
  100. TPPLPartition::PartitionVertex::PartitionVertex() :
  101. previous(NULL), next(NULL) {
  102. }
  103. TPPLPoint TPPLPartition::Normalize(const TPPLPoint &p) {
  104. TPPLPoint r;
  105. tppl_float n = sqrt(p.x * p.x + p.y * p.y);
  106. if (n != 0) {
  107. r = p / n;
  108. } else {
  109. r.x = 0;
  110. r.y = 0;
  111. }
  112. return r;
  113. }
  114. tppl_float TPPLPartition::Distance(const TPPLPoint &p1, const TPPLPoint &p2) {
  115. tppl_float dx, dy;
  116. dx = p2.x - p1.x;
  117. dy = p2.y - p1.y;
  118. return (sqrt(dx * dx + dy * dy));
  119. }
  120. // Checks if two lines intersect.
  121. int TPPLPartition::Intersects(TPPLPoint &p11, TPPLPoint &p12, TPPLPoint &p21, TPPLPoint &p22) {
  122. if ((p11.x == p21.x) && (p11.y == p21.y)) {
  123. return 0;
  124. }
  125. if ((p11.x == p22.x) && (p11.y == p22.y)) {
  126. return 0;
  127. }
  128. if ((p12.x == p21.x) && (p12.y == p21.y)) {
  129. return 0;
  130. }
  131. if ((p12.x == p22.x) && (p12.y == p22.y)) {
  132. return 0;
  133. }
  134. TPPLPoint v1ort, v2ort, v;
  135. tppl_float dot11, dot12, dot21, dot22;
  136. v1ort.x = p12.y - p11.y;
  137. v1ort.y = p11.x - p12.x;
  138. v2ort.x = p22.y - p21.y;
  139. v2ort.y = p21.x - p22.x;
  140. v = p21 - p11;
  141. dot21 = v.x * v1ort.x + v.y * v1ort.y;
  142. v = p22 - p11;
  143. dot22 = v.x * v1ort.x + v.y * v1ort.y;
  144. v = p11 - p21;
  145. dot11 = v.x * v2ort.x + v.y * v2ort.y;
  146. v = p12 - p21;
  147. dot12 = v.x * v2ort.x + v.y * v2ort.y;
  148. if (dot11 * dot12 > 0) {
  149. return 0;
  150. }
  151. if (dot21 * dot22 > 0) {
  152. return 0;
  153. }
  154. return 1;
  155. }
  156. // Removes holes from inpolys by merging them with non-holes.
  157. int TPPLPartition::RemoveHoles(TPPLPolyList *inpolys, TPPLPolyList *outpolys) {
  158. TPPLPolyList polys;
  159. TPPLPolyList::Element *holeiter, *polyiter, *iter, *iter2;
  160. long i, i2, holepointindex, polypointindex;
  161. TPPLPoint holepoint, polypoint, bestpolypoint;
  162. TPPLPoint linep1, linep2;
  163. TPPLPoint v1, v2;
  164. TPPLPoly newpoly;
  165. bool hasholes;
  166. bool pointvisible;
  167. bool pointfound;
  168. // Check for the trivial case of no holes.
  169. hasholes = false;
  170. for (iter = inpolys->front(); iter; iter = iter->next()) {
  171. if (iter->get().IsHole()) {
  172. hasholes = true;
  173. break;
  174. }
  175. }
  176. if (!hasholes) {
  177. for (iter = inpolys->front(); iter; iter = iter->next()) {
  178. outpolys->push_back(iter->get());
  179. }
  180. return 1;
  181. }
  182. polys = *inpolys;
  183. while (1) {
  184. // Find the hole point with the largest x.
  185. hasholes = false;
  186. for (iter = polys.front(); iter; iter = iter->next()) {
  187. if (!iter->get().IsHole()) {
  188. continue;
  189. }
  190. if (!hasholes) {
  191. hasholes = true;
  192. holeiter = iter;
  193. holepointindex = 0;
  194. }
  195. for (i = 0; i < iter->get().GetNumPoints(); i++) {
  196. if (iter->get().GetPoint(i).x > holeiter->get().GetPoint(holepointindex).x) {
  197. holeiter = iter;
  198. holepointindex = i;
  199. }
  200. }
  201. }
  202. if (!hasholes) {
  203. break;
  204. }
  205. holepoint = holeiter->get().GetPoint(holepointindex);
  206. pointfound = false;
  207. for (iter = polys.front(); iter; iter = iter->next()) {
  208. if (iter->get().IsHole()) {
  209. continue;
  210. }
  211. for (i = 0; i < iter->get().GetNumPoints(); i++) {
  212. if (iter->get().GetPoint(i).x <= holepoint.x) {
  213. continue;
  214. }
  215. if (!InCone(iter->get().GetPoint((i + iter->get().GetNumPoints() - 1) % (iter->get().GetNumPoints())),
  216. iter->get().GetPoint(i),
  217. iter->get().GetPoint((i + 1) % (iter->get().GetNumPoints())),
  218. holepoint)) {
  219. continue;
  220. }
  221. polypoint = iter->get().GetPoint(i);
  222. if (pointfound) {
  223. v1 = Normalize(polypoint - holepoint);
  224. v2 = Normalize(bestpolypoint - holepoint);
  225. if (v2.x > v1.x) {
  226. continue;
  227. }
  228. }
  229. pointvisible = true;
  230. for (iter2 = polys.front(); iter2; iter2->next()) {
  231. if (iter2->get().IsHole()) {
  232. continue;
  233. }
  234. for (i2 = 0; i2 < iter2->get().GetNumPoints(); i2++) {
  235. linep1 = iter2->get().GetPoint(i2);
  236. linep2 = iter2->get().GetPoint((i2 + 1) % (iter2->get().GetNumPoints()));
  237. if (Intersects(holepoint, polypoint, linep1, linep2)) {
  238. pointvisible = false;
  239. break;
  240. }
  241. }
  242. if (!pointvisible) {
  243. break;
  244. }
  245. }
  246. if (pointvisible) {
  247. pointfound = true;
  248. bestpolypoint = polypoint;
  249. polyiter = iter;
  250. polypointindex = i;
  251. }
  252. }
  253. }
  254. if (!pointfound) {
  255. return 0;
  256. }
  257. newpoly.Init(holeiter->get().GetNumPoints() + polyiter->get().GetNumPoints() + 2);
  258. i2 = 0;
  259. for (i = 0; i <= polypointindex; i++) {
  260. newpoly[i2] = polyiter->get().GetPoint(i);
  261. i2++;
  262. }
  263. for (i = 0; i <= holeiter->get().GetNumPoints(); i++) {
  264. newpoly[i2] = holeiter->get().GetPoint((i + holepointindex) % holeiter->get().GetNumPoints());
  265. i2++;
  266. }
  267. for (i = polypointindex; i < polyiter->get().GetNumPoints(); i++) {
  268. newpoly[i2] = polyiter->get().GetPoint(i);
  269. i2++;
  270. }
  271. polys.erase(holeiter);
  272. polys.erase(polyiter);
  273. polys.push_back(newpoly);
  274. }
  275. for (iter = polys.front(); iter; iter = iter->next()) {
  276. outpolys->push_back(iter->get());
  277. }
  278. return 1;
  279. }
  280. bool TPPLPartition::IsConvex(TPPLPoint &p1, TPPLPoint &p2, TPPLPoint &p3) {
  281. tppl_float tmp;
  282. tmp = (p3.y - p1.y) * (p2.x - p1.x) - (p3.x - p1.x) * (p2.y - p1.y);
  283. if (tmp > 0) {
  284. return 1;
  285. } else {
  286. return 0;
  287. }
  288. }
  289. bool TPPLPartition::IsReflex(TPPLPoint &p1, TPPLPoint &p2, TPPLPoint &p3) {
  290. tppl_float tmp;
  291. tmp = (p3.y - p1.y) * (p2.x - p1.x) - (p3.x - p1.x) * (p2.y - p1.y);
  292. if (tmp < 0) {
  293. return 1;
  294. } else {
  295. return 0;
  296. }
  297. }
  298. bool TPPLPartition::IsInside(TPPLPoint &p1, TPPLPoint &p2, TPPLPoint &p3, TPPLPoint &p) {
  299. if (IsConvex(p1, p, p2)) {
  300. return false;
  301. }
  302. if (IsConvex(p2, p, p3)) {
  303. return false;
  304. }
  305. if (IsConvex(p3, p, p1)) {
  306. return false;
  307. }
  308. return true;
  309. }
  310. bool TPPLPartition::InCone(TPPLPoint &p1, TPPLPoint &p2, TPPLPoint &p3, TPPLPoint &p) {
  311. bool convex;
  312. convex = IsConvex(p1, p2, p3);
  313. if (convex) {
  314. if (!IsConvex(p1, p2, p)) {
  315. return false;
  316. }
  317. if (!IsConvex(p2, p3, p)) {
  318. return false;
  319. }
  320. return true;
  321. } else {
  322. if (IsConvex(p1, p2, p)) {
  323. return true;
  324. }
  325. if (IsConvex(p2, p3, p)) {
  326. return true;
  327. }
  328. return false;
  329. }
  330. }
  331. bool TPPLPartition::InCone(PartitionVertex *v, TPPLPoint &p) {
  332. TPPLPoint p1, p2, p3;
  333. p1 = v->previous->p;
  334. p2 = v->p;
  335. p3 = v->next->p;
  336. return InCone(p1, p2, p3, p);
  337. }
  338. void TPPLPartition::UpdateVertexReflexity(PartitionVertex *v) {
  339. PartitionVertex *v1 = NULL, *v3 = NULL;
  340. v1 = v->previous;
  341. v3 = v->next;
  342. v->isConvex = !IsReflex(v1->p, v->p, v3->p);
  343. }
  344. void TPPLPartition::UpdateVertex(PartitionVertex *v, PartitionVertex *vertices, long numvertices) {
  345. long i;
  346. PartitionVertex *v1 = NULL, *v3 = NULL;
  347. TPPLPoint vec1, vec3;
  348. v1 = v->previous;
  349. v3 = v->next;
  350. v->isConvex = IsConvex(v1->p, v->p, v3->p);
  351. vec1 = Normalize(v1->p - v->p);
  352. vec3 = Normalize(v3->p - v->p);
  353. v->angle = vec1.x * vec3.x + vec1.y * vec3.y;
  354. if (v->isConvex) {
  355. v->isEar = true;
  356. for (i = 0; i < numvertices; i++) {
  357. if ((vertices[i].p.x == v->p.x) && (vertices[i].p.y == v->p.y)) {
  358. continue;
  359. }
  360. if ((vertices[i].p.x == v1->p.x) && (vertices[i].p.y == v1->p.y)) {
  361. continue;
  362. }
  363. if ((vertices[i].p.x == v3->p.x) && (vertices[i].p.y == v3->p.y)) {
  364. continue;
  365. }
  366. if (IsInside(v1->p, v->p, v3->p, vertices[i].p)) {
  367. v->isEar = false;
  368. break;
  369. }
  370. }
  371. } else {
  372. v->isEar = false;
  373. }
  374. }
  375. // Triangulation by ear removal.
  376. int TPPLPartition::Triangulate_EC(TPPLPoly *poly, TPPLPolyList *triangles) {
  377. if (!poly->Valid()) {
  378. return 0;
  379. }
  380. long numvertices;
  381. PartitionVertex *vertices = NULL;
  382. PartitionVertex *ear = NULL;
  383. TPPLPoly triangle;
  384. long i, j;
  385. bool earfound;
  386. if (poly->GetNumPoints() < 3) {
  387. return 0;
  388. }
  389. if (poly->GetNumPoints() == 3) {
  390. triangles->push_back(*poly);
  391. return 1;
  392. }
  393. numvertices = poly->GetNumPoints();
  394. vertices = new PartitionVertex[numvertices];
  395. for (i = 0; i < numvertices; i++) {
  396. vertices[i].isActive = true;
  397. vertices[i].p = poly->GetPoint(i);
  398. if (i == (numvertices - 1)) {
  399. vertices[i].next = &(vertices[0]);
  400. } else {
  401. vertices[i].next = &(vertices[i + 1]);
  402. }
  403. if (i == 0) {
  404. vertices[i].previous = &(vertices[numvertices - 1]);
  405. } else {
  406. vertices[i].previous = &(vertices[i - 1]);
  407. }
  408. }
  409. for (i = 0; i < numvertices; i++) {
  410. UpdateVertex(&vertices[i], vertices, numvertices);
  411. }
  412. for (i = 0; i < numvertices - 3; i++) {
  413. earfound = false;
  414. // Find the most extruded ear.
  415. for (j = 0; j < numvertices; j++) {
  416. if (!vertices[j].isActive) {
  417. continue;
  418. }
  419. if (!vertices[j].isEar) {
  420. continue;
  421. }
  422. if (!earfound) {
  423. earfound = true;
  424. ear = &(vertices[j]);
  425. } else {
  426. if (vertices[j].angle > ear->angle) {
  427. ear = &(vertices[j]);
  428. }
  429. }
  430. }
  431. if (!earfound) {
  432. delete[] vertices;
  433. return 0;
  434. }
  435. triangle.Triangle(ear->previous->p, ear->p, ear->next->p);
  436. triangles->push_back(triangle);
  437. ear->isActive = false;
  438. ear->previous->next = ear->next;
  439. ear->next->previous = ear->previous;
  440. if (i == numvertices - 4) {
  441. break;
  442. }
  443. UpdateVertex(ear->previous, vertices, numvertices);
  444. UpdateVertex(ear->next, vertices, numvertices);
  445. }
  446. for (i = 0; i < numvertices; i++) {
  447. if (vertices[i].isActive) {
  448. triangle.Triangle(vertices[i].previous->p, vertices[i].p, vertices[i].next->p);
  449. triangles->push_back(triangle);
  450. break;
  451. }
  452. }
  453. delete[] vertices;
  454. return 1;
  455. }
  456. int TPPLPartition::Triangulate_EC(TPPLPolyList *inpolys, TPPLPolyList *triangles) {
  457. TPPLPolyList outpolys;
  458. TPPLPolyList::Element *iter;
  459. if (!RemoveHoles(inpolys, &outpolys)) {
  460. return 0;
  461. }
  462. for (iter = outpolys.front(); iter; iter = iter->next()) {
  463. if (!Triangulate_EC(&(iter->get()), triangles)) {
  464. return 0;
  465. }
  466. }
  467. return 1;
  468. }
  469. int TPPLPartition::ConvexPartition_HM(TPPLPoly *poly, TPPLPolyList *parts) {
  470. if (!poly->Valid()) {
  471. return 0;
  472. }
  473. TPPLPolyList triangles;
  474. TPPLPolyList::Element *iter1, *iter2;
  475. TPPLPoly *poly1 = NULL, *poly2 = NULL;
  476. TPPLPoly newpoly;
  477. TPPLPoint d1, d2, p1, p2, p3;
  478. long i11, i12, i21, i22, i13, i23, j, k;
  479. bool isdiagonal;
  480. long numreflex;
  481. // Check if the poly is already convex.
  482. numreflex = 0;
  483. for (i11 = 0; i11 < poly->GetNumPoints(); i11++) {
  484. if (i11 == 0) {
  485. i12 = poly->GetNumPoints() - 1;
  486. } else {
  487. i12 = i11 - 1;
  488. }
  489. if (i11 == (poly->GetNumPoints() - 1)) {
  490. i13 = 0;
  491. } else {
  492. i13 = i11 + 1;
  493. }
  494. if (IsReflex(poly->GetPoint(i12), poly->GetPoint(i11), poly->GetPoint(i13))) {
  495. numreflex = 1;
  496. break;
  497. }
  498. }
  499. if (numreflex == 0) {
  500. parts->push_back(*poly);
  501. return 1;
  502. }
  503. if (!Triangulate_EC(poly, &triangles)) {
  504. return 0;
  505. }
  506. for (iter1 = triangles.front(); iter1; iter1 = iter1->next()) {
  507. poly1 = &(iter1->get());
  508. for (i11 = 0; i11 < poly1->GetNumPoints(); i11++) {
  509. d1 = poly1->GetPoint(i11);
  510. i12 = (i11 + 1) % (poly1->GetNumPoints());
  511. d2 = poly1->GetPoint(i12);
  512. isdiagonal = false;
  513. for (iter2 = iter1; iter2; iter2 = iter2->next()) {
  514. if (iter1 == iter2) {
  515. continue;
  516. }
  517. poly2 = &(iter2->get());
  518. for (i21 = 0; i21 < poly2->GetNumPoints(); i21++) {
  519. if ((d2.x != poly2->GetPoint(i21).x) || (d2.y != poly2->GetPoint(i21).y)) {
  520. continue;
  521. }
  522. i22 = (i21 + 1) % (poly2->GetNumPoints());
  523. if ((d1.x != poly2->GetPoint(i22).x) || (d1.y != poly2->GetPoint(i22).y)) {
  524. continue;
  525. }
  526. isdiagonal = true;
  527. break;
  528. }
  529. if (isdiagonal) {
  530. break;
  531. }
  532. }
  533. if (!isdiagonal) {
  534. continue;
  535. }
  536. p2 = poly1->GetPoint(i11);
  537. if (i11 == 0) {
  538. i13 = poly1->GetNumPoints() - 1;
  539. } else {
  540. i13 = i11 - 1;
  541. }
  542. p1 = poly1->GetPoint(i13);
  543. if (i22 == (poly2->GetNumPoints() - 1)) {
  544. i23 = 0;
  545. } else {
  546. i23 = i22 + 1;
  547. }
  548. p3 = poly2->GetPoint(i23);
  549. if (!IsConvex(p1, p2, p3)) {
  550. continue;
  551. }
  552. p2 = poly1->GetPoint(i12);
  553. if (i12 == (poly1->GetNumPoints() - 1)) {
  554. i13 = 0;
  555. } else {
  556. i13 = i12 + 1;
  557. }
  558. p3 = poly1->GetPoint(i13);
  559. if (i21 == 0) {
  560. i23 = poly2->GetNumPoints() - 1;
  561. } else {
  562. i23 = i21 - 1;
  563. }
  564. p1 = poly2->GetPoint(i23);
  565. if (!IsConvex(p1, p2, p3)) {
  566. continue;
  567. }
  568. newpoly.Init(poly1->GetNumPoints() + poly2->GetNumPoints() - 2);
  569. k = 0;
  570. for (j = i12; j != i11; j = (j + 1) % (poly1->GetNumPoints())) {
  571. newpoly[k] = poly1->GetPoint(j);
  572. k++;
  573. }
  574. for (j = i22; j != i21; j = (j + 1) % (poly2->GetNumPoints())) {
  575. newpoly[k] = poly2->GetPoint(j);
  576. k++;
  577. }
  578. triangles.erase(iter2);
  579. iter1->get() = newpoly;
  580. poly1 = &(iter1->get());
  581. i11 = -1;
  582. continue;
  583. }
  584. }
  585. for (iter1 = triangles.front(); iter1; iter1 = iter1->next()) {
  586. parts->push_back(iter1->get());
  587. }
  588. return 1;
  589. }
  590. int TPPLPartition::ConvexPartition_HM(TPPLPolyList *inpolys, TPPLPolyList *parts) {
  591. TPPLPolyList outpolys;
  592. TPPLPolyList::Element *iter;
  593. if (!RemoveHoles(inpolys, &outpolys)) {
  594. return 0;
  595. }
  596. for (iter = outpolys.front(); iter; iter = iter->next()) {
  597. if (!ConvexPartition_HM(&(iter->get()), parts)) {
  598. return 0;
  599. }
  600. }
  601. return 1;
  602. }
  603. // Minimum-weight polygon triangulation by dynamic programming.
  604. // Time complexity: O(n^3)
  605. // Space complexity: O(n^2)
  606. int TPPLPartition::Triangulate_OPT(TPPLPoly *poly, TPPLPolyList *triangles) {
  607. if (!poly->Valid()) {
  608. return 0;
  609. }
  610. long i, j, k, gap, n;
  611. DPState **dpstates = NULL;
  612. TPPLPoint p1, p2, p3, p4;
  613. long bestvertex;
  614. tppl_float weight, minweight, d1, d2;
  615. Diagonal diagonal, newdiagonal;
  616. DiagonalList diagonals;
  617. TPPLPoly triangle;
  618. int ret = 1;
  619. n = poly->GetNumPoints();
  620. dpstates = new DPState *[n];
  621. for (i = 1; i < n; i++) {
  622. dpstates[i] = new DPState[i];
  623. }
  624. // Initialize states and visibility.
  625. for (i = 0; i < (n - 1); i++) {
  626. p1 = poly->GetPoint(i);
  627. for (j = i + 1; j < n; j++) {
  628. dpstates[j][i].visible = true;
  629. dpstates[j][i].weight = 0;
  630. dpstates[j][i].bestvertex = -1;
  631. if (j != (i + 1)) {
  632. p2 = poly->GetPoint(j);
  633. // Visibility check.
  634. if (i == 0) {
  635. p3 = poly->GetPoint(n - 1);
  636. } else {
  637. p3 = poly->GetPoint(i - 1);
  638. }
  639. if (i == (n - 1)) {
  640. p4 = poly->GetPoint(0);
  641. } else {
  642. p4 = poly->GetPoint(i + 1);
  643. }
  644. if (!InCone(p3, p1, p4, p2)) {
  645. dpstates[j][i].visible = false;
  646. continue;
  647. }
  648. if (j == 0) {
  649. p3 = poly->GetPoint(n - 1);
  650. } else {
  651. p3 = poly->GetPoint(j - 1);
  652. }
  653. if (j == (n - 1)) {
  654. p4 = poly->GetPoint(0);
  655. } else {
  656. p4 = poly->GetPoint(j + 1);
  657. }
  658. if (!InCone(p3, p2, p4, p1)) {
  659. dpstates[j][i].visible = false;
  660. continue;
  661. }
  662. for (k = 0; k < n; k++) {
  663. p3 = poly->GetPoint(k);
  664. if (k == (n - 1)) {
  665. p4 = poly->GetPoint(0);
  666. } else {
  667. p4 = poly->GetPoint(k + 1);
  668. }
  669. if (Intersects(p1, p2, p3, p4)) {
  670. dpstates[j][i].visible = false;
  671. break;
  672. }
  673. }
  674. }
  675. }
  676. }
  677. dpstates[n - 1][0].visible = true;
  678. dpstates[n - 1][0].weight = 0;
  679. dpstates[n - 1][0].bestvertex = -1;
  680. for (gap = 2; gap < n; gap++) {
  681. for (i = 0; i < (n - gap); i++) {
  682. j = i + gap;
  683. if (!dpstates[j][i].visible) {
  684. continue;
  685. }
  686. bestvertex = -1;
  687. for (k = (i + 1); k < j; k++) {
  688. if (!dpstates[k][i].visible) {
  689. continue;
  690. }
  691. if (!dpstates[j][k].visible) {
  692. continue;
  693. }
  694. if (k <= (i + 1)) {
  695. d1 = 0;
  696. } else {
  697. d1 = Distance(poly->GetPoint(i), poly->GetPoint(k));
  698. }
  699. if (j <= (k + 1)) {
  700. d2 = 0;
  701. } else {
  702. d2 = Distance(poly->GetPoint(k), poly->GetPoint(j));
  703. }
  704. weight = dpstates[k][i].weight + dpstates[j][k].weight + d1 + d2;
  705. if ((bestvertex == -1) || (weight < minweight)) {
  706. bestvertex = k;
  707. minweight = weight;
  708. }
  709. }
  710. if (bestvertex == -1) {
  711. for (i = 1; i < n; i++) {
  712. delete[] dpstates[i];
  713. }
  714. delete[] dpstates;
  715. return 0;
  716. }
  717. dpstates[j][i].bestvertex = bestvertex;
  718. dpstates[j][i].weight = minweight;
  719. }
  720. }
  721. newdiagonal.index1 = 0;
  722. newdiagonal.index2 = n - 1;
  723. diagonals.push_back(newdiagonal);
  724. while (!diagonals.is_empty()) {
  725. diagonal = diagonals.front()->get();
  726. diagonals.pop_front();
  727. bestvertex = dpstates[diagonal.index2][diagonal.index1].bestvertex;
  728. if (bestvertex == -1) {
  729. ret = 0;
  730. break;
  731. }
  732. triangle.Triangle(poly->GetPoint(diagonal.index1), poly->GetPoint(bestvertex), poly->GetPoint(diagonal.index2));
  733. triangles->push_back(triangle);
  734. if (bestvertex > (diagonal.index1 + 1)) {
  735. newdiagonal.index1 = diagonal.index1;
  736. newdiagonal.index2 = bestvertex;
  737. diagonals.push_back(newdiagonal);
  738. }
  739. if (diagonal.index2 > (bestvertex + 1)) {
  740. newdiagonal.index1 = bestvertex;
  741. newdiagonal.index2 = diagonal.index2;
  742. diagonals.push_back(newdiagonal);
  743. }
  744. }
  745. for (i = 1; i < n; i++) {
  746. delete[] dpstates[i];
  747. }
  748. delete[] dpstates;
  749. return ret;
  750. }
  751. void TPPLPartition::UpdateState(long a, long b, long w, long i, long j, DPState2 **dpstates) {
  752. Diagonal newdiagonal;
  753. DiagonalList *pairs = NULL;
  754. long w2;
  755. w2 = dpstates[a][b].weight;
  756. if (w > w2) {
  757. return;
  758. }
  759. pairs = &(dpstates[a][b].pairs);
  760. newdiagonal.index1 = i;
  761. newdiagonal.index2 = j;
  762. if (w < w2) {
  763. pairs->clear();
  764. pairs->push_front(newdiagonal);
  765. dpstates[a][b].weight = w;
  766. } else {
  767. if ((!pairs->is_empty()) && (i <= pairs->front()->get().index1)) {
  768. return;
  769. }
  770. while ((!pairs->is_empty()) && (pairs->front()->get().index2 >= j)) {
  771. pairs->pop_front();
  772. }
  773. pairs->push_front(newdiagonal);
  774. }
  775. }
  776. void TPPLPartition::TypeA(long i, long j, long k, PartitionVertex *vertices, DPState2 **dpstates) {
  777. DiagonalList *pairs = NULL;
  778. DiagonalList::Element *iter, *lastiter;
  779. long top;
  780. long w;
  781. if (!dpstates[i][j].visible) {
  782. return;
  783. }
  784. top = j;
  785. w = dpstates[i][j].weight;
  786. if (k - j > 1) {
  787. if (!dpstates[j][k].visible) {
  788. return;
  789. }
  790. w += dpstates[j][k].weight + 1;
  791. }
  792. if (j - i > 1) {
  793. pairs = &(dpstates[i][j].pairs);
  794. iter = pairs->back();
  795. lastiter = pairs->back();
  796. while (iter != pairs->front()) {
  797. iter--;
  798. if (!IsReflex(vertices[iter->get().index2].p, vertices[j].p, vertices[k].p)) {
  799. lastiter = iter;
  800. } else {
  801. break;
  802. }
  803. }
  804. if (lastiter == pairs->back()) {
  805. w++;
  806. } else {
  807. if (IsReflex(vertices[k].p, vertices[i].p, vertices[lastiter->get().index1].p)) {
  808. w++;
  809. } else {
  810. top = lastiter->get().index1;
  811. }
  812. }
  813. }
  814. UpdateState(i, k, w, top, j, dpstates);
  815. }
  816. void TPPLPartition::TypeB(long i, long j, long k, PartitionVertex *vertices, DPState2 **dpstates) {
  817. DiagonalList *pairs = NULL;
  818. DiagonalList::Element *iter, *lastiter;
  819. long top;
  820. long w;
  821. if (!dpstates[j][k].visible) {
  822. return;
  823. }
  824. top = j;
  825. w = dpstates[j][k].weight;
  826. if (j - i > 1) {
  827. if (!dpstates[i][j].visible) {
  828. return;
  829. }
  830. w += dpstates[i][j].weight + 1;
  831. }
  832. if (k - j > 1) {
  833. pairs = &(dpstates[j][k].pairs);
  834. iter = pairs->front();
  835. if ((!pairs->is_empty()) && (!IsReflex(vertices[i].p, vertices[j].p, vertices[iter->get().index1].p))) {
  836. lastiter = iter;
  837. while (iter) {
  838. if (!IsReflex(vertices[i].p, vertices[j].p, vertices[iter->get().index1].p)) {
  839. lastiter = iter;
  840. iter = iter->next();
  841. } else {
  842. break;
  843. }
  844. }
  845. if (IsReflex(vertices[lastiter->get().index2].p, vertices[k].p, vertices[i].p)) {
  846. w++;
  847. } else {
  848. top = lastiter->get().index2;
  849. }
  850. } else {
  851. w++;
  852. }
  853. }
  854. UpdateState(i, k, w, j, top, dpstates);
  855. }
  856. int TPPLPartition::ConvexPartition_OPT(TPPLPoly *poly, TPPLPolyList *parts) {
  857. if (!poly->Valid()) {
  858. return 0;
  859. }
  860. TPPLPoint p1, p2, p3, p4;
  861. PartitionVertex *vertices = NULL;
  862. DPState2 **dpstates = NULL;
  863. long i, j, k, n, gap;
  864. DiagonalList diagonals, diagonals2;
  865. Diagonal diagonal, newdiagonal;
  866. DiagonalList *pairs = NULL, *pairs2 = NULL;
  867. DiagonalList::Element *iter, *iter2;
  868. int ret;
  869. TPPLPoly newpoly;
  870. List<long> indices;
  871. List<long>::Element *iiter;
  872. bool ijreal, jkreal;
  873. n = poly->GetNumPoints();
  874. vertices = new PartitionVertex[n];
  875. dpstates = new DPState2 *[n];
  876. for (i = 0; i < n; i++) {
  877. dpstates[i] = new DPState2[n];
  878. }
  879. // Initialize vertex information.
  880. for (i = 0; i < n; i++) {
  881. vertices[i].p = poly->GetPoint(i);
  882. vertices[i].isActive = true;
  883. if (i == 0) {
  884. vertices[i].previous = &(vertices[n - 1]);
  885. } else {
  886. vertices[i].previous = &(vertices[i - 1]);
  887. }
  888. if (i == (poly->GetNumPoints() - 1)) {
  889. vertices[i].next = &(vertices[0]);
  890. } else {
  891. vertices[i].next = &(vertices[i + 1]);
  892. }
  893. }
  894. for (i = 1; i < n; i++) {
  895. UpdateVertexReflexity(&(vertices[i]));
  896. }
  897. // Initialize states and visibility.
  898. for (i = 0; i < (n - 1); i++) {
  899. p1 = poly->GetPoint(i);
  900. for (j = i + 1; j < n; j++) {
  901. dpstates[i][j].visible = true;
  902. if (j == i + 1) {
  903. dpstates[i][j].weight = 0;
  904. } else {
  905. dpstates[i][j].weight = 2147483647;
  906. }
  907. if (j != (i + 1)) {
  908. p2 = poly->GetPoint(j);
  909. // Visibility check.
  910. if (!InCone(&vertices[i], p2)) {
  911. dpstates[i][j].visible = false;
  912. continue;
  913. }
  914. if (!InCone(&vertices[j], p1)) {
  915. dpstates[i][j].visible = false;
  916. continue;
  917. }
  918. for (k = 0; k < n; k++) {
  919. p3 = poly->GetPoint(k);
  920. if (k == (n - 1)) {
  921. p4 = poly->GetPoint(0);
  922. } else {
  923. p4 = poly->GetPoint(k + 1);
  924. }
  925. if (Intersects(p1, p2, p3, p4)) {
  926. dpstates[i][j].visible = false;
  927. break;
  928. }
  929. }
  930. }
  931. }
  932. }
  933. for (i = 0; i < (n - 2); i++) {
  934. j = i + 2;
  935. if (dpstates[i][j].visible) {
  936. dpstates[i][j].weight = 0;
  937. newdiagonal.index1 = i + 1;
  938. newdiagonal.index2 = i + 1;
  939. dpstates[i][j].pairs.push_back(newdiagonal);
  940. }
  941. }
  942. dpstates[0][n - 1].visible = true;
  943. vertices[0].isConvex = false; // By convention.
  944. for (gap = 3; gap < n; gap++) {
  945. for (i = 0; i < n - gap; i++) {
  946. if (vertices[i].isConvex) {
  947. continue;
  948. }
  949. k = i + gap;
  950. if (dpstates[i][k].visible) {
  951. if (!vertices[k].isConvex) {
  952. for (j = i + 1; j < k; j++) {
  953. TypeA(i, j, k, vertices, dpstates);
  954. }
  955. } else {
  956. for (j = i + 1; j < (k - 1); j++) {
  957. if (vertices[j].isConvex) {
  958. continue;
  959. }
  960. TypeA(i, j, k, vertices, dpstates);
  961. }
  962. TypeA(i, k - 1, k, vertices, dpstates);
  963. }
  964. }
  965. }
  966. for (k = gap; k < n; k++) {
  967. if (vertices[k].isConvex) {
  968. continue;
  969. }
  970. i = k - gap;
  971. if ((vertices[i].isConvex) && (dpstates[i][k].visible)) {
  972. TypeB(i, i + 1, k, vertices, dpstates);
  973. for (j = i + 2; j < k; j++) {
  974. if (vertices[j].isConvex) {
  975. continue;
  976. }
  977. TypeB(i, j, k, vertices, dpstates);
  978. }
  979. }
  980. }
  981. }
  982. // Recover solution.
  983. ret = 1;
  984. newdiagonal.index1 = 0;
  985. newdiagonal.index2 = n - 1;
  986. diagonals.push_front(newdiagonal);
  987. while (!diagonals.is_empty()) {
  988. diagonal = diagonals.front()->get();
  989. diagonals.pop_front();
  990. if ((diagonal.index2 - diagonal.index1) <= 1) {
  991. continue;
  992. }
  993. pairs = &(dpstates[diagonal.index1][diagonal.index2].pairs);
  994. if (pairs->is_empty()) {
  995. ret = 0;
  996. break;
  997. }
  998. if (!vertices[diagonal.index1].isConvex) {
  999. iter = pairs->back();
  1000. iter--;
  1001. j = iter->get().index2;
  1002. newdiagonal.index1 = j;
  1003. newdiagonal.index2 = diagonal.index2;
  1004. diagonals.push_front(newdiagonal);
  1005. if ((j - diagonal.index1) > 1) {
  1006. if (iter->get().index1 != iter->get().index2) {
  1007. pairs2 = &(dpstates[diagonal.index1][j].pairs);
  1008. while (1) {
  1009. if (pairs2->is_empty()) {
  1010. ret = 0;
  1011. break;
  1012. }
  1013. iter2 = pairs2->back();
  1014. iter2--;
  1015. if (iter->get().index1 != iter2->get().index1) {
  1016. pairs2->pop_back();
  1017. } else {
  1018. break;
  1019. }
  1020. }
  1021. if (ret == 0) {
  1022. break;
  1023. }
  1024. }
  1025. newdiagonal.index1 = diagonal.index1;
  1026. newdiagonal.index2 = j;
  1027. diagonals.push_front(newdiagonal);
  1028. }
  1029. } else {
  1030. iter = pairs->front();
  1031. j = iter->get().index1;
  1032. newdiagonal.index1 = diagonal.index1;
  1033. newdiagonal.index2 = j;
  1034. diagonals.push_front(newdiagonal);
  1035. if ((diagonal.index2 - j) > 1) {
  1036. if (iter->get().index1 != iter->get().index2) {
  1037. pairs2 = &(dpstates[j][diagonal.index2].pairs);
  1038. while (1) {
  1039. if (pairs2->is_empty()) {
  1040. ret = 0;
  1041. break;
  1042. }
  1043. iter2 = pairs2->front();
  1044. if (iter->get().index2 != iter2->get().index2) {
  1045. pairs2->pop_front();
  1046. } else {
  1047. break;
  1048. }
  1049. }
  1050. if (ret == 0) {
  1051. break;
  1052. }
  1053. }
  1054. newdiagonal.index1 = j;
  1055. newdiagonal.index2 = diagonal.index2;
  1056. diagonals.push_front(newdiagonal);
  1057. }
  1058. }
  1059. }
  1060. if (ret == 0) {
  1061. for (i = 0; i < n; i++) {
  1062. delete[] dpstates[i];
  1063. }
  1064. delete[] dpstates;
  1065. delete[] vertices;
  1066. return ret;
  1067. }
  1068. newdiagonal.index1 = 0;
  1069. newdiagonal.index2 = n - 1;
  1070. diagonals.push_front(newdiagonal);
  1071. while (!diagonals.is_empty()) {
  1072. diagonal = diagonals.front()->get();
  1073. diagonals.pop_front();
  1074. if ((diagonal.index2 - diagonal.index1) <= 1) {
  1075. continue;
  1076. }
  1077. indices.clear();
  1078. diagonals2.clear();
  1079. indices.push_back(diagonal.index1);
  1080. indices.push_back(diagonal.index2);
  1081. diagonals2.push_front(diagonal);
  1082. while (!diagonals2.is_empty()) {
  1083. diagonal = diagonals2.front()->get();
  1084. diagonals2.pop_front();
  1085. if ((diagonal.index2 - diagonal.index1) <= 1) {
  1086. continue;
  1087. }
  1088. ijreal = true;
  1089. jkreal = true;
  1090. pairs = &(dpstates[diagonal.index1][diagonal.index2].pairs);
  1091. if (!vertices[diagonal.index1].isConvex) {
  1092. iter = pairs->back();
  1093. iter--;
  1094. j = iter->get().index2;
  1095. if (iter->get().index1 != iter->get().index2) {
  1096. ijreal = false;
  1097. }
  1098. } else {
  1099. iter = pairs->front();
  1100. j = iter->get().index1;
  1101. if (iter->get().index1 != iter->get().index2) {
  1102. jkreal = false;
  1103. }
  1104. }
  1105. newdiagonal.index1 = diagonal.index1;
  1106. newdiagonal.index2 = j;
  1107. if (ijreal) {
  1108. diagonals.push_back(newdiagonal);
  1109. } else {
  1110. diagonals2.push_back(newdiagonal);
  1111. }
  1112. newdiagonal.index1 = j;
  1113. newdiagonal.index2 = diagonal.index2;
  1114. if (jkreal) {
  1115. diagonals.push_back(newdiagonal);
  1116. } else {
  1117. diagonals2.push_back(newdiagonal);
  1118. }
  1119. indices.push_back(j);
  1120. }
  1121. //std::sort(indices.begin(), indices.end());
  1122. indices.sort();
  1123. newpoly.Init((long)indices.size());
  1124. k = 0;
  1125. for (iiter = indices.front(); iiter != indices.back(); iiter = iiter->next()) {
  1126. newpoly[k] = vertices[iiter->get()].p;
  1127. k++;
  1128. }
  1129. parts->push_back(newpoly);
  1130. }
  1131. for (i = 0; i < n; i++) {
  1132. delete[] dpstates[i];
  1133. }
  1134. delete[] dpstates;
  1135. delete[] vertices;
  1136. return ret;
  1137. }
  1138. // Creates a monotone partition of a list of polygons that
  1139. // can contain holes. Triangulates a set of polygons by
  1140. // first partitioning them into monotone polygons.
  1141. // Time complexity: O(n*log(n)), n is the number of vertices.
  1142. // Space complexity: O(n)
  1143. // The algorithm used here is outlined in the book
  1144. // "Computational Geometry: Algorithms and Applications"
  1145. // by Mark de Berg, Otfried Cheong, Marc van Kreveld, and Mark Overmars.
  1146. int TPPLPartition::MonotonePartition(TPPLPolyList *inpolys, TPPLPolyList *monotonePolys) {
  1147. TPPLPolyList::Element *iter;
  1148. MonotoneVertex *vertices = NULL;
  1149. long i, numvertices, vindex, vindex2, newnumvertices, maxnumvertices;
  1150. long polystartindex, polyendindex;
  1151. TPPLPoly *poly = NULL;
  1152. MonotoneVertex *v = NULL, *v2 = NULL, *vprev = NULL, *vnext = NULL;
  1153. ScanLineEdge newedge;
  1154. bool error = false;
  1155. numvertices = 0;
  1156. for (iter = inpolys->front(); iter; iter++) {
  1157. numvertices += iter->get().GetNumPoints();
  1158. }
  1159. maxnumvertices = numvertices * 3;
  1160. vertices = new MonotoneVertex[maxnumvertices];
  1161. newnumvertices = numvertices;
  1162. polystartindex = 0;
  1163. for (iter = inpolys->front(); iter; iter++) {
  1164. poly = &(iter->get());
  1165. polyendindex = polystartindex + poly->GetNumPoints() - 1;
  1166. for (i = 0; i < poly->GetNumPoints(); i++) {
  1167. vertices[i + polystartindex].p = poly->GetPoint(i);
  1168. if (i == 0) {
  1169. vertices[i + polystartindex].previous = polyendindex;
  1170. } else {
  1171. vertices[i + polystartindex].previous = i + polystartindex - 1;
  1172. }
  1173. if (i == (poly->GetNumPoints() - 1)) {
  1174. vertices[i + polystartindex].next = polystartindex;
  1175. } else {
  1176. vertices[i + polystartindex].next = i + polystartindex + 1;
  1177. }
  1178. }
  1179. polystartindex = polyendindex + 1;
  1180. }
  1181. // Construct the priority queue.
  1182. long *priority = new long[numvertices];
  1183. for (i = 0; i < numvertices; i++) {
  1184. priority[i] = i;
  1185. }
  1186. std::sort(priority, &(priority[numvertices]), VertexSorter(vertices));
  1187. // Determine vertex types.
  1188. TPPLVertexType *vertextypes = new TPPLVertexType[maxnumvertices];
  1189. for (i = 0; i < numvertices; i++) {
  1190. v = &(vertices[i]);
  1191. vprev = &(vertices[v->previous]);
  1192. vnext = &(vertices[v->next]);
  1193. if (Below(vprev->p, v->p) && Below(vnext->p, v->p)) {
  1194. if (IsConvex(vnext->p, vprev->p, v->p)) {
  1195. vertextypes[i] = TPPL_VERTEXTYPE_START;
  1196. } else {
  1197. vertextypes[i] = TPPL_VERTEXTYPE_SPLIT;
  1198. }
  1199. } else if (Below(v->p, vprev->p) && Below(v->p, vnext->p)) {
  1200. if (IsConvex(vnext->p, vprev->p, v->p)) {
  1201. vertextypes[i] = TPPL_VERTEXTYPE_END;
  1202. } else {
  1203. vertextypes[i] = TPPL_VERTEXTYPE_MERGE;
  1204. }
  1205. } else {
  1206. vertextypes[i] = TPPL_VERTEXTYPE_REGULAR;
  1207. }
  1208. }
  1209. // Helpers.
  1210. long *helpers = new long[maxnumvertices];
  1211. // Binary search tree that holds edges intersecting the scanline.
  1212. // Note that while set doesn't actually have to be implemented as
  1213. // a tree, complexity requirements for operations are the same as
  1214. // for the balanced binary search tree.
  1215. Set<ScanLineEdge> edgeTree;
  1216. // Store iterators to the edge tree elements.
  1217. // This makes deleting existing edges much faster.
  1218. Set<ScanLineEdge>::Element **edgeTreeIterators, *edgeIter;
  1219. edgeTreeIterators = new Set<ScanLineEdge>::Element *[maxnumvertices];
  1220. //Pair<Set<ScanLineEdge>::iterator, bool> edgeTreeRet;
  1221. for (i = 0; i < numvertices; i++) {
  1222. edgeTreeIterators[i] = nullptr;
  1223. }
  1224. // For each vertex.
  1225. for (i = 0; i < numvertices; i++) {
  1226. vindex = priority[i];
  1227. v = &(vertices[vindex]);
  1228. vindex2 = vindex;
  1229. v2 = v;
  1230. // Depending on the vertex type, do the appropriate action.
  1231. // Comments in the following sections are copied from
  1232. // "Computational Geometry: Algorithms and Applications".
  1233. // Notation: e_i = e subscript i, v_i = v subscript i, etc.
  1234. switch (vertextypes[vindex]) {
  1235. case TPPL_VERTEXTYPE_START:
  1236. // Insert e_i in T and set helper(e_i) to v_i.
  1237. newedge.p1 = v->p;
  1238. newedge.p2 = vertices[v->next].p;
  1239. newedge.index = vindex;
  1240. //edgeTreeRet = edgeTree.insert(newedge);
  1241. //edgeTreeIterators[vindex] = edgeTreeRet.first;
  1242. edgeTreeIterators[vindex] = edgeTree.insert(newedge);
  1243. helpers[vindex] = vindex;
  1244. break;
  1245. case TPPL_VERTEXTYPE_END:
  1246. if (edgeTreeIterators[v->previous] == edgeTree.back()) {
  1247. error = true;
  1248. break;
  1249. }
  1250. // If helper(e_i - 1) is a merge vertex
  1251. if (vertextypes[helpers[v->previous]] == TPPL_VERTEXTYPE_MERGE) {
  1252. // Insert the diagonal connecting vi to helper(e_i - 1) in D.
  1253. AddDiagonal(vertices, &newnumvertices, vindex, helpers[v->previous],
  1254. vertextypes, edgeTreeIterators, &edgeTree, helpers);
  1255. }
  1256. // Delete e_i - 1 from T
  1257. edgeTree.erase(edgeTreeIterators[v->previous]);
  1258. break;
  1259. case TPPL_VERTEXTYPE_SPLIT:
  1260. // Search in T to find the edge e_j directly left of v_i.
  1261. newedge.p1 = v->p;
  1262. newedge.p2 = v->p;
  1263. edgeIter = edgeTree.lower_bound(newedge);
  1264. if (edgeIter == edgeTree.front()) {
  1265. error = true;
  1266. break;
  1267. }
  1268. edgeIter--;
  1269. // Insert the diagonal connecting vi to helper(e_j) in D.
  1270. AddDiagonal(vertices, &newnumvertices, vindex, helpers[edgeIter->get().index],
  1271. vertextypes, edgeTreeIterators, &edgeTree, helpers);
  1272. vindex2 = newnumvertices - 2;
  1273. v2 = &(vertices[vindex2]);
  1274. // helper(e_j) in v_i.
  1275. helpers[edgeIter->get().index] = vindex;
  1276. // Insert e_i in T and set helper(e_i) to v_i.
  1277. newedge.p1 = v2->p;
  1278. newedge.p2 = vertices[v2->next].p;
  1279. newedge.index = vindex2;
  1280. //edgeTreeRet = edgeTree.insert(newedge);
  1281. //edgeTreeIterators[vindex2] = edgeTreeRet.first;
  1282. edgeTreeIterators[vindex2] = edgeTree.insert(newedge);
  1283. helpers[vindex2] = vindex2;
  1284. break;
  1285. case TPPL_VERTEXTYPE_MERGE:
  1286. if (edgeTreeIterators[v->previous] == edgeTree.back()) {
  1287. error = true;
  1288. break;
  1289. }
  1290. // if helper(e_i - 1) is a merge vertex
  1291. if (vertextypes[helpers[v->previous]] == TPPL_VERTEXTYPE_MERGE) {
  1292. // Insert the diagonal connecting vi to helper(e_i - 1) in D.
  1293. AddDiagonal(vertices, &newnumvertices, vindex, helpers[v->previous],
  1294. vertextypes, edgeTreeIterators, &edgeTree, helpers);
  1295. vindex2 = newnumvertices - 2;
  1296. v2 = &(vertices[vindex2]);
  1297. }
  1298. // Delete e_i - 1 from T.
  1299. edgeTree.erase(edgeTreeIterators[v->previous]);
  1300. // Search in T to find the edge e_j directly left of v_i.
  1301. newedge.p1 = v->p;
  1302. newedge.p2 = v->p;
  1303. edgeIter = edgeTree.lower_bound(newedge);
  1304. if (edgeIter == edgeTree.front()) {
  1305. error = true;
  1306. break;
  1307. }
  1308. edgeIter--;
  1309. // If helper(e_j) is a merge vertex.
  1310. if (vertextypes[helpers[edgeIter->get().index]] == TPPL_VERTEXTYPE_MERGE) {
  1311. // Insert the diagonal connecting v_i to helper(e_j) in D.
  1312. AddDiagonal(vertices, &newnumvertices, vindex2, helpers[edgeIter->get().index],
  1313. vertextypes, edgeTreeIterators, &edgeTree, helpers);
  1314. }
  1315. // helper(e_j) <- v_i
  1316. helpers[edgeIter->get().index] = vindex2;
  1317. break;
  1318. case TPPL_VERTEXTYPE_REGULAR:
  1319. // If the interior of P lies to the right of v_i.
  1320. if (Below(v->p, vertices[v->previous].p)) {
  1321. if (edgeTreeIterators[v->previous] == edgeTree.back()) {
  1322. error = true;
  1323. break;
  1324. }
  1325. // If helper(e_i - 1) is a merge vertex.
  1326. if (vertextypes[helpers[v->previous]] == TPPL_VERTEXTYPE_MERGE) {
  1327. // Insert the diagonal connecting v_i to helper(e_i - 1) in D.
  1328. AddDiagonal(vertices, &newnumvertices, vindex, helpers[v->previous],
  1329. vertextypes, edgeTreeIterators, &edgeTree, helpers);
  1330. vindex2 = newnumvertices - 2;
  1331. v2 = &(vertices[vindex2]);
  1332. }
  1333. // Delete e_i - 1 from T.
  1334. edgeTree.erase(edgeTreeIterators[v->previous]);
  1335. // Insert e_i in T and set helper(e_i) to v_i.
  1336. newedge.p1 = v2->p;
  1337. newedge.p2 = vertices[v2->next].p;
  1338. newedge.index = vindex2;
  1339. //edgeTreeRet = edgeTree.insert(newedge);
  1340. //edgeTreeIterators[vindex2] = edgeTreeRet.first;
  1341. edgeTreeIterators[vindex2] = edgeTree.insert(newedge);
  1342. helpers[vindex2] = vindex;
  1343. } else {
  1344. // Search in T to find the edge e_j directly left of v_i.
  1345. newedge.p1 = v->p;
  1346. newedge.p2 = v->p;
  1347. edgeIter = edgeTree.lower_bound(newedge);
  1348. if (edgeIter == edgeTree.front()) {
  1349. error = true;
  1350. break;
  1351. }
  1352. edgeIter = edgeIter->prev();
  1353. // If helper(e_j) is a merge vertex.
  1354. if (vertextypes[helpers[edgeIter->get().index]] == TPPL_VERTEXTYPE_MERGE) {
  1355. // Insert the diagonal connecting v_i to helper(e_j) in D.
  1356. AddDiagonal(vertices, &newnumvertices, vindex, helpers[edgeIter->get().index],
  1357. vertextypes, edgeTreeIterators, &edgeTree, helpers);
  1358. }
  1359. // helper(e_j) <- v_i.
  1360. helpers[edgeIter->get().index] = vindex;
  1361. }
  1362. break;
  1363. }
  1364. if (error)
  1365. break;
  1366. }
  1367. char *used = new char[newnumvertices];
  1368. memset(used, 0, newnumvertices * sizeof(char));
  1369. if (!error) {
  1370. // Return result.
  1371. long size;
  1372. TPPLPoly mpoly;
  1373. for (i = 0; i < newnumvertices; i++) {
  1374. if (used[i]) {
  1375. continue;
  1376. }
  1377. v = &(vertices[i]);
  1378. vnext = &(vertices[v->next]);
  1379. size = 1;
  1380. while (vnext != v) {
  1381. vnext = &(vertices[vnext->next]);
  1382. size++;
  1383. }
  1384. mpoly.Init(size);
  1385. v = &(vertices[i]);
  1386. mpoly[0] = v->p;
  1387. vnext = &(vertices[v->next]);
  1388. size = 1;
  1389. used[i] = 1;
  1390. used[v->next] = 1;
  1391. while (vnext != v) {
  1392. mpoly[size] = vnext->p;
  1393. used[vnext->next] = 1;
  1394. vnext = &(vertices[vnext->next]);
  1395. size++;
  1396. }
  1397. monotonePolys->push_back(mpoly);
  1398. }
  1399. }
  1400. // Cleanup.
  1401. delete[] vertices;
  1402. delete[] priority;
  1403. delete[] vertextypes;
  1404. delete[] edgeTreeIterators;
  1405. delete[] helpers;
  1406. delete[] used;
  1407. if (error) {
  1408. return 0;
  1409. } else {
  1410. return 1;
  1411. }
  1412. }
  1413. // Adds a diagonal to the doubly-connected list of vertices.
  1414. void TPPLPartition::AddDiagonal(MonotoneVertex *vertices, long *numvertices, long index1, long index2,
  1415. TPPLVertexType *vertextypes, Set<ScanLineEdge>::Element **edgeTreeIterators,
  1416. Set<ScanLineEdge> *edgeTree, long *helpers) {
  1417. long newindex1, newindex2;
  1418. newindex1 = *numvertices;
  1419. (*numvertices)++;
  1420. newindex2 = *numvertices;
  1421. (*numvertices)++;
  1422. vertices[newindex1].p = vertices[index1].p;
  1423. vertices[newindex2].p = vertices[index2].p;
  1424. vertices[newindex2].next = vertices[index2].next;
  1425. vertices[newindex1].next = vertices[index1].next;
  1426. vertices[vertices[index2].next].previous = newindex2;
  1427. vertices[vertices[index1].next].previous = newindex1;
  1428. vertices[index1].next = newindex2;
  1429. vertices[newindex2].previous = index1;
  1430. vertices[index2].next = newindex1;
  1431. vertices[newindex1].previous = index2;
  1432. // Update all relevant structures.
  1433. vertextypes[newindex1] = vertextypes[index1];
  1434. edgeTreeIterators[newindex1] = edgeTreeIterators[index1];
  1435. helpers[newindex1] = helpers[index1];
  1436. if (edgeTreeIterators[newindex1] != edgeTree->back()) {
  1437. edgeTreeIterators[newindex1]->get().index = newindex1;
  1438. }
  1439. vertextypes[newindex2] = vertextypes[index2];
  1440. edgeTreeIterators[newindex2] = edgeTreeIterators[index2];
  1441. helpers[newindex2] = helpers[index2];
  1442. if (edgeTreeIterators[newindex2] != edgeTree->back()) {
  1443. edgeTreeIterators[newindex2]->get().index = newindex2;
  1444. }
  1445. }
  1446. bool TPPLPartition::Below(TPPLPoint &p1, TPPLPoint &p2) {
  1447. if (p1.y < p2.y) {
  1448. return true;
  1449. } else if (p1.y == p2.y) {
  1450. if (p1.x < p2.x) {
  1451. return true;
  1452. }
  1453. }
  1454. return false;
  1455. }
  1456. // Sorts in the falling order of y values, if y is equal, x is used instead.
  1457. bool TPPLPartition::VertexSorter::operator()(long index1, long index2) {
  1458. if (vertices[index1].p.y > vertices[index2].p.y) {
  1459. return true;
  1460. } else if (vertices[index1].p.y == vertices[index2].p.y) {
  1461. if (vertices[index1].p.x > vertices[index2].p.x) {
  1462. return true;
  1463. }
  1464. }
  1465. return false;
  1466. }
  1467. bool TPPLPartition::ScanLineEdge::IsConvex(const TPPLPoint &p1, const TPPLPoint &p2, const TPPLPoint &p3) const {
  1468. tppl_float tmp;
  1469. tmp = (p3.y - p1.y) * (p2.x - p1.x) - (p3.x - p1.x) * (p2.y - p1.y);
  1470. if (tmp > 0) {
  1471. return 1;
  1472. }
  1473. return 0;
  1474. }
  1475. bool TPPLPartition::ScanLineEdge::operator<(const ScanLineEdge &other) const {
  1476. if (other.p1.y == other.p2.y) {
  1477. if (p1.y == p2.y) {
  1478. return (p1.y < other.p1.y);
  1479. }
  1480. return IsConvex(p1, p2, other.p1);
  1481. } else if (p1.y == p2.y) {
  1482. return !IsConvex(other.p1, other.p2, p1);
  1483. } else if (p1.y < other.p1.y) {
  1484. return !IsConvex(other.p1, other.p2, p1);
  1485. } else {
  1486. return IsConvex(p1, p2, other.p1);
  1487. }
  1488. }
  1489. // Triangulates monotone polygon.
  1490. // Time complexity: O(n)
  1491. // Space complexity: O(n)
  1492. int TPPLPartition::TriangulateMonotone(TPPLPoly *inPoly, TPPLPolyList *triangles) {
  1493. if (!inPoly->Valid()) {
  1494. return 0;
  1495. }
  1496. long i, i2, j, topindex, bottomindex, leftindex, rightindex, vindex;
  1497. TPPLPoint *points = NULL;
  1498. long numpoints;
  1499. TPPLPoly triangle;
  1500. numpoints = inPoly->GetNumPoints();
  1501. points = inPoly->GetPoints();
  1502. // Trivial case.
  1503. if (numpoints == 3) {
  1504. triangles->push_back(*inPoly);
  1505. return 1;
  1506. }
  1507. topindex = 0;
  1508. bottomindex = 0;
  1509. for (i = 1; i < numpoints; i++) {
  1510. if (Below(points[i], points[bottomindex])) {
  1511. bottomindex = i;
  1512. }
  1513. if (Below(points[topindex], points[i])) {
  1514. topindex = i;
  1515. }
  1516. }
  1517. // Check if the poly is really monotone.
  1518. i = topindex;
  1519. while (i != bottomindex) {
  1520. i2 = i + 1;
  1521. if (i2 >= numpoints) {
  1522. i2 = 0;
  1523. }
  1524. if (!Below(points[i2], points[i])) {
  1525. return 0;
  1526. }
  1527. i = i2;
  1528. }
  1529. i = bottomindex;
  1530. while (i != topindex) {
  1531. i2 = i + 1;
  1532. if (i2 >= numpoints) {
  1533. i2 = 0;
  1534. }
  1535. if (!Below(points[i], points[i2])) {
  1536. return 0;
  1537. }
  1538. i = i2;
  1539. }
  1540. char *vertextypes = new char[numpoints];
  1541. long *priority = new long[numpoints];
  1542. // Merge left and right vertex chains.
  1543. priority[0] = topindex;
  1544. vertextypes[topindex] = 0;
  1545. leftindex = topindex + 1;
  1546. if (leftindex >= numpoints) {
  1547. leftindex = 0;
  1548. }
  1549. rightindex = topindex - 1;
  1550. if (rightindex < 0) {
  1551. rightindex = numpoints - 1;
  1552. }
  1553. for (i = 1; i < (numpoints - 1); i++) {
  1554. if (leftindex == bottomindex) {
  1555. priority[i] = rightindex;
  1556. rightindex--;
  1557. if (rightindex < 0) {
  1558. rightindex = numpoints - 1;
  1559. }
  1560. vertextypes[priority[i]] = -1;
  1561. } else if (rightindex == bottomindex) {
  1562. priority[i] = leftindex;
  1563. leftindex++;
  1564. if (leftindex >= numpoints) {
  1565. leftindex = 0;
  1566. }
  1567. vertextypes[priority[i]] = 1;
  1568. } else {
  1569. if (Below(points[leftindex], points[rightindex])) {
  1570. priority[i] = rightindex;
  1571. rightindex--;
  1572. if (rightindex < 0) {
  1573. rightindex = numpoints - 1;
  1574. }
  1575. vertextypes[priority[i]] = -1;
  1576. } else {
  1577. priority[i] = leftindex;
  1578. leftindex++;
  1579. if (leftindex >= numpoints) {
  1580. leftindex = 0;
  1581. }
  1582. vertextypes[priority[i]] = 1;
  1583. }
  1584. }
  1585. }
  1586. priority[i] = bottomindex;
  1587. vertextypes[bottomindex] = 0;
  1588. long *stack = new long[numpoints];
  1589. long stackptr = 0;
  1590. stack[0] = priority[0];
  1591. stack[1] = priority[1];
  1592. stackptr = 2;
  1593. // For each vertex from top to bottom trim as many triangles as possible.
  1594. for (i = 2; i < (numpoints - 1); i++) {
  1595. vindex = priority[i];
  1596. if (vertextypes[vindex] != vertextypes[stack[stackptr - 1]]) {
  1597. for (j = 0; j < (stackptr - 1); j++) {
  1598. if (vertextypes[vindex] == 1) {
  1599. triangle.Triangle(points[stack[j + 1]], points[stack[j]], points[vindex]);
  1600. } else {
  1601. triangle.Triangle(points[stack[j]], points[stack[j + 1]], points[vindex]);
  1602. }
  1603. triangles->push_back(triangle);
  1604. }
  1605. stack[0] = priority[i - 1];
  1606. stack[1] = priority[i];
  1607. stackptr = 2;
  1608. } else {
  1609. stackptr--;
  1610. while (stackptr > 0) {
  1611. if (vertextypes[vindex] == 1) {
  1612. if (IsConvex(points[vindex], points[stack[stackptr - 1]], points[stack[stackptr]])) {
  1613. triangle.Triangle(points[vindex], points[stack[stackptr - 1]], points[stack[stackptr]]);
  1614. triangles->push_back(triangle);
  1615. stackptr--;
  1616. } else {
  1617. break;
  1618. }
  1619. } else {
  1620. if (IsConvex(points[vindex], points[stack[stackptr]], points[stack[stackptr - 1]])) {
  1621. triangle.Triangle(points[vindex], points[stack[stackptr]], points[stack[stackptr - 1]]);
  1622. triangles->push_back(triangle);
  1623. stackptr--;
  1624. } else {
  1625. break;
  1626. }
  1627. }
  1628. }
  1629. stackptr++;
  1630. stack[stackptr] = vindex;
  1631. stackptr++;
  1632. }
  1633. }
  1634. vindex = priority[i];
  1635. for (j = 0; j < (stackptr - 1); j++) {
  1636. if (vertextypes[stack[j + 1]] == 1) {
  1637. triangle.Triangle(points[stack[j]], points[stack[j + 1]], points[vindex]);
  1638. } else {
  1639. triangle.Triangle(points[stack[j + 1]], points[stack[j]], points[vindex]);
  1640. }
  1641. triangles->push_back(triangle);
  1642. }
  1643. delete[] priority;
  1644. delete[] vertextypes;
  1645. delete[] stack;
  1646. return 1;
  1647. }
  1648. int TPPLPartition::Triangulate_MONO(TPPLPolyList *inpolys, TPPLPolyList *triangles) {
  1649. TPPLPolyList monotone;
  1650. TPPLPolyList::Element *iter;
  1651. if (!MonotonePartition(inpolys, &monotone)) {
  1652. return 0;
  1653. }
  1654. for (iter = monotone.front(); iter; iter = iter->next()) {
  1655. if (!TriangulateMonotone(&(iter->get()), triangles)) {
  1656. return 0;
  1657. }
  1658. }
  1659. return 1;
  1660. }
  1661. int TPPLPartition::Triangulate_MONO(TPPLPoly *poly, TPPLPolyList *triangles) {
  1662. TPPLPolyList polys;
  1663. polys.push_back(*poly);
  1664. return Triangulate_MONO(&polys, triangles);
  1665. }