diff --git a/modules/core/include/visp3/core/vpArray2D.h b/modules/core/include/visp3/core/vpArray2D.h index 8576bc4d61..e2a829cc60 100644 --- a/modules/core/include/visp3/core/vpArray2D.h +++ b/modules/core/include/visp3/core/vpArray2D.h @@ -223,11 +223,23 @@ template 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(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(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 diff --git a/modules/python/test/test_numpy_array.py b/modules/python/test/test_numpy_array.py index f4fd9eff71..010176fb50 100644 --- a/modules/python/test/test_numpy_array.py +++ b/modules/python/test/test_numpy_array.py @@ -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()