Home Technical Talk

Distance - store three nearest Vertices to given Vertex .... Getting Crazy :D (Maxscript)

EliderDeli
node

Hoi Folks,
Im building a conform script and i struggle a lot how to get the three nearest vertices to a given vertice in one iteration. I coded a little thing to get it for two but Im hardly confused how to do it. Any help would be really nice . Greetings EliderDeli

local oldDistance = 1e9
local oldDistance2 = 1e9
local oldDistance3 = 1e9

indexNearestVertice = undefined

first = 0
second = 0
third = 0
myList = refObj.selectedVerts

for i=1 to myList.count by 1 do
(

distanceO = calculatdistance myList[i].pos SelectedVerticeID.pos

if (distanceO < oldDistance) then
(
second = first
first = myList[i]
oldDistance = distanceO
)
if (distanceO < oldDistance2 && distanceO != oldDistance) then second = myList[i]
)


Replies

  • Swordslayer
    Offline / Send Message
    Swordslayer interpolator
    You say it's a conform script so the calculations will be repeated. In that case look up voxel grids - you want to be able to repeatedly get verts near a given position first and filter only those.
  • RN
    Offline / Send Message
    RN sublime tool
    Couple of ideas:

    - When you just want to compare the distances between themselves (like finding the farthest or closest distances), you can optimise by comparing the squared distances (which uses simpler math) instead of the actual distances (which needs square root).
    (This happens to work because if x > y, then x² > y²)

    - Use a list to keep track of the three closest distances, and each time you find a closer distance, advance all entries on the list so it always keeps the closest 3 distances that you found.

    closestDistances = #( 1e9, 1e9, 1e9 )<br>closestVertices = #( undefined, undefined, undefined )<br><br>myList = refObj.selectedVerts<br><br>for i = 1 to myList.count do<br>(<br>    local v = myList[i].pos - selectedVerticeID.pos<br>    squaredDistance = dot v v<br>    <br>    if squaredDistance < closestDistances[1] then (<br>        // Advance everyone one step and forget the third closest distance.<br>        <br>        closestDistances[3] = squaredDistances[2]<br>        closestVertices[3]  = closestVertices[2]<br>        <br>        closestDistances[2] = squaredDistances[1]<br>        closestVertices[2]  = closestVertices[1]<br>        <br>        closestDistances[1] = squaredDistance<br>        closestVertices[1]  = myList[i]<br>    )<br>)<br><br>// Actual closest three distances:<br>sqrt closestDistances[1]<br>sqrt closestDistances[2]<br>sqrt closestDistances[3]<br><br>// Relevant vertices or objects:<br>closestVertices[1]<br>closestVertices[2]<br>closestVertices[3]

  • EliderDeli
    Thanks Sworslayer I will have a Look at voxel grids. 
    Greentooth thank

    Someone piostet a good solution for keeping the vertices.
    http://www.scriptspot.com/forums/3ds-max/general-scripting/distance-store-three-nearest-vertices-to-given-vertex-getting-crazy-d#comment-38156

Sign In or Register to comment.