-
Notifications
You must be signed in to change notification settings - Fork 77
Add pybindings for VolumetricGridLookupField #657
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
Open
gokulp01
wants to merge
6
commits into
gazebosim:gz-math8
Choose a base branch
from
gokulp01:gz-math8
base: gz-math8
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
20ef6b8
Update CMakeLists for VolumetricGridLookupField Python bindings
gokulp01 b3dbeb8
Add pybindings for InterpolationPoint
gokulp01 e5e45cc
Add pybindings for VolumetricGridLookupField
gokulp01 2a6cf37
Add test for VolumetricGridLookupField pybindings
gokulp01 4f993ce
Address review suggestions
gokulp01 43fcfe0
Update src/python_pybind11/src/VolumetricGridLookupField.hh
gokulp01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,32 @@ | ||
/* | ||
* Copyright (C) 2025 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 "InterpolationPoint.hh" | ||
#include <string> | ||
|
||
namespace gz { | ||
namespace math { | ||
namespace python { | ||
|
||
void defineMathInterpolationPoint3D(py::module &m, const std::string &typestr) { | ||
helpDefineMathInterpolationPoint3D<double>(m, typestr + "d"); | ||
helpDefineMathInterpolationPoint3D<float>(m, typestr + "f"); | ||
} | ||
|
||
} // namespace python | ||
} // namespace math | ||
} // namespace gz |
This file contains hidden or 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,81 @@ | ||
/* | ||
* Copyright (C) 2025 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. | ||
* | ||
*/ | ||
|
||
#ifndef GZ_MATH_PYTHON__INTERPOLATION_POINT_HH_ | ||
#define GZ_MATH_PYTHON__INTERPOLATION_POINT_HH_ | ||
|
||
#include <sstream> | ||
#include <string> | ||
|
||
#include <pybind11/operators.h> | ||
#include <pybind11/pybind11.h> | ||
#include <pybind11/stl.h> | ||
|
||
#include <gz/math/Vector3.hh> | ||
#include <gz/math/detail/InterpolationPoint.hh> | ||
#include <optional> | ||
|
||
namespace py = pybind11; | ||
using namespace pybind11::literals; | ||
|
||
namespace gz { | ||
namespace math { | ||
namespace python { | ||
/// define a pybind11 wrapper for gz::math::InterpolationPoint3D | ||
/** | ||
* \param[in] module a pybind11 module to add the definition to | ||
*/ | ||
template <typename T> | ||
void helpDefineMathInterpolationPoint3D(py::module &m, | ||
const std::string &typestr) { | ||
using Class = gz::math::InterpolationPoint3D<T>; | ||
using Vector3Type = gz::math::Vector3<T>; | ||
|
||
auto toString = [](const Class &si) { | ||
std::stringstream stream; | ||
stream << "InterpolationPoint3D(position=" << si.position; | ||
if (si.index.has_value()) { | ||
stream << ", index=" << si.index.value(); | ||
} else { | ||
stream << ", index=None"; | ||
} | ||
stream << ")"; | ||
return stream.str(); | ||
}; | ||
|
||
std::string pyclass_name = typestr; | ||
py::class_<Class>(m, pyclass_name.c_str(), py::dynamic_attr()) | ||
.def(py::init<>()) | ||
.def(py::init<Vector3Type, std::optional<std::size_t>>(), | ||
py::arg("position"), py::arg("index") = std::nullopt) | ||
.def_readwrite("position", &Class::position) | ||
.def_readwrite("index", &Class::index) | ||
.def("__str__", toString) | ||
.def("__repr__", toString); | ||
} | ||
|
||
/// define a pybind11 wrapper for gz::math::InterpolationPoint3D | ||
/** | ||
* \param[in] module a pybind11 module to add the definition to | ||
*/ | ||
void defineMathInterpolationPoint3D(py::module &m, const std::string &typestr); | ||
|
||
} // namespace python | ||
} // namespace math | ||
} // namespace gz | ||
|
||
#endif // GZ_MATH_PYTHON__INTERPOLATION_POINT_HH_ |
This file contains hidden or 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,35 @@ | ||
/* | ||
* Copyright (C) 2025 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 "VolumetricGridLookupField.hh" | ||
#include <string> | ||
|
||
namespace gz { | ||
namespace math { | ||
namespace python { | ||
|
||
void defineMathVolumetricGridLookupField(py::module &m, | ||
const std::string &typestr) { | ||
// Define for double type | ||
helpDefineMathVolumetricGridLookupField<double>(m, typestr + "d"); | ||
// Define for float type | ||
helpDefineMathVolumetricGridLookupField<float>(m, typestr + "f"); | ||
} | ||
|
||
} // namespace python | ||
} // namespace math | ||
} // namespace gz |
This file contains hidden or 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,93 @@ | ||
/* | ||
* Copyright (C) 2025 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. | ||
* | ||
*/ | ||
|
||
#ifndef GZ_MATH_PYTHON__VOLUMETRIC_GRID_LOOKUP_FIELD_HH_ | ||
#define GZ_MATH_PYTHON__VOLUMETRIC_GRID_LOOKUP_FIELD_HH_ | ||
|
||
#include <sstream> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include <pybind11/operators.h> | ||
#include <pybind11/pybind11.h> | ||
#include <pybind11/stl.h> | ||
|
||
#include <gz/math/Vector3.hh> | ||
#include <gz/math/VolumetricGridLookupField.hh> | ||
#include <gz/math/detail/InterpolationPoint.hh> | ||
|
||
namespace py = pybind11; | ||
using namespace pybind11::literals; | ||
|
||
namespace gz { | ||
namespace math { | ||
namespace python { | ||
/// define a pybind11 wrapper for a gz::math::VolumetricGridLookupField | ||
/** | ||
* \param[in] module a pybind11 module to add the definition to | ||
*/ | ||
template <typename T, typename I = std::size_t> | ||
void helpDefineMathVolumetricGridLookupField(py::module &m, | ||
const std::string &typestr) { | ||
using Class = gz::math::VolumetricGridLookupField<T, I>; | ||
using Vector3Type = gz::math::Vector3<T>; | ||
|
||
auto toString = [](const Class &si) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
std::stringstream stream; | ||
stream << si; | ||
return stream.str(); | ||
}; | ||
|
||
std::string pyclass_name = typestr; | ||
py::class_<Class>(m, pyclass_name.c_str(), py::buffer_protocol(), | ||
py::dynamic_attr()) | ||
.def(py::init<const std::vector<Vector3Type> &>()) | ||
.def(py::init<const std::vector<Vector3Type> &, const std::vector<I> &>()) | ||
.def( | ||
"get_interpolators", | ||
[](const Class &self, const Vector3Type &pt, double xTol, double yTol, | ||
double zTol) { | ||
return self.GetInterpolators(pt, xTol, yTol, zTol); | ||
}, | ||
"Get interpolators for a given point", py::arg("point"), | ||
py::arg("x_tol") = 1e-6, py::arg("y_tol") = 1e-6, | ||
py::arg("z_tol") = 1e-6) | ||
.def( | ||
"estimate_value_using_trilinear", | ||
[](const Class &self, const Vector3Type &pt, | ||
const std::vector<double> &values, double defaultVal = 0.0) { | ||
return self.EstimateValueUsingTrilinear(pt, values, defaultVal); | ||
}, | ||
"Estimate value using trilinear interpolation", py::arg("point"), | ||
py::arg("values"), py::arg("default") = 0.0) | ||
.def("bounds", &Class::Bounds, "Get the bounds of the grid field") | ||
.def("__str__", toString) | ||
.def("__repr__", toString); | ||
} | ||
|
||
/// Define a pybind11 wrapper for a gz::math::VolumetricGridLookupField | ||
/** | ||
* \param[in] module a pybind11 module to add the definition to | ||
*/ | ||
void defineMathVolumetricGridLookupField(py::module &m, | ||
const std::string &typestr); | ||
|
||
} // namespace python | ||
} // namespace math | ||
} // namespace gz | ||
|
||
#endif // GZ_MATH_PYTHON__VOLUMETRIC_GRID_LOOKUP_FIELD_HH_ |
This file contains hidden or 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
98 changes: 98 additions & 0 deletions
98
src/python_pybind11/test/VolumetricGridLookupField_TEST.py
This file contains hidden or 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,98 @@ | ||
# Copyright (C) 2025 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 unittest | ||
|
||
from gz.math8 import Vector3d, VolumetricGridLookupFieldd | ||
|
||
|
||
class TestVolumetricGridLookupField(unittest.TestCase): | ||
def test_interpolation_box_eight_points(self): | ||
# Create cloud points as in the C++ test | ||
cloud = [ | ||
Vector3d(0, 0, 0), | ||
Vector3d(0, 0, 1), | ||
Vector3d(0, 1, 0), | ||
Vector3d(0, 1, 1), | ||
Vector3d(1, 0, 0), | ||
Vector3d(1, 0, 1), | ||
Vector3d(1, 1, 0), | ||
Vector3d(1, 1, 1), | ||
] | ||
|
||
# Create lookup field | ||
field = VolumetricGridLookupFieldd(cloud) | ||
|
||
# Test inside point (should return 8 interpolators??) | ||
pos = Vector3d(0.5, 0.5, 0.5) | ||
interpolators = field.get_interpolators(pos) | ||
self.assertEqual(len(interpolators), 8) | ||
|
||
# Test outside point (should return 0 interpolators) | ||
pos = Vector3d(-0.5, -0.5, -0.5) | ||
interpolators = field.get_interpolators(pos) | ||
self.assertEqual(len(interpolators), 0) | ||
|
||
# Test point on plane (should return 4 interpolators) | ||
pos = Vector3d(0.5, 0.5, 0) | ||
interpolators = field.get_interpolators(pos) | ||
self.assertEqual(len(interpolators), 4) | ||
|
||
# Test point on edge (should return 2 interpolators) | ||
pos = Vector3d(0.5, 0, 0) | ||
interpolators = field.get_interpolators(pos) | ||
self.assertEqual(len(interpolators), 2) | ||
|
||
def test_trilinear_interpolation(self): | ||
# Create cloud points | ||
cloud = [ | ||
Vector3d(0, 0, 0), | ||
Vector3d(0, 0, 1), | ||
Vector3d(0, 1, 0), | ||
Vector3d(0, 1, 1), | ||
Vector3d(1, 0, 0), | ||
Vector3d(1, 0, 1), | ||
Vector3d(1, 1, 0), | ||
Vector3d(1, 1, 1), | ||
] | ||
|
||
values = [0, 0, 0, 0, 1, 1, 1, 1] | ||
field = VolumetricGridLookupFieldd(cloud) | ||
|
||
# Test inside point | ||
pos = Vector3d(0.5, 0.5, 0.5) | ||
value = field.estimate_value_using_trilinear(pos, values) | ||
self.assertIsNotNone(value) | ||
self.assertAlmostEqual(value, 0.5, places=3) | ||
|
||
# Test outside point | ||
pos = Vector3d(-0.5, -0.5, -0.5) | ||
value = field.estimate_value_using_trilinear(pos, values) | ||
self.assertIsNone(value) | ||
|
||
# Test point on plane | ||
pos = Vector3d(0, 0.5, 0.5) | ||
value = field.estimate_value_using_trilinear(pos, values) | ||
self.assertIsNotNone(value) | ||
self.assertAlmostEqual(value, 0.0, places=3) | ||
|
||
# Test point on vertex | ||
pos = Vector3d(0, 0, 0) | ||
value = field.estimate_value_using_trilinear(pos, values) | ||
self.assertIsNotNone(value) | ||
self.assertAlmostEqual(value, 0.0, places=3) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.