42 #include "sampleCompTop.h"
43 #include "sampleCompRight.h"
45 #define max(a,b) ((a>b)? a:b)
62 assert(leftStart <= leftEnd);
63 for(i=leftEnd; i>= leftStart; i--)
65 if(leftChain->getVertex(i)[0] >= u)
69 if(ret_index_large >= leftStart)
71 for(i=ret_index_large; i>leftStart; i--)
73 if(leftChain->getVertex(i-1)[0] <= leftChain->getVertex(i)[0])
88 assert(rightStart<=rightEnd);
89 for(i=rightEnd; i>=rightStart; i--)
91 if(rightChain->getVertex(i)[0] <= u)
95 if(ret_index_large >= rightStart)
97 for(i=ret_index_large; i>rightStart;i--)
99 if(rightChain->getVertex(i-1)[0] >= rightChain->getVertex(i)[0])
107 void sampleTopRightWithGridLinePost(Real* topVertex,
120 if(segIndexLarge < rightEnd)
123 if(segIndexLarge >= rightStart)
124 tempTop = rightChain->getVertex(segIndexLarge);
128 tempBot[0] = grid->get_u_value(rightU);
129 tempBot[1] = grid->get_v_value(gridV);
130 monoTriangulationRecGenOpt(tempTop, tempBot,
132 rightChain, segIndexLarge+1, rightEnd,
146 if(segIndexLarge >= rightStart)
148 stripOfFanRight(rightChain, segIndexLarge, segIndexSmall, grid, gridV, leftU, rightU, pStream, 0);
150 tempBot[0] = grid->get_u_value(leftU);
151 tempBot[1] = grid->get_v_value(gridV);
152 monoTriangulation2(topVertex, tempBot, rightChain, rightStart, segIndexSmall, 0, pStream);
155 grid->outputFanWithPoint(gridV, leftU, rightU, topVertex, pStream);
158 void sampleTopRightWithGridLine(Real* topVertex,
170 if(rightEnd < rightStart){
171 grid->outputFanWithPoint(gridV, leftU, rightU, topVertex, pStream);
175 Int segIndexSmall, segIndexLarge;
176 findTopRightSegment(rightChain,
179 grid->get_u_value(rightU),
183 sampleTopRightWithGridLinePost(topVertex, rightChain,
196 void sampleTopLeftWithGridLinePost(Real* topVertex,
210 if(segIndexLarge < leftEnd)
213 if(segIndexLarge >= leftStart)
214 tempTop = leftChain->getVertex(segIndexLarge);
218 tempBot[0] = grid->get_u_value(leftU);
219 tempBot[1] = grid->get_v_value(gridV);
221 monoTriangulation2(tempTop, tempBot,
230 if(segIndexLarge >= leftStart)
236 if(topVertex[0] >= grid->get_u_value(rightU))
243 for(i=leftStart; i<=segIndexSmall; i++)
244 if(leftChain->getVertex(i)[0] >= topVertex[0])
256 while(grid->get_u_value(midU) >= topVertex[0])
264 grid->outputFanWithPoint(gridV, midU, rightU, topVertex, pStream);
265 stripOfFanLeft(leftChain, segIndexLarge, segIndexSmall, grid, gridV, leftU, midU, pStream, 0);
267 tempBot[0] = grid->get_u_value(midU);
268 tempBot[1] = grid->get_v_value(gridV);
269 monoTriangulation2(topVertex, tempBot, leftChain, leftStart, segIndexSmall, 1, pStream);
274 stripOfFanLeft(leftChain, segIndexLarge, segIndexSmall, grid, gridV, leftU, rightU, pStream, 0);
276 tempBot[0] = grid->get_u_value(rightU);
277 tempBot[1] = grid->get_v_value(gridV);
278 monoTriangulation2(topVertex, tempBot, leftChain, leftStart, segIndexSmall, 1, pStream);
282 grid->outputFanWithPoint(gridV, leftU, rightU, topVertex, pStream);
286 void sampleTopLeftWithGridLine(Real* topVertex,
297 Int segIndexSmall, segIndexLarge;
300 if(leftEnd < leftStart) {
301 grid->outputFanWithPoint(gridV, leftU, rightU, topVertex, pStream);
304 findTopLeftSegment(leftChain,
307 grid->get_u_value(leftU),
311 sampleTopLeftWithGridLinePost(topVertex,
336 Int oldLeftI, oldRightI, newLeftI, newRightI;
340 if(leftChain->getVertex(leftEndIndex)[1] > rightChain->getVertex(rightEndIndex)[1])
342 oldLeftI = leftEndIndex+1;
343 oldRightI = rightEndIndex;
344 leftMax = leftChain->getVertex(leftEndIndex)[0] - Real(1.0);
345 rightMin = rightChain->getVertex(rightEndIndex)[0];
349 oldLeftI = leftEndIndex;
350 oldRightI = rightEndIndex+1;
351 leftMax = leftChain->getVertex(leftEndIndex)[0];
352 rightMin = rightChain->getVertex(rightEndIndex)[0] + Real(1.0);
364 newRightI = oldRightI;
368 for(k=j-1; k>= rightStartIndex; k--)
370 if(rightChain->getVertex(k)[0] > leftMax)
373 if(rightChain->getVertex(k)[0] < rightMin)
375 rightMin = rightChain->getVertex(k)[0];
384 else if(j<rightStartIndex)
386 for(k=i-1; k>= leftStartIndex; k--)
388 if(leftChain->getVertex(k)[0] < rightMin)
391 if(leftChain->getVertex(k)[0] > leftMax)
393 leftMax = leftChain->getVertex(k)[0];
402 else if(leftChain->getVertex(i)[1] > rightChain->getVertex(j)[1])
404 if(leftChain->getVertex(i)[0] > leftMax)
406 leftMax = leftChain->getVertex(i)[0];
409 for(k=j-1; k>= rightStartIndex; k--)
411 if(rightChain->getVertex(k)[1] > leftChain->getVertex(i)[1])
413 if(rightChain->getVertex(k)[0] < rightMin)
415 rightMin = rightChain->getVertex(k)[0];
420 if(leftMax >= rightMin)
425 oldRightI = newRightI;
430 if(rightChain->getVertex(j)[0] < rightMin)
432 rightMin = rightChain->getVertex(j)[0];
435 for(k=i-1; k>= leftStartIndex; k--)
437 if(leftChain->getVertex(k)[1] > rightChain->getVertex(j)[1])
439 if(leftChain->getVertex(k)[0] > leftMax)
441 leftMax = leftChain->getVertex(k)[0];
447 if(leftMax >= rightMin)
452 oldRightI = newRightI;
457 if(oldLeftI > leftEndIndex || oldRightI > rightEndIndex)
461 ret_sep_left = oldLeftI;
462 ret_sep_right = oldRightI;
468 void sampleCompTop(Real* topVertex,
476 Int up_leftCornerWhere,
477 Int up_leftCornerIndex,
478 Int up_rightCornerWhere,
479 Int up_rightCornerIndex,
482 if(up_leftCornerWhere == 1 && up_rightCornerWhere == 1)
484 leftGridChain->getGrid()->outputFanWithPoint(leftGridChain->getVlineIndex(gridIndex1),
485 leftGridChain->getUlineIndex(gridIndex1),
486 rightGridChain->getUlineIndex(gridIndex1),
492 else if(up_leftCornerWhere != 0)
496 if(up_leftCornerWhere == 1){
497 tempRightStart = rightStartIndex;
502 tempRightStart = up_leftCornerIndex+1;
503 tempTop = rightChain->getVertex(up_leftCornerIndex);
505 sampleTopRightWithGridLine(tempTop, rightChain, tempRightStart, up_rightCornerIndex,
506 rightGridChain->getGrid(),
507 leftGridChain->getVlineIndex(gridIndex1),
508 leftGridChain->getUlineIndex(gridIndex1),
509 rightGridChain->getUlineIndex(gridIndex1),
512 else if(up_rightCornerWhere != 2)
516 if(up_rightCornerWhere == 1)
518 tempLeftStart = leftStartIndex;
523 tempLeftStart = up_rightCornerIndex+1;
524 tempTop = leftChain->getVertex(up_rightCornerIndex);
534 sampleCompTopSimple(topVertex,
550 sampleCompTopSimple(topVertex,
564 #ifdef NOT_REACHABLE //code is not reachable, for test purpose only
566 Int sep_left, sep_right;
567 if(findTopSeparator(leftChain,
578 if( leftChain->getVertex(sep_left)[0] >= leftGridChain->get_u_value(gridIndex1) &&
579 rightChain->getVertex(sep_right)[0] <= rightGridChain->get_u_value(gridIndex1))
582 Int segLeftSmall, segLeftLarge, segRightSmall, segRightLarge;
584 findTopLeftSegment(leftChain,
587 leftGridChain->get_u_value(gridIndex1),
590 findTopRightSegment(rightChain,
593 rightGridChain->get_u_value(gridIndex1),
596 if(leftChain->getVertex(segLeftSmall)[1] >= rightChain->getVertex(segRightSmall)[1])
598 gridSep = rightGridChain->getUlineIndex(gridIndex1);
599 while(leftGridChain->getGrid()->get_u_value(gridSep) > leftChain->getVertex(segLeftSmall)[0])
601 if(segLeftSmall<segLeftLarge)
602 if(leftGridChain->getGrid()->get_u_value(gridSep) < leftChain->getVertex(segLeftSmall+1)[0])
609 gridSep = leftGridChain->getUlineIndex(gridIndex1);
610 while(leftGridChain->getGrid()->get_u_value(gridSep) < rightChain->getVertex(segRightSmall)[0])
612 if(segRightSmall<segRightLarge)
613 if(leftGridChain->getGrid()->get_u_value(gridSep) > rightChain->getVertex(segRightSmall+1)[0])
621 sampleCompTopSimple(topVertex,
637 sampleTopLeftWithGridLinePost(leftChain->getVertex(segLeftSmall),
643 leftGridChain->getGrid(),
644 leftGridChain->getVlineIndex(gridIndex1),
645 leftGridChain->getUlineIndex(gridIndex1),
648 sampleTopRightWithGridLinePost(rightChain->getVertex(segRightSmall),
654 leftGridChain->getGrid(),
655 leftGridChain->getVlineIndex(gridIndex1),
657 rightGridChain->getUlineIndex(gridIndex1),
660 tempBot[0] = leftGridChain->getGrid()->get_u_value(gridSep);
661 tempBot[1] = leftGridChain->get_v_value(gridIndex1);
662 monoTriangulationRecGen(topVertex, tempBot,
663 leftChain, leftStartIndex, segLeftSmall,
664 rightChain, rightStartIndex, segRightSmall,
668 else if(leftChain->getVertex(sep_left)[0] >= leftGridChain->get_u_value(gridIndex1))
671 Int segLeftSmall, segLeftLarge;
672 findTopLeftSegment(leftChain,
675 leftGridChain->get_u_value(gridIndex1),
678 assert(segLeftLarge >= sep_left);
679 monoTriangulation2(leftChain->getVertex(segLeftLarge),
680 leftGridChain->get_vertex(gridIndex1),
687 stripOfFanLeft(leftChain, segLeftLarge, segLeftSmall,
688 leftGridChain->getGrid(),
689 leftGridChain->getVlineIndex(gridIndex1),
690 leftGridChain->getUlineIndex(gridIndex1),
691 rightGridChain->getUlineIndex(gridIndex1),
695 monoTriangulationRecGen(topVertex, rightGridChain->get_vertex(gridIndex1),
696 leftChain, leftStartIndex, segLeftSmall,
697 rightChain, rightStartIndex, up_rightCornerIndex,
700 else if(rightChain->getVertex(sep_right)[0] <= rightGridChain->get_u_value(gridIndex1))
702 Int segRightSmall, segRightLarge;
703 findTopRightSegment(rightChain,
706 rightGridChain->get_u_value(gridIndex1),
709 assert(segRightLarge>=sep_right);
710 monoTriangulation2(rightChain->getVertex(segRightLarge),
711 rightGridChain->get_vertex(gridIndex1),
717 stripOfFanRight(rightChain, segRightLarge, segRightSmall,
718 rightGridChain->getGrid(),
719 rightGridChain->getVlineIndex(gridIndex1),
720 leftGridChain->getUlineIndex(gridIndex1),
721 rightGridChain->getUlineIndex(gridIndex1),
725 monoTriangulationRecGen(topVertex, leftGridChain->get_vertex(gridIndex1),
726 leftChain, leftStartIndex, up_leftCornerIndex,
727 rightChain, rightStartIndex,segRightSmall,
734 sampleCompTopSimple(topVertex,
752 sampleCompTopSimple(topVertex,
771 static void sampleCompTopSimpleOpt(
gridWrap* grid,
773 Real* topVertex, Real* botVertex,
774 vertexArray* inc_chain, Int inc_current, Int inc_end,
775 vertexArray* dec_chain, Int dec_current, Int dec_end,
778 if(gridV <= 0 || dec_end<dec_current || inc_end <inc_current)
780 monoTriangulationRecGenOpt(topVertex, botVertex,
781 inc_chain, inc_current, inc_end,
782 dec_chain, dec_current, dec_end,
786 if(grid->get_v_value(gridV+1) >= topVertex[1])
788 monoTriangulationRecGenOpt(topVertex, botVertex,
789 inc_chain, inc_current, inc_end,
790 dec_chain, dec_current, dec_end,
795 Real currentV = grid->get_v_value(gridV+1);
796 if(inc_chain->getVertex(inc_end)[1] <= currentV &&
797 dec_chain->getVertex(dec_end)[1] < currentV)
801 for(i=inc_end; i >= inc_current; i--)
803 if(inc_chain->getVertex(i)[1] > currentV)
807 for(j=dec_end; j >= dec_current; j--)
809 if(dec_chain->getVertex(j)[1] >= currentV)
813 if(inc_chain->getVertex(i)[1] <= dec_chain->getVertex(j)[1])
816 for(k=j; k<=dec_end; k++)
818 if(dec_chain->getVertex(k)[1] < inc_chain->getVertex(i)[1])
827 Real tempI = Real(j);
828 Real tempMin = (Real)fabs(inc_chain->getVertex(i)[0] - dec_chain->getVertex(j)[0]);
829 for(l=j+1; l<= k-1; l++)
831 if(fabs(inc_chain->getVertex(i)[0] - dec_chain->getVertex(l)[0])
834 tempMin = (Real)fabs(inc_chain->getVertex(i)[0] - dec_chain->getVertex(l)[0]);
839 monoTriangulationRecGenOpt(dec_chain->getVertex((
int)tempI),
841 inc_chain, i, inc_end,
842 dec_chain, (int)(tempI+1), dec_end,
845 sampleCompTopSimpleOpt(grid,
847 topVertex, inc_chain->getVertex(i),
848 inc_chain, inc_current, i-1,
849 dec_chain, dec_current, (int)tempI,
855 for(k=i; k<=inc_end; k++)
857 if(inc_chain->getVertex(k)[1] <= dec_chain->getVertex(j)[1])
867 Real tempMin = (Real)fabs(inc_chain->getVertex(i)[0] - dec_chain->getVertex(j)[0]);
868 for(l=i+1; l<=k-1; l++)
870 if(fabs(inc_chain->getVertex(l)[0] - dec_chain->getVertex(j)[0]) <= tempMin)
872 tempMin = (Real)fabs(inc_chain->getVertex(l)[0] - dec_chain->getVertex(j)[0]);
879 monoTriangulationRecGenOpt(inc_chain->getVertex(tempI),
881 inc_chain, tempI+1, inc_end,
882 dec_chain, j, dec_end,
886 sampleCompTopSimpleOpt(grid, gridV+1,
887 topVertex, dec_chain->getVertex(j),
888 inc_chain, inc_current, tempI,
889 dec_chain, dec_current, j-1,
895 sampleCompTopSimpleOpt(grid,
897 topVertex, botVertex,
898 inc_chain, inc_current, inc_end,
899 dec_chain, dec_current, dec_end,
904 void sampleCompTopSimple(Real* topVertex,
912 Int up_leftCornerWhere,
913 Int up_leftCornerIndex,
914 Int up_rightCornerWhere,
915 Int up_rightCornerIndex,
922 Int ActualLeftStart, ActualLeftEnd;
923 Int ActualRightStart, ActualRightEnd;
926 gridWrap* grid = leftGridChain->getGrid();
927 Int gridV = leftGridChain->getVlineIndex(gridIndex1);
928 Int gridLeftU = leftGridChain->getUlineIndex(gridIndex1);
929 Int gridRightU = rightGridChain->getUlineIndex(gridIndex1);
931 Real2* gridPoints = (Real2*) malloc(
sizeof(Real2) * (gridRightU - gridLeftU +1));
934 for(k=0, i=gridRightU; i>= gridLeftU; i--, k++)
936 gridPoints[k][0] = grid->get_u_value(i);
937 gridPoints[k][1] = grid->get_v_value(gridV);
940 if(up_leftCornerWhere != 2)
941 ActualRightStart = rightStartIndex;
943 ActualRightStart = up_leftCornerIndex+1;
945 if(up_rightCornerWhere != 2)
946 ActualRightEnd = rightStartIndex-1;
948 ActualRightEnd = up_rightCornerIndex;
950 vertexArray ActualRightChain(max(0, ActualRightEnd-ActualRightStart+1) + gridRightU-gridLeftU+1);
952 for(i=ActualRightStart; i<= ActualRightEnd; i++)
953 ActualRightChain.appendVertex(rightChain->getVertex(i));
954 for(i=0; i<gridRightU-gridLeftU+1; i++)
955 ActualRightChain.appendVertex(gridPoints[i]);
958 if(up_leftCornerWhere != 0)
959 ActualLeftEnd = leftStartIndex-1;
961 ActualLeftEnd = up_leftCornerIndex;
963 if(up_rightCornerWhere != 0)
964 ActualLeftStart = leftStartIndex;
966 ActualLeftStart = up_rightCornerIndex+1;
968 if(up_leftCornerWhere == 0)
970 if(up_rightCornerWhere == 0)
971 ActualTop = leftChain->getVertex(up_rightCornerIndex);
973 ActualTop = topVertex;
975 else if(up_leftCornerWhere == 1)
976 ActualTop = topVertex;
978 ActualTop = rightChain->getVertex(up_leftCornerIndex);
980 ActualBot = gridPoints[gridRightU - gridLeftU];
985 if(leftChain->getVertex(ActualLeftEnd)[1] == ActualBot[1])
997 sampleCompTopSimpleOpt(grid, gridV,
998 ActualTop, leftChain->getVertex(ActualLeftEnd),
1000 ActualLeftStart, ActualLeftEnd-1,
1003 ActualRightChain.getNumElements()-1,
1017 sampleCompTopSimpleOpt(grid, gridV,
1018 ActualTop, ActualBot, leftChain,
1019 ActualLeftStart, ActualLeftEnd,
1021 0, ActualRightChain.getNumElements()-2,