Jump to content

VEX Point Cloud export/write


aghourab

Recommended Posts

Hello All,

I have a set of points and Im trying to find the point nearest to the first point (ptnum == 0), but I also then need to edit the color of the point we established was closest to the first point.

Im trying to do this in a single VOP. The point of the system is to allow me to then create a line between my first point and its nearest nieghbor, change the color value of the nieghbor ( decrements every frame untill it is zero), then connect to the next nearest niegbor and repeat untill all points are colored black.

I am currently using a sop solver with a vex node that takes a single input, and although I am able to connect to the nearest point, I have been unable to edit its values.

I also get a lot of wierdness (new to vex really), how is it that after my first while loop, my if statement evaluates as always true in my second while loop???

Any ideas? :)

Thanks in advance!

Vex Code

closestPoint(string pcFilename="op:`opinputpath('.',0)`";int maxPoints = 10)
{

    // The position of ptnum 0
    vector startingPoint;
    import("P", startingPoint,0,0); 

    // Open point clous
    int handle = pcopen(pcFilename,"P",startingPoint,10,maxPoints);
    // Pointnumber of closest point
    int pointClose;

    // Distance to current point
    vector distance = 0;

    // Distance to closest point
    vector distanceMin = 0 ;


    // If point cloud open, then->
    if( pciterate(handle) )
    {
        // Loop through all surrounding points
        while( pciterate(handle) )
        {

            // Current points position and charge
            vector pos, charge;

            // Importing values for the current point position and 
            pcimport( handle, "P", pos);
            pcimport( handle, "charge", charge);

            // Calculate distance between first and current point
            distance = (startingPoint - pos);


            // If that distance is less than the current minimum distance evaluate
            if( length(distanceMin) < length(distance))
            {
                printf("%p\n%p\n",distance,distanceMin);
                // Assign new distance
                distanceMin = distance;

                // Assign position of closest point
                pointClose = ptnum;
            }
        }

        while( pciterate( handle ) )
        {
            // Strange, pointClose value is everything from 0 - npts, so all points are recolored:p!
            if ( pointClose == ptnum)
            {
                Cd = 0.5;
            }
        }
        addattribute ("pcMin",distanceMin);
    }
}

Edited by aghourab
Link to comment
Share on other sites

More VEX shinanigans.

Apparently you cant assign a number of float variables to a matrix, how come? Is there a way around that?

        float theta = radians(16);
        float c,s,t;
        float x1,y1,z1,x2,y2,z2,x3,y3,z3;

        c = cos(theta);
        s = sin(theta);
        t = 1-cos(theta);
        x1 = t*pow(P.x,2) + c;
        y1 = t*P.x*P.y-s*P.z;
        z1 = t*P.x*P.z + s*P.y;
        x2 = t*P.x*P.y+s*P.z;
        y2 = t*pow(P.y,2) +c;
        z2 = t*P.y*P.z - s*P.x;
        x3 = t*P.x*P.z-s*P.y;
        y3 = t*P.y*P.z+s*P.x;
        z3 = t*pow(P.z,2) + c;

        matrix rotation = { x1, y1, z1, 0, x2, y2, z3, 0, x3, y3, z3, 0, 0, 0, 0, 1};

Errors detected:

"stdin" line 23 ERROR (1000) Syntax error
        matrix rotation = { x1, y1, z1, 0, x2, y2, z3, 0, x3, y3, z3, 0, 0, 0, 0, 1};

Link to comment
Share on other sites

More VEX shinanigans.

Apparently you cant assign a number of float variables to a matrix, how come? Is there a way around that?

        float theta = radians(16);
        float c,s,t;
        float x1,y1,z1,x2,y2,z2,x3,y3,z3;

        c = cos(theta);
        s = sin(theta);
        t = 1-cos(theta);
        x1 = t*pow(P.x,2) + c;
        y1 = t*P.x*P.y-s*P.z;
        z1 = t*P.x*P.z + s*P.y;
        x2 = t*P.x*P.y+s*P.z;
        y2 = t*pow(P.y,2) +c;
        z2 = t*P.y*P.z - s*P.x;
        x3 = t*P.x*P.z-s*P.y;
        y3 = t*P.y*P.z+s*P.x;
        z3 = t*pow(P.z,2) + c;

        matrix rotation = { x1, y1, z1, 0, x2, y2, z3, 0, x3, y3, z3, 0, 0, 0, 0, 1};

Errors detected:

"stdin" line 23 ERROR (1000) Syntax error
        matrix rotation = { x1, y1, z1, 0, x2, y2, z3, 0, x3, y3, z3, 0, 0, 0, 0, 1};

Doh, second problem solved... You cant assign, must use the set method.

        matrix rotation = set( x1, y1, z1, 0, x2, y2, z3, 0, x3, y3, z3, 0, 0, 0, 0, 1);

Link to comment
Share on other sites

Hey Ahmad,

in regards to finding the nearest neighbours, you can do a pcopen and only open it for 1 or 2 points. I don't remember if the first point the pointcloud returns is itself or the first closest neighbour. So either the first or the second point is the closest - it will return the point number and from there you can import the attributes of the point using that point number. You can not set the values of the other point in the pointcloud, it's more a read-only thing.

In order to set values you have to think about it from the point of view of each individual point. You logic could be - assuming that all points are black in the beginning except for point 0 :

-> Am I black (or is my trigger 0)?

--->yes -> look for closest neighbour(s), is any of those neighbours white? If yes, make me white too and set trigger to 1. If No, do nothing.

--->No -> subtract 0.1 from your color so you will slowly fade to black over time (10 frames). If your color reaches zero (black), set trigger to 0.

The trigger is not necessary, but is helpful as you eventually might want to move away from using color and instead use attributes (that you can visualize in the color channel).

In regards to creating the lines, you can not do that in vex. You can not create new geometry with vex, so no new lines and no new points. But you can use an add sop, a blast, a delete or a python sop to de the creation and destruction of geometry.

Also vex is very fast at performing math on huge numbers of points. So often you can get away with simply processing only a group of points. And you can add points to a group within vex. So it could be a matter of specifying a subgroup of your data set to perform your calculations on. Also pointclouds only start multithreading over 256 or over 512 (can't remember exactly) number of points.

It may not be necessary to create new geometry at every frame, but instead reveal geometry at every frame. It is a different effect though. Creating geometry at every frame allows you to create completely new and undefined shapes, whereas generally in revealing you have a prebuilt shape.

Have a look at this file, it is a great yet rather advanced example of some of the operations you can do in the sense of revealing a pre built mesh:

It is a bit hard to figure out exactly what you are aiming to do. I know you are using vex and sopsolver to create some new geometry, but what is the final goal?

I would highly recommend using the add sop for building your lines (by attribute) and treat each line as a single segment at first, which means you will need two or more points lying on top of each other if you are branching out. Fusing those points together should be a post solve or post simulation operation. You want to keep your points pure and unfused during the sim.

good luck and I'm curious to see what you come up with.

  • Like 1
Link to comment
Share on other sites

Hey Ahmad,

in regards to finding the nearest neighbours, you can do a pcopen and only open it for 1 or 2 points. I don't remember if the first point the pointcloud returns is itself or the first closest neighbour. So either the first or the second point is the closest - it will return the point number and from there you can import the attributes of the point using that point number. You can not set the values of the other point in the pointcloud, it's more a read-only thing.

In order to set values you have to think about it from the point of view of each individual point. You logic could be - assuming that all points are black in the beginning except for point 0 :

-> Am I black (or is my trigger 0)?

--->yes -> look for closest neighbour(s), is any of those neighbours white? If yes, make me white too and set trigger to 1. If No, do nothing.

--->No -> subtract 0.1 from your color so you will slowly fade to black over time (10 frames). If your color reaches zero (black), set trigger to 0.

The trigger is not necessary, but is helpful as you eventually might want to move away from using color and instead use attributes (that you can visualize in the color channel).

In regards to creating the lines, you can not do that in vex. You can not create new geometry with vex, so no new lines and no new points. But you can use an add sop, a blast, a delete or a python sop to de the creation and destruction of geometry.

Also vex is very fast at performing math on huge numbers of points. So often you can get away with simply processing only a group of points. And you can add points to a group within vex. So it could be a matter of specifying a subgroup of your data set to perform your calculations on. Also pointclouds only start multithreading over 256 or over 512 (can't remember exactly) number of points.

It may not be necessary to create new geometry at every frame, but instead reveal geometry at every frame. It is a different effect though. Creating geometry at every frame allows you to create completely new and undefined shapes, whereas generally in revealing you have a prebuilt shape.

Have a look at this file, it is a great yet rather advanced example of some of the operations you can do in the sense of revealing a pre built mesh:

It is a bit hard to figure out exactly what you are aiming to do. I know you are using vex and sopsolver to create some new geometry, but what is the final goal?

I would highly recommend using the add sop for building your lines (by attribute) and treat each line as a single segment at first, which means you will need two or more points lying on top of each other if you are branching out. Fusing those points together should be a post solve or post simulation operation. You want to keep your points pure and unfused during the sim.

good luck and I'm curious to see what you come up with.

Thanks Peter:D Thats all I think I need to get it working:) I didnt know it looks at the closest nighbor first, though i thought it was bit strange they had a furtherstPC vop and not a nearest:p Also, not really creating any new geometry, just manipulating the hell out my current geometry!

Im making a lightning system, sort of a tesla coil type system where the user inputs the tesla coil, then the environment with vertex color values. The idea is that the bolts from a tesla coil will always attempt to connect to the nearest object with a charge.

I currently have a lightning bolt with a sort of fBm noise applied to it. Ive also created vex code that causes the noise to propogate along the curve. This curve will then need to inherit the direction vector of the system you described. If it cant connect to anything it would instead be "free" and branch out and decrease in intensity. Currently creating the branches (faked L-system with a depth of 3,which all need to be random, and need to have motion).

The point of previous question was to rondomize the movement of the lightning much like a real tesla coild. It also means I get to have some fun by applying some other secondary time related effects (when a lightning bolt remains connected to a point in an environment for a while, it's center begins to curve outwards).

Much of the other functionality is coming into place, so all is good.

Some tesla funnies:p (skip to the middle of the video) :D ->

I'll post the digital asset in a week or two:)

P.s. Stabby's "half arsed attempt" is pretty amazing!

Edited by aghourab
Link to comment
Share on other sites

Some tesla funnies:p (skip to the middle of the video) :D ->

That is ridiculously cool! :)

Sounds like a fun project! -looking forward to some of your results in the effects/WIP section.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...