0

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.

user1275607
  • 99
  • 1
  • 10

1 Answers1

1

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

Severin Pappadeux
  • 18,636
  • 3
  • 38
  • 64
  • Severin Thanks a lot for your answer. If I implement uniform rotation operation, do you think the overall Monte-carlo sampling I am using is good? You mentioned something about "you don't really care whether old orientation matters or not". Could you please elaborate? I am also wondering about the probability distribution for random variables and acceptance of move (I read somewhere, it says ~40% of moves needs to get accepted). – user1275607 Aug 13 '15 at 11:31
  • yes, sampling looks ok. Wrt not caring about old orientation, think about it in this way: if your start angle is some XX degrees, and then you sample rotation angle uniform in the range [0...360] and then add it to XX and you'll get [XX...360+XX]. Then when you map it back to your interval [0...360] when you start to compute rotation matrix, it is absolutely equivalent to the case where you don't care about start point and just sample angle uniformly in [0...360]. Uniform sampling with wrapping does not care about start point – Severin Pappadeux Aug 13 '15 at 15:16
  • Wrt acceptance rate you don't provide enough info to look at it. I would advice you to ask another question with relevant bits and pieces included – Severin Pappadeux Aug 13 '15 at 15:17
  • Just another link to look at uniform-on-sphere sampling, http://mathworld.wolfram.com/SpherePointPicking.html – Severin Pappadeux Aug 13 '15 at 15:19
  • Severin Thanks again. You are right I am not caring about start point. As the points are moving, once you find the best fit as compared to previous pose. For the acceptance rate, I have no idea how to introduced it in MC algorithm in my case. The code I put in my post, is the code I am using. Thanks again for your help. – user1275607 Aug 17 '15 at 07:19
  • 1
    @user1275607 Well, acceptance/rejection shall be based upon some criteria, which is computed to be in [0...1) range and then compared to getRanNumberDouble(0.0, 1.0). Something along the line `crit = f(oldpos, newpos); if (crit < getRanNumberDouble(0.0, 1.0)) /* accepted */ return newpos; else return oldpos; /* rejected */ ` – Severin Pappadeux Aug 17 '15 at 19:35