81 lines
1.7 KiB
C
81 lines
1.7 KiB
C
#pragma once
|
|
#include <cmath>
|
|
|
|
struct double3 {
|
|
union {
|
|
struct {double x, y, z;};
|
|
double data[3]; // For legacy access
|
|
};
|
|
double3() {}
|
|
double3(const double x, const double y, const double z)
|
|
: x(x), y(y), z(z) {}
|
|
double& operator[](int i) {return data[i];}
|
|
const double operator[](int i) const {return data[i];}
|
|
double3& operator=(const double3& a)
|
|
{
|
|
x = a.x;
|
|
y = a.y;
|
|
z = a.z;
|
|
return *this;
|
|
}
|
|
double3& operator+=(const double3& a)
|
|
{
|
|
x += a.x;
|
|
y += a.y;
|
|
z += a.z;
|
|
return *this;
|
|
}
|
|
double3& operator-=(const double3& a)
|
|
{
|
|
x -= a.x;
|
|
y -= a.y;
|
|
z -= a.z;
|
|
return *this;
|
|
}
|
|
double3& operator/=(const double& c)
|
|
{
|
|
x /= c;
|
|
y /= c;
|
|
z /= c;
|
|
return *this;
|
|
}
|
|
double norm2() const
|
|
{
|
|
return x*x + y*y + z*z;
|
|
}
|
|
double norm() const
|
|
{
|
|
return sqrt(x*x + y*y + z*z);
|
|
}
|
|
operator double*() {return data;}
|
|
};
|
|
|
|
inline double3 operator*(const double& c, const double3& a)
|
|
{
|
|
return double3(a.x*c, a.y*c, a.z*c);
|
|
}
|
|
|
|
inline double3 operator*(const double3& a, const double& c)
|
|
{
|
|
return double3(a.x*c, a.y*c, a.z*c);
|
|
}
|
|
|
|
inline double operator*(const double3& a, const double3& b)
|
|
{
|
|
return a.x*b.x+a.y*b.y+a.z*b.z;
|
|
}
|
|
|
|
inline double3 operator/(const double3& a, const double& c)
|
|
{
|
|
return double3(a.x/c, a.y/c, a.z/c);
|
|
}
|
|
|
|
inline double3 operator+(const double3& a, const double3& b)
|
|
{
|
|
return double3(a.x+b.x, a.y+b.y, a.z+b.z);
|
|
}
|
|
|
|
inline double3 operator-(const double3& a, const double3& b)
|
|
{
|
|
return double3(a.x-b.x, a.y-b.y, a.z-b.z);
|
|
}
|