Acknowledgements

#This script is based on a MatLab code provided by 
# Carlo A. Beretta
# Math Clinic CellNetworks, University of Heidelberg
# Email: carlo.beretta@bioquant.uni-heidelberg.de
# Web: http://math-clinic.bioquant.uni-heidelberg.de
# Tel.: +49 (0) 6221 54 51435

Step1: Get the data

folder<-"/Path/To/Folder/"
data<-"/Path/To/Data/Data.csv"
setwd(folder)
WorkData<-read.delim(file = data,header = T,sep = ",",stringsAsFactors = F)
endX <- WorkData$EndX
endY <- WorkData$EndY
endZ <- WorkData$EndZ
startX <- WorkData$StartX[1]
startY <- WorkData$StartY[1]
startZ <- WorkData$StartZ[1]
centerEndX <- mean(endX)
centerEndY <- mean(endY)
centerEndZ <- mean(endZ)

par(mfrow=c(2,2))
plot(endX, endY,col="grey",
     xlim=c(-50,250),ylim=c(-50,250),
     xlab="X",ylab="Y",
     main="Input Coordinates")
points(centerEndX, centerEndY, pch=19, col="navyblue")
points(startX, startY, pch=19, col="darkcyan")
points(centerEndX, max(endY), pch=19, col="red")

plot(endZ, endY,col="grey",
     xlim=c(-50,250),ylim=c(-50,250),
     xlab="Z",ylab="Y",
     main="Input Coordinates")
points(centerEndZ, centerEndY, pch=19, col="navyblue")
points(startZ, startY, pch=19, col="darkcyan")
points(centerEndZ, max(endY), pch=19, col="red")

plot(endX, endZ,col="grey",
     xlim=c(-50,250),ylim=c(-50,250),
     xlab="X",ylab="Y",
     main="Input Coordinates")
points(centerEndX, centerEndZ, pch=19, col="navyblue")
points(startX, startZ, pch=19, col="darkcyan")
points(centerEndX, max(endZ), pch=19, col="red")

3D plots

par(mar=c(0,0,0,0))
plot3d( 
  x=c(endX,startX), y=c(endY,startY), z=c(endZ,startZ),
  xlim = c(min(endX,endY,endZ),max(endX,endY,endZ)),
  ylim = c(min(endX,endY,endZ),max(endX,endY,endZ)),
  zlim = c(min(endX,endY,endZ),max(endX,endY,endZ)),
  col = c(rep("blue",length(endX)),"red"), 
  type = 's', 
  radius = c(rep(1,length(endX)),5),
  xlab="X", ylab="Y", zlab="Z")

You must enable Javascript to view this page properly.

Step2: Rotate around XY

  #% Positive clockwise rotation
  if (startX >= centerEndX){
    maxY <- max(endY)
    originXY <- c(startX, startY)
    centroidXY <- c(centerEndX, centerEndY)
    intercepXY <- c(centerEndX, maxY)
    angleXY <- atan2(abs(det(matrix(c(intercepXY - centroidXY, originXY - centroidXY),nrow = 2,byrow = T))),
                     ((intercepXY - centroidXY)%*%( originXY - centroidXY)))
    theta <- angleXY
    #% Update the user
    message(sprintf("> Positive xy clockwise rotation of: %f (RAD)", theta))
  }  else if( startX < centerEndX){
    
    #% Negative clockwise rotation
    maxY <- max(endY)
    originXY <- c(startX, startY)
    centroidXY <- c(centerEndX, centerEndY)
    intercepXY <- c(centerEndX, maxY)
    angleXY <- atan2(abs(det(matrix(c(intercepXY - centroidXY, originXY - centroidXY),nrow=2,byrow=T))),
                     ((intercepXY - centroidXY)%*%(originXY - centroidXY)))
    theta <- -angleXY
    
    #% Update the user 
    message(sprintf("> Negative xy clockwise rotation of: %f (RAD)", theta))
  }
## > Positive xy clockwise rotation of: 0.128301 (RAD)
  #% End coordinates
  endX_R1 <- endX * cos(theta) - endY * sin(theta)
  endY_R1 <- endY * cos(theta) + endX * sin(theta)
  #% Center coordinates
  centerEndX_R1 <- centerEndX * cos(theta) - centerEndY * sin(theta)
  centerEndY_R1 <- centerEndY * cos(theta) + centerEndX * sin(theta)
  #% Start coordinates
  startX_R1 <- startX * cos(theta) - startY * sin(theta)
  startY_R1 <- startY * cos(theta) + startX * sin(theta)

Step3: Rotate around ZY

  if (startZ >= centerEndZ){
    maxY <- max(endY_R1)
    originZY <- c(startZ, startY_R1)
    centroidZY <- c(centerEndZ, centerEndY_R1)
    intercepZY <- c(centerEndZ, maxY)
    angleZY <- atan2(abs(det(matrix(c(intercepZY - centroidZY, originZY - centroidZY),nrow = 2,byrow = T))),
                     ((intercepZY - centroidZY)%*%( originZY - centroidZY)))
    theta <- angleZY
    
    #% Update the user
    message(sprintf("> Positive zy clockwise rotation of: %f (RAD)", theta))
  }  else if( startZ < centerEndZ){
    maxY <- max(endY_R1)
    originZY <- c(startZ, startY_R1)
    centroidZY <- c(centerEndZ, centerEndY_R1)
    intercepZY <- c(centerEndZ, maxY)
    angleZY <- atan2(abs(det(matrix(c(intercepZY - centroidZY, originZY - centroidZY),nrow=2,byrow=T))),
                     ((intercepZY - centroidZY)%*%(originZY - centroidZY)))
    theta <- -angleZY
    
    #% Update the user 
    message(sprintf("> Negative zy clockwise rotation of: %f (RAD)", theta))
  }
## > Positive zy clockwise rotation of: 0.360156 (RAD)
  #end coordinates
  endZ_R1 <- endZ * cos(theta) - endY_R1 * sin(theta)
  endY_R2 <- endY_R1 * cos(theta) + endZ * sin(theta)
  #% Center coordinates
  centerEndZ_R1 <- centerEndZ * cos(theta) - centerEndY_R1 * sin(theta)
  centerEndY_R2 <- centerEndY_R1 * cos(theta) + centerEndZ * sin(theta)
  #% Start coordinates
  startZ_R1 <- startZ * cos(theta) - startY_R1 * sin(theta)
  startY_R2 <- startY_R1 * cos(theta) + startZ * sin(theta)

Step4: Recenter image

  shiftX <- centerEndX_R1
  shiftY <- centerEndY_R2
  shiftZ <- centerEndZ_R1
  
  #end coordinates
  recenterEndX <- endX_R1 - shiftX
  recenterEndY <- endY_R2 - shiftY
  recenterEndZ <- endZ_R1 - shiftZ
  
  #% Center coordinates
  recenterCenterEndX <- centerEndX_R1 - shiftX
  recenterCenterEndY <- centerEndY_R2 - shiftY
  recenterCenterEndZ <- centerEndZ_R1 - shiftZ
  
  #% Start coordinates
  recenterStartX <- startX_R1 - shiftX
  recenterStartY <- startY_R2 - shiftY
  recenterStartZ <- startZ_R1 - shiftZ
  
  par(mfrow=c(2,2))
  plot(recenterEndX, recenterEndY,col="grey",
       xlim=c(-150,150),ylim=c(-150,150),
       xlab="X",ylab="Y",
       main="Center of mass coordinates to [0,0]")
  points(recenterCenterEndX, recenterCenterEndY, pch=19, col="navyblue")
  points(recenterStartX, recenterStartY, pch=19, col="darkcyan")
  points(recenterCenterEndX, max(recenterEndY), pch=19, col="red")
  
  plot(recenterEndZ, recenterEndY,col="grey",
       xlim=c(-150,150),ylim=c(-150,150),
       xlab="Z",ylab="Y",
       main="Center of mass coordinates to [0,0]")
  points(recenterCenterEndZ, recenterCenterEndY, pch=19, col="navyblue")
  points(recenterStartZ, recenterStartY, pch=19, col="darkcyan")
  points(recenterCenterEndZ, max(recenterEndY), pch=19, col="red")
  
  plot(recenterEndX, recenterEndZ,col="grey",
       xlim=c(-150,150),ylim=c(-150,150),
       xlab="X",ylab="Z",
       main="Center of mass coordinates to [0,0]")
  points(recenterCenterEndX, recenterCenterEndZ, pch=19, col="navyblue")
  points(recenterStartX, recenterStartZ, pch=19, col="darkcyan")
  points(recenterCenterEndX, max(recenterEndZ), pch=19, col="red")