/* Menes - C++ High-Level Utility Library * Copyright (C) 2004 Jay Freeman (saurik) */ /* * Redistribution and use in source and binary * forms, with or without modification, are permitted * provided that the following conditions are met: * * 1. Redistributions of source code must retain the * above copyright notice, this list of conditions * and the following disclaimer. * 2. Redistributions in binary form must reproduce the * above copyright notice, this list of conditions * and the following disclaimer in the documentation * and/or other materials provided with the * distribution. * 3. The name of the author may not be used to endorse * or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef MENES_ALG_TENSOR_HPP #define MENES_ALG_TENSOR_HPP #include "cxx/platform.hpp" #include "alg/linkage.hpp" #ifdef MENES_PRAGMA_ONCE #pragma once #endif #include "etl/integral.hpp" #include "etl/template.hpp" #include "etl/list.hpp" namespace alg { template class Tensor : public Tensor, Ranks_>, Element_> { private: typedef Tensor This_; typedef Tensor, Ranks_>, Element_> Base_; private: typename Base_::Value value_; public: _finline Tensor() { } _finline Tensor(const Base_ &base, const typename Base_::Value &value = typename Base_::Value()) : Base_(base), value_(value) { } _finline const Base_ &GetBase() const { return *this; } _finline const typename Base_::Value &GetValue() const { return value_; } _finline typename Base_::Value &GetValue() { return value_; } }; template class Tensor, Ranks_>, Element_> { private: typedef Tensor, Ranks_>, Element_> This_; public: typedef Tensor Value; static const unsigned Rank = etl::Length::value; const Value &operator [](size_t index) const { return reinterpret_cast(this)[index]; } Value &operator [](size_t index) { return reinterpret_cast(this)[index]; } }; template class Tensor { private: typedef Tensor This_; public: typedef Element_ Value; static const unsigned Rank = 0; private: Value value_; public: Tensor() { } Tensor(const Value &value) : value_(value) { } _finline const Value &GetValue() const { return value_; } _finline Value &GetValue() { return value_; } operator const Value &() const { return value_; } operator Value &() { return value_; } }; template class Tensor >, Element_> : public Tensor >, Element_> { private: typedef Tensor >, Element_> This_; typedef Tensor >, Element_> Base_; public: typename Base_::Value x; public: _finline Tensor(const Base_ &base, const typename Base_::Value &value) : Base_(base), x(value) { } _finline Tensor( const typename Base_::Value &x = typename Base_::Value() ) : x(x) { } _finline const Base_ &GetBase() const { return *this; } _finline const typename Base_::Value &GetValue() const { return x; } _finline typename Base_::Value &GetValue() { return x; } }; template class Tensor >, Element_> : public Tensor >, Element_> { private: typedef Tensor >, Element_> This_; typedef Tensor >, Element_> Base_; public: typedef typename Base_::Value Value; Value y; public: _finline Tensor(const Base_ &base, const typename Base_::Value &value) : Base_(base), y(value) { } _finline Tensor( const Value &x = Value(), const Value &y = Value() ) : Base_(x), y(y) { } _finline const Base_ &GetBase() const { return *this; } _finline const typename Base_::Value &GetValue() const { return y; } _finline typename Base_::Value &GetValue() { return y; } }; template class Tensor >, Element_> : public Tensor >, Element_> { private: typedef Tensor >, Element_> This_; typedef Tensor >, Element_> Base_; public: typename Base_::Value z; public: _finline Tensor(const Base_ &base, const typename Base_::Value &value) : Base_(base), z(value) { } _finline Tensor( const typename Base_::Value &x = typename Base_::Value(), const typename Base_::Value &y = typename Base_::Value(), const typename Base_::Value &z = typename Base_::Value() ) : Base_(x, y), z(z) { } _finline const Base_ &GetBase() const { return *this; } _finline const typename Base_::Value &GetValue() const { return z; } _finline typename Base_::Value &GetValue() { return z; } }; template class Tensor >, Element_> : public Tensor >, Element_> { private: typedef Tensor >, Element_> This_; typedef Tensor >, Element_> Base_; public: typedef typename Base_::Value Value; Value w; public: _finline Tensor(const Base_ &base, const typename Base_::Value &value) : Base_(base), w(value) { } _finline Tensor( const Value &x = Value(), const Value &y = Value(), const Value &z = Value(), const Value &w = Value() ) : Base_(x, y, z), w(w) { } _finline const Base_ &GetBase() { return *this; } _finline const typename Base_::Value &GetValue() const { return w; } _finline typename Base_::Value &GetValue() { return w; } }; /*template Tensor operator +(const Tensor &lhs, const Scalar_ &rhs) { return lhs.GetValue() + rhs; } template Tensor, Ranks_>, Element_> operator +(const Tensor, Ranks_>, Element_> &lhs, const Scalar_ &rhs) { return Tensor, Ranks_>, Element_>(); } template Tensor operator +(const Tensor &lhs, const Scalar_ &rhs) { return Tensor(lhs.GetBase() + rhs, lhs.GetValue() + rhs); } template Tensor operator +(const Scalar_ &lhs, const Tensor &rhs) { return rhs + lhs; } template Tensor operator -(const Tensor &lhs, const Scalar_ &rhs) { return lhs.GetValue() - rhs; } template Tensor, Ranks_>, Element_> operator -(const Tensor, Ranks_>, Element_> &lhs, const Scalar_ &rhs) { return Tensor, Ranks_>, Element_>(); } template Tensor operator -(const Tensor &lhs, const Scalar_ &rhs) { return Tensor(lhs.GetBase() - rhs, lhs.GetValue() - rhs); }*/ /*template Tensor, Ranks_>, Element_> operator +( const Tensor, Ranks_>, Element_> &lhs, const Tensor, Ranks_>, Element_> &rhs ) { return Tensor, Ranks_>, Element_>(); } template Tensor operator +( const Tensor &lhs, const Tensor &rhs ) { return Tensor(lhs.GetBase() + rhs.GetBase(), lhs.GetValue() + rhs.GetValue()); } template Tensor, Ranks_>, Element_> operator -(const Tensor, Ranks_>, Element_> &lhs, const Tensor, Ranks_>, Element_> &rhs) { return Tensor, Ranks_>, Element_>(); } template Tensor operator -(const Tensor &lhs, const Tensor &rhs) { return Tensor(lhs.GetBase() - rhs.GetBase(), lhs.GetValue() - rhs.GetValue()); } template Tensor operator *(const Tensor &lhs, const Scalar_ &rhs) { return lhs.GetValue() * rhs; } template Tensor, Ranks_>, Element_> operator *(const Tensor, Ranks_>, Element_> &lhs, const Scalar_ &rhs) { return Tensor, Ranks_>, Element_>(); } template Tensor operator *(const Tensor &lhs, const Scalar_ &rhs) { return Tensor(lhs.GetBase() * rhs, lhs.GetValue() * rhs); } template Tensor operator *(const Scalar_ &lhs, const Tensor &rhs) { return rhs * lhs; } template Tensor operator /(const Tensor &lhs, const Scalar_ &rhs) { return lhs.GetValue() / rhs; } template Tensor, Ranks_>, Element_> operator /(const Tensor, Ranks_>, Element_> &lhs, const Scalar_ &rhs) { return Tensor, Ranks_>, Element_>(); } template Tensor operator /(const Tensor &lhs, const Scalar_ &rhs) { return Tensor(lhs.GetBase() / rhs, lhs.GetValue() / rhs); } template Tensor::value> >, Element_> CrossProduct( const Tensor >, Element_> &lhs, const Tensor >, Element_> &rhs ) { return; } template Tensor >, Element_> CrossProduct(const Tensor >, Element_> values[Dimensions_ - 1]) { }*/ template struct Vector { typedef Tensor >, Element_> Result; }; template struct Matrix { typedef Tensor, etl::Cons< etl::Integer > >, Element_> Result; }; typedef Vector<1, float>::Result Vector1f; typedef Vector<1, double>::Result Vector1d; typedef Vector<2, float>::Result Vector2f; typedef Vector<2, double>::Result Vector2d; typedef Vector<3, float>::Result Vector3f; typedef Vector<3, double>::Result Vector3d; typedef Vector<4, float>::Result Vector4f; typedef Vector<4, double>::Result Vector4d; typedef Matrix<2, 2, float>::Result Matrix22f; typedef Matrix<2, 2, double>::Result Matrix22d; typedef Matrix<3, 3, float>::Result Matrix33f; typedef Matrix<3, 3, double>::Result Matrix33d; typedef Matrix<4, 4, float>::Result Matrix44f; typedef Matrix<4, 4, double>::Result Matrix44d; typedef Matrix<3, 4, float>::Result Matrix34f; typedef Matrix<3, 4, double>::Result Matrix34d; } #endif//MENES_ALG_TENSOR_HPP