classe 'Vector3' : ajout d'un constructeur et surcharge de l'operateur 'ostream&...
[ProjetInfo.git] / math / src / vector3.cc
blobf763e1f88a1ac59a75684ab0d8055ba51b139c56
1 #include "../inc/vector3.h"
3 using namespace std;
5 Vector3::Vector3 ()
7 m_components = new double[3];
8 m_components[0] = 0;
9 m_components[1] = 0;
10 m_components[2] = 0;
13 Vector3::Vector3 (const double cmp1, const double cmp2, const double cmp3)
15 m_components = new double[3];
16 m_components[0] = cmp1;
17 m_components[1] = cmp2;
18 m_components[2] = cmp3;
21 //Vector3 constructor for praticular vectors of R3
22 Vector3::Vector3 (STANDARD_BASIS aVector)
24 m_components = new double[3];
26 switch (aVector) {
27 case e1:
28 m_components[0] = 1;
29 m_components[1] = 0;
30 m_components[2] = 0;
31 break;
32 case e2:
33 m_components[0] = 0;
34 m_components[1] = 1;
35 m_components[2] = 0;
36 break;
37 case e3:
38 m_components[0] = 0;
39 m_components[1] = 0;
40 m_components[2] = 1;
41 break;
42 default:
43 m_components[0] = 0;
44 m_components[1] = 0;
45 m_components[2] = 0;
46 break;
50 Vector3::Vector3 (double components[])
52 m_components = new double[3];
53 m_components[0] = components[0];
54 m_components[1] = components[1];
55 m_components[2] = components[2];
58 //Copy-constructor
59 Vector3::Vector3 (const Vector3 &otherVector)
61 m_components = new double[3];
62 m_components[0] = otherVector.m_components[0];
63 m_components[1] = otherVector.m_components[1];
64 m_components[2] = otherVector.m_components[2];
67 Vector3::~Vector3 ()
69 delete[] m_components;
72 //===================================================================
74 void Vector3::setVector (const double cmp1, const double cmp2, const double cmp3)
76 m_components[0] = cmp1;
77 m_components[1] = cmp2;
78 m_components[2] = cmp3;
81 //===================================================================
83 void Vector3::show () const
85 cout << m_components[0] << endl;
86 cout << m_components[1] << endl;
87 cout << m_components[2] << endl << endl;
90 //===================================================================
92 //Operator =
93 void Vector3::operator = (const Vector3 &otherVector)
95 m_components[0] = otherVector.m_components[0];
96 m_components[1] = otherVector.m_components[1];
97 m_components[2] = otherVector.m_components[2];
100 //Arithmetic operators +, +=
101 Vector3 Vector3::Vector3::operator + (const Vector3 &otherVector) const
103 return ( Vector3(*this) += otherVector);
106 Vector3& Vector3::operator += (const Vector3 &otherVector)
108 m_components[0] += otherVector.m_components[0];
109 m_components[1] += otherVector.m_components[1];
110 m_components[2] += otherVector.m_components[2];
111 return *this;
114 //Arithmetic operators -, -=
115 Vector3 Vector3::operator - (const Vector3 &otherVector) const
117 return ( Vector3(*this) -= otherVector );
120 Vector3& Vector3::operator -= (const Vector3 &otherVector)
122 m_components[0] -= otherVector.m_components[0];
123 m_components[1] -= otherVector.m_components[1];
124 m_components[2] -= otherVector.m_components[2];
125 return *this;
128 //Multiplication by a real number (double) operators
129 Vector3 Vector3::operator * (const double scalar) const
131 return ( Vector3(*this) *= scalar );
134 Vector3& Vector3::operator *= (const double scalar)
136 m_components[0] *= scalar;
137 m_components[1] *= scalar;
138 m_components[2] *= scalar;
139 return *this;
142 //Inner product operators *, *=
143 double Vector3::operator * (const Vector3 &otherVector) const
145 return ( dotProduct(otherVector) );
148 double Vector3::operator *= (const Vector3 &otherVector) const
150 return ( dotProduct(otherVector) );
153 //Division by a real number operators /, /=
154 Vector3 Vector3::operator / (const double scalar) const
156 if (scalar == 0) {
157 throw domain_error ("Division by zero occured !");
159 return ( Vector3(*this) /= scalar );
162 Vector3& Vector3::operator /= (const double scalar)
164 if (scalar == 0) {
165 throw domain_error ("Division by zero occured !");
168 m_components[0] /= scalar;
169 m_components[1] /= scalar;
170 m_components[2] /= scalar;
171 return *this;
174 //Cross product operator ^
175 Vector3 Vector3::operator ^ (const Vector3 &otherVector) const
177 return ( Vector3( (m_components[1]*otherVector.m_components[2] - m_components[2]*otherVector.m_components[1]),
178 (m_components[2]*otherVector.m_components[0] - m_components[0]*otherVector.m_components[2]),
179 (m_components[0]*otherVector.m_components[1] - m_components[1]*otherVector.m_components[0]) ) );
183 //Equality operators ==, !=
184 bool Vector3::operator == (const Vector3 &otherVector) const
186 return ( (m_components[0] == otherVector.m_components[0]) and
187 (m_components[1] == otherVector.m_components[1]) and
188 (m_components[2] == otherVector.m_components[2]) );
192 bool Vector3::operator != (const Vector3 &otherVector) const
194 return not(*this == otherVector);
197 //Index operator [] read-only
198 // math like getter
199 double Vector3::operator [] (const unsigned int index) const
201 if (index > 3 or index == 0)
203 throw out_of_range ("Vector3::operator[] : index is out of range !");
205 return ( m_components[(index - 1)]);
208 //Index operator [] with write possibility
209 // math like setter
210 double& Vector3::operator [] (const unsigned int index)
212 if (index > 3 or index == 0)
214 throw out_of_range ("Vector3::operator[] : index is out of range !");
216 return ( m_components[(index - 1)]);
219 // in c++ like accessors/manipulators, case index == 0 shouldn't happen but the test is there for security...
221 // C++ like getter
222 double Vector3::at (const unsigned int index) const
224 if (index > 2) {
225 throw out_of_range ("Passed invalid C++ like index to Vector3::at");
227 return m_components[index];
230 // C++ like setter
231 double& Vector3::at (const unsigned int index)
233 if (index > 2) {
234 throw out_of_range ("Passed invalid C++ like index to Vector3::at");
236 return m_components[index];
239 //Overload of operator<< to use with every ostream objects (cout, files, etc.)
240 ostream& operator<< (ostream& os, const Vector3 &aVector)
242 os << aVector[1] << ' ' << aVector[2] << ' ' << aVector[3];
243 return os;
246 //===================================================================
248 //Inner product calculation method (used by operator * and *=)
249 double Vector3::dotProduct (const Vector3 &otherVector, const DOT_PRODUCT_TYPE dpType,
250 const double w1, const double w2, const double w3) const
252 if (dpType == EUCLID) { //Standard Euclidean inner product
253 return ( (m_components[0]*otherVector.m_components[0]) +
254 (m_components[1]*otherVector.m_components[1]) +
255 (m_components[2]*otherVector.m_components[2]) );
257 else { //Second method calculates weighted Euclidean inner product with weight w1, w2 and w3
258 return ( w1*(m_components[0]*otherVector.m_components[0]) +
259 w2*(m_components[1]*otherVector.m_components[1]) +
260 w3*(m_components[2]*otherVector.m_components[2]) );
264 //Magnitude calculation methods
265 double Vector3::magnitude () const
267 return ( sqrt((*this) * (*this)) );
270 double Vector3::magnitude2 () const
272 return ( (*this) * (*this) );