1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
namespace math3d
{
struct vec3d
{
double x, y, z;
explicit vec3d(double x = 0, double y = 0, double z = 0) : x(x), y(y), z(z) {};
bool operator == (const vec3d &rhs) const;
bool operator != (const vec3d &rhs) const;
friend std::ostream& operator << (std::ostream& ostrm, const vec3d& a)
{
return ostrm << "vec3d (" << a.x << ", " << a.y << ", " << a.z << ")" << "\n";
}
};
vec3d operator + (const vec3d &lhs, const vec3d &rhs);
vec3d operator - (const vec3d &lhs, const vec3d &rhs);
vec3d operator - (const vec3d &a);
vec3d operator * (double lhs, const vec3d &rhs);
vec3d operator * (const vec3d &lhs, const vec3d &rhs);
vec3d operator / (const vec3d &lhs, const vec3d &rhs);
vec3d operator += (const vec3d &lhs, const vec3d &rhs);
vec3d operator -= (const vec3d &lhs, const vec3d &rhs);
vec3d operator *= (const vec3d &lhs, const vec3d &rhs);
vec3d operator /= (const vec3d &lhs, const vec3d &rhs);
//vector-vector ops begin
bool vec3d::operator == (const vec3d &rhs) const
{
return x == rhs.x && y == rhs.y && z == rhs.z;
}
bool vec3d::operator != (const vec3d &a) const
{
return !(*this == a);
}
vec3d operator + (const vec3d &lhs, const vec3d &rhs)
{
return vec3d(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z);
}
vec3d operator - (const vec3d &lhs, const vec3d &rhs)
{
return lhs + -rhs;
}
vec3d operator * (const vec3d &lhs, const vec3d &rhs)
{
return vec3d(lhs.x * rhs.x, lhs.y * rhs.y, lhs.z * rhs.z);
}
vec3d operator / (const vec3d &lhs, const vec3d &rhs)
{
return vec3d(lhs.x / rhs.x, lhs.y / rhs.y, lhs.z / rhs.z);
}
vec3d operator += (vec3d &lhs, const vec3d &rhs)
{
return lhs = lhs + rhs;
}
vec3d operator -= (vec3d &lhs, const vec3d &rhs)
{
return lhs = lhs - rhs;
}
vec3d operator *= (vec3d &lhs, const vec3d &rhs)
{
return lhs = lhs * rhs;
}
vec3d operator /= (vec3d &lhs, const vec3d &rhs)
{
return lhs = lhs / rhs;
}
//vector-scalar ops begin
vec3d operator - (const vec3d &a) //unary negation, avoid confusion
{
return -1.0 * a;
}
vec3d operator * (double lhs, const vec3d &rhs)
{
return vec3d(lhs * rhs.x, lhs * rhs.y, lhs * rhs.z);
}
vec3d operator * (const vec3d &lhs, double rhs)
{
return vec3d(lhs.x * rhs, lhs.y * rhs, lhs.z * rhs);
}
vec3d operator / (const vec3d &lhs, double rhs)
{
return (1 / rhs) * lhs;
}
vec3d operator *= (vec3d &lhs, double rhs)
{
return lhs = rhs * lhs;
}
vec3d operator /= (vec3d &lhs, double rhs)
{
return lhs = lhs / rhs;
}
//vector-scalar ops end
double length(const vec3d &a)
{
return sqrt(a.x * a.x + a.y * a.y + a.z * a.z);
}
double dot(const vec3d &a, const vec3d &b)
{
return a.x * b.x + a.y * b.y + a.z * b.z;
}
double radian_angle_between(const vec3d &a, const vec3d &b)
{
double theta = dot(a, b) / (length(a) * (length(b)));
return acos(theta);
}
vec3d floor_vec3d(const vec3d &a)
{
return vec3d(floor(a.x), floor(a.y), floor(a.z));
}
bool almost_equal(const vec3d &a, const vec3d &b, double epsilon)
{
if (floor(a.x - b.x) <= epsilon && floor(a.y - b.y) <= epsilon && floor(a.z - b.z) <= epsilon)
{
return true;
}
else return false;
}
bool orthogonal(const vec3d &a, const vec3d &b)
{
return dot(a, b) == 0;
}
bool orthogonal_almost(const vec3d &a, const vec3d &b, double epsilon)
{
return floor(dot(a, b)) <= epsilon;
}
vec3d cross(const vec3d &a, const vec3d &b)
{
return vec3d(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
}
vec3d normalize(const vec3d &a)
{
return a / length(a);
}
}
| |