Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Binary stl support #60

Merged
merged 4 commits into from

2 participants

@matthewSorensen

Here's the implementation of binary stl export. Seems stable - I've been using it since August 28th or so. Note that it does introduce one new dependency, storable-endian, in order to nail down the byte order for floats.

I've been seeing a 25% speed improvement on simple tasks such as the benchmark from the other pull request.

matthewSorensen added some commits
@matthewSorensen matthewSorensen Binary stl version. So much faster than the ASCII output 5985981
@matthewSorensen matthewSorensen Binary stl version. So much faster than the ASCII output e8fbffe
@matthewSorensen matthewSorensen Merge branch 'binary-stl' of github.com:matthewSorensen/ImplicitCAD i…
…nto binary-stl
4bd85a6
@matthewSorensen matthewSorensen 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).
912a363
@matthewSorensen

Also, commit 104d106 has a misspelled identifier.

@colah
Owner

Fixed the broken commit. That was really sloppy of me. Sorry about that.

I'll try to test and merge your code tomorrow.

@colah colah merged commit d3a7ca2 into colah:master
@colah
Owner

Or I could just do it now. :)

Great work! Thanks for your contribution.

@colah
Owner

And congratulations, this is now the default output for extopenscad.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 29, 2012
  1. @matthewSorensen
Commits on Sep 10, 2012
  1. @matthewSorensen
  2. @matthewSorensen
  3. @matthewSorensen

    Be more explicit about endianness for stl export.

    matthewSorensen authored
    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).
This page is out of date. Refresh to see the latest.
View
12 Graphics/Implicit.hs
@@ -30,6 +30,7 @@ module Graphics.Implicit(
-- Export
writeSVG,
writeSTL,
+ writeBinSTL,
writeOBJ,
writeTHREEJS,
writeSCAD2,
@@ -50,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 ()
View
4 Graphics/Implicit/Export.hs
@@ -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
View
4 Graphics/Implicit/Export/TextBuilderUtils.hs
@@ -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
View
21 Graphics/Implicit/Export/TriangleMeshFormats.hs
@@ -8,6 +8,14 @@ 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)
+
stl triangles = toLazyText $ stlHeader <> mconcat (map triangle triangles) <> stlFooter
where
stlHeader = "solid ImplictCADExport\n"
@@ -26,6 +34,19 @@ 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 $ float32LE x <> float32LE y <> float32LE z
+ normal = fromWrite $ float32LE 0 <> float32LE 0 <> float32LE 0
+
jsTHREE :: TriangleMesh -> Text
jsTHREE triangles = toLazyText $ header <> vertcode <> facecode <> footer
where
View
5 implicit.cabal
@@ -26,7 +26,10 @@ Library
deepseq,
text,
blaze-svg,
- mtl
+ mtl,
+ bytestring,
+ blaze-builder,
+ storable-endian
ghc-options:
-O2 -optc-O3
Something went wrong with that request. Please try again.