diff options
author | Joe Crayne <joe@jerkface.net> | 2019-06-14 20:41:03 -0400 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2019-06-14 20:41:03 -0400 |
commit | 98aa7d7177aaf46171b095bbb28e2f1e868323c5 (patch) | |
tree | b62a6866386ce8199d675a09180583c0b8150b61 /src/Wavefront.hs | |
parent | 913a569f9c5176c436d6abffbba64b7a98cbac86 (diff) |
Reorganizing.
Diffstat (limited to 'src/Wavefront.hs')
-rw-r--r-- | src/Wavefront.hs | 27 |
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 #-} |
2 | module Wavefront where | 2 | module Wavefront where |
3 | 3 | ||
4 | import Data.Wavefront | 4 | import Wavefront.Types |
5 | import Wavefront.Lex | 5 | import Wavefront.Lex |
6 | 6 | ||
7 | import Control.Arrow | 7 | import Control.Arrow |
@@ -16,10 +16,13 @@ import qualified Data.Vector as Vector | |||
16 | ;import Data.Vector (Vector) | 16 | ;import Data.Vector (Vector) |
17 | import qualified Rank2 | 17 | import qualified Rank2 |
18 | 18 | ||
19 | type WavefrontOBJ = OBJ Vector | ||
20 | |||
19 | newtype Count x = Count Int | 21 | newtype Count x = Count Int |
20 | 22 | ||
21 | updateCount :: MonadState s m => (s -> Count x) -> (Count x -> s -> s) -> m () | 23 | |
22 | updateCount field setField = do | 24 | incrementCount :: MonadState s m => (s -> Count x) -> (Count x -> s -> s) -> m () |
25 | incrementCount 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 | |||
28 | fixupRef (Count n) x | x > 0 = x - 1 -- Renumber from 0. | 31 | fixupRef (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 | ||
31 | fixupTriple :: WavefrontOBJ Count -> RefTriple -> RefTriple | 34 | fixupTriple :: OBJ Count -> RefTriple -> RefTriple |
32 | fixupTriple o (RefTriple v t n) = | 35 | fixupTriple 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 | ||
37 | objBookKeeping :: Monad m => ObjBuilder m -> ObjBuilder (StateT (WavefrontOBJ Count) m) | 40 | objBookKeeping :: Monad m => ObjBuilder m -> ObjBuilder (StateT (OBJ Count) m) |
38 | objBookKeeping builder = (lift Rank2.<$> builder) | 41 | objBookKeeping 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 | |||
75 | modifyFirst :: MonadState (c, d) m => (c -> c) -> m () | 78 | modifyFirst :: MonadState (c, d) m => (c -> c) -> m () |
76 | modifyFirst = modify' . first | 79 | modifyFirst = modify' . first |
77 | 80 | ||
78 | buildOBJ :: ObjBuilder (State (WavefrontOBJ DList,Element ())) | 81 | buildOBJ :: ObjBuilder (State (OBJ DList,Element ())) |
79 | buildOBJ = (nullBuilder $ pure ()) | 82 | buildOBJ = (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 | ||
115 | parse :: L.ByteString -> WavefrontOBJ Vector | 118 | parse :: L.ByteString -> OBJ Vector |
116 | parse bs = Rank2.fmap (Vector.fromList . DList.toList) obj | 119 | parse 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 = [] |