一直对OpenCASACDE不满意的地方就是它的渲染,看起来确实不够专业。
前段时间一直想用OpenInventor渲染它的模型拓扑结构TopoDS_Shape,遇到了一个棘手的问题:如何将实体的任意Edge转化成OpenInventor可以识别的Nurbs曲线。
分析了良久还是没有结果,不知道是方法不当还是OpenCASCADE本身的Bug。
下面的代码是我写的转化代码:
bool TopeEdge2Curve(const TopoDS_Edge& topoEdge, SoSeparator* pRootNode)


{
TopLoc_Location topLoc;
Standard_Real fStart, fEnd;
Handle_Geom_Curve hCurve = BRep_Tool::Curve(topoEdge, topLoc, fStart, fEnd);

if(hCurve.IsNull())
return false;

Handle(Geom_TrimmedCurve) myTrimmed = new Geom_TrimmedCurve(hCurve, fStart, fEnd);

Handle_Geom_BSplineCurve hBSpline = GeomConvert::CurveToBSplineCurve(myTrimmed);
if(hBSpline.IsNull() )
return false;

gp_Trsf transfom;
Standard_Boolean bIdentity = true;
if(!topLoc.IsIdentity())

{
bIdentity = false;
transfom = topLoc.Transformation();
}



// control points
Standard_Integer nCtrlPts = hBSpline->NbPoles();

CString szControl;
SbVec3f* pts = new SbVec3f[nCtrlPts];
for(int ii=1; ii<= nCtrlPts; ++ii)

{
gp_Pnt pt = hBSpline->Pole(ii);
if(!bIdentity)
pt.Transform(transfom);

pts[ii-1] = SbVec3f(pt.X(), pt.Y(), pt.Z());
}

// knots
Standard_Integer nKnots = hBSpline->NbKnots();
float* knots = new float[nKnots];
for( int kk=1; kk <= nKnots; ++kk)

{
knots[kk-1] = hBSpline->Knot(kk);
}

SoSeparator *curveSep = new SoSeparator();
SoDrawStyle* pDrawStyle = new SoDrawStyle;
pDrawStyle->lineWidth.setValue(4.0f);
curveSep->addChild(pDrawStyle);

// and a complexity.
SoComplexity *complexity = new SoComplexity;
SoCoordinate3 *controlPts = new SoCoordinate3;
SoNurbsCurve *curve = new SoNurbsCurve;
complexity->value = 1.0f;
controlPts->point.setValues(0, nCtrlPts, pts);
curve->numControlPoints = nCtrlPts;
curve->knotVector.setValues(0, nKnots, knots);
curveSep->addChild(complexity);
curveSep->addChild(controlPts);
curveSep->addChild(curve);

pRootNode->addChild(curveSep);

delete[] pts;
delete[] knots;

return true;
}