1 /*# ---------------------------------------------------------------------
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 # This software is governed by the CeCILL-B license under French law and
10 # abiding by the rules of distribution of free software. You can use,
11 # modify and/ or redistribute the software under the terms of the CeCILL-B
12 # license as circulated by CEA, CNRS and INRIA at the following URL
13 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
14 # or in the file LICENSE.txt.
16 # As a counterpart to the access to the source code and rights to copy,
17 # modify and redistribute granted by the license, users are provided only
18 # with a limited warranty and the software's author, the holder of the
19 # economic rights, and the successive licensors have only limited
22 # The fact that you are presently reading this means that you have had
23 # knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */
26 // marVector.cpp: implementation of the marVector class.
28 //////////////////////////////////////////////////////////////////////
29 #include "marVector.h"
34 //////////////////////////////////////////////////////////////////////
36 //////////////////////////////////////////////////////////////////////
37 marVector::marVector(size_t s)
53 marVector::marVector(const marVector &v)
57 _data=new double[_size];
65 marVector::marVector(double*v,size_t s)
72 // PS -> _data=new double[_size];
73 // PS -> for (int i=0;i<_size;i++)
75 // PS -> _data[i]=v[i];
88 //////////////////////////////////////////////////////////////////////
90 //////////////////////////////////////////////////////////////////////
91 marVector::~marVector()
103 //////////////////////////////////////////////////////////////////////
105 //////////////////////////////////////////////////////////////////////
106 // ---------------------------------------------------------------------
108 std::ostream& operator<<(std::ostream& os,const marVector& v)
111 for(i=0;i < v.size();i++)
119 // ---------------------------------------------------------------------
121 marVector::operator double*() const
126 // ---------------------------------------------------------------------
128 double& marVector::operator()(size_t i)
133 const double& marVector::operator()(size_t i) const
138 // ---------------------------------------------------------------------
140 marVector& marVector::operator=(const marVector& o)
143 if (!(_data==NULL)&&(!shallowCopy))
148 _data=new double[_size];
150 for (i=0;i<_size;i++)
157 marVector& marVector::operator=(double o)
161 if (!(_data==NULL)&&(!shallowCopy))
166 _data=new double[_size];
168 for (i=0;i<_size;i++)
180 marVector& marVector::operator=(double*o)
184 if (!(_data==NULL)&&(!shallowCopy))
189 _data=new double[_size];
191 for (i=0;i<_size;i++)
203 // ---------------------------------------------------------------------
205 bool marVector::operator==(const marVector& o) const
215 for(i=0;i<_size && equal;i++)
217 equal=equal && (_data[i]==o._data[i]);
222 bool marVector::operator!=(const marVector& o) const
224 return(!((*this)==o));
227 // ---------------------------------------------------------------------
229 marVector marVector::operator+(const marVector& o)
231 marVector result(*this);
232 size_t s=result._size;
234 if ((o._size!=s) || (s==0))
240 for (int i=0;i < s;i++)
242 result._data[i] +=o._data[i];
248 marVector marVector::operator+(double o)
250 marVector result(*this);
251 size_t s=result._size;
259 for (int i=0;i < s;i++)
267 marVector marVector::operator+(double*o)
269 marVector result(*this);
270 size_t s=result._size;
278 for (int i=0;i < s;i++)
280 result._data[i] +=o[i];
286 // ---------------------------------------------------------------------
287 /*Addition + Affectation*/
288 marVector& marVector::operator+=(const marVector& o)
290 if ((o._size!=_size) || (_size==0))
296 for (int i=0;i < _size;i++)
298 _data[i] +=o._data[i];
304 marVector& marVector::operator+=(double o)
312 for (int i=0;i < _size;i++)
320 marVector& marVector::operator+=(double*o)
328 for (int i=0;i < _size;i++)
336 // ---------------------------------------------------------------------
338 marVector marVector::operator-(const marVector& o)
340 marVector result(*this);
341 size_t s=result._size;
343 if ((o._size !=s) || (s==0))
349 for (int i=0;i < s;i++)
351 result._data[i] -=o._data[i];
357 marVector marVector::operator-(double o)
359 marVector result(*this);
360 size_t s=result._size;
368 for (int i=0;i < s;i++)
376 marVector marVector::operator-(double*o)
378 marVector result(*this);
379 size_t s=result._size;
387 for (int i=0;i < s;i++)
389 result._data[i] -=o[i];
395 // ---------------------------------------------------------------------
396 /*Sooustraction + Affection*/
397 marVector& marVector::operator-=(const marVector& o)
399 if ((o._size!=_size) || (_size==0))
405 for (int i=0;i < _size;i++)
407 _data[i] -=o._data[i];
413 marVector& marVector::operator-=(double o)
421 for (int i=0;i < _size;i++)
429 marVector& marVector::operator-=(double*o)
437 for (int i=0;i < _size;i++)
445 // ---------------------------------------------------------------------
446 /*Multiplication (produit scalaire)*/
447 marVector marVector::operator*(double o)
449 marVector result(*this);
450 size_t s=result._size;
458 for (int i=0;i < s;i++)
466 // ---------------------------------------------------------------------
467 /*Multiplication (produit scalaire) + Affectation*/
468 marVector& marVector::operator*=(double o)
476 for (int i=0;i < _size;i++)
484 // ---------------------------------------------------------------------
485 /*Division (division scalaire)*/
486 marVector marVector::operator/(double o)
488 marVector result(*this);
489 size_t s=result._size;
497 for (int i=0;i < s;i++)
505 // ---------------------------------------------------------------------
506 /*Division (division scalaire) + Affectation*/
507 marVector& marVector::operator/=(double o)
515 for (int i=0;i < _size;i++)
523 //////////////////////////////////////////////////////////////////////
525 //////////////////////////////////////////////////////////////////////
526 // ---------------------------------------------------------------------
528 double marVector::dot(const marVector& o)
531 if ((_size!=o._size) || (_size==0))
538 for (int i=0;i<_size;i++)
540 result+=(_data[i]*o._data[i]);
546 double marVector::dot(double*o)
556 for (int i=0;i<_size;i++)
558 result+=(_data[i]*o[i]);
564 // ---------------------------------------------------------------------
565 /*Produit vectoriel*/
566 marVector marVector::cross(const marVector& o)
568 marVector result(*this);
569 if ((o._size!=_size) || (_size!=3))
578 S=_data[1]*o._data[2];
579 s=_data[2]*o._data[1];
580 result._data[0]=S - s;
583 S=_data[2]*o._data[0];
584 s=_data[0]*o._data[2];
585 result._data[1]=S - s;
588 S=_data[0]*o._data[1];
589 s=_data[1]*o._data[0];
590 result._data[2]=S - s;
595 marVector marVector::cross(double*o)
597 marVector result(*this);
609 result._data[0]=S - s;
614 result._data[1]=S - s;
619 result._data[2]=S - s;
624 int marVector::scross(const marVector& o)
626 if ((o._size!=_size) && (_size!=3))
637 int marVector::scross(double*o)
650 // ---------------------------------------------------------------------
651 /*Norme euclidienne*/
652 double marVector::norm2()
653 // Calcule la norme euclidienne du vecteur
654 // en utilisant l'agorithme de Blue
664 const double seuil1=pow( 2., 154. ),seuil2=1/seuil1;
665 double n1=0; /* les sommes partielles */
670 for ( i=0; i<_size; i++ )
672 double x=_data[i]; /* x=abs(vecteur[i]) */
678 /* sommation par classe */
684 else if ( x < seuil2 )
698 n4=seuil1 * sqrt(n1);
702 n4=seuil2 * sqrt(n2);
710 norme2=n4 * sqrt( 1 + n3*n3 );
715 norme2=n3 * sqrt( 1 + n4*n4 );
723 // ---------------------------------------------------------------------
725 marVector marVector::normalize( )
727 marVector result=marVector(*this);
728 result *= (1/result.norm2());
732 int marVector::snormalize( )
734 (*this)*=(1/this->norm2());
736 }// renvoie 0 si OK, 1 sinon
738 //////////////////////////////////////////////////////////////////////
740 //////////////////////////////////////////////////////////////////////
742 size_t marVector::size() const