Skip to content

Commit 3d6c96d

Browse files
defFactorType macro (#1077)
* defFactorFunction macro * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * update defFactorType * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update DFGFactor.jl --------- Co-authored-by: Johannes Terblanche <Affie@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 14edc09 commit 3d6c96d

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

src/entities/DFGFactor.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
## Abstract Types
33
##==============================================================================
44

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

src/services/DFGFactor.jl

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,59 @@ function _getPriorType(_type::Type{<:InferenceVariable})
6464
return getfield(_type.name.module, Symbol(:Prior, _type.name.name))
6565
end
6666

67+
##==============================================================================
68+
## Default Factors Function Macro
69+
##==============================================================================
70+
export PackedSamplableBelief
71+
# export pack, unpack, packDistribution, unpackDistribution
72+
73+
function pack end
74+
function unpack end
75+
function packDistribution end
76+
function unpackDistribution end
77+
78+
abstract type PackedSamplableBelief end
79+
StructTypes.StructType(::Type{<:PackedSamplableBelief}) = StructTypes.UnorderedStruct()
80+
81+
"""
82+
@defFactorType StructName factortype<:AbstractFactor manifolds<:ManifoldsBase.AbstractManifold
83+
84+
A macro to create a new factor function with name `StructName` and manifolds. Note that
85+
the `manifolds` is an object and *must* be a subtype of `ManifoldsBase.AbstractManifold`.
86+
See documentation in [Manifolds.jl on making your own](https://juliamanifolds.github.io/Manifolds.jl/stable/examples/manifold.html).
87+
88+
Example:
89+
```
90+
DFG.@defFactorType Pose2Pos2 AbstractManifoldMinimize SpecialEuclidean(2)
91+
```
92+
"""
93+
macro defFactorType(structname, factortype, manifold)
94+
packedstructname = Symbol("Packed", structname)
95+
return esc(
96+
quote
97+
Base.@__doc__ struct $structname{T} <: $factortype
98+
Z::T
99+
end
100+
101+
Base.@__doc__ struct $packedstructname{T <: PackedSamplableBelief} <:
102+
AbstractPackedFactor
103+
Z::T
104+
end
105+
106+
# user manifold must be a <:Manifold
107+
@assert ($manifold isa AbstractManifold) "@defFactorType of " *
108+
string($structname) *
109+
" requires that the " *
110+
string($manifold) *
111+
" be a subtype of `ManifoldsBase.AbstractManifold`"
112+
113+
DFG.getManifold(::Type{$structname}) = $manifold
114+
DFG.pack(d::$structname) = $packedstructname(DFG.packDistribution(d.Z))
115+
DFG.unpack(d::$packedstructname) = $structname(DFG.unpackDistribution(d.Z))
116+
end,
117+
)
118+
end
119+
67120
##==============================================================================
68121
## Factors
69122
##==============================================================================

0 commit comments

Comments
 (0)