summaryrefslogtreecommitdiffstats
path: root/src/glu/sgi/libnurbs/nurbtess/sampleCompTop.cc
blob: b7b929623a2f917e35f2bd3ff88e56d41029ac25 (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
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
/*
** 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 <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "zlassert.h"
#include "sampleCompTop.h"
#include "sampleCompRight.h"

#define max(a,b) ((a>b)? a:b)

//return : index_small, and index_large,
//from [small, large] is strictly U-monotne,
//from [large+1, end] is <u
//and vertex[large][0] is >= u
//if eveybody is <u, the large = start-1.
//otherwise both large and small are meaningful and we have start<=small<=large<=end
void findTopLeftSegment(vertexArray* leftChain,
                        Int leftStart,
                        Int leftEnd,
                        Real u,
                        Int& ret_index_small,
                        Int& ret_index_large
                        )
{
  Int i;
  assert(leftStart <= leftEnd);
  for(i=leftEnd; i>= leftStart; i--)
    {
      if(leftChain->getVertex(i)[0] >= u)
        break;
    }
  ret_index_large = i;
  if(ret_index_large >= leftStart)
    {
      for(i=ret_index_large; i>leftStart; i--)
        {
          if(leftChain->getVertex(i-1)[0] <= leftChain->getVertex(i)[0])
            break;
        }
      ret_index_small = i;
    }
}

void findTopRightSegment(vertexArray* rightChain,
                         Int rightStart,
                         Int rightEnd,
                         Real u,
                         Int& ret_index_small,
                         Int& ret_index_large)
{
  Int i;
  assert(rightStart<=rightEnd);
  for(i=rightEnd; i>=rightStart; i--)
    {
      if(rightChain->getVertex(i)[0] <= u)
        break;
    }
  ret_index_large = i;
  if(ret_index_large >= rightStart)
    {
      for(i=ret_index_large; i>rightStart;i--)
        {
          if(rightChain->getVertex(i-1)[0] >= rightChain->getVertex(i)[0])           
	    break;
        }
      ret_index_small = i;
    }
}


void sampleTopRightWithGridLinePost(Real* topVertex,
				   vertexArray* rightChain,
				   Int rightStart,
				   Int segIndexSmall,
				   Int segIndexLarge,
				   Int rightEnd,
				   gridWrap* grid,
				   Int gridV,
				   Int leftU,
				   Int rightU,
				   primStream* pStream)
{
  //the possible section which is to the right of rightU
  if(segIndexLarge < rightEnd)
    {
      Real *tempTop;
      if(segIndexLarge >= rightStart)
        tempTop = rightChain->getVertex(segIndexLarge);
      else
        tempTop = topVertex;
      Real tempBot[2];
      tempBot[0] = grid->get_u_value(rightU);
      tempBot[1] = grid->get_v_value(gridV);
monoTriangulationRecGenOpt(tempTop, tempBot,
			   NULL, 1,0,
			   rightChain, segIndexLarge+1, rightEnd,
			   pStream);
/*
      monoTriangulation2(tempTop, tempBot,
                         rightChain,
                         segIndexLarge+1,
                         rightEnd,
                         0, //a decrease  chian
                         pStream);
*/

    }
  
  //the possible section which is strictly Umonotone
  if(segIndexLarge >= rightStart)
    {
      stripOfFanRight(rightChain, segIndexLarge, segIndexSmall, grid, gridV, leftU, rightU, pStream, 0);
      Real tempBot[2];
      tempBot[0] = grid->get_u_value(leftU);
      tempBot[1] = grid->get_v_value(gridV);
      monoTriangulation2(topVertex, tempBot, rightChain, rightStart, segIndexSmall, 0, pStream);
    }
  else //the topVertex forms a fan with the grid points
    grid->outputFanWithPoint(gridV, leftU, rightU, topVertex, pStream);
}

void sampleTopRightWithGridLine(Real* topVertex,
                                vertexArray* rightChain,
                                Int rightStart,
                                Int rightEnd,
                                gridWrap* grid,
                                Int gridV,
                                Int leftU,
                                Int rightU,
                                primStream* pStream
                                )
{
  //if right chian is empty, then there is only one topVertex with one grid line
  if(rightEnd < rightStart){
    grid->outputFanWithPoint(gridV, leftU, rightU, topVertex, pStream);
    return;
  }

  Int segIndexSmall, segIndexLarge;
  findTopRightSegment(rightChain,
                      rightStart,
                      rightEnd,
                      grid->get_u_value(rightU),
                      segIndexSmall,
                      segIndexLarge
                      );
  sampleTopRightWithGridLinePost(topVertex, rightChain,
				 rightStart,
				 segIndexSmall,
				 segIndexLarge,
				 rightEnd,
				 grid,
				 gridV,
				 leftU,
				 rightU,
				 pStream);
}


void sampleTopLeftWithGridLinePost(Real* topVertex,
				   vertexArray* leftChain,
				   Int leftStart,
				   Int segIndexSmall,
				   Int segIndexLarge,
				   Int leftEnd,
				   gridWrap* grid,
				   Int gridV,
				   Int leftU,
				   Int rightU,
				   primStream* pStream)
{
  //the possible section which is to the left of leftU

  if(segIndexLarge < leftEnd)
    {
      Real *tempTop;
      if(segIndexLarge >= leftStart)
        tempTop = leftChain->getVertex(segIndexLarge);
      else
        tempTop = topVertex;
      Real tempBot[2];
      tempBot[0] = grid->get_u_value(leftU);
      tempBot[1] = grid->get_v_value(gridV);

      monoTriangulation2(tempTop, tempBot,
                         leftChain,
                         segIndexLarge+1,
                         leftEnd,
                         1, //a increase  chian
                         pStream);
    }

  //the possible section which is strictly Umonotone
  if(segIndexLarge >= leftStart)
    {
      //if there are grid points which are to the right of topV,
      //then we should use topVertex to form a fan with these points to
      //optimize the triangualtion
      int do_optimize=1;
      if(topVertex[0] >= grid->get_u_value(rightU))
	do_optimize = 0;
      else
	{
	  //we also have to make sure that topVertex are the right most vertex
          //on the chain.
	  int i;
	  for(i=leftStart; i<=segIndexSmall; i++)
	    if(leftChain->getVertex(i)[0] >= topVertex[0])
	      {
		do_optimize = 0;
		break;
	      }
	}

      if(do_optimize)
	{
	  //find midU so that grid->get_u_value(midU) >= topVertex[0]
	  //and               grid->get_u_value(midU-1) < topVertex[0]
	  int midU=rightU;
	  while(grid->get_u_value(midU) >= topVertex[0])
	    {
	      midU--;
	      if(midU < leftU)
		break;
	    }
	  midU++;

	  grid->outputFanWithPoint(gridV, midU, rightU, topVertex, pStream);
	  stripOfFanLeft(leftChain, segIndexLarge, segIndexSmall, grid, gridV, leftU, midU, pStream, 0);
	  Real tempBot[2];
	  tempBot[0] = grid->get_u_value(midU);
	  tempBot[1] = grid->get_v_value(gridV);
	  monoTriangulation2(topVertex, tempBot, leftChain, leftStart, segIndexSmall, 1, pStream);      
	}
      else //not optimize
	{

	  stripOfFanLeft(leftChain, segIndexLarge, segIndexSmall, grid, gridV, leftU, rightU, pStream, 0);
	  Real tempBot[2];
	  tempBot[0] = grid->get_u_value(rightU);
	  tempBot[1] = grid->get_v_value(gridV);
	  monoTriangulation2(topVertex, tempBot, leftChain, leftStart, segIndexSmall, 1, pStream);
	}
    }
  else //the topVertex forms a fan with the grid points
    grid->outputFanWithPoint(gridV, leftU, rightU, topVertex, pStream);  
}				   
				   

void sampleTopLeftWithGridLine(Real* topVertex,
                                vertexArray* leftChain,
                                Int leftStart,
                                Int leftEnd,
                                gridWrap* grid,
                                Int gridV,
                                Int leftU,
                                Int rightU,
                                primStream* pStream
                                )
{
  Int segIndexSmall, segIndexLarge;
  //if left chain is empty, then there is only one top vertex with one grid 
  //  line
  if(leftEnd < leftStart) {
    grid->outputFanWithPoint(gridV, leftU, rightU, topVertex, pStream);
    return;
  }
  findTopLeftSegment(leftChain,
                      leftStart,
                      leftEnd,
                      grid->get_u_value(leftU),
                      segIndexSmall,
                      segIndexLarge
                      );
  sampleTopLeftWithGridLinePost(topVertex,
				leftChain,
				leftStart,
				segIndexSmall,
				segIndexLarge,
				leftEnd,
				grid,
				gridV,
			        leftU,
				rightU,
				pStream);   
}
 
                
//return 1 if saprator exits, 0 otherwise
Int findTopSeparator(vertexArray* leftChain,
		     Int leftStartIndex,
		     Int leftEndIndex,
		     vertexArray* rightChain,
		     Int rightStartIndex,
		     Int rightEndIndex,
		     Int& ret_sep_left,
		     Int& ret_sep_right)
{
  
  Int oldLeftI, oldRightI, newLeftI, newRightI;
  Int i,j,k;
  Real leftMax /*= leftChain->getVertex(leftEndIndex)[0]*/;
  Real rightMin /*= rightChain->getVertex(rightEndIndex)[0]*/;
  if(leftChain->getVertex(leftEndIndex)[1] > rightChain->getVertex(rightEndIndex)[1]) //left higher
    {
      oldLeftI = leftEndIndex+1;
      oldRightI = rightEndIndex;
      leftMax =  leftChain->getVertex(leftEndIndex)[0] - Real(1.0); //initilza to left of leftU
      rightMin = rightChain->getVertex(rightEndIndex)[0];
    }
  else
    {
      oldLeftI = leftEndIndex;
      oldRightI = rightEndIndex+1;
      leftMax =  leftChain->getVertex(leftEndIndex)[0]; 
      rightMin = rightChain->getVertex(rightEndIndex)[0] + Real(1.0);      
    }
  
  //i: the current working leftChain index, 
  //j: the current working rightChain index,
  //if left(i) is higher than right(j), then the two chains beloew right(j) are separated.
  //else the two chains below left(i) are separeated.
  i=leftEndIndex; 
  j=rightEndIndex;
  while(1)
    {
      newLeftI = oldLeftI;
      newRightI = oldRightI;

      if(i<leftStartIndex) //left chain is done, go through remining right chain.
	{
	  for(k=j-1; k>= rightStartIndex; k--)
	    {
	      if(rightChain->getVertex(k)[0] > leftMax) //no conflict
		{
		  //update oldRightI if necessary
		  if(rightChain->getVertex(k)[0] < rightMin)
		    {
		      rightMin = rightChain->getVertex(k)[0];
		      oldRightI = k;
		    }
		}
	      else  //there is a conflict
		break; //the for-loop. below right(k-1) is seperated: oldLeftI, oldRightI.
	    }
	  break; //the while loop
	}
      else if(j<rightStartIndex) //rightChain is done
	{
	  for(k=i-1; k>= leftStartIndex; k--)
	    {
	      if(leftChain->getVertex(k)[0] < rightMin) //no conflict
		{
		  //update oldLeftI if necessary
		  if(leftChain->getVertex(k)[0] > leftMax)
		    {
		      leftMax = leftChain->getVertex(k)[0];
		      oldLeftI = k;
		    }
		}
	      else //there is a conflict
		break; //the for loop
	    }
	  break; //the while loop
	}
      else if(leftChain->getVertex(i)[1] > rightChain->getVertex(j)[1]) //left hgiher
	{
	  if(leftChain->getVertex(i)[0] > leftMax) //update leftMax and newLeftI.
	    {
	      leftMax = leftChain->getVertex(i)[0];	     
	      newLeftI = i;
	    }
	  for(k=j-1; k>= rightStartIndex; k--) //update rightMin and newRightI.
	    {
	      if(rightChain->getVertex(k)[1] > leftChain->getVertex(i)[1])
		break;
	      if(rightChain->getVertex(k)[0] < rightMin)
		{
		  rightMin = rightChain->getVertex(k)[0];
		  newRightI = k;
		}
	    }
	  j = k; //next working j, since j will be higher than i in next loop
	  if(leftMax >= rightMin) //there is a conflict
	    break;
	  else //still no conflict
	    {
	      oldLeftI = newLeftI;
	      oldRightI = newRightI;
	    }
	}
      else //right higher
	{
	  if(rightChain->getVertex(j)[0] < rightMin)
	    {
	      rightMin = rightChain->getVertex(j)[0];
	      newRightI = j;
	    }
	  for(k=i-1; k>= leftStartIndex; k--)
	    {
	      if(leftChain->getVertex(k)[1] > rightChain->getVertex(j)[1])
		break;
	      if(leftChain->getVertex(k)[0] > leftMax)
		{
		  leftMax = leftChain->getVertex(k)[0];
		  newLeftI = k;
		}
	    }
	  i = k; //next working i, since i will be higher than j next loop
	  
	  if(leftMax >= rightMin) //there is a conflict
	    break;
	  else //still no conflict
	    {
	      oldLeftI = newLeftI;
	      oldRightI = newRightI;
	    }
	}
    }//end of while loop
  //now oldLeftI and oldRightI are the desired separeator index, notice that there are not necessarily valid
  if(oldLeftI > leftEndIndex || oldRightI > rightEndIndex)
    return 0;
  else
    {
      ret_sep_left = oldLeftI;
      ret_sep_right = oldRightI;
      return 1;
    }
}

        
void sampleCompTop(Real* topVertex,
                   vertexArray* leftChain,
                   Int leftStartIndex,
                   vertexArray* rightChain,
                   Int rightStartIndex,
                   gridBoundaryChain* leftGridChain,
                   gridBoundaryChain* rightGridChain,
                   Int gridIndex1,
                   Int up_leftCornerWhere,
                   Int up_leftCornerIndex,
                   Int up_rightCornerWhere,
                   Int up_rightCornerIndex,
                   primStream* pStream)
{
  if(up_leftCornerWhere == 1 && up_rightCornerWhere == 1) //the top is topVertex with possible grid points
    {
      leftGridChain->getGrid()->outputFanWithPoint(leftGridChain->getVlineIndex(gridIndex1),
						   leftGridChain->getUlineIndex(gridIndex1),
						   rightGridChain->getUlineIndex(gridIndex1),
						   topVertex,
						   pStream);
      return;
    }

  else if(up_leftCornerWhere != 0)
    {
      Real* tempTop;
      Int tempRightStart;
      if(up_leftCornerWhere == 1){
	tempRightStart = rightStartIndex;
	tempTop = topVertex;
      }
      else
	{
	  tempRightStart = up_leftCornerIndex+1;
	  tempTop = rightChain->getVertex(up_leftCornerIndex);
	}
      sampleTopRightWithGridLine(tempTop, rightChain, tempRightStart, up_rightCornerIndex,
				 rightGridChain->getGrid(),
				 leftGridChain->getVlineIndex(gridIndex1),
				 leftGridChain->getUlineIndex(gridIndex1),
				 rightGridChain->getUlineIndex(gridIndex1),
				 pStream);
    }
  else if(up_rightCornerWhere != 2)
    {
      Real* tempTop;
      Int tempLeftStart;
      if(up_rightCornerWhere == 1)
	{
	  tempLeftStart = leftStartIndex;
	  tempTop = topVertex;
	}
      else //0
	{
	  tempLeftStart = up_rightCornerIndex+1;
	  tempTop = leftChain->getVertex(up_rightCornerIndex);
	}
/*
      sampleTopLeftWithGridLine(tempTop, leftChain, tempLeftStart, up_leftCornerIndex,
				leftGridChain->getGrid(),
				 leftGridChain->getVlineIndex(gridIndex1),
				 leftGridChain->getUlineIndex(gridIndex1),
				 rightGridChain->getUlineIndex(gridIndex1),
				 pStream);
*/
      sampleCompTopSimple(topVertex,
			  leftChain,
			  leftStartIndex,
			  rightChain,
			  rightStartIndex,
			  leftGridChain,
			  rightGridChain,
			  gridIndex1,
			  up_leftCornerWhere,
			  up_leftCornerIndex,
			  up_rightCornerWhere,
			  up_rightCornerIndex,
			  pStream);	      	      
    }
  else //up_leftCornerWhere == 0, up_rightCornerWhere == 2.
    {
      sampleCompTopSimple(topVertex,
			  leftChain,
			  leftStartIndex,
			  rightChain,
			  rightStartIndex,
			  leftGridChain,
			  rightGridChain,
			  gridIndex1,
			  up_leftCornerWhere,
			  up_leftCornerIndex,
			  up_rightCornerWhere,
			  up_rightCornerIndex,
			  pStream);	      	      
      return;
#ifdef NOT_REACHABLE //code is not reachable, for test purpose only
      //the following code is trying to do some optimization, but not quite working, also see sampleCompBot.C:
      Int sep_left, sep_right;
      if(findTopSeparator(leftChain,
			  leftStartIndex,
			  up_leftCornerIndex,
			  rightChain,
			  rightStartIndex,
			  up_rightCornerIndex,
			  sep_left,
			  sep_right)
	 ) //separator exists
	{

	  if( leftChain->getVertex(sep_left)[0] >= leftGridChain->get_u_value(gridIndex1) &&
	     rightChain->getVertex(sep_right)[0] <= rightGridChain->get_u_value(gridIndex1))
	    {
	      Int gridSep;
	      Int segLeftSmall, segLeftLarge, segRightSmall, segRightLarge;
	      Int valid=1; //whether the gridStep is valid or not.
	      findTopLeftSegment(leftChain,
				 sep_left,
				 up_leftCornerIndex,
				 leftGridChain->get_u_value(gridIndex1),
				 segLeftSmall,
				 segLeftLarge);
	      findTopRightSegment(rightChain,
				 sep_right,
				 up_rightCornerIndex,
				 rightGridChain->get_u_value(gridIndex1),
				 segRightSmall,
				 segRightLarge);
	      if(leftChain->getVertex(segLeftSmall)[1] >= rightChain->getVertex(segRightSmall)[1])
		{
		  gridSep = rightGridChain->getUlineIndex(gridIndex1);
		  while(leftGridChain->getGrid()->get_u_value(gridSep) > leftChain->getVertex(segLeftSmall)[0])
		    gridSep--;
		  if(segLeftSmall<segLeftLarge)
		    if(leftGridChain->getGrid()->get_u_value(gridSep) < leftChain->getVertex(segLeftSmall+1)[0])
		      {
			valid = 0;
		      }
		}
	      else
		{
		  gridSep = leftGridChain->getUlineIndex(gridIndex1);
		  while(leftGridChain->getGrid()->get_u_value(gridSep) < rightChain->getVertex(segRightSmall)[0])
		    gridSep++;
		  if(segRightSmall<segRightLarge)
		    if(leftGridChain->getGrid()->get_u_value(gridSep) > rightChain->getVertex(segRightSmall+1)[0])
		      {
			valid = 0;
		      }
		}		
		  
	      if(! valid)
		{
		  sampleCompTopSimple(topVertex,
				      leftChain,
				      leftStartIndex,
				      rightChain,
				      rightStartIndex,
				      leftGridChain,
				      rightGridChain,
				      gridIndex1,
				      up_leftCornerWhere,
				      up_leftCornerIndex,
				      up_rightCornerWhere,
				      up_rightCornerIndex,
				      pStream);	      	      
		}
	      else
		{
		  sampleTopLeftWithGridLinePost(leftChain->getVertex(segLeftSmall),
						leftChain,
						segLeftSmall+1,
						segLeftSmall+1,
						segLeftLarge,
						up_leftCornerIndex,
						leftGridChain->getGrid(),
						leftGridChain->getVlineIndex(gridIndex1),
						leftGridChain->getUlineIndex(gridIndex1),
						gridSep,
						pStream);
		  sampleTopRightWithGridLinePost(rightChain->getVertex(segRightSmall),
						 rightChain,
						 segRightSmall+1,
						 segRightSmall+1,
						 segRightLarge,
						 up_rightCornerIndex,
						 leftGridChain->getGrid(),
						 leftGridChain->getVlineIndex(gridIndex1),
						 gridSep,
						 rightGridChain->getUlineIndex(gridIndex1),
						 pStream);
		  Real tempBot[2];
		  tempBot[0] = leftGridChain->getGrid()->get_u_value(gridSep);
		  tempBot[1] = leftGridChain->get_v_value(gridIndex1);
		  monoTriangulationRecGen(topVertex, tempBot,
					  leftChain, leftStartIndex, segLeftSmall,
					  rightChain, rightStartIndex, segRightSmall,
					  pStream);
		}
	    }//end if both sides have vetices inside the gridboundary points
	  else if(leftChain->getVertex(sep_left)[0] >= leftGridChain->get_u_value(gridIndex1)) //left is in, right is nout
	    {

	      Int segLeftSmall, segLeftLarge;
	      findTopLeftSegment(leftChain,
				 sep_left,
				 up_leftCornerIndex,
				 leftGridChain->get_u_value(gridIndex1),
				 segLeftSmall,
				 segLeftLarge);	      
	      assert(segLeftLarge >= sep_left); 
              monoTriangulation2(leftChain->getVertex(segLeftLarge),
				 leftGridChain->get_vertex(gridIndex1),
				 leftChain,
				 segLeftLarge+1,
				 up_leftCornerIndex,
				 1, //a increase chain,
				 pStream);

	      stripOfFanLeft(leftChain, segLeftLarge, segLeftSmall, 
			     leftGridChain->getGrid(),
			     leftGridChain->getVlineIndex(gridIndex1),
			     leftGridChain->getUlineIndex(gridIndex1),
			     rightGridChain->getUlineIndex(gridIndex1),
			     pStream, 0);


	      monoTriangulationRecGen(topVertex, rightGridChain->get_vertex(gridIndex1),
				      leftChain, leftStartIndex, segLeftSmall,
				      rightChain, rightStartIndex, up_rightCornerIndex,
				      pStream);	    
	    }//end left in right out
	  else if(rightChain->getVertex(sep_right)[0] <= rightGridChain->get_u_value(gridIndex1))
	    {
	      Int segRightSmall, segRightLarge;
	      findTopRightSegment(rightChain,
				 sep_right,
				 up_rightCornerIndex,
				 rightGridChain->get_u_value(gridIndex1),
				 segRightSmall,
				 segRightLarge);
	      assert(segRightLarge>=sep_right);
	      monoTriangulation2(rightChain->getVertex(segRightLarge),
				 rightGridChain->get_vertex(gridIndex1),
				 rightChain,
				 segRightLarge+1,
				 up_rightCornerIndex,
				 0, //a decrease chain
				 pStream);
	      stripOfFanRight(rightChain, segRightLarge, segRightSmall,
			      rightGridChain->getGrid(),
			      rightGridChain->getVlineIndex(gridIndex1),
			      leftGridChain->getUlineIndex(gridIndex1),
			      rightGridChain->getUlineIndex(gridIndex1),
			      pStream, 0);


	      monoTriangulationRecGen(topVertex, leftGridChain->get_vertex(gridIndex1),
				      leftChain, leftStartIndex, up_leftCornerIndex,
				      rightChain, rightStartIndex,segRightSmall,
				      pStream);

	    }//end left out rigth in
	  else //left out , right out
	    {

	      sampleCompTopSimple(topVertex,
				  leftChain,
				  leftStartIndex,
				  rightChain,
				  rightStartIndex,
				  leftGridChain,
				  rightGridChain,
				  gridIndex1,
				  up_leftCornerWhere,
				  up_leftCornerIndex,
				  up_rightCornerWhere,
				  up_rightCornerIndex,
				  pStream);	      	      
	    }//end leftout, right out
	}//end if separator exixts.
      else //no separator
	{

	  sampleCompTopSimple(topVertex,
			    leftChain,
			      leftStartIndex,
			      rightChain,
			      rightStartIndex,
			      leftGridChain,
			      rightGridChain,
			      gridIndex1,
			    up_leftCornerWhere,
			      up_leftCornerIndex,
			      up_rightCornerWhere,
			      up_rightCornerIndex,
			    pStream);
	}
#endif
    }//end if 0,2
}//end if the function

		   
static void sampleCompTopSimpleOpt(gridWrap* grid,
				   Int gridV,
				   Real* topVertex, Real* botVertex,
				   vertexArray* inc_chain, Int inc_current, Int inc_end,
				   vertexArray* dec_chain, Int dec_current, Int dec_end,
				   primStream* pStream)
{
  if(gridV <= 0 || dec_end<dec_current || inc_end <inc_current)
    {
      monoTriangulationRecGenOpt(topVertex, botVertex,
				 inc_chain, inc_current, inc_end,
				 dec_chain, dec_current, dec_end,
				 pStream);
      return;
    }
  if(grid->get_v_value(gridV+1) >= topVertex[1])
    {
      monoTriangulationRecGenOpt(topVertex, botVertex,
				 inc_chain, inc_current, inc_end,
				 dec_chain, dec_current, dec_end,
				 pStream);
      return;
    }      
 Int i,j,k;
  Real currentV = grid->get_v_value(gridV+1);
  if(inc_chain->getVertex(inc_end)[1] <= currentV &&
     dec_chain->getVertex(dec_end)[1] < currentV)
    {
      //find i bottom up so that inc_chain[i]<= curentV and inc_chain[i-1] > currentV, 
      //find j botom up so that dec_chain[j] < currentV and dec_chain[j-1] >= currentV
      for(i=inc_end; i >= inc_current; i--) 
	{
	  if(inc_chain->getVertex(i)[1] > currentV)
	    break;
	}
      i++;
      for(j=dec_end; j >= dec_current; j--)
	{
	  if(dec_chain->getVertex(j)[1] >= currentV)
	    break;
	}
      j++;
     if(inc_chain->getVertex(i)[1] <= dec_chain->getVertex(j)[1])
       {
	 //find the k so that dec_chain[k][1] < inc_chain[i][1]
	 for(k=j; k<=dec_end; k++)
	   {
	     if(dec_chain->getVertex(k)[1] < inc_chain->getVertex(i)[1])
	       break;
	   }
         //we know that dec_chain[j][1] >= inc_chian[i][1]
	 //we know that dec_chain[k-1][1]>=inc_chain[i][1]
         //we know that dec_chian[k][1] < inc_chain[i][1]
         //find l in [j, k-1] so that dec_chain[l][0] 0 is closest to
         // inc_chain[i]
         int l;
         Real tempI = Real(j);
         Real tempMin = (Real)fabs(inc_chain->getVertex(i)[0] - dec_chain->getVertex(j)[0]);
         for(l=j+1; l<= k-1; l++)
	   {
	     if(fabs(inc_chain->getVertex(i)[0] - dec_chain->getVertex(l)[0])
		<= tempMin)
	       {
		 tempMin = (Real)fabs(inc_chain->getVertex(i)[0] - dec_chain->getVertex(l)[0]);
		 tempI = (Real)l;
	       }
	   }
	 //inc_chain[i] and dec_chain[tempI] are connected.
	 monoTriangulationRecGenOpt(dec_chain->getVertex((int)tempI),
				    botVertex,
				    inc_chain, i, inc_end,
				    dec_chain, (int)(tempI+1), dec_end,
				    pStream);
	 //recursively do the rest
	 sampleCompTopSimpleOpt(grid,
				gridV+1,
				topVertex, inc_chain->getVertex(i),
				inc_chain, inc_current, i-1,
				dec_chain, dec_current, (int)tempI,
				pStream);
       }
      else
	{
	  //find the k so that inc_chain[k][1] <= dec_chain[j][1]
	  for(k=i; k<=inc_end; k++)
	    {
	      if(inc_chain->getVertex(k)[1] <= dec_chain->getVertex(j)[1])
		break;
	    }
	  //we know that inc_chain[i] > dec_chain[j]
	  //we know that inc_chain[k-1][1] > dec_chain[j][1]
	  //we know that inc_chain[k][1] <= dec_chain[j][1]
	  //so we find l between [i,k-1] so that 
	  //inc_chain[l][0] is the closet to dec_chain[j][0]
	  int tempI = i;
	  int l;
	  Real tempMin = (Real)fabs(inc_chain->getVertex(i)[0] - dec_chain->getVertex(j)[0]);
	  for(l=i+1; l<=k-1; l++)
	    {
	      if(fabs(inc_chain->getVertex(l)[0] - dec_chain->getVertex(j)[0]) <= tempMin)
		{
		  tempMin = (Real)fabs(inc_chain->getVertex(l)[0] - dec_chain->getVertex(j)[0]);
		  tempI = l;
		}
	    }				 	      

	  //inc_chain[tempI] and dec_chain[j] are connected

	  monoTriangulationRecGenOpt(inc_chain->getVertex(tempI),
				     botVertex,
				     inc_chain, tempI+1, inc_end,
				     dec_chain, j, dec_end,
				     pStream);

	  //recurvesily do the rest
	  sampleCompTopSimpleOpt(grid, gridV+1,
				 topVertex, dec_chain->getVertex(j),
				 inc_chain, inc_current, tempI,
				 dec_chain, dec_current, j-1,
				 pStream);
	}	    				   	       
    }
  else //go to the next higher gridV
    {
      sampleCompTopSimpleOpt(grid,
			     gridV+1,
			     topVertex, botVertex,
			     inc_chain, inc_current, inc_end,
			     dec_chain, dec_current, dec_end,
			     pStream);
    }
}
			  
void sampleCompTopSimple(Real* topVertex,
                   vertexArray* leftChain,
                   Int leftStartIndex,
                   vertexArray* rightChain,
                   Int rightStartIndex,
                   gridBoundaryChain* leftGridChain,
                   gridBoundaryChain* rightGridChain,
                   Int gridIndex1,
                   Int up_leftCornerWhere,
                   Int up_leftCornerIndex,
                   Int up_rightCornerWhere,
                   Int up_rightCornerIndex,
                   primStream* pStream)
{
  //the plan is to use monotriangulation algortihm.
  Int i,k;
  Real* ActualTop;
  Real* ActualBot;
  Int ActualLeftStart, ActualLeftEnd;
  Int ActualRightStart, ActualRightEnd;
  
  //creat an array to store the points on the grid line
  gridWrap* grid = leftGridChain->getGrid();
  Int gridV = leftGridChain->getVlineIndex(gridIndex1);
  Int gridLeftU = leftGridChain->getUlineIndex(gridIndex1);
  Int gridRightU = rightGridChain->getUlineIndex(gridIndex1);

  Real2* gridPoints = (Real2*) malloc(sizeof(Real2) * (gridRightU - gridLeftU +1));
  assert(gridPoints);
  
  for(k=0, i=gridRightU; i>= gridLeftU; i--, k++)
    {
      gridPoints[k][0] = grid->get_u_value(i);
      gridPoints[k][1] = grid->get_v_value(gridV);
    }

  if(up_leftCornerWhere != 2)
    ActualRightStart = rightStartIndex;
  else
    ActualRightStart = up_leftCornerIndex+1; //up_leftCornerIndex will be the ActualTop
  
  if(up_rightCornerWhere != 2) //right corner is not on right chain
    ActualRightEnd = rightStartIndex-1; //meaning that there is no actual rigth section
  else
    ActualRightEnd = up_rightCornerIndex;
  
  vertexArray ActualRightChain(max(0, ActualRightEnd-ActualRightStart+1) + gridRightU-gridLeftU+1);

  for(i=ActualRightStart; i<= ActualRightEnd; i++)
    ActualRightChain.appendVertex(rightChain->getVertex(i));
  for(i=0; i<gridRightU-gridLeftU+1; i++)
    ActualRightChain.appendVertex(gridPoints[i]);    

  //determine ActualLeftEnd
  if(up_leftCornerWhere != 0)
    ActualLeftEnd = leftStartIndex-1;
  else
    ActualLeftEnd = up_leftCornerIndex;
  
  if(up_rightCornerWhere != 0)
    ActualLeftStart = leftStartIndex;
  else
    ActualLeftStart = up_rightCornerIndex+1; //up_rightCornerIndex will be the actual top
  
  if(up_leftCornerWhere == 0) 
    {
      if(up_rightCornerWhere == 0)
	ActualTop = leftChain->getVertex(up_rightCornerIndex);
      else
	ActualTop = topVertex;
    }
  else if(up_leftCornerWhere == 1) 
    ActualTop = topVertex;
  else  //up_leftCornerWhere == 2
    ActualTop = rightChain->getVertex(up_leftCornerIndex);
  
  ActualBot = gridPoints[gridRightU - gridLeftU];
  



  if(leftChain->getVertex(ActualLeftEnd)[1] == ActualBot[1])
    {
/*
    monoTriangulationRecGenOpt(ActualTop, leftChain->getVertex(ActualLeftEnd),
			    leftChain,
			    ActualLeftStart, ActualLeftEnd-1,
			    &ActualRightChain,
			    0,
			    ActualRightChain.getNumElements()-1,
			    pStream);
*/
   
    sampleCompTopSimpleOpt(grid, gridV,
			   ActualTop, leftChain->getVertex(ActualLeftEnd),
			    leftChain,
			    ActualLeftStart, ActualLeftEnd-1,
			    &ActualRightChain,
			    0,
			    ActualRightChain.getNumElements()-1,
			    pStream);
    
  }
  else
    {
/*
    monoTriangulationRecGenOpt(ActualTop, ActualBot, leftChain,
			  ActualLeftStart, ActualLeftEnd,
			  &ActualRightChain,
			  0, ActualRightChain.getNumElements()-2, //the last is the bot.
			  pStream);
*/
   	  
    sampleCompTopSimpleOpt(grid, gridV,
			   ActualTop, ActualBot, leftChain,
			  ActualLeftStart, ActualLeftEnd,
			  &ActualRightChain,
			  0, ActualRightChain.getNumElements()-2, //the last is the bot.
			  pStream);

   
  }

  free(gridPoints);
      
}