I am writing java code for rigid body superposition of two objects (A and B). Object A is fix in a space while object B is free to move in the space. Goal is to get the best matching fit between A and B.
In order to find optimal fit between A and B, I would like to sample the possible poses of B. For this purpose I am trying to implement the Monte-Carlo based sampling procedure. But I am not sure if I am doing it in right way or not. I read a couple of articles found on google, but I am bit confuse about probability distribution for random variables and move acceptance criterion.
As the object B can translate and rotate in space, I defined 6 variables (6D space) which corrosponds to the x,y,z translation and rotation. I do the 50000 random move. After each move I check if the fit between A and new pose of B is good or not. Below is simplified java code which I am using right now.
object A;
object B;
object B_NewPose;
double winnerScore;
for (int iter = 0; iter < 50000; iter++) {
double x_tranlation = 0; //translate in X (max translation is +-10 Amstrong)
double y_tranlation = 0; //translate in Y (max translation is +-10 Amstrong)
double z_tranlation = 0; //translate in Z (max translation is +-10 Amstrong)
double x_rotation = 0; //rotate around X (max rotation is 360 degree)
double y_rotation = 0; //rotate around Y (max rotation is 360 degree)
double z_rotation = 0; //rotate around Z (max rotation is 360 degree)
//random selection of the axes for translation and rotation
int axis_Idx = getRanNumberInt(0, 2); // (0=x, 1=y and 2=z)
//get the amount of translation (can be right (+) or left(-))
double translate = getRanNumberDouble(0, 10);
if (axis_Idx==0) {
x_tranlation=translate;
}
if (axis_Idx==1) {
y_tranlation=translate;
}
if (axis_Idx==2) {
z_tranlation=translate;
}
//get the amount of rotation
double rotation = getRanNumberDouble(0, 360);
if (axis_Idx==0) {
x_rotation=rotation;
}
if (axis_Idx==1) {
y_rotation=rotation;
}
if (axis_Idx==2){
z_rotation=rotation;
}
//Now move the object: translation and rotation
B_NewPose= moveTheObj(object B, x_tranlation, y_tranlation, z_tranlation, x_rotation, y_rotation, z_rotation);
//Check how good is the fit between A and N_NewPose
double score = calculateFit(A, B_NewPose);
if (score> oldScore){
oldScore=score;
B=B_NewPose;
}
}
I would be very grateful if someone could directs me in right direction or help me to clarify the Monte-carlo algorithm in this scene. Thanks a lot in advance.
Translations looks ok, they are linear
wrt rotations, I'm not sure it is uniform on the sphere. So, you might consider the case that you might want just random orientation of your object, because you don't really care whether old orientation matters or not
For that there is really simple algorithm to generate new object axis/orientation position
phi = 2.0 * PI * getRanNumberDouble(0.0, 1.0);
csth = 2.0 * getRanNumberDouble(0.0, 1.0) - 1.0;
snth = sqrt((1.0 - csth)*(1.0 + csth));
wx = snth * sin(phi);
wy = snth * cos(phi);
wz = csth;
That will give you normalized vector (wx, wy, wz)
which would be your object new orientation/axis.
If you really need random rotation, you might want to sample uniform-on-the-sphere vector to rotate around just like above method, and add angle of rotation
angle = 2.0 * PI * getRanNumberDouble(0.0, 1.0);
Then (wx, wy, wz, angle)
is basically a uniformly distributed quaternion, which you could use to build rotation operation (using quaternion, or making matrix out of it or Rodriges formula, whatever you prefer).
I'm using radians