1 #include <gtest/gtest.h> 8 TEST(PolyhedronTest, CubeTotalVolume)
11 auto volume = cube.ComputeFullVolume();
12 EXPECT_FLOAT_EQ(volume.volume, 8.0);
13 EXPECT_FLOAT_EQ(volume.centroid.X(), 0.0);
14 EXPECT_FLOAT_EQ(volume.centroid.Y(), 0.0);
15 EXPECT_FLOAT_EQ(volume.centroid.Z(), 0.0);
19 TEST(PolyhedronTest, CylinderTotalVolume)
22 auto volume = cylinder.ComputeFullVolume();
23 EXPECT_NEAR(volume.volume, 1.57, 0.01);
24 EXPECT_NEAR(volume.centroid.X(), 0.0, 1e-10);
25 EXPECT_NEAR(volume.centroid.Y(), 0.0, 1e-10);
26 EXPECT_NEAR(volume.centroid.Z(), 0.0, 1e-10);
30 TEST(PolyhedronTest, CubeNotSubmerged)
36 auto volume = cube.SubmergedVolume(ignition::math::Vector3d{0,0,2},
37 ignition::math::Quaterniond{1,0,0,0},
40 EXPECT_FLOAT_EQ(volume.volume, 0.0);
41 EXPECT_FLOAT_EQ(volume.centroid.X(), 0.0);
42 EXPECT_FLOAT_EQ(volume.centroid.Y(), 0.0);
43 EXPECT_FLOAT_EQ(volume.centroid.Z(), 0.0);
47 TEST(PolyhedronTest, CylinderNotSubmerged)
53 auto volume = cylinder.SubmergedVolume(ignition::math::Vector3d{0,0,2},
54 ignition::math::Quaterniond{1,0,0,0},
57 EXPECT_FLOAT_EQ(volume.volume, 0.0);
58 EXPECT_FLOAT_EQ(volume.centroid.X(), 0.0);
59 EXPECT_FLOAT_EQ(volume.centroid.Y(), 0.0);
60 EXPECT_FLOAT_EQ(volume.centroid.Z(), 0.0);
64 TEST(PolyhedronTest, CubeSubmerged)
74 auto volume = cube.SubmergedVolume(ignition::math::Vector3d{0,0,0},
75 ignition::math::Quaterniond{1,0,0,0},
77 EXPECT_FLOAT_EQ(volume.volume, 0.5);
78 EXPECT_FLOAT_EQ(volume.centroid.X(), 0.0);
79 EXPECT_FLOAT_EQ(volume.centroid.Y(), 0.0);
80 EXPECT_FLOAT_EQ(volume.centroid.Z(), -0.25);
87 volume = cube.SubmergedVolume(ignition::math::Vector3d{0,0,-2},
88 ignition::math::Quaterniond{1,0,0,0},
90 EXPECT_FLOAT_EQ(volume.volume, 1.0);
91 EXPECT_FLOAT_EQ(volume.centroid.X(), 0.0);
92 EXPECT_FLOAT_EQ(volume.centroid.Y(), 0.0);
93 EXPECT_FLOAT_EQ(volume.centroid.Z(), -2.0);
100 volume = cube.SubmergedVolume(ignition::math::Vector3d{0,0,0.25},
101 ignition::math::Quaterniond{0.9238795, 0.3826834, 0, 0},
103 EXPECT_NEAR(volume.volume, 0.21, 0.01);
104 EXPECT_FLOAT_EQ(volume.centroid.X(), 0.0);
105 EXPECT_FLOAT_EQ(volume.centroid.Y(), 0.0);
106 EXPECT_NEAR(volume.centroid.Z(), -0.15, 0.01);
110 TEST(PolyhedronTest, CylinderSubmerged)
120 auto volume = cylinder.SubmergedVolume(ignition::math::Vector3d{0,0,0.0},
121 ignition::math::Quaterniond{1,0,0,0},
123 EXPECT_NEAR(volume.volume, 0.785, 0.001);
124 EXPECT_FLOAT_EQ(volume.centroid.X(), 0.0);
125 EXPECT_FLOAT_EQ(volume.centroid.Y(), 0.0);
126 EXPECT_FLOAT_EQ(volume.centroid.Z(), -0.5);
133 volume = cylinder.SubmergedVolume(ignition::math::Vector3d{0,0,-4},
134 ignition::math::Quaterniond{1,0,0,0},
136 EXPECT_NEAR(volume.volume, 1.57, 0.01);
137 EXPECT_FLOAT_EQ(volume.centroid.X(), 0.0);
138 EXPECT_FLOAT_EQ(volume.centroid.Y(), 0.0);
139 EXPECT_FLOAT_EQ(volume.centroid.Z(), -4.0);
145 volume = cylinder.SubmergedVolume(ignition::math::Vector3d{0,0,0},
146 ignition::math::Quaterniond{0.707,0.707,0,0},
148 EXPECT_NEAR(volume.volume, 0.785, 0.001);
149 EXPECT_FLOAT_EQ(volume.centroid.X(), 0.0);
150 EXPECT_FLOAT_EQ(volume.centroid.Y(), 0.0);
151 EXPECT_NEAR(volume.centroid.Z(), -0.21, 0.01);
157 std::ostringstream modelStr;
158 modelStr <<
"<sdf version='" << SDF_VERSION <<
"'>" 159 <<
"<model name ='model'>" 160 <<
"<link name ='link'>" 161 <<
" <visual name ='vis'>" 170 sdf.SetFromString(modelStr.str());
171 return sdf.Root()->GetElement(
"model")->GetElement(
"link")->GetElement(
"visual")->GetElement(
"geometry");
175 TEST(ShapeVolumeTest, CubeNoRotation)
179 const double fluidLevel = 0;
180 const std::vector<std::vector<double>> poses = {{0,0,1.0},
186 const std::vector<std::vector<double>> expectedResult = {{0,0,0,0},
192 ignition::math::Pose3d pose;
193 for(
size_t i=0; i<poses.size(); i++)
195 pose.Pos().X() = poses[i][0];
196 pose.Pos().Y() = poses[i][1];
197 pose.Pos().Z() = poses[i][2];
198 auto vol = box->CalculateVolume(pose, fluidLevel);
199 EXPECT_FLOAT_EQ(vol.volume, expectedResult[i][0]);
200 EXPECT_FLOAT_EQ(vol.centroid.X(), expectedResult[i][1]);
201 EXPECT_FLOAT_EQ(vol.centroid.Y(), expectedResult[i][2]);
202 EXPECT_FLOAT_EQ(vol.centroid.Z(), expectedResult[i][3]);
207 TEST(ShapeVolumeTest, CubeRotation)
211 const double fluidLevel = 0;
212 const std::vector<std::vector<double>> poses = {{0,0,1.5},
219 const std::vector<std::vector<double>> expectedResult = {{0,0,0,0},
226 ignition::math::Pose3d pose;
227 pose.Rot() = {0.9238795, 0.3826834, 0, 0};
228 for(
size_t i=0; i<poses.size(); i++)
230 pose.Pos().X() = poses[i][0];
231 pose.Pos().Y() = poses[i][1];
232 pose.Pos().Z() = poses[i][2];
233 auto vol = box->CalculateVolume(pose, fluidLevel);
234 EXPECT_NEAR(vol.volume, expectedResult[i][0], 0.001);
235 EXPECT_FLOAT_EQ(vol.centroid.X(), expectedResult[i][1]);
236 EXPECT_FLOAT_EQ(vol.centroid.Y(), expectedResult[i][2]);
237 EXPECT_NEAR(vol.centroid.Z(), expectedResult[i][3], 0.001);
242 TEST(ShapeVolumeTest, CylinderNoRotation)
244 sdf::ElementPtr cylinderElem =
generateGeometryElem(
"<cylinder><radius>0.5</radius><length>2</length></cylinder>");
246 const double fluidLevel = 0;
247 const std::vector<std::vector<double>> poses = {{0,0,1.0},
255 const std::vector<std::vector<double>> expectedResult = {{0,0,0,0},
261 ignition::math::Pose3d pose;
262 for(
size_t i=0; i<poses.size(); i++)
264 pose.Pos().X() = poses[i][0];
265 pose.Pos().Y() = poses[i][1];
266 pose.Pos().Z() = poses[i][2];
267 auto vol = cylinder->CalculateVolume(pose, fluidLevel);
268 EXPECT_NEAR(vol.volume, expectedResult[i][0], 0.01);
269 EXPECT_FLOAT_EQ(vol.centroid.X(), expectedResult[i][1]);
270 EXPECT_FLOAT_EQ(vol.centroid.Y(), expectedResult[i][2]);
271 EXPECT_FLOAT_EQ(vol.centroid.Z(), expectedResult[i][3]);
276 TEST(ShapeVolumeTest, CylinderRotation)
278 sdf::ElementPtr cylinderElem =
generateGeometryElem(
"<cylinder><radius>0.5</radius><length>2</length></cylinder>");
280 const double fluidLevel = 0;
281 const std::vector<std::vector<double>> poses = {{0,0,0.5},
286 const std::vector<std::vector<double>> expectedResult = {{0,0,0,0},
290 ignition::math::Pose3d pose;
291 pose.Rot() = {0.707, 0.707, 0, 0};
292 for(
size_t i=0; i<poses.size(); i++)
294 pose.Pos().X() = poses[i][0];
295 pose.Pos().Y() = poses[i][1];
296 pose.Pos().Z() = poses[i][2];
297 auto vol = cylinder->CalculateVolume(pose, fluidLevel);
298 EXPECT_NEAR(vol.volume, expectedResult[i][0], 0.01);
299 EXPECT_FLOAT_EQ(vol.centroid.X(), expectedResult[i][1]);
300 EXPECT_FLOAT_EQ(vol.centroid.Y(), expectedResult[i][2]);
301 EXPECT_NEAR(vol.centroid.Z(), expectedResult[i][3], 0.01);
306 TEST(ShapeVolumeTest, Sphere) {
309 const double fluidLevel = 0;
310 const std::vector<std::vector<double>> poses = {{0, 0, 0.5},
317 const std::vector<std::vector<double>> expectedResult = {{0, 0, 0, 0},
318 {0.082, 0, 0, -0.088},
319 {0.262, 0, 0, -0.188},
320 {0.442, 0, 0, -0.313},
321 {0.523, 0, 0, -0.500},
322 {0.523, 0, 0, -1.000}};
324 ignition::math::Pose3d pose;
325 pose.Rot() = {0.9238795, 0.3826834, 0, 0};
326 for(
size_t i=0; i<poses.size(); i++)
328 pose.Pos().X() = poses[i][0];
329 pose.Pos().Y() = poses[i][1];
330 pose.Pos().Z() = poses[i][2];
331 auto vol = sphere->CalculateVolume(pose, fluidLevel);
332 EXPECT_NEAR(vol.volume, expectedResult[i][0], 0.001);
333 EXPECT_FLOAT_EQ(vol.centroid.X(), expectedResult[i][1]);
334 EXPECT_FLOAT_EQ(vol.centroid.Y(), expectedResult[i][2]);
335 EXPECT_NEAR(vol.centroid.Z(), expectedResult[i][3], 0.001);
341 int main(
int argc,
char **argv)
343 ::testing::InitGoogleTest(&argc, argv);
344 return RUN_ALL_TESTS();
std::unique_ptr< ShapeVolume > ShapeVolumePtr
static Polyhedron makeCube(double x, double y, double z)
Generate a cube polyhedron centered at origin.
static Polyhedron makeCylinder(double r, double l, int n)
Generate a cylinder polyhedron centered at origin.
TEST(PolyhedronTest, CubeTotalVolume)
sdf::ElementPtr generateGeometryElem(const std::string &str)
static std::unique_ptr< ShapeVolume > makeShape(const sdf::ElementPtr sdf)
Factory method for shape. Parses a shape object from sdf data.
Represents a plane as a normal and offset.
int main(int argc, char **argv)
Main.