Skip to content

Commit 7670dd6

Browse files
authored
Merge pull request #1836 from JuliaRobotics/24Q3/enh/localchanges
few local changes to HeatMapSampler
2 parents af18de6 + cc72c63 commit 7670dd6

File tree

2 files changed

+73
-8
lines changed

2 files changed

+73
-8
lines changed

ext/HeatmapSampler.jl

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,16 @@ end
160160

161161
# Helper function to construct HGD
162162
function HeatmapGridDensity(
163-
data::AbstractMatrix{<:Real},
163+
# NAME SUGGESTION: SAMPLEABLE_FIELD, GenericField
164+
field_on_grid::AbstractMatrix{<:Real},
164165
domain::Tuple{<:AbstractVector{<:Real}, <:AbstractVector{<:Real}},
166+
# encapsulate above
165167
hint_callback::Union{<:Function, Nothing} = nothing,
166168
bw_factor::Real = 0.7; # kde spread between domain points
167169
N::Int = 10000,
168170
)
169171
#
170-
pos, weights_ = sampleHeatmap(data, domain..., 0)
172+
pos, weights_ = sampleHeatmap(field_on_grid, domain..., 0)
171173
# recast to the appropriate shape
172174
@cast support_[i, j] := pos[j][i]
173175

@@ -178,9 +180,9 @@ function HeatmapGridDensity(
178180

179181
@cast vec_preIS[j][i] := pts_preIS[i, j]
180182

181-
# weight the intermediate samples according to interpolation of raw data
183+
# weight the intermediate samples according to interpolation of raw field_on_grid
182184
# interpolated heatmap
183-
hm = Interpolations.linear_interpolation(domain, data) # depr .LinearInterpolation(..)
185+
hm = Interpolations.linear_interpolation(domain, field_on_grid) # depr .LinearInterpolation(..)
184186
d_scalar = Vector{Float64}(undef, length(vec_preIS))
185187

186188
# interpolate d_scalar for intermediate test points
@@ -204,7 +206,7 @@ function HeatmapGridDensity(
204206
density = ManifoldKernelDensity(TranslationGroup(Ndim(bel)), bel)
205207

206208
# return `<:SamplableBelief` object
207-
return HeatmapGridDensity(data, domain, hint_callback, bw_factor, density)
209+
return HeatmapGridDensity(field_on_grid, domain, hint_callback, bw_factor, density)
208210
end
209211

210212
function Base.isapprox(
@@ -225,7 +227,7 @@ end
225227

226228
# legacy construct helper
227229
function LevelSetGridNormal(
228-
data::AbstractMatrix{<:Real},
230+
field_on_grid::AbstractMatrix{<:Real},
229231
domain::Tuple{<:AbstractVector{<:Real}, <:AbstractVector{<:Real}},
230232
level::Real,
231233
sigma::Real;
@@ -235,8 +237,18 @@ function LevelSetGridNormal(
235237
N::Int = 10000,
236238
)
237239
#
238-
hgd = HeatmapGridDensity(data, domain, hint_callback, bw_factor; N = N)
239-
return LevelSetGridNormal(level, sigma, float(sigma_scale), hgd)
240+
field = HeatmapGridDensity(field_on_grid, domain, hint_callback, bw_factor; N = N)
241+
return LevelSetGridNormal(level, sigma, float(sigma_scale), field)
240242
end
241243

244+
# Field: domain (R^2/3), image (R^1/n scalar or tensor) e.g.: x,y -> elevation ;; x, y, z, t -> EM-field (R^(4x4))
245+
# Field( grid_x, grid_y,.... field_grid )
246+
# Field^ = interpolator(field_at_grid, grid)
247+
#
248+
# FieldGrid(data_on_grid, grid_domain) # internally does interpolation vodoo (same as Field^)
249+
# BeliefGrid <: FieldGrid
250+
# BeliefGrid(field_data: FieldGrid, measurement: Normal(mean: image_domain, cov: image_domain^2) ) -> domain, R_0+
251+
#
252+
# calcApproxLoss(ref::BeliefGrid, appr::ManifoldKernelDensity)::Field{Real}
253+
# ref = Normal(ScalarField - measurement, cov)
242254
#

src/Factors/GenericFunctions.jl

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,59 @@ function (cf::CalcFactor{<:ManifoldFactor})(X, p, q)
9999
return distanceTangent2Point(cf.factor.M, X, p, q)
100100
end
101101

102+
103+
## ======================================================================================
104+
## adjoint factor - adjoint action applied to the measurement
105+
## ======================================================================================
106+
107+
108+
# Adjoints defined in ApproxManifoldProducts
109+
struct AdFactor{F <: AbstractManifoldMinimize} <: AbstractManifoldMinimize
110+
factor::F
111+
end
112+
113+
function (cf::CalcFactor{<:AdFactor})(Xϵ, p, q)
114+
# M = getManifold(cf.factor)
115+
# p,q ∈ M
116+
# Xϵ ∈ TϵM
117+
# ϵ = identity_element(M)
118+
# transform measurement from TϵM to TpM (global to local coordinates)
119+
# Adₚ⁻¹ = AdjointMatrix(M, p)⁻¹ = AdjointMatrix(M, p⁻¹)
120+
# Xp = Adₚ⁻¹ * Xϵᵛ
121+
# ad = Ad(M, inv(M, p))
122+
# Xp = Ad(M, inv(M, p), Xϵ)
123+
# Xp = adjoint_action(M, inv(M, p), Xϵ)
124+
#TODO is vector transport supposed to be the same?
125+
# Xp = vector_transport_to(M, ϵ, Xϵ, p)
126+
127+
# Transform measurement covariance
128+
# ᵉΣₚ = Adₚ ᵖΣₚ Adₚᵀ
129+
#TODO test if transforming sqrt_iΣ is the same as Σ
130+
# Σ = ad * inv(cf.sqrt_iΣ^2) * ad'
131+
# sqrt_iΣ = convert(typeof(cf.sqrt_iΣ), sqrt(inv(Σ)))
132+
# sqrt_iΣ = convert(typeof(cf.sqrt_iΣ), ad * cf.sqrt_iΣ * ad')
133+
Xp =
134+
135+
child_cf = CalcFactorResidual(
136+
cf.faclbl,
137+
cf.factor.factor,
138+
cf.varOrder,
139+
cf.varOrderIdxs,
140+
cf.meas,
141+
cf.sqrt_iΣ,
142+
cf.cache,
143+
)
144+
return child_cf(Xp, p, q)
145+
end
146+
147+
getMeasurementParametric(f::AdFactor) = getMeasurementParametric(f.factor)
148+
149+
getManifold(f::AdFactor) = getManifold(f.factor)
150+
function getSample(cf::CalcFactor{<:AdFactor})
151+
M = getManifold(cf)
152+
return sampleTangent(M, cf.factor.factor.Z)
153+
end
154+
102155
## ======================================================================================
103156
## ManifoldPrior
104157
## ======================================================================================

0 commit comments

Comments
 (0)