8 template <size_t DIM> struct vec {
9 float& operator[](const size_t i) { assert(i<DIM); return data[i]; }
10 const float& operator[](const size_t i) const { assert(i<DIM); return data[i]; }
14 template<size_t DIM> vec<DIM> operator*(const vec<DIM> &lhs, const float rhs) {
16 for (size_t i=DIM; i--; ret[i]=lhs[i]*rhs);
20 template<size_t DIM> float operator*(const vec<DIM>& lhs, const vec<DIM>& rhs) {
22 for (size_t i=DIM; i--; ret+=lhs[i]*rhs[i]);
26 template<size_t DIM> vec<DIM> operator+(vec<DIM> lhs, const vec<DIM>& rhs) {
27 for (size_t i=DIM; i--; lhs[i]+=rhs[i]);
31 template<size_t DIM> vec<DIM> operator-(vec<DIM> lhs, const vec<DIM>& rhs) {
32 for (size_t i=DIM; i--; lhs[i]-=rhs[i]);
36 template<size_t DIM> vec<DIM> operator-(const vec<DIM> &lhs) {
40 template <> struct vec<3> {
41 float& operator[](const size_t i) { assert(i<3); return i==0 ? x : (1==i ? y : z); }
42 const float& operator[](const size_t i) const { assert(i<3); return i==0 ? x : (1==i ? y : z); }
43 float norm() { return std::sqrt(x*x+y*y+z*z); }
44 vec<3> & normalize(float l=1) { *this = (*this)*(l/norm()); return *this; }
45 float x = 0, y = 0, z = 0;
51 inline vec3 cross(vec3 v1, vec3 v2) {
52 return { v1.y*v2.z - v1.z*v2.y, v1.z*v2.x - v1.x*v2.z, v1.x*v2.y - v1.y*v2.x };
55 template <size_t DIM> std::ostream& operator<<(std::ostream& out, const vec<DIM>& v) {
56 for (size_t i=0; i<DIM; i++)
61 #endif //__GEOMETRY_H__