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.
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;