From f26f97b06aa071009dcb11f618d5f9d03c201156 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Thu, 8 Jun 2017 10:12:20 +0100 Subject: Continuing the matrix class --- .clang-format | 96 ++++++++++++++++++++++++++ CMakeLists.txt | 9 ++- include/YAGE/Math/matrix.hpp | 156 +++++++++++++++++++++++++++++++++++++++++-- test/matrix_test.cpp | 29 ++++++-- 4 files changed, 275 insertions(+), 15 deletions(-) create mode 100644 .clang-format 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 +#include +#include +#include #include namespace yage @@ -15,15 +17,20 @@ namespace detail template class Row { private: - std::shared_ptr> parent_; + Matrix *parent_; int index_; public: - Row(std::shared_ptr> parent, int index) : + Row(Matrix *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 class Matrix { friend class detail::Row; -private: +protected: std::vector data_; public: Matrix() : data_(Rows*Cols) {} - Matrix(int rows, int cols) : data_(rows*cols) {} detail::Row operator[](int row) { - return detail::Row(std::make_shared>(*this), row); + return detail::Row(this, row); + } + + Matrix& operator=(const Matrix &other) + { + if(this!=&other) + { + data_=other.data_; + } + return *this; + } + + Matrix& operator+=(const Matrix &rhs) + { + std::vector 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 operator+(Matrix lhs, + const Matrix &rhs) + { + lhs+=rhs; + return lhs; + } + + Matrix& operator-=(const Matrix &rhs) + { + std::vector 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 operator-(Matrix lhs, + const Matrix &rhs) + { + lhs-=rhs; + return lhs; + } + + friend std::ostream& operator<<(std::ostream &os, const Matrix &mat) + { + os<<'['; + for(std::size_t i=0; i class Vector : public Matrix +{ +public: + Vector() : Matrix() {} + + Type& operator[](int col) + { + return this->data_[col]; + } + + const Type& operator[](int col) const + { + return this->data_[col]; } + + }; +template class Vector2 : public Vector<2, Type> +{ +public: + Vector2() : 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 Vector2d; + +namespace matrix +{ + +template +Matrix multiply(const Matrix &m1, const Matrix &m2) +{ + if(N!=P) + { + throw std::runtime_error("Matrices don't have the right dimensions for multiplication"); + } + + Matrix 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: "<