packages feed

juicy-gcode 0.1.0.5.2 → 0.1.0.6

raw patch · 8 files changed

+58/−15 lines, 8 files

Files

ChangeLog.md view
@@ -1,5 +1,9 @@ # Revision history for juicy-gcode
 
+## 0.1.0.6  -- 2020-05-11
+
+* Add option to mirror Y axis
+
 ## 0.1.0.5.2  -- 2020-04-11
 
 * Update dependencies
README.md view
@@ -25,7 +25,7 @@ juicy-gcode - The SVG to G-Code converter
 
 Usage: juicy-gcode.exe SVGFILE [-f|--flavor CONFIGFILE] [-o|--output OUTPUTFILE]
-                       [-d|--dpi DPI]
+                       [-d|--dpi DPI] [-m|--mirror-y-axis]
   Convert SVGFILE to G-Code
 
 Available options:
@@ -34,6 +34,7 @@   -f,--flavor CONFIGFILE   Configuration of G-Code flavor
   -o,--output OUTPUTFILE   The output G-Code file (default is standard output)
   -d,--dpi DPI             Density of the SVG file (default is 72 DPI)
+  -m,--mirror-y-axis       Mirror Y axis  
 ```
 
 ## Configuration 
juicy-gcode.cabal view
@@ -1,5 +1,5 @@ name:                juicy-gcode
-version:             0.1.0.5.2
+version:             0.1.0.6
 license:             BSD3
 license-file:        LICENSE
 author:              dlacko
@@ -19,7 +19,7 @@   hs-source-dirs:           src
   main-is:                  Main.hs
 
-  other-modules:            Approx BiArc CircularArc CubicBezier GCode Line Render SvgArcSegment Transformation Types
+  other-modules:            Approx BiArc CircularArc CubicBezier GCode Line Render SvgArcSegment Transformation Types BoundingBox
     
   build-depends:       
     base                    >=4.8    && <5, 
+ src/BoundingBox.hs view
@@ -0,0 +1,22 @@+module BoundingBox ( BoundingBox
+                   , boundingBox
+                   ) where
+
+import Types
+
+type BoundingBox = (Point, Point)
+
+points :: DrawOp -> [Point]
+points (DMoveTo p) = [p]
+points (DLineTo p) = [p]
+points (DBezierTo p1 p2 p3) = [p1, p2, p3]
+
+extendBB :: BoundingBox -> Point -> BoundingBox
+extendBB ((topLeftX, topLeftRight), (bottomRightX, bottomRightY)) (x,y) =
+    ((min topLeftX x, min topLeftRight y), (max bottomRightX x,max bottomRightY y))
+
+boundingBox  :: [DrawOp] -> BoundingBox
+boundingBox drawOps = 
+    foldl extendBB ((0,0),(0,0)) (concatMap points drawOps)
+
+
src/Main.hs view
@@ -3,15 +3,18 @@ import Data.Text
 import qualified Data.Configurator as C
 
+import Data.Monoid
+
 import Options.Applicative
 
 import Render
 import GCode
                                                  
-data Options = Options { _svgfile :: String
-                       , _cfgfile :: Maybe String
-                       , _outfile :: Maybe String
-                       , _dpi     :: Int
+data Options = Options { _svgfile     :: String
+                       , _cfgfile     :: Maybe String
+                       , _outfile     :: Maybe String
+                       , _dpi         :: Int
+                       , _mirrorYAxis :: Bool
                        }                
                 
 options :: Parser Options
@@ -35,14 +38,18 @@      <> short 'd'
      <> metavar "DPI"     
      <> help "Density of the SVG file (default is 72 DPI)" ))
+  <*> (switch
+      ( long "mirror-y-axis"
+     <> short 'm'
+     <> help "Mirror Y axis" ))
 
 runWithOptions :: Options -> IO ()
-runWithOptions (Options svgFile mbCfg mbOut dpi) =
+runWithOptions (Options svgFile mbCfg mbOut dpi mirrorYAxis) =
     do 
         mbDoc <- SVG.loadSvgFile svgFile
         flavor <- maybe (return defaultFlavor) readFlavor mbCfg
         case mbDoc of
-            (Just doc) -> writer (toString flavor dpi $ renderDoc dpi doc)
+            (Just doc) -> writer (toString flavor dpi $ renderDoc mirrorYAxis dpi doc)
             Nothing    -> putStrLn "juicy-gcode: error during opening the SVG file"
     where
         writer = maybe putStrLn (\fn -> writeFile fn) mbOut
src/Render.hs view
@@ -58,9 +58,17 @@ toAbsolute _ SVG.OriginAbsolute p = p
 toAbsolute (cx,cy) SVG.OriginRelative (dx,dy) = (cx+dx, cy+dy)
 
-renderDoc :: Int -> SVG.Document -> [GCodeOp]
-renderDoc dpi doc = stage2 $ renderTrees identityMatrix (SVG._elements doc)
+docTransform :: Bool -> Int-> SVG.Document -> TransformationMatrix
+docTransform False _ _ = identityMatrix
+docTransform _ dpi doc = mirrorYMatrix (fromIntegral w) (fromIntegral h)
     where
+        (w, h) = SVG.documentSize dpi doc
+
+renderDoc :: Bool -> Int -> SVG.Document -> [GCodeOp]
+renderDoc mirrorYAxis dpi doc = stage2 $ renderTrees 
+                                            (docTransform mirrorYAxis dpi doc)
+                                            (SVG._elements doc)
+    where
         -- TODO: make it tail recursive
         stage2 :: [DrawOp] -> [GCodeOp]
         stage2 dops = convert dops (Linear.V2 0 0)
@@ -68,7 +76,7 @@                 convert [] _ = []
                 convert (DMoveTo p:ds) _ = GMoveTo p : convert ds (fromPoint p)
                 convert (DLineTo p:ds) _ = GLineTo p : convert ds (fromPoint p)
-                convert (DBezierTo c1 c2 p2:ds) cp = concat (map biarc2garc biarcs) ++ convert ds (fromPoint p2)
+                convert (DBezierTo c1 c2 p2:ds) cp = concatMap biarc2garc biarcs ++ convert ds (fromPoint p2)
                     where
                         biarcs = bezier2biarc (B.CubicBezier cp (fromPoint c1) (fromPoint c2) (fromPoint p2)) 5 1
                         biarc2garc biarc = [arc2garc (BA._a1 biarc), arc2garc (BA._a2 biarc)] 
src/Transformation.hs view
@@ -1,5 +1,6 @@ module Transformation ( TransformationMatrix
                       , identityMatrix
+                      , mirrorYMatrix
                       , transformPoint
                       , transformDrawOp
                       , applyTransformations
@@ -13,6 +14,9 @@              
 identityMatrix :: TransformationMatrix
 identityMatrix = identity 3
+
+mirrorYMatrix :: Double -> Double -> TransformationMatrix
+mirrorYMatrix _ h = fromElements [1, 0, 0, -1, 0, h]
 
 fromElements :: [Double] -> TransformationMatrix
 fromElements [a,b,c,d,e,f] = fromList 3 3 [a,c,e,b,d,f,0,0,1]
src/Types.hs view
@@ -1,5 +1,4 @@ module Types ( Point
-             , DArcDir
              , DrawOp (..)
              , GCodeOp (..)
              , if'
@@ -7,8 +6,6 @@ 
 -- type Command = String
 type Point = (Double,Double) -- A point in the plane, absolute coordinates
-
-data DArcDir = CC | CCW deriving Show
 
 -- all of them are invariant under affine transformation
 data DrawOp = DMoveTo Point