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

WIP first version of tps speedup #515

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
27 changes: 20 additions & 7 deletions R/xform.R
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,12 @@
#' plot(da2pns.R.L, col='blue', add=TRUE)
#' }
tpsreg<-function(sample, reference, ...){
structure(list(refmat=data.matrix(sample), tarmat=data.matrix(reference), ...),
reg=structure(list(refmat=data.matrix(sample), tarmat=data.matrix(reference), ...),

Check warning on line 426 in R/xform.R

View check run for this annotation

Codecov / codecov/patch

R/xform.R#L426

Added line #L426 was not covered by tests
class='tpsreg')
# precompute forward and reverse
attr(reg,'fwd')=Morpho::computeTransform(x=reg$tarmat, y=reg$refmat, type = "tps", ...)
attr(reg,'rev')=Morpho::computeTransform(x=reg$refmat, y=reg$tarmat, type = "tps", ...)
reg

Check warning on line 431 in R/xform.R

View check run for this annotation

Codecov / codecov/patch

R/xform.R#L429-L431

Added lines #L429 - L431 were not covered by tests
}

#' @description \code{xformpoints.tpsreg} enables \code{\link[nat]{xform}} and
Expand All @@ -436,12 +440,21 @@
#' @param points The 3D points to transform
#' @param swap Whether to change the direction of registration (default of
#' \code{NULL} checks if reg has a \code{attr('swap'=TRUE)}) otherwise
#' @param FallBackToAffine,na.action Not applicable to this function
#' @export
xformpoints.tpsreg <- function(reg, points, swap=NULL, ...){
if(isTRUE(swap) || isTRUE(attr(reg, 'swap'))) {
tmp=reg$refmat
reg$refmat=reg$tarmat
reg$tarmat=tmp
xformpoints.tpsreg <- function(reg, points, swap=NULL, FallBackToAffine = NULL, na.action = NULL, ...){

dir <- ifelse(isTRUE(swap) || isTRUE(attr(reg, 'swap')), 'rev', 'fwd')
trafo <- attr(reg, dir)
if(is.null(trafo)) {
warning("Incomplete thin plate splines registration!\n",
"I am recomputing, but please use `tpsreg()` to make a new registration")
trafo <- if(dir=='fwd') Morpho::computeTransform(x=reg$tarmat, y=reg$refmat, type = "tps", ...)

Check warning on line 452 in R/xform.R

View check run for this annotation

Codecov / codecov/patch

R/xform.R#L447-L452

Added lines #L447 - L452 were not covered by tests
else
Morpho::computeTransform(x=reg$refmat, y=reg$tarmat, type = "tps", ...)

Check warning on line 454 in R/xform.R

View check run for this annotation

Codecov / codecov/patch

R/xform.R#L454

Added line #L454 was not covered by tests
}
do.call(Morpho::tps3d, c(list(x=points), reg, list(...)))

Morpho::applyTransform(points, trafo, ...)

Check warning on line 457 in R/xform.R

View check run for this annotation

Codecov / codecov/patch

R/xform.R#L457

Added line #L457 was not covered by tests
}


11 changes: 10 additions & 1 deletion man/tpsreg.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/testthat/test-xformpoints.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ test_that("Thin plate spine registration", {
landmarks=xyz[sids,] #take some points as landmarks
landmarkst=scale(xyz[sids,], center = T, scale = rep(1.5, 3)) #now scale the landmarks..

#compute a transformation with sameple as scaled landmarks and reference as the original landmarks
#compute a transformation with sample as scaled landmarks and reference as the original landmarks
mytps=tpsreg(sample=landmarkst, reference = landmarks)

kcs20.t=xform(kcs20, mytps)
Expand Down
Loading