1 PLANTEAMIENTO DEL PROBLEMA, ANTECEDENTES Y OBJETIVOS DE LA
2.6 GESTION DEL SISTEMA DE RIEGO
In order to define geometric transformation functions, it is necessary to define a higher-order function to map a function over each element in a tree. The effect of the function is the same as the map function on a list. The code for this function can be defined as follows:
mapTree::(*->**)->tree *->tree **
mapTree f Nil = Nil
mapTree f (Leaf x) = Leaf (f x)
mapTree f (Node xl x2) = Node (mapTree f xl) (mapTree f x2)
Using this function, and considering that transformations do not affect the tree part, geometric transformations can be defined in an identical way to those defined for the list representation (See Section 5.3.2):
transRowT d (r,s,t) = (d+r,s,t)
scaleRowT sc (r,s,t) = (sc*r,sc*s,t)
transImageT dx dy (ry,sy,t)
= transRowT dy (ry,sy,mapTree (transRowT dx) t) scalelmageT sex scy (ry,sy,t)
= scaleRowT scy (ry,sy,mapTree (scaleRowT sex) t)
6.4.6 D isplaying an image
Displaying an image represented by a hierarchical data structure is carried out lazily in terms of resolution. When resolution becomes higher than required, traversal stops at that level and
- 1 0 4-
the average value of the pixels underneath is calculated. The process for displaying can be modularised into the following three sub-processes which can be implemented as separate functions. Figure 6-5 illustrates how transformed pixels are displayed at required resolution:
a. Scanning the output image along the display grid. An interval between two display positions is 1 (the rightward arrow in Figure 6-5 (b)).
b. Traversing down the tree of transformed pixels until an interval between two elements becomes smaller than 1, i.e. the first step in the interval data structure is less than 0.5 (the downward arrows in Figure 6-5 (b)).
c. Averaging pixels underneath which gives the value of the output pixel (the upward arrows in Figure 6-5 (b)).
If a pixel outside the range of the original is required, a background value is returned (the display pixels at x=6,7,8, in Figure 6-5).
(a) A pixel tree x=0 1 Second level Third level
f
Display pixel list L Z ] b g b g b g Fourth level I T I 1 j T ...Transformed original pixels . , .1.1 . 1 .1 J
(Bottom level) 1
(b) Displaying at appropriate resolution
F igu re 6-5 D isp la y in g p ixels w ith a v er a g in g at a required reso lu tio n
The following dispT function corresponds to the sub-process (a.). Assuming that a
function to look up the value from the pixels underneath is properly defined (lookUpT), the
dispT;:num->num->(*->*->*)->*->intervalT *->[*] dispT X n fn bg i
= [] , if n=0 .
= lookUpT X fn bg i;dispT (x+1) (n-1) fn bg i , otherwise
The first and second arguments specify the starting position and the num ber of pixels to be displayed. The third argument is a function for averaging which is polymorphic to average either pixels or rows. This is similar to the disp function defined for lists (page 83) which takes a polymorphic interpolate function. It is also necessary to pass a background value as an argument for the same reason as described in Section 5 3 3 .
The second function, sub-process (b.), traverses a tree until the required resolution is reached, and looks up a value from the subtrees underneath. A background value is returned if the position sought is outside the range or the value of the tree is Nil. If the tree is a Leaf, then the value of the Leaf is returned. Otherwise, either an average value is returned or the tree is further traversed depending on the required resolution. The definition follows:
lookUpT::intervalT *->num->(*->*->*)->*->* lookUpT (r,s,t) X fn bg = bg , if (x<r-2*s)\/(r+2*s<=x) lookUpT (r,s,Nil) x fn bg = bg lookUpT (r,s,Leaf a) x fn bg = a lookUpT (r,s,Node tl t2) x fn bg = avrTree (Node tl t2) fn bg , if s<0.5 = lookUpT (r-s,s/2,tl) x fn bg, if x<r = lookUpT (r+s,s/2,t2) x fn bg, otherwise
The function a v r T r e e calculates the average value of the subtrees underneath, i.e. the
sub-process (c.). Depending on whether the elements of a tree are numbers or lists of numbers,
the m ethods for averaging differ. So, a v r T r e e is defined as a higher-order function which
takes a function to average either numbers or lists of numbers: a v r T r e e t r e e *->(*->*->*)->*->*
avrTree Nil fn bg = bg
avrTree (Leaf a) fn bg = a
avrTree (Node tl t2) fn bg= fn (avrTree tl fn bg) (avrTree t2 fn bg)
avrList; :[num]-> [num]- > [num]
avrList 11 12 = [ (a+b)/2 I (a,b)<-zip2 11 12]
avrNum:;num->num->num avrNum a b = (a+b)/2
- 106-
prepared. Using these functions, the function to display an image represented by a tree of trees
can be defined. The d is p T function is mapped over each row (by m ap T ree) to output a list of
pixels of a required resolution in the x direction. And the resulting tree of lists is operated by
the d is p T function to output a list of lists of pixels. The definition follows:
display?::num->num->num->num->imageT num->([num]]
display? X y x n yn (r,s,t)
= disp? y yn avrList (rep xn bgp) (r,s,map?ree (disp? x xn avrNum bgp) t)
6.4.7 Discussions
For the same reason as we discussed in Section 53.4, i.e. keeping x and y operations separate, rotation is not implemented using the current data structure of a tree of trees. However, the reason for taking this representation is to test whether the technique w e have developed (i.e. an identical pattern for x and y data structures makes programm ing easier) can be applied to data structures other than lists. It has now been confirmed that this technique can be applied to a binary tree of binary trees.
Rotation may be implemented fairly easily by using existing techniques. For example, we can modify the definition of a row to be a triple of a root position (x, y), a first step length (x, y), and a binary pixel tree. The whole image may be represented by a list of rows. This representation is very similar to the one we defined in Section 5.4.1 except that a tree instead
of a lis t is used to represent a row. By mapping the one dimensional d is p T function defined
on page 105 onto the x elements of each row, a list of lists of pixels aligned in the x direction is obtained. A function for aligning these pixels in the y direction has previously been implemented (See Section 5.43).
In the next section, we introduce a quadtree to represent an image. Affine transformations and display at required resolution are implemented using quadtrees.