OpenCASCADE Make Primitives-Sphere
Abstract. The sphere is the simplest topology shape of the BRep structure. But there are several import concept of the sphere edges, such as degenerated edge and seam edge. So construct a sphere by code, you will learn these.
Key Words. OpenCASCADE, Sphere, BRep
1. Introduction
球 体(sphere)是边界表示法(BRep)中最简单的一个拓朴形状了,因为它直接由球面来构造。但是其中包含了一些重要的概念,如退化边 (degenerated edge)、衔接边(seam edge)。由代码手工来构造一个球体,可以学习这些概念。首先要知道OpenCASCADE中球面的参数方程:
在Draw Test Harness中显示如下图所示:
Figure 1.1 Sphere in Draw Test Harness
由球面的参数方程可知,当参数u=0或2PI时,对应球面上的点就是上图所示的绿线,实际上是由两个线重合在一起了。
当参数v=-PI/2或PI/2时,对应球面上两个极点,因为球面的两个极点处法向为零,而球面在两个极点处的法向是存在的,所以这样的点即为边退化而成,称为退化边。
三维曲线圆的参数方程如下所示:
通过代码从点开始来构造一个球体,从而来加深理解OpenCASCADE的BRep表示法。
2. Make the Sphere
2.1 Make Vertex
从顶点开始来创建球体。因为球体就是一个球面,为了得到Face的Wire,需要构造一个闭合的区域。这里选择两个极点作为球体的顶点。创建球体的两个极点,程序代码如下所示:
aBuilder.MakeVertex(aNorthPole, aPoints[ 0 ], Precision::Confusion());
aBuilder.MakeVertex(aSouthPole, aPoints[ 1 ], Precision::Confusion());
2.2 Make Edge
为了得到闭合的Wire,需要四条边,其中在球面两个极点处的两条退化边,还有连接两个极点的重合的衔接边。创建边的代码如下所示:
aBuilder.MakeEdge(aSeamEdge, new Geom_Circle(aCircle), Precision::Confusion());
// there is no 3D geometry curve in the degenerated edge.
aBuilder.MakeEdge(aNorthEdge);
aBuilder.Degenerated(aNorthEdge, Standard_True);
// there is no 3D geometry curve in the degenerated edge.
aBuilder.MakeEdge(aSouthEdge);
aBuilder.Degenerated(aSouthEdge, Standard_True);
// set the vertex info of the seam edges.
{
TopoDS_Vertex V1 = aNorthPole;
TopoDS_Vertex V2 = aSouthPole;
V1.Reverse();
aBuilder.Add(aSeamEdge, V1);
aBuilder.Add(aSeamEdge, V2);
aBuilder.UpdateVertex(V1, ElCLib::Parameter(aCircle, aPoints[ 0 ]), aSeamEdge, Precision::Confusion());
aBuilder.UpdateVertex(V2, ElCLib::Parameter(aCircle, aPoints[ 1 ]), aSeamEdge, Precision::Confusion());
BRepTools::Update(aSeamEdge);
}
// set the vertex info of the north degenerated edge.
{
TopoDS_Vertex V1 = aNorthPole;
TopoDS_Vertex V2 = aNorthPole;
V2.Reverse();
aBuilder.Add(aNorthEdge, V1);
aBuilder.Add(aNorthEdge, V2);
BRepTools::Update(aNorthEdge);
}
// set the vertex info of the south degenerated edge.
{
TopoDS_Vertex V1 = aSouthPole;
TopoDS_Vertex V2 = aSouthPole;
V2.Reverse();
aBuilder.Add(aSouthEdge, V1);
aBuilder.Add(aSouthEdge, V2);
BRepTools::Update(aSouthEdge);
}
由上述代码可知,衔接边中包含了几何信息:三维曲线圆;退化边中未包含几何信息,但将其退化边属性设置为true。之后将边上顶点在曲线上对应的参数值设置到边中,退化边不需要设置。
2.3 Make Wire
创建Wire需要确保组成Wire的边要闭合。程序代码如下所示:
aBuilder.MakeWire(aWire);
// add edges to the wire.
{
TopoDS_Edge E1 = aNorthEdge;
TopoDS_Edge E2 = aSeamEdge;
TopoDS_Edge E3 = aSouthEdge;
TopoDS_Edge E4 = aSeamEdge;
E1.Reverse();
E4.Reverse();
aBuilder.Add(aWire, E1);
aBuilder.Add(aWire, E2);
aBuilder.Add(aWire, E3);
aBuilder.Add(aWire, E4);
BRepTools::Update(aWire);
}
2.4 Make Face
创建面后,将边与面关联起来至关重要,即PCurve的设置。程序代码如下所示:
aBuilder.MakeFace(aFace, new Geom_SphericalSurface(aSphere), Precision::Confusion());
// set the pcurve info between edge and face.
{
aBuilder.Range(aNorthEdge, 0.0 , 2 * M_PI);
aBuilder.UpdateEdge(aNorthEdge, new Geom2d_Line(aLines[ 0 ]), aFace, Precision::Confusion());
aBuilder.Range(aSeamEdge, 1.5 * M_PI, 2.5 * M_PI);
aBuilder.UpdateEdge(aSeamEdge, new Geom2d_Line(aLines[ 1 ]), new Geom2d_Line(aLines[ 2 ]), aFace, Precision::Confusion());
aBuilder.Continuity(aSeamEdge, aFace, aFace, GeomAbs_CN);
aBuilder.Range(aSouthEdge, 0.0 , 2 * M_PI);
aBuilder.UpdateEdge(aSouthEdge, new Geom2d_Line(aLines[ 3 ]), aFace, Precision::Confusion());
BRepTools::Update(aFace);
}
由上述代码可知,球面中包含了一个几何的曲面。创建球面后,将相关的边与面关联起来。参数曲线PCurve的范围Range在球面的参数空间中应该闭合。其中两个退化边的范围都是从0到2PI,而衔接边的范围设置不当,会产生不正确的结果,如下图所示:
Figure 2.4.1 Seam Edge Range[-PI/2, PI/2]
线框模式显示正常,但是不能切换到渲染模式,即不能显示出面。结合其PCurve的范围可以发现组成Wire的边的PCurve不能闭合。
当Seam边的三维曲线方向不当时,会不与球面的Seam重合,如下图所示:
Figure 2.4.2 Circle in Seam Edge Range [-PI/2, PI/2]
Figure 2.4.3 Wrong Seam Edge Geometry Curve
Figure 2.4.4 Wrong Seam Edge Geometry Curve
3. Test the Sphere
正确生成球体后导出为brep文件即可以在Draw Test Harness中来显示及进行一些操作来验证结果的正确性。在Draw Test Harness中打开brep文件并显示球体如下图所示:
Figure 3.1 Show the Sphere from file in Draw Test Harness
将其与一个长方体进行布尔运算,效果如下图所示:
Figure 3.2 Spher and a Box
Figure 3.3 Sphere cut a Box
由上图可知,球体与长方体布尔运算结果正确。
4. Conclusion
通过生成一个球体,示例了特殊边的构造,如退化边和衔接边。需要注意的事项还是组成Wire的所有边中的PCurve必须在面的参数空间中闭合。由PCurve可知,球面对应的参数空间不是几何曲面的范围,而是在v方向上偏移了2PI。
5. References
1. OpenCascade Primitives BRep - Sphere,
http://www.cppblog.com/eryar/archive/2014/03/22/206279.html
2. PCurve - Curve on Surface,
http://www.cppblog.com/eryar/archive/2014/03/15/206180.html
3. Topology and Geometry in OpenCascade-Face,
http://www.cppblog.com/eryar/archive/2013/09/12/203199.html
PDF Version and Source code: OpenCASCADE Make Primitives - Sphere