-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector3D.cpp
197 lines (125 loc) · 3.92 KB
/
Vector3D.cpp
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
// Vector3D Class
// Damien MATTEI
#include "Vector3D.hpp"
// implementations
template <class T> Vector3D<T>::Vector3D() : x(0), y(0), z(0) {}
template <class T> Vector3D<T>::Vector3D(T x,T y,T z) : x(x), y(y), z(z) {
#ifdef DISPLAY_CONSTRUCTOR
cout << "# Vector3D constructor #" << endl;
#endif
}
template <class T> Vector3D<T>::~Vector3D() {
#ifdef DISPLAY_CONSTRUCTOR
cout << "# Vector3D destructor #" << endl;
#endif
}
// create vector AB from point A and B
template <class T> Vector3D<T>::Vector3D(Point3D<T> & a,Point3D<T> & b)
: x(b.x - a.x), y(b.y - a.y), z(b.z - a.z) {}
template <class T> ostream& operator<< (ostream &out, Vector3D<T> &v3d)
{
out << "(" << v3d.x << ", "
<< v3d.y << ", "
<< v3d.z << ")";
return out;
}
// copy constructor
// exist but should NOT be used because one point in Universe should be unique
template <class T> Vector3D<T>::Vector3D(const Vector3D<T> &oneVector3D) {
x=oneVector3D.x;
y=oneVector3D.y;
z=oneVector3D.z;
#ifdef DISPLAY_CONSTRUCTOR
cout << "# Vector3D copy constructor # " << &oneVector3D << " => " << this << " " << *this << endl;
#endif
}
// assignation operator
template <class T> Vector3D<T> & Vector3D<T>::operator=(const Vector3D<T> &v)
{
if( this != &v ) {
x = v.x;
y = v.y;
z = v.z;
}
return *this;
}
// equality operator
template <class T> bool Vector3D<T>::operator== (const Vector3D<T> &v3d) {
return (x==v3d.x) && (y==v3d.y) && (z==v3d.z);
}
template <class T> Vector3D<T> & Vector3D<T>::operator+=(const Vector3D<T> &v)
{
this->x += v.x; // x += v.x works also
this->y += v.y;
this->z += v.z;
return *this;
}
template<class T> Vector3D<T> operator+(const Vector3D<T> &u,const Vector3D<T> &v) {
return Vector3D<T>(
u.x + v.x,
u.y + v.y,
u.z + v.z
);
}
template<class T> Vector3D<T> operator/(const Vector3D<T> &v,const T d) {
return Vector3D<T>(
v.x / d,
v.y / d,
v.z / d
);
}
template<class T> Vector3D<T> * operator|(const Vector3D<T> &v,const T d) {
return new Vector3D<T>(
v.x / d,
v.y / d,
v.z / d
);
}
template<class T> Vector3D<T> operator*(const T m,const Vector3D<T> &v) {
return Vector3D<T>(
m * v.x,
m * v.y,
m * v.z
);
}
template<class T> Vector3D<T> operator*(const Vector3D<T> &v,const T m) {
// return Vector3D<T>(
// v.x * m,
// v.y * m,
// v.z * m
// );
return m*v;
}
// dot product - produit scalaire
template<class T> T Vector3D<T>::operator* (const Vector3D<T> &v) {
return x * v.x + y * v.y + z * v.z;
}
// cross product - produit vectoriel
// should not be returned a reference and first allocate memory for the vector3d ?
template<class T> Vector3D<T> Vector3D<T>::operator^(const Vector3D<T> &v) {
return Vector3D<T>(
y * v.z - z * v.y,
z * v.x - x * v.z,
x * v.y - y * v.x
);
}
// matrix multiplication
template<class T> Vector3D<T> operator*(const T m3d[][3],const Vector3D<T> &u) {
return Vector3D<T>(
m3d[0][0] * u.x + m3d[0][1] * u.y + m3d[0][2] * u.z,
m3d[1][0] * u.x + m3d[1][1] * u.y + m3d[1][2] * u.z,
m3d[2][0] * u.x + m3d[2][1] * u.y + m3d[2][2] * u.z
);
}
template<class T> T Vector3D<T>::norm() {
//return ( sqrt(x * x + y * y + z * z) );
Vector3D<T> & v = *this;
return sqrt( v * v ) ;
}
template class Vector3D<float>;
template Vector3D<float> operator/(const Vector3D<float> &v,const float d);
template ostream& operator<< (ostream &out, Vector3D<float> &v3d);
template Vector3D<float> operator+(const Vector3D<float> &u,const Vector3D<float> &v);
template Vector3D<float> operator*(const Vector3D<float> &v,const float m);
template Vector3D<float> operator*(const float m,const Vector3D<float> &v);
template Vector3D<float> operator*(const float m3d[][3],const Vector3D<float> &u);