转载地址:http://www.cppblog.com/eryar/archive/2012/06/30/180916.html
一、简介 Introduction to Package gp

gp是几何处理程序包(Geometric Processor package),简称gp。包gp提供以下功能:

  • 代数计算;如坐标计算、矩阵计算;
  • 基本解析几何元素;如变换、点、矢量、线、面、轴、二次曲线和初等曲面;

这些实体同时在二维和三维空间中定义,且包中的类都是非持续的(non-persistent),即这些类的实例都是以值的方式处理而不是引用。当复制这种对象时,是对象本体。改变一个实例不会影响到其他的实例。

可用的几何实体如下所示:

  1. 2D&3D Cartesian coordinates(x,y,z); 二维&三维笛卡尔坐标;
  2. Matrices; 矩阵;
  3. Cartesian points; 笛卡尔坐标点;
  4. Vector; 矢量;
  5. Direction; 方向;
  6. Axis; 轴;
  7. Line; 直线;
  8. Circle; 圆;
  9. Ellipse; 椭圆;
  10. Hyperbola; 双曲线;
  11. Parabola; 抛物线;
  12. Plane; 面;
  13. Infinite cylindrical surface; 柱面;
  14. Spherical surface; 球面;
  15. Toroidal surface; 环面;
  16. Conical surface; 锥面;
二、几何元素的集合 Collections of Primitive Geometric Types

创建几何对象之前,根据你是将几何对象用于二维还是三维来确定。若你需要一个几何对象集而不是单一的几何对象,即用来处理一类几何元素,包TColgp就是提供这种功能的。

Package TColgp提供类如:XY, XYZ, Pnt, Pnt2d, Vec, Vec2d, Lin, Lin2D, Circ, Circ2dTCollection的实例。包中的类简单列举如下:

  • 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创建的几何对象,初等曲线曲面的有用算法库在包:ElCLibElSLib中。包Precision提供两个数字比较的功能。

  • Package ElCLib; ElCLib代表:Elementary Curves Library. 提供初等曲线曲面的基本几何计算功能;
  • Package ElSLib; ElSLib代表:Elementary Surfaces Library. 提供初等曲面的基本几何计算。
  • Package Bnd;提供二维和三维空间中几何元素包围盒的计算功能;
  • Package Precision; 由于浮点数在计算机内实际上是一个近似表示,在手工计算看来为正确的结果,在计算机中运算未必得出正确的结果。所以,我们得到一个重要的经验:使用浮点数进行相等(==)和不等(!=)比较的操作通常是有问题的。浮点数的相等比较,一般总是使用两者相减的值是否落在0的邻域中来判断。这就是邻域比较技术。在OpenCASCADE中专门提供包Precision来处理两个数值的比较问题。
四、代码示例 Code Sample
  1. //------------------------------------------------------------------------------
  2. // Copyright (c) 2012 eryar All Rights Reserved.
  3. //
  4. // File : Main.cpp
  5. // Author : eryar@163.com
  6. // Date : 2012-6-23 21:30
  7. // Version : 1.0v
  8. //
  9. // Description : Test primitive Geometric Types in OpenCASCADE.
  10. //
  11. // The Geometric Processor package, called gp.
  12. //
  13. // The pg package offers classes for both 2D and 3D objects which
  14. // are handled by value rather than by reference. When this sort of object
  15. // is copied, it is copied entirely. Changes in one instance will not be
  16. // reflected in another.
  17. //
  18. //==============================================================================
  19.  
  20. // Use Toolkit TKernel.
  21. #pragma comment(lib,"TKernel.lib")
  22. // Use Toolkit TKMath.
  23. #pragma comment(lib, "TKMath.lib")
  24.  
  25. #include <gp.hxx>
  26. #include <gp_Pnt.hxx>
  27. #include <gp_Trsf.hxx>
  28. #include <Precision.hxx>
  29.  
  30. void DumpPoint(const gp_Pnt& p);
  31.  
  32. int main(int argc, char* argv[])
  33. {
  34. gp_Pnt aPoint(, , );
  35.  
  36. // 1. Translate a point in a direction.
  37. // The direction determined by a gp_Vec or two gp_Pnt.
  38. cout<<"Before translated:";
  39. DumpPoint(aPoint);
  40.  
  41. aPoint.Translate(gp_Pnt(, , ), gp_Pnt(, , ));
  42.  
  43. cout<<"After translated:";
  44. DumpPoint(aPoint);
  45.  
  46. // 2. Rotate a point.
  47. // Rotate a point by an axis and the rotate angle.
  48. cout<<"Before rotated:";
  49. DumpPoint(aPoint);
  50.  
  51. // Roate 45 degree about Z axis.
  52. // Positive angle value will be rotated counterclockwise.
  53. aPoint.Rotate(gp::OZ(), PI/);
  54.  
  55. cout<<"After rotated 45 degree about Z axis:";
  56. DumpPoint(aPoint);
  57.  
  58. // 2.1 Test Package Precision.
  59. if (aPoint.X() < Precision::Confusion() && aPoint.X() > -Precision::Confusion())
  60. {
  61. cout<<"Point X value:"<<aPoint.X()<<endl;
  62. cout<<"Precision::Confusion() value:"<<Precision::Confusion()<<endl;
  63. }
  64.  
  65. aPoint.Rotate(gp::OZ(), PI/);
  66. cout<<"After rotate 45 degree about Z axis:";
  67. DumpPoint(aPoint);
  68.  
  69. // 3. Transform a point by gp_Trsf.
  70. gp_Trsf transform;
  71. transform.SetMirror(gp::OX());
  72.  
  73. cout<<"Before gp_Trsf:";
  74. DumpPoint(aPoint);
  75.  
  76. aPoint.Transform(transform);
  77.  
  78. cout<<"After gp_Trsf:";
  79. DumpPoint(aPoint);
  80.  
  81. // 4. Mirror a point.
  82. // 4.1 Performs the symmetrical transformation of
  83. // a point with respect to an axis placement which
  84. // is the axis of the symmetry.
  85. cout<<"Before mirrored with a symmetric axis:";
  86. DumpPoint(aPoint);
  87.  
  88. aPoint.Mirror(gp::OY());
  89.  
  90. cout<<"After mirrored with a symmetric axis:";
  91. DumpPoint(aPoint);
  92.  
  93. // 4.2 Performs the symmetrical transformation of
  94. // a point with respect to a plane.
  95. cout<<"Before mirrored with a symmetric plane:";
  96. DumpPoint(aPoint);
  97.  
  98. aPoint.Mirror(gp::XOY());
  99.  
  100. cout<<"After mirrored with a symmetric plane";
  101. DumpPoint(aPoint);
  102.  
  103. // 5. Scale a point.
  104. aPoint.SetCoord(, , );
  105. cout<<"Before Scaled:";
  106. DumpPoint(aPoint);
  107.  
  108. /*
  109. // Scale point source code...
  110. inline void gp_Pnt::Scale (const gp_Pnt& P,
  111. const Standard_Real S)
  112. {
  113. gp_XYZ XYZ = P.coord;
  114. XYZ.Multiply (1.0 - S);
  115. coord.Multiply (S);
  116. coord.Add (XYZ);
  117. }
  118. */
  119.  
  120. aPoint.Scale(gp_Pnt(, , ), );
  121.  
  122. cout<<"After Scaled:";
  123. DumpPoint(aPoint);
  124.  
  125. return ;
  126. }
  127.  
  128. /**
  129. * Description: Dump point information.
  130. */
  131. void DumpPoint( const gp_Pnt& p )
  132. {
  133. cout<<"("<<p.X()<<","<<p.Y()<<","<<p.Z()<<")"<<endl;
  134. }
  135.  
  136. /*
  137. 输出结果如下:
  138. 1: Before translated:(0,0,0)
  139. 2: After translated:(8,8,-3)
  140. 3: Before rotated:(8,8,-3)
  141. 4: After rotated 45 degree about Z axis:(8.88178e-016,11.3137,-3)
  142. 5: Point X value:8.88178e-016
  143. 6: Precision::Confusion() value:1e-007
  144. 7: After rotate 45 degree about Z axis:(-8,8,-3)
  145. 8: Before gp_Trsf:(-8,8,-3)
  146. 9: After gp_Trsf:(-8,-8,3)
  147. 10: Before mirrored with a symmetric axis:(-8,-8,3)
  148. 11: After mirrored with a symmetric axis:(8,-8,-3)
  149. 12: Before mirrored with a symmetric plane:(8,-8,-3)
  150. 13: After mirrored with a symmetric plane(8,-8,3)
  151. 14: Before Scaled:(1,2,1)
  152. 15: After Scaled:(1,2,0)
  153. 16: Press any key to continue . . .
  154. */
五、结论

gp提供了基本的几何元素表示及初等解析几何计算功能。对于几何元素的集合也有自己的类库。对于两个数值的比较采用了邻域比较技术。

Python代码:

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3.  
  4. from OCC.gp import *
  5. from OCC.TColgp import *
  6. from OCC.TColStd import *
  7. from OCC.Precision import *
  8. PI = 3.141592653589793238
  9.  
  10. def dump_point(pnt: gp_Pnt):
  11. print('\t(%s, %s, %s)' % pnt.Coord())
  12.  
  13. def main_basic():
  14. # 1.1 基本对象
  15. # (1) OCgp_Pnt类:创建三维空间上的一个几何点对象。
  16. pt1 = gp_Pnt(113, 0, 0.05)
  17. # print(pt1.Coord())
  18. pt2 = gp_Pnt(1, 2, 2)
  19.  
  20. # (2) OCTColgp_Array1OfPnt类:创建三维空间几何点的一维数组对象。
  21. array1 = TColgp_Array1OfPnt(0, 1)
  22. array1.SetValue(0, pt1)
  23. array1.SetValue(1, pt2)
  24.  
  25. # (3) OCTColgp_Array2OfPnt:二维点数组
  26. Poles = TColgp_Array2OfPnt(1, 2, 1, 4)
  27. Poles.SetValue(1, 1, gp_Pnt(0, 0, 0))
  28. Poles.SetValue(1, 2, gp_Pnt(0, 10, 2))
  29. Poles.SetValue(1, 3, gp_Pnt(0, 20, 10))
  30. Poles.SetValue(1, 4, gp_Pnt(0, 30, 0))
  31. Poles.SetValue(2, 1, gp_Pnt(10, 0, 5))
  32. Poles.SetValue(2, 2, gp_Pnt(10, 10, 3))
  33. Poles.SetValue(2, 3, gp_Pnt(10, 20, 20))
  34. Poles.SetValue(2, 4, gp_Pnt(10, 30, 0))
  35.  
  36. # (4) OCTColStd_Array1OfReal:一维Double数组
  37. # (5) OCTColStd_Array1OfInteger:一维Integer数组
  38. UKnots = TColStd_Array1OfReal(1, 2)
  39. UKnots.SetValue(1, 0)
  40. UKnots.SetValue(2, 1)
  41.  
  42. # (13) OCgp_Mat:矩阵对象
  43. rot = gp_Mat(1, 0, 0, 0, 0.5, 0, 0, 0, 1.5)
  44.  
  45. def main():
  46. aPoint = gp_Pnt(0, 0, 0)
  47. print('平移前:')
  48. dump_point(aPoint)
  49.  
  50. aPoint.Translate(gp_Pnt(2, 2, 3), gp_Pnt(10, 10, 0))
  51. print('平移后:')
  52. dump_point(aPoint)
  53.  
  54. # Roate 45 degree about Z axis.
  55. # Positive angle value will be rotated counterclockwise.
  56. aPoint.Rotate(gp_OZ(), PI/4);
  57. print('绕Z轴旋转45度后:')
  58. dump_point(aPoint)
  59.  
  60. # 2.1 Test Package Precision.
  61. if (aPoint.X() < precision_Confusion() and aPoint.X() > -precision_Confusion()):
  62. print("\t点 X 值:", aPoint.X())
  63. print("\tPrecision::Confusion() 值:", precision_Confusion())
  64.  
  65. aPoint.Rotate(gp_OZ(), PI/4);
  66. print('再绕Z轴旋转45度后:')
  67. dump_point(aPoint)
  68. if (aPoint.X() + 8 < precision_Confusion() and aPoint.X() + 8 > -precision_Confusion()):
  69. print("\t点 X 值:", aPoint.X())
  70. print("\tPrecision::Confusion() 值:", precision_Confusion())
  71.  
  72. aPoint.Mirror(gp_OY())
  73. print('相对Y轴镜像后:')
  74. dump_point(aPoint)
  75.  
  76. aPoint.Mirror(gp_XOY())
  77. print('相对XOY平面镜像后:')
  78. dump_point(aPoint)
  79.  
  80. aPoint.SetCoord(1, 2, 1)
  81. print('缩放前:')
  82. dump_point(aPoint)
  83.  
  84. scale_center_point = gp_Pnt(1, 2, 2)
  85. aPoint.Scale(gp_Pnt(1, 2, 2), 2)
  86. print('相对点%s缩放后:' % (scale_center_point.Coord(), ))
  87. dump_point(aPoint)
  88.  
  89. if __name__ == '__main__':
  90. main()

OpenCASCADE 包说明的更多相关文章

  1. 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 ...

  2. OpenCASCADE Expression Interpreter by Flex & Bison

    OpenCASCADE Expression Interpreter by Flex & Bison eryar@163.com Abstract. OpenCASCADE provide d ...

  3. OpenCASCADE Interpolations and Approximations

    OpenCASCADE Interpolations and Approximations eryar@163.com Abstract. In modeling, it is often requi ...

  4. OpenCASCADE Interpolation - Lagrange

    OpenCASCADE Interpolation - Lagrange eryar@163.com Abstract. Power basis polynomial is the most simp ...

  5. Create views of OpenCASCADE objects in the Debugger

    Create views of OpenCASCADE objects in the Debugger eryar@163.com Abstract. The Visual Studio Natvis ...

  6. OpenCASCADE Make Primitives-Box

    OpenCASCADE Make Primitives-Box eryar@163.com Abstract. By making a simple box to demonstrate the BR ...

  7. OpenCASCADE BRep vs. OpenNURBS BRep

    OpenCASCADE BRep vs. OpenNURBS BRep eryar@163.com Abstract. BRep short for Boundary Representation. ...

  8. OpenCASCADE Root-Finding Algorithm

    OpenCASCADE Root-Finding Algorithm eryar@163.com Abstract. A root-finding algorithm is a numerical m ...

  9. OpenCASCADE Conic to BSpline Curves-Hyperbola

    OpenCASCADE Conic to BSpline Curves-Hyperbola eryar@163.com Abstract. Rational Bezier Curve can repr ...

随机推荐

  1. Java_Web之Servlet基础

    请简要描述EL表达式的语法及使用特点? ${  EL exprission } ${  bean.name } 或  ${  bean['name'] } 请简要描述什么是JSTL? JSP标准标签库 ...

  2. JavaScript小技巧总结

    JavaScript是一种脚本语言: 语法类似于常见的高级语言 脚本语言,不需要编译就可以由解释器直接运行 变量松散定义 面向对象 JSON是一种数据交换格式,而JSONP是JSON的一种使用模式,是 ...

  3. PKCS #1 RSA Encryption Version 1.5 填充方式

    在进行RSA运算时需要将源数据D转化为Encryption block(EB).其中pkcs1padding V1.5的填充模式安装以下方式进行 (1) EB = 00+ BT+PS +00 + D ...

  4. 微信jssdk图片上传

    一.html页面如下: <div class="weui-cell"> <div class="weui-cell__hd"></ ...

  5. 数据结构总结(UPDATING......)

    目标: 1.栈........√ 2.队列......√ 3.堆.........× 4.并查集...× 栈: #define MAXN 65536 struct stack{ int sz[MAXN ...

  6. NOIP2013 DAY2 T3火车运输

    传送门 题目描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情况 ...

  7. Oracle 解决表死锁

    select 'alter system kill session ''' || SID || ',' || SERIAL# || ''';' from ( select distinct a.sid ...

  8. 3分钟实现小程序唤起微信支付 Laravel教程

    微信支付的接入,如果不使用成熟的开发包,将是巨大的工作量. 依赖 EasyWechat 先在 laravel 项目中依赖 easywechat 这个包 composer require "o ...

  9. Optimization on content service with local search in cloud of clouds

    曾老师的这篇文章发表于Journal of Network and Computer Applications,主要解决的是利用启发式算法决定如何在cloud of clouds中进行副本分发,满足用 ...

  10. 0419如何利用关系角度看待SQL

    转自http://www.open-open.com/solution/view/1389339225820 十步完全理解SQL   1. SQL 是一种声明式语言 首先要把这个概念记在脑中:“声明” ...