\name{find.endpoints}
\alias{find.endpoints}
\title{
Find endpoints for the line of action of a muscle
}
\description{
Uses a binary mask of either a tendon or the origin and insertion sites of a muscle. Used by \code{\link{fiber.angle}} to calculate the pennation angle of fibers. 
}
\usage{
find.endpoints(images, mode = "origin.insertion", show.plot = FALSE, method = "kmeans")}
\arguments{
  \item{images}{
A vector of png image file names, created using list.files. Should include a binary mask.
}
  \item{mode}{
The coordinates of all white values from the image mask are obtained. If "origin.insertion", first uses k-means or hclust to automatically identify two clusters representing the origin and insertion sites. The mean of each of these clusters is taken and returned in endpoints. 
If "tendon"  is selected, a line is fit through the coordinates of the mask and the fitted values from the highest and lowest z-value of the mask are returned.
}
  \item{show.plot}{
Optionally show the two clusters generated by k-means for the "origin.insertion" option. Check that these are sensible.
}
  \item{method}{
Either "kmeans" or "hclust". "hclust" option first downsamples the mask to 5000 points, then conducts hierarchical cluster analysis using a Eucliedean distance matrix and Ward's algorithm. Default "kmeans" is often faster but may not always be effective if regions are variable in size or very wide.
}

}
\value{
 \item{endpoints}{
A 2X3 matrix of coordinates featuring the endpoints of a line to rotate the muscle fibers. To be used in \code{\link{fiber.angle}} to determine pennation angles around a line of action.
}
 \item{mask}{
The coordinates representing the binary mask in "images"
}
}
\details{
"images" must have the exact same dimensions (pixel width and height, image slice position) and orientation of the images used for fiber tracking. If cropped, the exact same settings should have been used on both image stacks.
}
\references{
Arbour, J. 2023. GoodFibes: an R package for the detection of muscle fibers from diceCT scans. \emph{Integrative Organismal Biology} 5(1): obad030.}
\author{
J. Arbour
}
\seealso{
\code{\link{fiber.angle}}
}
