Clipper.RectClip.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. 
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace Clipper2Lib
  6. {
  7. public class RectClip
  8. {
  9. protected enum Location
  10. {
  11. left, top, right, bottom, inside
  12. };
  13. readonly protected Rect64 rect_;
  14. readonly protected Point64 mp_;
  15. readonly protected Path64 rectPath_;
  16. protected Path64 result_;
  17. protected Location firstCross_;
  18. protected List<Location> startLocs_ = new List<Location>();
  19. internal RectClip(Rect64 rect)
  20. {
  21. rect_ = rect;
  22. mp_ = rect.MidPoint();
  23. rectPath_ = rect_.AsPath();
  24. result_ = new Path64();
  25. firstCross_ = Location.inside;
  26. }
  27. private static PointInPolygonResult Path1ContainsPath2(Path64 path1, Path64 path2)
  28. {
  29. PointInPolygonResult result = PointInPolygonResult.IsOn;
  30. foreach (Point64 pt in path2)
  31. {
  32. result = Clipper.PointInPolygon(pt, path1);
  33. if (result != PointInPolygonResult.IsOn)
  34. {
  35. break;
  36. }
  37. }
  38. return result;
  39. }
  40. private static bool IsClockwise(Location prev, Location curr,
  41. Point64 prevPt, Point64 currPt, Point64 rectMidPoint)
  42. {
  43. if (AreOpposites(prev, curr))
  44. {
  45. return InternalClipper.CrossProduct(prevPt, rectMidPoint, currPt) < 0;
  46. }
  47. else
  48. {
  49. return HeadingClockwise(prev, curr);
  50. }
  51. }
  52. private static bool AreOpposites(Location prev, Location curr)
  53. {
  54. return Math.Abs((int)prev - (int)curr) == 2;
  55. }
  56. private static bool HeadingClockwise(Location prev, Location curr)
  57. {
  58. return ((int)prev + 1) % 4 == (int)curr;
  59. }
  60. private static Location GetAdjacentLocation(Location loc, bool isClockwise)
  61. {
  62. int delta = (isClockwise) ? 1 : 3;
  63. return (Location)(((int)loc + delta) % 4);
  64. }
  65. private void AddCorner(Location prev, Location curr)
  66. {
  67. if (HeadingClockwise(prev, curr))
  68. {
  69. result_.Add(rectPath_[(int)prev]);
  70. }
  71. else
  72. {
  73. result_.Add(rectPath_[(int)curr]);
  74. }
  75. }
  76. private void AddCorner(ref Location loc, bool isClockwise)
  77. {
  78. if (isClockwise)
  79. {
  80. result_.Add(rectPath_[(int)loc]);
  81. loc = GetAdjacentLocation(loc, true);
  82. }
  83. else
  84. {
  85. loc = GetAdjacentLocation(loc, false);
  86. result_.Add(rectPath_[(int)loc]);
  87. }
  88. }
  89. static protected bool GetLocation(Rect64 rec, Point64 pt, out Location loc)
  90. {
  91. if (pt.X == rec.left && pt.Y >= rec.top && pt.Y <= rec.bottom)
  92. {
  93. loc = Location.left; return false; // pt on rec
  94. }
  95. if (pt.X == rec.right && pt.Y >= rec.top && pt.Y <= rec.bottom)
  96. {
  97. loc = Location.right; return false; // pt on rec
  98. }
  99. if (pt.Y == rec.top && pt.X >= rec.left && pt.X <= rec.right)
  100. {
  101. loc = Location.top; return false; // pt on rec
  102. }
  103. if (pt.Y == rec.bottom && pt.X >= rec.left && pt.X <= rec.right)
  104. {
  105. loc = Location.bottom; return false; // pt on rec
  106. }
  107. if (pt.X < rec.left)
  108. {
  109. loc = Location.left;
  110. }
  111. else if (pt.X > rec.right)
  112. {
  113. loc = Location.right;
  114. }
  115. else if (pt.Y < rec.top)
  116. {
  117. loc = Location.top;
  118. }
  119. else if (pt.Y > rec.bottom)
  120. {
  121. loc = Location.bottom;
  122. }
  123. else
  124. {
  125. loc = Location.inside;
  126. }
  127. return true;
  128. }
  129. static protected bool GetIntersection(Path64 rectPath, Point64 p, Point64 p2, ref Location loc, out Point64 ip)
  130. {
  131. // gets the pt of intersection between rectPath and segment(p, p2) that's closest to 'p'
  132. // when result == false, loc will remain unchanged
  133. ip = new Point64();
  134. switch (loc)
  135. {
  136. case Location.left:
  137. if (InternalClipper.SegsIntersect(p, p2, rectPath[0], rectPath[3], true))
  138. {
  139. InternalClipper.GetIntersectPt(p, p2, rectPath[0], rectPath[3], out ip);
  140. }
  141. else if (p.Y < rectPath[0].Y &&
  142. InternalClipper.SegsIntersect(p, p2, rectPath[0], rectPath[1], true))
  143. {
  144. InternalClipper.GetIntersectPt(p, p2, rectPath[0], rectPath[1], out ip);
  145. loc = Location.top;
  146. }
  147. else if (InternalClipper.SegsIntersect(p, p2, rectPath[2], rectPath[3], true))
  148. {
  149. InternalClipper.GetIntersectPt(p, p2, rectPath[2], rectPath[3], out ip);
  150. loc = Location.bottom;
  151. }
  152. else
  153. {
  154. return false;
  155. }
  156. break;
  157. case Location.right:
  158. if (InternalClipper.SegsIntersect(p, p2, rectPath[1], rectPath[2], true))
  159. {
  160. InternalClipper.GetIntersectPt(p, p2, rectPath[1], rectPath[2], out ip);
  161. }
  162. else if (p.Y < rectPath[0].Y &&
  163. InternalClipper.SegsIntersect(p, p2, rectPath[0], rectPath[1], true))
  164. {
  165. InternalClipper.GetIntersectPt(p, p2, rectPath[0], rectPath[1], out ip);
  166. loc = Location.top;
  167. }
  168. else if (InternalClipper.SegsIntersect(p, p2, rectPath[2], rectPath[3], true))
  169. {
  170. InternalClipper.GetIntersectPt(p, p2, rectPath[2], rectPath[3], out ip);
  171. loc = Location.bottom;
  172. }
  173. else
  174. {
  175. return false;
  176. }
  177. break;
  178. case Location.top:
  179. if (InternalClipper.SegsIntersect(p, p2, rectPath[0], rectPath[1], true))
  180. {
  181. InternalClipper.GetIntersectPt(p, p2, rectPath[0], rectPath[1], out ip);
  182. }
  183. else if (p.X < rectPath[0].X &&
  184. InternalClipper.SegsIntersect(p, p2, rectPath[0], rectPath[3], true))
  185. {
  186. InternalClipper.GetIntersectPt(p, p2, rectPath[0], rectPath[3], out ip);
  187. loc = Location.left;
  188. }
  189. else if (p.X > rectPath[1].X &&
  190. InternalClipper.SegsIntersect(p, p2, rectPath[1], rectPath[2], true))
  191. {
  192. InternalClipper.GetIntersectPt(p, p2, rectPath[1], rectPath[2], out ip);
  193. loc = Location.right;
  194. }
  195. else
  196. {
  197. return false;
  198. }
  199. break;
  200. case Location.bottom:
  201. if (InternalClipper.SegsIntersect(p, p2, rectPath[2], rectPath[3], true))
  202. {
  203. InternalClipper.GetIntersectPt(p, p2, rectPath[2], rectPath[3], out ip);
  204. }
  205. else if (p.X < rectPath[3].X &&
  206. InternalClipper.SegsIntersect(p, p2, rectPath[0], rectPath[3], true))
  207. {
  208. InternalClipper.GetIntersectPt(p, p2, rectPath[0], rectPath[3], out ip);
  209. loc = Location.left;
  210. }
  211. else if (p.X > rectPath[2].X &&
  212. InternalClipper.SegsIntersect(p, p2, rectPath[1], rectPath[2], true))
  213. {
  214. InternalClipper.GetIntersectPt(p, p2, rectPath[1], rectPath[2], out ip);
  215. loc = Location.right;
  216. }
  217. else
  218. {
  219. return false;
  220. }
  221. break;
  222. case Location.inside:
  223. if (InternalClipper.SegsIntersect(p, p2, rectPath[0], rectPath[3], true))
  224. {
  225. InternalClipper.GetIntersectPt(p, p2, rectPath[0], rectPath[3], out ip);
  226. loc = Location.left;
  227. }
  228. else if (InternalClipper.SegsIntersect(p, p2, rectPath[0], rectPath[1], true))
  229. {
  230. InternalClipper.GetIntersectPt(p, p2, rectPath[0], rectPath[1], out ip);
  231. loc = Location.top;
  232. }
  233. else if (InternalClipper.SegsIntersect(p, p2, rectPath[1], rectPath[2], true))
  234. {
  235. InternalClipper.GetIntersectPt(p, p2, rectPath[1], rectPath[2], out ip);
  236. loc = Location.right;
  237. }
  238. else if (InternalClipper.SegsIntersect(p, p2, rectPath[2], rectPath[3], true))
  239. {
  240. InternalClipper.GetIntersectPt(p, p2, rectPath[2], rectPath[3], out ip);
  241. loc = Location.bottom;
  242. }
  243. else
  244. {
  245. return false;
  246. }
  247. break;
  248. }
  249. return true;
  250. }
  251. protected void GetNextLocation(Path64 path,
  252. ref Location loc, ref int i, int highI)
  253. {
  254. switch (loc)
  255. {
  256. case Location.left:
  257. {
  258. while (i <= highI && path[i].X <= rect_.left)
  259. {
  260. i++;
  261. }
  262. if (i > highI)
  263. {
  264. break;
  265. }
  266. if (path[i].X >= rect_.right)
  267. {
  268. loc = Location.right;
  269. }
  270. else if (path[i].Y <= rect_.top)
  271. {
  272. loc = Location.top;
  273. }
  274. else if (path[i].Y >= rect_.bottom)
  275. {
  276. loc = Location.bottom;
  277. }
  278. else
  279. {
  280. loc = Location.inside;
  281. }
  282. }
  283. break;
  284. case Location.top:
  285. {
  286. while (i <= highI && path[i].Y <= rect_.top)
  287. {
  288. i++;
  289. }
  290. if (i > highI)
  291. {
  292. break;
  293. }
  294. if (path[i].Y >= rect_.bottom)
  295. {
  296. loc = Location.bottom;
  297. }
  298. else if (path[i].X <= rect_.left)
  299. {
  300. loc = Location.left;
  301. }
  302. else if (path[i].X >= rect_.right)
  303. {
  304. loc = Location.right;
  305. }
  306. else
  307. {
  308. loc = Location.inside;
  309. }
  310. }
  311. break;
  312. case Location.right:
  313. {
  314. while (i <= highI && path[i].X >= rect_.right)
  315. {
  316. i++;
  317. }
  318. if (i > highI)
  319. {
  320. break;
  321. }
  322. if (path[i].X <= rect_.left)
  323. {
  324. loc = Location.left;
  325. }
  326. else if (path[i].Y <= rect_.top)
  327. {
  328. loc = Location.top;
  329. }
  330. else if (path[i].Y >= rect_.bottom)
  331. {
  332. loc = Location.bottom;
  333. }
  334. else
  335. {
  336. loc = Location.inside;
  337. }
  338. }
  339. break;
  340. case Location.bottom:
  341. {
  342. while (i <= highI && path[i].Y >= rect_.bottom)
  343. {
  344. i++;
  345. }
  346. if (i > highI)
  347. {
  348. break;
  349. }
  350. if (path[i].Y <= rect_.top)
  351. {
  352. loc = Location.top;
  353. }
  354. else if (path[i].X <= rect_.left)
  355. {
  356. loc = Location.left;
  357. }
  358. else if (path[i].X >= rect_.right)
  359. {
  360. loc = Location.right;
  361. }
  362. else
  363. {
  364. loc = Location.inside;
  365. }
  366. }
  367. break;
  368. case Location.inside:
  369. {
  370. while (i <= highI)
  371. {
  372. if (path[i].X < rect_.left)
  373. {
  374. loc = Location.left;
  375. }
  376. else if (path[i].X > rect_.right)
  377. {
  378. loc = Location.right;
  379. }
  380. else if (path[i].Y > rect_.bottom)
  381. {
  382. loc = Location.bottom;
  383. }
  384. else if (path[i].Y < rect_.top)
  385. {
  386. loc = Location.top;
  387. }
  388. else
  389. {
  390. result_.Add(path[i]);
  391. i++;
  392. continue;
  393. }
  394. break;
  395. }
  396. }
  397. break;
  398. } // switch
  399. }
  400. internal Path64 ExecuteInternal(Path64 path)
  401. {
  402. if (path.Count < 3 || rect_.IsEmpty())
  403. {
  404. return new Path64();
  405. }
  406. result_.Clear();
  407. startLocs_.Clear();
  408. int i = 0, highI = path.Count - 1;
  409. firstCross_ = Location.inside;
  410. Location crossingLoc = Location.inside, prev;
  411. if (!GetLocation(rect_, path[highI], out Location loc))
  412. {
  413. prev = loc;
  414. i = highI - 1;
  415. while (i >= 0 && !GetLocation(rect_, path[i], out prev))
  416. {
  417. i--;
  418. }
  419. if (i < 0)
  420. {
  421. return path;
  422. }
  423. if (prev == Location.inside)
  424. {
  425. loc = Location.inside;
  426. }
  427. i = 0;
  428. }
  429. Location startingLoc = loc;
  430. ///////////////////////////////////////////////////
  431. while (i <= highI)
  432. {
  433. prev = loc;
  434. Location prevCrossLoc = crossingLoc;
  435. GetNextLocation(path, ref loc, ref i, highI);
  436. if (i > highI)
  437. {
  438. break;
  439. }
  440. Point64 prevPt = (i == 0) ? path[highI] : path[i - 1];
  441. crossingLoc = loc;
  442. if (!GetIntersection(rectPath_, path[i], prevPt, ref crossingLoc, out Point64 ip))
  443. {
  444. // ie remaining outside (& crossingLoc still == loc)
  445. if (prevCrossLoc == Location.inside)
  446. {
  447. bool isClockw = IsClockwise(prev, loc, prevPt, path[i], mp_);
  448. do
  449. {
  450. startLocs_.Add(prev);
  451. prev = GetAdjacentLocation(prev, isClockw);
  452. } while (prev != loc);
  453. crossingLoc = prevCrossLoc; // still not crossed
  454. }
  455. else if (prev != Location.inside && prev != loc)
  456. {
  457. bool isClockw = IsClockwise(prev, loc, prevPt, path[i], mp_);
  458. do
  459. {
  460. AddCorner(ref prev, isClockw);
  461. } while (prev != loc);
  462. }
  463. ++i;
  464. continue;
  465. }
  466. ////////////////////////////////////////////////////
  467. // we must be crossing the rect boundary to get here
  468. ////////////////////////////////////////////////////
  469. if (loc == Location.inside) // path must be entering rect
  470. {
  471. if (firstCross_ == Location.inside)
  472. {
  473. firstCross_ = crossingLoc;
  474. startLocs_.Add(prev);
  475. }
  476. else if (prev != crossingLoc)
  477. {
  478. bool isClockw = IsClockwise(prev, crossingLoc, prevPt, path[i], mp_);
  479. do
  480. {
  481. AddCorner(ref prev, isClockw);
  482. } while (prev != crossingLoc);
  483. }
  484. }
  485. else if (prev != Location.inside)
  486. {
  487. // passing right through rect. 'ip' here will be the second
  488. // intersect pt but we'll also need the first intersect pt (ip2)
  489. loc = prev;
  490. GetIntersection(rectPath_, prevPt, path[i], ref loc, out Point64 ip2);
  491. if (prevCrossLoc != Location.inside)
  492. {
  493. AddCorner(prevCrossLoc, loc);
  494. }
  495. if (firstCross_ == Location.inside)
  496. {
  497. firstCross_ = loc;
  498. startLocs_.Add(prev);
  499. }
  500. loc = crossingLoc;
  501. result_.Add(ip2);
  502. if (ip == ip2)
  503. {
  504. // it's very likely that path[i] is on rect
  505. GetLocation(rect_, path[i], out loc);
  506. AddCorner(crossingLoc, loc);
  507. crossingLoc = loc;
  508. continue;
  509. }
  510. }
  511. else // path must be exiting rect
  512. {
  513. loc = crossingLoc;
  514. if (firstCross_ == Location.inside)
  515. {
  516. firstCross_ = crossingLoc;
  517. }
  518. }
  519. result_.Add(ip);
  520. } //while i <= highI
  521. ///////////////////////////////////////////////////
  522. // path must be entering rect
  523. if (firstCross_ == Location.inside)
  524. {
  525. if (startingLoc == Location.inside)
  526. {
  527. return path;
  528. }
  529. Rect64 tmp_rect = Clipper.GetBounds(path);
  530. if (tmp_rect.Contains(rect_) &&
  531. Path1ContainsPath2(path, rectPath_)
  532. != PointInPolygonResult.IsOutside)
  533. {
  534. return rectPath_;
  535. }
  536. return new Path64();
  537. }
  538. if (loc != Location.inside &&
  539. (loc != firstCross_ || startLocs_.Count > 2))
  540. {
  541. if (startLocs_.Count > 0)
  542. {
  543. prev = loc;
  544. foreach (Location loc2 in startLocs_)
  545. {
  546. if (prev == loc2)
  547. {
  548. continue;
  549. }
  550. AddCorner(ref prev, HeadingClockwise(prev, loc2));
  551. prev = loc2;
  552. }
  553. loc = prev;
  554. }
  555. if (loc != firstCross_)
  556. {
  557. AddCorner(ref loc, HeadingClockwise(loc, firstCross_));
  558. }
  559. }
  560. if (result_.Count < 3)
  561. {
  562. return new Path64();
  563. }
  564. // finally, tidy up result
  565. int k = 0, len = result_.Count;
  566. Point64 lastPt = result_[len - 1];
  567. Path64 result = new Path64(len) { result_[0] };
  568. foreach (Point64 pt in result_.Skip(1))
  569. {
  570. if (InternalClipper.CrossProduct(lastPt, result[k], pt) != 0)
  571. {
  572. lastPt = result[k++];
  573. result.Add(pt);
  574. }
  575. else
  576. {
  577. result[k] = pt;
  578. }
  579. }
  580. if (k < 2)
  581. {
  582. result.Clear();
  583. }
  584. else if (InternalClipper.CrossProduct(result[0], result[k - 1], result[k]) == 0)
  585. {
  586. result.RemoveAt(result.Count - 1);
  587. }
  588. return result;
  589. }
  590. internal Paths64 ExecuteInternal(Paths64 paths)
  591. {
  592. Paths64 result = new Paths64(paths.Count);
  593. foreach (Path64 path in paths)
  594. {
  595. if (rect_.Intersects(Clipper.GetBounds(path)))
  596. {
  597. result.Add(ExecuteInternal(path));
  598. }
  599. }
  600. return result;
  601. }
  602. } // RectClip class
  603. public class RectClipLines : RectClip
  604. {
  605. internal RectClipLines(Rect64 rect) : base(rect) { }
  606. internal new Paths64 ExecuteInternal(Path64 path)
  607. {
  608. result_.Clear();
  609. Paths64 result = new Paths64();
  610. if (path.Count < 2 || rect_.IsEmpty())
  611. {
  612. return result;
  613. }
  614. Location prev = Location.inside;
  615. int i = 1, highI = path.Count - 1;
  616. if (!GetLocation(rect_, path[0], out Location loc))
  617. {
  618. while (i <= highI && !GetLocation(rect_, path[i], out prev))
  619. {
  620. i++;
  621. }
  622. if (i > highI)
  623. {
  624. result.Add(path);
  625. return result;
  626. }
  627. if (prev == Location.inside)
  628. {
  629. loc = Location.inside;
  630. }
  631. i = 1;
  632. }
  633. if (loc == Location.inside)
  634. {
  635. result_.Add(path[0]);
  636. }
  637. ///////////////////////////////////////////////////
  638. while (i <= highI)
  639. {
  640. prev = loc;
  641. GetNextLocation(path, ref loc, ref i, highI);
  642. if (i > highI)
  643. {
  644. break;
  645. }
  646. Point64 prevPt = path[i - 1];
  647. Location crossingLoc = loc;
  648. if (!GetIntersection(rectPath_, path[i], prevPt, ref crossingLoc, out Point64 ip))
  649. {
  650. // ie remaining outside (& crossingLoc still == loc)
  651. ++i;
  652. continue;
  653. }
  654. ////////////////////////////////////////////////////
  655. // we must be crossing the rect boundary to get here
  656. ////////////////////////////////////////////////////
  657. if (loc == Location.inside) // path must be entering rect
  658. {
  659. result_.Add(ip);
  660. }
  661. else if (prev != Location.inside)
  662. {
  663. // passing right through rect. 'ip' here will be the second
  664. // intersect pt but we'll also need the first intersect pt (ip2)
  665. crossingLoc = prev;
  666. GetIntersection(rectPath_, prevPt, path[i], ref crossingLoc, out Point64 ip2);
  667. result_.Add(ip2);
  668. result_.Add(ip);
  669. result.Add(result_);
  670. result_ = new Path64();
  671. }
  672. else // path must be exiting rect
  673. {
  674. result_.Add(ip);
  675. result.Add(result_);
  676. result_ = new Path64();
  677. }
  678. } //while i <= highI
  679. ///////////////////////////////////////////////////
  680. if (result_.Count > 1)
  681. {
  682. result.Add(result_);
  683. result_ = new Path64();
  684. }
  685. return result;
  686. } // RectClipOpen.ExecuteInternal
  687. } // RectClipOpen class
  688. } // namespace