Varlor
Varlor

Reputation: 1471

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

Because my previous question was very unclear, i edited it:

I have the following Problem:

I want to construct a pattern for a hallow sphere with the radius: r+fcr_size. The cavity in the hallow sphere should have the radius r. With the pattern I could use it on many different sphere centers and get many hallow spheres. Now i searching for the fastest solution. my approach is:

    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))

Upvotes: 3

Views: 247

Answers (1)

Menglong Li
Menglong Li

Reputation: 2255

This solution is based on Python Functional Programming, and hope you like it.

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)

Upvotes: 1

Related Questions