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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
import Data.Vect
import Data.Fin
import System as System
import Graphics.SDL2 as SDL2
infixr 2 <:>
infixr 4 </>
Point : Nat -> Type
Point n = Vect n Double
Simplex : Nat -> Type
Simplex n = Vect (S n) $ Point (S n)
(+) : Point n -> Point n -> Point n
xs + ys = zipWith (+) xs ys
(*) : Double -> Point n -> Point n
n * xs = map ((*) n) xs
average : Double -> Double -> Double
average a b = (a + b) / 2
midpoint : Point n -> Point n -> Point n
midpoint a b = zipWith average a b
nCr : Nat -> Nat -> Double
nCr n r = (cast $ fact n) / (cast $ fact r) / (cast $ fact $ minus n r)
bernstein : Nat -> Nat -> Double -> Double
bernstein i n t = (nCr n i) * (pow t i) * (pow (1 - t) $ minus n i)
nats : (n: Nat) -> Vect n Nat
nats (S k) = reverse $ zo k where
zo Z = Z::[]
zo (S k) = (S k)::(zo k)
RVect : Vect m Nat -> Type -> Type
RVect [] t = t
RVect (n::ns) t = Vect n (RVect ns t)
Matrix : Nat -> Nat -> Type -> Type
Matrix n m t = Vect m $ Vect n t
RTuple : Vect n Type -> Type
RTuple [] = ()
RTuple [t] = t
RTuple (t::ts) = (t, RTuple ts)
BezierFunc : Nat -> Nat -> Type
BezierFunc Z d = Point d
BezierFunc (S n) d = (Double -> BezierFunc n d)
-- bezier : {ds: Vect e Nat} -> RVect ds $ Point d -> BezierFunc e d
(<:>) : Num a => Vect n a -> Vect n a -> a
w <:> v = sum $ zipWith (*) w v
(</>) : Num a => Matrix n m a -> Vect n a -> Vect m a
m </> v = map (v <:>) m
origin : (n: Nat) -> Point n
origin Z = []
origin (S k) = 0::(origin k)
sump : Vect n $ Point d -> Point d
sump (x::xs) = foldr (+) x xs
comb : Vect n a -> Vect m b -> Vect (n * m) (a, b)
comb (a::as) bs = (comb' a bs) ++ comb as bs where
comb' a bs = map (\x => (a, x)) bs
vcomb : Vect n a -> Vect m a -> Vect (n * m) $ Vect 2 a
vcomb as bs = map (\(a,b) => [a,b]) $ comb as bs
vvcomb : Vect n a -> Vect m $ Vect o a -> Vect (n * m) $ Vect (S o) a
vvcomb (x::xs) ys = (vvcomb' x ys) ++ vvcomb xs ys where
vvcomb' x ys = map ((::)x) ys
Indices : Vect n Nat -> Type
Indices [] = ()
Indices (n::ns) = (Fin n, Indices ns)
-- Create a combination of all numbers counting up to the number mentioned in
-- the input list.
-- For example, [4, 2] should produce
-- `[[0,0],[0,1],[1,0],[1,1],[2,0],[2,1],[3,0],[3,1]]`
natComb : (ds: Vect n Nat) -> Vect (product ds) $ Indices ds
bezier : Vect n $ Point d -> Vect 1 Double -> BezierFunc 0 d
bezier {n} ps [t] = sump $ zipWith bs ps $ nats n where
bs p i = (bernstein i (minus n 1) t) * p
finer : Vect (S (S n)) Double -> Vect (S (2 * (S n))) Double
index : {ds: Vect n Nat} -> Indices ds -> RVect ds a -> a
index {ds = []} _ x = x
index {ds = _::[]} (i, ()) rv = index i rv
index {ds = _::_} is rv = Main.index (snd is) $ index (fst is) rv
asVect : {ds: Vect n Nat} -> Indices ds -> Vect n Nat
asVect {ds = []} _ = []
asVect {ds = _::[]} (i, ()) = [finToNat i]
asVect {ds = _::_} is = (finToNat $ Basics.fst is)::(asVect $ Basics.snd is)
bezier' : {ds: Vect e Nat} -> RVect ds $ Point d -> Vect e Double -> BezierFunc 0 d
bezier' {ds} x ts = sump $ map (\i => (bernsteins i ds ts) * (Main.index i x)) (natComb ds) where
bernsteins i ds ts = product $ zipWith3 bernstein (asVect i) ds ts
bezier'' : {ds: Vect e Nat} -> RVect ds $ Point d -> BezierFunc e d
-- Taken from https://github.com/steshaw/idris-sdl2/blob/master/test/hello.idr
fail : (msg: String) -> IO ()
fail msg = do
err <- getError
fPutStr stderr $ msg ++ " failed:" ++ err
fflush stderr
System.exit 1
test : Vect 4 $ Point 2
test = [[179,381],[100,100],[447,342],[353,92]]
divNat : Double -> Double -> Nat
divNat a b =
if a < 0 then Z else S (divNat (a - b) b)
-- between : (l: Double) -> (u: Double) -> (s: Double) -> Vect ((divNat (u - l) s) + 0) Double
-- between lower upper step =
-- if lower < upper then lower::(between (lower + step) upper step) else [upper]
model : List $ Point 2
model = map (\t => bezier test [t]) [(cast x) / 10 | x <- [1..10]]
samplePoints : Vect n Double -> Matrix x y $ Vect 2 Double -> Matrix x y $ Vect (n + 2) Double
samplePoints ts xy = map (\cs => map (\c => ts ++ c) cs) xy
getSamples : {ds: Vect e Nat} -> RVect ds $ Point d -> Matrix x y $ Vect e Double -> Matrix x y $ Point d
-- getSamples rv ts xy = map (\cs => map (\c => bezier' rv (ts ++ c)) cs) xy
getSamples rv tss = map doRows tss where
doCols : Vect e Double -> Point d
doCols ts = bezier' rv ts
doRows : Vect x $ Vect e Double -> Vect x $ Point d
doRows cs = map doCols cs
xrotator : Double -> Matrix 3 3 Double
xrotator a = [[1,0,0],[0, cos a, -sin a],[0, sin a, cos a]]
yrotator : Double -> Matrix 3 3 Double
yrotator a = [[cos a, 0, sin a],[0,1,0],[-sin a, 0, cos a]]
reduceTo3d : Point n -> Point 3
reduceTo3d {n = Z} p = reduceTo3d [0]
reduceTo3d {n = (S Z)} [x] = reduceTo3d [x, 0]
reduceTo3d {n = (S (S Z))} [x, y] = [x, y, 0]
reduceTo3d {n = (S (S (S _)))} p = take 3 p
reduceTo2d : Point n -> Vect 2 Int
reduceTo2d {n = Z} p = reduceTo2d [0]
reduceTo2d {n = (S Z)} [x] = reduceTo2d [x, 0]
reduceTo2d {n = (S (S _))} p = map cast $ take 2 p
-- drawModel : Model -> (Double, Double) -> IO ()
-- drawModel m (x,y) = do
-- three <-
drawBetween : Renderer -> List $ Point 2 -> Int -> Int -> Int -> Int -> IO ()
drawBetween _ [] _ _ _ _ = pure ()
drawBetween _ [p] _ _ _ _ = pure ()
drawBetween rend (p::q::ps) r g b a = do
SDL2.drawLine rend (cast $ head p) (cast $ last p)
(cast $ head q) (cast $ last q) r g b a
drawBetween rend (q::ps) r g b a
drawPoints : Renderer -> Vect n $ Point 2 -> Int -> Int -> Int -> Int -> IO ()
drawPoints _ [] _ _ _ _ = pure ()
drawPoints rend (p::ps) r g b a = do
SDL2.filledEllipse rend (cast $ head p) (cast $ last p) 10 10 r g b a
drawPoints rend ps r g b a
main : IO ()
main = (do
renderer <- SDL2.init 500 500
loop renderer
SDL2.destroyRenderer renderer
quit)
where
loop : Renderer -> IO ()
loop renderer = do
False <- SDL2.pollEventsForQuit | pure ()
True <- SDL2.setRendererDrawColor renderer 0 0 0 255
| fail "setRendererDrawColor"
SDL2.renderClear renderer
drawPoints renderer test 255 0 0 255
drawBetween renderer model 255 255 255 255
SDL2.renderPresent renderer
loop renderer
-- bezier : Vect m $ Vect (S 1) Double -> Vect 1 Double -> Vect (S 1) Double
-- bezier [ p0 ] [ t ] = p0
-- bezier ps [ t ] = sum $ zipWith bernstein' ps $ oneOnwards l where
-- bezier ps [t] = sum (map (bernstein t l) ps)
-- bezier xs (x :: ys) = ?bezier_rhs_2 where
-- bezier' ps t = sum (map (bernstein t l) ps)
-- bezier [p0] t = p0
-- bezier ps@(_::_) t = (1 - t)*(bezier (init ps) t) + (t)*(bezier (tail ps) t)
-- test : Vect 4 (Vect 2 Double)
-- test = [ [ 100.0, 250.0], [ 100.0, 100.0 ], [ 400.0, 400.0 ], [ 400.0, 250.0 ] ]
-- testOut : Vect 2 Double
-- testOut = bezier test 0.5
|