From 5a01e6c93947d86e0a7021ae596530a31ba9bb92 Mon Sep 17 00:00:00 2001 From: James Goppert Date: Tue, 16 Feb 2016 12:22:08 -0500 Subject: [PATCH] Added slice function for matrix. --- README.md | 16 ++++++++++++++++ matrix/Matrix.hpp | 7 +++++++ test/CMakeLists.txt | 3 ++- test/slice.cpp | 25 +++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 test/slice.cpp diff --git a/README.md b/README.md index 3da2001634..cf5c05c62c 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,8 @@ A simple and efficient template based matrix library. ## Example +See the test directory for detailed examples. Some simple examples are included below: + ```c++ // define an euler angle (Body 3(yaw)-2(pitch)-1(roll) rotation) float roll = 0.1f; @@ -91,4 +93,18 @@ A simple and efficient template based matrix library. // correction x += K*r; + // slicing + float data[9] = {0, 2, 3, + 4, 5, 6, + 7, 8, 10 + }; + SquareMatrix A(data); + + // Slice a 3,3 matrix starting at row 1, col 0, + // with size 2 x 3, warning, no size checking + Matrix B(A.slice<2, 3>(1, 0)); + + // this results in: + // 4, 5, 6 + // 7, 8, 10 ``` diff --git a/matrix/Matrix.hpp b/matrix/Matrix.hpp index dedd2065a7..4991002fd3 100644 --- a/matrix/Matrix.hpp +++ b/matrix/Matrix.hpp @@ -317,6 +317,13 @@ public: return transpose(); } + template + Matrix slice(size_t x0, size_t y0) const + { + Matrix res(&(_data[x0][y0])); + return res; + } + void setZero() { memset(_data, 0, sizeof(_data)); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 28c55533de..2cff34c3ac 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,6 +1,7 @@ set(tests setIdentity inverse + slice matrixMult vectorAssignment matrixAssignment @@ -33,4 +34,4 @@ if (${CMAKE_BUILD_TYPE} STREQUAL "Profile") WORKING_DIRECTORY ${CMAKE_BUILD_DIR} DEPENDS test_build ) -endif() \ No newline at end of file +endif() diff --git a/test/slice.cpp b/test/slice.cpp new file mode 100644 index 0000000000..1f0bf99819 --- /dev/null +++ b/test/slice.cpp @@ -0,0 +1,25 @@ +#include + +#include +#include "test_macros.hpp" + +using namespace matrix; + +int main() +{ + float data[9] = {0, 2, 3, + 4, 5, 6, + 7, 8, 10 + }; + float data_check[6] = { + 4, 5, 6, + 7, 8, 10 + }; + SquareMatrix A(data); + Matrix B_check(data_check); + Matrix B(A.slice<2, 3>(1, 0)); + TEST(isEqual(B, B_check)); + return 0; +} + +/* vim: set et fenc=utf-8 ff=unix sts=0 sw=4 ts=4 : */