Skip to content

Commit be4d03b

Browse files
authored
Merge pull request #775 from JuliaRobotics/maint/21Q2/fix_defVariable
Cleanup defVariable and add getPointType
2 parents 3200541 + 9fb530a commit be4d03b

File tree

5 files changed

+55
-89
lines changed

5 files changed

+55
-89
lines changed

src/DistributedFactorGraphs.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export getSolverData
135135
export getVariableType
136136

137137
# VariableType functions
138-
export getDimension, getManifolds, getManifold
138+
export getDimension, getManifolds, getManifold, getPointType
139139

140140
# Small Data CRUD
141141
export SmallDataTypes, getSmallData, addSmallData!, updateSmallData!, deleteSmallData!, listSmallData, emptySmallData!

src/services/DFGVariable.jl

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,6 @@ getVariableType(dfg::AbstractDFG, lbl::Symbol) = getVariableType(getVariable(dfg
6363
##------------------------------------------------------------------------------
6464
## InferenceVariable
6565
##------------------------------------------------------------------------------
66-
"""
67-
$SIGNATURES
68-
Interface function to return the `variableType` dimension of an InferenceVariable, extend this function for all Types<:InferenceVariable.
69-
"""
70-
function getDimension end
71-
"""
72-
$SIGNATURES
73-
Interface function to return the `<:ManifoldsBase.AbstractManifold` object of `variableType<:InferenceVariable`, extend this function for all `Types<:InferenceVariable`.
74-
"""
75-
getManifold(vari::DFGVariable) = getVariableType(vari) |> getManifold
7666

7767
# """
7868
# $SIGNATURES
@@ -83,7 +73,6 @@ getManifold(vari::DFGVariable) = getVariableType(vari) |> getManifold
8373
# getManifolds(::Type{<:T}) where {T <: ManifoldsBase.AbstractManifold} = convert(Tuple, T)
8474
# getManifolds(::T) where {T <: ManifoldsBase.AbstractManifold} = getManifolds(T)
8575

86-
getDimension(t_::T) where {T <: ManifoldsBase.AbstractManifold} = manifold_dimension(t_)
8776

8877
"""
8978
@defVariable StructName manifolds<:ManifoldsBase.AbstractManifold
@@ -97,27 +86,49 @@ Example:
9786
DFG.@defVariable Pose2 SpecialEuclidean(2)
9887
```
9988
"""
100-
macro defVariable(structname, manifold)
89+
macro defVariable(structname, manifold, point_type)
10190
return esc(quote
10291
Base.@__doc__ struct $structname <: InferenceVariable end
10392

93+
# user manifold must be a <:Manifold
10494
@assert ($manifold isa AbstractManifold) "@defVariable of "*string($structname)*" requires that the "*string($manifold)*" be a subtype of `ManifoldsBase.AbstractManifold`"
10595

106-
# user manifold must be a <:Manifold
107-
Base.convert(::Type{<:AbstractManifold}, ::Union{<:T, Type{<:T}}) where {T <: $structname} = $manifold
96+
DFG.getManifold(::Type{$structname}) = $manifold
10897

109-
getManifold(::Type{M}) where {M <: $structname} = $manifold
110-
getManifold(::M) where {M <: $structname} = getManifold(M)
111-
112-
DFG.getDimension(::Type{M}) where {M <: $structname} = manifold_dimension(getManifold(M))
113-
DFG.getDimension(::M) where {M <: $structname} = manifold_dimension(getManifold(M))
98+
DFG.getPointType(::Type{$structname}) = $point_type
11499

115-
# # # FIXME legacy API to be deprecated
116-
# DFG.getManifolds(::Type{M}) where {M <: $structname} = convert(Tuple, $manifold)
117-
# DFG.getManifolds(::M) where {M <: $structname} = convert(Tuple, $manifold)
118100
end)
119101
end
120102

103+
Base.convert(::Type{<:AbstractManifold}, ::Union{<:T, Type{<:T}}) where {T <: InferenceVariable} = getManifold(T)
104+
105+
"""
106+
$SIGNATURES
107+
Interface function to return the `<:ManifoldsBase.AbstractManifold` object of `variableType<:InferenceVariable`.
108+
"""
109+
getManifold(vari::DFGVariable) = getVariableType(vari) |> getManifold
110+
getManifold(::T) where {T <: InferenceVariable} = getManifold(T)
111+
112+
113+
"""
114+
$SIGNATURES
115+
Interface function to return the `variableType` dimension of an InferenceVariable, extend this function for all Types<:InferenceVariable.
116+
"""
117+
function getDimension end
118+
119+
getDimension(::Type{T}) where {T <: InferenceVariable} = manifold_dimension(getManifold(T))
120+
getDimension(::T) where {T <: InferenceVariable} = manifold_dimension(getManifold(T))
121+
getDimension(M::ManifoldsBase.AbstractManifold) = manifold_dimension(M)
122+
getDimension(var::DFGVariable) = getDimension(getVariableType(var))
123+
124+
125+
"""
126+
$SIGNATURES
127+
Interface function to return the manifold point type of an InferenceVariable, extend this function for all Types<:InferenceVariable.
128+
"""
129+
function getPointType end
130+
getPointType(::T) where {T <: InferenceVariable} = getPointType(T)
131+
121132
##------------------------------------------------------------------------------
122133
## solvedCount
123134
##------------------------------------------------------------------------------

test/runtests.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ using UUIDs
2121
# logger = SimpleLogger(stdout, Logging.Debug)
2222
# global_logger(logger)
2323

24-
# @testset "Check @defVariable design" begin
25-
# include("test_defVariable.jl")
26-
# end
24+
include("test_defVariable.jl")
2725

2826
include("testBlocks.jl")
2927

test/testBlocks.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import Base: convert
1616
Base.convert(::Type{<:Tuple}, ::typeof(Euclidean(1))) = (:Euclid,)
1717
Base.convert(::Type{<:Tuple}, ::typeof(Euclidean(2))) = (:Euclid, :Euclid)
1818

19-
@defVariable TestVariableType1 Euclidean(1)
20-
@defVariable TestVariableType2 Euclidean(2)
19+
@defVariable TestVariableType1 Euclidean(1) Vector{Float64}
20+
@defVariable TestVariableType2 Euclidean(2) Vector{Float64}
2121

2222
# struct TestVariableType2 <: InferenceVariable
2323
# dims::Int

test/test_defVariable.jl

Lines changed: 18 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,39 @@
1-
2-
using ManifoldsBase, Manifolds
1+
using Manifolds
32
using Test
43

5-
# abstract type InferenceVariable end
6-
7-
##
8-
9-
Base.convert(::Type{<:Tuple}, ::typeof(Euclidean(1))) = (:Euclid,)
10-
Base.convert(::Type{<:Tuple}, ::typeof(Euclidean(2))) = (:Euclid, :Euclid)
11-
12-
##
13-
14-
## WARNING, THIS IS A DUPLICATE OF THE MACRO IN DFGVariable.jl TO MAKE SURE OVERLOADS ARE WORKING RIGHT
15-
macro defVariable(structname, manifold)
16-
return esc(quote
17-
Base.@__doc__ struct $structname <: InferenceVariable end
18-
19-
@assert ($manifold isa AbstractManifold) "@defVariable of "*string($structname)*" requires that the "*string($manifold)*" be a subtype of `ManifoldsBase.AbstractManifold`"
20-
21-
# manifold must be is a <:Manifold
22-
Base.convert(::Type{<:AbstractManifold}, ::Union{<:T, Type{<:T}}) where {T <: $structname} = $manifold
23-
24-
getManifold(::Type{M}) where {M <: $structname} = $manifold
25-
getManifold(::M) where {M <: $structname} = getManifold(M)
26-
27-
getDimension(::Type{M}) where {M <: $structname} = manifold_dimension(getManifold(M))
28-
getDimension(::M) where {M <: $structname} = manifold_dimension(getManifold(M))
29-
# FIXME legacy API to be deprecated
30-
# getManifolds(::Type{M}) where {M <: $structname} = convert(Tuple, $manifold)
31-
# getManifolds(::M) where {M <: $structname} = convert(Tuple, $manifold)
32-
end)
33-
end
34-
35-
36-
##
37-
38-
39-
ex = macroexpand(Main, :(@defVariable(TestVariableType1, Euclidean(1))) )
40-
41-
4+
@testset "Testing @defVariable" begin
425
##
436

447
struct NotAManifold end
458

46-
try
47-
@defVariable(MyVar, NotAManifold())
48-
catch AssertionError
49-
@test true
50-
end
9+
@test_throws AssertionError @defVariable(MyVar, NotAManifold(), Matrix{3})
5110

5211
##
5312

54-
@defVariable(TestVariableType1, Euclidean(1))
55-
@defVariable(TestVariableType2, Euclidean(2))
13+
@defVariable(TestVarType1, Euclidean(3), Vector{Float64})
14+
@defVariable(TestVarType2, SpecialEuclidean(3), ProductRepr{Tuple{Vector{Float64}, Matrix{Float64}}})
5615

5716

5817
##
5918

60-
@test getManifold( TestVariableType1) == Euclidean(1)
61-
@test getManifold( TestVariableType2) == Euclidean(2)
19+
@test getManifold( TestVarType1) == Euclidean(3)
20+
@test getManifold( TestVarType2) == SpecialEuclidean(3)
6221

63-
# legacy
64-
# @test getManifolds(TestVariableType1) == (:Euclid,)
65-
@test getDimension(TestVariableType1) === 1
66-
# @test getManifolds(TestVariableType2) == (:Euclid,:Euclid)
67-
@test getDimension(TestVariableType2) === 2
22+
@test getDimension(TestVarType1) === 3
23+
@test getDimension(TestVarType2) === 6
6824

25+
@test getPointType(TestVarType1) == Vector{Float64}
26+
@test getPointType(TestVarType2) == ProductRepr{Tuple{Vector{Float64}, Matrix{Float64}}}
6927
##
7028

7129

72-
@test getManifold( TestVariableType1()) == Euclidean(1)
73-
@test getManifold( TestVariableType2()) == Euclidean(2)
30+
@test getManifold( TestVarType1()) == Euclidean(3)
31+
@test getManifold( TestVarType2()) == SpecialEuclidean(3)
7432

75-
# legacy
76-
# @test getManifolds(TestVariableType1()) == (:Euclid,)
77-
@test getDimension(TestVariableType1()) === 1
78-
# @test getManifolds(TestVariableType2()) == (:Euclid,:Euclid)
79-
@test getDimension(TestVariableType2()) === 2
33+
@test getDimension(TestVarType1()) === 3
34+
@test getDimension(TestVarType2()) === 6
8035

36+
@test getPointType(TestVarType1()) == Vector{Float64}
37+
@test getPointType(TestVarType2()) == ProductRepr{Tuple{Vector{Float64}, Matrix{Float64}}}
8138

82-
##
39+
end

0 commit comments

Comments
 (0)