Geogram  Version 1.9.1-rc
A programming library of geometric algorithms
GEO::vecng< DIM, T > Class Template Reference

Generic maths vector. More...

#include <geogram/basic/vecg.h>

Public Types

typedef vecng< DIM, T > vector_type
 This vector type.
 
typedef T value_type
 The type of the vector coordinates.
 

Public Member Functions

 vecng ()
 Default vector constructor. More...
 
template<class T2 >
 vecng (const vecng< DIM, T2 > &v)
 Constructs a vector by copy. More...
 
template<class T2 , index_t DIM2>
 vecng (const vecng< DIM2, T2 > &v)
 
template<class T2 >
 vecng (const T2 *v)
 Constructs a vector from an array. More...
 
 vecng (const std::initializer_list< T > &Vi)
 Constructs a vector from an initializer list. More...
 
index_t dimension () const
 Gets the vector dimension. More...
 
T * data ()
 Gets modifiable vector data. More...
 
const T * data () const
 Gets non-modifiable vector data. More...
 
T & operator[] (index_t i)
 Gets a modifiable vector coordinate. More...
 
const T & operator[] (index_t i) const
 Gets a non-modifiable vector coordinate. More...
 
length2 () const
 Gets the squared length of the vector.
 
length () const
 Gets the length of the vector.
 
distance2 (const vector_type &v) const
 Gets the squared distance to a vector. More...
 
distance (const vector_type &v) const
 Gets the distance to a vector. More...
 
vector_typeoperator+= (const vector_type &v)
 Adds a vector in place. More...
 
vector_typeoperator-= (const vector_type &v)
 Subtracts a vector in place. More...
 
template<class T2 >
vector_typeoperator*= (T2 s)
 Multiplies by a scalar in place. More...
 
template<class T2 >
vector_typeoperator/= (T2 s)
 Divides by a scalar in place. More...
 
vector_type operator+ (const vector_type &v) const
 Adds 2 vectors. More...
 
vector_type operator- (const vector_type &v) const
 Subtracts 2 vectors. More...
 
template<class T2 >
vector_type operator* (T2 s) const
 Multiplies a vector by a scalar. More...
 
template<class T2 >
vector_type operator/ (T2 s) const
 Divides a vector by a scalar. More...
 
vector_type operator- () const
 Negates a vector. More...
 

Static Public Attributes

static const index_t dim = DIM
 The dimension of the vector.
 

Related Functions

(Note that these are not member functions.)

template<index_t DIM, class T >
dot (const vecng< DIM, T > &v1, const vecng< DIM, T > &v2)
 Computes the dot product of 2 vectors. More...
 
template<class T2 , index_t DIM, class T >
vecng< DIM, T > operator* (T2 s, const vecng< DIM, T > &v)
 Multiplies a scalar by a vector. More...
 
template<index_t DIM, class T >
length (const vecng< DIM, T > &v)
 Gets the norm of a vector. More...
 
template<index_t DIM, class T >
length2 (const vecng< DIM, T > &v)
 Gets the square norm of a vector. More...
 
template<index_t DIM, class T >
distance2 (const vecng< DIM, T > &v1, const vecng< DIM, T > &v2)
 Gets the square distance between 2 vectors. More...
 
template<index_t DIM, class T >
distance (const vecng< DIM, T > &v1, const vecng< DIM, T > &v2)
 Gets the distance between 2 vectors. More...
 
template<index_t DIM, class T >
vecng< DIM, T > normalize (const vecng< DIM, T > &v)
 Normalizes a vector. More...
 
template<index_t DIM, class T >
vecng< DIM, T > mix (const vecng< DIM, T > &v1, const vecng< DIM, T > &v2, T s)
 Computes a weighted barycenter. More...
 
template<class T >
dot (const vecng< 2, T > &v1, const vecng< 2, T > &v2)
 Computes the dot product of 2 vectors. More...
 
template<class T >
det (const vecng< 2, T > &v1, const vecng< 2, T > &v2)
 Computes the determinant of 2 vectors. More...
 
template<class T2 , class T >
vecng< 2, T > operator* (T2 s, const vecng< 2, T > &v)
 Multiplies a scalar by a vector. More...
 
template<class T >
dot (const vecng< 3, T > &v1, const vecng< 3, T > &v2)
 Computes the dot product of 2 vectors. More...
 
template<class T >
vecng< 3, T > cross (const vecng< 3, T > &v1, const vecng< 3, T > &v2)
 Computes the cross product of 2 vectors. More...
 
template<class T2 , class T >
vecng< 3, T > operator* (T2 s, const vecng< 3, T > &v)
 Multiplies a scalar by a vector. More...
 
template<class T >
dot (const vecng< 4, T > &v1, const vecng< 4, T > &v2)
 Computes the dot product of 2 vectors. More...
 
template<class T2 , class T >
vecng< 4, T > operator* (T2 s, const vecng< 4, T > &v)
 Multiplies a scalar by a vector. More...
 
template<index_t DIM, class T >
std::ostream & operator<< (std::ostream &out, const GEO::vecng< DIM, T > &v)
 Writes a vector to a stream. More...
 
template<index_t DIM, class T >
std::istream & operator>> (std::istream &in, GEO::vecng< DIM, T > &v)
 Reads a vector from a stream. More...
 

Detailed Description

template<index_t DIM, class T>
class GEO::vecng< DIM, T >

Generic maths vector.

Vecng implements a maths vector of dimension DIM containing coordinates of type T and provides operations for manipulating it. Type T is expected to be a numeric type.

Template Parameters
DIMdimension of the vector
Ttype of the vector coordinates.

Definition at line 70 of file vecg.h.

Constructor & Destructor Documentation

◆ vecng() [1/4]

template<index_t DIM, class T >
GEO::vecng< DIM, T >::vecng ( )
inline

Default vector constructor.

All coordinates are initialized to 0 (zero).

Definition at line 85 of file vecg.h.

◆ vecng() [2/4]

template<index_t DIM, class T >
template<class T2 >
GEO::vecng< DIM, T >::vecng ( const vecng< DIM, T2 > &  v)
inlineexplicit

Constructs a vector by copy.

This copies coordinates of vector v to this vector. The type T2 of the coordinates in v must be convertible to the type T of this vector.

Parameters
[in]van vector of same dimension with coordinates of type T2
Template Parameters
T2the type of coordinates in vector v

Definition at line 104 of file vecg.h.

◆ vecng() [3/4]

template<index_t DIM, class T >
template<class T2 >
GEO::vecng< DIM, T >::vecng ( const T2 *  v)
inlineexplicit

Constructs a vector from an array.

This copies coordinates the first DIM coordinates of array v to this vector. The type T2 of the coordinates in v must be convertible to the type T of this vector.

Parameters
[in]van array of values of type T2
Template Parameters
T2the type of coordinates in vector v

Definition at line 130 of file vecg.h.

◆ vecng() [4/4]

template<index_t DIM, class T >
GEO::vecng< DIM, T >::vecng ( const std::initializer_list< T > &  Vi)
inline

Constructs a vector from an initializer list.

Parameters
[in]Vithe initializer list, should contain DIM elements.

Definition at line 140 of file vecg.h.

Member Function Documentation

◆ data() [1/2]

template<index_t DIM, class T >
T* GEO::vecng< DIM, T >::data ( )
inline

Gets modifiable vector data.

Returns
a pointer to the first element of the vector

Definition at line 161 of file vecg.h.

◆ data() [2/2]

template<index_t DIM, class T >
const T* GEO::vecng< DIM, T >::data ( ) const
inline

Gets non-modifiable vector data.

Returns
a const pointer to the first element of the vector

Definition at line 169 of file vecg.h.

◆ dimension()

template<index_t DIM, class T >
index_t GEO::vecng< DIM, T >::dimension ( ) const
inline

Gets the vector dimension.

Returns
the value of DIM

Definition at line 153 of file vecg.h.

◆ distance()

template<index_t DIM, class T >
T GEO::vecng< DIM, T >::distance ( const vector_type v) const
inline

Gets the distance to a vector.

Parameters
[in]vanother vector
Returns
(v - this).length()

Definition at line 229 of file vecg.h.

◆ distance2()

template<index_t DIM, class T >
T GEO::vecng< DIM, T >::distance2 ( const vector_type v) const
inline

Gets the squared distance to a vector.

Parameters
[in]vanother vector
Returns
(v - this).length2()

Definition at line 216 of file vecg.h.

◆ operator*()

template<index_t DIM, class T >
template<class T2 >
vector_type GEO::vecng< DIM, T >::operator* ( T2  s) const
inline

Multiplies a vector by a scalar.

Builds a vector by multipying this vector coordinates by value s. The type T2 of s must be convertible to the type T of this vector coordinates.

Parameters
[in]sa value of type T2
Template Parameters
T2the type of value s
Returns
the result vector (this * s)

Definition at line 337 of file vecg.h.

◆ operator*=()

template<index_t DIM, class T >
template<class T2 >
vector_type& GEO::vecng< DIM, T >::operator*= ( T2  s)
inline

Multiplies by a scalar in place.

Multiplies this vector coordinates by value s. The type T2 of s must be convertible to the type T of this vector coordinates.

Parameters
[in]sa value of type T2
Template Parameters
T2the type of value s
Returns
a reference to this vector

Definition at line 273 of file vecg.h.

◆ operator+()

template<index_t DIM, class T >
vector_type GEO::vecng< DIM, T >::operator+ ( const vector_type v) const
inline

Adds 2 vectors.

Builds a vector by adding coordinates of this vector and coordinates of vector v.

Parameters
[in]vanother vector
Returns
the result vector (this + v)

Definition at line 304 of file vecg.h.

◆ operator+=()

template<index_t DIM, class T >
vector_type& GEO::vecng< DIM, T >::operator+= ( const vector_type v)
inline

Adds a vector in place.

Adds coordinates of vector v to this vector coordinates

Parameters
[in]vanother vector
Returns
a reference to this vector

Definition at line 242 of file vecg.h.

◆ operator-() [1/2]

template<index_t DIM, class T >
vector_type GEO::vecng< DIM, T >::operator- ( ) const
inline

Negates a vector.

Builds a vector by negating coordinates of this vector.

Returns
the result vector (-this)

Definition at line 368 of file vecg.h.

◆ operator-() [2/2]

template<index_t DIM, class T >
vector_type GEO::vecng< DIM, T >::operator- ( const vector_type v) const
inline

Subtracts 2 vectors.

Builds a vector by subtracting coordinates of vector v to coordinates of this vector.

Parameters
[in]vanother vector
Returns
the result vector (this - v)

Definition at line 319 of file vecg.h.

◆ operator-=()

template<index_t DIM, class T >
vector_type& GEO::vecng< DIM, T >::operator-= ( const vector_type v)
inline

Subtracts a vector in place.

Subtracts coordinates of vector v from this vector coordinates

Parameters
[in]vanother vector
Returns
a reference to this vector

Definition at line 256 of file vecg.h.

◆ operator/()

template<index_t DIM, class T >
template<class T2 >
vector_type GEO::vecng< DIM, T >::operator/ ( T2  s) const
inline

Divides a vector by a scalar.

Builds a vector by dividing this vector coordinates by value s. The type T2 of s must be convertible to the type T of this vector coordinates.

Parameters
[in]sa value of type T2
Template Parameters
T2the type of value s
Returns
the result vector (this / s)

Definition at line 355 of file vecg.h.

◆ operator/=()

template<index_t DIM, class T >
template<class T2 >
vector_type& GEO::vecng< DIM, T >::operator/= ( T2  s)
inline

Divides by a scalar in place.

Divides this vector coordinates by value s. The type T2 of s must be convertible to the type T of this vector coordinates.

Parameters
[in]sa value of type T2
Template Parameters
T2the type of value s
Returns
a reference to this vector

Definition at line 290 of file vecg.h.

◆ operator[]() [1/2]

template<index_t DIM, class T >
T& GEO::vecng< DIM, T >::operator[] ( index_t  i)
inline

Gets a modifiable vector coordinate.

Parameters
[in]iindex of the coordinate
Returns
a reference to coordinate at index i

Definition at line 178 of file vecg.h.

◆ operator[]() [2/2]

template<index_t DIM, class T >
const T& GEO::vecng< DIM, T >::operator[] ( index_t  i) const
inline

Gets a non-modifiable vector coordinate.

Parameters
[in]iindex of the coordinate
Returns
a const reference to coordinate at index i

Definition at line 188 of file vecg.h.

Friends And Related Function Documentation

◆ cross()

template<class T >
vecng< 3, T > cross ( const vecng< 3, T > &  v1,
const vecng< 3, T > &  v2 
)
related

Computes the cross product of 2 vectors.

Parameters
[in]v1the first vector
[in]v2the second vector
Returns
the cross product (v1 x v2)

Definition at line 930 of file vecg.h.

◆ det()

template<class T >
T det ( const vecng< 2, T > &  v1,
const vecng< 2, T > &  v2 
)
related

Computes the determinant of 2 vectors.

Parameters
[in]v1the first vector
[in]v2the second vector
Returns
the value of the determinant

Definition at line 707 of file vecg.h.

◆ distance()

template<index_t DIM, class T >
T distance ( const vecng< DIM, T > &  v1,
const vecng< DIM, T > &  v2 
)
related

Gets the distance between 2 vectors.

Parameters
[in]v1the first vector
[in]v2the second vector
Returns
the distance between v1 and v2.
See also
vecng::distance()

Definition at line 470 of file vecg.h.

◆ distance2()

template<index_t DIM, class T >
T distance2 ( const vecng< DIM, T > &  v1,
const vecng< DIM, T > &  v2 
)
related

Gets the square distance between 2 vectors.

Parameters
[in]v1the first vector
[in]v2the second vector
Returns
the square distance between v1 and v2.
See also
vecng::distance2()

Definition at line 455 of file vecg.h.

◆ dot() [1/4]

template<class T >
T dot ( const vecng< 2, T > &  v1,
const vecng< 2, T > &  v2 
)
related

Computes the dot product of 2 vectors.

Parameters
[in]v1the first vector
[in]v2the second vector
Returns
the dot product (v1 . v2)

Definition at line 693 of file vecg.h.

◆ dot() [2/4]

template<class T >
T dot ( const vecng< 3, T > &  v1,
const vecng< 3, T > &  v2 
)
related

Computes the dot product of 2 vectors.

Parameters
[in]v1the first vector
[in]v2the second vector
Returns
the dot product (v1 . v2)

Definition at line 916 of file vecg.h.

◆ dot() [3/4]

template<class T >
T dot ( const vecng< 4, T > &  v1,
const vecng< 4, T > &  v2 
)
related

Computes the dot product of 2 vectors.

Parameters
[in]v1the first vector
[in]v2the second vector
Returns
the dot product (v1 . v2)

Definition at line 1147 of file vecg.h.

◆ dot() [4/4]

template<index_t DIM, class T >
T dot ( const vecng< DIM, T > &  v1,
const vecng< DIM, T > &  v2 
)
related

Computes the dot product of 2 vectors.

Parameters
[in]v1the first vector
[in]v2the second vector
Returns
the dot product (v1 . v2)

Definition at line 388 of file vecg.h.

◆ length()

template<index_t DIM, class T >
T length ( const vecng< DIM, T > &  v)
related

Gets the norm of a vector.

Parameters
[in]va vector
Returns
the norm of vector v
See also
vecng::length()

Definition at line 430 of file vecg.h.

◆ length2()

template<index_t DIM, class T >
T length2 ( const vecng< DIM, T > &  v)
related

Gets the square norm of a vector.

Parameters
[in]va vector
Returns
the square norm of vector v
See also
vecng::length2()

Definition at line 442 of file vecg.h.

◆ mix()

template<index_t DIM, class T >
vecng< DIM, T > mix ( const vecng< DIM, T > &  v1,
const vecng< DIM, T > &  v2,
s 
)
related

Computes a weighted barycenter.

Computes the barycenter of v1 and v2 weighted by value 1 - s and s.

Parameters
[in]v1the first vector
[in]v2the second vector
[in]sthe weight value
Returns
the barycenter (1 - s) * v1 + s * v2

Definition at line 507 of file vecg.h.

◆ normalize()

template<index_t DIM, class T >
vecng< DIM, T > normalize ( const vecng< DIM, T > &  v)
related

Normalizes a vector.

Returns a normalized vector constructed by dividing coordinates of vector v by it norm. If the norm is 0, then the result is undefined.

Parameters
[in]va vector
Returns
the normalized vector

Definition at line 486 of file vecg.h.

◆ operator*() [1/4]

template<class T2 , class T >
vecng< 2, T > operator* ( T2  s,
const vecng< 2, T > &  v 
)
related

Multiplies a scalar by a vector.

Builds a vector by multipying this vector coordinates by value s. The type T2 of s must be convertible to the type T of this vector coordinates.

Parameters
[in]sa value of type T2
[in]vthe vector to multiply
Template Parameters
T2the type of value s
Returns
the result vector (s * v)

Definition at line 718 of file vecg.h.

◆ operator*() [2/4]

template<class T2 , class T >
vecng< 3, T > operator* ( T2  s,
const vecng< 3, T > &  v 
)
related

Multiplies a scalar by a vector.

Builds a vector by multipying this vector coordinates by value s. The type T2 of s must be convertible to the type T of this vector coordinates.

Parameters
[in]sa value of type T2
[in]vthe vector to multiply
Template Parameters
T2the type of value s
Returns
the result vector (s * v)

Definition at line 945 of file vecg.h.

◆ operator*() [3/4]

template<class T2 , class T >
vecng< 4, T > operator* ( T2  s,
const vecng< 4, T > &  v 
)
related

Multiplies a scalar by a vector.

Builds a vector by multipying this vector coordinates by value s. The type T2 of s must be convertible to the type T of this vector coordinates.

Parameters
[in]sa value of type T2
[in]vthe vector to multiply
Template Parameters
T2the type of value s
Returns
the result vector (s * v)

Definition at line 1158 of file vecg.h.

◆ operator*() [4/4]

template<class T2 , index_t DIM, class T >
vecng< DIM, T > operator* ( T2  s,
const vecng< DIM, T > &  v 
)
related

Multiplies a scalar by a vector.

Builds a vector by multipying this vector coordinates by value s. The type T2 of s must be convertible to the type T of this vector coordinates.

Parameters
[in]sa value of type T2
[in]vthe vector to multiply
Template Parameters
T2the type of value s
Returns
the result vector (s * v)

Definition at line 410 of file vecg.h.

◆ operator<<()

template<index_t DIM, class T >
std::ostream & operator<< ( std::ostream &  out,
const GEO::vecng< DIM, T > &  v 
)
related

Writes a vector to a stream.

This writes the coordinates of vector v separated by a space character to the output stream out.

Parameters
[in]outthe output stream
[in]vthe vector to write
Returns
a reference to the output stream out

Definition at line 1174 of file vecg.h.

◆ operator>>()

template<index_t DIM, class T >
std::istream & operator>> ( std::istream &  in,
GEO::vecng< DIM, T > &  v 
)
related

Reads a vector from a stream.

This reads DIM coordinates from the input stream in and stores them in vector v. Understands both "x y z" and "[x, y, z]" formats.

Parameters
[in]inthe input stream
[out]vthe vector to read
Returns
a reference to the input stream in

Definition at line 1196 of file vecg.h.


The documentation for this class was generated from the following file: