-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds python interface to MaterialType and Material. (#234)
Signed-off-by: LolaSegura <[email protected]> * Fixed material Python interface (#248) Signed-off-by: ahcorde <[email protected]> Co-authored-by: Alejandro Hernández Cordero <[email protected]>
- Loading branch information
1 parent
23a24d7
commit f93eeea
Showing
5 changed files
with
219 additions
and
0 deletions.
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 |
---|---|---|
@@ -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); | ||
}; | ||
} | ||
} |
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 |
---|---|---|
@@ -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 | ||
}; | ||
} | ||
} |
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 |
---|---|---|
@@ -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() |
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