Jump to content

Closest point script not function


Blacklisted_Guy

Recommended Posts

Hello houdini wizards,

I've been trying to make a script that makes a curve out of a bunch of points by finding the closest point.
For some reason though, the points don't connect to their closest point

File with the script in action as well as the script down below.

//-----------------------------------------//
//          Find closest point V1          //
//               By Jonne Geven            //
//-----------------------------------------//
//imported functions
//returns: [0] = 2D distance. [1] = 3D distance
vector2 distanceToPoint(int currentPoint; int targetPoint; int currentPointInput; int targetPointInput)
{
        //importing user inputs
        int cp = currentPoint;
        int cpi = currentPointInput;
        int tp = targetPoint;
        int tpi = targetPointInput;

        //extracting point position
        vector cpp = point(cpi, "P", cp);
        vector tpp = point(tpi, "P", tp);

        //deltas
        float dx = tpp[0] - cpp[0];
        float dy = tpp[1] - cpp[1];
        float dz = tpp[2] - cpp[2];

        //2d distance
        float DD2;

        DD2 = sqrt((dx * dx) + (dz * dz));

        //3d distance
        float DD3;

        DD3 = sqrt((DD2 * DD2) + (dy * dy));

        vector2 distances = set(DD2, DD3);
        return distances;
}

//closest point
int closePoint(int currentPoint;int currentPointInput; int targetPointInput)
{
        int tpc = npoints(targetPointInput); //this used to be user defined but that doesnt work since it needs to be the amount of points
        int cp = currentPoint;
        int cpi = currentPointInput;
        int tpi = targetPointInput;

        float C;
        float CP;
        int returnValue;

        for(int i = 0; i < tpc; i++)
        {
                //distance between points
                C = distanceToPoint(cp, i, cpi, tpi)[0];

                //importing Z values
                vector zvalT = point(tpi, "P", i);
                vector zvalC = point(cpi, "P", cp);

                if(zvalT[2] < zvalC[2]) //comparing the target and current z position to make sure
                {                                 //the curve has a direction
                        //max search distance and making sure the processed point isn't selected.
                        if(abs(C) < 19 && C != 0)
                        {
                                if(i != 0)
                                {
                                        //comparing current distance to currently smallest distance
                                        if(C < CP)
                                        {
                                                CP = C;
                                                returnValue = i;
                                        }
                                }
                                //initializing CP value with first distance
                                else if(i == 0)
                                {
                                        CP = C;
                                        returnValue = 0;
                                }
        
                        }
                }
        }
        return returnValue;
}

//finding closestpoint
i@target = closePoint(@ptnum, 0, 0);

//This script is made with the general direction being -Z
//vector pp0 = point(geoself(), "P", @ptnum);
//vector pp1 = point(geoself(), "P", target);

int line = addprim(0, "polyline");
addvertex(0, line, @ptnum);
addvertex(0, line, @target);
//-----------------------------------------//
//             For questions               //
//        odforce: Blacklisted_Guy         //
//-----------------------------------------//

 

closestPointNotFound.hiplc

Link to comment
Share on other sites

Hmm..am I reading that right? You are declaring your return value from distanceToPoint as a float yet you are returning a vector2. Is that the intention? These kinds of scripts often work better when running in detail mode(only once). Search this site, I remember a recent thread where there were multiple solutions to this problem provided.

 

Link to comment
Share on other sites

3 minutes ago, Atom said:

Hmm..am I reading that right? You are declaring your return value from distanceToPoint as a float yet you are returning a vector2. Is that the intention? These kinds of scripts often work better when running in detail mode(only once). Search this site, I remember a recent thread where there were multiple solutions to this problem provided.

 

yes, the distanceToPoint  function returns a vector with the 2D distance between the points and a 3D distance.

So, what you'd suggest is running the script with a for loop in detail mode instead of in point mode? I'll try that one!
Thank you for the advice!

Link to comment
Share on other sites

Running in Detail mode you do loose VEX's multi-processor benefit so in these cases python becomes a viable alternative as well. When looping over points in detail mode it is sometimes easier to create flags to remember if you have already detected a candidate. When running under points you have no "memory" of the last point unless you construct some way to store variables as detail attributes.

 

Here is the thread I was referring to up above.

 

  • Like 1
Link to comment
Share on other sites

41 minutes ago, Atom said:

Running in Detail mode you do loose VEX's multi-processor benefit so in these cases python becomes a viable alternative as well. When looping over points in detail mode it is sometimes easier to create flags to remember if you have already detected a candidate. When running under points you have no "memory" of the last point unless you construct some way to store variables as detail attributes.

 

Here is the thread I was referring to up above.

 

thank you for the link, I'll be looking through it!

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...