MFC绘制高次B样条曲线

MFC绘制高次B样条曲线

MFC绘制高次B样条曲线_第1张图片
随着B样条的绘制次数越高,曲线连续性越高,光滑性越好,但是逼近程度变差,局部性质变差,工程中一般使用2、3次B样条。
图示为1-8次的B样条曲线,红色依次加深,次数依次升高。

参考《计算几何算法与实现》–孔令德

实现代码:

#pragma once
#include"P2.h"

class High_Level_BSpline
{
public:
	High_Level_BSpline(void);
	~High_Level_BSpline(void);

public:
	//计算基函数
	 double BasicFunctionValue(double t, int i, int k);

	 //初始化控制点,节点矢量
	 void InitPoint();

	 //绘制样条线
	 void DrawCurve(CDC*pDC);

	 //绘制控制点
	 void DrawControlPoint(CDC*pDC);

	 //H-J法计算节点
	 void HJ_calculate_knot();

private:
	double knot[19];	//定义节点矢量数组
	int n;          //控制点个数减1
	int k;          //次数
	CP2 P[9];       //控制点坐标
};


#include "StdAfx.h"
#include "High_Level_BSpline.h"
#include"math.h"

#define ROUND(d) int(d+0.5)//四舍五入宏定义

High_Level_BSpline::High_Level_BSpline(void)
{
	InitPoint();
}


High_Level_BSpline::~High_Level_BSpline(void)
{
}

/*----------------------------------------------------
input   : t, i, k分别为参数值,支撑区间左端节点的下标,曲线次数
function: 计算第i个k次B样条基函数值
-----------------------------------------------------*/

double High_Level_BSpline::BasicFunctionValue(double t, int i, int k)
{
	//次数为0时的基函数值
	if(k==0)
	{
		if(t>=knot[i]&&t<knot[i+1])
			return 1.0;
		else 
			return 0.0;
	}

	//次数不为0时的基函数计算
	double value1, value2, value;
	if(k>0)
	{
		//节点矢量位于定义域之外
		if(t<knot[i]||t>knot[i+k+1])
			return 0.0;

		//节点矢量位于定义域之外
		else
		{
			double coff1, coff2;    //系数
			double denominator=0.0; //分母

			//递推公式第一项
			denominator=knot[i+k]-knot[i];
			if(denominator==0.0)
				coff1=0.0;                  //重节点分母为0
			else
				coff1=(t-knot[i])/denominator;

			//递推公式第二项
			denominator=knot[i+k+1]-knot[i+1];
			if(denominator==0.0)
				coff2=0.0;                  //重节点分母为0
			else
				coff2=(knot[i+k+1]-t)/denominator;

			//递推公式第一、二项的值
			value1=coff1*BasicFunctionValue(t,i,k-1);
			value2=coff2*BasicFunctionValue(t,i+1,k-1);

			value=value1+value2;
		}
	}
	return value;
}

/*----------------------------------------------------
input   : 
function: 初始化节点与控制点
-----------------------------------------------------*/
void High_Level_BSpline::InitPoint()
{
	n=8;

	//控制点初始化
	P[0].x=150, P[0].y=300;
	P[1].x=-150, P[1].y=300;
	P[2].x=-150, P[2].y=0;
	P[3].x=150, P[3].y=0;
	P[4].x=150, P[4].y=-300;
	P[5].x=-150, P[5].y=-300;
	P[6]=P[2];
	P[7]=P[3];
	P[8]=P[0];
}

/*----------------------------------------------------
input   : 
function: 绘制曲线
-----------------------------------------------------*/
void High_Level_BSpline::DrawCurve(CDC*pDC)
{
	//绘制控制点
	DrawControlPoint(pDC);

	for(k=1; k<9; k++)  //order为曲线次数
	{
		//节点计算
		HJ_calculate_knot();

		//创建并选取画笔
		CPen NewPen, *pOldPen;
		NewPen.CreatePen(PS_SOLID,2,RGB(k*30,0,0));//颜色依次变红
		pOldPen=pDC->SelectObject(&NewPen);

		//等距取参数点计算值
		double tStep=0.01;
		for(double t=0.0; t<=1.0; t+=tStep)
		{
			CP2 p(0,0);//离散点
			for(int i=0; i<=n; i++)
			{
				double BValue=BasicFunctionValue(t, i, k);
				p+=P[i]*BValue;
			}
			if(t==0)
				pDC->MoveTo(ROUND(p.x), ROUND(p.y));
			else
				pDC->LineTo(ROUND(p.x), ROUND(p.y));
		}

		//还原画笔,删除创建的画笔
		pDC->SelectObject(pOldPen);	
		NewPen.DeleteObject();      
	}
}

/*----------------------------------------------------
input   : 
function: 绘制控制点
-----------------------------------------------------*/
void High_Level_BSpline::DrawControlPoint(CDC*pDC)
{
	//初始化画笔
	CPen NewPen,*pOldPen;
	NewPen.CreatePen(PS_SOLID,3,RGB(0,0,0));
	pOldPen=pDC->SelectObject(&NewPen);

	//初始化画刷
	CBrush NewBrush,*pOldBrush;
	NewBrush.CreateSolidBrush(RGB(0,0,0));
	pOldBrush=pDC->SelectObject(&NewBrush);

	//加粗控制点
	for(int i=0;i<=n;i++)
	{
		if(0==i)
		{
			pDC->MoveTo(ROUND(P[i].x),ROUND(P[i].y));
			pDC->Ellipse(ROUND(P[i].x)-5,ROUND(P[i].y)-5,ROUND(P[i].x)+5,ROUND(P[i].y)+5);
		}
		else
		{
			pDC->LineTo(ROUND(P[i].x),ROUND(P[i].y));
			pDC->Ellipse(ROUND(P[i].x)-5,ROUND(P[i].y)-5,ROUND(P[i].x)+5,ROUND(P[i].y)+5);
		}
	}

	//还原画笔画刷
	pDC->SelectObject(pOldBrush);
	pDC->SelectObject(pOldPen);
	NewPen.DeleteObject(); 
	NewBrush.DeleteObject(); 
}

/*----------------------------------------------------
input   : 
function:哈特利-贾德法计算非均匀节点
-----------------------------------------------------*/
void High_Level_BSpline::HJ_calculate_knot(void)
{
	//两端点取K+1个重复度
	for(int i=0; i<=k; i++)
		knot[i]=0.0;                 
	for(int i=n+1; i<=n+k+1; i++)
		knot[i]=1.0;
	
	//计算n-k个内节点
	for(int i=k+1; i<=n; i++)
	{
		double sum=0.0;

		for(int j=k+1; j<=i; j++)
		{
			//计算分子项
			double numerator=0.0;
			for(int loop=j-k; loop<=j-1; loop++)
			{
				numerator+=sqrt((P[loop].x-P[loop-1].x)*(P[loop].x-P[loop-1].x)
		                      +(P[loop].y-P[loop-1].y)*(P[loop].y-P[loop-1].y));
			}

			//计算分母项
			double denominator=0.0;
			for(int loop1=k+1; loop1<=n+1; loop1++)
			{
				for(int loop2=loop1-k; loop2<=loop1-1; loop2++)
				{
					denominator+=sqrt((P[loop2].x-P[loop2-1].x)*(P[loop2].x-P[loop2-1].x)
		                           +(P[loop2].y-P[loop2-1].y)*(P[loop2].y-P[loop2-1].y));
				}
			}
			sum+=numerator/denominator;
		}
		knot[i]=sum;
	}
}


你可能感兴趣的:(计算几何算法与实现,计算几何,C++,MFC)