From 47074c1224e3b45e8661f1dd2ad147619017625b Mon Sep 17 00:00:00 2001 From: Pavel Krajcevski Date: Tue, 8 Oct 2013 20:29:56 -0400 Subject: [PATCH] Abstract away a bit more logic in order to allow us to easily override the core vector operations if need be. --- Base/include/Pixel.h | 35 ++++++++++++++++++++++++++++ Base/include/VectorBase.h | 49 +++++++++++++++++++++------------------ 2 files changed, 61 insertions(+), 23 deletions(-) diff --git a/Base/include/Pixel.h b/Base/include/Pixel.h index 1d2e6fe..3c1a189 100644 --- a/Base/include/Pixel.h +++ b/Base/include/Pixel.h @@ -137,6 +137,41 @@ class Pixel : public Vector4 { }; REGISTER_VECTOR_TYPE(Pixel); +// Overload operators so that we can preserve bit depths... +template +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 +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 +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 +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_ diff --git a/Base/include/VectorBase.h b/Base/include/VectorBase.h index e7962bb..f870799 100644 --- a/Base/include/VectorBase.h +++ b/Base/include/VectorBase.h @@ -87,39 +87,48 @@ namespace FasTC { }; // Operators + template + 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 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 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 + 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 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 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 @@ -217,18 +226,12 @@ namespace FasTC { template 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 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); } };