-
Notifications
You must be signed in to change notification settings - Fork 2
/
Vector.h
205 lines (174 loc) · 4.14 KB
/
Vector.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* Copyright (C) 1993-2017 Walter Brisken wbrisken@lbo.us
*
* This file is part of pmpar.
*
* pmpar is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* pmpar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with pmpar. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* A Vector Package for C++
(c) Walter Brisken 1-17-1993
Pasadena, CA
Princeton, NJ
Socorro, NM
St Paul, MN
*/
#ifndef __VECTOR_H__
#define __VECTOR_H__
#include <iostream>
#include <cmath>
template <class T> class Vector
{
public:
T x, y, z;
Vector(T x1 = 0, T y1 = 0, T z1 = 0) : x(x1), y(y1), z(z1) { }
Vector(const Vector &v) : x(v.x), y(v.y), z(v.z) { }
Vector &operator = (const Vector &v) // equation
{
x = v.x;
y = v.y;
z = v.z;
return *this;
}
Vector &operator += (const Vector &v) // a=a+b
{
x += v.x;
y += v.y;
z += v.z;
return *this;
}
Vector &operator -= (const Vector &v) // a=a-b
{
x -= v.x;
y -= v.y;
z -= v.z;
return *this;
}
Vector &operator *= (const T &a) // a=a*b
{
x *= a;
y *= a;
z *= a;
return *this;
}
Vector &operator *= (const Vector &v)
{
x *= v.x;
y *= v.y;
z *= v.z;
return *this;
}
Vector &operator /= (const T &a) // a=a/b
{
x /= a;
y /= a;
z /= a;
return *this;
}
friend Vector operator - (const Vector &v) // negation
{
return Vector<T>(-v.x, -v.y, -v.z);
}
friend Vector operator + (const Vector &a, const Vector &b)
{
return Vector<T>(a.x + b.x, a.y + b.y, a.z + b.z);
}
friend Vector operator - (const Vector &a, const Vector &b)
{
return Vector<T>(a.x - b.x, a.y - b.y, a.z - b.z);
}
friend Vector operator * (const Vector &a, const Vector &b)
{
return Vector<T>(a.x*b.x, a.y*b.y, a.z*b.z);
}
friend Vector operator * (const Vector &v, const T &a)
{
return Vector<T>(v.x*a, v.y*a, v.z*a);
}
friend Vector operator / (const Vector &a, const Vector &b)
{
return Vector(a.x/b.x, a.y/b.y, a.z/b.z);
}
friend Vector operator / (const Vector &v, T a)
{
return Vector(v.x/a, v.y/a, v.z/a);
}
// dot product
friend T operator % (const Vector &u, const Vector &v)
{
return u.x*v.x + u.y*v.y + u.z*v.z;
}
// cross product
friend Vector operator ^ (const Vector &u, const Vector &v)
{
return Vector<T>(u.y*v.z - u.z*v.y,
u.z*v.x - u.x*v.z,
u.x*v.y - u.y*v.x);
}
friend T Dot(const Vector &u, const Vector &v)
{
return u.x*v.x + u.y*v.y + u.z*v.z;
}
friend T Square(const Vector &v)
{
return v.x*v.x + v.y*v.y + v.z*v.z;
}
T length()
{
return sqrt(x*x + y*y + z*z);
}
Vector &normalize()
{
T l = length();
if(l < 0.000001) l=0.000001;
x/=l;
y/=l;
z/=l;
return *this;
}
Vector proj(const Vector &v)
{
T div = Square(v);
T fac;
if(div < 0.0000001) div = 0.0000001;
fac = (x*v.x + y*v.y + z*v.z)/div;
return Vector<T>(v.x*fac, v.y*fac, v.z*fac);
}
Vector unitproj(const Vector &unitv)
{
T fac = x*unitv.x + y*unitv.y + z*unitv.z;
return Vector<T>(unitv.x*fac, unitv.y*fac, unitv.z*fac);
}
Vector ortho(const Vector &v)
{
return *this-proj(v);
}
inline Vector operator~() // normalation
{
T l = sqrt(x*x + y*y + z*z);
if(l < 0.000001) return Vector(1, 0, 0);
else return Vector<T>(x/l, y/l, z/l);
}
// friend ostream& operator << (ostream &s, const Vector &v)
// {
// return s << "(" << v.x << ", " << v.y << ", " << v.z << ")";
// }
Vector min(const Vector &); // and much, much more!!
Vector max(const Vector &);
Vector rotate(double cos1, double sin1, double cos2, double sin2);
Vector &rotatex(double cos1, double sin1);
Vector &rotatey(double cos1, double sin1);
Vector &rotatez(double cos1, double sin1);
Vector rev_rotate(double cos1, double sin1, double cos2, double sin2);
};
#endif