From 96b2e472e3f62c21817f2113783b5d42240bedc6 Mon Sep 17 00:00:00 2001 From: Divyani Date: Tue, 10 Nov 2026 18:55:35 +0530 Subject: [PATCH] Fix Zonotope copy/move operations and add regression tests (Fixes #511) --- include/convex_bodies/zpolytope.h | 139 +++++++----------------------- test/CMakeLists.txt | 4 + test/zonotope_test.cpp | 110 +++++++++++++++++++++++ 3 files changed, 146 insertions(+), 107 deletions(-) create mode 100644 test/zonotope_test.cpp diff --git a/include/convex_bodies/zpolytope.h b/include/convex_bodies/zpolytope.h index f6f0773db..c86a329bd 100644 --- a/include/convex_bodies/zpolytope.h +++ b/include/convex_bodies/zpolytope.h @@ -15,6 +15,7 @@ #include #include +#include #include #include "lp_oracles/vpolyoracles.h" #include "lp_oracles/zpolyoracles.h" @@ -38,8 +39,12 @@ class Zonotope { NT maxNT = std::numeric_limits::max(); NT minNT = std::numeric_limits::lowest(); - REAL *conv_comb, *row_mem, *row; - int *colno, *colno_mem; + // Scratch buffers handed to the lp_solve based oracles. They are mutable + // because the const oracles (e.g. line_intersect) write into them. Being + // std::vectors, the implicitly generated copy/move operations and the + // destructor are correct (rule of zero). + mutable std::vector conv_comb, row_mem, row; + mutable std::vector colno, colno_mem; MT sigma; MT Q0; @@ -50,11 +55,11 @@ class Zonotope { Zonotope(const unsigned int &dim, const MT &_V, const VT &_b): _d{dim}, V{_V}, b{_b}, - conv_comb{new REAL[V.rows() + 1]}, - row_mem{new REAL[V.rows()]}, - row{new REAL[V.rows() + 1]}, - colno{new int[V.rows() + 1]}, - colno_mem{new int[V.rows()]} + conv_comb(V.rows() + 1), + row_mem(V.rows()), + row(V.rows() + 1), + colno(V.rows() + 1), + colno_mem(V.rows()) { compute_eigenvectors(V.transpose()); } @@ -94,95 +99,15 @@ class Zonotope { } } - conv_comb = new REAL[Pin.size()]; - row_mem = new REAL[V.rows()]; - row = new REAL[V.rows() + 1]; - colno = new int[V.rows() + 1]; - colno_mem = new int[V.rows()]; + conv_comb.resize(Pin.size()); + row_mem.resize(V.rows()); + row.resize(V.rows() + 1); + colno.resize(V.rows() + 1); + colno_mem.resize(V.rows()); compute_eigenvectors(V.transpose()); } - template - void copy_array(T* source, T* result, size_t count) - { - T* tarray; - tarray = new T[count]; - std::copy_n(source, count, tarray); - delete [] result; - result = tarray; - } - - Zonotope& operator=(const Zonotope& other) - { - if (this != &other) { // protect against invalid self-assignment - _d = other._d; - V = other.V; - b = other.b; - T = other.T; - - copy_array(other.conv_comb, conv_comb, V.rows() + 1); - copy_array(other.row_mem, row_mem, V.rows()); - copy_array(other.row, row, V.rows() + 1); - copy_array(other.colno, colno, V.rows() + 1); - copy_array(other.colno_mem, colno_mem, V.rows()); - } - return *this; - } - - Zonotope& operator=(Zonotope&& other) - { - if (this != &other) { // protect against invalid self-assignment - _d = other._d; - V = other.V; - b = other.b; - T = other.T; - - conv_comb = other.conv_comb; other.conv_comb = nullptr; - row_mem = other.row_mem; other.row_mem = nullptr; - row = other.row; other.row = nullptr; - colno = other.colno; colno = nullptr; - colno_mem = other.colno_mem; colno_mem = nullptr; - } - return *this; - } - - - Zonotope(const Zonotope& other) : - _d{other._d}, V{other.V}, b{other.b}, T{other.T}, - conv_comb{new REAL[V.rows() + 1]}, - row_mem{new REAL[V.rows()]}, - row{new REAL[V.rows() + 1]}, - colno{new int[V.rows() + 1]}, - colno_mem{new int[V.rows()]} - { - std::copy_n(other.conv_comb, V.rows() + 1, conv_comb); - std::copy_n(other.row_mem, V.rows(), row_mem); - std::copy_n(other.row, V.rows() + 1, row); - std::copy_n(other.colno, V.rows() + 1, colno); - std::copy_n(other.colno_mem, V.rows(), colno_mem); - } - - Zonotope(Zonotope&& other) : - _d{other._d}, V{other.V}, b{other.b}, T{other.T}, - conv_comb{nullptr}, row_mem{nullptr}, row{nullptr}, - colno{nullptr}, colno_mem{nullptr} - { - conv_comb = other.conv_comb; other.conv_comb = nullptr; - row_mem = other.row_mem; other.row_mem = nullptr; - row = other.row; other.row = nullptr; - colno = other.colno; colno = nullptr; - colno_mem = other.colno_mem; colno_mem = nullptr; - } - - ~Zonotope() { - delete [] conv_comb; - delete [] colno; - delete [] colno_mem; - delete [] row; - delete [] row_mem; - } - void set_interior_point(Point const& r) { _inner_ball.first = r; @@ -358,7 +283,7 @@ class Zonotope { // check if point p belongs to the convex hull of V-Polytope P int is_in(Point const& p, NT tol=NT(0)) const { - if(memLP_Zonotope(V, p, row_mem, colno_mem)) + if(memLP_Zonotope(V, p, row_mem.data(), colno_mem.data())) { return -1; } @@ -377,8 +302,8 @@ class Zonotope { temp.assign(_d,0); temp[i] = 1.0; Point v(_d,temp.begin(), temp.end()); - min_plus = intersect_line_Vpoly(V, center, v, conv_comb, - row, colno, false, true); + min_plus = intersect_line_Vpoly(V, center, v, conv_comb.data(), + row.data(), colno.data(), false, true); if (min_plus < radius) radius = min_plus; } @@ -391,7 +316,7 @@ class Zonotope { // with the Zonotope std::pair line_intersect(Point const& r, Point const& v) const { - return intersect_line_zono(V, r, v, conv_comb, colno); + return intersect_line_zono(V, r, v, conv_comb.data(), colno.data()); } @@ -402,7 +327,7 @@ class Zonotope { VT const& Ar, VT const& Av) const { - return intersect_line_zono(V, r, v, conv_comb, colno); + return intersect_line_zono(V, r, v, conv_comb.data(), colno.data()); } // compute intersection point of ray starting from r and pointing to v @@ -413,7 +338,7 @@ class Zonotope { VT const& Av, NT const& lambda_prev) const { - return intersect_line_zono(V, r, v, conv_comb, colno); + return intersect_line_zono(V, r, v, conv_comb.data(), colno.data()); } std::pair line_positive_intersect(Point const& r, @@ -421,8 +346,8 @@ class Zonotope { VT const& Ar, VT const& Av) const { - return std::pair (intersect_line_Vpoly(V, r, v, conv_comb, - row, colno, + return std::pair (intersect_line_Vpoly(V, r, v, conv_comb.data(), + row.data(), colno.data(), false, true), 1); } @@ -482,7 +407,7 @@ class Zonotope { temp[rand_coord]=1.0; Point v(_d,temp.begin(), temp.end()); - return intersect_line_zono(V, r, v, conv_comb, colno); + return intersect_line_zono(V, r, v, conv_comb.data(), colno.data()); } @@ -577,10 +502,10 @@ class Zonotope { const NT e = 0.0000000001; for (int j = 0; j < num_of_generators(); ++j) { - if (((1.0 - *(conv_comb + j) ) > e || (1.0 - *(conv_comb + j) ) - > e*std::abs(*(conv_comb + j))) && - ((1.0 + *(conv_comb + j) ) > e || (1.0 + *(conv_comb + j) ) - > e*std::abs(*(conv_comb + j)))) + if (((1.0 - conv_comb[j] ) > e || (1.0 - conv_comb[j] ) + > e*std::abs(conv_comb[j])) && + ((1.0 + conv_comb[j] ) > e || (1.0 + conv_comb[j] ) + > e*std::abs(conv_comb[j]))) { Fmat.row(count) = V.row(j); count++; @@ -609,8 +534,8 @@ class Zonotope { MT Fmat(_d-1,_d); const NT e = 0.0000000001; for (int j = 0; j < num_of_generators(); ++j) { - if (((1.0 - *(conv_comb + j) ) > e || (1.0 - *(conv_comb + j) ) > e*std::abs(*(conv_comb + j))) && - ((1.0 + *(conv_comb + j) ) > e || (1.0 + *(conv_comb + j) ) > e*std::abs(*(conv_comb + j)))) { + if (((1.0 - conv_comb[j] ) > e || (1.0 - conv_comb[j] ) > e*std::abs(conv_comb[j])) && + ((1.0 + conv_comb[j] ) > e || (1.0 + conv_comb[j] ) > e*std::abs(conv_comb[j]))) { Fmat.row(count) = V.row(j); count++; } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 16a3867c2..f600cf699 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -258,6 +258,9 @@ add_executable (boundary_oracles_test boundary_oracles_test.cpp $) +add_test(NAME zonotope_copy_move COMMAND zonotope_test) + add_executable (volume_sob_hpolytope volume_sob_hpolytope.cpp $) add_test(NAME volume_sob_hpolytope_cube COMMAND volume_sob_hpolytope -tc=cube) add_test(NAME volume_sob_hpolytope_cross COMMAND volume_sob_hpolytope -tc=cross) @@ -467,6 +470,7 @@ TARGET_LINK_LIBRARIES(volume_cg_vpolytope lp_solve coverage_config) TARGET_LINK_LIBRARIES(volume_cb_hpolytope lp_solve coverage_config) TARGET_LINK_LIBRARIES(volume_cb_vpolytope lp_solve coverage_config) TARGET_LINK_LIBRARIES(volume_cb_zonotopes lp_solve coverage_config) +TARGET_LINK_LIBRARIES(zonotope_test lp_solve coverage_config) TARGET_LINK_LIBRARIES(volume_cb_vpoly_intersection_vpoly lp_solve coverage_config) TARGET_LINK_LIBRARIES(volume_cb_vpoly_intersection_vpoly lp_solve ${MKL_LINK} coverage_config) TARGET_LINK_LIBRARIES(rounding_test lp_solve ${MKL_LINK} coverage_config) diff --git a/test/zonotope_test.cpp b/test/zonotope_test.cpp new file mode 100644 index 000000000..8f3308e64 --- /dev/null +++ b/test/zonotope_test.cpp @@ -0,0 +1,110 @@ +// VolEsti (volume computation and sampling library) + +// Licensed under GNU LGPL.3, see LICENCE file + +// Regression tests for the special member functions (copy/move construction +// and assignment) of Zonotope. Zonotope owns scratch buffers that are passed +// to the lp_solve based oracles, so a copy or a move that leaves them dangling +// or null breaks every later oracle call. Run under AddressSanitizer +// (-fsanitize=address) to also detect use-after-free and leaks. + +#include "doctest.h" + +#include + +#include "cartesian_geom/cartesian_kernel.h" +#include "convex_bodies/zpolytope.h" + +typedef Cartesian Kernel; +typedef Kernel::Point Point; +typedef Zonotope zonotope; +typedef zonotope::MT MT; +typedef zonotope::VT VT; + +// Helper to construct a hypercube zonotope [-1, 1]^d from standard basis vectors +static zonotope make_cube_zonotope(unsigned int d) +{ + MT V = MT::Identity(d, d); + VT b = VT::Ones(d); + return zonotope(d, V, b); +} + +static std::pair shoot_ray(zonotope const& P) +{ + unsigned int d = P.dimension(); + Point origin(d), v(d); + for (unsigned int i = 0; i < d; ++i) { + origin.set_coord(i, 0.0); + v.set_coord(i, i == 0 ? 1.0 : 0.0); + } + return P.line_intersect(origin, v); +} + +static void check_oracles(zonotope const& P) +{ + std::pair res = shoot_ray(P); + CHECK(res.first == doctest::Approx(1.0)); + CHECK(res.second == doctest::Approx(-1.0)); + + unsigned int d = P.dimension(); + Point inside(d), outside(d); + for (unsigned int i = 0; i < d; ++i) { + inside.set_coord(i, 0.0); + outside.set_coord(i, 2.0); + } + CHECK(P.is_in(inside) == -1); + CHECK(P.is_in(outside) == 0); +} + +TEST_CASE("zonotope_copy_constructor") { + zonotope P = make_cube_zonotope(3); + zonotope Q(P); + check_oracles(Q); + check_oracles(P); +} + +TEST_CASE("zonotope_copy_assignment") { + zonotope P = make_cube_zonotope(3); + zonotope Q = make_cube_zonotope(3); + Q = P; + check_oracles(Q); + check_oracles(P); +} + +TEST_CASE("zonotope_move_constructor") { + zonotope P = make_cube_zonotope(3); + zonotope Q(std::move(P)); + check_oracles(Q); +} + +TEST_CASE("zonotope_move_assignment") { + zonotope Q = make_cube_zonotope(3); + Q = make_cube_zonotope(3); + check_oracles(Q); +} + +TEST_CASE("zonotope_default_then_move_assignment") { + // the pattern used throughout the volume tests + zonotope P; + P = make_cube_zonotope(3); + check_oracles(P); +} + +TEST_CASE("zonotope_copy_and_move_preserve_inner_ball") { + zonotope P = make_cube_zonotope(3); + P.set_InnerBall(std::make_pair(Point(3), 0.5)); + + zonotope copy_constructed(P); + CHECK(copy_constructed.InnerBall().second == doctest::Approx(0.5)); + + zonotope copy_assigned; + copy_assigned = P; + CHECK(copy_assigned.InnerBall().second == doctest::Approx(0.5)); + + zonotope move_constructed(std::move(P)); + CHECK(move_constructed.InnerBall().second == doctest::Approx(0.5)); + + zonotope move_assigned; + move_assigned = std::move(move_constructed); + CHECK(move_assigned.InnerBall().second == doctest::Approx(0.5)); +}