C 3D中的Morton排序使用unit64\t作为输入

C 3D中的Morton排序使用unit64\t作为输入,c,z-order-curve,C,Z Order Curve,我试图使用Morton代码为给定的(x,y,z)生成唯一的编码,其中x,y,z是双精度浮点数。我假设我可以使用类型转换将浮点数转换为整数,并对这些整数运行Morton排序。例如,考虑以下代码 C++ >代码>代码。(我现在不知道如何在C中执行相同的操作) 我设法找到了一些莫顿编码的代码,甚至在这个论坛上也找到了一些,但没有一个使用了3D中的int64\t。因此,我需要论坛专家的帮助,如何编码和解码int64_t整数 我成功地对以下代码进行了反向工程。不幸的是,有一些错误,我没有得到正确的数字,当

我试图使用Morton代码为给定的(x,y,z)生成唯一的编码,其中x,y,z是双精度浮点数。我假设我可以使用类型转换将浮点数转换为整数,并对这些整数运行Morton排序。例如,考虑以下代码<代码> C++ >代码>代码。(我现在不知道如何在
C
中执行相同的操作)

我设法找到了一些莫顿编码的代码,甚至在这个论坛上也找到了一些,但没有一个使用了3D中的
int64\t
。因此,我需要论坛专家的帮助,如何编码和解码
int64_t
整数

我成功地对以下代码进行了反向工程。不幸的是,有一些错误,我没有得到正确的数字,当我运行解码部分。如果能帮我找出问题所在,我将不胜感激

#包括
#包括
#包括
使用名称空间std;
uint64\u t代码\u 2D\u M(双xd,双yd){
uint64\u t x=重新解释铸件(xd);
uint64_t y=重新解释铸件(yd);

x=(x |)(x“我假设我可以使用类型转换将浮点转换为整数,并对这些整数运行Morton排序”--可以。我想你实际上是在看指数、尾数、符号的位置。我觉得,如果你的动态范围很大,它可能不会给出你想要的结果。如果你的动态范围足够小,也许你可以做一个线性标度,从
float
int64
。我是基本的ally试图将3个数字塞进一个数字中,然后将它们解码回来。我这样做是为了节省内存,我认为Morton order可能是最简单的解决方案。如果你想做大量的数字,最好在整个数据流中使用更通用的压缩器,而不是像y一样使用每个三元组的解决方案这里有。我需要压缩3个浮点数。我不熟悉替代解决方案。“我假设我可以使用类型转换将浮点数转换为整数,并对这些整数执行Morton排序”--可以。我想你实际上是在看指数、尾数、符号的位置。我觉得,如果你的动态范围很大,它可能不会给出你想要的结果。如果你的动态范围足够小,也许你可以做一个线性标度,从
float
int64
。我是基本的ally试图将3个数字塞进一个数字中,然后将它们解码回来。我这样做是为了节省内存,我认为Morton order可能是最简单的解决方案。如果你想做大量的数字,最好在整个数据流中使用更通用的压缩器,而不是像y一样使用每个三元组的解决方案这里有。我需要压缩3个浮点数。我不熟悉其他解决方案。
double x=-1.123456789123456E205;
int64_t i = reinterpret_cast<int64_t &>(x);
cout<<i<<endl;
output >>> i = -1548698869907112442
double y = reinterpret_cast<double &>(i);
cout<<setprecision(16)<<y<<endl;
output>>-1.123456789123456e+205
#include <iostream>
#include <stdint.h>
#include<iomanip>

using namespace std;

uint64_t code_2D_M(double xd,double yd){

uint64_t x = reinterpret_cast<uint64_t& >(xd);
uint64_t y = reinterpret_cast<uint64_t& >(yd);

x = (x | (x << 16)) & 0x0000FFFF0000FFFF;
x = (x | (x << 8)) & 0x00FF00FF00FF00FF;
x = (x | (x << 4)) & 0x0F0F0F0F0F0F0F0F;
x = (x | (x << 2)) & 0x3333333333333333;
x = (x | (x << 1)) & 0x5555555555555555;

y = (y | (y << 16)) & 0x0000FFFF0000FFFF;
y = (y | (y << 8)) & 0x00FF00FF00FF00FF;
y = (y | (y << 4)) & 0x0F0F0F0F0F0F0F0F;
y = (y | (y << 2)) & 0x3333333333333333;
y = (y | (y << 1)) & 0x5555555555555555;

return x | (y << 1);

}


uint64_t code_3D_M(double xd,double yd,double zd){


uint64_t x = reinterpret_cast<uint64_t& >(xd);
uint64_t y = reinterpret_cast<uint64_t& >(yd);
uint64_t z = reinterpret_cast<uint64_t& >(zd);

x = (x | (x << 16)) & 0x0000FFFF0000FFFF;
x = (x | (x << 8)) & 0x00FF00FF00FF00FF;
x = (x | (x << 4)) & 0x0F0F0F0F0F0F0F0F;
x = (x | (x << 2)) & 0x3333333333333333;
x = (x | (x << 1)) & 0x5555555555555555;

y = (y | (y << 16)) & 0x0000FFFF0000FFFF;
y = (y | (y << 8)) & 0x00FF00FF00FF00FF;
y = (y | (y << 4)) & 0x0F0F0F0F0F0F0F0F;
y = (y | (y << 2)) & 0x3333333333333333;
y = (y | (y << 1)) & 0x5555555555555555;

z = (y | (y << 16)) & 0x0000FFFF0000FFFF;
z = (y | (y << 8)) & 0x00FF00FF00FF00FF;
z = (y | (y << 4)) & 0x0F0F0F0F0F0F0F0F;
z = (y | (y << 2)) & 0x3333333333333333;
z = (y | (y << 1)) & 0x5555555555555555;

return x | (y << 1) | (z << 2);

}

double decode_M(uint64_t x)
{
    x = x & 0x5555555555555555;
    x = (x | (x >> 1)) & 0x3333333333333333;
    x = (x | (x >> 2)) & 0x0F0F0F0F0F0F0F0F;
    x = (x | (x >> 4)) & 0x00FF00FF00FF00FF;
    x = (x | (x >> 8)) & 0x0000FFFF0000FFFF;
    x = (x | (x >> 16)) & 0xFFFFFFFFFFFFFFFF;
    return reinterpret_cast<double& >(x);
}


int main (void){

uint64_t  mort;
double  x,y,z;

// test input
x=2.123456789123459E205;
y=1.789789123456129E205;
z=9.999999912345779E205;

// echo the input
cout<<setprecision(17)<<x<<endl;
cout<<setprecision(17)<<y<<endl;
cout<<setprecision(17)<<z<<endl;

// encode 2D case
mort = code_2D_M(x,y);
//decode and print the results to see if all was fine
cout<<setprecision(17)<<decode_M(mort>>0)<<endl;
cout<<setprecision(17)<<decode_M(mort>>1)<<endl;

// encode 3D case
mort = code_3D_M(x,y,z);
//decode and print the results to see if all was fine
cout<<setprecision(17)<<decode_M(mort>>0)<<endl;
cout<<setprecision(17)<<decode_M(mort>>1)<<endl;
cout<<setprecision(17)<<decode_M(mort>>2)<<endl;



return 0;
}