agg_VertexSequence.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. //
  20. // vertex_sequence container and vertex_dist struct
  21. //
  22. //----------------------------------------------------------------------------
  23. namespace MatterHackers.Agg
  24. {
  25. //----------------------------------------------------------vertex_sequence
  26. // Modified agg::pod_vector. The data is interpreted as a sequence
  27. // of vertices. It means that the type T must expose:
  28. //
  29. // bool T::operator() (const T& val)
  30. //
  31. // that is called every time a new vertex is being added. The main purpose
  32. // of this operator is the possibility to calculate some values during
  33. // adding and to return true if the vertex fits some criteria or false if
  34. // it doesn't. In the last case the new vertex is not added.
  35. //
  36. // The simple example is filtering coinciding vertices with calculation
  37. // of the distance between the current and previous ones:
  38. //
  39. // struct vertex_dist
  40. // {
  41. // double x;
  42. // double y;
  43. // double dist;
  44. //
  45. // vertex_dist() {}
  46. // vertex_dist(double x_, double y_) :
  47. // x(x_),
  48. // y(y_),
  49. // dist(0.0)
  50. // {
  51. // }
  52. //
  53. // bool operator () (const vertex_dist& val)
  54. // {
  55. // return (dist = calc_distance(x, y, val.x, val.y)) > EPSILON;
  56. // }
  57. // };
  58. //
  59. // Function close() calls this operator and removes the last vertex if
  60. // necessary.
  61. //------------------------------------------------------------------------
  62. public class VertexSequence : VectorPOD<VertexDistance>
  63. {
  64. public override void add(VertexDistance val)
  65. {
  66. if (base.Count > 1)
  67. {
  68. if (!Array[base.Count - 2].IsEqual(Array[base.Count - 1]))
  69. {
  70. base.RemoveLast();
  71. }
  72. }
  73. base.add(val);
  74. }
  75. public void modify_last(VertexDistance val)
  76. {
  77. base.RemoveLast();
  78. add(val);
  79. }
  80. public void close(bool closed)
  81. {
  82. while (base.Count > 1)
  83. {
  84. if (Array[base.Count - 2].IsEqual(Array[base.Count - 1])) break;
  85. VertexDistance t = this[base.Count - 1];
  86. base.RemoveLast();
  87. modify_last(t);
  88. }
  89. if (closed)
  90. {
  91. while (base.Count > 1)
  92. {
  93. if (Array[base.Count - 1].IsEqual(Array[0])) break;
  94. base.RemoveLast();
  95. }
  96. }
  97. }
  98. internal VertexDistance prev(int idx)
  99. {
  100. return this[(idx + currentSize - 1) % currentSize];
  101. }
  102. internal VertexDistance curr(int idx)
  103. {
  104. return this[idx];
  105. }
  106. internal VertexDistance next(int idx)
  107. {
  108. return this[(idx + 1) % currentSize];
  109. }
  110. }
  111. //-------------------------------------------------------------vertex_dist
  112. // Vertex (x, y) with the distance to the next one. The last vertex has
  113. // distance between the last and the first points if the polygon is closed
  114. // and 0.0 if it's a polyline.
  115. public struct VertexDistance
  116. {
  117. public double x;
  118. public double y;
  119. public double dist;
  120. public VertexDistance(double x_, double y_)
  121. {
  122. x = x_;
  123. y = y_;
  124. dist = 0.0;
  125. }
  126. public bool IsEqual(VertexDistance val)
  127. {
  128. bool ret = (dist = agg_math.calc_distance(x, y, val.x, val.y)) > agg_math.vertex_dist_epsilon;
  129. if (!ret) dist = 1.0 / agg_math.vertex_dist_epsilon;
  130. return ret;
  131. }
  132. }
  133. /*
  134. //--------------------------------------------------------vertex_dist_cmd
  135. // Save as the above but with additional "command" value
  136. struct vertex_dist_cmd : vertex_dist
  137. {
  138. unsigned cmd;
  139. vertex_dist_cmd() {}
  140. vertex_dist_cmd(double x_, double y_, unsigned cmd_) :
  141. base (x_, y_)
  142. {
  143. cmd = cmd;
  144. }
  145. };
  146. */
  147. }
  148. //#endif