A Groovy array question

Help
Pete
2015-08-14
2015-08-16
  • Pete

    Pete - 2015-08-14

    Hi!

    Here I've got an example of a difference between BSh and Groovy -- I ended up helping myself on this one, but I thought I'd post it anyway :D This is a scripted object.

    In BeanShell:

    addElement(Vec3 p0, Vec3 p1)
    {
        Tube t = new Tube(new Vec3[]{p0,p1},0.1,Tube.INTERPOLATING,Tube.FLAT_ENDS);
        script.addObject(t, new CoordinateSystem());
    }
    
    Vec3 p0 = new Vec3(), p1 = new Vec3(0,1,0);
    addElement(p0,p1);
    

    To make it palatible to Groovy, I had to write:

    void addElement(Vec3 p0, Vec3 p1)
    {
        Vec3[] v = new Vec3[2];
        v[0] = p0;
        v[1] = p1;
        Tube t = new Tube(v,0.1,Tube.INTERPOLATING,Tube.FLAT_ENDS);
        script.addObject(t, new CoordinateSystem());
    }
    
    Vec3 p0 = new Vec3(), p1 = new Vec3(0,1,0);
    addElement(p0,p1);
    

    The above is acceptable for both languages.

    It turns out that the expression new Vec3(){p0,p1} in it's groovy form is [p0,p1] as Vec3[], like so:

    void addElement(Vec3 p0, Vec3 p1)
    {
        Tube t = new Tube([p0,p1]as Vec3[],0.1,Tube.INTERPOLATING,Tube.FLAT_ENDS);
        script.addObject(t, new CoordinateSystem());
    }
    
    Vec3 p0 = new Vec3(), p1 = new Vec3(0,1,0);
    addElement(p0,p1);
    

    In the original context the add.element() is going to be used up to a few million times for each rendered frame, so getting rid of variables, that are used only once seemed relevant. -- Having said that, there is much more to be optimized in that code and certainly this was not the only syntax issue, but maybe the key-one. ;)

     
  • Peter Eastman

    Peter Eastman - 2015-08-14

    If I recall correctly, you can also write "(Vec3[]) [p0, p1]".

    Peter

     
  • Pete

    Pete - 2015-08-16

    Thanks. Looks nicer. :)

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks