ShapePath.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System;
  2. namespace MatterHackers.Agg
  3. {
  4. public static class ShapePath
  5. {
  6. [Flags]
  7. public enum FlagsAndCommand
  8. {
  9. Stop = 0x00,
  10. MoveTo = 0x01,
  11. LineTo = 0x02,
  12. Curve3 = 0x03,
  13. Curve4 = 0x04,
  14. EndPoly = 0x0F,
  15. CommandsMask = 0x0F,
  16. FlagNone = 0x00,
  17. FlagCCW = 0x10,
  18. FlagCW = 0x20,
  19. FlagClose = 0x40,
  20. FlagsMask = 0xF0
  21. };
  22. public static bool is_vertex(FlagsAndCommand c)
  23. {
  24. return c >= FlagsAndCommand.MoveTo
  25. && c < FlagsAndCommand.EndPoly;
  26. }
  27. public static bool is_drawing(FlagsAndCommand c)
  28. {
  29. return c >= FlagsAndCommand.LineTo && c < FlagsAndCommand.EndPoly;
  30. }
  31. public static bool is_stop(FlagsAndCommand c)
  32. {
  33. return c == FlagsAndCommand.Stop;
  34. }
  35. public static bool is_move_to(FlagsAndCommand c)
  36. {
  37. return c == FlagsAndCommand.MoveTo;
  38. }
  39. public static bool is_line_to(FlagsAndCommand c)
  40. {
  41. return c == FlagsAndCommand.LineTo;
  42. }
  43. public static bool is_curve(FlagsAndCommand c)
  44. {
  45. return c == FlagsAndCommand.Curve3
  46. || c == FlagsAndCommand.Curve4;
  47. }
  48. public static bool is_curve3(FlagsAndCommand c)
  49. {
  50. return c == FlagsAndCommand.Curve3;
  51. }
  52. public static bool is_curve4(FlagsAndCommand c)
  53. {
  54. return c == FlagsAndCommand.Curve4;
  55. }
  56. public static bool is_end_poly(FlagsAndCommand c)
  57. {
  58. return (c & FlagsAndCommand.CommandsMask) == FlagsAndCommand.EndPoly;
  59. }
  60. public static bool is_close(FlagsAndCommand c)
  61. {
  62. return (c & ~(FlagsAndCommand.FlagCW | FlagsAndCommand.FlagCCW)) ==
  63. (FlagsAndCommand.EndPoly | FlagsAndCommand.FlagClose);
  64. }
  65. public static bool is_next_poly(FlagsAndCommand c)
  66. {
  67. return is_stop(c) || is_move_to(c) || is_end_poly(c);
  68. }
  69. public static bool is_cw(FlagsAndCommand c)
  70. {
  71. return (c & FlagsAndCommand.FlagCW) != 0;
  72. }
  73. public static bool is_ccw(FlagsAndCommand c)
  74. {
  75. return (c & FlagsAndCommand.FlagCCW) != 0;
  76. }
  77. public static bool is_oriented(FlagsAndCommand c)
  78. {
  79. return (c & (FlagsAndCommand.FlagCW | FlagsAndCommand.FlagCCW)) != 0;
  80. }
  81. public static bool is_closed(FlagsAndCommand c)
  82. {
  83. return (c & FlagsAndCommand.FlagClose) != 0;
  84. }
  85. public static FlagsAndCommand get_close_flag(FlagsAndCommand c)
  86. {
  87. return (FlagsAndCommand)(c & FlagsAndCommand.FlagClose);
  88. }
  89. public static FlagsAndCommand clear_orientation(FlagsAndCommand c)
  90. {
  91. return c & ~(FlagsAndCommand.FlagCW | FlagsAndCommand.FlagCCW);
  92. }
  93. public static FlagsAndCommand get_orientation(FlagsAndCommand c)
  94. {
  95. return c & (FlagsAndCommand.FlagCW | FlagsAndCommand.FlagCCW);
  96. }
  97. /*
  98. //---------------------------------------------------------set_orientation
  99. public static path_flags_e set_orientation(int c, path_flags_e o)
  100. {
  101. return clear_orientation(c) | o;
  102. }
  103. */
  104. static public void shorten_path(MatterHackers.Agg.VertexSequence vs, double s)
  105. {
  106. shorten_path(vs, s, 0);
  107. }
  108. static public void shorten_path(VertexSequence vs, double s, int closed)
  109. {
  110. if (s > 0.0 && vs.Count > 1)
  111. {
  112. double d;
  113. int n = (int)(vs.Count - 2);
  114. while (n != 0)
  115. {
  116. d = vs[n].dist;
  117. if (d > s) break;
  118. vs.RemoveLast();
  119. s -= d;
  120. --n;
  121. }
  122. if (vs.Count < 2)
  123. {
  124. vs.Clear();
  125. }
  126. else
  127. {
  128. n = (int)vs.Count - 1;
  129. VertexDistance prev = vs[n - 1];
  130. VertexDistance last = vs[n];
  131. d = (prev.dist - s) / prev.dist;
  132. double x = prev.x + (last.x - prev.x) * d;
  133. double y = prev.y + (last.y - prev.y) * d;
  134. last.x = x;
  135. last.y = y;
  136. if (!prev.IsEqual(last)) vs.RemoveLast();
  137. vs.close(closed != 0);
  138. }
  139. }
  140. }
  141. }
  142. }