Point Cloud Library (PCL)  1.9.1-dev
vector.hpp
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 ////////////
33 // Vector //
34 ////////////
35 namespace pcl
36 {
37  namespace poisson
38  {
39 
40 
41  template<class T>
43  {
44  m_N = 0;
45  m_pV = 0;
46  }
47  template<class T>
49  {
50  m_N = 0;
51  m_pV = 0;
52  Resize(V.m_N);
53  memcpy( m_pV, V.m_pV, m_N*sizeof(T) );
54  }
55  template<class T>
56  Vector<T>::Vector( std::size_t N )
57  {
58  m_N=0;
59  m_pV=0;
60  Resize(N);
61  }
62  template<class T>
63  void Vector<T>::Resize( std::size_t N )
64  {
65  if(m_N!=N){
66  if(m_N){delete[] m_pV;}
67  m_pV=NULL;
68  m_N = N;
69  if(N){m_pV = new T[N];}
70  }
71  memset( m_pV, 0, N*sizeof(T) );
72  }
73  template<class T>
74  Vector<T>::Vector( std::size_t N, T* pV )
75  {
76  Resize(N);
77  memcpy( m_pV, pV, N*sizeof(T) );
78  }
79  template<class T>
80  Vector<T>::~Vector(){Resize(0);}
81  template<class T>
83  {
84  Resize(V.m_N);
85  memcpy( m_pV, V.m_pV, m_N*sizeof(T) );
86  return *this;
87  }
88  template<class T>
89  std::size_t Vector<T>::Dimensions() const{return m_N;}
90  template<class T>
91  void Vector<T>::SetZero(void){for (std::size_t i=0; i<m_N; i++){m_pV[i] = T(0);}}
92  template<class T>
93  const T& Vector<T>::operator () (std::size_t i) const
94  {
95  Assert( i < m_N );
96  return m_pV[i];
97  }
98  template<class T>
99  T& Vector<T>::operator () (std::size_t i)
100  {
101  return m_pV[i];
102  }
103  template<class T>
104  const T& Vector<T>::operator [] (std::size_t i) const
105  {
106  return m_pV[i];
107  }
108  template<class T>
109  T& Vector<T>::operator [] (std::size_t i)
110  {
111  return m_pV[i];
112  }
113  template<class T>
115  {
116  Vector V(*this);
117  for (std::size_t i=0; i<m_N; i++)
118  V.m_pV[i] *= A;
119  return V;
120  }
121  template<class T>
123  {
124  for (std::size_t i=0; i<m_N; i++)
125  m_pV[i] *= A;
126  return *this;
127  }
128  template<class T>
130  {
131  Vector V(*this);
132  for (std::size_t i=0; i<m_N; i++)
133  V.m_pV[i] /= A;
134  return V;
135  }
136  template<class T>
138  {
139  for (std::size_t i=0; i<m_N; i++)
140  m_pV[i] /= A;
141  return *this;
142  }
143  template<class T>
145  {
146  Vector<T> V(m_N);
147  for (std::size_t i=0; i<m_N; i++)
148  V.m_pV[i] = m_pV[i] + V0.m_pV[i];
149 
150  return V;
151  }
152  template<class T>
153  Vector<T>& Vector<T>::AddScaled(const Vector<T>& V,const T& scale)
154  {
155  for (std::size_t i=0; i<m_N; i++)
156  m_pV[i] += V.m_pV[i]*scale;
157 
158  return *this;
159  }
160  template<class T>
162  {
163  for (std::size_t i=0; i<m_N; i++)
164  m_pV[i] -= V.m_pV[i]*scale;
165 
166  return *this;
167  }
168  template<class T>
169  void Vector<T>::Add(const Vector<T>& V1,const T& scale1,const Vector<T>& V2,const T& scale2,Vector<T>& Out){
170  for (std::size_t i=0; i<V1.m_N; i++)
171  Out.m_pV[i]=V1.m_pV[i]*scale1+V2.m_pV[i]*scale2;
172  }
173  template<class T>
174  void Vector<T>::Add(const Vector<T>& V1,const T& scale1,const Vector<T>& V2,Vector<T>& Out){
175  for (std::size_t i=0; i<V1.m_N; i++)
176  Out.m_pV[i]=V1.m_pV[i]*scale1+V2.m_pV[i];
177  }
178  template<class T>
180  {
181  for (std::size_t i=0; i<m_N; i++)
182  m_pV[i] += V.m_pV[i];
183 
184  return *this;
185  }
186  template<class T>
188  {
189  Vector<T> V(m_N);
190  for (std::size_t i=0; i<m_N; i++)
191  V.m_pV[i] = m_pV[i] - V0.m_pV[i];
192 
193  return V;
194  }
195  template<class T>
197  {
198  Vector<T> V(m_N);
199 
200  for (std::size_t i=0; i<m_N; i++)
201  V.m_pV[i] = -m_pV[i];
202 
203  return V;
204  }
205  template<class T>
207  {
208  for (std::size_t i=0; i<m_N; i++)
209  m_pV[i] -= V.m_pV[i];
210 
211  return *this;
212  }
213  template<class T>
214  T Vector<T>::Norm( std::size_t Ln ) const
215  {
216  T N = T();
217  for (std::size_t i = 0; i<m_N; i++)
218  N += pow(m_pV[i], (T)Ln);
219  return pow(N, (T)1.0/Ln);
220  }
221  template<class T>
223  {
224  T N = 1.0f/Norm(2);
225  for (std::size_t i = 0; i<m_N; i++)
226  m_pV[i] *= N;
227  }
228  template<class T>
230  {
231  T N = T();
232  for (std::size_t i = 0; i<m_N; i++)
233  N += m_pV[i]*m_pV[i];
234  return sqrt(N);
235  }
236  template<class T>
237  T Vector<T>::Dot( const Vector<T>& V ) const
238  {
239  T V0 = T();
240  for (std::size_t i=0; i<m_N; i++)
241  V0 += m_pV[i]*V.m_pV[i];
242 
243  return V0;
244  }
245 
246  template< class T >
247  bool Vector< T >::read( const char* fileName )
248  {
249  FILE* fp = fopen( fileName , "rb" );
250  if( !fp ) return false;
251  bool ret = read( fp );
252  fclose( fp );
253  return ret;
254  }
255  template< class T >
256  bool Vector< T >::write( const char* fileName ) const
257  {
258  FILE* fp = fopen( fileName , "wb" );
259  if( !fp ) return false;
260  bool ret = write( fp );
261  fclose( fp );
262  return ret;
263  }
264  template< class T >
265  bool Vector< T >::read( FILE* fp )
266  {
267  int d;
268  if( fread( &d , sizeof(int) , 1 , fp )!=1 ) return false;
269  Resize( d );
270  if( fread( &(*this)[0] , sizeof( T ) , d , fp )!=d ) return false;
271  return true;
272  }
273  template< class T >
274  bool Vector< T >::write( FILE* fp ) const
275  {
276  if( fwrite( &m_N , sizeof( int ) , 1 , fp )!=1 ) return false;
277  if( fwrite( &(*this)[0] , sizeof( T ) , m_N , fp )!=m_N ) return false;
278  return true;
279  }
280 
281 
282  /////////////
283  // NVector //
284  /////////////
285  template<class T,int Dim>
287  {
288  m_N = 0;
289  m_pV = 0;
290  }
291  template<class T,int Dim>
293  {
294  m_N = 0;
295  m_pV = 0;
296  Resize(V.m_N);
297  memcpy( m_pV, V.m_pV, m_N*sizeof(T)*Dim );
298  }
299  template<class T,int Dim>
300  NVector<T,Dim>::NVector( std::size_t N )
301  {
302  m_N=0;
303  m_pV=0;
304  Resize(N);
305  }
306  template<class T,int Dim>
307  void NVector<T,Dim>::Resize( std::size_t N )
308  {
309  if(m_N!=N){
310  if(m_N){delete[] m_pV;}
311  m_pV=NULL;
312  m_N = N;
313  if(N){m_pV = new T[Dim*N];}
314  }
315  memset( m_pV, 0, N*sizeof(T)*Dim );
316  }
317  template<class T,int Dim>
318  NVector<T,Dim>::NVector( std::size_t N, T* pV )
319  {
320  Resize(N);
321  memcpy( m_pV, pV, N*sizeof(T)*Dim );
322  }
323  template<class T,int Dim>
325  template<class T,int Dim>
327  {
328  Resize(V.m_N);
329  memcpy( m_pV, V.m_pV, m_N*sizeof(T)*Dim );
330  return *this;
331  }
332  template<class T,int Dim>
333  std::size_t NVector<T,Dim>::Dimensions() const{return m_N;}
334  template<class T,int Dim>
335  void NVector<T,Dim>::SetZero(void){for (std::size_t i=0; i<m_N*Dim; i++){m_pV[i] = T(0);}}
336  template<class T,int Dim>
337  const T* NVector<T,Dim>::operator () (std::size_t i) const
338  {
339  Assert( i < m_N );
340  return &m_pV[i*Dim];
341  }
342  template<class T,int Dim>
344  {
345  return &m_pV[i*Dim];
346  }
347  template<class T,int Dim>
348  const T* NVector<T,Dim>::operator [] (std::size_t i) const
349  {
350  return &m_pV[i*Dim];
351  }
352  template<class T,int Dim>
354  {
355  return &m_pV[i*Dim];
356  }
357  template<class T,int Dim>
359  {
360  NVector<T,Dim> V(*this);
361  for (std::size_t i=0; i<m_N*Dim; i++)
362  V.m_pV[i] *= A;
363  return V;
364  }
365  template<class T,int Dim>
367  {
368  for (std::size_t i=0; i<m_N*Dim; i++)
369  m_pV[i] *= A;
370  return *this;
371  }
372  template<class T,int Dim>
374  {
375  NVector<T,Dim> V(*this);
376  for (std::size_t i=0; i<m_N*Dim; i++)
377  V.m_pV[i] /= A;
378  return V;
379  }
380  template<class T,int Dim>
382  {
383  for (std::size_t i=0; i<m_N*Dim; i++)
384  m_pV[i] /= A;
385  return *this;
386  }
387  template<class T,int Dim>
389  {
390  NVector<T,Dim> V(m_N);
391  for (std::size_t i=0; i<m_N*Dim; i++)
392  V.m_pV[i] = m_pV[i] + V0.m_pV[i];
393 
394  return V;
395  }
396  template<class T,int Dim>
398  {
399  for (std::size_t i=0; i<m_N*Dim; i++)
400  m_pV[i] += V.m_pV[i]*scale;
401 
402  return *this;
403  }
404  template<class T,int Dim>
406  {
407  for (std::size_t i=0; i<m_N*Dim; i++)
408  m_pV[i] -= V.m_pV[i]*scale;
409 
410  return *this;
411  }
412  template<class T,int Dim>
413  void NVector<T,Dim>::Add(const NVector<T,Dim>& V1,const T& scale1,const NVector<T,Dim>& V2,const T& scale2,NVector<T,Dim>& Out){
414  for (std::size_t i=0; i<V1.m_N*Dim; i++)
415  Out.m_pV[i]=V1.m_pV[i]*scale1+V2.m_pV[i]*scale2;
416  }
417  template<class T,int Dim>
418  void NVector<T,Dim>::Add(const NVector<T,Dim>& V1,const T& scale1,const NVector<T,Dim>& V2,NVector<T,Dim>& Out){
419  for (std::size_t i=0; i<V1.m_N*Dim; i++)
420  Out.m_pV[i]=V1.m_pV[i]*scale1+V2.m_pV[i];
421  }
422  template<class T,int Dim>
424  {
425  for (std::size_t i=0; i<m_N*Dim; i++)
426  m_pV[i] += V.m_pV[i];
427 
428  return *this;
429  }
430  template<class T,int Dim>
432  {
433  NVector<T,Dim> V(m_N);
434  for (std::size_t i=0; i<m_N*Dim; i++)
435  V.m_pV[i] = m_pV[i] - V0.m_pV[i];
436 
437  return V;
438  }
439  template<class T,int Dim>
441  {
442  NVector<T,Dim> V(m_N);
443 
444  for (std::size_t i=0; i<m_N*Dim; i++)
445  V.m_pV[i] = -m_pV[i];
446 
447  return V;
448  }
449  template<class T,int Dim>
451  {
452  for (std::size_t i=0; i<m_N*Dim; i++)
453  m_pV[i] -= V.m_pV[i];
454 
455  return *this;
456  }
457  template<class T,int Dim>
458  T NVector<T,Dim>::Norm( std::size_t Ln ) const
459  {
460  T N = T();
461  for (std::size_t i = 0; i<m_N*Dim; i++)
462  N += pow(m_pV[i], (T)Ln);
463  return pow(N, (T)1.0/Ln);
464  }
465  template<class T,int Dim>
467  {
468  T N = 1.0f/Norm(2);
469  for (std::size_t i = 0; i<m_N*3; i++)
470  m_pV[i] *= N;
471  }
472  template<class T,int Dim>
474  {
475  T N = T();
476  for (std::size_t i = 0; i<m_N*Dim; i++)
477  N += m_pV[i]*m_pV[i];
478  return sqrt(N);
479  }
480  template<class T,int Dim>
482  {
483  T V0 = T();
484  for (std::size_t i=0; i<m_N*Dim; i++)
485  V0 += m_pV[i]*V.m_pV[i];
486 
487  return V0;
488  }
489 
490  }
491 }
492 #endif
std::size_t Dimensions() const
Definition: vector.hpp:333
NVector operator*(const T &A) const
Definition: vector.hpp:358
NVector operator+(const NVector &V) const
Definition: vector.hpp:388
const T * operator[](std::size_t i) const
Definition: vector.hpp:348
const T & operator[](std::size_t i) const
Definition: vector.hpp:104
Vector & SubtractScaled(const Vector &V, const T &scale)
Definition: vector.hpp:161
std::size_t m_N
Definition: vector.h:145
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
static void Add(const NVector &V1, const T &scale1, const NVector &V2, const T &scale2, NVector &Out)
Definition: vector.hpp:413
NVector & operator*=(const T &A)
Definition: vector.hpp:366
bool read(FILE *fp)
Definition: vector.hpp:265
NVector & operator-=(const NVector &V)
Definition: vector.hpp:450
void read(std::istream &stream, Type &value)
Function for reading data from a stream.
Definition: region_xy.h:46
Vector & operator+=(const Vector &V)
Definition: vector.hpp:179
NVector & AddScaled(const NVector &V, const T &scale)
Definition: vector.hpp:397
T Dot(const Vector &V) const
Definition: vector.hpp:237
const T & operator()(std::size_t i) const
Definition: vector.hpp:93
NVector & SubtractScaled(const NVector &V, const T &scale)
Definition: vector.hpp:405
T Length() const
Definition: vector.hpp:229
NVector operator/(const T &A) const
Definition: vector.hpp:373
bool write(FILE *fp) const
Definition: vector.hpp:274
const T * operator()(std::size_t i) const
Definition: vector.hpp:337
std::size_t Dimensions() const
Definition: vector.hpp:89
Vector & operator-=(const Vector &V)
Definition: vector.hpp:206
T Dot(const NVector &V) const
Definition: vector.hpp:481
Vector operator/(const T &A) const
Definition: vector.hpp:129
Vector operator-() const
Definition: vector.hpp:196
std::size_t m_N
Definition: vector.h:93
Vector operator*(const T &A) const
Definition: vector.hpp:114
T Norm(std::size_t Ln) const
Definition: vector.hpp:214
Vector operator+(const Vector &V) const
Definition: vector.hpp:144
NVector & operator+=(const NVector &V)
Definition: vector.hpp:423
static void Add(const Vector &V1, const T &scale1, const Vector &V2, const T &scale2, Vector &Out)
Definition: vector.hpp:169
T Norm(std::size_t Ln) const
Definition: vector.hpp:458
Vector & operator=(const Vector &V)
Definition: vector.hpp:82
Vector & operator/=(const T &A)
Definition: vector.hpp:137
void write(std::ostream &stream, Type value)
Function for writing data to a stream.
Definition: region_xy.h:63
void Resize(std::size_t N)
Definition: vector.hpp:63
Vector & operator*=(const T &A)
Definition: vector.hpp:122
NVector & operator/=(const T &A)
Definition: vector.hpp:381
NVector operator-() const
Definition: vector.hpp:440
Vector & AddScaled(const Vector &V, const T &scale)
Definition: vector.hpp:153
NVector & operator=(const NVector &V)
Definition: vector.hpp:326
void Resize(std::size_t N)
Definition: vector.hpp:307