soma-io  5.1.2
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>
137 {
138  public:
142  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  typedef T value_type;
209 
210  std::vector<T> toStdVector() const;
211 
212  protected:
214  T _value[D];
215 };
216 
217 
220 // Keep those for compatibility
227 // 2016: l now means long (64bit), i int (32bit), s short (16bit)
253 
254 
255 extern template class AimsVector<int16_t,2>;
256 extern template class AimsVector<int16_t,3>;
257 extern template class AimsVector<int16_t,4>;
258 extern template class AimsVector<int32_t,2>;
259 extern template class AimsVector<int32_t,3>;
260 extern template class AimsVector<int32_t,4>;
261 extern template class AimsVector<int64_t,2>;
262 extern template class AimsVector<int64_t,3>;
263 extern template class AimsVector<int64_t,4>;
264 extern template class AimsVector<uint16_t,2>;
265 extern template class AimsVector<uint16_t,3>;
266 extern template class AimsVector<uint16_t,4>;
267 extern template class AimsVector<uint32_t,2>;
268 extern template class AimsVector<uint32_t,3>;
269 extern template class AimsVector<uint32_t,4>;
270 extern template class AimsVector<uint64_t,2>;
271 extern template class AimsVector<uint64_t,3>;
272 extern template class AimsVector<uint64_t,4>;
273 extern template class AimsVector<float,2>;
274 extern template class AimsVector<float,3>;
275 extern template class AimsVector<float,4>;
276 extern template class AimsVector<double,2>;
277 extern template class AimsVector<double,3>;
278 extern template class AimsVector<double,4>;
279 
280 #ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
281 
282 namespace carto
283 {
308 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point2ds> )
309 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point3ds> )
310 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point4ds> )
311 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point2di> )
312 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point3di> )
313 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point4di> )
314 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point2dl> )
315 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point3dl> )
316 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point4dl> )
317 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point2dus> )
318 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point3dus> )
319 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point4dus> )
320 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point2dui> )
321 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point3dui> )
322 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point4dui> )
323 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point2dul> )
324 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point3dul> )
325 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point4dul> )
326 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point2df> )
327 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point3df> )
328 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point4df> )
329 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point2dd> )
330 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point3dd> )
331 DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point4dd> )
332 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point2ds> > )
333 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point3ds> > )
334 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point4ds> > )
335 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point2di> > )
336 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point3di> > )
337 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point4di> > )
338 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point2dl> > )
339 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point3dl> > )
340 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point4dl> > )
341 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point2dus> > )
342 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point3dus> > )
343 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point4dus> > )
344 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point2dui> > )
345 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point3dui> > )
346 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point4dui> > )
347 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point2dul> > )
348 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point3dul> > )
349 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point4dul> > )
350 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point2df> > )
351 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point3df> > )
352 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point4df> > )
353 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point2dd> > )
354 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point3dd> > )
355 DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point4dd> > )
356 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point2ds> > )
357 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point3ds> > )
358 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point4ds> > )
359 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point2di> > )
360 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point3di> > )
361 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point4di> > )
362 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point2dl> > )
363 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point3dl> > )
364 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point4dl> > )
365 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point2dus> > )
366 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point3dus> > )
367 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point4dus> > )
368 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point2dui> > )
369 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point3dui> > )
370 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point4dui> > )
371 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point2dul> > )
372 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point3dul> > )
373 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point4dul> > )
374 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point2df> > )
375 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point3df> > )
376 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point4df> > )
377 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point2dd> > )
378 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point3dd> > )
379 DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point4dd> > )
380 
381  // partial specializations
382 
383  template<class T, int D> class DataTypeCode<AimsVector<T,D> >
384  {
385  public:
386  static std::string objectType()
387  { return( "Vector" ); }
388  static std::string dataType()
389  {
390  char num[10];
391  sprintf( num, "%d", (int) D );
392  return( std::string( "VECTOR_OF_" ) + num + "_" +
394  }
395  static std::string name()
396  {
397  return DataTypeCode<AimsVector<T,D> >::dataType();
398  }
399  };
400 
401  template<> inline std::string DataTypeCode<Point2df>::dataType()
402  {
403  return "POINT2DF";
404  }
405 
406  template<> inline std::string DataTypeCode<Point3df>::dataType()
407  {
408  return "POINT3DF";
409  }
410 
411 
412 template<typename T, int D>
414 {
415  public :
416  typedef T ChannelType;
418 
419  static const bool is_scalar = false;
420  static const bool is_multichannel = true;
421  static const bool has_bool_conversion = false;
422  static const unsigned int channelcount = D;
423 };
424 
425 }
426 
427 #endif // DOXYGEN_HIDE_INTERNAL_CLASSES
428 
429 
430 //
431 // Members of class AimsVector<T,D>
432 //
433 template <class T,int D>
434 inline
436 {
437 }
438 
439 
440 template <class T,int D>
441 inline
443 {
444  for (int d = 0; d < D; d++)
445  _value[d] = value;
446 }
447 
448 
449 template <class T,int D>
450 inline
452 {
453  for (int d = 0; d < D; d++)
454  _value[d] = value[d];
455 }
456 
457 template <class T, int D>
458 inline
459 AimsVector<T,D>::AimsVector( const std::vector<T> & value )
460 {
461  int d, N = std::min( typename std::vector<T>::size_type( D ), value.size() );
462  for( d = 0; d < N; ++d )
463  _value[d] = value[d];
464  for( ; d<D; ++d )
465  _value[d] = T(0);
466 }
467 
468 #ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
469 namespace internal
470 {
471 
472  template <typename T>
474  {
475  public:
476  static T null()
477  {
478  return T();
479  }
480  };
481 
482  template <> inline int8_t null_value<int8_t>::null() { return 0; }
483  template <> inline uint8_t null_value<uint8_t>::null() { return 0; }
484  template <> inline int16_t null_value<int16_t>::null() { return 0; }
485  template <> inline uint16_t null_value<uint16_t>::null() { return 0; }
486  template <> inline int32_t null_value<int32_t>::null() { return 0; }
487  template <> inline uint32_t null_value<uint32_t>::null() { return 0; }
488  template <> inline int64_t null_value<int64_t>::null() { return 0; }
489  template <> inline uint64_t null_value<uint64_t>::null() { return 0; }
490  template <> inline float null_value<float>::null() { return 0; }
491  template <> inline double null_value<double>::null() { return 0; }
492 
493  template <typename T>
494  class null_value<T*>
495  {
496  public:
497  static T* null()
498  {
499  return 0;
500  }
501  };
502 
503 
504  template <typename T, int D>
506  {
507  public:
508  inline static void doit( T _value[D], const T& x, const T& y )
509  {
510  _value[0] = x;
511  _value[1] = y;
512  for (int d = 2; d < D; d++)
513  _value[d] = null_value<T>::null();
514  }
515  };
516 
517  template <typename T, int D>
519  {
520  public:
521  inline static void doit( T _value[D], const T& x, const T& y, const T& z )
522  {
523  _value[0] = x;
524  _value[1] = y;
525  _value[2] = z;
526  for (int d = 3; d < D; d++)
527  _value[d] = null_value<T>::null();
528  }
529  };
530 
531  template <typename T, int D>
533  {
534  public:
535  inline static void doit( T _value[D], const T& x, const T& y, const T& z,
536  const T& t )
537  {
538  _value[0] = x;
539  _value[1] = y;
540  _value[2] = z;
541  _value[3] = t;
542  for (int d = 4; d < D; d++)
543  _value[d] = null_value<T>::null();
544  }
545  };
546 
547  template <typename T>
548  class fill_aimsvector2<T, 1>
549  {
550  public:
551  inline static void doit( T _value[1], const T& x, const T& )
552  {
553  _value[0] = x;
554  }
555  };
556 
557  template <typename T>
558  class fill_aimsvector3<T, 1>
559  {
560  public:
561  inline static void doit( T _value[1], const T& x, const T&, const T& )
562  {
563  _value[0] = x;
564  }
565  };
566 
567  template <typename T>
568  class fill_aimsvector4<T, 1>
569  {
570  public:
571  inline static void doit( T _value[1], const T& x, const T&, const T&,
572  const T& )
573  {
574  _value[0] = x;
575  }
576  };
577 
578  template <typename T>
579  class fill_aimsvector3<T, 2>
580  {
581  public:
582  inline static void doit( T _value[2], const T& x, const T& y, const T& )
583  {
584  _value[0] = x;
585  _value[1] = y;
586  }
587  };
588 
589  template <typename T>
590  class fill_aimsvector4<T, 2>
591  {
592  public:
593  inline static void doit( T _value[2], const T& x, const T& y, const T&,
594  const T& )
595  {
596  _value[0] = x;
597  _value[1] = y;
598  }
599  };
600 
601  template <typename T>
602  class fill_aimsvector4<T, 3>
603  {
604  public:
605  inline static void doit( T _value[3], const T& x, const T& y, const T& z,
606  const T& )
607  {
608  _value[0] = x;
609  _value[1] = y;
610  _value[2] = z;
611  }
612  };
613 
614 }
615 #endif
616 
617 template <class T,int D>
618 inline
619 AimsVector<T,D>::AimsVector(const T& x, const T& y)
620 {
622 }
623 
624 
625 template <class T,int D>
626 inline
627 AimsVector<T,D>::AimsVector(const T& x, const T& y, const T& z)
628 {
629  ::internal::fill_aimsvector3<T,D>::doit( _value, x, y, z );
630 }
631 
632 
633 template <class T,int D>
634 inline
635 AimsVector<T,D>::AimsVector(const T& x, const T& y, const T& z, const T& t)
636 {
637  ::internal::fill_aimsvector4<T,D>::doit( _value, x, y, z, t );
638 }
639 
640 
641 template <class T,int D>
642 inline
644 {
645  for (int d = 0; d < D; d++)
646  _value[d] = other._value[d];
647 }
648 
649 template <class T,int D> template<class U>
650 inline
652 {
653  for (int d = 0; d < D; d++)
654  _value[d] = other[d];
655 }
656 
657 
658 template <class T,int D>
659 inline
661 {
662  if (this == &other)
663  return *this;
664  for (int d = 0; d < D; d++)
665  _value[d] = other._value[d];
666  return *this;
667 }
668 
669 
670 template <class T,int D>
671 inline
673 {
674 }
675 
676 
677 template <class T,int D>
678 inline
680 {
681  for (int d = 0;d < D; d++)
682  _value[d] += other._value[d];
683  return *this;
684 }
685 
686 
687 template <class T,int D>
688 inline
690 {
691  for (int d = 0; d < D; d++)
692  _value[d] -= other._value[d];
693  return *this;
694 }
695 
696 
697 template <class T,int D>
698 inline
700 {
701  for (int d = 0; d < D; d++)
702  _value[d] = (T) ( _value[d] * val );
703  return *this;
704 }
705 
706 
707 template <class T,int D>
708 inline
710 {
711  for (int d = 0; d < D; d++)
712  _value[d] = (T) ( _value[d] / val );
713  return *this;
714 }
715 
716 
717 template <class T,int D>
718 inline
720 {
721  float n = norm();
722  if( n != 0.f )
723  return operator /= ( n );
724  else
725  return *this;
726 }
727 
728 
729 template <class T,int D>
730 inline
731 const T& AimsVector<T,D>::item(int d) const
732 {
733  ASSERT(d < D);
734  return _value[d];
735 }
736 
737 
738 template <class T,int D>
739 inline
740 const T& AimsVector<T,D>::operator[](int d) const
741 {
742  ASSERT(d < D);
743  return _value[d];
744 }
745 
746 
747 template <class T,int D>
748 inline
750 {
751  ASSERT(d < D);
752  return _value[d];
753 }
754 
755 
756 template <class T,int D>
757 inline
759 {
760  ASSERT(d < D);
761  return _value[d];
762 }
763 
764 template <class T,int D>
765 inline
767 {
768  return D;
769 }
770 
771 template <class T,int D>
772 inline
774 {
775  for (int d = 0; d < D; ++d) {
776  if ( _value[ d ] ) return false;
777  }
778  return true;
779 }
780 
781 template <class T,int D>
782 inline
784 {
785  float result = 0;
786  for (int d = 0; d < D; ++d)
787  result += float(item(d)) * float(item(d));
788  return sqrt(result);
789 }
790 
791 template <class T,int D>
792 inline
794 {
795  float result = 0;
796  for (int d = 0; d < D; ++d)
797  result += float(item(d)) * float(item(d));
798  return result;
799 }
800 
801 template <class T,int D>
802 inline
804 {
805  double result = 0;
806  for (int d = 0; d < D; ++d)
807  result += double(item(d)) * double(item(d));
808  return sqrt(result);
809 }
810 
811 template <class T,int D>
812 inline
814 {
815  double result = 0;
816  for (int d = 0; d < D; ++d)
817  result += double(item(d)) * double(item(d));
818  return result;
819 }
820 
821 
822 template <class T,int D>
823 inline
825 {
826  T result = (T)0;
827  for (int d = 0; d < D; ++d)
828  result += item(d) * other.item(d);
829  return result;
830 }
831 
832 
833 template <class T,int D>
834 inline
835 std::vector<T> AimsVector<T, D>::toStdVector() const
836 {
837  std::vector<T> vec( D );
838  int i;
839  for( i=0; i<D; ++i )
840  vec[i] = item(i);
841  return vec;
842 }
843 
844 
845 //
846 // Friend functions
847 //
848 
849 template <class T,int D>
850 inline
852  const AimsVector<T,D>& v2)
853 {
854  for (int d = 0; d < D; ++d)
855  if (v1.item(d) != v2.item(d))
856  return false;
857  return true;
858 }
859 
860 
861 template <class T,int D>
862 inline
864  const T& value)
865 {
866  for (int d = 0; d < D; ++d)
867  if (v1[d] != value)
868  return false;
869  return true;
870 }
871 
872 
873 template <class T,int D>
874 inline
876  const AimsVector<T,D> &v2)
877 {
878  AimsVector<T,D> result;
879  for (int d = 0; d < D; ++d)
880  result[d] = v1[d] + v2[d];
881  return result;
882 }
883 
884 
885 template <class T,int D>
886 inline
888  const AimsVector<T,D>& v2)
889 {
890  AimsVector<T,D> result;
891  for (int d = 0; d < D; ++d)
892  result[d] = v1[d] - v2[d];
893  return result;
894 }
895 
896 
897 template <class T,int D>
898 inline
900  double value )
901 {
902  AimsVector<T,D> result;
903  for (int d = 0; d < D; ++d)
904  result[d] = (T) ( v1[d] * value );
905  return result;
906 }
907 
908 
909 template <class T,int D>
910 inline
912  const AimsVector<T,D>& v1 )
913 {
914  AimsVector<T,D> result;
915  for (int d = 0; d < D; ++d)
916  result[d] = (T) ( v1[d] * value );
917  return result;
918 }
919 
920 
921 template <class T,int D>
922 inline
924  double value )
925 {
926  AimsVector<T,D> result;
927  for (int d = 0; d < D; ++d)
928  result[d] = (T) ( v1[d] / value );
929  return result;
930 }
931 
932 
933 template <class T,int D>
934 inline
936 {
937  return v1;
938 }
939 
940 
941 template <class T,int D>
942 inline
944 {
945  AimsVector<T,D> result;
946  for (int d = 0; d < D; ++d)
947  result[d] = -v1[d];
948  return result;
949 }
950 
951 
952 template <class T,int D>
953 inline
955  const AimsVector<T,D>& v2)
956 {
957  AimsVector<T,3> result;
958  result[0] = v1[1] * v2[2] - v1[2] * v2[1];
959  result[1] = v1[2] * v2[0] - v1[0] * v2[2];
960  result[2] = v1[0] * v2[1] - v1[1] * v2[0];
961  return result;
962 }
963 
964 
965 template <class T,int D>
966 inline
967 float norm(const AimsVector<T,D>& v1)
968 {
969  return v1.norm();
970 }
971 
972 
973 template <class T,int D>
974 inline
975 float norm2(const AimsVector<T,D>& v1)
976 {
977  return v1.norm2();
978 }
979 
980 template <class T,int D>
981 inline
982 double dnorm(const AimsVector<T,D>& v1)
983 {
984  return v1.dnorm();
985 }
986 
987 
988 template <class T,int D>
989 inline
990 double dnorm2(const AimsVector<T,D>& v1)
991 {
992  return v1.dnorm2();
993 }
994 
995 
996 template <class T,int D>
997 inline
998 std::ostream& operator << ( std::ostream& out,const AimsVector<T,D>& thing)
999 {
1000  out << '(';
1001  for (int d = 0; d < D-1 ; ++d)
1002  out << thing.item(d) << ", ";
1003  out << thing.item(D-1) << ')';
1004  return(out);
1005 }
1006 
1007 
1008 template <int D>
1009 inline
1010 std::ostream& operator << ( std::ostream& out,const AimsVector<char,D>& thing)
1011 {
1012  out << '(';
1013  for (int d = 0; d < D-1 ; ++d)
1014  out << (int)thing.item(d) << ", ";
1015  out << (int)thing.item(D-1) << ')';
1016  return(out);
1017 }
1018 
1019 
1020 template <int D>
1021 inline
1022 std::ostream& operator << ( std::ostream& out,const AimsVector<::byte,D>& thing)
1023 {
1024  out << '(';
1025  for (int d = 0; d < D-1 ; ++d)
1026  out << (int)thing.item(d) << ", ";
1027  out << (int)thing.item(D-1) << ')';
1028  return(out);
1029 }
1030 
1031 
1032 template <class T,int D>
1033 inline
1034 std::istream& operator >> (std::istream& is, AimsVector<T,D>& thing)
1035 {
1036  char ch = 0;
1037 
1038  //std::istream::sentry s( is ); // to use in the future...
1039  carto::StreamUtil::skip( is );
1040  if( !is )
1041  return is;
1042  if (is.peek() == '(')
1043  {
1044  is >> ch;
1045  carto::StreamUtil::skip( is );
1046  is >> thing.item(0);
1047  for (int i = 1; i < D; ++i)
1048  {
1049  carto::StreamUtil::skip( is );
1050  is >> ch;
1051  if (ch != ',')
1052  break;
1053  carto::StreamUtil::skip( is );
1054  is >> thing.item(i);
1055  }
1056  if( is )
1057  {
1058  carto::StreamUtil::skip( is );
1059  is >> ch;
1060  }
1061  }
1062 
1063  if( ch != ')' )
1064  std::cout << "vector read ends not with )\n";
1065  //is.setstate (std::ios::failbit);
1066 
1067  return is;
1068 }
1069 
1070 
1071 template <int D>
1072 inline
1073 std::istream& operator >> (std::istream& is, AimsVector<char,D>& thing)
1074 {
1075  char ch = 0;
1076  int tmp;
1077 
1078  //std::istream::sentry s( is ); // to use in the future...
1079  carto::StreamUtil::skip( is );
1080  if( !is )
1081  return is;
1082  if (is.peek() == '(')
1083  {
1084  is >> ch;
1085  carto::StreamUtil::skip( is );
1086  is >> tmp;
1087  thing.item(0) = (char)tmp;
1088  for (int i = 1; i < D; ++i)
1089  {
1090  carto::StreamUtil::skip( is );
1091  is >> ch;
1092  if (ch != ',')
1093  break;
1094  carto::StreamUtil::skip( is );
1095  is >> tmp;
1096  thing.item(i) = (char)tmp;
1097  }
1098  if( is )
1099  {
1100  carto::StreamUtil::skip( is );
1101  is >> ch;
1102  }
1103  }
1104 
1105  if( ch != ')' )
1106  is.setstate (std::ios::failbit);
1107 
1108  return is;
1109 }
1110 
1111 
1112 template <int D>
1113 inline
1114 std::istream& operator >> (std::istream& is, AimsVector<::byte,D>& thing)
1115 {
1116  char ch = 0;
1117  int tmp;
1118 
1119  //std::istream::sentry s( is ); // to use in the future...
1120  carto::StreamUtil::skip( is );
1121  if( !is )
1122  return is;
1123  if (is.peek() == '(')
1124  {
1125  is >> ch;
1126  carto::StreamUtil::skip( is );
1127  is >> tmp;
1128  thing.item(0) = (::byte)tmp;
1129  for (int i = 1; i < D; ++i)
1130  {
1131  carto::StreamUtil::skip( is );
1132  is >> ch;
1133  if (ch != ',')
1134  break;
1135  carto::StreamUtil::skip( is );
1136  is >> tmp;
1137  thing.item(i) = (::byte)tmp;
1138  }
1139  if( is )
1140  {
1141  carto::StreamUtil::skip( is );
1142  is >> ch;
1143  }
1144  }
1145 
1146  if( ch != ')' )
1147  is.setstate (std::ios::failbit);
1148 
1149  return is;
1150 }
1151 
1152 
1153 
1154 template <typename T>
1155 inline
1157  const AimsVector<T,3> & v2 )
1158 {
1159  return AimsVector<T,3>( v1[1] * v2[2] - v1[2] * v2[1],
1160  v1[2] * v2[0] - v1[0] * v2[2],
1161  v1[0] * v2[1] - v1[1] * v2[0] );
1162 }
1163 
1164 
1165 //=== AsciiDataSourceTraits specializations ===================================
1166 
1167 namespace soma
1168 {
1169  //=== AIMSVECTOR<T> =========================================================
1170 
1171  template<typename T, int D>
1173  {
1174  public:
1175  static bool read( DataSource & ds, AimsVector<T, D> & item );
1176  static bool write( DataSource & ds, const AimsVector<T, D> & item );
1177  };
1178 
1179 
1180  // -----------
1181 
1182 #ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
1183 
1184  template<typename T, int D>
1185  inline
1187  AimsVector<T, D> & x )
1188  {
1189  char c;
1190 
1191  if( !StreamUtil::skip( ds ) )
1192  return false;
1193  c = ds.getch();
1194  if( c != '(' )
1195  return false;
1196 
1197  for( int i=0; i<D; ++i )
1198  {
1199  if( !StreamUtil::skip( ds ) )
1200  return false;
1201  if( !AsciiDataSourceTraits<T>::read( ds, x[i] ) )
1202  return false;
1203  if( i < D-1 )
1204  {
1205  if( !StreamUtil::skip( ds ) )
1206  return false;
1207  c = ds.getch();
1208  if( c != ',' )
1209  return false;
1210  }
1211  }
1212 
1213  if( !StreamUtil::skip( ds ) )
1214  return false;
1215  c = ds.getch();
1216  if( c != ')' )
1217  return false;
1218 
1219  return true;
1220  }
1221 
1222 
1223  template<typename T, int D>
1224  inline
1226  DataSource & ds, const AimsVector<T, D> & x )
1227  {
1228  ds.putch( '(');
1229 
1230 
1231  for( int i=0; i<D; ++i )
1232  {
1233  AsciiDataSourceTraits<T>::write( ds, x[i] );
1234  if( i < D-1 )
1235  {
1236  ds.putch( ',' );
1237  ds.putch( ' ' );
1238  }
1239  }
1240 
1241  ds.putch( ')');
1242  return ds.isOpen();
1243  }
1244 
1245 
1246  template <typename T, int D>
1248  const AimsVector<T, D> & x )
1249  {
1250  AsciiDataSourceTraits<AimsVector<T, D> >::write( ds, x );
1251  return ds;
1252  }
1253 
1254 #endif
1255 
1256 }
1257 
1258 #endif
#define ASSERT(EX)
The template class to implement basic vectors.
Definition: vector.h:137
const T * const_iterator
Definition: vector.h:201
std::vector< T > toStdVector() const
Definition: vector.h:835
~AimsVector()
The destructor deletes the allocated memory space.
Definition: vector.h:672
AimsVector< T, D > & operator/=(double val)
Definition: vector.h:709
int size() const
Definition: vector.h:766
const T & item(int d) const
Get a const reference to the dth item.
Definition: vector.h:731
bool isNull() const
Test if the vector is the null vector.
Definition: vector.h:773
AimsVector< T, D > & normalize()
Definition: vector.h:719
AimsVector< T, D > & operator+=(const AimsVector< T, D > &other)
Definition: vector.h:679
AimsVector< T, D > & operator=(const AimsVector< T, D > &other)
Definition: vector.h:660
T _value[D]
Memory space allocated.
Definition: vector.h:214
T value_type
Definition: vector.h:208
AimsVector< T, D > & operator-=(const AimsVector< T, D > &other)
Definition: vector.h:689
T dot(const AimsVector< T, D > &other) const
Get the dot product between 2 vectors.
Definition: vector.h:824
float norm() const
Return the magnitude of the vector.
Definition: vector.h:783
const T & operator[](int d) const
Get a const reference to the dth item.
Definition: vector.h:740
double dnorm2() const
Return the square magnitude of the vector.
Definition: vector.h:813
iterator end()
Definition: vector.h:204
T * iterator
Definition: vector.h:200
const_iterator end() const
Definition: vector.h:205
double dnorm() const
Return the magnitude of the vector.
Definition: vector.h:803
iterator begin()
Definition: vector.h:202
AimsVector()
The constructor allocates an appropriate amount of memory.
Definition: vector.h:435
AimsVector< T, D > & operator*=(double val)
Definition: vector.h:699
float norm2() const
Return the square magnitude of the vector.
Definition: vector.h:793
const_iterator begin() const
Definition: vector.h:203
static void doit(T _value[1], const T &x, const T &)
Definition: vector.h:551
static void doit(T _value[D], const T &x, const T &y)
Definition: vector.h:508
static void doit(T _value[1], const T &x, const T &, const T &)
Definition: vector.h:561
static void doit(T _value[2], const T &x, const T &y, const T &)
Definition: vector.h:582
static void doit(T _value[D], const T &x, const T &y, const T &z)
Definition: vector.h:521
static void doit(T _value[1], const T &x, const T &, const T &, const T &)
Definition: vector.h:571
static void doit(T _value[2], const T &x, const T &y, const T &, const T &)
Definition: vector.h:593
static void doit(T _value[3], const T &x, const T &y, const T &z, const T &)
Definition: vector.h:605
static void doit(T _value[D], const T &x, const T &y, const T &z, const T &t)
Definition: vector.h:535
static T null()
Definition: vector.h:476
static bool read(DataSource &ds, T &item)
static bool write(DataSource &ds, const T &item)
Abstraction layer for various data sources (file, buffer, socket...).
Definition: datasource.h:65
virtual int getch()=0
virtual int putch(int ch)=0
virtual bool isOpen() const =0
static bool skip(DataSource &, const std::string &chars=" \t\n\r", bool ascii=true)
Definition: allocator.h:49
std::ostream & operator<<(std::ostream &os, const MemoryAllocator &thing)
#define DECLARE_GENERIC_OBJECT_TYPE(T)
uint8_t byte
AimsVector< float, 3 > Point3df
Definition: vector.h:247
AimsVector< int16_t, 3 > Point3d
Definition: vector.h:222
AimsVector< T, D > operator+(const AimsVector< T, D > &v1, const AimsVector< T, D > &v2)
Definition: vector.h:875
AimsVector< uint32_t, 2 > Point2du
Definition: vector.h:224
std::ostream & operator<<(std::ostream &out, const AimsVector< T, D > &thing)
Definition: vector.h:998
AimsVector< uint32_t, 3 > Point3dui
Definition: vector.h:241
AimsVector< uint32_t, 2 > Point2dui
Definition: vector.h:240
AimsVector< int32_t, 3 > Point3di
Definition: vector.h:232
AimsVector< T, D > operator-(const AimsVector< T, D > &v1, const AimsVector< T, D > &v2)
Definition: vector.h:887
AimsVector< double, 2 > Point2dd
Definition: vector.h:249
AimsVector< uint16_t, 4 > Point4dus
Definition: vector.h:239
AimsVector< int64_t, 4 > Point4dl
Definition: vector.h:236
double dnorm(const AimsVector< T, D > &v1)
Definition: vector.h:982
AimsVector< T, 3 > vectProduct(const AimsVector< T, 3 > &v1, const AimsVector< T, 3 > &v2)
Definition: vector.h:1156
AimsVector< int32_t, 2 > Point2di
Definition: vector.h:231
AimsVector< int16_t, 2 > Point2ds
Definition: vector.h:228
AimsVector< uint64_t, 4 > Point4dul
Definition: vector.h:245
bool operator==(const AimsVector< T, D > &v1, const AimsVector< T, D > &v2)
Definition: vector.h:851
AimsVector< int64_t, 3 > Point3dl
Definition: vector.h:235
AimsVector< int16_t, 4 > Point4ds
Definition: vector.h:230
AimsVector< uint32_t, 4 > Point4dui
Definition: vector.h:242
AimsVector< double, 3 > Point3dd
Definition: vector.h:250
AimsVector< T, 3 > crossed(const AimsVector< T, D > &v1, const AimsVector< T, D > &v2)
Definition: vector.h:954
AimsVector< uint32_t, 3 > Point3du
Definition: vector.h:225
std::istream & operator>>(std::istream &in, AimsVector< T, D > &thing)
Definition: vector.h:1034
AimsVector< T, D > operator/(const AimsVector< T, D > &v1, double value)
Definition: vector.h:923
AimsVector< uint16_t, 2 > Point2dus
Definition: vector.h:237
double dnorm2(const AimsVector< T, D > &v1)
Definition: vector.h:990
AimsVector< float, 4 > Point4df
Definition: vector.h:248
AimsVector< uint32_t, 4 > Point4du
Definition: vector.h:226
AimsVector< uint64_t, 2 > Point2dul
Definition: vector.h:243
float norm2(const AimsVector< T, D > &v1)
Definition: vector.h:975
float norm(const AimsVector< T, D > &v1)
Definition: vector.h:967
AimsVector< uint16_t, 3 > Point3dus
Definition: vector.h:238
AimsVector< float, 2 > Point2df
Definition: vector.h:246
AimsVector< T, D > operator*(const AimsVector< T, D > &v1, double value)
Definition: vector.h:899
bool operator!=(const AimsVector< T, D > &v1, const AimsVector< T, D > &v2)
should be defined implicitly, but fails with some compilers (Intel)
Definition: vector.h:66
AimsVector< int16_t, 2 > Point2d
Definition: vector.h:221
AimsVector< uint64_t, 3 > Point3dul
Definition: vector.h:244
AimsVector< int64_t, 2 > Point2dl
Definition: vector.h:234
AimsVector< int32_t, 4 > Point4di
Definition: vector.h:233
AimsVector< double, 4 > Point4dd
Definition: vector.h:251
AimsVector< int16_t, 3 > Point3ds
Definition: vector.h:229
AimsVector< int16_t, 4 > Point4d
Definition: vector.h:223