-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvec.h
140 lines (105 loc) · 3.3 KB
/
vec.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#ifndef __vec__
#define __vec__
#include <cmath>
#include <iostream>
#include <cassert>
template<class T, int n> struct vec;
template<class T, int n> T dot(const vec<T,n>& u,const vec<T,n>& v);
template<class T, int n>
struct vec
{
T x[n];
vec()
{make_zero();}
explicit vec(const T& a)
{assert(n == 1);x[0]=a;}
vec(const T& a, const T& b)
{assert(n == 2);x[0]=a;x[1]=b;}
vec(const T& a, const T& b, const T& c)
{assert(n == 3);x[0]=a;x[1]=b;x[2]=c;}
template<class U>
explicit vec(const vec<U,n>& v)
{for(int i = 0; i < n; i++) x[i] = (T)v.x[i];}
void make_zero()
{for(int i = 0; i < n; i++) x[i] = 0;}
vec& operator += (const vec& v)
{for(int i = 0; i < n; i++) x[i] += v.x[i]; return *this;}
vec& operator -= (const vec& v)
{for(int i = 0; i < n; i++) x[i] -= v.x[i]; return *this;}
vec& operator *= (const vec& v)
{for(int i = 0; i < n; i++) x[i] *= v.x[i]; return *this;}
vec& operator /= (const vec& v)
{for(int i = 0; i < n; i++) x[i] /= v.x[i]; return *this;}
vec& operator *= (const T& c)
{for(int i = 0; i < n; i++) x[i] *= c; return *this;}
vec& operator /= (const T& c)
{for(int i = 0; i < n; i++) x[i] /= c; return *this;}
vec operator + () const
{return *this;}
vec operator - () const
{vec r; for(int i = 0; i < n; i++) r[i] = -x[i]; return r;}
vec operator + (const vec& v) const
{vec r; for(int i = 0; i < n; i++) r[i] = x[i] + v.x[i]; return r;}
vec operator - (const vec& v) const
{vec r; for(int i = 0; i < n; i++) r[i] = x[i] - v.x[i]; return r;}
vec operator * (const vec& v) const
{vec r; for(int i = 0; i < n; i++) r[i] = x[i] * v.x[i]; return r;}
vec operator / (const vec& v) const
{vec r; for(int i = 0; i < n; i++) r[i] = x[i] / v.x[i]; return r;}
vec operator * (const T& c) const
{vec r; for(int i = 0; i < n; i++) r[i] = x[i] * c; return r;}
vec operator / (const T& c) const
{vec r; for(int i = 0; i < n; i++) r[i] = x[i] / c; return r;}
const T& operator[] (int i) const
{return x[i];}
T& operator[] (int i)
{return x[i];}
T magnitude_squared() const
{return dot(*this, *this);}
T magnitude() const
{return sqrt(magnitude_squared());}
// Be careful to handle the zero vector gracefully
vec normalized() const
{T mag = magnitude(); if(mag) return *this / mag; vec r; r[0] = 1; return r;};
};
template <class T, int n>
vec<T,n> operator * (const T& c, const vec<T,n>& v)
{return v*c;}
template <class T, int n>
T dot(const vec<T,n> & u, const vec<T,n> & v)
{
T r = 0;
for(int i = 0; i < n; i++) r += u.x[i] * v.x[i];
return r;
}
template <class T >
vec<T,3> cross(const vec<T,3> & u, const vec<T,3> & v)
{
return vec<T,3> (
u[1] * v[2] - u[2] * v[1],
u[2] * v[0] - u[0] * v[2],
u[0] * v[1] - u[1] * v[0]);
}
template <class T, int n>
std::ostream& operator << (std::ostream& out, const vec<T,n> & u)
{
for(int i = 0; i < n; i++)
{
if(i) out << ' ';
out << u[i];
}
return out;
}
template <class T, int n>
std::istream& operator >> (std::istream& in, vec<T,n> & u)
{
for(int i = 0; i < n; i++)
{
in >> u[i];
}
return in;
}
typedef vec<float,2> vec2;
typedef vec<float,3> vec3;
typedef vec<int,2> ivec2;
#endif