aboutsummaryrefslogtreecommitdiff
path: root/julia/CrystFEL/src/detgeom.jl
blob: d8dc5972b555feb8997cf551a91b2f874abeee06 (plain)
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
156
157
158
159
160
161
162
163
164
165
module DetGeoms
export DetGeom, DetGeomPanel, DetGeomGroup, InternalDetGeom, copydetgeom
export findgroup

mutable struct InternalPanel
    name::Cstring
    cx::Cdouble
    cy::Cdouble
    cz::Cdouble
    pixel_pitch::Cdouble
    adu_per_photon::Cdouble
    max_adu::Cdouble
    fsx::Cdouble
    fsy::Cdouble
    fsz::Cdouble
    ssx::Cdouble
    ssy::Cdouble
    ssz::Cdouble
    w::Cint
    h::Cint
    group::Ptr{Cvoid}
end


mutable struct InternalDetGeomPanelGroup
    name::Cstring
    n_children::Cint
    parent::Ptr{InternalDetGeomPanelGroup}
    serial::Cint
    cx::Cdouble
    cy::Cdouble
    cz::Cdouble
    children::Ptr{Ptr{InternalDetGeomPanelGroup}}
    panel::Ptr{InternalPanel}
end


mutable struct InternalDetGeom
    panels::Ptr{InternalPanel}
    n_panels::Cint
    top_group::Ptr{InternalDetGeomPanelGroup}
end


mutable struct DetGeomPanel
    name
    corner
    fsvec
    ssvec
    pixel_pitch
    adu_per_photon
    max_adu
    w
    h
end

struct DetGeomGroup
    serial
    name
    children
    center    # For rotation
end


mutable struct DetGeom
    panels::Vector{DetGeomPanel}
    topgroup::DetGeomGroup
end


function copy_dg_panel(p::InternalPanel)
    DetGeomPanel(unsafe_string(p.name),
                 [p.cx, p.cy, p.cz],
                 [p.fsx, p.fsy, p.fsz],
                 [p.ssx, p.ssy, p.ssz],
                 p.pixel_pitch, p.adu_per_photon, p.max_adu,
                 p.w, p.h)
end


function copydggroup(panels, grp)
    grpdata = unsafe_load(grp, 1)
    name = unsafe_string(grpdata.name)
    children = DetGeomGroup[]
    if grpdata.n_children > 1
        for i in 1:grpdata.n_children
            cdata = unsafe_load(grpdata.children, i)
            push!(children, copydggroup(panels, cdata))
        end
        DetGeomGroup(grpdata.serial, name, children,
                     [grpdata.cx, grpdata.cy, grpdata.cz])
    else
        name = unsafe_string(grpdata.name)
        pn = findfirst(x->x.name==name, panels)
        DetGeomGroup(grpdata.serial, name, [panels[pn]],
                     [grpdata.cx, grpdata.cy, grpdata.cz])
    end
end


function copydetgeom(dg::Ptr{InternalDetGeom})
    dgdata = unsafe_load(dg, 1)
    panels = DetGeomPanel[]
    for i in 1:dgdata.n_panels
        paneldata = unsafe_load(dgdata.panels, i)
        push!(panels, copy_dg_panel(paneldata))
    end
    topgroup = copydggroup(panels, dgdata.top_group)
    DetGeom(panels, topgroup)
end


function seq100(ser, head)
    if ser == 0
        ()
    elseif ser < 100
        (head...,ser % 100)
    else
        seq100(ser ÷ 100, (head..., ser % 100))
    end
end

findgroup(dg::DetGeom, ser) = findgroup(dg.topgroup, ser, seq100(ser÷100, ()))

function findgroup(group::DetGeomGroup, ser, path)
    if length(path) == 0
        @assert(group.serial == ser)
        return group
    end
    findgroup(group.children[path[1]], ser, path[2:end])
end


function Base.show(io::IO, p::DetGeomPanel)
    write(io, "Panel(")
    write(io, "name=\"")
    write(io, p.name)
    write(io, "\", corner=")
    show(io, p.corner)
    write(io, "), fs=(")
    show(io, p.fsvec)
    write(io, "), ss=(")
    show(io, p.ssvec)
    write(io, "), size=(")
    show(io, p.w); write(io, ", "); show(io, p.h)
    write(io, "))")
end

function showgroup(io, p::DetGeomPanel, prefix)
    println(io, prefix, "Panel ", p.name, " (",p.w,"×",p.h,")")
end

function showgroup(io, grp::DetGeomGroup, prefix)
    println(io, prefix, "Group ", grp.name, " (serial number ", grp.serial, ")")
    for child in grp.children
        showgroup(io, child, prefix*" ")
    end
end

function Base.show(io::IO, ::MIME"text/plain", dg::DetGeom)
    println(io, "Detector geometry structure with ", length(dg.panels), " panels")
    showgroup(io, dg.topgroup, " ")
end

end  # of module