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