资源简介
生成经过首尾节点的三次均匀B样条曲线,代码简单,适合初学者
代码片段和文件信息
#include “stdafx.h“
#include “BSpline.h“
#include
BSpline::BSpline() : _degree(3)
{
}
BSpline::~BSpline()
{
}
double BSpline::EvalBasis(double u int i int degree) const
{
if (degree == 0)
{
if (u >= _knots[i] && u < _knots[i + 1])
{
return 1.0;
}
else
{
return 0.0;
}
}
double l0 = u - _knots[i];
double l1 = _knots[i + degree] - _knots[i];
double l2 = Divide(l0 l1);
double k0 = _knots[i + degree + 1] - u;
double k1 = _knots[i + degree + 1] - _knots[i + 1];
double k2 = Divide(k0 k1);
double res = l2 * EvalBasis(u i degree - 1) + k2 * EvalBasis(u i + 1 degree - 1);
return res;
}
double BSpline::Divide(double x double y) const
{
if (y == 0.0)
{
return 0.0;
}
return x / y;
}
void BSpline::Add(osg::Vec3d vertex)
{
_vertexs.push_back(vertex);
}
osg::Vec3dArray* BSpline::Generate(int iStep) const
{
if (_vertexs.size() < 4)
{
return NULL;
}
InitKnots();
int iLen = _knots.size();
double dStart = _knots[0];
double dEnd = _knots[iLen - 1];
double dDelta = (dEnd - dStart) / iStep;
osg::ref_ptr v3dArray = new osg::Vec3dArray;
for (int s = 0; s <= iStep; s++)
{
double u = s * dDelta;
osg::Vec3d v;
double sum = 0.0;
for (int i = 0; i < _vertexs.size(); i++)
{
double t = EvalBasis(u i _degree);
v += _vertexs.at(i) * t;
sum += t;
}
if (v != osg::Vec3d())
{
v3dArray->push_back(v);
}
}
return v3dArray.release();
}
void BSpline::InitKnots() const
{
std::size_t count = _vertexs.size();//_vertexs是控制点数组
for (int i = 0; i < _degree; i++)//degress表示次数这里为3
{
_knots.push_back(0.0); //前p个0
}
double dParam = 0.0;
for (std::size_t i = 0; i < count - _degree + 1; i++)//这里如果是0则dParm = 0也就是说前p+1个节点为0
{
if (i > 0)
dParam += 1.0;
_knots.push_back(dParam);
}
for (std::size_t i = 0; i < _degree; i++)//这里是后p个等于最大实际上倒数第p+1个也是最大
{
_knots.push_back(dParam); //后p个 0
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 2091 2016-11-03 17:40 BSpline.cpp
文件 552 2016-11-03 17:40 BSpline.h
----------- --------- ---------- ----- ----
2643 2
- 上一篇:downloadfrompan.txt
- 下一篇:电流保护PSCAD
评论
共有 条评论