向心 Catmull-Rom 样条插值 alpha 参数
Centripetal Catmull-Rom spline interpolation alpha parameter
在搜索了几乎所有关于 Catmull-Rom 样条的主题并最终成功实施之后,我现在陷入了一个我不知道是我犯了逻辑错误还是我的代码完全错误的地步。
我的问题是,我的代码不接受向心 catmull-rom 样条插值的 alpha 参数。具有不同 alpha 值的结果始终相同。
我的主要目标是使用向心 catmull-rom 算法插入 4 个点(当我将第一个和最后一个点加倍时为 6 个)。
我基本上实现了可以在此处找到的 C# 版本 (Centripetal Catmull-Rom Spline) or also in a couple of other threads throughout SO (Catmull-rom curve with no cusps and no self-intersections, )。
我的实现:
首先,我有一个 3D 图像,其中 4 个不同的体素设置为一个。我尝试识别那些体素并保存坐标。例如 z=250、y=100、x=323。我保存这些值并将它们转换为双精度值,以将它们用作我的 CR 算法的控制点。
for(int nx=0; nx<Nx; ++nx)
for(int ny=0; ny<Ny; ++ny)
for(int nz=0; nz<Nz; ++nz)
{
if(TempArray[nz*Ny*Nx+ny*Nx+nx] > 0)
{
Coordinates[counter*3+0] = nz; // +0 = z0
Coordinates[counter*3+1] = ny; // +1 = y0
Coordinates[counter*3+2] = nx; // +2 = x0
++counter;
}
}
澄清一下。阵列具有以下布局:
坐标[z0,y0,x0,z1,y1,x1,...,zn,yn,xn]。
我的catamull-rom插值实现如下:
double P0z, P0y, P0x, P1z, P1y, P1x, P2z, P2y, P2x, P3z, P3y, P3x;
P0z = Coordinates[n*3+0];
P1z = Coordinates[n*3+3];
P2z = Coordinates[n*3+6];
P3z = Coordinates[n*3+9];
P0y = Coordinates[n*3+1];
P1y = Coordinates[n*3+4];
P2y = Coordinates[n*3+7];
P3y = Coordinates[n*3+10];
P0x = Coordinates[n*3+2];
P1x = Coordinates[n*3+5];
P2x = Coordinates[n*3+8];
P3x = Coordinates[n*3+11];
double disAxBx, disAyBy, disAzBz; disAzBz=disAyBy=disAxBx=0.0;
disAzBz = pow( P1z - P0z, 2 );
disAyBy = pow( P1y - P0y, 2 );
disAxBx = pow( P1x - P0x, 2 );
double distanceVec = pow( disAzBz + disAyBy + disAxBx, 0.5 );
t0 = 0.0;
t1 = pow( distanceVec, alpha) + t0; // This is the part where the alpha comes in
t2 = pow( distanceVec, alpha) + t1;
t3 = pow( distanceVec, alpha) + t2;
for ( double t=t1; t<t2; t+= (t2-t1)/(NumberOfPoints) ) // t starts at t1 and runs till t2 (like in the examples)
{
z=y=x=0;
A1[0] = ( ( (t1-t)/(t1-t0) ) * P0z ) + ( ( (t-t0)/(t1-t0) ) * P1z );
A1[1] = ( ( (t1-t)/(t1-t0) ) * P0y ) + ( ( (t-t0)/(t1-t0) ) * P1y );
A1[2] = ( ( (t1-t)/(t1-t0) ) * P0x ) + ( ( (t-t0)/(t1-t0) ) * P1x );
A2[0] = ( ( (t2-t)/(t2-t1) ) * P1z ) + ( ( (t-t1)/(t2-t1) ) * P2z );
A2[1] = ( ( (t2-t)/(t2-t1) ) * P1y ) + ( ( (t-t1)/(t2-t1) ) * P2y );
A2[2] = ( ( (t2-t)/(t2-t1) ) * P1x ) + ( ( (t-t1)/(t2-t1) ) * P2x );
A3[0] = ( ( (t3-t)/(t3-t2) ) * P2z ) + ( ( (t-t2)/(t3-t2) ) * P3z );
A3[1] = ( ( (t3-t)/(t3-t2) ) * P2y ) + ( ( (t-t2)/(t3-t2) ) * P3y );
A3[2] = ( ( (t3-t)/(t3-t2) ) * P2x ) + ( ( (t-t2)/(t3-t2) ) * P3x );
B1[0] = ( ( (t2-t)/(t2-t0) ) * A1[0] ) + ( ( (t-t0)/(t2-t0) ) * A2[0] );
B1[1] = ( ( (t2-t)/(t2-t0) ) * A1[1] ) + ( ( (t-t0)/(t2-t0) ) * A2[1] );
B1[2] = ( ( (t2-t)/(t2-t0) ) * A1[2] ) + ( ( (t-t0)/(t2-t0) ) * A2[2] );
B2[0] = ( ( (t3-t)/(t3-t1) ) * A2[0] ) + ( ( (t-t1)/(t3-t1) ) * A3[0] );
B2[1] = ( ( (t3-t)/(t3-t1) ) * A2[1] ) + ( ( (t-t1)/(t3-t1) ) * A3[1] );
B2[2] = ( ( (t3-t)/(t3-t1) ) * A2[2] ) + ( ( (t-t1)/(t3-t1) ) * A3[2] );
C[0] =int ( ( ( (t2-t)/(t2-t1) ) * B1[0] ) + ( ( (t-t1)/(t2-t1) ) * B2[0] ) +0.5 );
C[1] =int ( ( ( (t2-t)/(t2-t1) ) * B1[1] ) + ( ( (t-t1)/(t2-t1) ) * B2[1] ) +0.5 );
C[2] =int ( ( ( (t2-t)/(t2-t1) ) * B1[2] ) + ( ( (t-t1)/(t2-t1) ) * B2[2] ) +0.5 );
z=C[0]; y=C[1]; x=C[2];
OutputArray[z*Nx*Ny+y*Nx+x] = 1.f;
}
} // Loop over 4 consecutive points
现在我尝试做的是,将每个体素(或2D中的像素)设置为1,这是通过CR插值方法计算的。我基本上想用CR算法计算坐标。如果我设置 alpha=0,结果如预期的那样(我使用了与维基百科示例相似的点)。我在顶部得到了一个很好的自交点。但如果我将值更改为 0.5 或 1,我仍然会得到相同的结果。
现在我怀疑我使用的类型有问题。将整数坐标转换为双精度或将它们转换回整数(+0.5)可能是不明智的。但这并不能解释我得到的自相交。
我实际上没有提供图像,因为它与我们在 1 and 2 中的图像非常相似。
感谢所有考虑阅读本文的人。
查看代码几个小时后,我终于发现了我的错误。我的代码在计算 t2 和 t3 值时完全错误。查看 t (Centripetal Catmull-Rom Spline) 的公式提供了解决方案。
t2 和 t3 取决于 |P2 - P1|分别在 |P3 - P2| 上。我将 t1 的值也放在 t2 和 t3 上,因此无论使用哪个 alpha,都会导致 CR 算法得到相同的统一结果。这也是有道理的,为什么它适用于 alpha = 0.
我按如下方式更改了代码,现在我得到了正确的结果:
disAzBz0 = pow( P1z - P0z, 2 ); disAzBz1 = pow( P2z - P1z, 2 ); disAzBz2 = pow( P3z - P2z, 2 ); // Value for distance calculation for P0 and P1
disAyBy0 = pow( P1y - P0y, 2 ); disAyBy1 = pow( P2y - P1y, 2 ); disAyBy2 = pow( P3y - P2y, 2 ); // Value for distance calculation for P1 and P2
disAxBx0 = pow( P1x - P0x, 2 ); disAxBx1 = pow( P2x - P1x, 2 ); disAxBx2 = pow( P3x - P2x, 2 ); // Value for distance calculation for P2 and P3
double distanceVec01 = pow( disAzBz0 + disAyBy0 + disAxBx0, 0.5 ); // Distance P0-P1
double distanceVec12 = pow( disAzBz1 + disAyBy1 + disAxBx1, 0.5 ); // Distance P1-P2
double distanceVec23 = pow( disAzBz2 + disAyBy2 + disAxBx2, 0.5 ); // Distance P2-P3
t0 = 0.0;
t1 = pow( distanceVec01, alpha) + t0; // This was right in the code above
t2 = pow( distanceVec12, alpha) + t1; // Here was the mistake. I used distanceVec01 instead of distanceVec12
t3 = pow( distanceVec23, alpha) + t2; // Same here
在搜索了几乎所有关于 Catmull-Rom 样条的主题并最终成功实施之后,我现在陷入了一个我不知道是我犯了逻辑错误还是我的代码完全错误的地步。
我的问题是,我的代码不接受向心 catmull-rom 样条插值的 alpha 参数。具有不同 alpha 值的结果始终相同。
我的主要目标是使用向心 catmull-rom 算法插入 4 个点(当我将第一个和最后一个点加倍时为 6 个)。
我基本上实现了可以在此处找到的 C# 版本 (Centripetal Catmull-Rom Spline) or also in a couple of other threads throughout SO (Catmull-rom curve with no cusps and no self-intersections,
我的实现:
首先,我有一个 3D 图像,其中 4 个不同的体素设置为一个。我尝试识别那些体素并保存坐标。例如 z=250、y=100、x=323。我保存这些值并将它们转换为双精度值,以将它们用作我的 CR 算法的控制点。
for(int nx=0; nx<Nx; ++nx)
for(int ny=0; ny<Ny; ++ny)
for(int nz=0; nz<Nz; ++nz)
{
if(TempArray[nz*Ny*Nx+ny*Nx+nx] > 0)
{
Coordinates[counter*3+0] = nz; // +0 = z0
Coordinates[counter*3+1] = ny; // +1 = y0
Coordinates[counter*3+2] = nx; // +2 = x0
++counter;
}
}
澄清一下。阵列具有以下布局: 坐标[z0,y0,x0,z1,y1,x1,...,zn,yn,xn]。
我的catamull-rom插值实现如下:
double P0z, P0y, P0x, P1z, P1y, P1x, P2z, P2y, P2x, P3z, P3y, P3x;
P0z = Coordinates[n*3+0];
P1z = Coordinates[n*3+3];
P2z = Coordinates[n*3+6];
P3z = Coordinates[n*3+9];
P0y = Coordinates[n*3+1];
P1y = Coordinates[n*3+4];
P2y = Coordinates[n*3+7];
P3y = Coordinates[n*3+10];
P0x = Coordinates[n*3+2];
P1x = Coordinates[n*3+5];
P2x = Coordinates[n*3+8];
P3x = Coordinates[n*3+11];
double disAxBx, disAyBy, disAzBz; disAzBz=disAyBy=disAxBx=0.0;
disAzBz = pow( P1z - P0z, 2 );
disAyBy = pow( P1y - P0y, 2 );
disAxBx = pow( P1x - P0x, 2 );
double distanceVec = pow( disAzBz + disAyBy + disAxBx, 0.5 );
t0 = 0.0;
t1 = pow( distanceVec, alpha) + t0; // This is the part where the alpha comes in
t2 = pow( distanceVec, alpha) + t1;
t3 = pow( distanceVec, alpha) + t2;
for ( double t=t1; t<t2; t+= (t2-t1)/(NumberOfPoints) ) // t starts at t1 and runs till t2 (like in the examples)
{
z=y=x=0;
A1[0] = ( ( (t1-t)/(t1-t0) ) * P0z ) + ( ( (t-t0)/(t1-t0) ) * P1z );
A1[1] = ( ( (t1-t)/(t1-t0) ) * P0y ) + ( ( (t-t0)/(t1-t0) ) * P1y );
A1[2] = ( ( (t1-t)/(t1-t0) ) * P0x ) + ( ( (t-t0)/(t1-t0) ) * P1x );
A2[0] = ( ( (t2-t)/(t2-t1) ) * P1z ) + ( ( (t-t1)/(t2-t1) ) * P2z );
A2[1] = ( ( (t2-t)/(t2-t1) ) * P1y ) + ( ( (t-t1)/(t2-t1) ) * P2y );
A2[2] = ( ( (t2-t)/(t2-t1) ) * P1x ) + ( ( (t-t1)/(t2-t1) ) * P2x );
A3[0] = ( ( (t3-t)/(t3-t2) ) * P2z ) + ( ( (t-t2)/(t3-t2) ) * P3z );
A3[1] = ( ( (t3-t)/(t3-t2) ) * P2y ) + ( ( (t-t2)/(t3-t2) ) * P3y );
A3[2] = ( ( (t3-t)/(t3-t2) ) * P2x ) + ( ( (t-t2)/(t3-t2) ) * P3x );
B1[0] = ( ( (t2-t)/(t2-t0) ) * A1[0] ) + ( ( (t-t0)/(t2-t0) ) * A2[0] );
B1[1] = ( ( (t2-t)/(t2-t0) ) * A1[1] ) + ( ( (t-t0)/(t2-t0) ) * A2[1] );
B1[2] = ( ( (t2-t)/(t2-t0) ) * A1[2] ) + ( ( (t-t0)/(t2-t0) ) * A2[2] );
B2[0] = ( ( (t3-t)/(t3-t1) ) * A2[0] ) + ( ( (t-t1)/(t3-t1) ) * A3[0] );
B2[1] = ( ( (t3-t)/(t3-t1) ) * A2[1] ) + ( ( (t-t1)/(t3-t1) ) * A3[1] );
B2[2] = ( ( (t3-t)/(t3-t1) ) * A2[2] ) + ( ( (t-t1)/(t3-t1) ) * A3[2] );
C[0] =int ( ( ( (t2-t)/(t2-t1) ) * B1[0] ) + ( ( (t-t1)/(t2-t1) ) * B2[0] ) +0.5 );
C[1] =int ( ( ( (t2-t)/(t2-t1) ) * B1[1] ) + ( ( (t-t1)/(t2-t1) ) * B2[1] ) +0.5 );
C[2] =int ( ( ( (t2-t)/(t2-t1) ) * B1[2] ) + ( ( (t-t1)/(t2-t1) ) * B2[2] ) +0.5 );
z=C[0]; y=C[1]; x=C[2];
OutputArray[z*Nx*Ny+y*Nx+x] = 1.f;
}
} // Loop over 4 consecutive points
现在我尝试做的是,将每个体素(或2D中的像素)设置为1,这是通过CR插值方法计算的。我基本上想用CR算法计算坐标。如果我设置 alpha=0,结果如预期的那样(我使用了与维基百科示例相似的点)。我在顶部得到了一个很好的自交点。但如果我将值更改为 0.5 或 1,我仍然会得到相同的结果。
现在我怀疑我使用的类型有问题。将整数坐标转换为双精度或将它们转换回整数(+0.5)可能是不明智的。但这并不能解释我得到的自相交。 我实际上没有提供图像,因为它与我们在 1 and 2 中的图像非常相似。 感谢所有考虑阅读本文的人。
查看代码几个小时后,我终于发现了我的错误。我的代码在计算 t2 和 t3 值时完全错误。查看 t (Centripetal Catmull-Rom Spline) 的公式提供了解决方案。
t2 和 t3 取决于 |P2 - P1|分别在 |P3 - P2| 上。我将 t1 的值也放在 t2 和 t3 上,因此无论使用哪个 alpha,都会导致 CR 算法得到相同的统一结果。这也是有道理的,为什么它适用于 alpha = 0.
我按如下方式更改了代码,现在我得到了正确的结果:
disAzBz0 = pow( P1z - P0z, 2 ); disAzBz1 = pow( P2z - P1z, 2 ); disAzBz2 = pow( P3z - P2z, 2 ); // Value for distance calculation for P0 and P1
disAyBy0 = pow( P1y - P0y, 2 ); disAyBy1 = pow( P2y - P1y, 2 ); disAyBy2 = pow( P3y - P2y, 2 ); // Value for distance calculation for P1 and P2
disAxBx0 = pow( P1x - P0x, 2 ); disAxBx1 = pow( P2x - P1x, 2 ); disAxBx2 = pow( P3x - P2x, 2 ); // Value for distance calculation for P2 and P3
double distanceVec01 = pow( disAzBz0 + disAyBy0 + disAxBx0, 0.5 ); // Distance P0-P1
double distanceVec12 = pow( disAzBz1 + disAyBy1 + disAxBx1, 0.5 ); // Distance P1-P2
double distanceVec23 = pow( disAzBz2 + disAyBy2 + disAxBx2, 0.5 ); // Distance P2-P3
t0 = 0.0;
t1 = pow( distanceVec01, alpha) + t0; // This was right in the code above
t2 = pow( distanceVec12, alpha) + t1; // Here was the mistake. I used distanceVec01 instead of distanceVec12
t3 = pow( distanceVec23, alpha) + t2; // Same here