Last active
February 14, 2020 18:40
-
-
Save xaedes/023b6485d73f5dc7ff7953c5b5b42a58 to your computer and use it in GitHub Desktop.
C++ Header Only Type Generic Vector Implementation (can be used as replacement for cv::Vec to avoid heavy dependency on OpenCV when you just need basic vector artihmetic)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#define ARITHMETIC_OPERATOR(OP) \ | |
Vec<TValueType,TNumDimensions>& operator OP ## =(const Vec<TValueType,TNumDimensions>& rhs) \ | |
{ \ | |
for(unsigned int i=0; i<TNumDimensions; ++i) \ | |
*this[i] OP ## = rhs[i]; \ | |
return *this; \ | |
} \ | |
\ | |
friend Vec<TValueType,TNumDimensions> operator OP(Vec<TValueType,TNumDimensions> lhs, const Vec<TValueType,TNumDimensions>& rhs) \ | |
{ \ | |
for(unsigned int i=0; i<TNumDimensions; ++i) \ | |
lhs[i] OP ## = rhs[i]; \ | |
return lhs; \ | |
} \ | |
\ | |
Vec<TValueType,TNumDimensions>& operator OP ## =(const TValueType& rhs) \ | |
{ \ | |
for(unsigned int i=0; i<TNumDimensions; ++i) \ | |
*this[i] OP ## = rhs; \ | |
return *this; \ | |
} \ | |
\ | |
friend Vec<TValueType,TNumDimensions> operator OP(Vec<TValueType,TNumDimensions> lhs, const TValueType& rhs) \ | |
{ \ | |
for(unsigned int i=0; i<TNumDimensions; ++i) \ | |
lhs[i] OP ## = rhs; \ | |
return lhs; \ | |
} \ | |
\ | |
template<typename TValueType, unsigned int TNumDimensions> | |
struct Vec | |
{ | |
TValueType operator[](int i) const { return data[i]; } | |
TValueType& operator[](int i) { return data[i]; } | |
TValueType data[TNumDimensions]; | |
Vec(TValueType value) | |
{ | |
for(unsigned int i=0; i<TNumDimensions; ++i) | |
data[i] = value; | |
} | |
template<typename ... TArgs> Vec(TValueType first, TArgs... args) : data{first, (TValueType)args...} {} | |
ARITHMETIC_OPERATOR(+) | |
ARITHMETIC_OPERATOR(-) | |
ARITHMETIC_OPERATOR(*) | |
ARITHMETIC_OPERATOR(/) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment