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

Fixed material Python interface #248

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/python/Material.i
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@

%include "std_string.i"
%include "std_map.i"
%template(map_material_type) std::map<ignition::math::MaterialType,

%template(map_material_type) std::map<int,
ignition::math::Material>;

namespace ignition
Expand All @@ -41,7 +42,7 @@ namespace math
public: explicit Material(const double _density);
public: Material(const Material &_material);
public: ~Material();
public: static const std::map<MaterialType, Material> &Predefined();
public: static const std::map<int, Material> &Predefined();
public: void SetToNearestDensity(
const double _value,
const double _epsilon = std::numeric_limits<double>::max());
Expand Down
30 changes: 29 additions & 1 deletion src/python/Material_TEST.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,43 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
import unittest

import ignition.math


class TestMaterial(unittest.TestCase):

def test_init(self):
mats = ignition.math.Material.predefined()
self.assertFalse(mats.empty())
self.assertTrue(len(mats))

# Make sure that the number of elements in the MaterialType enum matches
# the number of elements in the MaterialDensity::materials map.
self.assertEqual(ignition.math.MaterialType_UNKNOWN_MATERIAL, len(mats))

# Iterate over each element in the enum. Check the that enum value
# matches the type value in the mats map.
for i in range(ignition.math.MaterialType_UNKNOWN_MATERIAL):
# Get the type of the material for MaterialType i.
self.assertEqual(i, next(iter(mats.find(i)))[1].type())

# The name should not be empty
self.assertTrue(next(iter(mats.find(i)))[1].name())

# The density should be less than the max double value and greater than
# zero.
self.assertLess(next(iter(mats.find(i)))[1].density(), sys.float_info.max)
self.assertGreater(next(iter(mats.find(i)))[1].density(), 0.0)

malicious = ignition.math.Material(42)
self.assertEqual(-1.0, malicious.density())
self.assertEqual('', malicious.name())

byDensity = ignition.math.Material(42.2)
self.assertEqual(42.2, byDensity.density())
self.assertEqual(ignition.math.MaterialType_UNKNOWN_MATERIAL, byDensity.type())

def test_comparison(self):
aluminum = ignition.math.Material(ignition.math.MaterialType_ALUMINUM)
Expand Down