1 {-# LANGUAGE FlexibleInstances #-}
6 import Data.Array.Repa (
20 import Data.Array.Repa.IO.Vector (readVectorFromTextFile,
21 writeVectorToTextFile)
22 import System.FilePath ()
23 import Test.QuickCheck (Arbitrary(..), Gen, choose, vectorOf)
29 type Values sh = Array sh Double
30 type Values1D = Values DIM1
31 type Values2D = Values DIM2
32 type Values3D = Values DIM3
35 instance Arbitrary Values3D where
37 -- I declare not to care about empty lists.
38 x_dim <- choose (1, 27)
39 y_dim <- choose (1, 27)
40 z_dim <- choose (1, 27)
41 elements <- vectorOf (x_dim * y_dim * z_dim) (arbitrary :: Gen Double)
42 let new_shape = (Z :. x_dim :. y_dim :. z_dim)
43 let three_d = Data.Array.Repa.fromList new_shape elements
47 read_values_1d :: FilePath -> IO Values1D
48 read_values_1d = readVectorFromTextFile
51 read_values_3d :: DIM3 -> FilePath -> IO Values3D
52 read_values_3d sh path = do
53 one_d <- read_values_1d path
54 return $ reshape sh one_d
56 write_values_1d :: Values3D -> FilePath -> IO ()
57 write_values_1d v3d path = do
58 let size3d = size $ extent v3d
59 let shape1d = (Z :. size3d)
60 let v1d = reshape shape1d v3d
61 writeVectorToTextFile v1d path
64 empty3d = Data.Array.Repa.fromList (Z :. 0 :. 0 :. 0) []
67 dims :: Values3D -> (Int, Int, Int)
69 let (Z :. x :. y :. z) = extent v3d
74 idx :: Values3D -> Int -> Int -> Int -> Double
79 shape = (Z :. i :. j :. k)
82 zoom_shape :: ScaleFactor -> DIM3 -> DIM3
83 zoom_shape (sfx, sfy, sfz) sh =
84 let (Z :. x :. y :. z) = sh
92 drop_z :: DIM3 -> DIM2
93 drop_z (Z :. 1 :. y :. x) = (Z :. y :. x)
94 drop_z _ = error "can't drop the z-dimension unless its size is 1"