]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/include/vector.h
Support #1768 CREATIS Licence insertion
[creaMaracasVisu.git] / lib / maracasVisuLib / include / vector.h
1 /*# ---------------------------------------------------------------------
2 #
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
4 #                        pour la Sant�)
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
8 #
9 #  This software is governed by the CeCILL-B license under French law and
10 #  abiding by the rules of distribution of free software. You can  use,
11 #  modify and/ or redistribute the software under the terms of the CeCILL-B
12 #  license as circulated by CEA, CNRS and INRIA at the following URL
13 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
14 #  or in the file LICENSE.txt.
15 #
16 #  As a counterpart to the access to the source code and  rights to copy,
17 #  modify and redistribute granted by the license, users are provided only
18 #  with a limited warranty  and the software's author,  the holder of the
19 #  economic rights,  and the successive licensors  have only  limited
20 #  liability.
21 #
22 #  The fact that you are presently reading this means that you have had
23 #  knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */
25
26 ////////////////////////////////////////////////////////////////////////////////
27 // vector.h
28 // Creation : 24/02/2000
29 // Author   : Leonardo FLOREZ VALENCIA
30 //               l-florez@uniandes.edu.co
31 //               lflorez@creatis.insa-lyon.fr
32 // Copyright (C) 2000-2002 Leonardo FLOREZ VALENCIA
33 //
34 // This program is free software; you can redistribute it and/or
35 // modify it under the terms of the GNU General Public License
36 // as published by the Free Software Foundation; either version 2
37 // of the License, or (at your option) any later version.
38 //
39 // This program is distributed in the hope that it will be useful,
40 // but WITHOUT ANY WARRANTY; without even the implied warranty of
41 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
42 // GNU General Public License for more details.
43 //
44 // You should have received a copy of the GNU General Public License
45 // along with this program; if not, write to the Free Software
46 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
47 ////////////////////////////////////////////////////////////////////////////////
48
49 #ifndef GTMLIB__MATH__VECTOR__HXX
50 #define GTMLIB__MATH__VECTOR__HXX
51
52 #include "mathdefs.h"
53 #include "vmfuncs.h"
54
55 /// Vector type IDs
56 //@{
57 #define ROW_VECTOR 0x01
58 #define COL_VECTOR 0x02
59 //@}
60
61 namespace gtm
62 {
63     template< class T >
64     class TMatrix;
65
66     /** TVector class.
67      *
68      *  This class defines a C++ template to use mathematical vectors
69      *  in a N space.
70      *  @see TMatrix
71      */
72     template< class T >
73     class TVector
74     {
75     public:
76
77         /** Contructors.
78          *
79          *  @param N Cardinality.
80          *  @param data Initial data.
81          *  @param type Vector type (ROW_VECTOR/COL_VECTOR).
82          *  @param r Right object (vector or matrix).
83          *  @param block ANSI array.
84          *  @param copy Make a copy of given array?
85          */
86         //@{
87         /// Default constructor.
88         TVector( uint32_t N = 3, T data = ( T )0, int type = COL_VECTOR  );
89         /// Copy constructor.
90         TVector( const TVector< T >& r );
91         /// Use this to treat an ANSI array as a TVector.
92         TVector( T* block, uint32_t N, bool copy = true, int type = COL_VECTOR );
93         //@}
94
95         /// Destructor.
96         ~TVector( ) {
97             if( _myMemory ) VectorFreeMemory< T >( _vector );
98         };
99
100         /// Size change
101         void SetN( uint32_t N );
102
103         /** Assignation operators.
104          *
105          *  @param r Right object (vector or matrix).
106          */
107         //@{
108         /// Vector assignation.
109         TVector< T >& operator=( const TVector< T >& r );
110         /// Matrix assignation (defined in class TMatrix).
111         TVector< T >& operator=( TMatrix< T >& r );
112         /// ANSI assignation (r size must be, at least, N).
113         TVector< T >& operator=( T* r ) {
114             VectorAssignMemory< T >( _vector, r, _N );
115             return( *this );
116         };
117         /// Scalar assignation.
118         TVector< T >& operator=( T r ) {
119             VectorAssignScalar< T >( _vector, r, _N );
120             return( *this );
121         };
122         //@}
123
124         /** Comparation operators.
125          *
126          *  @param r Right object.
127          */
128         //@{
129         /// Equality.
130         bool operator==( const TVector< T >& r );
131         /// Inequality.
132         bool operator!=( const TVector< T >& r );
133         //@}
134
135         /// Reference operator.
136         T& operator()( uint32_t i ) {
137             return( _vector[ i ] );
138         };
139         /// ANSI (C/C++ array) reference.
140         T* GetAnsiRef( ) {
141             return( _vector );
142         };
143         /// Vector's cardinality.
144         uint32_t GetN( ) {
145             return( _N );
146         };
147         /// Vector's type (ROW_VECTOR/COL_VECTOR).
148         int GetType( ) {
149             return( _type );
150         };
151         /// Mathematical norm (L2).
152         T GetNorm( ) {
153             return( VectorNorm< T >( _vector, _N ) );
154         };
155         /// Normalizes the vector.
156         void Normalize( ) {
157             VectorNormalize< T >( _vector, _vector, _N );
158         };
159         /// Dot product.
160         T Dot( const TVector< T >& r ) {
161             return( VectorDotProduct< T >( _vector, r._vector, GTM_MIN( _N, r._N ) ) );
162         };
163
164         /** Binary operators.
165          *
166          *  @param r Right object (vector, matrix or scalar).
167          */
168         //@{
169         /// Addition.
170         TVector< T > operator+( const TVector< T >& r );
171         /// Substraction.
172         TVector< T > operator-( const TVector< T >& r );
173         /// Cross product.
174         TVector< T > operator*( const TVector< T >& r );
175         /// Matrix product (defined in class TMatrix).
176         TMatrix< T > operator*( TMatrix< T >& r );
177         /// Scalar product.
178         TVector< T > operator*( T r );
179         //@}
180
181         /** Self-assignation binary operators.
182          *
183          *  @param r Right object (vector, matrix or scalar).
184          */
185         //@{
186         /// Addition.
187         TVector< T >& operator+=( const TVector< T >& r ) {
188             *this = *this + r;
189             return( *this );
190         };
191         /// Substraction.
192         TVector< T >& operator-=( const TVector< T >& r ) {
193             *this = *this - r;
194             return( *this );
195         };
196         /// Cross product.
197         TVector< T >& operator*=( const TVector< T >& r ) {
198             *this = *this * r;
199             return( *this );
200         };
201         /// Scalar product.
202         TVector< T >& operator*=( T r ) {
203             *this = *this * r;
204             return( *this );
205         };
206         //@}
207
208         /// Unary operators.
209         //@{
210         /// Additive inverse.
211         TVector< T > operator-( );
212         /// Normalized vector.
213         TVector< T > operator!( );
214         /// Transposed vector.
215         TVector< T > operator~( );
216         //@}
217
218     private:
219
220         /// Vector's internal state.
221         //@{
222         /// Memory block.
223         T* _vector;
224         /// Cardinality.
225         uint32_t _N;
226         /// Type (ROW_VECTOR/COL_VECTOR).
227         int _type;
228         /// Have I created _vector?
229         bool _myMemory;
230         //@}
231
232     };
233
234 // -----------------------------------------------------------------------------
235     template< class T >
236     TVector< T >::TVector( uint32_t N, T data, int type )
237     {
238         _N        = N;
239         _type     = type;
240         _vector   = VectorAllocateMemory< T >( _N );
241         _myMemory = true;
242         VectorAssignScalar< T >( _vector, data, _N );
243
244     }
245
246 // -----------------------------------------------------------------------------
247     template< class T >
248     TVector< T >::TVector( const TVector< T >& r )
249     {
250         _N        = r._N;
251         _type     = r._type;
252         _myMemory = true;
253         _vector   = VectorCopyMemory< T >( r._vector, _N );
254
255     }
256
257 // -----------------------------------------------------------------------------
258     template< class T >
259     TVector< T >::TVector( T* block, uint32_t N, bool copy, int type )
260     {
261         _N        = N;
262         _type     = type;
263         _myMemory = copy;
264         _vector   = ( copy )? VectorCopyMemory< T >( block, _N ): block;
265
266     }
267
268 // -----------------------------------------------------------------------------
269     template< class T >
270     void TVector< T >::SetN( uint32_t N )
271     {
272         if( _myMemory ) VectorFreeMemory< T >( _vector );
273         _N        = N;
274         _vector   = VectorAllocateMemory< T >( _N );
275         _myMemory = true;
276         VectorAssignScalar< T >( _vector, ( T )0, _N );
277
278     }
279
280 // -----------------------------------------------------------------------------
281     template< class T >
282     TVector< T >& TVector< T >::operator=( const TVector< T >& r )
283     {
284         /*
285          * Only assigns the minimum cardinality (WARNING WITH NON-LOCAL MEMORY).
286          */
287         _type = r._type;
288         VectorAssignMemory< T >( _vector, r._vector, GTM_MIN( _N, r._N ) );
289         return( *this );
290
291     }
292
293 // -----------------------------------------------------------------------------
294     template< class T >
295     bool TVector< T >::operator==( const TVector< T >& r )
296     {
297         uint32_t i;
298         bool ret;
299
300         for(
301             i = 0, ret = ( _N == r._N );
302             i < _N && ret;
303             ret &= ( _vector[ i ] == r._vector[ i ] ), i++
304             );
305         return( ret );
306
307     }
308
309 // -----------------------------------------------------------------------------
310     template< class T >
311     bool TVector< T >::operator!=( const TVector< T >& r )
312     {
313         uint32_t i;
314         bool ret;
315
316         for(
317             i = 0, ret = ( _N != r._N );
318             i < _N && !ret;
319             ret |= ( _vector[ i ] != r._vector[ i ] ), i++
320             );
321         return( ret );
322
323     }
324
325 // -----------------------------------------------------------------------------
326     template< class T >
327     TVector< T > TVector< T >::operator+( const TVector< T >& r )
328     {
329         TVector< T > ret( GTM_MIN( _N, r._N ) );
330   
331         VectorAdd< T >( ret._vector, _vector, r._vector, GTM_MIN( _N, r._N ) );
332         return( ret );
333   
334     }
335
336 // -----------------------------------------------------------------------------
337     template< class T >
338     TVector< T > TVector< T >::operator-( const TVector< T >& r )
339     {
340         TVector< T > ret( GTM_MIN( _N, r._N ) );
341   
342         VectorSubtract< T >( ret._vector, _vector, r._vector, GTM_MIN( _N, r._N ) );
343         return( ret );
344   
345     }
346
347 // -----------------------------------------------------------------------------
348     template< class T >
349     TVector< T > TVector< T >::operator*( const TVector< T >& r )
350     {
351         TVector< T > ret( GTM_MIN( _N, r._N ) );
352   
353         VectorCrossProduct< T >( ret._vector, _vector, r._vector );
354         return( ret );
355   
356     }
357
358 // -----------------------------------------------------------------------------
359     template< class T >
360     TVector< T > TVector< T >::operator*( T r )
361     {
362         TVector< T > ret( _N );
363   
364         VectorScalarProduct< T >( ret._vector, _vector, r, _N );
365         return( ret );
366   
367     }
368
369 // -----------------------------------------------------------------------------
370     template< class T >
371     TVector< T > TVector< T >::operator-( )
372     {
373         TVector< T > ret( _N );
374         uint32_t i;
375   
376         for( i = 0; i < _N; ret._vector[ i ] = ( T )0 - _vector[ i ], i++ );
377         return( ret );
378   
379     }
380
381 // -----------------------------------------------------------------------------
382     template< class T >
383     TVector< T > TVector< T >::operator!( )
384     {
385         TVector< T > ret( _N );
386   
387         VectorNormalize< T >( ret._vector, _vector, _N );
388         return( ret );
389   
390     }
391
392 // -----------------------------------------------------------------------------
393     template< class T >
394     TVector< T > TVector< T >::operator~( )
395     {
396         TVector< T > ret = *this;
397   
398         ret._type = ( _type == COL_VECTOR )? ROW_VECTOR: COL_VECTOR;
399         return( ret );
400   
401     }
402
403 } // namespace
404
405 #endif // GTMLIB__MATH__VECTOR__HXX
406
407 // EOF - vector.h