-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDFGFactor.jl
More file actions
241 lines (194 loc) · 8.17 KB
/
DFGFactor.jl
File metadata and controls
241 lines (194 loc) · 8.17 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
##==============================================================================
## Accessors
##==============================================================================
##==============================================================================
## GenericFunctionNodeData
##==============================================================================
## COMMON
# getSolveInProgress
# isSolveInProgress
#TODO getFactorFunction = getFactorType
"""
$SIGNATURES
Return reference to the user factor in `<:AbstractDFG` identified by `::Symbol`.
"""
getFactorFunction(fcd::GenericFunctionNodeData) = fcd.fnc.usrfnc!
getFactorFunction(fc::FactorCompute) = getFactorFunction(getSolverData(fc))
getFactorFunction(dfg::AbstractDFG, fsym::Symbol) = getFactorFunction(getFactor(dfg, fsym))
"""
$SIGNATURES
Return user factor type from factor graph identified by label `::Symbol`.
Notes
- Replaces older `getfnctype`.
"""
getFactorType(data::GenericFunctionNodeData) = data.fnc.usrfnc!
getFactorType(fct::FactorCompute) = getFactorType(getSolverData(fct))
getFactorType(f::FactorDFG) = getTypeFromSerializationModule(f.fnctype)() # TODO find a better way to do this that does not rely on empty constructor
getFactorType(dfg::AbstractDFG, lbl::Symbol) = getFactorType(getFactor(dfg, lbl))
"""
$SIGNATURES
If you know a variable is `::Type{<:Pose2}` but want to find its default prior `::Type{<:PriorPose2}`.
Assumptions
- The prior type will be defined in the same module as the variable type.
- Not exported per default, but can be used with knowledge of the caveats.
Example
```julia
using RoME
@assert RoME.PriorPose2 == DFG._getPriorType(Pose2)
```
"""
function _getPriorType(_type::Type{<:InferenceVariable})
return getfield(_type.name.module, Symbol(:Prior, _type.name.name))
end
##==============================================================================
## Default Factors Function Macro
##==============================================================================
export PackedSamplableBelief
# export pack, unpack, packDistribution, unpackDistribution
function pack end
function unpack end
function packDistribution end
function unpackDistribution end
abstract type PackedSamplableBelief end
StructTypes.StructType(::Type{<:PackedSamplableBelief}) = StructTypes.UnorderedStruct()
"""
@defFactorType StructName factortype<:AbstractFactor manifolds<:ManifoldsBase.AbstractManifold
A macro to create a new factor function with name `StructName` and manifolds. Note that
the `manifolds` is an object and *must* be a subtype of `ManifoldsBase.AbstractManifold`.
See documentation in [Manifolds.jl on making your own](https://juliamanifolds.github.io/Manifolds.jl/stable/examples/manifold.html).
Example:
```
DFG.@defFactorType Pose2Pos2 AbstractManifoldMinimize SpecialEuclidean(2)
```
"""
macro defFactorType(structname, factortype, manifold)
packedstructname = Symbol("Packed", structname)
return esc(
quote
Base.@__doc__ struct $structname{T} <: $factortype
Z::T
end
Base.@__doc__ struct $packedstructname{T <: PackedSamplableBelief} <:
AbstractPackedFactor
Z::T
end
# user manifold must be a <:Manifold
@assert ($manifold isa AbstractManifold) "@defFactorType of " *
string($structname) *
" requires that the " *
string($manifold) *
" be a subtype of `ManifoldsBase.AbstractManifold`"
DFG.getManifold(::Type{$structname}) = $manifold
DFG.pack(d::$structname) = $packedstructname(DFG.packDistribution(d.Z))
DFG.unpack(d::$packedstructname) = $structname(DFG.unpackDistribution(d.Z))
end,
)
end
##==============================================================================
## Factors
##==============================================================================
# | | label | tags | timestamp | solvable | solverData |
# |-------------------|:-----:|:----:|:---------:|:--------:|:----------:|
# | FactorSkeleton | X | x | | | |
# | FactorSummary | X | X | X | | |
# | FactorCompute | X | X | X | X | X |
##------------------------------------------------------------------------------
## label
##------------------------------------------------------------------------------
## COMMON
# getLabel
##------------------------------------------------------------------------------
## tags
##------------------------------------------------------------------------------
## COMMON
# getTags
# setTags!
##------------------------------------------------------------------------------
## timestamp
##------------------------------------------------------------------------------
## COMMON
# getTimestamp
function setTimestamp(f::AbstractDFGFactor, ts::DateTime, timezone = localzone())
return setTimestamp(f, ZonedDateTime(ts, timezone))
end
function setTimestamp(f::FactorCompute, ts::ZonedDateTime)
return FactorCompute(
f.label,
ts,
f.nstime,
f.tags,
f.solverData,
f.solvable,
getfield(f, :_variableOrderSymbols);
id = f.id,
)
end
function setTimestamp(f::FactorSummary, ts::ZonedDateTime)
return FactorSummary(f.id, f.label, f.tags, f._variableOrderSymbols, ts)
end
function setTimestamp(f::FactorSummary, ts::DateTime)
return FactorSummary(f, ZonedDateTime(ts, localzone()))
end
function setTimestamp(v::FactorDFG, timestamp::ZonedDateTime)
return FactorDFG(;
(key => getproperty(v, key) for key in fieldnames(FactorDFG))...,
timestamp,
)
end
##------------------------------------------------------------------------------
## solvable
##------------------------------------------------------------------------------
## COMMON
# getSolvable
# setSolvable!
# isSolvable
##------------------------------------------------------------------------------
## solvable
##------------------------------------------------------------------------------
## COMMON
##------------------------------------------------------------------------------
## _variableOrderSymbols
##------------------------------------------------------------------------------
#TODO perhaps making _variableOrderSymbols imutable (NTuple) will be a save option
"""
$SIGNATURES
Get the variable ordering for this factor.
Should be equivalent to listNeighbors unless something was deleted in the graph.
"""
getVariableOrder(fct::FactorCompute) = fct._variableOrderSymbols::Vector{Symbol}
getVariableOrder(fct::FactorDFG) = fct._variableOrderSymbols::Vector{Symbol}
getVariableOrder(dfg::AbstractDFG, fct::Symbol) = getVariableOrder(getFactor(dfg, fct))
##------------------------------------------------------------------------------
## solverData
##------------------------------------------------------------------------------
"""
$SIGNATURES
Retrieve solver data structure stored in a factor.
"""
function getSolverData(f::FactorCompute)
return f.solverData
end
setSolverData!(f::FactorCompute, data::GenericFunctionNodeData) = f.solverData = data
##------------------------------------------------------------------------------
## utility
##------------------------------------------------------------------------------
"""
$SIGNATURES
Return `::Bool` on whether given factor `fc::Symbol` is a prior in factor graph `dfg`.
"""
function isPrior(dfg::AbstractDFG, fc::Symbol)
fco = getFactor(dfg, fc)
return isPrior(getFactorType(fco))
end
function isPrior(::AbstractPrior)
return true
end
function isPrior(::AbstractRelative)
return false
end
##==============================================================================
## Layer 2 CRUD (none) and Sets
##==============================================================================
##==============================================================================
## TAGS - See CommonAccessors
##==============================================================================