diff --git a/bindings/python/src/OpenSpaceToolkitMathematicsPy/Geometry/2D/Objects/PointSet.cpp b/bindings/python/src/OpenSpaceToolkitMathematicsPy/Geometry/2D/Objects/PointSet.cpp index 49002eb1..3029d91b 100644 --- a/bindings/python/src/OpenSpaceToolkitMathematicsPy/Geometry/2D/Objects/PointSet.cpp +++ b/bindings/python/src/OpenSpaceToolkitMathematicsPy/Geometry/2D/Objects/PointSet.cpp @@ -40,8 +40,15 @@ inline void OpenSpaceToolkitMathematicsPy_Geometry_2D_Objects_PointSet(pybind11: .def_static("empty", &PointSet::Empty) - // .def("__iter__", boost::python::range(static_cast - // (&PointSet::begin), static_cast (&PointSet::end))) + .def("__len__", &PointSet::getSize) + .def( + "__iter__", + [](const PointSet& aPointSet) + { + return make_iterator(aPointSet.begin(), aPointSet.end()); + }, + keep_alive<0, 1>() + ) // Keep vector alive while iterator is used ; } diff --git a/bindings/python/src/OpenSpaceToolkitMathematicsPy/Geometry/3D/Objects/LineString.cpp b/bindings/python/src/OpenSpaceToolkitMathematicsPy/Geometry/3D/Objects/LineString.cpp index 49d4308e..5d029ab5 100644 --- a/bindings/python/src/OpenSpaceToolkitMathematicsPy/Geometry/3D/Objects/LineString.cpp +++ b/bindings/python/src/OpenSpaceToolkitMathematicsPy/Geometry/3D/Objects/LineString.cpp @@ -42,7 +42,8 @@ inline void OpenSpaceToolkitMathematicsPy_Geometry_3D_Objects_LineString(pybind1 { return aLineString.accessPointAt(anIndex); }, - return_value_policy::reference_internal + return_value_policy::reference_internal, + arg("index") ) .def( "__iter__", diff --git a/bindings/python/src/OpenSpaceToolkitMathematicsPy/Geometry/3D/Objects/PointSet.cpp b/bindings/python/src/OpenSpaceToolkitMathematicsPy/Geometry/3D/Objects/PointSet.cpp index 16186615..5050f2f7 100644 --- a/bindings/python/src/OpenSpaceToolkitMathematicsPy/Geometry/3D/Objects/PointSet.cpp +++ b/bindings/python/src/OpenSpaceToolkitMathematicsPy/Geometry/3D/Objects/PointSet.cpp @@ -33,8 +33,15 @@ inline void OpenSpaceToolkitMathematicsPy_Geometry_3D_Objects_PointSet(pybind11: .def_static("empty", &PointSet::Empty) - // .def_static("__iter__", boost::python::range(static_cast - // (&PointSet::begin), static_cast (&PointSet::end))) + .def("__len__", &PointSet::getSize) + .def( + "__iter__", + [](const PointSet& aPointSet) + { + return make_iterator(aPointSet.begin(), aPointSet.end()); + }, + keep_alive<0, 1>() + ) // Keep vector alive while iterator is used ; } diff --git a/bindings/python/test/geometry/d2/objects/test_linestring.py b/bindings/python/test/geometry/d2/objects/test_linestring.py index ed3d7169..fe75e4f9 100644 --- a/bindings/python/test/geometry/d2/objects/test_linestring.py +++ b/bindings/python/test/geometry/d2/objects/test_linestring.py @@ -1,14 +1,15 @@ # Apache License 2.0 -import pytest +from collections.abc import Iterator, Iterable import numpy as np -from collections.abc import Iterator, Iterable -import ostk.mathematics as mathematics +import pytest from ostk.core.types import String +import ostk.mathematics as mathematics + Object = mathematics.geometry.d2.Object Point = mathematics.geometry.d2.objects.Point @@ -124,20 +125,16 @@ def test_get_point_count_success(self): assert len(linestring) == linestring.get_point_count() assert len(LineString.empty()) == 0 - def test_iter_success(self): + def test_len_success(self): point_1: Point = Point(-1.0, 1.0) point_2: Point = Point(1.0, 1.0) linestring: LineString = LineString([point_1, point_2]) - for point in linestring: - assert isinstance(point, Point) - - assert iter(linestring) is not None - assert isinstance(iter(linestring), Iterator) - assert isinstance(iter(linestring), Iterable) + assert len(linestring) == 2 + assert len(LineString.empty()) == 0 - def test_getitem(self): + def test_getitem_success(self): point_1: Point = Point(-1.0, 1.0) point_2: Point = Point(1.0, 1.0) @@ -159,6 +156,19 @@ def test_getitem(self): assert isinstance(linestring_list[1], Point) assert len(linestring_list) == 2 - # def test_to_string_success (self): + def test_iter_success(self): + point_1: Point = Point(-1.0, 1.0) + point_2: Point = Point(1.0, 1.0) + + linestring: LineString = LineString([point_1, point_2]) + + for point in linestring: + assert isinstance(point, Point) + + assert iter(linestring) is not None + assert isinstance(iter(linestring), Iterator) + assert isinstance(iter(linestring), Iterable) + + # def test_to_string_success(self): - # def test_apply_transformation_success (self): + # def test_apply_transformation_success(self): diff --git a/bindings/python/test/geometry/d2/objects/test_point_set.py b/bindings/python/test/geometry/d2/objects/test_point_set.py index d0d67a86..25f2b578 100644 --- a/bindings/python/test/geometry/d2/objects/test_point_set.py +++ b/bindings/python/test/geometry/d2/objects/test_point_set.py @@ -1,5 +1,7 @@ # Apache License 2.0 +from collections.abc import Iterator, Iterable + import pytest import ostk.mathematics as mathematics @@ -79,10 +81,22 @@ def test_get_size_success(self): def test_distance_to_success_point(self, point_set: PointSet): assert point_set.distance_to(Point(1.0, 2.0)) == 0.0 - # def test_is_near_success (self): + def test_len_success(self, point_set: PointSet): + assert len(point_set) == 2 + assert len(PointSet.empty()) == 0 + + def test_iter_success(self, point_set: PointSet): + for point in point_set: + assert isinstance(point, Point) + + assert iter(point_set) is not None + assert isinstance(iter(point_set), Iterator) + assert isinstance(iter(point_set), Iterable) + + # def test_is_near_success(self): - # def test_get_point_closest_to_success (self): + # def test_get_point_closest_to_success(self): - # def test_to_string_success (self): + # def test_to_string_success(self): - # def test_apply_transformation_success (self): + # def test_apply_transformation_success(self): diff --git a/bindings/python/test/geometry/d3/objects/test_linestring.py b/bindings/python/test/geometry/d3/objects/test_linestring.py index 6b317eba..583c06f0 100644 --- a/bindings/python/test/geometry/d3/objects/test_linestring.py +++ b/bindings/python/test/geometry/d3/objects/test_linestring.py @@ -1,14 +1,15 @@ # Apache License 2.0 -import pytest +from collections.abc import Iterator, Iterable import numpy as np -from collections.abc import Iterator, Iterable -import ostk.mathematics as mathematics +import pytest from ostk.core.types import String +import ostk.mathematics as mathematics + Object = mathematics.geometry.d3.Object Point = mathematics.geometry.d3.objects.Point @@ -16,157 +17,150 @@ Transformation = mathematics.geometry.d3.Transformation -def test_geometry_d3_objects_linestring_constructor(): - point_1: Point = Point(-1.0, 1.0, 1.0) - point_2: Point = Point(1.0, 1.0, -1.0) - point_3: Point = Point(1.0, -1.0, 1.0) - point_4: Point = Point(-1.0, -1.0, 0.0) - - # Construction with Array of Points using python list - linestring: LineString = LineString([point_1, point_2, point_3, point_4]) - - assert linestring is not None - assert isinstance(linestring, LineString) - assert isinstance(linestring, Object) - assert linestring.is_defined() - - # Construction with Array of Points using python tuple - linestring: LineString = LineString((point_1, point_2, point_3, point_4)) - - assert linestring is not None - assert isinstance(linestring, LineString) - assert isinstance(linestring, Object) - assert linestring.is_defined() - - # Construction with Array of Points using python numpy array - linestring: LineString = LineString(np.array((point_1, point_2, point_3, point_4))) - - assert linestring is not None - assert isinstance(linestring, LineString) - assert isinstance(linestring, Object) - assert linestring.is_defined() - - -def test_geometry_d3_objects_linestring_empty(): - linestring: LineString = LineString.empty() +class TestLineString: + def test_constructor_success(self): + point_1: Point = Point(-1.0, 1.0, 1.0) + point_2: Point = Point(1.0, 1.0, -1.0) + point_3: Point = Point(1.0, -1.0, 1.0) + point_4: Point = Point(-1.0, -1.0, 0.0) - assert linestring is not None - assert isinstance(linestring, LineString) - assert isinstance(linestring, Object) - assert linestring.is_defined() is False - assert linestring.is_empty() + # Construction with Array of Points using python list + linestring: LineString = LineString([point_1, point_2, point_3, point_4]) + assert linestring is not None + assert isinstance(linestring, LineString) + assert isinstance(linestring, Object) + assert linestring.is_defined() -def test_geometry_d3_objects_linestring_comparators(): - point_1: Point = Point(-1.0, 1.0, 0.0) - point_2: Point = Point(1.0, 1.0, -1.0) + # Construction with Array of Points using python tuple + linestring: LineString = LineString((point_1, point_2, point_3, point_4)) - linestring_1: LineString = LineString([point_1, point_2]) - linestring_2: LineString = LineString((point_1, point_2)) - linestring_3: LineString = LineString(np.array([point_1, point_2])) - linestring_4: LineString = LineString([point_1]) + assert linestring is not None + assert isinstance(linestring, LineString) + assert isinstance(linestring, Object) + assert linestring.is_defined() - assert linestring_1 == linestring_1 - assert linestring_1 == linestring_2 - assert linestring_1 == linestring_3 - assert linestring_1 != linestring_4 - assert linestring_2 != linestring_4 - assert linestring_3 != linestring_4 + # Construction with Array of Points using python numpy array + linestring: LineString = LineString(np.array((point_1, point_2, point_3, point_4))) + assert linestring is not None + assert isinstance(linestring, LineString) + assert isinstance(linestring, Object) + assert linestring.is_defined() -def test_geometry_d3_objects_linestring_is_near(): - point_1: Point = Point(-1.0, 1.0, 0.0) - point_2: Point = Point(1.0, 1.0, 0.0) + def test_empty_success(self): + linestring: LineString = LineString.empty() - point_3: Point = Point(-0.996, 0.9995, 0.0) - point_4: Point = Point(0.997, 0.996, 0.0) + assert linestring is not None + assert isinstance(linestring, LineString) + assert isinstance(linestring, Object) + assert linestring.is_defined() is False + assert linestring.is_empty() - linestring_1: LineString = LineString([point_1, point_2]) - linestring_2: LineString = LineString([point_3, point_4]) + def test_comparators_success(self): + point_1: Point = Point(-1.0, 1.0, 0.0) + point_2: Point = Point(1.0, 1.0, -1.0) - assert linestring_1.is_near(linestring_1, 10.0) - assert linestring_1.is_near(linestring_1, 0.1) - assert linestring_1.is_near(linestring_1, 1e-9) + linestring_1: LineString = LineString([point_1, point_2]) + linestring_2: LineString = LineString((point_1, point_2)) + linestring_3: LineString = LineString(np.array([point_1, point_2])) + linestring_4: LineString = LineString([point_1]) - assert linestring_2.is_near(linestring_2, 10.0) - assert linestring_2.is_near(linestring_2, 0.1) - assert linestring_2.is_near(linestring_2, 1e-9) + assert linestring_1 == linestring_1 + assert linestring_1 == linestring_2 + assert linestring_1 == linestring_3 + assert linestring_1 != linestring_4 + assert linestring_2 != linestring_4 + assert linestring_3 != linestring_4 - assert linestring_1.is_near(linestring_2, 1e-1) - assert linestring_1.is_near(linestring_2, 1e-2) + def test_is_near_success(self): + point_1: Point = Point(-1.0, 1.0, 0.0) + point_2: Point = Point(1.0, 1.0, 0.0) - assert linestring_2.is_near(linestring_1, 1e-1) - assert linestring_2.is_near(linestring_1, 1e-2) + point_3: Point = Point(-0.996, 0.9995, 0.0) + point_4: Point = Point(0.997, 0.996, 0.0) + linestring_1: LineString = LineString([point_1, point_2]) + linestring_2: LineString = LineString([point_3, point_4]) -def test_geometry_d3_objects_linestring_getters(): - point_1: Point = Point(-1.0, 1.0, 0.0) - point_2: Point = Point(1.0, 1.0, 0.0) + assert linestring_1.is_near(linestring_1, 10.0) + assert linestring_1.is_near(linestring_1, 0.1) + assert linestring_1.is_near(linestring_1, 1e-9) - linestring_1: LineString = LineString([point_1]) - linestring_2: LineString = LineString([point_1, point_2]) + assert linestring_2.is_near(linestring_2, 10.0) + assert linestring_2.is_near(linestring_2, 0.1) + assert linestring_2.is_near(linestring_2, 1e-9) - assert LineString.empty().get_point_count() == 0 - assert linestring_1.get_point_count() == 1 - assert linestring_2.get_point_count() == 2 + assert linestring_1.is_near(linestring_2, 1e-1) + assert linestring_1.is_near(linestring_2, 1e-2) - assert linestring_1.get_point_closest_to(Point(0.0, 0.0, 0.0)) == point_1 + assert linestring_2.is_near(linestring_1, 1e-1) + assert linestring_2.is_near(linestring_1, 1e-2) - assert linestring_2.get_point_closest_to(Point(0.0, 0.0, 0.0)) == point_1 - assert linestring_2.get_point_closest_to(Point(-0.9, 1.0, 0.0)) == point_1 - assert linestring_2.get_point_closest_to(Point(0.9, 1.0, 0.0)) == point_2 + def test_getters_success(self): + point_1: Point = Point(-1.0, 1.0, 0.0) + point_2: Point = Point(1.0, 1.0, 0.0) + linestring_1: LineString = LineString([point_1]) + linestring_2: LineString = LineString([point_1, point_2]) -def test_geometry_d3_objects_linestring_len(): - point_1: Point = Point(-1.0, 1.0, 0.0) - point_2: Point = Point(1.0, 1.0, 0.0) + assert LineString.empty().get_point_count() == 0 + assert linestring_1.get_point_count() == 1 + assert linestring_2.get_point_count() == 2 - linestring: LineString = LineString([point_1, point_2]) + assert linestring_1.get_point_closest_to(Point(0.0, 0.0, 0.0)) == point_1 - assert len(linestring) == 2 - assert len(linestring) == linestring.get_point_count() - assert len(LineString.empty()) == 0 + assert linestring_2.get_point_closest_to(Point(0.0, 0.0, 0.0)) == point_1 + assert linestring_2.get_point_closest_to(Point(-0.9, 1.0, 0.0)) == point_1 + assert linestring_2.get_point_closest_to(Point(0.9, 1.0, 0.0)) == point_2 - linestring: LineString = LineString([point_1, point_1]) - assert len(linestring) == 2 - assert len(linestring) == linestring.get_point_count() + def test_len_success(self): + point_1: Point = Point(-1.0, 1.0, 0.0) + point_2: Point = Point(1.0, 1.0, 0.0) + linestring: LineString = LineString([point_1, point_2]) -def test_geometry_d3_objects_linestring_iter(): - point_1: Point = Point(-1.0, 1.0, 0.0) - point_2: Point = Point(1.0, 1.0, 1.0) + assert len(linestring) == 2 + assert len(linestring) == linestring.get_point_count() + assert len(LineString.empty()) == 0 - linestring: LineString = LineString([point_1, point_2]) + linestring: LineString = LineString([point_1, point_1]) + assert len(linestring) == 2 + assert len(linestring) == linestring.get_point_count() - for point in linestring: - assert isinstance(point, Point) + def test_getitem_success(self): + point_1: Point = Point(-1.0, 1.0, 0.0) + point_2: Point = Point(1.0, 1.0, 1.0) - assert iter(linestring) is not None - assert isinstance(iter(linestring), Iterator) - assert isinstance(iter(linestring), Iterable) + linestring: LineString = LineString([point_1, point_2]) + assert linestring[0] == point_1 + assert linestring[1] == point_2 -def test_geometry_d3_objects_linestring_getitem(): - point_1: Point = Point(-1.0, 1.0, 0.0) - point_2: Point = Point(1.0, 1.0, 1.0) + assert isinstance(linestring[0], Point) + assert isinstance(linestring[1], Point) - linestring: LineString = LineString([point_1, point_2]) + with pytest.raises(RuntimeError): + point = linestring[2] - assert linestring[0] == point_1 - assert linestring[1] == point_2 + linestring_list = list(linestring) - assert isinstance(linestring[0], Point) - assert isinstance(linestring[1], Point) + assert isinstance(linestring_list, list) + assert isinstance(linestring_list[0], Point) + assert isinstance(linestring_list[1], Point) + assert len(linestring_list) == 2 - with pytest.raises(RuntimeError): - point = linestring[2] + def test_iter_success(self): + point_1: Point = Point(-1.0, 1.0, 0.0) + point_2: Point = Point(1.0, 1.0, 1.0) - linestring_list = list(linestring) + linestring: LineString = LineString([point_1, point_2]) - assert isinstance(linestring_list, list) - assert isinstance(linestring_list[0], Point) - assert isinstance(linestring_list[1], Point) - assert len(linestring_list) == 2 + for point in linestring: + assert isinstance(point, Point) + assert iter(linestring) is not None + assert isinstance(iter(linestring), Iterator) + assert isinstance(iter(linestring), Iterable) -# def test_geometry_d3_objects_linestring_apply_transformation (): + # def test_apply_transformation_success(self): diff --git a/bindings/python/test/geometry/d3/objects/test_point_set.py b/bindings/python/test/geometry/d3/objects/test_point_set.py index a00a3ea6..f92ee80d 100644 --- a/bindings/python/test/geometry/d3/objects/test_point_set.py +++ b/bindings/python/test/geometry/d3/objects/test_point_set.py @@ -1,5 +1,7 @@ # Apache License 2.0 +from collections.abc import Iterator, Iterable + import pytest import ostk.mathematics as mathematics @@ -58,19 +60,6 @@ def test_empty_success(self): assert point_set.is_defined() is False assert point_set.is_empty() - def test_comparators_success(self): - point_1: Point = Point(1.0, 2.0, 1.0) - point_2: Point = Point(3.0, 4.0, 1.0) - - point_set_1: PointSet = PointSet([point_1, point_2]) - point_set_2: PointSet = PointSet((point_2, point_1)) - point_set_3: PointSet = PointSet([point_2]) - - assert point_set_1 == point_set_2 - assert point_set_1 == point_set_2 - assert point_set_3 != point_set_1 - assert point_set_2 != point_set_3 - def test_get_size_success(self): point_1: Point = Point(1.0, 2.0, 1.0) point_2: Point = Point(3.0, 4.0, 1.0) @@ -87,13 +76,38 @@ def test_get_size_success(self): assert point_set_4.get_size() == 0 assert point_set_5.get_size() == 1 + def test_comparators_success(self): + point_1: Point = Point(1.0, 2.0, 1.0) + point_2: Point = Point(3.0, 4.0, 1.0) + + point_set_1: PointSet = PointSet([point_1, point_2]) + point_set_2: PointSet = PointSet((point_2, point_1)) + point_set_3: PointSet = PointSet([point_2]) + + assert point_set_1 == point_set_2 + assert point_set_1 == point_set_2 + assert point_set_3 != point_set_1 + assert point_set_2 != point_set_3 + def test_distance_to_success_point(self, point_set: PointSet): assert point_set.distance_to(Point(1.0, 2.0, 3.0)) == 2.0 - # def test_is_near_success (self): + def test_len_success(self, point_set: PointSet): + assert len(point_set) == 2 + assert len(PointSet.empty()) == 0 + + def test_iter_success(self, point_set: PointSet): + for point in point_set: + assert isinstance(point, Point) + + assert iter(point_set) is not None + assert isinstance(iter(point_set), Iterator) + assert isinstance(iter(point_set), Iterable) + + # def test_is_near_success(self): - # def test_get_point_closest_to_success (self): + # def test_get_point_closest_to_success(self): - # def test_to_string_success (self): + # def test_to_string_success(self): - # def test_apply_transformation_success (self): + # def test_apply_transformation_success(self):