1 package org.ibex.graphics;
\r
3 import org.ibex.util.*;
\r
6 // This is a very heavily modified (nearly complete rewrite) version
\r
7 // of GPCJ, which is itself a Java port of the Generalized Polygon
\r
10 // http://www.cs.man.ac.uk/aig/staff/alan/software/gpc.html
\r
12 // Modifications by Adam Megacz
\r
15 // Possible remaining optimizations:
\r
16 // -- recycle EdgeNode instances
\r
17 // -- evolve PolygonNode into the Polygon class?
\r
20 // !! WARNING !! !! WARNING !! !! WARNING !!
\r
22 // Unlike GPCJ, this code is NOT reentrant or thread-safe; static
\r
23 // arrays are used to avoid allocation penalties. Also, the union(),
\r
24 // intersection(), and xor() methods destructively update the 'this'
\r
29 * The SEI Software Open Source License, Version 1.0
\r
31 * Copyright (c) 2004, Solution Engineering, Inc.
\r
32 * All rights reserved.
\r
34 * Redistribution and use in source and binary forms, with or without
\r
35 * modification, are permitted provided that the following conditions
\r
38 * 1. Redistributions of source code must retain the above copyright
\r
39 * notice, this list of conditions and the following disclaimer.
\r
41 * 2. The end-user documentation included with the redistribution,
\r
42 * if any, must include the following acknowledgment:
\r
43 * "This product includes software developed by the
\r
44 * Solution Engineering, Inc. (http://www.seisw.com/)."
\r
45 * Alternately, this acknowledgment may appear in the software itself,
\r
46 * if and wherever such third-party acknowledgments normally appear.
\r
48 * 3. The name "Solution Engineering" must not be used to endorse or
\r
49 * promote products derived from this software without prior
\r
50 * written permission. For written permission, please contact
\r
53 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
\r
54 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
\r
55 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
\r
56 * DISCLAIMED. IN NO EVENT SHALL SOLUTION ENGINEERING, INC. OR
\r
57 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
\r
58 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
\r
59 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
\r
60 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
\r
61 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
\r
62 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
\r
63 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
\r
65 * ====================================================================
\r
69 public final class Polygon {
\r
71 private static final int DEFAULT_PATHLEN = 10;
\r
72 private static final int DEFAULT_CONTOURS = 4;
\r
74 private static final int BIGNUM = 65535;
\r
76 public boolean[] hole = new boolean[DEFAULT_CONTOURS];
\r
77 public boolean[] contributing = new boolean[DEFAULT_CONTOURS];
\r
78 public float[] x = new float[DEFAULT_PATHLEN];
\r
79 public float[] y = new float[DEFAULT_PATHLEN];
\r
80 public int numvertices = 0;
\r
81 public int[] edges = null;
\r
82 public int numedges = 0;
\r
83 public int[] contours = new int[DEFAULT_CONTOURS];
\r
84 public int numcontours = 0;
\r
85 public float[] minx_ = new float[DEFAULT_CONTOURS];
\r
86 public float[] miny_ = new float[DEFAULT_CONTOURS];
\r
87 public float[] maxx_ = new float[DEFAULT_CONTOURS];
\r
88 public float[] maxy_ = new float[DEFAULT_CONTOURS];
\r
89 public float minx = Float.MAX_VALUE;
\r
90 public float miny = Float.MAX_VALUE;
\r
91 public float maxx = Float.MIN_VALUE;
\r
92 public float maxy = Float.MIN_VALUE;
\r
93 public boolean sealed = false;
\r
95 public Polygon() { }
\r
96 public Polygon(Path p, Affine a) { p.addTo(this, a); }
\r
97 public void intersection(Polygon p2) { clip(GPC_INT, this, p2); }
\r
98 public void intersect(Polygon p2) { clip(GPC_INT, this, p2); }
\r
99 public void union(Polygon p2) { clip(GPC_UNION, this, p2); }
\r
100 public void xor(Polygon p2) { clip(GPC_XOR, this, p2); }
\r
101 public void subtract(Polygon p2) { clip(GPC_DIFF, this, p2); }
\r
102 private static Polygon rectclipper = new Polygon();
\r
103 public Polygon addrect(float x1, float y1, float x2, float y2, Affine a) {
\r
104 add(a.multiply_px(x1, y1), a.multiply_py(x1, y1));
\r
105 add(a.multiply_px(x2, y1), a.multiply_py(x2, y1));
\r
106 add(a.multiply_px(x2, y2), a.multiply_py(x2, y2));
\r
107 add(a.multiply_px(x1, y2), a.multiply_py(x1, y2));
\r
111 public void clipto(float x1, float y1, float x2, float y2, Affine a) {
\r
112 rectclipper.clear();
\r
113 rectclipper.addrect(x1, y1, x2, y2, a);
\r
114 intersection(rectclipper);
\r
116 public void closepath() {
\r
117 if (numcontours > 0 && numvertices == 0) return;
\r
118 if (numcontours > 0 && (x[contours[numcontours-1]] != x[numvertices-1] || y[contours[numcontours-1]] != y[numvertices-1]))
\r
119 add(x[contours[numcontours-1]], y[contours[numcontours-1]]);
\r
121 public void newcontour() {
\r
122 if (numcontours > 0 && numvertices == contours[numcontours-1]) return;
\r
124 maxx_[numcontours] = maxy_[numcontours] = Float.MIN_VALUE;
\r
125 minx_[numcontours] = miny_[numcontours] = Float.MAX_VALUE;
\r
126 contours[numcontours++] = numvertices;
\r
127 if (numcontours >= contours.length - 2) {
\r
128 int[] z = new int[contours.length * 4]; System.arraycopy(contours, 0, z, 0, contours.length); contours = z;
\r
129 boolean[] s = new boolean[hole.length * 4]; System.arraycopy(hole, 0, s, 0, hole.length); hole = s;
\r
130 s = new boolean[contributing.length * 4];System.arraycopy(contributing,0,s,0,contributing.length);contributing = s;
\r
131 float[] f = new float[minx_.length * 4]; System.arraycopy(minx_, 0, f, 0, minx_.length); minx_ = f;
\r
132 f = new float[maxx_.length * 4]; System.arraycopy(maxx_, 0, f, 0, maxx_.length); maxx_ = f;
\r
133 f = new float[miny_.length * 4]; System.arraycopy(miny_, 0, f, 0, miny_.length); miny_ = f;
\r
134 f = new float[maxy_.length * 4]; System.arraycopy(maxy_, 0, f, 0, maxy_.length); maxy_ = f;
\r
135 Log.debug(this, "growing contour list to " + contours.length);
\r
138 public void add(float x, float y) {
\r
139 if (sealed) { Log.error(this, "tried to add a vertex to a sealed polygon!"); return; }
\r
140 if (numcontours == 0) newcontour();
\r
141 this.x[numvertices] = x;
\r
142 this.y[numvertices] = y;
\r
144 if (x > maxx_[numcontours-1]) maxx_[numcontours-1] = x;
\r
145 if (x < minx_[numcontours-1]) minx_[numcontours-1] = x;
\r
146 if (y > maxy_[numcontours-1]) maxy_[numcontours-1] = y;
\r
147 if (y < miny_[numcontours-1]) miny_[numcontours-1] = y;
\r
148 if (x > maxx) maxx = x;
\r
149 if (x < minx) minx = x;
\r
150 if (y > maxy) maxy = y;
\r
151 if (y < miny) miny = y;
\r
152 if (numvertices >= this.x.length) {
\r
153 float[] new_x = new float[this.x.length * 4]; System.arraycopy(this.x, 0, new_x, 0, this.x.length); this.x = new_x;
\r
154 float[] new_y = new float[this.y.length * 4]; System.arraycopy(this.y, 0, new_y, 0, this.y.length); this.y = new_y;
\r
155 Log.debug(this, "growing vertex list to " + this.x.length);
\r
158 public void clear() {
\r
159 numvertices = 0; numedges = 0; numcontours = 0; sealed = false;
\r
160 maxx = Float.MIN_VALUE; maxy = Float.MIN_VALUE; minx = Float.MAX_VALUE; miny = Float.MIN_VALUE;
\r
162 public boolean isEmpty() { return numvertices == 0; }
\r
163 public void add(Polygon p) { add(p, Affine.identity()); }
\r
164 public void add(Polygon p, Affine a) { for(int i=0; i<p.numcontours; i++) add(p, i, a); }
\r
165 public void add(Polygon p, int idx) { add(p, idx, Affine.identity()); }
\r
166 public void add(Polygon p, int idx, Affine a) {
\r
168 for(int i=p.contours[idx]; i<p.contours[idx+1]; i++) {
\r
169 float x = p.x[p.contours[idx]+i];
\r
170 float y = p.y[p.contours[idx]+i];
\r
171 add(a.multiply_px(x, y), a.multiply_py(x, y));
\r
174 public Polygon transform(Affine a) {
\r
175 maxx = Float.MIN_VALUE; maxy = Float.MIN_VALUE; minx = Float.MAX_VALUE; miny = Float.MIN_VALUE;
\r
177 for(int i=0; i<numvertices; i++) {
\r
178 while (i >= contours[s+1]) s++;
\r
179 float x = a.multiply_px(this.x[i], this.y[i]);
\r
180 float y = a.multiply_py(this.x[i], this.y[i]);
\r
183 if (x > maxx_[s]) maxx_[s] = x;
\r
184 if (x < minx_[s]) minx_[s] = x;
\r
185 if (y > maxy_[s]) maxy_[s] = y;
\r
186 if (y < miny_[s]) miny_[s] = y;
\r
187 if (x > maxx) maxx = x;
\r
188 if (x < minx) minx = x;
\r
189 if (y > maxy) maxy = y;
\r
190 if (y < miny) miny = y;
\r
195 public void stroke(PixelBuffer buf, int color) {
\r
197 if (!p.sealed) p.sort();
\r
198 for(int i=0; i<p.numedges; i++) {
\r
199 float x1 = p.x[p.edges[i]];
\r
200 float y1 = p.y[p.edges[i]];
\r
201 float x2 = p.x[p.edges[i]+1];
\r
202 float y2 = p.y[p.edges[i]+1];
\r
203 buf.drawLine((int)Math.floor(x1), (int)Math.floor(y1), (int)Math.ceil(x2), (int)Math.ceil(y2), color);
\r
207 /** finds the x value at which the line intercepts the line y=_y */
\r
208 private int intercept(int i, float _y, boolean includeTop, boolean includeBottom) {
\r
210 if (includeTop ? (_y < Math.min(p.y[i], p.y[i+1])) : (_y <= Math.min(p.y[i], p.y[i+1])))
\r
211 return Integer.MIN_VALUE;
\r
212 if (includeBottom ? (_y > Math.max(p.y[i], p.y[i+1])) : (_y >= Math.max(p.y[i], p.y[i+1])))
\r
213 return Integer.MIN_VALUE;
\r
214 float f = (((float)(p.x[i + 1] - p.x[i])) / ((float)(p.y[i + 1] - p.y[i])) ) * ((float)(_y - p.y[i])) + p.x[i];
\r
215 return (int)Math.floor(f);
\r
218 /** fill the interior of the path */
\r
219 public void fill(PixelBuffer buf, Paint paint) {
\r
221 if (!p.sealed) p.sort();
\r
222 if (p.numedges == 0) return;
\r
223 float y0 = p.y[p.edges[0]], y1 = y0;
\r
224 boolean useEvenOdd = false;
\r
226 // we iterate over all endpoints in increasing y-coordinate order
\r
227 OUTER: for(int index = 1; index<p.numedges; index++) {
\r
230 // we now examine the horizontal band between y=y0 and y=y1
\r
232 y1 = p.y[p.edges[index]];
\r
233 if (y0 == y1) continue;
\r
235 // within this band, we iterate over all p.edges
\r
236 int x0 = Integer.MIN_VALUE;
\r
237 int leftSegment = -1;
\r
239 int x1 = Integer.MAX_VALUE;
\r
240 int rightSegment = Integer.MAX_VALUE;
\r
241 for(int i=0; i<p.numedges; i++) {
\r
242 if (p.y[p.edges[i]] == p.y[p.edges[i]+1]) continue; // ignore horizontal lines; they are irrelevant.
\r
243 // we order the segments by the x-coordinate of their midpoint;
\r
244 // since segments cannot intersect, this is a well-ordering
\r
245 int i0 = intercept(p.edges[i], y0, true, false);
\r
246 int i1 = intercept(p.edges[i], y1, false, true);
\r
247 if (i0 == Integer.MIN_VALUE || i1 == Integer.MIN_VALUE) continue;
\r
248 int midpoint = i0 + i1;
\r
249 if (midpoint < x0) continue;
\r
250 if (midpoint == x0 && i <= leftSegment) continue;
\r
251 if (midpoint > x1) continue;
\r
252 if (midpoint == x1 && i >= rightSegment) continue;
\r
256 if (leftSegment == rightSegment || rightSegment == Integer.MAX_VALUE) break;
\r
257 if (leftSegment != -1)
\r
258 if ((useEvenOdd && count % 2 != 0) || (!useEvenOdd && count != 0)) {
\r
259 int tx1a = intercept(p.edges[leftSegment], y0, true, true);
\r
260 int tx1b = intercept(p.edges[rightSegment], y0, true, true);
\r
261 int tx2a = intercept(p.edges[leftSegment], y1, true, true);
\r
262 int tx2b = intercept(p.edges[rightSegment], y1, true, true);
\r
263 buf.fillTrapezoid(tx1a, tx1b, (int)y0, tx2a, tx2b, (int)y1, ((Paint.SingleColorPaint)paint).color);
\r
265 if (useEvenOdd) count++;
\r
266 else count += (p.y[p.edges[rightSegment]] < p.y[p.edges[rightSegment]+1]) ? -1 : 1;
\r
267 leftSegment = rightSegment; x0 = x1;
\r
272 //////////////////////////////////////////////////////////////////////////////
\r
274 public Polygon sort() {
\r
276 contours[numcontours] = numvertices;
\r
279 edges = new int[numvertices];
\r
280 for(int i=0; i<numcontours; i++)
\r
281 for(int j=contours[i]; j<contours[i+1]-1; j++)
\r
282 edges[numedges++] = j;
\r
283 sort(0, numedges - 1, false);
\r
287 /** simple quicksort, from http://sourceforge.net/snippet/detail.php?type=snippet&id=100240 */
\r
288 int sort(int left, int right, boolean partition) {
\r
291 middle = (left + right) / 2;
\r
292 int s = edges[right]; edges[right] = edges[middle]; edges[middle] = s;
\r
293 for (i = left - 1, j = right; ; ) {
\r
294 while (y[edges[++i]] < y[edges[right]]);
\r
295 while (j > left && y[edges[--j]] > y[edges[right]]);
\r
297 s = edges[i]; edges[i] = edges[j]; edges[j] = s;
\r
299 s = edges[right]; edges[right] = edges[i]; edges[i] = s;
\r
302 if (left >= right) return 0;
\r
303 int p = sort(left, right, true);
\r
304 sort(left, p - 1, false);
\r
305 sort(p + 1, right, false);
\r
310 // Rendering //////////////////////////////////////////////////////////////////////////////
\r
313 private static int bound(int min, int mid, int max) { return mid < min ? min : mid > max ? max : mid; }
\r
315 public String toString(int i) {
\r
317 for(int j=contours[i]; j<contours[i+1]; j++) ret += x[j]+","+y[j];
\r
320 public String toString() {
\r
321 String ret = "Polygon\n";
\r
322 for(int i=0; i<numcontours; i++) ret += toString(i);
\r
327 // GPC //////////////////////////////////////////////////////////////////////////////
\r
329 //private static final float GPC_EPSILON = 2.2204460492503131e-016 ;
\r
330 private static final float GPC_EPSILON = (float)1e-8;
\r
331 private static final String GPC_VERSION = "2.31" ;
\r
332 private static final int LEFT = 0 ;
\r
333 private static final int RIGHT = 1 ;
\r
334 private static final int ABOVE = 0 ;
\r
335 private static final int BELOW = 1 ;
\r
336 private static final int CLIP = 0 ;
\r
337 private static final int SUBJ = 1 ;
\r
339 // evilbadnonthreadsafestuff
\r
340 private static ScanBeamList sbte = new ScanBeamList();
\r
341 private static EdgeTable s_heap = new EdgeTable();
\r
342 private static EdgeTable c_heap = new EdgeTable();
\r
343 private static LmtTable lmt_table = new LmtTable();
\r
344 private static TopPolygonNode out_poly = new TopPolygonNode();
\r
345 private static AetTree aet = new AetTree();
\r
346 private static ItNodeTable it_table = new ItNodeTable();
\r
348 private static Polygon clip(byte op, Polygon subj, Polygon clip) {
\r
350 return clip_(op, subj, clip);
\r
351 } catch (Exception npe) {
\r
352 npe.printStackTrace();
\r
356 private static Polygon clip_(byte op, Polygon subj, Polygon clip) {
\r
357 int parity_CLIP = LEFT;
\r
358 int parity_SUBJ = LEFT;
\r
359 float[] sbt = null;
\r
363 numFreeEdgeNodes = numEdgeNodes;
\r
365 subj.closepath(); subj.contours[subj.numcontours] = subj.numvertices;
\r
366 clip.closepath(); clip.contours[clip.numcontours] = clip.numvertices;
\r
367 PolygonNode.clear();
\r
369 // Test for trivial NULL result cases
\r
370 if ((subj.isEmpty() && clip.isEmpty()) || (subj.isEmpty() && ((op == GPC_INT) || (op == GPC_DIFF))) ||
\r
371 (clip.isEmpty() && (op == GPC_INT)))
\r
372 { subj.clear(); return subj; }
\r
374 // Identify potentialy contributing contours
\r
375 if (((op == GPC_INT) || (op == GPC_DIFF)) && !subj.isEmpty() && !clip.isEmpty())
\r
376 minimax_test(subj, clip, op);
\r
383 if (!subj.isEmpty()) build_lmt(s_heap, lmt_table, sbte, subj, SUBJ, op);
\r
384 if (!clip.isEmpty()) build_lmt(c_heap, lmt_table, sbte, clip, CLIP, op);
\r
385 if (lmt_table.isEmpty()) { subj.clear(); return subj; } // Return a NULL result if no contours contribute
\r
387 // Build scanbeam table from scanbeam tree
\r
389 // Invert clip polygon for difference operation
\r
390 if (op == GPC_DIFF) parity_CLIP = RIGHT;
\r
394 // Process each scanbeam
\r
395 while(scanbeam < sbte.entries) {
\r
396 // Set yb and yt to the bottom and top of the scanbeam
\r
397 float yb = sbt[scanbeam++];
\r
398 float yt = (float)0.0;
\r
399 float dy = (float)0.0;
\r
400 if (scanbeam < sbte.entries) { yt = sbt[scanbeam]; dy = yt - yb; }
\r
402 // === SCANBEAM BOUNDARY PROCESSING ================================
\r
403 // If LMT node corresponding to yb exists
\r
404 if (local_min < lmt_table.numentries && lmt_table.y[local_min] == yb) {
\r
405 // Add edges starting at this local minimum to the AET
\r
406 for(EdgeNode edge = lmt_table.first_bound[local_min]; (edge != null); edge= edge.next_bound)
\r
407 add_edge_to_aet(aet, edge);
\r
411 float px = -Float.MAX_VALUE; // Set dummy previous x value
\r
412 EdgeNode e0 = aet.top_node; // Create bundles within AET
\r
413 EdgeNode e1 = aet.top_node;
\r
415 // Set up bundle fields of first edge
\r
416 if (aet.top_node.type == CLIP) aet.top_node.bundle_ABOVE_CLIP = (aet.top_node.top_y != yb) ? 1 : 0;
\r
417 else aet.top_node.bundle_ABOVE_SUBJ = (aet.top_node.top_y != yb) ? 1 : 0;
\r
419 if (((aet.top_node.type==0) ? 1 : 0) == CLIP) aet.top_node.bundle_ABOVE_CLIP = 0;
\r
420 else aet.top_node.bundle_ABOVE_SUBJ = 0;
\r
421 aet.top_node.bstate_ABOVE = UNBUNDLED;
\r
423 for (EdgeNode next_edge = aet.top_node.next; next_edge != null; next_edge = next_edge.next) {
\r
424 int ne_type = next_edge.type;
\r
425 int ne_type_opp = ((next_edge.type==0) ? 1 : 0); // next edge type opposite
\r
427 // Set up bundle fields of next edge
\r
428 if (ne_type == CLIP) next_edge.bundle_ABOVE_CLIP = (next_edge.top_y != yb) ? 1 : 0;
\r
429 else next_edge.bundle_ABOVE_SUBJ = (next_edge.top_y != yb) ? 1 : 0;
\r
430 if (ne_type_opp == CLIP) next_edge.bundle_ABOVE_CLIP = 0;
\r
431 else next_edge.bundle_ABOVE_SUBJ = 0;
\r
432 next_edge.bstate_ABOVE = UNBUNDLED;
\r
434 // Bundle edges above the scanbeam boundary if they coincide
\r
435 if ((ne_type == CLIP ? next_edge.bundle_ABOVE_CLIP : next_edge.bundle_ABOVE_SUBJ) == 1) {
\r
436 if (EQ(e0.xb, next_edge.xb) && EQ(e0.dx, next_edge.dx) && (e0.top_y != yb)) {
\r
437 if (ne_type == CLIP)
\r
438 next_edge.bundle_ABOVE_CLIP ^= ne_type == CLIP ? e0.bundle_ABOVE_CLIP : e0.bundle_ABOVE_SUBJ;
\r
439 else next_edge.bundle_ABOVE_SUBJ ^= ne_type == CLIP ? e0.bundle_ABOVE_CLIP : e0.bundle_ABOVE_SUBJ;
\r
440 if (ne_type_opp == CLIP)
\r
441 next_edge.bundle_ABOVE_CLIP = ne_type_opp == CLIP ? e0.bundle_ABOVE_CLIP : e0.bundle_ABOVE_SUBJ;
\r
442 else next_edge.bundle_ABOVE_SUBJ = ne_type_opp == CLIP ? e0.bundle_ABOVE_CLIP : e0.bundle_ABOVE_SUBJ;
\r
443 next_edge.bstate_ABOVE = BUNDLE_HEAD;
\r
444 e0.bundle_ABOVE_CLIP = 0;
\r
445 e0.bundle_ABOVE_SUBJ = 0;
\r
446 e0.bstate_ABOVE = BUNDLE_TAIL;
\r
452 int horiz_CLIP = HState.NH;
\r
453 int horiz_SUBJ = HState.NH;
\r
454 int exists_CLIP = 0;
\r
455 int exists_SUBJ = 0;
\r
456 PolygonNode cf = null;
\r
458 // Process each edge at this scanbeam boundary
\r
459 for (EdgeNode edge = aet.top_node; (edge != null); edge = edge.next) {
\r
460 exists_CLIP = edge.bundle_ABOVE_CLIP + (edge.bundle_BELOW_CLIP << 1);
\r
461 exists_SUBJ = edge.bundle_ABOVE_SUBJ + (edge.bundle_BELOW_SUBJ << 1);
\r
463 if ((exists_CLIP != 0) || (exists_SUBJ != 0)) { // Set bundle side
\r
464 edge.bside_CLIP = parity_CLIP;
\r
465 edge.bside_SUBJ = parity_SUBJ;
\r
466 boolean contributing = false;
\r
467 int br=0, bl=0, tr=0, tl=0;
\r
468 // Determine contributing status and quadrant occupancies
\r
472 contributing= ((exists_CLIP!=0) && ((parity_SUBJ!=0) || (horiz_SUBJ!=0))) ||
\r
473 ((exists_SUBJ!=0) && ((parity_CLIP!=0) || (horiz_CLIP!=0))) ||
\r
474 ((exists_CLIP!=0) && (exists_SUBJ!=0) && (parity_CLIP == parity_SUBJ));
\r
475 br = ((parity_CLIP!=0) && (parity_SUBJ!=0)) ? 1 : 0;
\r
476 bl = (((parity_CLIP ^ edge.bundle_ABOVE_CLIP)!=0) &&
\r
477 ((parity_SUBJ ^ edge.bundle_ABOVE_SUBJ)!=0)) ? 1 : 0;
\r
478 tr = (((parity_CLIP ^ ((horiz_CLIP!=HState.NH)?1:0)) !=0) &&
\r
479 ((parity_SUBJ ^ ((horiz_SUBJ!=HState.NH)?1:0)) !=0)) ? 1 : 0;
\r
480 tl = (((parity_CLIP ^ ((horiz_CLIP!=HState.NH)?1:0) ^ edge.bundle_BELOW_CLIP)!=0) &&
\r
481 ((parity_SUBJ ^ ((horiz_SUBJ!=HState.NH)?1:0) ^ edge.bundle_BELOW_SUBJ)!=0))?1:0;
\r
485 contributing= (exists_CLIP!=0) || (exists_SUBJ!=0);
\r
486 br= (parity_CLIP) ^ (parity_SUBJ);
\r
487 bl= (parity_CLIP ^ edge.bundle_ABOVE_CLIP) ^ (parity_SUBJ ^ edge.bundle_ABOVE_SUBJ);
\r
488 tr= (parity_CLIP ^ ((horiz_CLIP!=HState.NH)?1:0)) ^ (parity_SUBJ ^ ((horiz_SUBJ!=HState.NH)?1:0));
\r
489 tl= (parity_CLIP ^ ((horiz_CLIP!=HState.NH)?1:0) ^ edge.bundle_BELOW_CLIP)
\r
490 ^ (parity_SUBJ ^ ((horiz_SUBJ!=HState.NH)?1:0) ^ edge.bundle_BELOW_SUBJ);
\r
494 contributing= ((exists_CLIP!=0) && (!(parity_SUBJ!=0) || (horiz_SUBJ!=0))) ||
\r
495 ((exists_SUBJ!=0) && (!(parity_CLIP!=0) || (horiz_CLIP!=0))) ||
\r
496 ((exists_CLIP!=0) && (exists_SUBJ!=0) && (parity_CLIP == parity_SUBJ));
\r
497 br= ((parity_CLIP!=0) || (parity_SUBJ!=0))?1:0;
\r
498 bl= (((parity_CLIP ^ edge.bundle_ABOVE_CLIP)!=0) || ((parity_SUBJ ^ edge.bundle_ABOVE_SUBJ)!=0))?1:0;
\r
499 tr= (((parity_CLIP ^ ((horiz_CLIP!=HState.NH)?1:0))!=0) ||
\r
500 ((parity_SUBJ ^ ((horiz_SUBJ!=HState.NH)?1:0))!=0)) ?1:0;
\r
501 tl= (((parity_CLIP ^ ((horiz_CLIP!=HState.NH)?1:0) ^ edge.bundle_BELOW_CLIP)!=0) ||
\r
502 ((parity_SUBJ ^ ((horiz_SUBJ!=HState.NH)?1:0) ^ edge.bundle_BELOW_SUBJ)!=0)) ? 1:0;
\r
504 default: throw new IllegalStateException("Unknown op");
\r
508 parity_CLIP ^= edge.bundle_ABOVE_CLIP;
\r
509 parity_SUBJ ^= edge.bundle_ABOVE_SUBJ;
\r
511 // Update horizontal state
\r
512 if (exists_CLIP!=0) horiz_CLIP = HState.next_h_state[horiz_CLIP][((exists_CLIP - 1) << 1) + parity_CLIP];
\r
513 if (exists_SUBJ!=0) horiz_SUBJ = HState.next_h_state[horiz_SUBJ][((exists_SUBJ - 1) << 1) + parity_SUBJ];
\r
515 if (contributing) {
\r
516 float xb = edge.xb;
\r
517 int vclass = VertexType.getType(tr, tl, br, bl);
\r
519 case VertexType.EMN:
\r
520 case VertexType.IMN:
\r
521 edge.outp_ABOVE = out_poly.add_local_min(xb, yb);
\r
523 cf = edge.outp_ABOVE;
\r
525 case VertexType.ERI:
\r
526 if (xb != px) { cf.add_right(xb, yb); px= xb; }
\r
527 edge.outp_ABOVE= cf;
\r
530 case VertexType.ELI:
\r
531 edge.outp_BELOW.add_left(xb, yb);
\r
533 cf= edge.outp_BELOW;
\r
535 case VertexType.EMX:
\r
536 if (xb != px) { cf.add_left(xb, yb); px= xb; }
\r
537 out_poly.merge_right(cf, edge.outp_BELOW);
\r
540 case VertexType.ILI:
\r
541 if (xb != px) { cf.add_left(xb, yb); px= xb; }
\r
542 edge.outp_ABOVE= cf;
\r
545 case VertexType.IRI:
\r
546 edge.outp_BELOW.add_right(xb, yb);
\r
548 cf= edge.outp_BELOW;
\r
549 edge.outp_BELOW= null;
\r
551 case VertexType.IMX:
\r
552 if (xb != px) { cf.add_right(xb, yb); px= xb; }
\r
553 out_poly.merge_left(cf, edge.outp_BELOW);
\r
555 edge.outp_BELOW= null;
\r
557 case VertexType.IMM:
\r
558 if (xb != px) { cf.add_right(xb, yb); px= xb; }
\r
559 out_poly.merge_left(cf, edge.outp_BELOW);
\r
560 edge.outp_BELOW= null;
\r
561 edge.outp_ABOVE = out_poly.add_local_min(xb, yb);
\r
562 cf= edge.outp_ABOVE;
\r
564 case VertexType.EMM:
\r
565 if (xb != px) { cf.add_left(xb, yb); px= xb; }
\r
566 out_poly.merge_right(cf, edge.outp_BELOW);
\r
567 edge.outp_BELOW= null;
\r
568 edge.outp_ABOVE = out_poly.add_local_min(xb, yb);
\r
569 cf= edge.outp_ABOVE;
\r
571 case VertexType.LED:
\r
572 if (edge.bot_y == yb) edge.outp_BELOW.add_left(xb, yb);
\r
573 edge.outp_ABOVE= edge.outp_BELOW;
\r
576 case VertexType.RED:
\r
577 if (edge.bot_y == yb) edge.outp_BELOW.add_right(xb, yb);
\r
578 edge.outp_ABOVE= edge.outp_BELOW;
\r
583 } // End of switch
\r
584 } // End of contributing conditional
\r
585 } // End of edge exists conditional
\r
586 } // End of AET loop
\r
588 // Delete terminating edges from the AET, otherwise compute xt
\r
589 for (EdgeNode edge = aet.top_node; (edge != null); edge = edge.next) {
\r
590 if (edge.top_y == yb) {
\r
591 EdgeNode prev_edge = edge.prev;
\r
592 EdgeNode next_edge= edge.next;
\r
594 if (prev_edge != null) prev_edge.next = next_edge;
\r
595 else aet.top_node = next_edge;
\r
596 if (next_edge != null) next_edge.prev = prev_edge;
\r
598 // Copy bundle head state to the adjacent tail edge if required
\r
599 if ((edge.bstate_BELOW == BUNDLE_HEAD) && (prev_edge!=null)) {
\r
600 if (prev_edge.bstate_BELOW == BUNDLE_TAIL) {
\r
601 prev_edge.outp_BELOW= edge.outp_BELOW;
\r
602 prev_edge.bstate_BELOW= UNBUNDLED;
\r
603 if (prev_edge.prev != null) {
\r
604 if (prev_edge.prev.bstate_BELOW == BUNDLE_TAIL)
\r
605 prev_edge.bstate_BELOW = BUNDLE_HEAD;
\r
610 if (edge.top_y == yt) edge.xt = edge.top_x;
\r
611 else edge.xt= edge.bot_x + edge.dx * (yt - edge.bot_y);
\r
615 if (scanbeam < sbte.entries) {
\r
616 // === SCANBEAM INTERIOR PROCESSING ==============================
\r
617 // Build intersection table for the current scanbeam
\r
619 it_table.build_intersection_table(aet, dy);
\r
621 // Process each node in the intersection table
\r
622 for (int intersect = 0; intersect < it_table.num; intersect++) {
\r
623 e0 = it_table.ie_0[intersect];
\r
624 e1 = it_table.ie_1[intersect];
\r
626 // Only generate output for contributing intersections
\r
627 if (((e0.bundle_ABOVE_CLIP!=0) || (e0.bundle_ABOVE_SUBJ!=0)) &&
\r
628 ((e1.bundle_ABOVE_CLIP!=0) || (e1.bundle_ABOVE_SUBJ!=0))) {
\r
630 PolygonNode p = e0.outp_ABOVE;
\r
631 PolygonNode q = e1.outp_ABOVE;
\r
632 float ix = it_table.x[intersect];
\r
633 float iy = it_table.y[intersect] + yb;
\r
635 int in_clip = (((e0.bundle_ABOVE_CLIP!=0) && !(e0.bside_CLIP!=0)) ||
\r
636 ((e1.bundle_ABOVE_CLIP!=0) && (e1.bside_CLIP!=0)) ||
\r
637 (!(e0.bundle_ABOVE_CLIP!=0) && !(e1.bundle_ABOVE_CLIP!=0) &&
\r
638 (e0.bside_CLIP!=0) && (e1.bside_CLIP!=0))) ? 1 : 0;
\r
640 int in_subj = (((e0.bundle_ABOVE_SUBJ!=0) && !(e0.bside_SUBJ!=0)) ||
\r
641 ((e1.bundle_ABOVE_SUBJ!=0) && (e1.bside_SUBJ!=0)) ||
\r
642 (!(e0.bundle_ABOVE_SUBJ!=0) && !(e1.bundle_ABOVE_SUBJ!=0) &&
\r
643 (e0.bside_SUBJ!=0) && (e1.bside_SUBJ!=0))) ? 1 : 0;
\r
645 int tr=0, tl=0, br=0, bl=0;
\r
646 // Determine quadrant occupancies
\r
650 tr= ((in_clip!=0) && (in_subj!=0)) ? 1 : 0;
\r
651 tl= (((in_clip ^ e1.bundle_ABOVE_CLIP)!=0) && ((in_subj ^ e1.bundle_ABOVE_SUBJ)!=0))?1:0;
\r
652 br= (((in_clip ^ e0.bundle_ABOVE_CLIP)!=0) && ((in_subj ^ e0.bundle_ABOVE_SUBJ)!=0))?1:0;
\r
653 bl= (((in_clip ^ e1.bundle_ABOVE_CLIP ^ e0.bundle_ABOVE_CLIP)!=0) &&
\r
654 ((in_subj ^ e1.bundle_ABOVE_SUBJ ^ e0.bundle_ABOVE_SUBJ)!=0)) ? 1:0;
\r
657 tr= (in_clip)^ (in_subj);
\r
658 tl= (in_clip ^ e1.bundle_ABOVE_CLIP) ^ (in_subj ^ e1.bundle_ABOVE_SUBJ);
\r
659 br= (in_clip ^ e0.bundle_ABOVE_CLIP) ^ (in_subj ^ e0.bundle_ABOVE_SUBJ);
\r
660 bl= (in_clip ^ e1.bundle_ABOVE_CLIP ^ e0.bundle_ABOVE_CLIP)
\r
661 ^ (in_subj ^ e1.bundle_ABOVE_SUBJ ^ e0.bundle_ABOVE_SUBJ);
\r
664 tr= ((in_clip!=0) || (in_subj!=0)) ? 1 : 0;
\r
665 tl= (((in_clip ^ e1.bundle_ABOVE_CLIP)!=0) || ((in_subj ^ e1.bundle_ABOVE_SUBJ)!=0)) ? 1 : 0;
\r
666 br= (((in_clip ^ e0.bundle_ABOVE_CLIP)!=0) || ((in_subj ^ e0.bundle_ABOVE_SUBJ)!=0)) ? 1 : 0;
\r
667 bl= (((in_clip ^ e1.bundle_ABOVE_CLIP ^ e0.bundle_ABOVE_CLIP)!=0) ||
\r
668 ((in_subj ^ e1.bundle_ABOVE_SUBJ ^ e0.bundle_ABOVE_SUBJ)!=0)) ? 1 : 0;
\r
670 default: throw new IllegalStateException("Unknown op type, "+op);
\r
673 int vclass = VertexType.getType(tr, tl, br, bl);
\r
675 case VertexType.EMN:
\r
676 e0.outp_ABOVE = out_poly.add_local_min(ix, iy);
\r
677 e1.outp_ABOVE = e0.outp_ABOVE;
\r
679 case VertexType.ERI:
\r
680 if (p != null) { p.add_right(ix, iy); e1.outp_ABOVE= p; e0.outp_ABOVE= null; }
\r
682 case VertexType.ELI:
\r
683 if (q != null) { q.add_left(ix, iy); e0.outp_ABOVE= q; e1.outp_ABOVE= null; }
\r
685 case VertexType.EMX:
\r
686 if ((p!=null) && (q!=null)) {
\r
687 p.add_left(ix, iy);
\r
688 out_poly.merge_right(p, q);
\r
689 e0.outp_ABOVE= null;
\r
690 e1.outp_ABOVE= null;
\r
693 case VertexType.IMN:
\r
694 e0.outp_ABOVE = out_poly.add_local_min(ix, iy);
\r
695 e1.outp_ABOVE = e0.outp_ABOVE;
\r
697 case VertexType.ILI:
\r
699 p.add_left(ix, iy);
\r
701 e0.outp_ABOVE= null;
\r
704 case VertexType.IRI:
\r
706 q.add_right(ix, iy);
\r
708 e1.outp_ABOVE= null;
\r
711 case VertexType.IMX:
\r
712 if ((p!=null) && (q!=null)) {
\r
713 p.add_right(ix, iy);
\r
714 out_poly.merge_left(p, q);
\r
715 e0.outp_ABOVE= null;
\r
716 e1.outp_ABOVE= null;
\r
719 case VertexType.IMM:
\r
720 if ((p!=null) && (q!=null)) {
\r
721 p.add_right(ix, iy);
\r
722 out_poly.merge_left(p, q);
\r
723 e0.outp_ABOVE = out_poly.add_local_min(ix, iy);
\r
724 e1.outp_ABOVE= e0.outp_ABOVE;
\r
727 case VertexType.EMM:
\r
728 if ((p!=null) && (q!=null)) {
\r
729 p.add_left(ix, iy);
\r
730 out_poly.merge_right(p, q);
\r
731 e0.outp_ABOVE = out_poly.add_local_min(ix, iy);
\r
732 e1.outp_ABOVE = e0.outp_ABOVE;
\r
736 } // End of switch
\r
737 } // End of contributing intersection conditional
\r
739 // Swap bundle sides in response to edge crossing
\r
740 if (e0.bundle_ABOVE_CLIP!=0) e1.bside_CLIP = (e1.bside_CLIP==0)?1:0;
\r
741 if (e1.bundle_ABOVE_CLIP!=0) e0.bside_CLIP= (e0.bside_CLIP==0)?1:0;
\r
742 if (e0.bundle_ABOVE_SUBJ!=0) e1.bside_SUBJ= (e1.bside_SUBJ==0)?1:0;
\r
743 if (e1.bundle_ABOVE_SUBJ!=0) e0.bside_SUBJ= (e0.bside_SUBJ==0)?1:0;
\r
745 // Swap e0 and e1 bundles in the AET
\r
746 EdgeNode prev_edge = e0.prev;
\r
747 EdgeNode next_edge = e1.next;
\r
748 if (next_edge != null) next_edge.prev = e0;
\r
750 if (e0.bstate_ABOVE == BUNDLE_HEAD) {
\r
751 boolean search = true;
\r
753 prev_edge= prev_edge.prev;
\r
754 if (prev_edge != null) {
\r
755 if (prev_edge.bstate_ABOVE != BUNDLE_TAIL)
\r
762 if (prev_edge == null) {
\r
763 aet.top_node.prev = e1;
\r
764 e1.next = aet.top_node;
\r
765 aet.top_node = e0.next;
\r
767 prev_edge.next.prev = e1;
\r
768 e1.next = prev_edge.next;
\r
769 prev_edge.next = e0.next;
\r
771 e0.next.prev = prev_edge;
\r
773 e0.next = next_edge;
\r
774 } // End of IT loop
\r
776 // Prepare for next scanbeam
\r
777 for (EdgeNode edge = aet.top_node; (edge != null); edge = edge.next) {
\r
778 EdgeNode next_edge = edge.next;
\r
779 EdgeNode succ_edge = edge.succ;
\r
780 if ((edge.top_y == yt) && (succ_edge!=null)) {
\r
781 // Replace AET edge by its successor
\r
782 succ_edge.outp_BELOW= edge.outp_ABOVE;
\r
783 succ_edge.bstate_BELOW= edge.bstate_ABOVE;
\r
784 succ_edge.bundle_BELOW_CLIP= edge.bundle_ABOVE_CLIP;
\r
785 succ_edge.bundle_BELOW_SUBJ= edge.bundle_ABOVE_SUBJ;
\r
786 EdgeNode prev_edge = edge.prev;
\r
787 if (prev_edge != null) prev_edge.next = succ_edge;
\r
788 else aet.top_node = succ_edge;
\r
789 if (next_edge != null) next_edge.prev= succ_edge;
\r
790 succ_edge.prev = prev_edge;
\r
791 succ_edge.next = next_edge;
\r
793 // Update this edge
\r
794 edge.outp_BELOW= edge.outp_ABOVE;
\r
795 edge.bstate_BELOW= edge.bstate_ABOVE;
\r
796 edge.bundle_BELOW_CLIP= edge.bundle_ABOVE_CLIP;
\r
797 edge.bundle_BELOW_SUBJ= edge.bundle_ABOVE_SUBJ;
\r
800 edge.outp_ABOVE= null;
\r
803 } // === END OF SCANBEAM PROCESSING ==================================
\r
805 // Generate result polygon from out_poly
\r
807 return out_poly.getResult(subj);
\r
810 private static boolean EQ(float a, float b) { return (Math.abs(a - b) <= GPC_EPSILON); }
\r
811 private static int PREV_INDEX(int i, int n) { return ((i - 1 + n) % n); }
\r
812 private static int NEXT_INDEX(int i, int n) { return ((i + 1 ) % n); }
\r
813 private static boolean OPTIMAL(Polygon p, int index, int i) {
\r
814 return (p.y[p.contours[index] + PREV_INDEX(i, p.contours[index+1]-p.contours[index])] != p.y[p.contours[index] + i]) ||
\r
815 (p.y[p.contours[index] + NEXT_INDEX(i, p.contours[index+1]-p.contours[index])] != p.y[p.contours[index] + i]);
\r
818 private static void minimax_test(Polygon subj, Polygon clip, byte op) {
\r
819 // For each clip contour, search for any subject contour overlaps
\r
820 for(int c=0; c < clip.numcontours; c++) {
\r
821 boolean overlap = false;
\r
822 for(int s = 0; !overlap && (s < subj.numcontours); s++)
\r
823 overlap = (!((subj.maxx_[s] < clip.minx_[c]) || (subj.minx_[s] > clip.maxx_[c]))) &&
\r
824 (!((subj.maxy_[s] < clip.miny_[c]) || (subj.miny_[s] > clip.maxy_[c])));
\r
825 clip.contributing[c] = overlap;
\r
827 if (op != GPC_INT) return;
\r
828 // For each subject contour, search for any clip contour overlaps
\r
829 for (int s=0; s < subj.numcontours; s++) {
\r
830 boolean overlap = false;
\r
831 for (int c=0; !overlap && (c < clip.numcontours); c++)
\r
832 overlap = (!((subj.maxx_[s] < clip.minx_[c]) || (subj.minx_[s] > clip.maxx_[c]))) &&
\r
833 (!((subj.maxy_[s] < clip.miny_[c]) || (subj.miny_[s] > clip.maxy_[c])));
\r
834 subj.contributing[s] = overlap;
\r
838 private static void insert_bound(LmtTable lmt_table, float y, EdgeNode e) {
\r
839 int index = lmt_table.add(y, null);
\r
840 // Link node e to the tail of the list
\r
841 if (lmt_table.first_bound[index] == null) { lmt_table.first_bound[index] = e; return; }
\r
842 EdgeNode prev_bound = null;
\r
843 EdgeNode current_bound = lmt_table.first_bound[index];
\r
845 // Do primary sort on the x field
\r
846 if (e.bot_x < current_bound.bot_x) {
\r
847 // Insert a new node mid-list
\r
848 if (prev_bound == null) lmt_table.first_bound[index] = e;
\r
849 else prev_bound.next_bound = e;
\r
850 e.next_bound = current_bound;
\r
852 } else if (e.bot_x == current_bound.bot_x) {
\r
853 // Do secondary sort on the dx field
\r
854 if (e.dx < current_bound.dx) {
\r
855 // Insert a new node mid-list
\r
856 if (prev_bound == null) lmt_table.first_bound[index] = e;
\r
857 else prev_bound.next_bound = e;
\r
858 e.next_bound = current_bound;
\r
861 // Head further down the list
\r
862 if (current_bound.next_bound == null) { current_bound.next_bound = e; break; }
\r
863 prev_bound = current_bound;
\r
864 current_bound = current_bound.next_bound;
\r
866 // Head further down the list
\r
867 if (current_bound.next_bound == null) { current_bound.next_bound = e; break; }
\r
868 prev_bound = current_bound;
\r
869 current_bound = current_bound.next_bound;
\r
873 private static void add_edge_to_aet(AetTree aet, EdgeNode edge) {
\r
874 if (aet.top_node == null) {
\r
875 // Append edge onto the tail end of the AET
\r
876 aet.top_node = edge;
\r
881 EdgeNode current_edge = aet.top_node;
\r
882 EdgeNode prev = null;
\r
884 // Do primary sort on the xb field
\r
885 if (edge.xb < current_edge.xb) {
\r
886 // Insert edge here (before the AET edge)
\r
888 edge.next= current_edge;
\r
889 current_edge.prev = edge;
\r
890 if (prev == null) aet.top_node = edge;
\r
891 else prev.next = edge;
\r
893 } else if (edge.xb == current_edge.xb) {
\r
894 // Do secondary sort on the dx field
\r
895 if (edge.dx < current_edge.dx) {
\r
896 // Insert edge here (before the AET edge)
\r
898 edge.next= current_edge;
\r
899 current_edge.prev = edge;
\r
900 if (prev == null) aet.top_node = edge;
\r
901 else prev.next = edge;
\r
904 // Head further into the AET
\r
905 prev = current_edge;
\r
906 if (current_edge.next == null) {
\r
907 current_edge.next = edge;
\r
908 edge.prev = current_edge;
\r
912 current_edge = current_edge.next;
\r
914 // Head further into the AET
\r
915 prev = current_edge;
\r
916 if (current_edge.next == null) {
\r
917 current_edge.next = edge;
\r
918 edge.prev = current_edge;
\r
922 current_edge = current_edge.next;
\r
926 private static void build_lmt(EdgeTable edge_table, LmtTable lmt_table, ScanBeamList sbte, Polygon p, int type, byte op) {
\r
927 for (int c=0; c < p.numcontours; c++) {
\r
928 if (!p.contributing[c]) { p.contributing[c] = true; continue; }
\r
929 // Perform contour optimisation
\r
930 int num_vertices= 0;
\r
932 edge_table.clear();
\r
933 for (int i= 0; i < p.contours[c+1] - p.contours[c]; i++)
\r
934 if (OPTIMAL(p, c, i)) {
\r
935 edge_table.addNode(p.x[p.contours[c]+i], p.y[p.contours[c]+i]);
\r
936 sbte.add(p.y[p.contours[c]+i]); // Record vertex in the scanbeam table
\r
940 // Do the contour forward pass
\r
941 for (int min= 0; min < num_vertices; min++) {
\r
942 // If a forward local minimum...
\r
943 if (edge_table.FWD_MIN(min)) {
\r
944 // Search for the next local maximum...
\r
946 int max = NEXT_INDEX(min, num_vertices);
\r
947 while(edge_table.NOT_FMAX(max)) { num_edges++; max = NEXT_INDEX(max, num_vertices); }
\r
949 // Build the next edge list
\r
951 EdgeNode e = edge_table.getNode(e_index);
\r
952 e.bstate_BELOW = UNBUNDLED;
\r
953 e.bundle_BELOW_CLIP = 0;
\r
954 e.bundle_BELOW_SUBJ = 0;
\r
956 for (int i= 0; i < num_edges; i++) {
\r
957 EdgeNode ei = edge_table.getNode(e_index+i);
\r
958 EdgeNode ev = edge_table.getNode(v);
\r
959 ei.xb = ev.vertex_x;
\r
960 ei.bot_x = ev.vertex_x;
\r
961 ei.bot_y = ev.vertex_y;
\r
962 v = NEXT_INDEX(v, num_vertices);
\r
963 ev = edge_table.getNode(v);
\r
964 ei.top_x= ev.vertex_x;
\r
965 ei.top_y= ev.vertex_y;
\r
966 ei.dx= (ev.vertex_x - ei.bot_x) / (ei.top_y - ei.bot_y);
\r
968 ei.outp_ABOVE = null;
\r
969 ei.outp_BELOW = null;
\r
972 ei.succ = ((num_edges > 1) && (i < (num_edges - 1))) ? edge_table.getNode(e_index+i+1) : null;
\r
973 ei.pred = ((num_edges > 1) && (i > 0)) ? edge_table.getNode(e_index+i-1) : null;
\r
974 ei.next_bound = null;
\r
975 ei.bside_CLIP = (op == GPC_DIFF) ? RIGHT : LEFT;
\r
976 ei.bside_SUBJ = LEFT;
\r
978 insert_bound(lmt_table, edge_table.getNode(min).vertex_y, e);
\r
979 e_index += num_edges;
\r
983 // Do the contour reverse pass
\r
984 for (int min= 0; min < num_vertices; min++) {
\r
985 // If a reverse local minimum...
\r
986 if (edge_table.REV_MIN(min)) {
\r
987 // Search for the previous local maximum...
\r
989 int max = PREV_INDEX(min, num_vertices);
\r
990 while(edge_table.NOT_RMAX(max)) { num_edges++; max = PREV_INDEX(max, num_vertices); }
\r
991 // Build the previous edge list
\r
993 EdgeNode e = edge_table.getNode(e_index);
\r
994 e.bstate_BELOW = UNBUNDLED;
\r
995 e.bundle_BELOW_CLIP = 0;
\r
996 e.bundle_BELOW_SUBJ = 0;
\r
997 for (int i= 0; i < num_edges; i++) {
\r
998 EdgeNode ei = edge_table.getNode(e_index+i);
\r
999 EdgeNode ev = edge_table.getNode(v);
\r
1000 ei.xb = ev.vertex_x;
\r
1001 ei.bot_x = ev.vertex_x;
\r
1002 ei.bot_y = ev.vertex_y;
\r
1003 v= PREV_INDEX(v, num_vertices);
\r
1004 ev = edge_table.getNode(v);
\r
1005 ei.top_x = ev.vertex_x;
\r
1006 ei.top_y = ev.vertex_y;
\r
1007 ei.dx = (ev.vertex_x - ei.bot_x) / (ei.top_y - ei.bot_y);
\r
1009 ei.outp_ABOVE = null;
\r
1010 ei.outp_BELOW = null;
\r
1013 ei.succ = ((num_edges > 1) && (i < (num_edges - 1))) ? edge_table.getNode(e_index+i+1) : null;
\r
1014 ei.pred = ((num_edges > 1) && (i > 0)) ? edge_table.getNode(e_index+i-1) : null;
\r
1015 ei.next_bound = null;
\r
1016 ei.bside_CLIP = (op == GPC_DIFF) ? RIGHT : LEFT;
\r
1017 ei.bside_SUBJ = LEFT;
\r
1019 insert_bound(lmt_table, edge_table.getNode(min).vertex_y, e);
\r
1020 e_index+= num_edges;
\r
1026 public static final byte GPC_DIFF = 0;
\r
1027 public static final byte GPC_INT = 1;
\r
1028 public static final byte GPC_XOR = 2;
\r
1029 public static final byte GPC_UNION = 3;
\r
1031 // Edge intersection classes
\r
1032 private static class VertexType {
\r
1033 public static final int NUL = 0; // Empty non-intersection
\r
1034 public static final int EMX = 1; // External maximum
\r
1035 public static final int ELI = 2; // External left intermediate
\r
1036 public static final int TED = 3; // Top edge
\r
1037 public static final int ERI = 4; // External right intermediate
\r
1038 public static final int RED = 5; // Right edge
\r
1039 public static final int IMM = 6; // Internal maximum and minimum
\r
1040 public static final int IMN = 7; // Internal minimum
\r
1041 public static final int EMN = 8; // External minimum
\r
1042 public static final int EMM = 9; // External maximum and minimum
\r
1043 public static final int LED = 10; // Left edge
\r
1044 public static final int ILI = 11; // Internal left intermediate
\r
1045 public static final int BED = 12; // Bottom edge
\r
1046 public static final int IRI = 13; // Internal right intermediate
\r
1047 public static final int IMX = 14; // Internal maximum
\r
1048 public static final int FUL = 15; // Full non-intersection
\r
1049 public static int getType(int tr, int tl, int br, int bl) { return tr + (tl << 1) + (br << 2) + (bl << 3); }
\r
1052 private static class HState {
\r
1053 public static final int NH = 0; // No horizontal edge
\r
1054 public static final int BH = 1; // Bottom horizontal edge
\r
1055 public static final int TH = 2; // Top horizontal edge
\r
1056 // Horizontal edge state transitions within scanbeam boundary
\r
1057 public static final int[][] next_h_state =
\r
1059 // ABOVE BELOW CROSS
\r
1061 /* NH */ {BH, TH, TH, BH, NH, NH},
\r
1062 /* BH */ {NH, NH, NH, NH, TH, TH},
\r
1063 /* TH */ {NH, NH, NH, NH, BH, BH}
\r
1067 public static final byte UNBUNDLED = 0; // Isolated edge not within a bundle
\r
1068 public static final byte BUNDLE_HEAD = 1; // Bundle head node
\r
1069 public static final byte BUNDLE_TAIL = 2; // Passive bundle tail node
\r
1071 private static class PolygonNode {
\r
1072 public static void clear() { numvertices = 1; }
\r
1073 private static final float[] x = new float[BIGNUM];
\r
1074 private static final float[] y = new float[BIGNUM];
\r
1075 private static final int[] nxt = new int[BIGNUM];
\r
1076 private static int numvertices = 1;
\r
1078 int active = 1; // Active flag / vertex count
\r
1079 boolean hole; // Hole / external contour flag
\r
1080 int v_LEFT; // Left and right vertex list ptrs
\r
1081 int v_RIGHT; // Left and right vertex list ptrs
\r
1082 PolygonNode next; // Pointer to next polygon contour
\r
1083 PolygonNode proxy; // Pointer to actual structure used
\r
1085 public PolygonNode(PolygonNode next, float x0, float y0) {
\r
1086 // Make v_LEFT and v_RIGHT point to new vertex
\r
1087 x[numvertices] = x0; y[numvertices] = y0; nxt[numvertices] = 0;
\r
1088 this.v_LEFT = numvertices;
\r
1089 this.v_RIGHT = numvertices;
\r
1091 this.proxy = this; // Initialise proxy to point to p itself
\r
1094 public void merge(PolygonNode q) { nxt[proxy.v_RIGHT] = q.proxy.v_LEFT; q.proxy.v_LEFT = proxy.v_LEFT; }
\r
1095 public void mergeRight(PolygonNode p) { nxt[proxy.v_RIGHT] = p.proxy.v_LEFT; proxy.v_RIGHT = p.proxy.v_RIGHT; }
\r
1096 public void addSelfTo(Polygon poly) {
\r
1097 for (int vtx = v_LEFT; vtx != 0; vtx = nxt[vtx]) poly.add(x[vtx], y[vtx]);
\r
1098 poly.newcontour();
\r
1100 public int count() { int ret = 0; for (int vtx = v_LEFT; vtx != 0; vtx = nxt[vtx]) ret++; return ret; }
\r
1101 public void add_right(float x0, float y0) {
\r
1102 x[numvertices] = x0; y[numvertices] = y0; nxt[numvertices] = 0;
\r
1103 nxt[proxy.v_RIGHT] = numvertices;
\r
1104 proxy.v_RIGHT = numvertices;
\r
1107 public void add_left(float x0, float y0) {
\r
1108 x[numvertices] = x0; y[numvertices] = y0; nxt[numvertices] = 0;
\r
1109 nxt[numvertices] = proxy.v_LEFT;
\r
1110 proxy.v_LEFT = numvertices;
\r
1115 private static class TopPolygonNode {
\r
1116 PolygonNode top_node = null;
\r
1117 public void clear() { top_node = null; }
\r
1118 public PolygonNode add_local_min(float x, float y) {
\r
1119 PolygonNode existing_min = top_node;
\r
1120 top_node = new PolygonNode(existing_min, x, y);
\r
1123 public void merge_left(PolygonNode p, PolygonNode q) {
\r
1124 // Label contour as a hole
\r
1125 q.proxy.hole = true;
\r
1126 if (p.proxy == q.proxy) return;
\r
1127 // Assign p's vertex list to the left end of q's list
\r
1129 // Redirect any p.proxy references to q.proxy
\r
1130 PolygonNode target = p.proxy;
\r
1131 for(PolygonNode node = top_node; (node != null); node = node.next)
\r
1132 if (node.proxy == target) { node.active= 0; node.proxy= q.proxy; }
\r
1135 public void merge_right(PolygonNode p, PolygonNode q) {
\r
1136 // Label contour as external
\r
1137 q.proxy.hole = false;
\r
1138 if (p.proxy == q.proxy) return;
\r
1139 // Assign p's vertex list to the right end of q's list
\r
1141 // Redirect any p->proxy references to q->proxy
\r
1142 PolygonNode target = p.proxy;
\r
1143 for (PolygonNode node = top_node; (node != null); node = node.next)
\r
1144 if (node.proxy == target) { node.active = 0; node.proxy= q.proxy; }
\r
1147 public int count_contours() {
\r
1149 for (PolygonNode polygon = top_node; (polygon != null); polygon = polygon.next)
\r
1150 if (polygon.active != 0) {
\r
1151 // Count the vertices in the current contour
\r
1152 int nv= polygon.proxy.count();
\r
1153 // Record valid vertex counts in the active field
\r
1154 if (nv > 2) { polygon.active = nv; nc++; }
\r
1155 else polygon.active= 0;
\r
1160 public Polygon getResult(Polygon result) {
\r
1161 int num_contours = count_contours();
\r
1162 if (num_contours <= 0) return result;
\r
1164 PolygonNode npoly_node = null;
\r
1165 for (PolygonNode poly_node = top_node; (poly_node != null); poly_node = npoly_node) {
\r
1166 npoly_node = poly_node.next;
\r
1167 if (poly_node.active == 0) continue;
\r
1168 int prepoly = result.numcontours;
\r
1169 // This algorithm puts the verticies into the poly in reverse order
\r
1170 poly_node.proxy.addSelfTo(result);
\r
1171 if (poly_node.proxy.hole) {
\r
1172 for(int i=prepoly; i<result.numcontours; i++)
\r
1173 result.hole[i] = poly_node.proxy.hole;
\r
1181 private static EdgeNode[] allEdgeNodes = new EdgeNode[BIGNUM];
\r
1182 private static int numEdgeNodes = 0;
\r
1183 private static int numFreeEdgeNodes = 0;
\r
1184 private static EdgeNode newEdgeNode(float x, float y) {
\r
1185 if (numFreeEdgeNodes == 0) {
\r
1186 return allEdgeNodes[numEdgeNodes++] = new EdgeNode(x, y);
\r
1188 EdgeNode ret = allEdgeNodes[--numFreeEdgeNodes];
\r
1194 private static class EdgeNode {
\r
1201 float xb; // Scanbeam bottom x coordinate
\r
1202 float xt; // Scanbeam top x coordinate
\r
1203 float dx; // Change in x for a unit y increase
\r
1204 int type; // Clip / subject edge flag
\r
1205 int bundle_ABOVE_CLIP;
\r
1206 int bundle_ABOVE_SUBJ;
\r
1207 int bundle_BELOW_CLIP;
\r
1208 int bundle_BELOW_SUBJ;
\r
1211 byte bstate_ABOVE;
\r
1212 byte bstate_BELOW;
\r
1213 PolygonNode outp_ABOVE; // Output polygon / tristrip pointer
\r
1214 PolygonNode outp_BELOW; // Output polygon / tristrip pointer
\r
1215 EdgeNode prev; // Previous edge in the AET
\r
1216 EdgeNode next; // Next edge in the AET
\r
1217 EdgeNode pred; // Edge connected at the lower end
\r
1218 EdgeNode succ; // Edge connected at the upper end
\r
1219 EdgeNode next_bound; // Pointer to next bound in LMT
\r
1220 public EdgeNode(float x, float y) { vertex_x = x; vertex_y = y; }
\r
1222 private static class AetTree { EdgeNode top_node; public void clear() { top_node = null; } }
\r
1224 private static class EdgeTable {
\r
1225 public EdgeNode[] edges = new EdgeNode[BIGNUM];
\r
1226 public int entries;
\r
1227 public void clear() { for(; entries >= 0; entries--) edges[entries] = null; entries = 0; }
\r
1228 public void addNode(float x, float y) { edges[entries++] = newEdgeNode(x, y); }
\r
1229 public EdgeNode getNode(int index) { return edges[index]; }
\r
1230 public boolean NOT_RMAX(int i) { return (edges[PREV_INDEX(i, entries)].vertex_y > edges[i].vertex_y); }
\r
1231 public boolean NOT_FMAX(int i) { return(edges[NEXT_INDEX(i, entries)].vertex_y > edges[i].vertex_y); }
\r
1232 public boolean FWD_MIN(int i) {
\r
1233 return ((edges[PREV_INDEX(i, entries)].vertex_y >= edges[i].vertex_y) &&
\r
1234 (edges[NEXT_INDEX(i, entries)].vertex_y > edges[i].vertex_y));
\r
1236 public boolean REV_MIN(int i) {
\r
1237 return ((edges[PREV_INDEX(i, entries)].vertex_y > edges[i].vertex_y) &&
\r
1238 (edges[NEXT_INDEX(i, entries)].vertex_y >= edges[i].vertex_y));
\r
1242 private static class LmtTable {
\r
1243 float[] y = new float[BIGNUM];
\r
1244 EdgeNode[] first_bound = new EdgeNode[BIGNUM];
\r
1245 int numentries = 0;
\r
1246 public void clear() { for(; numentries >= 0; numentries--) first_bound[numentries] = null; numentries = 0; }
\r
1247 public int count() { return numentries; }
\r
1248 public boolean isEmpty() { return numentries == 0; }
\r
1249 public int add(float y0, EdgeNode e) {
\r
1250 for(int i=0; i<numentries; i++)
\r
1251 if (y[i] == y0) return i;
\r
1252 else if (y[i] > y0) {
\r
1253 System.arraycopy(y, i, y, i+1, numentries-i);
\r
1254 System.arraycopy(first_bound, i, first_bound, i+1, numentries-i);
\r
1256 first_bound[i] = e;
\r
1260 y[numentries] = y0;
\r
1261 first_bound[numentries] = e;
\r
1262 return numentries++;
\r
1266 private static class ScanBeamList {
\r
1267 public int entries = 0;
\r
1268 public float[] floats = new float[BIGNUM];
\r
1269 public void clear() { entries = 0; }
\r
1270 public void add(float f) { floats[entries++] = f; }
\r
1271 public float[] sort() {
\r
1272 org.ibex.util.Vec.sortFloats(floats, 0, entries-1);
\r
1274 for(int i=1; i<entries; i++) if (floats[j] != floats[i]) floats[++j] = floats[i];
\r
1280 private static class ItNodeTable {
\r
1281 EdgeNode ie_0[] = new EdgeNode[BIGNUM];
\r
1282 EdgeNode ie_1[] = new EdgeNode[BIGNUM];
\r
1283 float x[] = new float[BIGNUM];
\r
1284 float y[] = new float[BIGNUM];
\r
1286 public void clear() { for(; num>=0; num--) { ie_0[num] = null; ie_1[num] = null; } num = 0; }
\r
1287 public void build_intersection_table(AetTree aet, float dy) {
\r
1289 // Process each AET edge
\r
1290 for (EdgeNode edge = aet.top_node; (edge != null); edge = edge.next)
\r
1291 if ((edge.bstate_ABOVE == BUNDLE_HEAD) ||
\r
1292 (edge.bundle_ABOVE_CLIP != 0) || (edge.bundle_ABOVE_SUBJ != 0))
\r
1293 st = add_st_edge(st, edge, dy);
\r
1295 for(;numst>=0; numst--) edge[numst] = null; numst = 0;
\r
1299 EdgeNode edge[] = new EdgeNode[BIGNUM]; // Pointer to AET edge
\r
1300 float xb[] = new float[BIGNUM]; // Scanbeam bottom x coordinate
\r
1301 float xt[] = new float[BIGNUM]; // Scanbeam top x coordinate
\r
1302 float dx[] = new float[BIGNUM]; // Change in x for a unit y increase
\r
1303 int prev[] = new int[BIGNUM]; // Previous edge in sorted list
\r
1305 private int add_st_edge(int st, EdgeNode e, float dy) {
\r
1306 if (st == -1) { st = numst++; edge[st] = e; xb[st] = e.xb; xt[st] = e.xt; dx[st] = e.dx; prev[st] = -1; return st; }
\r
1307 float den = (xt[st] - xb[st]) - (e.xt - e.xb);
\r
1309 // If new edge and ST edge don't cross, No intersection - insert edge here (before the ST edge)
\r
1310 if ((e.xt >= xt[st]) || (e.dx == dx[st]) || (Math.abs(den) <= GPC_EPSILON))
\r
1311 { prev[numst] = st; st = numst++; edge[st] = e; xb[st] = e.xb; xt[st] = e.xt; dx[st] = e.dx; return st; }
\r
1313 // Compute intersection between new edge and ST edge
\r
1314 float r = (e.xb - xb[st]) / den;
\r
1315 float x = xb[st] + r * (xt[st] - xb[st]);
\r
1317 // Insert the edge pointers and the intersection point in the IT
\r
1318 add_intersection(edge[st], e, x, y);
\r
1319 prev[st] = add_st_edge(prev[st], e, dy);
\r
1322 private void add_intersection(EdgeNode edge0, EdgeNode edge1, float x0, float y0) {
\r
1323 ie_0[num] = edge0; ie_1[num] = edge1; x[num] = x0; y[num] = y0; num++; }
\r
1325 public final void sort(int start, int end) {
\r
1326 if(start >= end) return;
\r
1327 if(end-start <= 6) {
\r
1328 for(int i=start+1;i<=end;i++) {
\r
1329 float tmpa = y[i];
\r
1330 float tmpx = x[i];
\r
1331 EdgeNode tmpe0 = ie_0[i];
\r
1332 EdgeNode tmpe1 = ie_1[i];
\r
1334 for(j=i-1;j>=start;j--) {
\r
1335 if(y[j] <= tmpa) break;
\r
1338 ie_0[j+1] = ie_0[j];
\r
1339 ie_1[j+1] = ie_1[j];
\r
1343 ie_0[j+1] = tmpe0;
\r
1344 ie_1[j+1] = tmpe1;
\r
1348 float pivot = y[end];
\r
1349 int lo = start - 1;
\r
1352 while(y[++lo] < pivot) { }
\r
1353 while((hi > lo) && y[--hi] > pivot) { }
\r
1357 sort(start, lo-1);
\r
1360 private final void swap(int a, int b) {
\r
1362 float tmp = x[a]; x[a] = x[b]; x[b] = tmp;
\r
1363 tmp = y[a]; y[a] = y[b]; y[b] = tmp;
\r
1364 EdgeNode tmpe = ie_0[a]; ie_0[a] = ie_0[b]; ie_0[b] = tmpe;
\r
1365 tmpe = ie_1[a]; ie_1[a] = ie_1[b]; ie_1[b] = tmpe;
\r