diff options
-rw-r--r-- | .clang-format | 96 | ||||
-rw-r--r-- | CMakeLists.txt | 9 | ||||
-rw-r--r-- | include/YAGE/Math/matrix.hpp | 156 | ||||
-rw-r--r-- | test/matrix_test.cpp | 29 |
4 files changed, 275 insertions, 15 deletions
diff --git a/.clang-format b/.clang-format new file mode 100644 index 00000000..f02c2c3e --- /dev/null +++ b/.clang-format @@ -0,0 +1,96 @@ +--- +Language: Cpp +# BasedOnStyle: LLVM +AccessModifierOffset: -2 +AlignAfterOpenBracket: Align +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlinesLeft: false +AlignOperands: true +AlignTrailingComments: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: All +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: false +BinPackArguments: true +BinPackParameters: true +BraceWrapping: + AfterClass: false + AfterControlStatement: false + AfterEnum: false + AfterFunction: false + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + BeforeCatch: false + BeforeElse: false + IndentBraces: false +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Attach +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +BreakAfterJavaFieldAnnotations: false +BreakStringLiterals: true +ColumnLimit: 80 +CommentPragmas: '^ IWYU pragma:' +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DerivePointerAlignment: false +DisableFormat: false +ExperimentalAutoDetectBinPacking: false +ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ] +IncludeCategories: + - Regex: '^"(llvm|llvm-c|clang|clang-c)/' + Priority: 2 + - Regex: '^(<|"(gtest|isl|json)/)' + Priority: 3 + - Regex: '.*' + Priority: 1 +IncludeIsMainRegex: '$' +IndentCaseLabels: false +IndentWidth: 2 +IndentWrappedFunctionNames: false +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: true +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBlockIndentWidth: 2 +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakString: 1000 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 60 +PointerAlignment: Right +ReflowComments: true +SortIncludes: true +SpaceAfterCStyleCast: false +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeParens: ControlStatements +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: false +SpacesInContainerLiterals: true +SpacesInCStyleCastParentheses: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: Cpp11 +TabWidth: 8 +UseTab: Never +... + diff --git a/CMakeLists.txt b/CMakeLists.txt index d30bc4ef..a1bb9806 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,6 +13,13 @@ set(YAGE_VERSION # set standard set(CMAKE_CXX_STANDARD 14) +if ( CMAKE_COMPILER_IS_GNUCC ) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra") +endif ( CMAKE_COMPILER_IS_GNUCC ) +if ( MSVC ) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4") +endif ( MSVC ) + # set the test sources set(YAGE_SOURCE_DIR ${PROJECT_SOURCE_DIR}/src) set(YAGE_TEST_DIR ${PROJECT_SOURCE_DIR}/test) @@ -40,7 +47,7 @@ set(YAGE_SOURCES ${PROJECT_SOURCE_DIR}/src/spritebatch.cpp ${PROJECT_SOURCE_DIR}/src/sprite.cpp ${PROJECT_SOURCE_DIR}/src/texturecache.cpp -# ${PROJECT_SOURCE_DIR}/src/vector2d.cpp + # ${PROJECT_SOURCE_DIR}/src/vector2d.cpp ${PROJECT_SOURCE_DIR}/src/window.cpp) diff --git a/include/YAGE/Math/matrix.hpp b/include/YAGE/Math/matrix.hpp index 2ada55eb..889db336 100644 --- a/include/YAGE/Math/matrix.hpp +++ b/include/YAGE/Math/matrix.hpp @@ -1,7 +1,9 @@ #ifndef YAGE_MATH_MATRIX_HPP #define YAGE_MATH_MATRIX_HPP -#include <memory> +#include <algorithm> +#include <exception> +#include <iostream> #include <vector> namespace yage @@ -15,15 +17,20 @@ namespace detail template<int Rows, int Cols, class Type> class Row { private: - std::shared_ptr<Matrix<Rows, Cols, Type>> parent_; + Matrix<Rows, Cols, Type> *parent_; int index_; public: - Row<Rows, Cols, Type>(std::shared_ptr<Matrix<Rows, Cols, Type>> parent, int index) : + Row<Rows, Cols, Type>(Matrix<Rows, Cols, Type> *parent, int index) : parent_(parent), index_(index) {} - Type &operator[](int col) + Type& operator[](int col) + { + return parent_->data_[index_*Cols+col]; + } + + const Type& operator[](int col) const { return parent_->data_[index_*Cols+col]; } @@ -34,19 +41,154 @@ public: template<int Rows=4, int Cols=4, class Type=double> class Matrix { friend class detail::Row<Rows, Cols, Type>; -private: +protected: std::vector<Type> data_; public: Matrix<Rows, Cols, Type>() : data_(Rows*Cols) {} - Matrix<Rows, Cols, Type>(int rows, int cols) : data_(rows*cols) {} detail::Row<Rows, Cols, Type> operator[](int row) { - return detail::Row<Rows, Cols, Type>(std::make_shared<Matrix<Rows, Cols, Type>>(*this), row); + return detail::Row<Rows, Cols, Type>(this, row); + } + + Matrix<Rows, Cols, Type>& operator=(const Matrix<Rows, Cols, Type> &other) + { + if(this!=&other) + { + data_=other.data_; + } + return *this; + } + + Matrix<Rows, Cols, Type>& operator+=(const Matrix<Rows, Cols, Type> &rhs) + { + std::vector<Type> out; + out.reserve(data_.size()); + std::transform(data_.begin(), data_.end(), rhs.data_.begin(), std::back_inserter(out), + [] (Type a, Type b) { + return a+b; + }); + data_=std::move(out); + return *this; + } + + friend Matrix<Rows, Cols, Type> operator+(Matrix<Rows, Cols, Type> lhs, + const Matrix<Rows, Cols, Type> &rhs) + { + lhs+=rhs; + return lhs; + } + + Matrix<Rows, Cols, Type>& operator-=(const Matrix<Rows, Cols, Type> &rhs) + { + std::vector<Type> out; + out.reserve(data_.size()); + std::transform(data_.begin(), data_.end(), rhs.begin(), std::back_inserter(out), + [] (Type a, Type b) { + return a-b; + }); + data_=std::move(out); + return *this; + } + + friend Matrix<Rows, Cols, Type> operator-(Matrix<Rows, Cols, Type> lhs, + const Matrix<Rows, Cols, Type> &rhs) + { + lhs-=rhs; + return lhs; + } + + friend std::ostream& operator<<(std::ostream &os, const Matrix<Rows, Cols, Type> &mat) + { + os<<'['; + for(std::size_t i=0; i<mat.data_.size()-1; ++i) + { + if(i%Cols == 0 && i!=0) + { + os<<"]\n["; + } + + if((i+1)%Cols == 0) + { + os<<mat.data_[i]; + } + else + { + os<<mat.data_[i]<<" "; + } + } + os<<mat.data_[mat.data_.size()-1]<<"]"; + return os; + } +}; + +template<int Rows=2, class Type=double> class Vector : public Matrix<Rows, 1, Type> +{ +public: + Vector<Rows, Type>() : Matrix<Rows, 1, Type>() {} + + Type& operator[](int col) + { + return this->data_[col]; + } + + const Type& operator[](int col) const + { + return this->data_[col]; } + + }; +template<class Type=double> class Vector2 : public Vector<2, Type> +{ +public: + Vector2<Type>() : Vector<2, Type>() {} + + Type& x() + { + return this->data_[0]; + } + + const Type& x() const + { + return this->data_[0]; + } + + Type& y() + { + return this->data_[1]; + } + + const Type& y() const + { + return this->data_[1]; + } +}; + +typedef Vector2<double> Vector2d; + +namespace matrix +{ + +template<int M, int N, int P, int Q, class T> +Matrix<M, Q, T> multiply(const Matrix<M, N, T> &m1, const Matrix<P, Q, T> &m2) +{ + if(N!=P) + { + throw std::runtime_error("Matrices don't have the right dimensions for multiplication"); + } + + Matrix<M, Q, T> res; + + + + return res; +} + +} // vector + } // yage #endif diff --git a/test/matrix_test.cpp b/test/matrix_test.cpp index e975794f..a6974c7f 100644 --- a/test/matrix_test.cpp +++ b/test/matrix_test.cpp @@ -4,14 +4,29 @@ int main() { - yage::Matrix<4, 4> matrix; + yage::Matrix<4, 4, int> m1, m2; - double x=matrix[2][2]; - matrix[2][2]=4; + yage::Vector<2, int> v1, v2; - std::cout<<"at: "<<x<<", "<<matrix[2][2]<<'\n'; + yage::Vector2d v3; - if(matrix[2][2]==4 && x==0) - return 0; - return 1; + m1[0][1]=1; + m2[1][1]=2; + + v1[0]=2; + v1[1]=3; + v2[0]=5; + v2[1]=2; + + v2 += v1+v1; + + v3.x() = 2; + v3.y() = 1; + + yage::matrix::multiply(m1, m2); + + std::cout<<m2<<'\n'; + std::cout<<v3<<'\n'; + + return 0; } |