Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Binary stl version. So much faster than the ASCII output

  • Loading branch information...
commit 59859814926b0c75b3668ccd86c2c9387cc3fa1c 1 parent 71dc7e4
Matthew Sorensen authored
1  Graphics/Implicit.hs
View
@@ -30,6 +30,7 @@ module Graphics.Implicit(
-- Export
writeSVG,
writeSTL,
+ writeBinSTL,
writeOBJ,
writeTHREEJS,
writeSCAD2,
4 Graphics/Implicit/Export.hs
View
@@ -9,6 +9,7 @@ import Graphics.Implicit.Definitions
import Data.Text.Lazy (Text,pack)
import Data.Text.Lazy.IO (writeFile)
import Prelude hiding (writeFile)
+import qualified Data.ByteString.Lazy as LBS
-- class DiscreteApproxable
import Graphics.Implicit.Export.Definitions
@@ -45,6 +46,9 @@ formatObject res format = format . discreteAprox res
writeSVG res = writeObject res PolylineFormats.svg
writeSTL res = writeObject res TriangleMeshFormats.stl
+
+writeBinSTL res file obj = LBS.writeFile file $ TriangleMeshFormats.binaryStl $ discreteAprox res obj
+
writeOBJ res = writeObject res NormedTriangleMeshFormats.obj
writeTHREEJS res = writeObject res TriangleMeshFormats.jsTHREE
4 Graphics/Implicit/Export/TextBuilderUtils.hs
View
@@ -7,7 +7,6 @@ module Graphics.Implicit.Export.TextBuilderUtils
-- Values from Data.Text.Lazy
Text
,pack
- ,replicate
-- Values from Data.Text.Lazy.Builder, as well as some special builders
,Builder
,toLazyText
@@ -36,9 +35,6 @@ import Data.Text.Lazy.Builder.Int
import Graphics.Implicit.Definitions
-import Prelude hiding (replicate)
-
-
-- The chunk size for toLazyText is very small (128 bytes), so we export
-- a version with a much larger size (~16 K)
toLazyText :: Builder -> Text
17 Graphics/Implicit/Export/TriangleMeshFormats.hs
View
@@ -8,6 +8,15 @@ module Graphics.Implicit.Export.TriangleMeshFormats where
import Graphics.Implicit.Definitions
import Graphics.Implicit.Export.TextBuilderUtils
+
+
+import Blaze.ByteString.Builder hiding (Builder)
+import Blaze.ByteString.Builder.ByteString
+import Data.ByteString (replicate)
+import Data.ByteString.Lazy (ByteString)
+
+import Prelude hiding (replicate)
+
stl triangles = toLazyText $ stlHeader <> mconcat (map triangle triangles) <> stlFooter
where
stlHeader = "solid ImplictCADExport\n"
@@ -26,6 +35,14 @@ stl triangles = toLazyText $ stlHeader <> mconcat (map triangle triangles) <> st
<> vertex c
<> "\nendloop\nendfacet\n"
+binaryStl :: [Triangle] -> ByteString
+binaryStl triangles = toLazyByteString $ header <> lengthField <> mconcat (map triangle triangles)
+ where header = fromByteString $ replicate 80 0
+ lengthField = fromWord32le $ toEnum $ length triangles
+ triangle (a,b,c) = normal <> point a <> point b <> point c <> fromWord16le 0
+ point (x,y,z) = fromWrite $ writeStorable x <> writeStorable y <> writeStorable z
+ normal = fromWrite $ writeStorable (0::Float) <> writeStorable (0::Float) <> writeStorable (0::Float) --}
+
jsTHREE :: TriangleMesh -> Text
jsTHREE triangles = toLazyText $ header <> vertcode <> facecode <> footer
where
4 implicit.cabal
View
@@ -26,7 +26,9 @@ Library
deepseq,
text,
blaze-svg,
- mtl
+ mtl,
+ bytestring,
+ blaze-builder
ghc-options:
-O2 -optc-O3
Please sign in to comment.
Something went wrong with that request. Please try again.