forked from Haskell-Things/ImplicitCAD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
implicitsnap.hs
158 lines (135 loc) · 5.32 KB
/
implicitsnap.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
{-# LANGUAGE OverloadedStrings, ViewPatterns #-}
-- PACKAGES: snap, silently
{- This is a Snap server providing a ImplicitCAD REST API.
It does not install by default. Its dependencies are not in the cabal file.
We're just sticking it in the repo for lack of a better place... -}
module Main where
import Control.Applicative
import Snap.Core
import Snap.Http.Server
import Snap.Util.GZip (withCompression)
import Graphics.Implicit (runOpenscad, extrudeR)
import Graphics.Implicit.ExtOpenScad.Definitions (OVal (ONum))
import Graphics.Implicit.ObjectUtil (getBox2, getBox3)
import Graphics.Implicit.Export.TriangleMeshFormats (jsTHREE, stl)
import Graphics.Implicit.Export.PolylineFormats (svg, hacklabLaserGCode)
import Graphics.Implicit.Definitions (xmlErrorOn, errorMessage)
import Data.Map as Map
import Text.ParserCombinators.Parsec (errorPos, sourceLine)
import Text.ParserCombinators.Parsec.Error
-- class DiscreteApproxable
import Graphics.Implicit.Export.Definitions
-- instances of DiscreteApproxable...
import Graphics.Implicit.Export.SymbolicObj2
import Graphics.Implicit.Export.SymbolicObj3
import System.IO.Unsafe (unsafePerformIO)
import System.IO.Silently (capture)
import qualified Data.ByteString.Char8 as BS.Char
import qualified Data.Text.Lazy as TL
main :: IO ()
main = quickHttpServe site
site :: Snap ()
site = route
[
("render/", renderHandler)
] <|> writeBS "fall through"
renderHandler :: Snap ()
renderHandler = method GET $ withCompression $ do
modifyResponse $ setContentType "application/x-javascript"
request <- getRequest
case (rqParam "source" request, rqParam "callback" request, rqParam "format" request) of
(Just [source], Just [callback], Nothing) -> do
writeBS $ BS.Char.pack $ executeAndExport
(BS.Char.unpack source)
(BS.Char.unpack callback)
Nothing
(Just [source], Just [callback], Just [format]) -> do
writeBS $ BS.Char.pack $ executeAndExport
(BS.Char.unpack source)
(BS.Char.unpack callback)
(Just $ BS.Char.unpack format)
(_, _, _) -> writeBS "must provide source and callback as 1 GET variable each"
getRes (varlookup, obj2s, obj3s) =
let
qual = case Map.lookup "$quality" varlookup of
Just (ONum n) | n >= 1 -> n
_ -> 1
(defaultRes, qualRes) = case (obj2s, obj3s) of
(_, obj:_) -> ( min (minimum [x,y,z]/2) ((x*y*z )**(1/3) / 22)
, min (minimum [x,y,z]/2) ((x*y*z/qual)**(1/3) / 22))
where
((x1,y1,z1),(x2,y2,z2)) = getBox3 obj
(x,y,z) = (x2-x1, y2-y1, z2-z1)
(obj:_, _) -> ( min (min x y/2) ((x*y )**0.5 / 30)
, min (min x y/2) ((x*y/qual)**0.5 / 30) )
where
((x1,y1),(x2,y2)) = getBox2 obj
(x,y) = (x2-x1, y2-y1)
_ -> (1, 1)
in case Map.lookup "$res" varlookup of
Just (ONum requestedRes) ->
if defaultRes <= 30*requestedRes
then requestedRes
else -1
_ ->
if qual <= 30
then qualRes
else -1
getWidth (varlookup, _, obj:_) = maximum [x2-x1, y2-y1, z2-z1]
where ((x1,y1,z1),(x2,y2,z2)) = getBox3 obj
getWidth (varlookup, obj:_, _) = max (x2-x1) (y2-y1)
where ((x1,y1),(x2,y2)) = getBox2 obj
-- | Give an openscad object to run and the basename of
-- the target to write to... write an object!
executeAndExport :: String -> String -> Maybe String -> String
executeAndExport content callback maybeFormat =
let
showB True = "true"
showB False = "false"
callbackF :: Bool -> Bool -> Float -> String -> String
callbackF False is2D w msg =
callback ++ "([null," ++ show msg ++ "," ++ showB is2D ++ "," ++ show w ++ "]);"
callbackF True is2D w msg =
callback ++ "([new Shape()," ++ show msg ++ "," ++ showB is2D ++ "," ++ show w ++ "]);"
callbackS str msg = callback ++ "([" ++ show str ++ "," ++ show msg ++ ",null,null]);"
in case runOpenscad content of
Left err ->
let
line = sourceLine . errorPos $ err
showErrorMessages' = showErrorMessages
"or" "unknown parse error" "expecting" "unexpected" "end of input"
msgs :: String
msgs = showErrorMessages' $ errorMessages err
in callbackF False False 1 $ (\s-> "error (" ++ show line ++ "):" ++ s) msgs
Right openscadProgram -> unsafePerformIO $ do
(msgs,s) <- capture $ openscadProgram
let
res = getRes s
w = getWidth s
is2D = case s of
(_, _, x:xs) -> False
(_, x:xs, _) -> True
_ -> False
highResError = "Unreasonable resolution requested: "
++ "the server imps revolt! "
++ "(Install ImplicitCAD locally -- github.com/colah/ImplicitCAD/)"
objOrErr = case s of
(_, _, x:xs) ->
if res > 0
then Right (Nothing, x)
else Left highResError
(_, x:xs, _) ->
if res > 0
then Right (Just x, extrudeR 0 x res)
else Left highResError
_ -> Left $ msgs ++ "Nothing to render."
return $ case (objOrErr, maybeFormat) of
(Left errmsg, _) -> callbackF False False 1 errmsg
(Right (_,obj), Nothing) ->
TL.unpack (jsTHREE (discreteAprox res obj)) ++ callbackF True is2D w msgs
(Right (_,obj), Just "STL") ->
callbackS (TL.unpack (stl (discreteAprox res obj))) msgs
(Right (Just obj, _), Just "SVG") ->
callbackS (TL.unpack (svg (discreteAprox res obj))) msgs
(Right (Just obj, _), Just "gcode/hacklab-laser") ->
callbackS (TL.unpack (hacklabLaserGCode (discreteAprox res obj))) msgs