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
|
// This file is part of Golly.
// See docs/License.html for the copyright notice.
/**
* This file is where we figure out how to draw ghashbase structures,
* no matter what the magnification or renderer.
*/
#include "ghashbase.h"
#include "util.h"
#include <vector>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std ;
// AKT: a 256x256 pixmap is good for OpenGL and matches the size
// used in qlifedraw.cpp and hlifedraw.cpp;
// note that logpmsize *must* be 8 in iOS 9.x to avoid drawing problems
// in my iPad (probably due to a bug in the OpenGL ES 2 driver)
const int logpmsize = 8; // 8=256x256
const int pmsize = (1<<logpmsize) ; // pixmap wd and ht, in pixels
const int bpp = 4 ; // bytes per pixel (RGBA)
const int rowoff = (pmsize*bpp) ; // row offset, in bytes
const int ibufsize = (pmsize*pmsize*bpp) ; // buffer size, in bytes
static unsigned char ipixbuf[ibufsize] ; // shared buffer for pixels
static unsigned char *pixbuf = ipixbuf ;
// rowett: RGBA view of pixbuf
static unsigned int *pixRGBAbuf = (unsigned int *)ipixbuf;
// AKT: arrays of RGB colors for each cell state (set by getcolors call)
static unsigned char* cellred;
static unsigned char* cellgreen;
static unsigned char* cellblue;
// AKT: alpha values for dead pixels and live pixels (also set by getcolors call)
static unsigned char deada;
static unsigned char livea;
// rowett: RGBA view of cell colors
static unsigned int cellRGBA[256] ; // cell colours in RGBA format
static unsigned int state1RGBA ; // live RGBA color
void ghashbase::drawpixel(int x, int y) {
// AKT: draw all live cells using state 1 color
// pmag == 1, so store RGBA info
int i = (pmsize - 1 - y) * pmsize + x;
pixRGBAbuf[i] = state1RGBA;
}
/*
* Draw a 4x4 area yielding 1x1, 2x2, or 4x4 pixels.
*/
void ghashbase::draw4x4_1(state sw, state se, state nw, state ne,
int llx, int lly) {
// sw,se,nw,ne contain cell states (0..255)
int i = (pmsize-1+lly) * pmsize - llx;
if (renderer->justState() || pmag > 1) {
// store state info
pixbuf[i] = sw;
pixbuf[i+1] = se;
i -= pmsize;
pixbuf[i] = nw;
pixbuf[i+1] = ne;
} else {
// store RGBA info
if (sw) {
pixRGBAbuf[i] = cellRGBA[sw] ;
}
if (se) {
pixRGBAbuf[i+1] = cellRGBA[se] ;
}
i -= pmsize ;
if (nw) {
pixRGBAbuf[i] = cellRGBA[nw] ;
}
if (ne) {
pixRGBAbuf[i+1] = cellRGBA[ne] ;
}
}
}
void ghashbase::draw4x4_1(ghnode *n, ghnode *z, int llx, int lly) {
// AKT: draw all live cells using state 1 color
// pmag == 1, so store RGBA info
int i = (pmsize-1+lly) * pmsize - llx;
if (n->sw != z) {
pixRGBAbuf[i] = state1RGBA;
}
if (n->se != z) {
pixRGBAbuf[i+1] = state1RGBA;
}
i -= pmsize;
if (n->nw != z) {
pixRGBAbuf[i] = state1RGBA;
}
if (n->ne != z) {
pixRGBAbuf[i+1] = state1RGBA;
}
}
// AKT: kill all cells in pixbuf
void ghashbase::killpixels() {
if (renderer->justState() || pmag > 1) {
// pixblit assumes pixbuf contains pmsize*pmsize bytes where each byte
// is a cell state, so it's easy to kill all cells
memset(pixbuf, 0, pmsize*pmsize);
} else {
// pixblit assumes pixbuf contains 4 bytes (RGBA) for each pixel
if (deada == 0) {
// dead cells are 100% transparent so we can use fast method
// (RGB values are irrelevant if alpha is 0)
memset(pixbuf, 0, sizeof(ipixbuf));
} else {
// use slower method
unsigned int deadRGBA = cellRGBA[0];
unsigned int *rgbabuf = pixRGBAbuf;
// fill the first row with the dead pixel state
for (int i = 0 ; i < pmsize; i++) {
*rgbabuf++ = deadRGBA;
}
// copy 1st row to remaining rows
for (int i = rowoff; i < ibufsize; i += rowoff) {
memcpy(&pixbuf[i], pixbuf, rowoff);
}
}
}
}
void ghashbase::renderbm(int x, int y) {
// x,y is lower left corner
int rx = x ;
int ry = y ;
int rw = pmsize ;
int rh = pmsize ;
if (pmag > 1) {
rx *= pmag ;
ry *= pmag ;
rw *= pmag ;
rh *= pmag ;
}
ry = uviewh - ry - rh ;
if (renderer->justState())
renderer->stateblit(rx, ry, rw, rh, pixbuf) ;
else
renderer->pixblit(rx, ry, rw, rh, pixbuf, pmag);
killpixels();
}
/*
* Here, llx and lly are coordinates in screen pixels describing
* where the lower left pixel of the screen is. Draw one ghnode.
* This is our main recursive routine.
*/
void ghashbase::drawghnode(ghnode *n, int llx, int lly, int depth, ghnode *z) {
int sw = 1 << (depth - mag + 1) ;
if (sw >= pmsize &&
(llx + vieww <= 0 || lly + viewh <= 0 || llx >= sw || lly >= sw))
return ;
if (n == z) {
// don't do anything
} else if (depth > 0 && sw > 2) {
z = z->nw ;
sw >>= 1 ;
depth-- ;
if (sw == (pmsize >> 1)) {
drawghnode(n->sw, 0, 0, depth, z) ;
drawghnode(n->se, -(pmsize/2), 0, depth, z) ;
drawghnode(n->nw, 0, -(pmsize/2), depth, z) ;
drawghnode(n->ne, -(pmsize/2), -(pmsize/2), depth, z) ;
renderbm(-llx, -lly) ;
} else {
drawghnode(n->sw, llx, lly, depth, z) ;
drawghnode(n->se, llx-sw, lly, depth, z) ;
drawghnode(n->nw, llx, lly-sw, depth, z) ;
drawghnode(n->ne, llx-sw, lly-sw, depth, z) ;
}
} else if (depth > 0 && sw == 2) {
draw4x4_1(n, z->nw, llx, lly) ;
} else if (sw == 1) {
drawpixel(-llx, -lly) ;
} else {
struct ghleaf *l = (struct ghleaf *)n ;
sw >>= 1 ;
if (sw == 1) {
draw4x4_1(l->sw, l->se, l->nw, l->ne, llx, lly) ;
} else {
lifefatal("Can't happen") ;
}
}
}
/*
* Fill in the llxb and llyb bits from the viewport information.
* Allocate if necessary. This arithmetic should be done carefully.
*/
void ghashbase::fill_ll(int d) {
pair<bigint, bigint> coor = view->at(0, view->getymax()) ;
coor.second.mul_smallint(-1) ;
bigint s = 1 ;
s <<= d ;
coor.first += s ;
coor.second += s ;
int bitsreq = coor.first.bitsreq() ;
int bitsreq2 = coor.second.bitsreq() ;
if (bitsreq2 > bitsreq)
bitsreq = bitsreq2 ;
if (bitsreq <= d)
bitsreq = d + 1 ; // need to access llxyb[d]
if (bitsreq > llsize) {
if (llsize) {
delete [] llxb ;
delete [] llyb ;
}
llxb = new char[bitsreq] ;
llyb = new char[bitsreq] ;
llsize = bitsreq ;
}
llbits = bitsreq ;
coor.first.tochararr(llxb, llbits) ;
coor.second.tochararr(llyb, llbits) ;
}
/*
* This is the top-level draw routine that takes the root ghnode.
* It maintains four ghnodes onto which the screen fits and uses the
* high bits of llx/lly to project those four ghnodes as far down
* the tree as possible, so we know we can get away with just
* 32-bit arithmetic in the above recursive routine. This way
* we don't need any high-precision addition or subtraction to
* display an image.
*/
void ghashbase::draw(viewport &viewarg, liferender &rendererarg) {
/* AKT: call killpixels below
memset(pixbuf, 0, sizeof(ipixbuf)) ;
*/
ensure_hashed() ;
renderer = &rendererarg ;
if (!renderer->justState()) {
// AKT: get cell colors and alpha values for dead and live pixels
renderer->getcolors(&cellred, &cellgreen, &cellblue, &deada, &livea);
// rowett: create RGBA view
unsigned char *rgbaptr = (unsigned char *)cellRGBA;
// create dead color
*rgbaptr++ = cellred[0];
*rgbaptr++ = cellgreen[0];
*rgbaptr++ = cellblue[0];
*rgbaptr++ = deada;
// create live colors
unsigned int livestates = NumCellStates() - 1;
for (unsigned int ui = 1; ui <= livestates; ui++) {
*rgbaptr++ = cellred[ui];
*rgbaptr++ = cellgreen[ui];
*rgbaptr++ = cellblue[ui];
*rgbaptr++ = livea;
}
// remember the live state 1 color
state1RGBA = cellRGBA[1];
}
view = &viewarg ;
uvieww = view->getwidth() ;
uviewh = view->getheight() ;
if (view->getmag() > 0) {
pmag = 1 << (view->getmag()) ;
mag = 0 ;
viewh = ((uviewh - 1) >> view->getmag()) + 1 ;
vieww = ((uvieww - 1) >> view->getmag()) + 1 ;
uviewh += (-uviewh) & (pmag - 1) ;
} else {
mag = (-view->getmag()) ;
pmag = 1 ;
viewh = uviewh ;
vieww = uvieww ;
}
// AKT: must call killpixels after setting pmag
killpixels();
int d = depth ;
fill_ll(d) ;
int maxd = vieww ;
int i ;
ghnode *z = zeroghnode(d) ;
ghnode *sw = root, *nw = z, *ne = z, *se = z ;
if (viewh > maxd)
maxd = viewh ;
int llx=-llxb[llbits-1], lly=-llyb[llbits-1] ;
/* Skip down to top of tree. */
for (i=llbits-1; i>d && i>=mag; i--) { /* go down to d, but not further than mag */
llx = (llx << 1) + llxb[i] ;
lly = (lly << 1) + llyb[i] ;
if (llx > 2*maxd || lly > 2*maxd || llx < -2*maxd || lly < -2*maxd) {
goto bail ;
}
}
/* Find the lowest four we need to examine */
while (d > 0 && d - mag >= 0 &&
(d - mag > 28 || (1 << (d - mag)) > 2 * maxd)) {
llx = (llx << 1) + llxb[d] ;
lly = (lly << 1) + llyb[d] ;
if (llx >= 1) {
if (lly >= 1) {
ne = ne->sw ;
nw = nw->se ;
se = se->nw ;
sw = sw->ne ;
lly-- ;
} else {
ne = se->nw ;
nw = sw->ne ;
se = se->sw ;
sw = sw->se ;
}
llx-- ;
} else {
if (lly >= 1) {
ne = nw->se ;
nw = nw->sw ;
se = sw->ne ;
sw = sw->nw ;
lly-- ;
} else {
ne = sw->ne ;
nw = sw->nw ;
se = sw->se ;
sw = sw->sw ;
}
}
if (llx > 2*maxd || lly > 2*maxd || llx < -2*maxd || lly < -2*maxd) {
goto bail ;
}
d-- ;
}
/* At this point we know we can use 32-bit arithmetic. */
for (i=d; i>=mag; i--) {
llx = (llx << 1) + llxb[i] ;
lly = (lly << 1) + llyb[i] ;
}
/* clear the border *around* the universe if necessary */
if (d + 1 <= mag) {
ghnode *z = zeroghnode(d) ;
if (llx > 0 || lly > 0 || llx + vieww <= 0 || lly + viewh <= 0 ||
(sw == z && se == z && nw == z && ne == z)) {
// no live cells
} else {
drawpixel(0, 0) ;
renderbm(-llx, -lly) ;
}
} else {
z = zeroghnode(d) ;
maxd = 1 << (d - mag + 2) ;
if (maxd <= pmsize) {
maxd >>= 1 ;
drawghnode(sw, 0, 0, d, z) ;
drawghnode(se, -maxd, 0, d, z) ;
drawghnode(nw, 0, -maxd, d, z) ;
drawghnode(ne, -maxd, -maxd, d, z) ;
renderbm(-llx, -lly) ;
} else {
maxd >>= 1 ;
drawghnode(sw, llx, lly, d, z) ;
drawghnode(se, llx-maxd, lly, d, z) ;
drawghnode(nw, llx, lly-maxd, d, z) ;
drawghnode(ne, llx-maxd, lly-maxd, d, z) ;
}
}
bail:
renderer = 0 ;
view = 0 ;
}
static
int getbitsfromleaves(const vector<ghnode *> &v) {
unsigned short nw=0, ne=0, sw=0, se=0 ;
int i;
for (i=0; i<(int)v.size(); i++) {
ghleaf *p = (ghleaf *)v[i] ;
nw |= p->nw ;
ne |= p->ne ;
sw |= p->sw ;
se |= p->se ;
}
int r = 0 ;
// horizontal bits are least significant ones
unsigned short w = nw | sw ;
unsigned short e = ne | se ;
// vertical bits are next 8
unsigned short n = nw | ne ;
unsigned short s = sw | se ;
if (w)
r |= 512 ;
if (e)
r |= 256 ;
if (n)
r |= 2 ;
if (s)
r |= 1 ;
return r ;
}
/**
* Copy the vector, but sort it and uniquify it so we don't have a ton
* of duplicate ghnodes.
*/
static
void sortunique(vector<ghnode *> &dest, vector<ghnode *> &src) {
swap(src, dest) ; // note: this is superfast
sort(dest.begin(), dest.end()) ;
vector<ghnode *>::iterator new_end = unique(dest.begin(), dest.end()) ;
dest.erase(new_end, dest.end()) ;
src.clear() ;
}
using namespace std ;
void ghashbase::findedges(bigint *ptop, bigint *pleft, bigint *pbottom, bigint *pright) {
// following code is from fit() but all goal/size stuff
// has been removed so it finds the exact pattern edges
ensure_hashed() ;
bigint xmin = -1 ;
bigint xmax = 1 ;
bigint ymin = -1 ;
bigint ymax = 1 ;
int currdepth = depth ;
int i;
if (root == zeroghnode(currdepth)) {
// return impossible edges to indicate empty pattern;
// not really a problem because caller should check first
*ptop = 1 ;
*pleft = 1 ;
*pbottom = 0 ;
*pright = 0 ;
return ;
}
vector<ghnode *> top, left, bottom, right ;
top.push_back(root) ;
left.push_back(root) ;
bottom.push_back(root) ;
right.push_back(root) ;
int topbm = 0, bottombm = 0, rightbm = 0, leftbm = 0 ;
while (currdepth >= -2) {
currdepth-- ;
if (currdepth == -1) { // we have ghleaf ghnodes; turn them into bitmasks
topbm = getbitsfromleaves(top) & 0xff ;
bottombm = getbitsfromleaves(bottom) & 0xff ;
leftbm = getbitsfromleaves(left) >> 8 ;
rightbm = getbitsfromleaves(right) >> 8 ;
}
if (currdepth == -1) {
int sz = 1 << (currdepth + 2) ;
int maskhi = (1 << sz) - (1 << (sz >> 1)) ;
int masklo = (1 << (sz >> 1)) - 1 ;
ymax += ymax ;
if ((topbm & maskhi) == 0) {
ymax.add_smallint(-2) ;
} else {
topbm >>= (sz >> 1) ;
}
ymin += ymin ;
if ((bottombm & masklo) == 0) {
ymin.add_smallint(2) ;
bottombm >>= (sz >> 1) ;
}
xmax += xmax ;
if ((rightbm & masklo) == 0) {
xmax.add_smallint(-2) ;
rightbm >>= (sz >> 1) ;
}
xmin += xmin ;
if ((leftbm & maskhi) == 0) {
xmin.add_smallint(2) ;
} else {
leftbm >>= (sz >> 1) ;
}
} else if (currdepth >= 0) {
ghnode *z = 0 ;
if (hashed)
z = zeroghnode(currdepth) ;
vector<ghnode *> newv ;
int outer = 0 ;
for (i=0; i<(int)top.size(); i++) {
ghnode *t = top[i] ;
if (!outer && (t->nw != z || t->ne != z)) {
newv.clear() ;
outer = 1 ;
}
if (outer) {
if (t->nw != z)
newv.push_back(t->nw) ;
if (t->ne != z)
newv.push_back(t->ne) ;
} else {
if (t->sw != z)
newv.push_back(t->sw) ;
if (t->se != z)
newv.push_back(t->se) ;
}
}
sortunique(top, newv) ;
ymax += ymax ;
if (!outer) {
ymax.add_smallint(-2) ;
}
outer = 0 ;
for (i=0; i<(int)bottom.size(); i++) {
ghnode *t = bottom[i] ;
if (!outer && (t->sw != z || t->se != z)) {
newv.clear() ;
outer = 1 ;
}
if (outer) {
if (t->sw != z)
newv.push_back(t->sw) ;
if (t->se != z)
newv.push_back(t->se) ;
} else {
if (t->nw != z)
newv.push_back(t->nw) ;
if (t->ne != z)
newv.push_back(t->ne) ;
}
}
sortunique(bottom, newv) ;
ymin += ymin ;
if (!outer) {
ymin.add_smallint(2) ;
}
outer = 0 ;
for (i=0; i<(int)right.size(); i++) {
ghnode *t = right[i] ;
if (!outer && (t->ne != z || t->se != z)) {
newv.clear() ;
outer = 1 ;
}
if (outer) {
if (t->ne != z)
newv.push_back(t->ne) ;
if (t->se != z)
newv.push_back(t->se) ;
} else {
if (t->nw != z)
newv.push_back(t->nw) ;
if (t->sw != z)
newv.push_back(t->sw) ;
}
}
sortunique(right, newv) ;
xmax += xmax ;
if (!outer) {
xmax.add_smallint(-2) ;
}
outer = 0 ;
for (i=0; i<(int)left.size(); i++) {
ghnode *t = left[i] ;
if (!outer && (t->nw != z || t->sw != z)) {
newv.clear() ;
outer = 1 ;
}
if (outer) {
if (t->nw != z)
newv.push_back(t->nw) ;
if (t->sw != z)
newv.push_back(t->sw) ;
} else {
if (t->ne != z)
newv.push_back(t->ne) ;
if (t->se != z)
newv.push_back(t->se) ;
}
}
sortunique(left, newv) ;
xmin += xmin ;
if (!outer) {
xmin.add_smallint(2) ;
}
}
}
xmin >>= 1 ;
xmax >>= 1 ;
ymin >>= 1 ;
ymax >>= 1 ;
xmin <<= (currdepth + 3) ;
ymin <<= (currdepth + 3) ;
xmax <<= (currdepth + 3) ;
ymax <<= (currdepth + 3) ;
xmax -= 1 ;
ymax -= 1 ;
ymin.mul_smallint(-1) ;
ymax.mul_smallint(-1) ;
// set pattern edges
*ptop = ymax ; // due to y flip
*pbottom = ymin ; // due to y flip
*pleft = xmin ;
*pright = xmax ;
}
void ghashbase::fit(viewport &view, int force) {
ensure_hashed() ;
bigint xmin = -1 ;
bigint xmax = 1 ;
bigint ymin = -1 ;
bigint ymax = 1 ;
int xgoal = view.getwidth() ;
int ygoal = view.getheight() ;
if (xgoal < 8)
xgoal = 8 ;
if (ygoal < 8)
ygoal = 8 ;
int xsize = 2 ;
int ysize = 2 ;
int currdepth = depth ;
int i;
if (root == zeroghnode(currdepth)) {
view.center() ;
view.setmag(MAX_MAG) ;
return ;
}
vector<ghnode *> top, left, bottom, right ;
top.push_back(root) ;
left.push_back(root) ;
bottom.push_back(root) ;
right.push_back(root) ;
int topbm = 0, bottombm = 0, rightbm = 0, leftbm = 0 ;
while (currdepth >= 0) {
currdepth-- ;
if (currdepth == -1) { // we have ghleaf ghnodes; turn them into bitmasks
topbm = getbitsfromleaves(top) & 0xff ;
bottombm = getbitsfromleaves(bottom) & 0xff ;
leftbm = getbitsfromleaves(left) >> 8 ;
rightbm = getbitsfromleaves(right) >> 8 ;
}
if (currdepth == -1) {
int sz = 1 << (currdepth + 2) ;
int maskhi = (1 << sz) - (1 << (sz >> 1)) ;
int masklo = (1 << (sz >> 1)) - 1 ;
ymax += ymax ;
if ((topbm & maskhi) == 0) {
ymax.add_smallint(-2) ;
ysize-- ;
} else {
topbm >>= (sz >> 1) ;
}
ymin += ymin ;
if ((bottombm & masklo) == 0) {
ymin.add_smallint(2) ;
ysize-- ;
bottombm >>= (sz >> 1) ;
}
xmax += xmax ;
if ((rightbm & masklo) == 0) {
xmax.add_smallint(-2) ;
xsize-- ;
rightbm >>= (sz >> 1) ;
}
xmin += xmin ;
if ((leftbm & maskhi) == 0) {
xmin.add_smallint(2) ;
xsize-- ;
} else {
leftbm >>= (sz >> 1) ;
}
xsize <<= 1 ;
ysize <<= 1 ;
} else if (currdepth >= 0) {
ghnode *z = 0 ;
if (hashed)
z = zeroghnode(currdepth) ;
vector<ghnode *> newv ;
int outer = 0 ;
for (i=0; i<(int)top.size(); i++) {
ghnode *t = top[i] ;
if (!outer && (t->nw != z || t->ne != z)) {
newv.clear() ;
outer = 1 ;
}
if (outer) {
if (t->nw != z)
newv.push_back(t->nw) ;
if (t->ne != z)
newv.push_back(t->ne) ;
} else {
if (t->sw != z)
newv.push_back(t->sw) ;
if (t->se != z)
newv.push_back(t->se) ;
}
}
top = newv ;
newv.clear() ;
ymax += ymax ;
if (!outer) {
ymax.add_smallint(-2) ;
ysize-- ;
}
outer = 0 ;
for (i=0; i<(int)bottom.size(); i++) {
ghnode *t = bottom[i] ;
if (!outer && (t->sw != z || t->se != z)) {
newv.clear() ;
outer = 1 ;
}
if (outer) {
if (t->sw != z)
newv.push_back(t->sw) ;
if (t->se != z)
newv.push_back(t->se) ;
} else {
if (t->nw != z)
newv.push_back(t->nw) ;
if (t->ne != z)
newv.push_back(t->ne) ;
}
}
bottom = newv ;
newv.clear() ;
ymin += ymin ;
if (!outer) {
ymin.add_smallint(2) ;
ysize-- ;
}
ysize *= 2 ;
outer = 0 ;
for (i=0; i<(int)right.size(); i++) {
ghnode *t = right[i] ;
if (!outer && (t->ne != z || t->se != z)) {
newv.clear() ;
outer = 1 ;
}
if (outer) {
if (t->ne != z)
newv.push_back(t->ne) ;
if (t->se != z)
newv.push_back(t->se) ;
} else {
if (t->nw != z)
newv.push_back(t->nw) ;
if (t->sw != z)
newv.push_back(t->sw) ;
}
}
right = newv ;
newv.clear() ;
xmax += xmax ;
if (!outer) {
xmax.add_smallint(-2) ;
xsize-- ;
}
outer = 0 ;
for (i=0; i<(int)left.size(); i++) {
ghnode *t = left[i] ;
if (!outer && (t->nw != z || t->sw != z)) {
newv.clear() ;
outer = 1 ;
}
if (outer) {
if (t->nw != z)
newv.push_back(t->nw) ;
if (t->sw != z)
newv.push_back(t->sw) ;
} else {
if (t->ne != z)
newv.push_back(t->ne) ;
if (t->se != z)
newv.push_back(t->se) ;
}
}
left = newv ;
newv.clear() ;
xmin += xmin ;
if (!outer) {
xmin.add_smallint(2) ;
xsize-- ;
}
xsize *= 2 ;
}
if (xsize > xgoal || ysize > ygoal)
break ;
}
if (currdepth < 0){
xmin >>= -currdepth ;
ymin >>= -currdepth ;
xmax >>= -currdepth ;
ymax >>= -currdepth ;
} else {
xmin <<= currdepth ;
ymin <<= currdepth ;
xmax <<= currdepth ;
ymax <<= currdepth ;
}
xmax -= 1 ;
ymax -= 1 ;
ymin.mul_smallint(-1) ;
ymax.mul_smallint(-1) ;
if (!force) {
// if all four of the above dimensions are in the viewport, don't change
if (view.contains(xmin, ymin) && view.contains(xmax, ymax))
return ;
}
int mag = - currdepth - 1 ;
while (xsize <= xgoal && ysize <= ygoal && mag < MAX_MAG) {
mag++ ;
xsize *= 2 ;
ysize *= 2 ;
}
view.setpositionmag(xmin, xmax, ymin, ymax, mag) ;
}
void ghashbase::lowerRightPixel(bigint &x, bigint &y, int mag) {
if (mag >= 0)
return ;
x >>= -mag ;
x <<= -mag ;
y -= 1 ;
y >>= -mag ;
y <<= -mag ;
y += 1 ;
}
|