sweep_context.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. #include "sweep_context.h"
  32. #include <algorithm>
  33. #include "advancing_front.h"
  34. namespace p2t {
  35. SweepContext::SweepContext(const std::vector<Point*>& polyline) : points_(polyline),
  36. front_(0),
  37. head_(0),
  38. tail_(0),
  39. af_head_(0),
  40. af_middle_(0),
  41. af_tail_(0)
  42. {
  43. InitEdges(points_);
  44. }
  45. void SweepContext::AddHole(const std::vector<Point*>& polyline)
  46. {
  47. InitEdges(polyline);
  48. for(unsigned int i = 0; i < polyline.size(); i++) {
  49. points_.push_back(polyline[i]);
  50. }
  51. }
  52. void SweepContext::AddPoint(Point* point) {
  53. points_.push_back(point);
  54. }
  55. std::vector<Triangle*> &SweepContext::GetTriangles()
  56. {
  57. return triangles_;
  58. }
  59. std::list<Triangle*> &SweepContext::GetMap()
  60. {
  61. return map_;
  62. }
  63. void SweepContext::InitTriangulation()
  64. {
  65. double xmax(points_[0]->x), xmin(points_[0]->x);
  66. double ymax(points_[0]->y), ymin(points_[0]->y);
  67. // Calculate bounds.
  68. for (unsigned int i = 0; i < points_.size(); i++) {
  69. Point& p = *points_[i];
  70. if (p.x > xmax)
  71. xmax = p.x;
  72. if (p.x < xmin)
  73. xmin = p.x;
  74. if (p.y > ymax)
  75. ymax = p.y;
  76. if (p.y < ymin)
  77. ymin = p.y;
  78. }
  79. double dx = kAlpha * (xmax - xmin);
  80. double dy = kAlpha * (ymax - ymin);
  81. head_ = new Point(xmax + dx, ymin - dy);
  82. tail_ = new Point(xmin - dx, ymin - dy);
  83. // Sort points along y-axis
  84. std::sort(points_.begin(), points_.end(), cmp);
  85. }
  86. void SweepContext::InitEdges(const std::vector<Point*>& polyline)
  87. {
  88. size_t num_points = polyline.size();
  89. for (size_t i = 0; i < num_points; i++) {
  90. size_t j = i < num_points - 1 ? i + 1 : 0;
  91. edge_list.push_back(new Edge(*polyline[i], *polyline[j]));
  92. }
  93. }
  94. Point* SweepContext::GetPoint(size_t index)
  95. {
  96. return points_[index];
  97. }
  98. void SweepContext::AddToMap(Triangle* triangle)
  99. {
  100. map_.push_back(triangle);
  101. }
  102. Node& SweepContext::LocateNode(const Point& point)
  103. {
  104. // TODO implement search tree
  105. return *front_->LocateNode(point.x);
  106. }
  107. void SweepContext::CreateAdvancingFront(const std::vector<Node*>& nodes)
  108. {
  109. (void) nodes;
  110. // Initial triangle
  111. Triangle* triangle = new Triangle(*points_[0], *tail_, *head_);
  112. map_.push_back(triangle);
  113. af_head_ = new Node(*triangle->GetPoint(1), *triangle);
  114. af_middle_ = new Node(*triangle->GetPoint(0), *triangle);
  115. af_tail_ = new Node(*triangle->GetPoint(2));
  116. front_ = new AdvancingFront(*af_head_, *af_tail_);
  117. // TODO: More intuitive if head is middles next and not previous?
  118. // so swap head and tail
  119. af_head_->next = af_middle_;
  120. af_middle_->next = af_tail_;
  121. af_middle_->prev = af_head_;
  122. af_tail_->prev = af_middle_;
  123. }
  124. void SweepContext::RemoveNode(Node* node)
  125. {
  126. delete node;
  127. }
  128. void SweepContext::MapTriangleToNodes(Triangle& t)
  129. {
  130. for (int i = 0; i < 3; i++) {
  131. if (!t.GetNeighbor(i)) {
  132. Node* n = front_->LocatePoint(t.PointCW(*t.GetPoint(i)));
  133. if (n)
  134. n->triangle = &t;
  135. }
  136. }
  137. }
  138. void SweepContext::RemoveFromMap(Triangle* triangle)
  139. {
  140. map_.remove(triangle);
  141. }
  142. void SweepContext::MeshClean(Triangle& triangle)
  143. {
  144. std::vector<Triangle *> triangles;
  145. triangles.push_back(&triangle);
  146. while(!triangles.empty()){
  147. Triangle *t = triangles.back();
  148. triangles.pop_back();
  149. if (t != NULL && !t->IsInterior()) {
  150. t->IsInterior(true);
  151. triangles_.push_back(t);
  152. for (int i = 0; i < 3; i++) {
  153. if (!t->constrained_edge[i])
  154. triangles.push_back(t->GetNeighbor(i));
  155. }
  156. }
  157. }
  158. }
  159. SweepContext::~SweepContext()
  160. {
  161. // Clean up memory
  162. delete head_;
  163. delete tail_;
  164. delete front_;
  165. delete af_head_;
  166. delete af_middle_;
  167. delete af_tail_;
  168. typedef std::list<Triangle*> type_list;
  169. for(type_list::iterator iter = map_.begin(); iter != map_.end(); ++iter) {
  170. Triangle* ptr = *iter;
  171. delete ptr;
  172. }
  173. for(unsigned int i = 0; i < edge_list.size(); i++) {
  174. delete edge_list[i];
  175. }
  176. }
  177. }