Convert Jitter Matrix to Eigen Matrix C++

Samuel Parke-Wolfe's icon

I'm a C++ and have made max externals before.

This is my first time trying to make a jitter object and I'm suffering trying to traverse through a N-dimensional jitter matrix.

I want to work with the Eigen matrix library which I'm used to.
I thought I could just write the jitter matrix into an Eigen matrix and then back again but I'm having a lot of trouble trying to accomplish the conversions.

For example, I want to be able to send a jitter matrix to my object of any size e.g. 1x28x28 with any plane size e.g. 3, convert the input jitter matrix to an Eigen matrix with size 1x28x28x3, do some processing on the Eigen matrix, write the Eigen matrix back to an output jitter matrix.

All the examples in the SDK use the recursive "jit_objectname_calculate_ndim" function to loop over a n-dimentional array.
This breaks down the input matrix and output matrix recursively into smaller vectors to process.
I'm finding these examples really hard to breakdown and use.

Max Gardener's icon

Might I politely suggest that you open your Package Manager and download and investigate the min-devkit (a recent package release for C++ developers)?

Samuel Parke-Wolfe's icon

Thanks for your reply! I'm checking it out now, it seems to only be available for Max 8 and I'm on Max 7.
Edit: I built it for Max 7 anyway and it didn't work, I'm still holding out for a conversion method but it looks like I'll buy an upgrade if not.

Rob Ramirez's icon

happy to try and answer any questions you have on accessing matrix data in an external.

Samuel Parke-Wolfe's icon

Thanks Rob!

I guess the question which if answered would help a lot is:

How can I access each cell of a multi-dimensional jitter matrix whilst simultaneously having it's dimensional coordinate? (In C / C++)

e.g. in a matrix which is 20x20, how can get each value of each of the 400 values and their multi-dimensional coordinate e.g. "0, 1", "0, 2" ... "19, 18", "19, 19"?

This would be for reading and writing to a multi-dimensional jitter matrix using dimensional co-ordinates co-ordinates?

Timothy Place's icon

Hi,

Just confirming that yes, the Min Development Kit requires Max 8.

Cheers,
Tim

Rob Ramirez's icon

simple for loops are all you need, one for columns and one for rows

char *in_bp;
char *ip;
jit_object_method(in_matrix,_jit_sym_getdata,&in_bp);
for (i=0; i<height; i++) {
    ip = in_bp + i*in_minfo->dimstride[1];
    for (j=0; j<width; j++) {
        // you are now retrieving values for cell j i
        char plane0val = (*ip++);
        char plane1val = (*ip++);
        // continue planecount times to get all plane values

    }
}
Rob Ramirez's icon

if you want a specific cell i j

ip = in_bp + j*in_minfo->dimstride[1] + i*in_minfo->dimstride[0];

Samuel Parke-Wolfe's icon

Thank you very much for your reply!!

This is really going to help me a lot.

But how would I scale this up for a variable number of dimensions rather than just 2?
e.g. 1x30x200x200 or more?

I assume I would do more nested for loops, but what would this line become:
ip = in_bp + i*in_minfo->dimstride[1];

EDIT:
Sorry I just saw your second reply
So would my code look like this for a 3 dimensional input?
Adding another for loop and pointer addition for every dimension?

for (i=0; i<dim3; i++) {
for (j=0; j<dim2; j++) {
     for (k=0; k<dim1; k++) {
ip = in_bp + k*in_minfo->dimstride[2] + j*in_minfo->dimstride[1] + i*in_minfo->dimstride[0];
         char plane0val = (*ip++);
         char plane1val = (*ip++);
     }
}
}

Samuel Parke-Wolfe's icon
It works!! 
Thank you very much Rob!!
Here is code which I just wrote and tested in the simplejit project!

It will take a 3 dimensional matrix with a plane size of 4 and set it's output matrix to it.


char* ip;
char* op;

for (int i = 0; i < in_minfo.dim [0]; i++)
{
    for (int j = 0; j < in_minfo.dim [1]; j++)
    {
        for (int k = 0; k < in_minfo.dim [2]; k++)
        {
            ip = in_bp + (k * in_minfo.dimstride[2]) + (j * in_minfo.dimstride[1]) + (i * in_minfo.dimstride[0]);
            op = out_bp + (k * out_minfo.dimstride[2]) + (j * out_minfo.dimstride[1]) + (i * out_minfo.dimstride[0]);
                
            *op = *ip;
            op++; ip++;
            
            *op = *ip;
            op++; ip++;
            
            *op = *ip;
            op++; ip++;

            *op = *ip;
            op++; ip++;
        }
    }
}