vector.hpp
/*
Copyright (c) 2006, Michael Kazhdan and Matthew Bolitho
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer. Redistributions in binary form must reproduce
the above copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the distribution.
Neither the name of the Johns Hopkins University nor the names of its contributors
may be used to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
*/
#ifndef __VECTORIMPL_HPP
#define __VECTORIMPL_HPP
////////////
// Vector //
////////////
namespace pcl
{
namespace poisson
{
template<class T>
Vector<T>::Vector()
{
m_N = 0;
m_pV = 0;
}
template<class T>
Vector<T>::Vector( const Vector<T>& V )
{
m_N = 0;
m_pV = 0;
Resize(V.m_N);
memcpy( m_pV, V.m_pV, m_N*sizeof(T) );
}
template<class T>
Vector<T>::Vector( std::size_t N )
{
m_N=0;
m_pV=0;
Resize(N);
}
template<class T>
void Vector<T>::Resize( std::size_t N )
{
if(m_N!=N){
if(m_N){delete[] m_pV;}
m_pV=NULL;
m_N = N;
if(N){m_pV = new T[N];}
}
if (m_pV) {
memset( m_pV, 0, N*sizeof(T) );
}
}
template<class T>
Vector<T>::Vector( std::size_t N, T* pV )
{
Resize(N);
memcpy( m_pV, pV, N*sizeof(T) );
}
template<class T>
Vector<T>::~Vector(){Resize(0);}
template<class T>
Vector<T>& Vector<T>::operator = (const Vector& V)
{
Resize(V.m_N);
memcpy( m_pV, V.m_pV, m_N*sizeof(T) );
return *this;
}
template<class T>
std::size_t Vector<T>::Dimensions() const{return m_N;}
template<class T>
void Vector<T>::SetZero(void){for (std::size_t i=0; i<m_N; i++){m_pV[i] = T(0);}}
template<class T>
const T& Vector<T>::operator () (std::size_t i) const
{
Assert( i < m_N );
return m_pV[i];
}
template<class T>
T& Vector<T>::operator () (std::size_t i)
{
return m_pV[i];
}
template<class T>
const T& Vector<T>::operator [] (std::size_t i) const
{
return m_pV[i];
}
template<class T>
T& Vector<T>::operator [] (std::size_t i)
{
return m_pV[i];
}
template<class T>
Vector<T> Vector<T>::operator * (const T& A) const
{
Vector V(*this);
for (std::size_t i=0; i<m_N; i++)
V.m_pV[i] *= A;
return V;
}
template<class T>
Vector<T>& Vector<T>::operator *= (const T& A)
{
for (std::size_t i=0; i<m_N; i++)
m_pV[i] *= A;
return *this;
}
template<class T>
Vector<T> Vector<T>::operator / (const T& A) const
{
Vector V(*this);
for (std::size_t i=0; i<m_N; i++)
V.m_pV[i] /= A;
return V;
}
template<class T>
Vector<T>& Vector<T>::operator /= (const T& A)
{
for (std::size_t i=0; i<m_N; i++)
m_pV[i] /= A;
return *this;
}
template<class T>
Vector<T> Vector<T>::operator + (const Vector<T>& V0) const
{
Vector<T> V(m_N);
for (std::size_t i=0; i<m_N; i++)
V.m_pV[i] = m_pV[i] + V0.m_pV[i];
return V;
}
template<class T>
Vector<T>& Vector<T>::AddScaled(const Vector<T>& V,const T& scale)
{
for (std::size_t i=0; i<m_N; i++)
m_pV[i] += V.m_pV[i]*scale;
return *this;
}
template<class T>
Vector<T>& Vector<T>::SubtractScaled(const Vector<T>& V,const T& scale)
{
for (std::size_t i=0; i<m_N; i++)
m_pV[i] -= V.m_pV[i]*scale;
return *this;
}
template<class T>
void Vector<T>::Add(const Vector<T>& V1,const T& scale1,const Vector<T>& V2,const T& scale2,Vector<T>& Out){
for (std::size_t i=0; i<V1.m_N; i++)
Out.m_pV[i]=V1.m_pV[i]*scale1+V2.m_pV[i]*scale2;
}
template<class T>
void Vector<T>::Add(const Vector<T>& V1,const T& scale1,const Vector<T>& V2,Vector<T>& Out){
for (std::size_t i=0; i<V1.m_N; i++)
Out.m_pV[i]=V1.m_pV[i]*scale1+V2.m_pV[i];
}
template<class T>
Vector<T>& Vector<T>::operator += (const Vector<T>& V)
{
for (std::size_t i=0; i<m_N; i++)
m_pV[i] += V.m_pV[i];
return *this;
}
template<class T>
Vector<T> Vector<T>::operator - (const Vector<T>& V0) const
{
Vector<T> V(m_N);
for (std::size_t i=0; i<m_N; i++)
V.m_pV[i] = m_pV[i] - V0.m_pV[i];
return V;
}
template<class T>
Vector<T> Vector<T>::operator - (void) const
{
Vector<T> V(m_N);
for (std::size_t i=0; i<m_N; i++)
V.m_pV[i] = -m_pV[i];
return V;
}
template<class T>
Vector<T>& Vector<T>::operator -= (const Vector<T>& V)
{
for (std::size_t i=0; i<m_N; i++)
m_pV[i] -= V.m_pV[i];
return *this;
}
template<class T>
T Vector<T>::Norm( std::size_t Ln ) const
{
T N = T();
for (std::size_t i = 0; i<m_N; i++)
N += pow(m_pV[i], (T)Ln);
return pow(N, (T)1.0/Ln);
}
template<class T>
void Vector<T>::Normalize()
{
T N = 1.0f/Norm(2);
for (std::size_t i = 0; i<m_N; i++)
m_pV[i] *= N;
}
template<class T>
T Vector<T>::Length() const
{
T N = T();
for (std::size_t i = 0; i<m_N; i++)
N += m_pV[i]*m_pV[i];
return sqrt(N);
}
template<class T>
T Vector<T>::Dot( const Vector<T>& V ) const
{
T V0 = T();
for (std::size_t i=0; i<m_N; i++)
V0 += m_pV[i]*V.m_pV[i];
return V0;
}
template< class T >
bool Vector< T >::read( const char* fileName )
{
FILE* fp = fopen( fileName , "rb" );
if( !fp ) return false;
bool ret = read( fp );
fclose( fp );
return ret;
}
template< class T >
bool Vector< T >::write( const char* fileName ) const
{
FILE* fp = fopen( fileName , "wb" );
if( !fp ) return false;
bool ret = write( fp );
fclose( fp );
return ret;
}
template< class T >
bool Vector< T >::read( FILE* fp )
{
int d;
if( fread( &d , sizeof(int) , 1 , fp )!=1 ) return false;
Resize( d );
if( fread( &(*this)[0] , sizeof( T ) , d , fp )!=d ) return false;
return true;
}
template< class T >
bool Vector< T >::write( FILE* fp ) const
{
if( fwrite( &m_N , sizeof( int ) , 1 , fp )!=1 ) return false;
if( fwrite( &(*this)[0] , sizeof( T ) , m_N , fp )!=m_N ) return false;
return true;
}
/////////////
// NVector //
/////////////
template<class T,int Dim>
NVector<T,Dim>::NVector()
{
m_N = 0;
m_pV = 0;
}
template<class T,int Dim>
NVector<T,Dim>::NVector( const NVector<T,Dim>& V )
{
m_N = 0;
m_pV = 0;
Resize(V.m_N);
memcpy( m_pV, V.m_pV, m_N*sizeof(T)*Dim );
}
template<class T,int Dim>
NVector<T,Dim>::NVector( std::size_t N )
{
m_N=0;
m_pV=0;
Resize(N);
}
template<class T,int Dim>
void NVector<T,Dim>::Resize( std::size_t N )
{
if(m_N!=N){
if(m_N){delete[] m_pV;}
m_pV=NULL;
m_N = N;
if(N){m_pV = new T[Dim*N];}
}
memset( m_pV, 0, N*sizeof(T)*Dim );
}
template<class T,int Dim>
NVector<T,Dim>::NVector( std::size_t N, T* pV )
{
Resize(N);
memcpy( m_pV, pV, N*sizeof(T)*Dim );
}
template<class T,int Dim>
NVector<T,Dim>::~NVector(){Resize(0);}
template<class T,int Dim>
NVector<T,Dim>& NVector<T,Dim>::operator = (const NVector& V)
{
Resize(V.m_N);
memcpy( m_pV, V.m_pV, m_N*sizeof(T)*Dim );
return *this;
}
template<class T,int Dim>
std::size_t NVector<T,Dim>::Dimensions() const{return m_N;}
template<class T,int Dim>
void NVector<T,Dim>::SetZero(void){for (std::size_t i=0; i<m_N*Dim; i++){m_pV[i] = T(0);}}
template<class T,int Dim>
const T* NVector<T,Dim>::operator () (std::size_t i) const
{
Assert( i < m_N );
return &m_pV[i*Dim];
}
template<class T,int Dim>
T* NVector<T,Dim>::operator () (std::size_t i)
{
return &m_pV[i*Dim];
}
template<class T,int Dim>
const T* NVector<T,Dim>::operator [] (std::size_t i) const
{
return &m_pV[i*Dim];
}
template<class T,int Dim>
T* NVector<T,Dim>::operator [] (std::size_t i)
{
return &m_pV[i*Dim];
}
template<class T,int Dim>
NVector<T,Dim> NVector<T,Dim>::operator * (const T& A) const
{
NVector<T,Dim> V(*this);
for (std::size_t i=0; i<m_N*Dim; i++)
V.m_pV[i] *= A;
return V;
}
template<class T,int Dim>
NVector<T,Dim>& NVector<T,Dim>::operator *= (const T& A)
{
for (std::size_t i=0; i<m_N*Dim; i++)
m_pV[i] *= A;
return *this;
}
template<class T,int Dim>
NVector<T,Dim> NVector<T,Dim>::operator / (const T& A) const
{
NVector<T,Dim> V(*this);
for (std::size_t i=0; i<m_N*Dim; i++)
V.m_pV[i] /= A;
return V;
}
template<class T,int Dim>
NVector<T,Dim>& NVector<T,Dim>::operator /= (const T& A)
{
for (std::size_t i=0; i<m_N*Dim; i++)
m_pV[i] /= A;
return *this;
}
template<class T,int Dim>
NVector<T,Dim> NVector<T,Dim>::operator + (const NVector<T,Dim>& V0) const
{
NVector<T,Dim> V(m_N);
for (std::size_t i=0; i<m_N*Dim; i++)
V.m_pV[i] = m_pV[i] + V0.m_pV[i];
return V;
}
template<class T,int Dim>
NVector<T,Dim>& NVector<T,Dim>::AddScaled(const NVector<T,Dim>& V,const T& scale)
{
for (std::size_t i=0; i<m_N*Dim; i++)
m_pV[i] += V.m_pV[i]*scale;
return *this;
}
template<class T,int Dim>
NVector<T,Dim>& NVector<T,Dim>::SubtractScaled(const NVector<T,Dim>& V,const T& scale)
{
for (std::size_t i=0; i<m_N*Dim; i++)
m_pV[i] -= V.m_pV[i]*scale;
return *this;
}
template<class T,int Dim>
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){
for (std::size_t i=0; i<V1.m_N*Dim; i++)
Out.m_pV[i]=V1.m_pV[i]*scale1+V2.m_pV[i]*scale2;
}
template<class T,int Dim>
void NVector<T,Dim>::Add(const NVector<T,Dim>& V1,const T& scale1,const NVector<T,Dim>& V2,NVector<T,Dim>& Out){
for (std::size_t i=0; i<V1.m_N*Dim; i++)
Out.m_pV[i]=V1.m_pV[i]*scale1+V2.m_pV[i];
}
template<class T,int Dim>
NVector<T,Dim>& NVector<T,Dim>::operator += (const NVector<T,Dim>& V)
{
for (std::size_t i=0; i<m_N*Dim; i++)
m_pV[i] += V.m_pV[i];
return *this;
}
template<class T,int Dim>
NVector<T,Dim> NVector<T,Dim>::operator - (const NVector<T,Dim>& V0) const
{
NVector<T,Dim> V(m_N);
for (std::size_t i=0; i<m_N*Dim; i++)
V.m_pV[i] = m_pV[i] - V0.m_pV[i];
return V;
}
template<class T,int Dim>
NVector<T,Dim> NVector<T,Dim>::operator - (void) const
{
NVector<T,Dim> V(m_N);
for (std::size_t i=0; i<m_N*Dim; i++)
V.m_pV[i] = -m_pV[i];
return V;
}
template<class T,int Dim>
NVector<T,Dim>& NVector<T,Dim>::operator -= (const NVector<T,Dim>& V)
{
for (std::size_t i=0; i<m_N*Dim; i++)
m_pV[i] -= V.m_pV[i];
return *this;
}
template<class T,int Dim>
T NVector<T,Dim>::Norm( std::size_t Ln ) const
{
T N = T();
for (std::size_t i = 0; i<m_N*Dim; i++)
N += pow(m_pV[i], (T)Ln);
return pow(N, (T)1.0/Ln);
}
template<class T,int Dim>
void NVector<T,Dim>::Normalize()
{
T N = 1.0f/Norm(2);
for (std::size_t i = 0; i<m_N*3; i++)
m_pV[i] *= N;
}
template<class T,int Dim>
T NVector<T,Dim>::Length() const
{
T N = T();
for (std::size_t i = 0; i<m_N*Dim; i++)
N += m_pV[i]*m_pV[i];
return sqrt(N);
}
template<class T,int Dim>
T NVector<T,Dim>::Dot( const NVector<T,Dim>& V ) const
{
T V0 = T();
for (std::size_t i=0; i<m_N*Dim; i++)
V0 += m_pV[i]*V.m_pV[i];
return V0;
}
}
}
#endif
pcl::poisson::Vector::Normalize
void Normalize()
Definition: vector.hpp:224
pcl
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
pcl::poisson::Vector::operator*
Vector operator*(const T &A) const
Definition: vector.hpp:116
pcl::poisson::NVector::SetZero
void SetZero()
Definition: vector.hpp:337
pcl::poisson::Vector::Add
static void Add(const Vector &V1, const T &scale1, const Vector &V2, const T &scale2, Vector &Out)
Definition: vector.hpp:171
pcl::poisson::NVector::Dimensions
std::size_t Dimensions() const
Definition: vector.hpp:335
pcl::poisson::NVector::operator+=
NVector & operator+=(const NVector &V)
Definition: vector.hpp:425
pcl::poisson::Vector::operator=
Vector & operator=(const Vector &V)
Definition: vector.hpp:84
pcl::poisson::Vector::operator()
const T & operator()(std::size_t i) const
Definition: vector.hpp:95
pcl::poisson::NVector::operator=
NVector & operator=(const NVector &V)
Definition: vector.hpp:328
pcl::poisson::NVector::operator+
NVector operator+(const NVector &V) const
Definition: vector.hpp:390
pcl::poisson::Vector
Definition: vector.h:41
pcl::poisson::Vector::operator[]
const T & operator[](std::size_t i) const
Definition: vector.hpp:106
pcl::poisson::Vector::~Vector
~Vector()
Definition: vector.hpp:82
pcl::poisson::NVector::Resize
void Resize(std::size_t N)
Definition: vector.hpp:309
pcl::poisson::NVector
Definition: vector.h:98
pcl::poisson::NVector::AddScaled
NVector & AddScaled(const NVector &V, const T &scale)
Definition: vector.hpp:399
pcl::poisson::Vector::operator+
Vector operator+(const Vector &V) const
Definition: vector.hpp:146
pcl::read
void read(std::istream &stream, Type &value)
Function for reading data from a stream.
Definition: region_xy.h:46
pcl::poisson::Vector::read
bool read(FILE *fp)
Definition: vector.hpp:267
pcl::poisson::Vector::Vector
Vector()
Definition: vector.hpp:42
pcl::poisson::NVector::Length
T Length() const
Definition: vector.hpp:475
pcl::poisson::NVector::Normalize
void Normalize()
Definition: vector.hpp:468
pcl::poisson::Vector::m_N
std::size_t m_N
Definition: vector.h:93
pcl::poisson::NVector::Norm
T Norm(std::size_t Ln) const
Definition: vector.hpp:460
pcl::poisson::Vector::Length
T Length() const
Definition: vector.hpp:231
pcl::poisson::Vector::operator/
Vector operator/(const T &A) const
Definition: vector.hpp:131
pcl::poisson::Vector::m_pV
T * m_pV
Definition: vector.h:91
pcl::poisson::Vector::Dimensions
std::size_t Dimensions() const
Definition: vector.hpp:91
pcl::poisson::Vector::Norm
T Norm(std::size_t Ln) const
Definition: vector.hpp:216
pcl::poisson::NVector::m_N
std::size_t m_N
Definition: vector.h:145
pcl::poisson::Vector::operator/=
Vector & operator/=(const T &A)
Definition: vector.hpp:139
pcl::poisson::NVector::SubtractScaled
NVector & SubtractScaled(const NVector &V, const T &scale)
Definition: vector.hpp:407
pcl::poisson::NVector::operator-=
NVector & operator-=(const NVector &V)
Definition: vector.hpp:452
pcl::poisson::NVector::m_pV
T * m_pV
Definition: vector.h:143
pcl::write
void write(std::ostream &stream, Type value)
Function for writing data to a stream.
Definition: region_xy.h:63
pcl::poisson::Vector::Dot
T Dot(const Vector &V) const
Definition: vector.hpp:239
pcl::poisson::NVector::Dot
T Dot(const NVector &V) const
Definition: vector.hpp:483
pcl::poisson::NVector::operator/=
NVector & operator/=(const T &A)
Definition: vector.hpp:383
pcl::poisson::NVector::operator-
NVector operator-() const
Definition: vector.hpp:442
pcl::poisson::Vector::AddScaled
Vector & AddScaled(const Vector &V, const T &scale)
Definition: vector.hpp:155
pcl::poisson::NVector::operator*
NVector operator*(const T &A) const
Definition: vector.hpp:360
pcl::poisson::NVector::NVector
NVector()
Definition: vector.hpp:288
pcl::poisson::Vector::operator*=
Vector & operator*=(const T &A)
Definition: vector.hpp:124
pcl::poisson::NVector::operator()
const T * operator()(std::size_t i) const
Definition: vector.hpp:339
pcl::poisson::Vector::SetZero
void SetZero()
Definition: vector.hpp:93
pcl::poisson::Vector::Resize
void Resize(std::size_t N)
Definition: vector.hpp:63
pcl::poisson::NVector::operator[]
const T * operator[](std::size_t i) const
Definition: vector.hpp:350
pcl::poisson::NVector::~NVector
~NVector()
Definition: vector.hpp:326
pcl::poisson::Vector::operator-=
Vector & operator-=(const Vector &V)
Definition: vector.hpp:208
pcl::poisson::NVector::operator/
NVector operator/(const T &A) const
Definition: vector.hpp:375
pcl::poisson::Vector::write
bool write(FILE *fp) const
Definition: vector.hpp:276
pcl::poisson::NVector::Add
static void Add(const NVector &V1, const T &scale1, const NVector &V2, const T &scale2, NVector &Out)
Definition: vector.hpp:415
pcl::poisson::Vector::operator-
Vector operator-() const
Definition: vector.hpp:198
pcl::poisson::Vector::SubtractScaled
Vector & SubtractScaled(const Vector &V, const T &scale)
Definition: vector.hpp:163
pcl::poisson::NVector::operator*=
NVector & operator*=(const T &A)
Definition: vector.hpp:368
pcl::poisson::Vector::operator+=
Vector & operator+=(const Vector &V)
Definition: vector.hpp:181