Skip to content

Commit

Permalink
Fix numpy test failing due to new vpArray2d constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
SamFlt committed Apr 17, 2024
1 parent 80581ff commit 351af1d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
12 changes: 12 additions & 0 deletions modules/core/include/visp3/core/vpArray2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,23 @@ template <class Type> class vpArray2D
resize(r, c, false, false);
}
else if (c == 0) {
if (r != vec.size()) {
throw(vpException(vpException::dimensionError,
"Cannot initialize vpArray(%d, %d) from std::vector(%d). Wrong dimension", r, c, vec.size()));
}
resize(static_cast<unsigned int>(vec.size()), 1, false, false);
}
else if (r == 0) {
if (c != vec.size()) {
throw(vpException(vpException::dimensionError,
"Cannot initialize vpArray(%d, %d) from std::vector(%d). Wrong dimension", r, c, vec.size()));
}
resize(1, static_cast<unsigned int>(vec.size()), false, false);
}
else {
throw(vpException(vpException::dimensionError,
"Cannot initialize vpArray(%d, %d) from std::vector(%d). Wrong dimension", r, c, vec.size()));
}
#if ((__cplusplus >= 201103L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201103L))) // Check if cxx11 or higher
std::copy(vec.begin(), vec.end(), data);
#else
Expand Down
10 changes: 9 additions & 1 deletion modules/python/test/test_numpy_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,17 @@ def test_numpy_constructor():
a = ArrayDouble2D(n_invalid)
n_valid = np.array([[1, 2, 3], [4, 5, 6]])
a = ArrayDouble2D(n_valid)

assert np.all(np.equal(a.numpy(), n_valid))

def test_numpy_constructor_interpreted_as_1d_vector():
n_1d = np.array([1, 2, 3])
with pytest.raises(RuntimeError):
a = ArrayDouble2D(n_1d) # R = 0, c = 0
ar = ArrayDouble2D(n_1d, r=len(n_1d))
ac = ArrayDouble2D(n_1d, c=len(n_1d))



def test_numpy_conversion_and_back():
a = ArrayDouble2D(10, 10, 2.0)
a_np = a.numpy().copy()
Expand Down

0 comments on commit 351af1d

Please sign in to comment.