Skip to content

Commit 92f5dcf

Browse files
authored
Merge pull request #929 from JuliaRobotics/22Q4/enh/objaffsubcloud
ObjectAffordanceSubcloud Factor
2 parents 7679bf8 + 095b489 commit 92f5dcf

18 files changed

Lines changed: 655 additions & 142 deletions

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
1919
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
2020
FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341"
2121
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
22+
GeometryBasics = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
2223
ImageCore = "a09fc81d-aa75-5fe9-8630-4744c3626534"
2324
ImageMagick = "6218d12a-5da1-5696-b52f-db25d2ecc6d1"
2425
IncrementalInference = "904591bb-b899-562f-9e6f-b8df64c7d480"
@@ -92,7 +93,6 @@ BSON = "fbb218c0-5317-5bc6-957e-2ee96dd4b1f0"
9293
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
9394
Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
9495
FixedPointNumbers = "53c48c17-4a7d-5ca2-90c5-79b7896eea93"
95-
Gadfly = "c91e804a-d5a3-530f-b6f0-dfbca275c004"
9696
GraphPlot = "a2cc645c-3eea-5389-862e-a155d0052231"
9797
Images = "916415d5-f1e6-5110-898d-aaa5f9f070e0"
9898
PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0"
@@ -103,4 +103,4 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
103103
ZMQ = "c2297ded-f4af-51ae-bb23-16f91089e4e1"
104104

105105
[targets]
106-
test = ["AprilTags", "BSON", "DelimitedFiles", "Downloads", "FixedPointNumbers", "Gadfly", "GraphPlot", "Images", "PyCall", "Random", "RobotOS", "Test", "ZMQ"]
106+
test = ["AprilTags", "BSON", "DelimitedFiles", "Downloads", "FixedPointNumbers", "GraphPlot", "Images", "PyCall", "Random", "RobotOS", "Test", "ZMQ"]

examples/dev/icp/simpleicp.jl

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ function estimate_rigid_body_transformation(x_fix, y_fix, z_fix, nx_fix, ny_fix,
191191

192192
t = x[4:6]
193193

194-
H = create_homogeneous_transformation_matrix(R, t)
194+
H = affine_matrix(_SE3_MANI, ArrayPartition(t, R))
195+
# H = create_homogeneous_transformation_matrix(R, t)
195196

196197
return H, residuals
197198

@@ -205,12 +206,6 @@ function euler_angles_to_linearized_rotation_matrix(α1, α2, α3)
205206

206207
end
207208

208-
function create_homogeneous_transformation_matrix(R, t)
209-
210-
H = [R t
211-
zeros(1,3) 1]
212-
213-
end
214209

215210
function euler_coord_to_homogeneous_coord(XE)
216211

src/3rdParty/_PCL/_PCL.jl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
module _PCL
55

66
# using ..Colors
7-
import ..Caesar: _FastTransform3D, homogeneous_coord_to_euler_coord, euler_coord_to_homogeneous_coord, euler_angles_to_linearized_rotation_matrix, create_homogeneous_transformation_matrix
7+
import ..Caesar: _FastTransform3D
8+
import ..Caesar: homogeneous_coord_to_euler_coord, euler_coord_to_homogeneous_coord, euler_angles_to_linearized_rotation_matrix
9+
import ..Caesar: _SE3_MANI # create_homogeneous_transformation_matrix
810

911
using Colors
1012
using Dates
@@ -18,11 +20,13 @@ using LinearAlgebra
1820
using NearestNeighbors
1921
using Manifolds
2022
import Rotations as _Rot
23+
import GeometryBasics as GeoB # name collisions on members: Point, etc.
2124
using DistributedFactorGraphs
2225
using TensorCast
2326
using UUIDs
2427

25-
using Serialization # FIXME REMOVE, only used for legacy getDataPointCloud
28+
# FIXME REMOVE, only used for legacy getDataPointCloud
29+
using Serialization
2630

2731
# Going to add dispatches on these functions
2832
import Base: getindex, setindex!, resize!, cat, convert, sizeof, hasproperty, getproperty
@@ -33,12 +37,14 @@ import IncrementalInference: ArrayPartition
3337

3438
## hold off on exports, users can in the mean-time use/import via e.g. _PCL.PointXYZ
3539
# export PointT, PointXYZ, PointXYZRGB, PointXYZRGBA
40+
# export inside, getSubcloud
3641
# export PCLHeader, PointCloud
3742

3843

3944
# bring in the types
4045
include("entities/PCLTypes.jl")
4146
# bring in further source code
47+
include("services/GeomBasicsUtils.jl")
4248
include("services/PointCloud.jl")
4349
include("services/PointCloudUtils.jl")
4450
include("services/ConsolidateRigidTransform.jl")
@@ -47,7 +53,7 @@ include("services/ICP_Simple.jl")
4753

4854
function __init__()
4955
@require RobotOS="22415677-39a4-5241-a37a-00beabbbdae8" include("services/ROSConversions.jl")
50-
@require Gadfly="c91e804a-d5a3-530f-b6f0-dfbca275c004" include("services/PlottingUtilsPCL.jl")
56+
# moved plotting out of Caesar, use Arena.jl or RoMEPlotting.jl instead
5157
end
5258

5359

src/3rdParty/_PCL/services/ConsolidateRigidTransform.jl

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,59 @@
11

2+
@doc raw"""
3+
$SIGNATURES
4+
5+
Transform and put 2D pointcloud as [x,y] coordinates of a-frame into `aP_dest`, by transforming
6+
incoming b-frame points `bP_src` as [x,y] coords via the transform `aTb` describing the b-to-a (aka a-in-b)
7+
relation. Return the vector of points `aP_dest`.
8+
9+
````math
10+
{}^a \begin{bmatrix} x, y \end{bmatrix} = {}^a_b \mathbf{T} \, {}^b \begin{bmatrix} x, y \end{bmatrix}
11+
````
12+
13+
DevNotes
14+
- Consolidate functionality with [`_FastTransform3D`](@ref)
15+
16+
"""
17+
function _transformPointCloud!(
18+
# manifold
19+
M::Union{<:typeof(SpecialEuclidean(2)),<:typeof(SpecialEuclidean(3))},
20+
# destination points
21+
aP_dest::AbstractVector,
22+
# source points
23+
bP_src::AbstractVector,
24+
# transform coordinates
25+
aCb::AbstractVector{<:Real};
26+
# base point on manifold about which to do the transform
27+
e0::ArrayPartition = getPointIdentity(M),
28+
backward::Bool=false
29+
)
30+
#
31+
32+
aPb = retract(M, e0, hat(M, e0, aCb))
33+
aPb = backward ? inv(M,aPb) : aPb
34+
# can do 2d or 3d
35+
aTb = _FastTransform3D(M,aPb)
36+
aP_dest .= aTb.(bP_src)
37+
38+
aP_dest
39+
end
40+
41+
function _transformPointCloud(
42+
# manifold
43+
M::Union{<:typeof(SpecialEuclidean(2)),<:typeof(SpecialEuclidean(3))},
44+
# source points
45+
bP_src::AbstractVector,
46+
# transform coordinates
47+
aCb::AbstractVector{<:Real};
48+
kw...
49+
)
50+
#
51+
#dest points
52+
aP_dest = Vector{MVector{length(bP_src[1]),Float64}}(undef,length(bP_src))
53+
_transformPointCloud!(M, aP_dest, bP_src, aCb; kw...)
54+
end
55+
56+
257

358
## ============================================================
459
## Consolidated rigid transform standardized against Manifolds.jl
@@ -8,9 +63,11 @@
863
# Works for transform of both 2D and 3D point clouds
964
# FIXME, to optimize, this function will likely be slow
1065
# TODO, consolidate with transformPointcloud(::ScatterAlign,..) function
11-
function Manifolds.apply( M_::Union{<:typeof(SpecialEuclidean(2)),<:typeof(SpecialEuclidean(3))},
12-
rPp::Manifolds.ArrayPartition,
13-
pc::PointCloud{T} ) where T
66+
function Manifolds.apply(
67+
M_::Union{<:typeof(SpecialEuclidean(2)),<:typeof(SpecialEuclidean(3))},
68+
rPp::Manifolds.ArrayPartition,
69+
pc::PointCloud{T}
70+
) where T
1471
#
1572

1673
# allocate destination
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Utils based on GeometryBasics
2+
3+
#
4+
function transformFromWorldToLocal(
5+
dfg::AbstractDFG,
6+
vlbl::Symbol,
7+
w_BBo::GeoB.HyperRectangle;
8+
solveKey=:default
9+
)
10+
#
11+
v = getVariable(dfg, vlbl)
12+
# vt = getVariableType(v)
13+
M = getManifold(v)
14+
e0 = ArrayPartition(SA[1;1;1.], SMatrix{3,3}(diagm([1;1;1.])))
15+
16+
b_Cwp = getPPESuggested(dfg, vlbl, solveKey)
17+
w_T_p = exp(M, e0, hat(M, e0, b_Cwp))
18+
p_T_w = inv(M, w_T_p)
19+
p_H_w = SMatrix{4,4}(affine_matrix(M, p_T_w))
20+
21+
p_P1 = p_H_w * SA[w_BBo.origin...; 1.]
22+
p_P2 = p_H_w * SA[(w_BBo.origin+w_BBo.widths)...; 1.]
23+
24+
# pose to approximate object frame, ohat_T_p
25+
# NOTE, slightly weird transform in that world rotation and local translation are mixed, so one is inverted to get consistent left action
26+
# NOTE, assuming rectangular bounding box, make object frame the center of the volume
27+
ohat_V_p = SA[(SA[b_Cwp[1:3]...] - (w_BBo.origin+0.5*w_BBo.widths))...]
28+
ohat_T_p = ArrayPartition(ohat_V_p, SMatrix{3,3}(w_T_p.x[2]))
29+
30+
(
31+
GeoB.Rect3( GeoB.Point3(p_P1[1:3]...), GeoB.Point3((p_P2-p_P1)[1:3]...) ),
32+
ohat_T_p
33+
)
34+
end
35+
36+
"""
37+
$SIGNATURES
38+
39+
Is point p inside the HyperRectangle.
40+
"""
41+
function inside(
42+
hr::GeoB.HyperRectangle,
43+
p::GeoB.Point
44+
)
45+
#
46+
_p0,_p1 = GeoB.minmax(p, hr.origin, hr.origin + hr.widths)
47+
_p0 == hr.origin && hr.widths == (_p1-_p0)
48+
end
49+
50+
"""
51+
$SIGNATURES
52+
53+
Create a new subcloud containing the portion of the full point cloud that is inside the mask.
54+
"""
55+
function getSubcloud(
56+
pc_full::PointCloud, # the full point cloud
57+
mask # starting out with GeometryBasics.Rect3
58+
)
59+
objmask = map(s->_PCL.inside(mask,GeoB.Point(s.data[1:3]...)), pc_full.points)
60+
spt = (s->s.data[1:3]).(pc_full.points[objmask])
61+
@cast pts[i,d] := spt[i][d]
62+
_PCL.PointCloud(pts)
63+
end
64+
65+
66+
67+
#

src/3rdParty/_PCL/services/ICP_Simple.jl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ function estimate_rigid_body_transformation(x_fix, y_fix, z_fix, nx_fix, ny_fix,
193193

194194
t = x[4:6]
195195

196-
H = create_homogeneous_transformation_matrix(R, t)
196+
H = affine_matrix(_SE3_MANI, ArrayPartition(t, R))
197+
# H = create_homogeneous_transformation_matrix(R, t)
197198

198199
return H, residuals
199200

@@ -331,4 +332,17 @@ function alignICP_Simple(
331332
end
332333

333334

335+
function alignICP_Simple(
336+
p_PC::PointCloud,
337+
hatp_PC::PointCloud;
338+
kw...
339+
)
340+
p_pts_ = (s->s.data[1:3]).(p_PC.points)
341+
@cast p_pts[i,d] := p_pts_[i][d]
342+
hatp_pts_ = (s->s.data[1:3]).(hatp_PC.points)
343+
@cast hatp_pts[i,d] := hatp_pts_[i][d]
344+
345+
alignICP_Simple(p_pts, hatp_pts; kw...)
346+
end
347+
334348
##

src/3rdParty/_PCL/services/PlottingUtilsPCL.jl

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/3rdParty/_PCL/services/PointCloud.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ Base.show(io::IO, ::MIME"application/prs.juno.inline", pc::Header) = show(io, pc
408408

409409

410410
function Base.show(io::IO, pc::PCLPointCloud2)
411+
println(io)
411412
printstyled(io, "Caesar._PCL.PCLPointCloud2", bold=true, color=:blue)
412413
# println(io)
413414
# printstyled(io, " T = ", bold=true, color=:magenta)
@@ -441,6 +442,7 @@ Base.show(io::IO, ::MIME"application/prs.juno.inline", pc::PCLPointCloud2) = sho
441442

442443

443444
function Base.show(io::IO, pc::PointCloud{T,P,R}) where {T,P,R}
445+
println(io)
444446
printstyled(io, "Caesar._PCL.PointCloud{", bold=true, color=:blue)
445447
println(io)
446448
printstyled(io, " T = ", bold=true, color=:magenta)

0 commit comments

Comments
 (0)