General Category > ASL Scripts

Old Magnet Tool

<< < (9/15) > >>

Claude:
The pivot point location and orientation aren't  accessible.

Could you describe what you want to do exactly,maybe
you don't really need the pivot point.
As an example,if you want to calculate the true coordinates
of the points,you don't need it.

Francesco:
Hi Claude,
thanks for dropping in.

I need to know the pivot's position to make the polimagnet script more flexible and easy to use.

Currently, the script checks if a point of mesh01 falls in the "cone" (let me pass this term for clarity) built from a face of the magnet and its origin.

The thing I want to do is to build that "cone" from the pivot's position, so that users can easily modify the center of the magnet.

I'm about to make a sketch for clarity, this way I can clearly illustrate how the polimagnet script works - for the people that's interested in using it.

I attach the sketch here:

NickE:
Francesco,
Thank you for the diagram.  That helps me understand what you are attempting.

As far as I can tell, an object's position and orientation are a combination of three factors:  the raw point coordinates retrieved by the GetPoints() function, the Location (loc), and the Orientation.  When you move or rotate a whole object in the object editor, you are only changing the Location and the Orientation.  When you move a point, you are changing the raw point coordinates relative to the current Location and Orientation.   Whenever you need to know how one object's points relate to another object's points, both objects must be in the same coordinate system. 

Below is some code that moves two meshes into the same coordinate system.

--- Code: --- file $output;
string $fname;
$fname="$console";
$output.open($fname,"w");

object $curObject;
shape $shape, $shapes[0], $childShapes[1];
shape $source,$target;

int $i,$j,$havesource,$havetarget;
int $ii,$jj,$kk;

int $sourcenumpts,$sourcenumfaces,$sourcenumedges;
point3 $p,$sourcemin,$sourcemax,$sourcecenter,$sourceloc;
quaternion $sourceorientation,$zeroor;
float4x4 $sourcerotmat;


point3 $zeropoint;

float $sourcedepth,$sourcedepthoffset;

int $targetnumpts,$targetnumfaces,$targetnumedges;
point3 $targetmin,$targetmax,$targetcenter,$targetloc;
quaternion $targetorientation;
float4x4 $targetrotmat;

$zeropoint.x=0.0;
$zeropoint.y=0.0;
$zeropoint.z=0.0;
$zeroor.x=0.0;
$zeroor.y=0.0;
$zeroor.z=0.0;
$zeroor.w=1.0;

$curObject = project.curObject;
$curObject.GetShapes($childShapes);
$shapes.size = 0;
while ($childShapes.size > 0)
    $shapes.push($childShapes.pop());

while ($shapes.size > 0) {
    $shape = $shapes.pop();

    if ($shape.GetKind() == SHAPE_KIND_MESH && $shape.name == "source")
    {
       $source=$shape;
       $sourcenumpts=$source.GetNumPoints();
       $sourcenumfaces=$source.GetNumFaces();
       $sourcenumedges=$source.GetNumEdges();
       $sourceloc=$source.loc;
       $sourceorientation=$source.orientation;
       $sourcerotmat=toFloat4x4($sourceorientation);
       $havesource=1;
    }
   
    if ($shape.GetKind() == SHAPE_KIND_MESH && $shape.name == "target")
    {
       $target=$shape;
       $targetnumpts=$target.GetNumPoints();
       $targetnumfaces=$target.GetNumFaces();
       $targetnumedges=$target.GetNumEdges();
       $targetloc=$target.loc;
       $targetorientation=$target.orientation;
       $targetrotmat=toFloat4x4($targetorientation);
       $havetarget=1;
    }
}

if ($havesource==1 && $havetarget==1)
{

   $output.print("Source Before:\n");
   $output.print("Loc: (%0.2f,%0.2f,%0.2f)\n",$source.loc.x,$source.loc.y,$source.loc.z);
   $output.print("Center: (%0.2f,%0.2f,%0.2f)\n",$sourcecenter.x,$sourcecenter.y,$sourcecenter.z);
   $output.print("Min: (%0.2f,%0.2f,%0.2f) Max: (%0.2f,%0.2f,%0.2f)\n",$sourcemin.x,$sourcemin.y,$sourcemin.z,$sourcemax.x,$sourcemax.y,$sourcemax.z);
   $output.print("Orientation (%0.2f,%0.2f,%0.2f,%0.2f)\n",$sourceorientation.x,$sourceorientation.y,$sourceorientation.z,$sourceorientation.w);



   /* apply orientation and location to shapes */
   for $ii = 0 to $sourcenumpts-1 do
   {
     $p=$source.GetPoint($ii);
     $p=$sourcerotmat.Project($p)+$source.loc;
     $source.SetPoint($ii,$p);
   }
   $source.loc=$zeropoint;
   $source.orientation=$zeroor;
   
   for $ii = 0 to $targetnumpts-1 do
   {
     $p=$target.GetPoint($ii);
     $p=$targetrotmat.Project($p)+$target.loc;
     $target.SetPoint($ii,$p);
   }
   $target.loc=$zeropoint;
   $target.orientation=$zeroor;

   /* get source stats */
   $sourcemin.x=10000;
   $sourcemin.y=10000;
   $sourcemin.z=10000;
   $sourcemax.x=-10000;
   $sourcemax.y=-10000;
   $sourcemax.z=-10000;

   for $i=0 to $sourcenumpts-1 do
   {
    $p=$source.GetPoint($i);
    if ($p.x < $sourcemin.x) $sourcemin.x=$p.x;
    if ($p.y < $sourcemin.y) $sourcemin.y=$p.y;
    if ($p.z < $sourcemin.z) $sourcemin.z=$p.z;
    if ($p.x > $sourcemax.x) $sourcemax.x=$p.x;
    if ($p.y > $sourcemax.y) $sourcemax.y=$p.y;
    if ($p.z > $sourcemax.z) $sourcemax.z=$p.z;
   }

   $sourcecenter.x = 0.5 * ($sourcemax.x + $sourcemin.x);
   $sourcecenter.y = 0.5 * ($sourcemax.y + $sourcemin.y);
   $sourcecenter.z = 0.5 * ($sourcemax.z + $sourcemin.z);

   $output.print("Source:\n");
   $output.print("Points: %d, Faces: %d, Edges: %d\n",$sourcenumpts,$sourcenumfaces,$sourcenumedges);
   $output.print("Loc: (%0.2f,%0.2f,%0.2f)\n",$source.loc.x,$source.loc.y,$source.loc.z);
   $output.print("Center: (%0.2f,%0.2f,%0.2f)\n",$sourcecenter.x,$sourcecenter.y,$sourcecenter.z);
   $output.print("Min: (%0.2f,%0.2f,%0.2f) Max: (%0.2f,%0.2f,%0.2f)\n",$sourcemin.x,$sourcemin.y,$sourcemin.z,$sourcemax.x,$sourcemax.y,$sourcemax.z);
   $output.print("Orientation (%0.2f,%0.2f,%0.2f,%0.2f)\n",$sourceorientation.x,$sourceorientation.y,$sourceorientation.z,$sourceorientation.w);


   /* get target stats */
   $targetmin.x=10000;
   $targetmin.y=10000;
   $targetmin.z=10000;
   $targetmax.x=-10000;
   $targetmax.y=-10000;
   $targetmax.z=-10000;

   for $i=0 to $targetnumpts-1 do
   {
    $p=$target.GetPoint($i);
    if ($p.x < $targetmin.x) $targetmin.x=$p.x;
    if ($p.y < $targetmin.y) $targetmin.y=$p.y;
    if ($p.z < $targetmin.z) $targetmin.z=$p.z;
    if ($p.x > $targetmax.x) $targetmax.x=$p.x;
    if ($p.y > $targetmax.y) $targetmax.y=$p.y;
    if ($p.z > $targetmax.z) $targetmax.z=$p.z;
   }

   $targetcenter.x = 0.5 * ($targetmax.x + $targetmin.x);
   $targetcenter.y = 0.5 * ($targetmax.y + $targetmin.y);
   $targetcenter.z = 0.5 * ($targetmax.z + $targetmin.z);

   $output.print("Target:\n");
   $output.print("Points: %d, Faces: %d, Edges: %d\n",$targetnumpts,$targetnumfaces,$targetnumedges);
   $output.print("Loc: (%0.2f,%0.2f,%0.2f)\n",$target.loc.x,$target.loc.y,$target.loc.z);
   $output.print("Center: (%0.2f,%0.2f,%0.2f)\n",$targetcenter.x,$targetcenter.y,$targetcenter.z);
   $output.print("Min: (%0.2f,%0.2f,%0.2f) Max: (%0.2f,%0.2f,%0.2f)\n",$targetmin.x,$targetmin.y,$targetmin.z,$targetmax.x,$targetmax.y,$targetmax.z);
   $output.print("Orientation (%0.2f,%0.2f,%0.2f,%0.2f)\n",$targetorientation.x,$targetorientation.y,$targetorientation.z,$targetorientation.w);


} else {
   $output.print("No source and/or target\n");
} /* end of have source or target if */
$output.close;

--- End code ---

Francesco:
Actually the diagram shows what I already have done in the Polimagnet script attached some posts above (that is not the MultiMagnet scripts, sorry for this mess of names).

A little resume:
Magnet script --> a single spherical magnet;
MultiMagnet script --> a spherical magnet for each magnet's vertex (the "crap" script);
Polimagnet script --> the last one, the one that checks each vertex of mesh01 against each magnet's face;

Now I know that I cannot access pivot's position, thus I have to wait for the next ASL release before fixing that.

The other thing I needed to know was how to apply the rotation, I think that I can spill it out from your code NickE.

Thank you all for your support.

neirao:
Hi Francesco,

your script is time in time better.. ;)

soon he will be perfect for to recognize collision with all object formats!

congratulations! :D

neirão

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version