Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Simitary3 with Align feature #464

Merged
merged 44 commits into from
Sep 28, 2020
Merged

Conversation

Alexma3312
Copy link
Contributor

This PR adds four functions into the Similarity3 class.
The code format is based on Google format and GTSAM::Pose3::Align().

  1. transformFrom - Similarity transform of a Pose3 object
  2. Align(point pairs) - Calculate Similarity transform from matched point pairs
  3. rotationAveraging - Calculate average rotation from a list of rotations
  4. Align(pose pairs) - Calculate Similarity transform from matched pose pairs

Visualization of unit tests for the Align functions:

  1. Calculate Similarity transform from matched point pairs
    Input:
    r_point1 = Point3(0, 0, 0) g_point1 = Point3(6, 8, 10)
    r_point2 = Point3(3, 0, 0) g_point2 = Point3(6, 2, 10)
    r_point3 = Point3(3, 0, 4) g_point3 = Point3(6, 2, 18)
    pr1

Output:
gRr = Rot3.Rz(math.radians(-90))
s = 2
gtr = Point3(6, 8, 10)

  1. Calculate Similarity transform from matched pose pairs
    r_pose1 = Pose3(Rot3(1, 0, 0, 0, 1, 0, 0, 0, 1), Point3(0, 0, 0))
    r_pose2 = Pose3(Rot3(-1, 0, 0, 0, 1, 0, 0, 0, 1), Point3(4, 0, 0))
    g_pose1 = Pose3(Rot3(-1, 0, 0, 0, 1, 0, 0, 0, -1), Point3(4, 6, 10))
    g_pose2 = Pose3(Rot3(1, 0, 0, 0, 1, 0, 0, 0, -1), Point3(-4, 6, 10))
    pr2

Output:
gRr = Rot3.Ry(math.radians(180))
s = 2
gtr = Point3(4, 6, 10)

Copy link
Collaborator

@varunagrawal varunagrawal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome. I've been meaning to update the Sim(3) code for a while now so this makes life really good. Thanks @Alexma3312.

Please look at my comments and address them?

gtsam/geometry/Pose3.h Outdated Show resolved Hide resolved
gtsam_unstable/geometry/Similarity3.cpp Outdated Show resolved Hide resolved
gtsam_unstable/geometry/Similarity3.cpp Outdated Show resolved Hide resolved
gtsam_unstable/geometry/Similarity3.cpp Outdated Show resolved Hide resolved
Copy link
Member

@dellaert dellaert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! But I have a lot of advice :-)

gtsam_unstable/geometry/tests/testSimilarity3.cpp Outdated Show resolved Hide resolved
gtsam_unstable/geometry/tests/testSimilarity3.cpp Outdated Show resolved Hide resolved
gtsam_unstable/geometry/tests/testSimilarity3.cpp Outdated Show resolved Hide resolved
gtsam_unstable/geometry/tests/testSimilarity3.cpp Outdated Show resolved Hide resolved
gtsam_unstable/geometry/Similarity3.cpp Outdated Show resolved Hide resolved
gtsam_unstable/geometry/Similarity3.cpp Outdated Show resolved Hide resolved
gtsam_unstable/geometry/Similarity3.cpp Outdated Show resolved Hide resolved
gtsam_unstable/geometry/Similarity3.cpp Outdated Show resolved Hide resolved
gtsam_unstable/geometry/Similarity3.cpp Show resolved Hide resolved
@varunagrawal varunagrawal added the feature New proposed feature label Aug 15, 2020
@dellaert
Copy link
Member

@Alexma3312 please don't push to repo until you resolved all comments in my review. Every push triggers a long CI process.

@dellaert
Copy link
Member

Please merge in develop before pushing, as well. We changed CI system.

Copy link
Member

@dellaert dellaert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Becoming beautiful! I'd just like to see one more big change, in addition to a few minor comments

gtsam_unstable/geometry/Similarity3.cpp Outdated Show resolved Hide resolved
gtsam_unstable/geometry/Similarity3.cpp Outdated Show resolved Hide resolved
gtsam_unstable/geometry/Similarity3.cpp Outdated Show resolved Hide resolved
gtsam_unstable/geometry/Similarity3.cpp Outdated Show resolved Hide resolved
gtsam_unstable/geometry/Similarity3.cpp Outdated Show resolved Hide resolved
gtsam_unstable/geometry/Similarity3.cpp Show resolved Hide resolved
gtsam_unstable/geometry/Similarity3.h Outdated Show resolved Hide resolved
gtsam_unstable/geometry/Similarity3.h Outdated Show resolved Hide resolved
gtsam_unstable/geometry/Similarity3.h Outdated Show resolved Hide resolved
Copy link
Member

@dellaert dellaert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confused. If my comment does not make sense maybe we should meet. But try to understand what I am suggesting first :-)

gtsam_unstable/geometry/Similarity3.cpp Outdated Show resolved Hide resolved
Copy link
Member

@dellaert dellaert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woohoo, so much clearer and faster. One more round which is mostly style but also some remaining performance improvements. Esp unpacking pairs is not needed in most cases, which will yield speed improvement and clarity (in case of centroids).

gtsam_unstable/geometry/Similarity3.h Show resolved Hide resolved
gtsam_unstable/geometry/Similarity3.cpp Outdated Show resolved Hide resolved
gtsam_unstable/geometry/Similarity3.cpp Outdated Show resolved Hide resolved
gtsam_unstable/geometry/Similarity3.cpp Outdated Show resolved Hide resolved
gtsam_unstable/geometry/Similarity3.cpp Outdated Show resolved Hide resolved
gtsam_unstable/geometry/Similarity3.cpp Outdated Show resolved Hide resolved
gtsam_unstable/geometry/Similarity3.cpp Outdated Show resolved Hide resolved
gtsam_unstable/geometry/Similarity3.cpp Outdated Show resolved Hide resolved
gtsam_unstable/geometry/Similarity3.cpp Outdated Show resolved Hide resolved
gtsam_unstable/geometry/Similarity3.cpp Show resolved Hide resolved
Copy link
Member

@dellaert dellaert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Brilliant! This is high-quality code now!! super-factored :-)

There is one issue to be resolved before we can merge: the 2 checks with quaternion=ON are failing. Please compile with quaternions to see whether you can diagnose the issue?

@Alexma3312
Copy link
Contributor Author

Alexma3312 commented Sep 27, 2020

Brilliant! This is high-quality code now!! super-factored :-)

There is one issue to be resolved before we can merge: the 2 checks with quaternion=ON are failing. Please compile with quaternions to see whether you can diagnose the issue?

I found that the issue occurs on the Rot3(1, 0, 0, 0, 1, 0, 0, 0, -1).

Rot3 R = Rot3::ClosestTo(Rot3(1, 0, 0, 0, 1, 0, 0, 0, -1).matrix());
R.print();

Output:

R: [
        1, 0, 0;
        0, 1, 0;
        0, 0, 1
]

Moreover, I created a new test for the compose function in testRot3.cpp:
Expected_1 passes the test when the quaternions flag is both on and off.
Expected_2 only passes the test when the quaternions flag is off.

TEST(Rot3, compose_2) {
  Rot3 R = Rot3(-1, 0, 0, 0, 1, 0, 0, 0, -1);
  // Create source poses
  Rot3 R1 = Rot3();
  Rot3 R2 = Rot3(-1, 0, 0, 0, 1, 0, 0, 0, 1);
  // Create destination poses
  Rot3 expected_1 = Rot3(-1, 0, 0, 0, 1, 0, 0, 0, -1);
  Rot3 expected_2 = Rot3(1, 0, 0, 0, 1, 0, 0, 0, -1);

  EXPECT(assert_equal(expected_1, R.compose(R1)));
  EXPECT(assert_equal(expected_2, R.compose(R2)));
  EXPECT(assert_equal(expected_2, R * R2));
}

The error of expected_2 when the quaternions flag is on is:
The error is weird because the expected output (1,0,0,0,1,0,0,0,1) is different from what I declared (1, 0, 0, 0, 1, 0, 0, 0, -1).

Not equal:
expected:
 [
        1, 0, 0;
        0, 1, 0;
        0, 0, 1
]
actual:
 [
        -2.22045e-16, 0, 0;
        0, 1, 0;
        0, 0, -2.22045e-16
]
testRot3.cpp:404: Failure: "assert_equal(expected_2, R.compose(R2))"

The error still occurs and remains the same when I switch from

R.compose(R2)

to

R*R2

@dellaert
Copy link
Member

That matrix is a reflection, not a rotation, I think. I have no idea where you copied this line from, though. Is that in a test, or where??

@Alexma3312
Copy link
Contributor Author

Alexma3312 commented Sep 28, 2020

That matrix is a reflection, not a rotation, I think. I have no idea where you copied this line from, though. Is that in a test, or where??

That line is not in a test. I can create a test in testRot3, but yes, you are right the matrix is a reflection. And that causes the quaternions issue, I will fix the unittests for Similarity3.

@dellaert
Copy link
Member

Congrats @Alexma3312 , and many thanks for your patience and diligence. I know I was demanding during the review :-)

@dellaert dellaert merged commit b89478c into borglab:develop Sep 28, 2020
@dellaert
Copy link
Member

PS @Alexma3312 if you want to write a blog post about this (you have examples in the test) then I’ll post it on gtsam.org and tweet about it :-)

@Alexma3312
Copy link
Contributor Author

PS @Alexma3312 if you want to write a blog post about this (you have examples in the test) then I’ll post it on gtsam.org and tweet about it :-)

Thank you @dellaert for spending your time helping me to improve my coding style. I will be happy to write a blog post about this.

aCentroid += abPair.first;
bCentroid += abPair.second;
}
const double f = 1.0 / n;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throw an exception if n==0?
PS: Sorry for the late comment... :-S

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jlblancoc Thanks for the catch. I will create a new PR to fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New proposed feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants