JavaScripte - Convert 2d Structure to 1d and back.

bernardo's picture

hi can i do this in javascript?

point1:
      x:0.00023
      y:0.00433
      z:0.02023
    point2:
      x:0.10023
      y:0.00023
      z:0.00023
(...)
INTO:THIS:
    (1)point1:
    (2)x:0.00023
    (3)y:0.00433
    (4)z:0.02023
    (5)point2:
    (6)x:0.10023
    (7)y:0.00023
    (8)z:0.00023

thanks

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

dust's picture
Re: JavaScripte - Convert 2d Structure to 1d and back.

yes java script would maybe be the preferred method of most for something like this. you could however use kineme structure maker as well. parse out your points with a structure index and then re assemble them with kineme structure maker if you would rather not use java script. both are simple but the java script would be predicated on some requisite knowledge of the C language in order to iterate through your loop with your index offsets. you can do the same with structure maker and a iterator in qc. im not sure which is the fastest performance wise, probably java script. here is an example of how to take a 4d structure and split it into 4 individual structures you will want to substitute the queues this might help give you an idea of how to do this without java script.

PreviewAttachmentSize
structure_loop.qtz31.74 KB

bernardo's picture
Re: JavaScripte - Convert 2d Structure to 1d and back.

thanks i am going the javascript way:

var result = new Object();
result.OneDStruc = new Array();
 
function (__structure OneDStruc) main (__structure TWODStruc, __index StrucCount, __number childCount)
{
    var i = 0;
 
    for(i=0;i<TWODStruc.length;i++)
 
    {
 
       for (a = 0; a< TWODStruc[i].length; a++){
              OneDStruc[i]=TWODStruc[i][a];
 
      }
 
 
    }
 
    return result;
}

somehow i cant get to work i found a javascript framework that simply flattens the multidimensional array but i dont know how to implement it.

dust's picture
Re: JavaScripte - Convert 2d Structure to 1d and back.

q=[] function (__structure s1d) main (__structure s2d, __index size, __number len) { var result = new Object();

var j = 0;

//var c = 0;

for(var i=0;i<size;i++){

for(var n=0;n<len;n++){

j=s2d[i][n];

q.push(j);

//q[c++]=s2d[i][n];

} }

result.s1d=q; return result;

}

//you can push like a std vector. attached is sending OSC i used the structure //maker to make a structure to parse because i have no idea how your getting //your values. i will leave that up to you.

PreviewAttachmentSize
structure_loop.qtz27.9 KB

bernardo's picture
Re: JavaScripte - Convert 2d Structure to 1d and back.

uau dust thanks alot!

bernardo's picture
Re: JavaScripte - Convert 2d Structure to 1d and back.

and how can i reverse it from 1d to 2d parsing it in anyway possible?

dust's picture
Re: JavaScripte - Convert 2d Structure to 1d and back.

if you have snow leopard look at the ribbon example file this will show you how to push your variables into a 2d structure

bernardo's picture
Re: JavaScripte - Convert 2d Structure to 1d and back.

a time to share: sorry for the delay:

your 2d to 2d modified:

function (__structure s1d) main (__structure s2d, __index size, __number len) 
{ 
   var result = new Object();
   var j = 0;
   var f=0;
   q=new Array();
 
   for(var i=0;i<size;i++)   
   {
   f="point00" + i;
   q.push(f); //pass the point names
 
      for(var n=0;n<len;n++)
      {
            j=s2d[i][n];
            q.push(j);
         }
 
      }
 
   result.s1d=q; 
   return result;
 
}

and the 1d to 2d: but i cant seem o pass the point names so here it is anyway.

var result = new Object();
result.s2d = new Array();
 
function (__structure s2d) main (__structure s1d, __index size, __number len) 
{ 
   var item = new Number;
   var secStruct = new Array();
 
   for(var i=0;i<size;i++){
 
      secStruct[i] = new Object;
 
      for(var n=0;n<len;n++)
      {
      item = i * len + n;    
 
          secStruct[i] [n] = s1d[item];
 
 
        }
   result.s2d = secStruct;
   }
   return result;
 
}

thanks dust.