Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Vector.inl
1 /*
2 Copyright (c) 2006, Michael Kazhdan and Matthew Bolitho
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7 
8 Redistributions of source code must retain the above copyright notice, this list of
9 conditions and the following disclaimer. Redistributions in binary form must reproduce
10 the above copyright notice, this list of conditions and the following disclaimer
11 in the documentation and/or other materials provided with the distribution.
12 
13 Neither the name of the Johns Hopkins University nor the names of its contributors
14 may be used to endorse or promote products derived from this software without specific
15 prior written permission.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
18 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
20 SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26 DAMAGE.
27 */
28 
29 #ifndef __VECTORIMPL_HPP
30 #define __VECTORIMPL_HPP
31 
32 #include <stdio.h>
33 #include <string.h>
34 
36 // Vector //
38 template<class T>
40 {
41  m_N = 0;
42  m_pV = NullPointer< T >();
43 }
44 template< class T >
46 {
47  m_N = 0;
48  m_pV = NullPointer< T >();
49  Resize( V.m_N );
50  memcpy( m_pV , V.m_pV , m_N*sizeof(T) );
51 }
52 template<class T>
54 {
55  m_N=0;
56  m_pV = NullPointer< T >();
57  Resize(N);
58 }
59 template<class T>
60 void PoissonVector<T>::Resize( size_t N )
61 {
62  if( m_N!=N )
63  {
64  if( m_N ) DeletePointer( m_pV );
65  m_N = N;
66  m_pV = NewPointer< T >( N );
67  }
68  if( N ) memset( m_pV , 0 , N*sizeof(T) );
69 }
70 
71 template<class T>
72 PoissonVector<T>::PoissonVector( size_t N, ConstPointer( T ) pV )
73 {
74  Resize(N);
75  memcpy( m_pV, pV, N*sizeof(T) );
76 }
77 template<class T>
79 template<class T>
81 {
82  Resize(V.m_N);
83  memcpy( m_pV, V.m_pV, m_N*sizeof(T) );
84  return *this;
85 }
86 template<class T>
87 size_t PoissonVector<T>::Dimensions() const{return m_N;}
88 template<class T>
89 void PoissonVector<T>::SetZero(void){for (size_t i=0; i<m_N; i++){m_pV[i] = T(0);}}
90 template<class T>
91 const T& PoissonVector<T>::operator () (size_t i) const
92 {
93  return m_pV[i];
94 }
95 template<class T>
97 {
98  return m_pV[i];
99 }
100 template<class T>
101 const T& PoissonVector<T>::operator [] (size_t i) const
102 {
103  return m_pV[i];
104 }
105 template<class T>
107 {
108  return m_pV[i];
109 }
110 template<class T>
112 {
113  PoissonVector V(*this);
114  for (size_t i=0; i<m_N; i++) V.m_pV[i] *= A;
115  return V;
116 }
117 template<class T>
119 {
120  for (size_t i=0; i<m_N; i++) m_pV[i] *= A;
121  return *this;
122 }
123 template<class T>
125 {
126  PoissonVector V(*this);
127  for (size_t i=0; i<m_N; i++) V.m_pV[i] /= A;
128  return V;
129 }
130 template<class T>
132 {
133  for (size_t i=0; i<m_N; i++) m_pV[i] /= A;
134  return *this;
135 }
136 template<class T>
138 {
139  PoissonVector<T> V(m_N);
140  for (size_t i=0; i<m_N; i++) V.m_pV[i] = m_pV[i] + V0.m_pV[i];
141  return V;
142 }
143 template<class T>
145 {
146  for ( size_t i=0 ; i<m_N ; i++ ) m_pV[i] += V.m_pV[i];
147  return *this;
148 }
149 template<class T>
151 {
152  PoissonVector<T> V(m_N);
153  for (size_t i=0; i<m_N; i++) V.m_pV[i] = m_pV[i] - V0.m_pV[i];
154  return V;
155 }
156 template<class T>
158 {
159  for (size_t i=0; i<m_N; i++) m_pV[i] -= V.m_pV[i];
160  return *this;
161 }
162 
163 template<class T>
165 {
166  PoissonVector<T> V(m_N);
167  for (size_t i=0; i<m_N; i++) V.m_pV[i] = -m_pV[i];
168  return V;
169 }
170 
171 template< class T >
173 {
174  for( int c=0 ; c<count ; c++ ) for( size_t i=0 ; i<m_N ; i++ ) m_pV[i] += V[c].m_pV[i];
175  return *this;
176 }
177 
178 template< class T >
180 {
181  for (size_t i=0; i<m_N; i++) m_pV[i] += V.m_pV[i]*scale;
182  return *this;
183 }
184 template<class T>
186 {
187  for (size_t i=0; i<m_N; i++) m_pV[i] -= V.m_pV[i]*scale;
188  return *this;
189 }
190 
191 template<class T>
192 void PoissonVector<T>::Add( const PoissonVector<T>& V1 , const T& scale1 , const PoissonVector<T>& V2 , const T& scale2 , PoissonVector<T>& Out )
193 {
194  for( size_t i=0 ; i<V1.m_N ; i++ ) Out.m_pV[i]=V1.m_pV[i]*scale1+V2.m_pV[i]*scale2;
195 }
196 template<class T>
197 void PoissonVector<T>::Add(const PoissonVector<T>& V1,const T& scale1,const PoissonVector<T>& V2,PoissonVector<T>& Out)
198 {
199  for( size_t i=0 ; i<V1.m_N ; i++ ) Out.m_pV[i]=V1.m_pV[i]*scale1+V2.m_pV[i];
200 }
201 
202 template<class T>
203 T PoissonVector<T>::Norm( size_t Ln ) const
204 {
205  T N = T();
206  for (size_t i = 0; i<m_N; i++)
207  N += pow(m_pV[i], (T)Ln);
208  return pow(N, (T)1.0/Ln);
209 }
210 template<class T>
212 {
213  T N = 1.0f/Norm(2);
214  for (size_t i = 0; i<m_N; i++)
215  m_pV[i] *= N;
216 }
217 template<class T>
218 T PoissonVector<T>::Length() const
219 {
220  T N = T();
221  for (size_t i = 0; i<m_N; i++)
222  N += m_pV[i]*m_pV[i];
223  return sqrt(N);
224 }
225 template<class T>
226 T PoissonVector<T>::Dot( const PoissonVector<T>& V ) const
227 {
228  T V0 = T();
229  for( size_t i=0 ; i<m_N ; i++ ) V0 += m_pV[i]*V.m_pV[i];
230  return V0;
231 }
232 
233 template< class T >
234 bool PoissonVector< T >::read( const char* fileName )
235 {
236  FILE* fp = fopen( fileName , "rb" );
237  if( !fp ) return false;
238  bool ret = read( fp );
239  fclose( fp );
240  return ret;
241 }
242 template< class T >
243 bool PoissonVector< T >::write( const char* fileName ) const
244 {
245  FILE* fp = fopen( fileName , "wb" );
246  if( !fp ) return false;
247  bool ret = write( fp );
248  fclose( fp );
249  return ret;
250 }
251 template< class T >
252 bool PoissonVector< T >::read( FILE* fp )
253 {
254  int d;
255  if( fread( &d , sizeof(int) , 1 , fp )!=1 ) return false;
256  Resize( d );
257  if( fread( &(*this)[0] , sizeof( T ) , d , fp )!=d ) return false;
258  return true;
259 }
260 template< class T >
261 bool PoissonVector< T >::write( FILE* fp ) const
262 {
263  if( fwrite( &m_N , sizeof( int ) , 1 , fp )!=1 ) return false;
264  if( fwrite( &(*this)[0] , sizeof( T ) , m_N , fp )!=m_N ) return false;
265  return true;
266 }
267 
268 
269 
270 #endif