2017-04-05 604 views
3

一個數組,因爲我剛纔的問題是非常不清楚,我編輯它:的Python:計算eucledean距離從一個座標,座標

我有以下問題:

我想構建一個模式一個半徑爲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

這樣一個球形腔的立方體? –

+0

@ Ev.Kounis:在邊界框中有一個球體空腔的球體 – Varlor

+1

但是邊界框體是球形的嗎?盒子對我來說總是聽起來是矩形的,這就是爲什麼我問... – jotasi

回答

1

該解決方案基於Python函數式編程,希望您喜歡。

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)