sweep_context.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Poly2Tri Copyright (c) 2009-2010, Poly2Tri Contributors
  3. * http://code.google.com/p/poly2tri/
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted provided that the following conditions are met:
  9. *
  10. * * Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. * * Neither the name of Poly2Tri nor the names of its contributors may be
  16. * used to endorse or promote products derived from this software without specific
  17. * prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #ifndef SWEEP_CONTEXT_H
  32. #define SWEEP_CONTEXT_H
  33. #include <list>
  34. #include <vector>
  35. #include <cstddef>
  36. namespace p2t {
  37. // Inital triangle factor, seed triangle will extend 30% of
  38. // PointSet width to both left and right.
  39. const double kAlpha = 0.3;
  40. struct Point;
  41. class Triangle;
  42. struct Node;
  43. struct Edge;
  44. class AdvancingFront;
  45. class SweepContext {
  46. public:
  47. /// Constructor
  48. SweepContext(const std::vector<Point*>& polyline);
  49. /// Destructor
  50. ~SweepContext();
  51. void set_head(Point* p1);
  52. Point* head() const;
  53. void set_tail(Point* p1);
  54. Point* tail() const;
  55. size_t point_count() const;
  56. Node& LocateNode(const Point& point);
  57. void RemoveNode(Node* node);
  58. void CreateAdvancingFront(const std::vector<Node*>& nodes);
  59. /// Try to map a node to all sides of this triangle that don't have a neighbor
  60. void MapTriangleToNodes(Triangle& t);
  61. void AddToMap(Triangle* triangle);
  62. Point* GetPoint(size_t index);
  63. Point* GetPoints();
  64. void RemoveFromMap(Triangle* triangle);
  65. void AddHole(const std::vector<Point*>& polyline);
  66. void AddPoint(Point* point);
  67. AdvancingFront* front() const;
  68. void MeshClean(Triangle& triangle);
  69. std::vector<Triangle*> &GetTriangles();
  70. std::list<Triangle*> &GetMap();
  71. std::vector<Edge*> edge_list;
  72. struct Basin {
  73. Node* left_node;
  74. Node* bottom_node;
  75. Node* right_node;
  76. double width;
  77. bool left_highest;
  78. Basin() : left_node(NULL), bottom_node(NULL), right_node(NULL), width(0.0), left_highest(false)
  79. {
  80. }
  81. void Clear()
  82. {
  83. left_node = NULL;
  84. bottom_node = NULL;
  85. right_node = NULL;
  86. width = 0.0;
  87. left_highest = false;
  88. }
  89. };
  90. struct EdgeEvent {
  91. Edge* constrained_edge;
  92. bool right;
  93. EdgeEvent() : constrained_edge(NULL), right(false)
  94. {
  95. }
  96. };
  97. Basin basin;
  98. EdgeEvent edge_event;
  99. private:
  100. friend class Sweep;
  101. std::vector<Triangle*> triangles_;
  102. std::list<Triangle*> map_;
  103. std::vector<Point*> points_;
  104. // Advancing front
  105. AdvancingFront* front_;
  106. // head point used with advancing front
  107. Point* head_;
  108. // tail point used with advancing front
  109. Point* tail_;
  110. Node *af_head_, *af_middle_, *af_tail_;
  111. void InitTriangulation();
  112. void InitEdges(const std::vector<Point*>& polyline);
  113. };
  114. inline AdvancingFront* SweepContext::front() const
  115. {
  116. return front_;
  117. }
  118. inline size_t SweepContext::point_count() const
  119. {
  120. return points_.size();
  121. }
  122. inline void SweepContext::set_head(Point* p1)
  123. {
  124. head_ = p1;
  125. }
  126. inline Point* SweepContext::head() const
  127. {
  128. return head_;
  129. }
  130. inline void SweepContext::set_tail(Point* p1)
  131. {
  132. tail_ = p1;
  133. }
  134. inline Point* SweepContext::tail() const
  135. {
  136. return tail_;
  137. }
  138. }
  139. #endif