Skip to content

Commit 2f226b5

Browse files
committed
defFactorFunction macro
1 parent 82e6588 commit 2f226b5

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

src/entities/DFGFactor.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22
## Abstract Types
33
##==============================================================================
44

5-
# TODO consider changing this to AbstractFactor
65
abstract type AbstractFactor end
76
abstract type AbstractPackedFactor end
87

98
abstract type AbstractPrior <: AbstractFactor end
109
abstract type AbstractRelative <: AbstractFactor end
11-
# abstract type AbstractRelativeRoots <: AbstractRelative end # TODO deprecate
1210
abstract type AbstractRelativeMinimize <: AbstractRelative end
1311
abstract type AbstractManifoldMinimize <: AbstractRelative end
1412

src/services/DFGFactor.jl

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,51 @@ function _getPriorType(_type::Type{<:InferenceVariable})
5151
return getfield(_type.name.module, Symbol(:Prior, _type.name.name))
5252
end
5353

54+
55+
##==============================================================================
56+
## Default Factors Function Macro
57+
##==============================================================================
58+
function pack end
59+
function unpack end
60+
"""
61+
@defFactorFunction StructName factortype<:AbstractFactor manifolds<:ManifoldsBase.AbstractManifold
62+
63+
A macro to create a new factor function with name `StructName` and manifolds. Note that
64+
the `manifolds` is an object and *must* be a subtype of `ManifoldsBase.AbstractManifold`.
65+
See documentation in [Manifolds.jl on making your own](https://juliamanifolds.github.io/Manifolds.jl/stable/examples/manifold.html).
66+
67+
Example:
68+
```
69+
DFG.@defFactorFunction Pose2Pos2 AbstractManifoldMinimize SpecialEuclidean(2)
70+
```
71+
"""
72+
macro defFactorFunction(structname, factortype, manifold)
73+
packedstructname = Symbol("Packed", structname)
74+
return esc(
75+
quote
76+
Base.@__doc__ struct $structname{T} <: $factortype
77+
Z::T
78+
end
79+
80+
# Base.@__doc__ struct $packedstructname{T<:PackedSamplableBelief} <: AbstractPackedFactor
81+
Base.@__doc__ struct $packedstructname{T} <: AbstractPackedFactor
82+
Z::T
83+
end
84+
85+
# user manifold must be a <:Manifold
86+
@assert ($manifold isa AbstractManifold) "@defVariable of " *
87+
string($structname) *
88+
" requires that the " *
89+
string($manifold) *
90+
" be a subtype of `ManifoldsBase.AbstractManifold`"
91+
92+
DFG.getManifold(::Type{$structname}) = $manifold
93+
DFG.pack(d::$structname) = $packedstructname(packDistribution(d.Z))
94+
DFG.unpack(d::$packedstructname) = $structname(unpackDistribution(d.Z))
95+
end,
96+
)
97+
end
98+
5499
##==============================================================================
55100
## Factors
56101
##==============================================================================

0 commit comments

Comments
 (0)