Point Cloud Library (PCL) 1.12.1
vector.hpp
1/*
2Copyright (c) 2006, Michael Kazhdan and Matthew Bolitho
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without modification,
6are permitted provided that the following conditions are met:
7
8Redistributions of source code must retain the above copyright notice, this list of
9conditions and the following disclaimer. Redistributions in binary form must reproduce
10the above copyright notice, this list of conditions and the following disclaimer
11in the documentation and/or other materials provided with the distribution.
12
13Neither the name of the Johns Hopkins University nor the names of its contributors
14may be used to endorse or promote products derived from this software without specific
15prior written permission.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
18EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES
19OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
20SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26DAMAGE.
27*/
28
29#ifndef __VECTORIMPL_HPP
30#define __VECTORIMPL_HPP
31
32////////////
33// Vector //
34////////////
35namespace 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 if (m_pV) {
72 memset( m_pV, 0, N*sizeof(T) );
73 }
74 }
75 template<class T>
76 Vector<T>::Vector( std::size_t N, T* pV )
77 {
78 Resize(N);
79 memcpy( m_pV, pV, N*sizeof(T) );
80 }
81 template<class T>
82 Vector<T>::~Vector(){Resize(0);}
83 template<class T>
85 {
86 Resize(V.m_N);
87 memcpy( m_pV, V.m_pV, m_N*sizeof(T) );
88 return *this;
89 }
90 template<class T>
91 std::size_t Vector<T>::Dimensions() const{return m_N;}
92 template<class T>
93 void Vector<T>::SetZero(void){for (std::size_t i=0; i<m_N; i++){m_pV[i] = T(0);}}
94 template<class T>
95 const T& Vector<T>::operator () (std::size_t i) const
96 {
97 Assert( i < m_N );
98 return m_pV[i];
99 }
100 template<class T>
101 T& Vector<T>::operator () (std::size_t i)
102 {
103 return m_pV[i];
104 }
105 template<class T>
106 const T& Vector<T>::operator [] (std::size_t i) const
107 {
108 return m_pV[i];
109 }
110 template<class T>
111 T& Vector<T>::operator [] (std::size_t i)
112 {
113 return m_pV[i];
114 }
115 template<class T>
117 {
118 Vector V(*this);
119 for (std::size_t i=0; i<m_N; i++)
120 V.m_pV[i] *= A;
121 return V;
122 }
123 template<class T>
125 {
126 for (std::size_t i=0; i<m_N; i++)
127 m_pV[i] *= A;
128 return *this;
129 }
130 template<class T>
132 {
133 Vector V(*this);
134 for (std::size_t i=0; i<m_N; i++)
135 V.m_pV[i] /= A;
136 return V;
137 }
138 template<class T>
140 {
141 for (std::size_t i=0; i<m_N; i++)
142 m_pV[i] /= A;
143 return *this;
144 }
145 template<class T>
147 {
148 Vector<T> V(m_N);
149 for (std::size_t i=0; i<m_N; i++)
150 V.m_pV[i] = m_pV[i] + V0.m_pV[i];
151
152 return V;
153 }
154 template<class T>
155 Vector<T>& Vector<T>::AddScaled(const Vector<T>& V,const T& scale)
156 {
157 for (std::size_t i=0; i<m_N; i++)
158 m_pV[i] += V.m_pV[i]*scale;
159
160 return *this;
161 }
162 template<class T>
164 {
165 for (std::size_t i=0; i<m_N; i++)
166 m_pV[i] -= V.m_pV[i]*scale;
167
168 return *this;
169 }
170 template<class T>
171 void Vector<T>::Add(const Vector<T>& V1,const T& scale1,const Vector<T>& V2,const T& scale2,Vector<T>& Out){
172 for (std::size_t i=0; i<V1.m_N; i++)
173 Out.m_pV[i]=V1.m_pV[i]*scale1+V2.m_pV[i]*scale2;
174 }
175 template<class T>
176 void Vector<T>::Add(const Vector<T>& V1,const T& scale1,const Vector<T>& V2,Vector<T>& Out){
177 for (std::size_t i=0; i<V1.m_N; i++)
178 Out.m_pV[i]=V1.m_pV[i]*scale1+V2.m_pV[i];
179 }
180 template<class T>
182 {
183 for (std::size_t i=0; i<m_N; i++)
184 m_pV[i] += V.m_pV[i];
185
186 return *this;
187 }
188 template<class T>
190 {
191 Vector<T> V(m_N);
192 for (std::size_t i=0; i<m_N; i++)
193 V.m_pV[i] = m_pV[i] - V0.m_pV[i];
194
195 return V;
196 }
197 template<class T>
199 {
200 Vector<T> V(m_N);
201
202 for (std::size_t i=0; i<m_N; i++)
203 V.m_pV[i] = -m_pV[i];
204
205 return V;
206 }
207 template<class T>
209 {
210 for (std::size_t i=0; i<m_N; i++)
211 m_pV[i] -= V.m_pV[i];
212
213 return *this;
214 }
215 template<class T>
216 T Vector<T>::Norm( std::size_t Ln ) const
217 {
218 T N = T();
219 for (std::size_t i = 0; i<m_N; i++)
220 N += pow(m_pV[i], (T)Ln);
221 return pow(N, (T)1.0/Ln);
222 }
223 template<class T>
225 {
226 T N = 1.0f/Norm(2);
227 for (std::size_t i = 0; i<m_N; i++)
228 m_pV[i] *= N;
229 }
230 template<class T>
232 {
233 T N = T();
234 for (std::size_t i = 0; i<m_N; i++)
235 N += m_pV[i]*m_pV[i];
236 return sqrt(N);
237 }
238 template<class T>
239 T Vector<T>::Dot( const Vector<T>& V ) const
240 {
241 T V0 = T();
242 for (std::size_t i=0; i<m_N; i++)
243 V0 += m_pV[i]*V.m_pV[i];
244
245 return V0;
246 }
247
248 template< class T >
249 bool Vector< T >::read( const char* fileName )
250 {
251 FILE* fp = fopen( fileName , "rb" );
252 if( !fp ) return false;
253 bool ret = read( fp );
254 fclose( fp );
255 return ret;
256 }
257 template< class T >
258 bool Vector< T >::write( const char* fileName ) const
259 {
260 FILE* fp = fopen( fileName , "wb" );
261 if( !fp ) return false;
262 bool ret = write( fp );
263 fclose( fp );
264 return ret;
265 }
266 template< class T >
267 bool Vector< T >::read( FILE* fp )
268 {
269 int d;
270 if( fread( &d , sizeof(int) , 1 , fp )!=1 ) return false;
271 Resize( d );
272 if( fread( &(*this)[0] , sizeof( T ) , d , fp )!=d ) return false;
273 return true;
274 }
275 template< class T >
276 bool Vector< T >::write( FILE* fp ) const
277 {
278 if( fwrite( &m_N , sizeof( int ) , 1 , fp )!=1 ) return false;
279 if( fwrite( &(*this)[0] , sizeof( T ) , m_N , fp )!=m_N ) return false;
280 return true;
281 }
282
283
284 /////////////
285 // NVector //
286 /////////////
287 template<class T,int Dim>
289 {
290 m_N = 0;
291 m_pV = 0;
292 }
293 template<class T,int Dim>
295 {
296 m_N = 0;
297 m_pV = 0;
298 Resize(V.m_N);
299 memcpy( m_pV, V.m_pV, m_N*sizeof(T)*Dim );
300 }
301 template<class T,int Dim>
302 NVector<T,Dim>::NVector( std::size_t N )
303 {
304 m_N=0;
305 m_pV=0;
306 Resize(N);
307 }
308 template<class T,int Dim>
309 void NVector<T,Dim>::Resize( std::size_t N )
310 {
311 if(m_N!=N){
312 if(m_N){delete[] m_pV;}
313 m_pV=NULL;
314 m_N = N;
315 if(N){m_pV = new T[Dim*N];}
316 }
317 memset( m_pV, 0, N*sizeof(T)*Dim );
318 }
319 template<class T,int Dim>
320 NVector<T,Dim>::NVector( std::size_t N, T* pV )
321 {
322 Resize(N);
323 memcpy( m_pV, pV, N*sizeof(T)*Dim );
324 }
325 template<class T,int Dim>
327 template<class T,int Dim>
329 {
330 Resize(V.m_N);
331 memcpy( m_pV, V.m_pV, m_N*sizeof(T)*Dim );
332 return *this;
333 }
334 template<class T,int Dim>
335 std::size_t NVector<T,Dim>::Dimensions() const{return m_N;}
336 template<class T,int Dim>
337 void NVector<T,Dim>::SetZero(void){for (std::size_t i=0; i<m_N*Dim; i++){m_pV[i] = T(0);}}
338 template<class T,int Dim>
339 const T* NVector<T,Dim>::operator () (std::size_t i) const
340 {
341 Assert( i < m_N );
342 return &m_pV[i*Dim];
343 }
344 template<class T,int Dim>
346 {
347 return &m_pV[i*Dim];
348 }
349 template<class T,int Dim>
350 const T* NVector<T,Dim>::operator [] (std::size_t i) const
351 {
352 return &m_pV[i*Dim];
353 }
354 template<class T,int Dim>
356 {
357 return &m_pV[i*Dim];
358 }
359 template<class T,int Dim>
361 {
362 NVector<T,Dim> V(*this);
363 for (std::size_t i=0; i<m_N*Dim; i++)
364 V.m_pV[i] *= A;
365 return V;
366 }
367 template<class T,int Dim>
369 {
370 for (std::size_t i=0; i<m_N*Dim; i++)
371 m_pV[i] *= A;
372 return *this;
373 }
374 template<class T,int Dim>
376 {
377 NVector<T,Dim> V(*this);
378 for (std::size_t i=0; i<m_N*Dim; i++)
379 V.m_pV[i] /= A;
380 return V;
381 }
382 template<class T,int Dim>
384 {
385 for (std::size_t i=0; i<m_N*Dim; i++)
386 m_pV[i] /= A;
387 return *this;
388 }
389 template<class T,int Dim>
391 {
392 NVector<T,Dim> V(m_N);
393 for (std::size_t i=0; i<m_N*Dim; i++)
394 V.m_pV[i] = m_pV[i] + V0.m_pV[i];
395
396 return V;
397 }
398 template<class T,int Dim>
400 {
401 for (std::size_t i=0; i<m_N*Dim; i++)
402 m_pV[i] += V.m_pV[i]*scale;
403
404 return *this;
405 }
406 template<class T,int Dim>
408 {
409 for (std::size_t i=0; i<m_N*Dim; i++)
410 m_pV[i] -= V.m_pV[i]*scale;
411
412 return *this;
413 }
414 template<class T,int Dim>
415 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){
416 for (std::size_t i=0; i<V1.m_N*Dim; i++)
417 Out.m_pV[i]=V1.m_pV[i]*scale1+V2.m_pV[i]*scale2;
418 }
419 template<class T,int Dim>
420 void NVector<T,Dim>::Add(const NVector<T,Dim>& V1,const T& scale1,const NVector<T,Dim>& V2,NVector<T,Dim>& Out){
421 for (std::size_t i=0; i<V1.m_N*Dim; i++)
422 Out.m_pV[i]=V1.m_pV[i]*scale1+V2.m_pV[i];
423 }
424 template<class T,int Dim>
426 {
427 for (std::size_t i=0; i<m_N*Dim; i++)
428 m_pV[i] += V.m_pV[i];
429
430 return *this;
431 }
432 template<class T,int Dim>
434 {
435 NVector<T,Dim> V(m_N);
436 for (std::size_t i=0; i<m_N*Dim; i++)
437 V.m_pV[i] = m_pV[i] - V0.m_pV[i];
438
439 return V;
440 }
441 template<class T,int Dim>
443 {
444 NVector<T,Dim> V(m_N);
445
446 for (std::size_t i=0; i<m_N*Dim; i++)
447 V.m_pV[i] = -m_pV[i];
448
449 return V;
450 }
451 template<class T,int Dim>
453 {
454 for (std::size_t i=0; i<m_N*Dim; i++)
455 m_pV[i] -= V.m_pV[i];
456
457 return *this;
458 }
459 template<class T,int Dim>
460 T NVector<T,Dim>::Norm( std::size_t Ln ) const
461 {
462 T N = T();
463 for (std::size_t i = 0; i<m_N*Dim; i++)
464 N += pow(m_pV[i], (T)Ln);
465 return pow(N, (T)1.0/Ln);
466 }
467 template<class T,int Dim>
469 {
470 T N = 1.0f/Norm(2);
471 for (std::size_t i = 0; i<m_N*3; i++)
472 m_pV[i] *= N;
473 }
474 template<class T,int Dim>
476 {
477 T N = T();
478 for (std::size_t i = 0; i<m_N*Dim; i++)
479 N += m_pV[i]*m_pV[i];
480 return sqrt(N);
481 }
482 template<class T,int Dim>
484 {
485 T V0 = T();
486 for (std::size_t i=0; i<m_N*Dim; i++)
487 V0 += m_pV[i]*V.m_pV[i];
488
489 return V0;
490 }
491
492 }
493}
494#endif
NVector operator*(const T &A) const
Definition: vector.hpp:360
static void Add(const NVector &V1, const T &scale1, const NVector &V2, const T &scale2, NVector &Out)
Definition: vector.hpp:415
NVector & operator-=(const NVector &V)
Definition: vector.hpp:452
const T * operator[](std::size_t i) const
Definition: vector.hpp:350
NVector & AddScaled(const NVector &V, const T &scale)
Definition: vector.hpp:399
NVector & operator/=(const T &A)
Definition: vector.hpp:383
NVector operator-() const
Definition: vector.hpp:442
void Resize(std::size_t N)
Definition: vector.hpp:309
T Dot(const NVector &V) const
Definition: vector.hpp:483
std::size_t m_N
Definition: vector.h:145
const T * operator()(std::size_t i) const
Definition: vector.hpp:339
std::size_t Dimensions() const
Definition: vector.hpp:335
NVector operator+(const NVector &V) const
Definition: vector.hpp:390
T Norm(std::size_t Ln) const
Definition: vector.hpp:460
NVector & SubtractScaled(const NVector &V, const T &scale)
Definition: vector.hpp:407
NVector & operator+=(const NVector &V)
Definition: vector.hpp:425
NVector operator/(const T &A) const
Definition: vector.hpp:375
NVector & operator=(const NVector &V)
Definition: vector.hpp:328
NVector & operator*=(const T &A)
Definition: vector.hpp:368
const T & operator()(std::size_t i) const
Definition: vector.hpp:95
Vector operator+(const Vector &V) const
Definition: vector.hpp:146
Vector & SubtractScaled(const Vector &V, const T &scale)
Definition: vector.hpp:163
Vector & AddScaled(const Vector &V, const T &scale)
Definition: vector.hpp:155
T Norm(std::size_t Ln) const
Definition: vector.hpp:216
Vector & operator/=(const T &A)
Definition: vector.hpp:139
bool read(FILE *fp)
Definition: vector.hpp:267
Vector & operator+=(const Vector &V)
Definition: vector.hpp:181
Vector operator*(const T &A) const
Definition: vector.hpp:116
static void Add(const Vector &V1, const T &scale1, const Vector &V2, const T &scale2, Vector &Out)
Definition: vector.hpp:171
std::size_t m_N
Definition: vector.h:93
std::size_t Dimensions() const
Definition: vector.hpp:91
void Resize(std::size_t N)
Definition: vector.hpp:63
const T & operator[](std::size_t i) const
Definition: vector.hpp:106
Vector & operator=(const Vector &V)
Definition: vector.hpp:84
T Length() const
Definition: vector.hpp:231
bool write(FILE *fp) const
Definition: vector.hpp:276
Vector & operator-=(const Vector &V)
Definition: vector.hpp:208
Vector operator/(const T &A) const
Definition: vector.hpp:131
Vector operator-() const
Definition: vector.hpp:198
Vector & operator*=(const T &A)
Definition: vector.hpp:124
T Dot(const Vector &V) const
Definition: vector.hpp:239
void read(std::istream &stream, Type &value)
Function for reading data from a stream.
Definition: region_xy.h:46
void write(std::ostream &stream, Type value)
Function for writing data to a stream.
Definition: region_xy.h:63