From 422b286a594d5708d7d0d17ff565e719e0b371c6 Mon Sep 17 00:00:00 2001 From: Johannes Terblanche Date: Tue, 18 Jun 2024 14:53:14 +0200 Subject: [PATCH 1/5] defFactorFunction macro --- src/entities/DFGFactor.jl | 1 - src/services/DFGFactor.jl | 45 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/entities/DFGFactor.jl b/src/entities/DFGFactor.jl index 8b4b66f9..37d7d44c 100644 --- a/src/entities/DFGFactor.jl +++ b/src/entities/DFGFactor.jl @@ -2,7 +2,6 @@ ## Abstract Types ##============================================================================== -# TODO consider changing this to AbstractFactor abstract type AbstractFactor end abstract type AbstractPackedFactor end diff --git a/src/services/DFGFactor.jl b/src/services/DFGFactor.jl index 9629720e..340f984f 100644 --- a/src/services/DFGFactor.jl +++ b/src/services/DFGFactor.jl @@ -52,6 +52,51 @@ function _getPriorType(_type::Type{<:InferenceVariable}) return getfield(_type.name.module, Symbol(:Prior, _type.name.name)) end + +##============================================================================== +## Default Factors Function Macro +##============================================================================== +function pack end +function unpack end +""" + @defFactorFunction 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.@defFactorFunction Pose2Pos2 AbstractManifoldMinimize SpecialEuclidean(2) +``` +""" +macro defFactorFunction(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 + Base.@__doc__ struct $packedstructname{T} <: AbstractPackedFactor + Z::T + end + + # user manifold must be a <:Manifold + @assert ($manifold isa AbstractManifold) "@defVariable of " * + string($structname) * + " requires that the " * + string($manifold) * + " be a subtype of `ManifoldsBase.AbstractManifold`" + + DFG.getManifold(::Type{$structname}) = $manifold + DFG.pack(d::$structname) = $packedstructname(packDistribution(d.Z)) + DFG.unpack(d::$packedstructname) = $structname(unpackDistribution(d.Z)) + end, + ) +end + ##============================================================================== ## Factors ##============================================================================== From f6fe309117ff7be2c1d5c2433461320e56240070 Mon Sep 17 00:00:00 2001 From: Johannes Terblanche <6612981+Affie@users.noreply.github.com> Date: Tue, 18 Jun 2024 15:08:46 +0200 Subject: [PATCH 2/5] Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/services/DFGFactor.jl | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/services/DFGFactor.jl b/src/services/DFGFactor.jl index 340f984f..bed3e845 100644 --- a/src/services/DFGFactor.jl +++ b/src/services/DFGFactor.jl @@ -52,7 +52,6 @@ function _getPriorType(_type::Type{<:InferenceVariable}) return getfield(_type.name.module, Symbol(:Prior, _type.name.name)) end - ##============================================================================== ## Default Factors Function Macro ##============================================================================== @@ -74,12 +73,12 @@ macro defFactorFunction(structname, factortype, manifold) packedstructname = Symbol("Packed", structname) return esc( quote - Base.@__doc__ struct $structname{T} <: $factortype + Base.@__doc__ struct $structname{T} <: $factortype Z::T end # Base.@__doc__ struct $packedstructname{T<:PackedSamplableBelief} <: AbstractPackedFactor - Base.@__doc__ struct $packedstructname{T} <: AbstractPackedFactor + Base.@__doc__ struct $packedstructname{T} <: AbstractPackedFactor Z::T end @@ -92,7 +91,7 @@ macro defFactorFunction(structname, factortype, manifold) DFG.getManifold(::Type{$structname}) = $manifold DFG.pack(d::$structname) = $packedstructname(packDistribution(d.Z)) - DFG.unpack(d::$packedstructname) = $structname(unpackDistribution(d.Z)) + DFG.unpack(d::$packedstructname) = $structname(unpackDistribution(d.Z)) end, ) end From 92524ae78ac02ace5eaa935923319ce2e8c0798b Mon Sep 17 00:00:00 2001 From: Johannes Terblanche Date: Thu, 11 Jul 2024 16:23:19 +0200 Subject: [PATCH 3/5] update defFactorType --- src/services/DFGFactor.jl | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/services/DFGFactor.jl b/src/services/DFGFactor.jl index bed3e845..882aaf7e 100644 --- a/src/services/DFGFactor.jl +++ b/src/services/DFGFactor.jl @@ -55,10 +55,19 @@ 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() + """ - @defFactorFunction StructName factortype<:AbstractFactor manifolds<:ManifoldsBase.AbstractManifold + @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`. @@ -66,10 +75,10 @@ See documentation in [Manifolds.jl on making your own](https://juliamanifolds.gi Example: ``` -DFG.@defFactorFunction Pose2Pos2 AbstractManifoldMinimize SpecialEuclidean(2) +DFG.@defFactorType Pose2Pos2 AbstractManifoldMinimize SpecialEuclidean(2) ``` """ -macro defFactorFunction(structname, factortype, manifold) +macro defFactorType(structname, factortype, manifold) packedstructname = Symbol("Packed", structname) return esc( quote @@ -77,8 +86,7 @@ macro defFactorFunction(structname, factortype, manifold) Z::T end - # Base.@__doc__ struct $packedstructname{T<:PackedSamplableBelief} <: AbstractPackedFactor - Base.@__doc__ struct $packedstructname{T} <: AbstractPackedFactor + Base.@__doc__ struct $packedstructname{T<:PackedSamplableBelief} <: AbstractPackedFactor Z::T end @@ -90,8 +98,8 @@ macro defFactorFunction(structname, factortype, manifold) " be a subtype of `ManifoldsBase.AbstractManifold`" DFG.getManifold(::Type{$structname}) = $manifold - DFG.pack(d::$structname) = $packedstructname(packDistribution(d.Z)) - DFG.unpack(d::$packedstructname) = $structname(unpackDistribution(d.Z)) + DFG.pack(d::$structname) = $packedstructname(DFG.packDistribution(d.Z)) + DFG.unpack(d::$packedstructname) = $structname(DFG.unpackDistribution(d.Z)) end, ) end From fc8923b1c57838f03502d675af7c9af585bd2e8e Mon Sep 17 00:00:00 2001 From: Johannes Terblanche <6612981+Affie@users.noreply.github.com> Date: Fri, 12 Jul 2024 17:59:49 +0200 Subject: [PATCH 4/5] Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/services/DFGFactor.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/services/DFGFactor.jl b/src/services/DFGFactor.jl index 882aaf7e..8d83d431 100644 --- a/src/services/DFGFactor.jl +++ b/src/services/DFGFactor.jl @@ -86,7 +86,8 @@ macro defFactorType(structname, factortype, manifold) Z::T end - Base.@__doc__ struct $packedstructname{T<:PackedSamplableBelief} <: AbstractPackedFactor + Base.@__doc__ struct $packedstructname{T <: PackedSamplableBelief} <: + AbstractPackedFactor Z::T end From 72f7c39b0decaa3e100ccc71966c3e71d4d56b7a Mon Sep 17 00:00:00 2001 From: Johannes Terblanche <6612981+Affie@users.noreply.github.com> Date: Wed, 2 Apr 2025 10:57:56 +0200 Subject: [PATCH 5/5] Update DFGFactor.jl --- src/services/DFGFactor.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/DFGFactor.jl b/src/services/DFGFactor.jl index 8d83d431..7d2574b0 100644 --- a/src/services/DFGFactor.jl +++ b/src/services/DFGFactor.jl @@ -92,7 +92,7 @@ macro defFactorType(structname, factortype, manifold) end # user manifold must be a <:Manifold - @assert ($manifold isa AbstractManifold) "@defVariable of " * + @assert ($manifold isa AbstractManifold) "@defFactorType of " * string($structname) * " requires that the " * string($manifold) *