41 #include "glimports.h"
44 #include "quicksort.h"
45 #include "directedLine.h"
49 #pragma warning 726 10
55 if(begin->head()[0] == end->tail()[0] &&
56 begin->head()[1] == end->tail()[1]
60 begin->prev->next = end->next;
61 end->next->prev = begin->prev;
96 dline->next->head()[0] = dline->prev->tail()[0];
97 dline->next->head()[1] = dline->prev->tail()[1];
99 dline->prev->next = dline->next;
100 dline->next->prev = dline->prev;
106 static Int myequal(Real a[2], Real b[2])
116 if(fabs(a[0]-b[0]) < 0.00001 &&
117 fabs(a[1]-b[1]) < 0.00001)
127 if(this->next ==
this)
129 if(this->next == this->prev)
135 if(! myequal(head(), tail()))
143 for(temp = this->next; temp !=
this; temp = temp->next)
149 if(! myequal(temp->head(), temp->tail()))
161 deleteSinglePolygonWithSline();
166 for(temp =first->next; temp != first; temp = tempNext)
168 tempNext = temp->getNext();
174 if(myequal(temp->head(), temp->tail()))
175 deleteSingleLine(temp);
180 directedLine* directedLine::deleteDegenerateLinesAllPolygons()
186 for(temp=
this; temp != NULL; temp = tempNext)
188 tempNext = temp->nextPolygon;
189 temp->nextPolygon = NULL;
192 ret = retEnd = temp->deleteDegenerateLines();
197 directedLine *newPolygon = temp->deleteDegenerateLines();
198 if(newPolygon != NULL)
200 retEnd->nextPolygon = temp->deleteDegenerateLines();
201 retEnd = retEnd->nextPolygon;
208 directedLine* directedLine::cutIntersectionAllPoly(
int &cutOccur)
215 for(temp=
this; temp != NULL; temp = tempNext)
218 tempNext = temp->nextPolygon;
219 temp->nextPolygon = NULL;
223 ret = retEnd = DBG_cutIntersectionPoly(temp, eachCutOccur);
230 retEnd->nextPolygon = DBG_cutIntersectionPoly(temp, eachCutOccur);
231 retEnd = retEnd->nextPolygon;
240 void directedLine::deleteSinglePolygonWithSline()
244 for(temp=
this; temp != NULL; temp = tempNext)
246 tempNext = temp->next;
252 void directedLine::deletePolygonListWithSline()
255 for(temp=
this; temp != NULL; temp=tempNext)
257 tempNext = temp->nextPolygon;
258 temp->deleteSinglePolygonWithSline();
262 void directedLine::deleteSinglePolygon()
266 for(temp=
this; temp != NULL; temp = tempNext)
268 tempNext = temp->next;
273 void directedLine::deletePolygonList()
276 for(temp=
this; temp != NULL; temp=tempNext)
278 tempNext = temp->nextPolygon;
279 temp->deleteSinglePolygon();
285 directedLine::directedLine(
short dir,
sampledLine* sl)
299 void directedLine::init(
short dir,
sampledLine* sl)
305 directedLine::directedLine()
314 directedLine::~directedLine()
318 Real* directedLine::head()
321 return (direction==INCREASING)? (sline->get_points())[0] : (sline->get_points())[sline->get_npoints()-1];
324 Real* directedLine::getVertex(Int i)
326 return (direction==INCREASING)? (sline->get_points())[i] : (sline->get_points())[sline->get_npoints() - 1 -i];
329 Real* directedLine::tail()
331 return (direction==DECREASING)? (sline->get_points())[0] : (sline->get_points())[sline->get_npoints()-1];
344 Int directedLine::numEdges()
348 if(next ==
this)
return 1;
351 for(temp = next; temp !=
this; temp = temp->next)
356 Int directedLine::numEdgesAllPolygons()
360 for(temp=
this; temp!= NULL; temp=temp->nextPolygon)
362 ret += temp->numEdges();
369 short directedLine::isPolygon()
374 if(numEdges() <=2)
return 0;
377 if(! isConnected())
return 0;
380 for(temp=next; temp !=
this; temp = temp->next){
381 if(!isConnected())
return 0;
389 short directedLine::isConnected()
391 if( (head()[0] == prev->tail()[0]) && (head()[1] == prev->tail()[1]))
397 Int compV2InY(Real A[2], Real B[2])
399 if(A[1] < B[1])
return -1;
400 if(A[1] == B[1] && A[0] < B[0])
return -1;
401 if(A[1] == B[1] && A[0] == B[0])
return 0;
405 Int compV2InX(Real A[2], Real B[2])
407 if(A[0] < B[0])
return -1;
408 if(A[0] == B[0] && A[1] < B[1])
return -1;
409 if(A[0] == B[0] && A[1] == B[1])
return 0;
423 if(head()[1] < nl->head()[1])
return -1;
424 if(head()[1] == nl->head()[1] && head()[0] < nl->head()[0])
return -1;
438 if(head()[0] < nl->head()[0])
return -1;
439 if(head()[0] == nl->head()[0] && head()[1] < nl->head()[1])
return -1;
447 return v1->compInY(v2);
452 return v1->compInX(v2);
461 Int total_num_edges = 0;
462 directedLine** array = toArrayAllPolygons(total_num_edges);
463 quicksort( (
void**)array, 0, total_num_edges-1, (Int (*)(
void *,
void *)) compInY2);
468 void directedLine::printSingle()
470 if(direction == INCREASING)
471 printf(
"direction is INCREASING\n");
473 printf(
"direction is DECREASING\n");
474 printf(
"head=%f,%f)\n", head()[0], head()[1]);
479 void directedLine::printList()
483 for(temp = next; temp!=
this; temp=temp->next)
488 void directedLine::printAllPolygons()
491 for(temp =
this; temp!=NULL; temp = temp->nextPolygon)
493 printf(
"polygon:\n");
503 if(oldList == NULL)
return this;
504 nextPolygon = oldList;
516 if(p == NULL)
return this;
518 for(temp=
this; temp != p; temp = temp->nextPolygon)
522 fprintf(stderr,
"in cutoffPolygon, not found\n");
531 if(prev_polygon == NULL)
534 prev_polygon->nextPolygon = p->nextPolygon;
539 Int directedLine::numPolygons()
541 if(nextPolygon == NULL)
return 1;
542 else return 1+nextPolygon->numPolygons();
550 Int directedLine::toArraySinglePolygon(
directedLine** array, Int index)
553 array[index++] =
this;
554 for(temp = next; temp !=
this; temp = temp->next)
556 array[index++] = temp;
565 directedLine** directedLine::toArrayAllPolygons(Int& total_num_edges)
567 total_num_edges=numEdgesAllPolygons();
573 for(temp=
this; temp != NULL; temp=temp->nextPolygon) {
574 index = temp->toArraySinglePolygon(ret, index);
583 Real directedLine::polyArea()
588 x1 = this->head()[0];
589 y1 = this->head()[1];
590 x2 = this->next->head()[0];
591 y2 = this->next->head()[1];
592 ret = -(x2*y1-x1*y2);
593 for(temp=this->next; temp!=
this; temp = temp->next)
595 x1 = temp->head()[0];
596 y1 = temp->head()[1];
597 x2 = temp->next->head()[0];
598 y2 = temp->next->head()[1];
599 ret += -( x2*y1-x1*y2);
601 return Real(0.5)*ret;
640 nsline->setPoint(0, v1->head());
641 nsline->setPoint(1, v2->head());
656 v1 ->prev = newLineDec;
657 v2Prev ->next = newLineDec;
658 newLineDec->next = v1;
659 newLineDec->prev = v2Prev;
661 v2 ->prev = newLineInc;
662 v1Prev ->next = newLineInc;
663 newLineInc->next = v2;
664 newLineInc->prev = v1Prev;
666 *ret_p1 = newLineDec;
667 *ret_p2 = newLineInc;
668 *generatedLine = nsline;
681 nsline->setPoint(0, v1->head());
682 nsline->setPoint(1, v2->head());
683 nsline2->setPoint(0, v1->head());
684 nsline2->setPoint(1, v2->head());
694 v1 ->prev = newLineDec;
695 v2Prev ->next = newLineDec;
696 newLineDec->next = v1;
697 newLineDec->prev = v2Prev;
699 v2 ->prev = newLineInc;
700 v1Prev ->next = newLineInc;
701 newLineInc->next = v2;
702 newLineInc->prev = v1Prev;
704 *ret_p1 = newLineDec;
705 *ret_p2 = newLineInc;
711 if(v1 == v2)
return 1;
713 for(temp = v1->next; temp != v1; temp = temp->next)
715 if(temp == v2)
return 1;
722 if(rootBit)
return this;
724 for(temp = next; temp !=
this; temp = temp->next)
725 if(temp -> rootBit )
return temp;
735 while(tempLink != NULL){
737 tempLink = tempRoot->rootLink;
754 void directedLine::writeAllPolygons(
char* filename)
757 FILE* fp = fopen(filename,
"w");
759 Int nPolygons = numPolygons();
761 fprintf(fp,
"%i\n", nPolygons);
762 for(root =
this; root != NULL; root = root->nextPolygon)
766 npoints = root->get_npoints()-1;
767 for(temp = root->next; temp != root; temp=temp->next)
768 npoints += temp->get_npoints()-1;
769 fprintf(fp,
"%i\n", npoints);
772 for(Int i=0; i<root->get_npoints()-1; i++){
773 fprintf(fp,
"%f ", root->getVertex(i)[0]);
774 fprintf(fp,
"%f ", root->getVertex(i)[1]);
777 for(temp=root->next; temp != root; temp = temp->next)
779 for(Int i=0; i<temp->get_npoints()-1; i++){
781 fprintf(fp,
"%f ", temp->getVertex(i)[0]);
782 fprintf(fp,
"%f ", temp->getVertex(i)[1]);
798 FILE* fp = fopen(filename,
"r");
801 fscanf(fp,
"%i", &nPolygons);
803 for(i=0; i<nPolygons; i++)
806 fscanf(fp,
"%i", &nEdges);
810 fscanf(fp,
"%f", &(vert[0][0]));
811 fscanf(fp,
"%f", &(vert[0][1]));
812 fscanf(fp,
"%f", &(vert[1][0]));
813 fscanf(fp,
"%f", &(vert[1][1]));
814 VV[1][0] = vert[0][0];
815 VV[1][1] = vert[0][1];
818 thisPoly->rootLinkSet(NULL);
821 for(j=2; j<nEdges; j++)
823 vert[0][0]=vert[1][0];
824 vert[0][1]=vert[1][1];
825 fscanf(fp,
"%f", &(vert[1][0]));
826 fscanf(fp,
"%f", &(vert[1][1]));
829 dLine->rootLinkSet(thisPoly);
830 thisPoly->insert(dLine);
837 dLine->rootLinkSet(thisPoly);
838 thisPoly->insert(dLine);
840 ret = thisPoly->insertPolygon(ret);