PDA

View Full Version : Help with vertex indexes

 chandan101401-07-2012, 05:27 AMHi, I am new to MaxScript so please pardon me if I make any silly points. I need to the find the index of the vertex that is closest to a point in space. Here's what I have done so far: 1. created a snapshot of the object as a meshobject so that I can perform meshvertex operations on it. 2. create a loop that works n times, where n is the number of vertices of the object 3. calculate the distance of the first vertex to the point and store it in a variable dist, and the index number of the vertex in a variable vertindx 4. calculate the distance of the second vertex to the point and compare it to dist, if it's smaller then update dist and store the index number of the vertex into vertindx, else ignore to update dist and vertindx and proceed to perform the same operations on the remaining vertices. 5. If all goes right (hopefully!), i will end up with the dist (distance) and the vertindx (vertex index) of the closest vertex. The problem I'm facing is that I can't seem to find a way to get the index number of the vertex being operated upon. Can someone please show me the way to do so?
lo
01-08-2012, 07:48 AM
2. create a loop that works n times, where n is the number of vertices of the object

The problem I'm facing is that I can't seem to find a way to get the index number of the vertex being operated upon. Can someone please show me the way to do so?

Isn't the vertex index simply your iterator?

for i = 1 to yourObject.numVerts do currentVertex = getVert yourObject i
In this case the index of the vertex is i

videep
01-09-2012, 06:40 AM
This is what you need to do probably:

thePoint = [0,0,0] --point3 position which i have taken to be [0,0,0]

-- myObject = the object you want to take the snapshot of
theMesh = snapshotAsMesh myObject

theDistanceArray = #() --create an empty array

for i = 1 to theMesh.numVerts do
(
theDistanceArray[i] = distance (getVert theMesh i) thePoint
)

--till here, the distance of all the vertices has been stored in the array.

theDistanceArray = sort theDistanceArray

so this will sort your distance array

--sorry for the lengthy process, but I just had 5 mins...

you can figure out the rest to find the vertex integer or make a struct!!!

miauu
01-09-2012, 07:31 AM
I don't have 3ds max open so this is not tested:

thePoint = [0,0,0] --point3 position which i have taken to be [0,0,0]
-- myObject = the object you want to take the snapshot of
theMesh = snapshotAsMesh myObject

-- colect vert index and distance for that vert to the thePoint
theDistanceArray = for i = 1 to theMesh.numVerts collect #(i, (distance (getVert theMesh i) thePoint))

Now you have multidimensional array. Every elemets in the theDistanceArray is array that contains the vert Index and the distance to the thePoint. You have to use qsort function to srot the elements in the in the theDistanceArray by distance(check the maxscript help file for qsort). Later you can get the desired distance and the vert. Let say that you want to collect all verts that have distance less than 100:
vertsWithDistLess100 = for i in theDistanceArray where theDistanceArray[i][2] < 100 collect theDistanceArray[i][1]

Moosley
01-11-2012, 10:48 AM
A method to do it without array and sorting etc:

fn getClosestVertToPoint theNode thePoint selectVert:false =
(
if classOf theNode == Editable_Mesh or classOf theNode == Editable_Poly then
(
d = 10000000.0
v = 1
getVertPos = case classOf theNode of
(
Editable_Mesh : meshop.getVert
Editable_Poly : polyop.getVert
)
for i = 1 to theNode.numVerts do
(
curDist = distance (getVertPos theNode i) thePoint
if curDist < d do
(
v = i
d = curDist
)
)
if selectVert do theNode.selectedVerts = #{v}
v
)
else(false)
)

vert = getClosestVertToPoint myObject [0,0,0] selectVert:true

(Revised to include below optimisation)

denisT
01-11-2012, 02:12 PM
A method to do it without array and sorting etc:

fn getClosestVertToPoint theNode thePoint selectVert:false =
(
if classOf theNode == Editable_Mesh or classOf theNode == Editable_Poly then
(
d = 10000000.0
v = 1
for i = 1 to theNode.numVerts do
(
case classOf theNode of
(
Editable_Mesh : curDist = distance (meshop.getVert theNode i) thePoint
Editable_Poly : curDist = distance (polyop.getVert theNode i) thePoint
)
if curDist < d do
(
v = i
d = curDist
)
)
if selectVert do theNode.selectedVerts = #{v}
v
)
else(false)
)

vert = getClosestVertToPoint myObject [0,0,0] selectVert:true

because it's unlikely that the node will change its own class during the loop, the class check has to be out of the loop:

getVertex = case classof theNode of
(
Editable_Mesh : getVert
Editable_Poly : polyop.getVert
)
for v = 1 to theNode.numVerts do curDist = distance (getVertex theNode v) thePoint
...

it makes everything faster...

Moosley
01-11-2012, 02:17 PM
Nice optimisation :)

CGTalk Moderation
01-11-2012, 02:17 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.