Skip to content
This repository

Xorc #16

Merged
merged 2 commits into from almost 2 years ago

2 participants

Pedro E. López de Teruel Alcolea Alberto Ruiz
Pedro E. López de Teruel Alcolea
pedroe commented

Alberto, you know what we are doing...

Pedro Enriqu... and others added some commits
Tipos 31 y 32, con código comentado que sobra. ccb74e7
Pedro E. López de Teruel Alcolea Clipping now returns polygons for xorext following our convention.
(first point is always an intersection, and the first segment belongs to the second polygon)
19d477b
Alberto Ruiz
Owner

Thanks!

Alberto Ruiz albertoruiz merged commit b5f2d65 into from
Alberto Ruiz albertoruiz closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 2 authors.

May 04, 2012
Tipos 31 y 32, con código comentado que sobra. ccb74e7
Pedro E. López de Teruel Alcolea Clipping now returns polygons for xorext following our convention.
(first point is always an intersection, and the first segment belongs to the second polygon)
19d477b
This page is out of date. Refresh to see the latest.
6  packages/contrib/clipping/src/Contours/Clipping.hs
@@ -115,9 +115,9 @@ preclip mode (Closed a'') (Closed b'') = unsafePerformIO $ do
115 115
     -- mapM_ print $ zip3 xs ys os
116 116
 
117 117
     -- provisional
118  
-    let vxs = map (tail . toList) $ takesV ls (fromList xs)
119  
-        vys = map (tail . toList) $ takesV ls (fromList ys)
120  
-        vos = map (tail . toList) $ takesV ls (fromList os)
  118
+    let vxs = map (init . toList) $ takesV ls (fromList xs)
  119
+        vys = map (init . toList) $ takesV ls (fromList ys)
  120
+        vos = map (init . toList) $ takesV ls (fromList os)
121 121
         r | n > 0 = zip (zipWith f vxs vys) vos
122 122
           | otherwise = []
123 123
           where f as bs = Closed $ zipWith Point as bs
144  packages/contrib/clipping/src/Contours/Clipping/clipping.c
@@ -73,7 +73,7 @@ void deletePolygons(struct vertex *poly)
73 73
     while (paux) {
74 74
         l = paux;
75 75
         paux = paux->nextPoly;
76  
-        
  76
+
77 77
         aux = l->next;
78 78
         while (aux) {
79 79
             free(l);
@@ -86,7 +86,7 @@ void deletePolygons(struct vertex *poly)
86 86
 
87 87
 
88 88
 // TODO: add perturbations in case alphaP or alphaQ = 0
89  
-int intersect(struct vertex *p1, struct vertex *p2, 
  89
+int intersect(struct vertex *p1, struct vertex *p2,
90 90
     struct vertex *q1, struct vertex *q2, double *alphaP, double *alphaQ)
91 91
 {
92 92
     double wec_p1, wec_p2, wec_q1, wec_q2;
@@ -97,7 +97,7 @@ int intersect(struct vertex *p1, struct vertex *p2,
97 97
     {
98 98
         wec_q1 = (q1->x - p1->x)*(p1->y - p2->y) + (q1->y - p1->y)*(p2->x - p1->x);
99 99
         wec_q2 = (q2->x - p1->x)*(p1->y - p2->y) + (q2->y - p1->y)*(p2->x - p1->x);
100  
-        
  100
+
101 101
         if (wec_q1 * wec_q2 <= 0)
102 102
         {
103 103
             *alphaP = wec_p1/(wec_p1 - wec_p2);
@@ -120,14 +120,14 @@ int findIntersections(struct vertex *lclip, struct vertex *lsubject)
120 120
     for (v = lsubject; v; v = v->nextVertex == lsubject ? NULL : v->nextVertex)
121 121
         for (w = lclip; w; w = w->nextVertex == lclip ? NULL : w->nextVertex)
122 122
             if (!DISCARD_INTER(v->x, v->y, v->nextVertex->x, v->nextVertex->y,
123  
-                                MIN(w->x, w->nextVertex->x), MIN(w->y, w->nextVertex->y), 
  123
+                                MIN(w->x, w->nextVertex->x), MIN(w->y, w->nextVertex->y),
124 124
                                 MAX(w->x, w->nextVertex->x), MAX(w->y, w->nextVertex->y))
125 125
                 && intersect(v, v->nextVertex, w, w->nextVertex, &a, &b))
126 126
             {
127 127
                 // create intersection points
128  
-                struct vertex *i1 = 
  128
+                struct vertex *i1 =
129 129
                     (struct vertex *) malloc (sizeof(struct vertex));
130  
-                struct vertex *i2 = 
  130
+                struct vertex *i2 =
131 131
                     (struct vertex *) malloc (sizeof(struct vertex));
132 132
 
133 133
                 intersections++;
@@ -137,21 +137,21 @@ int findIntersections(struct vertex *lclip, struct vertex *lsubject)
137 137
                 i2->x = i1->x = w->x + b * (w->nextVertex->x - w->x);
138 138
                 i2->y = i1->y = w->y + b * (w->nextVertex->y - w->y);
139 139
 
140  
-                i1->o = 3;
141  
-                i2->o = 3;
  140
+                i1->o = 31;
  141
+                i2->o = 32;
142 142
 
143 143
                 i1->intersect = 1;
144 144
                 i2->intersect = 1;
145 145
                 i1->processed = 0;
146 146
                 i2->processed = 0;
147  
-                
  147
+
148 148
                 // link intersection points
149 149
                 i1->neighbour = i2;
150 150
                 i2->neighbour = i1;
151 151
 
152 152
                 // sort intersection points into polygons
153 153
                 sort = v;
154  
-                while (sort->next && sort->next->intersect 
  154
+                while (sort->next && sort->next->intersect
155 155
                     && sort->next->alpha < i1->alpha)
156 156
                     sort = sort->next;
157 157
 
@@ -161,9 +161,9 @@ int findIntersections(struct vertex *lclip, struct vertex *lsubject)
161 161
                 aux->prev = i1;
162 162
                 i1->prev = sort;
163 163
 
164  
-                
  164
+
165 165
                 sort = w;
166  
-                while (sort->next && sort->next->intersect 
  166
+                while (sort->next && sort->next->intersect
167 167
                     && sort->next->alpha < i2->alpha)
168 168
                     sort = sort->next;
169 169
 
@@ -190,7 +190,7 @@ int isInside(struct vertex *p, struct vertex *polygon)
190 190
     for (q = polygon; q; q = q->nextVertex == polygon ? NULL : q->nextVertex)
191 191
     {
192 192
         node1 = q;
193  
-        
  193
+
194 194
         if (q->nextVertex)
195 195
             node2 = q->nextVertex;
196 196
         else
@@ -209,7 +209,7 @@ int isInside(struct vertex *p, struct vertex *polygon)
209 209
 }
210 210
 
211 211
 /**
212  
-  * Mark intersection points in polygon @p 
  212
+  * Mark intersection points in polygon @p
213 213
   * depending on whether it's an entry or exit point
214 214
   * with respect the @interior_exterior of polygon @q
215 215
   */
@@ -218,10 +218,10 @@ void markEntries(struct vertex *p, struct vertex *q, int interior_exterior)
218 218
     int status;
219 219
 
220 220
     if (isInside(p, q))
221  
-        status = interior_exterior == POLYGON_INTERIOR ? 
  221
+        status = interior_exterior == POLYGON_INTERIOR ?
222 222
                                 STATUS_ENTRY : STATUS_EXIT;
223 223
     else
224  
-        status = interior_exterior == POLYGON_INTERIOR ? 
  224
+        status = interior_exterior == POLYGON_INTERIOR ?
225 225
                                 STATUS_EXIT : STATUS_ENTRY;
226 226
     struct vertex *pi;
227 227
     for (pi = p->next; pi != p; pi = pi->next)
@@ -229,6 +229,7 @@ void markEntries(struct vertex *p, struct vertex *q, int interior_exterior)
229 229
         if (pi->intersect)
230 230
         {
231 231
             pi->entry_exit = status;
  232
+            pi->processed = 0;
232 233
             status = !status;
233 234
         }
234 235
     }
@@ -243,10 +244,10 @@ struct vertex * newPolygon(struct vertex *lastPoly, struct vertex *p)
243 244
     struct vertex *poly = (struct vertex *) malloc (sizeof(struct vertex));
244 245
     poly->x = p->x;
245 246
     poly->y = p->y;
246  
-//  poly->o = p->o;
  247
+    poly->o = p->o;
247 248
     poly->nextPoly = NULL;
248 249
     poly->next = NULL;
249  
-    
  250
+
250 251
     if (lastPoly)
251 252
         lastPoly->nextPoly = poly;
252 253
     return poly;
@@ -258,7 +259,7 @@ struct vertex * newPolygon(struct vertex *lastPoly, struct vertex *p)
258 259
   */
259 260
 void newVertex(struct vertex *last, struct vertex *p)
260 261
 {
261  
-    struct vertex *point = 
  262
+    struct vertex *point =
262 263
         last->next = (struct vertex *) malloc (sizeof(struct vertex));
263 264
     point->x = p->x;
264 265
     point->y = p->y;
@@ -266,7 +267,7 @@ void newVertex(struct vertex *last, struct vertex *p)
266 267
 
267 268
     point->next = NULL;
268 269
     point->nextPoly = NULL;
269  
-    
  270
+
270 271
 }
271 272
 
272 273
 
@@ -274,7 +275,7 @@ void newVertex(struct vertex *last, struct vertex *p)
274 275
   * Intersect the subject polygon using clip polygon and store into
275 276
   * polygons structure, returns the total number of polygons.
276 277
   */
277  
-int createClippedPolygon(struct vertex *lclip, struct vertex *lsubject, 
  278
+int createClippedPolygon(struct vertex *lclip, struct vertex *lsubject,
278 279
             struct vertex **polygons, int *total)
279 280
 {
280 281
     struct vertex *isubject = lsubject->next, *current;
@@ -284,33 +285,32 @@ int createClippedPolygon(struct vertex *lclip, struct vertex *lsubject,
284 285
 
285 286
     while (isubject)
286 287
     {
287  
-        for (; isubject != lsubject && !(isubject->intersect && !isubject->processed); 
  288
+        for (; isubject != lsubject && !(isubject->intersect && !isubject->processed);
288 289
                 isubject = isubject->next);
289 290
 
290  
-        if (isubject == lsubject) 
  291
+        if (isubject == lsubject)
291 292
             break;
292  
-        
  293
+
293 294
         isubject->processed = 1;
294 295
         current = isubject;
295  
-        
296  
-        if (first == NULL) 
  296
+
  297
+        if (first == NULL)
297 298
         {
298 299
             first = (struct vertex *) malloc (sizeof(struct vertex));
299 300
             first->x = current->x;
300 301
             first->y = current->y;
301  
-//          first->o = current->o;
  302
+            first->o = current->o;
302 303
             first->nextPoly = NULL;
303 304
             poly = first;
304 305
         }
305 306
         else
306 307
             poly = newPolygon(poly, current);
307  
-            
308  
-            
  308
+
309 309
         npolys++;
310 310
         nvertex++;
311  
-        
  311
+
312 312
         struct vertex *lastPoint = poly;
313  
-        
  313
+
314 314
         do {
315 315
             if (current->entry_exit == STATUS_ENTRY)
316 316
                 do {
@@ -326,13 +326,13 @@ int createClippedPolygon(struct vertex *lclip, struct vertex *lsubject,
326 326
                     lastPoint = lastPoint->next;
327 327
                     nvertex++;
328 328
                 } while (!current->intersect);
329  
-            
  329
+
330 330
             current->processed = 1;
331 331
             current = current->neighbour;
332 332
             current->processed = 1;
333 333
 
334 334
         } while (! (poly->x == current->x && poly->y == current->y) );
335  
-        
  335
+
336 336
 
337 337
     }
338 338
     if (polygons)
@@ -351,7 +351,7 @@ int createClippedPolygon(struct vertex *lclip, struct vertex *lsubject,
351 351
   * @polys: array of vertex for all the polygons
352 352
   * @lengths: array of lengths for each polygon
353 353
   */
354  
-void copy(struct vertex *polygons, int npolys, int nvertex, 
  354
+void copy(struct vertex *polygons, int npolys, int nvertex,
355 355
                 double **polysx, double **polysy, int ** origin, int **lengths)
356 356
 {
357 357
     double *px = (double *) malloc (nvertex * sizeof(double));
@@ -360,17 +360,23 @@ void copy(struct vertex *polygons, int npolys, int nvertex,
360 360
     int *ls = (int *) malloc (npolys * sizeof(int));
361 361
     int polycount = 0, vertexcount = 0;
362 362
     struct vertex *ipoly, *ivertex;
  363
+    //int i,offset;
  364
+    int curlen;
363 365
 
364 366
     for (ipoly = polygons; ipoly; ipoly = ipoly->nextPoly)
365 367
     {
366 368
         ls[polycount] = 0;
  369
+        curlen = 0;
367 370
         for (ivertex = ipoly; ivertex; ivertex = ivertex->next)
368 371
         {
369 372
             ls[polycount]++;
370 373
             po[vertexcount] = ivertex->o;
371 374
             px[vertexcount] = ivertex->x;
372 375
             py[vertexcount++] = ivertex->y;
  376
+            curlen++;
373 377
         }
  378
+        // Last (repeated) vertex copies its origin label to first one (which was initiall "flipped").
  379
+        po[vertexcount-curlen] = po[vertexcount-1];
374 380
         polycount++;
375 381
     }
376 382
     *polysx = px;
@@ -380,17 +386,17 @@ void copy(struct vertex *polygons, int npolys, int nvertex,
380 386
 }
381 387
 
382 388
 
383  
-// Perform clipping of the polygon clip with nc points against 
  389
+// Perform clipping of the polygon clip with nc points against
384 390
 // a subject with ns points. Returns a set of nl polygons with specified lengths
385 391
 // in an array of coordinates polys.
386  
-int clip(double *clipx, double *clipy, int nc, 
387  
-            double *subjectx, double *subjecty, int ns, 
388  
-                double **polysx, double **polysy, int **origin, int **lengths, int *nl, int op)
  392
+int clip(double *clipx, double *clipy, int nc,
  393
+            double *subjectx, double *subjecty, int ns,
  394
+                double **polysx, double **polysy, int **origin, int **lengths, int *nl, /*int *nlp, */int op)
389 395
 {
390 396
     struct vertex *lclip, *lsubject;
391  
-    struct vertex *polygons = NULL;
  397
+    struct vertex *polygons = NULL, *polygons2 = NULL, *auxpoly = NULL;
392 398
     int cIntExt, sIntExt;
393  
-    int nvertex, npolys;
  399
+    int nvertex, nvertex2, npolys, npolys2;
394 400
 
395 401
     // create data structures
396 402
     createList(1,clipx, clipy, nc, &lclip);
@@ -401,22 +407,62 @@ int clip(double *clipx, double *clipy, int nc,
401 407
     // phase one of the algorithm
402 408
     findIntersections(lclip, lsubject);
403 409
 
404  
-
405 410
     //printf("found intersections\n");
406 411
 
407  
-    // phase two of the algorithm
408  
-    cIntExt = op & (POLYGON_INTERSECTION | POLYGON_DIFF) ? 
409  
-                POLYGON_INTERIOR : POLYGON_EXTERIOR;
410  
-    sIntExt = op & POLYGON_INTERSECTION ? POLYGON_INTERIOR : POLYGON_EXTERIOR;
  412
+    switch(op) {
  413
+        case POLYGON_UNION:
  414
+            cIntExt = sIntExt = POLYGON_INTERIOR;
  415
+            break;
  416
+        case POLYGON_INTERSECTION:
  417
+            cIntExt = sIntExt = POLYGON_EXTERIOR;
  418
+            break;
  419
+        case POLYGON_DIFF_AB:
  420
+            cIntExt = POLYGON_EXTERIOR;
  421
+            sIntExt = POLYGON_INTERIOR;
  422
+            break;
  423
+        case POLYGON_XOREXT:
  424
+            cIntExt = POLYGON_EXTERIOR;
  425
+            sIntExt = POLYGON_INTERIOR;
  426
+            break;
  427
+        case POLYGON_DIFF_BA:
  428
+            cIntExt = POLYGON_INTERIOR;
  429
+            sIntExt = POLYGON_EXTERIOR;
  430
+            break;
  431
+        }
  432
+
411 433
     markEntries(lclip, lsubject, sIntExt);
412 434
     markEntries(lsubject, lclip, cIntExt);
413 435
 
414  
-
415 436
     //printf("marked entries\n");
416 437
 
417 438
     // phase three of the algorithm
418 439
     npolys = createClippedPolygon(lclip, lsubject, &polygons, &nvertex);
419 440
 
  441
+    if(op==POLYGON_XOREXT) {
  442
+        cIntExt = POLYGON_INTERIOR;
  443
+        sIntExt = POLYGON_EXTERIOR;
  444
+
  445
+        markEntries(lclip, lsubject, sIntExt);
  446
+        markEntries(lsubject, lclip, cIntExt);
  447
+
  448
+        npolys2 = createClippedPolygon(lclip, lsubject, &polygons2, &nvertex2);
  449
+
  450
+        // number of "positive" polygons:
  451
+        // *nlp = npolys;
  452
+
  453
+        npolys += npolys2;
  454
+        nvertex += nvertex2;
  455
+
  456
+        auxpoly = polygons;
  457
+        while(auxpoly->nextPoly)
  458
+            auxpoly = auxpoly->nextPoly;
  459
+        auxpoly->nextPoly = polygons2;
  460
+
  461
+    } else {
  462
+        // only xorext operation uses nlp:
  463
+        // *nlp = 0;
  464
+    }
  465
+
420 466
     //printf("clip polygon\n");
421 467
 
422 468
     // copy polygons into polys array
@@ -433,7 +479,7 @@ int clip(double *clipx, double *clipy, int nc,
433 479
     return 0;
434 480
 }
435 481
 
436  
-void readFromStdin(double **vclipx, double **vclipy, double **vsubjectx, 
  482
+void readFromStdin(double **vclipx, double **vclipy, double **vsubjectx,
437 483
                 double **vsubjecty, int *lclip, int *lsubject)
438 484
 {
439 485
     int nread = scanf("%d %d\n", lclip, lsubject);
@@ -467,7 +513,7 @@ int main2(void)
467 513
     int * origin;
468 514
     int *lengths, nl, i,j;
469 515
 
470  
-    clip(clipx, clipy, lclip, subjectx, subjecty, lsubject, 
  516
+    clip(clipx, clipy, lclip, subjectx, subjecty, lsubject,
471 517
             &polysx, &polysy, &origin, &lengths, &nl, POLYGON_INTERSECTION);
472 518
 
473 519
     int v = 0;
@@ -479,7 +525,7 @@ int main2(void)
479 525
             v++;
480 526
         }
481 527
     }
482  
-    
  528
+
483 529
     free(clipx);
484 530
     free(clipy);
485 531
     free(subjectx);
56  packages/contrib/clipping/src/Contours/Clipping/clipping.h
... ...
@@ -1,7 +1,7 @@
1 1
 /*******************************************************
2 2
  *******************************************************
3 3
  ** Efficient clipping of arbitrary polygons
4  
- ** Implementation by Adrián Amor Martíenz
  4
+ ** Implementation by Adrián Amor Martínez
5 5
  ** Extracted from G. Greiner, K. Hormann
6 6
  ** ACM Transactions on Graphics
7 7
  *******************************************************
@@ -9,41 +9,45 @@
9 9
 
10 10
 #define STATUS_ENTRY 1
11 11
 #define STATUS_EXIT 0
  12
+
12 13
 #define POLYGON_INTERIOR 1
13 14
 #define POLYGON_EXTERIOR 0
  15
+
14 16
 #define POLYGON_INTERSECTION 0x1
15  
-#define POLYGON_UNION 0x2
16  
-#define POLYGON_DIFF 0x4
  17
+#define POLYGON_UNION        0x2
  18
+#define POLYGON_DIFF_AB      0x4
  19
+#define POLYGON_XOREXT       0x8
  20
+#define POLYGON_DIFF_BA      0x16
17 21
 
18 22
 #include <stdlib.h>
19 23
 
20 24
 struct vertex {
21  
-	// coordinates
22  
-	double x;
23  
-	double y;
24  
-	
25  
-	// origin
26  
-	int o;
27  
-
28  
-	// doubly linked list
29  
-	struct vertex *next, *prev;
30  
-	struct vertex *nextVertex;
31  
-	struct vertex *nextPoly;
32  
-
33  
-	// used by the algorithm
34  
-	unsigned char intersect;
35  
-	unsigned char processed;
36  
-	char entry_exit;
37  
-	struct vertex *neighbour;
38  
-
39  
-	double alpha;
  25
+    // coordinates
  26
+    double x;
  27
+    double y;
  28
+
  29
+    // origin
  30
+    int o;
  31
+
  32
+    // doubly linked list
  33
+    struct vertex *next, *prev;
  34
+    struct vertex *nextVertex;
  35
+    struct vertex *nextPoly;
  36
+
  37
+    // used by the algorithm
  38
+    unsigned char intersect;
  39
+    unsigned char processed;
  40
+    char entry_exit;
  41
+    struct vertex *neighbour;
  42
+
  43
+    double alpha;
40 44
 };
41 45
 
42 46
 
43  
-// Perform clipping of the polygon clip with nc points against 
  47
+// Perform clipping of the polygon clip with nc points against
44 48
 // a subject with ns points. Returns a set of nl polygons with specified lengths
45 49
 // in an array of coordinates polys.
46  
-int clip(double *clipx, double *clipy, int nc, 
47  
-            double *subjectx, double *subjecty, int ns, 
48  
-                double **polysx, double **polysy, int **origin, int **lengths, int *nl, int op);
  50
+int clip(double *clipx, double *clipy, int nc,
  51
+         double *subjectx, double *subjecty, int ns,
  52
+         double **polysx, double **polysy, int **origin, int **lengths, int *nl, /*int*nlp,*/ int op);
49 53
 
15  packages/contrib/clipping/test/xorext.hs
@@ -15,7 +15,7 @@ win = browser "clipping" ds (const id)
15 15
            ++ map (msg "positive" . color blue . shOrig) zp
16 16
            ++ map (msg "negative" . color red  . shOrig) zn
17 17
 
18  
-
  18
+{-
19 19
 a = Closed [Point x1 y1, Point 0 y1, Point x2 y1, Point x2 y2, Point x1 y2]
20 20
   where
21 21
     x1 = 0.6; x2 = -x1; y1 = 0.4; y2 = -y1
@@ -25,7 +25,20 @@ b = Closed [ Point x1 y2,  Point x1 0, Point x1 y1, Point x2 y1, Point x2 y2
25 25
   where
26 26
     x1 = 0.4; x2 = -x1; x3 = 0.2; x4 = -x3;
27 27
     y1 = 0.8; y2 = -0.6; y3 = 0.6
  28
+-}
28 29
 
  30
+a = Closed [Point x1 y0, Point x8 y0, Point x8 y7, Point x6 y7, Point x6 y1
  31
+           , Point x4 y1, Point x4 y7, Point x3 y7, Point x3 y1, Point x2 y1
  32
+           , Point x2 y7, Point x1 y7 ]
  33
+  where
  34
+    x0 = 0.9; x1 = 0.8; x2 = 0.4; x3 = 0.2; x4 =  0.0; x5 = -0.2; x6 = -0.4; x7 = -0.6; x8 = -0.8; x9 = -0.9;
  35
+    y0 = 0.8; y1 = 0.4; y2 = 0.2; y3 = 0.0; y4 = -0.2; y5 = -0.4; y6 = -0.6; y7 = -0.8;
  36
+
  37
+b = Closed [Point x0 y2, Point x9 y2, Point x9 y6, Point x0 y6, Point x0 y5 
  38
+           , Point x7 y5, Point x7 y4, Point x5 y4, Point x5 y3, Point x0 y3]
  39
+  where
  40
+    x0 = 0.9; x1 = 0.8; x2 = 0.4; x3 = 0.2; x4 =  0.0; x5 = -0.2; x6 = -0.4; x7 = -0.6; x8 = -0.8; x9 = -0.9;
  41
+    y0 = 0.8; y1 = 0.4; y2 = 0.2; y3 = 0.0; y4 = -0.2; y5 = -0.4; y6 = -0.6; y7 = -0.8;
29 42
 
30 43
 (zp,zn) = partition ((<0).orientedArea.fst) (xorext a b)
31 44
 
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.