23
APPLICATIONS OF SPATIAL DATA STRUCTURES: S COMPUTER GRAPHICS, IMAGE PROCESSING, AND GEOGRAPHIC INFORMATION SYSTEM (REVISED) t C Hanan Same omputer Science Department C University of Maryland ollege Park, Maryland 20742 A Abstract comprehensive presentation is made of data structures used to represent v spatial data. Spatial data consists of points, lines, rectangles, regions, surfaces, and olumes. Such data is used in applications in computer graphics, computer vision, m data base management systems, computer-aided design, robotics, geographic infor- ation systems, image processing, computational geometry, pattern recognition, , o and other areas. The focus is on hierarchical data structures such as quadtrees ctrees, and other related hierarchical representations. These representations are f based on the principle of recursive decomposition. A number of operations drawn rom applications in which such data structures find use are examined in greater F detail. ebruary 12, 1996 Copyright 1990 by Hanan Samet. No part of this book (including the figures) a may be reproduced, stored in a retrieval system, or transmitted, in any form or by ny means, electronic, mechanical, photocopying, recording, or otherwise, without N the express written prior permission of the author. ote to the reader: All figures and tables are referred to by a label of the form r t XX.nn where XX refers to a collection of Figures and nn is the actual figure o able number. All figures and tables are found at the end of each chapter. In gen- u eral, for a figure or table that is referred to by the label XX.nn , XX is found on the pper right corner of the page while the figure or table is labeled with nn . 1

APPLICATIONS OF SPATIAL DATA STRUCTURES ...hjs/cmsc725/schrack.pdfCOMPUTER GRAPHICS, IMAGE PROCESSING, AND GEOGRAPHIC INFORMATION SYSTEMS (REVISED) t C Hanan Same omputer Science Department

  • Upload
    hakhanh

  • View
    216

  • Download
    2

Embed Size (px)

Citation preview

APPLICATIONS OF SPATIAL DATA STRUCTURES:SCOMPUTER GRAPHICS, IMAGE PROCESSING, AND GEOGRAPHIC INFORMATION SYSTEM

(REVISED)

tC

Hanan Sameomputer Science Department

CUniversity of Maryland

ollege Park, Maryland 20742

A

Abstract

comprehensive presentation is made of data structures used to represent

vspatial data. Spatial data consists of points, lines, rectangles, regions, surfaces, andolumes. Such data is used in applications in computer graphics, computer vision,

mdata base management systems, computer-aided design, robotics, geographic infor-

ation systems, image processing, computational geometry, pattern recognition,,

oand other areas. The focus is on hierarchical data structures such as quadtreesctrees, and other related hierarchical representations. These representations are

fbased on the principle of recursive decomposition. A number of operations drawnrom applications in which such data structures find use are examined in greater

F

detail.

ebruary 12, 1996

Copyright 1990 by Hanan Samet. No part of this book (including the figures)

amay be reproduced, stored in a retrieval system, or transmitted, in any form or byny means, electronic, mechanical, photocopying, recording, or otherwise, without

N

the express written prior permission of the author.

ote to the reader: All figures and tables are referred to by a label of the formr

tXX.nn where XX refers to a collection of Figures and nn is the actual figure oable number. All figures and tables are found at the end of each chapter. In gen-

ueral, for a figure or table that is referred to by the label XX.nn , XX is found on thepper right corner of the page while the figure or table is labeled with nn .

1

CHAPTER 3

S

3

3.1.

ALTERNATIVE QUADTREE REPRESENTATION

.1.1.

3.1.2. CONSTRUCTING AN FD LOCATIONAL CODE (insert on p. 41 at theend of Sec. 2.1.1)

There are many ways of constructing the locational code. At present,FD

-cfrom the standpoint of computational complexity, all the methods are asymptotially the same. In other words, given a 2 ×2 image, they require O (n ) time.n n

ptNevertheless, some practical improvements are possible by use of table lookuechniques and shift operations. In this section, we will only discuss the quadtree.

EHowever, the extension to octrees and higher dimensions is straightforward (see

xercises 2.1.2.5 and 2.1.2.6).

The simplest way to construct the locational code is to do it directly fromtthe quadtree. This is similar to the technique described in procedure . ACODE1

evtimes, it is preferable to construct the code directly from the x and y coordinatalues of the upper leftmost pixel in each block (assuming that the origin is in the

tupper left corner of the image). This technique is known as bit interleaving. Inhis case, there are several ways to proceed. The simplest is to apply a variant of

procedure to the binary representations of the x and y coordinate values inCODE1

.an alternating manner

A somewhat more straightforward approach is to convert the x and yn

tcoordinate values individually to their interleaved coordinates under the assumptiohat the other value is zero, and then apply a logical or (i.e., lor) operation to the

tresults. An even simpler approach is to temporarily ignore the distinction betweenhe x and y coordinates and interleave each value with zero (i.e., insert a 0 to the

eoleft of each bit position). For example, assuming n =3, 6 becomes 010100. Thperation of inserting the intervening leading zeros is termed dilation, and the

epresulting number is called a dilated integer by Schrack [Schr92], while the oppositrocess of converting a dilated integer to its conventional representation is termed

,scontraction [Schr92]. Next, assuming that the y coordinate is the most significanthift the dilated integer corresponding to the y coordinate value by one bit to the

rcleft, and then perform a logical or (i.e., lor) operation on it and the dilated integeorresponding to the x coordinate value.

As an example of this techniques, suppose that n =6 and we want to com-tpute the locational code of the 2×2 block whose upper leftmost pixel is aFD

(14,26). In this case, the binary representation of the pixel’s coordinate values is

d(001110,011010). Applying dilation to 14 yields 000001010100 while applyingilation to 26 yields 000101000100. Now, shifting the dilated integer correspond-

aing to the y coordinate value by one to the left (assuming that the incoming bit is

0) and applying a logical or (i.e., lor) operation with the value of the x coordi-nate yields 001011011100 which is 732 in base 10.

The drawback of the above technique is that the process of forming the

bdilated integer also takes O (n ) time. Nevertheless, this process can be speeded upy using table lookup methods. For a 2 ×2 image, we can obtain the loca-n n DF

hptional code by use of a two-dimensional table which contains one entry for eacossible pair of x and y coordinate values. Such a table requires 2 ×2 entriesn n

ybwhich is clearly prohibitive. However, we can reduce the table size significantly performing the lookup operation more than once [Shaf90d]. In particular, for a

e2 ×2 image, we can decompose the group of bits comprising each coordinatn n

m m esvalue into n /m subgroups of m bits, and use a table with 2 ×2 entries. Thesubgroups of m bits are successively looked up in the table, and the results are

flconcatenated to form the desired locational code. This process also makes use oogical or (i.e., lor), logical and (i.e., land), and shift (i.e., lsh) operations. The

result is that the decoding now takes time proportional to n /m with 2 space.2.m

ot

The space requirements can be reduced further by applying table lookup the subgroups of x and y coordinate values separately, and then applying a logical

.Ior (i.e., lor) operation prior to reapplying the process to the remaining subgroupsn such a case, we would need two tables of 2 entries apiece. The space require-m

m hsments can be reduced even further to just one table of size 2 by looking up botubgroups of x and y coordinate values in the same table (i.e., obtaining their

tsdilated integer representations), shifting the coordinate that is deemed the mosignificant by one bit to the left, and then applying a logical or (i.e., lor) operation

prior to reapplying the process to the remaining subgroups.

For example, if n =12, then using subgroups of 6 bits means that we only-require a table of 2 =64 entries to obtain the locational code with just two itera6 FD

4 eotions. Using subgroups of 4 bits requires a table of just 2 =16 entries, and wbtain the locational code with three iterations.

A

FD

n implementation of the above encoding process is given below by pro-cedure . It makes use of table , illustrated in Table D.1 for m =4, toENCODE DIL

.

i

yield the dilated integer representation of the groups of x and y coordinate values

iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii

i Table D.1. DIL[I].iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii

i I 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiDIL[I] 0 1 4 5 16 17 20 21 64 65 68 69 80 81 84 85iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiccccc

ccc

ccc

ccc

ccc

ccc

ccc

ccc

ccc

ccc

ccc

ccc

ccc

ccc

ccc

ccc

ccc

ccccc

/fdilocationalcode procedure ENCODE(N,M,X,Y);* Given a 2 ×2 image, apply bit

i

N N

nterleaving in groups of M bits to compute the FD locational code of the

oblock whose upper leftmost pixel is at location (X,Y) (assuming that therigin is in the upper leftmost corner of the image). DIL is a table

rnindicating a mapping from integers to their dilated integer values foumbers ranging from 0 to 2↑M −1. The y coordinate is more

/b

significant than the x coordinate. Assume that N is divisible by M. *eginvalue integer X,Y,N,M;

;global integer array DIL[0:2↑M −1]

3-2

finteger I,MASK;dilocationalcode P;

;PMASK ← 2↑M − 1

← 0;for I←0 step 1 until (N/M)−1 do

beginP ← P lor ((DIL[X land MASK] lor (DIL[Y land MASK] lsh 1)) lsh 2∗M∗I);

YX ← X lsh −M; /* next subgroup of M bits of x */

← Y lsh −M; /* next subgroup of M bits of y */

rend;

eturn(P);end;

FDDecoding an locational code to obtain the x and y coordinate values of-

cthe upper leftmost pixel in the relevant block is the reverse of the encoding proess. Once again, for a 2 ×2 image, this will require O (n ) time. The straightfor-

w

n n

ard approach is to process the locational code two bits at a time, thereby obtain-

sing the values of the x and y coordinates one bit at a time. This process can bepeeded up by applying table lookup methods in an analogous manner to that used

elin conjunction with the encoding method. The difference is that the tables ararger. In particular, if we want to use the same number of groups as we did in the

euencoding process, then each subgroup will have to be twice as large as the onsed in the encoding process (see Exercise 2.1.2.1).

ec

The speeded-up decoding process requires two tables - one for each of thoordinate values. Let m denote the number of bits of the locational code thatFD

tswe are decoding at one time. Assuming that the y coordinate is the mosignificant, we can reduce the number of tables to one by shifting the locationalFD

.Tcode by one bit to the right before the table lookup operation for the y coordinate

he size of the table is 2 since we are only interested in m −1 of the bits inm −1

.either case (i.e., for the x and y coordinate)

The size of the table can be reduced further by using the dilated integer

drepresentation of the x and y coordinate values in the table lookup operation. The

ilated integer is calculated by applying a logical and (i.e., land) operation to the,F locational code with a mask containing a 0 and 1 in alternating positions - i.e.D

n e2for n =6, the mask is 010101010101 which is equal to (4 −1)/3 (see Exercis.1.2.2). When we are decoding m bits at a time, then the mask is only m bits

slong. The mask is also the largest possible dilated integer - i.e., (2 −1)/3. Thum

t m m −1he table need only contain (2 −1)/3+1 entries instead of 2 . Of course, mshould be a multiple of the dimension of the image (i.e., 2 in this case).

For example, if n =12, then using two iterations (i.e., m =12) requires a,table of (2 −1)/3+1=1366 entries. On the other hand, using four iterations (i.e.12

6m =6) requires a table of (2 −1)/3+1=22 entries which is more reasonable. Usingtsix iterations (i.e., m =4) requires a table of (2 −1)/3+1=6, entries which is no4

.really worth the added expense of separate table lookup and shifting operations

An implementation of the above decoding process is given below by pro-cedure . It makes use of table , illustrated in Table D.2 for m =6, toDECODE CON

soyield the integer representation of the dilated integers corresponding to the groupf interleaved x and y coordinate values. Entries with a ‘‘?’’ are undefined as

they do not correspond to valid dilated integers.

3-3

iiiiiiiiiiiiiiiiiiiTable D.2. CON[I].

iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiI CON[I] iiiiiiiiiiiiiiiiiii0 0

121

??

43

23

65

??

87

??

190 ?

?1112 ?

?1134 ?

?1156 4

51178 ?

?2190 6

7i21iiiiiiiiiiiiiiiiiicccccccccccccccccccccccccccccc

cccccccccccccccccccccccccccc

cccccccccccccccccccccccccccccc

/procedure DECODE(P,N,M,X,Y);* Given a 2 ×2 image, decode the FD

l

N N

ocational code P in groups of M bits to compute (X,Y) which is the location

lof the upper leftmost pixel (assuming that the origin is in the uppereftmost corner of the image) in the block corresponding to P. CON is a

vtable indicating a mapping from dilated integers to the actual integeralues of numbers ranging from 0 to 2 −1. The yM/2

t2coordinate is more significant than the x coordinate. Assume tha.N is divisible by M, and that M is divisible by 2. */

beginvalue fdilocationalcode P;

rvalue integer N,M;eference integer X,Y;

;iglobal integer array CON[0:(2↑M −1)/3]nteger I,MASK;

MASK ← (2↑M −1)/3; /* Calculate mask to extract dilated integer */

YX ← 0;

← 0;for I←0 step 1 until (2∗N/M)−1 do

beginX ← X lor ((CON[P land MASK]) lsh M∗I/2);

;PY ← Y lor ((CON[(P lsh −1) land MASK]) lsh M∗I/2)

← P lsh −M; /* next subgroup of M bits of P */end;;

Eend

xercises

3.1: Suppose that an locational code is being decoded by use of table lookupt

FDechniques with subgroups of bits. Show that if the decoding process uses the

esame number of subgroups as the encoding process, then the number of bits inach subgroup is twice the number of bits in the encoding process.

l3.2: Given a 2 ×2 image, show that the maximum dilated integer value of a pixen n

n .coordinate is (4 −1)/3

3-4

.3.3: Modify procedure to deal with the case that is not a divisor ofENCODE M N

N3 DECODE M.4: Modify procedure to deal with the case that is not a divisor of 2. .

3.5: Generalize procedure to deal images of arbitrary dimension (say ).ENCODE D

D3 DECODE.6: Generalize procedure to deal images of arbitrary dimension (say ).

nt3.7: [Donald E. Knuth] How would you add two dilated integers, say a and b , iwo-dimensions.

3-5

4

N

CHAPTER

EIGHBOR FINDING TECHNIQUES

4

4.1.

.2.

4.3. NEIGHBOR FINDING IN POINTER-LESS REPRESENTATIONS

)r

In Chapter 3 we presented a number of pointer-less quadtree (octreeepresentations. Although their use may lead to significant savings in space, they

sbvary in the ease with which they can be manipulated. In particular, as we discuselow, performing neighbor finding requires quite a bit of work with some of them,

balthough it is possible. Recall that pointer-less quadtree (octree) representations cane grouped into two categories. The first treats the image as a collection of leaf

onodes while the second represents the image in the form of a traversal of the nodesf its tree.

In this section, we concentrate on the locational code because it is theFD

egmost efficient. We give algorithms for finding neighbors in all directions for theneral case of a d -dimensional image and for a quadtree that are represented by

ethe locational code. We also show how to do neighbor finding when only thFD

FD locational codes of the black nodes are stored. Next, we briefly discuss the useeof and linear quadtrees. We conclude with an equally brief mention of thFL VL

DFuse of -expressions. Recall that the latter is a traversal of the nodes of the tree.The sections dealing with and linear quadtrees and -expressions are veryFL VL DF

.

4

specialized. They are included primarily for completeness and can be skipped

.3.1. FD LINEAR QUADTREE

When an image is represented as a collection of the leaf nodes comprisingeit, there are a number of methods of representing the individual leaf nodes. In th

linear quadtree (octree), each leaf node (black and white) is represented by itsF

FD

D locational code. Assuming an image of side length 2 , there are two equivalentn

kw FDays to define the locational code of each leaf node of side length 2 . The firsttechnique treats the locational code as being n digits long, where the leadingFD

n −k digits contain the directional codes that locate the leaf along a path from thet

lroot of the tree. The k trailing digits contain the directional codes (usually 0) thaocate the pixel in the block’s ( ) corner. The second technique treats the

F

NW LDB

D locational code as having 2.n (3.n ) bits for a quadtree (octree) and is obtaineds

oby interleaving the bits that comprise the values of the x and y (and z ) coordinatef the pixel (voxel) in the block’s ( ) corner. In the rest of this section, weNW LDB

.assume the bit interleaving definition

4-1

The directional codes are numeric equivalents of the different quadrants,(i.e., 0, 1, 2, 3 for , , , , respectively) and octants (i.e., 0, 1, 2, 3, 4, 5, 6NW NE SW SE

LDB LDF LUB LUF RDB RDF RUB RUF eo7 for , , , , , , , , respectively). We assume that thrigin is in the ( ) corner of the block. Therefore, for a block of side lengthNW LDB

FD2k , the directional codes stored in the k trailing digits are 0. When the loca-tional code is viewed as the result of bit interleaving, a block of side length 2 isk

mvrepresented by its pixel (voxel) for which bit interleaving yields the minimualue in the block.

A node’s locational code is implemented by a record of typeFD

CODE LEV COL emf d hlocationalcode with 3 fields , , and corresponding to th

inimum bit-interleaved value in the node’s block, the level of the node, and theecolor of the node, respectively. For an image of maximum side length 2 , thn

c Pode, say , is implemented as an integer.

Given a node with locational code , finding its neighbor in directionI

A FD P

Aof size greater than or equal to is conceptually simple. During this process, wefirst compute , the code component of the locational code of the equal-sizedT FD

d Qesired neighbor, say . The result is analogous to calculating the address of the

vpixel (voxel) in the desired neighboring block with the minimum bit-interleavedalue, except that it is more complex since we are not dealing with the individual

tvalues of the x , y (and z ) coordinates of the address. Instead, we are dealing withhe interleaved binary representations of the individual coordinate values.

rs

The address of a neighbor of equal size can be calculated by adding oubtracting the size of the block to the individual coordinate values, depending on

-the direction of the desired neighbor. Since the block is represented by its locaFD

dttional code, this process is not so easy. In particular, it would seem that we neeo unpack the individual coordinate values, increment or decrement them as neces-

usary, and then repack them to form the new interleaved value. This process makesse of procedures and described in Section 2.1.1.

T

DECODE ENCODE

ocher [Toch54] describes a method of performing the addition and sub-traction that does not require unpacking the individual coordinate values of the FD

.Ilocational code. This method is applied to neighbor finding by Schrack [Schr92]t takes advantage of the observation that if we only want to increment the value of

lthe x coordinate component of the locational code, then we can use normaFD

addition by preceding it with a logical or (i.e., lor) operation that loads 1s in everye

cbit position that does not correspond to the x coordinate. This will propagate tharry bit between the bit positions that correspond to the x coordinate, and will

,wskip over the bit positions corresponding to the remaining coordinates. Of course

e must also save the values of the bit positions that do not correspond to the xcoordinate prior to the addition, and restore them after the addition.

The exact sequence of operations is as follows. For the moment, let us

cassume that we are only incrementing the value of the x coordinate of the codeomponent, say , of locational code , and that our increment has the value 1.C FD P

E faThis means that we are only considering neighbors in the direction in the case o

quadtree and in the direction in the case of an octree. Let be a mask thatR Mx

o

bhas a 1 in every bit position that correspond to the x coordinate. Similarly, let M

e a mask that has a 1 in every bit position that does not correspond to the x coor-dinate (for an alternative, see Exercise 3.4.1.3).

4-2

x(1) Save in S the values of the bit positions that do not correspond to thecoordinate by applying a logical and (i.e., land) operation to and .C Mo

( C2) Load a 1 in every bit position of that does not correspond to the x coordi-nate by applying a logical or (i.e., lor) operation to and .C Mo

C.((3) Add 1 to4) Load a 0 in every bit position of that does not correspond to the x coordi-C

MC x .(

nate by applying a logical and (i.e., land) operation to and5) Reset the values of the bit positions of that do not correspond to the xC

r(coordinate to their values at the start of step (1) by applying a logical oi.e., lor) operation to and .

C

C S

E ec

will now reflect the x coordinate value of the neighbor in the direction in thase of a quadtree and in the direction in the case of an octree. In order to

iR

ncrement the other coordinate values (still assuming that the value of the incre-

ament is 1), we simply reapply steps (1)-(5) to each coordinate value with theppropriate masks. Note that the increment in step (3) differs depending on the

tcoordinate. In particular, the increment is really 2 for the coordinate thai

c thorresponds to the i least significant bit (i =0 for the least significant coordinatewhich is x in our example).

Now, if we want to find neighbors in more than one direction (i.e., thes

mvalue of several coordinates will change), then we simply apply steps (1)-(5) a

any times as we have directional changes. Moreover, at each successive applica-ttion of steps (1)-(5), we use the value of from the previous application. Thus aC

t Che end of this process, has the desired value. Note that this process is

aequivalent to incrementing the individual coordinates separately and then applying

logical or (i.e., lor) operation to all of them.

lSo far, we have only shown how to perform addition on the locationaFD

,acode. Moreover, the value of our increment was restricted to 1. For a quadtreessuming that our origin is in the upper leftmost corner of each block, this means

.that we can compute pixel-size neighbors of pixels in the , , and directionsE S SE

LDB -pSimilarly, for an octree, assuming that the origin is in the corner, we can comute voxel-size neighbors of voxels in the , , , , , , and directions.R U F RU UF RF RUF

efHowever, we need to be able to compute neighbors of greater than or equal sizor nodes of all sizes (not just pixels and voxels), and in all directions. We will

now show how to remove these restrictions in a step-by-step manner.

Computing a neighbor in an arbitrary direction requires that we be able toe

aperform subtraction. Subtraction is quite straightforward once we realize that thddition process described in steps (1)-(5) will work provided that negative

snumbers are represented using twos complement notation. Of course, when usingubtraction, the increment in step (3) is negated. However, when combining

sseveral directions (e.g., ) we must be careful to negate only the bit positionNE

corresponding to the negative direction (but see Exercise 3.4.1.9).

lFor example, suppose that we wish to compute the neighbor of a pixeNE

FD gtin a 16×16image at position (11,10) with locational code 11001110 (assuminhat x is the least significant coordinate). The neighbor is represented by an

i N

E

ncrement of 1, while the neighbor is represented by an increment of -2. The-

tresulting combined increment has a value of (−1,1) which has a binary representaion of 10101011. It is not the same as combining increments of 1 and −2 in step

lc(3) to yield −1. Applying step (1)-(5) as many times as we have directionahanges (i.e., twice) yields 11010010 which is position (12,9) as expected.

4-3

In order to facilitate the computation of the increment, we make use of the.function to contain the values of the increments for all possible directionsINC

INC 4 4 ngTable D.3 contains the definition of for a 2 ×2 two-dimensional image. Ieneral, for a d -dimensional image, has 3 −1 different values (see ExerciseINC d

3.4.1.12).

iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii

i Table D.3. INC(I).iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii

i I N E S W NW NE SW SEiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiINC(I) 170 1 2 85 255 171 87 3iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiccccc

ccc

ccc

ccc

ccc

ccc

ccc

ccc

ccc

ccccc

so

Now, let us turn our attention to computing equal-size neighbors for nodef arbitrary size (i.e., side length 2 where j >0). This is quite easy and we once

a

j

gain use steps (1)-(5). The only difference is that, given an image of dimensions

nd (e.g., d =2 for a quadtree and d =3 for an octree), the increment in step (3) iow 2 or −2 for the coordinate that corresponds to the i least significant

b

d . j +i d . j +i th

it (recall that in our example the x coordinate corresponds to the least significant,bit - i.e., i =0). The negative increment is used when the direction is negative (i.e.

and in the case of a quadtree, and , , and in the case of an octree). TheN W L D B

INC tbincrement is obtained by applying the function and then multiplying the resuly 2 or simply by shifting the result to the left by d .j bits.d . j

C FD ro

Once the code component, say , of the locational code of the neighbof equal size (of the node with locational code ) has been computed, we must

dFD P

etermine if it actually exists, as well as its color. Assume, without loss of gen-ferality, that the collection of locational codes, say , is implemented as a list oFD L

DL F acrecords of type f dilocationalcode . Search for the locational code withode that has the maximum value that is still less than or equal to that of , say -C R

Ci CODE R C R.e., ( )≤ . If the level of is greater than or equal to that associated with

d(i.e., ( )), then ’s node contains the node represented by , and is returneLEV P R C R

F

1

Das the locational code of the neighbor. For example, in Figure the path to theenorthern neighbor of node 7 (with locational code 210) is 032. However, in thFD

FD FD ,wlist of locational codes for this image, the nearest locational code is 000

hich corresponds to node 1 which is larger than node 7.

dn

Otherwise, there is no leaf node in the tree that corresponds to the desireeighbor, and the neighbor is gray (see Exercise 3.4.1.1). In this case, we return

ean locational code for a gray node at level ( ) with as the path from thFD LEV P C

fnroot to it. For example, in Figure the code for the equal-size eastern neighbor oode 13 (with locational code 300) is 210. 210 is present in the list of loca-

tFD FD

ional codes for this image and it corresponds to node 7. However, node 7 ist

msmaller than 13 (i.e., its level is smaller) and thus we return its locational code bu

ark it as gray and set the field to the same value as that of 13.

W

LEV

hen the node whose neighbor is sought is along the border of the space-

srepresented by the image, then, depending on the direction, the neighbor may posibly not exist (e.g., the neighbor of node 5 in Figure 3.1). In such a case, our

pE

rocedure will perform a wraparound in the sense that it will return 1 as thehhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh

Recall that we use the convention that for a 2 ×2 image the root is at level n while a pixel is at level0

1 n n

.

4-4

noneighbor. This is because we are performing arithmetic and we have relied overflow being ignored. This is pretty standard especially when using a twos com-

tcplement representation for negative numbers. If this is not desired, then we musheck for overflow after each iteration of step (3) (see Exercise 3.4.1.10). In par-

dticular, if the direction is positive, then overflow implies wraparound while if theirection is negative, then the absence of an overflow implies wraparound.

tGiven a 2 ×2 image whose quadtree contains m leaf nodes that are kepn n

FDin a list sorted by their locational codes, the neighbor computation processsdescribed above has a worst-case execution time of O (log m +n ). This assume2

-cthat the addition of two n bit numbers takes O (n ) time. However, from a practial standpoint we can say that it is constant since the word size is fixed and we

eeassume that addition is a unit operation. Under such an assumption, the worst-casxecution time is O (log m ).2

GENhFDhNEIGHBOR nb

Now let us look at the general case. Procedure , giveelow, is used to calculate the locational code of the neighbor of a node, say ,

F

FD A

D P FD .Iwith locational code , in all directions in a d -dimensional linear quadtreet implements steps (1)-(5) of the algorithm described above. The masks needed

efor steps (2) and (4) are represented by the mask array and the variablMASK2

MASK4, respectively. They do not need to be recomputed each time procedureG is invoked. Instead, global values can be used.ENhFDhNEIGHBOR

MASK4 Mxcorresponds to - i.e., it has a 1 in every bit position that

tcorresponds to the x coordinate. It is computed by observing that it is the sum ofhe powers of 2 - i.e., 2 where i ranges from 0 to n −1. This sum has the value

d

d di

n dy zM M g(2 −1)/(2 −1). The remaining masks (i.e., , , . . . ) are obtained by shiftin

to the left by a number equal to the coordinates bit position relative to that ofMx

MASK2 Mo eithe x coordinate. Mask [i ] corresponds to the values of for coordinat. It is computed by complementing (i.e., lnot which is a logical not operation) the

value of and then applying a left circular shift of i bits.MASK4

The increment in step (3) is computed by shifting the result of the applica-tion of function to the left by a number of bits equal to the size of the nodeINC

A INC rawhose neighbor is being sought, i.e., . Function was given in Table D.3 fo

2 ×2 two-dimensional image. In general, for a d -dimensional image, has4 4 INC

3d INC−1 different values. can be precomputed for a d -dimensional image by-

icomputing its values for the 2.d different (d −1)-dimensional adjacencies comprisng it. For example, in two (three) dimensions these are , , , and ( , , , ,

B F

N S E W L R D U

, and ). These adjacencies can be considered as pairs of principal directionsr

cwhere one corresponds to a unit step in the positive direction while the otheorresponds to a unit step in the negative direction.

re

Of course, these directions are really the same as the coordinates. Foxample, assuming that the direction of the x coordinate is the least significant, we

have that a unit step in the positive x direction has an value of 1 while a stepINC

INC Σ in −1 d0i = sin the negative x direction has an value of 2 , which is the same a

. Assigning an order for these directions means that for an image of sideMASK4

INC il nength 2 , positive direction i (0≤i <d ) has an entry of 2 while negative direc--tion i has an entry of 2 . . The values of combinations of the princiINC MASK4 INC

N

i

W LDBpal directions (e.g., , , etc.) are obtained by applying logical or (i.e., lor) oraddition operations to the values of the component principal directions.INC

4-5

;/fdilocationalcode procedure GENhFDhNEIGHBOR(D,N,L,I,P)* Given a D-dimensional quadtree of a side of width 2N

Dlrepresented by a linear quadtree in the form of a list L, of the Focational codes of its nodes, return the FD locational code of the

onneighbor in direction I of a node with FD locational code P. If neighbor exists, then NIL is returned. Assume N>0. */

beginvalue integer D,N;

;vvalue pointer fdilist Lalue direction I;

value pointer fdilocationalcode P;

ipointer fdilocationalcode B,Q;nteger C,J,MASK4;

;iinteger array MASK2[0:D−1]f LEV(P) ≥ N then return(NIL); /* No neighbor exists in direction I */

MMASK4 ← (2↑(D∗N) −1)/(2↑D−1); /* Calculate mask for step (4) */

ASK2[0] ← lnot(MASK4); /* Take 1’s complement *//for J←1 step 1 until D−1 do /* Calculate masks for step (2) *

MASK2[J] ← MASK2[J−1] lcsh 1; /* Left circular shift by one bit */

fC ← 0;or J←0 step 1 until D−1 doC ← C lor (((CODE(P) lor MASK2[J]) + ((INC(I) lsh LEV(P)∗D) land (MASK4 lsh J)))

B!land (MASK4 lsh J));

← MAXLEQ(C,L); /* Find maximum FD locational code in L that is ≤C */

eif LEV(B)≥LEV(P) then return(B)lse /* The neighbor is smaller; create a new gray node */begin

Q ← create(fdilocationalcode);

LCODE(Q) ← C;

EV(Q) ← LEV(P);;

rCOL(Q) ← ‘GRAY’eturn(Q);

end;;end

GENhFDhNEIGHBORProcedure can be streamlined for the case when d =2.In particular, there is no need for the array since in the case of the x coor-MASK2

MM MASK4o o sdinate, is twice the value of and in the case of the y coordinate, i. There is also no need for a loop running through the two dimensionsMASK4

J GENhFDhNEIGHBOR QTh-(indexed by in ). The result is given below by procedure.FDhNEIGHBOR

fdilocationalcode procedure QThFDhNEIGHBOR(N,L,I,P);/* Given a 2 ×2 image represented by a linearN N

quadtree in the form of a list L, of the FD locational codes of itsa

nnodes, return the FD locational code of the neighbor in direction I ofode with FD locational code P. If no neighbor exists, then NIL is

breturned. Assume N>0. */

eginvalue integer N;

;vvalue pointer fdilist Lalue direction I;

value pointer fdilocationalcode P;

ipointer fdilocationalcode B,Q;nteger C,MASK4;

if LEV(P) ≥ N then return(NIL); /* No neighbor exists in direction I */

CMASK4 ← (4↑N −1)/3; /* Calculate mask for step (4) */

← (((CODE(P) lor (MASK4 lsh 1)) + ((INC(I) lsh LEV(P)∗2) land MASK4)) land MASK4) lor

B!(((CODE(P) lor MASK4) + ((INC(I) lsh LEV(P)∗2) land MASK4∗2)) land MASK4∗2);

← MAXLEQ(C,L); /* Find maximum FD locational code in L that is ≤C */if LEV(B)≥LEV(P) then return(B)

4-6

/else /* The neighbor is smaller; create a new gray node *begin

Q ← create(fdilocationalcode);

LCODE(Q) ← C;

EV(Q) ← LEV(P);;

rCOL(Q) ← ‘GRAY’eturn(Q);

end;;end

FDSuppose that we only store the locational codes of the black nodes (ass

cproposed by Gargantini [Garg82a]). In such a case, determining the neighbor’olor and size is more complex. Assume an image of side length 2 . Moreover,n

E

fifor the purpose of the following explanation, let us treat the value of the COD

eld, say , as if it is an array of type f d hpath of n directional codes stored in theF

F

F F F A FD lcorder [n −1] [n −2]... [1] [0]. To find the neighbor of node , with locationaode , of size greater than or equal to , we first construct the code component ofP A

Q LEV P .the equal-sized neighbor, say , with level k = ( )

Assume that an equal-sized neighbor exists (i.e., we are not at the borderof the image), and let denote its code component. Now, search the list ofC FD

l L B H FDocational codes , using binary search, for and , the locational codes whosecode components have the maximum value that is still less than or equal to andC

CC CODE B ≤athe minimum value that is still greater than , respectively - that is, ( )nd < ( ).C CODE H

CODE B C LEV B B FD -b

If ( )= and ( )=k , then is the locational code of the neighor that is black and of equal size. If ( )>k and [ j ]= ( )[ j ] forLEV B C CODE B

DLEV B C CODE B B F( )≤ j <n , then is a descendant of ( ) and hence is the locationalcode of the neighbor that is black and of side length 2 . If [ j ]= ( )[ j ](B )LEV H

f CODE H C C

C CODE

or k ≤ j <n , then ( ) is a descendant of and hence is the code componentof the neighbor that is gray and of equal size.

Otherwise, the neighbor is white and we must determine its locationalFD

HCODE B CODE ewcode and its size. To do this, we must examine both ( ) and ( ) to se

hich one has the largest number of consecutive leading digits that match those oftC (see Exercise 3.4.1.15). Without loss of generality, suppose that it is and thaB

CODE B tithe first digit that mismatches is at position j - i.e., ( )[ j ]. Although nommediately obvious, this means that the neighbor is white and its side length is 2 j

( CODE FD Csee Exercise 3.4.1.16). The field of its locational code is [i ] for the.

Fdigits in position i ( j ≤i <n ), and the remaining digits contain a 0 directional code

or example, in Figure the western neighbor of node 4, with locational codeFD

FD

.120, is white node 1, with locational code 000

Procedure , given below, is used to calculate theF

GENhBLACKhFDhNEIGHBOR

D A FD P na

locational code of the neighbor of a node, say with locational code , in linear quadtree where only the black nodes are represented. It uses pro-FD

MAXLEQ MINGT FD lccedures and (not given here) to search the list of locationaodes that comprises the linear quadtree to find the locational codes of the

n A

FD

earest nodes to ’s equal-sized neighbor.;

/fdilocationalcode procedure GENhBLACKhFDhNEIGHBOR(D,N,L,I,P)* Given a D-dimensional quadtree of a side of width 2N

Dlrepresented by a linear quadtree in the form of a list L, of the Focational codes of its BLACK nodes, return the FD locational code of the

neighbor in direction I of a node with FD locational code P. If no

4-7

/b

neighbor exists, then NIL is returned. Assume N>0. *eginvalue integer D,N;

;vvalue pointer fdilist Lalue direction I;

value pointer fdilocationalcode P;

ipointer fdilocationalcode B,H,Q;nteger C,J,K,M,MASK4;

;iinteger array MASK2[0:D−1]f LEV(P) ≥ N then return(NIL); /* No neighbor exists in direction I */

MMASK4 ← (2↑(D∗N) −1)/(2↑D−1); /* Calculate mask for step (4) */

ASK2[0] ← lnot(MASK4); /* Take 1’s complement *//for J←1 step 1 until D−1 do /* Calculate masks for step (2) *

MASK2[J] ← MASK2[J−1] lcsh 1; /* Left circular shift by one bit */

fC ← 0;or J←0 step 1 until D−1 doC ← C lor (((CODE(P) lor MASK2[J]) + ((INC(I) lsh LEV(P)∗D) land (MASK4 lsh J)))

B!land (MASK4 lsh J));

← MAXLEQ(C,L); /* Find maximum FD locational code in L that is ≤C */

MH ← MINGT(C,L); /* Find minimum FD locational code in L that is >C */

← N−1;nif not(null(B)) the

begin /* The neighbor is not before the first FD locational code in L */;while M≥LEV(B) and (((CODE(B) xor C) lsh −D∗M)=0) do M←M−1

/* The xor combined with the right shift is the same as CODE(B)[M]=C[M] */if M<LEV(B) then

return(B); /* Neighbor is black and of greater than or equal size */

Qend;← create(fdilocationalcode);

iJ ← N−1;f not(null(H)) thenbegin /* The neighbor is not after the last FD locational code in L */

while ((CODE(H) xor C) lsh −D∗J)=0 do J←J−1;/* The xor combined with the right shift is the same as CODE(H)[J]=C[J] */

if J<LEV(P) thenbegin /* H is a descendant of C */

LCODE(Q) ← C;

EV(Q) ← LEV(P);;

rCOL(Q) ← ‘GRAY’eturn(Q);

end;;

Cend← C land (lnot(2↑(min(M,J)∗D)−1));

/* Equivalent to zeroing the min(M,J)−1 least significant directional digits */

LCODE(Q) ← C;

EV(Q) ← min(M,J);;

rCOL(Q) ← ‘WHITE’eturn(Q);

Eend;

xercises

4.1: After having calculated the locational code of an equal-sized neighbor, sayQ

FDC GENhFDhNEIGHBOR -

mwith code , procedures and its quadtree counterpart deter

ine ’s real size by searching the list of locational codes for the locationalc

Q FD FDode whose code component has the maximum value still less than or equal to that

of , say - i.e., ( )≤ . The levels of the code components are compared toC R CODE R Cdetermine the size of the neighbor. Prove that this technique works correctly.

e4.2: Suppose that the locational code is implemented in such a way that thFDtrailing k digits of the code component of a node at level k are 3 instead of 0.

4-8

How does this affect the algorithm for computing the neighbors of greater than or

4

equal size?

.3: [Donald E. Knuth] The following is an alternative to steps (1)-(5) which serveas the basis of procedure .GENhFDhNEIGHBOR

C S.((1) Save in2) Load a 1 in every bit position of that does not correspond to the x coordi-C

MC o .(

nate by applying a logical or (i.e., lor) operation to and3) Add 1 to .C

C S.((4) Apply an exclusive or (i.e., xor) operation to with5) Load a 0 in every bit position of that does not correspond to the x coordi-C

MC x .(

nate by applying a logical and (i.e., land) operation to and6) Apply an exclusive or (i.e., xor) operation to with .

4

Show that the results are equivalent.C S

.4: Is the method of Exercise 4.3 better or worse than our original method?

y‘4.5: The procedures described in this section made use of parameter transmittal bvalue.’ Can you make them more efficient by transmitting parameters by ‘refer-

4

ence?’

.6: What features must a programming language provide so that the algorithms-

tdiscussed in this section can be implemented efficiently? Compare an implementaion in , , , and . Which one yields the most efficientADA C PASCAL MODULA 2

?

4

encoding

.7: Rewrite procedure so that it runs as fast as possible.

G

GENhFDhNEIGHBOR

ENhFDhNEIGHBOR .

4

4.8: Adapt procedure to deal with octrees

.9: Suppose that you are only allowed transitions in one of the directions (i.e.,

tonly in one of x , y , or z ). Show how to avoid step (2) for a negative transition inhe direction of coordinate i from a block with code of side length 2 .C k

ra4.10: In the case of wraparound, our proposed solution checks for the presence obsence of overflow at the conclusion of step (3). Explain why this is correct even

ybthough it would seem that for a d -dimensional image, we must examine the carrit from the d most significant bit position of the appropriate coordinate.

e4.11: Consider a d -dimensional image of side width 2 . Suppose that you storn

e CODE FDvery field of an locational code in a word with a maximum of c(c >d .n ) bits. Prove that procedure handles addition overflowGENhFDhNEIGHBOR

4

correctly.

.12: Prove that for a d -dimensional image, function has 3 −1 possible values.INC d

INC fs4.13: Write a procedure to implement function for a d -dimensional image oide length 2 . The idea is to devise a table so that the values can be obtained by

t

n

able lookup. To do this, you will need to devise a way to specify the various

4

combinations of directions so that the appropriate table entry can be accessed.

.14: Procedure contains a loop of the form ‘whileM

GENhBLACKhFDhNEIGHBORLEV B CODE B C D M M M f

t≥ ( ) and ((( ( ) xor ) lsh − ∗ )=0) do ← −1;’ and another loop o

he form ‘while (( ( ) xor ) lsh − ∗ )=0 do ← −1;’. Why is there no need toJ

CODE H C D J J JLEV Pcheck for ‘ ≥ ( )’ in the second loop?

4.15: When the neighboring node is white, why does procedure -

4-9

GENhBLACKhFDh

examine both ( ) and ( ) to see which one has the largestNEIGHBOR CODE B CODE HC?

4

number of consecutive leading digits that match those of

.16: Continuing Exercise 4.15, suppose that the first mismatching directional digit

wis in position j , why are all remaining digits after the mismatch set to 0? In other

ords, how do we know that the neighbor’s side length is 2 ?

4 FD

j

.17: The locational code of a neighbor of equal size can also be obtained by a

rprocess that is analogous to that used for finding neighbors in a pointer-basedepresentation [Same89a]. Given a node with locational code , finding itsA FD P

An Ieighbor in direction of size greater than or equal to is done as follows. Duringfthis process, we first construct , the code component of the locational code oC FD

t Qhe equal-sized desired neighbor, say . Starting with the digit position in the codecorresponding to the link from to its father (i.e., ( )[ ( )]), reflect eachA CODE P LEV P

gedirectional code in the designated direction (and assign it to the correspondinntry in ) until encountering the nearest common ancestor of and . TheC A Q

ADJ nTnearest common ancestor is detected by using the function given previously i

able Note that unlike the algorithm for computing neighbors in a pointer-basede

aquadtree (octree), there is no need to descend the tree since reflection occurs whilscending the tree. Once the locational code of the equal-sized neighbor hasFD

GENhFDhNEIGH-been computed, the rest of the process is identical to that used in. Write a procedure to implement this technique. ToBOR FDhQThGTEQhNEIGHBOR

CODE efacilitate the expression of the algorithm, implement the field of thf d hlocationalcode record as an array of type f d hpath of n directional codesstored in the order [n −1] [n −2]... [1] [0].P P P P

FDhQThGTEQhNEIGHBOR d4.18: Rewrite procedure in Exercise 4.17 so that the fielis accessed via bit manipulation instructions instead of being treated as an

aCODErray.

4.19: In Section 2.1.4 we discussed the two-dimensional run encoding ( )2DREe

l[Lauz84]. It first constructs a linear quadtree in which each of the black and whiteaf nodes is encoded with the Morton Matrix number of the pixel that occupies its

tlower right corner - i.e., the pixel having the maximum Morton Matrix number inhe leaf node. It assumes that the origin is at the upper left corner of the universe.

This is a variant of the locational code, which only uses the path from the rootFDto the node and does not require recording the level of the node. The list of leaf

cnodes is sorted by their corresponding Morton Matrix numbers. Show how toompute edge and vertex-neighbors of greater than or equal size using such a

4

representation. The result should also indicate the color of the neighbor.

.20: Suppose that you have a list of the Morton Matrix numbers of a quadtree asfdescribed in Exercise 4.19. The is formed by viewing this list as a set o2DRE

subsequences of Morton Matrix numbers corresponding to nodes of the same color

cand discarding all but the last element of each subsequence of blocks of the sameolor. Show how to compute edge and vertex-neighbors of greater than or equal

size when a quadtree is represented by the method. The result should also2DRE

4

indicate the color of the neighbor.

.21: Show how to compute face, edge, and vertex-neighbors of greater than ort

oequal size of a face, an edge, and a vertex when an octree is represented by a lisf the Morton Matrix numbers of its leaf nodes. The result should also indicate the

4

color of the neighbor.

.22: Consider the adaptation of discussed in Exercise 4.20 scheme to an2DRE3DRE -

poctree (the result is a three-dimensional run encoding ( )). Show how to com

ute face, edge, and vertex-neighbors of greater than or equal size when an octreeeis represented by the method. The result should also indicate the color of th3DRE

4-10

4

neighbor.

.3.2. FL LINEAR QUADTREE

The locational code is similar to the locational code. It is also aFL FD

FD -tfixed length code. The difference is that the locational code requires an addiional field to record the level of a node, while the locational code has an addi-FL

nitional possible value for the directional code, which is a don’t care. Assuming amage of side length 2 , the locational code of each leaf node of side length 2kn FL

is n digits long where the k trailing digits contain a don’t care directional code,

pand the leading n −k digits contain the directional codes that locate the leaf along a

ath from the root of the tree.

The locational code of a quadtree (octree) node is a base 5 (9) number.FL

FL gbHowever, all arithmetic operations on the locational code are performed usinase 4 (8). The directional codes are numeric equivalents of the different quadrants

-and octants (i.e., 0, 1, 2, and 3 for , , , and , respectively, for a quadNW NE SW SE

t FLree) and 4 denotes a don’t care. The locational code can also be defined byl

crepresenting the don’t care directional code by 0 and the remaining directionaodes by 1-4 for a quadtree and 1-8 for an octree (see Exercise 3.4.2.3).

sEach leaf node in the linear quadtree (octree) is represented by itFL FL

L

llocational code. In the version that we describe in this section, we only store the F

ocational codes of the black nodes, as proposed by Gargantini [Garg82a]. The LF

dflocational codes of the remaining white (as well as gray) nodes can be inferrerom the locational codes of the black leaf nodes.

rFinding the neighbor of a node with locational code of size greateA FL P

Dt A Fhan or equal to is achieved in a manner similar to that used for an locationalcode (recall procedure in Section 4.3.1). Once again weGENhBLACKhFDhNEIGHBOR

nafirst find a neighbor of equal size. However, we cannot use addition or subtractios in the case of the locational code due to the presence of the don’t care

dFD

igits. Thus we must use an analog of the procedure used for a pointer-based treerepresentation (see Exercise 4.17).

The key difference is that there is never a need to traverse links since thee

wonly operation is one of bit manipulation. This difference is of no consequenc

hen the tree is represented in internal memory. However, when storage require-

aments are such that the tree is represented in external memory (e.g., using a B-trees in [Same84d, Same87a, Shaf89a]), then this difference is very important. It

smeans that there is no need to traverse links between nodes on different pages, aituation that could cause a page fault. In such a situation, bit manipulation will,

in most cases, be considerably faster than link traversal.

Once the locational code of the neighbor of equal size has been found,FL

FL L B H FL

lsearch the list of locational codes , using binary search, for and , theocational codes whose code components have the maximum value that is still less

-than and the minimum value that is still greater than or equal to , respectivelyC C

t CODE B C C CODE Hhat is, ( )< and ≤ ( ). This procedure is analogous to that used inyG for locational codes (see Exercise 3.4.2.2). The onlENhBLACKhFDhNEIGHBOR FD

CODE B C C CODE H .slight difference is that there ( )≤ and < ( ) (see Exercise 3.4.2.3)

4-11

Decoding the individual directional codes of a particular locational codeFL

,sis a complicated process, which requires use of division operations. Unfortunatelyince the directional codes are represented by base 5 (9) numbers, we can not

tfreplace division operations by shifts unless we are willing to waste much of one bior each digit by using 3 (4) bits to represent each of the directional codes. For this

reason, the locational code is more popular. Also, most implementations retainFD

the locational codes of all of the leaf nodes (not just the black ones).

4

Exercises

.23: When looking for the neighbor of equal size you need to know the size of

4

the current node. How would you do this?

.24: Explain the details of how to find the neighbor of greater than or equal sizein an linear quadtree once the locational code, say , of the equal-sizedFL FL C

LB H F lcneighbor at level k has been found. Assume that and are the locationaodes whose codes have the maximum value that is still less than and the

CC

.

4

minimum value that is still greater than or equal to , respectively

.25: Once the locational code of an equal-sized neighbor has been found, sayC

FLFL FL B H

s, the list of locational codes is searched for the locational codes anduch that ( )< and ≤ ( ). On the other hand, in the case of an DCODE B C C CODE H F

FD B H tlocational code, the search is for the locational codes and such tha( )≤ and < ( ). Explain the reason for the difference.CODE B C C CODE H

FLhALLhQThGTEQhNEIGHBOR FDhQThGTEQh-4.26: Write a procedure analogous toin Exercise 4.17 that computes a neighbor of greater than or equal sizeNEIGHBOR

FL FL s(in an linear quadtree that contains the locational codes of all the leaf nodei.e., both black and white).

4.27: Write a procedure analogous to -B

FLhQThGTEQhNEIGHBOR FDhQThGTEQhNEIGHOR in Exercise 4.17 that computes a neighbor of greater than or equal size in an

F linear quadtree that only contains the locational codes of the black nodes.L FL

4.3.3. VL LINEAR QUADTREE

The locational code is similar to the locational code with the princi-p

VL FL

al difference being that it is based on using a variable-length locational code. Ittis obtained by reversing the locational code and letting 0 represent the don’FL

NW NE SW SE,rcare directional code, while the values 1, 2, 3, and 4 represent , , , andespectively. Thus, the leading digits correspond to the don’t care directional

ttcodes. Since the leading digits are 0, there is no reason to explicitly represenhem in the locational code. This is why we have a variable-length code. InVL

FLfact, this is its advantage over the locational code - i.e., the code componentsare shorter. Assuming an image of side length 2 , the locational code of eachn LV

Ll k Veaf node of side length 2 is just n −k digits long. For example, the locationalcode of node 4 in Figure is the base 5 number 23.

Each leaf node in the linear quadtree (octree) is represented by its L

lVL V

ocational code. In the version that we describe in this section, we only store theV locational codes of the black nodes. The locational codes of the remainingL VL

ebwhite (as well as gray) nodes can be inferred from the locational codes of thlack leaf nodes.

4-12

Finding the neighbor of a node with locational code of size greaterA

A VL P

FD lcthan or equal to is achieved in a manner similar to that used for an locationaode (recall procedure in Section 4.3.1). Once again we

fiGENhBLACKhFDhNEIGHBOR

rst find a neighbor of equal size. However, we cannot use addition or subtractioneas in the case of the locational code, in part, because of the presence of thFD

don’t care digits, and their order. Thus we must use an analog of the proceduree

iused for a pointer-based tree representation (see Exercise 4.17). Once again, thers never a need to traverse links since the only operation is one of bit manipula-

tion.

Once we have calculated the locational code of the neighbor of equalVL

FD lcsize, the remaining process is more complex than that used with the locationaode since when the list of locational codes is kept in a sorted order, the

sVL

equence corresponds to a form of a breadth-first traversal of the tree. This meansthat locational codes do not satisfy the relation that the descendants of a givenVL

FD Qlocational code, , immediately precede it in a sorted list. This was usefulbecause we can determine the identity of the desired locational code, say , byFD D

e FD Dxamining the maximum locational code that is less than or equal to and theminimum locational code that is greater than .FD D

T VLhe locational code of a neighbor that is greater than or equal in size isobtained as follows. Calculate the locational code of the neighbor, say . IfVL D D

-tdoes not correspond to a black leaf node, then we may need to search for a conaining node in up to n lists - one for each level. This is in contrast to procedure

lG that only requires a search of the list of locationaENhBLACKhFDhNEIGHBOR FD

E

codes of the entire image.

xercises

4.28: Suppose that the linear quadtree and octree contain the locationalVL VLVLh-codes of all the leaf nodes (i.e., both black and white). Rewrite procedures

and , and the procedures that theyiQThGTEQhNEIGHBOR VLhOThGTEQhNEIGHBORnvoke, to cope with this definition.

4.29: Write a pair of procedures and -B

VLhQThGTEQhNEIGHBOR VLhOThGTEQhNEIGHOR VL e

ato compute a neighbor of greater than or equal size in a linear quadtre

nd octree that only contain the locational codes of the black nodes.

4.3.4. DF-EXPRESSIONS

VL

The second pointer-less representation that we discussed is one that

mrepresents the image in the form of a traversal of the nodes of its tree. This is com-

only termed a -expression [Kawa80a] (see Section 2.2). Neighbor finding isn

DF

ot very convenient using such a representation since there is no notion of randome

taccess. For example, a node is only identified by its position in the traversal of three and is represented by a code corresponding to its type (i.e., black, white, or

.Tgray). We have no explicit information on the path from the root of the tree to it

hus, to perform neighbor finding, it is necessary to start at the root of the tree ande

tlocate the node whose neighbor we are seeking, remember the path to it, computhe path to its neighbor, and then sequentially search the list for the neighbor start-

ing at the root of the tree.

This is a cumbersome process and what usually happens is that algorithmssthat require neighbor finding are recoded in such a way that neighbor finding i

4-13

amavoided. For example, Samet and Tamminen [Same85f, Same84e] present

ethod for computing geometric properties of images represented by pointer-less-

tquadtree representations that does not require neighbor finding operations (see Secion 5.1.3). Instead, they use a data structure termed an active border.

4

Exercises

.30: Show how to compute face, edge, and vertex-neighbors of greater than orequal size in an image that is represented as a -expression. The input is an indexDFi DFnto the list representing the -expression and the output should be a pointer tothe element in the list corresponding to the neighbor.

4-14

SOLUTIONS TO CHAPTER 4

eb4.1: The comparison between the values of the code components is feasiblecause the trailing k digits of the code components of a node at level k are 0.

Recall that the locational code corresponds to the coordinates of a specific pixelFDLDB e

b(or voxel) in the block (e.g., for an octree, it is the voxel in the corner of thlock when the origin is in the corner of the image).

G

LDB

ENhFDhNEIGHBOR QThFDhNEIGHBOR dt4.2: In procedures and we would have to finhe minimum locational code in that is ≥ instead of the maximum loca-FD L P FD

L P MAXLEQ P L MIN-tional code in that is ≤ . Thus, we need to replace ‘ ( , )’ by ‘( , ).’

4

GEQ P L

.3: The key is the fact that the exclusive or operation (i.e., ) is commutative andtassociative, and that =0 and 0= . The first three steps are equivalent to the firsAA A A

C ntthree steps of the previous approach. Letting i denote the result of each step ihe new method, we find that after step (5) we have 5←( 3 ) land . ThisC C S Mx

SC eameans that all bit positions corresponding to the x coordinate are set to 3 whilll those corresponding to the remaining coordinates are set to 0. Applying step

(6) means that all bit positions corresponding to the x coordinate are set to ( 3 )C S Sw C SS C Chich is the same as 3 since =0 and 30= 3. Similarly, all bit positionscorresponding to the remaining coordinates are set to 0 which is .S S

ea4.4: The answer depends on the instruction set of the computer [Henn90]. Therre two factors. The first is the number of operands for each instruction. Assume

eothat each instruction has two source operands and a destination. When the threperands are distinct, we have a three-operand instruction format, while when one

-tof the operands is both a source and a destination, we have a two-operand instrucion format. The second is how many of the operands may be memory addresses.

aWhen one of the operands (usually the destination) in the two-operand format is in

general-purpose register, we have a register-memory computer [Henn90]. Eache

4of steps (2)-(5) of the original method and steps (1)-(6) of the method of Exercis.3 can be executed by one instruction on a register-memory computer. Step (1) of

tthe original method requires us to save the result in another address or register thanhe one operated on by steps (2)-(5). This will require two instructions on a

aregister-memory computer, while it only needs one instruction on a computer with

three-operand instruction format. Thus the methods are equivalent on a register--

tmemory computer, and the original method is superior on a three-operand instrucion format.

4.5: You must be careful not to overwrite the locational code of the node whose

4

neighbor is being sought.

.6: It should have bit manipulation.

4.7: Replace the shifts of by table lookups and precompute the value of‘INC I LEV P D

MASK4( ) lsh ( )∗ ’.

4.9: Assuming a d -dimensional image, such a transition can be represented by andincrement value of −2 . The k .d +1 least significant bits of this value are 0 ank .d +i

the remaining bits are 1. Treat the increment as a simple negative number and dot

tnot zero the bit positions corresponding to the other coordinates. This means thahere is no need for step (2) which loads a 1 in the bit positions of that do not

4

correspond to coordinate i .C

.10: Recall that step (2) loaded a 1 in every bit position that did not correspond to

S-4-1

.

4

the direction being incremented during the particular iteration of steps (1)-(5)

.11: This is guaranteed by the fact that a logical and (i.e., land) operation is per-

4

formed after incrementing each coordinate value.

.12: In each of the d directions we have three choices - a positive motion, a nega--

btive motion, and no motion. The case of no motion in all d directions is impossile, hence the 3 −1 values. See also Exercise 3.3.1.2.

C

d

ODE H C4.14: Because ( )> and thus equality for all directional digit positions is

4

impossible.

.15: It means that their nearest common ancestor is closer.

o4.16: Since we chose one of and , say , this means that must correspond tB H B Ck

nthe largest possible white node consistent with the absence of any other blacodes between it and the code component of . If any of these digits would beH

DB H F lcnonzero, this would contradict either the premise that and are the locationaodes with the maximum code component value that is still less than or equal to ,

CCr

tand the minimum code component value that is still greater than , respectively; ohe premise that the code component of has the largest number of consecutive

CB

.

4

leading digits that match those of

.17:fdilocationalcode procedure FDhQThGTEQhNEIGHBOR(N,L,I,P);/* Given a image represented by a linear2 ×2N N

quadtree in the form of a list L, of the FD locational codes of its nodes,h

Freturn the FD locational code of the neighbor in direction I of a node wit

D locational code P. If no neighbor exists, then NIL is returned with no

bwraparound. Assume N>0. */

eginvalue integer N;

;vvalue pointer fdilist Lalue direction I;

value pointer fdilocationalcode P;

pfdipath array T[0:N-1];ointer fdilocationalcode B,Q;

if LEV(P) N then return(NIL); /* No neighbor exists in direction I */T

≥← if TYPE(I)=‘EDGE’ then

! FDLhQThEQhEDGEhNEIGHBOR(N,I,CODE(P),LEV(P));

i!else FDLhQThEQhVERTEXhNEIGHBOR(N,I,CODE(P),LEV(P))

f null(T) then return(NIL) /* No neighbor exists in direction I *//else B ← MAXLEQ(T,L); /* Find maximum FD locational code in L that is T *≤

i ≥f LEV(B) LEV(P) then return(B)/else /* The neighbor is smaller; create a new node *gray

beginQ ← create(fdilocationalcode);

LCODE(Q) ← T;

EV(Q) ← LEV(P);;

rCOL(Q) ← ‘GRAY’eturn(Q);

end;;

p

end

ath array procedure FDLhQThEQhEDGEhNEIGHBOR(N,I,P,LEVEL);/* Given a image represented by an FD2 ×2N N

linear quadtree, return the path from the root to the edge-neighbor in/

bdirection I of a node at level LEVEL whose path from the root is P. *

eginvalue integer N;

vvalue edge I;alue path array P[0:N-1];

dvalue integer LEVEL;o

S-4-2

beginP[LEVEL] ← REFLECT(I,P[LEVEL]);

eLEVEL ← LEVEL+1;nd

until LEVEL=N or ADJ(I,P[LEVEL-1]);Lreturn(if not(ADJ(I,P[LEVEL-1])) then NI

!else P);p

end;

ath array procedure FDLhQThEQhVERTEXhNEIGHBOR(N,I,P,LEVEL);/* Given a image represented by an FD2 ×2N N

linear quadtree, return the path from the root to the vertex-neighbor in/

bdirection I of a node at level LEVEL whose path from the root is P. *

eginvalue integer N;

vvalue vertex I;alue path array P[0:N-1];

dvalue integer LEVEL;irectionicode PREVIOUS;

dobegin

PREVIOUS ← P[LEVEL];;

LP[LEVEL] ← REFLECT(I,PREVIOUS)

EVEL ← LEVEL+1;

uendntil LEVEL=N or not(ADJ(I,PREVIOUS));

return(if ADJ(I,PREVIOUS) then NIL!else if COMMONhEDGE(I,PREVIOUS)$ $Ω then≠

,!FDLhQThEQhEDGEhNEIGHBOR(N,COMMONhEDGE(I,PREVIOUS)!P,LEVEL)

4end;

!else P);

.19: You must first determine the size (i.e., level) of the node, which requires

4

looking up the next element in the sequence.

.23: Starting at the position of the least significant directional digit in the loca-FLt

dtional code of the current node, find the position of the first directional digit thaoes not contain a don’t care directional code, say at digit position k .

k4.24: If = ( ), then is the locational code of the neighbor that is blacC CODE H H FLC CODE H dand of equal size. If there exists m such that [ j ]= ( )[ j ] for k <m ≤ j <n , an

( )[ j ]= for 0≤ j <m , then is a descendant of ( ) and isCODE H DONThCARE C CODE H HFL m fthe locational code of the neighbor that is black and of side length 2 . I

[ j ]= ( )[ j ] for k ≤ j <n , then ( ) is a descendant of and is the valueC CODE B CODE B C CCODE FLof the field of the locational code of the neighbor that is gray and of

equal size.

Otherwise, the neighbor is white and we must determine its locationalFL

HCODE B CODE ewcode and its size. To do this, we must examine both ( ) and ( ) to se

hich one has the largest number of consecutive leading digits that match those oftC. Without loss of generality, suppose that it is and that the first digit thaB

m CODE Bismatches is at position j - i.e., ( )[ j ]. This means that the neighbor islwhite and its side length is 2 . The value of the field of its locationaj CODE FL

c Code is [i ] for the digits in position i ( j ≤i <n ), and the remaining digits contain a

4

don’t care directional code.

.25: The reason is that if a node is contained in another node , then the DB

A B FA s

rlocational code of is less than or equal to that of , whereas the situation waeversed when using the locational code since in a quadtree (octree) a don’t

cFL

are directional code is denoted by 4 (8) rather than 0 as is effectively the case foran locational code.FD

S-4-3