CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Common/Math/lin/vec3.h
Views: 1401
#ifndef _MATH_LIN_VEC31#define _MATH_LIN_VEC323#include <math.h>4#include <string.h> // memset56namespace Lin {78class Matrix4x4;910// Hm, doesn't belong in this file.11class Vec4 {12public:13float x,y,z,w;14Vec4(){}15Vec4(float a, float b, float c, float d) {x=a;y=b;z=c;w=d;}16};1718class Vec3 {19public:20float x,y,z;2122Vec3() { }23explicit Vec3(float f) {x=y=z=f;}2425float operator [] (int i) const { return (&x)[i]; }26float &operator [] (int i) { return (&x)[i]; }2728Vec3(const float _x, const float _y, const float _z) {29x=_x; y=_y; z=_z;30}31void Set(float _x, float _y, float _z) {32x=_x; y=_y; z=_z;33}34Vec3 operator + (const Vec3 &other) const {35return Vec3(x+other.x, y+other.y, z+other.z);36}37void operator += (const Vec3 &other) {38x+=other.x; y+=other.y; z+=other.z;39}40Vec3 operator -(const Vec3 &v) const {41return Vec3(x-v.x,y-v.y,z-v.z);42}43void operator -= (const Vec3 &other)44{45x-=other.x; y-=other.y; z-=other.z;46}47Vec3 operator -() const {48return Vec3(-x,-y,-z);49}5051Vec3 operator * (const float f) const {52return Vec3(x*f,y*f,z*f);53}54Vec3 operator / (const float f) const {55float invf = (1.0f/f);56return Vec3(x*invf,y*invf,z*invf);57}58void operator /= (const float f)59{60*this = *this / f;61}62float operator * (const Vec3 &other) const {63return x*other.x + y*other.y + z*other.z;64}65void operator *= (const float f) {66*this = *this * f;67}68void scaleBy(const Vec3 &other) {69x *= other.x; y *= other.y; z *= other.z;70}71Vec3 scaledBy(const Vec3 &other) const {72return Vec3(x*other.x, y*other.y, z*other.z);73}74Vec3 scaledByInv(const Vec3 &other) const {75return Vec3(x/other.x, y/other.y, z/other.z);76}77Vec3 operator *(const Matrix4x4 &m) const;78void operator *=(const Matrix4x4 &m) {79*this = *this * m;80}81Vec3 rotatedBy(const Matrix4x4 &m) const;82Vec3 operator %(const Vec3 &v) const {83return Vec3(y*v.z-z*v.y, z*v.x-x*v.z, x*v.y-y*v.x);84}85float length2() const {86return x*x + y*y + z*z;87}88float length() const {89return sqrtf(length2());90}91void setLength(const float l) {92(*this) *= l/length();93}94Vec3 withLength(const float l) const {95return (*this) * l / length();96}97float distance2To(const Vec3 &other) const {98return Vec3(other-(*this)).length2();99}100Vec3 normalized() const {101return (*this) / length();102}103float normalize() { //returns the previous length, is often useful104float len = length();105(*this) = (*this)/len;106return len;107}108bool operator == (const Vec3 &other) const {109if (x==other.x && y==other.y && z==other.z)110return true;111else112return false;113}114Vec3 lerp(const Vec3 &other, const float t) const {115return (*this)*(1-t) + other*t;116}117void setZero() {118memset((void *)this,0,sizeof(float)*3);119}120};121122inline Vec3 operator * (const float f, const Vec3 &v) {return v * f;}123124// In new code, prefer these to the operators.125126inline float dot(const Vec3 &a, const Vec3 &b) {127return a.x * b.x + a.y * b.y + a.z * b.z;128}129130inline Vec3 cross(const Vec3 &a, const Vec3 &b) {131return a % b;132}133134class AABBox {135public:136Vec3 min;137Vec3 max;138};139140} // namespace Lin141142#endif // _MATH_LIN_VEC3143144145