Jump to content

Closest Distance to point!


Navneet Arora

Recommended Posts

hey guys, 

 I need to figure out a way through which i can find the closest point distance between two different scattered surfaces. For example :

 There are some point scattered on a grid and a sphere. Then these points  on grid need to go to the sphere and lock themselves but on the basis of closest distance found.

 If one point finds then closest neighbour on the target position then that point on the target position gets unavailable for other points to search for as it has been found by the source point.

So this way we can tell the remaining points to search for next closest target.

I have tried some ways using point cloud but it didnt help .

Can somebody help me with this?

Link to comment
Share on other sites

4 minutes ago, Farmfield said:

The functionality you are after is basically how the "connect adjacent pieces" SOP works in point mode - and you could probably mod that SOP to do what you are after.

hey johnny,

  could you show a demo file which could incorporate that? i mean i need to have point attracted to closest distance of target object

Link to comment
Share on other sites

What about something like this. It makes a unique 1:1 connection between two scatter sets based upon distance. It assumes each scatter has the same number of points. It creates a target_location, target_distance and target_id attributes on the source points. You can use these attributes to make a line between the source and the target or conduct further processing.

source_node = hou.pwd()
source_geo = source_node.geometry()
target_node = hou.pwd().inputs()[1]     #hou.node('/obj/sphere_target_points/scatter1')
target_geo = target_node.geometry()
source_count = len(source_geo.points())
target_count = len(target_geo.points())

attr_location = source_geo.addAttrib(hou.attribType.Point, "target_location", (0.0, 0.0, 0.0))
attr_distance = source_geo.addAttrib(hou.attribType.Point, "target_distance", 1000000.0)
attr_target_id = source_geo.addAttrib(hou.attribType.Point, "target_id", -1)

if target_count >= source_count:
    # We need at least as many targets as we have source points. (caveat #1).
    lst_target_id_taken = []
    for j,source_point in enumerate(source_geo.points()):
        # Scan every target point looking for the smallest distance.
        v = source_point.attribValue('P') 
        p1 = hou.Vector3((v[0],v[1],v[2]))
        current_distance = source_point.attribValue('target_distance')
        for i, target_point in enumerate(target_geo.points()):
            if i in lst_target_id_taken:
                # Already taken, skip.
                pass
            else:
                # Could be our nearest target.
                v = target_point.attribValue('P') 
                p2 = hou.Vector3((v[0],v[1],v[2]))
                dist = p1.distanceTo(p2)
                if dist < current_distance:
                    candidate_location = p2
                    candidate_distance = dist
                    candidate_id = i
                    current_distance = dist
            
        source_point.setAttribValue("target_location", candidate_location)
        source_point.setAttribValue("target_distance", candidate_distance)  
        source_point.setAttribValue("target_id", candidate_id)
        lst_target_id_taken.append(candidate_id)

 

ap_source_target_distance_1a.hiplc

Edited by Atom
Link to comment
Share on other sites

On 8/26/2016 at 10:31 PM, Atom said:

What about something like this. It makes a unique 1:1 connection between two scatter sets based upon distance. It assumes each scatter has the same number of points. It creates a target_location, target_distance and target_id attributes on the source points. You can use these attributes to make a line between the source and the target or conduct further processing.


source_node = hou.pwd()
source_geo = source_node.geometry()
target_node = hou.pwd().inputs()[1]     #hou.node('/obj/sphere_target_points/scatter1')
target_geo = target_node.geometry()
source_count = len(source_geo.points())
target_count = len(target_geo.points())

attr_location = source_geo.addAttrib(hou.attribType.Point, "target_location", (0.0, 0.0, 0.0))
attr_distance = source_geo.addAttrib(hou.attribType.Point, "target_distance", 1000000.0)
attr_target_id = source_geo.addAttrib(hou.attribType.Point, "target_id", -1)

if target_count >= source_count:
    # We need at least as many targets as we have source points. (caveat #1).
    lst_target_id_taken = []
    for j,source_point in enumerate(source_geo.points()):
        # Scan every target point looking for the smallest distance.
        v = source_point.attribValue('P') 
        p1 = hou.Vector3((v[0],v[1],v[2]))
        current_distance = source_point.attribValue('target_distance')
        for i, target_point in enumerate(target_geo.points()):
            if i in lst_target_id_taken:
                # Already taken, skip.
                pass
            else:
                # Could be our nearest target.
                v = target_point.attribValue('P') 
                p2 = hou.Vector3((v[0],v[1],v[2]))
                dist = p1.distanceTo(p2)
                if dist < current_distance:
                    candidate_location = p2
                    candidate_distance = dist
                    candidate_id = i
                    current_distance = dist
            
        source_point.setAttribValue("target_location", candidate_location)
        source_point.setAttribValue("target_distance", candidate_distance)  
        source_point.setAttribValue("target_id", candidate_id)
        lst_target_id_taken.append(candidate_id)

 

ap_source_target_distance_1a.hiplc

hey ,

thanks for finding time and trying this. i tried using this but it doesnt seem to work as it doesnt seem to snap to closest point, can you try once and let me know if i am wrong?

Link to comment
Share on other sites

On 8/27/2016 at 9:07 AM, Malf said:

Also another way to go about it. One wrangle running in detail mode is finding the point to match to and then the one below is doing a simple position lerp.

Hope it helps.

 

Cheers Jake.

matchClosestPoint.hipnc

Hey Malf,

Thanks for the time you put into this, But the points seem to cris cross each other which shouldn't be the case if we are looking for closest point. otherwise it is a good method.

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