0 | module Geom.Gen2D.Generate
 1 |
 2 | import Data.Graph.Indexed.Subgraph
 3 | import Data.Linear.Traverse1
 4 | import Syntax.T1
 5 |
 6 | import Geom.Gen2D.Rings
 7 | import Geom.Gen2D.State
 8 | import Geom.Gen2D.Place
 9 | import Geom.Gen2D.Types
10 |
11 | %default total
12 |
13 | VECT_INI : MolVector
14 | VECT_INI = rotate (fromDegree 30) (V BOND_LEN 0)
15 |
16 | parameters {k : _}
17 |            {0 e, n  : Type}
18 |            {auto dg : DebugFlag}
19 |            {auto ce : Cast n Elem}
20 |            {auto ch : Cast n Hybridization}
21 |            (g : IGraph k e n)
22 |
23 |   pchain : PlaceST s k => AttachPoint k -> List (Fin k) -> F1' s
24 |   pchain _            []      = pure () -- will not happen
25 |   pchain None         (x::xs) = place x origin >> placeChain g x xs VECT_INI
26 |   pchain (Attach p _) (x::xs) = bondVector p x >>= placeChain g p (x::xs)
27 |
28 |   placeComp : PlaceST s k => Component k e n -> F1' s
29 |   placeComp (C a ns False _)  = pchain a ns >> for1_ ns (ignore1 . placeNeighbours g)
30 |   placeComp (C a ns True  sg) = placeRing g a ns sg
31 |
32 |   coords : IGraph k e (MolPoint, n)
33 |   coords =
34 |     run1 $ T1.do
35 |       st <- placeST k
36 |       for1_ (components g) placeComp
37 |       ps <- getPoints st
38 |       pure $ {graph $= mapWithIndex $ \x => map (ps `at` x,)} g
39 |
40 | 0 CGraph : Nat -> Type -> Type -> Type
41 | CGraph k e n = Graph e (MolPoint,Fin k, n)
42 |
43 | coordArray : {k : _} -> List (CGraph k e n) -> IArray k MolPoint
44 | coordArray gs =
45 |   alloc k origin $ \m => T1.do
46 |     for1_ (gs >>= \(G _ h) => labels h) $ \(p,x,_) => set m x p
47 |     unsafeFreeze m
48 |
49 | adjust : {k : _} -> IGraph k e n -> List (CGraph k e n) -> IGraph k e (MolPoint,n)
50 | adjust g gs =
51 |  let cs := coordArray gs
52 |   in mapWithCtxt (\x => (at cs x,) . label) g
53 |
54 | Cast n e => Cast (a,n) e where cast = cast . snd
55 |
56 | ModPoint (MolPoint,a) where
57 |   mtrans = Mol
58 |   modPoint f (p,v) = (modPoint f p, v)
59 |
60 | GetPoint (MolPoint,a) where
61 |   gtrans = Mol
62 |   point = fst
63 |
64 | export
65 | coordinates :
66 |      {k : _}
67 |   -> {0 e, n  : Type}
68 |   -> {auto dg : DebugFlag}
69 |   -> {auto ce : Cast n Elem}
70 |   -> {auto ch : Cast n Hybridization}
71 |   -> (g : IGraph k e n)
72 |   -> IGraph k e (MolPoint, n)
73 | coordinates g =
74 |      connectedComponents g            -- split into components
75 |   |> map (\(G _ y) => G _ $ coords y) -- place them individually
76 |   |> align                            -- align them in a grid
77 |   |> adjust g                         -- write coords to original graph
78 |