OpenCASCADE Interpolations and Approximations
eryar@163.com
Abstract. In modeling, it is often required to approximate or interpolate points to curves and surfaces. In interpolation, the process is complete when the curve or surface passes through all the points; in approximation, when it is as close to these points as possible. The paper is the translation of the OpenCASCADE Modeling Data user guide.
Key Words. Interpolation, Approximation, Fitting
1.Introduction
在幾何造型中,經(jīng)常會(huì)需要根據(jù)點(diǎn)去擬合(插值Interpolation或逼近Approximation)曲線曲面。插值要求擬合所得的曲線或曲面必須通過(guò)所有擬合點(diǎn);而逼近要求擬合所得曲線或曲面盡可能地靠近全部擬合點(diǎn)。OpenCASCADE中提供了曲線曲面擬合的功能有:
v 對(duì)二維點(diǎn)進(jìn)行二維B樣條或Bezier曲線插值;
v 對(duì)二維點(diǎn)進(jìn)行二維B樣條或Bezier曲線逼近;
v 對(duì)三維點(diǎn)進(jìn)行三維B樣條或Bezier曲線或B樣條曲面插值;
v 對(duì)三維點(diǎn)進(jìn)行三維B樣條或Bezier曲線或B樣條曲面逼近;
程序中使用擬合功能有兩種方式:
v 使用高級(jí)功能:提供簡(jiǎn)單的方法調(diào)用來(lái)獲得擬合結(jié)果;
v 使用低級(jí)功能:專(zhuān)為想對(duì)擬合有更多控制的用戶(hù)設(shè)計(jì);
插值和逼近也是數(shù)值分析或計(jì)算方法中的主要內(nèi)容。用B樣條作為插值或逼近函數(shù)是數(shù)值分析中的一個(gè)具體方法。通過(guò)對(duì)OpenCASCADE中插值和逼近算法的學(xué)習(xí),加深對(duì)一些算法,如最小二乘法,多元函數(shù)求極值等數(shù)學(xué)知識(shí)的理解。
本文主要是對(duì)OpenCASCADE的文檔Modeling Data 中的插值和逼近部分進(jìn)行翻譯,并給出了使用其低級(jí)功能具體的代碼示例。
2.Analysis of a set of points
包GProp中的類(lèi)PEquation提供了對(duì)點(diǎn)集、點(diǎn)云數(shù)據(jù)進(jìn)行分析的功能,可以用來(lái)驗(yàn)證在給定的精度范圍內(nèi)是否重合、共線或共面等,如果檢測(cè)結(jié)果是,那么算法將計(jì)算出這些點(diǎn)的重合點(diǎn)、線或面。如果檢測(cè)結(jié)果不是,則算法會(huì)計(jì)算出點(diǎn)集或點(diǎn)云的包圍盒。
3.Basic Interpolation and Approximation
包Geom2dAPI和GeomAPI提供了擬合(逼近和插值)簡(jiǎn)單的方法。
v 2D Interpolation: 類(lèi)Geom2dAPI_Interpolate可以用于生成一條通過(guò)一系列點(diǎn)的二維B樣條曲線。如果需要,還可以設(shè)置點(diǎn)相應(yīng)的切矢及參數(shù)來(lái)對(duì)插值曲線做進(jìn)一步的約束。
v 3D Interpolation: 類(lèi)GeomAPI_Interpolate可以用于生成一條通過(guò)一系列點(diǎn)的三維B樣條曲線。如果需要,還可以設(shè)置點(diǎn)相應(yīng)的切矢及參數(shù)來(lái)對(duì)插值曲線做進(jìn)一步的約束。因?yàn)槭歉呒?jí)功能,所以需要很少的代碼就可以得到插值曲線,用法如下:
GeomAPI_Interpolate Interp(Points);
Handle_Geom_BSplineCurve C = Interp.Curve()
v 2D Approximation: 類(lèi)Geom2dAPI_PointsToBSpline可以用于生成逼近一系列點(diǎn)的二維B樣條曲線。你需要定義曲線次數(shù)范圍,連續(xù)性和容差。容差值只是用來(lái)檢查逼近點(diǎn)之間是不是有重合點(diǎn),或者切矢是否太小。逼近曲線將會(huì)是C2連續(xù)或2次曲線,當(dāng)有切矢約束時(shí),將會(huì)得到C1連續(xù)的曲線。
v 3D Approximation:類(lèi)GeomAPI_PointsToBSpline可以用于生成逼近一系列點(diǎn)的三維B樣條曲線。你需要定義曲線次數(shù)范圍,連續(xù)性和容差。容差值只是用來(lái)檢查逼近點(diǎn)之間是不是有重合點(diǎn),或者切矢是否太小。逼近曲線將會(huì)是C2連續(xù)或2次曲線,當(dāng)有切矢約束時(shí),將會(huì)得到C1連續(xù)的曲線。類(lèi)的用法如下:
GeomAPI_PointsToBSpline Approx(Points, DegMin, DegMax, Continutity, Tol);
Handle_Geom_BSplineCurve K = Approx.Curve()
v Surface Approximation:類(lèi)GeomAPI_PointsToBSplineSurface可以用于根據(jù)點(diǎn)集擬合B樣條曲面。
4.Advanced Approximation
包AppDef和AppParCurves提供了低級(jí)的功能,允許對(duì)擬合有更多地控制。低級(jí)功能提供了如下函數(shù)接口API:
v 定義擬合切矢的規(guī)則,這些切矢有原點(diǎn)和極值;
v 根據(jù)各自參數(shù)平行地?cái)M合一系列曲線;
v 光滑擬合:生成光順(faired curve)的曲線。
注:包AppDef和AppParCurves中通過(guò)宏定義實(shí)現(xiàn)了一種類(lèi)似多態(tài)的功能,但是程序調(diào)試不方便,導(dǎo)致一些類(lèi)名沒(méi)有看到聲明文件,對(duì)程序的理解造成一些不便。
4.1 Approximation by multiple point constraints
包AppDef中提供了低級(jí)工具用于對(duì)帶約束的點(diǎn)集進(jìn)行Bezier或B樣條曲線擬合。其中的功能有:
v 定義一組約束點(diǎn),使用類(lèi)AppDef_MultiPointConstraint;
v 定義一組約束線,使用類(lèi)AppDef_MultiLine;
v 擬合Bezier曲線,使用類(lèi)AppDef_Compute;
v 擬合B樣條曲線,使用類(lèi)AppDef_BSplineCompute;
v 定義變分標(biāo)準(zhǔn)Variational Criteria;
注:在類(lèi)AppDef_Variational中可以看到Variational Optimization變分優(yōu)化。對(duì)于工科出來(lái)的人來(lái)說(shuō),這些概念還真是有些陌生,還是學(xué)數(shù)學(xué)的人厲害!
其中類(lèi)AppDef_MultiLine的用法也有點(diǎn)特別,通過(guò)文檔中的圖來(lái)理解下:
其中:Pi, Qi, Ri, ..., Si可以是二維點(diǎn)或三維點(diǎn);
按組來(lái)定義,其中Pn, Qn, Rn, ..., Sn都是在類(lèi)AppDef_MultiPointConstraint中定義;
P1, P2, .., Pn或Q, R,..., S的點(diǎn)系列用來(lái)擬合。
類(lèi)AppDef_Compute用Bezier曲線來(lái)擬合點(diǎn);
類(lèi)AppDef_BSplineCompute用B樣條曲線來(lái)擬合點(diǎn);
注:需要仔細(xì)理解AppDef_MultiPointConstraint和AppDef_MultiLine的用法。
4.2 Example: How to approximate a curve with low-level tools
使用低級(jí)功能的過(guò)程可分為如下幾步:
v 定義擬合點(diǎn);
v 根據(jù)擬合點(diǎn)創(chuàng)建MultiLine;
v 使用AppDef_Compute或AppDef_BSplineCompute來(lái)擬合曲線;
下面給出使用低級(jí)功能的代碼示例:
/*
* Copyright (c) 2016 Shing Liu All Rights Reserved.
*
* File : main.cpp
* Author : Shing Liu(eryar@163.com)
* Date : 2016-03-17 21:00
* Version : OpenCASCADE6.9.0
*
* Description : test the low-level tools of approximation.
*/
#define WNT
#include <TColgp_Array1OfPnt.hxx>
#include <AppDef_MultiPointConstraint.hxx>
#include <AppDef_MultiLine.hxx>
#include <AppDef_Compute.hxx>
#include <AppDef_BSplineCompute.hxx>
#pragma comment(lib, "TKernel.lib")
#pragma comment(lib, "TKMath.lib")
#pragma comment(lib, "TKGeomBase.lib")
void testBezierApprox()
{
TColgp_Array1OfPnt aPoints(1, 3);
aPoints.SetValue(1, gp_Pnt(0.0, 0.0, 0.0));
aPoints.SetValue(2, gp_Pnt(1.0, 0.0, 0.0));
aPoints.SetValue(3, gp_Pnt(1.0, 1.0, 0.0));
AppDef_MultiLine aMultiLine(aPoints);
AppDef_Compute aBezierApprox(aMultiLine);
aBezierApprox.Value().Dump(std::cout);
}
void testBSplineApprox()
{
AppDef_MultiLine aMultiLine(3);
AppDef_MultiPointConstraint aMPC1(2, 0);
AppDef_MultiPointConstraint aMPC2(2, 0);
AppDef_MultiPointConstraint aMPC3(2, 0);
aMPC1.SetPoint(1, gp_Pnt(0.0, 0.0, 0.0));
aMPC1.SetPoint(2, gp_Pnt(0.0, 2.0, 0.0));
aMPC2.SetPoint(1, gp_Pnt(1.0, 0.0, 0.0));
aMPC2.SetPoint(2, gp_Pnt(1.0, 2.0, 0.0));
aMPC3.SetPoint(1, gp_Pnt(1.0, 1.0, 0.0));
aMPC3.SetPoint(2, gp_Pnt(1.0, 3.0, 1.0));
aMultiLine.SetValue(1, aMPC1);
aMultiLine.SetValue(2, aMPC2);
aMultiLine.SetValue(3, aMPC3);
AppDef_BSplineCompute aBSplineApprox(aMultiLine);
aBSplineApprox.Value().Dump(std::cout);
}
int main(int argc, char* argv[])
{
testBezierApprox();
testBSplineApprox();
return 0;
}
程序運(yùn)行結(jié)果如下圖所示:
5.Conclusion
關(guān)于點(diǎn)的插值和逼近是《數(shù)據(jù)逼近》或《數(shù)值分析》或《計(jì)算方法》書(shū)中關(guān)注的內(nèi)容。從文檔中可以看到包AppDef中的一些關(guān)鍵字:Gradient, BFGS, LeastSquare等,根據(jù)這些關(guān)鍵字可以看出OpenCASCADE中逼近使用的算法了。
通過(guò)使用AppDef_MultiPointConstraint及AppDef_MultiLine等低級(jí)接口,來(lái)理解擬合數(shù)據(jù)點(diǎn)輸入及擬合結(jié)果的輸出。進(jìn)而去學(xué)習(xí)BFGS、LeastSquare等數(shù)學(xué)理論工具在實(shí)際中的應(yīng)用。對(duì)于多元函數(shù),梯度及BFGS等概念還好接受,對(duì)于變分法優(yōu)化之類(lèi)理論已經(jīng)超出了工科數(shù)學(xué)的范圍,不過(guò)結(jié)合實(shí)際去學(xué)習(xí)這些概念應(yīng)該會(huì)更有效率。