void DecomposeRect(SRECT* r1, SRECT* r2) // Restructure two overlaping rectangles to eliminate the intersecting area // while still covering the same area and perhaps more area { FLASHASSERT(RectTestOverlap(r1, r2)); // Build the 3 rect slabs on y-axis SRECT r[3]; if ( r1->ymin < r2->ymin ) { r[0].ymin = r1->ymin; r[0].ymax = r2->ymin; r[0].xmin = r1->xmin; r[0].xmax = r1->xmax; } else { r[0].ymin = r2->ymin; r[0].ymax = r1->ymin; r[0].xmin = r2->xmin; r[0].xmax = r2->xmax; } if ( r1->ymax < r2->ymax ) { r[2].ymin = r1->ymax; r[2].ymax = r2->ymax; r[2].xmin = r2->xmin; r[2].xmax = r2->xmax; } else { r[2].ymin = r2->ymax; r[2].ymax = r1->ymax; r[2].xmin = r1->xmin; r[2].xmax = r1->xmax; } r[1].ymin = r[0].ymax; r[1].ymax = r[2].ymin; r[1].xmin = Min(r1->xmin, r2->xmin); r[1].xmax = Max(r1->xmax, r2->xmax); // Combine the middle slab with the slab that will generate the smallest area S32 a[3]; for ( int i = 0; i < 3; i++ ) a[i] = RectArea(r+i); SRECT u1, u2; RectUnion(&r[0], &r[1], &u1); RectUnion(&r[1], &r[2], &u2); S32 delta0 = a[0] + a[1] - RectArea(&u1); S32 delta1 = a[1] + a[2] - RectArea(&u2); if ( delta0 > delta1 ) { *r1 = u1; *r2 = r[2]; } else { *r1 = r[0]; *r2 = u2; } }
RectArea ImageProcessor::findSquare(int x, int y) { const int line_size=2; int square_size=(ChessArea.height-18)/8; return RectArea(ChessArea.x+x*(square_size+line_size)+line_size, ChessArea.y+y*(square_size+line_size)+line_size, square_size+1,square_size+1); }
BOOL DisplayList::MergeDirtyList(BOOL forceMerge) // Merge the pair of rectangles that will cause the smallest increase in the total dirty area { if ( nDirty > 1 ) { // Check to merge areas to reduce the number S32 bestDelta = forceMerge ? 0x7FFFFFFFL : 0; // if there is no empty slot, we must merge int mergeA = 0; int mergeB = 0; for ( int i = 0; i < nDirty-1; i++ ) { for ( int j = i+1; j < nDirty; j++ ) { S32 delta = UnionArea(devDirtyRect+i, devDirtyRect+j) - devDirtyArea[i] - devDirtyArea[j]; if ( bestDelta > delta ) { mergeA = i; mergeB = j; bestDelta = delta; } } } if ( mergeA != mergeB ) { RectUnion(devDirtyRect+mergeA, devDirtyRect+mergeB, devDirtyRect+mergeA); devDirtyArea[mergeA] = RectArea(devDirtyRect+mergeA); for ( int i = mergeB+1; i < nDirty; i++ ) { devDirtyRect[i-1] = devDirtyRect[i]; devDirtyArea[i-1] = devDirtyArea[i]; } nDirty--; return true; } } return false; }
static int RTreePickBranch (rect_t r, node_t n) { int i; double area; double inc_area; rect_t tmp; int best_i; double best_inc; double best_i_area; best_i = 0; best_inc = DBL_MAX; /* double Max value */ best_i_area = DBL_MAX; for (i = 0; i < MAXCARD; i++) if (n->branch[i].child) { area = RectArea (n->branch[i].mbr); tmp = RectCombine (r, n->branch[i].mbr); inc_area = RectArea (tmp) - area; if (inc_area < best_inc) { best_inc = inc_area; best_i = i; best_i_area = area; } else if (inc_area == best_inc && best_i_area > area) { best_inc = inc_area; best_i = i; best_i_area = area; } } else break; return best_i; }
static void RTreePickNext(partition_t p, node_t n1, node_t n2) /* linear version */ { branch_t b; double area[2], inc_area[2]; rect_t tmp; b = PartitionPop(p); area[0] = RectArea (p->cover[0]); tmp = RectCombine (p->cover[0], b.mbr); inc_area[0] = RectArea (tmp) - area[0]; area[1] = RectArea (p->cover[1]); tmp = RectCombine (p->cover[1], b.mbr); inc_area[1] = RectArea (tmp) - area[1]; if (inc_area[0] < inc_area[1] || (inc_area[0] == inc_area[1] && area[0] < area[1])) RTreeNodeAddBranch(&(p->cover[0]),n1,b); else RTreeNodeAddBranch(&(p->cover[1]),n2,b); }
/* Pick a branch. Pick the one that will need the smallest increase ** in area to accommodate the new rectangle. This will result in the ** least total area for the covering rectangles in the current node. ** In case of a tie, pick the one which was smaller before, to get ** the best resolution when searching. */ int PickBranch(Rect_t * r, Node_t * n) { register Rect_t *rr=0; register int i=0, flag=1, increase=0, bestIncr=0, area=0, bestArea=0; int best=0; assert(r && n); for (i = 0; i < NODECARD; i++) { if (n->branch[i].child) { Rect_t rect; rr = &n->branch[i].rect; area = RectArea(rr); /* increase = RectArea(&CombineRect(r, rr)) - area; */ rect = CombineRect(r, rr); increase = RectArea(&rect) - area; if (increase < bestIncr || flag) { best = i; bestArea = area; bestIncr = increase; flag = 0; } else if (increase == bestIncr && area < bestArea) { best = i; bestArea = area; bestIncr = increase; } # ifdef RTDEBUG fprintf(stderr, "i=%d area before=%d area after=%d increase=%d\n", i, area, area + increase, increase); # endif } } # ifdef RTDEBUG fprintf(stderr, "\tpicked %d\n", best); # endif return best; }
void DisplayList::InvalidateRect(SRECT* r) { SRECT rect; rect.xmin = r->xmin; rect.xmax = r->xmax; rect.ymin = r->ymin; rect.ymax = r->ymax; RectInset(antialias ? -8 : -2, &rect); if ( RectTestIntersect(&devViewRect, &rect) ) { FLASHASSERT(nDirty < maxDirtyAreas); // Add to the list RectIntersect(&devViewRect, &rect, &devDirtyRect[nDirty]); RectUnion(&devDirtyRgn, &devDirtyRect[nDirty], &devDirtyRgn); // add to the dirty region devDirtyArea[nDirty] = RectArea(devDirtyRect+nDirty); // add to the list nDirty++; MergeDirtyList(nDirty == maxDirtyAreas); } }
RectArea ImageProcessor::findDesk(void) { std::vector<int> HProjection; std::vector<int> VProjection; getProjections(RectArea(0,0,image->width(),image->height()),HProjection, VProjection); int HMinElem=255; int VMinElem=255; for(unsigned int i=0;i<HProjection.size();i++) { if (HProjection[i]<HMinElem) {HMinElem=HProjection[i];} } for(unsigned int i=0;i<VProjection.size();i++) { if (VProjection[i]<VMinElem) {VMinElem=VProjection[i];} } int HCountMin=0; int VCountMin=0; while (HCountMin<18) { HCountMin=0; for(unsigned int i=0;i<HProjection.size();i++) { if (HProjection[i]<=HMinElem) {HCountMin++;} } HMinElem++; } while (VCountMin<18) { VCountMin=0; for(unsigned int i=0;i<VProjection.size();i++) { if (VProjection[i]<=VMinElem) {VCountMin++;} } VMinElem++; } if (HCountMin>18) { writeLog(QString(tr("ERROR: Can't find desk")),1); } int x1=0; for(x1=0;(static_cast<unsigned int>(x1))<HProjection.size();x1++) { if (HProjection[x1]<=HMinElem) {break;} } int y1=0; for(y1=0;(static_cast<unsigned int>(y1))<VProjection.size();y1++) { if (VProjection[y1]<=VMinElem) break; } int x2=0; for(x2=HProjection.size()-1;x2>=0;x2--) { if (HProjection[x2]<=HMinElem) break; } int y2=0; for(y2=VProjection.size()-1;y2>=0;y2--) { if (VProjection[y2]<=VMinElem) break; } int StepX=(x2-x1-1)/8; for (int i=0;i<=8;i++) { if (((HProjection[x1+i*StepX])>HMinElem) || ((HProjection[x1+i*StepX+1])>HMinElem)) { writeLog(QString(tr("ERROR: Can't found 2px - lines on X axis")),1); } } int StepY=(y2-y1-1)/8; for (int i=0;i<=8;i++) { if (((VProjection[y1+i*StepY])>VMinElem) || ((VProjection[y1+i*StepY+1])>VMinElem)) { writeLog(QString(tr("ERROR: Can't found 2px - lines on Y axis")),1); } } writeLog(QString(tr("Found chess desk at X: %1; Y: %2; Width: %3; Height: %4;")).arg(x1).arg(y1).arg(x2-x1).arg(y2-y1)); return RectArea(x1,y1,x2-x1+1,y2-y1+1); }
inline S32 UnionArea(SRECT* r1, SRECT* r2) { SRECT area; RectUnion(r1, r2, &area); return RectArea(&area); }