OpenCASCADE 包说明
转载地址:http://www.cppblog.com/eryar/archive/2012/06/30/180916.html
一、简介 Introduction to Package gp
gp是几何处理程序包(Geometric Processor package),简称gp。包gp提供以下功能:
- 代数计算;如坐标计算、矩阵计算;
- 基本解析几何元素;如变换、点、矢量、线、面、轴、二次曲线和初等曲面;
这些实体同时在二维和三维空间中定义,且包中的类都是非持续的(non-persistent),即这些类的实例都是以值的方式处理而不是引用。当复制这种对象时,是对象本体。改变一个实例不会影响到其他的实例。
可用的几何实体如下所示:
- 2D&3D Cartesian coordinates(x,y,z); 二维&三维笛卡尔坐标;
- Matrices; 矩阵;
- Cartesian points; 笛卡尔坐标点;
- Vector; 矢量;
- Direction; 方向;
- Axis; 轴;
- Line; 直线;
- Circle; 圆;
- Ellipse; 椭圆;
- Hyperbola; 双曲线;
- Parabola; 抛物线;
- Plane; 面;
- Infinite cylindrical surface; 柱面;
- Spherical surface; 球面;
- Toroidal surface; 环面;
- Conical surface; 锥面;
二、几何元素的集合 Collections of Primitive Geometric Types
创建几何对象之前,根据你是将几何对象用于二维还是三维来确定。若你需要一个几何对象集而不是单一的几何对象,即用来处理一类几何元素,包TColgp就是提供这种功能的。
Package TColgp提供类如:XY, XYZ, Pnt, Pnt2d, Vec, Vec2d, Lin, Lin2D, Circ, Circ2d的TCollection的实例。包中的类简单列举如下:
- TColgp_Array1OfCirc;
- TColgp_Array1OfDir;
- TColgp_Array1OfPnt;
- TColgp_Array1OfVec;
- TColgp_Array2OfCirc2d;
- TColgp_Array2OfPnt;
- TColgp_HArray1OfCirc2d;
- TColgp_HArray2OfDir;
- TColgp_HSequenceOfDir;
- TColgp_SequenceOfDir;
- TColgp_SequenceOfPnt;
- TColgp_SequenceOfXYZ;
个人意见,若使用标准C++的容器类(The STL Template Container),就不需要创建这么多类了。
三、基本几何库 Basic Geometric Libraries
有几个库提供了曲线和曲面的基本计算功能。若要处理由包gp创建的几何对象,初等曲线曲面的有用算法库在包:ElCLib和ElSLib中。包Precision提供两个数字比较的功能。
- Package ElCLib; ElCLib代表:Elementary Curves Library. 提供初等曲线曲面的基本几何计算功能;
- Package ElSLib; ElSLib代表:Elementary Surfaces Library. 提供初等曲面的基本几何计算。
- Package Bnd;提供二维和三维空间中几何元素包围盒的计算功能;
- Package Precision; 由于浮点数在计算机内实际上是一个近似表示,在手工计算看来为正确的结果,在计算机中运算未必得出正确的结果。所以,我们得到一个重要的经验:使用浮点数进行相等(==)和不等(!=)比较的操作通常是有问题的。浮点数的相等比较,一般总是使用两者相减的值是否落在0的邻域中来判断。这就是邻域比较技术。在OpenCASCADE中专门提供包Precision来处理两个数值的比较问题。
四、代码示例 Code Sample
- //------------------------------------------------------------------------------
- // Copyright (c) 2012 eryar All Rights Reserved.
- //
- // File : Main.cpp
- // Author : eryar@163.com
- // Date : 2012-6-23 21:30
- // Version : 1.0v
- //
- // Description : Test primitive Geometric Types in OpenCASCADE.
- //
- // The Geometric Processor package, called gp.
- //
- // The pg package offers classes for both 2D and 3D objects which
- // are handled by value rather than by reference. When this sort of object
- // is copied, it is copied entirely. Changes in one instance will not be
- // reflected in another.
- //
- //==============================================================================
- // Use Toolkit TKernel.
- #pragma comment(lib,"TKernel.lib")
- // Use Toolkit TKMath.
- #pragma comment(lib, "TKMath.lib")
- #include <gp.hxx>
- #include <gp_Pnt.hxx>
- #include <gp_Trsf.hxx>
- #include <Precision.hxx>
- void DumpPoint(const gp_Pnt& p);
- int main(int argc, char* argv[])
- {
- gp_Pnt aPoint(, , );
- // 1. Translate a point in a direction.
- // The direction determined by a gp_Vec or two gp_Pnt.
- cout<<"Before translated:";
- DumpPoint(aPoint);
- aPoint.Translate(gp_Pnt(, , ), gp_Pnt(, , ));
- cout<<"After translated:";
- DumpPoint(aPoint);
- // 2. Rotate a point.
- // Rotate a point by an axis and the rotate angle.
- cout<<"Before rotated:";
- DumpPoint(aPoint);
- // Roate 45 degree about Z axis.
- // Positive angle value will be rotated counterclockwise.
- aPoint.Rotate(gp::OZ(), PI/);
- cout<<"After rotated 45 degree about Z axis:";
- DumpPoint(aPoint);
- // 2.1 Test Package Precision.
- if (aPoint.X() < Precision::Confusion() && aPoint.X() > -Precision::Confusion())
- {
- cout<<"Point X value:"<<aPoint.X()<<endl;
- cout<<"Precision::Confusion() value:"<<Precision::Confusion()<<endl;
- }
- aPoint.Rotate(gp::OZ(), PI/);
- cout<<"After rotate 45 degree about Z axis:";
- DumpPoint(aPoint);
- // 3. Transform a point by gp_Trsf.
- gp_Trsf transform;
- transform.SetMirror(gp::OX());
- cout<<"Before gp_Trsf:";
- DumpPoint(aPoint);
- aPoint.Transform(transform);
- cout<<"After gp_Trsf:";
- DumpPoint(aPoint);
- // 4. Mirror a point.
- // 4.1 Performs the symmetrical transformation of
- // a point with respect to an axis placement which
- // is the axis of the symmetry.
- cout<<"Before mirrored with a symmetric axis:";
- DumpPoint(aPoint);
- aPoint.Mirror(gp::OY());
- cout<<"After mirrored with a symmetric axis:";
- DumpPoint(aPoint);
- // 4.2 Performs the symmetrical transformation of
- // a point with respect to a plane.
- cout<<"Before mirrored with a symmetric plane:";
- DumpPoint(aPoint);
- aPoint.Mirror(gp::XOY());
- cout<<"After mirrored with a symmetric plane";
- DumpPoint(aPoint);
- // 5. Scale a point.
- aPoint.SetCoord(, , );
- cout<<"Before Scaled:";
- DumpPoint(aPoint);
- /*
- // Scale point source code...
- inline void gp_Pnt::Scale (const gp_Pnt& P,
- const Standard_Real S)
- {
- gp_XYZ XYZ = P.coord;
- XYZ.Multiply (1.0 - S);
- coord.Multiply (S);
- coord.Add (XYZ);
- }
- */
- aPoint.Scale(gp_Pnt(, , ), );
- cout<<"After Scaled:";
- DumpPoint(aPoint);
- return ;
- }
- /**
- * Description: Dump point information.
- */
- void DumpPoint( const gp_Pnt& p )
- {
- cout<<"("<<p.X()<<","<<p.Y()<<","<<p.Z()<<")"<<endl;
- }
- /*
- 输出结果如下:
- 1: Before translated:(0,0,0)
- 2: After translated:(8,8,-3)
- 3: Before rotated:(8,8,-3)
- 4: After rotated 45 degree about Z axis:(8.88178e-016,11.3137,-3)
- 5: Point X value:8.88178e-016
- 6: Precision::Confusion() value:1e-007
- 7: After rotate 45 degree about Z axis:(-8,8,-3)
- 8: Before gp_Trsf:(-8,8,-3)
- 9: After gp_Trsf:(-8,-8,3)
- 10: Before mirrored with a symmetric axis:(-8,-8,3)
- 11: After mirrored with a symmetric axis:(8,-8,-3)
- 12: Before mirrored with a symmetric plane:(8,-8,-3)
- 13: After mirrored with a symmetric plane(8,-8,3)
- 14: Before Scaled:(1,2,1)
- 15: After Scaled:(1,2,0)
- 16: Press any key to continue . . .
- */
五、结论
包gp提供了基本的几何元素表示及初等解析几何计算功能。对于几何元素的集合也有自己的类库。对于两个数值的比较采用了邻域比较技术。
Python代码:
- #!/usr/bin/env python
- # -*- coding:utf-8 -*-
- from OCC.gp import *
- from OCC.TColgp import *
- from OCC.TColStd import *
- from OCC.Precision import *
- PI = 3.141592653589793238
- def dump_point(pnt: gp_Pnt):
- print('\t(%s, %s, %s)' % pnt.Coord())
- def main_basic():
- # 1.1 基本对象
- # (1) OCgp_Pnt类:创建三维空间上的一个几何点对象。
- pt1 = gp_Pnt(113, 0, 0.05)
- # print(pt1.Coord())
- pt2 = gp_Pnt(1, 2, 2)
- # (2) OCTColgp_Array1OfPnt类:创建三维空间几何点的一维数组对象。
- array1 = TColgp_Array1OfPnt(0, 1)
- array1.SetValue(0, pt1)
- array1.SetValue(1, pt2)
- # (3) OCTColgp_Array2OfPnt:二维点数组
- Poles = TColgp_Array2OfPnt(1, 2, 1, 4)
- Poles.SetValue(1, 1, gp_Pnt(0, 0, 0))
- Poles.SetValue(1, 2, gp_Pnt(0, 10, 2))
- Poles.SetValue(1, 3, gp_Pnt(0, 20, 10))
- Poles.SetValue(1, 4, gp_Pnt(0, 30, 0))
- Poles.SetValue(2, 1, gp_Pnt(10, 0, 5))
- Poles.SetValue(2, 2, gp_Pnt(10, 10, 3))
- Poles.SetValue(2, 3, gp_Pnt(10, 20, 20))
- Poles.SetValue(2, 4, gp_Pnt(10, 30, 0))
- # (4) OCTColStd_Array1OfReal:一维Double数组
- # (5) OCTColStd_Array1OfInteger:一维Integer数组
- UKnots = TColStd_Array1OfReal(1, 2)
- UKnots.SetValue(1, 0)
- UKnots.SetValue(2, 1)
- # (13) OCgp_Mat:矩阵对象
- rot = gp_Mat(1, 0, 0, 0, 0.5, 0, 0, 0, 1.5)
- def main():
- aPoint = gp_Pnt(0, 0, 0)
- print('平移前:')
- dump_point(aPoint)
- aPoint.Translate(gp_Pnt(2, 2, 3), gp_Pnt(10, 10, 0))
- print('平移后:')
- dump_point(aPoint)
- # Roate 45 degree about Z axis.
- # Positive angle value will be rotated counterclockwise.
- aPoint.Rotate(gp_OZ(), PI/4);
- print('绕Z轴旋转45度后:')
- dump_point(aPoint)
- # 2.1 Test Package Precision.
- if (aPoint.X() < precision_Confusion() and aPoint.X() > -precision_Confusion()):
- print("\t点 X 值:", aPoint.X())
- print("\tPrecision::Confusion() 值:", precision_Confusion())
- aPoint.Rotate(gp_OZ(), PI/4);
- print('再绕Z轴旋转45度后:')
- dump_point(aPoint)
- if (aPoint.X() + 8 < precision_Confusion() and aPoint.X() + 8 > -precision_Confusion()):
- print("\t点 X 值:", aPoint.X())
- print("\tPrecision::Confusion() 值:", precision_Confusion())
- aPoint.Mirror(gp_OY())
- print('相对Y轴镜像后:')
- dump_point(aPoint)
- aPoint.Mirror(gp_XOY())
- print('相对XOY平面镜像后:')
- dump_point(aPoint)
- aPoint.SetCoord(1, 2, 1)
- print('缩放前:')
- dump_point(aPoint)
- scale_center_point = gp_Pnt(1, 2, 2)
- aPoint.Scale(gp_Pnt(1, 2, 2), 2)
- print('相对点%s缩放后:' % (scale_center_point.Coord(), ))
- dump_point(aPoint)
- if __name__ == '__main__':
- main()
OpenCASCADE 包说明的更多相关文章
- Convert BSpline Curve to Arc Spline in OpenCASCADE
Convert BSpline Curve to Arc Spline in OpenCASCADE eryar@163.com Abstract. The paper based on OpenCA ...
- OpenCASCADE Expression Interpreter by Flex & Bison
OpenCASCADE Expression Interpreter by Flex & Bison eryar@163.com Abstract. OpenCASCADE provide d ...
- OpenCASCADE Interpolations and Approximations
OpenCASCADE Interpolations and Approximations eryar@163.com Abstract. In modeling, it is often requi ...
- OpenCASCADE Interpolation - Lagrange
OpenCASCADE Interpolation - Lagrange eryar@163.com Abstract. Power basis polynomial is the most simp ...
- Create views of OpenCASCADE objects in the Debugger
Create views of OpenCASCADE objects in the Debugger eryar@163.com Abstract. The Visual Studio Natvis ...
- OpenCASCADE Make Primitives-Box
OpenCASCADE Make Primitives-Box eryar@163.com Abstract. By making a simple box to demonstrate the BR ...
- OpenCASCADE BRep vs. OpenNURBS BRep
OpenCASCADE BRep vs. OpenNURBS BRep eryar@163.com Abstract. BRep short for Boundary Representation. ...
- OpenCASCADE Root-Finding Algorithm
OpenCASCADE Root-Finding Algorithm eryar@163.com Abstract. A root-finding algorithm is a numerical m ...
- OpenCASCADE Conic to BSpline Curves-Hyperbola
OpenCASCADE Conic to BSpline Curves-Hyperbola eryar@163.com Abstract. Rational Bezier Curve can repr ...
随机推荐
- Java_Web之Servlet基础
请简要描述EL表达式的语法及使用特点? ${ EL exprission } ${ bean.name } 或 ${ bean['name'] } 请简要描述什么是JSTL? JSP标准标签库 ...
- JavaScript小技巧总结
JavaScript是一种脚本语言: 语法类似于常见的高级语言 脚本语言,不需要编译就可以由解释器直接运行 变量松散定义 面向对象 JSON是一种数据交换格式,而JSONP是JSON的一种使用模式,是 ...
- PKCS #1 RSA Encryption Version 1.5 填充方式
在进行RSA运算时需要将源数据D转化为Encryption block(EB).其中pkcs1padding V1.5的填充模式安装以下方式进行 (1) EB = 00+ BT+PS +00 + D ...
- 微信jssdk图片上传
一.html页面如下: <div class="weui-cell"> <div class="weui-cell__hd"></ ...
- 数据结构总结(UPDATING......)
目标: 1.栈........√ 2.队列......√ 3.堆.........× 4.并查集...× 栈: #define MAXN 65536 struct stack{ int sz[MAXN ...
- NOIP2013 DAY2 T3火车运输
传送门 题目描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情况 ...
- Oracle 解决表死锁
select 'alter system kill session ''' || SID || ',' || SERIAL# || ''';' from ( select distinct a.sid ...
- 3分钟实现小程序唤起微信支付 Laravel教程
微信支付的接入,如果不使用成熟的开发包,将是巨大的工作量. 依赖 EasyWechat 先在 laravel 项目中依赖 easywechat 这个包 composer require "o ...
- Optimization on content service with local search in cloud of clouds
曾老师的这篇文章发表于Journal of Network and Computer Applications,主要解决的是利用启发式算法决定如何在cloud of clouds中进行副本分发,满足用 ...
- 0419如何利用关系角度看待SQL
转自http://www.open-open.com/solution/view/1389339225820 十步完全理解SQL 1. SQL 是一种声明式语言 首先要把这个概念记在脑中:“声明” ...