- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在执行此操作时遇到问题,因此不胜感激。
简短版本:给定立方体的中点和大小,我需要将它分成 8 个较小的部分 (2x2x2),并且可能对每个部分重复。生成的坐标是唯一需要的东西。
我正在写一些八叉树风格的代码,我试图让它接受不同深度的输入(深度与点之间的间距有关,即 2^depth
,例如。深度 0 有 1 个单位网格,深度 -1 有 0.5,深度 1 有 2)。我需要它能够获得更高深度的坐标,并将其分解为适合实际深度的立方体。
例如,如果我在深度 1 处有点 (0,0,0)
,而场景的深度为 0,我需要将它分成 8 block ,然后移动每一个 +-0.5
单位以适应旧立方体 (2^(depth-1)
)。
如果场景的深度为 -1,我需要将其分成 8 block ,然后再将其分成 8 block 。我基本上需要它来给出 8^(difference in depth)
结果,这听起来很容易做到,但它完全让我难过,因为它出错了。
#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 示例,我在绘画中做了一个快速的事情,你可以明白为什么我认为它会 super 简单。经过 3 次迭代后的最终结果将产生适合场景的 64 个坐标。
最佳答案
我仍然不太确定这是否是您想要的,但这是我想要的这样做:
首先,我将创建一个表示 3D 空间中的点的类:
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 的对象。 (我已将较短的名称 DIR
命名为您所谓的 octreeStructure
。)
立方体对象有一个辅助函数 _divide
向下一层,这用于递归函数 divide
从立方体的深度向下到目标深度。
注意用于生成扁平化列表的二维列表理解。
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)]
您的示例是这样完成的:
# 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)}
关于python - 递归地将立方体分解为 8 个更小的立方体(当立方体由中点和大小定义时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29237493/
我正在尝试在 R 中计算任意 N x J 矩阵 S 的投影矩阵 P: P = S (S'S) ^ -1 S' 我一直在尝试使用以下函数来执行此操作: P 概述 solve 基于一般方阵的 LU 分解
所以我有一个包含数千行的非常旧的文件(我猜是手工生成的),我正试图将它们移动到一个 rdb 中,但是这些行没有转换为列的格式/模式。例如,文件中的行如下所示: blah blahsdfas
这实际上只是一个“最佳实践”问题...... 我发现在开发应用程序时,我经常会得到很多 View 。 将这些 View 分解为几个 View 文件是常见的做法吗?换句话说......而不只是有view
使用以下函数foo()作为简单示例,如果可能的话,我想将...中给出的值分配给两个不同的函数。 foo args(mapply) function (FUN, ..., MoreArgs = NUL
正面案例:可以进入列表 groovy> println GroovySystem.version groovy> final data1 = [[99,2] , [100,4]] groovy> d
省略素数计算方法和因式分解方法的详细信息。 为什么要进行因式分解? 它的应用是什么? 最佳答案 哇,这个线程里有这么多争斗。 具有讽刺意味的是,这个问题有一个主要的有效答案。 因式分解实际上在加密/解
术语“分解不良”和“重构”程序是什么意思?你能举一个简单的例子来理解基本的区别吗? 最佳答案 重构是一种通用技术,可以指代许多任务。它通常意味着清理代码、去除冗余、提高代码质量和可读性。 分解不良代码
我以前有,here ,表明 C++ 函数不容易在汇编中表示。现在我有兴趣以一种或另一种方式阅读它们,因为 Callgrind 是 Valgrind 的一部分,在组装时显示它们已损坏。 所以我想要么破坏
最初,我一直在打开并同时阅读两个文件,内容如下: with open(file1, 'r') as R1: with open(file2, 'r') as R2: ### m
我正在尝试摆脱 标签和标签内的内容使用 beatifulsoup。我去看了文档,似乎是一个非常简单的调用函数。有关该功能的更多信息是 here .这是我到目前为止解析的 html 页面的内容...
给定一个 float ,我想将它分成几个部分的总和,每个部分都有给定的位数。例如,给定 3.1415926535 并要求将其分成以 10 为基数的部分,每部分 4 位数字,它将返回 3.141 + 5
我的 JSF 项目被部署为一个 EAR 文件。它还包括一些 war 文件。我需要 EAR 的分解版本(包括分解的内部 WAR)。 有什么工具可以做到吗? 最佳答案 以编程方式还是手动? EAR 和 W
以下函数不使用行透视进行 LU 分解。 R 中是否有一个现有的函数可以使用行数据进行 LU 分解? > require(Matrix) > expand(lu(matrix(rnorm(16),4,4
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 提供事实和引用来回答它. 7年前关闭。 Improve this
我正在使用登记数据进行病假研究。从登记册上,我只得到了每个人的病假开始日期和结束日期。但日期并没有逐年分割。例如,对于人 A,只有开始日期 (1-may-2016) 和结束日期 (14-feb-201
我发现以下 R 代码使用 qr 因式分解无法恢复原始矩阵。我不明白为什么。 a <- matrix(runif(180),ncol=6) a[,c(2,4)] <- 0 b <- qr(a) d <-
我正在尝试检测气候数据时间序列中的异常值,其中一些缺失的观测值。在网上搜索我发现了许多可用的方法。其中,STL 分解似乎很有吸引力,因为它去除了趋势和季节性成分并研究了其余部分。阅读 STL: A S
我想使用 javascript 分解数组中的 VIN,可能使用正则表达式,然后使用某种循环... 以下是读取 VIN 的方法: http://forum.cardekho.com/topic/600-
我正在研究 Databricks 示例。数据框的架构如下所示: > parquetDF.printSchema root |-- department: struct (nullable = true
我正在尝试简化我的代码并将其分解为多个文件。例如,我设法做到了: socket.once("disconnect", disconnectSocket); 然后有一个名为 disconnectSock
我是一名优秀的程序员,十分优秀!