World of Rigid Bodies (WoRB)
|
Represents a simple matrix class that keeps components in a column-major order. More...
#include <mexWoRB.h>
Public Member Functions | |
Matrix (mwIndex rowCount, mwIndex columnCount) | |
Creates an uninitialized matrix allocated using mxAlloc . | |
Matrix (int nArgIn, const mxArray *argIn[], int argNo, const char *argDesc) | |
Constructs the matrix from a mexFunction argument given by the argument number. | |
Matrix (const char *varName, const char *workspace="caller") | |
Constructs the matrix from MATLAB variable from the specified workspace. | |
Matrix (const mxArray *obj, mwIndex index, const char *fieldName) | |
Constructs the matrix from the given MATLAB structure field. | |
~Matrix () | |
Destructor. | |
operator mxArray * () | |
Converts an instance to a MATLAB double matrix (allocated on MATLAB heap) | |
bool | IsEmpty () const |
Returns true if the instance is an empty array. | |
bool | IsScalar () const |
Returns true if the instance is a scalar (1x1 matrix). | |
bool | IsVector () const |
Returns true if the instance is a vector (1xM or Nx1 matrix) or a scalar. | |
bool | IsRowVector () const |
Returns true if the instance is a row vector (1xM matrix). | |
bool | IsColumnVector () const |
Returns true if the instance is a column vector (Nx1 matrix). | |
bool | IsFullMatrix () const |
Returns true if the instance is non-empty matrix that is not a vector. | |
bool | IsSize (mwIndex M, mwIndex N) const |
Checks the sizes of each dimension of the matrix. | |
bool | IsValidRow (mwIndex r) const |
Returns true if the specified index points to a valid row. | |
bool | IsValidColumn (mwIndex c) const |
Returns true if the specified index points to a valid column. | |
mwIndex | Length () const |
Returns the length of the (linear) array of the matrix. | |
mwIndex | GetM () const |
Gets the number of rows in the matrix. | |
mwIndex | GetN () const |
Gets the number of columns in the matrix. | |
mwSize | MemorySize () const |
Gets the memory size (in bytes) occupied by array. | |
void | VerifyDims (mwIndex M, mwIndex N, const char *error_message) const |
Verifies sizes of each matrix dimension. | |
double & | operator() (mwIndex ix) |
Returns matrix element given by the linear index. | |
const double & | operator() (mwIndex ix) const |
Returns matrix element given by the linear index. | |
double & | operator() (mwIndex r, mwIndex c) |
Returns matrix element given by row and column index. | |
const double & | operator() (mwIndex r, mwIndex c) const |
Returns matrix element given by row and column index. | |
Private Member Functions | |
void | Initialize (const mxArray *arg, const char *argDesc) |
Initializes the matrix to refer to the given MATLAB array. | |
Private Attributes | |
mwIndex | NRows |
Holds the number of rows in matrix. | |
mwIndex | NCols |
Holds the number of columns in matrix. | |
double * | Data |
Holds the matrix data in column-major order. |
Represents a simple matrix class that keeps components in a column-major order.
Mex::Matrix::Matrix | ( | mwIndex | rowCount, |
mwIndex | columnCount | ||
) | [inline] |
Creates an uninitialized matrix allocated using mxAlloc
.
Definition at line 65 of file mexWoRB.h.
References Data, and MemorySize().
: NRows( rowCount ) , NCols( columnCount ) , Data( NULL ) { Data = (double*)mxMalloc( MemorySize () ); }
Mex::Matrix::Matrix | ( | int | nArgIn, |
const mxArray * | argIn[], | ||
int | argNo, | ||
const char * | argDesc | ||
) | [inline] |
Constructs the matrix from a mexFunction
argument given by the argument number.
Definition at line 75 of file mexWoRB.h.
References Initialize().
: NRows( 0 ) , NCols( 0 ) , Data( NULL ) { if ( argNo < nArgIn ) { Initialize( argIn[ argNo ], argDesc ); } }
Mex::Matrix::Matrix | ( | const char * | varName, |
const char * | workspace = "caller" |
||
) | [inline] |
Constructs the matrix from MATLAB variable from the specified workspace.
Definition at line 87 of file mexWoRB.h.
References Initialize().
{ Initialize( mexGetVariable( workspace, varName ), varName ); }
Mex::Matrix::Matrix | ( | const mxArray * | obj, |
mwIndex | index, | ||
const char * | fieldName | ||
) | [inline] |
Constructs the matrix from the given MATLAB structure field.
Definition at line 94 of file mexWoRB.h.
References Initialize().
{ Initialize( mxGetField( obj, index, fieldName ), fieldName ); }
Mex::Matrix::~Matrix | ( | ) | [inline] |
mwIndex Mex::Matrix::GetM | ( | ) | const [inline] |
Gets the number of rows in the matrix.
Definition at line 200 of file mexWoRB.h.
References NRows.
Referenced by WoRB_MexFunction::CreateResultMatrix().
{ return NRows; }
mwIndex Mex::Matrix::GetN | ( | ) | const [inline] |
Gets the number of columns in the matrix.
Definition at line 207 of file mexWoRB.h.
References NCols.
Referenced by WoRB_MexFunction::CreateResultMatrix().
{ return NCols; }
void Mex::Matrix::Initialize | ( | const mxArray * | arg, |
const char * | argDesc | ||
) | [inline, private] |
Initializes the matrix to refer to the given MATLAB array.
This method does not copy data from the given MATLAB array.
Definition at line 37 of file mexWoRB.h.
References Data, NCols, NRows, and WoRB::SevereError().
Referenced by Matrix().
{ if ( arg == NULL ) { return; } // We expect a scalar, vector or a matrix of real numbers. // int ndim = mxGetNumberOfDimensions( arg ); if ( ! mxIsDouble( arg ) || mxIsComplex( arg ) || ndim > 2 ) { WoRB::SevereError( "WoRB:invarg", "'%s' must be a scalar, vector or a matrix of real numbers.", argDesc ); } // Get the matrix dimensions and the first element of the real data const mwSize* dims = mxGetDimensions( arg ); NRows = ndim >= 1 ? dims[0] : 1; NCols = ndim >= 2 ? dims[1] : 1; Data = mxGetPr( arg ); }
bool Mex::Matrix::IsColumnVector | ( | ) | const [inline] |
bool Mex::Matrix::IsEmpty | ( | ) | const [inline] |
bool Mex::Matrix::IsFullMatrix | ( | ) | const [inline] |
bool Mex::Matrix::IsRowVector | ( | ) | const [inline] |
bool Mex::Matrix::IsScalar | ( | ) | const [inline] |
bool Mex::Matrix::IsSize | ( | mwIndex | M, |
mwIndex | N | ||
) | const [inline] |
Checks the sizes of each dimension of the matrix.
Definition at line 172 of file mexWoRB.h.
Referenced by WoRB_MexFunction::Parse(), and VerifyDims().
bool Mex::Matrix::IsValidColumn | ( | mwIndex | c | ) | const [inline] |
bool Mex::Matrix::IsValidRow | ( | mwIndex | r | ) | const [inline] |
bool Mex::Matrix::IsVector | ( | ) | const [inline] |
mwIndex Mex::Matrix::Length | ( | ) | const [inline] |
Returns the length of the (linear) array of the matrix.
Definition at line 193 of file mexWoRB.h.
Referenced by operator()(), and VerifyDims().
mwSize Mex::Matrix::MemorySize | ( | ) | const [inline] |
Mex::Matrix::operator mxArray * | ( | ) | [inline] |
Converts an instance to a MATLAB double matrix (allocated on MATLAB heap)
Definition at line 108 of file mexWoRB.h.
References Data, NCols, and NRows.
{ // Note: The array creation with mxCreateDoubleMatrix initializes // the array memory by filling it with zeros. The following code avoids // this unnecessary initialization. // Create an uninitialized empty array mxArray* result = mxCreateDoubleMatrix( 0, 0, mxREAL ); // Set dimensions (note that either of NRows/NCols may be 0) mxSetM( result, NRows ); // Set M = row count mxSetN( result, NCols ); // Set N = column count if ( NRows && NCols ) { mxSetData( result, Data ); // Set mxArray data to point to output } return result; }
double& Mex::Matrix::operator() | ( | mwIndex | ix | ) | [inline] |
Returns matrix element given by the linear index.
Definition at line 239 of file mexWoRB.h.
References Data, Length(), NCols, NRows, and WoRB::SevereError().
{ if ( ix < 0 || ix >= Length () ) { WoRB::SevereError( "Matrix:elem:invdim", "Linear index %d outside dimensions [ %d × %d ]", ix, NRows, NCols ); } return Data[ ix ]; }
const double& Mex::Matrix::operator() | ( | mwIndex | ix | ) | const [inline] |
Returns matrix element given by the linear index.
Definition at line 253 of file mexWoRB.h.
References Data, Length(), NCols, NRows, and WoRB::SevereError().
{ if ( ix < 0 || ix >= Length () ) { WoRB::SevereError( "Matrix:elem:invdim", "Linear index %d outside dimensions [ %d × %d ]", ix, NRows, NCols ); } return Data[ ix ]; }
double& Mex::Matrix::operator() | ( | mwIndex | r, |
mwIndex | c | ||
) | [inline] |
Returns matrix element given by row and column index.
Definition at line 267 of file mexWoRB.h.
References Data, Length(), NCols, NRows, and WoRB::SevereError().
{ mwIndex ix = r + c * NRows; if ( ix < 0 || ix >= Length () ) { WoRB::SevereError( "Matrix:elem:invdim", "Index (%d,%d) outside dimensions [ %d × %d ]", r, c, NRows, NCols ); } return Data[ ix ]; }
const double& Mex::Matrix::operator() | ( | mwIndex | r, |
mwIndex | c | ||
) | const [inline] |
Returns matrix element given by row and column index.
Definition at line 282 of file mexWoRB.h.
References Data, Length(), NCols, NRows, and WoRB::SevereError().
{ mwIndex ix = r + c * NRows; if ( ix < 0 || ix >= Length () ) { WoRB::SevereError( "Matrix:elem:invdim", "Index (%d,%d) outside dimensions [ %d × %d ]", r, c, NRows, NCols ); } return Data[ ix ]; }
void Mex::Matrix::VerifyDims | ( | mwIndex | M, |
mwIndex | N, | ||
const char * | error_message | ||
) | const [inline] |
Verifies sizes of each matrix dimension.
Reports severe error if dimensions do not agree.
Definition at line 222 of file mexWoRB.h.
References IsSize(), Length(), NCols, NRows, and WoRB::SevereError().
Referenced by WoRB_MexFunction::Parse().
{ if ( IsSize( M, N ) || ( Length () == 0 && M * N == 0 ) ) { return; // OK } WoRB::SevereError( "Matrix:VerifyDims:invsize", "%s; Required dimensions [ %d × %d ] have only [ %d × %d ]", error_message, M, N, NRows, NCols ); }
double* Mex::Matrix::Data [private] |
Holds the matrix data in column-major order.
Definition at line 32 of file mexWoRB.h.
Referenced by Initialize(), Matrix(), operator mxArray *(), and operator()().
mwIndex Mex::Matrix::NCols [private] |
Holds the number of columns in matrix.
Definition at line 28 of file mexWoRB.h.
Referenced by GetN(), Initialize(), IsColumnVector(), IsEmpty(), IsFullMatrix(), IsRowVector(), IsScalar(), IsSize(), IsValidColumn(), IsVector(), Length(), MemorySize(), operator mxArray *(), operator()(), and VerifyDims().
mwIndex Mex::Matrix::NRows [private] |
Holds the number of rows in matrix.
Definition at line 24 of file mexWoRB.h.
Referenced by GetM(), Initialize(), IsColumnVector(), IsEmpty(), IsFullMatrix(), IsRowVector(), IsScalar(), IsSize(), IsValidRow(), IsVector(), Length(), MemorySize(), operator mxArray *(), operator()(), and VerifyDims().