Template Class Matrix

Class Documentation

template<class T>
class Polylidar::Matrix

This class hold a generic matrix datastructure used to hold much of the data in our meshes (vertices, triangles, etc.). It can own the data or it may not. It can also take ownserhip of a memory buffer. Over time I have become concerned with this datastructure because of this flexibility which can bring bugs if one is not careful.

Public Functions

inline Matrix(T *ptr_, size_t rows_, size_t cols_)

Construct a new Matrix< T> object.

Parameters
  • ptr_ – Raw pointer to underlying data we DON’T own

  • rows_

  • cols_

inline Matrix()

Construct a new Matrix< T> object, everything is empty but is owned.

inline Matrix(std::vector<T> &&old_vector, size_t rows_, size_t cols_)

Construct a new Matrix< T> object. We will take ownership of this data buffer.

Parameters
  • old_vector – Old vector that will moved and managed by this new Matrix Object

  • rows_

  • cols_

~Matrix() = default
inline Matrix(Matrix<T> &a)

Construct a new Matrix< T> object, Copy Constructor. Will take an existing matrix and will perform a copy. If the data is owned it will be copied, if not it wont be copied.

Parameters

a

inline Matrix(const Matrix<T> &a)

Construct a new Matrix< T> object, Copy Constructor. Will take an existing matrix and will perform a copy. If the data is owned it will be copied, if not it wont be copied.

Parameters

a

Matrix(Matrix<T> &&other) = default

Default move constructor, take ownership of all the data of other

Parameters

other

inline Matrix<T> &operator=(const Matrix<T> &a)

Copy assignment operator This one is a little tricky. We make a copy of every element in the data. However if we own the data we reassign ptr to belong to our new copied buffer.

Parameters

a

Returns

Matrix<T>&

inline void UpdatePtrFromData()

Simple helper function to change our ptr to point to our own buffer.

inline void UpdatePtrFromData(const size_t rows_, const size_t cols_)

Simply updates our rows and columns and data.

Parameters
  • rows_

  • cols_

inline const T &operator()(size_t i, size_t j) const

Acces an element of the Matrix using 2D indices.

Parameters
  • i

  • j

Returns

const T&

inline const T &operator()(size_t index) const

Access an element in the matrix using its underlying buffer 1D index.

Parameters

index

Returns

const T&

Public Members

bool own_data

Does the matrix own the data pointed to by ptr. If yes then ptr == data.data().

std::vector<T> data

Buffer of memory that may/may-not be used based upon own_data flag

T *ptr

This is raw pointer that never needs to be freed. It either point to memory in data which will be automatically freed during object destruction (own_data = true). Or it points some other managed memory controlled by the user (own_data = false)

size_t rows

Rows in the matrix

size_t cols

Columns in the matrix

Public Static Functions

template<class G>
static inline Matrix<T> CopyFromDifferentType(G *ptr_from, size_t rows, size_t cols)

This actually performs a memory copy from an unknown buffer of one type (G) to our buffer of a different type (T). We own this new copied data.

Template Parameters

G

Parameters
  • ptr_from

  • rows

  • cols

Returns

Matrix<T>