Permalink
Browse files

Switch to using Data.Text.Lazy instead of String for output.

Note that all of the existing formatting functions still build strings,
so this commit introduces no performance gains. However, I plan on rewriting
all of them using the relevant builder modules, so all of the output
code paths should get much faster.

(Commit 1/n of my quest to add efficient GHCLive support for ImplicitCAD.)
  • Loading branch information...
1 parent 15e88c2 commit d6164e95f8eba6c3b82875830c7b9be5acb97698 @matthewSorensen committed Aug 23, 2012
View
31 Graphics/Implicit/Export.hs
@@ -6,7 +6,9 @@ module Graphics.Implicit.Export where
import Graphics.Implicit.Definitions
--import Graphics.Implicit.Operations (slice)
-import System.IO (writeFile)
+import Data.Text.Lazy (Text,pack)
+import Data.Text.Lazy.IO (writeFile)
+import Prelude hiding (writeFile)
-- class DiscreteApproxable
import Graphics.Implicit.Export.Definitions
@@ -24,18 +26,21 @@ import qualified Graphics.Implicit.Export.SymbolicFormats as SymbolicFormats
-- Write an object in a given formet...
writeObject :: (DiscreteAproxable obj aprox) =>
- -- ^ Resolution
- -> (aprox -> String) -- ^ File Format (Function that formats)
- -> FilePath -- ^ File Name
- -> obj -- ^ Object to render
- -> IO() -- ^ Writing Action!
+ -- ^ Resolution
+ -> (aprox -> Text) -- ^ File Format (Function that formats)
+ -> FilePath -- ^ File Name
+ -> obj -- ^ Object to render
+ -> IO () -- ^ Writing Action!
-writeObject res format filename obj = writeFile filename text
- where
- aprox = discreteAprox res obj
- text = format aprox
+writeObject res format filename obj = writeFile filename $ formatObject res format obj
--- Now functions to write it in specific formats
+formatObject :: (DiscreteAproxable obj aprox) =>
+ -- ^ Resolution
+ -> (aprox -> Text) -- ^ File Format (Function that formats)
+ -> obj -- ^ Object to render
+ -> Text -- ^ Resulting lazy ByteString
+
+formatObject res format = format . discreteAprox res
writeSVG res = writeObject res PolylineFormats.svg
@@ -45,8 +50,8 @@ writeTHREEJS res = writeObject res TriangleMeshFormats.jsTHREE
writeGCodeHacklabLaser res = writeObject res PolylineFormats.hacklabLaserGCode
-writeSCAD3 res filename obj = writeFile filename (SymbolicFormats.scad3 res obj)
-writeSCAD2 res filename obj = writeFile filename (SymbolicFormats.scad2 res obj)
+writeSCAD3 res filename obj = writeFile filename $ pack $ SymbolicFormats.scad3 res obj
+writeSCAD2 res filename obj = writeFile filename $ pack $ SymbolicFormats.scad2 res obj
{-
View
4 Graphics/Implicit/Export/NormedTriangleMeshFormats.hs
@@ -5,7 +5,9 @@ module Graphics.Implicit.Export.NormedTriangleMeshFormats where
import Graphics.Implicit.Definitions
-obj normedtriangles = text
+import Data.Text.Lazy (pack)
+
+obj normedtriangles = pack text
where
-- A vertex line; v (0.0, 0.0, 1.0) = "v 0.0 0.0 1.0\n"
v :: ℝ3 -> String
View
10 Graphics/Implicit/Export/PolylineFormats.hs
@@ -8,8 +8,10 @@ import Graphics.Implicit.Definitions
import Text.Printf (printf)
-svg :: [Polyline] -> String
-svg polylines = text
+import Data.Text.Lazy (Text,pack)
+
+svg :: [Polyline] -> Text
+svg polylines = pack text
where
-- SVG is stupidly laid out... (0,0) is the top left corner
(xs, ys) = unzip (concat polylines)
@@ -25,8 +27,8 @@ svg polylines = text
++ svglines
++ "</svg> "
-hacklabLaserGCode :: [Polyline] -> String
-hacklabLaserGCode polylines = text
+hacklabLaserGCode :: [Polyline] -> Text
+hacklabLaserGCode polylines = pack text
where
gcodeHeader =
"(generated by ImplicitCAD, based of hacklab wiki example)\n"
View
8 Graphics/Implicit/Export/TriangleMeshFormats.hs
@@ -5,7 +5,9 @@ module Graphics.Implicit.Export.TriangleMeshFormats where
import Graphics.Implicit.Definitions
-stl triangles = text
+import Data.Text.Lazy (Text,pack)
+
+stl triangles = pack text
where
stlHeader = "solid ImplictCADExport\n"
stlFooter = "endsolid ImplictCADExport\n"
@@ -25,8 +27,8 @@ stl triangles = text
++ stlFooter
-jsTHREE :: TriangleMesh -> String
-jsTHREE triangles = text
+jsTHREE :: TriangleMesh -> Text
+jsTHREE triangles = pack text
where
-- some dense JS. Let's make helper functions so that we don't repeat code each line
header =
View
3 implicit.cabal
@@ -23,7 +23,8 @@ Library
parallel,
containers,
plugins,
- deepseq
+ deepseq,
+ text
ghc-options:
-O2 -optc-O3

0 comments on commit d6164e9

Please sign in to comment.