def test_transform_many_matrices_one_vector(): for dim in [2, 3, 4]: # no translation: v = np.random.random((dim)) xforms = np.random.random(((1000, dim, dim))) reference = np.array([M.dot(v) for M in xforms]) result = V.transform(v, xforms) assert result.shape == (1000, dim) npt.assert_allclose(reference, result) # with translation, no perspective (e.g. the common 3x4 matrices) v = np.random.random((dim)) xforms = np.random.random(((1000, dim, dim + 1))) reference = np.array([M.dot(V.hom(v)) for M in xforms]) result = V.transform(v, xforms) assert result.shape == (1000, dim) npt.assert_allclose(reference, result) # with translation, no perspective v = np.random.random((dim)) xforms = np.random.random(((1000, dim + 1, dim + 1))) reference = np.array([V.dehom(M.dot(V.hom(v))) for M in xforms]) result = V.transform(v, xforms) assert result.shape == (1000, dim) npt.assert_allclose(reference, result)
def test_transform_one_matrices_one_vector(): for dim in [2, 3, 4]: # no translation: v = np.random.random((dim)) M = np.random.random(((dim, dim))) reference = M.dot(v) result = V.transform(v, M) assert result.shape == v.shape npt.assert_allclose(reference, result) # with translation, no perspective (e.g. the common 3x4 matrices) v = np.random.random((dim)) M = np.random.random(((dim, dim + 1))) reference = M.dot(V.hom(v)) result = V.transform(v, M) assert result.shape == v.shape npt.assert_allclose(reference, result) # with translation, no perspective v = np.random.random((dim)) M = np.random.random(((dim + 1, dim + 1))) reference = V.dehom(M.dot(V.hom(v))) result = V.transform(v, M) assert result.shape == v.shape npt.assert_allclose(reference, result)
def test_transform_one_matrix_many_vectors(): for dim in [2, 3, 4]: # no translation: vectors = np.random.random((1000, dim)) M = np.random.random(((dim, dim))) reference = np.array([M.dot(v) for v in vectors]) result = V.transform(vectors, M) assert result.shape == vectors.shape npt.assert_allclose(reference, result) # with translation, no perspective (e.g. the common 3x4 matrices) vectors = np.random.random((1000, dim)) M = np.random.random(((dim, dim + 1))) reference = np.array([M.dot(V.hom(v)) for v in vectors]) result = V.transform(vectors, M) assert result.shape == vectors.shape npt.assert_allclose(reference, result) # with translation, no perspective vectors = np.random.random((1000, dim)) M = np.random.random(((dim + 1, dim + 1))) reference = np.array([V.dehom(M.dot(V.hom(v))) for v in vectors]) result = V.transform(vectors, M) assert result.shape == vectors.shape npt.assert_allclose(reference, result)
def test_procrustes3d_reflection(): for axis in [0, 1, 2]: N = 100 pts1 = np.random.random((N, 3)) S = np.eye(3) S[axis, axis] = -1 pts2 = V.transform(pts1, S) M = procrustes3d(pts1, pts2) R = M[:3, :3] np.testing.assert_allclose(np.linalg.det(R), 1) assert not np.allclose(R, S) assert V.veclen(V.transform(pts1, M) - pts2).sum() > 0 M2 = procrustes3d(pts1, pts2, allow_reflection=True) np.testing.assert_allclose(M2[:3, :3], S, atol=1.e-9) np.testing.assert_allclose(V.transform(pts1, M2), pts2)
def test_procrustes3d(): # test with 100 random rotations/translations trials = 0 while trials < 100: R = np.linalg.qr(np.random.uniform(-1, 1, size=(3, 3)))[0] print((np.linalg.det(R))) if np.linalg.det(R) < 0: continue t = np.random.uniform(-2, 2, size=3) M = np.eye(4) M[:3, :3] = R M[:3, 3] = t N = np.random.randint(3, 1000) frompts = np.random.random((N, 3)) topts = V.transform(frompts, M) M_est = procrustes3d(frompts, topts) np.testing.assert_allclose(M, M_est) np.testing.assert_allclose(V.transform(frompts, M_est), topts) R = M_est[:3, :3] trials += 1