Instantly share code, notes, and snippets.

# NathanHowell/bar.hssecretLast active Aug 29, 2015

## Revisions

1. NathanHowell revised this gist Mar 25, 2014. 1 changed file with 25 additions and 18 deletions.
43 bar.hs
 @@ -1,10 +1,15 @@ import Prelude +-- import Data.List +import qualified Data.Sequence as S +import Data.Foldable +import Data.Monoid +import Data.Traversable data Qt = Qt { - qx :: Double, - qy :: Double, - qz :: Double, - qw :: Double + qx :: {-# UNPACK #-} !Double, + qy :: {-# UNPACK #-} !Double, + qz :: {-# UNPACK #-} !Double, + qw :: {-# UNPACK #-} !Double } deriving (Show) qmul :: Qt -> Qt -> Qt @@ -21,9 +26,9 @@ axis_ang :: Vec -> Double -> Qt axis_ang axis ang = Qt ((vx axis) * (sin (ang/2))) ((vy axis) * (sin (ang/2))) ((vz axis) * (sin (ang/2))) (cos (ang/2)) data Vec = Vec { - vx :: Double, - vy :: Double, - vz :: Double + vx :: {-# UNPACK #-} !Double, + vy :: {-# UNPACK #-} !Double, + vz :: {-# UNPACK #-} !Double } deriving (Show) vrot :: Qt -> Vec -> Vec @@ -67,11 +72,11 @@ data Pivot = Pivot { } deriving (Show) data Mesh = Mesh { - mesh_rot :: Qt, - mesh_cpos :: Vec, - mesh_positions :: [Double], - mesh_colors :: [Double], - mesh_indexes :: [Int] + mesh_rot :: !Qt, + mesh_cpos :: !Vec, + mesh_positions :: !(S.Seq Double), + mesh_colors :: !(S.Seq Double), + mesh_indexes :: !(S.Seq Int) } deriving (Show) faceIndexes :: Int -> Int -> Int -> Int -> [Int] @@ -86,23 +91,25 @@ faceIndexes i j w h = buildMesh :: [Pivot] -> Mesh -buildMesh pivots = foldl build_layer initial (zip [0..] pivots) where - initial = Mesh (Qt 0 0 0 1) (Vec 0 0 0) [] [] [] +buildMesh pivots = foldl' build_layer initial (zip [0..] pivots) where + initial = Mesh (Qt 0 0 0 1) (Vec 0 0 0) S.empty S.empty S.empty width = length (pivot_rads (head pivots)) height = length pivots depth = 3 - build_layer step (j,pivot) = foldl build_vertexes step (zip3 [0..] (pivot_rads pivot) (pivot_cols pivot)) where + build_layer step (j,pivot) = foldl' build_vertexes step (zip3 [0..] (pivot_rads pivot) (pivot_cols pivot)) where rot = qmul (mesh_rot step) (pivot_rot pivot) cpos = vadd (mesh_cpos step) (vrot rot (Vec 0 0 (pivot_len pivot))) build_vertexes step (i,vrad,vcol) = Mesh rot cpos positions colors indexes where vang = ((fromIntegral i)/(fromIntegral width))*2.0*pi vpos = vadd cpos (vrot rot (vproj vang vrad)) - positions = (mesh_positions step) ++ [(vx vpos),(vy vpos),(vz vpos)] - colors = (mesh_colors step) ++ [(r vcol),(g vcol),(b vcol)] - indexes = (mesh_indexes step) ++ (faceIndexes i j width height) + positions = (mesh_positions step) <> S.fromList [(vx vpos),(vy vpos),(vz vpos)] + colors = (mesh_colors step) <> S.fromList [(r vcol),(g vcol),(b vcol)] + indexes = (mesh_indexes step) <> S.fromList (faceIndexes i j width height) +mesh :: Mesh mesh = buildMesh (replicate 128 (Pivot 1 (Qt 0 0 0 1) [1,1,1,1,1,1,1,1] [(Col 1 1 1),(Col 1 1 1),(Col 1 1 1),(Col 1 1 1),(Col 1 1 1),(Col 1 1 1),(Col 1 1 1),(Col 1 1 1)])) +main :: IO () main = print mesh
2. NathanHowell created this gist Mar 25, 2014.
108 bar.hs