soma-io 6.0.6
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
44#include <cartobase/exception/assert.h>
45#include <cartobase/type/types.h>
46#include <cartobase/type/datatypetraits.h>
47#include <cartobase/object/object.h>
49#include <fstream>
50#include <math.h>
51#include <vector>
52
53template <class T,int D> class AimsVector;
54
55
56template <class T,int D>
57bool operator == (const AimsVector<T,D>& v1,
58 const AimsVector<T,D>& v2);
59
60template <class T,int D>
61bool operator == (const AimsVector<T,D>& v1,
62 const T& val);
63
65template <class T,int D>
67 const AimsVector<T,D>& v2)
68{
69 return !( v1 == v2 );
70}
71
73template <class T,int D>
75 const T& val)
76{
77 return !( v1 == val );
78}
79
80template <class T,int D>
82 const AimsVector<T,D>& v2);
83
84template <class T,int D>
86 const AimsVector<T,D>& v2);
87
88template <class T,int D>
90 double value );
91
92template <class T,int D>
93AimsVector<T,D> operator * (double value,
94 const AimsVector<T,D>& v1);
95
96template <class T,int D>
98 double value);
99
100template <class T,int D>
102
103template <class T,int D>
105
106template <class T,int D>
108 const AimsVector<T,D>& v2);
109
110template <class T,int D>
111float norm(const AimsVector<T,D>& v1);
112
113template <class T,int D>
114float norm2(const AimsVector<T,D>& v1);
115
116template <class T,int D>
117double dnorm(const AimsVector<T,D>& v1);
118
119template <class T,int D>
120double dnorm2(const AimsVector<T,D>& v1);
121
122template <class T,int D>
123std::ostream& operator << (std::ostream& out,
124 const AimsVector<T,D>& thing);
125
126template <class T,int D>
127std::istream& operator >> (std::istream& in,
128 AimsVector<T,D>& thing);
129
130
135template <class T,int D>
137{
138 public:
141
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);
157 template<class U>
158 explicit AimsVector(const AimsVector<U,D>& other);
159
163
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
255extern template class AimsVector<int16_t,2>;
256extern template class AimsVector<int16_t,3>;
257extern template class AimsVector<int16_t,4>;
258extern template class AimsVector<int32_t,2>;
259extern template class AimsVector<int32_t,3>;
260extern template class AimsVector<int32_t,4>;
261extern template class AimsVector<int64_t,2>;
262extern template class AimsVector<int64_t,3>;
263extern template class AimsVector<int64_t,4>;
264extern template class AimsVector<uint16_t,2>;
265extern template class AimsVector<uint16_t,3>;
266extern template class AimsVector<uint16_t,4>;
267extern template class AimsVector<uint32_t,2>;
268extern template class AimsVector<uint32_t,3>;
269extern template class AimsVector<uint32_t,4>;
270extern template class AimsVector<uint64_t,2>;
271extern template class AimsVector<uint64_t,3>;
272extern template class AimsVector<uint64_t,4>;
273extern template class AimsVector<float,2>;
274extern template class AimsVector<float,3>;
275extern template class AimsVector<float,4>;
276extern template class AimsVector<double,2>;
277extern template class AimsVector<double,3>;
278extern template class AimsVector<double,4>;
279
280#ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
281
282namespace carto
283{
308DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point2ds> )
309DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point3ds> )
310DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point4ds> )
311DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point2di> )
312DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point3di> )
313DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point4di> )
314DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point2dl> )
315DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point3dl> )
316DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point4dl> )
317DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point2dus> )
318DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point3dus> )
319DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point4dus> )
320DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point2dui> )
321DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point3dui> )
322DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point4dui> )
323DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point2dul> )
324DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point3dul> )
325DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point4dul> )
326DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point2df> )
327DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point3df> )
328DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point4df> )
329DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point2dd> )
330DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point3dd> )
331DECLARE_GENERIC_OBJECT_TYPE( std::vector<Point4dd> )
332DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point2ds> > )
333DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point3ds> > )
334DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point4ds> > )
335DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point2di> > )
336DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point3di> > )
337DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point4di> > )
338DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point2dl> > )
339DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point3dl> > )
340DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point4dl> > )
341DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point2dus> > )
342DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point3dus> > )
343DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point4dus> > )
344DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point2dui> > )
345DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point3dui> > )
346DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point4dui> > )
347DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point2dul> > )
348DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point3dul> > )
349DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point4dul> > )
350DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point2df> > )
351DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point3df> > )
352DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point4df> > )
353DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point2dd> > )
354DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point3dd> > )
355DECLARE_GENERIC_OBJECT_TYPE( std::vector<std::vector<Point4dd> > )
356DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point2ds> > )
357DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point3ds> > )
358DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point4ds> > )
359DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point2di> > )
360DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point3di> > )
361DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point4di> > )
362DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point2dl> > )
363DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point3dl> > )
364DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point4dl> > )
365DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point2dus> > )
366DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point3dus> > )
367DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point4dus> > )
368DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point2dui> > )
369DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point3dui> > )
370DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point4dui> > )
371DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point2dul> > )
372DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point3dul> > )
373DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point4dul> > )
374DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point2df> > )
375DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point3df> > )
376DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point4df> > )
377DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point2dd> > )
378DECLARE_GENERIC_OBJECT_TYPE( std::list<std::vector<Point3dd> > )
379DECLARE_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 {
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
412template<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//
433template <class T,int D>
434inline
438
439
440template <class T,int D>
441inline
443{
444 for (int d = 0; d < D; d++)
445 _value[d] = value;
446}
447
448
449template <class T,int D>
450inline
452{
453 for (int d = 0; d < D; d++)
454 _value[d] = value[d];
455}
456
457template <class T, int D>
458inline
459AimsVector<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
469namespace 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>
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>
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>
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>
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>
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>
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
617template <class T,int D>
618inline
619AimsVector<T,D>::AimsVector(const T& x, const T& y)
620{
622}
623
624
625template <class T,int D>
626inline
627AimsVector<T,D>::AimsVector(const T& x, const T& y, const T& z)
628{
630}
631
632
633template <class T,int D>
634inline
635AimsVector<T,D>::AimsVector(const T& x, const T& y, const T& z, const T& t)
636{
638}
639
640
641template <class T,int D>
642inline
644{
645 for (int d = 0; d < D; d++)
646 _value[d] = other._value[d];
647}
648
649template <class T,int D> template<class U>
650inline
652{
653 for (int d = 0; d < D; d++)
654 _value[d] = other[d];
655}
656
657
658template <class T,int D>
659inline
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
670template <class T,int D>
671inline
675
676
677template <class T,int D>
678inline
680{
681 for (int d = 0;d < D; d++)
682 _value[d] += other._value[d];
683 return *this;
684}
685
686
687template <class T,int D>
688inline
690{
691 for (int d = 0; d < D; d++)
692 _value[d] -= other._value[d];
693 return *this;
694}
695
696
697template <class T,int D>
698inline
700{
701 for (int d = 0; d < D; d++)
702 _value[d] = (T) ( _value[d] * val );
703 return *this;
704}
705
706
707template <class T,int D>
708inline
710{
711 for (int d = 0; d < D; d++)
712 _value[d] = (T) ( _value[d] / val );
713 return *this;
714}
715
716
717template <class T,int D>
718inline
720{
721 float n = norm();
722 if( n != 0.f )
723 return operator /= ( n );
724 else
725 return *this;
726}
727
728
729template <class T,int D>
730inline
731const T& AimsVector<T,D>::item(int d) const
732{
733 ASSERT(d < D);
734 return _value[d];
735}
736
737
738template <class T,int D>
739inline
740const T& AimsVector<T,D>::operator[](int d) const
741{
742 ASSERT(d < D);
743 return _value[d];
744}
745
746
747template <class T,int D>
748inline
750{
751 ASSERT(d < D);
752 return _value[d];
753}
754
755
756template <class T,int D>
757inline
759{
760 ASSERT(d < D);
761 return _value[d];
762}
763
764template <class T,int D>
765inline
767{
768 return D;
769}
770
771template <class T,int D>
772inline
774{
775 for (int d = 0; d < D; ++d) {
776 if ( _value[ d ] ) return false;
777 }
778 return true;
779}
780
781template <class T,int D>
782inline
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
791template <class T,int D>
792inline
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
801template <class T,int D>
802inline
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
811template <class T,int D>
812inline
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
822template <class T,int D>
823inline
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
833template <class T,int D>
834inline
835std::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
849template <class T,int D>
850inline
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
861template <class T,int D>
862inline
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
873template <class T,int D>
874inline
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
885template <class T,int D>
886inline
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
897template <class T,int D>
898inline
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
909template <class T,int D>
910inline
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
921template <class T,int D>
922inline
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
933template <class T,int D>
934inline
936{
937 return v1;
938}
939
940
941template <class T,int D>
942inline
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
952template <class T,int D>
953inline
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
965template <class T,int D>
966inline
967float norm(const AimsVector<T,D>& v1)
968{
969 return v1.norm();
970}
971
972
973template <class T,int D>
974inline
975float norm2(const AimsVector<T,D>& v1)
976{
977 return v1.norm2();
978}
979
980template <class T,int D>
981inline
982double dnorm(const AimsVector<T,D>& v1)
983{
984 return v1.dnorm();
985}
986
987
988template <class T,int D>
989inline
990double dnorm2(const AimsVector<T,D>& v1)
991{
992 return v1.dnorm2();
993}
994
995
996template <class T,int D>
997inline
998std::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
1008template <int D>
1009inline
1010std::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
1020template <int D>
1021inline
1022std::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
1032template <class T,int D>
1033inline
1034std::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
1071template <int D>
1072inline
1073std::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
1112template <int D>
1113inline
1114std::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
1154template <typename T>
1155inline
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
1167namespace 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 {
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>
1247 inline DataSource & operator << ( DataSource & ds,
1248 const AimsVector<T, D> & x )
1249 {
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
AimsVector(const T &x, const T &y, const T &z)
Constructor for D=3.
Definition vector.h:627
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(const T &x, const T &y, const T &z, const T &t)
Constructor for D=4.
Definition vector.h:635
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_type
Definition vector.h:208
AimsVector< T, D > & operator-=(const AimsVector< T, D > &other)
Definition vector.h:689
AimsVector(const AimsVector< T, D > &other)
Copy constructor.
Definition vector.h:643
T dot(const AimsVector< T, D > &other) const
Get the dot product between 2 vectors.
Definition vector.h:824
T & operator[](int d)
Get a const reference to the dth item.
Definition vector.h:758
AimsVector(const T value[])
The constructor copy the D item of value[].
Definition vector.h:451
float norm() const
Return the magnitude of the vector.
Definition vector.h:783
AimsVector(const AimsVector< U, D > &other)
Type conversion.
Definition vector.h:651
AimsVector(const T &x, const T &y)
Constructor for D=2.
Definition vector.h:619
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
T & item(int d)
Get a non const reference to the dth item.
Definition vector.h:749
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
AimsVector(const T &value)
The constructor fills the D items of vector with value.
Definition vector.h:442
AimsVector(const std::vector< T > &value)
Definition vector.h:459
std::string dataType()
static const unsigned int channelcount
Definition vector.h:422
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, AimsVector< T, D > &item)
Definition vector.h:1186
static bool write(DataSource &ds, const AimsVector< T, D > &item)
Definition vector.h:1225
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)
#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< T, 3 > vectProduct(const AimsVector< T, 3 > &v1, const AimsVector< T, 3 > &v2)
Definition vector.h:1156
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< 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