Read the obj file as ByteString and pass ByteString to the Parser

This commit is contained in:
hasufell 2014-11-21 04:49:17 +01:00
parent 2d7a62770a
commit e673fee652
No known key found for this signature in database
GPG Key ID: 220CD1C5BDEED020
5 changed files with 13 additions and 10 deletions

View File

@ -2,6 +2,7 @@
module CLI.Gif where module CLI.Gif where
import qualified Data.ByteString.Char8 as B
import Diagrams.Backend.Cairo.CmdLine import Diagrams.Backend.Cairo.CmdLine
import Graphics.Diagram.Gif import Graphics.Diagram.Gif
import MyPrelude import MyPrelude
@ -9,5 +10,5 @@ import MyPrelude
gifCLI :: FilePath -> IO () gifCLI :: FilePath -> IO ()
gifCLI _ = do gifCLI _ = do
mesh <- readFile "UB1_sonderfaelle.obj" mesh <- B.readFile "UB1_sonderfaelle.obj"
gifMain (gifDiagS def mesh) gifMain (gifDiagS def mesh)

View File

@ -5,6 +5,7 @@ module GUI.Gtk (makeGUI) where
import Control.Applicative import Control.Applicative
import Control.Monad(unless) import Control.Monad(unless)
import Control.Monad.IO.Class import Control.Monad.IO.Class
import qualified Data.ByteString.Char8 as B
import Diagrams.Prelude import Diagrams.Prelude
import Diagrams.Backend.Cairo import Diagrams.Backend.Cairo
import Diagrams.Backend.Cairo.Internal import Diagrams.Backend.Cairo.Internal
@ -235,7 +236,7 @@ saveAndDrawDiag :: FilePath -- ^ obj file to parse
saveAndDrawDiag fp fps mygui = saveAndDrawDiag fp fps mygui =
if (==) ".obj" . takeExtension $ fp if (==) ".obj" . takeExtension $ fp
then do then do
mesh <- readFile fp mesh <- B.readFile fp
mainDrawWindow <- widgetGetDrawWindow (mainDraw mygui) mainDrawWindow <- widgetGetDrawWindow (mainDraw mygui)
treeDrawWindow <- widgetGetDrawWindow (treeDraw mygui) treeDrawWindow <- widgetGetDrawWindow (treeDraw mygui)
adjustment <- rangeGetAdjustment (ptScale mygui) adjustment <- rangeGetAdjustment (ptScale mygui)

View File

@ -5,6 +5,7 @@ module Graphics.Diagram.Gif where
import Algebra.VectorTypes import Algebra.VectorTypes
import Algorithms.ConvexHull.GrahamScan import Algorithms.ConvexHull.GrahamScan
import Codec.Picture.Gif import Codec.Picture.Gif
import qualified Data.ByteString.Char8 as B
import Data.Monoid import Data.Monoid
import Diagrams.Backend.Cairo import Diagrams.Backend.Cairo
import Diagrams.Prelude hiding ((<>)) import Diagrams.Prelude hiding ((<>))
@ -33,5 +34,5 @@ gifDiag p xs =
-- |Same as gifDiag, except that it takes a string containing the -- |Same as gifDiag, except that it takes a string containing the
-- mesh file content instead of the the points. -- mesh file content instead of the the points.
gifDiagS :: DiagProp -> MeshString -> [(Diagram Cairo R2, GifDelay)] gifDiagS :: DiagProp -> B.ByteString -> [(Diagram Cairo R2, GifDelay)]
gifDiagS p = gifDiag p . filterValidPT p . meshToArr gifDiagS p = gifDiag p . filterValidPT p . meshToArr

View File

@ -2,6 +2,7 @@
module Graphics.Diagram.Gtk where module Graphics.Diagram.Gtk where
import qualified Data.ByteString.Char8 as B
import Diagrams.Backend.Cairo import Diagrams.Backend.Cairo
import Diagrams.Prelude import Diagrams.Prelude
import Graphics.Diagram.Plotter import Graphics.Diagram.Plotter
@ -36,7 +37,7 @@ diag p objs@(Objects _)
-- |Create the Diagram from a String which is supposed to be the contents -- |Create the Diagram from a String which is supposed to be the contents
-- of an obj file. -- of an obj file.
diagS :: DiagProp -> MeshString -> Diagram Cairo R2 diagS :: DiagProp -> B.ByteString -> Diagram Cairo R2
diagS p mesh diagS p mesh
| algo p == 2 || algo p == 3 = | algo p == 2 || algo p == 3 =
diag p diag p
@ -49,7 +50,7 @@ diagS p mesh
-- |Create the tree diagram from a String which is supposed to be the contents -- |Create the tree diagram from a String which is supposed to be the contents
-- of an obj file. -- of an obj file.
diagTreeS :: DiagProp -> MeshString -> Diagram Cairo R2 diagTreeS :: DiagProp -> B.ByteString -> Diagram Cairo R2
diagTreeS p mesh diagTreeS p mesh
| algo p == 4 = mkDiag treePretty p (Object | algo p == 4 = mkDiag treePretty p (Object
. filterValidPT p . filterValidPT p

View File

@ -12,24 +12,23 @@ import Diagrams.TwoD.Types
-- |Convert a text String with multiple vertices and faces into -- |Convert a text String with multiple vertices and faces into
-- a list of vertices, ordered by the faces specification. -- a list of vertices, ordered by the faces specification.
facesToArr :: String -> [[PT]] facesToArr :: B.ByteString -> [[PT]]
facesToArr str = fmap (fmap (\y -> meshs str !! (fromIntegral y - 1))) facesToArr str = fmap (fmap (\y -> meshs str !! (fromIntegral y - 1)))
(faces str) (faces str)
where where
meshs = meshToArr meshs = meshToArr
faces = rights . fmap (parseOnly parseFace) . B.lines . B.pack faces = rights . fmap (parseOnly parseFace) . B.lines
-- |Convert a text String with multiple vertices into -- |Convert a text String with multiple vertices into
-- an array of float tuples. -- an array of float tuples.
meshToArr :: String -- ^ the string to convert meshToArr :: B.ByteString -- ^ the string to convert
-> [PT] -- ^ the resulting vertice table -> [PT] -- ^ the resulting vertice table
meshToArr = meshToArr =
fmap p2 fmap p2
. rights . rights
. fmap (parseOnly parseVertice) . fmap (parseOnly parseVertice)
. B.lines . B.lines
. B.pack
-- |Creates a Parser that accepts a single vertice, such as 'v 1.0 2.0'. -- |Creates a Parser that accepts a single vertice, such as 'v 1.0 2.0'.