1 /*# ---------------------------------------------------------------------
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 # This software is governed by the CeCILL-B license under French law and
10 # abiding by the rules of distribution of free software. You can use,
11 # modify and/ or redistribute the software under the terms of the CeCILL-B
12 # license as circulated by CEA, CNRS and INRIA at the following URL
13 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
14 # or in the file LICENSE.txt.
16 # As a counterpart to the access to the source code and rights to copy,
17 # modify and redistribute granted by the license, users are provided only
18 # with a limited warranty and the software's author, the holder of the
19 # economic rights, and the successive licensors have only limited
22 # The fact that you are presently reading this means that you have had
23 # knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */
26 ////////////////////////////////////////////////////////////////////////////////
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
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.
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.
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 ////////////////////////////////////////////////////////////////////////////////
49 #ifndef GTMLIB__MATH__VECTOR__HXX
50 #define GTMLIB__MATH__VECTOR__HXX
57 #define ROW_VECTOR 0x01
58 #define COL_VECTOR 0x02
68 * This class defines a C++ template to use mathematical vectors
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?
87 /// Default constructor.
88 TVector( uint32_t N = 3, T data = ( T )0, int type = COL_VECTOR );
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 );
97 if( _myMemory ) VectorFreeMemory< T >( _vector );
101 void SetN( uint32_t N );
103 /** Assignation operators.
105 * @param r Right object (vector or matrix).
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 );
117 /// Scalar assignation.
118 TVector< T >& operator=( T r ) {
119 VectorAssignScalar< T >( _vector, r, _N );
124 /** Comparation operators.
126 * @param r Right object.
130 bool operator==( const TVector< T >& r );
132 bool operator!=( const TVector< T >& r );
135 /// Reference operator.
136 T& operator()( uint32_t i ) {
137 return( _vector[ i ] );
139 /// ANSI (C/C++ array) reference.
143 /// Vector's cardinality.
147 /// Vector's type (ROW_VECTOR/COL_VECTOR).
151 /// Mathematical norm (L2).
153 return( VectorNorm< T >( _vector, _N ) );
155 /// Normalizes the vector.
157 VectorNormalize< T >( _vector, _vector, _N );
160 T Dot( const TVector< T >& r ) {
161 return( VectorDotProduct< T >( _vector, r._vector, GTM_MIN( _N, r._N ) ) );
164 /** Binary operators.
166 * @param r Right object (vector, matrix or scalar).
170 TVector< T > operator+( const TVector< T >& r );
172 TVector< T > operator-( const TVector< T >& r );
174 TVector< T > operator*( const TVector< T >& r );
175 /// Matrix product (defined in class TMatrix).
176 TMatrix< T > operator*( TMatrix< T >& r );
178 TVector< T > operator*( T r );
181 /** Self-assignation binary operators.
183 * @param r Right object (vector, matrix or scalar).
187 TVector< T >& operator+=( const TVector< T >& r ) {
192 TVector< T >& operator-=( const TVector< T >& r ) {
197 TVector< T >& operator*=( const TVector< T >& r ) {
202 TVector< T >& operator*=( T r ) {
210 /// Additive inverse.
211 TVector< T > operator-( );
212 /// Normalized vector.
213 TVector< T > operator!( );
214 /// Transposed vector.
215 TVector< T > operator~( );
220 /// Vector's internal state.
226 /// Type (ROW_VECTOR/COL_VECTOR).
228 /// Have I created _vector?
234 // -----------------------------------------------------------------------------
236 TVector< T >::TVector( uint32_t N, T data, int type )
240 _vector = VectorAllocateMemory< T >( _N );
242 VectorAssignScalar< T >( _vector, data, _N );
246 // -----------------------------------------------------------------------------
248 TVector< T >::TVector( const TVector< T >& r )
253 _vector = VectorCopyMemory< T >( r._vector, _N );
257 // -----------------------------------------------------------------------------
259 TVector< T >::TVector( T* block, uint32_t N, bool copy, int type )
264 _vector = ( copy )? VectorCopyMemory< T >( block, _N ): block;
268 // -----------------------------------------------------------------------------
270 void TVector< T >::SetN( uint32_t N )
272 if( _myMemory ) VectorFreeMemory< T >( _vector );
274 _vector = VectorAllocateMemory< T >( _N );
276 VectorAssignScalar< T >( _vector, ( T )0, _N );
280 // -----------------------------------------------------------------------------
282 TVector< T >& TVector< T >::operator=( const TVector< T >& r )
285 * Only assigns the minimum cardinality (WARNING WITH NON-LOCAL MEMORY).
288 VectorAssignMemory< T >( _vector, r._vector, GTM_MIN( _N, r._N ) );
293 // -----------------------------------------------------------------------------
295 bool TVector< T >::operator==( const TVector< T >& r )
301 i = 0, ret = ( _N == r._N );
303 ret &= ( _vector[ i ] == r._vector[ i ] ), i++
309 // -----------------------------------------------------------------------------
311 bool TVector< T >::operator!=( const TVector< T >& r )
317 i = 0, ret = ( _N != r._N );
319 ret |= ( _vector[ i ] != r._vector[ i ] ), i++
325 // -----------------------------------------------------------------------------
327 TVector< T > TVector< T >::operator+( const TVector< T >& r )
329 TVector< T > ret( GTM_MIN( _N, r._N ) );
331 VectorAdd< T >( ret._vector, _vector, r._vector, GTM_MIN( _N, r._N ) );
336 // -----------------------------------------------------------------------------
338 TVector< T > TVector< T >::operator-( const TVector< T >& r )
340 TVector< T > ret( GTM_MIN( _N, r._N ) );
342 VectorSubtract< T >( ret._vector, _vector, r._vector, GTM_MIN( _N, r._N ) );
347 // -----------------------------------------------------------------------------
349 TVector< T > TVector< T >::operator*( const TVector< T >& r )
351 TVector< T > ret( GTM_MIN( _N, r._N ) );
353 VectorCrossProduct< T >( ret._vector, _vector, r._vector );
358 // -----------------------------------------------------------------------------
360 TVector< T > TVector< T >::operator*( T r )
362 TVector< T > ret( _N );
364 VectorScalarProduct< T >( ret._vector, _vector, r, _N );
369 // -----------------------------------------------------------------------------
371 TVector< T > TVector< T >::operator-( )
373 TVector< T > ret( _N );
376 for( i = 0; i < _N; ret._vector[ i ] = ( T )0 - _vector[ i ], i++ );
381 // -----------------------------------------------------------------------------
383 TVector< T > TVector< T >::operator!( )
385 TVector< T > ret( _N );
387 VectorNormalize< T >( ret._vector, _vector, _N );
392 // -----------------------------------------------------------------------------
394 TVector< T > TVector< T >::operator~( )
396 TVector< T > ret = *this;
398 ret._type = ( _type == COL_VECTOR )? ROW_VECTOR: COL_VECTOR;
405 #endif // GTMLIB__MATH__VECTOR__HXX