1 #include "../inc/vector3.h"
7 m_components
= new double[3];
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];
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];
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];
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 //===================================================================
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];
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];
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
;
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
157 throw domain_error ("Division by zero occured !");
159 return ( Vector3(*this) /= scalar
);
162 Vector3
& Vector3::operator /= (const double scalar
)
165 throw domain_error ("Division by zero occured !");
168 m_components
[0] /= scalar
;
169 m_components
[1] /= scalar
;
170 m_components
[2] /= scalar
;
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
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
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...
222 double Vector3::at (const unsigned int index
) const
225 throw out_of_range ("Passed invalid C++ like index to Vector3::at");
227 return m_components
[index
];
231 double& Vector3::at (const unsigned int index
)
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];
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) );