/*! \file pimathvector.h
* \brief PIMathVector
*
* This file declare math vector class, which performs various vector operations
*/
/*
PIP - Platform Independent Primitives
PIMathVector
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see .
*/
#ifndef PIMATHVECTOR_H
#define PIMATHVECTOR_H
#include "pimathbase.h"
template
class PIMathMatrixT;
/// Vector templated
#define PIMV_FOR(v, s) for (uint v = s; v < Size; ++v)
//! \brief A class that works with vector operations, the input data of which are size and the data type of the vector
//! @tparam Size number of matrix elements
//! @tparam Type is the data type of the vector. There are can be basic C++ language data and different classes where the arithmetic operators(=, +=, -=, *=, /=, ==, !=, +, -, *, /)
//! of the C++ language are implemented
template
class PIP_EXPORT PIMathVectorT {
typedef PIMathVectorT _CVector;
static_assert(std::is_arithmetic::value, "Type must be arithmetic");
public:
/**
* @brief Constructor that calls the private resize method
*
*/
PIMathVectorT() {resize();}
/**
* @brief Constructor that fills a vector PIMathVectorT with the values of another vector "PIVector"
*
* @param val vector of type PIVector which is identified PIMathVectorT
*/
PIMathVectorT(const PIVector & val) {resize(); PIMV_FOR(i, 0) c[i] = val[i];}
/**
* @brief Constructor that fills a vector PIMathVectorT with the subtraction of two vectors
*
* @param st vector of type PIMathVectorT
* @param fn vector of type PIMathVectorT
*/
PIMathVectorT(const _CVector & st, const _CVector & fn) {resize(); set(st, fn);}
/**
* @brief Method that returns the number of elements contained in the vector
*
* @return type uint shows number of elements in this vector
*/
uint size() const {return Size;}
/**
* @brief Method that set this elements to value "v"
*
* @param v value of which the vector is filled
* @return reference to this
*/
_CVector & fill(const Type & v) {PIMV_FOR(i, 0) c[i] = v; return *this;}
/**
* @brief Method that set this with the subtraction of two vectors
*
* @param st vector of type PIMathVectorT
* @param fn vector of type PIMathVectorT
* @return reference to this
*/
_CVector & set(const _CVector & st, const _CVector & fn) {PIMV_FOR(i, 0) c[i] = fn[i] - st[i]; return *this;}
/**
* @brief Method that sets this using a vector, each element of which is added to the value of "v"
*
* @param v value of which the vector is filled
* @return reference to this
*/
_CVector & move(const Type & v) {PIMV_FOR(i, 0) c[i] += v; return *this;}
/**
* @brief Method that sets this with a vector, each element of which is added to each element of the vector "v"
*
* @param v vector of type PIMathVectorT
* @return reference to this
*/
_CVector & move(const _CVector & v) {PIMV_FOR(i, 0) c[i] += v[i]; return *this;}
/**
* @brief Method that returns sum of the squares of all elements of the vector
*
* @return value equal to the sum of the squares of all elements of the vector
*/
Type lengthSqr() const {Type tv(0); PIMV_FOR(i, 0) tv += (c[i] * c[i]); return tv;}
/**
* @brief Method that returns a scalar physical value equal to the absolute value of vector
*
* @return value equal to length of a vector
*/
Type length() const {return sqrt(lengthSqr());}
/**
* @brief Method that returns the sum of the absolute values of all vector values
*
* @return value equal sum of the absolute values of all vector values
*/
Type manhattanLength() const {Type tv(0); PIMV_FOR(i, 0) tv += fabs(c[i]); return tv;}
/**
* @brief Method that returns the cos of the current vector and vector "v"
*
* @param v vector of type PIMathVectorT
* @return cos value of the angle between two vectors
*/
Type angleCos(const _CVector & v) const {Type tv = v.length() * length(); return (tv == Type(0) ? Type(0) : ((*this) ^ v) / tv);}
/**
* @brief Method that returns the sin of the current vector and vector "v". Works only with vectors which consists of 3 elements
*
* @param v vector of type PIMathVectorT
* @return sin value of the angle between two vector
*/
Type angleSin(const _CVector & v) const {Type tv = angleCos(v); return sqrt(Type(1) - tv * tv);}
/**
* @brief Method that returns the angle between of the current vector and vector "v" in Rad
*
* @param v vector of type PIMathVectorT
* @return value of the angle between two vectors in Rad
*/
Type angleRad(const _CVector & v) const {return acos(angleCos(v));}
/**
* @brief Method that returns the angle between of the current vector and vector "v" in Deg
*
* @param v vector of type PIMathVectorT
* @return value of the angle between two vectors in Deg
*/
Type angleDeg(const _CVector & v) const {return toDeg(acos(angleCos(v)));}
/**
* @brief Method that returns the angle elevation between of the current vector and vector "v" in Deg
*
* @param v vector of type PIMathVectorT
* @return value of the angle elevation between two vectors in Deg
*/
Type angleElevation(const _CVector & v) const {_CVector z = v - *this; double c = z.angleCos(*this); return 90.0 - acos(c) * rad2deg;}
/**
* @brief Method that returns a vector equal to the projection of the current vector onto the vector "v".
*
* @param v vector of type PIMathVectorT
* @return vector of type PIMathVectorT equal to the projection of the current vector onto the vector "v"
*/
_CVector projection(const _CVector & v) {Type tv = v.length(); return (tv == Type(0) ? _CVector() : v * (((*this) ^ v) / tv));}
/**
* @brief Method that returns this normalized vector (each element of a vector is divided by the absolute value of this vector)
*
* @return reference to this
*/
_CVector & normalize() {Type tv = length(); if (tv == Type(1)) return *this; if (piAbs(tv) <= Type(1E-100)) {fill(Type(0)); return *this;} PIMV_FOR(i, 0) c[i] /= tv; return *this;}
/**
* @brief Method that returns a normalized vector (each element of a vector is divided by the absolute value of this vector)
*
* @return normalized vector of type PIMathVectorT
*/
_CVector normalized() {_CVector tv(*this); tv.normalize(); return tv;}
/**
* @brief Method that returns a vector equal to vector product of current vector and vector "v". Works only with vectors which consists of 3 elements
*
* @param v vector of type PIMathVectorT
* @return vector equal to vector product of current vector and vector "v" type of PIMathVectorT
*/
_CVector cross(const _CVector & v) {return (*this) * v;}
/**
* @brief Method that returns a value equal to absolute value of dot product of current vector and vector "v"
*
* @param v vector of type PIMathVectorT
* @return value equal to absolute value of dot product of current vector and vector "v"
*/
Type dot(const _CVector & v) const {return (*this) ^ v;}
/**
* @brief Method which checks if every elements of vector are zeros
*
* @return true if vector is zero, else false
*/
bool isNull() const {PIMV_FOR(i, 0) if (c[i] != Type(0)) return false; return true;}
/**
* @brief Method which checks if current vector is orthogonal to vector "v"
*
* @param v vector of type PIMathVectorT
* @return true if vectors are orthogonal, else fal */
bool isOrtho(const _CVector & v) const {return ((*this) ^ v) == Type(0);}
/**
* @brief Read-only access to elements reference by index of the vector element "index"
* If you enter an index out of the border of the vector there will be "undefined behavior"
*
* @param index is a parameter that shows the index number of the vector of the selected element
* @return reference to element of vector by index
*/
const Type & at(uint index) {return c[index];}
/**
* @brief Full access to the element of vector by index. If you enter an index out of the border of the vector there will be "undefined behavior"
*
* @param index is the index of necessary element
* @return element of vector
*/
Type & operator [](uint index) {return c[index];}
/**
* @brief Read-only access to the element of vector by index. If you enter an index out of the border of the vector there will be "undefined behavior"
*
* @param index is the index of necessary element
* @return element of vector
*/
const Type & operator [](uint index) const {return c[index];}
/**
* @brief Assignment all elements of this vector with all elements of vector "v"
* If the vectors have different dimensions, it returns this without changing anything
*
* @param v vector for the assigment
* @return reference to this
*/
_CVector & operator =(const _CVector & v) {memcpy(c, v.c, sizeof(Type) * Size); return *this;}
/**
* @brief Assignment all elements of this vector with all elements of value "v"
*
* @param v value for the assigment
* @return reference to this
*/
_CVector & operator =(const Type & v) {PIMV_FOR(i, 0) c[i] = v; return *this;}
/**
* @brief Compare all elements of this vector with all elements of vector "v"
*
* @param v vector for the compare
* @return if vectors are equal true, else false
*/
bool operator ==(const _CVector & v) const {PIMV_FOR(i, 0) if (c[i] != v[i]) return false; return true;}
/**
* @brief Compare all elements of this vector with all elements of vector "v"
*
* @param v vector for the compare
* @return if vectors are not equal true, else false
*/
bool operator !=(const _CVector & v) const {return !(*this == v);}
/**
* @brief Addition all elements of this vector with all elements vector "v"
*
* @param v vector for the addition assigment
*/
void operator +=(const _CVector & v) {PIMV_FOR(i, 0) c[i] += v[i];}
/**
* @brief Subtraction all elements of this vector with all elements vector "v"
*
* @param v vector for the subtraction assigment
*/
void operator -=(const _CVector & v) {PIMV_FOR(i, 0) c[i] -= v[i];}
/**
* @brief Multiplication all elements of this vector with value "v"
*
* @param v value for the multiplication assigment
*/
void operator *=(const Type & v) {PIMV_FOR(i, 0) c[i] *= v;}
/**
* @brief Multiplication all elements of this vector with all elements vector "v"
*
* @param v vector for the multiplication assigment
*/
void operator *=(const _CVector & v) {PIMV_FOR(i, 0) c[i] *= v[i];}
/**
* @brief Division all elements of this vector with value "v"
*
* @param v value for the division assigment
*/
void operator /=(const Type & v) {PIMV_FOR(i, 0) c[i] /= v;}
/**
* @brief Division all elements of this vector with all elements vector "v"
*
* @param v vector for the division assigment
*/
void operator /=(const _CVector & v) {PIMV_FOR(i, 0) c[i] /= v[i];}
/**
* @brief Vector substraction this vector
*
* @return the result of vector substraction
*/
_CVector operator -() const {_CVector tv; PIMV_FOR(i, 0) tv[i] = -c[i]; return tv;}
/**
* @brief Addition all elements of this vector with all elements of vector "v"
*
* @param v is vector term
* @return the result of vector addition
*/
_CVector operator +(const _CVector & v) const {_CVector tv = _CVector(*this); PIMV_FOR(i, 0) tv[i] += v[i]; return tv;}
/**
* @brief Substraction all elements of this vector with all elements of vector "v"
*
* @param v is vector term
* @return the result of vector substraction
*/
_CVector operator -(const _CVector & v) const {_CVector tv = _CVector(*this); PIMV_FOR(i, 0) tv[i] -= v[i]; return tv;}
/**
* @brief Multiplication all elements of this vector with value "v"
*
* @param v is value factor
* @return the result of vector multiplication
*/
_CVector operator *(const Type & v) const {_CVector tv = _CVector(*this); PIMV_FOR(i, 0) tv[i] *= v; return tv;}
/**
* @brief Division all elements of this vector with value "v"
*
* @param v is value divider
* @return the result of vector division
*/
_CVector operator /(const Type & v) const {_CVector tv = _CVector(*this); PIMV_FOR(i, 0) tv[i] /= v; return tv;}
/**
* @brief Division all elements of this vector with all elements of vector "v"
*
* @param v is vector divider
* @return the result of vector division
*/
_CVector operator /(const _CVector & v) const {_CVector tv = _CVector(*this); PIMV_FOR(i, 0) tv[i] /= v[i]; return tv;}
/**
* @brief Cross product of two vectors. Works only with vector containing three elements, otherwise returns current vector
*
* @param v is vector for cross product
* @return the result vector equal of cross product
*/
_CVector operator *(const _CVector & v) const {if (Size != 3) return _CVector(); _CVector tv; tv.fill(Type(1)); tv[0] = c[1]*v[2] - v[1]*c[2]; tv[1] = v[0]*c[2] - c[0]*v[2]; tv[2] = c[0]*v[1] - v[0]*c[1]; return tv;}
/**
* @brief Elementwise assignment of multiplication of two vectors
*
* @param v is vector for multiplication
* @return resulting vector
*/
_CVector operator &(const _CVector & v) const {_CVector tv = _CVector(*this); PIMV_FOR(i, 0) tv[i] *= v[i]; return tv;}
/**
* @brief Absolute value of the dot product
*
* @param v is vector for dot product
* @return resulting vector
*/
Type operator ^(const _CVector & v) const {Type tv(0); PIMV_FOR(i, 0) tv += c[i] * v[i]; return tv;}
PIMathMatrixT<1, Size, Type> transposed() const {
PIMathMatrixT<1, Size, Type> ret;
PIMV_FOR(i, 0) ret[0][i] = c[i];
return ret;
}
/**
* @brief The method returns a part of the selected vector from the given vector
*
* @return the resulting vector that is part of this vector
*/
template /// vector {Size, Type} to vector {Size1, Type1}
PIMathVectorT turnTo() const {PIMathVectorT tv; uint sz = piMin(Size, Size1); for (uint i = 0; i < sz; ++i) tv[i] = c[i]; return tv;}
/**
* @brief Creates a vector each element of which is equal to value "v"
*
* @param v this value fills the cells of the vector
* @return filled vector of type PIMathVectorT
*/
static _CVector filled(const Type & v) {_CVector vv; PIMV_FOR(i, 0) vv[i] = v; return vv;}
private:
void resize(const Type & new_value = Type()) {for (uint i = 0; i < Size; ++i) c[i] = new_value;}
Type c[Size];
};
/**
* @brief Multiplication all vector elements with value "x"
*
* @param x value for the multiplication
* @param v vector for the multiplication
* @return resulting vector
*/
template
inline PIMathVectorT operator *(const Type & x, const PIMathVectorT & v) {
return v * x;
}
/**
* @brief Outputting the vector to the console
*
* @param s PICout type
* @param the vector type PIMathVectorT that we print to the console
* @return PIMathVectorT printed to the console
*/
template
inline PICout operator <<(PICout s, const PIMathVectorT & v) {s << "{"; PIMV_FOR(i, 0) {s << v[i]; if (i < Size - 1) s << ", ";} s << "}"; return s;}
/**
* @brief Checking if the cross product of two vectors is zero. Works only with vector containing three elements, otherwise returns current vector
*
* @param f vector of the first operand
* @param s vector of the second operand
* @return true if the cross product is zero, else false
*/
template
inline bool operator ||(const PIMathVectorT & f, const PIMathVectorT & s) {return (f * s).isNull();}
/**
* @brief The square root of every element in the vector
*
* @param v vector of whose elements the square root is taken
* @return resulting vector
*/
template
inline PIMathVectorT sqrt(const PIMathVectorT & v) {PIMathVectorT ret; PIMV_FOR(i, 0) {ret[i] = sqrt(v[i]);} return ret;}
/**
* @brief Squares every element of the vector
*
* @param v vector whose elements are squared
* @return resulting vector
*/
template
inline PIMathVectorT sqr(const PIMathVectorT & v) {PIMathVectorT ret; PIMV_FOR(i, 0) {ret[i] = sqr(v[i]);} return ret;}
/**
* @brief Serializing a vector into a PIByteArray
*
* @param s PIByteArray type
* @param v PIMathVectorT type
* @return PIBiteArray serialized PIMathVectorT
*/
template
inline PIByteArray & operator <<(PIByteArray & s, const PIMathVectorT & v) {for (uint i = 0; i < Size; ++i) s << v[i]; return s;}
/**
* @brief Deserializing vector from PIByteArray
*
* @param s PIByteArray type
* @param v PIMathVector type
* @return PIMathVector deserialized from PIByteArray
*/
template
inline PIByteArray & operator >>(PIByteArray & s, PIMathVectorT & v) {for (uint i = 0; i < Size; ++i) s >> v[i]; return s;}
/**
* @brief Function which returns vector size 2 and type of T
*
* @param x first element of vector
* @param y second element of vector
* @return resulting vector
*/
template
inline PIMathVectorT<2u, T> createVectorT2(T x, T y) {return PIMathVectorT<2u, T>(PIVector() << x << y);}
/**
* @brief Function which returns vector size 3 and type of T
*
* @param x first element of vector
* @param y second element of vector
* @param z third element of vector
* @return resulting vector
*/
template
inline PIMathVectorT<3u, T> createVectorT3(T x, T y, T z) {return PIMathVectorT<3u, T>(PIVector() << x << y << z);}
/**
* @brief Function which returns vector size 4 and type of T
*
* @param x first element of vector
* @param y second element of vector
* @param z third element of vector
* @param w fouth element of vector
* @return resulting vector
*/
template
inline PIMathVectorT<4u, T> createVectorT4(T x, T y, T z, T w) {return PIMathVectorT<4u, T>(PIVector() << x << y << z << w);}
typedef PIMathVectorT<2u, int> PIMathVectorT2i;
typedef PIMathVectorT<3u, int> PIMathVectorT3i;
typedef PIMathVectorT<4u, int> PIMathVectorT4i;
typedef PIMathVectorT<2u, double> PIMathVectorT2d;
typedef PIMathVectorT<3u, double> PIMathVectorT3d;
typedef PIMathVectorT<4u, double> PIMathVectorT4d;
#define createVectorT2i createVectorT2
#define createVectorT3i createVectorT3
#define createVectorT4i createVectorT4
#define createVectorT2f createVectorT2
#define createVectorT3f createVectorT3
#define createVectorT4f createVectorT4
#define createVectorT2d createVectorT2
#define createVectorT3d createVectorT3
#define createVectorT4d createVectorT4
#undef PIMV_FOR
/// Vector
#define PIMV_FOR(v, s) for (uint v = s; v < c.size(); ++v)
//! \brief A class that works with vector operations, the input data of which is the data type of the vector
//! @tparam Type is the data type of the vector. There are can be basic C++ language data and different classes where the arithmetic operators(=, +=, -=, *=, /=, ==, !=, +, -, *, /)
//! of the C++ language are implemented
template
class PIP_EXPORT PIMathVector {
typedef PIMathVector _CVector;
template friend PIByteArray & operator <<(PIByteArray & s, const PIMathVector & v);
template friend PIByteArray & operator >>(PIByteArray & s, PIMathVector & v);
public:
/**
* @brief Constructor that calls the resize method
*
* @param size vector dimension
*/
PIMathVector(const uint size = 0) {c.resize(size);}
/**
* @brief Constructor that fills a vector PIMathVector with the values of another vector "PIVector"
*
* @param val vector of type PIVector which is identified PIMathVector
*/
PIMathVector(const PIVector & val) {c.resize(val.size()); PIMV_FOR(i, 0) c[i] = val[i];}
/**
* @brief Constructor that fills a vector PIMathVector with the subtraction of two vectors
*
* @param st vector of type PIMathVector
* @param fn vector of type PIMathVector
*/
PIMathVector(const _CVector & st, const _CVector & fn) {c.resize(st.size()); PIMV_FOR(i, 0) c[i] = fn[i] - st[i];}
/**
* @brief Method that returns the number of elements contained in the vector
*
* @return type uint shows number of elements in this vector
*/
uint size() const {return c.size();}
/**
* @brief Returns self resized vector
*
* @param size new vector dimension
* @param new_value value with which the vector is filled
* @return reference to this
*/
_CVector & resize(uint size, const Type & new_value = Type()) {c.resize(size, new_value); return *this;}
/**
* @brief Returns copy of resized vector
*
* @param size new vector dimension
* @param new_value value with which the vector is filled
* @return resized vector
*/
_CVector resized(uint size, const Type & new_value = Type()) {_CVector tv = _CVector(*this); tv.resize(size, new_value); return tv;}
/**
* @brief Method that set this elements to value "v"
*
* @param v value of which the vector is filled
* @return reference to this
*/
_CVector & fill(const Type & v) {PIMV_FOR(i, 0) c[i] = v; return *this;}
/**
* @brief Method that sets this using a vector, each element of which is added to the value of "v"
*
* @param v value of which the vector is filled
* @return reference to this
*/
_CVector & move(const Type & v) {PIMV_FOR(i, 0) c[i] += v; return *this;}
/**
* @brief Method that sets this with a vector, each element of which is added to each element of the vector "v".
* If the vectors have different dimensions, it returns this without changing anything
*
* @param v vector of type PIMathVectorT
* @return reference to this
*/
_CVector & move(const _CVector & v) {if(v.size() != c.size()) return *this; PIMV_FOR(i, 0) c[i] += v[i]; return *this;}
/**
* @brief Method that replaces two elements in this vector by indices. You cannot use an index larger than the number vector dimension,
* otherwise there will be "undefined behavior"
*
* @param fe index of the first element
* @param se index of the second element
* @return reference to this
*/
_CVector & swap(uint fe, uint se) {piSwap(c[fe], c[se]); return *this;}
/**
* @brief Method that returns sum of the squares of all elements of the vector
*
* @return value equal to the sum of the squares of all elements of the vector
*/
Type lengthSqr() const {Type tv(0); PIMV_FOR(i, 0) tv += (c[i] * c[i]); return tv;}
/**
* @brief Method that returns a scalar physical value equal to the absolute value of vector
*
* @return value equal to length of a vector
*/
Type length() const {return sqrt(lengthSqr());}
/**
* @brief Method that returns the sum of the absolute values of all vector values
*
* @return value equal sum of the absolute values of all vector values
*/
Type manhattanLength() const {Type tv(0); PIMV_FOR(i, 0) tv += fabs(c[i]); return tv;}
/**
* @brief Method that returns the cos of the current vector and vector "v". If the vectors have different dimensions, it returns false
*
* @param v vector of type PIMathVector
* @return cos value of the angle between two vectors
*/
Type angleCos(const _CVector & v) const {if(v.size() != c.size()) return false; Type tv = v.length() * length(); return (tv == Type(0) ? Type(0) : ((*this) ^ v) / tv);}
/**
* @brief Method that returns the sin of the current vector and vector "v". Works only with vectors which consists of 3 elements.
* If the vectors have different dimensions, it returns false
*
* @param v vector of type PIMathVector
* @return sin value of the angle between two vector
*/
Type angleSin(const _CVector & v) const {if(v.size() != c.size()) return false; Type tv = angleCos(v); return sqrt(Type(1) - tv * tv);}
/**
* @brief Method that returns the angle between of the current vector and vector "v" in Rad.
* If the vectors have different dimensions, it returns false
*
* @param v vector of type PIMathVector
* @return value of the angle between two vectors in Rad
*/
Type angleRad(const _CVector & v) const {if(v.size() != c.size()) return false; return acos(angleCos(v));}
/**
* @brief Method that returns the angle between of the current vector and vector "v" in Deg.
* If the vectors have different dimensions, it returns false
*
* @param v vector of type PIMathVectorT
* @return value of the angle between two vectors in Deg
*/
Type angleDeg(const _CVector & v) const {if(v.size() != c.size()) return false; return toDeg(acos(angleCos(v)));}
/**
* @brief Method that returns a vector equal to the projection of the current vector onto the vector "v".
* If the vectors have different dimensions, it returns this without changing anything
*
* @param v vector of type PIMathVector
* @return vector of type PIMathVector equal to the projection of the current vector onto the vector "v"
*/
_CVector projection(const _CVector & v) {if(v.size() != c.size()) return *this; Type tv = v.length(); return (tv == Type(0) ? _CVector() : v * (((*this) ^ v) / tv));}
/**
* @brief Method that returns a normalized vector (each element of a vector is divided by the absolute value of this vector)
*
* @return copy of normalized vector of type PIMathVector
*/
_CVector & normalize() {Type tv = length(); if (tv == Type(1)) return *this; if (piAbs(tv) <= Type(1E-100)) {fill(Type(0)); return *this;} PIMV_FOR(i, 0) c[i] /= tv; return *this;}
/**
* @brief Method that returns a normalized vector (each element of a vector is divided by the absolute value of this vector)
*
* @return normalized vector of type PIMathVector
*/
_CVector normalized() {_CVector tv(*this); tv.normalize(); return tv;}
/**
* @brief Method which checks if every elements of vector are zeros
*
* @return true if vector is zero, else false
*/
bool isNull() const {PIMV_FOR(i, 0) if (c[i] != Type(0)) return false; return true;}
/**
* @brief Method which checks if vector is empty
*
* @return true if vector is valid, else false
*/
bool isValid() const {return !c.isEmpty();}
/**
* @brief Method which checks if current vector is orthogonal to vector "v".
* If the vectors have different dimensions, it returns false
*
* @param v vector of type PIMathVector
* @return true if vectors are orthogonal, else false
*/
bool isOrtho(const _CVector & v) const {if(v.size() != c.size()) return false; return ((*this) ^ v) == Type(0);}
/**
* @brief Read-only access to elements reference by index of the vector element "index"
* If you enter an index out of the border of the vector there will be "undefined behavior"
*
* @param index is a parameter that shows the index number of the vector of the selected element
* @return reference to element of vector by index
*/
const Type & at(uint index) {return c[index];}
/**
* @brief Full access to the element of vector by index. If you enter an index out of the border of the vector there will be "undefined behavior"
*
* @param index is the index of necessary element
* @return element of vector
*/
Type & operator [](uint index) {return c[index];}
/**
* @brief Read-only access to the element of vector by index. If you enter an index out of the border of the vector there will be "undefined behavior"
*
* @param index is the index of necessary element
* @return element of vector
*/
const Type & operator [](uint index) const {return c[index];}
/**
* @brief Assignment all elements of this vector with all elements of vector "v"
* If the vectors have different dimensions, it returns this without changing anything
*
* @param v vector for the assigment
* @return reference to this
*/
_CVector & operator =(const _CVector & v) {if(v.size() != c.size()) return *this; c = v.c; return *this;}
/**
* @brief Assignment all elements of this vector with all elements of value "v"
*
* @param v value for the assigment
* @return reference to this
*/
_CVector & operator =(const Type & v) {PIMV_FOR(i, 0) c[i] = v; return *this;}
/**
* @brief Compare all elements of this vector with all elements of vector "v"
*
* @param v vector for the compare
* @return if vectors are equal true, else false
*/
bool operator ==(const _CVector & v) const {PIMV_FOR(i, 0) if ((c[i] != v[i]) || (v.size() != c.size())) return false; return true;}
/**
* @brief Compare all elements of this vector with all elements of vector "v"
*
* @param v vector for the compare
* @return if vectors are not equal true, else false
*/
bool operator !=(const _CVector & v) const {return !(*this == v);}
/**
* @brief Addition all elements of this vector with all elements vector "v". If the vectors have different dimensions, it returns void()
*
* @param v vector for the addition assigment
*/
void operator +=(const _CVector & v) {if(v.size() != c.size()) return void(); PIMV_FOR(i, 0) c[i] += v[i];}
/**
* @brief Subtraction all elements of this vector with all elements vector "v". If the vectors have different dimensions, it returns void()
*
* @param v vector for the subtraction assigment
*/
void operator -=(const _CVector & v) {if(v.size() != c.size()) return void(); PIMV_FOR(i, 0) c[i] -= v[i];}
/**
* @brief Multiplication all elements of this vector with value "v"
*
* @param v value for the multiplication assigment
*/
void operator *=(const Type & v) {PIMV_FOR(i, 0) c[i] *= v;}
/**
* @brief Multiplication all elements of this vector with all elements vector "v". If the vectors have different dimensions, it returns void()
*
* @param v vector for the multiplication assigment
*/
void operator *=(const _CVector & v) {if(v.size() != c.size()) return void(); PIMV_FOR(i, 0) c[i] *= v[i];}
/**
* @brief Division all elements of this vector with value "v"
*
* @param v value for the division assigment
*/
void operator /=(const Type & v) {PIMV_FOR(i, 0) c[i] /= v;}
/**
* @brief Division all elements of this vector with all elements vector "v". If the vectors have different dimensions, it returns void()
*
* @param v vector for the division assigment
*/
void operator /=(const _CVector & v) {if(v.size() != c.size()) return void(); PIMV_FOR(i, 0) c[i] /= v[i];}
/**
* @brief Vector substraction this vector
*
* @return the result of vector substraction
*/
_CVector operator -() const {_CVector tv; PIMV_FOR(i, 0) tv[i] = -c[i]; return tv;}
/**
* @brief Addition all elements of this vector with all elements of vector "v". If the vectors have different dimensions, it returns this without changing anything
*
* @param v is vector term
* @return the result of vector addition
*/
_CVector operator +(const _CVector & v) const {if(v.size() != c.size()) return _CVector(*this); _CVector tv = _CVector(*this); PIMV_FOR(i, 0) tv[i] += v[i]; return tv;}
/**
* @brief Substraction all elements of this vector with all elements of vector "v". If the vectors have different dimensions, it returns this without changing anything
*
* @param v is vector term
* @return the result of vector substraction
*/
_CVector operator -(const _CVector & v) const {if(v.size() != c.size()) return _CVector(*this); _CVector tv = _CVector(*this); PIMV_FOR(i, 0) tv[i] -= v[i]; return tv;}
/**
* @brief Multiplication all elements of this vector with value "v"
*
* @param v is value factor
* @return the result of vector multiplication
*/
_CVector operator *(const Type & v) const {_CVector tv = _CVector(*this); PIMV_FOR(i, 0) tv[i] *= v; return tv;}
/**
* @brief Division all elements of this vector with value "v"
*
* @param v is value divider
* @return the result of vector division
*/
_CVector operator /(const Type & v) const {_CVector tv = _CVector(*this); PIMV_FOR(i, 0) tv[i] /= v; return tv;}
/**
* @brief Cross product of two vectors. Works only with vector containing three elements, otherwise returns current vector
*
* @param v is vector for cross product
* @return the result vector equal of cross product
*/
_CVector operator *(const _CVector & v) const {if ((c.size() != 3) || (v.size() != 3)) return _CVector(); _CVector tv(3); tv.fill(Type(1)); tv[0] = c[1]*v[2] - v[1]*c[2]; tv[1] = v[0]*c[2] - c[0]*v[2]; tv[2] = c[0]*v[1] - v[0]*c[1]; return tv;}
/**
* @brief Elementwise assignment of multiplication of two vectors. If the vectors have different dimensions, it returns this without changing anything
*
* @param v is vector for multiplication
* @return resulting vector
*/
_CVector operator &(const _CVector & v) const {if(v.size() != c.size()) return _CVector(*this); _CVector tv = _CVector(*this); PIMV_FOR(i, 0) tv[i] *= v[i]; return tv;}
/**
* @brief Value of the dot product. If the vectors have different dimensions, it returns false
*
* @param v is vector for dot product
* @return resulting value
*/
Type operator ^(const _CVector & v) const {if(v.size() != c.size()) return false; Type tv(0); PIMV_FOR(i, 0) tv += c[i] * v[i]; return tv;}
/**
* @brief Converts PIMathVector to PIVector type
*
* @return vector equal PIMathVector but in PIVector type
*/
PIVector toVector() const {return c;}
/**
* @brief Returns full access data of vector
*
* @return data of vector
*/
inline Type * data() {return c.data();}
/**
* @brief Returns read-only data of vector
*
* @return data of vector
*/
inline const Type * data() const {return c.data();}
private:
PIVector c;
};
#undef PIMV_FOR
#ifdef PIP_STD_IOSTREAM
template
inline std::ostream & operator <<(std::ostream & s, const PIMathVector & v) {s << "{"; for (uint i = 0; i < v.size(); ++i) {s << v[i]; if (i < v.size() - 1) s << ", ";} s << "}"; return s;}
#endif
/**
* @brief Outputting the vector to the console
*
* @param s PICout type
* @param the vector type PIMathVector that we print to the console
* @return PIMathVector printed to the console
*/
template
inline PICout operator <<(PICout s, const PIMathVector & v) {s << "Vector{"; for (uint i = 0; i < v.size(); ++i) {s << v[i]; if (i < v.size() - 1) s << ", ";} s << "}"; return s;}
/**
* @brief Serializing a vector into a PIByteArray
*
* @param s PIByteArray type
* @param v PIMathVector type
* @return PIBiteArray serialized PIMathVector
*/
template
inline PIByteArray & operator <<(PIByteArray & s, const PIMathVector & v) {s << v.c; return s;}
/**
* @brief Deserializing vector from PIByteArray
*
* @param s PIByteArray type
* @param v PIMathVector type
* @return PIMathVector deserialized from PIByteArray
*/
template
inline PIByteArray & operator >>(PIByteArray & s, PIMathVector & v) {s >> v.c; return s;}
typedef PIMathVector PIMathVectori;
typedef PIMathVector PIMathVectord;
#endif // PIMATHVECTOR_H