B-样条插值就可以。 处理时将多边形点首尾重叠。之后还可以计算交点,取出相交闭合的部分的点集 /* B-Spline B样条曲线 *//* class xyz_class { public: xyz_class(float ix=0, float iy=0, float iz=0) { x=ix; y=iy; z=iz; }public: float x; float y; float z; };*///typedef class xyz_class XYZ; /* Calculate the blending value, this is done recursively.
If the numerator and denominator are 0 the expression is 0. If the deonimator is 0 the expression is 0 */ double SplineBlend(int k,int t,int *u,double v) { double value; if (t == 1) { if ((u[k] <= v) && (v < u[k+1])) value = 1; else value = 0; } else { if ((u[k+t-1] == u[k]) && (u[k+t] == u[k+1])) value = 0; else if (u[k+t-1] == u[k]) value = (u[k+t] - v) / (u[k+t] - u[k+1]) * SplineBlend(k+1,t-1,u,v); else if (u[k+t] == u[k+1]) value = (v - u[k]) / (u[k+t-1] - u[k]) * SplineBlend(k,t-1,u,v); else value = (v - u[k]) / (u[k+t-1] - u[k]) * SplineBlend(k,t-1,u,v) + (u[k+t] - v) / (u[k+t] - u[k+1]) * SplineBlend(k+1,t-1,u,v); } return(value); } /* This returns the point "output" on the spline curve. The parameter "v" indicates the position, it ranges from 0 to n-t+2
*/ void SplinePoint(int *u,int n,int t,double v,XYZ *control,XYZ *output) { int k; double b; output->x = 0; output->y = 0; output->z = 0; for (k=0;k<=n;k++) { b = SplineBlend(k,t,u,v); output->x += (float)(control[k].x * b); output->y += (float)(control[k].y * b); output->z += (float)(control[k].z * b); } }/* The positions of the subintervals of v and breakpoints, the position on the curve are called knots. Breakpoints can be uniformly defined by setting u[j] = j, a more useful series of breakpoints are defined by the function below. This set of breakpoints localises changes to the vicinity of the control point being modified. */ void SplineKnots(int *u,int n,int t) { int j; for (j=0;j<=n+t;j++) { if (j < t) u[j] = 0; else if (j <= n) u[j] = j - t + 1; else if (j > n) u[j] = n - t + 2; } }/*------------------------------------------------------------------------- Create all the points along a spline curve Control points "inp", "n" of them. Knots "knots", degree "t". Ouput curve "outp", "res" of them. 调用例子: // Example of how to call the spline functions // Basically one needs to create the control points, then compute // the knot positions, then calculate points along the curve.
int i; int N = pIndex-1; //请求内插后的点数 #define RESOLUTION 200 XYZ outp[RESOLUTION];
//输入点 XYZ inp[1000]; // inp[N+1] = {0.0,0.0,0.0, 1.0,0.0,3.0, 2.0,0.0,1.0, 4.0,0.0,4.0}; int T = 3; //T的取值为3或4。
int knots[1000+3+1]; for (i=0; i<=N; i++) { inp[i].x = pDoc->xSel[i]; inp[i].y = 0; inp[i].z = pDoc->ySel[i]; } SplineKnots(knots,N,T); SplineCurve(inp,N,knots,T,outp,RESOLUTION); //* Display the curve, in this case in OOGL format for GeomView *
处理时将多边形点首尾重叠。之后还可以计算交点,取出相交闭合的部分的点集
/*
B-Spline B样条曲线
*//*
class xyz_class
{
public:
xyz_class(float ix=0, float iy=0, float iz=0)
{
x=ix; y=iy; z=iz;
}public:
float x;
float y;
float z;
};*///typedef class xyz_class XYZ;
/*
Calculate the blending value, this is done recursively.
If the numerator and denominator are 0 the expression is 0.
If the deonimator is 0 the expression is 0
*/
double SplineBlend(int k,int t,int *u,double v)
{
double value; if (t == 1)
{
if ((u[k] <= v) && (v < u[k+1]))
value = 1;
else
value = 0;
} else
{
if ((u[k+t-1] == u[k]) && (u[k+t] == u[k+1]))
value = 0;
else if (u[k+t-1] == u[k])
value = (u[k+t] - v) / (u[k+t] - u[k+1]) * SplineBlend(k+1,t-1,u,v);
else if (u[k+t] == u[k+1])
value = (v - u[k]) / (u[k+t-1] - u[k]) * SplineBlend(k,t-1,u,v);
else
value = (v - u[k]) / (u[k+t-1] - u[k]) * SplineBlend(k,t-1,u,v) +
(u[k+t] - v) / (u[k+t] - u[k+1]) * SplineBlend(k+1,t-1,u,v);
}
return(value);
}
/*
This returns the point "output" on the spline curve.
The parameter "v" indicates the position, it ranges from 0 to n-t+2
*/
void SplinePoint(int *u,int n,int t,double v,XYZ *control,XYZ *output)
{
int k;
double b; output->x = 0;
output->y = 0;
output->z = 0; for (k=0;k<=n;k++)
{
b = SplineBlend(k,t,u,v);
output->x += (float)(control[k].x * b);
output->y += (float)(control[k].y * b);
output->z += (float)(control[k].z * b);
}
}/*
The positions of the subintervals of v and breakpoints, the position
on the curve are called knots. Breakpoints can be uniformly defined
by setting u[j] = j, a more useful series of breakpoints are defined
by the function below. This set of breakpoints localises changes to
the vicinity of the control point being modified.
*/
void SplineKnots(int *u,int n,int t)
{
int j; for (j=0;j<=n+t;j++)
{
if (j < t)
u[j] = 0;
else if (j <= n)
u[j] = j - t + 1;
else if (j > n)
u[j] = n - t + 2;
}
}/*-------------------------------------------------------------------------
Create all the points along a spline curve
Control points "inp", "n" of them.
Knots "knots", degree "t".
Ouput curve "outp", "res" of them. 调用例子:
// Example of how to call the spline functions
// Basically one needs to create the control points, then compute
// the knot positions, then calculate points along the curve.
int i; int N = pIndex-1; //请求内插后的点数
#define RESOLUTION 200 XYZ outp[RESOLUTION];
//输入点
XYZ inp[1000];
// inp[N+1] = {0.0,0.0,0.0, 1.0,0.0,3.0, 2.0,0.0,1.0, 4.0,0.0,4.0};
int T = 3; //T的取值为3或4。
int knots[1000+3+1]; for (i=0; i<=N; i++)
{
inp[i].x = pDoc->xSel[i];
inp[i].y = 0;
inp[i].z = pDoc->ySel[i]; } SplineKnots(knots,N,T);
SplineCurve(inp,N,knots,T,outp,RESOLUTION); //* Display the curve, in this case in OOGL format for GeomView *
pDoc->nSel= pIndex + RESOLUTION;
for (i=0;i<RESOLUTION;i++)
{
pDoc->xSel[pIndex+i] = outp[i].x;
pDoc->ySel[pIndex+i] = outp[i].z;
}
*/
void SplineCurve(XYZ *inp,int n,int *knots,int t,XYZ *outp,int res)
{
int i;
double interval,increment; interval = 0;
increment = (n - t + 2) / (double)(res - 1);
for (i=0;i<res-1;i++)
{
SplinePoint(knots,n,t,interval,inp,&(outp[i]));
interval += increment;
}
outp[res-1] = inp[n];
}