Permalink
Browse files

Be more explicit about endianness for stl export.

Note that I'm explicitly using Float for the signature of float32LE
instead of ℝ - if ℝ should change to Double in the future, we'll type
error, not produce bad stl. storable-endian doesn't seem to add any
significant performance issues - although it (the package) does use
a depreciated function (unsafeIO from the wrong module).
  • Loading branch information...
matthewSorensen committed Sep 10, 2012
1 parent 4bd85a6 commit 912a3634672358aabe71912438454aaaaa528154
Showing with 16 additions and 10 deletions.
  1. +6 −5 Graphics/Implicit.hs
  2. +8 −4 Graphics/Implicit/Export/TriangleMeshFormats.hs
  3. +2 −1 implicit.cabal
View
@@ -51,11 +51,12 @@ import Graphics.Implicit.Definitions
-- We want Export to be a bit less polymorphic
-- (so that types will collapse nicely)
-writeSVG = Export.writeSVG :: -> FilePath -> SymbolicObj2 -> IO ()
-writeSTL = Export.writeSTL :: -> FilePath -> SymbolicObj3 -> IO ()
-writeOBJ = Export.writeOBJ :: -> FilePath -> SymbolicObj3 -> IO ()
-writeSCAD2 = Export.writeSCAD2 :: -> FilePath -> SymbolicObj2 -> IO ()
-writeSCAD3 = Export.writeSCAD3 :: -> FilePath -> SymbolicObj3 -> IO ()
+writeSVG = Export.writeSVG :: -> FilePath -> SymbolicObj2 -> IO ()
+writeSTL = Export.writeSTL :: -> FilePath -> SymbolicObj3 -> IO ()
+writeBinSTL = Export.writeBinSTL :: -> FilePath -> SymbolicObj3 -> IO ()
+writeOBJ = Export.writeOBJ :: -> FilePath -> SymbolicObj3 -> IO ()
+writeSCAD2 = Export.writeSCAD2 :: -> FilePath -> SymbolicObj2 -> IO ()
+writeSCAD3 = Export.writeSCAD3 :: -> FilePath -> SymbolicObj3 -> IO ()
writeTHREEJS = Export.writeTHREEJS :: -> FilePath -> SymbolicObj3 -> IO ()
writeGCodeHacklabLaser = Export.writeGCodeHacklabLaser :: -> FilePath -> SymbolicObj2 -> IO ()
@@ -8,12 +8,11 @@ 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 Data.Storable.Endian
import Prelude hiding (replicate)
@@ -35,13 +34,18 @@ stl triangles = toLazyText $ stlHeader <> mconcat (map triangle triangles) <> st
<> vertex c
<> "\nendloop\nendfacet\n"
+
+-- Write a 32-bit little-endian float to a buffer.
+float32LE :: Float -> Write
+float32LE = writeStorable . LE
+
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) --}
+ point (x,y,z) = fromWrite $ float32LE x <> float32LE y <> float32LE z
+ normal = fromWrite $ float32LE 0 <> float32LE 0 <> float32LE 0
jsTHREE :: TriangleMesh -> Text
jsTHREE triangles = toLazyText $ header <> vertcode <> facecode <> footer
View
@@ -28,7 +28,8 @@ Library
blaze-svg,
mtl,
bytestring,
- blaze-builder
+ blaze-builder,
+ storable-endian
ghc-options:
-O2 -optc-O3

0 comments on commit 912a363

Please sign in to comment.