aboutsummaryrefslogtreecommitdiff
path: root/mesalib/src/glu/sgi/libnurbs/nurbtess/monoChain.cc
blob: cb28129adfb06276d98bd6f1b0fcbbe39284c70e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
/*
** License Applicability. Except to the extent portions of this file are
** made subject to an alternative license as permitted in the SGI Free
** Software License B, Version 1.1 (the "License"), the contents of this
** file are subject only to the provisions of the License. You may not use
** this file except in compliance with the License. You may obtain a copy
** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
** 
** http://oss.sgi.com/projects/FreeB
** 
** Note that, as provided in the License, the Software is distributed on an
** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
** PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
** 
** Original Code. The Original Code is: OpenGL Sample Implementation,
** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
** Copyright in any portions created by third parties is as indicated
** elsewhere herein. All Rights Reserved.
** 
** Additional Notice Provisions: The application programming interfaces
** established by SGI in conjunction with the Original Code are The
** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
** Window System(R) (Version 1.3), released October 19, 1998. This software
** was created using the OpenGL(R) version 1.2.1 Sample Implementation
** published by SGI, but has not been independently verified as being
** compliant with the OpenGL(R) version 1.2.1 Specification.
**
*/
/*
*/

#include "gluos.h"
#include <stdlib.h>
#include <stdio.h>
#include <GL/gl.h>

#include "glimports.h"
#include "zlassert.h"

#include "monoChain.h"
#include "quicksort.h"
#include "searchTree.h"
#include "polyUtil.h"

#ifndef max
#define max(a,b) ((a>b)? a:b)
#endif
#ifndef min
#define min(a,b) ((a>b)? b:a)
#endif

extern Int isCusp(directedLine *v);
extern Int deleteRepeatDiagonals(Int num_diagonals, directedLine** diagonal_vertices, directedLine** new_vertices);

//for debug purpose only
#if 0 // UNUSED
static void drawDiagonals(Int num_diagonals, directedLine** diagonal_vertices)
{
  Int i;
  for(i=0; i<num_diagonals; i++)
    {
      glBegin(GL_LINE);
      glVertex2fv(diagonal_vertices[2*i]->head());
      glVertex2fv(diagonal_vertices[2*i+1]->head());
      glEnd();
    }
}
#endif

/*given (x_1, y_1) and (x_2, y_2), and y
 *return x such that (x,y) is on the line
 */
inline Real intersectHoriz(Real x1, Real y1, Real x2, Real y2, Real y)
{
  return ((y2==y1)? (x1+x2)*0.5 : x1 + ((y-y1)/(y2-y1)) * (x2-x1));
}

//compare the heads of the two chains
static int compChainHeadInY(monoChain* mc1, monoChain* mc2)
{
  return compV2InY(mc1->getHead()->head(), mc2->getHead()->head());
}

monoChain::monoChain(directedLine* cHead, directedLine* cTail)
{
  chainHead = cHead;
  chainTail = cTail;
  next = this;
  prev = this;
  
  nextPolygon = NULL;

  //compute bounding box
  directedLine* temp;
  minX = maxX = chainTail->head()[0];
  minY = maxY = chainTail->head()[1];

  for(temp=chainHead; temp!=cTail; temp = temp->getNext())
    {
      if(temp->head()[0] < minX)
	minX = temp->head()[0];
      if(temp->head()[0] > maxX)
	maxX = temp->head()[0];

      if(temp->head()[1] < minY)
	minY = temp->head()[1];
      if(temp->head()[1] > maxY)
	maxY = temp->head()[1];
    }

  //check whether the chain is increasing or decreasing
  if(chainHead->compInY(chainTail) <0)
    isIncrease = 1;
  else
    isIncrease = 0;
  
  //initilize currrent, this is used for accelerating search
  if(isIncrease)
    current = chainHead;
  else
    current = chainTail;

  isKey = 0;
  keyY = 0;
}

//insert a new line between prev and this
void monoChain::insert(monoChain* nc)
{
  nc->next = this;
  nc->prev = prev;
  prev->next = nc;
  prev = nc;
}

void monoChain::deleteLoop()
{
  monoChain *temp, *tempNext;
  prev->next = NULL;
  for(temp=this; temp != NULL; temp = tempNext)
    {
      tempNext = temp->next;
      delete temp;
    }
}

void monoChain::deleteLoopList()
{
  monoChain *temp, *tempNext;
  for(temp=this; temp != NULL; temp = tempNext)
    {
      tempNext = temp->nextPolygon;
      temp->deleteLoop();
    }
}

Int monoChain::toArraySingleLoop(monoChain** array, Int index)
{
  monoChain *temp;
  array[index++] = this;
  for(temp = next; temp != this; temp = temp->next)
    {
      array[index++] = temp;
    }
  return index;
}

monoChain** monoChain::toArrayAllLoops(Int& num_chains)
{
  num_chains = numChainsAllLoops();
  monoChain **ret =  (monoChain**) malloc(sizeof(monoChain*) * num_chains);
  assert(ret);
  monoChain *temp;
  Int index = 0;
  for(temp = this; temp != NULL; temp=temp->nextPolygon){
    index = temp->toArraySingleLoop(ret, index);
  }
  return ret;
}

Int monoChain::numChainsSingleLoop()
{
  Int ret=0;
  monoChain* temp;
  if(next == this) return 1;
  ret = 1;
  for(temp=next; temp != this; temp = temp->next)
    ret++;
  return ret;
}

Int monoChain::numChainsAllLoops()
{
  Int ret=0;
  monoChain *temp;
  for(temp =this; temp != NULL; temp = temp->nextPolygon)
    ret += temp->numChainsSingleLoop();
  return ret;
}

//update 'current'
Real monoChain::chainIntersectHoriz(Real y)
{
  directedLine* temp;
  if(isIncrease)
    {
      for(temp= current; temp != chainTail; temp = temp->getNext())
	{
	  if(temp->head()[1] > y)
	    break;
	}
      current = temp->getPrev();
    }
  else 
    {
      for(temp = current; temp != chainHead; temp = temp->getPrev())
	{
	  if(temp->head()[1] > y)
	    break;
	}
      current = temp->getNext();
    }
  return intersectHoriz(current->head()[0], current->head()[1], current->tail()[0], current->tail()[1], y);
}

monoChain* directedLineLoopToMonoChainLoop(directedLine* loop)
{
  directedLine *temp;
  monoChain *ret=NULL;

  //find the first cusp
  directedLine *prevCusp=NULL;
  directedLine *firstCusp;

  if(isCusp(loop))
    prevCusp = loop;
  else
    {
      for(temp = loop->getNext(); temp != loop; temp = temp->getNext())
	if(isCusp(temp))
	  break;
      prevCusp = temp;
    }
  firstCusp = prevCusp;
//printf("first cusp is (%f,%f), (%f,%f), (%f,%f)\n", prevCusp->getPrev()->head()[0], prevCusp->getPrev()->head()[1], prevCusp->head()[0], prevCusp->head()[1], prevCusp->tail()[0], prevCusp->tail()[1]);

  for(temp = prevCusp->getNext(); temp != loop; temp = temp->getNext())
    {
      if(isCusp(temp))
	{
//printf("the cusp is (%f,%f), (%f,%f), (%f,%f)\n", temp->getPrev()->head()[0], temp->getPrev()->head()[1], temp->head()[0], temp->head()[1], temp->tail()[0], temp->tail()[1]);
	  if(ret == NULL)
	    {
	      ret = new monoChain(prevCusp, temp);
	    }
	  else
	    ret->insert(new monoChain(prevCusp, temp));
	  prevCusp = temp;	  
	}
    }
  assert(ret);
  ret->insert(new monoChain(prevCusp, firstCusp));

  return ret;
}

monoChain* directedLineLoopListToMonoChainLoopList(directedLine* list)
{
  directedLine* temp;
  monoChain* mc;
  monoChain* mcEnd;
  mc = directedLineLoopToMonoChainLoop(list);
  mcEnd = mc;  
  for(temp = list->getNextPolygon(); temp != NULL; temp = temp->getNextPolygon())
    {
      monoChain *newLoop = directedLineLoopToMonoChainLoop(temp);
      mcEnd->setNextPolygon(newLoop);
      mcEnd = newLoop;
    }
  return mc;
}

/*compare two edges of a polygon.
 *edge A < edge B if there is a horizontal line so that the intersection
 *with A is to the left of the intersection with B.
 *This function is used in sweepY for the dynamic search tree insertion to
 *order the edges.
 * Implementation: (x_1,y_1) and (x_2, y_2)
 */
static Int compEdges(directedLine *e1, directedLine *e2)
{
  Real* head1 = e1->head();
  Real* tail1 = e1->tail();
  Real* head2 = e2->head();
  Real* tail2 = e2->tail();
/*
  Real h10 = head1[0];
  Real h11 = head1[1];
  Real t10 = tail1[0];
  Real t11 = tail1[1];
  Real h20 = head2[0];
  Real h21 = head2[1];
  Real t20 = tail2[0];
  Real t21 = tail2[1];
*/
  Real e1_Ymax, e1_Ymin, e2_Ymax, e2_Ymin;
/*
  if(h11>t11) {
    e1_Ymax= h11;
    e1_Ymin= t11;
  }
  else{
    e1_Ymax = t11;
    e1_Ymin = h11;
  }

  if(h21>t21) {
    e2_Ymax= h21;
    e2_Ymin= t21;
  }
  else{
    e2_Ymax = t21;
    e2_Ymin = h21;
  }
*/
 
  if(head1[1]>tail1[1]) {
    e1_Ymax= head1[1];
    e1_Ymin= tail1[1];
  }
  else{
    e1_Ymax = tail1[1];
    e1_Ymin = head1[1];
  }

  if(head2[1]>tail2[1]) {
    e2_Ymax= head2[1];
    e2_Ymin= tail2[1];
  }
  else{
    e2_Ymax = tail2[1];
    e2_Ymin = head2[1];
  }

  
  /*Real e1_Ymax = max(head1[1], tail1[1]);*/ /*max(e1->head()[1], e1->tail()[1]);*/
  /*Real e1_Ymin = min(head1[1], tail1[1]);*/ /*min(e1->head()[1], e1->tail()[1]);*/
  /*Real e2_Ymax = max(head2[1], tail2[1]);*/ /*max(e2->head()[1], e2->tail()[1]);*/
  /*Real e2_Ymin = min(head2[1], tail2[1]);*/ /*min(e2->head()[1], e2->tail()[1]);*/

  Real Ymax = min(e1_Ymax, e2_Ymax);
  Real Ymin = max(e1_Ymin, e2_Ymin);
    
  Real y = 0.5*(Ymax + Ymin);

/*  Real x1 = intersectHoriz(e1->head()[0], e1->head()[1], e1->tail()[0], e1->tail()[1], y);
  Real x2 = intersectHoriz(e2->head()[0], e2->head()[1], e2->tail()[0], e2->tail()[1], y);
*/
/*
  Real x1 = intersectHoriz(h10, h11, t10, t11, y);
  Real x2 = intersectHoriz(h20, h21, t20, t21, y);
*/
  Real x1 = intersectHoriz(head1[0], head1[1], tail1[0], tail1[1], y);
  Real x2 = intersectHoriz(head2[0], head2[1], tail2[0], tail2[1], y);

  if(x1<= x2) return -1;
  else return 1;
}

Int  compChains(monoChain* mc1, monoChain* mc2)
{
  Real y;
  assert(mc1->isKey || mc2->isKey);
  if(mc1->isKey)
    y = mc1->keyY;
  else
    y = mc2->keyY;
  directedLine *d1 = mc1->find(y);
  directedLine *d2 = mc2->find(y);
  mc2->find(y);
//  Real x1 = mc1->chainIntersectHoriz(y);
//  Real x2 = mc2->chainIntersectHoriz(y);
  return   compEdges(d1, d2);
}

//this function modifies current for efficiency
directedLine* monoChain::find(Real y)
{
  directedLine *ret;
  directedLine *temp;
  assert(current->head()[1] <= y);
  if(isIncrease)
    {
      assert(chainTail->head()[1] >=y);
      for(temp=current; temp!=chainTail; temp = temp->getNext())
	{
	  if(temp->head()[1] > y)
	    break;
	}
      current = temp->getPrev();
      ret = current;
    }
  else
    {
      for(temp=current; temp != chainHead; temp = temp->getPrev())
	{
	  if(temp->head()[1] > y)
	    break;
	}
      current = temp->getNext();
      ret = temp;
    }
  return ret;  
}

void monoChain::printOneChain()
{
  directedLine* temp;
  for(temp = chainHead; temp != chainTail; temp = temp->getNext())
    {
      printf("(%f,%f) ", temp->head()[0], temp->head()[1]);
    }
  printf("(%f,%f) \n", chainTail->head()[0], chainTail->head()[1]);  
}

void monoChain::printChainLoop()
{
  monoChain* temp;
  this->printOneChain();
  for(temp = next; temp != this; temp = temp->next)
    {
      temp->printOneChain();
    }
  printf("\n");
}

void monoChain::printAllLoops()
{
  monoChain* temp;
  for(temp=this; temp != NULL; temp = temp->nextPolygon)
    temp->printChainLoop();
}

//return 1 if error occures
Int MC_sweepY(Int nVertices, monoChain** sortedVertices, sweepRange** ret_ranges)
{
  Int i;
  Real keyY;
  Int errOccur=0;
//printf("enter MC_sweepY\n");
//printf("nVertices=%i\n", nVertices);
  /*for each vertex in the sorted list, update the binary search tree.
   *and store the range information for each vertex.
   */
  treeNode* searchTree = NULL;
//printf("nVertices=%i\n", nVertices);
  for(i=0; i<nVertices; i++)
    {
      monoChain* vert = sortedVertices[i];
      keyY = vert->getHead()->head()[1]; //the sweep line
      directedLine *dline = vert->getHead();
      directedLine *dlinePrev = dline->getPrev();
      if(isBelow(dline, dline) && isBelow(dline, dlinePrev))
	{
//printf("case 1\n");
	  //this<v and prev < v
	  //delete both edges
	  vert->isKey = 1;
	  vert->keyY = keyY;
	  treeNode* thisNode = TreeNodeFind(searchTree, vert, (Int (*) (void *, void *))compChains);
	  vert->isKey = 0;

	  vert->getPrev()->isKey = 1;
	  vert->getPrev()->keyY = keyY;
	  treeNode* prevNode = TreeNodeFind(searchTree, vert->getPrev(), (Int (*) (void *, void *))compChains);
	  vert->getPrev()->isKey = 0;

	  if(cuspType(dline) == 1)//interior cusp
	    {

	      treeNode* leftEdge = TreeNodePredecessor(prevNode);
	      treeNode* rightEdge = TreeNodeSuccessor(thisNode);
	      if(leftEdge == NULL ||  rightEdge == NULL)
		{
		  errOccur = 1;
		  goto JUMP_HERE;
		}

	      directedLine* leftEdgeDline = ((monoChain* ) leftEdge->key)->find(keyY);



	      directedLine* rightEdgeDline = ((monoChain* ) rightEdge->key)->find(keyY);

	      ret_ranges[i] = sweepRangeMake(leftEdgeDline, 1, rightEdgeDline, 1);
	    }
	  else /*exterior cusp*/
	    {
	      ret_ranges[i] = sweepRangeMake( dline, 1, dlinePrev, 1);
	    }

	  searchTree = TreeNodeDeleteSingleNode(searchTree, thisNode);
	  searchTree = TreeNodeDeleteSingleNode(searchTree, prevNode);

	}
      else if(isAbove(dline, dline) && isAbove(dline, dlinePrev))
	{
//printf("case 2\n");
	  //insert both edges
	  treeNode* thisNode = TreeNodeMake(vert);
	  treeNode* prevNode = TreeNodeMake(vert->getPrev());
	  
	  vert->isKey = 1;
          vert->keyY = keyY;
	  searchTree = TreeNodeInsert(searchTree, thisNode, (Int (*) (void *, void *))compChains);
          vert->isKey = 0;

          vert->getPrev()->isKey = 1;
          vert->getPrev()->keyY = keyY;
	  searchTree = TreeNodeInsert(searchTree, prevNode, (Int (*) (void *, void *))compChains);
          vert->getPrev()->isKey = 0;

	  if(cuspType(dline) == 1) //interior cusp
	    {
//printf("cuspType is 1\n");
	      treeNode* leftEdge = TreeNodePredecessor(thisNode);
	      treeNode* rightEdge = TreeNodeSuccessor(prevNode);
              if(leftEdge == NULL || rightEdge == NULL)
		{
		  errOccur = 1;
		  goto JUMP_HERE;
		}
//printf("leftEdge is %i, rightEdge is %i\n", leftEdge, rightEdge);
	      directedLine* leftEdgeDline = ((monoChain*) leftEdge->key)->find(keyY);
	      directedLine* rightEdgeDline = ((monoChain*) rightEdge->key)->find(keyY);
	      ret_ranges[i] = sweepRangeMake( leftEdgeDline, 1, rightEdgeDline, 1);
	    }
	  else //exterior cusp
	    {
//printf("cuspType is not 1\n");
	      ret_ranges[i] = sweepRangeMake(dlinePrev, 1, dline, 1);
	    }
	}
      else
	{	  
//printf("%i,%i\n", isAbove(dline, dline), isAbove(dline, dlinePrev));
	  errOccur = 1;
	  goto JUMP_HERE;
      
	  fprintf(stderr, "error in MC_sweepY\n");
	  exit(1);
	}
    }

 JUMP_HERE:
  //finally clean up space: delete  the search tree
  TreeNodeDeleteWholeTree(searchTree);
  return errOccur;
}
	  
void MC_findDiagonals(Int total_num_edges, monoChain** sortedVertices,
		   sweepRange** ranges, Int& num_diagonals, 
		   directedLine** diagonal_vertices)
{
  Int i,j,k;
  k=0;
  //reset 'current' of all the monoChains
  for(i=0; i<total_num_edges; i++)
    sortedVertices[i]->resetCurrent();
  
  for(i=0; i<total_num_edges; i++)
    {
      directedLine* vert = sortedVertices[i]->getHead();
      directedLine* thisEdge = vert;
      directedLine* prevEdge = vert->getPrev();
      if(isBelow(vert, thisEdge) && isBelow(vert, prevEdge) && compEdges(prevEdge, thisEdge)<0)
	{
	  //this is an upward interior cusp
	  diagonal_vertices[k++] = vert;

	  directedLine* leftEdge = ranges[i]->left;
	  directedLine* rightEdge = ranges[i]->right;
	  
	  directedLine* leftVert = leftEdge;
	  directedLine* rightVert = rightEdge->getNext();
	  assert(leftVert->head()[1] >= vert->head()[1]);
	  assert(rightVert->head()[1] >= vert->head()[1]);
	  directedLine* minVert = (leftVert->head()[1] <= rightVert->head()[1])?leftVert:rightVert;
	  Int found = 0;
	  for(j=i+1; j<total_num_edges; j++)
	    {
	      if(sortedVertices[j]->getHead()->head()[1] > minVert->head()[1])
		break;
	      
	      if(sweepRangeEqual(ranges[i], ranges[j]))
		{
		  found = 1;
		  break;
		}
	    }

	  if(found)
	    diagonal_vertices[k++] = sortedVertices[j]->getHead();
	  else
	    diagonal_vertices[k++] = minVert;
	}	  
      else if(isAbove(vert, thisEdge) && isAbove(vert, prevEdge) && compEdges(prevEdge, thisEdge)>0)
	{
	  //downward interior cusp
	  diagonal_vertices[k++] = vert;
	  directedLine* leftEdge = ranges[i]->left;
	  directedLine* rightEdge = ranges[i]->right;
	  directedLine* leftVert = leftEdge->getNext();
	  directedLine* rightVert = rightEdge;
	  assert(leftVert->head()[1] <= vert->head()[1]);
	  assert(rightVert->head()[1] <= vert->head()[1]);
	  directedLine* maxVert = (leftVert->head()[1] > rightVert->head()[1])? leftVert:rightVert;
	  Int found=0;
	  for(j=i-1; j>=0; j--)
	    {
	      if(sortedVertices[j]->getHead()->head()[1] < maxVert->head()[1])
		break;
	      if(sweepRangeEqual(ranges[i], ranges[j]))
		{
		  found = 1;
		  break;
		}
	    }
	  if(found)
	    diagonal_vertices[k++] = sortedVertices[j]->getHead();
	  else
	    diagonal_vertices[k++] = maxVert;
	}
    }
  num_diagonals = k/2;
}
	  
	  
	    

directedLine* MC_partitionY(directedLine *polygons, sampledLine **retSampledLines)
{
//printf("enter mc_partitionY\n");
  Int total_num_chains = 0;
  monoChain* loopList = directedLineLoopListToMonoChainLoopList(polygons);
  monoChain** array = loopList->toArrayAllLoops(total_num_chains);

  if(total_num_chains<=2) //there is just one single monotone polygon
    {
      loopList->deleteLoopList();
      free(array); 
      *retSampledLines = NULL;
      return polygons;
    }

//loopList->printAllLoops();
//printf("total_num_chains=%i\n", total_num_chains);
  quicksort( (void**)array, 0, total_num_chains-1, (Int (*)(void*, void*))compChainHeadInY);
//printf("after quicksort\n");  

  sweepRange** ranges = (sweepRange**)malloc(sizeof(sweepRange*) * (total_num_chains));
  assert(ranges);

  if(MC_sweepY(total_num_chains, array, ranges))
    {
      loopList->deleteLoopList();
      free(array); 
      *retSampledLines = NULL;
      return NULL;
    }
//printf("after MC_sweepY\n");


  Int num_diagonals;
  /*number diagonals is < total_num_edges*total_num_edges*/
  directedLine** diagonal_vertices = (directedLine**) malloc(sizeof(directedLine*) * total_num_chains*2/*total_num_edges*/);
  assert(diagonal_vertices);

//printf("before call MC_findDiagonales\n");

  MC_findDiagonals(total_num_chains, array, ranges, num_diagonals, diagonal_vertices);
//printf("after call MC_findDia, num_diagnla=%i\n", num_diagonals);

  directedLine* ret_polygons = polygons;
  sampledLine* newSampledLines = NULL;
  Int i,k;

  num_diagonals=deleteRepeatDiagonals(num_diagonals, diagonal_vertices, diagonal_vertices);



//drawDiagonals(num_diagonals, diagonal_vertices);
//printf("diagoanls are \n");
//for(i=0; i<num_diagonals; i++)
//  {
//    printf("(%f,%f)\n", diagonal_vertices[2*i]->head()[0], diagonal_vertices[2*i]->head()[1]);
//    printf("**(%f,%f)\n", diagonal_vertices[2*i+1]->head()[0], diagonal_vertices[2*i+1]->head()[1]);
//  }

  Int *removedDiagonals=(Int*)malloc(sizeof(Int) * num_diagonals);
  for(i=0; i<num_diagonals; i++)
    removedDiagonals[i] = 0;
//  printf("first pass\n");


 for(i=0,k=0; i<num_diagonals; i++,k+=2)
    {


      directedLine* v1=diagonal_vertices[k];
      directedLine* v2=diagonal_vertices[k+1];
      directedLine* ret_p1;
      directedLine* ret_p2;
      
      /*we ahve to determine whether v1 and v2 belong to the same polygon before
       *their structure are modified by connectDiagonal().
       */
/*
      directedLine *root1 = v1->findRoot();
      directedLine *root2 = v2->findRoot();
      assert(root1);      
      assert(root2);
*/

directedLine*  root1 = v1->rootLinkFindRoot();
directedLine*  root2 = v2->rootLinkFindRoot();

      if(root1 != root2)
	{

	  removedDiagonals[i] = 1;
	  sampledLine* generatedLine;



	  v1->connectDiagonal(v1,v2, &ret_p1, &ret_p2, &generatedLine, ret_polygons);



	  newSampledLines = generatedLine->insert(newSampledLines);
/*
	  ret_polygons = ret_polygons->cutoffPolygon(root1);

	  ret_polygons = ret_polygons->cutoffPolygon(root2);
	  ret_polygons = ret_p1->insertPolygon(ret_polygons);
root1->rootLinkSet(ret_p1);
root2->rootLinkSet(ret_p1);
ret_p1->rootLinkSet(NULL);
ret_p2->rootLinkSet(ret_p1);
*/
	  ret_polygons = ret_polygons->cutoffPolygon(root2);



root2->rootLinkSet(root1);
ret_p1->rootLinkSet(root1);
ret_p2->rootLinkSet(root1);

       /*now that we have connected the diagonal v1 and v2, 
        *we have to check those unprocessed diagonals which 
        *have v1 or v2 as an end point. Notice that the head of v1
        *has the same coodinates as the head of v2->prev, and the head of
        *v2 has the same coordinate as the head of v1->prev. 
        *Suppose these is a diagonal (v1, x). If (v1,x) is still a valid
        *diagonal, then x should be on the left hand side of the directed line:        *v1->prev->head -- v1->head -- v1->tail. Otherwise, (v1,x) should be  
        *replaced by (v2->prev, x), that is, x is on the left of 
        * v2->prev->prev->head, v2->prev->head, v2->prev->tail.
        */
        Int ii, kk;
        for(ii=0, kk=0; ii<num_diagonals; ii++, kk+=2)
	  if( removedDiagonals[ii]==0)
	    {
	      directedLine* d1=diagonal_vertices[kk];
	      directedLine* d2=diagonal_vertices[kk+1];
	      /*check d1, and replace diagonal_vertices[kk] if necessary*/
	      if(d1 == v1) {
		/*check if d2 is to left of v1->prev->head:v1->head:v1->tail*/
		if(! pointLeft2Lines(v1->getPrev()->head(), 
				     v1->head(), v1->tail(), d2->head()))
		  {
/*
		    assert(pointLeft2Lines(v2->getPrev()->getPrev()->head(),
					   v2->getPrev()->head(), 
					   v2->getPrev()->tail(), d2->head()));
*/
		    diagonal_vertices[kk] = v2->getPrev();
		  }
	      }
	      if(d1 == v2) {
		/*check if d2 is to left of v2->prev->head:v2->head:v2->tail*/
		if(! pointLeft2Lines(v2->getPrev()->head(),
				     v2->head(), v2->tail(), d2->head()))
		  {
/*
		    assert(pointLeft2Lines(v1->getPrev()->getPrev()->head(),
					   v1->getPrev()->head(),
					   v1->getPrev()->tail(), d2->head()));
*/
		    diagonal_vertices[kk] = v1->getPrev();
		  }
	      }
	      /*check d2 and replace diagonal_vertices[k+1] if necessary*/
	      if(d2 == v1) {
		/*check if d1 is to left of v1->prev->head:v1->head:v1->tail*/
		if(! pointLeft2Lines(v1->getPrev()->head(), 
				     v1->head(), v1->tail(), d1->head()))
		  {
/*		    assert(pointLeft2Lines(v2->getPrev()->getPrev()->head(),
					   v2->getPrev()->head(), 
					   v2->getPrev()->tail(), d1->head()));
*/
		    diagonal_vertices[kk+1] = v2->getPrev();
		  }
	      }
	      if(d2 == v2) {
		/*check if d1 is to left of v2->prev->head:v2->head:v2->tail*/
		if(! pointLeft2Lines(v2->getPrev()->head(),
				     v2->head(), v2->tail(), d1->head()))
		  {
/*		    assert(pointLeft2Lines(v1->getPrev()->getPrev()->head(),
					   v1->getPrev()->head(),
					   v1->getPrev()->tail(), d1->head()));
*/
		    diagonal_vertices[kk+1] = v1->getPrev();
		  }
	      }
	    }					    	       
}/*end if (root1 not equal to root 2)*/
}

  /*second pass,  now all diagoals should belong to the same polygon*/
//printf("second pass: \n");

//  for(i=0; i<num_diagonals; i++)
//    printf("%i ", removedDiagonals[i]);


  for(i=0,k=0; i<num_diagonals; i++, k += 2)
    if(removedDiagonals[i] == 0) 
      {


	directedLine* v1=diagonal_vertices[k];
	directedLine* v2=diagonal_vertices[k+1];



	directedLine* ret_p1;
	directedLine* ret_p2;

	/*we ahve to determine whether v1 and v2 belong to the same polygon before
	 *their structure are modified by connectDiagonal().
	 */
	directedLine *root1 = v1->findRoot();
/*
	directedLine *root2 = v2->findRoot();



	assert(root1);      
	assert(root2);      
	assert(root1 == root2);
  */    
	sampledLine* generatedLine;



	v1->connectDiagonal(v1,v2, &ret_p1, &ret_p2, &generatedLine, ret_polygons);
	newSampledLines = generatedLine->insert(newSampledLines);

	ret_polygons = ret_polygons->cutoffPolygon(root1);

	ret_polygons = ret_p1->insertPolygon(ret_polygons);

	ret_polygons = ret_p2->insertPolygon(ret_polygons);



	for(Int j=i+1; j<num_diagonals; j++) 
	  {
	    if(removedDiagonals[j] ==0)
	      {

		directedLine* temp1=diagonal_vertices[2*j];
		directedLine* temp2=diagonal_vertices[2*j+1];
               if(temp1==v1 || temp1==v2 || temp2==v1 || temp2==v2)
		if(! temp1->samePolygon(temp1, temp2))
		  {
		    /*if temp1 and temp2 are in different polygons, 
		     *then one of them must be v1 or v2.
		     */



		    assert(temp1==v1 || temp1 == v2 || temp2==v1 || temp2 ==v2);
		    if(temp1==v1) 
		      {
			diagonal_vertices[2*j] = v2->getPrev();
		      }
		    if(temp2==v1)
		      {
			diagonal_vertices[2*j+1] = v2->getPrev();
		      }
		    if(temp1==v2)
		      {
			diagonal_vertices[2*j] = v1->getPrev();		      
		      }
		    if(temp2==v2)
		      {
			diagonal_vertices[2*j+1] = v1->getPrev();
		      }
		  }
	      }
	  }      

      }


  //clean up
  loopList->deleteLoopList();
  free(array);
  free(ranges);
  free(diagonal_vertices);
  free(removedDiagonals);

  *retSampledLines = newSampledLines;
  return ret_polygons;
}