SPH
Classes | Typedefs | Enumerations | Functions | Variables
Vector.h File Reference

Basic vector algebra. Computations are accelerated using SIMD. More...

#include "common/Globals.h"
#include "objects/containers/Tuple.h"
#include "objects/geometry/Generic.h"
#include "objects/wrappers/Interval.h"
#include <immintrin.h>
#include <iomanip>
#include <smmintrin.h>

Go to the source code of this file.

Classes

struct  IsVectorType< T >
 Helper type trait to determine if the type is a vector of some kind. More...
 
struct  IsVectorType< BasicVector< T > >
 
class  BasicVector< float >
 3-dimensional vector, float precision More...
 
class  BasicVector< double >
 specialization for doubles or units of double precision More...
 
struct  SphericalCoords
 

Typedefs

using Vector = BasicVector< Float >
 

Enumerations

enum  Coordinate { X = 0 , Y = 1 , Z = 2 , H = 3 }
 Components of the 4D vector. More...
 

Functions

INLINE float dot (const BasicVector< float > &v1, const BasicVector< float > &v2)
 Make sure the vector is trivially constructible and destructible, needed for fast initialization of arrays. More...
 
INLINE double dot (const BasicVector< double > &v1, const BasicVector< double > &v2)
 
INLINE BasicVector< float > cross (const BasicVector< float > &v1, const BasicVector< float > &v2)
 Cross product between two vectors. More...
 
INLINE BasicVector< double > cross (const BasicVector< double > &v1, const BasicVector< double > &v2)
 
INLINE Float getSqrLength (const Vector &v)
 
INLINE Float getLength (const Vector &v)
 Returns the length of the vector. Enabled only for vectors of floating-point precision. More...
 
INLINE Float getLengthApprox (const Vector &v)
 Returns approximate value of the length. Enabled only for vectors of floating-point precision. More...
 
INLINE Vector getNormalized (const Vector &v)
 
INLINE Tuple< Vector, FloatgetNormalizedWithLength (const Vector &v)
 Returns normalized vector and length of the input vector as tuple. More...
 
template<>
INLINE BasicVector< float > min (const BasicVector< float > &v1, const BasicVector< float > &v2)
 Component-wise minimum. More...
 
template<>
INLINE BasicVector< float > max (const BasicVector< float > &v1, const BasicVector< float > &v2)
 Component-wise maximum. More...
 
template<>
INLINE BasicVector< double > min (const BasicVector< double > &v1, const BasicVector< double > &v2)
 
template<>
INLINE BasicVector< double > max (const BasicVector< double > &v1, const BasicVector< double > &v2)
 
template<>
INLINE Vector clamp (const Vector &v, const Vector &v1, const Vector &v2)
 Component-wise clamping. More...
 
template<>
INLINE Vector clamp (const Vector &v, const Interval &range)
 Clamping all components by range. More...
 
INLINE bool almostEqual (const Vector &v1, const Vector &v2, const Float eps=EPS)
 Checks if two vectors are equal to some given accuracy. More...
 
template<>
INLINE Float norm (const Vector &v)
 Returns norm of a vector, i.e. its (approximative) length. More...
 
template<>
INLINE Float normSqr (const Vector &v)
 Returns squared length of a vector. More...
 
template<>
INLINE Float minElement (const Vector &v)
 Returns minimum element of a vector. Considers only the first 3 component, 4th one is ignored. More...
 
template<>
INLINE Float maxElement (const Vector &v)
 Returns maximum element of a vector. Considers only the first 3 component, 4th one is ignored. More...
 
INLINE Size argMin (const Vector &v)
 Returns the index of the minimum element. More...
 
INLINE Size argMax (const Vector &v)
 Returns the index of the maximum element. More...
 
template<>
INLINE auto abs (const BasicVector< float > &v)
 Computes vector of absolute values. More...
 
template<>
INLINE auto abs (const BasicVector< double > &v)
 
INLINE Float l1Norm (const Vector &v)
 Returns the L1 norm (sum of absolute values) of the vector. More...
 
template<>
INLINE bool isReal (const BasicVector< float > &v)
 Computes vector of inverse squared roots. More...
 
template<>
INLINE bool isReal (const BasicVector< double > &v)
 
template<>
INLINE auto less (const Vector &v1, const Vector &v2)
 
template<>
INLINE StaticArray< Float, 6 > getComponents< Vector > (const Vector &v)
 
template<typename T1 , typename T2 >
INLINE BasicVector< T1 > vectorCast (const BasicVector< T2 > &v)
 
template<>
INLINE BasicVector< FloatvectorCast (const BasicVector< Float > &v)
 
INLINE Vector cos (const Vector &v)
 Cosine applied to all components of the vector. More...
 
INLINE Vector sphericalToCartesian (const Float r, const Float theta, const Float phi)
 Constructs a vector from spherical coordinates. More...
 
INLINE Vector cylindricalToCartesian (const Float r, const Float phi, const Float z)
 Constructs a vector from cylindrical coordinates. More...
 
INLINE SphericalCoords cartensianToSpherical (const Vector &v)
 Converts vector in cartesian coordinates to spherical coordinates. More...
 
INLINE Vector sphericalInversion (const Vector &v, const Vector &center, const Float radius)
 
INLINE Float distance (const Vector &r, const Vector &axis)
 Returns the distance of vector from given axis. The axis is assumed to be normalized. More...
 
INLINE bool lexicographicalLess (const Vector &v1, const Vector &v2)
 Compares components of two vectors lexicographically, primary component is z. More...
 

Variables

template<typename T >
constexpr bool IsVector = IsVectorType<T>::value
 

Detailed Description

Basic vector algebra. Computations are accelerated using SIMD.

Author
Pavel Sevecek (sevecek at sirrah.troja.mff.cuni.cz)
Date
2016-2021

Definition in file Vector.h.

Typedef Documentation

◆ Vector

Definition at line 539 of file Vector.h.

Enumeration Type Documentation

◆ Coordinate

enum Coordinate

Components of the 4D vector.

First 3 are simply cartesian coordinates, the 4th one is the smoothing length

Enumerator

Definition at line 21 of file Vector.h.

Function Documentation

◆ abs() [1/2]

template<>
INLINE auto abs ( const BasicVector< double > &  v)

Definition at line 722 of file Vector.h.

◆ abs() [2/2]

template<>
INLINE auto abs ( const BasicVector< float > &  v)

Computes vector of absolute values.

Definition at line 710 of file Vector.h.

◆ almostEqual()

INLINE bool almostEqual ( const Vector v1,
const Vector v2,
const Float  eps = EPS 
)

Checks if two vectors are equal to some given accuracy.

Definition at line 651 of file Vector.h.

◆ argMax()

INLINE Size argMax ( const Vector v)

Returns the index of the maximum element.

Definition at line 697 of file Vector.h.

◆ argMin()

INLINE Size argMin ( const Vector v)

Returns the index of the minimum element.

Definition at line 685 of file Vector.h.

◆ cartensianToSpherical()

INLINE SphericalCoords cartensianToSpherical ( const Vector v)

Converts vector in cartesian coordinates to spherical coordinates.

Definition at line 806 of file Vector.h.

◆ clamp() [1/2]

template<>
INLINE Vector clamp ( const Vector v,
const Interval range 
)

Clamping all components by range.

Definition at line 646 of file Vector.h.

◆ clamp() [2/2]

template<>
INLINE Vector clamp ( const Vector v,
const Vector v1,
const Vector v2 
)

Component-wise clamping.

Definition at line 640 of file Vector.h.

◆ cos()

INLINE Vector cos ( const Vector v)

Cosine applied to all components of the vector.

Todo:
return _mm_cos_ps(v.sse());

Definition at line 777 of file Vector.h.

◆ cross() [1/2]

INLINE BasicVector<double> cross ( const BasicVector< double > &  v1,
const BasicVector< double > &  v2 
)
Todo:
optimize

Definition at line 566 of file Vector.h.

◆ cross() [2/2]

INLINE BasicVector<float> cross ( const BasicVector< float > &  v1,
const BasicVector< float > &  v2 
)

Cross product between two vectors.

Definition at line 559 of file Vector.h.

◆ cylindricalToCartesian()

INLINE Vector cylindricalToCartesian ( const Float  r,
const Float  phi,
const Float  z 
)

Constructs a vector from cylindrical coordinates.

Definition at line 795 of file Vector.h.

◆ distance()

INLINE Float distance ( const Vector r,
const Vector axis 
)

Returns the distance of vector from given axis. The axis is assumed to be normalized.

Definition at line 827 of file Vector.h.

◆ dot() [1/2]

INLINE double dot ( const BasicVector< double > &  v1,
const BasicVector< double > &  v2 
)
Todo:
optimize

Definition at line 553 of file Vector.h.

◆ dot() [2/2]

INLINE float dot ( const BasicVector< float > &  v1,
const BasicVector< float > &  v2 
)

Make sure the vector is trivially constructible and destructible, needed for fast initialization of arrays.

Vector utils Generic dot product between two vectors.

Definition at line 548 of file Vector.h.

◆ getComponents< Vector >()

template<>
INLINE StaticArray<Float, 6> getComponents< Vector > ( const Vector v)

Definition at line 760 of file Vector.h.

◆ getLength()

INLINE Float getLength ( const Vector v)

Returns the length of the vector. Enabled only for vectors of floating-point precision.

Definition at line 579 of file Vector.h.

◆ getLengthApprox()

INLINE Float getLengthApprox ( const Vector v)

Returns approximate value of the length. Enabled only for vectors of floating-point precision.

Definition at line 584 of file Vector.h.

◆ getNormalized()

INLINE Vector getNormalized ( const Vector v)

Returns normalized vector. Throws assert if the vector has zero length. Enabled only for vectors of floating-point precision.

Definition at line 590 of file Vector.h.

◆ getNormalizedWithLength()

INLINE Tuple<Vector, Float> getNormalizedWithLength ( const Vector v)

Returns normalized vector and length of the input vector as tuple.

Definition at line 597 of file Vector.h.

◆ getSqrLength()

INLINE Float getSqrLength ( const Vector v)

Returns squared length of the vector. Faster than simply getting length as there is no need to compute square root.

Definition at line 574 of file Vector.h.

◆ isReal() [1/2]

template<>
INLINE bool isReal ( const BasicVector< double > &  v)
Todo:
optimize using SSE intrinsics

Definition at line 748 of file Vector.h.

◆ isReal() [2/2]

template<>
INLINE bool isReal ( const BasicVector< float > &  v)

Computes vector of inverse squared roots.

Todo:
optimize using SSE intrinsics

Definition at line 742 of file Vector.h.

◆ l1Norm()

INLINE Float l1Norm ( const Vector v)

Returns the L1 norm (sum of absolute values) of the vector.

Definition at line 729 of file Vector.h.

◆ less()

template<>
INLINE auto less ( const Vector v1,
const Vector v2 
)
Todo:
optimize

Definition at line 754 of file Vector.h.

◆ lexicographicalLess()

INLINE bool lexicographicalLess ( const Vector v1,
const Vector v2 
)

Compares components of two vectors lexicographically, primary component is z.

Definition at line 833 of file Vector.h.

◆ max() [1/2]

template<>
INLINE BasicVector<double> max ( const BasicVector< double > &  v1,
const BasicVector< double > &  v2 
)

Definition at line 632 of file Vector.h.

◆ max() [2/2]

template<>
INLINE BasicVector<float> max ( const BasicVector< float > &  v1,
const BasicVector< float > &  v2 
)

Component-wise maximum.

Definition at line 612 of file Vector.h.

◆ maxElement()

template<>
INLINE Float maxElement ( const Vector v)

Returns maximum element of a vector. Considers only the first 3 component, 4th one is ignored.

Definition at line 680 of file Vector.h.

◆ min() [1/2]

template<>
INLINE BasicVector<double> min ( const BasicVector< double > &  v1,
const BasicVector< double > &  v2 
)

Definition at line 627 of file Vector.h.

◆ min() [2/2]

template<>
INLINE BasicVector<float> min ( const BasicVector< float > &  v1,
const BasicVector< float > &  v2 
)

Component-wise minimum.

Definition at line 606 of file Vector.h.

◆ minElement()

template<>
INLINE Float minElement ( const Vector v)

Returns minimum element of a vector. Considers only the first 3 component, 4th one is ignored.

Todo:
possibly optimize with SSE? Only 3 components though ...

Definition at line 673 of file Vector.h.

◆ norm()

template<>
INLINE Float norm ( const Vector v)

Returns norm of a vector, i.e. its (approximative) length.

Definition at line 657 of file Vector.h.

◆ normSqr()

template<>
INLINE Float normSqr ( const Vector v)

Returns squared length of a vector.

Definition at line 665 of file Vector.h.

◆ sphericalInversion()

INLINE Vector sphericalInversion ( const Vector v,
const Vector center,
const Float  radius 
)

Computes a spherical inversion of a vector. Works in arbitrary number of dimensions.

Parameters
vVector to be inverted.
centerCenter of the spherical inversion.
radiusRadius of the spherical inversion. For vectors in radius from center, spherical inversion is an identity transform.

Definition at line 820 of file Vector.h.

◆ sphericalToCartesian()

INLINE Vector sphericalToCartesian ( const Float  r,
const Float  theta,
const Float  phi 
)

Constructs a vector from spherical coordinates.

The angle has generally different type to allow using units with dimensions.

Parameters
rRadius coordinate
thetaLatitude in radians, where 0 and PI correspond to poles.
phiLongitude in radians

Definition at line 788 of file Vector.h.

◆ vectorCast() [1/2]

template<>
INLINE BasicVector<Float> vectorCast ( const BasicVector< Float > &  v)

Definition at line 772 of file Vector.h.

◆ vectorCast() [2/2]

template<typename T1 , typename T2 >
INLINE BasicVector<T1> vectorCast ( const BasicVector< T2 > &  v)

Casts vector components to another precision. Casting vectors is only possible by using this function to avoid problems with contructor/conversion operator ambiguity.

Definition at line 768 of file Vector.h.

Variable Documentation

◆ IsVector

template<typename T >
constexpr bool IsVector = IsVectorType<T>::value
constexpr

Definition at line 44 of file Vector.h.