Skip to content

defFactorType macro #1077

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/entities/DFGFactor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
## Abstract Types
##==============================================================================

# TODO consider changing this to AbstractFactor
abstract type AbstractFactor end
abstract type AbstractPackedFactor end

Expand Down
53 changes: 53 additions & 0 deletions src/services/DFGFactor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,59 @@ 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
##==============================================================================
Expand Down
Loading