类 操作符 重载

55 篇文章 0 订阅
class Vec3 
{
public:
float x, y, z;


enum {MAX_INPUT_STRING = 40};

// Constructors and Destructors
Vec3() {x = y = z = 0.0f;};
Vec3(float x1, float y1, float z1) {x = x1; y = y1; z = z1;};
Vec3(float av[3]) {x = av[0]; y = av[1]; z = av[2];};
Vec3(const Vec3& v) {x = v.x; y = v.y; z = v.z;};
~Vec3() {}; // Destructor intentially does nothing

// Assignment operator
Vec3& operator=(const Vec3& v) {x = v.x; y = v.y; z = v.z; return *this;};

// Comparision operators
bool operator==(const Vec3& v) {return (x == v.x && y == v.y && z == v.z);};
bool operator!=(const Vec3& v) {return (x != v.x || y != v.y || z != v.z);};

// Scalar operations
Vec3 operator+(float f) const {return Vec3(x + f, y + f, z + f);};
Vec3 operator-(float f) const {return Vec3(x - f, y - f, z - f);};
Vec3 operator*(float f) const {return Vec3(x * f, y * f, z * f);};
Vec3 operator/(float f) const {Vec3 v1(x,y,z); if (f != 0.0f) {v1.x /= f; v1.y /= f; v1.z /= f;}; return v1;};

Vec3& operator+=(float f) {x += f; y += f; z += f; return *this;};
Vec3& operator-=(float f) {x -= f; y -= f; z -= f; return *this;};
Vec3& operator*=(float f) {x *= f; y *= f; z *= f; return *this;};
Vec3& operator/=(float f) {if(f!=0.0f){ x /= f; y /= f; z /= f;}; return *this;};

// Vector operations
Vec3 operator+(const Vec3& v) const {return Vec3(x + v.x, y + v.y, z + v.z);};
Vec3& operator+=(const Vec3& v) {x += v.x; y += v.y; z += v.z; return *this;};
Vec3 operator-(const Vec3& v) const {return Vec3(x - v.x, y - v.y, z - v.z);};
Vec3& operator-=(const Vec3& v) {x -= v.x; y -= v.y; z -= v.z; return *this;};

// Unary operators
Vec3 operator-() const {return Vec3 (-x, -y, -z); };

// Dot and Cross Products
float dot(const Vec3& v) const {return (x * v.x + y * v.y + z * v.z);};
Vec3 cross(const Vec3& v) const {return Vec3(y * v.z - z * v.y,
z * v.x - x * v.z,
x * v.y - y * v.x);};
Vec3 unitcross(const Vec3& v) const {Vec3 vr(y * v.z - z * v.y,
z * v.x - x * v.z,
x * v.y - y * v.x); vr.normalize(); return vr;};

// Miscellaneous
void normalize() {float a = float(sqrt(x*x + y*y + z*z)); if (a!=0.0f) {x/=a; y/=a; z/=a;};};
void setZero() {x = y = z = 0.0f;};
float length() {return float(sqrt(x*x + y*y + z*z));};

// Friend functions
friend Vec3 operator*(float a, const Vec3& v) {return Vec3 (a * v.x, a * v.y, a * v.z);};

// dot and cross products
float dot(const Vec3& v1, const Vec3& v2) {return (v1.x * v2.x + v1.y * v2.y +v1. z * v2.z);};
Vec3 cross(const Vec3& v1, const Vec3& v2)  {return Vec3 (v1.y * v2.z - v1.z * v2.y,
v1.z * v2.x - v1.x * v2.z,
v1.x * v2.y - v1.y * v2.x);};
Vec3 unitcross(const Vec3& v1, const Vec3& v2)  {Vec3 vr(v1.y * v2.z - v1.z * v2.y,
v1.z * v2.x - v1.x * v2.z,
v1.x * v2.y - v1.y * v2.x); 
vr.normalize(); return vr;};

// Input and Output
friend std::ostream& operator<<(std::ostream& os, const Vec3& vo);

// friend istream& operator>>(istream& is, Vec3& vi);

private:
};

#endif // #ifndef __Vec3_h


// Assignment operator
vertex& operator=(const vertex& v) 
{
if (this == &v) return *this; // check for assignment to self
_myVertex =v._myVertex;
_vertexNormal = v._vertexNormal; 
_vertNeighbors = v._vertNeighbors;
_triNeighbors = v._triNeighbors;
_bActive = v._bActive;
_cost = v._cost;
_minCostNeighbor = v._minCostNeighbor;
_index = v._index;
_QTriArea = v._QTriArea;
// copy quadric
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
_Q[i][j] = v._Q[i][j];
}
}
return *this;
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值