########################################################################
#		     Functions to extract mobility and space use variables 
########################################################################

# Subsequent distances calculator (accessory function)
#----------------------------------------------------
#given a set of coordinates, it computes the vector of distances
#from one point to the subsequent one.

seqdst<-function(xy) {
    #x=matrix or dataframe with coordinates in the first two column (x, y)
  
    n<-nrow(xy)
    xy.st<-xy[-n,]
    xy.en<-xy[-1,]
    dx<-xy.en[,1]-xy.st[,1]
    dy<-xy.en[,2]-xy.st[,2]
    dst<-sqrt(dx^2+dy^2)
    return(dst)
}

# Function to extract mobility variables
#----------------------------------------------------
mobility<-function(x,th=0) {
  #INPUT
  #x=matrix or dataframe with coordinates in the first two column (x, y)
  #th=threshold distance (in cm) to consider a distance as an actual movement
  
  #OUTPUT
  #named vector with the following measures: 
  #mobi=proportion of time spent moving
  #totdst=total distance travelled
  #velmed=average speed when moving
  
  xy.df<-seqdst(x)
  mobi<-length(xy.df[xy.df>th])/length(xy.df)      #proportion
  totdst<-sum(xy.df[xy.df>th])                     #cm
  velmed<-15*totdst/length(xy.df[xy.df>th])        #cm/s
  out<-c(mobi,totdst,velmed)
  names(out)<-c("mobi","totdst","velmed")
  return(out)
}

#Functions to extract space use variables
#-----------------------------------------------------

#Time spent and number of crosses in the central rectangle (15 x 10 cm)
qc<-function(x, dx=7.5, dy=5) {
  e<-which(abs(x[,1])<=dx & abs(x[,2])<=dy)
  if (length(e)>0) {
    if (e[1]==1) {
      if (length(e)==1) {
        x<-x[-1,]
      } else {
        ss<-e[-1]-e[-length(e)]
        e0<-ifelse(mean(ss)>1,min(which(ss>1)),length(e))
        x<-x[-c(1:e0),]
      }
    }
    e<-which(abs(x[,1])<=dx & abs(x[,2])<=dy)
    tt<-length(e)/nrow(x)
    ss<-e[-1]-e[-length(e)]
    nn<-ifelse(tt>0,length(which(ss>8))+1,0)
    
  } else {
    tt=0; nn=0
  }
  out<-c(tt,nn)
}

#Time spent and number of crosses in the four angles (5 x 5 cm)
ang<-function(x, th=8) {
  
  #topleft corner
  asx<-which(x[,1] < -10 & x[,2] > 5)
  if (length(asx)>0) {
    tasx<-table((asx[-1]-asx[-length(asx)])>th)
    nasx<-ifelse(length(tasx)==2,tasx[2]+1,1)
  } else {nasx=0}
  
  #topright corner
  adx<-which(x[,1] > 10 & x[,2] > 5)
  if (length(adx)>0) {
    tadx<-table((adx[-1]-adx[-length(adx)])>th)
    nadx<-ifelse(length(tadx)==2,tadx[2]+1,1)
  } else {nadx=0}
  
  #bottomright corner
  bdx<-which(x[,1] > 10 & x[,2] < -5)
  if (length(bdx)>0) {
    tbdx<-table((bdx[-1]-bdx[-length(bdx)])>th)
    nbdx<-ifelse(length(tbdx)==2,tbdx[2]+1,1)
  } else {nbdx=0}
  
  #bottomleft corner
  bsx<-which(x[,1] < -10 & x[,2] < -5)
  if (length(bsx)>0) {
    tbsx<-table((bsx[-1]-bsx[-length(bsx)])>th)
    nbsx<-ifelse(length(tbsx)==2,tbsx[2]+1,1)
  } else {nbsx=0}
  
  out<-data.frame(
    tt=sum(unlist(lapply(list(asx,adx,bdx,bsx),length)))/nrow(x),
    ntr=sum(nasx,nadx,nbsx,nbdx))
  return(out)
}

