OpenNURBS to OpenCASCADE
Abstract. The OpenNURBS initiative provides CAD/CAM/CAE and computer graphics software developers the tools to accurately transfer 3D geometry between applications. The OpenNURBS C++ source code is clean and fairly simple. The OpenNURBS Toolkit is intended for use by C++ and .NET programmers. The toolkit includes complete source code to create a library that will read and write 3dm files. OpenCASCADE providing services for 3D surface and solid modeling, CAD data exchange, and visulization. Most of OCCT functionality is available in the form of C++ libraries. If can convert OpenNURBS curve/surface to OpenCASCADE, then it will fill the gap of data exchange between OpenNURBS and OpenCASCADE.?
Key Words. OpenCASCADE, OpenNURBS, NURBS, 3DM, DataExchange?
1. Introduction
OpenNURBS 旨在為CAD、CAM、CAE與計算機圖形軟件開發人員提供一個在不同的軟件間精確轉換 3D 幾何的工具。OpenNURBS 所提供的功能包括:?
v 可以讀、寫3DM文件格式的C ++原代碼庫,支持目前的 Microsoft、Apple與 Gnu for Windows、Windows X64、Mac與Linux編繹器...?
v 可以讀、寫3DM文件格式的.NET 源代碼庫。?
v 品質保證與版本控制。?
v 技術支持。?
與其他開源產品的開發平臺不同之處:?
v 鼓勵使用在商業用途。?
v 提供免費的開發工具與技術支持。?
v 無任何約束,不受版權與公共版權(copyleft )條款約束。?
v 鼓勵但不強迫用戶分享自己的研發成果。?
NURBS是當前流行幾何造型系統中通用的曲線曲面表達形式,OpenNURBS開源庫提供了NURBS曲線曲面的數據表達,但缺少一些造型算法,而OpenCASCADE中提供了大量的造型算法。通過將OpenNURBS中的曲線曲面轉換到OpenCASCADE中,就可以將OpenNURBS和OpenCASCADE結合起來使用了。?
OpenNURBS中的代碼簡潔,易懂;OpenCASCADE的造型功能強大。所以先將OpenNURBS中的曲線曲面轉換到OpenCASCADE中,為OpenCASCADE的3DM格式的數據交換打下基礎。?
2.Convert OpenNURBS Curve?
OpenNURBS中統一使用NURBS方式來表達曲線和曲面。OpenCASCADE中對應NURBS曲線的類為Geom_BSplineCurve。因此,可以將OpenNURBS的曲線轉換到OpenCASCADE中。轉換時需要注意節點矢量Knot Vector的方式有很大的不同。OpenNURBS中的節點矢量是所有的節點值,包括了重節點,而且在節點矢量的首尾還少了兩個節點。OpenCASCADE中的節點矢量由不包含重復節點的節點矢量及其重數來構造。其它數據基本相同,將轉換曲線的示例代碼列出如下:?
/* * * @breif Convert OpenNURBS NURBS curve to OpenCASCADE Geom_BSplineCurve. * @param [in] theCurve opennurbs nurbs curve; * @param [in] theBRepFile the curve is in brep file of opencascade; * @note pay attention to the knots of opennurbs nurbs curve/surface. */ void ConvertCurve( const ON_NurbsCurve& theCurve, const std:: string & theBRepFile) { TColgp_Array1OfPnt aPoles( 1 , theCurve.CVCount()); TColStd_Array1OfReal aWeights( 1 , theCurve.CVCount()); TColStd_Array1OfReal aKnotSequence( 1 , theCurve.KnotCount() + 2 ); bool IsRational = theCurve.IsRational(); bool IsPeriodic = (theCurve.IsPeriodic()) ? true : false ; // Control point and its weight. for ( int i = 0 ; i < theCurve.CVCount(); ++ i) { if (IsRational) { ON_4dPoint aPole; theCurve.GetCV(i, aPole); aPoles.SetValue(i + 1 , gp_Pnt(aPole.x / aPole.w, aPole.y / aPole.w, aPole.z / aPole.w)); aWeights.SetValue(i + 1 , aPole.w); } else { ON_3dPoint aPole; theCurve.GetCV(i, aPole); aPoles.SetValue(i + 1 , gp_Pnt(aPole.x, aPole.y, aPole.z)); } } // Knot vector and its multiplicity. for ( int i = 0 ; i < theCurve.KnotCount(); ++ i) { aKnotSequence.SetValue(i + 2 , theCurve.Knot(i)); } aKnotSequence.SetValue(aKnotSequence.Lower(), theCurve.Knot( 0 )); aKnotSequence.SetValue(aKnotSequence.Upper(), theCurve.Knot(theCurve.KnotCount() - 1 )); TColStd_Array1OfReal aKnots( 1 , BSplCLib::KnotsLength(aKnotSequence, IsPeriodic)); TColStd_Array1OfInteger aMultiplicities( 1 , aKnots.Upper()); BSplCLib::Knots(aKnotSequence, aKnots, aMultiplicities); Handle_Geom_BSplineCurve aBSplineCurve = new Geom_BSplineCurve( aPoles, aWeights, aKnots, aMultiplicities, theCurve.Degree(), theCurve.IsPeriodic()); GeomTools_CurveSet::PrintCurve(aBSplineCurve, std::cout); TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge(aBSplineCurve); BRepTools::Write(anEdge, theBRepFile.c_str()); }
程序先轉換控制頂點,若是有理曲線,還要設置控制頂點的權值w。在轉換節點矢量時,使用了BSplCLib::Knots()函數將OpenNURBS的節點矢量轉換為OpenCASCADE的形式,并補齊了缺少的端部的兩個節點。最后為了在OpenCASCADE中顯示轉換結果,將曲線生成TopoDS_Edge并保存為BRep格式,方便在Draw Test Harness中查看結果如下圖所示:?
Figure 2.1 OpenNURBS nurbs circle in OpenCASCADE Draw Test Harness?
3.Convert OpenNURBS Surface
OpenNURBS曲面的轉換同樣也需要注意u, v方向上的節點矢量的問題,和曲線的轉換類似。除此之外就是把相同的屬性分別賦值即可,列出代碼如下所示:?
/* * * @breif Convert OpenNURBS NURBS surface to OpenCASCADE Geom_BSplineSurface. * @param [in] theSurface opennurbs nurbs surface; * @param [in] theBRepFile the surface is in the brep file of opencascade; * @note pay attention to the knots of opennurbs nurbs curve/surface. */ void ConvertSurface( const ON_NurbsSurface& theSurface, const std:: string & theBRepFile) { TColgp_Array2OfPnt aPoles( 1 , theSurface.CVCount( 0 ), 1 , theSurface.CVCount( 1 )); TColStd_Array2OfReal aWeights( 1 , theSurface.CVCount( 0 ), 1 , theSurface.CVCount( 1 )); TColStd_Array1OfReal aUKnotSequence( 1 , theSurface.KnotCount( 0 ) + 2 ); TColStd_Array1OfReal aVKnotSequence( 1 , theSurface.KnotCount( 1 ) + 2 ); bool IsRational = theSurface.IsRational(); bool IsUPeriodic = (theSurface.IsPeriodic( 0 )) ? true : false ; bool IsVPeriodic = (theSurface.IsPeriodic( 1 )) ? true : false ; // control point and its weight. for ( int i = 0 ; i < theSurface.CVCount( 0 ); ++ i) { for ( int j = 0 ; j < theSurface.CVCount( 1 ); ++ j) { if (IsRational) { ON_4dPoint aPole; theSurface.GetCV(i, j, aPole); aPoles.SetValue(i + 1 , j + 1 , gp_Pnt(aPole.x / aPole.w, aPole.y / aPole.w, aPole.z / aPole.w)); aWeights.SetValue(i + 1 , j + 1 , aPole.w); } else { ON_3dPoint aPole; theSurface.GetCV(i, j, aPole); aPoles.SetValue(i + 1 , j + 1 , gp_Pnt(aPole.x, aPole.y, aPole.z)); } } } // Knot vector and its multiplicity. for ( int i = 0 ; i < theSurface.KnotCount( 0 ); ++ i) { aUKnotSequence.SetValue(i + 2 , theSurface.Knot( 0 , i)); } aUKnotSequence.SetValue(aUKnotSequence.Lower(), theSurface.Knot( 0 , 0 )); aUKnotSequence.SetValue(aUKnotSequence.Upper(), theSurface.Knot( 0 , theSurface.KnotCount( 0 ) - 1 )); TColStd_Array1OfReal aUKnots( 1 , BSplCLib::KnotsLength(aUKnotSequence, IsUPeriodic)); TColStd_Array1OfInteger aUMults( 1 , aUKnots.Upper()); BSplCLib::Knots(aUKnotSequence, aUKnots, aUMults); for ( int j = 0 ; j < theSurface.KnotCount( 1 ); ++ j) { aVKnotSequence.SetValue(j + 2 , theSurface.Knot( 1 , j)); } aVKnotSequence.SetValue(aVKnotSequence.Lower(), theSurface.Knot( 1 , 0 )); aVKnotSequence.SetValue(aVKnotSequence.Upper(), theSurface.Knot( 1 , theSurface.KnotCount( 1 ) - 1 )); TColStd_Array1OfReal aVKnots( 1 , BSplCLib::KnotsLength(aVKnotSequence, IsVPeriodic)); TColStd_Array1OfInteger aVMults( 1 , aVKnots.Upper()); BSplCLib::Knots(aVKnotSequence, aVKnots, aVMults); Handle_Geom_BSplineSurface aBSplineSurface = new Geom_BSplineSurface(aPoles, aWeights, aUKnots, aVKnots, aUMults, aVMults, theSurface.Degree( 0 ), theSurface.Degree( 1 ), IsUPeriodic, IsVPeriodic); GeomTools_SurfaceSet::PrintSurface(aBSplineSurface, std::cout); TopoDS_Face aFace = BRepBuilderAPI_MakeFace(aBSplineSurface, Precision::Confusion()); BRepTools::Write(aFace, theBRepFile.c_str()); }
轉換過程與曲線的類似,只是多了v方向上的處理。以上兩個函數分別實現曲線曲面的轉換,完整程序可以在本文后面的鏈接中下載。以圓柱面和球面為例,測試了一下轉換效果,在Draw Test Harness中顯示結果如下圖所示:?
Figure 3.1 OpenNURBS nurbs sphere in OpenCASCADE Draw Test Harness?
Figure 3.2 OpenNURBS nurbs cylinder in OpenCASCADE Draw Test Harness?
由上圖可知,OpenCASCADE中的Geom_BSplineCurve/Geom_BSplineSurface可以完美表示OpenNURBS中的曲線曲面,不過有最高次數為25次的限制。?
4.Conclusion
現可看出,OpenNURBS中的曲線曲面可以轉換成OpenCASCADE中。不過在轉換時需要注意OpenNURBS的節點矢量數據與教科書中及OpenCASCADE中不同,需要做些處理。還有需要注意的事項就是OpenNURBS中數組是從0開始的,而在OpenCASCADE中可以指定為從1開始。?
將OpenNURBS的曲線曲面轉換成OpenCASCADE的曲線曲面后,下一步準備將OpenNURBS中的BRep體也轉換到OpenCASCADE中,從而對BRep表示形式做進一步的學習。?
5. References
1. OpenNURBS Web: http://www.rhino3d.com/opennurbs
2. Les Piegl, Wayne Tiller. The NURBS Book. Springer-Verlag. 1997?
3. 趙罡,穆國旺,王拉柱譯. 非均勻有理B樣條. 清華大學出版社. 2010?
?
PDF Version and Code: OpenNURBS to OpenCASCADE
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
