View Full Version : poly intersection
king21 07-13-2006, 01:00 PM Hello!
I need urgent help!
I am writing a script wich randomly positiones polygional objects on surface. What i want to implement is the they do not intersect. So i need something wich will tell me if two selected polygon surfaces intersect or no. Ithe best would be 1 for intersect or 0 for not intersect.
If anybody has an idea please tell me i will be greatfull for any help.
Jure
|
|
I’ve recently had a similar problem while writing a Lego block script. My method of solving the problem was to use the nearestPointOnMesh plug-in. It ships with Maya, although I think it was called closestPointOnMesh in older versions. Make sure this plug-in is loaded.
Save the following as riessBoxInVolume.mel.
//0 = inside, 1 = outside, 2 = border.
global proc int riessBoxInVolume()
{
//Variables.
string $selection[]; //The user selection.
string $mesh; //The Volume that the box is compared to.
string $nearestPointNode; //The name of the nearest point on mesh node.
int $counter; //The counter for loops.
float $arrayBuffer[]; //A temporary variable used to transfer float arrays into vectors.
vector $inPosition; //The value entered into the nearest point on mesh, in position.
vector $closestPoint; //The position value from the nearest point on mesh node.
vector $closestNormal; //The normal value from the nearest point on mesh node.
float $insideTest; //Used to test if two vectors are aligned.
int $insideOutside[]; //Each element of the array represents a vertex point being either inside (0) or outside a mesh (1).
int $positionIndex; //The return value. 0 = inside, 1 = outside, 2 = border.
$selection = `ls -sl`;
$mesh = $selection[0];
$box = $selection[1];
$nearestPointNode = `nearestPointOnMesh $mesh`;
//Construct an array where each element is either 0 if the point is outside or 1 if it is inside.
for($counter = 0; $counter < 8; $counter++)
{
//Change the nearest point node in position, to the world space position of each point.
$arrayBuffer = `pointPosition -w ($box + ".vtx[" + $counter + "]")`;
$inPosition = $arrayBuffer;
setAttr ($nearestPointNode + ".inPosition") ($inPosition.x) ($inPosition.y) ($inPosition.z);
//Get information from the nearest point node.
$arrayBuffer = `getAttr ($nearestPointNode + ".position")`;
$closestPoint = $arrayBuffer;
$arrayBuffer = `getAttr ($nearestPointNode + ".normal")`;
$closestNormal = $arrayBuffer;
$pointVector = $closestPoint - $inPosition;
$insideTest = dot($pointVector, $closestNormal);
//Construct the array.
if($insideTest >= 0)
{
$insideOutside[$counter] = 0;
}
else
{
$insideOutside[$counter] = 1;
};
};
//Border.
$positionIndex = 2;
//Inside.
if( ($insideOutside[0] == 0) &&
($insideOutside[1] == 0) &&
($insideOutside[2] == 0) &&
($insideOutside[3] == 0) &&
($insideOutside[4] == 0) &&
($insideOutside[5] == 0) &&
($insideOutside[6] == 0) &&
($insideOutside[7] == 0) )
{
$positionIndex = 0;
};
//Outside.
if( ($insideOutside[0] == 1) &&
($insideOutside[1] == 1) &&
($insideOutside[2] == 1) &&
($insideOutside[3] == 1) &&
($insideOutside[4] == 1) &&
($insideOutside[5] == 1) &&
($insideOutside[6] == 1) &&
($insideOutside[7] == 1) )
{
$positionIndex = 1;
}
return $positionIndex;
};
Create some poly objects.
$mesh = `polySphere -r 5 -ch 0 -n "Mesh"`;
$box = `polyCube -ch 0 -n "Box"`;
Select the objects in the correct order.
select -r $mesh $box;
Run the script (0 = inside).
riessBoxInVolume;
Move the box outside of the mesh.
move -ws 10 0 0 $box;
Run the script (1 = outside).
riessBoxInVolume;
Move the box to the edge of the mesh.
move -ws 5 0 0 $box;
Run the script (2 = border).
riessBoxInVolume;
It works by taking a given point and calculating the nearest point on the mesh. Depending on how the normal at this point on the mesh is aligned to a vector, created by the point minus its closest point on the mesh, it can be determined if this point is on one side of the mesh or the other. So it is important to make sure that the normals are facing the right direction. I hope this helps:)
dbsmith
07-14-2006, 12:58 AM
Hey Hoju,
Can you tell me more about what you are doing with Lego? I am very interested in doing something with Lego and Maya, like a big plugin or something i dunno...
Hi dbsmith,
Sorry I was a bit hazy with the description of my script. It doesn’t actually interact with physical Lego blocks, but makes a virtual Lego model out of a conventional polygon model. I hope this is of some help.;)
CGTalk Moderation
07-14-2006, 03:55 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.
vBulletin v3.0.5, Copyright ©2000-2013, Jelsoft Enterprises Ltd.