Matrix.hs 2.4 KB
Newer Older
Simon Marlow's avatar
Simon Marlow committed
1 2 3 4 5
{-
 -  Fulsom (The Solid Modeller, written in Haskell)
 -
 -  Copyright 1990,1991,1992,1993 Duncan Sinclair
 -
6
 - Permissiom to use, copy, modify, and distribute this software for any
Simon Marlow's avatar
Simon Marlow committed
7 8 9 10 11 12
 - purpose and without fee is hereby granted, provided that the above
 - copyright notice and this permission notice appear in all copies, and
 - that my name not be used in advertising or publicity pertaining to this
 - software without specific, written prior permission.  I makes no
 - representations about the suitability of this software for any purpose.
 - It is provided ``as is'' without express or implied warranty.
13
 -
Simon Marlow's avatar
Simon Marlow committed
14
 - Duncan Sinclair 1993.
15
 -
Simon Marlow's avatar
Simon Marlow committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 - Matrix arithmetic functions.
 -
 -}

module Matrix where

import Types
import Interval -- not used.

mat4x1 :: (Fractional a) => Arr -> R3 a -> R3 a
mat4x1 (r1,r2,r3) xyz = (x,y,z)
                          where
                             x = dorow r1 xyz
                             y = dorow r2 xyz
                             z = dorow r3 xyz

dorow :: (Fractional a) => Row -> R3 a -> a
33
dorow (m11,m12,m13,m14) (x,y,z)
Simon Marlow's avatar
Simon Marlow committed
34 35
    = case  (m1 * x) + (m2 * y) + (m3 * z) + m4 of n -> n
       where
Ben Gamari's avatar
Ben Gamari committed
36 37 38 39
         m1 = realToFrac m11
         m2 = realToFrac m12
         m3 = realToFrac m13
         m4 = realToFrac m14
Simon Marlow's avatar
Simon Marlow committed
40 41 42 43 44 45 46 47 48

mat4x1' :: (Fractional a) => Arr -> R3 a -> R3 a
mat4x1' (r1,r2,r3) xyz = (x,y,z)
                          where
                             x = dorow r1 xyz
                             y = dorow r2 xyz
                             z = dorow r3 xyz

dorow' :: (Fractional a) => Row -> R3 a -> a
49
dorow' (m11,m12,m13,m14) (x,y,z)
Simon Marlow's avatar
Simon Marlow committed
50 51
    = case (m1 * x) + (m2 * y) + (m3 * z) of n -> n
       where
Ben Gamari's avatar
Ben Gamari committed
52 53 54
         m1 = realToFrac m11
         m2 = realToFrac m12
         m3 = realToFrac m13
Simon Marlow's avatar
Simon Marlow committed
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

mat1x4 :: Row -> Arr -> Row
mat1x4 a (b1,b2,b3) = (c1,c2,c3,c4)
   where
     c1 = dorow' a (b11,b21,b31)
     c2 = dorow' a (b12,b22,b32)
     c3 = dorow' a (b13,b23,b33)
     c4 = dorow  a (b14,b24,b34)
     (b11,b12,b13,b14) = b1
     (b21,b22,b23,b24) = b2
     (b31,b32,b33,b34) = b3

mat4x4 :: Arr -> Arr -> Arr
mat4x4 a (b1,b2,b3) = (c1,c2,c3)
   where
     c1 = (c11,c12,c13,c14)
     c2 = (c21,c22,c23,c24)
     c3 = (c31,c32,c33,c34)
     (b11,b12,b13,b14) = b1
     (b21,b22,b23,b24) = b2
     (b31,b32,b33,b34) = b3
     (c11,c21,c31) = mat4x1' a (b11,b21,b31)
     (c12,c22,c32) = mat4x1' a (b12,b22,b32)
     (c13,c23,c33) = mat4x1' a (b13,b23,b33)
     (c14,c24,c34) = mat4x1  a (b14,b24,b34)