gdkpoly-generic.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. { Pointers to basic pascal types, inserted by h2pas conversion program.}
  2. Type
  3. PLongint = ^Longint;
  4. PSmallInt = ^SmallInt;
  5. PByte = ^Byte;
  6. PWord = ^Word;
  7. PDWord = ^DWord;
  8. PDouble = ^Double;
  9. {$PACKRECORDS C}
  10. { $TOG: poly.h /main/5 1998/02/06 17:47:27 kaleb $ }
  11. {
  12. Copyright 1987, 1998 The Open Group
  13. All Rights Reserved.
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  20. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. Except as contained in this notice, the name of The Open Group shall not be
  23. used in advertising or otherwise to promote the sale, use or other dealings
  24. in this Software without prior written authorization from The Open Group.
  25. Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
  26. All Rights Reserved
  27. Permission to use, copy, modify, and distribute this software and its
  28. documentation for any purpose and without fee is hereby granted,
  29. provided that the above copyright notice appear in all copies and that
  30. both that copyright notice and this permission notice appear in
  31. supporting documentation, and that the name of Digital not be
  32. used in advertising or publicity pertaining to distribution of the
  33. software without specific, written prior permission.
  34. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  35. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  36. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  37. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  38. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  39. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  40. SOFTWARE.
  41. }
  42. {
  43. This file contains a few macros to help track
  44. the edge of a filled anObject. The anObject is assumed
  45. to be filled in scanline order, and thus the
  46. algorithm used is an extension of Bresenham's line
  47. drawing algorithm which assumes that y is always the
  48. major axis.
  49. Since these pieces of code are the same for any filled shape,
  50. it is more convenient to gather the library in one
  51. place, but since these pieces of code are also in
  52. the inner loops of output primitives, procedure call
  53. overhead is out of the question.
  54. See the author for a derivation if needed.
  55. }
  56. {
  57. In scan converting polygons, we want to choose those pixels
  58. which are inside the polygon. Thus, we add .5 to the starting
  59. x coordinate for both left and right edges. Now we choose the
  60. first pixel which is inside the pgon for the left edge and the
  61. first pixel which is outside the pgon for the right edge.
  62. Draw the left pixel, but not the right.
  63. How to add .5 to the starting x coordinate:
  64. If the edge is moving to the right, then subtract dy from the
  65. error term from the general form of the algorithm.
  66. If the edge is moving to the left, then add dy to the error term.
  67. The reason for the difference between edges moving to the left
  68. and edges moving to the right is simple: If an edge is moving
  69. to the right, then we want the algorithm to flip immediately.
  70. If it is moving to the left, then we don't want it to flip until
  71. we traverse an entire pixel.
  72. }
  73. {#define BRESINITPGON(dy, x1, x2, xStart, d, m, m1, incr1, incr2) { \
  74. int dx; // local storage \
  75. \
  76. // \
  77. // if the edge is horizontal, then it is ignored \
  78. // and assumed not to be processed. Otherwise, do this stuff. \
  79. // \
  80. if ((dy) != 0) { \
  81. xStart = (x1); \
  82. dx = (x2) - xStart; \
  83. if (dx < 0) { \
  84. m = dx / (dy); \
  85. m1 = m - 1; \
  86. incr1 = -2 dx + 2 (dy) m1; \
  87. incr2 = -2 dx + 2 (dy) m; \
  88. d = 2 m (dy) - 2 dx - 2 (dy); \
  89. } else { \
  90. m = dx / (dy); \
  91. m1 = m + 1; \
  92. incr1 = 2 dx - 2 (dy) m1; \
  93. incr2 = 2 dx - 2 (dy) m; \
  94. d = -2 m (dy) + 2 dx; \
  95. } \
  96. } \
  97. }
  98. }
  99. {#define BRESINCRPGON(d, minval, m, m1, incr1, incr2) { \
  100. if (m1 > 0) { \
  101. if (d > 0) { \
  102. minval += m1; \
  103. d += incr1; \
  104. } \
  105. else { \
  106. minval += m; \
  107. d += incr2; \
  108. } \
  109. } else {\
  110. if (d >= 0) { \
  111. minval += m1; \
  112. d += incr1; \
  113. } \
  114. else { \
  115. minval += m; \
  116. d += incr2; \
  117. } \
  118. } \
  119. }
  120. }
  121. {
  122. This structure contains all of the information needed
  123. to run the bresenham algorithm.
  124. The variables may be hardcoded into the declarations
  125. instead of using this structure to make use of
  126. register declarations.
  127. }
  128. { minor axis }
  129. { decision variable }
  130. { slope and slope+1 }
  131. { error increments }
  132. type
  133. PBRESINFO = ^TBRESINFO;
  134. TBRESINFO = record
  135. minor_axis : longint;
  136. d : longint;
  137. m : longint;
  138. m1 : longint;
  139. incr1 : longint;
  140. incr2 : longint;
  141. end;
  142. { was #define dname(params) para_def_expr }
  143. { argument types are unknown }
  144. { return type might be wrong }
  145. function BRESINITPGONSTRUCT(dmaj,min1,min2,bres : longint) : longint;
  146. { was #define dname(params) para_def_expr }
  147. { argument types are unknown }
  148. { return type might be wrong }
  149. function BRESINCRPGONSTRUCT(bres : longint) : longint;
  150. {
  151. These are the data structures needed to scan
  152. convert regions. Two different scan conversion
  153. methods are available -- the even-odd method, and
  154. the winding number method.
  155. The even-odd rule states that a point is inside
  156. the polygon if a ray drawn from that point in any
  157. direction will pass through an odd number of
  158. path segments.
  159. By the winding number rule, a point is decided
  160. to be inside the polygon if a ray drawn from that
  161. point in any direction passes through a different
  162. number of clockwise and counter-clockwise path
  163. segments.
  164. These data structures are adapted somewhat from
  165. the algorithm in (Foley/Van Dam) for scan converting
  166. polygons.
  167. The basic algorithm is to start at the top (smallest y)
  168. of the polygon, stepping down to the bottom of
  169. the polygon by incrementing the y coordinate. We
  170. keep a list of edges which the current scanline crosses,
  171. sorted by x. This list is called the Active Edge Table (AET)
  172. As we change the y-coordinate, we update each entry in
  173. in the active edge table to reflect the edges new xcoord.
  174. This list must be sorted at each scanline in case
  175. two edges intersect.
  176. We also keep a data structure known as the Edge Table (ET),
  177. which keeps track of all the edges which the current
  178. scanline has not yet reached. The ET is basically a
  179. list of ScanLineList structures containing a list of
  180. edges which are entered at a given scanline. There is one
  181. ScanLineList per scanline at which an edge is entered.
  182. When we enter a new edge, we move it from the ET to the AET.
  183. From the AET, we can implement the even-odd rule as in
  184. (Foley/Van Dam).
  185. The winding number rule is a little trickier. We also
  186. keep the EdgeTableEntries in the AET linked by the
  187. nextWETE (winding EdgeTableEntry) link. This allows
  188. the edges to be linked just as before for updating
  189. purposes, but only uses the edges linked by the nextWETE
  190. link as edges representing spans of the polygon to
  191. drawn (as with the even-odd rule).
  192. }
  193. {
  194. for the winding number rule
  195. }
  196. const
  197. CLOCKWISE = 1;
  198. COUNTERCLOCKWISE = -(1);
  199. { ycoord at which we exit this edge. }
  200. { Bresenham info to run the edge }
  201. { next in the list }
  202. { for insertion sort }
  203. { for winding num rule }
  204. { flag for winding number rule }
  205. type
  206. PEdgeTableEntry = ^TEdgeTableEntry;
  207. TEdgeTableEntry = record
  208. ymax : longint;
  209. bres : TBRESINFO;
  210. next : PEdgeTableEntry;
  211. back : PEdgeTableEntry;
  212. nextWETE : PEdgeTableEntry;
  213. ClockWise : longint;
  214. end;
  215. { the scanline represented }
  216. { header node }
  217. { next in the list }
  218. PScanLineList = ^TScanLineList;
  219. TScanLineList = record
  220. scanline : longint;
  221. edgelist : PEdgeTableEntry;
  222. next : PScanLineList;
  223. end;
  224. { ymax for the polygon }
  225. { ymin for the polygon }
  226. { header node }
  227. PEdgeTable = ^TEdgeTable;
  228. TEdgeTable = record
  229. ymax : longint;
  230. ymin : longint;
  231. scanlines : TScanLineList;
  232. end;
  233. {
  234. Here is a struct to help with storage allocation
  235. so we can allocate a big chunk at a time, and then take
  236. pieces from this heap when we need to.
  237. }
  238. const
  239. SLLSPERBLOCK = 25;
  240. type
  241. PScanLineListBlock = ^TScanLineListBlock;
  242. TScanLineListBlock = record
  243. SLLs : array[0..(SLLSPERBLOCK)-1] of TScanLineList;
  244. next : PScanLineListBlock;
  245. end;
  246. {
  247. a few macros for the inner loops of the fill code where
  248. performance considerations don't allow a procedure call.
  249. Evaluate the given edge at the given scanline.
  250. If the edge has expired, then we leave it and fix up
  251. the active edge table; otherwise, we increment the
  252. x value to be ready for the next scanline.
  253. The winding number rule is in effect, so we must notify
  254. the caller when the edge has been removed so he
  255. can reorder the Winding Active Edge Table.
  256. }
  257. {
  258. #define EVALUATEEDGEWINDING(pAET, pPrevAET, y, fixWAET) { \
  259. if (pAET->ymax == y) { // leaving this edge \
  260. pPrevAET->next = pAET->next; \
  261. pAET = pPrevAET->next; \
  262. fixWAET = 1; \
  263. if (pAET) \
  264. pAET->back = pPrevAET; \
  265. } \
  266. else { \
  267. BRESINCRPGONSTRUCT(pAET->bres); \
  268. pPrevAET = pAET; \
  269. pAET = pAET->next; \
  270. } \
  271. }
  272. }
  273. {
  274. Evaluate the given edge at the given scanline.
  275. If the edge has expired, then we leave it and fix up
  276. the active edge table; otherwise, we increment the
  277. x value to be ready for the next scanline.
  278. The even-odd rule is in effect.
  279. }
  280. {#define EVALUATEEDGEEVENODD(pAET, pPrevAET, y) { \
  281. if (pAET->ymax == y) { // leaving this edge \
  282. pPrevAET->next = pAET->next; \
  283. pAET = pPrevAET->next; \
  284. if (pAET) \
  285. pAET->back = pPrevAET; \
  286. } \
  287. else { \
  288. BRESINCRPGONSTRUCT(pAET->bres); \
  289. pPrevAET = pAET; \
  290. pAET = pAET->next; \
  291. } \
  292. }
  293. }
  294. { was #define dname(params) para_def_expr }
  295. { argument types are unknown }
  296. { return type might be wrong }
  297. function BRESINITPGONSTRUCT(dmaj,min1,min2,bres : longint) : longint;
  298. begin
  299. BRESINITPGONSTRUCT:=BRESINITPGON(dmaj,min1,min2,bres.minor_axis,bres.d,bres.m,bres.m1,bres.incr1,bres.incr2);
  300. end;
  301. { was #define dname(params) para_def_expr }
  302. { argument types are unknown }
  303. { return type might be wrong }
  304. function BRESINCRPGONSTRUCT(bres : longint) : longint;
  305. begin
  306. BRESINCRPGONSTRUCT:=BRESINCRPGON(bres.d,bres.minor_axis,bres.m,bres.m1,bres.incr1,bres.incr2);
  307. end;