Browse Source

Added slice function for matrix.

master
James Goppert 9 years ago
parent
commit
5a01e6c939
  1. 16
      README.md
  2. 7
      matrix/Matrix.hpp
  3. 3
      test/CMakeLists.txt
  4. 25
      test/slice.cpp

16
README.md

@ -32,6 +32,8 @@ A simple and efficient template based matrix library.
## Example ## Example
See the test directory for detailed examples. Some simple examples are included below:
```c++ ```c++
// define an euler angle (Body 3(yaw)-2(pitch)-1(roll) rotation) // define an euler angle (Body 3(yaw)-2(pitch)-1(roll) rotation)
float roll = 0.1f; float roll = 0.1f;
@ -91,4 +93,18 @@ A simple and efficient template based matrix library.
// correction // correction
x += K*r; x += K*r;
// slicing
float data[9] = {0, 2, 3,
4, 5, 6,
7, 8, 10
};
SquareMatrix<float, 3> A(data);
// Slice a 3,3 matrix starting at row 1, col 0,
// with size 2 x 3, warning, no size checking
Matrix<float, 2, 3> B(A.slice<2, 3>(1, 0));
// this results in:
// 4, 5, 6
// 7, 8, 10
``` ```

7
matrix/Matrix.hpp

@ -317,6 +317,13 @@ public:
return transpose(); return transpose();
} }
template<size_t P, size_t Q>
Matrix<Type, P, Q> slice(size_t x0, size_t y0) const
{
Matrix<Type, P, Q> res(&(_data[x0][y0]));
return res;
}
void setZero() void setZero()
{ {
memset(_data, 0, sizeof(_data)); memset(_data, 0, sizeof(_data));

3
test/CMakeLists.txt

@ -1,6 +1,7 @@
set(tests set(tests
setIdentity setIdentity
inverse inverse
slice
matrixMult matrixMult
vectorAssignment vectorAssignment
matrixAssignment matrixAssignment
@ -33,4 +34,4 @@ if (${CMAKE_BUILD_TYPE} STREQUAL "Profile")
WORKING_DIRECTORY ${CMAKE_BUILD_DIR} WORKING_DIRECTORY ${CMAKE_BUILD_DIR}
DEPENDS test_build DEPENDS test_build
) )
endif() endif()

25
test/slice.cpp

@ -0,0 +1,25 @@
#include <stdio.h>
#include <matrix/math.hpp>
#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<float, 3> A(data);
Matrix<float, 2, 3> B_check(data_check);
Matrix<float, 2, 3> 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 : */
Loading…
Cancel
Save