关于OpenCasCade 曲线均分的测试,显示效果用OSG

    抽空写了个曲线均分测试,把任意曲线平成分成长度相等的N段。仅此记录, 下面测试了三种,前2种是测试方案:
第一种是通过参数进行分割:分别获得曲线的 FirstParameter 和 LastParameter ,然后对参数进行分割,获得n个ui,并对每个ui调用D0(获得这个点的坐标值)或D1(获得这个点的坐标值和切向量)。这个方法的优点是,简单易行,好操作,缺点均分参数轴获得的曲线的分割并不是均匀的,在OSG中显示如下:

1623451686@qq.com

测试代码如下:
////////////////////////////////////////////////////参数对曲线分割方法///////////////////////////////////////////////////////////////////

TColgp_Array1OfPnt Points1(1, 4);

Points1.SetValue(1, gp_Pnt(2, 0, 0));

Points1.SetValue(2, gp_Pnt(4, 0, 0));

Points1.SetValue(3, gp_Pnt(5, 6, 0));

Points1.SetValue(4, gp_Pnt(6, 3, 0));

GeomAPI_PointsToBSpline PTBS1(Points1);

Handle(Geom_BSplineCurve) curve = PTBS1.Curve();

Standard_Integer splitN = 10;  

Standard_Real firstParam = curve->FirstParameter();

Standard_Real lastParam = curve->LastParameter();

//

TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge(curve).Edge();

Handle(AIS_Shape) red = new AIS_Shape(anEdge);

TopoDS_Shape Sh1 = red->Shape();

buildCurve(Sh1);  //显示曲线

//

osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array();

osg::ref_ptr<osg::Geometry> keyPointGeom = new osg::Geometry();

osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;

osg::ref_ptr<osg::Geode> geode = new osg::Geode();

    //

for (Standard_Integer i = 0; i<splitN + 1; ++i)

{

Standard_Real ui = (i * (lastParam - firstParam) / splitN);

ui = firstParam + ui;

gp_Pnt pi;

gp_Vec veci;

curve->D1(ui, pi, veci);

vertices->push_back(osg::Vec3(pi.X(), pi.Y(), pi.Z()));

colors->push_back(osg::Vec4(1.0, 1.0, 0.0, 1.0));

}

if (vertices->size() > 0)

{

keyPointGeom->setVertexArray(vertices.get());

keyPointGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS, 0, vertices->size()));

keyPointGeom->setColorArray(colors, osg::Array::BIND_OVERALL);

osg::ref_ptr<osg::Point> pointSize = new osg::Point;

pointSize->setSize(7.5);

keyPointGeom->getOrCreateStateSet()->setAttribute(pointSize);

keyPointGeom->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED);

geode->addDrawable(keyPointGeom);

m_osgViewer->getRoot()->addChild(geode);

}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

第二种是直接对曲线分割:通过类 GCPnts_UniformAbscissa 实现对一个Geom_Curve对象的平均分割,获得分割点的坐标及对应的参数,在OSG中显示如下:

测试代码如下:
////////////////////////////////////////////////////直接对曲线分割方法///////////////////////////////////////////////////////////////////

TColgp_Array1OfPnt Points1(1, 4);

Points1.SetValue(1, gp_Pnt(2, 0, 0));

Points1.SetValue(2, gp_Pnt(4, 0, 0));

Points1.SetValue(3, gp_Pnt(5, 6, 0));

Points1.SetValue(4, gp_Pnt(6, 3, 0));

GeomAPI_PointsToBSpline PTBS1(Points1);

Handle(Geom_BSplineCurve) curve = PTBS1.Curve();

Standard_Integer splitN = 10;

GeomAdaptor_Curve GAC(curve);

GCPnts_UniformAbscissa UA(GAC, splitN + 1);

Standard_Real* params = new Standard_Real[splitN + 1];

//显示曲线

TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge(curve).Edge();

Handle(AIS_Shape) red = new AIS_Shape(anEdge);

TopoDS_Shape Sh1 = red->Shape();

buildCurve(Sh1);

//osg 

osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array();

osg::ref_ptr<osg::Geometry> keyPointGeom = new osg::Geometry();

osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;

osg::ref_ptr<osg::Geode> geode = new osg::Geode();

//

if (UA.IsDone())

{

Standard_Real n = UA.NbPoints();

Standard_Integer index = 0;

for (; index<n + 1; ++index)

{

Standard_Real parami = UA.Parameter(index + 1);

params[index] = parami;

gp_Pnt tpnt;

curve->D0(parami, tpnt);

vertices->push_back(osg::Vec3(tpnt.X(), tpnt.Y(), tpnt.Z()));

colors->push_back(osg::Vec4(1.0, 1.0, 0.0, 1.0));

}

}

if (vertices->size() > 0)

{

keyPointGeom->setVertexArray(vertices.get());

keyPointGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS, 0, vertices->size()));

keyPointGeom->setColorArray(colors, osg::Array::BIND_OVERALL);

osg::ref_ptr<osg::Point> pointSize = new osg::Point;

pointSize->setSize(7.5);

keyPointGeom->getOrCreateStateSet()->setAttribute(pointSize);

keyPointGeom->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED);

geode->addDrawable(keyPointGeom);

m_osgViewer->getRoot()->addChild(geode);

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

最后,测试下Topds_edge ,绘制一个椭圆测试,第二种方案均分效果如下:

总结:使用直接对曲线分割方法能获得更好的效果。  如有错误,请联系我修改,谢谢! qq:1623451686

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值