]> gitweb.michael.orlitzky.com - spline3.git/blob - src/Values.hs
Modify the Values module to use ScaleFactors instead of Ints.
[spline3.git] / src / Values.hs
1 {-# LANGUAGE FlexibleInstances #-}
2
3 module Values
4 where
5
6 import Data.Array.Repa (
7 Array,
8 Z(..),
9 (:.)(..),
10 DIM1,
11 DIM2,
12 DIM3,
13 extent,
14 fromList,
15 index,
16 reshape,
17 size
18 )
19
20 import Data.Array.Repa.IO.Vector (readVectorFromTextFile,
21 writeVectorToTextFile)
22 import System.FilePath ()
23 import Test.QuickCheck (Arbitrary(..), Gen)
24
25
26 import ScaleFactor
27
28
29 type Values1D = Array DIM1 Double
30 type Values2D = Array DIM2 Double
31 type Values3D = Array DIM3 Double
32
33
34 instance Arbitrary Values3D where
35 arbitrary = do
36 x_dim <- arbitrary :: Gen Int
37 y_dim <- arbitrary :: Gen Int
38 z_dim <- arbitrary :: Gen Int
39 one_d <- arbitrary :: Gen Values1D
40 let new_shape = (Z :. x_dim :. y_dim :. z_dim)
41 let three_d = reshape new_shape one_d
42 return three_d
43
44
45 instance Arbitrary Values1D where
46 arbitrary = do
47 x <- arbitrary :: Gen [Double]
48 let shape = (Z :. (length x))
49 let one_d = Data.Array.Repa.fromList shape x
50 return one_d
51
52
53 read_values_1d :: FilePath -> IO Values1D
54 read_values_1d path = readVectorFromTextFile path
55
56
57 read_values_3d :: DIM3 -> FilePath -> IO Values3D
58 read_values_3d sh path = do
59 one_d <- read_values_1d path
60 return $ reshape sh one_d
61
62 write_values_1d :: Values3D -> FilePath -> IO ()
63 write_values_1d v3d path = do
64 let size3d = size $ extent v3d
65 let shape1d = (Z :. size3d)
66 let v1d = reshape shape1d v3d
67 writeVectorToTextFile v1d path
68
69 empty3d :: Values3D
70 empty3d = Data.Array.Repa.fromList (Z :. 0 :. 0 :. 0) []
71
72
73 dims :: Values3D -> (Int, Int, Int)
74 dims v3d =
75 let (Z :. x :. y :. z) = extent v3d
76 in
77 (x,y,z)
78
79
80 idx :: Values3D -> Int -> Int -> Int -> Double
81 idx v3d i j k =
82 index v3d shape
83 where
84 shape :: DIM3
85 shape = (Z :. i :. j :. k)
86
87
88 zoom_shape :: ScaleFactor -> DIM3 -> DIM3
89 zoom_shape (sfx, sfy, sfz) sh =
90 let (Z :. x :. y :. z) = sh
91 x' = x * sfx
92 y' = y * sfy
93 z' = z * sfz
94 in
95 (Z :. x' :. y' :. z')