added 3D Vec3 class and it's operators.

This commit is contained in:
pezy_mbp
2014-09-09 00:13:58 +08:00
parent c83e03a4a9
commit 4789e90a85
2 changed files with 208 additions and 0 deletions

127
Vec3.h Normal file
View File

@@ -0,0 +1,127 @@
///
///@Class Vec3
///@Brief Simple 3D vector class
///@Author PEZY
///@Date Sept. 2014
///@See
///
#include <cmath>
namespace B3D
{
class Vec3 {
public:
Vec3() {}
Vec3(const Vec3 &v) : x(v.x), y(v.y), z(v.z) {}
Vec3(float fx, float fy, float fz) : x(fx), y(fy), z(fz) {}
Vec3 &operator =(const Vec3 &v)
{
x = v.x; y = v.y; z = v.z;
return *this;
}
bool operator ==(const Vec3 &v) const
{
return x == v.x && y == v.y && z == v.z;
}
bool operator !=(const Vec3 &v) const
{
return x != v.x || y != v.y || z != v.z;
}
void zero() { x = y = z = 0.0f; }
Vec3 operator -() const { return Vec3(-x, -y, -z); }
Vec3 operator +(const Vec3 &v) const
{
return Vec3(x+v.x, y+v.y, z+v.z);
}
Vec3 operator -(const Vec3 &v) const
{
return Vec3(x-v.x, y-v.y, z-v.z);
}
Vec3 operator *(float f) const
{
return Vec3(x*f, y*f, z*f);
}
Vec3 operator /(float f) const
{
float oneOverF = 1.f / f;
return Vec3(x*oneOverF, y*oneOverF, z*oneOverF);
}
Vec3 &operator +=(const Vec3 &v)
{
x += v.x; y += v.y; z += v.z;
return *this;
}
Vec3 &operator -=(const Vec3 &v)
{
x -= v.x; y -= v.y; z -= v.z;
return *this;
}
Vec3 &operator *=(float f)
{
x *= f; y *= f; z *= f;
return *this;
}
Vec3 &operator /=(float f)
{
float oneOverF = 1.f/f;
x *= oneOverF; y *= oneOverF;; z *= oneOverF;;
return *this;
}
void normalize()
{
float magSq = x*x + y*y + z*z;
if (magSq > 0.0f)
{
float oneOverMag = 1.0f / std::sqrt(magSq);
x *= oneOverMag;
y *= oneOverMag;
z *= oneOverMag;
}
}
float operator *(const Vec3 &v) const
{
return x*v.x + y*v.y + z*v.z;
}
public:
float x, y, z;
};
inline float vec3Mag(const Vec3 &v)
{
return std::sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
}
inline Vec3 crossProduct(const Vec3 &a, const Vec3 &b)
{
return Vec3(
a.y*b.z - a.z*b.y,
a.z*b.x - a.x*b.z,
a.x*b.y - a.y*b.x
);
}
inline Vec3 operator *(float f, const Vec3 &v)
{
return Vec3(f*v.x, f*v.y, f*v.z);
}
inline float distance(const Vec3 &a, const Vec3 &b)
{
float dx = a.x - b.x;
float dy = a.y - b.y;
float dz = a.z - b.z;
return std::sqrt(dx*dx + dy*dy + dz*dz);
}
const Vec3 kZeroVec3;
}

81
Vec3UTest.cpp Normal file
View File

@@ -0,0 +1,81 @@
///
///@Brief Check the exercise of 5.13
///@Author PEZY
///@Date Sept. 2014
///@See class B3D::Vec3
///
#include <iostream>
#include "Vec3.h"
using B3D::Vec3;
using std::cout; using std::endl;
using B3D::operator *;
int main()
{
///> Exercise 5.13.1
{
Vec3 va(3, 7, 0);
va = -va;
cout << "[" << va.x << "," << va.y << "," << va.z << "]" << endl;
Vec3 vb(-12, 5, 0);
cout << B3D::vec3Mag(vb) << endl;
Vec3 vc(8, -3, 0.5);
cout << B3D::vec3Mag(vc) << endl;
Vec3 vd(4, -7, 0);
vd = 3*vd;
cout << "[" << vd.x << "," << vd.y << "," << vd.z << "]" << endl;
Vec3 ve(4, 5, 0);
ve /= 2;
cout << "[" << ve.x << "," << ve.y << "," << ve.z << "]" << endl;
}
cout << "----------" << endl;
///> Exercise 5.13.2
{
Vec3 va(-12, 5, 0);
Vec3 vb(8, -3, 0.5);
va.normalize();
vb.normalize();
cout << "[" << va.x << "," << va.y << "," << va.z << "]" << endl;
cout << "[" << vb.x << "," << vb.y << "," << vb.z << "]" << endl;
}
cout << "----------" << endl;
///> Exercise 5.13.3
{
Vec3 va1(3, 10, 7);
Vec3 va2(8, -7, 4);
Vec3 va = va1 - va2;
cout << "[" << va.x << "," << va.y << "," << va.z << "]" << endl;
Vec3 vb1(1, 2, 3);
Vec3 vb2(2, 10, -6);
Vec3 vb = 3*vb1 - 4*vb2;
cout << "[" << vb.x << "," << vb.y << "," << vb.z << "]" << endl;
}
cout << "----------" << endl;
///> Exercise 5.13.4
{
Vec3 va1(3, 10, 7);
Vec3 va2(8, -7, 4);
cout << B3D::distance(va1, va2) << endl;
}
cout << "----------" << endl;
///> Exercise 5.13.5
{
Vec3 vb(-2, 0, 4);
cout << 3*vb*(Vec3(8, -2, 1.5) + Vec3(0, 9, 7)) << endl;
}
cout << "----------" << endl;
///> Exercise 5.13.8
{
Vec3 v = B3D::crossProduct(Vec3(3, 10, 7), Vec3(8, -7, 4));
cout << "[" << v.x << "," << v.y << "," << v.z << "]" << endl;
}
cout << "----------" << endl;
return 0;
}