-
Notifications
You must be signed in to change notification settings - Fork 67
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
Subtraction operator for Inertial class #432
Merged
Merged
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f8e9375
Added initial work on subtraction operator
deepanshubansal01 e36c314
add + operator inside ruby file
deepanshubansal01 a52d5bb
unit test subtract operator with two half cubes
deepanshubansal01 5d978f6
unit test two rotated half cubes
deepanshubansal01 fc2b01d
piggy bank on +operator for -operator
deepanshubansal01 0ed01be
implement -=operator separately instead of piggybanking to avoid conf…
deepanshubansal01 8508889
add -operator unit test along with +operator
deepanshubansal01 286086c
add remaining unit tests for -operator
deepanshubansal01 7689ede
make codecheck
deepanshubansal01 48cc689
correct unit test bug
deepanshubansal01 9b7f131
added python binding and unit test
deepanshubansal01 3ffe8eb
Merge branch 'main' into deepanshu/inertial-subtract-operator
adityapande-1995 fb94579
code review changes
deepanshubansal01 0142b93
added subtraction invalid unit test
deepanshubansal01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -366,21 +366,26 @@ TEST(Inertiald_Test, Diagonalize) | |
0.25*math::Vector3d(-sqrt(3), -sqrt(3)/2, 3.0)); | ||
} | ||
///////////////////////////////////////////////// | ||
TEST(Inertiald_Test, Addition) | ||
TEST(Inertiald_Test, AdditionSubtraction) | ||
{ | ||
// Add two half-cubes together | ||
// Add two half-cubes together and | ||
// Subtract one half-cube from a full cube | ||
{ | ||
const double mass = 12.0; | ||
const math::Vector3d size(1, 1, 1); | ||
math::MassMatrix3d cubeMM3; | ||
EXPECT_TRUE(cubeMM3.SetFromBox(mass, size)); | ||
const math::Inertiald cube(cubeMM3, math::Pose3d::Zero); | ||
math::MassMatrix3d half; | ||
EXPECT_TRUE(half.SetFromBox(0.5*mass, math::Vector3d(0.5, 1, 1))); | ||
const math::Vector3d half_size(0.5, 1, 1); | ||
EXPECT_TRUE(half.SetFromBox(0.5*mass, half_size)); | ||
math::Inertiald left(half, math::Pose3d(-0.25, 0, 0, 0, 0, 0)); | ||
math::Inertiald right(half, math::Pose3d(0.25, 0, 0, 0, 0, 0)); | ||
EXPECT_EQ(cube, left + right); | ||
EXPECT_EQ(cube, right + left); | ||
EXPECT_EQ(right, cube - left); | ||
EXPECT_EQ(left, cube - right); | ||
|
||
// test += operator | ||
{ | ||
math::Inertiald tmp = left; | ||
|
@@ -392,6 +397,18 @@ TEST(Inertiald_Test, Addition) | |
tmp += left; | ||
EXPECT_EQ(cube, tmp); | ||
} | ||
// test -= operator | ||
{ | ||
math::Inertiald tmp = cube; | ||
tmp -= right; | ||
EXPECT_EQ(left, tmp); | ||
} | ||
{ | ||
math::Inertiald tmp = cube; | ||
tmp -= left; | ||
EXPECT_EQ(right, tmp); | ||
} | ||
|
||
// Test EquivalentBox | ||
{ | ||
math::Vector3d size2; | ||
|
@@ -407,9 +424,24 @@ TEST(Inertiald_Test, Addition) | |
EXPECT_EQ(size, size2); | ||
EXPECT_EQ(rot2, math::Quaterniond::Identity); | ||
} | ||
{ | ||
math::Vector3d size2; | ||
math::Quaterniond rot2; | ||
EXPECT_TRUE((cube - right).MassMatrix().EquivalentBox(size2, rot2)); | ||
EXPECT_EQ(half_size, size2); | ||
EXPECT_EQ(rot2, math::Quaterniond::Identity); | ||
} | ||
{ | ||
math::Vector3d size2; | ||
math::Quaterniond rot2; | ||
EXPECT_TRUE((cube - left).MassMatrix().EquivalentBox(size2, rot2)); | ||
EXPECT_EQ(half_size, size2); | ||
EXPECT_EQ(rot2, math::Quaterniond::Identity); | ||
} | ||
} | ||
|
||
// Add two rotated half-cubes together | ||
// Add two rotated half-cubes together and | ||
// Subtract a rotated half-cube from rotated full-cube | ||
{ | ||
const double mass = 12.0; | ||
const math::Vector3d size(1, 1, 1); | ||
|
@@ -425,6 +457,7 @@ TEST(Inertiald_Test, Addition) | |
// objects won't match exactly | ||
// since inertia matrices will all be in base frame | ||
// but mass, center of mass, and base-frame MOI should match | ||
// +operator | ||
EXPECT_NE(cube, left + right); | ||
EXPECT_NE(cube, right + left); | ||
EXPECT_DOUBLE_EQ(cubeMM3.Mass(), (left + right).MassMatrix().Mass()); | ||
|
@@ -433,48 +466,69 @@ TEST(Inertiald_Test, Addition) | |
EXPECT_EQ(cube.Pose().Pos(), (right + left).Pose().Pos()); | ||
EXPECT_EQ(cube.Moi(), (left + right).Moi()); | ||
EXPECT_EQ(cube.Moi(), (right + left).Moi()); | ||
// -operator | ||
EXPECT_NE(left, cube - right); | ||
EXPECT_NE(right, cube - left); | ||
EXPECT_DOUBLE_EQ(left.MassMatrix().Mass(), | ||
(cube - right).MassMatrix().Mass()); | ||
EXPECT_DOUBLE_EQ(right.MassMatrix().Mass(), | ||
(cube - left).MassMatrix().Mass()); | ||
EXPECT_EQ(left.Pose().Pos(), (cube - right).Pose().Pos()); | ||
EXPECT_EQ(right.Pose().Pos(), (cube - left).Pose().Pos()); | ||
EXPECT_EQ(left.Moi(), (cube - right).Moi()); | ||
EXPECT_EQ(right.Moi(), (cube - left).Moi()); | ||
} | ||
|
||
// Add eight cubes together into larger cube | ||
// Add eight cubes together into larger cube and | ||
// Subtract seven cubes from larger cube | ||
{ | ||
const double mass = 12.0; | ||
const math::Vector3d size(1, 1, 1); | ||
math::MassMatrix3d cubeMM3; | ||
EXPECT_TRUE(cubeMM3.SetFromBox(mass, size)); | ||
const math::Inertiald addedCube = | ||
const math::Inertiald sevenCubes = | ||
math::Inertiald(cubeMM3, math::Pose3d(-0.5, -0.5, -0.5, 0, 0, 0)) + | ||
math::Inertiald(cubeMM3, math::Pose3d(-0.5, 0.5, -0.5, 0, 0, 0)) + | ||
math::Inertiald(cubeMM3, math::Pose3d(0.5, -0.5, -0.5, 0, 0, 0)) + | ||
math::Inertiald(cubeMM3, math::Pose3d(0.5, 0.5, -0.5, 0, 0, 0)) + | ||
math::Inertiald(cubeMM3, math::Pose3d(-0.5, -0.5, 0.5, 0, 0, 0)) + | ||
math::Inertiald(cubeMM3, math::Pose3d(-0.5, 0.5, 0.5, 0, 0, 0)) + | ||
math::Inertiald(cubeMM3, math::Pose3d(0.5, -0.5, 0.5, 0, 0, 0)) + | ||
math::Inertiald(cubeMM3, math::Pose3d(0.5, -0.5, 0.5, 0, 0, 0)); | ||
const math::Inertiald lastCube = | ||
math::Inertiald(cubeMM3, math::Pose3d(0.5, 0.5, 0.5, 0, 0, 0)); | ||
const math::Inertiald addedCube = sevenCubes + lastCube; | ||
|
||
math::MassMatrix3d trueCubeMM3; | ||
EXPECT_TRUE(trueCubeMM3.SetFromBox(8*mass, 2*size)); | ||
EXPECT_EQ(addedCube, math::Inertiald(trueCubeMM3, math::Pose3d::Zero)); | ||
EXPECT_EQ(lastCube, addedCube - sevenCubes); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: let's add |
||
EXPECT_EQ(sevenCubes, addedCube - lastCube); | ||
} | ||
|
||
// Add eight rotated cubes together into larger cube | ||
// Add eight rotated cubes together into larger cube and | ||
// Subtract seven rotated cubes from larger cube | ||
{ | ||
const double mass = 12.0; | ||
const math::Vector3d size(1, 1, 1); | ||
math::MassMatrix3d cubeMM3; | ||
EXPECT_TRUE(cubeMM3.SetFromBox(mass, size)); | ||
const math::Inertiald addedCube = | ||
const math::Inertiald sevenCubes = | ||
math::Inertiald(cubeMM3, math::Pose3d(-0.5, -0.5, -0.5, 0, 0, 0)) + | ||
math::Inertiald(cubeMM3, math::Pose3d(-0.5, 0.5, -0.5, IGN_PI_2, 0, 0)) + | ||
math::Inertiald(cubeMM3, math::Pose3d(0.5, -0.5, -0.5, 0, IGN_PI_2, 0)) + | ||
math::Inertiald(cubeMM3, math::Pose3d(0.5, 0.5, -0.5, 0, 0, IGN_PI_2)) + | ||
math::Inertiald(cubeMM3, math::Pose3d(-0.5, -0.5, 0.5, IGN_PI, 0, 0)) + | ||
math::Inertiald(cubeMM3, math::Pose3d(-0.5, 0.5, 0.5, 0, IGN_PI, 0)) + | ||
math::Inertiald(cubeMM3, math::Pose3d(0.5, -0.5, 0.5, 0, 0, IGN_PI)) + | ||
math::Inertiald(cubeMM3, math::Pose3d(0.5, -0.5, 0.5, 0, 0, IGN_PI)); | ||
const math::Inertiald lastCube = | ||
math::Inertiald(cubeMM3, math::Pose3d(0.5, 0.5, 0.5, 0, 0, 0)); | ||
const math::Inertiald addedCube = sevenCubes + lastCube; | ||
|
||
math::MassMatrix3d trueCubeMM3; | ||
EXPECT_TRUE(trueCubeMM3.SetFromBox(8*mass, 2*size)); | ||
EXPECT_EQ(addedCube, math::Inertiald(trueCubeMM3, math::Pose3d::Zero)); | ||
EXPECT_EQ(lastCube, addedCube - sevenCubes); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add |
||
EXPECT_EQ(sevenCubes, addedCube - lastCube); | ||
} | ||
|
||
// Add two cubes with diagonal corners touching at one point | ||
|
@@ -506,9 +560,11 @@ TEST(Inertiald_Test, Addition) | |
gz::math::Vector3d::Zero, | ||
cubeMM3.OffDiagonalMoments()); | ||
|
||
const math::Inertiald diagonalCubes = | ||
math::Inertiald(cubeMM3, math::Pose3d(-0.5, -0.5, -0.5, 0, 0, 0)) + | ||
math::Inertiald(cubeMM3, math::Pose3d(0.5, 0.5, 0.5, 0, 0, 0)); | ||
const math::Inertiald cube1 = | ||
math::Inertiald(cubeMM3, math::Pose3d(-0.5, -0.5, -0.5, 0, 0, 0)); | ||
const math::Inertiald cube2 = | ||
math::Inertiald(cubeMM3, math::Pose3d(0.5, 0.5, 0.5, 0, 0, 0)); | ||
const math::Inertiald diagonalCubes = cube1 + cube2; | ||
|
||
// lumped mass = 6 + 6 = 12 | ||
// lumped center of mass at (0, 0, 0) | ||
|
@@ -539,6 +595,24 @@ TEST(Inertiald_Test, Addition) | |
EXPECT_EQ( | ||
gz::math::Vector3d(-3, -3, -3), | ||
diagonalCubes.MassMatrix().OffDiagonalMoments()); | ||
|
||
// -operator | ||
EXPECT_EQ(cube1.Pose(), (diagonalCubes - cube2).Pose()); | ||
EXPECT_EQ(cube2.Pose(), (diagonalCubes - cube1).Pose()); | ||
EXPECT_DOUBLE_EQ(mass, (diagonalCubes - cube2).MassMatrix().Mass()); | ||
EXPECT_DOUBLE_EQ(mass, (diagonalCubes - cube1).MassMatrix().Mass()); | ||
EXPECT_EQ( | ||
cubeMM3.DiagonalMoments(), | ||
(diagonalCubes - cube2).MassMatrix().DiagonalMoments()); | ||
EXPECT_EQ( | ||
cubeMM3.DiagonalMoments(), | ||
(diagonalCubes - cube1).MassMatrix().DiagonalMoments()); | ||
EXPECT_EQ( | ||
cubeMM3.OffDiagonalMoments(), | ||
(diagonalCubes - cube2).MassMatrix().OffDiagonalMoments()); | ||
EXPECT_EQ( | ||
cubeMM3.OffDiagonalMoments(), | ||
(diagonalCubes - cube1).MassMatrix().OffDiagonalMoments()); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should also add a test case that exercises this line of code. It can be in a separate test case than
AdditionSubtraction
, likeInvalidSubtraction
or something like thatThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On it!