Open CASCADE学习|点和曲线的相互转化

文章详细介绍了在计算机图形学中,如何使用OpenCASCADE库中的GCPnts包对三维曲线进行按数量、长度和弦高三种方式进行离散化,以及通过B样条插值和近似方法由离散点合成曲线的过程。
摘要由CSDN通过智能技术生成

目录

1、把曲线离散成点

1.1按数量离散

1.2按长度离散

1.3按弦高离散

2、由点合成曲线

2.1B样条插值

2.2B样条近似


1、把曲线离散成点

计算机图形学中绘制曲线,无论是绘制参数曲线还是非参数曲线,都需要先将参数曲线进行离散化,通过离散化得到一组离散化的点集,然后再将点集发送给图形渲染管线进行处理,最终生成我们想要的曲线。

OpenCASCADE中提供了GCPnts包。利用GCPnts包中提供的类,我们可以很方便的将三维曲线进行离散化。

1.1按数量离散

#include <Geom_CylindricalSurface.hxx>#include <gp_Ax3.hxx>#include <GeomAPI_Interpolate.hxx>#include <BRepAdaptor_Curve.hxx>#include <BRepBuilderAPI_MakeEdge.hxx>#include <Geom2d_TrimmedCurve.hxx>#include <GCE2d_MakeSegment.hxx>#include <GeomAPI_PointsToBSpline.hxx>#include <BRepBuilderAPI_MakeFace.hxx>#include <GC_MakeCircle.hxx>#include <BRepBuilderAPI_MakeWire.hxx>#include <BRepOffsetAPI_MakePipe.hxx>#include <GC_MakeArcOfCircle.hxx>#include <BRepAlgoAPI_Fuse.hxx>#include <gp_GTrsf.hxx>#include <BRepBuilderAPI_MakeVertex.hxx>#include"Viewer.h"#include <BRepAdaptor_CompCurve.hxx>#include <GCPnts_UniformAbscissa.hxx>int main(int argc, char* argv[]){    gp_Dir  Z(0.0, 0.0, 1.0);    gp_Pnt center(0, 0, 0.0);    gp_Pnt xr(0.5, 0, 0.0);    gp_Pnt yr(0.0, 1.0, 0.0);    gp_Pnt zr(0.0, 0.0, 7.0);    gp_Ax2  wb(center, Z);    gp_Circ  wbcircle(wb, 0.125 / 2);    TopoDS_Edge wbe = BRepBuilderAPI_MakeEdge(wbcircle);    TopoDS_Wire te = BRepBuilderAPI_MakeWire(wbe);    BRepAdaptor_CompCurve compCurve(te);    GCPnts_UniformAbscissa uniAbs(compCurve, 100, -1);    gp_Pnt p;    Viewer vout(50, 50, 500, 500);    vout << te;    if (uniAbs.IsDone())    {        for (Standard_Integer i = 1; i <= uniAbs.NbPoints(); ++i)        {            Standard_Real u = uniAbs.Parameter(i);            compCurve.D0(u, p);//获取每个离散点            TopoDS_Vertex verti = BRepBuilderAPI_MakeVertex(p);            vout << verti;        }    }    vout.StartMessageLoop();    return 0;}

1.2按长度离散

#include <Geom_CylindricalSurface.hxx>#include <gp_Ax3.hxx>#include <GeomAPI_Interpolate.hxx>#include <BRepAdaptor_Curve.hxx>#include <BRepBuilderAPI_MakeEdge.hxx>#include <Geom2d_TrimmedCurve.hxx>#include <GCE2d_MakeSegment.hxx>#include <GeomAPI_PointsToBSpline.hxx>#include <BRepBuilderAPI_MakeFace.hxx>#include <GC_MakeCircle.hxx>#include <BRepBuilderAPI_MakeWire.hxx>#include <BRepOffsetAPI_MakePipe.hxx>#include <GC_MakeArcOfCircle.hxx>#include <BRepAlgoAPI_Fuse.hxx>#include <gp_GTrsf.hxx>#include <BRepBuilderAPI_MakeVertex.hxx>#include"Viewer.h"#include <BRepAdaptor_CompCurve.hxx>#include <GCPnts_UniformAbscissa.hxx>int main(int argc, char* argv[]){    gp_Dir  Z(0.0, 0.0, 1.0);    gp_Pnt center(0, 0, 0.0);    gp_Pnt xr(0.5, 0, 0.0);    gp_Pnt yr(0.0, 1.0, 0.0);    gp_Pnt zr(0.0, 0.0, 7.0);    gp_Ax2  wb(center, Z);    gp_Circ  wbcircle(wb, 0.125 / 2);    TopoDS_Edge wbe = BRepBuilderAPI_MakeEdge(wbcircle);    TopoDS_Wire te = BRepBuilderAPI_MakeWire(wbe);    BRepAdaptor_CompCurve compCurve(te);    GCPnts_UniformAbscissa uniAbs;    uniAbs.Initialize(compCurve, 0.05, -1);    gp_Pnt p;    Viewer vout(50, 50, 500, 500);    vout << te;    if (uniAbs.IsDone())    {        for (Standard_Integer i = 1; i <= uniAbs.NbPoints(); ++i)        {            Standard_Real u = uniAbs.Parameter(i);            compCurve.D0(u, p);//获取每个离散点            TopoDS_Vertex verti = BRepBuilderAPI_MakeVertex(p);            vout << verti;        }    }    vout.StartMessageLoop();    return 0;}

1.3按弦高离散

#include <Geom_CylindricalSurface.hxx>#include <gp_Ax3.hxx>#include <GeomAPI_Interpolate.hxx>#include <BRepAdaptor_Curve.hxx>#include <BRepBuilderAPI_MakeEdge.hxx>#include <Geom2d_TrimmedCurve.hxx>#include <GCE2d_MakeSegment.hxx>#include <GeomAPI_PointsToBSpline.hxx>#include <BRepBuilderAPI_MakeFace.hxx>#include <GC_MakeCircle.hxx>#include <BRepBuilderAPI_MakeWire.hxx>#include <BRepOffsetAPI_MakePipe.hxx>#include <GC_MakeArcOfCircle.hxx>#include <BRepAlgoAPI_Fuse.hxx>#include <GCPnts_QuasiUniformDeflection.hxx>#include <BRepBuilderAPI_MakeVertex.hxx>#include"Viewer.h"#include <BRepAdaptor_CompCurve.hxx>#include <GCPnts_UniformAbscissa.hxx>int main(int argc, char* argv[]){    gp_Dir  Z(0.0, 0.0, 1.0);    gp_Pnt center(0, 0, 0.0);    gp_Pnt xr(0.5, 0, 0.0);    gp_Pnt yr(0.0, 1.0, 0.0);    gp_Pnt zr(0.0, 0.0, 7.0);    gp_Ax2  wb(center, Z);    gp_Circ  wbcircle(wb, 0.125 / 2);    TopoDS_Edge wbe = BRepBuilderAPI_MakeEdge(wbcircle);    TopoDS_Wire te = BRepBuilderAPI_MakeWire(wbe);    BRepAdaptor_CompCurve compCurve(te);    GCPnts_QuasiUniformDeflection quasiUniDef;    quasiUniDef.Initialize(compCurve, 0.08, GeomAbs_C0);    gp_Pnt p;    Viewer vout(50, 50, 500, 500);    vout << te;    if (quasiUniDef.IsDone())    {        for (Standard_Integer i = 1; i <= quasiUniDef.NbPoints(); ++i)        {            p=quasiUniDef.Value(i);//获取每个离散点            TopoDS_Vertex verti = BRepBuilderAPI_MakeVertex(p);            vout << verti;        }    }    vout.StartMessageLoop();    return 0;}

2、由点合成曲线

曲线曲面拟合Curve and Surface Fitting的方式可以分为两类:插值interpolation和逼近approximation。采用插值的方式时,所创建的曲线或曲面必须精确地满足所给的数据条件,例如曲线通过所给的插值点。采用逼近的方式时,创建的曲线或曲面不必精确地满足所给的数据条件,只要在一定的误差范围内接近即可。

2.1B样条插值

#include <Geom_CylindricalSurface.hxx>
#include <gp_Ax3.hxx>
#include <GeomAPI_Interpolate.hxx>
#include <BRepAdaptor_Curve.hxx>
#include <BRepBuilderAPI_MakeEdge.hxx>
#include <Geom2d_TrimmedCurve.hxx>
#include <GCE2d_MakeSegment.hxx>
​
#include <GeomAPI_PointsToBSpline.hxx>
#include <BRepBuilderAPI_MakeFace.hxx>
#include <GC_MakeCircle.hxx>
#include <BRepBuilderAPI_MakeWire.hxx>
#include <BRepOffsetAPI_MakePipe.hxx>
#include <GC_MakeArcOfCircle.hxx>
#include <BRepAlgoAPI_Fuse.hxx>
​
#include <GCPnts_QuasiUniformDeflection.hxx>
#include <BRepBuilderAPI_MakeVertex.hxx>
​
#include"Viewer.h"
​
#include <BRepAdaptor_CompCurve.hxx>
#include <GeomTools.hxx>
​
int main(int argc, char* argv[])
{
    Handle(TColgp_HArray1OfPnt) aPoints = new TColgp_HArray1OfPnt(1, 3);
    Handle(Geom_BSplineCurve) aBSplineCurve;
​
    aPoints->SetValue(1, gp_Pnt(0.0, 0.0, 0.0));
    aPoints->SetValue(2, gp_Pnt(1.0, 1.0, 0.0));
    aPoints->SetValue(3, gp_Pnt(2.0, 6.0, 3.0));
​
    GeomAPI_Interpolate aInterpolater(aPoints, Standard_False, Precision::Approximation());
    aInterpolater.Perform();
    aBSplineCurve = aInterpolater.Curve();
    //std::cout << "ok";
    TopoDS_Edge s = BRepBuilderAPI_MakeEdge(aBSplineCurve);
    
 
    Viewer vout(50, 50, 500, 500);
    vout << s;
    vout << BRepBuilderAPI_MakeVertex(aPoints->Value(1));
    vout << BRepBuilderAPI_MakeVertex(aPoints->Value(2));
    vout << BRepBuilderAPI_MakeVertex(aPoints->Value(3));
    vout.StartMessageLoop();
    return 0;
}

2.2B样条近似

#include <Geom_CylindricalSurface.hxx>#include <gp_Ax3.hxx>#include <GeomAPI_Interpolate.hxx>#include <BRepAdaptor_Curve.hxx>#include <BRepBuilderAPI_MakeEdge.hxx>#include <Geom2d_TrimmedCurve.hxx>#include <GCE2d_MakeSegment.hxx>#include <GeomAPI_PointsToBSpline.hxx>#include <BRepBuilderAPI_MakeFace.hxx>#include <GC_MakeCircle.hxx>#include <BRepBuilderAPI_MakeWire.hxx>#include <BRepOffsetAPI_MakePipe.hxx>#include <GC_MakeArcOfCircle.hxx>#include <BRepAlgoAPI_Fuse.hxx>#include <GCPnts_QuasiUniformDeflection.hxx>#include <BRepBuilderAPI_MakeVertex.hxx>#include"Viewer.h"#include <BRepAdaptor_CompCurve.hxx>#include <GeomTools.hxx>int main(int argc, char* argv[]){    Handle(TColgp_HArray1OfPnt) aPoints = new TColgp_HArray1OfPnt(1, 3);    Handle(Geom_BSplineCurve) aBSplineCurve;    aPoints->SetValue(1, gp_Pnt(0.0, 0.0, 0.0));    aPoints->SetValue(2, gp_Pnt(1.0, 1.0, 0.0));    aPoints->SetValue(3, gp_Pnt(2.0, 6.0, 3.0));    GeomAPI_PointsToBSpline Approx(aPoints->Array1());    Handle(Geom_BSplineCurve) K = Approx.Curve();    TopoDS_Edge s = BRepBuilderAPI_MakeEdge(K);         Viewer vout(50, 50, 500, 500);    vout << s;    vout << BRepBuilderAPI_MakeVertex(aPoints->Value(1));    vout << BRepBuilderAPI_MakeVertex(aPoints->Value(2));    vout << BRepBuilderAPI_MakeVertex(aPoints->Value(3));    vout.StartMessageLoop();    return 0;}

       

  • 12
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Open Cascade is a powerful open-source framework for 3D modeling and CAD/CAM/CAE applications. It provides a wide range of functionality for geometric modeling, visualization, and data exchange. However, Open Cascade does not have direct support for Python. If you want to use Open Cascade with Python, you can utilize the PyOCCT library. PyOCCT is a Python binding for Open Cascade, which allows you to access and utilize Open Cascade functionality through Python scripts. To get started with Open Cascade and Python, you need to follow these steps: 1. Install Open Cascade: Download and install the Open Cascade libraries from the official website (www.opencascade.com). Make sure to choose the appropriate version for your operating system. 2. Install PyOCCT: Once Open Cascade is installed, you can install the PyOCCT library using pip. Run the following command in your terminal: ``` pip install pythonocc-core ``` 3. Start coding: Now you can import the `occt` module in your Python script and start utilizing Open Cascade's functionality. Here's a simple example to create a box: ```python from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox from OCC.Display.SimpleGui import init_display box = BRepPrimAPI_MakeBox(10, 10, 10).Shape() display, start_display, add_menu, add_function_to_menu = init_display() display.DisplayShape(box) start_display() ``` This is just a basic example to get you started. Open Cascade provides a vast set of features, including advanced 3D modeling operations, meshing algorithms, and more. You can refer to the official Open Cascade documentation and PyOCCT's GitHub repository for more information and examples. Note: It's important to mention that PyOCCT is a separate project developed by the community and not officially maintained by the Open Cascade company.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值