在一个三维空间上存储点的信息空间、信息

2023-09-08 00:44:18 作者:痛彻心扉

我在写一些code(只是为了好玩到目前为止)在Python中,将存储在每一个点在3D空间中的一些数据。

I'm writing some code (just for fun so far) in Python that will store some data on every point in a 3d space. I'm basically after a 3d matrix object that stores arbitary objects that will allow me to do some advanced selections, like:

获取其中x = 1,y = 2点,Z = 3。 在获取所有的点,其中y = 2。 在获取所有的点在3个单位的位置x = 1,Y = 2,Z = 3。 在获取所有的点在哪里point.getType()==富

在所有的上述情况,我需要结束了某种输出的,将给予我在该空间内的原来的位置,并储存在该点的数据。

In all of the above, I'd need to end up with some sort of output that would give me the original position in the space, and the data stored at that point.

显然numpy的可以做我想做的,但它似乎非常的科学计算和制定出如何让数据像我想​​上面迄今已躲避我进行了优化。

Apparently numpy can do what I want, but it seems highly optimised for scientific computing and working out how to get the data like I want above has so far eluded me.

有没有更好的选择,或者我应该回到敲我的脑袋上numpy的墙? :)

Is there a better alternative or should I return to banging my head on the numpy wall? :)

编辑:一些更多的信息前三的答案使我意识到我应该包括:我不担心的表现,这是一个纯粹的,我倒是preFER干净code为概念验证的很好的表现。我也将数据在给定的三维空间中的每一点,所以我想一个稀疏矩阵是坏?

some more info the first three answers made me realise I should include: I'm not worried about performance, this is purely a proof-of-concept where I'd prefer clean code to good performance. I will also have data for every point in the given 3d space, so I guess a Spare Matrix is bad?

推荐答案

下面是另一种常见的方法

Here's another common approach

class Point( object ):
    def __init__( self, x, y, z, data ):
        self.x, self.y, self.z = x, y, z
        self.data = data
    def distFrom( self, x, y, z )
        return math.sqrt( (self.x-x)**2 + (self.y-y)**2 + (self.z-z)**2 )

database = [ Point(x,y,z,data), Point(x,y,z,data), ... ]

让我们来看看你的用例。

Let's look at your use cases.

明白了吧,其中x = 1,Y = 2,Z = 3。

Get the point where x=1,y=2,z=3.

[ p for p in database if (p.x, p.y, p.z) == ( 1, 2, 3 ) ]

让所有百分点,其中y = 2。

Getting all points where y=2.

[ p for p in database if p.y == 2 ]

让所有点在3个单位的位置x = 1,Y = 2,Z = 3。

Getting all points within 3 units of position x=1,y=2,z=3.

[ p for p in database if p.distFrom( 1, 2, 3 ) <= 3.0 ]

让所有点在哪里point.getType()==富

Getting all points where point.getType() == "Foo"

[ p for p in database if type(p.data) == Foo ]