VectorClipper.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. //----------------------------------------------------------------------------
  2. // Anti-Grain Geometry - Version 2.4
  3. // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
  4. //
  5. // C# port by: Lars Brubaker
  6. // larsbrubaker@gmail.com
  7. // Copyright (C) 2007
  8. //
  9. // Permission to copy, use, modify, sell and distribute this software
  10. // is granted provided this copyright notice appears in all copies.
  11. // This software is provided "as is" without express or implied
  12. // warranty, and with no claim as to its suitability for any purpose.
  13. //
  14. //----------------------------------------------------------------------------
  15. // Contact: mcseem@antigrain.com
  16. // mcseemagg@yahoo.com
  17. // http://www.antigrain.com
  18. //----------------------------------------------------------------------------
  19. //#ifndef AGG_RASTERIZER_SL_CLIP_INCLUDED
  20. //#define AGG_RASTERIZER_SL_CLIP_INCLUDED
  21. //#include "agg_clip_liang_barsky.h"
  22. using poly_subpixel_scale_e = MatterHackers.Agg.Util.poly_subpixel_scale_e;
  23. namespace MatterHackers.Agg
  24. {
  25. //--------------------------------------------------------poly_max_coord_e
  26. internal enum poly_max_coord_e
  27. {
  28. poly_max_coord = (1 << 30) - 1 //----poly_max_coord
  29. };
  30. public class VectorClipper
  31. {
  32. public RectangleInt clipBox;
  33. private int m_x1;
  34. private int m_y1;
  35. private int m_f1;
  36. private bool m_clipping;
  37. private int mul_div(double a, double b, double c)
  38. {
  39. return Util.iround(a * b / c);
  40. }
  41. private int xi(int v)
  42. {
  43. return v;
  44. }
  45. private int yi(int v)
  46. {
  47. return v;
  48. }
  49. public int upscale(double v)
  50. {
  51. return Util.iround(v * (int)poly_subpixel_scale_e.poly_subpixel_scale);
  52. }
  53. public int downscale(int v)
  54. {
  55. return v / (int)poly_subpixel_scale_e.poly_subpixel_scale;
  56. }
  57. //--------------------------------------------------------------------
  58. public VectorClipper()
  59. {
  60. clipBox = new RectangleInt(0, 0, 0, 0);
  61. m_x1 = (0);
  62. m_y1 = (0);
  63. m_f1 = (0);
  64. m_clipping = (false);
  65. }
  66. //--------------------------------------------------------------------
  67. public void reset_clipping()
  68. {
  69. m_clipping = false;
  70. }
  71. //--------------------------------------------------------------------
  72. public void clip_box(int x1, int y1, int x2, int y2)
  73. {
  74. clipBox = new RectangleInt(x1, y1, x2, y2);
  75. clipBox.normalize();
  76. m_clipping = true;
  77. }
  78. //--------------------------------------------------------------------
  79. public void move_to(int x1, int y1)
  80. {
  81. m_x1 = x1;
  82. m_y1 = y1;
  83. if (m_clipping)
  84. {
  85. m_f1 = ClipLiangBarsky.clipping_flags(x1, y1, clipBox);
  86. }
  87. }
  88. //------------------------------------------------------------------------
  89. private void line_clip_y(RasterizerCellsAa ras,
  90. int x1, int y1,
  91. int x2, int y2,
  92. int f1, int f2)
  93. {
  94. f1 &= 10;
  95. f2 &= 10;
  96. if ((f1 | f2) == 0)
  97. {
  98. // Fully visible
  99. ras.line(x1, y1, x2, y2);
  100. }
  101. else
  102. {
  103. if (f1 == f2)
  104. {
  105. // Invisible by Y
  106. return;
  107. }
  108. int tx1 = x1;
  109. int ty1 = y1;
  110. int tx2 = x2;
  111. int ty2 = y2;
  112. if ((f1 & 8) != 0) // y1 < clip.y1
  113. {
  114. tx1 = x1 + mul_div(clipBox.Bottom - y1, x2 - x1, y2 - y1);
  115. ty1 = clipBox.Bottom;
  116. }
  117. if ((f1 & 2) != 0) // y1 > clip.y2
  118. {
  119. tx1 = x1 + mul_div(clipBox.Top - y1, x2 - x1, y2 - y1);
  120. ty1 = clipBox.Top;
  121. }
  122. if ((f2 & 8) != 0) // y2 < clip.y1
  123. {
  124. tx2 = x1 + mul_div(clipBox.Bottom - y1, x2 - x1, y2 - y1);
  125. ty2 = clipBox.Bottom;
  126. }
  127. if ((f2 & 2) != 0) // y2 > clip.y2
  128. {
  129. tx2 = x1 + mul_div(clipBox.Top - y1, x2 - x1, y2 - y1);
  130. ty2 = clipBox.Top;
  131. }
  132. ras.line(tx1, ty1, tx2, ty2);
  133. }
  134. }
  135. //--------------------------------------------------------------------
  136. public void line_to(RasterizerCellsAa ras, int x2, int y2)
  137. {
  138. if (m_clipping)
  139. {
  140. int f2 = ClipLiangBarsky.clipping_flags(x2, y2, clipBox);
  141. if ((m_f1 & 10) == (f2 & 10) && (m_f1 & 10) != 0)
  142. {
  143. // Invisible by Y
  144. m_x1 = x2;
  145. m_y1 = y2;
  146. m_f1 = f2;
  147. return;
  148. }
  149. int x1 = m_x1;
  150. int y1 = m_y1;
  151. int f1 = m_f1;
  152. int y3, y4;
  153. int f3, f4;
  154. switch (((f1 & 5) << 1) | (f2 & 5))
  155. {
  156. case 0: // Visible by X
  157. line_clip_y(ras, x1, y1, x2, y2, f1, f2);
  158. break;
  159. case 1: // x2 > clip.x2
  160. y3 = y1 + mul_div(clipBox.Right - x1, y2 - y1, x2 - x1);
  161. f3 = ClipLiangBarsky.clipping_flags_y(y3, clipBox);
  162. line_clip_y(ras, x1, y1, clipBox.Right, y3, f1, f3);
  163. line_clip_y(ras, clipBox.Right, y3, clipBox.Right, y2, f3, f2);
  164. break;
  165. case 2: // x1 > clip.x2
  166. y3 = y1 + mul_div(clipBox.Right - x1, y2 - y1, x2 - x1);
  167. f3 = ClipLiangBarsky.clipping_flags_y(y3, clipBox);
  168. line_clip_y(ras, clipBox.Right, y1, clipBox.Right, y3, f1, f3);
  169. line_clip_y(ras, clipBox.Right, y3, x2, y2, f3, f2);
  170. break;
  171. case 3: // x1 > clip.x2 && x2 > clip.x2
  172. line_clip_y(ras, clipBox.Right, y1, clipBox.Right, y2, f1, f2);
  173. break;
  174. case 4: // x2 < clip.x1
  175. y3 = y1 + mul_div(clipBox.Left - x1, y2 - y1, x2 - x1);
  176. f3 = ClipLiangBarsky.clipping_flags_y(y3, clipBox);
  177. line_clip_y(ras, x1, y1, clipBox.Left, y3, f1, f3);
  178. line_clip_y(ras, clipBox.Left, y3, clipBox.Left, y2, f3, f2);
  179. break;
  180. case 6: // x1 > clip.x2 && x2 < clip.x1
  181. y3 = y1 + mul_div(clipBox.Right - x1, y2 - y1, x2 - x1);
  182. y4 = y1 + mul_div(clipBox.Left - x1, y2 - y1, x2 - x1);
  183. f3 = ClipLiangBarsky.clipping_flags_y(y3, clipBox);
  184. f4 = ClipLiangBarsky.clipping_flags_y(y4, clipBox);
  185. line_clip_y(ras, clipBox.Right, y1, clipBox.Right, y3, f1, f3);
  186. line_clip_y(ras, clipBox.Right, y3, clipBox.Left, y4, f3, f4);
  187. line_clip_y(ras, clipBox.Left, y4, clipBox.Left, y2, f4, f2);
  188. break;
  189. case 8: // x1 < clip.x1
  190. y3 = y1 + mul_div(clipBox.Left - x1, y2 - y1, x2 - x1);
  191. f3 = ClipLiangBarsky.clipping_flags_y(y3, clipBox);
  192. line_clip_y(ras, clipBox.Left, y1, clipBox.Left, y3, f1, f3);
  193. line_clip_y(ras, clipBox.Left, y3, x2, y2, f3, f2);
  194. break;
  195. case 9: // x1 < clip.x1 && x2 > clip.x2
  196. y3 = y1 + mul_div(clipBox.Left - x1, y2 - y1, x2 - x1);
  197. y4 = y1 + mul_div(clipBox.Right - x1, y2 - y1, x2 - x1);
  198. f3 = ClipLiangBarsky.clipping_flags_y(y3, clipBox);
  199. f4 = ClipLiangBarsky.clipping_flags_y(y4, clipBox);
  200. line_clip_y(ras, clipBox.Left, y1, clipBox.Left, y3, f1, f3);
  201. line_clip_y(ras, clipBox.Left, y3, clipBox.Right, y4, f3, f4);
  202. line_clip_y(ras, clipBox.Right, y4, clipBox.Right, y2, f4, f2);
  203. break;
  204. case 12: // x1 < clip.x1 && x2 < clip.x1
  205. line_clip_y(ras, clipBox.Left, y1, clipBox.Left, y2, f1, f2);
  206. break;
  207. }
  208. m_f1 = f2;
  209. }
  210. else
  211. {
  212. ras.line(m_x1, m_y1,
  213. x2, y2);
  214. }
  215. m_x1 = x2;
  216. m_y1 = y2;
  217. }
  218. }
  219. }