Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix icp_numpy #1358

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

* Added `maxiter` parameter to `compas.geometry.icp_numpy`.

### Changed

* Fixed bug in `compas.geometry.ic_numpy`, which was caused by returning only the last transformation of the iteration process.

### Removed


Expand Down
13 changes: 10 additions & 3 deletions src/compas/geometry/icp_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from numpy import argmin
from numpy import asarray
from numpy.linalg import det
from numpy.linalg import multi_dot
from scipy.linalg import norm
from scipy.linalg import svd
from scipy.spatial.distance import cdist
Expand Down Expand Up @@ -36,7 +37,7 @@ def bestfit_transform(A, B):
return X


def icp_numpy(source, target, tol=None):
def icp_numpy(source, target, tol=None, maxiter=100):
"""Align two point clouds using the Iterative Closest Point (ICP) method.

Parameters
Expand All @@ -48,6 +49,8 @@ def icp_numpy(source, target, tol=None):
tol : float, optional
Tolerance for finding matches.
Default is :attr:`TOL.approximation`.
maxiter : int, optional
The maximum number of iterations.

Returns
-------
Expand Down Expand Up @@ -90,7 +93,9 @@ def icp_numpy(source, target, tol=None):
X = Transformation.from_frame_to_frame(A_frame, B_frame)
A = transform_points_numpy(A, X)

for i in range(20):
stack = [X]

for i in range(maxiter):
D = cdist(A, B, "euclidean")
closest = argmin(D, axis=1)
residual = norm(normrow(A - B[closest]))
Expand All @@ -101,4 +106,6 @@ def icp_numpy(source, target, tol=None):
X = bestfit_transform(A, B[closest])
A = transform_points_numpy(A, X)

return A, X
stack.append(X)

return A, multi_dot(stack[::-1])
Loading