copy from a portion of matrix to another portion of a matrix in javascript

Memo Akten's icon

Hi all, i'm trying to do what the title says. Specifically I have a 3D matrix (with 4 planes), and in javascript I'd like to slice a plane (not MAX plane, but actual plane of the 3D dimensions) and copy it into another matrix. In fact, depending on various options, I'd like to copy directly, or do more complex operations like weighted average of multiple src matrices with the current cells in the destination matrix etc.

i.e.

    temp_matrix = src_matrix[src_region]
    OR
    temp_matrix = (src_matrix1[src_region1] + src_matrix2[src_region2] ... src_matrixN[src_regionN]) / N 

    if(option1) dst_matrix[dst_region] = temp_matrix;
    else dst_matrix[dst_region] = (dst_matrix[dst_region]  + temp_matrix) * smoothing_factor;
    etc.

I saw the post linked to below and @Jesse's answer which I tried to adapt with dstdims but couldn't get it to work. Below is the modded example code and patch. Setting the dst dims in the outputted jit.window isn't an option, because I have many dst dims and I'll be iterating these inside the javascript patch.
https://cycling74.com/forums/using-jit-submatrix-in-javascript/

var src_start = [2, 2];
var dim_start = [3, 4];
var copy_size = [4, 4];

var temp = new JitterMatrix( 4, "char", 20, 20 );

function jit_matrix( x )
{        
        temp.usesrcdim = true;
        temp.srcdimstart = src_start;
        temp.srcdimend = [src_start[0] + copy_size[0], src_start[1] + copy_size[1]];

        temp.clear();
        
        temp.usesdstdim = true;
        temp.dstdimstart = dim_start;
        temp.dstdimend = [dim_start[0] + copy_size[0], dim_start[1] + copy_size[1]];

        temp.frommatrix( x );

        temp.usessrcdim = false;
        temp.usesdstdim = false;
        
        outlet( 0, "jit_matrix", temp.name );
}
Max Patch
Copy patch and select New From Clipboard in Max.

`

Rob Ramirez's icon

it seems to work as expected. you should note that srcdimstart/end values are inclusive, meaning if you want to start at 0 and copy 4 cells, then srcdimend should be 3 (0,1,2,3)

Memo Akten's icon
Max Patch
Copy patch and select New From Clipboard in Max.

Hi Rob, thanks for checking it out, and pointing out my off by one error. I've fixed that. However I'm still getting incorrect results. Here is a screenshot with an updated patch that shows what I'm getting and what I think I should be getting.

Javascript fixed for the off by one error:

var src_start = [2, 2];
var dim_start = [3, 5];
var copy_size = [4, 4];

var temp = new JitterMatrix(4, "char", 10, 10);

function jit_matrix( x )
{
    temp.usesrcdim = true;
    temp.srcdimstart = src_start;
    temp.srcdimend = [src_start[0] + copy_size[0]-1, src_start[1] + copy_size[1]-1];
    
    temp.clear();
    
    temp.usesdstdim = true;
    temp.dstdimstart = dim_start;
    temp.dstdimend = [dim_start[0] + copy_size[0]-1, dim_start[1] + copy_size[1]-1];
    
    temp.frommatrix( x );
    
    temp.usessrcdim = false;
    temp.usesdstdim = false;
    
    outlet( 0, "jit_matrix", temp.name );
}

Screenshot-2015-10-27-19.02.15.png
png
Memo Akten's icon

argh DAMN YOU JAVASCRIPT! I found my error. I wrote 'temp.usesdstdim = true;' instead of 'temp.usedstdim = true;'. And instead of giving me a nice error, or even a warning, javascript went ahead and happily created a new object called usesdstdim and I wasted my whole day on this :/