shapes.cc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 "shapes.h"
  32. #include <iostream>
  33. namespace p2t {
  34. Triangle::Triangle(Point& a, Point& b, Point& c)
  35. {
  36. points_[0] = &a; points_[1] = &b; points_[2] = &c;
  37. neighbors_[0] = NULL; neighbors_[1] = NULL; neighbors_[2] = NULL;
  38. constrained_edge[0] = constrained_edge[1] = constrained_edge[2] = false;
  39. delaunay_edge[0] = delaunay_edge[1] = delaunay_edge[2] = false;
  40. interior_ = false;
  41. }
  42. // Update neighbor pointers
  43. void Triangle::MarkNeighbor(Point* p1, Point* p2, Triangle* t)
  44. {
  45. if ((p1 == points_[2] && p2 == points_[1]) || (p1 == points_[1] && p2 == points_[2]))
  46. neighbors_[0] = t;
  47. else if ((p1 == points_[0] && p2 == points_[2]) || (p1 == points_[2] && p2 == points_[0]))
  48. neighbors_[1] = t;
  49. else if ((p1 == points_[0] && p2 == points_[1]) || (p1 == points_[1] && p2 == points_[0]))
  50. neighbors_[2] = t;
  51. else
  52. assert(0);
  53. }
  54. // Exhaustive search to update neighbor pointers
  55. void Triangle::MarkNeighbor(Triangle& t)
  56. {
  57. if (t.Contains(points_[1], points_[2])) {
  58. neighbors_[0] = &t;
  59. t.MarkNeighbor(points_[1], points_[2], this);
  60. } else if (t.Contains(points_[0], points_[2])) {
  61. neighbors_[1] = &t;
  62. t.MarkNeighbor(points_[0], points_[2], this);
  63. } else if (t.Contains(points_[0], points_[1])) {
  64. neighbors_[2] = &t;
  65. t.MarkNeighbor(points_[0], points_[1], this);
  66. }
  67. }
  68. /**
  69. * Clears all references to all other triangles and points
  70. */
  71. void Triangle::Clear()
  72. {
  73. Triangle *t;
  74. for( int i=0; i<3; i++ )
  75. {
  76. t = neighbors_[i];
  77. if( t != NULL )
  78. {
  79. t->ClearNeighbor( this );
  80. }
  81. }
  82. ClearNeighbors();
  83. points_[0]=points_[1]=points_[2] = NULL;
  84. }
  85. void Triangle::ClearNeighbor(const Triangle *triangle )
  86. {
  87. if( neighbors_[0] == triangle )
  88. {
  89. neighbors_[0] = NULL;
  90. }
  91. else if( neighbors_[1] == triangle )
  92. {
  93. neighbors_[1] = NULL;
  94. }
  95. else
  96. {
  97. neighbors_[2] = NULL;
  98. }
  99. }
  100. void Triangle::ClearNeighbors()
  101. {
  102. neighbors_[0] = NULL;
  103. neighbors_[1] = NULL;
  104. neighbors_[2] = NULL;
  105. }
  106. void Triangle::ClearDelunayEdges()
  107. {
  108. delaunay_edge[0] = delaunay_edge[1] = delaunay_edge[2] = false;
  109. }
  110. Point* Triangle::OppositePoint(Triangle& t, const Point& p)
  111. {
  112. Point *cw = t.PointCW(p);
  113. return PointCW(*cw);
  114. }
  115. // Legalized triangle by rotating clockwise around point(0)
  116. void Triangle::Legalize(Point& point)
  117. {
  118. points_[1] = points_[0];
  119. points_[0] = points_[2];
  120. points_[2] = &point;
  121. }
  122. // Legalize triagnle by rotating clockwise around oPoint
  123. void Triangle::Legalize(Point& opoint, Point& npoint)
  124. {
  125. if (&opoint == points_[0]) {
  126. points_[1] = points_[0];
  127. points_[0] = points_[2];
  128. points_[2] = &npoint;
  129. } else if (&opoint == points_[1]) {
  130. points_[2] = points_[1];
  131. points_[1] = points_[0];
  132. points_[0] = &npoint;
  133. } else if (&opoint == points_[2]) {
  134. points_[0] = points_[2];
  135. points_[2] = points_[1];
  136. points_[1] = &npoint;
  137. } else {
  138. assert(0);
  139. }
  140. }
  141. int Triangle::Index(const Point* p)
  142. {
  143. if (p == points_[0]) {
  144. return 0;
  145. } else if (p == points_[1]) {
  146. return 1;
  147. } else if (p == points_[2]) {
  148. return 2;
  149. }
  150. assert(0);
  151. return -1;
  152. }
  153. int Triangle::EdgeIndex(const Point* p1, const Point* p2)
  154. {
  155. if (points_[0] == p1) {
  156. if (points_[1] == p2) {
  157. return 2;
  158. } else if (points_[2] == p2) {
  159. return 1;
  160. }
  161. } else if (points_[1] == p1) {
  162. if (points_[2] == p2) {
  163. return 0;
  164. } else if (points_[0] == p2) {
  165. return 2;
  166. }
  167. } else if (points_[2] == p1) {
  168. if (points_[0] == p2) {
  169. return 1;
  170. } else if (points_[1] == p2) {
  171. return 0;
  172. }
  173. }
  174. return -1;
  175. }
  176. void Triangle::MarkConstrainedEdge(int index)
  177. {
  178. constrained_edge[index] = true;
  179. }
  180. void Triangle::MarkConstrainedEdge(Edge& edge)
  181. {
  182. MarkConstrainedEdge(edge.p, edge.q);
  183. }
  184. // Mark edge as constrained
  185. void Triangle::MarkConstrainedEdge(Point* p, Point* q)
  186. {
  187. if ((q == points_[0] && p == points_[1]) || (q == points_[1] && p == points_[0])) {
  188. constrained_edge[2] = true;
  189. } else if ((q == points_[0] && p == points_[2]) || (q == points_[2] && p == points_[0])) {
  190. constrained_edge[1] = true;
  191. } else if ((q == points_[1] && p == points_[2]) || (q == points_[2] && p == points_[1])) {
  192. constrained_edge[0] = true;
  193. }
  194. }
  195. // The point counter-clockwise to given point
  196. Point* Triangle::PointCW(const Point& point)
  197. {
  198. if (&point == points_[0]) {
  199. return points_[2];
  200. } else if (&point == points_[1]) {
  201. return points_[0];
  202. } else if (&point == points_[2]) {
  203. return points_[1];
  204. }
  205. assert(0);
  206. return NULL;
  207. }
  208. // The point counter-clockwise to given point
  209. Point* Triangle::PointCCW(const Point& point)
  210. {
  211. if (&point == points_[0]) {
  212. return points_[1];
  213. } else if (&point == points_[1]) {
  214. return points_[2];
  215. } else if (&point == points_[2]) {
  216. return points_[0];
  217. }
  218. assert(0);
  219. return NULL;
  220. }
  221. // The neighbor clockwise to given point
  222. Triangle* Triangle::NeighborCW(const Point& point)
  223. {
  224. if (&point == points_[0]) {
  225. return neighbors_[1];
  226. } else if (&point == points_[1]) {
  227. return neighbors_[2];
  228. }
  229. return neighbors_[0];
  230. }
  231. // The neighbor counter-clockwise to given point
  232. Triangle* Triangle::NeighborCCW(const Point& point)
  233. {
  234. if (&point == points_[0]) {
  235. return neighbors_[2];
  236. } else if (&point == points_[1]) {
  237. return neighbors_[0];
  238. }
  239. return neighbors_[1];
  240. }
  241. bool Triangle::GetConstrainedEdgeCCW(const Point& p)
  242. {
  243. if (&p == points_[0]) {
  244. return constrained_edge[2];
  245. } else if (&p == points_[1]) {
  246. return constrained_edge[0];
  247. }
  248. return constrained_edge[1];
  249. }
  250. bool Triangle::GetConstrainedEdgeCW(const Point& p)
  251. {
  252. if (&p == points_[0]) {
  253. return constrained_edge[1];
  254. } else if (&p == points_[1]) {
  255. return constrained_edge[2];
  256. }
  257. return constrained_edge[0];
  258. }
  259. void Triangle::SetConstrainedEdgeCCW(const Point& p, bool ce)
  260. {
  261. if (&p == points_[0]) {
  262. constrained_edge[2] = ce;
  263. } else if (&p == points_[1]) {
  264. constrained_edge[0] = ce;
  265. } else {
  266. constrained_edge[1] = ce;
  267. }
  268. }
  269. void Triangle::SetConstrainedEdgeCW(const Point& p, bool ce)
  270. {
  271. if (&p == points_[0]) {
  272. constrained_edge[1] = ce;
  273. } else if (&p == points_[1]) {
  274. constrained_edge[2] = ce;
  275. } else {
  276. constrained_edge[0] = ce;
  277. }
  278. }
  279. bool Triangle::GetDelunayEdgeCCW(const Point& p)
  280. {
  281. if (&p == points_[0]) {
  282. return delaunay_edge[2];
  283. } else if (&p == points_[1]) {
  284. return delaunay_edge[0];
  285. }
  286. return delaunay_edge[1];
  287. }
  288. bool Triangle::GetDelunayEdgeCW(const Point& p)
  289. {
  290. if (&p == points_[0]) {
  291. return delaunay_edge[1];
  292. } else if (&p == points_[1]) {
  293. return delaunay_edge[2];
  294. }
  295. return delaunay_edge[0];
  296. }
  297. void Triangle::SetDelunayEdgeCCW(const Point& p, bool e)
  298. {
  299. if (&p == points_[0]) {
  300. delaunay_edge[2] = e;
  301. } else if (&p == points_[1]) {
  302. delaunay_edge[0] = e;
  303. } else {
  304. delaunay_edge[1] = e;
  305. }
  306. }
  307. void Triangle::SetDelunayEdgeCW(const Point& p, bool e)
  308. {
  309. if (&p == points_[0]) {
  310. delaunay_edge[1] = e;
  311. } else if (&p == points_[1]) {
  312. delaunay_edge[2] = e;
  313. } else {
  314. delaunay_edge[0] = e;
  315. }
  316. }
  317. // The neighbor across to given point
  318. Triangle& Triangle::NeighborAcross(const Point& opoint)
  319. {
  320. if (&opoint == points_[0]) {
  321. return *neighbors_[0];
  322. } else if (&opoint == points_[1]) {
  323. return *neighbors_[1];
  324. }
  325. return *neighbors_[2];
  326. }
  327. void Triangle::DebugPrint()
  328. {
  329. using namespace std;
  330. cout << points_[0]->x << "," << points_[0]->y << " ";
  331. cout << points_[1]->x << "," << points_[1]->y << " ";
  332. cout << points_[2]->x << "," << points_[2]->y << endl;
  333. }
  334. }