經常會遇到要求點到曲線的距離的問題,在運用opencascade計算點到曲線和曲面的距離常用的兩個類是:
Extrema_ExtPC, Extrema_ExtPS
一般做法是先構造曲線特征:(構造了Wire拓撲邊)
TopoDS_Wire w = BRepBuilderAPI_MakePolygon(
gp_Pnt(0,0,0),
gp_Pnt(10,0,0),
gp_Pnt(10,10,0),
gp_Pnt(0,10,0),Standard_True);
然後将其轉化成曲線:
//create an adaptor curve
BRepAdaptor_CompCurve pseudoCurve(w);
//get the parameters
Standard_Real f = pseudoCurve.FirstParameter();
Standard_Real l = pseudoCurve.LastParameter();
//f = 0.0 ; l = 4.0
建立點,檢驗計算是否正确:
//create 2 points to project on the wire
gp_Pnt P1(5,0,0);
gp_Pnt P2(0,5,0);
gp_Pnt newP1,newP2;
Standard_Real newVal1,newVal2;
Extrema_ExtPC ext1(P1,pseudoCurve,f,l);
if ( ext1.IsDone() && ext1.NbExt() > 0 )
{
for ( Standard_Integer i = 1 ; i < ext1.NbExt() ; i++ )
{
if ( ext1.IsMin(i) )
{
newP1 = ext1.Point(i).Value();
cout << "points: " << newP1.X() << " " << newP1.Y() << " " << newP1.Z() << endl;
cout << "distance: " << sqrt((P1.X() - newP1.X() ) * (P1.X() - newP1.X() ) + (P1.Y() - newP1.Y() ) * (P1.Y() - newP1.Y() ) + (P1.Z() - newP1.Z() ) * (P1.Z() - newP1.Z() ) ) << endl;
//break;
}
}
}
輸出結果:
最後有個非常關鍵的問題:
這裡計算出了一系列的最短距離,開始看到一個函數
IsMin就讓容易讓人想到判斷是不是就是最短距離的,
其實不是,這個類估計是分段計算最短距離的,
結果儲存的是每一段中得到的最短距離,是以,每一個都是最短距離
非常值得注意的是:
1.計算到底哪個是最短距離 需要後續處理,這個非常關鍵,容易搞錯。
2.給定任意的一個點,經常會出現,找不到最短距離的點,是以這種情況需要特殊處理。