Abstract away a bit more logic in order to allow us to easily override the core vector operations if need be.

This commit is contained in:
Pavel Krajcevski 2013-10-08 20:29:56 -04:00
parent 01a38dc76b
commit 47074c1224
2 changed files with 61 additions and 23 deletions

View File

@ -137,6 +137,41 @@ class Pixel : public Vector4<uint16> {
};
REGISTER_VECTOR_TYPE(Pixel);
// Overload operators so that we can preserve bit depths...
template<typename ScalarType>
static inline Pixel ScalarMultiply(const Pixel &p, const ScalarType &s) {
Pixel a(p);
for(int i = 0; i < Pixel::Size; i++)
a(i) = p(i) * s;
return a;
}
template<typename ScalarType>
static inline Pixel ScalarDivide(const Pixel &p, const ScalarType &s) {
Pixel a(p);
for(int i = 0; i < Pixel::Size; i++)
a(i) = p(i) / s;
return a;
}
template<typename VectorType>
static inline Pixel VectorAddition(const Pixel &p, const VectorType &v) {
Pixel a(p);
for(int i = 0; i < Pixel::Size; i++) {
a(i) += v(i);
}
return a;
}
template<typename VectorType>
static inline Pixel VectorSubtraction(const Pixel &p, const VectorType &v) {
Pixel a(p);
for(int i = 0; i < Pixel::Size; i++) {
a(i) -= v(i);
}
return a;
}
} // namespace FasTC
#endif // BASE_INCLUDE_PIXEL_H_

View File

@ -87,39 +87,48 @@ namespace FasTC {
};
// Operators
template<typename VectorTypeOne, typename VectorTypeTwo>
static inline VectorTypeOne VectorAddition(const VectorTypeOne &v1,
const VectorTypeTwo &v2) {
VectorTypeOne a;
for(int i = 0; i < VectorTypeOne::Size; i++) {
a(i) = v1(i) + v2(i);
}
return a;
}
template<typename VectorTypeOne, typename VectorTypeTwo>
static inline VectorTypeOne operator+(const VectorTypeOne &v1,
const VectorTypeTwo &v2) {
VectorTypeOne a;
for(int i = 0; i < VectorTypeOne::Size; i++)
a(i) = v1(i) + v2(i);
return a;
return VectorAddition(v1, v2);
}
template<typename VectorTypeOne, typename VectorTypeTwo>
static inline VectorTypeOne &operator+=(VectorTypeOne &v1,
const VectorTypeTwo &v2) {
for(int i = 0; i < VectorTypeOne::Size; i++)
v1(i) += v2(i);
return v1;
return v1 = VectorAddition(v1, v2);
}
template<typename VectorTypeOne, typename VectorTypeTwo>
static inline VectorTypeOne VectorSubtraction(const VectorTypeOne &v1,
const VectorTypeTwo &v2) {
VectorTypeOne a;
for(int i = 0; i < VectorTypeOne::Size; i++) {
a(i) = v1(i) - v2(i);
}
return a;
}
template<typename VectorTypeOne, typename VectorTypeTwo>
static inline VectorTypeOne operator-(const VectorTypeOne &v1,
const VectorTypeTwo &v2) {
VectorTypeOne a;
for(int i = 0; i < VectorTypeOne::Size; i++)
a(i) = v1(i) - v2(i);
return a;
return VectorSubtraction(v1, v2);
}
template<typename VectorTypeOne, typename VectorTypeTwo>
static inline VectorTypeOne &operator-=(VectorTypeOne &v1,
const VectorTypeTwo &v2) {
for(int i = 0; i < VectorTypeOne::Size; i++) {
v1(i) -= v2(i);
}
return v1;
return v1 = VectorSubtraction(v1, v2);
}
template<typename T>
@ -217,18 +226,12 @@ namespace FasTC {
template<typename VectorType, typename ScalarType>
static inline VectorType &operator*=(VectorType &v, const ScalarType &s) {
for(int i = 0; i < VectorType::Size; i++) {
v(i) *= s;
}
return v;
return v = ScalarMultiply(v, s);
}
template<typename VectorType, typename ScalarType>
static inline VectorType &operator/=(VectorType &v, const ScalarType &s) {
for(int i = 0; i < VectorType::Size; i++) {
v(i) /= s;
}
return v;
return v = ScalarDivide(v, s);
}
};