Skip to content

Commit 5005b82

Browse files
authored
Merge pull request #743 from JuliaRobotics/21Q1/refac/defvarmani
switch defVariable to Manifolds.jl
2 parents 3c88a4d + f53d9e6 commit 5005b82

File tree

6 files changed

+143
-16
lines changed

6 files changed

+143
-16
lines changed

Project.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "DistributedFactorGraphs"
22
uuid = "b5cc3c7e-6572-11e9-2517-99fb8daf2f04"
3-
version = "0.12.1"
3+
version = "0.13.0"
44

55
[deps]
66
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
@@ -12,6 +12,7 @@ JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
1212
JSON2 = "2535ab7d-5cd8-5a07-80ac-9b1792aadce3"
1313
LightGraphs = "093fc24a-ae57-5d10-9952-331d41423f4d"
1414
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
15+
ManifoldsBase = "3362f125-f0bb-47a3-aa74-596ffd7ef2fb"
1516
Neo4j = "d2adbeaf-5838-5367-8a2f-e46d570981db"
1617
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
1718
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
@@ -29,6 +30,7 @@ DocStringExtensions = "0.8, 0.9, 0.10, 1"
2930
JSON = "0.21"
3031
JSON2 = "0.3.1"
3132
LightGraphs = "1.2, 1.3"
33+
ManifoldsBase = "0.10"
3234
Neo4j = "2"
3335
Pkg = "1.4, 1.5"
3436
Reexport = "0.2, 0.3, 0.4, 0.5, 1"
@@ -39,8 +41,9 @@ julia = "1.4"
3941

4042
[extras]
4143
GraphPlot = "a2cc645c-3eea-5389-862e-a155d0052231"
44+
Manifolds = "1cead3c2-87b3-11e9-0ccd-23c62b72b94e"
4245
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
4346
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
4447

4548
[targets]
46-
test = ["Test", "GraphPlot", "Pkg"]
49+
test = ["Test", "GraphPlot", "Manifolds", "Pkg"]

src/DistributedFactorGraphs.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ using SparseArrays
2929
using UUIDs
3030
using Pkg
3131

32+
# used for @defVariable
33+
import ManifoldsBase
34+
import ManifoldsBase: Manifold, manifold_dimension
35+
export Manifold, manifold_dimension
36+
3237
import Base: getindex
3338

3439

@@ -130,7 +135,7 @@ export getSolverData
130135
export getVariableType
131136

132137
# VariableType functions
133-
export getDimension, getManifolds
138+
export getDimension, getManifolds, getManifold
134139

135140
# Small Data CRUD
136141
export SmallDataTypes, getSmallData, addSmallData!, updateSmallData!, deleteSmallData!, listSmallData, emptySmallData!

src/services/DFGVariable.jl

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,28 +63,56 @@ 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

78+
getManifolds(::Type{<:T}) where {T <: ManifoldsBase.Manifold} = convert(Tuple, T)
79+
getManifolds(::T) where {T <: ManifoldsBase.Manifold} = getManifolds(T)
80+
81+
getDimension(t_::T) where {T <: ManifoldsBase.Manifold} = manifold_dimension(t_)
82+
7283
"""
73-
@defVariable StructName dimension manifolds
74-
A macro to create a new variable with name `StructName`, dimension and manifolds.
84+
@defVariable StructName manifolds<:ManifoldsBase.Manifold
85+
86+
A macro to create a new variable with name `StructName` and manifolds. Note that
87+
the `manifolds` is an object and *must* be a subtype of `ManifoldsBase.Manifold`.
88+
See documentation in [Manifolds.jl on making your own](https://juliamanifolds.github.io/Manifolds.jl/stable/examples/manifold.html).
89+
7590
Example:
7691
```
77-
DFG.@defVariable Pose2 3 (:Euclid, :Euclid, :Circular)
92+
DFG.@defVariable Pose2 SpecialEuclidean(2)
7893
```
7994
"""
80-
macro defVariable(structname, dimension::Int, manifolds)#::Vararg{Symbol})#NTuple{dimension, Symbol})
81-
# :(struct $structname <: InferenceVariable end)
95+
macro defVariable(structname, manifold)
8296
return esc(quote
8397
Base.@__doc__ struct $structname <: InferenceVariable end
84-
DistributedFactorGraphs.getDimension(::$structname) = $dimension
85-
DistributedFactorGraphs.getManifolds(::$structname) = $manifolds
98+
99+
@assert ($manifold isa Manifold) "@defVariable of "*string($structname)*" requires that the "*string($manifold)*" be a subtype of `ManifoldsBase.Manifold`"
100+
101+
# user manifold must be a <:Manifold
102+
Base.convert(::Type{<:Manifold}, ::Union{<:T, Type{<:T}}) where {T <: $structname} = $manifold
103+
104+
DFG.getManifold(::Type{M}) where {M <: $structname} = $manifold
105+
DFG.getManifold(::M) where {M <: $structname} = getManifold(M)
106+
107+
DFG.getDimension(::Type{M}) where {M <: $structname} = manifold_dimension(getManifold(M))
108+
DFG.getDimension(::M) where {M <: $structname} = manifold_dimension(getManifold(M))
109+
110+
# # FIXME legacy API to be deprecated
111+
DFG.getManifolds(::Type{M}) where {M <: $structname} = convert(Tuple, $manifold)
112+
DFG.getManifolds(::M) where {M <: $structname} = convert(Tuple, $manifold)
86113
end)
87114
end
115+
88116
##------------------------------------------------------------------------------
89117
## solvedCount
90118
##------------------------------------------------------------------------------

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: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using DistributedFactorGraphs
22
using Test
33
using Dates
4+
using Manifolds
45

56
import Base: convert
67

@@ -11,13 +12,18 @@ import Base: convert
1112
# TestVariableType1() = new(1,(:Euclid,))
1213
# end
1314

14-
DFG.@defVariable TestVariableType1 1 (:Euclid,)
1515

16-
struct TestVariableType2 <: InferenceVariable
17-
dims::Int
18-
manifolds::Tuple{Symbol, Symbol}
19-
TestVariableType2() = new(2,(:Euclid,:Circular,))
20-
end
16+
Base.convert(::Type{<:Tuple}, ::typeof(Euclidean(1))) = (:Euclid,)
17+
Base.convert(::Type{<:Tuple}, ::typeof(Euclidean(2))) = (:Euclid, :Euclid)
18+
19+
@defVariable TestVariableType1 Euclidean(1)
20+
@defVariable TestVariableType2 Euclidean(2)
21+
22+
# struct TestVariableType2 <: InferenceVariable
23+
# dims::Int
24+
# manifolds::Tuple{Symbol, Symbol}
25+
# TestVariableType2() = new(2,(:Euclid,:Circular,))
26+
# end
2127

2228

2329
struct TestFunctorInferenceType1 <: AbstractRelative end

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)