summaryrefslogtreecommitdiff
path: root/MeshSketch.hs
diff options
context:
space:
mode:
Diffstat (limited to 'MeshSketch.hs')
-rw-r--r--MeshSketch.hs67
1 files changed, 27 insertions, 40 deletions
diff --git a/MeshSketch.hs b/MeshSketch.hs
index 1019d72..14b3ebc 100644
--- a/MeshSketch.hs
+++ b/MeshSketch.hs
@@ -68,7 +68,8 @@ import RingBuffer
68import MaskableStream (AttributeKey,(@<-)) 68import MaskableStream (AttributeKey,(@<-))
69import SmallRing 69import SmallRing
70import Camera 70import Camera
71 71import FitCurves
72import Bezier
72 73
73prettyDebug :: GL.DebugMessage -> String 74prettyDebug :: GL.DebugMessage -> String
74prettyDebug (GL.DebugMessage src typ (GL.DebugMessageID mid) severity msg) = unwords ws 75prettyDebug (GL.DebugMessage src typ (GL.DebugMessageID mid) severity msg) = unwords ws
@@ -89,13 +90,6 @@ setupGLDebugging = do
89 GL.debugMessageControl (GL.MessageGroup Nothing Nothing Nothing) GL.$= GL.Enabled 90 GL.debugMessageControl (GL.MessageGroup Nothing Nothing Nothing) GL.$= GL.Enabled
90 GL.debugMessageCallback GL.$= Just pdebug 91 GL.debugMessageCallback GL.$= Just pdebug
91 92
92type Plane = Vector Float
93
94data RingPoint = RingPoint
95 { rpPosition :: AttributeKey (GLVector 3 Float)
96 , rpColor :: AttributeKey (GLVector 3 Float)
97 }
98 deriving Data
99 93
100ringPointAttr :: String -> String 94ringPointAttr :: String -> String
101ringPointAttr ('r':'p':c:cs) = toLower c : cs 95ringPointAttr ('r':'p':c:cs) = toLower c : cs
@@ -237,12 +231,6 @@ destroyState glarea st = do
237 -- widgetRemoveTickCallback glarea (stTickCallback st) 231 -- widgetRemoveTickCallback glarea (stTickCallback st)
238 return () 232 return ()
239 233
240deg30 :: Float
241deg30 = pi/6
242
243ĵ :: Vector Float
244ĵ = fromList [0,1,0]
245
246computePlaneModel :: Vector Float -> Matrix Float 234computePlaneModel :: Vector Float -> Matrix Float
247computePlaneModel plane = if n̂ == ĵ then translate4 p 235computePlaneModel plane = if n̂ == ĵ then translate4 p
248 else translate4 p <> rotate4 cosθ axis 236 else translate4 p <> rotate4 cosθ axis
@@ -258,7 +246,7 @@ whirlingCamera st = Animation $ \_ t -> do
258 let tf = realToFrac t :: Float 246 let tf = realToFrac t :: Float
259 rot = rotMatrixZ (-tf/2) <> rotMatrixX (-tf/pi) 247 rot = rotMatrixZ (-tf/2) <> rotMatrixX (-tf/pi)
260 modifyIORef (stCamera st) $ \cam -> cam 248 modifyIORef (stCamera st) $ \cam -> cam
261 { camUp = fromList [0,1,0] <# rot 249 { camUp = <# rot
262 , camDirection = (scale (1/camDistance cam) $ fromList [-2,-2,-10]) <# rot 250 , camDirection = (scale (1/camDistance cam) $ fromList [-2,-2,-10]) <# rot
263 , camWorldToScreen = Nothing 251 , camWorldToScreen = Nothing
264 , camScreenToWorld = Nothing 252 , camScreenToWorld = Nothing
@@ -571,25 +559,28 @@ worldCoordinates :: State -> Double -> Double -> Maybe (Vector Float) -> IO (Vec
571worldCoordinates st h k mplane = do 559worldCoordinates st h k mplane = do
572 pv <- atomicModifyIORef' (stCamera st) projectionView 560 pv <- atomicModifyIORef' (stCamera st) projectionView
573 cam <- readIORef (stCamera st) 561 cam <- readIORef (stCamera st)
574 let q0 = fromList [ 2 * realToFrac h/camWidth cam - 1 562 return $ camWorldCoordinates cam h k mplane
575 , 1 - 2 * realToFrac k/camHeight cam 563
576 , 1 564fitCurves :: State -> IO ()
577 , 1 565fitCurves st = do
578 ] :: Vector Float 566 _ <- atomicModifyIORef' (stCamera st) projectionView
579 q1 = pv #> q0 567 cam <- readIORef (stCamera st)
580 q2 = scale (1 /(q1!3)) $ G.init q1 568 plane <- readIORef (stPlane st)
581 p = camPos cam 569 mask <- ringMask (stDataRing st)
582 d = q2 - p 570 let max_curve_pts = ringCapacity (stRingBuffer st)
583 d̂ = unit d 571 buf = rBuffer (stRingBuffer st)
584 return $ case mplane of 572 dta = stDataPoints st
585 -- Write on the plane. 573 -- dta_cnt <- readIORef (rSize $ stDataRing st)
586 Just plane -> let n̂ = G.init plane 574 -- when (dta_cnt > 4) $ do
587 c = plane!3 575 -- when (idx > 0) $
588 a = (c - dot p n̂) / dot d̂ n̂ 576 midx <- fitCurve1 cam plane mask max_curve_pts buf dta
589 in p + scale a d̂ 577 forM_ midx $ \idx -> do
590 578 putStrLn $ "idx = " ++ show idx
591 -- Write on the camDistance sphere. 579 -- syncBuffer buf $ \cnt -> [(0,max cnt $ fromIntegral idx)]
592 Nothing -> p + scale (camDistance cam) d̂ 580 writeIORef (rBack $ stRingBuffer st) idx
581 writeIORef (rSize $ stRingBuffer st) idx
582 syncRing (stRingBuffer st)
583
593 584
594pushRing :: IsWidget w => w -> State 585pushRing :: IsWidget w => w -> State
595 -> Bool -- ^ True when press/release. 586 -> Bool -- ^ True when press/release.
@@ -647,14 +638,9 @@ pushRing w st endpt h k c = do
647 else do 638 else do
648 fromMaybe withEndpt $ take3 withTriple g 639 fromMaybe withEndpt $ take3 withTriple g
649 windowInvalidateRect win Nothing False 640 windowInvalidateRect win Nothing False
641 fitCurves st
650 return d 642 return d
651 643
652white,red,yellow,blue :: Vector Float
653white = fromList [1,1,1]
654yellow = fromList [1,1,0]
655blue = fromList [0,0,1]
656red = fromList [1,0,0]
657
658onEvent :: IsWidget w => w -> Realized -> Event -> IO Bool 644onEvent :: IsWidget w => w -> Realized -> Event -> IO Bool
659onEvent w realized ev = do 645onEvent w realized ev = do
660 msrc <- eventGetSourceDevice ev 646 msrc <- eventGetSourceDevice ev
@@ -710,6 +696,7 @@ onEvent w realized ev = do
710 writeIORef (stAngle st) 0 696 writeIORef (stAngle st) 0
711 writeIORef (stRecentPts st) Give0 697 writeIORef (stRecentPts st) Give0
712 clearRing (stRingBuffer st) 698 clearRing (stRingBuffer st)
699 clearRing (stDataRing st)
713 d <- pushRing w st True h k red 700 d <- pushRing w st True h k red
714 Just win <- getWidgetWindow w 701 Just win <- getWidgetWindow w
715 windowInvalidateRect win Nothing False 702 windowInvalidateRect win Nothing False