PDA

View Full Version : voronoi fracture


bigquix
02-28-2011, 05:07 PM
hi dear friends

I wrote a code for my final project whice is supposed to cut a polygonal object based on 3D Voronoi pattern. it works for lower amount of chunks but when I run it for amounts higher than 5 it does not work properly; i.e. some chunks are missing. I guess the problem is in the naming of my string array members which hold the chunks.
It will be great if you can help me fixing this problem as it's really important to me.
here is the code:

/*
Voronoi Fracture
Version 1.0
by Ali Seiffouri
started 2-27-2011
(code in MEL)
*/

{ // local scope

// parameters
int $numberOfChunks = 5; // min 2 chunks

// creating an array of selected objects
string $mesh[] = `ls -sl`;
string $mesh_shape[] = `listRelatives -s $mesh[0]`;

// create nearest node
string $nearestNode = `createNode closestPointOnMesh`;
connectAttr -f ($mesh_shape[0]+".worldMesh[0]") ($nearestNode+".inMesh");

// create random points in object's bounding box
select $mesh[0];
float $bbox[] = `exactWorldBoundingBox`;
float $planeSize = 2 * `mag << ($bbox[3]-$bbox[0]) , ($bbox[4]-$bbox[1]) , ($bbox[5]-$bbox[2]) >>`;
vector $planeAxis , $planePos , $randomPoints[];
string $chunks[];
for ( $i = 0; $i < $numberOfChunks; $i++ )
{
$tempObject = `duplicate $mesh[0]`;
$chunks[$i] = $tempObject[0];
}

for ( $i = 0; $i < $numberOfChunks; $i++ )
{

vector $myPoint = << `rand $bbox[0] $bbox[3]` , `rand $bbox[1] $bbox[4]` , `rand $bbox[2] $bbox[5]` >>;
vector $nearestPos , $nearestNormal , $connector;
float $dotProduct = 0;
float $pointX , $pointY , $pointZ;

do // do this as long as the point is outside the mesh...
{
$pointX = $myPoint.x;
$pointY = $myPoint.y;
$pointZ = $myPoint.z;

setAttr ($nearestNode+".inPosition") $pointX $pointY $pointZ;
$nearestPos = `getAttr ($nearestNode+".position")`;// nearest point on the mesh from myPoint
$nearestNormal = `getAttr ($nearestNode+".normal")`;// normal of the mesh on the nearest point to myPoint
$connector = << ($pointX - $nearestPos.x) , ($pointY - $nearestPos.y) , ($pointZ - $nearestPos.z) >>; // connecting vector
$dotProduct = dotProduct($nearestNormal, $connector, 0);

if ( $dotProduct > 0 )
{
$myPoint = << ($pointX - 2 * $connector.x) , ($pointY - 2 * $connector.y) , ($pointZ - 2 * $connector.z) >>;
}

} while ( $dotProduct > 0 );

/*
spaceLocator -p 0 0 0;
scale -r 0.1 0.1 0.1;
move -r ($pointX) ($pointY) ($pointZ);
*/
$randomPoints[$i] = $myPoint;
}

string $tempString1 , $tempString2;

// creating planes and intersection
for ( $i = 0; $i < $numberOfChunks - 1; $i++ )
{
for ( $j = $i+1; $j < $numberOfChunks; $j++ )
{
// creating cutter planes
$planePos = ( $randomPoints[$i] + $randomPoints[$j] ) / 2;
$planeAxis = $randomPoints[$j] - $randomPoints[$i] ;


$cutter1 = `polyPlane -w $planeSize -h $planeSize -sx 1 -sy 1 -ch 0 -ax ($planeAxis.x) ($planeAxis.y) ($planeAxis.z)`;
$cutter2 = `polyPlane -w $planeSize -h $planeSize -sx 1 -sy 1 -ch 0 -ax ($planeAxis.x) ($planeAxis.y) ($planeAxis.z)`;

move -r ($planePos.x) ($planePos.y) ($planePos.z) $cutter1;
move -r ($planePos.x) ($planePos.y) ($planePos.z) $cutter2;


// cut, cut and cut...
$temp1 = `polyBoolOp -op 3 -ch 0 -useThresholds 1 -preserveColor 0 $chunks[$i] $cutter1`;
$temp2 = `polyBoolOp -op 2 -ch 0 -useThresholds 1 -preserveColor 0 $chunks[$j] $cutter2`;

$chunks[$i] = $temp1[0];
$chunks[$j] = $temp2[0];
}
}

hide $mesh[0];
} // end local scope

dbsmith
03-01-2011, 08:22 PM
I think i see it.
These lines:
$cutter1 = `polyPlane -w $planeSize -h $planeSize -sx 1 -sy 1 -ch 0 -ax ($planeAxis.x) ($planeAxis.y) ($planeAxis.z)`;
$cutter2 = `polyPlane -w $planeSize -h $planeSize -sx 1 -sy 1 -ch 0 -ax ($planeAxis.x) ($planeAxis.y) ($planeAxis.z)`;

polyPlane command actually returns an array of 2 strings, the first entry being the name of the plane created.
So when you go to use the cutter1 and cutter2 variables here:
$temp1 = `polyBoolOp -op 3 -ch 0 -useThresholds 1 -preserveColor 0 $chunks[$i] $cutter1`;
$temp2 = `polyBoolOp -op 2 -ch 0 -useThresholds 1 -preserveColor 0 $chunks[$j] $cutter2`;

just refer to the first element in their arrays instead:
$temp1 = `polyBoolOp -op 3 -ch 0 -useThresholds 1 -preserveColor 0 $chunks[$i] $cutter1[0]`;
$temp2 = `polyBoolOp -op 2 -ch 0 -useThresholds 1 -preserveColor 0 $chunks[$j] $cutter2[0]`;

CGTalk Moderation
03-01-2011, 08:22 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.