C# 如何检查点是否在四面体中?
我知道四面体的所有坐标和我想确定的点。有人知道怎么做吗?我试图确定点属于四面体的每个三角形,如果它对所有三角形都成立,那么点就在四面体中。但这绝对是错误的 您可以通过四个顶点a、B、C和D定义一个四面体。 因此,您还可以使用4个三角形定义四面体的曲面 现在只需检查点p是否位于平面的另一侧。每个平面的法线指向远离四面体中心的方向。 所以你只需要对4架飞机进行测试C# 如何检查点是否在四面体中?,c#,opengl,tetrahedra,C#,Opengl,Tetrahedra,我知道四面体的所有坐标和我想确定的点。有人知道怎么做吗?我试图确定点属于四面体的每个三角形,如果它对所有三角形都成立,那么点就在四面体中。但这绝对是错误的 您可以通过四个顶点a、B、C和D定义一个四面体。 因此,您还可以使用4个三角形定义四面体的曲面 现在只需检查点p是否位于平面的另一侧。每个平面的法线指向远离四面体中心的方向。 所以你只需要对4架飞机进行测试 平面方程如下所示:a*x+b*y+c*z+d=0只需填写点值(x y z)。如果结果的符号大于0,则点与法线位于同一侧,结果==0,点位
平面方程如下所示:
a*x+b*y+c*z+d=0
只需填写点值(x y z)。如果结果的符号大于0,则点与法线位于同一侧,结果==0,点位于平面中,在您的情况下,您需要第三个选项:对于四面体的每个平面,检查点是否与其余顶点位于同一侧:
bool SameSide(v1, v2, v3, v4, p)
{
normal := cross(v2 - v1, v3 - v1)
dotV4 := dot(normal, v4 - v1)
dotP := dot(normal, p - v1)
return Math.Sign(dotV4) == Math.Sign(dotP);
}
你需要为每架飞机检查这个:
bool PointInTetrahedron(v1, v2, v3, v4, p)
{
return SameSide(v1, v2, v3, v4, p) &&
SameSide(v2, v3, v4, v1, p) &&
SameSide(v3, v4, v1, v2, p) &&
SameSide(v4, v1, v2, v3, p);
}
给定定义非退化四面体的4个点A、B、C、D和要测试的点p,一种方法是将p的坐标转换为四面体坐标系,例如以A为原点,向量B-A、C-A、D-A为单位向量 在该坐标系中,如果p在p内,则p的坐标都在0和1之间,但也可以位于由原点和3个单位向量定义的变换立方体中的任何位置。 断言P在(A、B、C、D)内的一种方法是依次将点(A、B、C和D)和其他三个点作为原点来定义新的坐标系。该试验重复4次是有效的,但可以改进 最有效的方法是只变换一次坐标并重用前面提出的SameSide函数,例如以A为原点,变换为(A,B,C,D)坐标系,p和A必须位于(B,C,D)平面的同一侧 下面是该测试的numpy/python实现。试验表明,该方法比平面法快2-3倍
import numpy as np
def sameside(v1,v2,v3,v4,p):
normal = np.cross(v2-v1, v3-v1)
return ((np.dot(normal, v4-v1)*p.dot(normal, p-v1) > 0)
def tetraCoord(A,B,C,D):
v1 = B-A ; v2 = C-A ; v3 = D-A
# mat defines an affine transform from the tetrahedron to the orthogonal system
mat = np.concatenate((np.array((v1,v2,v3,A)).T, np.array([[0,0,0,1]])))
# The inverse matrix does the opposite (from orthogonal to tetrahedron)
M1 = np.linalg.inv(mat)
return(M1)
def pointInsideT(v1,v2,v3,v4,p):
# Find the transform matrix from orthogonal to tetrahedron system
M1=tetraCoord(v1,v2,v3,v4)
# apply the transform to P
p1 = np.append(p,1)
newp = M1.dot(p1)
# perform test
return(np.all(newp>=0) and np.all(newp <=1) and sameside(v2,v3,v4,v1,p))
将numpy导入为np
def同侧(v1、v2、v3、v4、p):
正常=np交叉(v2-v1,v3-v1)
返回((np.dot(正常,v4-v1)*p.dot(正常,p-v1)>0)
def四脚(A、B、C、D):
v1=B-A;v2=C-A;v3=D-A
#mat定义了从四面体到正交系统的仿射变换
mat=np.concatenate((np.array((v1,v2,v3,A)).T,np.array([[0,0,0,1]]))
#逆矩阵的作用相反(从正交到四面体)
M1=np.linalg.inv(材料)
返回(M1)
def pointInsideT(v1、v2、v3、v4、p):
#求正交到四面体系统的变换矩阵
M1=四足动物(v1、v2、v3、v4)
#将变换应用于P
p1=np.append(p,1)
newp=M1.点(p1)
#执行测试
return(np.all(newp>=0)和np.all(newp从开始,这里是一个更简单(甚至)更有效的方法:
import numpy as np
def tetraCoord(A,B,C,D):
# Almost the same as Hugues' function,
# except it does not involve the homogeneous coordinates.
v1 = B-A ; v2 = C-A ; v3 = D-A
mat = np.array((v1,v2,v3)).T
# mat is 3x3 here
M1 = np.linalg.inv(mat)
return(M1)
def pointInside(v1,v2,v3,v4,p):
# Find the transform matrix from orthogonal to tetrahedron system
M1=tetraCoord(v1,v2,v3,v4)
# apply the transform to P (v1 is the origin)
newp = M1.dot(p-v1)
# perform test
return (np.all(newp>=0) and np.all(newp <=1) and np.sum(newp)<=1)
[编辑]
根据矢量化过程的基本原理,如果要查找网格的哪个元素包含给定点,这里有一个高度矢量化的解决方案:
输入数据:
node_坐标
:(n_节点
,3)包含每个节点坐标的数组
node_id
:(n_tet
,4)数组,其中第i行给出第i四面体的顶点索引
def其中(节点坐标,节点ID,p):
ori=节点坐标[节点ID[:,0],:]
v1=节点坐标[节点ID[:,1],:]-ori
v2=节点坐标[节点ID[:,2],:]-ori
v3=节点坐标[节点ID[:,3],:]-ori
n_tet=len(节点ID)
v1r=v1.T.重塑((3,1,n_-tet))
v2r=v2.T.重塑(3,1,n_-tet))
v3r=v3.T.重塑((3,1,n_-tet))
mat=np.连接((v1r,v2r,v3r),轴=1)
inv_mat=np.linalg.inv(mat.T).T#https://stackoverflow.com/a/41851137/12056867
如果p.size==3:
p=p.重塑((1,3))
n_p=p.shape[0]
orir=np.repeat(ori[:,:,np.newaxis],n_p,axis=2)
newp=np.einsum('imk kmj->kij',inv\U mat,p.T-orir)
val=np.all(newp>=0,axis=1)&np.all(newp我对Dorian和Hughes的解决方案进行了矢量化,以将整个点数组作为输入。我还将四色函数移到pointsInside函数之外,并重命名了这两个函数,因为没有必要为每个点调用它
在我的计算机上,@Dorian的解决方案和示例运行时间为2.5秒。在相同的数据上,我的计算机以0.003秒的速度运行近千倍。如果出于某种原因需要更高的速度,将GPU cupy包导入为“np”会将其推到100微秒的范围内
import time
# alternatively, import cupy as np if len(points)>1e7 and GPU
import numpy as np
def Tetrahedron(vertices):
"""
Given a list of the xyz coordinates of the vertices of a tetrahedron,
return tetrahedron coordinate system
"""
origin, *rest = vertices
mat = (np.array(rest) - origin).T
tetra = np.linalg.inv(mat)
return tetra, origin
def pointInside(point, tetra, origin):
"""
Takes a single point or array of points, as well as tetra and origin objects returned by
the Tetrahedron function.
Returns a boolean or boolean array indicating whether the point is inside the tetrahedron.
"""
newp = np.matmul(tetra, (point-origin).T).T
return np.all(newp>=0, axis=-1) & np.all(newp <=1, axis=-1) & (np.sum(newp, axis=-1) <=1)
npt=10000000
points = np.random.rand(npt,3)
# Coordinates of vertices A, B, C and D
A=np.array([0.1, 0.1, 0.1])
B=np.array([0.9, 0.2, 0.1])
C=np.array([0.1, 0.9, 0.2])
D=np.array([0.3, 0.3, 0.9])
start_time = time.time()
vertices = [A, B, C, D]
tetra, origin = Tetrahedron(vertices)
inTet = pointInside(points, tetra, origin)
print("--- %s seconds ---" % (time.time() - start_time))
导入时间
#或者,如果len(points)>1e7和GPU,则将cupy作为np导入
将numpy作为np导入
def四面体(顶点):
"""
给定四面体顶点的xyz坐标列表,
返回四面体坐标系
"""
原点,*静止=顶点
mat=(np.array(rest)-原点).T
tetra=np.linalg.inv(材料)
返回tetra,原点
def pointInside(point、tetra、origin):
"""
获取单个点或点数组,以及
四面体函数。
返回一个布尔或布尔数组,指示点是否位于四面体内部。
"""
newp=np.matmul(tetra,(点原点).T).T
返回np.all(newp>=0,axis=-1)和np.all(newp),多亏了Dorian的测试用例脚本,我可以处理另一个解决方案,并将其与目前为止的解决方案进行快速比较
直觉
对于三角形ABC和点p,如果将p连接到角以获得向量PA、PB、PC,并比较由PA、PC和PB、PC跨越的两个三角形X和Y,则如果X和Y重叠,则点p位于三角形ABC内
或者换句话说,不可能构造t
--- 15.621951341629028 seconds ---
--- 8.97989797592163 seconds ---
--- 4.597853660583496 seconds ---
def where(node_coordinates, node_ids, p):
ori=node_coordinates[node_ids[:,0],:]
v1=node_coordinates[node_ids[:,1],:]-ori
v2=node_coordinates[node_ids[:,2],:]-ori
v3=node_coordinates[node_ids[:,3],:]-ori
n_tet=len(node_ids)
v1r=v1.T.reshape((3,1,n_tet))
v2r=v2.T.reshape((3,1,n_tet))
v3r=v3.T.reshape((3,1,n_tet))
mat = np.concatenate((v1r,v2r,v3r), axis=1)
inv_mat = np.linalg.inv(mat.T).T # https://stackoverflow.com/a/41851137/12056867
if p.size==3:
p=p.reshape((1,3))
n_p=p.shape[0]
orir=np.repeat(ori[:,:,np.newaxis], n_p, axis=2)
newp=np.einsum('imk,kmj->kij',inv_mat,p.T-orir)
val=np.all(newp>=0, axis=1) & np.all(newp <=1, axis=1) & (np.sum(newp, axis=1)<=1)
id_tet, id_p = np.nonzero(val)
res = -np.ones(n_p, dtype=id_tet.dtype) # Sentinel value
res[id_p]=id_tet
return res
import time
# alternatively, import cupy as np if len(points)>1e7 and GPU
import numpy as np
def Tetrahedron(vertices):
"""
Given a list of the xyz coordinates of the vertices of a tetrahedron,
return tetrahedron coordinate system
"""
origin, *rest = vertices
mat = (np.array(rest) - origin).T
tetra = np.linalg.inv(mat)
return tetra, origin
def pointInside(point, tetra, origin):
"""
Takes a single point or array of points, as well as tetra and origin objects returned by
the Tetrahedron function.
Returns a boolean or boolean array indicating whether the point is inside the tetrahedron.
"""
newp = np.matmul(tetra, (point-origin).T).T
return np.all(newp>=0, axis=-1) & np.all(newp <=1, axis=-1) & (np.sum(newp, axis=-1) <=1)
npt=10000000
points = np.random.rand(npt,3)
# Coordinates of vertices A, B, C and D
A=np.array([0.1, 0.1, 0.1])
B=np.array([0.9, 0.2, 0.1])
C=np.array([0.1, 0.9, 0.2])
D=np.array([0.3, 0.3, 0.9])
start_time = time.time()
vertices = [A, B, C, D]
tetra, origin = Tetrahedron(vertices)
inTet = pointInside(points, tetra, origin)
print("--- %s seconds ---" % (time.time() - start_time))
import numpy as np
import time
def sameside(v1,v2,v3,v4,p):
normal = np.cross(v2-v1, v3-v1)
return (np.dot(normal, v4-v1) * np.dot(normal, p-v1) > 0)
# Nico's solution
def pointInside_Nico(v1,v2,v3,v4,p):
return sameside(v1, v2, v3, v4, p) and sameside(v2, v3, v4, v1, p) and sameside(v3, v4, v1, v2, p) and sameside(v4, v1, v2, v3, p)
# Hugues' solution
def tetraCoord(A,B,C,D):
v1 = B-A ; v2 = C-A ; v3 = D-A
# mat defines an affine transform from the tetrahedron to the orthogonal system
mat = np.concatenate((np.array((v1,v2,v3,A)).T, np.array([[0,0,0,1]])))
# The inverse matrix does the opposite (from orthogonal to tetrahedron)
M1 = np.linalg.inv(mat)
return(M1)
def pointInside_Hugues(v1,v2,v3,v4,p):
# Find the transform matrix from orthogonal to tetrahedron system
M1=tetraCoord(v1,v2,v3,v4)
# apply the transform to P
p1 = np.append(p,1)
newp = M1.dot(p1)
# perform test
return(np.all(newp>=0) and np.all(newp <=1) and sameside(v2,v3,v4,v1,p))
#Dorian's solution
def tetraCoord_Dorian(A,B,C,D):
v1 = B-A ; v2 = C-A ; v3 = D-A
# mat defines an affine transform from the tetrahedron to the orthogonal system
mat = np.array((v1,v2,v3)).T
# The inverse matrix does the opposite (from orthogonal to tetrahedron)
M1 = np.linalg.inv(mat)
return(M1)
def pointInside_Dorian(v1,v2,v3,v4,p):
# Find the transform matrix from orthogonal to tetrahedron system
M1=tetraCoord_Dorian(v1,v2,v3,v4)
# apply the transform to P
newp = M1.dot(p-v1)
# perform test
return (np.all(newp>=0) and np.all(newp <=1) and np.sum(newp)<=1)
#TomNorway's solution adapted to cope with n tetrahedrons
def Tetrahedron(vertices):
"""
Given a list of the xyz coordinates of the vertices of a tetrahedron,
return tetrahedron coordinate system
"""
origin, *rest = vertices
mat = (np.array(rest) - origin).T
tetra = np.linalg.inv(mat)
return tetra, origin
def pointInside(point, tetra, origin):
"""
Takes a single point or array of points, as well as tetra and origin objects returned by
the Tetrahedron function.
Returns a boolean or boolean array indicating whether the point is inside the tetrahedron.
"""
newp = np.matmul(tetra, (point-origin).T).T
return np.all(newp>=0, axis=-1) & np.all(newp <=1, axis=-1) & (np.sum(newp, axis=-1) <=1)
# Proposed solution
def det3x3_Philipp(b,c,d):
return b[0]*c[1]*d[2] + c[0]*d[1]*b[2] + d[0]*b[1]*c[2] - d[0]*c[1]*b[2] - c[0]*b[1]*d[2] - b[0]*d[1]*c[2]
def pointInside_Philipp(v0,v1,v2,v3,p):
a = v0 - p
b = v1 - p
c = v2 - p
d = v3 - p
detA = det3x3_Philipp(b,c,d)
detB = det3x3_Philipp(a,c,d)
detC = det3x3_Philipp(a,b,d)
detD = det3x3_Philipp(a,b,c)
ret0 = detA > 0.0 and detB < 0.0 and detC > 0.0 and detD < 0.0
ret1 = detA < 0.0 and detB > 0.0 and detC < 0.0 and detD > 0.0
return ret0 or ret1
npt=100000
Pt= np.array([ np.array([p[0]-0.5,p[1]-0.5,p[2]-0.5]) for p in np.random.rand(npt,3)])
A=np.array([ np.array([p[0]-0.5,p[1]-0.5,p[2]-0.5]) for p in np.random.rand(npt,3)])
B=np.array([ np.array([p[0]-0.5,p[1]-0.5,p[2]-0.5]) for p in np.random.rand(npt,3)])
C=np.array([ np.array([p[0]-0.5,p[1]-0.5,p[2]-0.5]) for p in np.random.rand(npt,3)])
D=np.array([ np.array([p[0]-0.5,p[1]-0.5,p[2]-0.5]) for p in np.random.rand(npt,3)])
inTet_Nico=np.zeros(shape=(npt,1),dtype=bool)
inTet_Hugues=np.copy(inTet_Nico)
inTet_Dorian=np.copy(inTet_Nico)
inTet_Philipp=np.copy(inTet_Nico)
print("non vectorized, n points, different tetrahedrons:")
start_time = time.time()
for i in range(0,npt):
inTet_Nico[i]=pointInside_Nico(A[i,:],B[i,:],C[i,:],D[i,:],Pt[i,:])
print("Nico's: --- %s seconds ---" % (time.time() - start_time)) # https://stackoverflow.com/questions/1557571/how-do-i-get-time-of-a-python-programs-execution
start_time = time.time()
for i in range(0,npt):
inTet_Hugues[i]=pointInside_Hugues(A[i,:],B[i,:],C[i,:],D[i,:],Pt[i,:])
print("Hugues': --- %s seconds ---" % (time.time() - start_time))
start_time = time.time()
for i in range(0,npt):
inTet_Dorian[i]=pointInside_Dorian(A[i,:],B[i,:],C[i,:],D[i,:],Pt[i,:])
print("Dorian's: --- %s seconds ---" % (time.time() - start_time))
start_time = time.time()
for i in range(0,npt):
inTet_Philipp[i]=pointInside_Philipp(A[i,:],B[i,:],C[i,:],D[i,:],Pt[i,:])
print("Philipp's:--- %s seconds ---" % (time.time() - start_time))
print("vectorized, n points, 1 tetrahedron:")
start_time = time.time()
vertices = [A[0], B[0], C[0], D[0]]
tetra, origin = Tetrahedron(vertices)
inTet_Tom = pointInside(Pt, tetra, origin)
print("TomNorway's: --- %s seconds ---" % (time.time() - start_time))
for i in range(0,npt):
assert inTet_Hugues[i] == inTet_Nico[i]
assert inTet_Dorian[i] == inTet_Hugues[i]
#assert inTet_Tom[i] == inTet_Dorian[i] can not compare because Tom implements 1 tetra instead of n
assert inTet_Philipp[i] == inTet_Dorian[i]
'''errors = 0
for i in range(0,npt):
if ( inTet_Philipp[i] != inTet_Dorian[i]):
errors = errors + 1
print("errors " + str(errors))'''
non vectorized, n points, different tetrahedrons:
Nico's: --- 25.439453125 seconds ---
Hugues': --- 28.724457263946533 seconds ---
Dorian's: --- 15.006574153900146 seconds ---
Philipp's:--- 4.389788389205933 seconds ---
vectorized, n points, 1 tetrahedron:
TomNorway's: --- 0.008165121078491211 seconds ---