summaryrefslogtreecommitdiff
path: root/src/Wavefront.hs
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2019-06-14 20:41:03 -0400
committerJoe Crayne <joe@jerkface.net>2019-06-14 20:41:03 -0400
commit98aa7d7177aaf46171b095bbb28e2f1e868323c5 (patch)
treeb62a6866386ce8199d675a09180583c0b8150b61 /src/Wavefront.hs
parent913a569f9c5176c436d6abffbba64b7a98cbac86 (diff)
Reorganizing.
Diffstat (limited to 'src/Wavefront.hs')
-rw-r--r--src/Wavefront.hs27
1 files changed, 15 insertions, 12 deletions
diff --git a/src/Wavefront.hs b/src/Wavefront.hs
index 6195eee..1fc8ec3 100644
--- a/src/Wavefront.hs
+++ b/src/Wavefront.hs
@@ -1,7 +1,7 @@
1{-# LANGUAGE FlexibleContexts #-} 1{-# LANGUAGE FlexibleContexts #-}
2module Wavefront where 2module Wavefront where
3 3
4import Data.Wavefront 4import Wavefront.Types
5import Wavefront.Lex 5import Wavefront.Lex
6 6
7import Control.Arrow 7import Control.Arrow
@@ -16,10 +16,13 @@ import qualified Data.Vector as Vector
16 ;import Data.Vector (Vector) 16 ;import Data.Vector (Vector)
17import qualified Rank2 17import qualified Rank2
18 18
19type WavefrontOBJ = OBJ Vector
20
19newtype Count x = Count Int 21newtype Count x = Count Int
20 22
21updateCount :: MonadState s m => (s -> Count x) -> (Count x -> s -> s) -> m () 23
22updateCount field setField = do 24incrementCount :: MonadState s m => (s -> Count x) -> (Count x -> s -> s) -> m ()
25incrementCount field setField = do
23 Count c0 <- gets field 26 Count c0 <- gets field
24 let c = succ c0 27 let c = succ c0
25 c `seq` modify (setField $ Count c) 28 c `seq` modify (setField $ Count c)
@@ -28,20 +31,20 @@ fixupRef :: Count x -> Int -> Int
28fixupRef (Count n) x | x > 0 = x - 1 -- Renumber from 0. 31fixupRef (Count n) x | x > 0 = x - 1 -- Renumber from 0.
29 | otherwise = n + x -- Negative values are relative. 32 | otherwise = n + x -- Negative values are relative.
30 33
31fixupTriple :: WavefrontOBJ Count -> RefTriple -> RefTriple 34fixupTriple :: OBJ Count -> RefTriple -> RefTriple
32fixupTriple o (RefTriple v t n) = 35fixupTriple o (RefTriple v t n) =
33 RefTriple (fixupRef (objLocations o) v) 36 RefTriple (fixupRef (objLocations o) v)
34 (fixupRef (objTexCoords o) <$> t) 37 (fixupRef (objTexCoords o) <$> t)
35 (fixupRef (objNormals o) <$> n) 38 (fixupRef (objNormals o) <$> n)
36 39
37objBookKeeping :: Monad m => ObjBuilder m -> ObjBuilder (StateT (WavefrontOBJ Count) m) 40objBookKeeping :: Monad m => ObjBuilder m -> ObjBuilder (StateT (OBJ Count) m)
38objBookKeeping builder = (lift Rank2.<$> builder) 41objBookKeeping builder = (lift Rank2.<$> builder)
39 { vertex = \xs -> do lift $ vertex builder xs 42 { vertex = \xs -> do lift $ vertex builder xs
40 updateCount objLocations $ \x o -> o { objLocations = x } 43 incrementCount objLocations $ \x o -> o { objLocations = x }
41 , vertexT = \xs -> do lift $ vertexT builder xs 44 , vertexT = \xs -> do lift $ vertexT builder xs
42 updateCount objTexCoords $ \x o -> o { objTexCoords = x } 45 incrementCount objTexCoords $ \x o -> o { objTexCoords = x }
43 , vertexN = \xs -> do lift $ vertexN builder xs 46 , vertexN = \xs -> do lift $ vertexN builder xs
44 updateCount objNormals $ \x o -> o { objNormals = x } 47 incrementCount objNormals $ \x o -> o { objNormals = x }
45 , points = \xs -> do 48 , points = \xs -> do
46 n <- gets objLocations 49 n <- gets objLocations
47 lift $ points builder $ fixupRef n <$> xs 50 lift $ points builder $ fixupRef n <$> xs
@@ -75,7 +78,7 @@ elemental element x = fmap (const x) element
75modifyFirst :: MonadState (c, d) m => (c -> c) -> m () 78modifyFirst :: MonadState (c, d) m => (c -> c) -> m ()
76modifyFirst = modify' . first 79modifyFirst = modify' . first
77 80
78buildOBJ :: ObjBuilder (State (WavefrontOBJ DList,Element ())) 81buildOBJ :: ObjBuilder (State (OBJ DList,Element ()))
79buildOBJ = (nullBuilder $ pure ()) 82buildOBJ = (nullBuilder $ pure ())
80 { vertex = \xs -> modifyFirst $ \o -> o { objLocations = objLocations o `DList.snoc` mkv xs } 83 { vertex = \xs -> modifyFirst $ \o -> o { objLocations = objLocations o `DList.snoc` mkv xs }
81 , vertexT = \xs -> modifyFirst $ \o -> o { objTexCoords = objTexCoords o `DList.snoc` mkt xs } 84 , vertexT = \xs -> modifyFirst $ \o -> o { objTexCoords = objTexCoords o `DList.snoc` mkt xs }
@@ -112,11 +115,11 @@ buildOBJ = (nullBuilder $ pure ())
112 modify $ second $ \e -> e { elSmoothingGroup = fromIntegral x } 115 modify $ second $ \e -> e { elSmoothingGroup = fromIntegral x }
113 } 116 }
114 117
115parse :: L.ByteString -> WavefrontOBJ Vector 118parse :: L.ByteString -> OBJ Vector
116parse bs = Rank2.fmap (Vector.fromList . DList.toList) obj 119parse bs = Rank2.fmap (Vector.fromList . DList.toList) obj
117 where 120 where
118 b = objBookKeeping buildOBJ 121 b = objBookKeeping buildOBJ
119 ls = WavefrontOBJ 122 ls = OBJ
120 { objLocations = DList.empty 123 { objLocations = DList.empty
121 , objTexCoords = DList.empty 124 , objTexCoords = DList.empty
122 , objNormals = DList.empty 125 , objNormals = DList.empty
@@ -125,7 +128,7 @@ parse bs = Rank2.fmap (Vector.fromList . DList.toList) obj
125 , objFaces = DList.empty 128 , objFaces = DList.empty
126 , objMtlLibs = DList.empty 129 , objMtlLibs = DList.empty
127 } 130 }
128 c = Rank2.fmap (const $ Count 0) ls :: WavefrontOBJ Count 131 c = Rank2.fmap (const $ Count 0) ls :: OBJ Count
129 el = Element 132 el = Element
130 { elObject = Nothing 133 { elObject = Nothing
131 , elGroups = [] 134 , elGroups = []