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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds python interface to MaterialType and Material. #234

Merged
merged 3 commits into from
Oct 7, 2021
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
1 change: 1 addition & 0 deletions src/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ if (PYTHONLIBS_FOUND)
Kmeans_TEST
Line2_TEST
Line3_TEST
Material_TEST
Matrix3_TEST
Matrix4_TEST
MovingWindowFilter_TEST
Expand Down
59 changes: 59 additions & 0 deletions src/python/Material.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (C) 2021 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.
*
*/

%module material
%{
#include <ignition/math/config.hh>
#include <ignition/math/Export.hh>
#include <ignition/math/Material.hh>
#include <ignition/math/MaterialType.hh>
%}

%include "std_string.i"
%include "std_map.i"

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

namespace ignition
{
namespace math
{
class Material
{
%rename("%(undercase)s", %$isfunction, %$ismember, %$not %$isconstructor) "";
public: Material();
public: explicit Material(const MaterialType _type);
public: explicit Material(const std::string &_typename);
public: explicit Material(const double _density);
public: Material(const Material &_material);
public: ~Material();
public: static const std::map<int, Material> &Predefined();
public: void SetToNearestDensity(
const double _value,
const double _epsilon = std::numeric_limits<double>::max());
public: bool operator==(const Material &_material) const;
public: bool operator!=(const Material &_material) const;
public: MaterialType Type() const;
public: void SetType(const MaterialType _type);
public: std::string Name() const;
public: void SetName(const std::string &_name);
public: double Density() const;
public: void SetDensity(const double _density);
};
}
}
47 changes: 47 additions & 0 deletions src/python/MaterialType.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (C) 2021 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.
*
*/

%module materialtype
%{
#include <ignition/math/config.hh>
#include <ignition/math/Export.hh>
#include <ignition/math/MaterialType.hh>
%}

namespace ignition
{
namespace math
{
enum class MaterialType
{
STYROFOAM = 0,
PINE,
WOOD,
OAK,
PLASTIC,
CONCRETE,
ALUMINUM,
STEEL_ALLOY,
STEEL_STAINLESS,
IRON,
BRASS,
COPPER,
TUNGSTEN,
UNKNOWN_MATERIAL
};
}
}
110 changes: 110 additions & 0 deletions src/python/Material_TEST.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# Copyright (C) 2021 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.

import sys
import unittest

import ignition.math


class TestMaterial(unittest.TestCase):

def test_init(self):
mats = ignition.math.Material.predefined()
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)

modified = ignition.math.Material(aluminum)
self.assertEqual(modified, aluminum)

modified.set_density(1234.0)
self.assertNotEqual(modified, aluminum)

modified = ignition.math.Material(aluminum)
self.assertEqual(modified, aluminum)

modified.set_type(ignition.math.MaterialType_PINE)
self.assertNotEqual(modified, aluminum)

def test_accessors(self):

mat = ignition.math.Material("Aluminum")
mat1 = ignition.math.Material("aluminum")
mat2 = ignition.math.Material(ignition.math.MaterialType_ALUMINUM)
mat3 = ignition.math.Material(mat2)

self.assertAlmostEqual(2700.0, mat.density())
self.assertEqual(mat, mat1)
self.assertEqual(mat1, mat2)
self.assertEqual(mat2, mat3)

# Test constructor
mat4 = ignition.math.Material(mat3)
self.assertEqual(mat2, mat4)

mat5 = ignition.math.Material(mat4)
self.assertEqual(mat2, mat5)

mat = ignition.math.Material("Notfoundium")
self.assertGreater(0.0, mat.density())
self.assertEqual(ignition.math.MaterialType_UNKNOWN_MATERIAL,
mat.type())
self.assertFalse(mat.name())

material = ignition.math.Material()
material.set_to_nearest_density(19300.0)
self.assertEqual(ignition.math.MaterialType_TUNGSTEN, material.type())
self.assertAlmostEqual(19300.0, material.density())

material = ignition.math.Material()
material.set_to_nearest_density(1001001.001, 1e-3)
self.assertEqual(ignition.math.MaterialType_UNKNOWN_MATERIAL,
material.type())
self.assertGreater(0.0, material.density())
material = ignition.math.Material()
material.set_to_nearest_density(1001001.001)
self.assertEqual(ignition.math.MaterialType_TUNGSTEN, material.type())
self.assertAlmostEqual(19300, material.density())


if __name__ == '__main__':
unittest.main()
2 changes: 2 additions & 0 deletions src/python/python.i
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
%include SignalStats.i
%include Spline.i
%include Temperature.i
%include MaterialType.i
%include Material.i
%include Triangle.i
%include Kmeans.i
%include Vector3Stats.i