soma-io  5.0.5
vector.h
Go to the documentation of this file.
1 /* This software and supporting documentation are distributed by
2  * Institut Federatif de Recherche 49
3  * CEA/NeuroSpin, Batiment 145,
4  * 91191 Gif-sur-Yvette cedex
5  * France
6  *
7  * This software is governed by the CeCILL-B license under
8  * French law and abiding by the rules of distribution of free software.
9  * You can use, modify and/or redistribute the software under the
10  * terms of the CeCILL-B license as circulated by CEA, CNRS
11  * and INRIA at the following URL "http://www.cecill.info".
12  *
13  * As a counterpart to the access to the source code and rights to copy,
14  * modify and redistribute granted by the license, users are provided only
15  * with a limited warranty and the software's author, the holder of the
16  * economic rights, and the successive licensors have only limited
17  * liability.
18  *
19  * In this respect, the user's attention is drawn to the risks associated
20  * with loading, using, modifying and/or developing or reproducing the
21  * software by the user in light of its specific status of free software,
22  * that may mean that it is complicated to manipulate, and that also
23  * therefore means that it is reserved for developers and experienced
24  * professionals having in-depth computer knowledge. Users are therefore
25  * encouraged to load and test the software's suitability as regards their
26  * requirements in conditions enabling the security of their systems and/or
27  * data to be ensured and, more generally, to use and operate it in the
28  * same conditions as regards security.
29  *
30  * The fact that you are presently reading this means that you have had
31  * knowledge of the CeCILL-B license and that you accept its terms.
32  */
33 
34 /*
35  * AIMS vector class
36  *
37  * These classes used to be in aimsdata, but they are used in Transformation
38  * classes, which are needed in IO, so they have been moved to soma-io
39  * in AIMS 4.7.
40  */
41 #ifndef SOMAIO_VECTOR_VECTOR_H
42 #define SOMAIO_VECTOR_VECTOR_H
43 
45 #include <cartobase/type/types.h>
49 #include <fstream>
50 #include <math.h>
51 #include <vector>
52 
53 template <class T,int D> class AimsVector;
54 
55 
56 template <class T,int D>
57 bool operator == (const AimsVector<T,D>& v1,
58  const AimsVector<T,D>& v2);
59 
60 template <class T,int D>
61 bool operator == (const AimsVector<T,D>& v1,
62  const T& val);
63 
65 template <class T,int D>
67  const AimsVector<T,D>& v2)
68 {
69  return !( v1 == v2 );
70 }
71 
73 template <class T,int D>
75  const T& val)
76 {
77  return !( v1 == val );
78 }
79 
80 template <class T,int D>
82  const AimsVector<T,D>& v2);
83 
84 template <class T,int D>
86  const AimsVector<T,D>& v2);
87 
88 template <class T,int D>
90  double value );
91 
92 template <class T,int D>
93 AimsVector<T,D> operator * (double value,
94  const AimsVector<T,D>& v1);
95 
96 template <class T,int D>
98  double value);
99 
100 template <class T,int D>
102 
103 template <class T,int D>
105 
106 template <class T,int D>
108  const AimsVector<T,D>& v2);
109 
110 template <class T,int D>
111 float norm(const AimsVector<T,D>& v1);
112 
113 template <class T,int D>
114 float norm2(const AimsVector<T,D>& v1);
115 
116 template <class T,int D>
117 double dnorm(const AimsVector<T,D>& v1);
118 
119 template <class T,int D>
120 double dnorm2(const AimsVector<T,D>& v1);
121 
122 template <class T,int D>
123 std::ostream& operator << (std::ostream& out,
124  const AimsVector<T,D>& thing);
125 
126 template <class T,int D>
127 std::istream& operator >> (std::istream& in,
128  AimsVector<T,D>& thing);
129 
130 
135 template <class T,int D>
136 class AimsVector
137 {
138  public:
141  AimsVector();
143  explicit AimsVector( const std::vector<T> & value );
145  AimsVector(const T& value);
147  explicit AimsVector(const T value[]);
149  AimsVector(const T& x,const T& y);
151  AimsVector(const T& x,const T& y,const T& z);
153  AimsVector(const T& x,const T& y,const T& z,const T& t);
155  AimsVector(const AimsVector<T,D>& other);
157  template<class U>
158  explicit AimsVector(const AimsVector<U,D>& other);
159 
161  ~AimsVector();
163 
169  AimsVector<T,D>& operator *= ( double val );
170  AimsVector<T,D>& operator /= ( double val );
172 
174  const T& item(int d) const;
176  const T& operator[](int d) const;
178  T& item(int d);
180  T& operator[](int d);
181 
182  int size() const;
184  bool isNull() const;
186  float norm() const;
188  float norm2() const;
190  double dnorm() const;
192  double dnorm2() const;
194  T dot(const AimsVector<T,D>& other) const;
195 
197 
200  typedef T * iterator;
201  typedef const T * const_iterator;
202  iterator begin() { return _value; }
203  const_iterator begin() const { return _value; }
204  iterator end() { return _value + D; }
205  const_iterator end() const { return _value + D; }
207 
208  std::vector<T> toStdVector() const;
209 
210  protected:
212  T _value[D];
213 };
214 
215 
218 // Keep those for compability
225 // 2016: l now means long (64bit), i int (32bit), s short (16bit)
251 
252 
253 extern template class AimsVector<int16_t,2>;
254 extern template class AimsVector<int16_t,3>;
255 extern template class AimsVector<int16_t,4>;
256 extern template class AimsVector<int32_t,2>;
257 extern template class AimsVector<int32_t,3>;
258 extern template class AimsVector<int32_t,4>;
259 extern template class AimsVector<int64_t,2>;
260 extern template class AimsVector<int64_t,3>;
261 extern template class AimsVector<int64_t,4>;
262 extern template class AimsVector<uint16_t,2>;
263 extern template class AimsVector<uint16_t,3>;
264 extern template class AimsVector<uint16_t,4>;
265 extern template class AimsVector<uint32_t,2>;
266 extern template class AimsVector<uint32_t,3>;
267 extern template class AimsVector<uint32_t,4>;
268 extern template class AimsVector<uint64_t,2>;
269 extern template class AimsVector<uint64_t,3>;
270 extern template class AimsVector<uint64_t,4>;
271 extern template class AimsVector<float,2>;
272 extern template class AimsVector<float,3>;
273 extern template class AimsVector<float,4>;
274 extern template class AimsVector<double,2>;
275 extern template class AimsVector<double,3>;
276 extern template class AimsVector<double,4>;
277 
278 #ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
279 
280 namespace carto
281 {
306 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point2ds> )
307 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point3ds> )
308 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point4ds> )
309 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point2di> )
310 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point3di> )
311 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point4di> )
312 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point2dl> )
313 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point3dl> )
314 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point4dl> )
315 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point2dus> )
316 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point3dus> )
317 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point4dus> )
318 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point2dui> )
319 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point3dui> )
320 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point4dui> )
321 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point2dul> )
322 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point3dul> )
323 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point4dul> )
324 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point2df> )
325 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point3df> )
326 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point4df> )
327 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point2dd> )
328 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point3dd> )
329 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point4dd> )
330 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point2ds> > )
331 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point3ds> > )
332 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point4ds> > )
333 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point2di> > )
334 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point3di> > )
335 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point4di> > )
336 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point2dl> > )
337 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point3dl> > )
338 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point4dl> > )
339 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point2dus> > )
340 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point3dus> > )
341 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point4dus> > )
342 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point2dui> > )
343 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point3dui> > )
344 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point4dui> > )
345 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point2dul> > )
346 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point3dul> > )
347 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point4dul> > )
348 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point2df> > )
349 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point3df> > )
350 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point4df> > )
351 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point2dd> > )
352 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point3dd> > )
353 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point4dd> > )
354 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point2ds> > )
355 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point3ds> > )
356 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point4ds> > )
357 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point2di> > )
358 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point3di> > )
359 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point4di> > )
360 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point2dl> > )
361 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point3dl> > )
362 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point4dl> > )
363 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point2dus> > )
364 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point3dus> > )
365 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point4dus> > )
366 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point2dui> > )
367 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point3dui> > )
368 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point4dui> > )
369 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point2dul> > )
370 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point3dul> > )
371 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point4dul> > )
372 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point2df> > )
373 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point3df> > )
374 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point4df> > )
375 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point2dd> > )
376 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point3dd> > )
377 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point4dd> > )
378 
379  // partial specializations
380 
381  template<class T, int D> class DataTypeCode<AimsVector<T,D> >
382  {
383  public:
384  static std::string objectType()
385  { return( "Vector" ); }
386  static std::string dataType()
387  {
388  char num[10];
389  sprintf( num, "%d", (int) D );
390  return( std::string( "VECTOR_OF_" ) + num + "_" +
392  }
393  static std::string name()
394  {
395  return DataTypeCode<AimsVector<T,D> >::dataType();
396  }
397  };
398 
399  template<> inline std::string DataTypeCode<Point2df>::dataType()
400  {
401  return "POINT2DF";
402  }
403 
404  template<> inline std::string DataTypeCode<Point3df>::dataType()
405  {
406  return "POINT3DF";
407  }
408 
409 
410 template<typename T, int D>
412 {
413  public :
414  typedef T ChannelType;
416 
417  static const bool is_scalar = false;
418  static const bool is_multichannel = true;
419  static const bool has_bool_conversion = false;
420  static const unsigned int channelcount = D;
421 };
422 
423 }
424 
425 #endif // DOXYGEN_HIDE_INTERNAL_CLASSES
426 
427 
428 //
429 // Members of class AimsVector<T,D>
430 //
431 template <class T,int D>
432 inline
434 {
435 }
436 
437 
438 template <class T,int D>
439 inline
441 {
442  for (int d = 0; d < D; d++)
443  _value[d] = value;
444 }
445 
446 
447 template <class T,int D>
448 inline
450 {
451  for (int d = 0; d < D; d++)
452  _value[d] = value[d];
453 }
454 
455 template <class T, int D>
456 inline
457 AimsVector<T,D>::AimsVector( const std::vector<T> & value )
458 {
459  int d, N = std::min( typename std::vector<T>::size_type( D ), value.size() );
460  for( d = 0; d < N; ++d )
461  _value[d] = value[d];
462  for( ; d<D; ++d )
463  _value[d] = T(0);
464 }
465 
466 #ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
467 namespace internal
468 {
469  template <typename T, int D>
471  {
472  public:
473  inline static void doit( T _value[D], const T& x, const T& y )
474  {
475  _value[0] = x;
476  _value[1] = y;
477  for (int d = 2; d < D; d++)
478  _value[d] = 0;
479  }
480  };
481 
482  template <typename T, int D>
484  {
485  public:
486  inline static void doit( T _value[D], const T& x, const T& y, const T& z )
487  {
488  _value[0] = x;
489  _value[1] = y;
490  _value[2] = z;
491  for (int d = 3; d < D; d++)
492  _value[d] = 0;
493  }
494  };
495 
496  template <typename T, int D>
498  {
499  public:
500  inline static void doit( T _value[D], const T& x, const T& y, const T& z,
501  const T& t )
502  {
503  _value[0] = x;
504  _value[1] = y;
505  _value[2] = z;
506  _value[3] = t;
507  for (int d = 4; d < D; d++)
508  _value[d] = 0;
509  }
510  };
511 
512  template <typename T>
513  class fill_aimsvector2<T, 1>
514  {
515  public:
516  inline static void doit( T _value[1], const T& x, const T& )
517  {
518  _value[0] = x;
519  }
520  };
521 
522  template <typename T>
523  class fill_aimsvector3<T, 1>
524  {
525  public:
526  inline static void doit( T _value[1], const T& x, const T&, const T& )
527  {
528  _value[0] = x;
529  }
530  };
531 
532  template <typename T>
533  class fill_aimsvector4<T, 1>
534  {
535  public:
536  inline static void doit( T _value[1], const T& x, const T&, const T&,
537  const T& )
538  {
539  _value[0] = x;
540  }
541  };
542 
543  template <typename T>
544  class fill_aimsvector3<T, 2>
545  {
546  public:
547  inline static void doit( T _value[2], const T& x, const T& y, const T& )
548  {
549  _value[0] = x;
550  _value[1] = y;
551  }
552  };
553 
554  template <typename T>
555  class fill_aimsvector4<T, 2>
556  {
557  public:
558  inline static void doit( T _value[2], const T& x, const T& y, const T&,
559  const T& )
560  {
561  _value[0] = x;
562  _value[1] = y;
563  }
564  };
565 
566  template <typename T>
567  class fill_aimsvector4<T, 3>
568  {
569  public:
570  inline static void doit( T _value[3], const T& x, const T& y, const T& z,
571  const T& )
572  {
573  _value[0] = x;
574  _value[1] = y;
575  _value[2] = z;
576  }
577  };
578 
579 }
580 #endif
581 
582 template <class T,int D>
583 inline
584 AimsVector<T,D>::AimsVector(const T& x, const T& y)
585 {
587 }
588 
589 
590 template <class T,int D>
591 inline
592 AimsVector<T,D>::AimsVector(const T& x, const T& y, const T& z)
593 {
595 }
596 
597 
598 template <class T,int D>
599 inline
600 AimsVector<T,D>::AimsVector(const T& x, const T& y, const T& z, const T& t)
601 {
603 }
604 
605 
606 template <class T,int D>
607 inline
609 {
610  for (int d = 0; d < D; d++)
611  _value[d] = other._value[d];
612 }
613 
614 template <class T,int D> template<class U>
615 inline
617 {
618  for (int d = 0; d < D; d++)
619  _value[d] = other[d];
620 }
621 
622 
623 template <class T,int D>
624 inline
626 {
627  if (this == &other)
628  return *this;
629  for (int d = 0; d < D; d++)
630  _value[d] = other._value[d];
631  return *this;
632 }
633 
634 
635 template <class T,int D>
636 inline
638 {
639 }
640 
641 
642 template <class T,int D>
643 inline
645 {
646  for (int d = 0;d < D; d++)
647  _value[d] += other._value[d];
648  return *this;
649 }
650 
651 
652 template <class T,int D>
653 inline
655 {
656  for (int d = 0; d < D; d++)
657  _value[d] -= other._value[d];
658  return *this;
659 }
660 
661 
662 template <class T,int D>
663 inline
665 {
666  for (int d = 0; d < D; d++)
667  _value[d] = (T) ( _value[d] * val );
668  return *this;
669 }
670 
671 
672 template <class T,int D>
673 inline
675 {
676  for (int d = 0; d < D; d++)
677  _value[d] = (T) ( _value[d] / val );
678  return *this;
679 }
680 
681 
682 template <class T,int D>
683 inline
685 {
686  float n = norm();
687  if( n != 0.f )
688  return operator /= ( n );
689  else
690  return *this;
691 }
692 
693 
694 template <class T,int D>
695 inline
696 const T& AimsVector<T,D>::item(int d) const
697 {
698  ASSERT(d < D);
699  return _value[d];
700 }
701 
702 
703 template <class T,int D>
704 inline
705 const T& AimsVector<T,D>::operator[](int d) const
706 {
707  ASSERT(d < D);
708  return _value[d];
709 }
710 
711 
712 template <class T,int D>
713 inline
715 {
716  ASSERT(d < D);
717  return _value[d];
718 }
719 
720 
721 template <class T,int D>
722 inline
724 {
725  ASSERT(d < D);
726  return _value[d];
727 }
728 
729 template <class T,int D>
730 inline
732 {
733  return D;
734 }
735 
736 template <class T,int D>
737 inline
739 {
740  for (int d = 0; d < D; ++d) {
741  if ( _value[ d ] ) return false;
742  }
743  return true;
744 }
745 
746 template <class T,int D>
747 inline
749 {
750  float result = 0;
751  for (int d = 0; d < D; ++d)
752  result += float(item(d)) * float(item(d));
753  return sqrt(result);
754 }
755 
756 template <class T,int D>
757 inline
759 {
760  float result = 0;
761  for (int d = 0; d < D; ++d)
762  result += float(item(d)) * float(item(d));
763  return result;
764 }
765 
766 template <class T,int D>
767 inline
769 {
770  double result = 0;
771  for (int d = 0; d < D; ++d)
772  result += double(item(d)) * double(item(d));
773  return sqrt(result);
774 }
775 
776 template <class T,int D>
777 inline
779 {
780  double result = 0;
781  for (int d = 0; d < D; ++d)
782  result += double(item(d)) * double(item(d));
783  return result;
784 }
785 
786 
787 template <class T,int D>
788 inline
790 {
791  T result = (T)0;
792  for (int d = 0; d < D; ++d)
793  result += item(d) * other.item(d);
794  return result;
795 }
796 
797 
798 template <class T,int D>
799 inline
800 std::vector<T> AimsVector<T, D>::toStdVector() const
801 {
802  std::vector<T> vec( D );
803  int i;
804  for( i=0; i<D; ++i )
805  vec[i] = item(i);
806  return vec;
807 }
808 
809 
810 //
811 // Friend functions
812 //
813 
814 template <class T,int D>
815 inline
817  const AimsVector<T,D>& v2)
818 {
819  for (int d = 0; d < D; ++d)
820  if (v1.item(d) != v2.item(d))
821  return false;
822  return true;
823 }
824 
825 
826 template <class T,int D>
827 inline
829  const T& value)
830 {
831  for (int d = 0; d < D; ++d)
832  if (v1[d] != value)
833  return false;
834  return true;
835 }
836 
837 
838 template <class T,int D>
839 inline
841  const AimsVector<T,D> &v2)
842 {
843  AimsVector<T,D> result;
844  for (int d = 0; d < D; ++d)
845  result[d] = v1[d] + v2[d];
846  return result;
847 }
848 
849 
850 template <class T,int D>
851 inline
853  const AimsVector<T,D>& v2)
854 {
855  AimsVector<T,D> result;
856  for (int d = 0; d < D; ++d)
857  result[d] = v1[d] - v2[d];
858  return result;
859 }
860 
861 
862 template <class T,int D>
863 inline
865  double value )
866 {
867  AimsVector<T,D> result;
868  for (int d = 0; d < D; ++d)
869  result[d] = (T) ( v1[d] * value );
870  return result;
871 }
872 
873 
874 template <class T,int D>
875 inline
877  const AimsVector<T,D>& v1 )
878 {
879  AimsVector<T,D> result;
880  for (int d = 0; d < D; ++d)
881  result[d] = (T) ( v1[d] * value );
882  return result;
883 }
884 
885 
886 template <class T,int D>
887 inline
889  double value )
890 {
891  AimsVector<T,D> result;
892  for (int d = 0; d < D; ++d)
893  result[d] = (T) ( v1[d] / value );
894  return result;
895 }
896 
897 
898 template <class T,int D>
899 inline
901 {
902  return v1;
903 }
904 
905 
906 template <class T,int D>
907 inline
909 {
910  AimsVector<T,D> result;
911  for (int d = 0; d < D; ++d)
912  result[d] = -v1[d];
913  return result;
914 }
915 
916 
917 template <class T,int D>
918 inline
920  const AimsVector<T,D>& v2)
921 {
922  AimsVector<T,3> result;
923  result[0] = v1[1] * v2[2] - v1[2] * v2[1];
924  result[1] = v1[2] * v2[0] - v1[0] * v2[2];
925  result[2] = v1[0] * v2[1] - v1[1] * v2[0];
926  return result;
927 }
928 
929 
930 template <class T,int D>
931 inline
932 float norm(const AimsVector<T,D>& v1)
933 {
934  return v1.norm();
935 }
936 
937 
938 template <class T,int D>
939 inline
940 float norm2(const AimsVector<T,D>& v1)
941 {
942  return v1.norm2();
943 }
944 
945 template <class T,int D>
946 inline
947 double dnorm(const AimsVector<T,D>& v1)
948 {
949  return v1.dnorm();
950 }
951 
952 
953 template <class T,int D>
954 inline
955 double dnorm2(const AimsVector<T,D>& v1)
956 {
957  return v1.dnorm2();
958 }
959 
960 
961 template <class T,int D>
962 inline
963 std::ostream& operator << ( std::ostream& out,const AimsVector<T,D>& thing)
964 {
965  out << '(';
966  for (int d = 0; d < D-1 ; ++d)
967  out << thing.item(d) << ", ";
968  out << thing.item(D-1) << ')';
969  return(out);
970 }
971 
972 
973 template <int D>
974 inline
975 std::ostream& operator << ( std::ostream& out,const AimsVector<char,D>& thing)
976 {
977  out << '(';
978  for (int d = 0; d < D-1 ; ++d)
979  out << (int)thing.item(d) << ", ";
980  out << (int)thing.item(D-1) << ')';
981  return(out);
982 }
983 
984 
985 template <int D>
986 inline
987 std::ostream& operator << ( std::ostream& out,const AimsVector<byte,D>& thing)
988 {
989  out << '(';
990  for (int d = 0; d < D-1 ; ++d)
991  out << (int)thing.item(d) << ", ";
992  out << (int)thing.item(D-1) << ')';
993  return(out);
994 }
995 
996 
997 template <class T,int D>
998 inline
999 std::istream& operator >> (std::istream& is, AimsVector<T,D>& thing)
1000 {
1001  char ch = 0;
1002 
1003  //std::istream::sentry s( is ); // to use in the future...
1004  carto::StreamUtil::skip( is );
1005  if( !is )
1006  return is;
1007  if (is.peek() == '(')
1008  {
1009  is >> ch;
1010  carto::StreamUtil::skip( is );
1011  is >> thing.item(0);
1012  for (int i = 1; i < D; ++i)
1013  {
1014  carto::StreamUtil::skip( is );
1015  is >> ch;
1016  if (ch != ',')
1017  break;
1018  carto::StreamUtil::skip( is );
1019  is >> thing.item(i);
1020  }
1021  if( is )
1022  {
1023  carto::StreamUtil::skip( is );
1024  is >> ch;
1025  }
1026  }
1027 
1028  if( ch != ')' )
1029  std::cout << "vector read ends not with )\n";
1030  //is.setstate (std::ios::failbit);
1031 
1032  return is;
1033 }
1034 
1035 
1036 template <int D>
1037 inline
1038 std::istream& operator >> (std::istream& is, AimsVector<char,D>& thing)
1039 {
1040  char ch = 0;
1041  int tmp;
1042 
1043  //std::istream::sentry s( is ); // to use in the future...
1044  carto::StreamUtil::skip( is );
1045  if( !is )
1046  return is;
1047  if (is.peek() == '(')
1048  {
1049  is >> ch;
1050  carto::StreamUtil::skip( is );
1051  is >> tmp;
1052  thing.item(0) = (char)tmp;
1053  for (int i = 1; i < D; ++i)
1054  {
1055  carto::StreamUtil::skip( is );
1056  is >> ch;
1057  if (ch != ',')
1058  break;
1059  carto::StreamUtil::skip( is );
1060  is >> tmp;
1061  thing.item(i) = (char)tmp;
1062  }
1063  if( is )
1064  {
1065  carto::StreamUtil::skip( is );
1066  is >> ch;
1067  }
1068  }
1069 
1070  if( ch != ')' )
1071  is.setstate (std::ios::failbit);
1072 
1073  return is;
1074 }
1075 
1076 
1077 template <int D>
1078 inline
1079 std::istream& operator >> (std::istream& is, AimsVector<byte,D>& thing)
1080 {
1081  char ch = 0;
1082  int tmp;
1083 
1084  //std::istream::sentry s( is ); // to use in the future...
1085  carto::StreamUtil::skip( is );
1086  if( !is )
1087  return is;
1088  if (is.peek() == '(')
1089  {
1090  is >> ch;
1091  carto::StreamUtil::skip( is );
1092  is >> tmp;
1093  thing.item(0) = (byte)tmp;
1094  for (int i = 1; i < D; ++i)
1095  {
1096  carto::StreamUtil::skip( is );
1097  is >> ch;
1098  if (ch != ',')
1099  break;
1100  carto::StreamUtil::skip( is );
1101  is >> tmp;
1102  thing.item(i) = (byte)tmp;
1103  }
1104  if( is )
1105  {
1106  carto::StreamUtil::skip( is );
1107  is >> ch;
1108  }
1109  }
1110 
1111  if( ch != ')' )
1112  is.setstate (std::ios::failbit);
1113 
1114  return is;
1115 }
1116 
1117 
1118 
1119 template <typename T>
1120 inline
1122  const AimsVector<T,3> & v2 )
1123 {
1124  return AimsVector<T,3>( v1[1] * v2[2] - v1[2] * v2[1],
1125  v1[2] * v2[0] - v1[0] * v2[2],
1126  v1[0] * v2[1] - v1[1] * v2[0] );
1127 }
1128 
1129 
1130 //=== AsciiDataSourceTraits specializations ===================================
1131 
1132 namespace soma
1133 {
1134  //=== AIMSVECTOR<T> =========================================================
1135 
1136  template<typename T, int D>
1138  {
1139  public:
1140  static bool read( DataSource & ds, AimsVector<T, D> & item );
1141  static bool write( DataSource & ds, const AimsVector<T, D> & item );
1142  };
1143 
1144 
1145  // -----------
1146 
1147 #ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
1148 
1149  template<typename T, int D>
1150  inline
1152  AimsVector<T, D> & x )
1153  {
1154  char c;
1155 
1156  if( !StreamUtil::skip( ds ) )
1157  return false;
1158  c = ds.getch();
1159  if( c != '(' )
1160  return false;
1161 
1162  for( int i=0; i<D; ++i )
1163  {
1164  if( !StreamUtil::skip( ds ) )
1165  return false;
1166  if( !AsciiDataSourceTraits<T>::read( ds, x[i] ) )
1167  return false;
1168  if( i < D-1 )
1169  {
1170  if( !StreamUtil::skip( ds ) )
1171  return false;
1172  c = ds.getch();
1173  if( c != ',' )
1174  return false;
1175  }
1176  }
1177 
1178  if( !StreamUtil::skip( ds ) )
1179  return false;
1180  c = ds.getch();
1181  if( c != ')' )
1182  return false;
1183 
1184  return true;
1185  }
1186 
1187 
1188  template<typename T, int D>
1189  inline
1191  DataSource & ds, const AimsVector<T, D> & x )
1192  {
1193  ds.putch( '(');
1194 
1195 
1196  for( int i=0; i<D; ++i )
1197  {
1198  AsciiDataSourceTraits<T>::write( ds, x[i] );
1199  if( i < D-1 )
1200  {
1201  ds.putch( ',' );
1202  ds.putch( ' ' );
1203  }
1204  }
1205 
1206  ds.putch( ')');
1207  return ds.isOpen();
1208  }
1209 
1210 
1211  template <typename T, int D>
1213  const AimsVector<T, D> & x )
1214  {
1215  AsciiDataSourceTraits<AimsVector<T, D> >::write( ds, x );
1216  return ds;
1217  }
1218 
1219 #endif
1220 
1221 }
1222 
1223 #endif
#define DECLARE_GENERIC_OBJECT_TYPE(T)
float norm2() const
Return the square magnitude of the vector.
Definition: vector.h:758
AimsVector< uint32_t, 2 > Point2dui
Definition: vector.h:238
static void doit(T _value[D], const T &x, const T &y, const T &z, const T &t)
Definition: vector.h:500
AimsVector< double, 4 > Point4dd
Definition: vector.h:249
AimsVector< uint16_t, 2 > Point2dus
Definition: vector.h:235
const_iterator begin() const
Definition: vector.h:203
AimsVector< int32_t, 2 > Point2di
Definition: vector.h:229
AimsVector< T, D > operator*(const AimsVector< T, D > &v1, double value)
Definition: vector.h:864
AimsVector< uint32_t, 3 > Point3du
Definition: vector.h:223
AimsVector< int16_t, 3 > Point3ds
Definition: vector.h:227
AimsVector< float, 4 > Point4df
Definition: vector.h:246
AimsVector< T, D > operator-(const AimsVector< T, D > &v1, const AimsVector< T, D > &v2)
Definition: vector.h:852
static void doit(T _value[D], const T &x, const T &y)
Definition: vector.h:473
The template class to implement basic vectors.
Definition: vector.h:53
static void doit(T _value[2], const T &x, const T &y, const T &)
Definition: vector.h:547
AimsVector< uint32_t, 4 > Point4dui
Definition: vector.h:240
AimsVector< int64_t, 4 > Point4dl
Definition: vector.h:234
AimsVector< double, 3 > Point3dd
Definition: vector.h:248
bool operator!=(const AimsVector< T, D > &v1, const AimsVector< T, D > &v2)
should be defined implicitely, but fails with some compilers (Intel)
Definition: vector.h:66
float norm2(const AimsVector< T, D > &v1)
Definition: vector.h:940
AimsVector< float, 2 > Point2df
Definition: vector.h:244
AimsVector< T, D > operator/(const AimsVector< T, D > &v1, double value)
Definition: vector.h:888
const T & operator[](int d) const
Get a const reference to the dth item.
Definition: vector.h:705
AimsVector< int16_t, 2 > Point2d
Definition: vector.h:219
AimsVector< T, D > & normalize()
Definition: vector.h:684
static void doit(T _value[1], const T &x, const T &)
Definition: vector.h:516
AimsVector< T, 3 > crossed(const AimsVector< T, D > &v1, const AimsVector< T, D > &v2)
Definition: vector.h:919
Abstraction layer for various data sources (file, buffer, socket...).
Definition: datasource.h:64
Definition: allocator.h:48
AimsVector< T, D > & operator/=(double val)
Definition: vector.h:674
bool operator==(const AimsVector< T, D > &v1, const AimsVector< T, D > &v2)
Definition: vector.h:816
std::istream & operator>>(std::istream &in, AimsVector< T, D > &thing)
Definition: vector.h:999
AimsVector< int64_t, 2 > Point2dl
Definition: vector.h:232
double dnorm2(const AimsVector< T, D > &v1)
Definition: vector.h:955
std::vector< T > toStdVector() const
Definition: vector.h:800
static void doit(T _value[D], const T &x, const T &y, const T &z)
Definition: vector.h:486
bool isNull() const
Test if the vector is the null vector.
Definition: vector.h:738
int size() const
Definition: vector.h:731
T * iterator
Definition: vector.h:200
AimsVector< uint32_t, 3 > Point3dui
Definition: vector.h:239
double dnorm2() const
Return the square magnitude of the vector.
Definition: vector.h:778
AimsVector< int16_t, 4 > Point4d
Definition: vector.h:221
AimsVector< float, 3 > Point3df
Definition: vector.h:245
AimsVector< T, D > operator+(const AimsVector< T, D > &v1, const AimsVector< T, D > &v2)
Definition: vector.h:840
const T * const_iterator
Definition: vector.h:201
AimsVector< T, D > & operator+=(const AimsVector< T, D > &other)
Definition: vector.h:644
float norm(const AimsVector< T, D > &v1)
Definition: vector.h:932
T dot(const AimsVector< T, D > &other) const
Get the dot product between 2 vectors.
Definition: vector.h:789
T _value[D]
Memory space allocated.
Definition: vector.h:212
static void doit(T _value[3], const T &x, const T &y, const T &z, const T &)
Definition: vector.h:570
AimsVector< T, D > & operator=(const AimsVector< T, D > &other)
Definition: vector.h:625
AimsVector()
The constructor allocates an appropriate amount of memory.
Definition: vector.h:433
static void doit(T _value[2], const T &x, const T &y, const T &, const T &)
Definition: vector.h:558
AimsVector< int16_t, 2 > Point2ds
Definition: vector.h:226
AimsVector< int32_t, 4 > Point4di
Definition: vector.h:231
AimsVector< uint64_t, 3 > Point3dul
Definition: vector.h:242
AimsVector< uint64_t, 2 > Point2dul
Definition: vector.h:241
float norm() const
Return the magnitude of the vector.
Definition: vector.h:748
AimsVector< int32_t, 3 > Point3di
Definition: vector.h:230
AimsVector< T, 3 > vectProduct(const AimsVector< T, 3 > &v1, const AimsVector< T, 3 > &v2)
Definition: vector.h:1121
AimsVector< int16_t, 3 > Point3d
Definition: vector.h:220
~AimsVector()
The destructor deletes the allocated memory space.
Definition: vector.h:637
AimsVector< uint64_t, 4 > Point4dul
Definition: vector.h:243
static void doit(T _value[1], const T &x, const T &, const T &, const T &)
Definition: vector.h:536
double dnorm(const AimsVector< T, D > &v1)
Definition: vector.h:947
AimsVector< uint32_t, 4 > Point4du
Definition: vector.h:224
iterator end()
Definition: vector.h:204
virtual int putch(int ch)=0
std::ostream & operator<<(std::ostream &out, const AimsVector< T, D > &thing)
Definition: vector.h:963
virtual bool isOpen() const =0
uint8_t byte
AimsVector< int64_t, 3 > Point3dl
Definition: vector.h:233
iterator begin()
Definition: vector.h:202
static void doit(T _value[1], const T &x, const T &, const T &)
Definition: vector.h:526
#define ASSERT(EX)
AimsVector< double, 2 > Point2dd
Definition: vector.h:247
AimsVector< int16_t, 4 > Point4ds
Definition: vector.h:228
AimsVector< T, D > & operator-=(const AimsVector< T, D > &other)
Definition: vector.h:654
AimsVector< uint16_t, 3 > Point3dus
Definition: vector.h:236
virtual int getch()=0
double dnorm() const
Return the magnitude of the vector.
Definition: vector.h:768
const T & item(int d) const
Get a const reference to the dth item.
Definition: vector.h:696
AimsVector< uint16_t, 4 > Point4dus
Definition: vector.h:237
AimsVector< uint32_t, 2 > Point2du
Definition: vector.h:222
const_iterator end() const
Definition: vector.h:205
AimsVector< T, D > & operator*=(double val)
Definition: vector.h:664