- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在优化光线追踪器时,我尝试使用 Morton 空间填充曲线改进交叉数据结构的数据局部性,但我的 3D 空间不是立方体(例如 512x512x256)。所有边都是 2 的幂,但并非所有边的长度都相同。
我一直无法找到边为 2 的幂的非正方形 Morton 曲线的任何示例。如果重要的话,我可以保证 x/y 轴的大小相同,只是 z 轴的长度不同。
请注意宽度是 2x 高度,但它也可以是 3x 或 4x 或任何其他。我一直无法找到执行此操作的方法。
理想情况下,解决方案会很快,因为 Morton 代码必须进行大量计算。所以我的问题是:如何为非立方空间生成空间填充莫顿曲线?这是专门针对 GPU (Cuda) 的。
维度上的条件是:
x, y, z 是二的幂
x == y
x, y >= z
或者如果更容易
x, y > z
最佳答案
它可能会因为 nz=11 而中断,但是对于 XY 正方形的一半大小它似乎对我有用
#include <cstdint>
#include <iostream>
static inline uint32_t spread(uint32_t x)
{
x = (x | (x << 10)) & 0x000F801F;
x = (x | (x << 4)) & 0x00E181C3;
x = (x | (x << 2)) & 0x03248649;
x = (x | (x << 2)) & 0x09249249;
return x;
}
static inline uint32_t morton(const uint32_t x, const uint32_t y, const uint32_t z)
{
return spread(x) << 0 | spread(y) << 1 | spread(z) << 2;
}
auto main() -> int {
int nx = 32;
int ny = 32;
int nz = 16;
for (int iz = 0; iz != nz; ++iz)
{
for (int iy = 0; iy != ny; ++iy)
{
for (int ix = 0; ix != nx; ++ix)
{
auto m = morton(ix, iy, iz);
std::cout << m << '\n';
}
}
}
return 0;
}
更新
如何使 Morton 代码适用于 256x256x64(8 位*8 位*6 位):您必须非等距离地展开 X 和 Y,同时考虑 Z 中的位数。基本上,对于立方体,您均匀地展开:位置上的每一位0, 3, 6, 9, 12, 15, 18, 21, 24,为正交轴上的其他两位留出空间。
所以立方体存在等距分布。但是对于从 Z 插入只有 6 位的情况,您必须有 6 个 3 的距离,但最后一个间隙没有 Z 位,因此 X 和 Y 扩展的最后一个间隙应该只有 1 位宽。因此,X 和 Y 的非等距分布。
沿线的东西:如果 Nx=Ny 是 XY 平面中的位数,并且 Nz!=Nx 或 Ny 是沿 Z 轴的位数,Nz 位的扩展间隙应为 2 位,剩余的间隙应为 1 位。因此有两个展开例程 - 一个用于具有非等距展开的 X&Y,现在取决于 Nz,另一个用于 Z 轴的展开函数。
好的,这是一个工作版本,似乎做对了
#include <cstdint>
#include <iostream>
#define func auto
func spreadZ(uint32_t v) -> uint32_t { // 2bit gap spread
v = (v | (v << 10)) & 0x000F801F;
v = (v | (v << 4)) & 0x00E181C3;
v = (v | (v << 2)) & 0x03248649;
v = (v | (v << 2)) & 0x09249249;
return v;
}
func spreadXY(const uint32_t v, const uint32_t bitsZ) -> uint32_t {
uint32_t mask_z = (1U << bitsZ) - 1U; // to mask bits which are going to have 2bit gap
uint32_t lo{ v & mask_z }; // lower part of the value where there are Z bits
lo = spreadZ(lo); // 2bit gap spread
uint32_t hi = v >> bitsZ; // high part of the value, 1bit gap
// 1bit gap spread
hi = (hi ^ (hi << 8)) & 0x00ff00ffU;
hi = (hi ^ (hi << 4)) & 0x0f0f0f0fU;
hi = (hi ^ (hi << 2)) & 0x33333333U;
hi = (hi ^ (hi << 1)) & 0x55555555U;
return lo + (hi << 3*bitsZ); // combine them all together
}
func morton(const uint32_t x, const uint32_t y, const uint32_t z, const uint32_t bitsZ) -> uint32_t {
return spreadXY(x, bitsZ) << 0 | spreadXY(y, bitsZ) << 1 | spreadZ(z) << 2;
}
func ispowerof2(const uint32_t n) -> bool {
return n && (!(n & (n - 1u)));
}
func bit_pos(const uint32_t n) -> uint32_t {
if (!ispowerof2(n))
throw -1;
uint32_t i{ 1u }, pos{ 1u };
while (!(i & n)) { // Iterate through bits of n till we find a set bit, i&n will be non-zero only when 'i' and 'n' have a same bit
i = i << 1; // unset current bit and set the next bit in 'i'
++pos; // increment position
}
return pos;
}
func main() -> int {
int nx = 256;
int ny = 256;
int nz = 256; //256...128...64...32...16...8...4...2...1 all works
int bitsZ = bit_pos(nz) - 1; // should be doing try/catch
for (int iz = 0; iz != nz; ++iz)
{
for (int iy = 0; iy != ny; ++iy)
{
for (int ix = 0; ix != nx; ++ix)
{
auto m = morton(ix, iy, iz, bitsZ);
std::cout << m << '\n';
}
}
}
return 0;
}
关于c++ - 非立方区域的 Morton 曲线是 2 的幂,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65313532/
我在编写数学函数时遇到了麻烦。它应该接受 3 个变量并像这样计算方程。 答案 = x(1 + y/100)^ z 我把它写成: public compute_cert (int years, doub
我正在开发一个计算器,以便更好地学习 Java。我编写了自己的代码来使用 BigDecimal 参数计算幂。截至目前,代码无法处理分数幂,例如 2^2.2。为了解决这个问题,我想在我的代码中实现指数恒
我正在寻找一种算法(或者更好的是,代码!)来生成幂,特别是奇数指数大于 1 的数字:三次幂、五次幂、七次幂等等。然后我想要的输出是 8, 27, 32, 125, 128, 216, 243, 343
在 Codewars 上找到这个。该函数接受两个参数 A 和 B,并返回 A^B 的最后一位。下面的代码通过了前两个测试用例,但不会通过下一个测试用例。 def last_digit(n1, n2):
像 2^(2%1) 这样的表达式在 GHCi 中不会进行类型检查,并且错误消息是神秘的。为什么这不起作用,我需要改变什么? 我无法转换为其他类型,我希望将其用于 27^(1%3) 等表达式。 最佳答案
我的二次幂没有达到应有的水平,所以我想也许我可以 #define 做点什么。 不幸的是,我在预处理器指令方面经验不足,我不知道如何做 for 循环之类的事情。我看了看: http://www.cplu
如何在 Math.net 中获得三角函数的幂? Expr x = Expr.Variable("x"); Expr g = (2 * x).Sinh().Pow(2); g.ToString()给出输
我正在尝试拟合这个渐近接近零(但从未达到它)的数据。 我相信最好的曲线是逆逻辑函数,但欢迎建议。关键是预期的衰减“S 曲线”形状。 这是我到目前为止的代码,以及下面的绘图图像,这是一个非常丑陋的适合。
这个问题在这里已经有了答案: The most efficient way to implement an integer based power function pow(int, int) (2
我试图获得指数非常大的 double 值的幂(Java BigInteger 可以包含它(指数),例如:10^30 ) 也就是说,我想找到类似 1.75^(10^30) 或 1.23^(3423453
我有一个数学表达式,例如: ((2-x+3)^2+(x-5+7)^10)^0.5 我需要更换 ^符号到pow C语言的功能。我认为正则表达式是我需要的,但我不知道像专业人士那样的正则表达式。所以我最终
这是我的 previous question on bit flags 的后续内容,我澄清了一些重大误解。 我需要创建这些函数来查找包含零个或多个标志的 int 中的单个位标志: BitBinaryU
我已经在 java 中为 BigInteger 尝试过 modPow() 函数。 但它需要太长时间。 我知道模乘法,甚至也知道求幂。 但由于条件限制,我无法解决这个问题。 a、b 的值可以包含 100
我是一名优秀的程序员,十分优秀!