mipolyutil.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /***********************************************************
  2. Copyright 1987, 1998 The Open Group
  3. Permission to use, copy, modify, distribute, and sell this software and its
  4. documentation for any purpose is hereby granted without fee, provided that
  5. the above copyright notice appear in all copies and that both that
  6. copyright notice and this permission notice appear in supporting
  7. documentation.
  8. The above copyright notice and this permission notice shall be included in
  9. all copies or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  11. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  13. OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  14. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  15. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. Except as contained in this notice, the name of The Open Group shall not be
  17. used in advertising or otherwise to promote the sale, use or other dealings
  18. in this Software without prior written authorization from The Open Group.
  19. Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
  20. All Rights Reserved
  21. Permission to use, copy, modify, and distribute this software and its
  22. documentation for any purpose and without fee is hereby granted,
  23. provided that the above copyright notice appear in all copies and that
  24. both that copyright notice and this permission notice appear in
  25. supporting documentation, and that the name of Digital not be
  26. used in advertising or publicity pertaining to distribution of the
  27. software without specific, written prior permission.
  28. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  29. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  30. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  31. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  32. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  33. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  34. SOFTWARE.
  35. ******************************************************************/
  36. #ifdef HAVE_DIX_CONFIG_H
  37. #include <dix-config.h>
  38. #endif
  39. #include "regionstr.h"
  40. #include "gc.h"
  41. #include "miscanfill.h"
  42. #include "mipoly.h"
  43. #include "misc.h" /* MAXINT */
  44. /*
  45. * fillUtils.c
  46. *
  47. * Written by Brian Kelleher; Oct. 1985
  48. *
  49. * This module contains all of the utility functions
  50. * needed to scan convert a polygon.
  51. *
  52. */
  53. /*
  54. * InsertEdgeInET
  55. *
  56. * Insert the given edge into the edge table.
  57. * First we must find the correct bucket in the
  58. * Edge table, then find the right slot in the
  59. * bucket. Finally, we can insert it.
  60. *
  61. */
  62. Bool
  63. miInsertEdgeInET(ET, ETE, scanline, SLLBlock, iSLLBlock)
  64. EdgeTable *ET;
  65. EdgeTableEntry *ETE;
  66. int scanline;
  67. ScanLineListBlock **SLLBlock;
  68. int *iSLLBlock;
  69. {
  70. EdgeTableEntry *start, *prev;
  71. ScanLineList *pSLL, *pPrevSLL;
  72. ScanLineListBlock *tmpSLLBlock;
  73. /*
  74. * find the right bucket to put the edge into
  75. */
  76. pPrevSLL = &ET->scanlines;
  77. pSLL = pPrevSLL->next;
  78. while (pSLL && (pSLL->scanline < scanline))
  79. {
  80. pPrevSLL = pSLL;
  81. pSLL = pSLL->next;
  82. }
  83. /*
  84. * reassign pSLL (pointer to ScanLineList) if necessary
  85. */
  86. if ((!pSLL) || (pSLL->scanline > scanline))
  87. {
  88. if (*iSLLBlock > SLLSPERBLOCK-1)
  89. {
  90. tmpSLLBlock =
  91. (ScanLineListBlock *)malloc(sizeof(ScanLineListBlock));
  92. if (!tmpSLLBlock)
  93. return FALSE;
  94. (*SLLBlock)->next = tmpSLLBlock;
  95. tmpSLLBlock->next = (ScanLineListBlock *)NULL;
  96. *SLLBlock = tmpSLLBlock;
  97. *iSLLBlock = 0;
  98. }
  99. pSLL = &((*SLLBlock)->SLLs[(*iSLLBlock)++]);
  100. pSLL->next = pPrevSLL->next;
  101. pSLL->edgelist = (EdgeTableEntry *)NULL;
  102. pPrevSLL->next = pSLL;
  103. }
  104. pSLL->scanline = scanline;
  105. /*
  106. * now insert the edge in the right bucket
  107. */
  108. prev = (EdgeTableEntry *)NULL;
  109. start = pSLL->edgelist;
  110. while (start && (start->bres.minor < ETE->bres.minor))
  111. {
  112. prev = start;
  113. start = start->next;
  114. }
  115. ETE->next = start;
  116. if (prev)
  117. prev->next = ETE;
  118. else
  119. pSLL->edgelist = ETE;
  120. return TRUE;
  121. }
  122. /*
  123. * CreateEdgeTable
  124. *
  125. * This routine creates the edge table for
  126. * scan converting polygons.
  127. * The Edge Table (ET) looks like:
  128. *
  129. * EdgeTable
  130. * --------
  131. * | ymax | ScanLineLists
  132. * |scanline|-->------------>-------------->...
  133. * -------- |scanline| |scanline|
  134. * |edgelist| |edgelist|
  135. * --------- ---------
  136. * | |
  137. * | |
  138. * V V
  139. * list of ETEs list of ETEs
  140. *
  141. * where ETE is an EdgeTableEntry data structure,
  142. * and there is one ScanLineList per scanline at
  143. * which an edge is initially entered.
  144. *
  145. */
  146. Bool
  147. miCreateETandAET(count, pts, ET, AET, pETEs, pSLLBlock)
  148. int count;
  149. DDXPointPtr pts;
  150. EdgeTable *ET;
  151. EdgeTableEntry *AET;
  152. EdgeTableEntry *pETEs;
  153. ScanLineListBlock *pSLLBlock;
  154. {
  155. DDXPointPtr top, bottom;
  156. DDXPointPtr PrevPt, CurrPt;
  157. int iSLLBlock = 0;
  158. int dy;
  159. if (count < 2) return TRUE;
  160. /*
  161. * initialize the Active Edge Table
  162. */
  163. AET->next = (EdgeTableEntry *)NULL;
  164. AET->back = (EdgeTableEntry *)NULL;
  165. AET->nextWETE = (EdgeTableEntry *)NULL;
  166. AET->bres.minor = MININT;
  167. /*
  168. * initialize the Edge Table.
  169. */
  170. ET->scanlines.next = (ScanLineList *)NULL;
  171. ET->ymax = MININT;
  172. ET->ymin = MAXINT;
  173. pSLLBlock->next = (ScanLineListBlock *)NULL;
  174. PrevPt = &pts[count-1];
  175. /*
  176. * for each vertex in the array of points.
  177. * In this loop we are dealing with two vertices at
  178. * a time -- these make up one edge of the polygon.
  179. */
  180. while (count--)
  181. {
  182. CurrPt = pts++;
  183. /*
  184. * find out which point is above and which is below.
  185. */
  186. if (PrevPt->y > CurrPt->y)
  187. {
  188. bottom = PrevPt, top = CurrPt;
  189. pETEs->ClockWise = 0;
  190. }
  191. else
  192. {
  193. bottom = CurrPt, top = PrevPt;
  194. pETEs->ClockWise = 1;
  195. }
  196. /*
  197. * don't add horizontal edges to the Edge table.
  198. */
  199. if (bottom->y != top->y)
  200. {
  201. pETEs->ymax = bottom->y-1; /* -1 so we don't get last scanline */
  202. /*
  203. * initialize integer edge algorithm
  204. */
  205. dy = bottom->y - top->y;
  206. BRESINITPGONSTRUCT(dy, top->x, bottom->x, pETEs->bres);
  207. if (!miInsertEdgeInET(ET, pETEs, top->y, &pSLLBlock, &iSLLBlock))
  208. {
  209. miFreeStorage(pSLLBlock->next);
  210. return FALSE;
  211. }
  212. ET->ymax = max(ET->ymax, PrevPt->y);
  213. ET->ymin = min(ET->ymin, PrevPt->y);
  214. pETEs++;
  215. }
  216. PrevPt = CurrPt;
  217. }
  218. return TRUE;
  219. }
  220. /*
  221. * loadAET
  222. *
  223. * This routine moves EdgeTableEntries from the
  224. * EdgeTable into the Active Edge Table,
  225. * leaving them sorted by smaller x coordinate.
  226. *
  227. */
  228. void
  229. miloadAET(AET, ETEs)
  230. EdgeTableEntry *AET, *ETEs;
  231. {
  232. EdgeTableEntry *pPrevAET;
  233. EdgeTableEntry *tmp;
  234. pPrevAET = AET;
  235. AET = AET->next;
  236. while (ETEs)
  237. {
  238. while (AET && (AET->bres.minor < ETEs->bres.minor))
  239. {
  240. pPrevAET = AET;
  241. AET = AET->next;
  242. }
  243. tmp = ETEs->next;
  244. ETEs->next = AET;
  245. if (AET)
  246. AET->back = ETEs;
  247. ETEs->back = pPrevAET;
  248. pPrevAET->next = ETEs;
  249. pPrevAET = ETEs;
  250. ETEs = tmp;
  251. }
  252. }
  253. /*
  254. * computeWAET
  255. *
  256. * This routine links the AET by the
  257. * nextWETE (winding EdgeTableEntry) link for
  258. * use by the winding number rule. The final
  259. * Active Edge Table (AET) might look something
  260. * like:
  261. *
  262. * AET
  263. * ---------- --------- ---------
  264. * |ymax | |ymax | |ymax |
  265. * | ... | |... | |... |
  266. * |next |->|next |->|next |->...
  267. * |nextWETE| |nextWETE| |nextWETE|
  268. * --------- --------- ^--------
  269. * | | |
  270. * V-------------------> V---> ...
  271. *
  272. */
  273. void
  274. micomputeWAET(AET)
  275. EdgeTableEntry *AET;
  276. {
  277. EdgeTableEntry *pWETE;
  278. int inside = 1;
  279. int isInside = 0;
  280. AET->nextWETE = (EdgeTableEntry *)NULL;
  281. pWETE = AET;
  282. AET = AET->next;
  283. while (AET)
  284. {
  285. if (AET->ClockWise)
  286. isInside++;
  287. else
  288. isInside--;
  289. if ((!inside && !isInside) ||
  290. ( inside && isInside))
  291. {
  292. pWETE->nextWETE = AET;
  293. pWETE = AET;
  294. inside = !inside;
  295. }
  296. AET = AET->next;
  297. }
  298. pWETE->nextWETE = (EdgeTableEntry *)NULL;
  299. }
  300. /*
  301. * InsertionSort
  302. *
  303. * Just a simple insertion sort using
  304. * pointers and back pointers to sort the Active
  305. * Edge Table.
  306. *
  307. */
  308. int
  309. miInsertionSort(AET)
  310. EdgeTableEntry *AET;
  311. {
  312. EdgeTableEntry *pETEchase;
  313. EdgeTableEntry *pETEinsert;
  314. EdgeTableEntry *pETEchaseBackTMP;
  315. int changed = 0;
  316. AET = AET->next;
  317. while (AET)
  318. {
  319. pETEinsert = AET;
  320. pETEchase = AET;
  321. while (pETEchase->back->bres.minor > AET->bres.minor)
  322. pETEchase = pETEchase->back;
  323. AET = AET->next;
  324. if (pETEchase != pETEinsert)
  325. {
  326. pETEchaseBackTMP = pETEchase->back;
  327. pETEinsert->back->next = AET;
  328. if (AET)
  329. AET->back = pETEinsert->back;
  330. pETEinsert->next = pETEchase;
  331. pETEchase->back->next = pETEinsert;
  332. pETEchase->back = pETEinsert;
  333. pETEinsert->back = pETEchaseBackTMP;
  334. changed = 1;
  335. }
  336. }
  337. return(changed);
  338. }
  339. /*
  340. * Clean up our act.
  341. */
  342. void
  343. miFreeStorage(pSLLBlock)
  344. ScanLineListBlock *pSLLBlock;
  345. {
  346. ScanLineListBlock *tmpSLLBlock;
  347. while (pSLLBlock)
  348. {
  349. tmpSLLBlock = pSLLBlock->next;
  350. free(pSLLBlock);
  351. pSLLBlock = tmpSLLBlock;
  352. }
  353. }