My Project
Vec.h
Go to the documentation of this file.
1 #ifndef _VEC_H
2 #define _VEC_H
3 
4 #include <ostream>
5 #include <math.h>
6 
7 // template version of a 3 component vector
8 template<typename T>
9 struct _vec3{
10  //constructor with an initializer list
11  _vec3(T x, T y, T z) : d{x,y,z} {}
12  _vec3() : _vec3(0,0,0) {} // default constructor
13  void operator+= (const _vec3<T> &o) {d[0]+=o.d[0]; d[1]+=o.d[1]; d[2]+=o.d[2];}
14  void operator-= (const _vec3<T> &o) {d[0]-=o.d[0]; d[1]-=o.d[1]; d[2]-=o.d[2];}
15  T &operator[] (int i) {return d[i];}
16  T operator[] (int i) const {return d[i];} // read only getter
17  friend _vec3<T> operator*(const _vec3<T> &a, double s) {return _vec3<T>(a.d[0]*s,a.d[1]*s,a.d[2]*s);}
18  friend _vec3<T> operator*(double s, const _vec3<T> &a) {return _vec3<T>(a.d[0]*s,a.d[1]*s,a.d[2]*s);}
19  friend _vec3<T> operator-(const _vec3<T> &a, double s) {return _vec3<T>(a.d[0]-s,a.d[1]-s,a.d[2]-s);}
20 
21  friend std::ostream& operator<<(std::ostream &out, const _vec3<T> &o) { out<<o.d[0]<<" "<<o.d[1]<<" "<<o.d[2];return out; }
22 
23  friend double mag(const _vec3<T> &v) {return sqrt(v[0]*v[0]+v[1]*v[1]+v[2]*v[2]);}
24  friend _vec3<T> operator- (const _vec3<T> &a, const _vec3<T> &b) {return _vec3<T>{a[0]-b[0],a[1]-b[1],a[2]-b[2]};}
25 
26  protected:
27  T d[3];
28 };
29 
30 //define "nicknames"
32 using int3 = _vec3<int>;
33 
34 #endif
_vec3(T x, T y, T z)
Definition: Vec.h:11
T & operator[](int i)
Definition: Vec.h:15
friend _vec3< T > operator*(const _vec3< T > &a, double s)
Definition: Vec.h:17
void operator+=(const _vec3< T > &o)
Definition: Vec.h:13
friend double mag(const _vec3< T > &v)
Definition: Vec.h:23
friend _vec3< T > operator*(double s, const _vec3< T > &a)
Definition: Vec.h:18
Definition: Vec.h:9
T d[3]
Definition: Vec.h:27
_vec3()
Definition: Vec.h:12
void operator-=(const _vec3< T > &o)
Definition: Vec.h:14
friend _vec3< T > operator-(const _vec3< T > &a, double s)
Definition: Vec.h:19