-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dataset.hs
93 lines (73 loc) · 3.17 KB
/
Dataset.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
{-# LANGUAGE FlexibleInstances, TypeOperators #-}
module Dataset where
import Data.Array.Repa hiding ( (++) )
import Data.Array.Repa.IO.Binary
import Data.Array.Repa.Repr.ForeignPtr
import qualified Graphics.Rendering.OpenGL.GL as GL
import System.IO (openBinaryFile, IOMode(ReadMode), hFileSize, hClose)
import Maths
-- Generic dataset class. Used to convert specific data types into internal
-- FizzData representation.
class Dataset d where
readData :: d -> IO (FizzData Float)
-- Sampling datatype - used to downsample datasets.
data Sampling a = Single a
| Range a a
| Sampled a a a
deriving Eq
type Samplings = (Sampling Int, Sampling Int, Sampling Int)
instance (Show a, Num a) => Show (Sampling a) where
show (Single i) = show i
show (Range i j) = show i ++ "-"++ show j
show (Sampled i j k) = show i ++ "-"++ show (i+j) ++ "-" ++ show k
samplingToList :: (Num a, Enum a) => Sampling a -> [a]
samplingToList (Single i) = [i]
samplingToList (Range f t) = [f..t]
samplingToList (Sampled f s t) = [f,s+f .. t]
samplingSize :: (Integral a) => Sampling a -> a
samplingSize (Single _) = 1
samplingSize (Range f t) = t - f + 1
samplingSize (Sampled f s t) = ((t - f) `div` s) + 1
listX d = let (dx, _, _) = d in samplingToList dx
listY d = let (_, dy, _) = d in samplingToList dy
listZ d = let (_, _, dz) = d in samplingToList dz
dimensions :: Samplings -> (Int, Int, Int)
dimensions (x, y, z) = (samplingSize x, samplingSize y, samplingSize z)
dimensions2D :: Samplings -> (Int, Int)
dimensions2D (x, y, _) = (samplingSize x, samplingSize y)
-- A datatype that is used intenrally to convert datasets to pictures.
data FizzData v = FizzData { origin :: String
, samplings :: Samplings
, datastream :: [Float]
--, arr :: Array F DIM1 v
} -- deriving Show
fileSize :: FilePath -> IO Int
fileSize path = do
h <- openBinaryFile path ReadMode
sz <- hFileSize h
hClose h
return $ fromIntegral sz
data Plane = X_equals Int
| Y_equals Int
| Z_equals Int
deriving (Eq, Show)
slicePlane :: Samplings -> Plane
slicePlane (r, _, _) | isSingleton r = X_equals . head . samplingToList $ r
slicePlane (_, r, _) | isSingleton r = Y_equals . head . samplingToList $ r
slicePlane (_, _, r) | isSingleton r = Z_equals . head . samplingToList $ r
slicePlane _ = error "slicePlane: no singleton dimension"
isSingleton :: Ord a => Sampling a -> Bool
isSingleton (Single _) = True
isSingleton (Range f t) = f == t
isSingleton (Sampled f s t) = f == t || f == s || s > t
planePoints :: Samplings -> [GL.Vertex3 GL.GLfloat]
planePoints samp =
case slicePlane samp of
X_equals v -> [coord v cy cz | cz <- samplingZ, cy <- samplingY]
Y_equals v -> [coord cx v cz | cz <- samplingZ, cx <- samplingX]
Z_equals v -> [coord cx cy v | cy <- samplingY, cx <- samplingX]
where
samplingZ = listZ $ samp
samplingY = listY $ samp
samplingX = listX $ samp
coord x y z = GL.Vertex3 (toFloat x) (toFloat y) (toFloat z)