Here is my problem. I have an hypercube I built using the following codes:
这是我的问题。我有一个使用以下代码构建的超立方体:
X <- seq (-1/sqrt(2),1/sqrt(2),length.out=100)
Y <- seq (-sqrt(2)/(2*sqrt(3)),sqrt(2)/sqrt(3),length.out=100)
Z <- seq (-1/(2*sqrt(3)),sqrt(3)/2,length.out=100)
grid <- data.frame (expand.grid(X=X,Y=Y,Z=Z))
Then, I would delete from the grid data.frame all the points that are not located within the tetrahedron defined by the following coordinates:
然后,我将从网格data.frame中删除所有不在以下坐标定义的四面体内的点:
w : (0,0,sqrt(3)/2)
x : (0,sqrt(2)/sqrt(3),-1/(2*sqrt(3)))
y : (-1/sqrt(2),-sqrt(2)/(2*sqrt(3)),-1/(2*sqrt(3)))
z : (1/sqrt(2),-sqrt(2)/(2*sqrt(3)),-1/(2*sqrt(3)))
I do not find a away to do this without howfully long codes. Can anyone help me please Thanks !!!
如果没有多么长的代码我就找不到了。请任何人帮助我,谢谢!
2 个解决方案
#1
1
Package ptinpoly
has a function pip3d
to find wether a point is in a polyhedron or not.
包装ptinpoly具有函数pip3d,以发现多边形中的点是否存在。
library(ptinpoly)
X <- seq(-1/sqrt(2),1/sqrt(2),length.out=10) #I used a smaller dataset here
Y <- seq(-sqrt(2)/(2*sqrt(3)),sqrt(2)/sqrt(3),length.out=10)
Z <- seq(-1/(2*sqrt(3)),sqrt(3)/2,length.out=10)
# The query points has to be inputted as a matrix.
grid <- as.matrix(expand.grid(X=X,Y=Y,Z=Z))
w <- c(0,0,sqrt(3)/2)
x <- c(0,sqrt(2)/sqrt(3),-1/(2*sqrt(3)))
y <- c(-1/sqrt(2),-sqrt(2)/(2*sqrt(3)),-1/(2*sqrt(3)))
z <- c(1/sqrt(2),-sqrt(2)/(2*sqrt(3)),-1/(2*sqrt(3)))
# The matrix of vertices
tetra_vert <- matrix(c(w,x,y,z),byrow=TRUE,nrow=4)
# The matrix of faces (each row correspond to a vector of vertices linked by a face.
tetra_faces <- matrix(c(1,2,3,
1,2,4,
1,3,4,
2,3,4),byrow=TRUE,nrow=4)
inout <- pip3d(tetra_vert, tetra_faces, grid)
The result is a vector of integers, 0
means the point fall on a face, 1
that it is inside the polyhedron, -1
outside.
结果是一个整数向量,0表示点落在一个面上,1表示它在多面体内,-1在外面。
The solution of your problem is therefore:
因此,您的问题的解决方案是:
grid[inout%in%c(0,1),]
#2
0
make planes which form the tetrahedron and compare if a point is on the right side of each of the planes.
制作形成四面体的平面,并比较一个点是否位于每个平面的右侧。
pointers: think of calculating dot products with the plane normal and such. One option is to draw a vector from tetrahedron point to each corner, 4 in total and 1 vector from point to point and then use dotproducts and whatnot to see if the point-point vector is within the 4 others.
指针:考虑用平面法等计算点积。一种选择是从四面体点到每个角绘制一个矢量,总共4个点和点到点的1个矢量,然后使用点积和什么来查看点点矢量是否在4个其他点之内。
the point is probably within the tetrahedron if vector to it can be expressed as a sum of non negative multiples of the corner vectors and the vector short enough.
如果向量的矢量可以表示为角矢量的非负多次和足够短的矢量之和,则该点可能在四面体内。
#1
1
Package ptinpoly
has a function pip3d
to find wether a point is in a polyhedron or not.
包装ptinpoly具有函数pip3d,以发现多边形中的点是否存在。
library(ptinpoly)
X <- seq(-1/sqrt(2),1/sqrt(2),length.out=10) #I used a smaller dataset here
Y <- seq(-sqrt(2)/(2*sqrt(3)),sqrt(2)/sqrt(3),length.out=10)
Z <- seq(-1/(2*sqrt(3)),sqrt(3)/2,length.out=10)
# The query points has to be inputted as a matrix.
grid <- as.matrix(expand.grid(X=X,Y=Y,Z=Z))
w <- c(0,0,sqrt(3)/2)
x <- c(0,sqrt(2)/sqrt(3),-1/(2*sqrt(3)))
y <- c(-1/sqrt(2),-sqrt(2)/(2*sqrt(3)),-1/(2*sqrt(3)))
z <- c(1/sqrt(2),-sqrt(2)/(2*sqrt(3)),-1/(2*sqrt(3)))
# The matrix of vertices
tetra_vert <- matrix(c(w,x,y,z),byrow=TRUE,nrow=4)
# The matrix of faces (each row correspond to a vector of vertices linked by a face.
tetra_faces <- matrix(c(1,2,3,
1,2,4,
1,3,4,
2,3,4),byrow=TRUE,nrow=4)
inout <- pip3d(tetra_vert, tetra_faces, grid)
The result is a vector of integers, 0
means the point fall on a face, 1
that it is inside the polyhedron, -1
outside.
结果是一个整数向量,0表示点落在一个面上,1表示它在多面体内,-1在外面。
The solution of your problem is therefore:
因此,您的问题的解决方案是:
grid[inout%in%c(0,1),]
#2
0
make planes which form the tetrahedron and compare if a point is on the right side of each of the planes.
制作形成四面体的平面,并比较一个点是否位于每个平面的右侧。
pointers: think of calculating dot products with the plane normal and such. One option is to draw a vector from tetrahedron point to each corner, 4 in total and 1 vector from point to point and then use dotproducts and whatnot to see if the point-point vector is within the 4 others.
指针:考虑用平面法等计算点积。一种选择是从四面体点到每个角绘制一个矢量,总共4个点和点到点的1个矢量,然后使用点积和什么来查看点点矢量是否在4个其他点之内。
the point is probably within the tetrahedron if vector to it can be expressed as a sum of non negative multiples of the corner vectors and the vector short enough.
如果向量的矢量可以表示为角矢量的非负多次和足够短的矢量之和,则该点可能在四面体内。