本文来探究下OSG中的各种数组!
一、根据我平日用的较多的,(比如设置顶点数组,法线数组),先看osg::Vec3。
osg::Vec3定义在<osg/Vec3>头文件中,只有一句:
namespace osg {
typedef Vec3fVec3;
}
仅是Vec3f的别名。
二、osg::Vec3f
顾名思义,是有三个float元素的容器。下面详细探究其内部。
1. 为float定义个别名。
typedef float value_type;
这种方式经常出现(VTK也用此方式),所以说这是个好习惯?
2. 定义个枚举,标识元素个数。
enum { num_components = 3 };
为什么使用枚举而不使用int常量甚至是short呢?
3. 定义内部元素数组。
value_type _v[3];
4. 构造函数与析构函数。
Vec3f() { _v[0]=0.0f; _v[1]=0.0f;_v[2]=0.0f;}
Vec3f(value_type x,value_type y,value_type z) {_v[0]=x; _v[1]=y; _v[2]=z; }
Vec3f(const Vec2f& v2,value_typezz)
{
_v[0] =v2[0];
_v[1] =v2[1];
_v[2] =zz;
}
构造函数的三个版本都很简单。
5. 重载比较操作符。
inline bool operator == (constVec3f& v) const { return _v[0]==v._v[0]&& _v[1]==v._v[1]&& _v[2]==v._v[2]; }
inline bool operator != (constVec3f& v) const { return _v[0]!=v._v[0] ||_v[1]!=v._v[1] || _v[2]!=v._v[2]; }
inline bool operator <
(const Vec3f& v) const
{
if(_v[0]<v._v[0]) return true;
else if(_v[0]>v._v[0]) return false;
else if(_v[1]<v._v[1]) return true;
else if(_v[1]>v._v[1]) return false;
elsereturn (_v[2]<v._v[2]);
}
这里重载了三个操作符:==, !=, <,关键在于,如何比较两个向量的大小。
6. 重载中括号[]操作符,以实现下标操作。
inline value_type& operator [](int i) { return _v[i]; }
inline value_type operator [] (int i) const {return _v[i]; }
7. 重载*操作符,实现点乘、标量乘法。
inline value_type operator * (constVec3f& rhs) const
{
return_v[0]*rhs._v[0]+_v[1]*rhs._v[1]+_v[2]*rhs._v[2];
}
inline const Vec3f operator * (value_type rhs)const
{
returnVec3f(_v[0]*rhs, _v[1]*rhs, _v[2]*rhs);
}
两个向量的点乘结果是一个值。
8. 重载^操作符,实现叉乘。
inline const Vec3f operator ^ (const Vec3f&rhs) const
{
returnVec3f(_v[1]*rhs._v[2]-_v[2]*rhs._v[1],
_v[2]*rhs._v[0]-_v[0]*rhs._v[2] ,
_v[0]*rhs._v[1]-_v[1]*rhs._v[0]);
}
两个向量的叉乘是垂直于这两个向量的向量(向量叉乘的方法,我忘了这个算法
![[osg]源码分析:osg::Vec3, <wbr>osg::Vec3f [osg]源码分析:osg::Vec3, <wbr>osg::Vec3f](https://i-blog.csdnimg.cn/blog_migrate/676ebda4729556d2ae74a2569eb85a7a.gif)
)
9. 重载*=操作符,点乘后再赋值。
inline Vec3f& operator *=(value_type rhs)
{
_v[0]*=rhs;
_v[1]*=rhs;
_v[2]*=rhs;
return*this;
}
为什么要返回*this?
考虑如下:
Vec3f v1,v2,v3;
v1 = v2 *= v3;
如果没有返回*this,此语句是不合法的。但是,谁会写v1 = v2 *= v3这种可读性巨差的语句呢?
10. 重载/操作符,实现标量除法:
inline const Vec3f operator / (value_type rhs)const
{
returnVec3f(_v[0]/rhs, _v[1]/rhs, _v[2]/rhs);
}
并没有检测rhs的值,留给运行时么?
11. 重载/=操作符,实现除完后赋值。
inline Vec3f& operator /=(value_type rhs)
{
_v[0]/=rhs;
_v[1]/=rhs;
_v[2]/=rhs;
return*this;
}
12. 重载+、+=、-、-=操作符。
inline const Vec3f operator + (constVec3f& rhs) const
{
returnVec3f(_v[0]+rhs._v[0], _v[1]+rhs._v[1], _v[2]+rhs._v[2]);
}
inline Vec3f& operator += (constVec3f& rhs)
{
_v[0] +=rhs._v[0];
_v[1] +=rhs._v[1];
_v[2] +=rhs._v[2];
return*this;
}
inline const Vec3f operator - (constVec3f& rhs) const
{
returnVec3f(_v[0]-rhs._v[0], _v[1]-rhs._v[1], _v[2]-rhs._v[2]);
}
inline Vec3f& operator -= (constVec3f& rhs)
{
_v[0]-=rhs._v[0];
_v[1]-=rhs._v[1];
_v[2]-=rhs._v[2];
return*this;
}
13. 负号-操作符。
inline const Vec3f operator - () const
{
returnVec3f (-_v[0], -_v[1], -_v[2]);
}
14. 得到指向内部数组的指针。
inline value_type* ptr() { return _v; }
inline const value_type* ptr() const { return_v; }
提供了两个版本,版本2不允许修改内部元素。
15. 提供了两个版本的访问元素的方法。
inline value_type& x() { return_v[0]; }
inline value_type& y() { return_v[1]; }
inline value_type& z() { return_v[2]; }
inline value_type x() const { return _v[0];}
inline value_type y() const { return _v[1];}
inline value_type z() const { return _v[2];}
版本1返回的是引用,故而是可以修改内部值的,版本2则仅仅是读。
16. 得到向量的长度。
inline value_type length2() const
{
return_v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2];
}
17. 向量的标准化。
inline value_type normalize()
{
value_typenorm = Vec3f::length();
if(norm>0.0)
{
value_type inv =1.0f/norm;
_v[0] *= inv;
_v[1] *= inv;
_v[2] *= inv;
}
return(norm );
}
为什么在实现时不直接除以norm,而要乘以1.0f/norm。并return了nrom,有什么意义吗?
18. set值的方法。
inline void set( value_type x, value_type y,value_type z)
{
_v[0]=x;_v[1]=y; _v[2]=z;
}
inline void set( const Vec3f&rhs)
{
_v[0]=rhs._v[0]; _v[1]=rhs._v[1]; _v[2]=rhs._v[2];
}
19.另外在<osg/Vec3f>中提供了两个非成员函数:
inline Vec3f componentMultiply(const Vec3f&lhs, const Vec3f& rhs)
{
returnVec3f(lhs[0]*rhs[0], lhs[1]*rhs[1], lhs[2]*rhs[2]);
}
inline Vec3f componentDivide(const Vec3f& lhs,const Vec3f& rhs)
{
returnVec3f(lhs[0]/rhs[0], lhs[1]/rhs[1], lhs[2]/rhs[2]);
}