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

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

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

#import "Tutorial6.i"

namespace tutorial
{
  template<T>
struct Ray3
{
Ray3();
Ray3(const Vector3<T>& origin, const Vector3<T>& direction);
void getPoint(Vector3<T>& point, T t) const;
Vector3<T> getPoint(T t) const;
Vector3<T> m_origin;
Vector3<T> m_direction;
}; export Ray3<float>;
export Ray3<double>;
typedef Ray3<float> Ray3f;
typedef Ray3<double> Ray3d; #{ template<typename T>
inline Ray3<T>::Ray3()
{} template<typename T>
inline Ray3<T>::Ray3(const Vector3<T>& origin, const Vector3<T>& direction) :
m_origin(origin), m_direction(direction)
{} template<typename T>
inline void Ray3<T>::getPoint(Vector3<T>& 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<typename T>
inline Vector3<T> Ray3<T>::getPoint(T t) const
{
return Vector3<T>(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,所以在此处要先引入此文件。

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的内容如下:

//DO NOT EDIT THIS FILE, it is generated by idlcpp
//http://www.idlcpp.org #pragma once #include "./Tutorial6.h" namespace tutorial
{
template<typename T>
struct Ray3
{
public: Ray3();
Ray3(const Vector3<T>& origin,const Vector3<T>& direction);
void getPoint(Vector3<T>& point,T t)const ;
Vector3<T> getPoint(T t)const ;
Vector3<T> m_origin;
Vector3<T> m_direction;
}; typedef Ray3<float> Ray3f;
typedef Ray3<double> Ray3d; template<typename T>
inline Ray3<T>::Ray3()
{} template<typename T>
inline Ray3<T>::Ray3(const Vector3<T>& origin, const Vector3<T>& direction) :
m_origin(origin), m_direction(direction)
{} template<typename T>
inline void Ray3<T>::getPoint(Vector3<T>& 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<typename T>
inline Vector3<T> Ray3<T>::getPoint(T t) const
{
return Vector3<T>(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

编译执行,结果如下图:

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. webstorm11怎么设置成sublime3的界面

    引入架包导入即可 下载路径:https://github.com/OtaK/jetbrains-monokai-sublime

  2. hdu 5534 (完全背包) Partial Tree

    题目:这里 题意: 感觉并不能表达清楚题意,所以 Problem Description In mathematics, and more specifically in graph theory, ...

  3. IT人 转型

    IT人 转型 转自: http://blog.sina.com.cn/s/blog_88534dff0101232b.html      “35岁,技术生涯即告终结.”这种说法在it界得到众多人认可, ...

  4. Merge Intervals 运行比较快

    class Solution { public: static bool cmp(Interval &a,Interval &b) { return a.start<b.star ...

  5. 前端工作面试问题--摘取自github

    前端工作面试问题 本文包含了一些用于考查候选者的前端面试问题.不建议对单个候选者问及每个问题 (那需要好几个小时).只要从列表里挑选一些,就能帮助你考查候选者是否具备所需要的技能. 备注: 这些问题中 ...

  6. JavaSE知识结构

  7. 第八章 springboot + mybatis + 多数据源(转载)

    本篇博客转发自:http://www.cnblogs.com/java-zhao/p/5413845.html 在实际开发中,我们一个项目可能会用到多个数据库,通常一个数据库对应一个数据源. 代码结构 ...

  8. cassandra的写过程

    如下: Message, get a new request,type:QUERY      //channelRead0函数 Message, get a new request,customPay ...

  9. IIS配置excel 权限

    http://www.cnblogs.com/zhuxiaohui/archive/2013/10/16/3371637.html

  10. JS 关闭页面事件

    主页面调用:<script src="<%=ResolveUrl("../JS/QuitJS.js") %>" type="text ...