| in.tetrahedron {pcds} | R Documentation |
Check whether a point is inside a tetrahedron
Description
Checks if the point p lies
in the tetrahedron, th,
using the barycentric coordinates, generally denoted as
(\alpha,\beta,\gamma).
If all (normalized or non-normalized) barycentric coordinates are positive
then the point p is inside the tetrahedron,
if all are nonnegative with one or more are zero,
then p falls on the boundary.
If some of the barycentric coordinates are negative,
then p falls outside the tetrahedron.
boundary is a logical argument (default=FALSE)
to include boundary or not, so if it is TRUE,
the function checks if the point, p,
lies in the closure of the tetrahedron (i.e., interior and boundary
combined) else it checks if p lies
in the interior of the tetrahedron.
Usage
in.tetrahedron(p, th, boundary = TRUE)
Arguments
p |
A 3D point to be checked whether it is inside the tetrahedron or not. |
th |
A |
boundary |
A logical parameter (default= |
Value
A list with two elements
in.tetra |
A logical output, if the point, |
barycentric |
The barycentric coordinates of the point |
Author(s)
Elvan Ceyhan
See Also
Examples
A<-c(0,0,0); B<-c(1,0,0); C<-c(1/2,sqrt(3)/2,0);
D<-c(1/2,sqrt(3)/6,sqrt(6)/3); P<-c(.1,.1,.1)
tetra<-rbind(A,B,C,D)
in.tetrahedron(P,tetra,boundary = FALSE)
in.tetrahedron(C,tetra)
in.tetrahedron(C,tetra,boundary = FALSE)
n1<-5; n2<-5; n<-n1+n2
Xp<-rbind(cbind(runif(n1),runif(n1,0,sqrt(3)/2),runif(n1,0,sqrt(6)/3)),
runif.tetra(n2,tetra)$g)
in.tetra<-vector()
for (i in 1:n)
{in.tetra<-c(in.tetra,in.tetrahedron(Xp[i,],tetra,boundary = TRUE)$in.tetra) }
in.tetra
dat.tet<-Xp[in.tetra,]
if (is.vector(dat.tet)) {dat.tet<-matrix(dat.tet,nrow=1)}
Xlim<-range(tetra[,1],Xp[,1])
Ylim<-range(tetra[,2],Xp[,2])
Zlim<-range(tetra[,3],Xp[,3])
xd<-Xlim[2]-Xlim[1]
yd<-Ylim[2]-Ylim[1]
zd<-Zlim[2]-Zlim[1]
plot3D::scatter3D(Xp[,1],Xp[,2],Xp[,3], phi=40,theta=40,
bty = "g", pch = 20, cex = 1,
ticktype="detailed",xlim=Xlim+xd*c(-.05,.05),
ylim=Ylim+yd*c(-.05,.05),zlim=Zlim+zd*c(-.05,.05))
#add the vertices of the tetrahedron
plot3D::points3D(tetra[,1],tetra[,2],tetra[,3], add=TRUE)
plot3D::points3D(dat.tet[,1],dat.tet[,2],dat.tet[,3],pch=4, add=TRUE)
L<-rbind(A,A,A,B,B,C); R<-rbind(B,C,D,C,D,D)
plot3D::segments3D(L[,1], L[,2], L[,3], R[,1], R[,2],R[,3], add=TRUE,lwd=2)
plot3D::text3D(tetra[,1],tetra[,2],tetra[,3],
labels=c("A","B","C","D"), add=TRUE)
in.tetrahedron(P,tetra) #this works fine