上一篇在这
第一篇在这
与前面的工程相比,工程 PythonTutorial7 中除了四个文件 PythonTutorial7.cpp, Tutorial7.cpp, Tutorial7.i, tutorial7.py 外,Tutorial6.cpp 也被加入了此工程中。其中 PythonTutorial7.cpp 的内容基本和 PythonTutorial6.cpp 雷同,不再赘述。首先看一下 Tutorial7.i 的内容:
- #import "Tutorial6.i"namespace tutorial {
- struct Ray3 {
- Ray3();
- Ray3(const Vector3 & origin, const Vector3 & direction);
- void getPoint(Vector3 & point, T t) const;
- Vector3 getPoint(T t) const;
- Vector3 m_origin;
- Vector3 m_direction;
- };
- export Ray3 < float > ;
- export Ray3 < double > ;
- typedef Ray3 < float > Ray3f;
- typedef Ray3 < double > Ray3d;# {
- template inline Ray3: :Ray3() {}
- template inline Ray3: :Ray3(const Vector3 & origin, const Vector3 & direction) : m_origin(origin),
- m_direction(direction) {}
- template inline void Ray3: :getPoint(Vector3 & point, T t) const {
- point.x = m_origin.x + m_direction.x * t;
- point.y = m_origin.y + m_direction.y * t;
- point.z = m_origin.z + m_direction.z * t;
- }
- template inline Vector3 Ray3: :getPoint(T t) const {
- return Vector3(m_origin.x + m_direction.x * t, m_origin.y + m_direction.y * t, m_origin.z + m_direction.z * t);
- }#
- }
- }
第一行
#import "Tutorial6.i"
在后面 Ray3 的定义中使用到了模板类 Vector3,所以在此处要先引入此文件。
struct Ray3<T>
此处定义了模板类 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 的内容如下:
- //DO NOT EDIT THIS FILE, it is generated by idlcpp
- //http://www.idlcpp.org
- #pragma once#include "./Tutorial6.h"namespace tutorial {
- templatestruct Ray3 {
- public: Ray3();
- Ray3(const Vector3 & origin, const Vector3 & direction);
- void getPoint(Vector3 & point, T t) const;
- Vector3 getPoint(T t) const;
- Vector3 m_origin;
- Vector3 m_direction;
- };
- typedef Ray3 < float > Ray3f;
- typedef Ray3 < double > Ray3d;
- template inline Ray3: :Ray3() {}
- template inline Ray3: :Ray3(const Vector3 & origin, const Vector3 & direction) : m_origin(origin),
- m_direction(direction) {}
- template inline void Ray3: :getPoint(Vector3 & point, T t) const {
- point.x = m_origin.x + m_direction.x * t;
- point.y = m_origin.y + m_direction.y * t;
- point.z = m_origin.z + m_direction.z * t;
- }
- template inline Vector3 Ray3: :getPoint(T t) const {
- return Vector3(m_origin.x + m_direction.x * t, m_origin.y + m_direction.y * t, m_origin.z + m_direction.z * t);
- }
- }
然后是 Tutorial7.cpp
- #include "Tutorial7.h"#include "Tutorial7.mh"#include "Tutorial7.ic"#include "Tutorial7.mc"
因为模板类的代码都写在头文件中了,所以 Tutorial7.cpp 只需要包含对应的四个文件即可。
另外模板类 Ray3 用到了模板类 Vector3,所以其实例化类型 Ray3<float> 和 Ray3<double> 也分别用到 Vector3 的实例化类型 Vector3<float> 和 Vector3<double>,相应的 Ray3<float> 元数据中也会用到 Vector3<float> 的元数据信息。所以在这个工程中需要将 Tutorial6.cpp 加入进来。
最后看一下 Tutorial7.py 的内容
- p = paf.float.NewArray(3);
- p[0] = 1;
- p[1] = 2;
- p[2] = 3;
- ray = paf.tutorial.Ray3f(paf.tutorial.Vector3f.s_zero, paf.tutorial.Vector3f(p));
- pt = paf.tutorial.Vector3f(0, 0, 0);
- ray.getPoint(pt, 2);
- print(pt.x._);
- print(pt.y._);
- print(pt.z._);
- pt = ray.getPoint(3);
- print(pt.x._);
- print(pt.y._);
- print(pt.z._);
第一行:
p = paf.float.NewArray(3);
创建一个 float 类型的数组,共三个元素,其中 float 是内置的类型。C++ 的原生类型在 idlcpp 中都是支持的,如下:
- bool char signed char unsigned char wchar_t short unsigned short long unsigned long long long unsigned long long int unsigned int float double long double
考虑到有些类型中间有空格,为脚本使用方便,还为这些类型定义了别名,具体参见 pafcore 中的 Typedef.i
编译执行,结果如下图:
来源: http://www.cnblogs.com/fdyjfd/p/5844176.html