首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >递归地将多维数据集分解为8个较小的立方体(当多维数据集由中间点和大小定义时)

递归地将多维数据集分解为8个较小的立方体(当多维数据集由中间点和大小定义时)
EN

Stack Overflow用户
提问于 2015-03-24 16:05:23
回答 1查看 2.9K关注 0票数 6

我在工作上遇到了麻烦,所以我会很感激你的帮助。

短版本:给定一个立方体的中点和大小,我需要将它分割成8个较小的(2x2x2),并可能对其中的每一个重复。得到的坐标是唯一需要的。

我正在编写一些八叉树风格的代码,我试图允许它接受不同深度的输入(深度与点之间的间距有关,即2^depth,例如。深度0有1单位网格,深度-1有0.5,深度1有2)。我需要它能够在更高的深度得到一个坐标,并把它分解成适合实际深度的立方体。

例如,如果(0,0,0)点位于深度1,场景为深度0,则需要将其分解为8块,并将每个+-0.5单元移动到旧的多维数据集(2^(depth-1))中。

如果场景在深度-1,我需要把它分成8块,然后再把它们分成8块。我基本上需要它给出8^(difference in depth)的结果,这听起来很容易做,但它完全让我困惑,它的错误。

代码语言:javascript
复制
#Set up structure
octreeRange = ( 1, -1 )
octreeStructure = set()
for x in octreeRange:
    for y in octreeRange:
        for z in octreeRange:
            octreeStructure.add( ( x, y, z ) )

#octreeStructure is the 8 coordinates that a cube can split into

def recursiveCoordinate( coordinate, coordinateInfo, minDepthLevel, octreeStructure ):

    newDictionary = {}

    #Turn into list if isn't already list
    if type( coordinateInfo ) != list:
        coordinateInfo = [coordinateInfo,minDepthLevel]

    coordinateDepth = coordinateInfo[1]

    #Run function again for each coordinate that has a depth too high
    if coordinateDepth > minDepthLevel:
        coordinateInfo[1] -= 1
        moveAmount = pow( 2, coordinateDepth-1 )
        for i in octreeStructure:
            newCoordinate = [i[j]*moveAmount+coordinate[j] for j in xrange( 3 )]
            newDictionary.update( recursiveCoordinate( newCoordinate, coordinateInfo, minDepthLevel, octreeStructure ) )

    else:
        newDictionary[tuple( coordinate )] = coordinateInfo

    return newDictionary

minDepthLevel = 0
grid = {}
#grid[(x, y, z)] = [block ID, depth level]
grid[(1.5,0,0)] = [1,2]

newGrid = {}
for coordinate in grid:
    newGrid.update( recursiveCoordinate( coordinate, grid[coordinate], minDepthLevel, octreeStructure ) )
print len( newGrid.keys() ) 

为了获得视觉效果,拍下这张照片。当场景为0级时,中间点位于中间,定义为深度2级。实心黑线是第一次迭代,虚线是第二次也是最后一次迭代。我需要所有虚线立方体中点的坐标。

我想另一种方法是根据深度计算立方体的大小,然后将其分割成所需的部分,但这需要3个嵌套循环,可能要通过数千个值,所以如果可能的话,我想避免嵌套循环。

编辑:一个快速的事情,我在油漆作为一个2D的例子,你可以看到为什么我认为这将是超级容易。最后的结果,经过3次迭代,将产生64个坐标,将适合现场。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-24 18:31:03

我仍然不太确定这是否是你想要的,但下面是我要怎么做的:

首先,我要创建一个类,表示3D空间中的点:

代码语言:javascript
复制
class Point3D:
    """Representation of a point in 3D space."""
    def __init__(self, x, y, z):
        self.x = x
        self.y = y
        self.z = z

    def __add__(self, other):
        """Add two points.

        >>> Point3D(1, 2, 3) + Point3D(100, 200, 300)
        Point3D(101, 202, 303)
        """
        x = self.x + other.x
        y = self.y + other.y
        z = self.z + other.z
        return Point3D(x, y, z)

    def __mul__(self, a):
        """Multiply a point with a number.

        >>> Point3D(1, 2, 3) * 2
        Point3D(2, 4, 6)
        """
        x = self.x * a
        y = self.y * a
        z = self.z * a
        return Point3D(x, y, z)

    def __rmul__(self, a):
        """Multiply a number with a point.

        >>> 2 * Point3D(1, 2, 3)
        Point3D(2, 4, 6)
        """
        return self.__mul__(a)

    def __repr__(self):
        return 'Point3D({p.x}, {p.y}, {p.z})'.format(p=self)

这允许在计算派生多维数据集的中心点时使用更易读的代码。

然后我会创建一个类来表示多维数据集。实例可以被分成八个部分,并且知道它们的“深度”,这对于被分割的立方体来说是减少的。

必须移动中心点的八个方向使用itertools.product获得,并表示为Point3D对象,其各自的坐标设置为-1/+1。(我已经给出了简称为octreeStructure的简称DIR )。

多维数据集对象有一个向下一级的辅助函数_divide,它用于递归函数divide,该函数从多维数据集的深度下降到目标深度。

注意二维列表理解,它用于生成一个扁平的列表.

代码语言:javascript
复制
from __future__ import division
from itertools import product

class Cube:
    """Representation of a cube."""

    # directions to all eight corners of a cube
    DIR = [Point3D(*s) for s in product([-1, +1], repeat=3)]

    def __init__(self, center, size, depth=0):
        if not isinstance(center, Point3D):
            center = Point3D(*center)
        self.center = center
        self.size = size
        self.depth = depth

    def __repr__(self):
        return 'Cube(center={c.center}, size={c.size}, depth={c.depth})'.format(c=self)

    def _divide(self):
        """Divide into eight cubes of half the size and one level deeper."""
        c = self.center
        a = self.size/2
        d = self.depth - 1
        return [Cube(c + a/2*e, a, d) for e in Cube.DIR]

    def divide(self, target_depth=0):
        """Recursively divide down to the given depth and return a list of
        all 8^d cubes, where d is the difference between the depth of the
        cube and the target depth, or 0 if the depth of the cube is already
        equal to or less than the target depth.

        >>> c = Cube(center=(0, 0, 0), size=2, depth=1)
        >>> len(c.divide(0))
        8
        >>> len(c.divide(-1))
        64
        >>> c.divide(5)[0] is c
        True
        >>> c.divide(-1)[0].size
        0.5
        """
        if self.depth <= target_depth:
            return [self]
        smaller_cubes = self._divide()
        return [c for s in smaller_cubes for c in s.divide(target_depth)]

您的示例如下所示:

代码语言:javascript
复制
# minDepthLevel = 0
# grid = {}
# grid[(1.5,0,0)] = [1,2]
# not sure what this ^ 1 means

cube = Cube((1.5, 0, 0), 4, 2)
grid = {c.center: [1, c.depth] for c in cube.divide(0)}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29237493

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档