Práctica Número 5, Curso 2004/05, Fecha 18/11/2004

1. ElevationGrid

El lenguaje VRML (y X3D) incluye una primitiva que permite construir superficies bidimensionales indicando los puntos de elevación. Almacenar el siguiente código VRML en un fichero y visualizarlo.



#VRML V2.0 utf8

Shape {  
   geometry ElevationGrid { 
   creaseAngle 2.5
   height [ 0 0 0 0 0 0 
            0 0 1 1 0 0
            0 1 2 2 1 0
            0 1 2 2 1 0
	    0 0 1 1 0 0
            0 0 0 0 0 0 ]
   solid FALSE
   xDimension 6   
   zDimension 6
 }
 appearance Appearance  { 
   material Material  { diffuseColor 0 1 0 }
 }  
}  




2. Plot2D

El siguiente programa permite representar superficies bidimensionales. Las superficies se representan como funciones de dos variables: Float -> Float -> Float.

Por ejemplo, la función f x y = sin (x + y) podría visualizarse mediante w (\x y -> sin (x + y))

> module Pf603 where

> type Superficie = Float -> Float -> Float

> w sup = writeFile "fun.wrl"  
>         (cabecera ++ 
>          viewPoint (40,30,95) ++ 
>          plotS (0,0,0) (0,1,0) sup)

> cabecera = "#VRML V2.0 utf8\n\n" 

> sx,sy :: Float
> sx = 64; sy = 64

> plotS p c = translate p . color c . verS

> viewPoint p = "Viewpoint { position " ++ sh3 p ++ "}\n" 

> verS f = 
>  " geometry ElevationGrid { \n" ++
>  "   creaseAngle 1.57\n" ++
>  "   height [\n" ++ puntos f ++ "]\n" ++
>  "   solid FALSE\n" ++
>  "   xDimension " ++ show (round sx) ++
>  "   zDimension " ++ show (round sy) ++ "}"

> puntos f = concat [linea y f ++ "\n" | y <- [ 1..sy]]
> linea y f = concat [show (f x y) ++ " " | x <- [1..sx]] 

> translate (x,y,z) s = 
>   "Transform { translation " ++ sh3 (x,y,z) ++
>   "\n children [" ++ s ++ " ]}"

> color (r,g,b) s = "Shape { \n" ++ s ++
>   "\n appearance Appearance { material Material { " ++
>   "\n   diffuseColor " ++ sh3 (r,g,b) ++ " }}}\n"

> sh3 (x,y,z) = show x ++ " " ++ show y ++ " " ++ show z ++ " "





3. superficies

Representar las siguientes funciones de dos variables




4. circulo

Construir una función circulo tal que al evaluar circulo (x,y) r h s genera una superficie insertando un círculo en la posición (x,y) de radio r y altura h a partir de la superficie s

En http://www.di.uniovi.es/~labra/PLF/prac/worlds/circulo.wrl puede observarse el resultado de evaluar w (circulo (32,32) 10 3 s1)




Tipo:


> circulo :: (Float,Float) -> Float -> Float -> 
>                Superficie -> Superficie



5. QuadTrees Genéricos

El siguiente fragmento define un tipo de datos que representa quadtrees genéricos QT a para un tipo a cualquiera.
> data QT a = B a
>           | D (QT a) (QT a) (QT a) (QT a)
>    deriving Show





6. foldQT

A continuación se define la función foldQT que transforma un quadtree en un valor

> foldQT :: (b -> b -> b -> b -> b) -> (a -> b) -> QT a -> b
> foldQT f g (B x) = g x
> foldQT f g (D a b c d) = f (foldQT f g a) (foldQT f g b) 
>                            (foldQT f g c) (foldQT f g d) 


Re-escribir las funciones del ejercicio anterior utilizando foldQT




7. q2s

El siguiente fragmento convierte un Quadtree de flotantes en una superficie. Con lo cual es posible visualizar valores de tipo QT Float
> q2s :: QT Float -> (Float -> Float -> Float)
> q2s qt = q2s' qt ((0,0),d) (\x y -> 0)

> q2s' (B h) ((x,y),d) f = 
>    cuadro ((x,y),(x+d,y+d)) h f

> q2s' (D q1 q2 q3 q4) ((x,y),d) f =
>   if d <= 0 then f
>   else
>    let d2 = d `div` 2 
>    in (q2s' q1 ((x,y),d2) .
>        q2s' q2 ((x+d2,y),d2) .
>        q2s' q3 ((x,y+d2),d2) .
>        q2s' q4 ((x+d2,y+d2),d2)) f
       
> cuadro (p1,p2) h f = 
>    \x y -> if dentro (x,y) p1 p2
>  	     then h
>            else f x y

> dentro (x,y) (x1,y1) (x2,y2) = 
>   x >= fromInt x1 && x <= fromInt x2 && 
>   y >= fromInt y1 && y <= fromInt y2 

> d :: Int
> d = round sx

> e1 = D (B 0) (B 5) (B (-5)) (B 10)





8. colores (Opcional)

Construir un programa que convierta un quadtree de alturas en un quadtree de colores. Cada color puede indicar un nivel de altura.




9. s2q (Opcional)

Construir un programa que convierta una superficie en un quadtree