template<class T> typename deque<T>::node * deque<T>::getNode(long i) const { if (i<0 || i>=sz) return new node(NULL,NULL,new T()); long fdist=i,ldist=sz-1-i; //Distances from first nodes and last nodes long cdist; bool dir; /* dir detects a direction of jump for current if (dir==true) i is next to current node else - previous */ deque * me=const_cast<deque *>(this); void * res; if (i>currentindex) {dir=true;cdist=i-currentindex;} else {dir=false;cdist=currentindex-i;} if (fdist>ldist) //If from first it will be gathered faster { if (cdist>fdist) //If from current it will be gathered { if (dir) {res=jumpNext(currentnode,cdist);} else {res=jumpPrevious(currentnode,cdist);} } else {res=jumpNext(firstnode,fdist);} } else { //From last it will be gathered faster if (cdist>ldist) //If from current it will be gathered { if (dir) {res=jumpNext(currentnode,cdist);} else {res=jumpPrevious(currentnode,cdist);} } else {res=jumpPrevious(lastnode,ldist);} } me->currentindex=i; me->currentnode=(node*)res; return (node*)res; }
void CmCurveEx::findEdge(Point seed, CEdge &crtEdge, bool isBackWard) { Point pnt = seed; float ornt = m_pOrnt1f.at<float>(pnt); if (isBackWard){ ornt += PI_FLOAT; if (ornt >= PI2) ornt -= PI2; } else{ crtEdge.pointNum = 1; m_pLabel1i.at<int>(pnt) = crtEdge.index; } int orntInd, nextInd1, nextInd2; while (true) { /*************按照优先级寻找下一个点,方向差异较大不加入**************/ //下一个点在DIRECTION16最佳方向上找 orntInd = int(ornt/PI_EIGHTH + 0.5f) % 16; if (jumpNext(pnt, ornt, crtEdge, orntInd, isBackWard)) continue; //下一个点在DIRECTION8最佳方向上找 orntInd = int(ornt/PI_QUARTER + 0.5f) % 8; if (goNext(pnt, ornt, crtEdge, orntInd, isBackWard)) continue; //下一个点在DIRECTION16次优方向上找 orntInd = int(ornt/PI_EIGHTH + 0.5f) % 16; nextInd1 = (orntInd + 1) % 16; nextInd2 = (orntInd + 15) % 16; if (angle(DRT_ANGLE[nextInd1], ornt) < angle(DRT_ANGLE[nextInd2], ornt)) { if(jumpNext(pnt, ornt, crtEdge, nextInd1, isBackWard)) continue; if(jumpNext(pnt, ornt, crtEdge, nextInd2, isBackWard)) continue; } else{//下一个点在DIRECTION16另一个方向上找 if(jumpNext(pnt, ornt, crtEdge, nextInd2, isBackWard)) continue; if(jumpNext(pnt, ornt, crtEdge, nextInd1, isBackWard)) continue; } //下一个点在DIRECTION8次优方向上找 orntInd = int(ornt/PI_QUARTER + 0.5f) % 8; nextInd1 = (orntInd + 1) % 8; nextInd2 = (orntInd + 7) % 8; if (angle(DRT_ANGLE[nextInd1], ornt) < angle(DRT_ANGLE[nextInd2], ornt)) { if(goNext(pnt, ornt, crtEdge, nextInd1, isBackWard)) continue; if(goNext(pnt, ornt, crtEdge, nextInd2, isBackWard)) continue; } else{//下一个点在DIRECTION8另一个方向上找 if(goNext(pnt, ornt, crtEdge, nextInd2, isBackWard)) continue; if(goNext(pnt, ornt, crtEdge, nextInd1, isBackWard)) continue; } /*************按照优先级寻找下一个点,方向差异较大也加入**************/ //下一个点在DIRECTION16最佳方向上找 orntInd = int(ornt/PI_EIGHTH + 0.5f) % 16; if (jumpNext(pnt, ornt, crtEdge, orntInd, isBackWard)) continue; //下一个点在DIRECTION8最佳方向上找 orntInd = int(ornt/PI_QUARTER + 0.5f) % 8; if (goNext(pnt, ornt, crtEdge, orntInd, isBackWard)) continue; //下一个点在DIRECTION16次优方向上找 orntInd = int(ornt/PI_EIGHTH + 0.5f) % 16; nextInd1 = (orntInd + 1) % 16; nextInd2 = (orntInd + 15) % 16; if (angle(DRT_ANGLE[nextInd1], ornt) < angle(DRT_ANGLE[nextInd2], ornt)) { if(jumpNext(pnt, ornt, crtEdge, nextInd1, isBackWard)) continue; if(jumpNext(pnt, ornt, crtEdge, nextInd2, isBackWard)) continue; } else{//下一个点在DIRECTION16另一个方向上找 if(jumpNext(pnt, ornt, crtEdge, nextInd2, isBackWard)) continue; if(jumpNext(pnt, ornt, crtEdge, nextInd1, isBackWard)) continue; } //下一个点在DIRECTION8次优方向上找 orntInd = int(ornt/PI_QUARTER + 0.5f) % 8; nextInd1 = (orntInd + 1) % 8; nextInd2 = (orntInd + 7) % 8; if (angle(DRT_ANGLE[nextInd1], ornt) < angle(DRT_ANGLE[nextInd2], ornt)) { if(goNext(pnt, ornt, crtEdge, nextInd1, isBackWard)) continue; if(goNext(pnt, ornt, crtEdge, nextInd2, isBackWard)) continue; } else{//下一个点在DIRECTION8另一个方向上找 if(goNext(pnt, ornt, crtEdge, nextInd2, isBackWard)) continue; if(goNext(pnt, ornt, crtEdge, nextInd1, isBackWard)) continue; } break;//如果ornt附近的三个方向上都没有的话,结束寻找 } if (isBackWard) crtEdge.start = pnt; else crtEdge.end = pnt; }