上一篇在这 C++混合编程之idlcpp教程Python篇(8)

第一篇在这 C++混合编程之idlcpp教程(一)

与前面的工程相比,工程PythonTutorial7中除了四个文件PythonTutorial7.cpp, Tutorial7.cpp, Tutorial7.i, tutorial7.py 外,Tutorial6.cpp也被加入了此工程中。其中PythonTutorial7.cpp的内容基本和PythonTutorial6.cpp雷同,不再赘述。首先看一下Tutorial7.i的内容:

  1. #import "Tutorial6.i"
  2.  
  3. namespace tutorial
  4. {
      template<T>
  5. struct Ray3
  6. {
  7. Ray3();
  8. Ray3(const Vector3<T>& origin, const Vector3<T>& direction);
  9. void getPoint(Vector3<T>& point, T t) const;
  10. Vector3<T> getPoint(T t) const;
  11. Vector3<T> m_origin;
  12. Vector3<T> m_direction;
  13. };
  14.  
  15. export Ray3<float>;
  16. export Ray3<double>;
  17. typedef Ray3<float> Ray3f;
  18. typedef Ray3<double> Ray3d;
  19.  
  20. #{
  21.  
  22. template<typename T>
  23. inline Ray3<T>::Ray3()
  24. {}
  25.  
  26. template<typename T>
  27. inline Ray3<T>::Ray3(const Vector3<T>& origin, const Vector3<T>& direction) :
  28. m_origin(origin), m_direction(direction)
  29. {}
  30.  
  31. template<typename T>
  32. inline void Ray3<T>::getPoint(Vector3<T>& point, T t) const
  33. {
  34. point.x = m_origin.x + m_direction.x * t;
  35. point.y = m_origin.y + m_direction.y * t;
  36. point.z = m_origin.z + m_direction.z * t;
  37. }
  38. template<typename T>
  39. inline Vector3<T> Ray3<T>::getPoint(T t) const
  40. {
  41. return Vector3<T>(m_origin.x + m_direction.x * t,
  42. m_origin.y + m_direction.y * t,
  43. m_origin.z + m_direction.z * t);
  44. }
  45.  
  46. #}
  47. }

第一行

#import "Tutorial6.i"

在后面Ray3的定义中使用到了模板类Vector3,所以在此处要先引入此文件。

template<T>

struct Ray3

此处定义了模板类Ray3。其中有类型为Vector3<T>的两个成员变量m_origin和m_direction。在这个类中以 m_origin + m_direction * t  (t >= 0) 参数方程的形式表示一个射线。有两个名为getPoint的重载函数用来获取射线上的一点坐标。

export Ray3<float>;

export Ray3<double>;

模板实例化,这两行代码指示idlcpp生成相应类型的元数据信息。

typedef Ray3<float> Ray3f;

typedef Ray3<double> Ray3d;

定义类型别名,方便使用。

编译后生成的Tutorial7.h的内容如下:

  1. //DO NOT EDIT THIS FILE, it is generated by idlcpp
  2. //http://www.idlcpp.org
  3.  
  4. #pragma once
  5.  
  6. #include "./Tutorial6.h"
  7.  
  8. namespace tutorial
  9. {
  10. template<typename T>
  11. struct Ray3
  12. {
  13. public:
  14.  
  15. Ray3();
  16. Ray3(const Vector3<T>& origin,const Vector3<T>& direction);
  17. void getPoint(Vector3<T>& point,T t)const ;
  18. Vector3<T> getPoint(T t)const ;
  19. Vector3<T> m_origin;
  20. Vector3<T> m_direction;
  21. };
  22.  
  23. typedef Ray3<float> Ray3f;
  24. typedef Ray3<double> Ray3d;
  25.  
  26. template<typename T>
  27. inline Ray3<T>::Ray3()
  28. {}
  29.  
  30. template<typename T>
  31. inline Ray3<T>::Ray3(const Vector3<T>& origin, const Vector3<T>& direction) :
  32. m_origin(origin), m_direction(direction)
  33. {}
  34.  
  35. template<typename T>
  36. inline void Ray3<T>::getPoint(Vector3<T>& point, T t) const
  37. {
  38. point.x = m_origin.x + m_direction.x * t;
  39. point.y = m_origin.y + m_direction.y * t;
  40. point.z = m_origin.z + m_direction.z * t;
  41. }
  42. template<typename T>
  43. inline Vector3<T> Ray3<T>::getPoint(T t) const
  44. {
  45. return Vector3<T>(m_origin.x + m_direction.x * t,
  46. m_origin.y + m_direction.y * t,
  47. m_origin.z + m_direction.z * t);
  48. }
  49.  
  50. }

然后是Tutorial7.cpp

  1. #include "Tutorial7.h"
  2. #include "Tutorial7.mh"
  3. #include "Tutorial7.ic"
  4. #include "Tutorial7.mc"

因为模板类的代码都写在头文件中了,所以Tutorial7.cpp只需要包含对应的四个文件即可。

另外模板类Ray3用到了模板类Vector3,所以其实例化类型Ray3<float>和Ray3<double>也分别用到Vector3的实例化类型Vector3<float>和Vector3<double>,相应的Ray3<float>元数据中也会用到Vector3<float>的元数据信息。所以在这个工程中需要将Tutorial6.cpp加入进来。

最后看一下Tutorial7.py的内容

  1. p = paf.float.NewArray(3);
  2. p[0] = 1;
  3. p[1] = 2;
  4. p[2] = 3;
  5. ray = paf.tutorial.Ray3f(paf.tutorial.Vector3f.s_zero, paf.tutorial.Vector3f(p));
  6. pt = paf.tutorial.Vector3f(0,0,0);
  7. ray.getPoint(pt, 2);
  8. print(pt.x._);
  9. print(pt.y._);
  10. print(pt.z._);
  11. pt = ray.getPoint(3);
  12. print(pt.x._);
  13. print(pt.y._);
  14. print(pt.z._);

第一行:

p = paf.float.NewArray(3);

创建一个float类型的数组,共三个元素,其中float是内置的类型。C++的原生类型在idlcpp中都是支持的,如下:

  1. bool
  2. char
  3. signed char
  4. unsigned char
  5. wchar_t
  6. short
  7. unsigned short
  8. long
  9. unsigned long
  10. long long
  11. unsigned long long
  12. int
  13. unsigned int
  14. float
  15. double
  16. long double

考虑到有些类型中间有空格,为脚本使用方便,还为这些类型定义了别名,具体参见pafcore中的Typedef.i

编译执行,结果如下图:

C++混合编程之idlcpp教程Python篇(9)的更多相关文章

  1. C++混合编程之idlcpp教程Python篇(8)

    上一篇在这 C++混合编程之idlcpp教程Python篇(7) 第一篇在这 C++混合编程之idlcpp教程(一) 与前面的工程相似,工程PythonTutorial6中,同样加入了四个文件:Pyt ...

  2. C++混合编程之idlcpp教程Python篇(7)

    上一篇在这 C++混合编程之idlcpp教程Python篇(6) 第一篇在这 C++混合编程之idlcpp教程(一) 与PythonTutorial4工程相似,工程PythonTutorial5中,同 ...

  3. C++混合编程之idlcpp教程Python篇(6)

    上一篇在这 C++混合编程之idlcpp教程Python篇(5) 第一篇在这 C++混合编程之idlcpp教程(一) 工程PythonTutorial4中加入了四个文件:PythonTutorial4 ...

  4. C++混合编程之idlcpp教程Python篇(5)

    上一篇在这  C++混合编程之idlcpp教程Python篇(4) 第一篇在这 C++混合编程之idlcpp教程(一) 与前面的工程相似,工程PythonTutorial3中,同样加入了三个文件:Py ...

  5. C++混合编程之idlcpp教程Python篇(4)

    上一篇在这 C++混合编程之idlcpp教程Python篇(3) 第一篇在这 C++混合编程之idlcpp教程(一) 与前面的工程相似,工程PythonTutorial2中,同样加入了三个文件 Pyt ...

  6. C++混合编程之idlcpp教程Python篇(3)

    上一篇 C++混合编程之idlcpp教程Python篇(2) 是一个 hello world 的例子,仅仅涉及了静态函数的调用.这一篇会有新的内容. 与PythonTutorial0相似,工程Pyth ...

  7. C++混合编程之idlcpp教程Python篇(2)

    在上一篇 C++混合编程之idlcpp教程(一) 中介绍了 idlcpp 工具的使用.现在对 idlcpp 所带的示例教程进行讲解,这里针对的 Python 语言的例子.首先看第一个示例程序 Pyth ...

  8. C++混合编程之idlcpp教程Lua篇(6)

    上一篇在这 C++混合编程之idlcpp教程Lua篇(5) 第一篇在这 C++混合编程之idlcpp教程(一) 工程LuaTutorial4中加入了四个文件:LuaTutorial4.cpp, Tut ...

  9. C++混合编程之idlcpp教程Lua篇(9)

    上一篇在这 C++混合编程之idlcpp教程Lua篇(8) 第一篇在这 C++混合编程之idlcpp教程(一) 与前面的工程相比,工程LuaTutorial7中除了四个文件LuaTutorial7.c ...

随机推荐

  1. Window Server 2012 R2 没有照片查看器 打开图片都是画板问题怎么解决

    新安装了 Window Server 2012 R2 系统,感觉屌屌的样子,加上开机速度蛮快,心里略爽.结果,打开图片一看,发现竟然是画板,而且还没有照片查看器,顿时泪流满面. 后来我利用了强大的百度 ...

  2. [转] Oracle sql 查询突然变慢 -- 案例分析

    转自:http://jingyan.baidu.com/article/8275fc868ce57946a03cf692.html 一条sql突然执行变慢,耗时9秒,应用是不能改的,只能从数据库方面下 ...

  3. Python模块:struct

    各个编程语言都有自己的数据类型,当python需要接受其他语言或者网络传输来交互数据的时候,需要考虑到python的数据类型与其他平台之间交互问题.而python的struct就是解决这个问题的. s ...

  4. NC 解决启动环境报内存溢出问题

    java heap space 内存溢出 解决方法如下: 在eclipse中,window-->preferences-->Java-->Installed JREs选中JRE 点击 ...

  5. 在windows下面配置redis集群遇到的一些坑

    最近工作不忙,就决定学习一下redis.因为一直在windows下工作,不会linux,没办法就选择在windows下配置redis. windows下配置redis集群的文章有很多,比如:http: ...

  6. 详解C语言的类型转换

    1.自动类型转换 字符型变量的值实质上是一个8位的整数值,因此取值范围一般是-128-127,char型变量也可以加修饰符unsigned,则unsigned char 型变量的取值范围是0-255( ...

  7. Web程序的运行原理及流程(二)

    其实WEB服务器和WEB应用服务器这两个概念特别容易混淆  可以理解为装了不同软件(服务)的两台计算机(服务器)吧 先对两个概念做一个简单介绍 了解了基本的概念 我们再用两个典型的例子做一下比较(建立 ...

  8. 1.linux服务器的性能分析与优化

    [教程主题]:1.linux服务器的性能分析与优化 [课程录制]: 创E [主要内容] [1]影响Linux服务器性能的因素 操作系统级 CPU 目前大部分CPU在同一时间只能运行一个线程,超线程的处 ...

  9. IE全屏浏览代码

    以前做过一个网络版的商场导购触摸屏系统,用ASP写的,就是要在运行的时候全屏浏览而不能出现标题栏.工具栏.状态栏等.解决方法是用JS弹出全屏窗口,建立html文件,代码如下: <script l ...

  10. JavaScript-BOM-history:保存当前窗口打开后成功访问过的url历史记录栈

    history:保存当前窗口打开后成功访问过的url历史记录栈history.go(n):前进n步前进一步:history.go(1);后退一步:history.go(-1);刷新:history.g ...