簡體   English   中英

Python:計算從一個坐標到帶坐標的數組的核子距離

[英]Python : Calculate the eucledean distances from one coordinates to a array with coordinates

因為我之前的問題很不清楚,我編輯了它:

我有以下問題:

我想構建一個半徑為: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))

此解決方案基於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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM