Skip to content

Commit 59bdfec

Browse files
committed
test defVariable
1 parent cdc9f1b commit 59bdfec

File tree

4 files changed

+107
-7
lines changed

4 files changed

+107
-7
lines changed

src/services/DFGVariable.jl

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,39 @@ getVariableType(dfg::AbstractDFG, lbl::Symbol) = getVariableType(getVariable(dfg
6363
Interface function to return the `variableType` dimension of an InferenceVariable, extend this function for all Types<:InferenceVariable.
6464
"""
6565
function getDimension end
66+
"""
67+
$SIGNATURES
68+
Interface function to return the `<:ManifoldsBase.Manifold` object of `variableType<:InferenceVariable`, extend this function for all `Types<:InferenceVariable`.
69+
"""
70+
function getManifold end
71+
6672
"""
6773
$SIGNATURES
6874
Interface function to return the `variableType` manifolds of an InferenceVariable, extend this function for all Types<:InferenceVariable.
6975
"""
7076
function getManifolds end
7177

7278
"""
73-
@defVariable StructName dimension manifolds
74-
A macro to create a new variable with name `StructName`, dimension and manifolds.
79+
@defVariable StructName manifolds<:ManifoldsBase.Manifold
80+
81+
A macro to create a new variable with name `StructName` and manifolds. Note that
82+
the `manifolds` is an object and *must* be a subtype of `ManifoldsBase.Manifold`.
83+
See documentation in [Manifolds.jl on making your own](https://juliamanifolds.github.io/Manifolds.jl/stable/examples/manifold.html).
84+
7585
Example:
7686
```
77-
DFG.@defVariable Pose2 3 (:Euclid, :Euclid, :Circular)
87+
DFG.@defVariable Pose2 SpecialEuclidean(2)
7888
```
7989
"""
80-
macro defVariable(structname, manifold) #::Vararg{Symbol})#NTuple{dimension, Symbol})
81-
# :(struct $structname <: InferenceVariable end)
90+
macro defVariable(structname, manifold)
8291
return esc(quote
8392
Base.@__doc__ struct $structname <: InferenceVariable end
93+
94+
@assert ($manifold isa Manifold) "@defVariable of "*string($structname)*" requires that the "*string($manifold)*" be a subtype of `ManifoldsBase.Manifold`"
95+
96+
# user manifold must be a <:Manifold
97+
Base.convert(::Type{<:Manifold}, ::Union{<:T, Type{<:T}}) where {T <: $structname} = $manifold
98+
8499
getManifold(::Type{M}) where {M <: $structname} = $manifold
85100
getManifold(::M) where {M <: $structname} = getManifold(M)
86101

test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ 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
27+
2428
include("testBlocks.jl")
2529

2630
@testset "Test generated ==" begin

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-
DFG.@defVariable TestVariableType1 Euclidean(1) # 1 (:Euclid,)
20-
DFG.@defVariable TestVariableType2 Euclidean(2) # 1 (:Euclid,)
19+
@defVariable TestVariableType1 Euclidean(1)
20+
@defVariable TestVariableType2 Euclidean(2)
2121

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

test/test_defVariable.jl

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
2+
using ManifoldsBase, Manifolds
3+
using Test
4+
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+
macro defVariable(structname, manifold)
15+
return esc(quote
16+
Base.@__doc__ struct $structname <: InferenceVariable end
17+
18+
@assert ($manifold isa Manifold) "@defVariable of "*string($structname)*" requires that the "*string($manifold)*" be a subtype of `ManifoldsBase.Manifold`"
19+
20+
# manifold must be is a <:Manifold
21+
Base.convert(::Type{<:Manifold}, ::Union{<:T, Type{<:T}}) where {T <: $structname} = $manifold
22+
23+
getManifold(::Type{M}) where {M <: $structname} = $manifold
24+
getManifold(::M) where {M <: $structname} = getManifold(M)
25+
26+
getDimension(::Type{M}) where {M <: $structname} = manifold_dimension(getManifold(M))
27+
getDimension(::M) where {M <: $structname} = manifold_dimension(getManifold(M))
28+
# FIXME legacy API to be deprecated
29+
getManifolds(::Type{M}) where {M <: $structname} = convert(Tuple, $manifold)
30+
getManifolds(::M) where {M <: $structname} = convert(Tuple, $manifold)
31+
end)
32+
end
33+
34+
35+
##
36+
37+
38+
ex = macroexpand(Main, :(@defVariable(TestVariableType1, Euclidean(1))) )
39+
40+
41+
##
42+
43+
struct NotAManifold end
44+
45+
try
46+
@defVariable(MyVar, NotAManifold())
47+
catch AssertionError
48+
@test true
49+
end
50+
51+
##
52+
53+
@defVariable(TestVariableType1, Euclidean(1))
54+
@defVariable(TestVariableType2, Euclidean(2))
55+
56+
57+
##
58+
59+
@test getManifold( TestVariableType1) == Euclidean(1)
60+
@test getManifold( TestVariableType2) == Euclidean(2)
61+
62+
# legacy
63+
@test getManifolds(TestVariableType1) == (:Euclid,)
64+
@test getDimension(TestVariableType1) === 1
65+
@test getManifolds(TestVariableType2) == (:Euclid,:Euclid)
66+
@test getDimension(TestVariableType2) === 2
67+
68+
##
69+
70+
71+
@test getManifold( TestVariableType1()) == Euclidean(1)
72+
@test getManifold( TestVariableType2()) == Euclidean(2)
73+
74+
# legacy
75+
@test getManifolds(TestVariableType1()) == (:Euclid,)
76+
@test getDimension(TestVariableType1()) === 1
77+
@test getManifolds(TestVariableType2()) == (:Euclid,:Euclid)
78+
@test getDimension(TestVariableType2()) === 2
79+
80+
81+
##

0 commit comments

Comments
 (0)