zeus/include/zeus/CVector3d.hpp

88 lines
2.4 KiB
C++
Raw Normal View History

2018-10-06 20:39:40 -07:00
#pragma once
2015-10-07 17:29:33 -07:00
2018-12-07 17:16:50 -08:00
#include "athena/Types.hpp"
2015-10-07 17:29:33 -07:00
#include "Global.hpp"
2016-03-04 15:03:26 -08:00
#include "zeus/Math.hpp"
#include "zeus/CVector3f.hpp"
2015-10-07 17:29:33 -07:00
2018-12-07 17:16:50 -08:00
namespace zeus {
class CVector3d {
2015-10-07 17:29:33 -07:00
public:
2018-12-07 17:16:50 -08:00
zeus::simd<double> mSimd;
CVector3d() : mSimd(0.0) {}
2017-12-18 19:02:59 -08:00
2018-12-07 17:16:50 -08:00
template <typename T>
CVector3d(const simd<T>& s) : mSimd(s) {}
2018-12-07 21:23:50 -08:00
2015-10-07 17:29:33 -07:00
#if ZE_ATHENA_TYPES
2018-12-07 17:16:50 -08:00
CVector3d(const atVec3d& vec) : mSimd(vec.simd) {}
2015-10-07 17:29:33 -07:00
#endif
2018-12-07 17:16:50 -08:00
explicit CVector3d(double xyz) : mSimd(xyz) {}
2015-10-07 17:29:33 -07:00
2018-12-07 17:16:50 -08:00
CVector3d(const CVector3f& vec) : mSimd(vec.mSimd) {}
2015-10-07 17:29:33 -07:00
2018-12-07 17:16:50 -08:00
CVector3d(double x, double y, double z) : mSimd(x, y, z) {}
2015-10-07 17:29:33 -07:00
2018-12-07 21:23:50 -08:00
CVector3f asCVector3f() { return mSimd; }
2015-10-07 17:29:33 -07:00
2018-12-07 21:23:50 -08:00
double magSquared() const { return mSimd.dot3(mSimd); }
2015-10-07 17:29:33 -07:00
2018-12-07 21:23:50 -08:00
double magnitude() const { return sqrt(magSquared()); }
2015-10-07 17:29:33 -07:00
2018-12-07 17:16:50 -08:00
CVector3d cross(const CVector3d& rhs) const {
2018-12-07 21:23:50 -08:00
return {y() * rhs.z() - z() * rhs.y(), z() * rhs.x() - x() * rhs.z(), x() * rhs.y() - y() * rhs.x()};
2018-12-07 17:16:50 -08:00
}
2015-10-07 17:29:33 -07:00
2018-12-07 21:23:50 -08:00
double dot(const CVector3d& rhs) const { return mSimd.dot3(rhs.mSimd); }
2015-10-07 17:29:33 -07:00
2018-12-07 17:16:50 -08:00
CVector3d asNormalized() {
double mag = magnitude();
mag = 1.0 / mag;
return mSimd * zeus::simd<double>(mag);
}
2015-10-07 17:29:33 -07:00
2018-12-07 21:23:50 -08:00
void splat(double xyz) { mSimd = zeus::simd<double>(xyz); }
2015-10-07 17:29:33 -07:00
2018-12-07 21:23:50 -08:00
void zeroOut() { *this = skZero; }
2017-03-14 00:02:09 -07:00
2018-12-07 21:23:50 -08:00
CVector3d operator+(const CVector3d& rhs) const { return mSimd + rhs.mSimd; }
2017-03-14 00:02:09 -07:00
2018-12-07 21:23:50 -08:00
CVector3d operator-(const CVector3d& rhs) const { return mSimd - rhs.mSimd; }
2018-12-07 17:16:50 -08:00
2018-12-07 21:23:50 -08:00
CVector3d operator*(const CVector3d& rhs) const { return mSimd * rhs.mSimd; }
2018-12-07 17:16:50 -08:00
2018-12-07 21:23:50 -08:00
CVector3d operator/(const CVector3d& rhs) const { return mSimd / rhs.mSimd; }
2018-12-07 17:16:50 -08:00
zeus::simd<double>::reference operator[](size_t idx) {
assert(idx < 3);
return mSimd[idx];
}
double operator[](size_t idx) const {
assert(idx < 3);
return mSimd[idx];
}
2017-03-17 16:30:14 -07:00
2018-12-07 17:16:50 -08:00
double x() const { return mSimd[0]; }
double y() const { return mSimd[1]; }
double z() const { return mSimd[2]; }
simd<double>::reference x() { return mSimd[0]; }
simd<double>::reference y() { return mSimd[1]; }
simd<double>::reference z() { return mSimd[2]; }
static const CVector3d skZero;
2015-10-07 17:29:33 -07:00
};
2018-12-07 21:23:50 -08:00
static inline CVector3d operator+(double lhs, const CVector3d& rhs) { return zeus::simd<double>(lhs) + rhs.mSimd; }
2015-10-07 17:29:33 -07:00
2018-12-07 21:23:50 -08:00
static inline CVector3d operator-(double lhs, const CVector3d& rhs) { return zeus::simd<double>(lhs) - rhs.mSimd; }
2015-10-07 17:29:33 -07:00
2018-12-07 21:23:50 -08:00
static inline CVector3d operator*(double lhs, const CVector3d& rhs) { return zeus::simd<double>(lhs) * rhs.mSimd; }
2018-12-07 17:16:50 -08:00
2018-12-07 21:23:50 -08:00
static inline CVector3d operator/(double lhs, const CVector3d& rhs) { return zeus::simd<double>(lhs) / rhs.mSimd; }
2015-10-07 17:29:33 -07:00
2018-12-07 21:23:50 -08:00
} // namespace zeus