1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
/*
* Copyright (C) 2018 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <gtest/gtest.h>
#include <cmath>
#include <iostream>
#include "ignition/math/Sphere.hh"
using namespace ignition;
using namespace math;
/////////////////////////////////////////////////
TEST(SphereTest, Constructor)
{
// Default constructor
{
math::Sphered sphere;
EXPECT_DOUBLE_EQ(0.0, sphere.Radius());
EXPECT_EQ(math::Material(), sphere.Material());
math::Sphered sphere2;
EXPECT_EQ(sphere, sphere2);
}
// Radius constructor
{
math::Sphered sphere(1.0);
EXPECT_DOUBLE_EQ(1.0, sphere.Radius());
EXPECT_EQ(math::Material(), sphere.Material());
math::Sphered sphere2(1.0);
EXPECT_EQ(sphere, sphere2);
}
// Radius and mat
{
math::Sphered sphere(1.0, math::Material(math::MaterialType::WOOD));
EXPECT_DOUBLE_EQ(1.0, sphere.Radius());
EXPECT_EQ(math::Material(math::MaterialType::WOOD), sphere.Material());
math::Sphered sphere2(1.0, math::Material(math::MaterialType::WOOD));
EXPECT_EQ(sphere, sphere2);
}
}
//////////////////////////////////////////////////
TEST(SphereTest, Comparison)
{
const math::Sphered wood(0.1, math::Material(math::MaterialType::WOOD));
{
math::Sphered modified = wood;
EXPECT_EQ(wood, modified);
modified.SetRadius(1.0);
EXPECT_NE(wood, modified);
}
{
math::Sphered modified = wood;
EXPECT_EQ(wood, modified);
modified.SetMaterial(math::Material(math::MaterialType::PINE));
EXPECT_NE(wood, modified);
}
}
//////////////////////////////////////////////////
TEST(SphereTest, Mutators)
{
math::Sphered sphere;
EXPECT_DOUBLE_EQ(0.0, sphere.Radius());
EXPECT_EQ(math::Material(), sphere.Material());
sphere.SetRadius(.123);
sphere.SetMaterial(math::Material(math::MaterialType::PINE));
EXPECT_DOUBLE_EQ(.123, sphere.Radius());
EXPECT_EQ(math::Material(math::MaterialType::PINE), sphere.Material());
}
//////////////////////////////////////////////////
TEST(SphereTest, VolumeAndDensity)
{
double mass = 1.0;
math::Sphered sphere(0.001);
double expectedVolume = (4.0/3.0) * IGN_PI * std::pow(0.001, 3);
EXPECT_DOUBLE_EQ(expectedVolume, sphere.Volume());
double expectedDensity = mass / expectedVolume;
EXPECT_DOUBLE_EQ(expectedDensity, sphere.DensityFromMass(mass));
// Bad density
math::Sphered sphere2;
EXPECT_GT(0.0, sphere2.DensityFromMass(mass));
sphere2.SetRadius(1.0);
EXPECT_GT(0.0, sphere2.DensityFromMass(0.0));
EXPECT_FALSE(sphere.SetDensityFromMass(0.0));
}
//////////////////////////////////////////////////
TEST(SphereTest, Mass)
{
double mass = 2.0;
double r = 0.1;
math::Sphered sphere(r);
EXPECT_TRUE(sphere.SetDensityFromMass(mass));
math::MassMatrix3d massMat;
double ixxIyyIzz = 0.4 * mass * r * r;
math::MassMatrix3d expectedMassMat;
expectedMassMat.SetInertiaMatrix(ixxIyyIzz, ixxIyyIzz, ixxIyyIzz,
0.0, 0.0, 0.0);
expectedMassMat.SetMass(mass);
sphere.MassMatrix(massMat);
EXPECT_EQ(expectedMassMat, massMat);
EXPECT_DOUBLE_EQ(expectedMassMat.Mass(), massMat.Mass());
}
|