因为我之前的问题非常不清楚,所以我编辑了它:
我有以下问题:
我想为半径为r + fcr_size的中空球体构建一个模式。中空球体中的空腔应具有半径r。通过这种模式,我可以在许多不同的球体中心使用它,并获得许多神圣的球体。现在我正在寻找最快的解决方案。我的方法是:
centoEdge = radius+fcr_size #Bounding box coordinates from center to edge
xyz_pattern=[]
#Create the Bounding Box only in positive x,y,z direction, because they are later mirrowed
x1 = range(0,int(centoEdge)+1)
y1 = range(0,int(centoEdge)+1)
z1 = range(0,int(centoEdge)+1)
#Check if coordinates are the hallow sphere and add them to xyz_pattern list
for coords in itertools.product(x1,y1,z1):
if radius < distance.euclidean([0,0,0],coords) <= (radius+fcr_size):
xyz_pattern.append(coords)
#mirrow the pattern arround center
out = []
for point in xyz_pattern:
for factors in itertools.product([1, -1], repeat=3): # (1, 1, 1), (1, 1, -1), (1, -1, 1), ..., (-1, -1, -1)
out.append(tuple(point[i]*factors[i] for i in range(3)))
xyz_pattern=list(set(out))
答案 0 :(得分:1)
此解决方案基于Python Functional Programming,希望您喜欢它。
import math
from functools import partial
import itertools
import numpy as np
def distance(p1, p2):
return math.sqrt(sum(math.pow(float(x1) - float(x2), 2) for x1, x2 in zip(p1, p2)))
def inside_radius(radius, p):
return distance(p, (0, 0, 0)) < float(radius)
def inside_squre(centoEdge, p):
return all(math.fabs(x) <= centoEdge for x in p)
radius = 5
fcr_siz = 5
centoEdge = radius + fcr_siz
x1 = range(0, int(centoEdge) + 1)
y1 = range(0, int(centoEdge) + 1)
z1 = range(0, int(centoEdge) + 1)
coords = np.array(list(itertools.product(x1, y1, z1)))
inside_squre_with_cento = partial(inside_squre, centoEdge)
inside_radius_with_radius = partial(inside_radius, radius)
result = filter(lambda p: not inside_radius_with_radius(p), filter(inside_squre_with_cento, coords))
print(result)