Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
@@ -30,6 +30,7 @@ module Graphics.Implicit(
30 30 -- Export
31 31 writeSVG,
32 32 writeSTL,
  33 + writeBinSTL,
33 34 writeOBJ,
34 35 writeTHREEJS,
35 36 writeSCAD2,
4 Graphics/Implicit/Export.hs
@@ -9,6 +9,7 @@ import Graphics.Implicit.Definitions
9 9 import Data.Text.Lazy (Text,pack)
10 10 import Data.Text.Lazy.IO (writeFile)
11 11 import Prelude hiding (writeFile)
  12 +import qualified Data.ByteString.Lazy as LBS
12 13
13 14 -- class DiscreteApproxable
14 15 import Graphics.Implicit.Export.Definitions
@@ -45,6 +46,9 @@ formatObject res format = format . discreteAprox res
45 46 writeSVG res = writeObject res PolylineFormats.svg
46 47
47 48 writeSTL res = writeObject res TriangleMeshFormats.stl
  49 +
  50 +writeBinSTL res file obj = LBS.writeFile file $ TriangleMeshFormats.binaryStl $ discreteAprox res obj
  51 +
48 52 writeOBJ res = writeObject res NormedTriangleMeshFormats.obj
49 53 writeTHREEJS res = writeObject res TriangleMeshFormats.jsTHREE
50 54
4 Graphics/Implicit/Export/TextBuilderUtils.hs
@@ -7,7 +7,6 @@ module Graphics.Implicit.Export.TextBuilderUtils
7 7 -- Values from Data.Text.Lazy
8 8 Text
9 9 ,pack
10   - ,replicate
11 10 -- Values from Data.Text.Lazy.Builder, as well as some special builders
12 11 ,Builder
13 12 ,toLazyText
@@ -36,9 +35,6 @@ import Data.Text.Lazy.Builder.Int
36 35
37 36 import Graphics.Implicit.Definitions
38 37
39   -import Prelude hiding (replicate)
40   -
41   -
42 38 -- The chunk size for toLazyText is very small (128 bytes), so we export
43 39 -- a version with a much larger size (~16 K)
44 40 toLazyText :: Builder -> Text
17 Graphics/Implicit/Export/TriangleMeshFormats.hs
@@ -8,6 +8,15 @@ module Graphics.Implicit.Export.TriangleMeshFormats where
8 8 import Graphics.Implicit.Definitions
9 9 import Graphics.Implicit.Export.TextBuilderUtils
10 10
  11 +
  12 +
  13 +import Blaze.ByteString.Builder hiding (Builder)
  14 +import Blaze.ByteString.Builder.ByteString
  15 +import Data.ByteString (replicate)
  16 +import Data.ByteString.Lazy (ByteString)
  17 +
  18 +import Prelude hiding (replicate)
  19 +
11 20 stl triangles = toLazyText $ stlHeader <> mconcat (map triangle triangles) <> stlFooter
12 21 where
13 22 stlHeader = "solid ImplictCADExport\n"
@@ -26,6 +35,14 @@ stl triangles = toLazyText $ stlHeader <> mconcat (map triangle triangles) <> st
26 35 <> vertex c
27 36 <> "\nendloop\nendfacet\n"
28 37
  38 +binaryStl :: [Triangle] -> ByteString
  39 +binaryStl triangles = toLazyByteString $ header <> lengthField <> mconcat (map triangle triangles)
  40 + where header = fromByteString $ replicate 80 0
  41 + lengthField = fromWord32le $ toEnum $ length triangles
  42 + triangle (a,b,c) = normal <> point a <> point b <> point c <> fromWord16le 0
  43 + point (x,y,z) = fromWrite $ writeStorable x <> writeStorable y <> writeStorable z
  44 + normal = fromWrite $ writeStorable (0::Float) <> writeStorable (0::Float) <> writeStorable (0::Float) --}
  45 +
29 46 jsTHREE :: TriangleMesh -> Text
30 47 jsTHREE triangles = toLazyText $ header <> vertcode <> facecode <> footer
31 48 where
4 implicit.cabal
@@ -26,7 +26,9 @@ Library
26 26 deepseq,
27 27 text,
28 28 blaze-svg,
29   - mtl
  29 + mtl,
  30 + bytestring,
  31 + blaze-builder
30 32
31 33 ghc-options:
32 34 -O2 -optc-O3

0 comments on commit 5985981

Please sign in to comment.
Something went wrong with that request. Please try again.