Longest Distance Between Any 2 Verts

Need a bit of help. I have a bit of code that I am using to find the longest distance between any 2 verts on a single polygon object. I have pasted a bit of the code below. It all seems to work find, but the issue is that it just takes to damn long on object that have lots of verts. Can anyone offer suggestions that may yeild the same results, but quicker.

string $objList[] = `ls -sl`;
string $objSelected;

for($objSelected in $objList){
	select $objSelected;
	ConvertSelectionToVertices;
	
	string $vertList[] = `ls -sl -fl`;
	string $vertSelected;
	float $storeDistance = 0;	
	
	for($vertSelected in $vertList){
		vector $wsPosStart = `xform -q -ws -t $vertSelected`;	
		string $diffList[] = stringArrayRemove({$vertSelected} , $vertList);

		for($diffSelected in $diffList){	
			vector $wsPosEnd = `xform -q -ws -t $diffSelected`;
			float $checkDistance = (sqrt((($wsPosEnd.x - $wsPosStart.x)*($wsPosEnd.x - $wsPosStart.x)) + (($wsPosEnd.y - $wsPosStart.y)*($wsPosEnd.y - $wsPosStart.y)) + (($wsPosEnd.z - $wsPosStart.z)*($wsPosEnd.z - $wsPosStart.z))));
			if($checkDistance > $storeDistance){
				$storeDistance = $checkDistance;															
			}
		}
	}
	print ($storeDistance + "is the longest distance between any 2 verts");
}

Well mel is not designed for algorithmic design so its not a good candidate for such operations, its a quiick and dirty solution operator. You should drive the data into some kind octree,then seach only the leaves that are far from each other, pretty certainly all points near the bb center can be skipped tough. So as a non memory algorithmic code you could ditch all points that are closer than 1/2 the ditsance to bb center than any other test dropped form test could be pretty safe assumption. (ad begin with half bb max side length is pretty good start condition before that)

your original version took 55 seconds on my test sphere and this version I wrote took 17secs.. That's still probably too long but like joojaa says, Mel is not good for this kind of computation.

The changes I made to yours to speed it up were:
1) to not bother checking any verts that had previously been checked against everything else, this gives you the biggest speed-up.

2) don't use Maya's runtime/script commands (i.e. ConvertSelectionToVertices) as these are almost always safety and feature padded, so it's usually quicker to use the direct commands (i.e. polyListComponentConversion)

3) I removed any call to change the selection as this is often a big speed killer.

4) don't do any string array operations, these are relatively slow (especially compared to NOT doing them!).

5) I used mag to get the distance between verts (dunno if this is actually quicker than the arithmentic method.. probably, but at least it's less code )

6) I used pointPosition instead of xform... again, I'm not sure if this is any faster but I'd guess it probably is as it is specifically for getting point info and xform is mainly for working on objects and transforms... (this is purely a guess though and may be incorrect, it's just that I'd never use xform for querying component positions)

you could probably speed this up even more, especially if you did something like joojaa suggests and it works. however, be careful that the checks you do to disregard certain verts are not actually slower to calculate than actually doing the vertex work!

:nathaN

CODE
{
    string $objList[] = ls -sl;

    for ($obj in $objList)
    {
        string $vertList[] = ls("-fl",polyListComponentConversion("-tv",$obj));
        float $storeDistance = 0;
        int $vNum = size($vertList);

        for($v=0; $v<$vNum; $v++)
        {
            vector $start = `pointPosition -w $vertList[$v]`;

            for($n=($v+1); $n<$vNum; $n++)
            {
                vector $end = `pointPosition -w $vertList[$n]`;
                float $checkDistance = mag($start-$end);
                if($checkDistance > $storeDistance)
                    $storeDistance = $checkDistance;
            }
        }
        print ($storeDistance + "is the longest distance between any 2 verts");
    }
}

QUOTE(Naughty @ 10/02/07, 01:30 PM) [snapback]274188[/snapback]
you could probably speed this up even more, especially if you did something like joojaa suggests and it works. however, be careful that the checks you do to disregard certain verts are not actually slower to calculate than actually doing the vertex work!

i tested and it does, bit the problem is that it does not accelerate certain structures at all. however since its a only n operations and the brute force check is (n/2+1)*n operations it does not really matter to the end result.

Anyway i tested this by splitting the surface into 2 point lists and having 2 lists and never compare points within lists and this is

(n/4+1)*n operations. wich say for 1000 points is 251000 operations versus 501000 thats to say allready 2 times faster and each layer of splitting (bsp tree in this case) will bring the speed 2 times faster.

So making a artificial 8 way container would already drop the times down by heaps. And offcourse the splitting operation is n tops 3n and is guaranteed to work.

(also here you can see that the stupid dump pass difference between 501000 and 502000 is insignificant if it can just omit one point its already done its job which is 500999.5 offcourse gain accumulates for each dumped point)

Thanks Joojaa and Naughty for your insight. I really appreciate the help. I am not sure I can implement an octree it may be out of my comfort zone. I was thinking about the issue some more and reading Joojaa's second post. Maybe I can find the longest distance from the center point of the bounding box, then compare it to the list of verts using Naughtys optimizations. This would greatly reduce the number of calculations and speed things up considerably. Do you see any reason that this method would not work?

Romey