向心Catmull-Rom样条插值alpha参数

时间:2018-07-16 13:11:38

标签: c++ interpolation spline catmull-rom-curve

在搜索了有关Catmull-Rom样条的几乎所有主题并最终成功实现后,我陷入了一个不知道自己是否犯了逻辑错误或代码是否完全错误的问题。

我的问题是,我的代码将不接受向心catmull-rom样条插值的alpha参数。具有不同alpha值的结果始终相同。

我的主要目标是使用向心catmull-rom算法插值4点(当我将第一点和最后一点加倍时为6点)。 我基本上实现了C#版本,可以在这里(Centripetal Catmull-Rom Spline)或整个SO中的其他几个线程(Catmull-rom curve with no cusps and no self-intersectionsCatmull-Rom interpolation on SVG Paths)中实现。

我的实现:

首先,我将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,则结果与预期的一样(我在Wikipedia示例中使用了类似的观点)。我在顶部有一个很好的自交点。但是,如果将值更改为0.5或1,我仍然会得到相同的结果。

现在我怀疑我使用的类型有问题。将整数坐标转换为双精度或将其转换回整数(+0.5)可能是不明智的。但这不能解释我得到的自相交。 我实际上没有提供图像,因为它与12中的图像非常相似。 感谢所有甚至考虑阅读此书的人。

1 个答案:

答案 0 :(得分:0)

看了几个小时的代码后,我终于发现了我的错误。我的代码在计算 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